@gjsify/webgl 0.3.16 → 0.3.18

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 (55) hide show
  1. package/lib/esm/conformance/attribs.spec.js +3 -315
  2. package/lib/esm/conformance/buffers.spec.js +1 -220
  3. package/lib/esm/conformance/context.spec.js +3 -302
  4. package/lib/esm/conformance/programs.spec.js +3 -477
  5. package/lib/esm/conformance/rendering-basic.spec.js +3 -141
  6. package/lib/esm/conformance/rendering.spec.js +7 -514
  7. package/lib/esm/conformance/setup.js +1 -47
  8. package/lib/esm/conformance/state.spec.js +1 -365
  9. package/lib/esm/conformance/textures.spec.js +3 -337
  10. package/lib/esm/conformance/uniforms.spec.js +1 -484
  11. package/lib/esm/conformance-test.js +1 -25
  12. package/lib/esm/extensions/ext-blend-minmax.js +1 -18
  13. package/lib/esm/extensions/ext-color-buffer-float.js +1 -12
  14. package/lib/esm/extensions/ext-color-buffer-half-float.js +1 -12
  15. package/lib/esm/extensions/ext-texture-filter-anisotropic.js +1 -18
  16. package/lib/esm/extensions/oes-element-index-unit.js +1 -13
  17. package/lib/esm/extensions/oes-standard-derivatives.js +1 -17
  18. package/lib/esm/extensions/oes-texture-float-linear.js +1 -13
  19. package/lib/esm/extensions/oes-texture-float.js +1 -13
  20. package/lib/esm/extensions/oes-texture-half-float.js +1 -19
  21. package/lib/esm/extensions/stackgl-destroy-context.js +1 -12
  22. package/lib/esm/extensions/stackgl-resize-drawing-buffer.js +1 -12
  23. package/lib/esm/html-canvas-element.js +1 -65
  24. package/lib/esm/index.js +1 -33
  25. package/lib/esm/linkable.js +1 -50
  26. package/lib/esm/test-utils.js +4 -186
  27. package/lib/esm/test.js +1 -11
  28. package/lib/esm/types/index.js +1 -5
  29. package/lib/esm/utils.js +1 -201
  30. package/lib/esm/webgl-active-info.js +1 -11
  31. package/lib/esm/webgl-bridge.js +1 -167
  32. package/lib/esm/webgl-buffer.js +1 -19
  33. package/lib/esm/webgl-context-attributes.js +1 -24
  34. package/lib/esm/webgl-context-base.js +8 -3069
  35. package/lib/esm/webgl-drawing-buffer-wrapper.js +1 -11
  36. package/lib/esm/webgl-framebuffer.js +1 -110
  37. package/lib/esm/webgl-program.js +1 -27
  38. package/lib/esm/webgl-query.js +1 -17
  39. package/lib/esm/webgl-renderbuffer.js +1 -25
  40. package/lib/esm/webgl-rendering-context.js +1 -175
  41. package/lib/esm/webgl-sampler.js +1 -17
  42. package/lib/esm/webgl-shader-precision-format.js +1 -11
  43. package/lib/esm/webgl-shader.js +1 -25
  44. package/lib/esm/webgl-sync.js +1 -17
  45. package/lib/esm/webgl-texture-unit.js +1 -13
  46. package/lib/esm/webgl-texture.js +1 -23
  47. package/lib/esm/webgl-transform-feedback.js +1 -17
  48. package/lib/esm/webgl-uniform-location.js +1 -15
  49. package/lib/esm/webgl-vertex-array-object.js +1 -23
  50. package/lib/esm/webgl-vertex-attribute.js +1 -151
  51. package/lib/esm/webgl1.spec.js +10 -1044
  52. package/lib/esm/webgl2-rendering-context.js +1 -1218
  53. package/lib/esm/webgl2.spec.js +45 -1288
  54. package/lib/types/webgl-bridge.d.ts +9 -9
  55. package/package.json +9 -9
