@gjsify/webgl 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/lib/esm/canvas-webgl-widget.js +12 -0
  2. package/lib/esm/conformance/attribs.spec.js +296 -0
  3. package/lib/esm/conformance/buffers.spec.js +203 -0
  4. package/lib/esm/conformance/context.spec.js +302 -0
  5. package/lib/esm/conformance/programs.spec.js +468 -0
  6. package/lib/esm/conformance/rendering-basic.spec.js +136 -0
  7. package/lib/esm/conformance/rendering.spec.js +424 -0
  8. package/lib/esm/conformance/setup.js +36 -0
  9. package/lib/esm/conformance/state.spec.js +348 -0
  10. package/lib/esm/conformance/textures.spec.js +354 -0
  11. package/lib/esm/conformance/uniforms.spec.js +305 -0
  12. package/lib/esm/conformance-test.js +23 -0
  13. package/lib/esm/extensions/ext-color-buffer-float.js +13 -0
  14. package/lib/esm/extensions/ext-color-buffer-half-float.js +13 -0
  15. package/lib/esm/extensions/oes-texture-half-float.js +19 -0
  16. package/lib/esm/index.js +5 -0
  17. package/lib/esm/test-utils.js +65 -0
  18. package/lib/esm/test.js +2 -2
  19. package/lib/esm/webgl-buffer.js +1 -1
  20. package/lib/esm/webgl-context-base.js +3371 -0
  21. package/lib/esm/webgl-framebuffer.js +1 -1
  22. package/lib/esm/webgl-program.js +1 -1
  23. package/lib/esm/webgl-renderbuffer.js +1 -1
  24. package/lib/esm/webgl-rendering-context.js +95 -3253
  25. package/lib/esm/webgl-shader.js +2 -1
  26. package/lib/esm/webgl-texture.js +1 -1
  27. package/lib/esm/{index.spec.js → webgl1.spec.js} +2 -2
  28. package/lib/esm/webgl2-rendering-context.js +617 -27
  29. package/lib/esm/webgl2.spec.js +573 -1
  30. package/lib/types/conformance/setup.d.ts +14 -0
  31. package/lib/types/extensions/ext-blend-minmax.d.ts +2 -2
  32. package/lib/types/extensions/ext-color-buffer-float.d.ts +4 -0
  33. package/lib/types/extensions/ext-color-buffer-half-float.d.ts +4 -0
  34. package/lib/types/extensions/ext-texture-filter-anisotropic.d.ts +2 -2
  35. package/lib/types/extensions/oes-element-index-unit.d.ts +2 -2
  36. package/lib/types/extensions/oes-standard-derivatives.d.ts +2 -2
  37. package/lib/types/extensions/oes-texture-float-linear.d.ts +2 -2
  38. package/lib/types/extensions/oes-texture-float.d.ts +2 -2
  39. package/lib/types/extensions/oes-texture-half-float.d.ts +5 -0
  40. package/lib/types/extensions/stackgl-destroy-context.d.ts +3 -3
  41. package/lib/types/extensions/stackgl-resize-drawing-buffer.d.ts +3 -3
  42. package/lib/types/index.d.ts +1 -0
  43. package/lib/types/test-utils.d.ts +24 -0
  44. package/lib/types/types/extension.d.ts +2 -2
  45. package/lib/types/utils.d.ts +9 -10
  46. package/lib/types/webgl-buffer.d.ts +3 -3
  47. package/lib/types/webgl-context-base.d.ts +267 -0
  48. package/lib/types/webgl-framebuffer.d.ts +3 -3
  49. package/lib/types/webgl-program.d.ts +3 -3
  50. package/lib/types/webgl-renderbuffer.d.ts +3 -3
  51. package/lib/types/webgl-rendering-context.d.ts +5 -250
  52. package/lib/types/webgl-shader.d.ts +4 -3
  53. package/lib/types/webgl-texture-unit.d.ts +3 -3
  54. package/lib/types/webgl-texture.d.ts +3 -3
  55. package/lib/types/webgl-vertex-attribute.d.ts +5 -5
  56. package/lib/types/webgl2-rendering-context.d.ts +95 -6
  57. package/package.json +13 -11
  58. package/prebuilds/linux-x86_64/Gwebgl-0.1.typelib +0 -0
  59. package/prebuilds/linux-x86_64/libgwebgl.so +0 -0
