/
texture2d.ts
453 lines (378 loc) · 16.6 KB
/
texture2d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* spellchecker: disable */
import { assert, log, logIf, LogLevel } from './auxiliaries';
import { byteSizeOfFormat } from './formatbytesizes';
import { GLsizei2 } from './tuples';
import { Bindable } from './bindable';
import { TexImage2DData } from './gl2facade';
import { Initializable } from './initializable';
import { AbstractObject } from './object';
/* spellchecker: enable */
/**
* Wrapper for an WebGL 2D texture providing size accessors and requiring for bind, unbind, resize, validity, and
* initialization implementations. The texture object is created on initialization and deleted on uninitialization.
* After being initialized, the texture can be resized, reformated, and data can set directly or via fetch:
* ```
* const texture = new Texture2D(context, 'Texture');
* texture.initialize(1, 1, gl.RGB8, gl.RGB, gl.UNSIGNED_BYTE);
* texture.fetch('/img/webgl-operate-logo.png', true);
* ```
*/
export class Texture2D extends AbstractObject<WebGLTexture> implements Bindable {
/**
* Default texture, e.g., used for unbind.
*/
static readonly DEFAULT_TEXTURE = undefined;
static MAX_ANISOTROPY: GLfloat | undefined = undefined;
/** @see {@link width} */
protected _width: GLsizei = 0;
/** @see {@link height} */
protected _height: GLsizei = 0;
/** @see {@link internalFormat} */
protected _internalFormat: GLenum = 0;
/** @see {@link format} */
protected _format: GLenum = 0;
/** @see {@link type} */
protected _type: GLenum = 0;
/**
* Whether or not to generate mip maps (based on minification settings).
* If true, gl.generateMipmap will be called whenever the image data changes.
*/
protected _mipmap = false;
/** @see {@link anisotropy} */
protected _anisotropy: GLfloat | undefined = undefined;
/**
* Create a texture object on the GPU.
* @param width - Initial width of the texture in px.
* @param height - Initial height of the texture in px.
* @param internalFormat - Internal format of the texture object.
* @param format - Format of the texture data even though no data is passed.
* @param type - Data type of the texel data.
*/
protected create(width: GLsizei, height: GLsizei, internalFormat: GLenum,
format: GLenum, type: GLenum): WebGLTexture | undefined {
assert(width > 0 && height > 0, `texture requires valid width and height of greater than zero`);
const gl = this._context.gl;
const gl2facade = this._context.gl2facade;
this._object = gl.createTexture();
this._width = width;
this._height = height;
this._internalFormat = internalFormat;
this._format = format;
this._type = type;
gl.bindTexture(gl.TEXTURE_2D, this._object);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
/* Query max anisotropy. This is done lazy, that is, only for the first texture created. */
if (Texture2D.MAX_ANISOTROPY === undefined) {
if (this._context.supportsTextureFilterAnisotropic) {
const ext = this._context.textureFilterAnisotropic;
Texture2D.MAX_ANISOTROPY = gl.getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
} else {
Texture2D.MAX_ANISOTROPY = 0.0;
}
}
gl2facade.texImage2D(gl.TEXTURE_2D, 0, this._internalFormat,
this._width, this._height, 0, this._format, this._type);
gl.bindTexture(gl.TEXTURE_2D, Texture2D.DEFAULT_TEXTURE);
/* note that gl.isTexture requires the texture to be bound */
this._valid = gl.isTexture(this._object);
this.reallocate();
return this._object;
}
/**
* Delete the texture object on the GPU. This should have the reverse effect of `create`.
*/
protected delete(): void {
assert(this._object instanceof WebGLTexture, `expected WebGLTexture object`);
this._context.gl.deleteTexture(this._object);
this._object = undefined;
this._valid = false;
this._internalFormat = 0;
this._format = 0;
this._type = 0;
this._width = 0;
this._height = 0;
}
protected reallocate(): void {
const gl = this.context.gl;
const gl2facade = this._context.gl2facade;
let bytes: GLsizei = this._width * this._height * byteSizeOfFormat(this.context, this._internalFormat);
// Fix in case of implicit float and half-float texture generation (e.g., in webgl with half_float support).
if (this._type === gl2facade.HALF_FLOAT && this._internalFormat !== gl.RGBA16F) {
bytes *= 2;
} else if (this._type === gl.FLOAT && this._internalFormat !== gl.RGBA16F) {
bytes *= 4;
}
this.context.allocationRegister.reallocate(this._identifier, bytes);
}
/**
* Bind the texture object to a texture unit.
*/
@Initializable.assert_initialized()
bind(unit?: GLenum): void {
const gl = this.context.gl;
if (unit) {
gl.activeTexture(unit);
}
gl.bindTexture(gl.TEXTURE_2D, this._object);
}
/**
* Unbind the texture object from a texture unit.
*/
@Initializable.assert_initialized()
unbind(unit?: GLenum): void {
const gl = this.context.gl;
if (unit) {
gl.activeTexture(unit);
}
gl.bindTexture(gl.TEXTURE_2D, Texture2D.DEFAULT_TEXTURE);
}
/**
* Asynchronous load of an image via URL or data URI.
* @param url - Uniform resource locator string referencing the image that should be loaded (data URI supported).
* @param crossOrigin - Enable cross origin data loading.
* @returns - Promise for handling image load status.
*/
@Initializable.assert_initialized()
fetch(url: string, crossOrigin: boolean = false, flipY: boolean = false): Promise<void> {
const gl = this.context.gl;
return new Promise((resolve, reject) => {
const image = new Image();
image.onerror = () => {
log(LogLevel.Error, `loading image from '${image.src}' failed`);
reject();
};
image.onload = () => {
this.resize(image.width, image.height);
// Flip the image horizontally, since Image has the origin on the top left
// while WebGL has it on the bottom left
if (!flipY) {
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
}
this.data(image);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
resolve();
};
if (crossOrigin) {
image.crossOrigin = 'anonymous';
}
image.src = url;
});
}
/**
* Pass image data to the texture object.
* @param data - Texel data that will be copied into the objects data store.
* @param bind - Allows to skip binding the texture (e.g., when binding is handled outside).
* @param unbind - Allows to skip unbinding the texture (e.g., when binding is handled outside).
*/
@Initializable.assert_initialized()
data(data: TexImage2DData, bind: boolean = true, unbind: boolean = true): void {
const gl = this.context.gl;
const gl2facade = this._context.gl2facade;
if (bind) {
this.bind();
}
gl2facade.texImage2D(gl.TEXTURE_2D, 0, this._internalFormat,
this._width, this._height, 0, this._format, this._type, data);
if (this._anisotropy !== undefined && this._anisotropy > 0.0) {
this.maxAnisotropy(this._anisotropy, false, false);
} else if (this._mipmap) {
this.generateMipMap(false, false);
}
if (unbind) {
this.unbind();
}
this.reallocate();
}
/**
* Sets the texture object's magnification and minification filter. If a mipmap mode is set for minification, will
* be generated automatically whenever the image data changes. The MipMap can be generated manually as well by
* calling generateMipMap (@see {@link generateMipMap}).
* @param mag - Value for the TEXTURE_MAG_FILTER parameter.
* @param min - Value for the TEXTURE_MIN_FILTER parameter.
* @param bind - Allows to skip binding the texture (e.g., when binding is handled outside).
* @param unbind - Allows to skip unbinding the texture (e.g., when binding is handled outside).
*/
@Initializable.assert_initialized()
filter(mag: GLenum, min: GLenum, bind: boolean = true, unbind: boolean = true): void {
const gl = this.context.gl;
if (bind) {
this.bind();
}
logIf(mag === gl.LINEAR_MIPMAP_LINEAR || mag === gl.LINEAR_MIPMAP_NEAREST, LogLevel.Debug,
`magnification does not utilize a MipMap (refer to LINEAR and NEAREST only)`);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, mag);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, min);
this._mipmap = min === gl.LINEAR_MIPMAP_LINEAR || min === gl.LINEAR_MIPMAP_NEAREST;
if (this._mipmap) {
gl.generateMipmap(gl.TEXTURE_2D);
}
if (unbind) {
this.unbind();
}
}
/**
* Sets the texture object's wrapping function for s and t coordinates.
* @param wrap_s - Value for the TEXTURE_WRAP_S parameter, defaulted to CLAMP_TO_EDGE.
* @param wrap_t - Value for the TEXTURE_WRAP_T parameter, defaulted to CLAMP_TO_EDGE.
* @param bind - Allows to skip binding the texture (e.g., when binding is handled outside).
* @param unbind - Allows to skip unbinding the texture (e.g., when binding is handled outside).
*/
@Initializable.assert_initialized()
wrap(wrap_s: GLenum, wrap_t: GLenum, bind: boolean = true, unbind: boolean = true): void {
const gl = this.context.gl;
if (bind) {
this.bind();
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap_s);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrap_t);
if (unbind) {
this.unbind();
}
}
/**
* This can be used to reformat the texture image without creating a new texture object. Please note that this
* resets the texture's image data to undefined. @see {@link data} for setting new image data.
* @param internalFormat - Internal format of the texture object.
* @param format - Format of the texture data even though no data is passed.
* @param type - Data type of the texel data.
* @param bind - Allows to skip binding the texture (e.g., when binding is handled outside).
* @param unbind - Allows to skip unbinding the texture (e.g., when binding is handled outside).
*/
@Initializable.assert_initialized()
reformat(internalFormat: GLenum, format?: GLenum, type?: GLenum,
bind: boolean = true, unbind: boolean = true): void {
if (internalFormat === this._internalFormat
&& (format === undefined || format === this._format)
&& (type === undefined || type === this._type)) {
return;
}
assert(internalFormat !== undefined, `valid internal format expected`);
this._internalFormat = internalFormat;
if (format) {
this._format = format;
}
if (type) {
this._type = type;
}
this.data(undefined, bind, unbind);
}
/**
* This should be used to implement efficient resize the texture.
* @param width - Targeted/new width of the texture in px.
* @param height - Targeted/new height of the texture in px.
* @param bind - Allows to skip binding the texture (e.g., when binding is handled outside).
* @param unbind - Allows to skip unbinding the texture (e.g., when binding is handled outside).
*/
@Initializable.assert_initialized()
resize(width: GLsizei, height: GLsizei, bind: boolean = true, unbind: boolean = true): void {
if (width === this._width && height === this._height) {
return;
}
this._width = width;
this._height = height;
this.data(undefined, bind, unbind);
}
/**
* Generate MipMap for the texture. This is only required when minification filter is set to use
* the MipMap. If the mipmap is generated, it will be automatically regenerated whenever the image data changes
* via one of this classes' methods. Should be called manually when updated image data from outside though.
* @param bind - Allows to skip binding the texture (e.g., when binding is handled outside).
* @param unbind - Allows to skip unbinding the texture (e.g., when binding is handled outside).
*/
generateMipMap(bind: boolean = true, unbind: boolean = true): void {
const gl = this.context.gl;
if (bind) {
this.bind();
}
gl.generateMipmap(gl.TEXTURE_2D);
if (unbind) {
this.unbind();
}
this._mipmap = true;
}
/**
* Sets this textures anisotropy value. If anisotropy is not supported a debug message is logged.
* If anisotropy is supported, the given value is clamped to the maximum supported anisotropy value and setup.
* Note that using as well as changing a texture's anisotropy value requires (re)generating a MipMap and takes only
* effect when minification filtering is setup to use MipMapping.
* @param max - Targeted maximum anisotropy value (will be clamped to [0.0, MAX_ANISOTROPY]).
* @param bind - Allows to skip binding the texture (e.g., when binding is handled outside).
* @param unbind - Allows to skip unbinding the texture (e.g., when binding is handled outside).
* @returns - The anisotropy value that was actually set (undefined if anisotropy is not supported).
*/
maxAnisotropy(max: GLfloat | undefined, bind: boolean = true, unbind: boolean = true): GLfloat | undefined {
if (this._context.supportsTextureFilterAnisotropic === false) {
log(LogLevel.Debug, `setting anisotropy not supported (EXT_texture_filter_anisotropic missing)`);
return undefined;
}
const gl = this.context.gl;
const ext = this._context.textureFilterAnisotropic;
this._anisotropy = max === undefined ? undefined : Math.max(0.0, Math.min(Texture2D.MAX_ANISOTROPY!, max));
logIf(max !== this._anisotropy, LogLevel.Debug,
`value clamped to max supported anisotropy of ${Texture2D.MAX_ANISOTROPY}, given ${max}`);
if (bind) {
this.bind();
}
gl.texParameterf(gl.TEXTURE_2D, ext.TEXTURE_MAX_ANISOTROPY_EXT,
this._anisotropy === undefined ? 0.0 : this._anisotropy);
this.generateMipMap(false, unbind);
return this._anisotropy;
}
/**
* Returns the number of bytes this object approximately allocates on the GPU. The size will be zero when no
* image data was passed to the texture object.
*/
get bytes(): GLsizei {
this.assertInitialized();
return this.context.allocationRegister.allocated(this._identifier);
}
/**
* Cached internal format of the texture for efficient resize. This can only be changed by re-initialization.
*/
get internalFormat(): GLenum {
this.assertInitialized();
return this._internalFormat!;
}
/**
* Cached format of the data provided to the texture object for efficient resize. This is set on initialization and
* might change on data transfers.
*/
get format(): GLenum {
this.assertInitialized();
return this._format!;
}
/**
* Cached type of the data provided to the texture used for efficient resize. This is set on initialization and
* might change on data transfers.
*/
get type(): GLenum {
this.assertInitialized();
return this._type!;
}
/**
* The width of the texture object in px.
*/
get width(): GLsizei {
this.assertInitialized();
return this._width;
}
/**
* The height of the texture object in px.
*/
get height(): GLsizei {
this.assertInitialized();
return this._height;
}
/**
* Convenience getter for the 2-tuple containing width and height.
* @see {@link width}
* @see {@link height}
*/
get size(): GLsizei2 {
this.assertInitialized();
return [this._width, this._height];
}
}