@gjsify/webgl 0.0.3 → 0.1.0

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