@@ -0,0 +1,3371 @@
1
+ import "@girs/gdkpixbuf-2.0";
2
+ import * as bits from "bit-twiddle";
3
+ import tokenize from "glsl-tokenizer/string";
4
+ import Gwebgl from "@girs/gwebgl-0.1";
5
+ import GdkPixbuf from "gi://GdkPixbuf?version=2.0";
6
+ import { WebGLContextAttributes } from "./webgl-context-attributes.js";
7
+ import {
8
+ extractImageData,
9
+ checkObject,
10
+ checkFormat,
11
+ checkUniform,
12
+ convertPixels,
13
+ validCubeTarget,
14
+ formatSize,
15
+ isTypedArray,
16
+ arrayToUint8Array,
17
+ flag,
18
+ listToArray,
19
+ isValidString,
20
+ uniformTypeSize,
21
+ vertexCount,
22
+ typeSize,
23
+ Uint8ArrayToVariant
24
+ } from "./utils.js";
25
+ import { getOESElementIndexUint } from "./extensions/oes-element-index-unit.js";
26
+ import { getOESStandardDerivatives } from "./extensions/oes-standard-derivatives.js";
27
+ import { getOESTextureFloat } from "./extensions/oes-texture-float.js";
28
+ import { getOESTextureFloatLinear } from "./extensions/oes-texture-float-linear.js";
29
+ import { getSTACKGLDestroyContext } from "./extensions/stackgl-destroy-context.js";
30
+ import { getSTACKGLResizeDrawingBuffer } from "./extensions/stackgl-resize-drawing-buffer.js";
31
+ import { getEXTBlendMinMax } from "./extensions/ext-blend-minmax.js";
32
+ import { getEXTColorBufferFloat } from "./extensions/ext-color-buffer-float.js";
33
+ import { getEXTColorBufferHalfFloat } from "./extensions/ext-color-buffer-half-float.js";
34
+ import { getEXTTextureFilterAnisotropic } from "./extensions/ext-texture-filter-anisotropic.js";
35
+ import { getOESTextureHalfFloat } from "./extensions/oes-texture-half-float.js";
36
+ import { WebGLActiveInfo } from "./webgl-active-info.js";
37
+ import { WebGLFramebuffer } from "./webgl-framebuffer.js";
38
+ import { WebGLBuffer } from "./webgl-buffer.js";
39
+ import { WebGLDrawingBufferWrapper } from "./webgl-drawing-buffer-wrapper.js";
40
+ import { WebGLProgram } from "./webgl-program.js";
41
+ import { WebGLRenderbuffer } from "./webgl-renderbuffer.js";
42
+ import { WebGLShader } from "./webgl-shader.js";
43
+ import { WebGLShaderPrecisionFormat } from "./webgl-shader-precision-format.js";
44
+ import { WebGLTextureUnit } from "./webgl-texture-unit.js";
45
+ import { WebGLTexture } from "./webgl-texture.js";
46
+ import { WebGLUniformLocation } from "./webgl-uniform-location.js";
47
+ import { WebGLVertexArrayObjectState, WebGLVertexArrayGlobalState } from "./webgl-vertex-attribute.js";
48
+ import { warnNotImplemented } from "@gjsify/utils";
49
+ const VERSION = "0.0.1";
50
+ let CONTEXT_COUNTER = 0;
51
+ const MAX_UNIFORM_LENGTH = 256;
52
+ const MAX_ATTRIBUTE_LENGTH = 256;
53
+ const availableExtensions = {
54
+ // angle_instanced_arrays: getANGLEInstancedArrays,
55
+ oes_element_index_uint: getOESElementIndexUint,
56
+ oes_texture_float: getOESTextureFloat,
57
+ oes_texture_float_linear: getOESTextureFloatLinear,
58
+ oes_standard_derivatives: getOESStandardDerivatives,
59
+ // oes_vertex_array_object: getOESVertexArrayObject,
60
+ stackgl_destroy_context: getSTACKGLDestroyContext,
61
+ stackgl_resize_drawingbuffer: getSTACKGLResizeDrawingBuffer,
62
+ // webgl_draw_buffers: getWebGLDrawBuffers,
63
+ ext_blend_minmax: getEXTBlendMinMax,
64
+ ext_color_buffer_float: getEXTColorBufferFloat,
65
+ ext_color_buffer_half_float: getEXTColorBufferHalfFloat,
66
+ ext_texture_filter_anisotropic: getEXTTextureFilterAnisotropic,
67
+ oes_texture_half_float: getOESTextureHalfFloat
68
+ };
69
+ class WebGLContextBase {
70
+ constructor(canvas, options = {}) {
71
+ this.unpackColorSpace = "srgb";
72
+ this.RGBA8 = 32856;
73
+ this.DEFAULT_ATTACHMENTS = [];
74
+ this.DEFAULT_COLOR_ATTACHMENTS = [];
75
+ /** context counter */
76
+ this._ = 0;
77
+ this._extensions = {};
78
+ this._programs = {};
79
+ this._shaders = {};
80
+ this._textures = {};
81
+ this._framebuffers = {};
82
+ this._renderbuffers = {};
83
+ this._buffers = {};
84
+ this._activeProgram = null;
85
+ this._activeFramebuffer = null;
86
+ this._activeRenderbuffer = null;
87
+ this._checkStencil = false;
88
+ this._stencilState = true;
89
+ this._activeTextureUnit = 0;
90
+ this._errorStack = [];
91
+ // Store limits
92
+ this._maxTextureSize = 0;
93
+ this._maxTextureLevel = 0;
94
+ this._maxCubeMapSize = 0;
95
+ this._maxCubeMapLevel = 0;
96
+ // Unpack alignment
97
+ this._unpackAlignment = 4;
98
+ this._packAlignment = 4;
99
+ this._unpackFlipY = false;
100
+ // Viewport and scissor — tracked in JS to avoid crashing native getParameterx for array returns
101
+ this._viewport = new Int32Array([0, 0, 0, 0]);
102
+ this._scissorBox = new Int32Array([0, 0, 0, 0]);
103
+ // GTK's own FBO ID (not FBO 0). GtkGLArea renders into a custom FBO, not the
104
+ // default surface FBO. Captured once at _init() time before any rebinding so
105
+ // that bindFramebuffer(target, null) can restore the correct FBO.
106
+ this._gtkFboId = 0;
107
+ this._textureUnits = [];
108
+ this._drawingBuffer = null;
109
+ this.canvas = canvas;
110
+ this._contextAttributes = new WebGLContextAttributes(
111
+ flag(options, "alpha", true),
112
+ flag(options, "depth", true),
113
+ flag(options, "stencil", false),
114
+ false,
115
+ // flag(options, 'antialias', true),
116
+ flag(options, "premultipliedAlpha", true),
117
+ flag(options, "preserveDrawingBuffer", false),
118
+ flag(options, "preferLowPowerToHighPerformance", false),
119
+ flag(options, "failIfMajorPerformanceCaveat", false)
120
+ );
121
+ this._contextAttributes.premultipliedAlpha = this._contextAttributes.premultipliedAlpha && this._contextAttributes.alpha;
122
+ }
123
+ get drawingBufferHeight() {
124
+ return this.canvas.height || 0;
125
+ }
126
+ get drawingBufferWidth() {
127
+ return this.canvas.width || 0;
128
+ }
129
+ /**
130
+ * Must be called by subclass constructors AFTER setting up the native GL object
131
+ * so that `this._gl` is available for GL-dependent initialization.
132
+ */
133
+ _init() {
134
+ const gtkFboVariant = this._gl.getParameterx(36006);
135
+ this._gtkFboId = gtkFboVariant?.deepUnpack() | 0;
136
+ this._initGLConstants();
137
+ this.DEFAULT_ATTACHMENTS = [
138
+ this.COLOR_ATTACHMENT0,
139
+ this.DEPTH_ATTACHMENT,
140
+ this.STENCIL_ATTACHMENT,
141
+ this.DEPTH_STENCIL_ATTACHMENT
142
+ ];
143
+ this.DEFAULT_COLOR_ATTACHMENTS = [this.COLOR_ATTACHMENT0];
144
+ const options = this._contextAttributes;
145
+ const width = this.drawingBufferWidth || options.width || 0;
146
+ const height = this.drawingBufferHeight || options.height || 0;
147
+ this._ = CONTEXT_COUNTER++;
148
+ const numTextures = this.getParameter(this.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
149
+ this._textureUnits = new Array(numTextures);
150
+ for (let i = 0; i < numTextures; ++i) {
151
+ this._textureUnits[i] = new WebGLTextureUnit(this, i);
152
+ }
153
+ this.activeTexture(this.TEXTURE0);
154
+ this._defaultVertexObjectState = new WebGLVertexArrayObjectState(this);
155
+ this._vertexObjectState = this._defaultVertexObjectState;
156
+ this._vertexGlobalState = new WebGLVertexArrayGlobalState(this);
157
+ this._maxTextureSize = this.getParameter(this.MAX_TEXTURE_SIZE);
158
+ this._maxTextureLevel = bits.log2(bits.nextPow2(this._maxTextureSize));
159
+ this._maxCubeMapSize = this.getParameter(this.MAX_CUBE_MAP_TEXTURE_SIZE);
160
+ this._maxCubeMapLevel = bits.log2(bits.nextPow2(this._maxCubeMapSize));
161
+ this._unpackAlignment = 4;
162
+ this._packAlignment = 4;
163
+ this._unpackFlipY = false;
164
+ this.bindBuffer(this.ARRAY_BUFFER, null);
165
+ this.bindBuffer(this.ELEMENT_ARRAY_BUFFER, null);
166
+ this.bindFramebuffer(this.FRAMEBUFFER, null);
167
+ this.bindRenderbuffer(this.RENDERBUFFER, null);
168
+ this.viewport(0, 0, width, height);
169
+ this.scissor(0, 0, width, height);
170
+ this.clearDepth(1);
171
+ this.clearColor(0, 0, 0, 0);
172
+ this.clearStencil(0);
173
+ this.clear(this.COLOR_BUFFER_BIT | this.DEPTH_BUFFER_BIT | this.STENCIL_BUFFER_BIT);
174
+ this.disable(this.DEPTH_TEST);
175
+ this.disable(this.STENCIL_TEST);
176
+ this.disable(this.BLEND);
177
+ this.disable(this.CULL_FACE);
178
+ this.disable(this.POLYGON_OFFSET_FILL);
179
+ this.disable(this.SCISSOR_TEST);
180
+ this._gl.colorMask(true, true, true, true);
181
+ }
182
+ _initGLConstants() {
183
+ const giBaseClass = new Gwebgl.WebGLRenderingContextBase();
184
+ const hash = giBaseClass.get_webgl_constants();
185
+ for (const [k, v] of Object.entries(hash)) {
186
+ Object.defineProperty(this, k, { value: v });
187
+ }
188
+ }
189
+ _getGlslVersion(es) {
190
+ return es ? "100" : "120";
191
+ }
192
+ // extWEBGL_draw_buffers() {
193
+ // return this._gl.extWEBGL_draw_buffers().deepUnpack<Record<string, number>>();
194
+ // }
195
+ _checkDimensions(target, width, height, level) {
196
+ if (level < 0 || width < 0 || height < 0) {
197
+ this.setError(this.INVALID_VALUE);
198
+ return false;
199
+ }
200
+ if (target === this.TEXTURE_2D) {
201
+ if (width > this._maxTextureSize || height > this._maxTextureSize || level > this._maxTextureLevel) {
202
+ this.setError(this.INVALID_VALUE);
203
+ return false;
204
+ }
205
+ } else if (this._validCubeTarget(target)) {
206
+ if (width > this._maxCubeMapSize || height > this._maxCubeMapSize || level > this._maxCubeMapLevel) {
207
+ this.setError(this.INVALID_VALUE);
208
+ return false;
209
+ }
210
+ } else {
211
+ this.setError(this.INVALID_ENUM);
212
+ return false;
213
+ }
214
+ return true;
215
+ }
216
+ _checkLocation(location) {
217
+ if (!(location instanceof WebGLUniformLocation)) {
218
+ this.setError(this.INVALID_VALUE);
219
+ return false;
220
+ } else if (location._program._ctx !== this || location._linkCount !== location._program._linkCount) {
221
+ this.setError(this.INVALID_OPERATION);
222
+ return false;
223
+ }
224
+ return true;
225
+ }
226
+ _checkLocationActive(location) {
227
+ if (!location) {
228
+ return false;
229
+ } else if (!this._checkLocation(location)) {
230
+ return false;
231
+ } else if (location._program !== this._activeProgram) {
232
+ this.setError(this.INVALID_OPERATION);
233
+ return false;
234
+ }
235
+ return true;
236
+ }
237
+ _checkOwns(object) {
238
+ return typeof object === "object" && object._ctx === this;
239
+ }
240
+ _checkShaderSource(shader) {
241
+ const source = shader._source;
242
+ const tokens = tokenize(source);
243
+ let errorStatus = false;
244
+ const errorLog = [];
245
+ for (let i = 0; i < tokens.length; ++i) {
246
+ const tok = tokens[i];
247
+ if (!tok) continue;
248
+ switch (tok.type) {
249
+ case "ident":
250
+ if (!this._validGLSLIdentifier(tok.data)) {
251
+ errorStatus = true;
252
+ errorLog.push(tok.line + ":" + tok.column + " invalid identifier - " + tok.data);
253
+ }
254
+ break;
255
+ case "preprocessor": {
256
+ const match = tok.data.match(/^\s*#\s*(.*)$/);
257
+ if (!match || match?.length < 2) {
258
+ break;
259
+ }
260
+ const bodyToks = tokenize(match[1]);
261
+ for (let j = 0; j < bodyToks.length; ++j) {
262
+ const btok = bodyToks[j];
263
+ if (btok.type === "ident" || btok.type === void 0) {
264
+ if (!this._validGLSLIdentifier(btok.data)) {
265
+ errorStatus = true;
266
+ errorLog.push(tok.line + ":" + btok.column + " invalid identifier - " + btok.data);
267
+ }
268
+ }
269
+ }
270
+ break;
271
+ }
272
+ case "keyword":
273
+ switch (tok.data) {
274
+ case "do":
275
+ errorStatus = true;
276
+ errorLog.push(tok.line + ":" + tok.column + " do not supported");
277
+ break;
278
+ }
279
+ break;
280
+ case "builtin":
281
+ switch (tok.data) {
282
+ case "dFdx":
283
+ case "dFdy":
284
+ case "fwidth":
285
+ if (!this._extensions.oes_standard_derivatives && this._getGlslVersion(true) === "100") {
286
+ errorStatus = true;
287
+ errorLog.push(tok.line + ":" + tok.column + " " + tok.data + " not supported");
288
+ }
289
+ break;
290
+ }
291
+ }
292
+ }
293
+ if (errorStatus) {
294
+ shader._compileInfo = errorLog.join("\n");
295
+ }
296
+ return !errorStatus;
297
+ }
298
+ _checkStencilState() {
299
+ if (!this._checkStencil) {
300
+ return this._stencilState;
301
+ }
302
+ this._checkStencil = false;
303
+ this._stencilState = true;
304
+ if (this.getParameter(this.STENCIL_WRITEMASK) !== this.getParameter(this.STENCIL_BACK_WRITEMASK) || this.getParameter(this.STENCIL_VALUE_MASK) !== this.getParameter(this.STENCIL_BACK_VALUE_MASK) || this.getParameter(this.STENCIL_REF) !== this.getParameter(this.STENCIL_BACK_REF)) {
305
+ this.setError(this.INVALID_OPERATION);
306
+ this._stencilState = false;
307
+ }
308
+ return this._stencilState;
309
+ }
310
+ _checkTextureTarget(target) {
311
+ const unit = this._getActiveTextureUnit();
312
+ let tex = null;
313
+ if (target === this.TEXTURE_2D) {
314
+ tex = unit._bind2D;
315
+ } else if (target === this.TEXTURE_CUBE_MAP) {
316
+ tex = unit._bindCube;
317
+ } else {
318
+ this.setError(this.INVALID_ENUM);
319
+ return false;
320
+ }
321
+ if (!tex) {
322
+ this.setError(this.INVALID_OPERATION);
323
+ return false;
324
+ }
325
+ return true;
326
+ }
327
+ _checkWrapper(object, Wrapper) {
328
+ if (!this._checkValid(object, Wrapper)) {
329
+ this.setError(this.INVALID_VALUE);
330
+ return false;
331
+ } else if (!this._checkOwns(object)) {
332
+ this.setError(this.INVALID_OPERATION);
333
+ return false;
334
+ }
335
+ return true;
336
+ }
337
+ _checkValid(object, Type) {
338
+ return object instanceof Type && object._ !== 0;
339
+ }
340
+ _checkVertexAttribState(maxIndex) {
341
+ const program = this._activeProgram;
342
+ if (!program) {
343
+ this.setError(this.INVALID_OPERATION);
344
+ return false;
345
+ }
346
+ const attribs = this._vertexObjectState._attribs;
347
+ for (let i = 0; i < attribs.length; ++i) {
348
+ const attrib = attribs[i];
349
+ if (attrib._isPointer) {
350
+ const buffer = attrib._pointerBuffer;
351
+ if (!buffer) {
352
+ this.setError(this.INVALID_OPERATION);
353
+ return false;
354
+ }
355
+ if (program._attributes.indexOf(i) >= 0) {
356
+ let maxByte = 0;
357
+ if (attrib._divisor) {
358
+ maxByte = attrib._pointerSize + attrib._pointerOffset;
359
+ } else {
360
+ maxByte = attrib._pointerStride * maxIndex + attrib._pointerSize + attrib._pointerOffset;
361
+ }
362
+ if (maxByte > buffer._size) {
363
+ this.setError(this.INVALID_OPERATION);
364
+ return false;
365
+ }
366
+ }
367
+ }
368
+ }
369
+ return true;
370
+ }
371
+ _checkVertexIndex(index) {
372
+ if (index < 0 || index >= this._vertexObjectState._attribs.length) {
373
+ this.setError(this.INVALID_VALUE);
374
+ return false;
375
+ }
376
+ return true;
377
+ }
378
+ _computePixelSize(type, internalFormat) {
379
+ const pixelSize = formatSize(this, internalFormat);
380
+ if (pixelSize === 0) {
381
+ this.setError(this.INVALID_ENUM);
382
+ return 0;
383
+ }
384
+ switch (type) {
385
+ case this.UNSIGNED_BYTE:
386
+ return pixelSize;
387
+ case this.UNSIGNED_SHORT_5_6_5:
388
+ if (internalFormat !== this.RGB) {
389
+ this.setError(this.INVALID_OPERATION);
390
+ break;
391
+ }
392
+ return 2;
393
+ case this.UNSIGNED_SHORT_4_4_4_4:
394
+ case this.UNSIGNED_SHORT_5_5_5_1:
395
+ if (internalFormat !== this.RGBA) {
396
+ this.setError(this.INVALID_OPERATION);
397
+ break;
398
+ }
399
+ return 2;
400
+ case this.FLOAT:
401
+ return 1;
402
+ }
403
+ this.setError(this.INVALID_ENUM);
404
+ return 0;
405
+ }
406
+ _computeRowStride(width, pixelSize) {
407
+ let rowStride = width * pixelSize;
408
+ if (rowStride % this._unpackAlignment) {
409
+ rowStride += this._unpackAlignment - rowStride % this._unpackAlignment;
410
+ }
411
+ return rowStride;
412
+ }
413
+ _fixupLink(program) {
414
+ if (!this._gl.getProgramParameter(program._, this.LINK_STATUS)) {
415
+ program._linkInfoLog = this._gl.getProgramInfoLog(program._);
416
+ return false;
417
+ }
418
+ const numAttribs = this.getProgramParameter(program, this.ACTIVE_ATTRIBUTES);
419
+ const names = new Array(numAttribs);
420
+ program._attributes.length = numAttribs;
421
+ for (let i = 0; i < numAttribs; ++i) {
422
+ names[i] = this.getActiveAttrib(program, i)?.name;
423
+ program._attributes[i] = this.getAttribLocation(program, names[i]) | 0;
424
+ }
425
+ for (let i = 0; i < names.length; ++i) {
426
+ if (names[i].length > MAX_ATTRIBUTE_LENGTH) {
427
+ program._linkInfoLog = "attribute " + names[i] + " is too long";
428
+ return false;
429
+ }
430
+ }
431
+ for (let i = 0; i < numAttribs; ++i) {
432
+ if (program._attributes[i] < 0) continue;
433
+ this._gl.bindAttribLocation(
434
+ program._ | 0,
435
+ program._attributes[i],
436
+ names[i]
437
+ );
438
+ }
439
+ this._gl.linkProgram(program._ | 0);
440
+ if (!this._gl.getProgramParameter(program._ | 0, this.LINK_STATUS)) {
441
+ program._linkInfoLog = this._gl.getProgramInfoLog(program._);
442
+ return false;
443
+ }
444
+ const numUniforms = this.getProgramParameter(program, this.ACTIVE_UNIFORMS);
445
+ program._uniforms.length = numUniforms;
446
+ for (let i = 0; i < numUniforms; ++i) {
447
+ const info = this.getActiveUniform(program, i);
448
+ if (info) program._uniforms[i] = info;
449
+ }
450
+ for (let i = 0; i < program._uniforms.length; ++i) {
451
+ if (program._uniforms[i].name.length > MAX_UNIFORM_LENGTH) {
452
+ program._linkInfoLog = "uniform " + program._uniforms[i].name + " is too long";
453
+ return false;
454
+ }
455
+ }
456
+ program._linkInfoLog = "";
457
+ return true;
458
+ }
459
+ _framebufferOk() {
460
+ const framebuffer = this._activeFramebuffer;
461
+ if (framebuffer && this._preCheckFramebufferStatus(framebuffer) !== this.FRAMEBUFFER_COMPLETE) {
462
+ this.setError(this.INVALID_FRAMEBUFFER_OPERATION);
463
+ return false;
464
+ }
465
+ return true;
466
+ }
467
+ _getActiveBuffer(target) {
468
+ if (target === this.ARRAY_BUFFER) {
469
+ return this._vertexGlobalState._arrayBufferBinding;
470
+ } else if (target === this.ELEMENT_ARRAY_BUFFER) {
471
+ return this._vertexObjectState._elementArrayBufferBinding;
472
+ }
473
+ return null;
474
+ }
475
+ _getActiveTextureUnit() {
476
+ return this._textureUnits[this._activeTextureUnit];
477
+ }
478
+ _getActiveTexture(target) {
479
+ const activeUnit = this._getActiveTextureUnit();
480
+ if (target === this.TEXTURE_2D) {
481
+ return activeUnit._bind2D;
482
+ } else if (target === this.TEXTURE_CUBE_MAP) {
483
+ return activeUnit._bindCube;
484
+ }
485
+ return null;
486
+ }
487
+ _getAttachments() {
488
+ return this._extensions.webgl_draw_buffers ? this._extensions.webgl_draw_buffers._ALL_ATTACHMENTS : this.DEFAULT_ATTACHMENTS;
489
+ }
490
+ _getColorAttachments() {
491
+ return this._extensions.webgl_draw_buffers ? this._extensions.webgl_draw_buffers._ALL_COLOR_ATTACHMENTS : this.DEFAULT_COLOR_ATTACHMENTS;
492
+ }
493
+ _getParameterDirect(pname) {
494
+ return this._gl.getParameterx(pname)?.deepUnpack();
495
+ }
496
+ _getTexImage(target) {
497
+ const unit = this._getActiveTextureUnit();
498
+ if (target === this.TEXTURE_2D) {
499
+ return unit._bind2D;
500
+ } else if (validCubeTarget(this, target)) {
501
+ return unit._bindCube;
502
+ }
503
+ this.setError(this.INVALID_ENUM);
504
+ return null;
505
+ }
506
+ _preCheckFramebufferStatus(framebuffer) {
507
+ const attachments = framebuffer._attachments;
508
+ const width = [];
509
+ const height = [];
510
+ const depthAttachment = attachments[this.DEPTH_ATTACHMENT];
511
+ const depthStencilAttachment = attachments[this.DEPTH_STENCIL_ATTACHMENT];
512
+ const stencilAttachment = attachments[this.STENCIL_ATTACHMENT];
513
+ if (depthStencilAttachment && (stencilAttachment || depthAttachment) || stencilAttachment && depthAttachment) {
514
+ return this.FRAMEBUFFER_UNSUPPORTED;
515
+ }
516
+ const colorAttachments = this._getColorAttachments();
517
+ let colorAttachmentCount = 0;
518
+ for (const attachmentEnum in attachments) {
519
+ if (attachments[attachmentEnum] && colorAttachments.indexOf(Number(attachmentEnum)) !== -1) {
520
+ colorAttachmentCount++;
521
+ }
522
+ }
523
+ if (colorAttachmentCount === 0) {
524
+ return this.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
525
+ }
526
+ if (depthStencilAttachment instanceof WebGLTexture) {
527
+ return this.FRAMEBUFFER_UNSUPPORTED;
528
+ } else if (depthStencilAttachment instanceof WebGLRenderbuffer) {
529
+ if (depthStencilAttachment._format !== this.DEPTH_STENCIL) {
530
+ return this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
531
+ }
532
+ width.push(depthStencilAttachment._width);
533
+ height.push(depthStencilAttachment._height);
534
+ }
535
+ if (depthAttachment instanceof WebGLTexture) {
536
+ return this.FRAMEBUFFER_UNSUPPORTED;
537
+ } else if (depthAttachment instanceof WebGLRenderbuffer) {
538
+ if (depthAttachment._format !== this.DEPTH_COMPONENT16) {
539
+ return this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
540
+ }
541
+ width.push(depthAttachment._width);
542
+ height.push(depthAttachment._height);
543
+ }
544
+ if (stencilAttachment instanceof WebGLTexture) {
545
+ return this.FRAMEBUFFER_UNSUPPORTED;
546
+ } else if (stencilAttachment instanceof WebGLRenderbuffer) {
547
+ if (stencilAttachment._format !== this.STENCIL_INDEX8) {
548
+ return this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
549
+ }
550
+ width.push(stencilAttachment._width);
551
+ height.push(stencilAttachment._height);
552
+ }
553
+ let colorAttached = false;
554
+ for (let i = 0; i < colorAttachments.length; ++i) {
555
+ const colorAttachment = attachments[colorAttachments[i]];
556
+ if (colorAttachment instanceof WebGLTexture) {
557
+ if (colorAttachment._format !== this.RGBA || !(colorAttachment._type === this.UNSIGNED_BYTE || colorAttachment._type === this.FLOAT)) {
558
+ return this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
559
+ }
560
+ colorAttached = true;
561
+ const level = framebuffer._attachmentLevel[this.COLOR_ATTACHMENT0];
562
+ if (level === null) throw new TypeError("level is null!");
563
+ width.push(colorAttachment._levelWidth[level]);
564
+ height.push(colorAttachment._levelHeight[level]);
565
+ } else if (colorAttachment instanceof WebGLRenderbuffer) {
566
+ const format = colorAttachment._format;
567
+ if (format !== this.RGBA4 && format !== this.RGB565 && format !== this.RGB5_A1) {
568
+ return this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
569
+ }
570
+ colorAttached = true;
571
+ width.push(colorAttachment._width);
572
+ height.push(colorAttachment._height);
573
+ }
574
+ }
575
+ if (!colorAttached && !stencilAttachment && !depthAttachment && !depthStencilAttachment) {
576
+ return this.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
577
+ }
578
+ if (width.length <= 0 || height.length <= 0) {
579
+ return this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
580
+ }
581
+ for (let i = 1; i < width.length; ++i) {
582
+ if (width[i - 1] !== width[i] || height[i - 1] !== height[i]) {
583
+ return this.FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
584
+ }
585
+ }
586
+ if (width[0] === 0 || height[0] === 0) {
587
+ return this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
588
+ }
589
+ framebuffer._width = width[0];
590
+ framebuffer._height = height[0];
591
+ return this.FRAMEBUFFER_COMPLETE;
592
+ }
593
+ _isConstantBlendFunc(factor) {
594
+ return factor === this.CONSTANT_COLOR || factor === this.ONE_MINUS_CONSTANT_COLOR || factor === this.CONSTANT_ALPHA || factor === this.ONE_MINUS_CONSTANT_ALPHA;
595
+ }
596
+ _isObject(object, method, Wrapper) {
597
+ if (!(object === null || object === void 0) && !(object instanceof Wrapper)) {
598
+ throw new TypeError(method + "(" + Wrapper.name + ")");
599
+ }
600
+ if (this._checkValid(object, Wrapper) && this._checkOwns(object)) {
601
+ return true;
602
+ }
603
+ return false;
604
+ }
605
+ _resizeDrawingBuffer(width, height) {
606
+ const prevFramebuffer = this._activeFramebuffer;
607
+ const prevTexture = this._getActiveTexture(this.TEXTURE_2D);
608
+ const prevRenderbuffer = this._activeRenderbuffer;
609
+ const contextAttributes = this._contextAttributes;
610
+ const drawingBuffer = this._drawingBuffer;
611
+ if (drawingBuffer?._framebuffer) {
612
+ this._gl.bindFramebuffer(this.FRAMEBUFFER, drawingBuffer?._framebuffer);
613
+ }
614
+ const attachments = this._getAttachments();
615
+ for (let i = 0; i < attachments.length; ++i) {
616
+ this._gl.framebufferTexture2D(
617
+ this.FRAMEBUFFER,
618
+ attachments[i],
619
+ this.TEXTURE_2D,
620
+ 0,
621
+ 0
622
+ );
623
+ }
624
+ if (drawingBuffer?._color) {
625
+ this._gl.bindTexture(this.TEXTURE_2D, drawingBuffer?._color);
626
+ }
627
+ const colorFormat = contextAttributes.alpha ? this.RGBA : this.RGB;
628
+ this._gl.texImage2D(
629
+ this.TEXTURE_2D,
630
+ 0,
631
+ colorFormat,
632
+ width,
633
+ height,
634
+ 0,
635
+ colorFormat,
636
+ this.UNSIGNED_BYTE,
637
+ Uint8ArrayToVariant(null)
638
+ );
639
+ this._gl.texParameteri(this.TEXTURE_2D, this.TEXTURE_MIN_FILTER, this.NEAREST);
640
+ this._gl.texParameteri(this.TEXTURE_2D, this.TEXTURE_MAG_FILTER, this.NEAREST);
641
+ if (drawingBuffer?._color) {
642
+ this._gl.framebufferTexture2D(
643
+ this.FRAMEBUFFER,
644
+ this.COLOR_ATTACHMENT0,
645
+ this.TEXTURE_2D,
646
+ drawingBuffer?._color,
647
+ 0
648
+ );
649
+ }
650
+ let storage = 0;
651
+ let attachment = 0;
652
+ if (contextAttributes.depth && contextAttributes.stencil) {
653
+ storage = this.DEPTH_STENCIL;
654
+ attachment = this.DEPTH_STENCIL_ATTACHMENT;
655
+ } else if (contextAttributes.depth) {
656
+ storage = 33191;
657
+ attachment = this.DEPTH_ATTACHMENT;
658
+ } else if (contextAttributes.stencil) {
659
+ storage = this.STENCIL_INDEX8;
660
+ attachment = this.STENCIL_ATTACHMENT;
661
+ }
662
+ if (storage) {
663
+ if (drawingBuffer?._depthStencil) {
664
+ this._gl.bindRenderbuffer(
665
+ this.RENDERBUFFER,
666
+ drawingBuffer?._depthStencil
667
+ );
668
+ }
669
+ this._gl.renderbufferStorage(
670
+ this.RENDERBUFFER,
671
+ storage,
672
+ width,
673
+ height
674
+ );
675
+ if (drawingBuffer?._depthStencil) {
676
+ this._gl.framebufferRenderbuffer(
677
+ this.FRAMEBUFFER,
678
+ attachment,
679
+ this.RENDERBUFFER,
680
+ drawingBuffer?._depthStencil
681
+ );
682
+ }
683
+ }
684
+ this.bindFramebuffer(this.FRAMEBUFFER, prevFramebuffer);
685
+ this.bindTexture(this.TEXTURE_2D, prevTexture);
686
+ this.bindRenderbuffer(this.RENDERBUFFER, prevRenderbuffer);
687
+ }
688
+ _restoreError(lastError) {
689
+ const topError = this._errorStack.pop();
690
+ if (topError === this.NO_ERROR) {
691
+ this.setError(lastError);
692
+ } else if (topError) {
693
+ this.setError(topError);
694
+ }
695
+ }
696
+ _saveError() {
697
+ this._errorStack.push(this.getError());
698
+ }
699
+ _switchActiveProgram(active) {
700
+ if (active) {
701
+ active._refCount -= 1;
702
+ active._checkDelete();
703
+ }
704
+ }
705
+ _tryDetachFramebuffer(framebuffer, renderbuffer) {
706
+ if (framebuffer && framebuffer._linked(renderbuffer)) {
707
+ const attachments = this._getAttachments();
708
+ const framebufferAttachments = Object.keys(framebuffer._attachments);
709
+ for (let i = 0; i < framebufferAttachments.length; ++i) {
710
+ if (framebuffer._attachments[attachments[i]] === renderbuffer) {
711
+ this.framebufferTexture2D(
712
+ this.FRAMEBUFFER,
713
+ attachments[i] | 0,
714
+ this.TEXTURE_2D,
715
+ null
716
+ );
717
+ }
718
+ }
719
+ }
720
+ }
721
+ _updateFramebufferAttachments(framebuffer) {
722
+ if (!framebuffer) {
723
+ return;
724
+ }
725
+ const prevStatus = framebuffer._status;
726
+ const attachments = this._getAttachments();
727
+ framebuffer._status = this._preCheckFramebufferStatus(framebuffer);
728
+ if (framebuffer._status !== this.FRAMEBUFFER_COMPLETE) {
729
+ if (prevStatus === this.FRAMEBUFFER_COMPLETE) {
730
+ for (let i = 0; i < attachments.length; ++i) {
731
+ const attachmentEnum = attachments[i];
732
+ this._gl.framebufferTexture2D(
733
+ this.FRAMEBUFFER,
734
+ attachmentEnum,
735
+ framebuffer._attachmentFace[attachmentEnum] || 0,
736
+ 0,
737
+ framebuffer._attachmentLevel[attachmentEnum] || 0
738
+ );
739
+ }
740
+ }
741
+ return;
742
+ }
743
+ for (let i = 0; i < attachments.length; ++i) {
744
+ const attachmentEnum = attachments[i];
745
+ this._gl.framebufferTexture2D(
746
+ this.FRAMEBUFFER,
747
+ attachmentEnum,
748
+ framebuffer._attachmentFace[attachmentEnum] || 0,
749
+ 0,
750
+ framebuffer._attachmentLevel[attachmentEnum] || 0
751
+ );
752
+ }
753
+ for (let i = 0; i < attachments.length; ++i) {
754
+ const attachmentEnum = attachments[i];
755
+ const attachment = framebuffer._attachments[attachmentEnum];
756
+ if (attachment instanceof WebGLTexture) {
757
+ this._gl.framebufferTexture2D(
758
+ this.FRAMEBUFFER,
759
+ attachmentEnum,
760
+ framebuffer._attachmentFace[attachmentEnum] || 0,
761
+ attachment._ | 0,
762
+ framebuffer._attachmentLevel[attachmentEnum] || 0
763
+ );
764
+ } else if (attachment instanceof WebGLRenderbuffer) {
765
+ this._gl.framebufferRenderbuffer(
766
+ this.FRAMEBUFFER,
767
+ attachmentEnum,
768
+ this.RENDERBUFFER,
769
+ attachment._ | 0
770
+ );
771
+ }
772
+ }
773
+ }
774
+ _validBlendFunc(factor) {
775
+ return factor === this.ZERO || factor === this.ONE || factor === this.SRC_COLOR || factor === this.ONE_MINUS_SRC_COLOR || factor === this.DST_COLOR || factor === this.ONE_MINUS_DST_COLOR || factor === this.SRC_ALPHA || factor === this.ONE_MINUS_SRC_ALPHA || factor === this.DST_ALPHA || factor === this.ONE_MINUS_DST_ALPHA || factor === this.SRC_ALPHA_SATURATE || factor === this.CONSTANT_COLOR || factor === this.ONE_MINUS_CONSTANT_COLOR || factor === this.CONSTANT_ALPHA || factor === this.ONE_MINUS_CONSTANT_ALPHA;
776
+ }
777
+ _validBlendMode(mode) {
778
+ return mode === this.FUNC_ADD || mode === this.FUNC_SUBTRACT || mode === this.FUNC_REVERSE_SUBTRACT || this._extensions.ext_blend_minmax && (mode === this._extensions.ext_blend_minmax.MIN_EXT || mode === this._extensions.ext_blend_minmax.MAX_EXT);
779
+ }
780
+ _validCubeTarget(target) {
781
+ return target === this.TEXTURE_CUBE_MAP_POSITIVE_X || target === this.TEXTURE_CUBE_MAP_NEGATIVE_X || target === this.TEXTURE_CUBE_MAP_POSITIVE_Y || target === this.TEXTURE_CUBE_MAP_NEGATIVE_Y || target === this.TEXTURE_CUBE_MAP_POSITIVE_Z || target === this.TEXTURE_CUBE_MAP_NEGATIVE_Z;
782
+ }
783
+ _validFramebufferAttachment(attachment) {
784
+ switch (attachment) {
785
+ case this.DEPTH_ATTACHMENT:
786
+ case this.STENCIL_ATTACHMENT:
787
+ case this.DEPTH_STENCIL_ATTACHMENT:
788
+ case this.COLOR_ATTACHMENT0:
789
+ return true;
790
+ }
791
+ if (this._extensions.webgl_draw_buffers) {
792
+ const { webgl_draw_buffers } = this._extensions;
793
+ return attachment < webgl_draw_buffers.COLOR_ATTACHMENT0_WEBGL + webgl_draw_buffers._maxDrawBuffers;
794
+ }
795
+ return false;
796
+ }
797
+ _validGLSLIdentifier(str) {
798
+ return !(str.indexOf("webgl_") === 0 || str.indexOf("_webgl_") === 0 || str.length > 256);
799
+ }
800
+ _validTextureTarget(target) {
801
+ return target === this.TEXTURE_2D || target === this.TEXTURE_CUBE_MAP;
802
+ }
803
+ _verifyTextureCompleteness(target, pname, param) {
804
+ const unit = this._getActiveTextureUnit();
805
+ let texture = null;
806
+ if (target === this.TEXTURE_2D) {
807
+ texture = unit._bind2D;
808
+ } else if (this._validCubeTarget(target)) {
809
+ texture = unit._bindCube;
810
+ }
811
+ if (this._extensions.oes_texture_float && !this._extensions.oes_texture_float_linear && texture && texture._type === this.FLOAT && (pname === this.TEXTURE_MAG_FILTER || pname === this.TEXTURE_MIN_FILTER) && (param === this.LINEAR || param === this.LINEAR_MIPMAP_NEAREST || param === this.NEAREST_MIPMAP_LINEAR || param === this.LINEAR_MIPMAP_LINEAR)) {
812
+ texture._complete = false;
813
+ this.bindTexture(target, texture);
814
+ return;
815
+ }
816
+ if (texture && texture._complete === false) {
817
+ texture._complete = true;
818
+ this.bindTexture(target, texture);
819
+ }
820
+ }
821
+ _wrapShader(_type, source) {
822
+ const hasVersion = source.startsWith("#version") || source.includes("\n#version");
823
+ let preamble = "";
824
+ if (!this._extensions.oes_standard_derivatives && /#ifdef\s+GL_OES_standard_derivatives/.test(source)) {
825
+ preamble += "#undef GL_OES_standard_derivatives\n";
826
+ }
827
+ if (!this._extensions.webgl_draw_buffers && !hasVersion) {
828
+ preamble += "#define gl_MaxDrawBuffers 1\n";
829
+ }
830
+ if (hasVersion) {
831
+ if (preamble) {
832
+ const newline = source.indexOf("\n");
833
+ if (newline !== -1) {
834
+ source = source.slice(0, newline + 1) + preamble + source.slice(newline + 1);
835
+ } else {
836
+ source = source + "\n" + preamble;
837
+ }
838
+ }
839
+ } else {
840
+ if (this.canvas) {
841
+ const glArea = this.canvas.getGlArea();
842
+ const es = glArea.get_use_es();
843
+ const usesGlsl1Syntax = /\b(attribute|varying)\b/.test(source);
844
+ const version = usesGlsl1Syntax ? es ? "100" : "120" : this._getGlslVersion(es);
845
+ if (version) {
846
+ source = "#version " + version + "\n" + preamble + source;
847
+ } else if (preamble) {
848
+ source = preamble + source;
849
+ }
850
+ } else if (preamble) {
851
+ source = preamble + source;
852
+ }
853
+ }
854
+ return source;
855
+ }
856
+ _allocateDrawingBuffer(width, height) {
857
+ this._drawingBuffer = new WebGLDrawingBufferWrapper(
858
+ this._gl.createFramebuffer(),
859
+ this._gl.createTexture(),
860
+ this._gl.createRenderbuffer()
861
+ );
862
+ this._resizeDrawingBuffer(width, height);
863
+ }
864
+ /**
865
+ * The `WebGLRenderingContext.getContextAttributes()` method returns a `WebGLContextAttributes` object that contains the actual context parameters.
866
+ * Might return `null`, if the context is lost.
867
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getContextAttributes
868
+ * @returns A `WebGLContextAttributes` object that contains the actual context parameters, or `null` if the context is lost.
869
+ */
870
+ getContextAttributes() {
871
+ return this._contextAttributes;
872
+ }
873
+ getExtension(name) {
874
+ const str = name.toLowerCase();
875
+ if (str in this._extensions) {
876
+ return this._extensions[str];
877
+ }
878
+ const ext = availableExtensions[str] ? availableExtensions[str](this) : null;
879
+ if (ext) {
880
+ this._extensions[str] = ext;
881
+ }
882
+ return ext;
883
+ }
884
+ bufferData(target = 0, dataOrSize, usage = 0) {
885
+ let size = 0;
886
+ let data = null;
887
+ if (typeof dataOrSize === "number") {
888
+ size = dataOrSize;
889
+ } else if (typeof dataOrSize === "object") {
890
+ data = dataOrSize;
891
+ }
892
+ if (usage !== this.STREAM_DRAW && usage !== this.STATIC_DRAW && usage !== this.DYNAMIC_DRAW) {
893
+ this.setError(this.INVALID_ENUM);
894
+ return;
895
+ }
896
+ if (target !== this.ARRAY_BUFFER && target !== this.ELEMENT_ARRAY_BUFFER) {
897
+ this.setError(this.INVALID_ENUM);
898
+ return;
899
+ }
900
+ const active = this._getActiveBuffer(target);
901
+ if (!active) {
902
+ this.setError(this.INVALID_OPERATION);
903
+ return;
904
+ }
905
+ if (data) {
906
+ let u8Data = null;
907
+ if (isTypedArray(data) || data instanceof DataView || data instanceof ArrayBuffer) {
908
+ u8Data = arrayToUint8Array(data);
909
+ } else {
910
+ this.setError(this.INVALID_VALUE);
911
+ return;
912
+ }
913
+ this._saveError();
914
+ this._gl.bufferData(
915
+ target,
916
+ Uint8ArrayToVariant(u8Data),
917
+ usage
918
+ );
919
+ const error = this.getError();
920
+ this._restoreError(error);
921
+ if (error !== this.NO_ERROR) {
922
+ return;
923
+ }
924
+ active._size = u8Data.length;
925
+ if (target === this.ELEMENT_ARRAY_BUFFER) {
926
+ active._elements = new Uint8Array(u8Data);
927
+ }
928
+ } else if (typeof dataOrSize === "number") {
929
+ if (size < 0) {
930
+ this.setError(this.INVALID_VALUE);
931
+ return;
932
+ }
933
+ this._saveError();
934
+ this._gl.bufferDataSizeOnly(
935
+ target,
936
+ size,
937
+ usage
938
+ );
939
+ const error = this.getError();
940
+ this._restoreError(error);
941
+ if (error !== this.NO_ERROR) {
942
+ return;
943
+ }
944
+ active._size = size;
945
+ if (target === this.ELEMENT_ARRAY_BUFFER) {
946
+ active._elements = new Uint8Array(size);
947
+ }
948
+ } else {
949
+ this.setError(this.INVALID_VALUE);
950
+ }
951
+ }
952
+ bufferSubData(target = 0, offset = 0, data) {
953
+ if (target !== this.ARRAY_BUFFER && target !== this.ELEMENT_ARRAY_BUFFER) {
954
+ this.setError(this.INVALID_ENUM);
955
+ return;
956
+ }
957
+ if (data === null || typeof data !== "object") {
958
+ throw new TypeError("bufferSubData: data must be a BufferSource");
959
+ }
960
+ const active = this._getActiveBuffer(target);
961
+ if (!active) {
962
+ this.setError(this.INVALID_OPERATION);
963
+ return;
964
+ }
965
+ if (offset < 0 || offset >= active._size) {
966
+ this.setError(this.INVALID_VALUE);
967
+ return;
968
+ }
969
+ let u8Data = null;
970
+ if (isTypedArray(data) || data instanceof DataView || data instanceof ArrayBuffer) {
971
+ u8Data = arrayToUint8Array(data);
972
+ } else {
973
+ this.setError(this.INVALID_VALUE);
974
+ return;
975
+ }
976
+ if (offset + u8Data.length > active._size) {
977
+ this.setError(this.INVALID_VALUE);
978
+ return;
979
+ }
980
+ if (target === this.ELEMENT_ARRAY_BUFFER) {
981
+ active._elements.set(u8Data, offset);
982
+ }
983
+ this._gl.bufferSubData(target, offset, Uint8ArrayToVariant(u8Data));
984
+ }
985
+ compressedTexImage2D(target, level, internalFormat, width, height, border, data) {
986
+ return this._gl.compressedTexImage2D(target, level, internalFormat, width, height, border, Uint8ArrayToVariant(arrayToUint8Array(data)));
987
+ }
988
+ compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, data) {
989
+ return this._gl.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, Uint8ArrayToVariant(arrayToUint8Array(data)));
990
+ }
991
+ readPixels(x = 0, y = 0, width = 0, height = 0, format = 0, type = 0, pixels) {
992
+ if (!pixels) return;
993
+ if (!(this._extensions.oes_texture_float && type === this.FLOAT && format === this.RGBA)) {
994
+ if (format === this.RGB || format === this.ALPHA || type !== this.UNSIGNED_BYTE) {
995
+ this.setError(this.INVALID_OPERATION);
996
+ return;
997
+ } else if (format !== this.RGBA) {
998
+ this.setError(this.INVALID_ENUM);
999
+ return;
1000
+ } else if (width < 0 || height < 0 || !(pixels instanceof Uint8Array)) {
1001
+ this.setError(this.INVALID_VALUE);
1002
+ return;
1003
+ }
1004
+ }
1005
+ if (!this._framebufferOk()) {
1006
+ console.error("framebuffer is not okay!");
1007
+ return;
1008
+ }
1009
+ let rowStride = width * 4;
1010
+ if (rowStride % this._packAlignment !== 0) {
1011
+ rowStride += this._packAlignment - rowStride % this._packAlignment;
1012
+ }
1013
+ const imageSize = rowStride * (height - 1) + width * 4;
1014
+ if (imageSize <= 0) {
1015
+ return;
1016
+ }
1017
+ const pixelsLength = pixels.length || pixels.byteLength || 0;
1018
+ if (pixelsLength < imageSize) {
1019
+ this.setError(this.INVALID_VALUE);
1020
+ return;
1021
+ }
1022
+ let viewWidth = this.drawingBufferWidth;
1023
+ let viewHeight = this.drawingBufferHeight;
1024
+ if (this._activeFramebuffer) {
1025
+ viewWidth = this._activeFramebuffer._width;
1026
+ viewHeight = this._activeFramebuffer._height;
1027
+ }
1028
+ const pixelData = arrayToUint8Array(pixels);
1029
+ if (x >= viewWidth || x + width <= 0 || y >= viewHeight || y + height <= 0) {
1030
+ for (let i = 0; i < pixelData.length; ++i) {
1031
+ pixelData[i] = 0;
1032
+ }
1033
+ } else if (x < 0 || x + width > viewWidth || y < 0 || y + height > viewHeight) {
1034
+ for (let i = 0; i < pixelData.length; ++i) {
1035
+ pixelData[i] = 0;
1036
+ }
1037
+ let nx = x;
1038
+ let nWidth = width;
1039
+ if (x < 0) {
1040
+ nWidth += x;
1041
+ nx = 0;
1042
+ }
1043
+ if (nx + width > viewWidth) {
1044
+ nWidth = viewWidth - nx;
1045
+ }
1046
+ let ny = y;
1047
+ let nHeight = height;
1048
+ if (y < 0) {
1049
+ nHeight += y;
1050
+ ny = 0;
1051
+ }
1052
+ if (ny + height > viewHeight) {
1053
+ nHeight = viewHeight - ny;
1054
+ }
1055
+ let nRowStride = nWidth * 4;
1056
+ if (nRowStride % this._packAlignment !== 0) {
1057
+ nRowStride += this._packAlignment - nRowStride % this._packAlignment;
1058
+ }
1059
+ if (nWidth > 0 && nHeight > 0) {
1060
+ const subPixels = new Uint8Array(nRowStride * nHeight);
1061
+ const result = this._gl.readPixels(
1062
+ nx,
1063
+ ny,
1064
+ nWidth,
1065
+ nHeight,
1066
+ format,
1067
+ type,
1068
+ Uint8ArrayToVariant(subPixels)
1069
+ );
1070
+ const src = result && result.length > 0 ? result : subPixels;
1071
+ const offset = 4 * (nx - x) + (ny - y) * rowStride;
1072
+ for (let j = 0; j < nHeight; ++j) {
1073
+ for (let i = 0; i < nWidth; ++i) {
1074
+ for (let k = 0; k < 4; ++k) {
1075
+ pixelData[offset + j * rowStride + 4 * i + k] = src[j * nRowStride + 4 * i + k];
1076
+ }
1077
+ }
1078
+ }
1079
+ }
1080
+ } else {
1081
+ const result = this._gl.readPixels(
1082
+ x,
1083
+ y,
1084
+ width,
1085
+ height,
1086
+ format,
1087
+ type,
1088
+ Uint8ArrayToVariant(pixelData)
1089
+ );
1090
+ if (result && result.length > 0) {
1091
+ pixelData.set(result);
1092
+ }
1093
+ }
1094
+ }
1095
+ // https://github.com/stackgl/headless-gl/blob/ce1c08c0ef0c31d8c308cb828fd2f172c0bf5084/src/javascript/webgl-rendering-context.js#L3131
1096
+ texImage2D(target = 0, level = 0, internalFormat = 0, formatOrWidth = 0, typeOrHeight = 0, sourceOrBorder = 0, _format = 0, type = 0, pixels) {
1097
+ let width = 0;
1098
+ let height = 0;
1099
+ let format = 0;
1100
+ let source;
1101
+ let pixbuf;
1102
+ let border = 0;
1103
+ if (arguments.length === 6) {
1104
+ type = typeOrHeight;
1105
+ format = formatOrWidth;
1106
+ if (sourceOrBorder instanceof GdkPixbuf.Pixbuf) {
1107
+ pixbuf = sourceOrBorder;
1108
+ width = pixbuf.get_width();
1109
+ height = pixbuf.get_height();
1110
+ ;
1111
+ pixels = pixbuf.get_pixels();
1112
+ } else {
1113
+ source = sourceOrBorder;
1114
+ const imageData = extractImageData(source);
1115
+ if (imageData == null) {
1116
+ throw new TypeError("texImage2D(GLenum, GLint, GLenum, GLint, GLenum, GLenum, ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement)");
1117
+ }
1118
+ width = imageData.width;
1119
+ height = imageData.height;
1120
+ pixels = imageData.data;
1121
+ }
1122
+ } else if (arguments.length === 9) {
1123
+ width = formatOrWidth;
1124
+ height = typeOrHeight;
1125
+ border = sourceOrBorder;
1126
+ format = _format;
1127
+ type = type;
1128
+ pixels = pixels;
1129
+ }
1130
+ if (typeof pixels !== "object" && pixels !== void 0) {
1131
+ throw new TypeError("texImage2D(GLenum, GLint, GLenum, GLint, GLint, GLint, GLenum, GLenum, Uint8Array)");
1132
+ }
1133
+ if (!checkFormat(this, format) || !checkFormat(this, internalFormat)) {
1134
+ this.setError(this.INVALID_ENUM);
1135
+ return;
1136
+ }
1137
+ if (type === this.FLOAT && !this._extensions.oes_texture_float) {
1138
+ this.setError(this.INVALID_ENUM);
1139
+ return;
1140
+ }
1141
+ const texture = this._getTexImage(target);
1142
+ if (!texture || format !== internalFormat) {
1143
+ this.setError(this.INVALID_OPERATION);
1144
+ return;
1145
+ }
1146
+ const pixelSize = this._computePixelSize(type, format);
1147
+ if (pixelSize === 0) {
1148
+ return;
1149
+ }
1150
+ if (!this._checkDimensions(
1151
+ target,
1152
+ width,
1153
+ height,
1154
+ level
1155
+ )) {
1156
+ return;
1157
+ }
1158
+ let data = convertPixels(pixels);
1159
+ const rowStride = this._computeRowStride(width, pixelSize);
1160
+ const imageSize = rowStride * height;
1161
+ if (data && data.length < imageSize) {
1162
+ this.setError(this.INVALID_OPERATION);
1163
+ return;
1164
+ }
1165
+ if (border !== 0 || validCubeTarget(this, target) && width !== height) {
1166
+ this.setError(this.INVALID_VALUE);
1167
+ return;
1168
+ }
1169
+ if (this._unpackFlipY && data && width > 0 && height > 0) {
1170
+ const flipped = new Uint8Array(data.length);
1171
+ for (let row = 0; row < height; row++) {
1172
+ const srcOffset = row * rowStride;
1173
+ const dstOffset = (height - 1 - row) * rowStride;
1174
+ flipped.set(data.subarray(srcOffset, srcOffset + rowStride), dstOffset);
1175
+ }
1176
+ data = flipped;
1177
+ }
1178
+ this._saveError();
1179
+ this._gl.texImage2D(target, level, internalFormat, width, height, border, format, type, Uint8ArrayToVariant(data));
1180
+ const error = this.getError();
1181
+ this._restoreError(error);
1182
+ if (error !== this.NO_ERROR) {
1183
+ return;
1184
+ }
1185
+ texture._levelWidth[level] = width;
1186
+ texture._levelHeight[level] = height;
1187
+ texture._format = format;
1188
+ texture._type = type;
1189
+ const activeFramebuffer = this._activeFramebuffer;
1190
+ if (activeFramebuffer) {
1191
+ let needsUpdate = false;
1192
+ const attachments = this._getAttachments();
1193
+ for (let i = 0; i < attachments.length; ++i) {
1194
+ if (activeFramebuffer._attachments[attachments[i]] === texture) {
1195
+ needsUpdate = true;
1196
+ break;
1197
+ }
1198
+ }
1199
+ if (needsUpdate && this._activeFramebuffer) {
1200
+ this._updateFramebufferAttachments(this._activeFramebuffer);
1201
+ }
1202
+ }
1203
+ }
1204
+ texSubImage2D(target = 0, level = 0, xoffset = 0, yoffset = 0, formatOrWidth = 0, typeOrHeight = 0, sourceOrFormat = 0, type = 0, pixels) {
1205
+ let width = 0;
1206
+ let height = 0;
1207
+ let format = 0;
1208
+ let source;
1209
+ let pixbuf;
1210
+ if (arguments.length === 7) {
1211
+ type = typeOrHeight;
1212
+ format = formatOrWidth;
1213
+ if (sourceOrFormat instanceof GdkPixbuf.Pixbuf) {
1214
+ pixbuf = sourceOrFormat;
1215
+ width = pixbuf.get_width();
1216
+ height = pixbuf.get_height();
1217
+ ;
1218
+ pixels = pixbuf.get_pixels();
1219
+ } else {
1220
+ source = sourceOrFormat;
1221
+ const imageData = extractImageData(source);
1222
+ if (imageData == null) {
1223
+ throw new TypeError("texSubImage2D(GLenum, GLint, GLint, GLint, GLenum, GLenum, ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement)");
1224
+ }
1225
+ width = imageData.width;
1226
+ height = imageData.height;
1227
+ pixels = imageData.data;
1228
+ }
1229
+ } else {
1230
+ width = formatOrWidth;
1231
+ height = typeOrHeight;
1232
+ format = sourceOrFormat;
1233
+ }
1234
+ if (typeof pixels !== "object") {
1235
+ throw new TypeError("texSubImage2D(GLenum, GLint, GLint, GLint, GLint, GLint, GLenum, GLenum, Uint8Array)");
1236
+ }
1237
+ const texture = this._getTexImage(target);
1238
+ if (!texture) {
1239
+ this.setError(this.INVALID_OPERATION);
1240
+ return;
1241
+ }
1242
+ if (type === this.FLOAT && !this._extensions.oes_texture_float) {
1243
+ this.setError(this.INVALID_ENUM);
1244
+ return;
1245
+ }
1246
+ const pixelSize = this._computePixelSize(type, format);
1247
+ if (pixelSize === 0) {
1248
+ return;
1249
+ }
1250
+ if (!this._checkDimensions(
1251
+ target,
1252
+ width,
1253
+ height,
1254
+ level
1255
+ )) {
1256
+ return;
1257
+ }
1258
+ if (xoffset < 0 || yoffset < 0) {
1259
+ this.setError(this.INVALID_VALUE);
1260
+ return;
1261
+ }
1262
+ let data = convertPixels(pixels);
1263
+ const rowStride = this._computeRowStride(width, pixelSize);
1264
+ const imageSize = rowStride * height;
1265
+ if (!data || data.length < imageSize) {
1266
+ this.setError(this.INVALID_OPERATION);
1267
+ return;
1268
+ }
1269
+ if (this._unpackFlipY && data && width > 0 && height > 0) {
1270
+ const flipped = new Uint8Array(data.length);
1271
+ for (let row = 0; row < height; row++) {
1272
+ const srcOffset = row * rowStride;
1273
+ const dstOffset = (height - 1 - row) * rowStride;
1274
+ flipped.set(data.subarray(srcOffset, srcOffset + rowStride), dstOffset);
1275
+ }
1276
+ data = flipped;
1277
+ }
1278
+ this._gl.texSubImage2D(
1279
+ target,
1280
+ level,
1281
+ xoffset,
1282
+ yoffset,
1283
+ width,
1284
+ height,
1285
+ format,
1286
+ type,
1287
+ Uint8ArrayToVariant(data)
1288
+ );
1289
+ }
1290
+ _checkUniformValid(location, v0, name, count, type) {
1291
+ if (!checkObject(location)) {
1292
+ throw new TypeError(`${name}(WebGLUniformLocation, ...)`);
1293
+ } else if (!location) {
1294
+ return false;
1295
+ } else if (this._checkLocationActive(location)) {
1296
+ const utype = location._activeInfo.type;
1297
+ if (utype === this.SAMPLER_2D || utype === this.SAMPLER_CUBE) {
1298
+ if (count !== 1) {
1299
+ this.setError(this.INVALID_VALUE);
1300
+ return;
1301
+ }
1302
+ if (type !== "i") {
1303
+ this.setError(this.INVALID_OPERATION);
1304
+ return;
1305
+ }
1306
+ if (v0 < 0 || v0 >= this._textureUnits.length) {
1307
+ this.setError(this.INVALID_VALUE);
1308
+ return false;
1309
+ }
1310
+ }
1311
+ if (uniformTypeSize(this, utype) > count) {
1312
+ this.setError(this.INVALID_OPERATION);
1313
+ return false;
1314
+ }
1315
+ return true;
1316
+ }
1317
+ return false;
1318
+ }
1319
+ _checkUniformValueValid(location, value, name, count, _type) {
1320
+ if (!checkObject(location) || !checkObject(value)) {
1321
+ throw new TypeError(`${name}v(WebGLUniformLocation, Array)`);
1322
+ } else if (!location) {
1323
+ return false;
1324
+ } else if (!this._checkLocationActive(location)) {
1325
+ return false;
1326
+ } else if (typeof value !== "object" || !value || typeof value.length !== "number") {
1327
+ throw new TypeError(`Second argument to ${name} must be array`);
1328
+ } else if (uniformTypeSize(this, location._activeInfo.type) > count) {
1329
+ this.setError(this.INVALID_OPERATION);
1330
+ return false;
1331
+ } else if (value.length >= count && value.length % count === 0) {
1332
+ if (location._array) {
1333
+ return true;
1334
+ } else if (value.length === count) {
1335
+ return true;
1336
+ } else {
1337
+ this.setError(this.INVALID_OPERATION);
1338
+ return false;
1339
+ }
1340
+ }
1341
+ this.setError(this.INVALID_VALUE);
1342
+ return false;
1343
+ }
1344
+ uniform1fv(location, value) {
1345
+ if (!location || !this._checkUniformValueValid(location, value, "uniform1fv", 1, "f")) return;
1346
+ if (location?._array) {
1347
+ const locs = location._array;
1348
+ for (let i = 0; i < locs.length && i < value.length; ++i) {
1349
+ const loc = locs[i];
1350
+ if (loc != null) {
1351
+ this._gl.uniform1f(loc, value[i]);
1352
+ }
1353
+ }
1354
+ return;
1355
+ }
1356
+ this._gl.uniform1f(location?._ | 0, value[0]);
1357
+ }
1358
+ uniform1iv(location, v) {
1359
+ if (!this._checkUniformValueValid(location, v, "uniform1iv", 1, "i")) return;
1360
+ if (location?._array) {
1361
+ const locs = location._array;
1362
+ for (let i = 0; i < locs.length && i < v.length; ++i) {
1363
+ const loc = locs[i];
1364
+ if (loc != null) {
1365
+ this._gl.uniform1i(loc, v[i]);
1366
+ }
1367
+ }
1368
+ return;
1369
+ }
1370
+ this.uniform1i(location, v[0]);
1371
+ }
1372
+ uniform2fv(location, v) {
1373
+ if (!this._checkUniformValueValid(location, v, "uniform2fv", 2, "f")) return;
1374
+ if (location?._array) {
1375
+ const locs = location._array;
1376
+ for (let i = 0; i < locs.length && 2 * i < v.length; ++i) {
1377
+ const loc = locs[i];
1378
+ if (loc != null) {
1379
+ this._gl.uniform2f(loc, v[2 * i], v[2 * i + 1]);
1380
+ }
1381
+ }
1382
+ return;
1383
+ }
1384
+ this._gl.uniform2f(location?._ || 0, v[0], v[1]);
1385
+ }
1386
+ uniform2iv(location, v) {
1387
+ if (!this._checkUniformValueValid(location, v, "uniform2iv", 2, "i")) return;
1388
+ if (location?._array) {
1389
+ const locs = location._array;
1390
+ for (let i = 0; i < locs.length && 2 * i < v.length; ++i) {
1391
+ const loc = locs[i];
1392
+ if (loc != null) {
1393
+ this._gl.uniform2i(loc, v[2 * i], v[2 * i + 1]);
1394
+ }
1395
+ }
1396
+ return;
1397
+ }
1398
+ this.uniform2i(location, v[0], v[1]);
1399
+ }
1400
+ uniform3fv(location, v) {
1401
+ if (!this._checkUniformValueValid(location, v, "uniform3fv", 3, "f")) return;
1402
+ if (location?._array) {
1403
+ const locs = location._array;
1404
+ for (let i = 0; i < locs.length && 3 * i < v.length; ++i) {
1405
+ const loc = locs[i];
1406
+ if (loc != null) {
1407
+ this._gl.uniform3f(loc, v[3 * i], v[3 * i + 1], v[3 * i + 2]);
1408
+ }
1409
+ }
1410
+ return;
1411
+ }
1412
+ this._gl.uniform3f(location?._ || 0, v[0], v[1], v[2]);
1413
+ }
1414
+ uniform3iv(location, v) {
1415
+ if (!this._checkUniformValueValid(location, v, "uniform3iv", 3, "i")) return;
1416
+ if (location?._array) {
1417
+ const locs = location._array;
1418
+ for (let i = 0; i < locs.length && 3 * i < v.length; ++i) {
1419
+ const loc = locs[i];
1420
+ if (loc != null) {
1421
+ this._gl.uniform3i(loc, v[3 * i], v[3 * i + 1], v[3 * i + 2]);
1422
+ }
1423
+ }
1424
+ return;
1425
+ }
1426
+ this.uniform3i(location, v[0], v[1], v[2]);
1427
+ }
1428
+ uniform4fv(location, v) {
1429
+ if (!this._checkUniformValueValid(location, v, "uniform4fv", 4, "f")) return;
1430
+ if (location?._array) {
1431
+ const locs = location._array;
1432
+ for (let i = 0; i < locs.length && 4 * i < v.length; ++i) {
1433
+ const loc = locs[i];
1434
+ if (loc != null) {
1435
+ this._gl.uniform4f(loc, v[4 * i], v[4 * i + 1], v[4 * i + 2], v[4 * i + 3]);
1436
+ }
1437
+ }
1438
+ return;
1439
+ }
1440
+ this._gl.uniform4f(location?._ || 0, v[0], v[1], v[2], v[3]);
1441
+ }
1442
+ uniform4iv(location, v) {
1443
+ if (!this._checkUniformValueValid(location, v, "uniform4iv", 4, "i")) return;
1444
+ if (location?._array) {
1445
+ const locs = location._array;
1446
+ for (let i = 0; i < locs.length && 4 * i < v.length; ++i) {
1447
+ const loc = locs[i];
1448
+ if (loc != null) {
1449
+ this._gl.uniform4i(loc, v[4 * i], v[4 * i + 1], v[4 * i + 2], v[4 * i + 3]);
1450
+ }
1451
+ }
1452
+ return;
1453
+ }
1454
+ this.uniform4i(location, v[0], v[1], v[2], v[3]);
1455
+ }
1456
+ _checkUniformMatrix(location, transpose, value, name, count) {
1457
+ if (!checkObject(location) || typeof value !== "object") {
1458
+ throw new TypeError(name + "(WebGLUniformLocation, Boolean, Array)");
1459
+ } else if (!!transpose || typeof value !== "object" || value === null || !value.length || value.length % count * count !== 0) {
1460
+ this.setError(this.INVALID_VALUE);
1461
+ return false;
1462
+ }
1463
+ if (!location) {
1464
+ return false;
1465
+ }
1466
+ if (!this._checkLocationActive(location)) {
1467
+ return false;
1468
+ }
1469
+ if (value.length === count * count) {
1470
+ return true;
1471
+ } else if (location._array) {
1472
+ return true;
1473
+ }
1474
+ this.setError(this.INVALID_VALUE);
1475
+ return false;
1476
+ }
1477
+ uniformMatrix2fv(location, transpose, value) {
1478
+ if (!this._checkUniformMatrix(location, transpose, value, "uniformMatrix2fv", 2)) return;
1479
+ const data = new Float32Array(value);
1480
+ this._gl.uniformMatrix2fv(
1481
+ location?._ || 0,
1482
+ !!transpose,
1483
+ listToArray(data)
1484
+ );
1485
+ }
1486
+ uniformMatrix3fv(location, transpose, value) {
1487
+ if (!this._checkUniformMatrix(location, transpose, value, "uniformMatrix3fv", 3)) return;
1488
+ const data = new Float32Array(value);
1489
+ this._gl.uniformMatrix3fv(
1490
+ location?._ || 0,
1491
+ !!transpose,
1492
+ listToArray(data)
1493
+ );
1494
+ }
1495
+ uniformMatrix4fv(location, transpose, value) {
1496
+ if (!this._checkUniformMatrix(location, transpose, value, "uniformMatrix4fv", 4)) return;
1497
+ const data = new Float32Array(value);
1498
+ this._gl.uniformMatrix4fv(
1499
+ location?._ || 0,
1500
+ !!transpose,
1501
+ listToArray(data)
1502
+ );
1503
+ }
1504
+ //////////// BASE ////////////
1505
+ activeTexture(texture = 0) {
1506
+ const texNum = texture - this.TEXTURE0;
1507
+ if (texNum >= 0 && texNum < this._textureUnits.length) {
1508
+ this._activeTextureUnit = texNum;
1509
+ return this._gl.activeTexture(texture);
1510
+ }
1511
+ this.setError(this.INVALID_ENUM);
1512
+ }
1513
+ attachShader(program, shader) {
1514
+ if (!checkObject(program) || !checkObject(shader)) {
1515
+ throw new TypeError("attachShader(WebGLProgram, WebGLShader)");
1516
+ }
1517
+ if (!program || !shader) {
1518
+ this.setError(this.INVALID_VALUE);
1519
+ return;
1520
+ } else if (program instanceof WebGLProgram && shader instanceof WebGLShader && this._checkOwns(program) && this._checkOwns(shader)) {
1521
+ if (!program._linked(shader)) {
1522
+ this._saveError();
1523
+ this._gl.attachShader(
1524
+ program._ | 0,
1525
+ shader._ | 0
1526
+ );
1527
+ const error = this.getError();
1528
+ this._restoreError(error);
1529
+ if (error === this.NO_ERROR) {
1530
+ program._link(shader);
1531
+ }
1532
+ return;
1533
+ }
1534
+ }
1535
+ this.setError(this.INVALID_OPERATION);
1536
+ }
1537
+ bindAttribLocation(program, index, name) {
1538
+ if (!checkObject(program) || typeof name !== "string") {
1539
+ throw new TypeError("bindAttribLocation(WebGLProgram, GLint, String)");
1540
+ }
1541
+ name += "";
1542
+ if (!isValidString(name) || name.length > MAX_ATTRIBUTE_LENGTH) {
1543
+ this.setError(this.INVALID_VALUE);
1544
+ } else if (/^_?webgl_a/.test(name)) {
1545
+ this.setError(this.INVALID_OPERATION);
1546
+ } else if (this._checkWrapper(program, WebGLProgram)) {
1547
+ return this._gl.bindAttribLocation(
1548
+ program._ | 0,
1549
+ index | 0,
1550
+ name
1551
+ );
1552
+ }
1553
+ }
1554
+ bindBuffer(target = 0, buffer) {
1555
+ if (!checkObject(buffer)) {
1556
+ throw new TypeError("bindBuffer(GLenum, WebGLBuffer)");
1557
+ }
1558
+ if (target !== this.ARRAY_BUFFER && target !== this.ELEMENT_ARRAY_BUFFER) {
1559
+ this.setError(this.INVALID_ENUM);
1560
+ return;
1561
+ }
1562
+ if (!buffer) {
1563
+ buffer = null;
1564
+ this._gl.bindBuffer(target, 0);
1565
+ } else if (buffer._pendingDelete) {
1566
+ return;
1567
+ } else if (this._checkWrapper(buffer, WebGLBuffer)) {
1568
+ if (buffer._binding && buffer._binding !== target) {
1569
+ this.setError(this.INVALID_OPERATION);
1570
+ return;
1571
+ }
1572
+ buffer._binding = target | 0;
1573
+ this._gl.bindBuffer(target, buffer._ | 0);
1574
+ } else {
1575
+ return;
1576
+ }
1577
+ if (target === this.ARRAY_BUFFER) {
1578
+ this._vertexGlobalState.setArrayBuffer(buffer);
1579
+ } else {
1580
+ this._vertexObjectState.setElementArrayBuffer(buffer);
1581
+ }
1582
+ }
1583
+ bindFramebuffer(target, framebuffer) {
1584
+ if (!checkObject(framebuffer)) {
1585
+ throw new TypeError("bindFramebuffer(GLenum, WebGLFramebuffer)");
1586
+ }
1587
+ if (target !== this.FRAMEBUFFER) {
1588
+ this.setError(this.INVALID_ENUM);
1589
+ return;
1590
+ }
1591
+ if (!framebuffer) {
1592
+ this._gl.bindFramebuffer(this.FRAMEBUFFER, this._gtkFboId);
1593
+ } else if (framebuffer._pendingDelete) {
1594
+ return;
1595
+ } else if (this._checkWrapper(framebuffer, WebGLFramebuffer)) {
1596
+ this._gl.bindFramebuffer(
1597
+ this.FRAMEBUFFER,
1598
+ framebuffer._ | 0
1599
+ );
1600
+ } else {
1601
+ return;
1602
+ }
1603
+ const activeFramebuffer = this._activeFramebuffer;
1604
+ if (activeFramebuffer !== framebuffer) {
1605
+ if (activeFramebuffer) {
1606
+ activeFramebuffer._refCount -= 1;
1607
+ activeFramebuffer._checkDelete();
1608
+ }
1609
+ if (framebuffer) {
1610
+ framebuffer._refCount += 1;
1611
+ }
1612
+ }
1613
+ this._activeFramebuffer = framebuffer;
1614
+ if (framebuffer) {
1615
+ this._updateFramebufferAttachments(framebuffer);
1616
+ }
1617
+ }
1618
+ bindRenderbuffer(target, renderbuffer) {
1619
+ if (!checkObject(renderbuffer)) {
1620
+ throw new TypeError("bindRenderbuffer(GLenum, WebGLRenderbuffer)");
1621
+ }
1622
+ if (target !== this.RENDERBUFFER) {
1623
+ this.setError(this.INVALID_ENUM);
1624
+ return;
1625
+ }
1626
+ if (!renderbuffer) {
1627
+ this._gl.bindRenderbuffer(
1628
+ target | 0,
1629
+ 0
1630
+ );
1631
+ } else if (renderbuffer._pendingDelete) {
1632
+ return;
1633
+ } else if (this._checkWrapper(renderbuffer, WebGLRenderbuffer)) {
1634
+ this._gl.bindRenderbuffer(
1635
+ target | 0,
1636
+ renderbuffer._ | 0
1637
+ );
1638
+ } else {
1639
+ return;
1640
+ }
1641
+ const active = this._activeRenderbuffer;
1642
+ if (active !== renderbuffer) {
1643
+ if (active) {
1644
+ active._refCount -= 1;
1645
+ active._checkDelete();
1646
+ }
1647
+ if (renderbuffer) {
1648
+ renderbuffer._refCount += 1;
1649
+ }
1650
+ }
1651
+ this._activeRenderbuffer = renderbuffer;
1652
+ }
1653
+ bindTexture(target = 0, texture) {
1654
+ if (!checkObject(texture)) {
1655
+ throw new TypeError("bindTexture(GLenum, WebGLTexture)");
1656
+ }
1657
+ if (!this._validTextureTarget(target)) {
1658
+ this.setError(this.INVALID_ENUM);
1659
+ return;
1660
+ }
1661
+ let textureId = 0;
1662
+ if (!texture) {
1663
+ texture = null;
1664
+ } else if (texture instanceof WebGLTexture && texture._pendingDelete) {
1665
+ return;
1666
+ } else if (this._checkWrapper(texture, WebGLTexture)) {
1667
+ if (texture._binding && texture._binding !== target) {
1668
+ this.setError(this.INVALID_OPERATION);
1669
+ return;
1670
+ }
1671
+ texture._binding = target;
1672
+ if (texture._complete) {
1673
+ textureId = texture._ | 0;
1674
+ }
1675
+ } else {
1676
+ return;
1677
+ }
1678
+ this._saveError();
1679
+ this._gl.bindTexture(
1680
+ target,
1681
+ textureId
1682
+ );
1683
+ const error = this.getError();
1684
+ this._restoreError(error);
1685
+ if (error !== this.NO_ERROR) {
1686
+ return;
1687
+ }
1688
+ const activeUnit = this._getActiveTextureUnit();
1689
+ const activeTex = this._getActiveTexture(target);
1690
+ if (activeTex !== texture) {
1691
+ if (activeTex) {
1692
+ activeTex._refCount -= 1;
1693
+ activeTex._checkDelete();
1694
+ }
1695
+ if (texture) {
1696
+ texture._refCount += 1;
1697
+ }
1698
+ }
1699
+ if (target === this.TEXTURE_2D) {
1700
+ activeUnit._bind2D = texture;
1701
+ } else if (target === this.TEXTURE_CUBE_MAP) {
1702
+ activeUnit._bindCube = texture;
1703
+ }
1704
+ }
1705
+ blendColor(red = 0, green = 0, blue = 0, alpha = 0) {
1706
+ return this._gl.blendColor(+red, +green, +blue, +alpha);
1707
+ }
1708
+ blendEquation(mode = 0) {
1709
+ if (this._validBlendMode(mode)) {
1710
+ return this._gl.blendEquation(mode);
1711
+ }
1712
+ this.setError(this.INVALID_ENUM);
1713
+ }
1714
+ blendEquationSeparate(modeRGB = 0, modeAlpha = 0) {
1715
+ if (this._validBlendMode(modeRGB) && this._validBlendMode(modeAlpha)) {
1716
+ return this._gl.blendEquationSeparate(modeRGB, modeAlpha);
1717
+ }
1718
+ this.setError(this.INVALID_ENUM);
1719
+ }
1720
+ blendFunc(sfactor = 0, dfactor = 0) {
1721
+ if (!this._validBlendFunc(sfactor) || !this._validBlendFunc(dfactor)) {
1722
+ this.setError(this.INVALID_ENUM);
1723
+ return;
1724
+ }
1725
+ if (this._isConstantBlendFunc(sfactor) && this._isConstantBlendFunc(dfactor)) {
1726
+ this.setError(this.INVALID_OPERATION);
1727
+ return;
1728
+ }
1729
+ this._gl.blendFunc(sfactor, dfactor);
1730
+ }
1731
+ blendFuncSeparate(srcRGB = 0, dstRGB = 0, srcAlpha = 0, dstAlpha = 0) {
1732
+ if (!(this._validBlendFunc(srcRGB) && this._validBlendFunc(dstRGB) && this._validBlendFunc(srcAlpha) && this._validBlendFunc(dstAlpha))) {
1733
+ this.setError(this.INVALID_ENUM);
1734
+ return;
1735
+ }
1736
+ if (this._isConstantBlendFunc(srcRGB) && this._isConstantBlendFunc(dstRGB) || this._isConstantBlendFunc(srcAlpha) && this._isConstantBlendFunc(dstAlpha)) {
1737
+ this.setError(this.INVALID_OPERATION);
1738
+ return;
1739
+ }
1740
+ this._gl.blendFuncSeparate(
1741
+ srcRGB,
1742
+ dstRGB,
1743
+ srcAlpha,
1744
+ dstAlpha
1745
+ );
1746
+ }
1747
+ checkFramebufferStatus(target) {
1748
+ if (target !== this.FRAMEBUFFER) {
1749
+ this.setError(this.INVALID_ENUM);
1750
+ return 0;
1751
+ }
1752
+ const framebuffer = this._activeFramebuffer;
1753
+ if (!framebuffer) {
1754
+ return this.FRAMEBUFFER_COMPLETE;
1755
+ }
1756
+ return this._preCheckFramebufferStatus(framebuffer);
1757
+ }
1758
+ clear(mask = 0) {
1759
+ if (!this._framebufferOk()) {
1760
+ return;
1761
+ }
1762
+ return this._gl.clear(mask);
1763
+ }
1764
+ clearColor(red, green, blue, alpha) {
1765
+ return this._gl.clearColor(+red, +green, +blue, +alpha);
1766
+ }
1767
+ clearDepth(depth) {
1768
+ return this._gl.clearDepth(+depth);
1769
+ }
1770
+ clearStencil(s = 0) {
1771
+ this._checkStencil = false;
1772
+ return this._gl.clearStencil(s);
1773
+ }
1774
+ colorMask(red, green, blue, alpha) {
1775
+ return this._gl.colorMask(!!red, !!green, !!blue, !!alpha);
1776
+ }
1777
+ compileShader(shader) {
1778
+ if (!checkObject(shader)) {
1779
+ throw new TypeError("compileShader(WebGLShader)");
1780
+ }
1781
+ if (this._checkWrapper(shader, WebGLShader) && this._checkShaderSource(shader)) {
1782
+ const prevError = this.getError();
1783
+ this._gl.compileShader(shader._ | 0);
1784
+ shader._needsRecompile = false;
1785
+ const error = this.getError();
1786
+ shader._compileStatus = !!this._gl.getShaderParameter(
1787
+ shader._ | 0,
1788
+ this.COMPILE_STATUS
1789
+ );
1790
+ shader._compileInfo = this._gl.getShaderInfoLog(shader._ | 0) || "null";
1791
+ this.getError();
1792
+ this.setError(prevError || error);
1793
+ }
1794
+ }
1795
+ copyTexImage2D(target = 0, level = 0, internalFormat = 0, x = 0, y = 0, width = 0, height = 0, border = 0) {
1796
+ const texture = this._getTexImage(target);
1797
+ if (!texture) {
1798
+ this.setError(this.INVALID_OPERATION);
1799
+ return;
1800
+ }
1801
+ if (internalFormat !== this.RGBA && internalFormat !== this.RGB && internalFormat !== this.ALPHA && internalFormat !== this.LUMINANCE && internalFormat !== this.LUMINANCE_ALPHA) {
1802
+ this.setError(this.INVALID_ENUM);
1803
+ return;
1804
+ }
1805
+ if (level < 0 || width < 0 || height < 0 || border !== 0) {
1806
+ this.setError(this.INVALID_VALUE);
1807
+ return;
1808
+ }
1809
+ if (level > 0 && !(bits.isPow2(width) && bits.isPow2(height))) {
1810
+ this.setError(this.INVALID_VALUE);
1811
+ return;
1812
+ }
1813
+ this._saveError();
1814
+ this._gl.copyTexImage2D(
1815
+ target,
1816
+ level,
1817
+ internalFormat,
1818
+ x,
1819
+ y,
1820
+ width,
1821
+ height,
1822
+ border
1823
+ );
1824
+ const error = this.getError();
1825
+ this._restoreError(error);
1826
+ if (error === this.NO_ERROR) {
1827
+ texture._levelWidth[level] = width;
1828
+ texture._levelHeight[level] = height;
1829
+ texture._format = this.RGBA;
1830
+ texture._type = this.UNSIGNED_BYTE;
1831
+ }
1832
+ }
1833
+ copyTexSubImage2D(target = 0, level = 0, xoffset = 0, yoffset = 0, x = 0, y = 0, width = 0, height = 0) {
1834
+ const texture = this._getTexImage(target);
1835
+ if (!texture) {
1836
+ this.setError(this.INVALID_OPERATION);
1837
+ return;
1838
+ }
1839
+ if (width < 0 || height < 0 || xoffset < 0 || yoffset < 0 || level < 0) {
1840
+ this.setError(this.INVALID_VALUE);
1841
+ return;
1842
+ }
1843
+ this._gl.copyTexSubImage2D(
1844
+ target,
1845
+ level,
1846
+ xoffset,
1847
+ yoffset,
1848
+ x,
1849
+ y,
1850
+ width,
1851
+ height
1852
+ );
1853
+ }
1854
+ createBuffer() {
1855
+ const id = this._gl.createBuffer();
1856
+ if (!id || id <= 0) return null;
1857
+ const webGLBuffer = new WebGLBuffer(id, this);
1858
+ this._buffers[id] = webGLBuffer;
1859
+ return webGLBuffer;
1860
+ }
1861
+ createFramebuffer() {
1862
+ const id = this._gl.createFramebuffer();
1863
+ if (id <= 0) return null;
1864
+ const webGLFramebuffer = new WebGLFramebuffer(id, this);
1865
+ this._framebuffers[id] = webGLFramebuffer;
1866
+ return webGLFramebuffer;
1867
+ }
1868
+ createProgram() {
1869
+ const id = this._gl.createProgram();
1870
+ if (id <= 0) return null;
1871
+ const webGLProgram = new WebGLProgram(id, this);
1872
+ this._programs[id] = webGLProgram;
1873
+ return webGLProgram;
1874
+ }
1875
+ createRenderbuffer() {
1876
+ const id = this._gl.createRenderbuffer();
1877
+ if (id <= 0) return null;
1878
+ const webGLRenderbuffer = new WebGLRenderbuffer(id, this);
1879
+ this._renderbuffers[id] = webGLRenderbuffer;
1880
+ return webGLRenderbuffer;
1881
+ }
1882
+ createShader(type = 0) {
1883
+ if (type !== this.FRAGMENT_SHADER && type !== this.VERTEX_SHADER) {
1884
+ this.setError(this.INVALID_ENUM);
1885
+ return null;
1886
+ }
1887
+ const id = this._gl.createShader(type);
1888
+ if (id < 0) {
1889
+ return null;
1890
+ }
1891
+ const result = new WebGLShader(id, this, type);
1892
+ this._shaders[id] = result;
1893
+ return result;
1894
+ }
1895
+ createTexture() {
1896
+ const id = this._gl.createTexture();
1897
+ if (id <= 0) return null;
1898
+ const webGlTexture = new WebGLTexture(id, this);
1899
+ this._textures[id] = webGlTexture;
1900
+ return webGlTexture;
1901
+ }
1902
+ cullFace(mode) {
1903
+ return this._gl.cullFace(mode | 0);
1904
+ }
1905
+ deleteBuffer(buffer) {
1906
+ if (!checkObject(buffer) || buffer !== null && !(buffer instanceof WebGLBuffer)) {
1907
+ throw new TypeError("deleteBuffer(WebGLBuffer)");
1908
+ }
1909
+ if (!(buffer instanceof WebGLBuffer && this._checkOwns(buffer))) {
1910
+ this.setError(this.INVALID_OPERATION);
1911
+ return;
1912
+ }
1913
+ if (this._vertexGlobalState._arrayBufferBinding === buffer) {
1914
+ this.bindBuffer(this.ARRAY_BUFFER, null);
1915
+ }
1916
+ if (this._vertexObjectState._elementArrayBufferBinding === buffer) {
1917
+ this.bindBuffer(this.ELEMENT_ARRAY_BUFFER, null);
1918
+ }
1919
+ if (this._vertexObjectState === this._defaultVertexObjectState) {
1920
+ this._vertexObjectState.releaseArrayBuffer(buffer);
1921
+ }
1922
+ buffer._pendingDelete = true;
1923
+ buffer._checkDelete();
1924
+ }
1925
+ deleteFramebuffer(framebuffer) {
1926
+ if (!checkObject(framebuffer)) {
1927
+ throw new TypeError("deleteFramebuffer(WebGLFramebuffer)");
1928
+ }
1929
+ if (!(framebuffer instanceof WebGLFramebuffer && this._checkOwns(framebuffer))) {
1930
+ this.setError(this.INVALID_OPERATION);
1931
+ return;
1932
+ }
1933
+ if (this._activeFramebuffer === framebuffer) {
1934
+ this.bindFramebuffer(this.FRAMEBUFFER, null);
1935
+ }
1936
+ framebuffer._pendingDelete = true;
1937
+ framebuffer._checkDelete();
1938
+ }
1939
+ _deleteLinkable(name, object, Type) {
1940
+ if (!checkObject(object)) {
1941
+ throw new TypeError(name + "(" + Type.name + ")");
1942
+ }
1943
+ if (object instanceof Type && this._checkOwns(object)) {
1944
+ object._pendingDelete = true;
1945
+ object._checkDelete();
1946
+ return;
1947
+ }
1948
+ this.setError(this.INVALID_OPERATION);
1949
+ }
1950
+ deleteProgram(program) {
1951
+ return this._deleteLinkable("deleteProgram", program, WebGLProgram);
1952
+ }
1953
+ // Need to handle textures and render buffers as a special case:
1954
+ // When a texture gets deleted, we need to do the following extra steps:
1955
+ // 1. Is it bound to the current texture unit?
1956
+ // If so, then unbind it
1957
+ // 2. Is it attached to the active fbo?
1958
+ // If so, then detach it
1959
+ //
1960
+ // For renderbuffers only need to do second step
1961
+ //
1962
+ // After this, proceed with the usual deletion algorithm
1963
+ //
1964
+ deleteRenderbuffer(renderbuffer) {
1965
+ if (!checkObject(renderbuffer)) {
1966
+ throw new TypeError("deleteRenderbuffer(WebGLRenderbuffer)");
1967
+ }
1968
+ if (!(renderbuffer instanceof WebGLRenderbuffer && this._checkOwns(renderbuffer))) {
1969
+ this.setError(this.INVALID_OPERATION);
1970
+ return;
1971
+ }
1972
+ if (this._activeRenderbuffer === renderbuffer) {
1973
+ this.bindRenderbuffer(this.RENDERBUFFER, null);
1974
+ }
1975
+ const activeFramebuffer = this._activeFramebuffer;
1976
+ this._tryDetachFramebuffer(activeFramebuffer, renderbuffer);
1977
+ renderbuffer._pendingDelete = true;
1978
+ renderbuffer._checkDelete();
1979
+ }
1980
+ deleteShader(shader) {
1981
+ return this._deleteLinkable("deleteShader", shader, WebGLShader);
1982
+ }
1983
+ deleteTexture(texture) {
1984
+ if (!checkObject(texture)) {
1985
+ throw new TypeError("deleteTexture(WebGLTexture)");
1986
+ }
1987
+ if (texture instanceof WebGLTexture) {
1988
+ if (!this._checkOwns(texture)) {
1989
+ this.setError(this.INVALID_OPERATION);
1990
+ return;
1991
+ }
1992
+ } else {
1993
+ return;
1994
+ }
1995
+ const curActive = this._activeTextureUnit;
1996
+ for (let i = 0; i < this._textureUnits.length; ++i) {
1997
+ const unit = this._textureUnits[i];
1998
+ if (unit._bind2D === texture) {
1999
+ this.activeTexture(this.TEXTURE0 + i);
2000
+ this.bindTexture(this.TEXTURE_2D, null);
2001
+ } else if (unit._bindCube === texture) {
2002
+ this.activeTexture(this.TEXTURE0 + i);
2003
+ this.bindTexture(this.TEXTURE_CUBE_MAP, null);
2004
+ }
2005
+ }
2006
+ this.activeTexture(this.TEXTURE0 + curActive);
2007
+ const ctx = this;
2008
+ const activeFramebuffer = this._activeFramebuffer;
2009
+ const tryDetach = (framebuffer) => {
2010
+ if (framebuffer && framebuffer._linked(texture)) {
2011
+ const attachments = ctx._getAttachments();
2012
+ for (let i = 0; i < attachments.length; ++i) {
2013
+ const attachment = attachments[i];
2014
+ if (framebuffer._attachments[attachment] === texture) {
2015
+ ctx.framebufferTexture2D(
2016
+ this.FRAMEBUFFER,
2017
+ attachment,
2018
+ this.TEXTURE_2D,
2019
+ null
2020
+ );
2021
+ }
2022
+ }
2023
+ }
2024
+ };
2025
+ tryDetach(activeFramebuffer);
2026
+ texture._pendingDelete = true;
2027
+ texture._checkDelete();
2028
+ }
2029
+ depthFunc(func) {
2030
+ func |= 0;
2031
+ switch (func) {
2032
+ case this.NEVER:
2033
+ case this.LESS:
2034
+ case this.EQUAL:
2035
+ case this.LEQUAL:
2036
+ case this.GREATER:
2037
+ case this.NOTEQUAL:
2038
+ case this.GEQUAL:
2039
+ case this.ALWAYS:
2040
+ return this._gl.depthFunc(func);
2041
+ default:
2042
+ this.setError(this.INVALID_ENUM);
2043
+ }
2044
+ }
2045
+ depthMask(flag2) {
2046
+ return this._gl.depthMask(!!flag2);
2047
+ }
2048
+ depthRange(zNear, zFar) {
2049
+ zNear = +zNear;
2050
+ zFar = +zFar;
2051
+ if (zNear <= zFar) {
2052
+ return this._gl.depthRange(zNear, zFar);
2053
+ }
2054
+ this.setError(this.INVALID_OPERATION);
2055
+ }
2056
+ destroy() {
2057
+ warnNotImplemented("destroy");
2058
+ }
2059
+ detachShader(program, shader) {
2060
+ if (!checkObject(program) || !checkObject(shader)) {
2061
+ throw new TypeError("detachShader(WebGLProgram, WebGLShader)");
2062
+ }
2063
+ if (this._checkWrapper(program, WebGLProgram) && this._checkWrapper(shader, WebGLShader)) {
2064
+ if (program._linked(shader)) {
2065
+ this._gl.detachShader(program._, shader._);
2066
+ program._unlink(shader);
2067
+ } else {
2068
+ this.setError(this.INVALID_OPERATION);
2069
+ }
2070
+ }
2071
+ }
2072
+ disable(cap = 0) {
2073
+ this._gl.disable(cap);
2074
+ if (cap === this.TEXTURE_2D || cap === this.TEXTURE_CUBE_MAP) {
2075
+ const active = this._getActiveTextureUnit();
2076
+ if (active._mode === cap) {
2077
+ active._mode = 0;
2078
+ }
2079
+ }
2080
+ }
2081
+ disableVertexAttribArray(index = 0) {
2082
+ if (index < 0 || index >= this._vertexObjectState._attribs.length) {
2083
+ this.setError(this.INVALID_VALUE);
2084
+ return;
2085
+ }
2086
+ this._gl.disableVertexAttribArray(index);
2087
+ this._vertexObjectState._attribs[index]._isPointer = false;
2088
+ }
2089
+ drawArrays(mode = 0, first = 0, count = 0) {
2090
+ if (first < 0 || count < 0) {
2091
+ this.setError(this.INVALID_VALUE);
2092
+ return;
2093
+ }
2094
+ if (!this._checkStencilState()) {
2095
+ return;
2096
+ }
2097
+ const reducedCount = vertexCount(this, mode, count);
2098
+ if (reducedCount < 0) {
2099
+ this.setError(this.INVALID_ENUM);
2100
+ return;
2101
+ }
2102
+ if (!this._framebufferOk()) {
2103
+ return;
2104
+ }
2105
+ if (count === 0) {
2106
+ return;
2107
+ }
2108
+ let maxIndex = first;
2109
+ if (count > 0) {
2110
+ maxIndex = count + first - 1 >>> 0;
2111
+ }
2112
+ if (this._checkVertexAttribState(maxIndex)) {
2113
+ this._gl.drawArrays(mode, first, reducedCount);
2114
+ }
2115
+ }
2116
+ drawElements(mode = 0, count = 0, type = 0, ioffset = 0) {
2117
+ if (count < 0 || ioffset < 0) {
2118
+ this.setError(this.INVALID_VALUE);
2119
+ return;
2120
+ }
2121
+ if (!this._checkStencilState()) {
2122
+ return;
2123
+ }
2124
+ const elementBuffer = this._vertexObjectState._elementArrayBufferBinding;
2125
+ if (!elementBuffer) {
2126
+ this.setError(this.INVALID_OPERATION);
2127
+ return;
2128
+ }
2129
+ let elementData = null;
2130
+ let offset = ioffset;
2131
+ if (type === this.UNSIGNED_SHORT) {
2132
+ if (offset % 2) {
2133
+ this.setError(this.INVALID_OPERATION);
2134
+ return;
2135
+ }
2136
+ offset >>= 1;
2137
+ elementData = new Uint16Array(elementBuffer._elements.buffer);
2138
+ } else if (this._extensions.oes_element_index_uint && type === this.UNSIGNED_INT) {
2139
+ if (offset % 4) {
2140
+ this.setError(this.INVALID_OPERATION);
2141
+ return;
2142
+ }
2143
+ offset >>= 2;
2144
+ elementData = new Uint32Array(elementBuffer._elements.buffer);
2145
+ } else if (type === this.UNSIGNED_BYTE) {
2146
+ elementData = elementBuffer._elements;
2147
+ } else {
2148
+ this.setError(this.INVALID_ENUM);
2149
+ return;
2150
+ }
2151
+ let reducedCount = count;
2152
+ switch (mode) {
2153
+ case this.TRIANGLES:
2154
+ if (count % 3) {
2155
+ reducedCount -= count % 3;
2156
+ }
2157
+ break;
2158
+ case this.LINES:
2159
+ if (count % 2) {
2160
+ reducedCount -= count % 2;
2161
+ }
2162
+ break;
2163
+ case this.POINTS:
2164
+ break;
2165
+ case this.LINE_LOOP:
2166
+ case this.LINE_STRIP:
2167
+ if (count < 2) {
2168
+ this.setError(this.INVALID_OPERATION);
2169
+ return;
2170
+ }
2171
+ break;
2172
+ case this.TRIANGLE_FAN:
2173
+ case this.TRIANGLE_STRIP:
2174
+ if (count < 3) {
2175
+ this.setError(this.INVALID_OPERATION);
2176
+ return;
2177
+ }
2178
+ break;
2179
+ default:
2180
+ this.setError(this.INVALID_ENUM);
2181
+ return;
2182
+ }
2183
+ if (!this._framebufferOk()) {
2184
+ return;
2185
+ }
2186
+ if (count === 0) {
2187
+ this._checkVertexAttribState(0);
2188
+ return;
2189
+ }
2190
+ if (count + offset >>> 0 > elementData.length) {
2191
+ this.setError(this.INVALID_OPERATION);
2192
+ return;
2193
+ }
2194
+ let maxIndex = -1;
2195
+ for (let i = offset; i < offset + count; ++i) {
2196
+ maxIndex = Math.max(maxIndex, elementData[i]);
2197
+ }
2198
+ if (maxIndex < 0) {
2199
+ this._checkVertexAttribState(0);
2200
+ return;
2201
+ }
2202
+ if (this._checkVertexAttribState(maxIndex)) {
2203
+ if (reducedCount > 0) {
2204
+ this._gl.drawElements(mode, reducedCount, type, ioffset);
2205
+ }
2206
+ }
2207
+ }
2208
+ enable(cap = 0) {
2209
+ return this._gl.enable(cap);
2210
+ }
2211
+ enableVertexAttribArray(index) {
2212
+ if (index < 0 || index >= this._vertexObjectState._attribs.length) {
2213
+ this.setError(this.INVALID_VALUE);
2214
+ return;
2215
+ }
2216
+ this._gl.enableVertexAttribArray(index);
2217
+ this._vertexObjectState._attribs[index]._isPointer = true;
2218
+ }
2219
+ finish() {
2220
+ return this._gl.finish();
2221
+ }
2222
+ flush() {
2223
+ return this._gl.flush();
2224
+ }
2225
+ framebufferRenderbuffer(target, attachment, renderbufferTarget, renderbuffer) {
2226
+ if (!checkObject(renderbuffer)) {
2227
+ throw new TypeError("framebufferRenderbuffer(GLenum, GLenum, GLenum, WebGLRenderbuffer)");
2228
+ }
2229
+ if (target !== this.FRAMEBUFFER || !this._validFramebufferAttachment(attachment) || renderbufferTarget !== this.RENDERBUFFER) {
2230
+ this.setError(this.INVALID_ENUM);
2231
+ return;
2232
+ }
2233
+ const framebuffer = this._activeFramebuffer;
2234
+ if (!framebuffer) {
2235
+ this.setError(this.INVALID_OPERATION);
2236
+ return;
2237
+ }
2238
+ if (renderbuffer && !this._checkWrapper(renderbuffer, WebGLRenderbuffer)) {
2239
+ return;
2240
+ }
2241
+ framebuffer._setAttachment(renderbuffer, attachment);
2242
+ this._updateFramebufferAttachments(framebuffer);
2243
+ }
2244
+ framebufferTexture2D(target, attachment, textarget, texture, level = 0) {
2245
+ target |= 0;
2246
+ attachment |= 0;
2247
+ textarget |= 0;
2248
+ level |= 0;
2249
+ if (!checkObject(texture)) {
2250
+ throw new TypeError("framebufferTexture2D(GLenum, GLenum, GLenum, WebGLTexture, GLint)");
2251
+ }
2252
+ if (target !== this.FRAMEBUFFER || !this._validFramebufferAttachment(attachment)) {
2253
+ this.setError(this.INVALID_ENUM);
2254
+ return;
2255
+ }
2256
+ if (level !== 0) {
2257
+ this.setError(this.INVALID_VALUE);
2258
+ return;
2259
+ }
2260
+ if (texture && !this._checkWrapper(texture, WebGLTexture)) {
2261
+ return;
2262
+ }
2263
+ if (textarget === this.TEXTURE_2D) {
2264
+ if (texture && texture._binding !== this.TEXTURE_2D) {
2265
+ this.setError(this.INVALID_OPERATION);
2266
+ return;
2267
+ }
2268
+ } else if (this._validCubeTarget(textarget)) {
2269
+ if (texture && texture._binding !== this.TEXTURE_CUBE_MAP) {
2270
+ this.setError(this.INVALID_OPERATION);
2271
+ return;
2272
+ }
2273
+ } else {
2274
+ this.setError(this.INVALID_ENUM);
2275
+ return;
2276
+ }
2277
+ const framebuffer = this._activeFramebuffer;
2278
+ if (!framebuffer) {
2279
+ this.setError(this.INVALID_OPERATION);
2280
+ return;
2281
+ }
2282
+ framebuffer._attachmentLevel[attachment] = level;
2283
+ framebuffer._attachmentFace[attachment] = textarget;
2284
+ framebuffer._setAttachment(texture, attachment);
2285
+ this._updateFramebufferAttachments(framebuffer);
2286
+ }
2287
+ frontFace(mode = 0) {
2288
+ return this._gl.frontFace(mode);
2289
+ }
2290
+ generateMipmap(target = 0) {
2291
+ return this._gl.generateMipmap(target);
2292
+ }
2293
+ getActiveAttrib(program, index) {
2294
+ if (!checkObject(program)) {
2295
+ throw new TypeError("getActiveAttrib(WebGLProgram)");
2296
+ } else if (!program) {
2297
+ throw new TypeError("getActiveAttrib(WebGLProgram, GLuint)");
2298
+ } else if (this._checkWrapper(program, WebGLProgram)) {
2299
+ const maxCount = program._linkStatus ? program._attributes.length : this._gl.getProgramParameter(program._ | 0, this.ACTIVE_ATTRIBUTES);
2300
+ if (index >= maxCount) {
2301
+ this._gl.getError();
2302
+ this.setError(this.INVALID_VALUE);
2303
+ return null;
2304
+ }
2305
+ const info = this._gl.getActiveAttrib(program._ | 0, index | 0);
2306
+ if (info) {
2307
+ return new WebGLActiveInfo(info);
2308
+ }
2309
+ }
2310
+ return null;
2311
+ }
2312
+ getActiveUniform(program, index) {
2313
+ if (!checkObject(program)) {
2314
+ throw new TypeError("getActiveUniform(WebGLProgram, GLint)");
2315
+ } else if (!program) {
2316
+ throw new TypeError("getActiveUniform(WebGLProgram, GLuint)");
2317
+ } else if (this._checkWrapper(program, WebGLProgram)) {
2318
+ const maxCount = program._linkStatus ? program._uniforms.length : this._gl.getProgramParameter(program._ | 0, this.ACTIVE_UNIFORMS);
2319
+ if (index >= maxCount) {
2320
+ this.setError(this.INVALID_VALUE);
2321
+ return null;
2322
+ }
2323
+ const info = this._gl.getActiveUniform(program._ | 0, index | 0);
2324
+ if (info) {
2325
+ return new WebGLActiveInfo(info);
2326
+ }
2327
+ }
2328
+ return null;
2329
+ }
2330
+ getAttachedShaders(program) {
2331
+ if (!checkObject(program) || typeof program === "object" && program !== null && !(program instanceof WebGLProgram)) {
2332
+ throw new TypeError("getAttachedShaders(WebGLProgram)");
2333
+ }
2334
+ if (!program) {
2335
+ this.setError(this.INVALID_VALUE);
2336
+ } else if (this._checkWrapper(program, WebGLProgram)) {
2337
+ return program._references.filter((r) => r instanceof WebGLShader);
2338
+ }
2339
+ return null;
2340
+ }
2341
+ getAttribLocation(program, name) {
2342
+ if (!checkObject(program)) {
2343
+ throw new TypeError("getAttribLocation(WebGLProgram, String)");
2344
+ }
2345
+ name += "";
2346
+ if (!isValidString(name) || name.length > MAX_ATTRIBUTE_LENGTH) {
2347
+ this.setError(this.INVALID_VALUE);
2348
+ } else if (this._checkWrapper(program, WebGLProgram)) {
2349
+ return this._gl.getAttribLocation(program._ | 0, name + "");
2350
+ }
2351
+ return -1;
2352
+ }
2353
+ getBufferParameter(target = 0, pname = 0) {
2354
+ if (target !== this.ARRAY_BUFFER && target !== this.ELEMENT_ARRAY_BUFFER) {
2355
+ this.setError(this.INVALID_ENUM);
2356
+ return null;
2357
+ }
2358
+ switch (pname) {
2359
+ case this.BUFFER_SIZE:
2360
+ case this.BUFFER_USAGE:
2361
+ return this._gl.getBufferParameteriv(target | 0, pname | 0)[0];
2362
+ default:
2363
+ this.setError(this.INVALID_ENUM);
2364
+ return null;
2365
+ }
2366
+ }
2367
+ getError() {
2368
+ return this._gl.getError();
2369
+ }
2370
+ setError(error) {
2371
+ this._gl.setError(error);
2372
+ }
2373
+ getFramebufferAttachmentParameter(target = 0, attachment = 0, pname = 0) {
2374
+ if (target !== this.FRAMEBUFFER || !this._validFramebufferAttachment(attachment)) {
2375
+ this.setError(this.INVALID_ENUM);
2376
+ return null;
2377
+ }
2378
+ const framebuffer = this._activeFramebuffer;
2379
+ if (!framebuffer) {
2380
+ this.setError(this.INVALID_OPERATION);
2381
+ return null;
2382
+ }
2383
+ const object = framebuffer._attachments[attachment];
2384
+ if (object === null) {
2385
+ if (pname === this.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE) {
2386
+ return this.NONE;
2387
+ }
2388
+ } else if (object instanceof WebGLTexture) {
2389
+ switch (pname) {
2390
+ case this.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
2391
+ return object;
2392
+ case this.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
2393
+ return this.TEXTURE;
2394
+ case this.FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
2395
+ return framebuffer._attachmentLevel[attachment];
2396
+ case this.FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE: {
2397
+ const face = framebuffer._attachmentFace[attachment];
2398
+ if (face === this.TEXTURE_2D) {
2399
+ return 0;
2400
+ }
2401
+ return face;
2402
+ }
2403
+ }
2404
+ } else if (object instanceof WebGLRenderbuffer) {
2405
+ switch (pname) {
2406
+ case this.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
2407
+ return object;
2408
+ case this.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
2409
+ return this.RENDERBUFFER;
2410
+ }
2411
+ }
2412
+ this.setError(this.INVALID_ENUM);
2413
+ return null;
2414
+ }
2415
+ getParameter(pname = 0) {
2416
+ switch (pname) {
2417
+ case this.ARRAY_BUFFER_BINDING:
2418
+ return this._vertexGlobalState._arrayBufferBinding;
2419
+ case this.ELEMENT_ARRAY_BUFFER_BINDING:
2420
+ return this._vertexObjectState._elementArrayBufferBinding;
2421
+ case this.CURRENT_PROGRAM:
2422
+ return this._activeProgram;
2423
+ case this.FRAMEBUFFER_BINDING:
2424
+ return this._activeFramebuffer;
2425
+ case this.RENDERBUFFER_BINDING:
2426
+ return this._activeRenderbuffer;
2427
+ case this.TEXTURE_BINDING_2D:
2428
+ return this._getActiveTextureUnit()._bind2D;
2429
+ case this.TEXTURE_BINDING_CUBE_MAP:
2430
+ return this._getActiveTextureUnit()._bindCube;
2431
+ case this.VERSION:
2432
+ return "WebGL 1.0 " + VERSION;
2433
+ case this.VENDOR:
2434
+ return "";
2435
+ case this.RENDERER:
2436
+ return "ANGLE";
2437
+ case this.SHADING_LANGUAGE_VERSION:
2438
+ return "WebGL GLSL ES 1.0 ";
2439
+ case this.COMPRESSED_TEXTURE_FORMATS:
2440
+ return new Uint32Array(0);
2441
+ // Int arrays — tracked in JS (native getParameterx crashes for array returns)
2442
+ case this.SCISSOR_BOX:
2443
+ return new Int32Array(this._scissorBox);
2444
+ case this.VIEWPORT:
2445
+ return new Int32Array(this._viewport);
2446
+ case this.MAX_VIEWPORT_DIMS:
2447
+ return new Int32Array([32767, 32767]);
2448
+ // Float arrays
2449
+ case this.ALIASED_LINE_WIDTH_RANGE:
2450
+ case this.ALIASED_POINT_SIZE_RANGE:
2451
+ return new Float32Array([1, 1]);
2452
+ case this.DEPTH_RANGE:
2453
+ return new Float32Array([0, 1]);
2454
+ case this.BLEND_COLOR:
2455
+ case this.COLOR_CLEAR_VALUE:
2456
+ return new Float32Array(this._gl.getParameterfv(pname, 4));
2457
+ case this.COLOR_WRITEMASK: {
2458
+ const iv = this._gl.getParameteriv(pname, 4);
2459
+ return [!!iv[0], !!iv[1], !!iv[2], !!iv[3]];
2460
+ }
2461
+ case this.DEPTH_CLEAR_VALUE:
2462
+ case this.LINE_WIDTH:
2463
+ case this.POLYGON_OFFSET_FACTOR:
2464
+ case this.POLYGON_OFFSET_UNITS:
2465
+ case this.SAMPLE_COVERAGE_VALUE:
2466
+ return this._gl.getParameterf(pname);
2467
+ case this.BLEND:
2468
+ case this.CULL_FACE:
2469
+ case this.DEPTH_TEST:
2470
+ case this.DEPTH_WRITEMASK:
2471
+ case this.DITHER:
2472
+ case this.POLYGON_OFFSET_FILL:
2473
+ case this.SAMPLE_COVERAGE_INVERT:
2474
+ case this.SCISSOR_TEST:
2475
+ case this.STENCIL_TEST:
2476
+ case this.UNPACK_PREMULTIPLY_ALPHA_WEBGL:
2477
+ return !!this._gl.getParameteri(pname);
2478
+ case this.UNPACK_FLIP_Y_WEBGL:
2479
+ return this._unpackFlipY;
2480
+ case this.ACTIVE_TEXTURE:
2481
+ case this.ALPHA_BITS:
2482
+ case this.BLEND_DST_ALPHA:
2483
+ case this.BLEND_DST_RGB:
2484
+ case this.BLEND_EQUATION_ALPHA:
2485
+ case this.BLEND_EQUATION_RGB:
2486
+ case this.BLEND_SRC_ALPHA:
2487
+ case this.BLEND_SRC_RGB:
2488
+ case this.BLUE_BITS:
2489
+ case this.CULL_FACE_MODE:
2490
+ case this.DEPTH_BITS:
2491
+ case this.DEPTH_FUNC:
2492
+ case this.FRONT_FACE:
2493
+ case this.GENERATE_MIPMAP_HINT:
2494
+ case this.GREEN_BITS:
2495
+ case this.MAX_COMBINED_TEXTURE_IMAGE_UNITS:
2496
+ case this.MAX_CUBE_MAP_TEXTURE_SIZE:
2497
+ case this.MAX_FRAGMENT_UNIFORM_VECTORS:
2498
+ case this.MAX_RENDERBUFFER_SIZE:
2499
+ case this.MAX_TEXTURE_IMAGE_UNITS:
2500
+ case this.MAX_TEXTURE_SIZE:
2501
+ case this.MAX_VARYING_VECTORS:
2502
+ case this.MAX_VERTEX_ATTRIBS:
2503
+ case this.MAX_VERTEX_TEXTURE_IMAGE_UNITS:
2504
+ case this.MAX_VERTEX_UNIFORM_VECTORS:
2505
+ case this.PACK_ALIGNMENT:
2506
+ case this.RED_BITS:
2507
+ case this.SAMPLE_BUFFERS:
2508
+ case this.SAMPLES:
2509
+ case this.STENCIL_BACK_FAIL:
2510
+ case this.STENCIL_BACK_FUNC:
2511
+ case this.STENCIL_BACK_PASS_DEPTH_FAIL:
2512
+ case this.STENCIL_BACK_PASS_DEPTH_PASS:
2513
+ case this.STENCIL_BACK_REF:
2514
+ case this.STENCIL_BACK_VALUE_MASK:
2515
+ case this.STENCIL_BACK_WRITEMASK:
2516
+ case this.STENCIL_BITS:
2517
+ case this.STENCIL_CLEAR_VALUE:
2518
+ case this.STENCIL_FAIL:
2519
+ case this.STENCIL_FUNC:
2520
+ case this.STENCIL_PASS_DEPTH_FAIL:
2521
+ case this.STENCIL_PASS_DEPTH_PASS:
2522
+ case this.STENCIL_REF:
2523
+ case this.STENCIL_VALUE_MASK:
2524
+ case this.STENCIL_WRITEMASK:
2525
+ case this.SUBPIXEL_BITS:
2526
+ case this.UNPACK_ALIGNMENT:
2527
+ case this.UNPACK_COLORSPACE_CONVERSION_WEBGL:
2528
+ return this._gl.getParameteri(pname);
2529
+ case this.IMPLEMENTATION_COLOR_READ_FORMAT:
2530
+ case this.IMPLEMENTATION_COLOR_READ_TYPE:
2531
+ return this._getParameterDirect(pname);
2532
+ default:
2533
+ if (this._extensions.webgl_draw_buffers) {
2534
+ const ext = this._extensions.webgl_draw_buffers;
2535
+ switch (pname) {
2536
+ case ext.DRAW_BUFFER0_WEBGL:
2537
+ case ext.DRAW_BUFFER1_WEBGL:
2538
+ case ext.DRAW_BUFFER2_WEBGL:
2539
+ case ext.DRAW_BUFFER3_WEBGL:
2540
+ case ext.DRAW_BUFFER4_WEBGL:
2541
+ case ext.DRAW_BUFFER5_WEBGL:
2542
+ case ext.DRAW_BUFFER6_WEBGL:
2543
+ case ext.DRAW_BUFFER7_WEBGL:
2544
+ case ext.DRAW_BUFFER8_WEBGL:
2545
+ case ext.DRAW_BUFFER9_WEBGL:
2546
+ case ext.DRAW_BUFFER10_WEBGL:
2547
+ case ext.DRAW_BUFFER11_WEBGL:
2548
+ case ext.DRAW_BUFFER12_WEBGL:
2549
+ case ext.DRAW_BUFFER13_WEBGL:
2550
+ case ext.DRAW_BUFFER14_WEBGL:
2551
+ case ext.DRAW_BUFFER15_WEBGL:
2552
+ if (ext._buffersState.length === 1 && ext._buffersState[0] === this.BACK) {
2553
+ return this.BACK;
2554
+ }
2555
+ return this._getParameterDirect(pname);
2556
+ case ext.MAX_DRAW_BUFFERS_WEBGL:
2557
+ case ext.MAX_COLOR_ATTACHMENTS_WEBGL:
2558
+ return this._getParameterDirect(pname);
2559
+ }
2560
+ }
2561
+ if (this._extensions.oes_standard_derivatives && pname === this._extensions.oes_standard_derivatives.FRAGMENT_SHADER_DERIVATIVE_HINT_OES) {
2562
+ return this._getParameterDirect(pname);
2563
+ }
2564
+ if (this._extensions.ext_texture_filter_anisotropic && pname === this._extensions.ext_texture_filter_anisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT) {
2565
+ return this._getParameterDirect(pname);
2566
+ }
2567
+ if (this._extensions.oes_vertex_array_object && pname === this._extensions.oes_vertex_array_object.VERTEX_ARRAY_BINDING_OES) {
2568
+ return this._extensions.oes_vertex_array_object._activeVertexArrayObject;
2569
+ }
2570
+ this.setError(this.INVALID_ENUM);
2571
+ return null;
2572
+ }
2573
+ }
2574
+ getProgramInfoLog(program) {
2575
+ if (!checkObject(program)) {
2576
+ throw new TypeError("getProgramInfoLog(WebGLProgram)");
2577
+ } else if (this._checkWrapper(program, WebGLProgram)) {
2578
+ return program._linkInfoLog;
2579
+ }
2580
+ return null;
2581
+ }
2582
+ getProgramParameter(program, pname = 0) {
2583
+ if (!checkObject(program)) {
2584
+ throw new TypeError("getProgramParameter(WebGLProgram, GLenum)");
2585
+ } else if (this._checkWrapper(program, WebGLProgram)) {
2586
+ switch (pname) {
2587
+ case this.DELETE_STATUS:
2588
+ return program._pendingDelete;
2589
+ case this.LINK_STATUS:
2590
+ return program._linkStatus;
2591
+ case this.VALIDATE_STATUS:
2592
+ return !!this._gl.getProgramParameter(program._, pname);
2593
+ case this.ATTACHED_SHADERS:
2594
+ return this._gl.getProgramParameter(program._, pname);
2595
+ case this.ACTIVE_ATTRIBUTES:
2596
+ return program._linkStatus ? program._attributes.length : this._gl.getProgramParameter(program._, pname);
2597
+ case this.ACTIVE_UNIFORMS:
2598
+ return program._linkStatus ? program._uniforms.length : this._gl.getProgramParameter(program._, pname);
2599
+ }
2600
+ this.setError(this.INVALID_ENUM);
2601
+ }
2602
+ return null;
2603
+ }
2604
+ getRenderbufferParameter(target = 0, pname = 0) {
2605
+ if (target !== this.RENDERBUFFER) {
2606
+ this.setError(this.INVALID_ENUM);
2607
+ return null;
2608
+ }
2609
+ const renderbuffer = this._activeRenderbuffer;
2610
+ if (!renderbuffer) {
2611
+ this.setError(this.INVALID_OPERATION);
2612
+ return null;
2613
+ }
2614
+ switch (pname) {
2615
+ case this.RENDERBUFFER_INTERNAL_FORMAT:
2616
+ return renderbuffer._format;
2617
+ case this.RENDERBUFFER_WIDTH:
2618
+ return renderbuffer._width;
2619
+ case this.RENDERBUFFER_HEIGHT:
2620
+ return renderbuffer._height;
2621
+ case this.MAX_RENDERBUFFER_SIZE:
2622
+ // TODO?
2623
+ case this.RENDERBUFFER_RED_SIZE:
2624
+ case this.RENDERBUFFER_GREEN_SIZE:
2625
+ case this.RENDERBUFFER_BLUE_SIZE:
2626
+ case this.RENDERBUFFER_ALPHA_SIZE:
2627
+ case this.RENDERBUFFER_DEPTH_SIZE:
2628
+ case this.RENDERBUFFER_STENCIL_SIZE:
2629
+ return this._gl.getRenderbufferParameter(target, pname);
2630
+ }
2631
+ this.setError(this.INVALID_ENUM);
2632
+ return null;
2633
+ }
2634
+ getShaderInfoLog(shader) {
2635
+ if (!checkObject(shader)) {
2636
+ throw new TypeError("getShaderInfoLog(WebGLShader)");
2637
+ } else if (this._checkWrapper(shader, WebGLShader)) {
2638
+ return shader._compileInfo;
2639
+ }
2640
+ return null;
2641
+ }
2642
+ getShaderParameter(shader, pname = 0) {
2643
+ if (!checkObject(shader)) {
2644
+ throw new TypeError("getShaderParameter(WebGLShader, GLenum)");
2645
+ } else if (this._checkWrapper(shader, WebGLShader)) {
2646
+ switch (pname) {
2647
+ case this.DELETE_STATUS:
2648
+ return shader._pendingDelete;
2649
+ case this.COMPILE_STATUS:
2650
+ return shader._compileStatus;
2651
+ case this.SHADER_TYPE:
2652
+ return shader._type;
2653
+ }
2654
+ this.setError(this.INVALID_ENUM);
2655
+ }
2656
+ return null;
2657
+ }
2658
+ getShaderPrecisionFormat(shaderType = 0, precisionType = 0) {
2659
+ if (!(shaderType === this.FRAGMENT_SHADER || shaderType === this.VERTEX_SHADER) || !(precisionType === this.LOW_FLOAT || precisionType === this.MEDIUM_FLOAT || precisionType === this.HIGH_FLOAT || precisionType === this.LOW_INT || precisionType === this.MEDIUM_INT || precisionType === this.HIGH_INT)) {
2660
+ this.setError(this.INVALID_ENUM);
2661
+ return null;
2662
+ }
2663
+ const format = this._gl.getShaderPrecisionFormat(shaderType, precisionType);
2664
+ if (!format) {
2665
+ return null;
2666
+ }
2667
+ return new WebGLShaderPrecisionFormat(format);
2668
+ }
2669
+ getShaderSource(shader) {
2670
+ if (!checkObject(shader)) {
2671
+ throw new TypeError("Input to getShaderSource must be an object");
2672
+ } else if (this._checkWrapper(shader, WebGLShader)) {
2673
+ return shader._source;
2674
+ }
2675
+ return null;
2676
+ }
2677
+ getSupportedExtensions() {
2678
+ const exts = [
2679
+ "ANGLE_instanced_arrays",
2680
+ "STACKGL_resize_drawingbuffer",
2681
+ "STACKGL_destroy_context"
2682
+ ];
2683
+ const supportedExts = this._gl.getSupportedExtensions();
2684
+ if (!supportedExts) {
2685
+ return exts;
2686
+ }
2687
+ if (supportedExts.indexOf("GL_OES_element_index_uint") >= 0) {
2688
+ exts.push("OES_element_index_uint");
2689
+ }
2690
+ if (supportedExts.indexOf("GL_OES_standard_derivatives") >= 0) {
2691
+ exts.push("OES_standard_derivatives");
2692
+ }
2693
+ if (supportedExts.indexOf("GL_OES_texture_float") >= 0) {
2694
+ exts.push("OES_texture_float");
2695
+ }
2696
+ if (supportedExts.indexOf("GL_OES_texture_float_linear") >= 0) {
2697
+ exts.push("OES_texture_float_linear");
2698
+ }
2699
+ if (supportedExts.indexOf("GL_OES_texture_half_float") >= 0 || supportedExts.indexOf("GL_ARB_half_float_pixel") >= 0) {
2700
+ exts.push("OES_texture_half_float");
2701
+ }
2702
+ if (supportedExts.indexOf("GL_EXT_color_buffer_float") >= 0 || supportedExts.indexOf("GL_ARB_color_buffer_float") >= 0) {
2703
+ exts.push("EXT_color_buffer_float");
2704
+ }
2705
+ if (supportedExts.indexOf("GL_EXT_color_buffer_half_float") >= 0) {
2706
+ exts.push("EXT_color_buffer_half_float");
2707
+ }
2708
+ if (supportedExts.indexOf("EXT_draw_buffers") >= 0) {
2709
+ exts.push("WEBGL_draw_buffers");
2710
+ }
2711
+ if (supportedExts.indexOf("EXT_blend_minmax") >= 0) {
2712
+ exts.push("EXT_blend_minmax");
2713
+ }
2714
+ if (supportedExts.indexOf("EXT_texture_filter_anisotropic") >= 0) {
2715
+ exts.push("EXT_texture_filter_anisotropic");
2716
+ }
2717
+ if (supportedExts.indexOf("GL_OES_vertex_array_object") >= 0) {
2718
+ exts.push("OES_vertex_array_object");
2719
+ }
2720
+ return exts;
2721
+ }
2722
+ _getTexParameterDirect(target = 0, pname = 0) {
2723
+ return this._gl.getTexParameterx(target, pname)?.unpack();
2724
+ }
2725
+ getTexParameter(target = 0, pname = 0) {
2726
+ if (!this._checkTextureTarget(target)) {
2727
+ return null;
2728
+ }
2729
+ const unit = this._getActiveTextureUnit();
2730
+ if (target === this.TEXTURE_2D && !unit._bind2D || target === this.TEXTURE_CUBE_MAP && !unit._bindCube) {
2731
+ this.setError(this.INVALID_OPERATION);
2732
+ return null;
2733
+ }
2734
+ switch (pname) {
2735
+ case this.TEXTURE_MAG_FILTER:
2736
+ case this.TEXTURE_MIN_FILTER:
2737
+ case this.TEXTURE_WRAP_S:
2738
+ case this.TEXTURE_WRAP_T:
2739
+ return this._getTexParameterDirect(target, pname);
2740
+ }
2741
+ if (this._extensions.ext_texture_filter_anisotropic && pname === this._extensions.ext_texture_filter_anisotropic.TEXTURE_MAX_ANISOTROPY_EXT) {
2742
+ return this._getTexParameterDirect(target, pname);
2743
+ }
2744
+ this.setError(this.INVALID_ENUM);
2745
+ return null;
2746
+ }
2747
+ getUniform(program, location) {
2748
+ if (!checkObject(program) || !checkObject(location)) {
2749
+ throw new TypeError("getUniform(WebGLProgram, WebGLUniformLocation)");
2750
+ } else if (!program) {
2751
+ this.setError(this.INVALID_VALUE);
2752
+ return null;
2753
+ } else if (!location) {
2754
+ return null;
2755
+ } else if (this._checkWrapper(program, WebGLProgram)) {
2756
+ if (!checkUniform(program, location)) {
2757
+ this.setError(this.INVALID_OPERATION);
2758
+ return null;
2759
+ }
2760
+ const data = this._gl.getUniform(program._ | 0, location._ | 0);
2761
+ if (!data) {
2762
+ return null;
2763
+ }
2764
+ switch (location._activeInfo.type) {
2765
+ case this.FLOAT:
2766
+ return data[0];
2767
+ case this.FLOAT_VEC2:
2768
+ return new Float32Array(data.slice(0, 2));
2769
+ case this.FLOAT_VEC3:
2770
+ return new Float32Array(data.slice(0, 3));
2771
+ case this.FLOAT_VEC4:
2772
+ return new Float32Array(data.slice(0, 4));
2773
+ case this.INT:
2774
+ return data[0] | 0;
2775
+ case this.INT_VEC2:
2776
+ return new Int32Array(data.slice(0, 2));
2777
+ case this.INT_VEC3:
2778
+ return new Int32Array(data.slice(0, 3));
2779
+ case this.INT_VEC4:
2780
+ return new Int32Array(data.slice(0, 4));
2781
+ case this.BOOL:
2782
+ return !!data[0];
2783
+ case this.BOOL_VEC2:
2784
+ return [!!data[0], !!data[1]];
2785
+ case this.BOOL_VEC3:
2786
+ return [!!data[0], !!data[1], !!data[2]];
2787
+ case this.BOOL_VEC4:
2788
+ return [!!data[0], !!data[1], !!data[2], !!data[3]];
2789
+ case this.FLOAT_MAT2:
2790
+ return new Float32Array(data.slice(0, 4));
2791
+ case this.FLOAT_MAT3:
2792
+ return new Float32Array(data.slice(0, 9));
2793
+ case this.FLOAT_MAT4:
2794
+ return new Float32Array(data.slice(0, 16));
2795
+ case this.SAMPLER_2D:
2796
+ case this.SAMPLER_CUBE:
2797
+ return data[0] | 0;
2798
+ default:
2799
+ return null;
2800
+ }
2801
+ }
2802
+ return null;
2803
+ }
2804
+ getUniformLocation(program, name) {
2805
+ if (!checkObject(program)) {
2806
+ throw new TypeError("getUniformLocation(WebGLProgram, String)");
2807
+ }
2808
+ name += "";
2809
+ if (!isValidString(name)) {
2810
+ this.setError(this.INVALID_VALUE);
2811
+ return null;
2812
+ }
2813
+ if (this._checkWrapper(program, WebGLProgram)) {
2814
+ const loc = this._gl.getUniformLocation(program._ | 0, name);
2815
+ if (loc !== null && loc >= 0) {
2816
+ let searchName = name;
2817
+ if (/\[\d+\]$/.test(name)) {
2818
+ searchName = name.replace(/\[\d+\]$/, "[0]");
2819
+ }
2820
+ let info = null;
2821
+ for (let i = 0; i < program._uniforms.length; ++i) {
2822
+ const infoItem = program._uniforms[i];
2823
+ if (infoItem.name === searchName) {
2824
+ info = {
2825
+ size: infoItem.size,
2826
+ type: infoItem.type,
2827
+ name: infoItem.name
2828
+ };
2829
+ }
2830
+ }
2831
+ if (!info) {
2832
+ return null;
2833
+ }
2834
+ const result = new WebGLUniformLocation(
2835
+ loc,
2836
+ program,
2837
+ info
2838
+ );
2839
+ if (/\[0\]$/.test(name)) {
2840
+ const baseName = name.replace(/\[0\]$/, "");
2841
+ const arrayLocs = [];
2842
+ this._saveError();
2843
+ for (let i = 0; this.getError() === this.NO_ERROR; ++i) {
2844
+ const xloc = this._gl.getUniformLocation(
2845
+ program._ | 0,
2846
+ baseName + "[" + i + "]"
2847
+ );
2848
+ if (this.getError() !== this.NO_ERROR || xloc == null || xloc < 0) {
2849
+ break;
2850
+ }
2851
+ arrayLocs.push(xloc);
2852
+ }
2853
+ this._restoreError(this.NO_ERROR);
2854
+ result._array = arrayLocs;
2855
+ } else if (name && /\[(\d+)\]$/.test(name)) {
2856
+ const _regexExec = /\[(\d+)\]$/.exec(name);
2857
+ if (!_regexExec || _regexExec.length <= 0) {
2858
+ return null;
2859
+ }
2860
+ const offset = +_regexExec[1];
2861
+ if (offset < 0 || offset >= info.size) {
2862
+ return null;
2863
+ }
2864
+ }
2865
+ return result;
2866
+ }
2867
+ }
2868
+ return null;
2869
+ }
2870
+ getVertexAttrib(index = 0, pname = 0) {
2871
+ if (index < 0 || index >= this._vertexObjectState._attribs.length) {
2872
+ this.setError(this.INVALID_VALUE);
2873
+ return null;
2874
+ }
2875
+ const attrib = this._vertexObjectState._attribs[index];
2876
+ const vertexAttribValue = this._vertexGlobalState._attribs[index]._data;
2877
+ const extInstancing = this._extensions.angle_instanced_arrays;
2878
+ if (extInstancing) {
2879
+ if (pname === extInstancing.VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE) {
2880
+ return attrib._divisor;
2881
+ }
2882
+ }
2883
+ switch (pname) {
2884
+ case this.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
2885
+ return attrib._pointerBuffer;
2886
+ case this.VERTEX_ATTRIB_ARRAY_ENABLED:
2887
+ return attrib._isPointer;
2888
+ case this.VERTEX_ATTRIB_ARRAY_SIZE:
2889
+ return attrib._inputSize;
2890
+ case this.VERTEX_ATTRIB_ARRAY_STRIDE:
2891
+ return attrib._inputStride;
2892
+ case this.VERTEX_ATTRIB_ARRAY_TYPE:
2893
+ return attrib._pointerType;
2894
+ case this.VERTEX_ATTRIB_ARRAY_NORMALIZED:
2895
+ return attrib._pointerNormal;
2896
+ case this.CURRENT_VERTEX_ATTRIB:
2897
+ return new Float32Array(vertexAttribValue);
2898
+ default:
2899
+ this.setError(this.INVALID_ENUM);
2900
+ return null;
2901
+ }
2902
+ }
2903
+ getVertexAttribOffset(index = 0, pname = 0) {
2904
+ if (index < 0 || index >= this._vertexObjectState._attribs.length) {
2905
+ this.setError(this.INVALID_VALUE);
2906
+ return -1;
2907
+ }
2908
+ if (pname === this.VERTEX_ATTRIB_ARRAY_POINTER) {
2909
+ return this._vertexObjectState._attribs[index]._pointerOffset;
2910
+ } else {
2911
+ this.setError(this.INVALID_ENUM);
2912
+ return -1;
2913
+ }
2914
+ }
2915
+ hint(target = 0, mode = 0) {
2916
+ if (!(target === this.GENERATE_MIPMAP_HINT || this._extensions.oes_standard_derivatives && target === this._extensions.oes_standard_derivatives.FRAGMENT_SHADER_DERIVATIVE_HINT_OES)) {
2917
+ this.setError(this.INVALID_ENUM);
2918
+ return;
2919
+ }
2920
+ if (mode !== this.FASTEST && mode !== this.NICEST && mode !== this.DONT_CARE) {
2921
+ this.setError(this.INVALID_ENUM);
2922
+ return;
2923
+ }
2924
+ return this._gl.hint(target, mode);
2925
+ }
2926
+ isBuffer(buffer) {
2927
+ if (!this._isObject(buffer, "isBuffer", WebGLBuffer)) return false;
2928
+ return this._gl.isBuffer(buffer?._);
2929
+ }
2930
+ isContextLost() {
2931
+ return false;
2932
+ }
2933
+ isEnabled(cap = 0) {
2934
+ return this._gl.isEnabled(cap);
2935
+ }
2936
+ isFramebuffer(framebuffer) {
2937
+ if (!this._isObject(framebuffer, "isFramebuffer", WebGLFramebuffer)) return false;
2938
+ return this._gl.isFramebuffer(framebuffer?._);
2939
+ }
2940
+ isProgram(program) {
2941
+ if (!this._isObject(program, "isProgram", WebGLProgram)) return false;
2942
+ return this._gl.isProgram(program?._);
2943
+ }
2944
+ isRenderbuffer(renderbuffer) {
2945
+ if (!this._isObject(renderbuffer, "isRenderbuffer", WebGLRenderbuffer)) return false;
2946
+ return this._gl.isRenderbuffer(renderbuffer?._);
2947
+ }
2948
+ isShader(shader) {
2949
+ if (!this._isObject(shader, "isShader", WebGLShader)) return false;
2950
+ return this._gl.isShader(shader?._);
2951
+ }
2952
+ isTexture(texture) {
2953
+ if (!this._isObject(texture, "isTexture", WebGLTexture)) return false;
2954
+ return this._gl.isTexture(texture?._);
2955
+ }
2956
+ lineWidth(width) {
2957
+ if (isNaN(width)) {
2958
+ this.setError(this.INVALID_VALUE);
2959
+ return;
2960
+ }
2961
+ return this._gl.lineWidth(+width);
2962
+ }
2963
+ linkProgram(program) {
2964
+ if (!checkObject(program)) {
2965
+ throw new TypeError("linkProgram(WebGLProgram)");
2966
+ }
2967
+ if (this._checkWrapper(program, WebGLProgram)) {
2968
+ program._linkCount += 1;
2969
+ program._attributes = [];
2970
+ const prevError = this.getError();
2971
+ for (const s of program._references) {
2972
+ if (s instanceof WebGLShader && s._needsRecompile) {
2973
+ this._gl.compileShader(s._ | 0);
2974
+ s._needsRecompile = false;
2975
+ }
2976
+ }
2977
+ this._gl.linkProgram(program._ | 0);
2978
+ const error = this.getError();
2979
+ if (error === this.NO_ERROR) {
2980
+ program._linkStatus = this._fixupLink(program);
2981
+ }
2982
+ this.getError();
2983
+ this.setError(prevError || error);
2984
+ }
2985
+ }
2986
+ /** The `WebGLRenderingContext.pixelStorei()` method of the WebGL API specifies the pixel storage modes. */
2987
+ pixelStorei(pname = 0, param = 0) {
2988
+ if (typeof param === "boolean") {
2989
+ param = param === false ? 0 : 1;
2990
+ }
2991
+ if (pname === this.UNPACK_ALIGNMENT) {
2992
+ if (param === 1 || param === 2 || param === 4 || param === 8) {
2993
+ this._unpackAlignment = param;
2994
+ } else {
2995
+ this.setError(this.INVALID_VALUE);
2996
+ return;
2997
+ }
2998
+ } else if (pname === this.PACK_ALIGNMENT) {
2999
+ if (param === 1 || param === 2 || param === 4 || param === 8) {
3000
+ this._packAlignment = param;
3001
+ } else {
3002
+ this.setError(this.INVALID_VALUE);
3003
+ return;
3004
+ }
3005
+ } else if (pname === this.UNPACK_COLORSPACE_CONVERSION_WEBGL) {
3006
+ if (!(param === this.NONE || param === this.BROWSER_DEFAULT_WEBGL)) {
3007
+ this.setError(this.INVALID_VALUE);
3008
+ return;
3009
+ }
3010
+ } else if (pname === this.UNPACK_FLIP_Y_WEBGL) {
3011
+ this._unpackFlipY = !!param;
3012
+ return;
3013
+ } else if (pname === this.UNPACK_PREMULTIPLY_ALPHA_WEBGL) {
3014
+ return;
3015
+ }
3016
+ return this._gl.pixelStorei(pname, param);
3017
+ }
3018
+ polygonOffset(factor, units) {
3019
+ return this._gl.polygonOffset(+factor, +units);
3020
+ }
3021
+ renderbufferStorage(target = 0, internalFormat = 0, width = 0, height = 0) {
3022
+ if (target !== this.RENDERBUFFER) {
3023
+ this.setError(this.INVALID_ENUM);
3024
+ return;
3025
+ }
3026
+ const renderbuffer = this._activeRenderbuffer;
3027
+ if (!renderbuffer) {
3028
+ this.setError(this.INVALID_OPERATION);
3029
+ return;
3030
+ }
3031
+ if (internalFormat !== this.RGBA4 && internalFormat !== this.RGB565 && internalFormat !== this.RGB5_A1 && internalFormat !== this.DEPTH_COMPONENT16 && internalFormat !== this.STENCIL_INDEX && internalFormat !== this.STENCIL_INDEX8 && internalFormat !== this.DEPTH_STENCIL) {
3032
+ this.setError(this.INVALID_ENUM);
3033
+ return;
3034
+ }
3035
+ this._saveError();
3036
+ this._gl.renderbufferStorage(
3037
+ target,
3038
+ internalFormat,
3039
+ width,
3040
+ height
3041
+ );
3042
+ const error = this.getError();
3043
+ this._restoreError(error);
3044
+ if (error !== this.NO_ERROR) {
3045
+ return;
3046
+ }
3047
+ renderbuffer._width = width;
3048
+ renderbuffer._height = height;
3049
+ renderbuffer._format = internalFormat;
3050
+ const activeFramebuffer = this._activeFramebuffer;
3051
+ if (activeFramebuffer) {
3052
+ let needsUpdate = false;
3053
+ const attachments = this._getAttachments();
3054
+ for (let i = 0; i < attachments.length; ++i) {
3055
+ if (activeFramebuffer._attachments[attachments[i]] === renderbuffer) {
3056
+ needsUpdate = true;
3057
+ break;
3058
+ }
3059
+ }
3060
+ if (needsUpdate) {
3061
+ this._updateFramebufferAttachments(this._activeFramebuffer);
3062
+ }
3063
+ }
3064
+ }
3065
+ resize(width = 0, height = 0) {
3066
+ width = width | 0;
3067
+ height = height | 0;
3068
+ if (!(width > 0 && height > 0)) {
3069
+ throw new Error("Invalid surface dimensions");
3070
+ } else if (width !== this.drawingBufferWidth || height !== this.drawingBufferHeight) {
3071
+ this._resizeDrawingBuffer(width, height);
3072
+ }
3073
+ }
3074
+ sampleCoverage(value, invert) {
3075
+ return this._gl.sampleCoverage(+value, !!invert);
3076
+ }
3077
+ scissor(x, y, width, height) {
3078
+ this._scissorBox[0] = x | 0;
3079
+ this._scissorBox[1] = y | 0;
3080
+ this._scissorBox[2] = width | 0;
3081
+ this._scissorBox[3] = height | 0;
3082
+ return this._gl.scissor(x | 0, y | 0, width | 0, height | 0);
3083
+ }
3084
+ shaderSource(shader, source) {
3085
+ if (!checkObject(shader)) {
3086
+ throw new TypeError("shaderSource(WebGLShader, String)");
3087
+ }
3088
+ if (!shader || !source && typeof source !== "string") {
3089
+ this.setError(this.INVALID_VALUE);
3090
+ return;
3091
+ }
3092
+ if (!isValidString(source)) {
3093
+ this.setError(this.INVALID_VALUE);
3094
+ } else if (this._checkWrapper(shader, WebGLShader)) {
3095
+ source = this._wrapShader(shader._type, source);
3096
+ this._gl.shaderSource(shader._ | 0, source);
3097
+ shader._source = source;
3098
+ shader._needsRecompile = true;
3099
+ }
3100
+ }
3101
+ stencilFunc(func, ref, mask) {
3102
+ this._checkStencil = true;
3103
+ return this._gl.stencilFunc(func | 0, ref | 0, mask | 0);
3104
+ }
3105
+ stencilFuncSeparate(face, func, ref, mask) {
3106
+ this._checkStencil = true;
3107
+ return this._gl.stencilFuncSeparate(face | 0, func | 0, ref | 0, mask | 0);
3108
+ }
3109
+ stencilMask(mask) {
3110
+ this._checkStencil = true;
3111
+ return this._gl.stencilMask(mask >>> 0);
3112
+ }
3113
+ stencilMaskSeparate(face, mask) {
3114
+ this._checkStencil = true;
3115
+ return this._gl.stencilMaskSeparate(face | 0, mask >>> 0);
3116
+ }
3117
+ stencilOp(fail, zfail, zpass) {
3118
+ this._checkStencil = true;
3119
+ return this._gl.stencilOp(fail | 0, zfail | 0, zpass | 0);
3120
+ }
3121
+ stencilOpSeparate(face, fail, zfail, zpass) {
3122
+ this._checkStencil = true;
3123
+ return this._gl.stencilOpSeparate(face | 0, fail | 0, zfail | 0, zpass | 0);
3124
+ }
3125
+ texParameterf(target = 0, pname = 0, param) {
3126
+ param = +param;
3127
+ if (this._checkTextureTarget(target)) {
3128
+ this._verifyTextureCompleteness(target, pname, param);
3129
+ switch (pname) {
3130
+ case this.TEXTURE_MIN_FILTER:
3131
+ case this.TEXTURE_MAG_FILTER:
3132
+ case this.TEXTURE_WRAP_S:
3133
+ case this.TEXTURE_WRAP_T:
3134
+ return this._gl.texParameterf(target, pname, param);
3135
+ }
3136
+ if (this._extensions.ext_texture_filter_anisotropic && pname === this._extensions.ext_texture_filter_anisotropic.TEXTURE_MAX_ANISOTROPY_EXT) {
3137
+ return this._gl.texParameterf(target, pname, param);
3138
+ }
3139
+ this.setError(this.INVALID_ENUM);
3140
+ }
3141
+ }
3142
+ texParameteri(target = 0, pname = 0, param = 0) {
3143
+ if (this._checkTextureTarget(target)) {
3144
+ this._verifyTextureCompleteness(target, pname, param);
3145
+ switch (pname) {
3146
+ case this.TEXTURE_MIN_FILTER:
3147
+ case this.TEXTURE_MAG_FILTER:
3148
+ case this.TEXTURE_WRAP_S:
3149
+ case this.TEXTURE_WRAP_T:
3150
+ return this._gl.texParameteri(target, pname, param);
3151
+ }
3152
+ if (this._extensions.ext_texture_filter_anisotropic && pname === this._extensions.ext_texture_filter_anisotropic.TEXTURE_MAX_ANISOTROPY_EXT) {
3153
+ return this._gl.texParameteri(target, pname, param);
3154
+ }
3155
+ this.setError(this.INVALID_ENUM);
3156
+ }
3157
+ }
3158
+ uniform1f(location, x) {
3159
+ if (!this._checkUniformValid(location, x, "uniform1f", 1, "f")) return;
3160
+ return this._gl.uniform1f(location?._ || 0, x);
3161
+ }
3162
+ uniform1i(location, x) {
3163
+ return this._gl.uniform1i(location?._ || 0, x);
3164
+ }
3165
+ uniform2f(location, x, y) {
3166
+ if (!this._checkUniformValid(location, x, "uniform2f", 2, "f")) return;
3167
+ return this._gl.uniform2f(location?._ || 0, x, y);
3168
+ }
3169
+ uniform2i(location, x, y) {
3170
+ if (!this._checkUniformValid(location, x, "uniform2i", 2, "i")) return;
3171
+ this._gl.uniform2i(location?._ || 0, x, y);
3172
+ }
3173
+ uniform3f(location, x, y, z) {
3174
+ if (!this._checkUniformValid(location, x, "uniform3f", 3, "f")) return;
3175
+ return this._gl.uniform3f(location?._ || 0, x, y, z);
3176
+ }
3177
+ uniform3i(location, x, y, z) {
3178
+ if (!this._checkUniformValid(location, x, "uniform3i", 3, "i")) return;
3179
+ return this._gl.uniform3i(location?._ || 0, x, y, z);
3180
+ }
3181
+ uniform4f(location, x, y, z, w) {
3182
+ if (!this._checkUniformValid(location, x, "uniform4f", 4, "f")) {
3183
+ console.error("uniform4f is not valid!");
3184
+ return;
3185
+ }
3186
+ return this._gl.uniform4f(location?._ || 0, x, y, z, w);
3187
+ }
3188
+ uniform4i(location, x, y, z, w) {
3189
+ if (!this._checkUniformValid(location, x, "uniform4i", 4, "i")) return;
3190
+ return this._gl.uniform4i(location?._ || 0, x, y, z, w);
3191
+ }
3192
+ useProgram(program) {
3193
+ if (!checkObject(program)) {
3194
+ throw new TypeError("useProgram(WebGLProgram)");
3195
+ } else if (!program) {
3196
+ this._switchActiveProgram(this._activeProgram);
3197
+ this._activeProgram = null;
3198
+ return this._gl.useProgram(0);
3199
+ } else if (this._checkWrapper(program, WebGLProgram)) {
3200
+ if (this._activeProgram !== program) {
3201
+ this._switchActiveProgram(this._activeProgram);
3202
+ this._activeProgram = program;
3203
+ program._refCount += 1;
3204
+ }
3205
+ return this._gl.useProgram(program._ | 0);
3206
+ }
3207
+ }
3208
+ validateProgram(program) {
3209
+ if (this._checkWrapper(program, WebGLProgram)) {
3210
+ this._gl.validateProgram(program._ | 0);
3211
+ const error = this.getError();
3212
+ if (error === this.NO_ERROR) {
3213
+ program._linkInfoLog = this._gl.getProgramInfoLog(program._ | 0);
3214
+ }
3215
+ this.getError();
3216
+ this.setError(error);
3217
+ }
3218
+ }
3219
+ vertexAttrib1f(index, x) {
3220
+ index |= 0;
3221
+ if (!this._checkVertexIndex(index)) return;
3222
+ const data = this._vertexGlobalState._attribs[index]._data;
3223
+ data[3] = 1;
3224
+ data[1] = data[2] = 0;
3225
+ data[0] = x;
3226
+ return this._gl.vertexAttrib1f(index | 0, +x);
3227
+ }
3228
+ vertexAttrib1fv(index, values) {
3229
+ index |= 0;
3230
+ if (!this._checkVertexIndex(index)) return;
3231
+ if (typeof values !== "object" || values === null || values.length < 1) {
3232
+ this.setError(this.INVALID_OPERATION);
3233
+ return;
3234
+ }
3235
+ const data = this._vertexGlobalState._attribs[index]._data;
3236
+ data[3] = 1;
3237
+ data[2] = 0;
3238
+ data[1] = 0;
3239
+ data[0] = values[0];
3240
+ return this._gl.vertexAttrib1f(index | 0, +values[0]);
3241
+ }
3242
+ vertexAttrib2f(index, x, y) {
3243
+ index |= 0;
3244
+ if (!this._checkVertexIndex(index)) return;
3245
+ const data = this._vertexGlobalState._attribs[index]._data;
3246
+ data[3] = 1;
3247
+ data[2] = 0;
3248
+ data[1] = y;
3249
+ data[0] = x;
3250
+ return this._gl.vertexAttrib2f(index | 0, +x, +y);
3251
+ }
3252
+ vertexAttrib2fv(index, values) {
3253
+ index |= 0;
3254
+ if (!this._checkVertexIndex(index)) return;
3255
+ if (typeof values !== "object" || values === null || values.length < 2) {
3256
+ this.setError(this.INVALID_OPERATION);
3257
+ return;
3258
+ }
3259
+ const data = this._vertexGlobalState._attribs[index]._data;
3260
+ data[3] = 1;
3261
+ data[2] = 0;
3262
+ data[1] = values[1];
3263
+ data[0] = values[0];
3264
+ return this._gl.vertexAttrib2f(index | 0, +values[0], +values[1]);
3265
+ }
3266
+ vertexAttrib3f(index, x, y, z) {
3267
+ index |= 0;
3268
+ if (!this._checkVertexIndex(index)) return;
3269
+ const data = this._vertexGlobalState._attribs[index]._data;
3270
+ data[3] = 1;
3271
+ data[2] = z;
3272
+ data[1] = y;
3273
+ data[0] = x;
3274
+ return this._gl.vertexAttrib3f(index | 0, +x, +y, +z);
3275
+ }
3276
+ vertexAttrib3fv(index, values) {
3277
+ index |= 0;
3278
+ if (!this._checkVertexIndex(index)) return;
3279
+ if (typeof values !== "object" || values === null || values.length < 3) {
3280
+ this.setError(this.INVALID_OPERATION);
3281
+ return;
3282
+ }
3283
+ const data = this._vertexGlobalState._attribs[index]._data;
3284
+ data[3] = 1;
3285
+ data[2] = values[2];
3286
+ data[1] = values[1];
3287
+ data[0] = values[0];
3288
+ return this._gl.vertexAttrib3f(index | 0, +values[0], +values[1], +values[2]);
3289
+ }
3290
+ vertexAttrib4f(index = 0, x, y, z, w) {
3291
+ if (!this._checkVertexIndex(index)) return;
3292
+ const data = this._vertexGlobalState._attribs[index]._data;
3293
+ data[3] = w;
3294
+ data[2] = z;
3295
+ data[1] = y;
3296
+ data[0] = x;
3297
+ return this._gl.vertexAttrib4f(index | 0, +x, +y, +z, +w);
3298
+ }
3299
+ vertexAttrib4fv(index, values) {
3300
+ index |= 0;
3301
+ if (!this._checkVertexIndex(index)) return;
3302
+ if (typeof values !== "object" || values === null || values.length < 4) {
3303
+ this.setError(this.INVALID_OPERATION);
3304
+ return;
3305
+ }
3306
+ const data = this._vertexGlobalState._attribs[index]._data;
3307
+ data[3] = values[3];
3308
+ data[2] = values[2];
3309
+ data[1] = values[1];
3310
+ data[0] = values[0];
3311
+ return this._gl.vertexAttrib4f(index | 0, +values[0], +values[1], +values[2], +values[3]);
3312
+ }
3313
+ vertexAttribPointer(index = 0, size = 0, type = 0, normalized = false, stride = 0, offset = 0) {
3314
+ if (stride < 0 || offset < 0) {
3315
+ this.setError(this.INVALID_VALUE);
3316
+ return;
3317
+ }
3318
+ if (stride < 0 || offset < 0 || index < 0 || index >= this._vertexObjectState._attribs.length || !(size === 1 || size === 2 || size === 3 || size === 4)) {
3319
+ this.setError(this.INVALID_VALUE);
3320
+ return;
3321
+ }
3322
+ if (this._vertexGlobalState._arrayBufferBinding === null) {
3323
+ this.setError(this.INVALID_OPERATION);
3324
+ return;
3325
+ }
3326
+ const byteSize = typeSize(this, type);
3327
+ if (byteSize === 0 || type === this.INT || type === this.UNSIGNED_INT) {
3328
+ this.setError(this.INVALID_ENUM);
3329
+ return;
3330
+ }
3331
+ if (stride > 255 || stride < 0) {
3332
+ this.setError(this.INVALID_VALUE);
3333
+ return;
3334
+ }
3335
+ if (stride % byteSize !== 0 || offset % byteSize !== 0) {
3336
+ this.setError(this.INVALID_OPERATION);
3337
+ return;
3338
+ }
3339
+ this._gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
3340
+ this._vertexObjectState.setVertexAttribPointer(
3341
+ /* buffer */
3342
+ this._vertexGlobalState._arrayBufferBinding,
3343
+ /* index */
3344
+ index,
3345
+ /* pointerSize */
3346
+ size * byteSize,
3347
+ /* pointerOffset */
3348
+ offset,
3349
+ /* pointerStride */
3350
+ stride || size * byteSize,
3351
+ /* pointerType */
3352
+ type,
3353
+ /* pointerNormal */
3354
+ normalized,
3355
+ /* inputStride */
3356
+ stride,
3357
+ /* inputSize */
3358
+ size
3359
+ );
3360
+ }
3361
+ viewport(x, y, width, height) {
3362
+ this._viewport[0] = x | 0;
3363
+ this._viewport[1] = y | 0;
3364
+ this._viewport[2] = width | 0;
3365
+ this._viewport[3] = height | 0;
3366
+ return this._gl.viewport(x, y, width, height);
3367
+ }
3368
+ }
3369
+ export {
3370
+ WebGLContextBase
3371
+ };