@@ -1,1218 +1 @@
1
- import { Uint8ArrayToVariant, arrayToUint8Array, checkObject, convertPixels, extractImageData, premultiplyAlpha, vertexCount } from "./utils.js";
2
- import { WebGLActiveInfo } from "./webgl-active-info.js";
3
- import { WebGLFramebuffer } from "./webgl-framebuffer.js";
4
- import { WebGLRenderbuffer } from "./webgl-renderbuffer.js";
5
- import { WebGLTexture } from "./webgl-texture.js";
6
- import { WebGLContextBase } from "./webgl-context-base.js";
7
- import { WebGLQuery } from "./webgl-query.js";
8
- import { WebGLSampler } from "./webgl-sampler.js";
9
- import { WebGLSync } from "./webgl-sync.js";
10
- import { WebGLTransformFeedback } from "./webgl-transform-feedback.js";
11
- import { WebGLVertexArrayObject } from "./webgl-vertex-array-object.js";
12
- import Gwebgl from "@girs/gwebgl-0.1";
13
- import GdkPixbuf from "gi://GdkPixbuf?version=2.0";
14
- import { warnNotImplemented } from "@gjsify/utils";
15
-
16
- //#region src/ts/webgl2-rendering-context.ts
17
- var WebGL2RenderingContext = class WebGL2RenderingContext extends WebGLContextBase {
18
- get _gl() {
19
- return this._native2;
20
- }
21
- constructor(canvas, options = {}) {
22
- super(canvas, options);
23
- this._queries = {};
24
- this._samplers = {};
25
- this._transformFeedbacks = {};
26
- this._vertexArrayObjects = {};
27
- this._syncs = {};
28
- this._activeReadFramebuffer = null;
29
- this._activeDrawFramebuffer = null;
30
- this._native2 = new Gwebgl.WebGL2RenderingContext({});
31
- this._init();
32
- }
33
- _getGlslVersion(es) {
34
- return es ? "300 es" : "130";
35
- }
36
- /**
37
- * WebGL2 delegates framebuffer completeness to the native GL driver.
38
- * Called by _framebufferOk() before draw calls and by _updateFramebufferAttachments.
39
- * The base class version uses JS-side format whitelists that reject valid WebGL2 formats.
40
- */
41
- /** WebGL2 allows COLOR_ATTACHMENT1–15 as framebuffer attachment points. */
42
- _validFramebufferAttachment(attachment) {
43
- if (super._validFramebufferAttachment(attachment)) return true;
44
- return attachment >= 36065 && attachment <= 36079;
45
- }
46
- static {
47
- this._WGL2_ALL_COLOR_ATTACHMENTS = [
48
- 36064,
49
- 36065,
50
- 36066,
51
- 36067,
52
- 36068,
53
- 36069,
54
- 36070,
55
- 36071,
56
- 36072,
57
- 36073,
58
- 36074,
59
- 36075,
60
- 36076,
61
- 36077,
62
- 36078,
63
- 36079
64
- ];
65
- }
66
- _getColorAttachments() {
67
- return WebGL2RenderingContext._WGL2_ALL_COLOR_ATTACHMENTS;
68
- }
69
- /**
70
- * WebGL2 extends the base-class framebuffer completeness pre-check to
71
- * accept WebGL2-specific formats that the WebGL1 whitelist rejects.
72
- *
73
- * NOTE: This is called by _updateFramebufferAttachments BEFORE the native
74
- * GL attachments are set, so we must NOT query glCheckFramebufferStatus
75
- * here (it would see an empty FBO and always return INCOMPLETE).
76
- * Instead we extend the JS-side format whitelist to cover WebGL2 formats.
77
- */
78
- _preCheckFramebufferStatus(framebuffer) {
79
- const attachments = framebuffer._attachments;
80
- let bestWidth = 0;
81
- let bestHeight = 0;
82
- const allEnums = [
83
- this.COLOR_ATTACHMENT0,
84
- this.DEPTH_ATTACHMENT,
85
- this.STENCIL_ATTACHMENT,
86
- this.DEPTH_STENCIL_ATTACHMENT,
87
- ...WebGL2RenderingContext._WGL2_ALL_COLOR_ATTACHMENTS
88
- ];
89
- for (const enumVal of allEnums) {
90
- const attach = attachments[enumVal];
91
- if (!attach) continue;
92
- if (attach instanceof WebGLTexture) {
93
- const level = framebuffer._attachmentLevel[enumVal] ?? 0;
94
- const w = attach._levelWidth[level] ?? 0;
95
- const h = attach._levelHeight[level] ?? 0;
96
- if (w > 0 && h > 0) {
97
- bestWidth = w;
98
- bestHeight = h;
99
- break;
100
- }
101
- } else if (attach instanceof WebGLRenderbuffer) {
102
- if (attach._width > 0 && attach._height > 0) {
103
- bestWidth = attach._width;
104
- bestHeight = attach._height;
105
- break;
106
- }
107
- }
108
- }
109
- if (bestWidth > 0 && bestHeight > 0) {
110
- framebuffer._width = bestWidth;
111
- framebuffer._height = bestHeight;
112
- return this.FRAMEBUFFER_COMPLETE;
113
- }
114
- return this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
115
- }
116
- /**
117
- * WebGL2 completely replaces the base class framebuffer attachment flow.
118
- *
119
- * The base class flow is: (1) pre-check formats in JS → (2) set native
120
- * attachments only if pre-check passes. This is wrong for WebGL2 because
121
- * the JS pre-check uses WebGL1 format whitelists that reject valid WebGL2
122
- * formats.
123
- *
124
- * WebGL2 flow: (1) always set native attachments first → (2) query native
125
- * glCheckFramebufferStatus to determine completeness. This delegates all
126
- * format validation to the native GL driver, matching browser behavior.
127
- *
128
- * Also handles COLOR_ATTACHMENT1–15 (WebGL2 MRT) that the base class
129
- * doesn't know about.
130
- */
131
- /**
132
- * Apply COLOR_ATTACHMENT1–15 to the native GL FBO (WebGL2 MRT).
133
- * The base class handles CA0, DEPTH, STENCIL, DEPTH_STENCIL and calls
134
- * _preCheckFramebufferStatus (which we override to query native GL).
135
- */
136
- _updateFramebufferAttachments(framebuffer) {
137
- super._updateFramebufferAttachments(framebuffer);
138
- if (!framebuffer) return;
139
- for (let i = 1; i <= 15; i++) {
140
- const attachmentEnum = 36064 + i;
141
- if (!(attachmentEnum in framebuffer._attachments)) continue;
142
- const attachment = framebuffer._attachments[attachmentEnum];
143
- if (attachment instanceof WebGLTexture) {
144
- const face = framebuffer._attachmentFace[attachmentEnum] || this.TEXTURE_2D;
145
- const level = framebuffer._attachmentLevel[attachmentEnum] ?? 0;
146
- this._gl.framebufferTexture2D(this.FRAMEBUFFER, attachmentEnum, face, attachment._ | 0, level | 0);
147
- } else if (attachment instanceof WebGLRenderbuffer) {
148
- this._gl.framebufferRenderbuffer(this.FRAMEBUFFER, attachmentEnum, this.RENDERBUFFER, attachment._ | 0);
149
- } else {
150
- this._gl.framebufferTexture2D(this.FRAMEBUFFER, attachmentEnum, this.TEXTURE_2D, 0, 0);
151
- }
152
- }
153
- }
154
- /** WebGL2 adds UNIFORM_BUFFER, TRANSFORM_FEEDBACK_BUFFER, etc. targets. */
155
- bindBuffer(target, buffer) {
156
- const isWebGL2Target = target === 35345 || target === 35982 || target === 36662 || target === 36663;
157
- if (isWebGL2Target) {
158
- const id = buffer ? buffer._ | 0 : 0;
159
- this._gl.bindBuffer(target, id);
160
- return;
161
- }
162
- super.bindBuffer(target, buffer);
163
- }
164
- /**
165
- * WebGL2 adds READ_FRAMEBUFFER (0x8CA8) and DRAW_FRAMEBUFFER (0x8CA9) targets.
166
- * The base class only accepts FRAMEBUFFER; this override handles the two new targets
167
- * and keeps _activeReadFramebuffer / _activeDrawFramebuffer in sync.
168
- */
169
- bindFramebuffer(target, framebuffer) {
170
- if (target === 36008 || target === 36009) {
171
- if (!checkObject(framebuffer)) {
172
- throw new TypeError("bindFramebuffer(GLenum, WebGLFramebuffer)");
173
- }
174
- if (framebuffer && framebuffer._pendingDelete) return;
175
- if (framebuffer && !this._checkWrapper(framebuffer, WebGLFramebuffer)) return;
176
- const id = framebuffer ? framebuffer._ | 0 : this._gtkFboId;
177
- this._gl.bindFramebuffer(target, id);
178
- if (target === 36008) {
179
- const prev = this._activeReadFramebuffer;
180
- if (prev !== framebuffer) {
181
- if (prev) {
182
- prev._refCount -= 1;
183
- prev._checkDelete();
184
- }
185
- if (framebuffer) framebuffer._refCount += 1;
186
- }
187
- this._activeReadFramebuffer = framebuffer;
188
- } else {
189
- const prev = this._activeDrawFramebuffer;
190
- if (prev !== framebuffer) {
191
- if (prev) {
192
- prev._refCount -= 1;
193
- prev._checkDelete();
194
- }
195
- if (framebuffer) framebuffer._refCount += 1;
196
- }
197
- this._activeDrawFramebuffer = framebuffer;
198
- this._activeFramebuffer = framebuffer;
199
- }
200
- return;
201
- }
202
- super.bindFramebuffer(this.FRAMEBUFFER, framebuffer);
203
- this._activeReadFramebuffer = framebuffer;
204
- this._activeDrawFramebuffer = framebuffer;
205
- }
206
- /** WebGL2 also unbinds from read/draw framebuffer slots when deleting. */
207
- deleteFramebuffer(framebuffer) {
208
- if (this._activeReadFramebuffer === framebuffer) {
209
- this.bindFramebuffer(36008, null);
210
- }
211
- if (this._activeDrawFramebuffer === framebuffer) {
212
- this.bindFramebuffer(36009, null);
213
- }
214
- super.deleteFramebuffer(framebuffer);
215
- }
216
- /** WebGL2 adds READ/COPY buffer usages and additional buffer targets. */
217
- bufferData(target, dataOrSize, usage) {
218
- const isWebGL2Target = target === 35345 || target === 35982 || target === 36662 || target === 36663;
219
- const isReadOrCopy = usage === 35041 || usage === 35043 || usage === 35045 || usage === 35042 || usage === 35044 || usage === 35046;
220
- const remappedUsage = isReadOrCopy ? this.STATIC_DRAW : usage;
221
- if (isWebGL2Target) {
222
- if (typeof dataOrSize === "number") {
223
- if (dataOrSize >= 0) this._gl.bufferDataSizeOnly(target, dataOrSize, remappedUsage);
224
- } else if (dataOrSize !== null && typeof dataOrSize === "object") {
225
- const u8Data = arrayToUint8Array(dataOrSize);
226
- this._gl.bufferData(target, Uint8ArrayToVariant(u8Data), remappedUsage);
227
- }
228
- return;
229
- }
230
- super.bufferData(target, dataOrSize, remappedUsage);
231
- }
232
- /** WebGL2 adds UNIFORM_BUFFER, TRANSFORM_FEEDBACK_BUFFER, COPY_READ/WRITE targets. */
233
- bufferSubData(target, offset, data) {
234
- const isWebGL2Target = target === 35345 || target === 35982 || target === 36662 || target === 36663;
235
- if (isWebGL2Target) {
236
- if (offset < 0) {
237
- this.setError(this.INVALID_VALUE);
238
- return;
239
- }
240
- if (!data) {
241
- this.setError(this.INVALID_VALUE);
242
- return;
243
- }
244
- const u8Data = arrayToUint8Array(data);
245
- this._gl.bufferSubData(target, offset, Uint8ArrayToVariant(u8Data));
246
- return;
247
- }
248
- super.bufferSubData(target, offset, data);
249
- }
250
- /** WebGL2 adds TEXTURE_3D and TEXTURE_2D_ARRAY target support. */
251
- bindTexture(target, texture) {
252
- if (target === 32879 || target === 35866) {
253
- const id = texture ? texture._ | 0 : 0;
254
- this._gl.bindTexture(target, id);
255
- if (texture) texture._binding = target;
256
- return;
257
- }
258
- super.bindTexture(target, texture);
259
- }
260
- /** WebGL2 adds TEXTURE_3D/TEXTURE_2D_ARRAY targets and many new pnames. */
261
- texParameteri(target, pname, param) {
262
- if (target === 32879 || target === 35866) {
263
- this._gl.texParameteri(target, pname, param);
264
- return;
265
- }
266
- const isWebGL2Pname = pname === 32882 || pname === 34892 || pname === 34893 || pname === 33084 || pname === 33085 || pname === 33083 || pname === 33082;
267
- if (isWebGL2Pname) {
268
- this._gl.texParameteri(target, pname, param);
269
- return;
270
- }
271
- super.texParameteri(target, pname, param);
272
- }
273
- /**
274
- * In WebGL2/GLES3 the attribute-0 requirement from WebGL1 does not apply.
275
- * Override drawArrays to skip the attrib0 hack and call glDrawArrays directly.
276
- */
277
- /**
278
- * In WebGL2/GLES3 the attribute-0 requirement from WebGL1 does not apply.
279
- * Override drawArrays to skip the attrib0 hack and call glDrawArrays directly.
280
- */
281
- drawArrays(mode, first, count) {
282
- if (first < 0 || count < 0) {
283
- this.setError(this.INVALID_VALUE);
284
- return;
285
- }
286
- if (!this._checkStencilState()) return;
287
- const rc = vertexCount(this, mode, count);
288
- if (rc < 0) {
289
- this.setError(this.INVALID_ENUM);
290
- return;
291
- }
292
- if (!this._framebufferOk()) return;
293
- if (count === 0) return;
294
- if (!this._checkVertexAttribState(count + first - 1 >>> 0)) return;
295
- this._native2.drawArrays(mode, first, rc);
296
- }
297
- /**
298
- * In WebGL2, UNSIGNED_INT element indices are a core feature — no extension needed.
299
- * Override drawElements to skip the oes_element_index_uint extension check.
300
- */
301
- drawElements(mode = 0, count = 0, type = 0, offset = 0) {
302
- if (count < 0 || offset < 0) {
303
- this.setError(this.INVALID_VALUE);
304
- return;
305
- }
306
- if (!this._checkStencilState()) return;
307
- const elementBuffer = this._vertexObjectState._elementArrayBufferBinding;
308
- if (!elementBuffer) {
309
- this.setError(this.INVALID_OPERATION);
310
- return;
311
- }
312
- let elementData = null;
313
- let adjustedOffset = offset;
314
- if (type === this.UNSIGNED_SHORT) {
315
- if (adjustedOffset % 2) {
316
- this.setError(this.INVALID_OPERATION);
317
- return;
318
- }
319
- adjustedOffset >>= 1;
320
- elementData = new Uint16Array(elementBuffer._elements.buffer);
321
- } else if (type === this.UNSIGNED_INT) {
322
- if (adjustedOffset % 4) {
323
- this.setError(this.INVALID_OPERATION);
324
- return;
325
- }
326
- adjustedOffset >>= 2;
327
- elementData = new Uint32Array(elementBuffer._elements.buffer);
328
- } else if (type === this.UNSIGNED_BYTE) {
329
- elementData = elementBuffer._elements;
330
- } else {
331
- this.setError(this.INVALID_ENUM);
332
- return;
333
- }
334
- let reducedCount = count;
335
- switch (mode) {
336
- case this.TRIANGLES:
337
- if (count % 3) reducedCount -= count % 3;
338
- break;
339
- case this.LINES:
340
- if (count % 2) reducedCount -= count % 2;
341
- break;
342
- case this.POINTS: break;
343
- case this.LINE_LOOP:
344
- case this.LINE_STRIP:
345
- if (count < 2) {
346
- this.setError(this.INVALID_OPERATION);
347
- return;
348
- }
349
- break;
350
- case this.TRIANGLE_FAN:
351
- case this.TRIANGLE_STRIP:
352
- if (count < 3) {
353
- this.setError(this.INVALID_OPERATION);
354
- return;
355
- }
356
- break;
357
- default:
358
- this.setError(this.INVALID_ENUM);
359
- return;
360
- }
361
- if (!this._framebufferOk()) return;
362
- if (count === 0) return;
363
- let maxIndex = 0;
364
- for (let i = adjustedOffset; i < adjustedOffset + reducedCount; ++i) {
365
- if (i < elementData.length && elementData[i] > maxIndex) maxIndex = elementData[i];
366
- }
367
- if (this._checkVertexAttribState(maxIndex)) {
368
- this._native2.drawElements(mode, reducedCount, type, offset);
369
- }
370
- }
371
- createVertexArray() {
372
- const id = this._native2.createVertexArray();
373
- if (!id) return null;
374
- const vao = new WebGLVertexArrayObject(id, this);
375
- this._vertexArrayObjects[id] = vao;
376
- return vao;
377
- }
378
- deleteVertexArray(vertexArray) {
379
- if (!vertexArray || !(vertexArray instanceof WebGLVertexArrayObject)) return;
380
- vertexArray._pendingDelete = true;
381
- vertexArray._checkDelete();
382
- }
383
- isVertexArray(vertexArray) {
384
- if (!vertexArray || !(vertexArray instanceof WebGLVertexArrayObject)) return false;
385
- return this._native2.isVertexArray(vertexArray._);
386
- }
387
- bindVertexArray(array) {
388
- if (array === null) {
389
- this._native2.bindVertexArray(0);
390
- this._vertexObjectState = this._defaultVertexObjectState;
391
- } else if (array instanceof WebGLVertexArrayObject) {
392
- this._native2.bindVertexArray(array._);
393
- this._vertexObjectState = array._objectState;
394
- } else {
395
- this.setError(this.INVALID_OPERATION);
396
- }
397
- }
398
- createQuery() {
399
- const id = this._native2.createQuery();
400
- if (!id) return null;
401
- const query = new WebGLQuery(id, this);
402
- this._queries[id] = query;
403
- return query;
404
- }
405
- deleteQuery(query) {
406
- if (!query || !(query instanceof WebGLQuery)) return;
407
- query._pendingDelete = true;
408
- query._checkDelete();
409
- }
410
- isQuery(query) {
411
- if (!query || !(query instanceof WebGLQuery)) return false;
412
- return this._native2.isQuery(query._);
413
- }
414
- beginQuery(target, query) {
415
- if (!(query instanceof WebGLQuery)) return;
416
- this._native2.beginQuery(target, query._);
417
- }
418
- endQuery(target) {
419
- this._native2.endQuery(target);
420
- }
421
- getQuery(_target, _pname) {
422
- warnNotImplemented("WebGL2RenderingContext.getQuery");
423
- return null;
424
- }
425
- getQueryParameter(query, pname) {
426
- if (!(query instanceof WebGLQuery)) return null;
427
- return this._native2.getQueryParameter(query._, pname);
428
- }
429
- createSampler() {
430
- const id = this._native2.createSampler();
431
- if (!id) return null;
432
- const sampler = new WebGLSampler(id, this);
433
- this._samplers[id] = sampler;
434
- return sampler;
435
- }
436
- deleteSampler(sampler) {
437
- if (!sampler || !(sampler instanceof WebGLSampler)) return;
438
- sampler._pendingDelete = true;
439
- sampler._checkDelete();
440
- }
441
- isSampler(sampler) {
442
- if (!sampler || !(sampler instanceof WebGLSampler)) return false;
443
- return this._native2.isSampler(sampler._);
444
- }
445
- bindSampler(unit, sampler) {
446
- this._native2.bindSampler(unit, sampler ? sampler._ : 0);
447
- }
448
- samplerParameteri(sampler, pname, param) {
449
- if (!(sampler instanceof WebGLSampler)) return;
450
- this._native2.samplerParameteri(sampler._, pname, param);
451
- }
452
- samplerParameterf(sampler, pname, param) {
453
- if (!(sampler instanceof WebGLSampler)) return;
454
- this._native2.samplerParameterf(sampler._, pname, param);
455
- }
456
- getSamplerParameter(sampler, pname) {
457
- if (!(sampler instanceof WebGLSampler)) return null;
458
- if (pname === 33082 || pname === 33083) {
459
- return this._native2.getSamplerParameterf(sampler._, pname);
460
- }
461
- return this._native2.getSamplerParameteri(sampler._, pname);
462
- }
463
- fenceSync(condition, flags) {
464
- const id = this._native2.fenceSync(condition, flags);
465
- if (!id) return null;
466
- const sync = new WebGLSync(id, this);
467
- this._syncs[id] = sync;
468
- return sync;
469
- }
470
- isSync(sync) {
471
- if (!sync || !(sync instanceof WebGLSync)) return false;
472
- return this._native2.isSync(sync._);
473
- }
474
- deleteSync(sync) {
475
- if (!sync || !(sync instanceof WebGLSync)) return;
476
- sync._pendingDelete = true;
477
- sync._checkDelete();
478
- }
479
- clientWaitSync(sync, flags, timeout) {
480
- if (!(sync instanceof WebGLSync)) return 37148;
481
- return this._native2.clientWaitSync(sync._, flags, timeout);
482
- }
483
- waitSync(sync, flags, timeout) {
484
- if (!(sync instanceof WebGLSync)) return;
485
- this._native2.waitSync(sync._, flags, timeout);
486
- }
487
- getSyncParameter(sync, pname) {
488
- if (!(sync instanceof WebGLSync)) return null;
489
- return this._native2.getSyncParameter(sync._, pname);
490
- }
491
- createTransformFeedback() {
492
- const id = this._native2.createTransformFeedback();
493
- if (!id) return null;
494
- const tf = new WebGLTransformFeedback(id, this);
495
- this._transformFeedbacks[id] = tf;
496
- return tf;
497
- }
498
- deleteTransformFeedback(tf) {
499
- if (!tf || !(tf instanceof WebGLTransformFeedback)) return;
500
- tf._pendingDelete = true;
501
- tf._checkDelete();
502
- }
503
- isTransformFeedback(tf) {
504
- if (!tf || !(tf instanceof WebGLTransformFeedback)) return false;
505
- return this._native2.isTransformFeedback(tf._);
506
- }
507
- bindTransformFeedback(target, tf) {
508
- this._native2.bindTransformFeedback(target, tf ? tf._ : 0);
509
- }
510
- beginTransformFeedback(primitiveMode) {
511
- this._native2.beginTransformFeedback(primitiveMode);
512
- }
513
- endTransformFeedback() {
514
- this._native2.endTransformFeedback();
515
- }
516
- pauseTransformFeedback() {
517
- this._native2.pauseTransformFeedback();
518
- }
519
- resumeTransformFeedback() {
520
- this._native2.resumeTransformFeedback();
521
- }
522
- transformFeedbackVaryings(program, varyings, bufferMode) {
523
- this._native2.transformFeedbackVaryings(program._, varyings, bufferMode);
524
- }
525
- getTransformFeedbackVarying(program, index) {
526
- const result = this._native2.getTransformFeedbackVarying(program._, index).deepUnpack();
527
- return new WebGLActiveInfo({
528
- size: result.size,
529
- type: result.type,
530
- name: result.name
531
- });
532
- }
533
- bindBufferBase(target, index, buffer) {
534
- this._native2.bindBufferBase(target, index, buffer ? buffer._ : 0);
535
- }
536
- bindBufferRange(target, index, buffer, offset, size) {
537
- this._native2.bindBufferRange(target, index, buffer ? buffer._ : 0, offset, size);
538
- }
539
- copyBufferSubData(readTarget, writeTarget, readOffset, writeOffset, size) {
540
- this._native2.copyBufferSubData(readTarget, writeTarget, readOffset, writeOffset, size);
541
- }
542
- getBufferSubData(target, srcByteOffset, dstBuffer, dstOffset, length) {
543
- const byteLength = length !== undefined ? length : dstBuffer.byteLength - (dstOffset ?? 0);
544
- const data = this._native2.getBufferSubData(target, srcByteOffset, byteLength);
545
- const dst = new Uint8Array(dstBuffer.buffer, dstBuffer.byteOffset + (dstOffset ?? 0) * (dstBuffer instanceof Uint8Array ? 1 : dstBuffer.BYTES_PER_ELEMENT ?? 1));
546
- dst.set(data.subarray(0, dst.byteLength));
547
- }
548
- texImage3D(target, level, internalformat, width, height, depth, border, format, type, pixels) {
549
- if (pixels === null) {
550
- this._native2.texImage3DNull(target, level, internalformat, width, height, depth, border, format, type);
551
- } else {
552
- this._native2.texImage3D(target, level, internalformat, width, height, depth, border, format, type, Uint8ArrayToVariant(new Uint8Array(pixels.buffer, pixels.byteOffset, pixels.byteLength)));
553
- }
554
- }
555
- texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels) {
556
- if (pixels === null) return;
557
- this._native2.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, Uint8ArrayToVariant(new Uint8Array(pixels.buffer, pixels.byteOffset, pixels.byteLength)));
558
- }
559
- compressedTexImage3D(target, level, internalformat, width, height, depth, border, _imageSize, data) {
560
- this._native2.compressedTexImage3D(target, level, internalformat, width, height, depth, border, Uint8ArrayToVariant(new Uint8Array(data.buffer, data.byteOffset, data.byteLength)));
561
- }
562
- compressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, _imageSize, data) {
563
- this._native2.compressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, Uint8ArrayToVariant(new Uint8Array(data.buffer, data.byteOffset, data.byteLength)));
564
- }
565
- copyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height) {
566
- this._native2.copyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height);
567
- }
568
- texStorage2D(target, levels, internalformat, width, height) {
569
- this._native2.texStorage2D(target, levels, internalformat, width, height);
570
- const texture = this._getTexImage(target);
571
- if (texture) {
572
- for (let lvl = 0; lvl < levels; lvl++) {
573
- texture._levelWidth[lvl] = Math.max(1, width >> lvl);
574
- texture._levelHeight[lvl] = Math.max(1, height >> lvl);
575
- }
576
- texture._format = this.RGBA;
577
- texture._type = this.UNSIGNED_BYTE;
578
- }
579
- }
580
- texStorage3D(target, levels, internalformat, width, height, depth) {
581
- this._native2.texStorage3D(target, levels, internalformat, width, height, depth);
582
- }
583
- texImage2D(target = 0, level = 0, internalFormat = 0, formatOrWidth = 0, typeOrHeight = 0, sourceOrBorder = 0, _format = 0, type = 0, pixels) {
584
- let width = 0;
585
- let height = 0;
586
- let format = 0;
587
- let border = 0;
588
- if (arguments.length === 6) {
589
- type = typeOrHeight;
590
- format = formatOrWidth;
591
- if (sourceOrBorder instanceof GdkPixbuf.Pixbuf) {
592
- const pixbuf = sourceOrBorder;
593
- width = pixbuf.get_width();
594
- height = pixbuf.get_height();
595
- pixels = pixbuf.get_pixels();
596
- } else {
597
- const imageData = extractImageData(sourceOrBorder);
598
- if (imageData == null) {
599
- throw new TypeError("texImage2D(GLenum, GLint, GLenum, GLint, GLenum, GLenum, ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement)");
600
- }
601
- width = imageData.width;
602
- height = imageData.height;
603
- pixels = imageData.data;
604
- }
605
- } else if (arguments.length >= 9) {
606
- width = formatOrWidth;
607
- height = typeOrHeight;
608
- border = sourceOrBorder;
609
- format = _format;
610
- }
611
- const texture = this._getTexImage(target);
612
- if (!texture) {
613
- this.setError(this.INVALID_OPERATION);
614
- return;
615
- }
616
- let data = convertPixels(pixels);
617
- if (this._unpackPremultAlpha && data && format === this.RGBA) {
618
- data = premultiplyAlpha(data);
619
- }
620
- if (this._unpackFlipY && data && width > 0 && height > 0) {
621
- const pixelSize = this._computePixelSize(type, format);
622
- if (pixelSize > 0) {
623
- const rowStride = this._computeRowStride(width, pixelSize);
624
- const flipped = new Uint8Array(data.length);
625
- for (let row = 0; row < height; row++) {
626
- const srcOffset = row * rowStride;
627
- const dstOffset = (height - 1 - row) * rowStride;
628
- flipped.set(data.subarray(srcOffset, srcOffset + rowStride), dstOffset);
629
- }
630
- data = flipped;
631
- }
632
- }
633
- this._saveError();
634
- this._gl.texImage2D(target, level, internalFormat, width, height, border, format, type, Uint8ArrayToVariant(data));
635
- const error = this.getError();
636
- this._restoreError(error);
637
- if (error !== this.NO_ERROR) return;
638
- texture._levelWidth[level] = width;
639
- texture._levelHeight[level] = height;
640
- texture._format = format;
641
- texture._type = type;
642
- const activeFramebuffer = this._activeFramebuffer;
643
- if (activeFramebuffer) {
644
- let needsUpdate = false;
645
- const attachments = this._getAttachments();
646
- for (let i = 0; i < attachments.length; ++i) {
647
- if (activeFramebuffer._attachments[attachments[i]] === texture) {
648
- needsUpdate = true;
649
- break;
650
- }
651
- }
652
- if (needsUpdate && this._activeFramebuffer) {
653
- this._updateFramebufferAttachments(this._activeFramebuffer);
654
- }
655
- }
656
- }
657
- texSubImage2D(target = 0, level = 0, xoffset = 0, yoffset = 0, formatOrWidth = 0, typeOrHeight = 0, sourceOrFormat = 0, type = 0, pixels) {
658
- let width = 0;
659
- let height = 0;
660
- let format = 0;
661
- if (arguments.length === 7) {
662
- type = typeOrHeight;
663
- format = formatOrWidth;
664
- if (sourceOrFormat instanceof GdkPixbuf.Pixbuf) {
665
- const pixbuf = sourceOrFormat;
666
- width = pixbuf.get_width();
667
- height = pixbuf.get_height();
668
- pixels = pixbuf.get_pixels();
669
- } else {
670
- const imageData = extractImageData(sourceOrFormat);
671
- if (imageData == null) {
672
- throw new TypeError("texSubImage2D(GLenum, GLint, GLint, GLint, GLenum, GLenum, ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement)");
673
- }
674
- width = imageData.width;
675
- height = imageData.height;
676
- pixels = imageData.data;
677
- }
678
- } else {
679
- width = formatOrWidth;
680
- height = typeOrHeight;
681
- format = sourceOrFormat;
682
- }
683
- const texture = this._getTexImage(target);
684
- if (!texture) {
685
- this.setError(this.INVALID_OPERATION);
686
- return;
687
- }
688
- let data = convertPixels(pixels);
689
- if (!data) {
690
- this.setError(this.INVALID_OPERATION);
691
- return;
692
- }
693
- if (this._unpackPremultAlpha && data && format === this.RGBA) {
694
- data = premultiplyAlpha(data);
695
- }
696
- if (this._unpackFlipY && data && width > 0 && height > 0) {
697
- const pixelSize = this._computePixelSize(type, format);
698
- if (pixelSize > 0) {
699
- const rowStride = this._computeRowStride(width, pixelSize);
700
- const flipped = new Uint8Array(data.length);
701
- for (let row = 0; row < height; row++) {
702
- const srcOffset = row * rowStride;
703
- const dstOffset = (height - 1 - row) * rowStride;
704
- flipped.set(data.subarray(srcOffset, srcOffset + rowStride), dstOffset);
705
- }
706
- data = flipped;
707
- }
708
- }
709
- this._gl.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, Uint8ArrayToVariant(data));
710
- }
711
- framebufferTextureLayer(target, attachment, texture, level, layer) {
712
- this._native2.framebufferTextureLayer(target, attachment, texture ? texture._ : 0, level, layer);
713
- }
714
- drawArraysInstanced(mode, first, count, instanceCount) {
715
- if (first < 0 || count < 0 || instanceCount < 0) {
716
- this.setError(this.INVALID_VALUE);
717
- return;
718
- }
719
- if (!this._checkStencilState()) return;
720
- const rc = vertexCount(this, mode, count);
721
- if (rc < 0) {
722
- this.setError(this.INVALID_ENUM);
723
- return;
724
- }
725
- if (!this._framebufferOk()) return;
726
- if (count === 0 || instanceCount === 0) return;
727
- if (!this._checkVertexAttribState(count + first - 1 >>> 0)) return;
728
- if (globalThis.__GJSIFY_DEBUG_GL) {
729
- const n = this.__drawInstCount = (this.__drawInstCount | 0) + 1;
730
- if (n <= 5 || n % 100 === 0) console.log(`[WebGL] drawArraysInstanced #${n} count=${rc} instances=${instanceCount} fbo=${this._activeFramebuffer?._ ?? "_gtkFbo"}`);
731
- }
732
- this._native2.drawArraysInstanced(mode, first, rc, instanceCount);
733
- }
734
- drawElementsInstanced(mode, count, type, offset, instanceCount) {
735
- if (count < 0 || offset < 0 || instanceCount < 0) {
736
- this.setError(this.INVALID_VALUE);
737
- return;
738
- }
739
- if (!this._checkStencilState()) return;
740
- const elementBuffer = this._vertexObjectState._elementArrayBufferBinding;
741
- if (!elementBuffer) {
742
- this.setError(this.INVALID_OPERATION);
743
- return;
744
- }
745
- let elementData = null;
746
- let adjustedOffset = offset;
747
- if (type === this.UNSIGNED_SHORT) {
748
- if (adjustedOffset % 2) {
749
- this.setError(this.INVALID_OPERATION);
750
- return;
751
- }
752
- adjustedOffset >>= 1;
753
- elementData = new Uint16Array(elementBuffer._elements.buffer);
754
- } else if (type === this.UNSIGNED_INT) {
755
- if (adjustedOffset % 4) {
756
- this.setError(this.INVALID_OPERATION);
757
- return;
758
- }
759
- adjustedOffset >>= 2;
760
- elementData = new Uint32Array(elementBuffer._elements.buffer);
761
- } else if (type === this.UNSIGNED_BYTE) {
762
- elementData = elementBuffer._elements;
763
- } else {
764
- this.setError(this.INVALID_ENUM);
765
- return;
766
- }
767
- let reducedCount = count;
768
- switch (mode) {
769
- case this.TRIANGLES:
770
- if (count % 3) reducedCount -= count % 3;
771
- break;
772
- case this.LINES:
773
- if (count % 2) reducedCount -= count % 2;
774
- break;
775
- case this.POINTS: break;
776
- case this.LINE_LOOP:
777
- case this.LINE_STRIP:
778
- if (count < 2) {
779
- this.setError(this.INVALID_OPERATION);
780
- return;
781
- }
782
- break;
783
- case this.TRIANGLE_FAN:
784
- case this.TRIANGLE_STRIP:
785
- if (count < 3) {
786
- this.setError(this.INVALID_OPERATION);
787
- return;
788
- }
789
- break;
790
- default:
791
- this.setError(this.INVALID_ENUM);
792
- return;
793
- }
794
- if (!this._framebufferOk()) return;
795
- if (reducedCount === 0 || instanceCount === 0) {
796
- this._checkVertexAttribState(0);
797
- return;
798
- }
799
- if (reducedCount + adjustedOffset >>> 0 > elementData.length) {
800
- this.setError(this.INVALID_OPERATION);
801
- return;
802
- }
803
- let maxIndex = 0;
804
- for (let i = adjustedOffset; i < adjustedOffset + reducedCount; ++i) {
805
- if (elementData[i] > maxIndex) maxIndex = elementData[i];
806
- }
807
- if (this._checkVertexAttribState(maxIndex)) {
808
- this._native2.drawElementsInstanced(mode, reducedCount, type, offset, instanceCount);
809
- }
810
- }
811
- vertexAttribDivisor(index, divisor) {
812
- this._native2.vertexAttribDivisor(index, divisor);
813
- }
814
- vertexAttribIPointer(index, size, type, stride, offset) {
815
- this._native2.vertexAttribIPointer(index, size, type, stride, offset);
816
- }
817
- drawBuffers(buffers) {
818
- let hasBack = false;
819
- for (let i = 0; i < buffers.length; i++) {
820
- if (buffers[i] === 1029) {
821
- hasBack = true;
822
- break;
823
- }
824
- }
825
- if (!hasBack) {
826
- this._native2.drawBuffers(buffers);
827
- return;
828
- }
829
- this._native2.drawBuffers(buffers.map((b) => b === 1029 ? this.COLOR_ATTACHMENT0 : b));
830
- }
831
- drawRangeElements(mode, start, end, count, type, offset) {
832
- if (count < 0 || offset < 0) {
833
- this.setError(this.INVALID_VALUE);
834
- return;
835
- }
836
- if (end < start) {
837
- this.setError(this.INVALID_VALUE);
838
- return;
839
- }
840
- this.drawElements(mode, count, type, offset);
841
- }
842
- blitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter) {
843
- if (globalThis.__GJSIFY_DEBUG_GL) {
844
- const errBefore = this._gl.getError();
845
- if (errBefore !== 0) console.log(`[WebGL] blitFramebuffer PRE-ERROR 0x${errBefore.toString(16)}`);
846
- }
847
- this._native2.blitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
848
- if (globalThis.__GJSIFY_DEBUG_GL) {
849
- const err = this._gl.getError();
850
- const n = this.__blitCount = (this.__blitCount | 0) + 1;
851
- if (n <= 5) console.log(`[WebGL] blitFramebuffer #${n} src=(${srcX0},${srcY0},${srcX1},${srcY1}) readFbo=${this._activeReadFramebuffer?._ ?? "_gtkFbo"} err=${err === 0 ? "OK" : "0x" + err.toString(16)}`);
852
- }
853
- }
854
- clearBufferfv(buffer, drawbuffer, values, _srcOffset) {
855
- const n2 = this._native2;
856
- if (typeof n2.clearBufferfv === "function") {
857
- n2.clearBufferfv(buffer, drawbuffer, Array.from(values));
858
- return;
859
- }
860
- const v = values;
861
- if (buffer === 6144) {
862
- const prev = this.getParameter(this.COLOR_CLEAR_VALUE);
863
- this.clearColor(v[0] ?? 0, v[1] ?? 0, v[2] ?? 0, v[3] ?? 0);
864
- this.clear(this.COLOR_BUFFER_BIT);
865
- if (prev) this.clearColor(prev[0], prev[1], prev[2], prev[3]);
866
- } else if (buffer === 6145) {
867
- const prev = this.getParameter(this.DEPTH_CLEAR_VALUE);
868
- this.clearDepth(v[0] ?? 1);
869
- this.clear(this.DEPTH_BUFFER_BIT);
870
- if (prev !== null) this.clearDepth(prev);
871
- }
872
- }
873
- clearBufferiv(buffer, drawbuffer, values, _srcOffset) {
874
- const n2 = this._native2;
875
- if (typeof n2.clearBufferiv === "function") {
876
- n2.clearBufferiv(buffer, drawbuffer, Array.from(values));
877
- return;
878
- }
879
- if (buffer === 6146) {
880
- const v = values;
881
- const prev = this.getParameter(this.STENCIL_CLEAR_VALUE);
882
- this.clearStencil(v[0] ?? 0);
883
- this.clear(this.STENCIL_BUFFER_BIT);
884
- if (prev !== null) this.clearStencil(prev);
885
- }
886
- }
887
- clearBufferuiv(buffer, drawbuffer, values, _srcOffset) {
888
- const n2 = this._native2;
889
- if (typeof n2.clearBufferuiv === "function") {
890
- n2.clearBufferuiv(buffer, drawbuffer, Array.from(values));
891
- return;
892
- }
893
- void buffer;
894
- void drawbuffer;
895
- }
896
- clearBufferfi(buffer, drawbuffer, depth, stencil) {
897
- const n2 = this._native2;
898
- if (typeof n2.clearBufferfi === "function") {
899
- n2.clearBufferfi(buffer, drawbuffer, depth, stencil);
900
- return;
901
- }
902
- if (buffer === 34041) {
903
- const prevDepth = this.getParameter(this.DEPTH_CLEAR_VALUE);
904
- const prevStencil = this.getParameter(this.STENCIL_CLEAR_VALUE);
905
- this.clearDepth(depth);
906
- this.clearStencil(stencil);
907
- this.clear(this.DEPTH_BUFFER_BIT | this.STENCIL_BUFFER_BIT);
908
- if (prevDepth !== null) this.clearDepth(prevDepth);
909
- if (prevStencil !== null) this.clearStencil(prevStencil);
910
- }
911
- void drawbuffer;
912
- }
913
- invalidateFramebuffer(target, attachments) {
914
- this._native2.invalidateFramebuffer(target, attachments);
915
- }
916
- invalidateSubFramebuffer(target, attachments, x, y, width, height) {
917
- this._native2.invalidateSubFramebuffer(target, attachments, x, y, width, height);
918
- }
919
- readBuffer(src) {
920
- this._native2.readBuffer(src);
921
- }
922
- renderbufferStorageMultisample(target, samples, internalFormat, width, height) {
923
- if (target !== this.RENDERBUFFER) {
924
- this.setError(this.INVALID_ENUM);
925
- return;
926
- }
927
- const renderbuffer = this._activeRenderbuffer;
928
- if (!renderbuffer) {
929
- this.setError(this.INVALID_OPERATION);
930
- return;
931
- }
932
- this._saveError();
933
- this._native2.renderbufferStorageMultisample(target, samples, internalFormat, width, height);
934
- const error = this.getError();
935
- this._restoreError(error);
936
- if (error !== this.NO_ERROR) return;
937
- renderbuffer._width = width;
938
- renderbuffer._height = height;
939
- renderbuffer._format = internalFormat;
940
- }
941
- uniform1ui(location, v0) {
942
- if (!location) return;
943
- this._native2.uniform1ui(location._, v0);
944
- }
945
- uniform2ui(location, v0, v1) {
946
- if (!location) return;
947
- this._native2.uniform2ui(location._, v0, v1);
948
- }
949
- uniform3ui(location, v0, v1, v2) {
950
- if (!location) return;
951
- this._native2.uniform3ui(location._, v0, v1, v2);
952
- }
953
- uniform4ui(location, v0, v1, v2, v3) {
954
- if (!location) return;
955
- this._native2.uniform4ui(location._, v0, v1, v2, v3);
956
- }
957
- uniform1uiv(location, data, _srcOffset, _srcLength) {
958
- if (!location) return;
959
- const arr = data instanceof Uint32Array ? data : new Uint32Array(data);
960
- this._native2.uniform1uiv(location._, arr.length, arr);
961
- }
962
- uniform2uiv(location, data, _srcOffset, _srcLength) {
963
- if (!location) return;
964
- const arr = data instanceof Uint32Array ? data : new Uint32Array(data);
965
- this._native2.uniform2uiv(location._, arr.length / 2, arr);
966
- }
967
- uniform3uiv(location, data, _srcOffset, _srcLength) {
968
- if (!location) return;
969
- const arr = data instanceof Uint32Array ? data : new Uint32Array(data);
970
- this._native2.uniform3uiv(location._, arr.length / 3, arr);
971
- }
972
- uniform4uiv(location, data, _srcOffset, _srcLength) {
973
- if (!location) return;
974
- const arr = data instanceof Uint32Array ? data : new Uint32Array(data);
975
- this._native2.uniform4uiv(location._, arr.length / 4, arr);
976
- }
977
- uniformMatrix2x3fv(location, transpose, data, _srcOffset, _srcLength) {
978
- if (!location) return;
979
- const arr = data instanceof Float32Array ? data : new Float32Array(data);
980
- this._native2.uniformMatrix2x3fv(location._, transpose, arr);
981
- }
982
- uniformMatrix3x2fv(location, transpose, data, _srcOffset, _srcLength) {
983
- if (!location) return;
984
- const arr = data instanceof Float32Array ? data : new Float32Array(data);
985
- this._native2.uniformMatrix3x2fv(location._, transpose, arr);
986
- }
987
- uniformMatrix2x4fv(location, transpose, data, _srcOffset, _srcLength) {
988
- if (!location) return;
989
- const arr = data instanceof Float32Array ? data : new Float32Array(data);
990
- this._native2.uniformMatrix2x4fv(location._, transpose, arr);
991
- }
992
- uniformMatrix4x2fv(location, transpose, data, _srcOffset, _srcLength) {
993
- if (!location) return;
994
- const arr = data instanceof Float32Array ? data : new Float32Array(data);
995
- this._native2.uniformMatrix4x2fv(location._, transpose, arr);
996
- }
997
- uniformMatrix3x4fv(location, transpose, data, _srcOffset, _srcLength) {
998
- if (!location) return;
999
- const arr = data instanceof Float32Array ? data : new Float32Array(data);
1000
- this._native2.uniformMatrix3x4fv(location._, transpose, arr);
1001
- }
1002
- uniformMatrix4x3fv(location, transpose, data, _srcOffset, _srcLength) {
1003
- if (!location) return;
1004
- const arr = data instanceof Float32Array ? data : new Float32Array(data);
1005
- this._native2.uniformMatrix4x3fv(location._, transpose, arr);
1006
- }
1007
- /** WebGL1 getUniform falls to default:null for UNSIGNED_INT types. Handle them here. */
1008
- getUniform(program, location) {
1009
- const type = location?._activeInfo?.type;
1010
- const UINT = 5125, UVEC2 = 36294, UVEC3 = 36295, UVEC4 = 36296;
1011
- const isUintType = type === UINT || type === UVEC2 || type === UVEC3 || type === UVEC4;
1012
- if (!isUintType) return super.getUniform(program, location);
1013
- if (!program || !location) return null;
1014
- const data = this._gl.getUniformi(program._ | 0, location._ | 0);
1015
- if (!data) return null;
1016
- if (type === UINT) return data[0] >>> 0;
1017
- if (type === UVEC2) return new Uint32Array([data[0] >>> 0, data[1] >>> 0]);
1018
- if (type === UVEC3) return new Uint32Array([
1019
- data[0] >>> 0,
1020
- data[1] >>> 0,
1021
- data[2] >>> 0
1022
- ]);
1023
- return new Uint32Array([
1024
- data[0] >>> 0,
1025
- data[1] >>> 0,
1026
- data[2] >>> 0,
1027
- data[3] >>> 0
1028
- ]);
1029
- }
1030
- getUniformBlockIndex(program, uniformBlockName) {
1031
- return this._native2.getUniformBlockIndex(program._, uniformBlockName);
1032
- }
1033
- uniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding) {
1034
- this._native2.uniformBlockBinding(program._, uniformBlockIndex, uniformBlockBinding);
1035
- }
1036
- getActiveUniformBlockName(program, uniformBlockIndex) {
1037
- const name = this._native2.getActiveUniformBlockName(program._, uniformBlockIndex);
1038
- return name.length > 0 ? name : null;
1039
- }
1040
- getActiveUniformBlockParameter(program, uniformBlockIndex, pname) {
1041
- return this._native2.getActiveUniformBlockParameter(program._, uniformBlockIndex, pname);
1042
- }
1043
- getActiveUniforms(program, uniformIndices, pname) {
1044
- const result = this._native2.getActiveUniforms(program._, uniformIndices, pname);
1045
- return result;
1046
- }
1047
- getFragDataLocation(program, name) {
1048
- return this._native2.getFragDataLocation(program._, name);
1049
- }
1050
- getIndexedParameter(target, index) {
1051
- return this._native2.getIndexedParameteri(target, index);
1052
- }
1053
- getInternalformatParameter(target, internalformat, pname) {
1054
- return this._native2.getInternalformatParameter(target, internalformat, pname);
1055
- }
1056
- getParameter(pname) {
1057
- if (pname === 7938) return "WebGL 2.0";
1058
- if (pname === 35724) return "WebGL GLSL ES 3.00";
1059
- if (pname === 7939) {
1060
- warnNotImplemented("WebGL2RenderingContext.getParameter(GL_EXTENSIONS)");
1061
- return "";
1062
- }
1063
- if (pname === 36006) return this._activeDrawFramebuffer;
1064
- if (pname === 36010) return this._activeReadFramebuffer;
1065
- switch (pname) {
1066
- case 36183:
1067
- case 35071:
1068
- case 32883:
1069
- case 36063:
1070
- case 34852:
1071
- case 36203:
1072
- case 33001:
1073
- case 33e3:
1074
- case 37157:
1075
- case 35373:
1076
- case 35657:
1077
- case 35077:
1078
- case 35978:
1079
- case 35979:
1080
- case 35968:
1081
- case 35376:
1082
- case 35375:
1083
- case 35659:
1084
- case 37154:
1085
- case 35371:
1086
- case 35658:
1087
- case 35379:
1088
- case 35374:
1089
- case 35377:
1090
- case 35076:
1091
- case 3330:
1092
- case 3332:
1093
- case 3331:
1094
- case 35053:
1095
- case 35055:
1096
- case 3074:
1097
- case 32874:
1098
- case 35869:
1099
- case 36389:
1100
- case 35983:
1101
- case 35368:
1102
- case 35380:
1103
- case 32878:
1104
- case 3314:
1105
- case 32877:
1106
- case 3316:
1107
- case 3315:
1108
- case 34045: return this._native2.getParameterx(pname)?.deepUnpack() | 0;
1109
- case 35977:
1110
- case 36388:
1111
- case 36387: return !!this._native2.getParameterx(pname)?.deepUnpack();
1112
- }
1113
- return super.getParameter(pname);
1114
- }
1115
- getStringi(name, index) {
1116
- const s = this._native2.getStringi(name, index);
1117
- return s.length > 0 ? s : null;
1118
- }
1119
- /**
1120
- * WebGL2 supports ~30+ renderbuffer formats (R8, RG8, RGBA8, RGBA16F,
1121
- * DEPTH_COMPONENT24, DEPTH32F_STENCIL8, etc.). The WebGL1 base class
1122
- * only allows 7 formats. Delegate format validation to native GL.
1123
- */
1124
- renderbufferStorage(target, internalFormat, width, height) {
1125
- if (target !== this.RENDERBUFFER) {
1126
- this.setError(this.INVALID_ENUM);
1127
- return;
1128
- }
1129
- const renderbuffer = this._activeRenderbuffer;
1130
- if (!renderbuffer) {
1131
- this.setError(this.INVALID_OPERATION);
1132
- return;
1133
- }
1134
- if (width < 0 || height < 0) {
1135
- this.setError(this.INVALID_VALUE);
1136
- return;
1137
- }
1138
- while (this._gl.getError() !== this.NO_ERROR) {}
1139
- this._gl.renderbufferStorage(target, internalFormat, width, height);
1140
- if (this._gl.getError() !== this.NO_ERROR) return;
1141
- renderbuffer._width = width;
1142
- renderbuffer._height = height;
1143
- renderbuffer._format = internalFormat;
1144
- const activeFramebuffer = this._activeFramebuffer;
1145
- if (activeFramebuffer) {
1146
- const attachments = this._getAttachments();
1147
- let needsUpdate = false;
1148
- for (let i = 0; i < attachments.length; ++i) {
1149
- if (activeFramebuffer._attachments[attachments[i]] === renderbuffer) {
1150
- needsUpdate = true;
1151
- break;
1152
- }
1153
- }
1154
- if (needsUpdate) this._updateFramebufferAttachments(activeFramebuffer);
1155
- }
1156
- }
1157
- /**
1158
- * WebGL2 makes several WebGL1 extensions part of the core spec.
1159
- * EXT_color_buffer_float and EXT_color_buffer_half_float are always
1160
- * available in WebGL2 contexts. Append them if the base class didn't.
1161
- */
1162
- getSupportedExtensions() {
1163
- const exts = super.getSupportedExtensions();
1164
- const ensure = [
1165
- "EXT_color_buffer_float",
1166
- "EXT_color_buffer_half_float",
1167
- "OES_texture_half_float"
1168
- ];
1169
- for (const ext of ensure) {
1170
- if (exts.indexOf(ext) === -1) exts.push(ext);
1171
- }
1172
- return exts;
1173
- }
1174
- /**
1175
- * WebGL2 allows reading pixels in many more format/type combinations
1176
- * than WebGL1's strict RGBA/UNSIGNED_BYTE. Delegate validation to native.
1177
- */
1178
- readPixels(x, y, width, height, format, type, pixels) {
1179
- if (!pixels) return;
1180
- if (width < 0 || height < 0) {
1181
- this.setError(this.INVALID_VALUE);
1182
- return;
1183
- }
1184
- if (!this._framebufferOk()) return;
1185
- const componentCount = format === 6408 || format === 32856 ? 4 : format === 6407 ? 3 : format === 33319 ? 2 : 1;
1186
- const bytesPerComponent = type === 5126 ? 4 : type === 5131 || type === 36193 ? 2 : type === 5125 || type === 5124 ? 4 : type === 5123 || type === 5122 ? 2 : 1;
1187
- const byteCount = width * height * componentCount * bytesPerComponent;
1188
- const pixelData = new Uint8Array(byteCount);
1189
- this._saveError();
1190
- const result = this._gl.readPixels(x, y, width, height, format, type, Uint8ArrayToVariant(pixelData));
1191
- const error = this.getError();
1192
- this._restoreError(error);
1193
- if (error !== this.NO_ERROR) return;
1194
- const src = result && result.length > 0 ? result : pixelData;
1195
- if (pixels instanceof Uint8Array) {
1196
- pixels.set(src);
1197
- } else if (pixels instanceof Float32Array) {
1198
- const floatView = new Float32Array(src.buffer, 0, pixels.length);
1199
- pixels.set(floatView);
1200
- }
1201
- }
1202
- /**
1203
- * WebGL2 never blocks draw calls on JS-side framebuffer format checks.
1204
- * The native GL (Mesa/libepoxy) handles completeness and generates
1205
- * INVALID_FRAMEBUFFER_OPERATION for truly incomplete FBOs at draw time.
1206
- * This matches headless-gl's approach: _framebufferOk() always returns true.
1207
- *
1208
- * The base class rejects valid WebGL2 formats (RGBA16F/HALF_FLOAT, depth
1209
- * textures, WebGL2 renderbuffer formats) causing silent rendering failures
1210
- * for postprocessing effects and environment maps.
1211
- */
1212
- _framebufferOk() {
1213
- return true;
1214
- }
1215
- };
1216
-
1217
- //#endregion
1218
- export { WebGL2RenderingContext };
1
+ import{Uint8ArrayToVariant as e,arrayToUint8Array as t,checkObject as n,convertPixels as r,extractImageData as i,premultiplyAlpha as a,vertexCount as o}from"./utils.js";import{WebGLActiveInfo as s}from"./webgl-active-info.js";import{WebGLFramebuffer as c}from"./webgl-framebuffer.js";import{WebGLRenderbuffer as l}from"./webgl-renderbuffer.js";import{WebGLTexture as u}from"./webgl-texture.js";import{WebGLContextBase as d}from"./webgl-context-base.js";import{WebGLQuery as f}from"./webgl-query.js";import{WebGLSampler as p}from"./webgl-sampler.js";import{WebGLSync as m}from"./webgl-sync.js";import{WebGLTransformFeedback as h}from"./webgl-transform-feedback.js";import{WebGLVertexArrayObject as g}from"./webgl-vertex-array-object.js";import _ from"@girs/gwebgl-0.1";import v from"gi://GdkPixbuf?version=2.0";import{warnNotImplemented as y}from"@gjsify/utils";var b=class b extends d{get _gl(){return this._native2}constructor(e,t={}){super(e,t),this._queries={},this._samplers={},this._transformFeedbacks={},this._vertexArrayObjects={},this._syncs={},this._activeReadFramebuffer=null,this._activeDrawFramebuffer=null,this._native2=new _.WebGL2RenderingContext({}),this._init()}_getGlslVersion(e){return e?`300 es`:`130`}_validFramebufferAttachment(e){return super._validFramebufferAttachment(e)?!0:e>=36065&&e<=36079}static{this._WGL2_ALL_COLOR_ATTACHMENTS=[36064,36065,36066,36067,36068,36069,36070,36071,36072,36073,36074,36075,36076,36077,36078,36079]}_getColorAttachments(){return b._WGL2_ALL_COLOR_ATTACHMENTS}_preCheckFramebufferStatus(e){let t=e._attachments,n=0,r=0,i=[this.COLOR_ATTACHMENT0,this.DEPTH_ATTACHMENT,this.STENCIL_ATTACHMENT,this.DEPTH_STENCIL_ATTACHMENT,...b._WGL2_ALL_COLOR_ATTACHMENTS];for(let a of i){let i=t[a];if(i){if(i instanceof u){let t=e._attachmentLevel[a]??0,o=i._levelWidth[t]??0,s=i._levelHeight[t]??0;if(o>0&&s>0){n=o,r=s;break}}else if(i instanceof l&&i._width>0&&i._height>0){n=i._width,r=i._height;break}}}return n>0&&r>0?(e._width=n,e._height=r,this.FRAMEBUFFER_COMPLETE):this.FRAMEBUFFER_INCOMPLETE_ATTACHMENT}_updateFramebufferAttachments(e){if(super._updateFramebufferAttachments(e),e)for(let t=1;t<=15;t++){let n=36064+t;if(!(n in e._attachments))continue;let r=e._attachments[n];if(r instanceof u){let t=e._attachmentFace[n]||this.TEXTURE_2D,i=e._attachmentLevel[n]??0;this._gl.framebufferTexture2D(this.FRAMEBUFFER,n,t,r._|0,i|0)}else r instanceof l?this._gl.framebufferRenderbuffer(this.FRAMEBUFFER,n,this.RENDERBUFFER,r._|0):this._gl.framebufferTexture2D(this.FRAMEBUFFER,n,this.TEXTURE_2D,0,0)}}bindBuffer(e,t){if(e===35345||e===35982||e===36662||e===36663){let n=t?t._|0:0;this._gl.bindBuffer(e,n);return}super.bindBuffer(e,t)}bindFramebuffer(e,t){if(e===36008||e===36009){if(!n(t))throw TypeError(`bindFramebuffer(GLenum, WebGLFramebuffer)`);if(t&&t._pendingDelete||t&&!this._checkWrapper(t,c))return;let r=t?t._|0:this._gtkFboId;if(this._gl.bindFramebuffer(e,r),e===36008){let e=this._activeReadFramebuffer;e!==t&&(e&&(--e._refCount,e._checkDelete()),t&&(t._refCount+=1)),this._activeReadFramebuffer=t}else{let e=this._activeDrawFramebuffer;e!==t&&(e&&(--e._refCount,e._checkDelete()),t&&(t._refCount+=1)),this._activeDrawFramebuffer=t,this._activeFramebuffer=t}return}super.bindFramebuffer(this.FRAMEBUFFER,t),this._activeReadFramebuffer=t,this._activeDrawFramebuffer=t}deleteFramebuffer(e){this._activeReadFramebuffer===e&&this.bindFramebuffer(36008,null),this._activeDrawFramebuffer===e&&this.bindFramebuffer(36009,null),super.deleteFramebuffer(e)}bufferData(n,r,i){let a=n===35345||n===35982||n===36662||n===36663,o=i===35041||i===35043||i===35045||i===35042||i===35044||i===35046?this.STATIC_DRAW:i;if(a){if(typeof r==`number`)r>=0&&this._gl.bufferDataSizeOnly(n,r,o);else if(typeof r==`object`&&r){let i=t(r);this._gl.bufferData(n,e(i),o)}return}super.bufferData(n,r,o)}bufferSubData(n,r,i){if(n===35345||n===35982||n===36662||n===36663){if(r<0){this.setError(this.INVALID_VALUE);return}if(!i){this.setError(this.INVALID_VALUE);return}let a=t(i);this._gl.bufferSubData(n,r,e(a));return}super.bufferSubData(n,r,i)}bindTexture(e,t){if(e===32879||e===35866){let n=t?t._|0:0;this._gl.bindTexture(e,n),t&&(t._binding=e);return}super.bindTexture(e,t)}texParameteri(e,t,n){if(e===32879||e===35866){this._gl.texParameteri(e,t,n);return}if(t===32882||t===34892||t===34893||t===33084||t===33085||t===33083||t===33082){this._gl.texParameteri(e,t,n);return}super.texParameteri(e,t,n)}drawArrays(e,t,n){if(t<0||n<0){this.setError(this.INVALID_VALUE);return}if(!this._checkStencilState())return;let r=o(this,e,n);if(r<0){this.setError(this.INVALID_ENUM);return}this._framebufferOk()&&n!==0&&this._checkVertexAttribState(n+t-1>>>0)&&this._native2.drawArrays(e,t,r)}drawElements(e=0,t=0,n=0,r=0){if(t<0||r<0){this.setError(this.INVALID_VALUE);return}if(!this._checkStencilState())return;let i=this._vertexObjectState._elementArrayBufferBinding;if(!i){this.setError(this.INVALID_OPERATION);return}let a=null,o=r;if(n===this.UNSIGNED_SHORT){if(o%2){this.setError(this.INVALID_OPERATION);return}o>>=1,a=new Uint16Array(i._elements.buffer)}else if(n===this.UNSIGNED_INT){if(o%4){this.setError(this.INVALID_OPERATION);return}o>>=2,a=new Uint32Array(i._elements.buffer)}else if(n===this.UNSIGNED_BYTE)a=i._elements;else{this.setError(this.INVALID_ENUM);return}let s=t;switch(e){case this.TRIANGLES:t%3&&(s-=t%3);break;case this.LINES:t%2&&(s-=t%2);break;case this.POINTS:break;case this.LINE_LOOP:case this.LINE_STRIP:if(t<2){this.setError(this.INVALID_OPERATION);return}break;case this.TRIANGLE_FAN:case this.TRIANGLE_STRIP:if(t<3){this.setError(this.INVALID_OPERATION);return}break;default:this.setError(this.INVALID_ENUM);return}if(!this._framebufferOk()||t===0)return;let c=0;for(let e=o;e<o+s;++e)e<a.length&&a[e]>c&&(c=a[e]);this._checkVertexAttribState(c)&&this._native2.drawElements(e,s,n,r)}createVertexArray(){let e=this._native2.createVertexArray();if(!e)return null;let t=new g(e,this);return this._vertexArrayObjects[e]=t,t}deleteVertexArray(e){!e||!(e instanceof g)||(e._pendingDelete=!0,e._checkDelete())}isVertexArray(e){return!e||!(e instanceof g)?!1:this._native2.isVertexArray(e._)}bindVertexArray(e){e===null?(this._native2.bindVertexArray(0),this._vertexObjectState=this._defaultVertexObjectState):e instanceof g?(this._native2.bindVertexArray(e._),this._vertexObjectState=e._objectState):this.setError(this.INVALID_OPERATION)}createQuery(){let e=this._native2.createQuery();if(!e)return null;let t=new f(e,this);return this._queries[e]=t,t}deleteQuery(e){!e||!(e instanceof f)||(e._pendingDelete=!0,e._checkDelete())}isQuery(e){return!e||!(e instanceof f)?!1:this._native2.isQuery(e._)}beginQuery(e,t){t instanceof f&&this._native2.beginQuery(e,t._)}endQuery(e){this._native2.endQuery(e)}getQuery(e,t){return y(`WebGL2RenderingContext.getQuery`),null}getQueryParameter(e,t){return e instanceof f?this._native2.getQueryParameter(e._,t):null}createSampler(){let e=this._native2.createSampler();if(!e)return null;let t=new p(e,this);return this._samplers[e]=t,t}deleteSampler(e){!e||!(e instanceof p)||(e._pendingDelete=!0,e._checkDelete())}isSampler(e){return!e||!(e instanceof p)?!1:this._native2.isSampler(e._)}bindSampler(e,t){this._native2.bindSampler(e,t?t._:0)}samplerParameteri(e,t,n){e instanceof p&&this._native2.samplerParameteri(e._,t,n)}samplerParameterf(e,t,n){e instanceof p&&this._native2.samplerParameterf(e._,t,n)}getSamplerParameter(e,t){return e instanceof p?t===33082||t===33083?this._native2.getSamplerParameterf(e._,t):this._native2.getSamplerParameteri(e._,t):null}fenceSync(e,t){let n=this._native2.fenceSync(e,t);if(!n)return null;let r=new m(n,this);return this._syncs[n]=r,r}isSync(e){return!e||!(e instanceof m)?!1:this._native2.isSync(e._)}deleteSync(e){!e||!(e instanceof m)||(e._pendingDelete=!0,e._checkDelete())}clientWaitSync(e,t,n){return e instanceof m?this._native2.clientWaitSync(e._,t,n):37148}waitSync(e,t,n){e instanceof m&&this._native2.waitSync(e._,t,n)}getSyncParameter(e,t){return e instanceof m?this._native2.getSyncParameter(e._,t):null}createTransformFeedback(){let e=this._native2.createTransformFeedback();if(!e)return null;let t=new h(e,this);return this._transformFeedbacks[e]=t,t}deleteTransformFeedback(e){!e||!(e instanceof h)||(e._pendingDelete=!0,e._checkDelete())}isTransformFeedback(e){return!e||!(e instanceof h)?!1:this._native2.isTransformFeedback(e._)}bindTransformFeedback(e,t){this._native2.bindTransformFeedback(e,t?t._:0)}beginTransformFeedback(e){this._native2.beginTransformFeedback(e)}endTransformFeedback(){this._native2.endTransformFeedback()}pauseTransformFeedback(){this._native2.pauseTransformFeedback()}resumeTransformFeedback(){this._native2.resumeTransformFeedback()}transformFeedbackVaryings(e,t,n){this._native2.transformFeedbackVaryings(e._,t,n)}getTransformFeedbackVarying(e,t){let n=this._native2.getTransformFeedbackVarying(e._,t).deepUnpack();return new s({size:n.size,type:n.type,name:n.name})}bindBufferBase(e,t,n){this._native2.bindBufferBase(e,t,n?n._:0)}bindBufferRange(e,t,n,r,i){this._native2.bindBufferRange(e,t,n?n._:0,r,i)}copyBufferSubData(e,t,n,r,i){this._native2.copyBufferSubData(e,t,n,r,i)}getBufferSubData(e,t,n,r,i){let a=i===void 0?n.byteLength-(r??0):i,o=this._native2.getBufferSubData(e,t,a),s=new Uint8Array(n.buffer,n.byteOffset+(r??0)*(n instanceof Uint8Array?1:n.BYTES_PER_ELEMENT??1));s.set(o.subarray(0,s.byteLength))}texImage3D(t,n,r,i,a,o,s,c,l,u){u===null?this._native2.texImage3DNull(t,n,r,i,a,o,s,c,l):this._native2.texImage3D(t,n,r,i,a,o,s,c,l,e(new Uint8Array(u.buffer,u.byteOffset,u.byteLength)))}texSubImage3D(t,n,r,i,a,o,s,c,l,u,d){d!==null&&this._native2.texSubImage3D(t,n,r,i,a,o,s,c,l,u,e(new Uint8Array(d.buffer,d.byteOffset,d.byteLength)))}compressedTexImage3D(t,n,r,i,a,o,s,c,l){this._native2.compressedTexImage3D(t,n,r,i,a,o,s,e(new Uint8Array(l.buffer,l.byteOffset,l.byteLength)))}compressedTexSubImage3D(t,n,r,i,a,o,s,c,l,u,d){this._native2.compressedTexSubImage3D(t,n,r,i,a,o,s,c,l,e(new Uint8Array(d.buffer,d.byteOffset,d.byteLength)))}copyTexSubImage3D(e,t,n,r,i,a,o,s,c){this._native2.copyTexSubImage3D(e,t,n,r,i,a,o,s,c)}texStorage2D(e,t,n,r,i){this._native2.texStorage2D(e,t,n,r,i);let a=this._getTexImage(e);if(a){for(let e=0;e<t;e++)a._levelWidth[e]=Math.max(1,r>>e),a._levelHeight[e]=Math.max(1,i>>e);a._format=this.RGBA,a._type=this.UNSIGNED_BYTE}}texStorage3D(e,t,n,r,i,a){this._native2.texStorage3D(e,t,n,r,i,a)}texImage2D(t=0,n=0,o=0,s=0,c=0,l=0,u=0,d=0,f){let p=0,m=0,h=0,g=0;if(arguments.length===6)if(d=c,h=s,l instanceof v.Pixbuf){let e=l;p=e.get_width(),m=e.get_height(),f=e.get_pixels()}else{let e=i(l);if(e==null)throw TypeError(`texImage2D(GLenum, GLint, GLenum, GLint, GLenum, GLenum, ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement)`);p=e.width,m=e.height,f=e.data}else arguments.length>=9&&(p=s,m=c,g=l,h=u);let _=this._getTexImage(t);if(!_){this.setError(this.INVALID_OPERATION);return}let y=r(f);if(this._unpackPremultAlpha&&y&&h===this.RGBA&&(y=a(y)),this._unpackFlipY&&y&&p>0&&m>0){let e=this._computePixelSize(d,h);if(e>0){let t=this._computeRowStride(p,e),n=new Uint8Array(y.length);for(let e=0;e<m;e++){let r=e*t,i=(m-1-e)*t;n.set(y.subarray(r,r+t),i)}y=n}}this._saveError(),this._gl.texImage2D(t,n,o,p,m,g,h,d,e(y));let b=this.getError();if(this._restoreError(b),b!==this.NO_ERROR)return;_._levelWidth[n]=p,_._levelHeight[n]=m,_._format=h,_._type=d;let x=this._activeFramebuffer;if(x){let e=!1,t=this._getAttachments();for(let n=0;n<t.length;++n)if(x._attachments[t[n]]===_){e=!0;break}e&&this._activeFramebuffer&&this._updateFramebufferAttachments(this._activeFramebuffer)}}texSubImage2D(t=0,n=0,o=0,s=0,c=0,l=0,u=0,d=0,f){let p=0,m=0,h=0;if(arguments.length===7)if(d=l,h=c,u instanceof v.Pixbuf){let e=u;p=e.get_width(),m=e.get_height(),f=e.get_pixels()}else{let e=i(u);if(e==null)throw TypeError(`texSubImage2D(GLenum, GLint, GLint, GLint, GLenum, GLenum, ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement)`);p=e.width,m=e.height,f=e.data}else p=c,m=l,h=u;if(!this._getTexImage(t)){this.setError(this.INVALID_OPERATION);return}let g=r(f);if(!g){this.setError(this.INVALID_OPERATION);return}if(this._unpackPremultAlpha&&g&&h===this.RGBA&&(g=a(g)),this._unpackFlipY&&g&&p>0&&m>0){let e=this._computePixelSize(d,h);if(e>0){let t=this._computeRowStride(p,e),n=new Uint8Array(g.length);for(let e=0;e<m;e++){let r=e*t,i=(m-1-e)*t;n.set(g.subarray(r,r+t),i)}g=n}}this._gl.texSubImage2D(t,n,o,s,p,m,h,d,e(g))}framebufferTextureLayer(e,t,n,r,i){this._native2.framebufferTextureLayer(e,t,n?n._:0,r,i)}drawArraysInstanced(e,t,n,r){if(t<0||n<0||r<0){this.setError(this.INVALID_VALUE);return}if(!this._checkStencilState())return;let i=o(this,e,n);if(i<0){this.setError(this.INVALID_ENUM);return}if(this._framebufferOk()&&!(n===0||r===0)&&this._checkVertexAttribState(n+t-1>>>0)){if(globalThis.__GJSIFY_DEBUG_GL){let e=this.__drawInstCount=(this.__drawInstCount|0)+1;(e<=5||e%100==0)&&console.log(`[WebGL] drawArraysInstanced #${e} count=${i} instances=${r} fbo=${this._activeFramebuffer?._??`_gtkFbo`}`)}this._native2.drawArraysInstanced(e,t,i,r)}}drawElementsInstanced(e,t,n,r,i){if(t<0||r<0||i<0){this.setError(this.INVALID_VALUE);return}if(!this._checkStencilState())return;let a=this._vertexObjectState._elementArrayBufferBinding;if(!a){this.setError(this.INVALID_OPERATION);return}let o=null,s=r;if(n===this.UNSIGNED_SHORT){if(s%2){this.setError(this.INVALID_OPERATION);return}s>>=1,o=new Uint16Array(a._elements.buffer)}else if(n===this.UNSIGNED_INT){if(s%4){this.setError(this.INVALID_OPERATION);return}s>>=2,o=new Uint32Array(a._elements.buffer)}else if(n===this.UNSIGNED_BYTE)o=a._elements;else{this.setError(this.INVALID_ENUM);return}let c=t;switch(e){case this.TRIANGLES:t%3&&(c-=t%3);break;case this.LINES:t%2&&(c-=t%2);break;case this.POINTS:break;case this.LINE_LOOP:case this.LINE_STRIP:if(t<2){this.setError(this.INVALID_OPERATION);return}break;case this.TRIANGLE_FAN:case this.TRIANGLE_STRIP:if(t<3){this.setError(this.INVALID_OPERATION);return}break;default:this.setError(this.INVALID_ENUM);return}if(!this._framebufferOk())return;if(c===0||i===0){this._checkVertexAttribState(0);return}if(c+s>>>0>o.length){this.setError(this.INVALID_OPERATION);return}let l=0;for(let e=s;e<s+c;++e)o[e]>l&&(l=o[e]);this._checkVertexAttribState(l)&&this._native2.drawElementsInstanced(e,c,n,r,i)}vertexAttribDivisor(e,t){this._native2.vertexAttribDivisor(e,t)}vertexAttribIPointer(e,t,n,r,i){this._native2.vertexAttribIPointer(e,t,n,r,i)}drawBuffers(e){let t=!1;for(let n=0;n<e.length;n++)if(e[n]===1029){t=!0;break}if(!t){this._native2.drawBuffers(e);return}this._native2.drawBuffers(e.map(e=>e===1029?this.COLOR_ATTACHMENT0:e))}drawRangeElements(e,t,n,r,i,a){if(r<0||a<0){this.setError(this.INVALID_VALUE);return}if(n<t){this.setError(this.INVALID_VALUE);return}this.drawElements(e,r,i,a)}blitFramebuffer(e,t,n,r,i,a,o,s,c,l){if(globalThis.__GJSIFY_DEBUG_GL){let e=this._gl.getError();e!==0&&console.log(`[WebGL] blitFramebuffer PRE-ERROR 0x${e.toString(16)}`)}if(this._native2.blitFramebuffer(e,t,n,r,i,a,o,s,c,l),globalThis.__GJSIFY_DEBUG_GL){let i=this._gl.getError(),a=this.__blitCount=(this.__blitCount|0)+1;a<=5&&console.log(`[WebGL] blitFramebuffer #${a} src=(${e},${t},${n},${r}) readFbo=${this._activeReadFramebuffer?._??`_gtkFbo`} err=${i===0?`OK`:`0x`+i.toString(16)}`)}}clearBufferfv(e,t,n,r){let i=this._native2;if(typeof i.clearBufferfv==`function`){i.clearBufferfv(e,t,Array.from(n));return}let a=n;if(e===6144){let e=this.getParameter(this.COLOR_CLEAR_VALUE);this.clearColor(a[0]??0,a[1]??0,a[2]??0,a[3]??0),this.clear(this.COLOR_BUFFER_BIT),e&&this.clearColor(e[0],e[1],e[2],e[3])}else if(e===6145){let e=this.getParameter(this.DEPTH_CLEAR_VALUE);this.clearDepth(a[0]??1),this.clear(this.DEPTH_BUFFER_BIT),e!==null&&this.clearDepth(e)}}clearBufferiv(e,t,n,r){let i=this._native2;if(typeof i.clearBufferiv==`function`){i.clearBufferiv(e,t,Array.from(n));return}if(e===6146){let e=n,t=this.getParameter(this.STENCIL_CLEAR_VALUE);this.clearStencil(e[0]??0),this.clear(this.STENCIL_BUFFER_BIT),t!==null&&this.clearStencil(t)}}clearBufferuiv(e,t,n,r){let i=this._native2;if(typeof i.clearBufferuiv==`function`){i.clearBufferuiv(e,t,Array.from(n));return}}clearBufferfi(e,t,n,r){let i=this._native2;if(typeof i.clearBufferfi==`function`){i.clearBufferfi(e,t,n,r);return}if(e===34041){let e=this.getParameter(this.DEPTH_CLEAR_VALUE),t=this.getParameter(this.STENCIL_CLEAR_VALUE);this.clearDepth(n),this.clearStencil(r),this.clear(this.DEPTH_BUFFER_BIT|this.STENCIL_BUFFER_BIT),e!==null&&this.clearDepth(e),t!==null&&this.clearStencil(t)}}invalidateFramebuffer(e,t){this._native2.invalidateFramebuffer(e,t)}invalidateSubFramebuffer(e,t,n,r,i,a){this._native2.invalidateSubFramebuffer(e,t,n,r,i,a)}readBuffer(e){this._native2.readBuffer(e)}renderbufferStorageMultisample(e,t,n,r,i){if(e!==this.RENDERBUFFER){this.setError(this.INVALID_ENUM);return}let a=this._activeRenderbuffer;if(!a){this.setError(this.INVALID_OPERATION);return}this._saveError(),this._native2.renderbufferStorageMultisample(e,t,n,r,i);let o=this.getError();this._restoreError(o),o===this.NO_ERROR&&(a._width=r,a._height=i,a._format=n)}uniform1ui(e,t){e&&this._native2.uniform1ui(e._,t)}uniform2ui(e,t,n){e&&this._native2.uniform2ui(e._,t,n)}uniform3ui(e,t,n,r){e&&this._native2.uniform3ui(e._,t,n,r)}uniform4ui(e,t,n,r,i){e&&this._native2.uniform4ui(e._,t,n,r,i)}uniform1uiv(e,t,n,r){if(!e)return;let i=t instanceof Uint32Array?t:new Uint32Array(t);this._native2.uniform1uiv(e._,i.length,i)}uniform2uiv(e,t,n,r){if(!e)return;let i=t instanceof Uint32Array?t:new Uint32Array(t);this._native2.uniform2uiv(e._,i.length/2,i)}uniform3uiv(e,t,n,r){if(!e)return;let i=t instanceof Uint32Array?t:new Uint32Array(t);this._native2.uniform3uiv(e._,i.length/3,i)}uniform4uiv(e,t,n,r){if(!e)return;let i=t instanceof Uint32Array?t:new Uint32Array(t);this._native2.uniform4uiv(e._,i.length/4,i)}uniformMatrix2x3fv(e,t,n,r,i){if(!e)return;let a=n instanceof Float32Array?n:new Float32Array(n);this._native2.uniformMatrix2x3fv(e._,t,a)}uniformMatrix3x2fv(e,t,n,r,i){if(!e)return;let a=n instanceof Float32Array?n:new Float32Array(n);this._native2.uniformMatrix3x2fv(e._,t,a)}uniformMatrix2x4fv(e,t,n,r,i){if(!e)return;let a=n instanceof Float32Array?n:new Float32Array(n);this._native2.uniformMatrix2x4fv(e._,t,a)}uniformMatrix4x2fv(e,t,n,r,i){if(!e)return;let a=n instanceof Float32Array?n:new Float32Array(n);this._native2.uniformMatrix4x2fv(e._,t,a)}uniformMatrix3x4fv(e,t,n,r,i){if(!e)return;let a=n instanceof Float32Array?n:new Float32Array(n);this._native2.uniformMatrix3x4fv(e._,t,a)}uniformMatrix4x3fv(e,t,n,r,i){if(!e)return;let a=n instanceof Float32Array?n:new Float32Array(n);this._native2.uniformMatrix4x3fv(e._,t,a)}getUniform(e,t){let n=t?._activeInfo?.type,r=5125,i=36294,a=36295;if(!(n===r||n===i||n===a||n===36296))return super.getUniform(e,t);if(!e||!t)return null;let o=this._gl.getUniformi(e._|0,t._|0);return o?n===r?o[0]>>>0:n===i?new Uint32Array([o[0]>>>0,o[1]>>>0]):n===a?new Uint32Array([o[0]>>>0,o[1]>>>0,o[2]>>>0]):new Uint32Array([o[0]>>>0,o[1]>>>0,o[2]>>>0,o[3]>>>0]):null}getUniformBlockIndex(e,t){return this._native2.getUniformBlockIndex(e._,t)}uniformBlockBinding(e,t,n){this._native2.uniformBlockBinding(e._,t,n)}getActiveUniformBlockName(e,t){let n=this._native2.getActiveUniformBlockName(e._,t);return n.length>0?n:null}getActiveUniformBlockParameter(e,t,n){return this._native2.getActiveUniformBlockParameter(e._,t,n)}getActiveUniforms(e,t,n){return this._native2.getActiveUniforms(e._,t,n)}getFragDataLocation(e,t){return this._native2.getFragDataLocation(e._,t)}getIndexedParameter(e,t){return this._native2.getIndexedParameteri(e,t)}getInternalformatParameter(e,t,n){return this._native2.getInternalformatParameter(e,t,n)}getParameter(e){if(e===7938)return`WebGL 2.0`;if(e===35724)return`WebGL GLSL ES 3.00`;if(e===7939)return y(`WebGL2RenderingContext.getParameter(GL_EXTENSIONS)`),``;if(e===36006)return this._activeDrawFramebuffer;if(e===36010)return this._activeReadFramebuffer;switch(e){case 36183:case 35071:case 32883:case 36063:case 34852:case 36203:case 33001:case 33e3:case 37157:case 35373:case 35657:case 35077:case 35978:case 35979:case 35968:case 35376:case 35375:case 35659:case 37154:case 35371:case 35658:case 35379:case 35374:case 35377:case 35076:case 3330:case 3332:case 3331:case 35053:case 35055:case 3074:case 32874:case 35869:case 36389:case 35983:case 35368:case 35380:case 32878:case 3314:case 32877:case 3316:case 3315:case 34045:return this._native2.getParameterx(e)?.deepUnpack()|0;case 35977:case 36388:case 36387:return!!this._native2.getParameterx(e)?.deepUnpack()}return super.getParameter(e)}getStringi(e,t){let n=this._native2.getStringi(e,t);return n.length>0?n:null}renderbufferStorage(e,t,n,r){if(e!==this.RENDERBUFFER){this.setError(this.INVALID_ENUM);return}let i=this._activeRenderbuffer;if(!i){this.setError(this.INVALID_OPERATION);return}if(n<0||r<0){this.setError(this.INVALID_VALUE);return}for(;this._gl.getError()!==this.NO_ERROR;);if(this._gl.renderbufferStorage(e,t,n,r),this._gl.getError()!==this.NO_ERROR)return;i._width=n,i._height=r,i._format=t;let a=this._activeFramebuffer;if(a){let e=this._getAttachments(),t=!1;for(let n=0;n<e.length;++n)if(a._attachments[e[n]]===i){t=!0;break}t&&this._updateFramebufferAttachments(a)}}getSupportedExtensions(){let e=super.getSupportedExtensions();for(let t of[`EXT_color_buffer_float`,`EXT_color_buffer_half_float`,`OES_texture_half_float`])e.indexOf(t)===-1&&e.push(t);return e}readPixels(t,n,r,i,a,o,s){if(!s)return;if(r<0||i<0){this.setError(this.INVALID_VALUE);return}if(!this._framebufferOk())return;let c=a===6408||a===32856?4:a===6407?3:a===33319?2:1,l=o===5126?4:o===5131||o===36193?2:o===5125||o===5124?4:o===5123||o===5122?2:1,u=r*i*c*l,d=new Uint8Array(u);this._saveError();let f=this._gl.readPixels(t,n,r,i,a,o,e(d)),p=this.getError();if(this._restoreError(p),p!==this.NO_ERROR)return;let m=f&&f.length>0?f:d;if(s instanceof Uint8Array)s.set(m);else if(s instanceof Float32Array){let e=new Float32Array(m.buffer,0,s.length);s.set(e)}}_framebufferOk(){return!0}};export{b as WebGL2RenderingContext};