@gjsify/webgl 0.0.2

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 (154) hide show
  1. package/README.md +14 -0
  2. package/lib/cjs/@types/glsl-tokenizer/index.d.js +0 -0
  3. package/lib/cjs/extensions/ext-blend-minmax.js +37 -0
  4. package/lib/cjs/extensions/ext-texture-filter-anisotropic.js +37 -0
  5. package/lib/cjs/extensions/oes-element-index-unit.js +33 -0
  6. package/lib/cjs/extensions/oes-standard-derivatives.js +36 -0
  7. package/lib/cjs/extensions/oes-texture-float-linear.js +33 -0
  8. package/lib/cjs/extensions/oes-texture-float.js +33 -0
  9. package/lib/cjs/extensions/stackgl-destroy-context.js +31 -0
  10. package/lib/cjs/extensions/stackgl-resize-drawing-buffer.js +31 -0
  11. package/lib/cjs/html-canvas-element.js +89 -0
  12. package/lib/cjs/index.js +34 -0
  13. package/lib/cjs/index.spec.js +175 -0
  14. package/lib/cjs/linkable.js +69 -0
  15. package/lib/cjs/test.js +25 -0
  16. package/lib/cjs/types/constructor.js +15 -0
  17. package/lib/cjs/types/extension.js +15 -0
  18. package/lib/cjs/types/index.js +21 -0
  19. package/lib/cjs/types/typed-array.js +15 -0
  20. package/lib/cjs/types/webgl-constants.js +15 -0
  21. package/lib/cjs/types/webgl-context-attribute-options.js +15 -0
  22. package/lib/cjs/utils.js +241 -0
  23. package/lib/cjs/webgl-active-info.js +29 -0
  24. package/lib/cjs/webgl-buffer.js +37 -0
  25. package/lib/cjs/webgl-context-attributes.js +43 -0
  26. package/lib/cjs/webgl-drawing-buffer-wrapper.js +29 -0
  27. package/lib/cjs/webgl-framebuffer.js +128 -0
  28. package/lib/cjs/webgl-program.js +45 -0
  29. package/lib/cjs/webgl-renderbuffer.js +43 -0
  30. package/lib/cjs/webgl-rendering-context.js +3355 -0
  31. package/lib/cjs/webgl-shader-precision-format.js +30 -0
  32. package/lib/cjs/webgl-shader.js +42 -0
  33. package/lib/cjs/webgl-texture-unit.js +31 -0
  34. package/lib/cjs/webgl-texture.js +41 -0
  35. package/lib/cjs/webgl-uniform-location.js +34 -0
  36. package/lib/cjs/webgl-vertex-attribute.js +166 -0
  37. package/lib/esm/@types/glsl-tokenizer/index.d.js +0 -0
  38. package/lib/esm/extensions/ext-blend-minmax.js +18 -0
  39. package/lib/esm/extensions/ext-texture-filter-anisotropic.js +18 -0
  40. package/lib/esm/extensions/oes-element-index-unit.js +14 -0
  41. package/lib/esm/extensions/oes-standard-derivatives.js +17 -0
  42. package/lib/esm/extensions/oes-texture-float-linear.js +14 -0
  43. package/lib/esm/extensions/oes-texture-float.js +14 -0
  44. package/lib/esm/extensions/stackgl-destroy-context.js +12 -0
  45. package/lib/esm/extensions/stackgl-resize-drawing-buffer.js +12 -0
  46. package/lib/esm/html-canvas-element.js +70 -0
  47. package/lib/esm/index.js +18 -0
  48. package/lib/esm/index.spec.js +146 -0
  49. package/lib/esm/linkable.js +50 -0
  50. package/lib/esm/test.js +3 -0
  51. package/lib/esm/types/constructor.js +0 -0
  52. package/lib/esm/types/extension.js +0 -0
  53. package/lib/esm/types/index.js +5 -0
  54. package/lib/esm/types/typed-array.js +0 -0
  55. package/lib/esm/types/webgl-constants.js +0 -0
  56. package/lib/esm/types/webgl-context-attribute-options.js +0 -0
  57. package/lib/esm/utils.js +212 -0
  58. package/lib/esm/webgl-active-info.js +10 -0
  59. package/lib/esm/webgl-buffer.js +18 -0
  60. package/lib/esm/webgl-context-attributes.js +24 -0
  61. package/lib/esm/webgl-drawing-buffer-wrapper.js +10 -0
  62. package/lib/esm/webgl-framebuffer.js +109 -0
  63. package/lib/esm/webgl-program.js +26 -0
  64. package/lib/esm/webgl-renderbuffer.js +24 -0
  65. package/lib/esm/webgl-rendering-context.js +3343 -0
  66. package/lib/esm/webgl-shader-precision-format.js +11 -0
  67. package/lib/esm/webgl-shader.js +23 -0
  68. package/lib/esm/webgl-texture-unit.js +12 -0
  69. package/lib/esm/webgl-texture.js +22 -0
  70. package/lib/esm/webgl-uniform-location.js +15 -0
  71. package/lib/esm/webgl-vertex-attribute.js +147 -0
  72. package/lib/types/extensions/ext-blend-minmax.d.ts +7 -0
  73. package/lib/types/extensions/ext-texture-filter-anisotropic.d.ts +7 -0
  74. package/lib/types/extensions/oes-element-index-unit.d.ts +4 -0
  75. package/lib/types/extensions/oes-standard-derivatives.d.ts +6 -0
  76. package/lib/types/extensions/oes-texture-float-linear.d.ts +4 -0
  77. package/lib/types/extensions/oes-texture-float.d.ts +4 -0
  78. package/lib/types/extensions/stackgl-destroy-context.d.ts +6 -0
  79. package/lib/types/extensions/stackgl-resize-drawing-buffer.d.ts +6 -0
  80. package/lib/types/html-canvas-element.d.ts +38 -0
  81. package/lib/types/index.d.ts +18 -0
  82. package/lib/types/linkable.d.ts +13 -0
  83. package/lib/types/types/constructor.d.ts +3 -0
  84. package/lib/types/types/extension.d.ts +2 -0
  85. package/lib/types/types/index.d.ts +5 -0
  86. package/lib/types/types/typed-array.d.ts +1 -0
  87. package/lib/types/types/webgl-constants.d.ts +299 -0
  88. package/lib/types/types/webgl-context-attribute-options.d.ts +12 -0
  89. package/lib/types/utils.d.ts +32 -0
  90. package/lib/types/webgl-active-info.d.ts +7 -0
  91. package/lib/types/webgl-buffer.d.ts +10 -0
  92. package/lib/types/webgl-context-attributes.d.ts +14 -0
  93. package/lib/types/webgl-drawing-buffer-wrapper.d.ts +6 -0
  94. package/lib/types/webgl-framebuffer.d.ts +17 -0
  95. package/lib/types/webgl-program.d.ts +14 -0
  96. package/lib/types/webgl-renderbuffer.d.ts +12 -0
  97. package/lib/types/webgl-rendering-context.d.ts +260 -0
  98. package/lib/types/webgl-shader-precision-format.d.ts +7 -0
  99. package/lib/types/webgl-shader.d.ts +12 -0
  100. package/lib/types/webgl-texture-unit.d.ts +10 -0
  101. package/lib/types/webgl-texture.d.ts +14 -0
  102. package/lib/types/webgl-uniform-location.d.ts +18 -0
  103. package/lib/types/webgl-vertex-attribute.d.ts +38 -0
  104. package/meson.build +39 -0
  105. package/package.json +55 -0
  106. package/src/test/app.vala +60 -0
  107. package/src/ts/@types/glsl-tokenizer/index.d.ts +18 -0
  108. package/src/ts/extensions/angle-instanced-arrays.ts.off +232 -0
  109. package/src/ts/extensions/ext-blend-minmax.ts +18 -0
  110. package/src/ts/extensions/ext-texture-filter-anisotropic.ts +18 -0
  111. package/src/ts/extensions/oes-element-index-unit.ts +14 -0
  112. package/src/ts/extensions/oes-standard-derivatives.ts +17 -0
  113. package/src/ts/extensions/oes-texture-float-linear.ts +14 -0
  114. package/src/ts/extensions/oes-texture-float.ts +14 -0
  115. package/src/ts/extensions/oes-vertex-array-object.ts.off +128 -0
  116. package/src/ts/extensions/stackgl-destroy-context.ts +12 -0
  117. package/src/ts/extensions/stackgl-resize-drawing-buffer.ts +14 -0
  118. package/src/ts/extensions/webgl-draw-buffers.ts.off +107 -0
  119. package/src/ts/html-canvas-element.ts +98 -0
  120. package/src/ts/index.spec.ts +186 -0
  121. package/src/ts/index.ts +21 -0
  122. package/src/ts/linkable.ts +55 -0
  123. package/src/ts/test.ts +6 -0
  124. package/src/ts/types/constructor.ts +3 -0
  125. package/src/ts/types/extension.ts +3 -0
  126. package/src/ts/types/index.ts +26 -0
  127. package/src/ts/types/typed-array.ts +1 -0
  128. package/src/ts/types/webgl-constants.ts +300 -0
  129. package/src/ts/types/webgl-context-attribute-options.ts +12 -0
  130. package/src/ts/utils.ts +266 -0
  131. package/src/ts/webgl-active-info.ts +13 -0
  132. package/src/ts/webgl-buffer.ts +21 -0
  133. package/src/ts/webgl-context-attributes.ts +24 -0
  134. package/src/ts/webgl-drawing-buffer-wrapper.ts +10 -0
  135. package/src/ts/webgl-framebuffer.ts +133 -0
  136. package/src/ts/webgl-program.ts +30 -0
  137. package/src/ts/webgl-renderbuffer.ts +28 -0
  138. package/src/ts/webgl-rendering-context.ts +4050 -0
  139. package/src/ts/webgl-shader-precision-format.ts +12 -0
  140. package/src/ts/webgl-shader.ts +29 -0
  141. package/src/ts/webgl-texture-unit.ts +16 -0
  142. package/src/ts/webgl-texture.ts +27 -0
  143. package/src/ts/webgl-uniform-location.ts +18 -0
  144. package/src/ts/webgl-vertex-attribute.ts +169 -0
  145. package/src/vala/handle-types.vala +23 -0
  146. package/src/vala/webgl-rendering-context-base.vala +1265 -0
  147. package/src/vala/webgl-rendering-context.vala +265 -0
  148. package/src/vapi/epoxy.vapi +14558 -0
  149. package/src/vapi/glesv2.vapi +670 -0
  150. package/test.gjs.js +39972 -0
  151. package/test.gjs.js.meta.json +1 -0
  152. package/tmp/.tsbuildinfo +1 -0
  153. package/tsconfig.json +38 -0
  154. package/tsconfig.types.json +7 -0
@@ -0,0 +1,232 @@
1
+ import { gl } from '../native-gl';
2
+ import { vertexCount } from '../utils';
3
+
4
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
5
+
6
+ export class ANGLEInstancedArrays {
7
+
8
+ VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE = 0x88fe
9
+ ctx: GjsifyWebGLRenderingContext;
10
+
11
+ _drawArraysInstanced = gl._drawArraysInstanced.bind(ctx)
12
+ _drawElementsInstanced = gl._drawElementsInstanced.bind(ctx)
13
+ _vertexAttribDivisor = gl._vertexAttribDivisor.bind(ctx)
14
+
15
+ constructor (ctx: GjsifyWebGLRenderingContext) {
16
+ this.VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE = 0x88fe
17
+ this.ctx = ctx
18
+
19
+ this._drawArraysInstanced = gl._drawArraysInstanced.bind(ctx)
20
+ this._drawElementsInstanced = gl._drawElementsInstanced.bind(ctx)
21
+ this._vertexAttribDivisor = gl._vertexAttribDivisor.bind(ctx)
22
+ }
23
+
24
+ drawArraysInstancedANGLE (mode: GLenum, first: GLint, count: GLuint, primCount: GLuint) {
25
+ const { ctx } = this
26
+ mode |= 0
27
+ first |= 0
28
+ count |= 0
29
+ primCount |= 0
30
+ if (first < 0 || count < 0 || primCount < 0) {
31
+ ctx.setError(gl.INVALID_VALUE)
32
+ return
33
+ }
34
+ if (!ctx._checkStencilState()) {
35
+ return
36
+ }
37
+ const reducedCount = vertexCount(mode, count)
38
+ if (reducedCount < 0) {
39
+ ctx.setError(gl.INVALID_ENUM)
40
+ return
41
+ }
42
+ if (!ctx._framebufferOk()) {
43
+ return
44
+ }
45
+ if (count === 0 || primCount === 0) {
46
+ return
47
+ }
48
+ let maxIndex = first
49
+ if (count > 0) {
50
+ maxIndex = (count + first - 1) >>> 0
51
+ }
52
+ if (this.checkInstancedVertexAttribState(maxIndex, primCount)) {
53
+ return this._drawArraysInstanced(mode, first, reducedCount, primCount)
54
+ }
55
+ }
56
+
57
+ drawElementsInstancedANGLE (mode: GLenum, count: GLint, type: GLenum, ioffset: GLint, primCount: GLuint) {
58
+ const { ctx } = this
59
+ mode |= 0
60
+ count |= 0
61
+ type |= 0
62
+ ioffset |= 0
63
+ primCount |= 0
64
+
65
+ if (count < 0 || ioffset < 0 || primCount < 0) {
66
+ ctx.setError(gl.INVALID_VALUE)
67
+ return
68
+ }
69
+
70
+ if (!ctx._checkStencilState()) {
71
+ return
72
+ }
73
+
74
+ const elementBuffer = ctx._vertexObjectState._elementArrayBufferBinding
75
+ if (!elementBuffer) {
76
+ ctx.setError(gl.INVALID_OPERATION)
77
+ return
78
+ }
79
+
80
+ // Unpack element data
81
+ let elementData = null
82
+ let offset = ioffset
83
+ if (type === gl.UNSIGNED_SHORT) {
84
+ if (offset % 2) {
85
+ ctx.setError(gl.INVALID_OPERATION)
86
+ return
87
+ }
88
+ offset >>= 1
89
+ elementData = new Uint16Array(elementBuffer._elements.buffer)
90
+ } else if (ctx._extensions.oes_element_index_uint && type === gl.UNSIGNED_INT) {
91
+ if (offset % 4) {
92
+ ctx.setError(gl.INVALID_OPERATION)
93
+ return
94
+ }
95
+ offset >>= 2
96
+ elementData = new Uint32Array(elementBuffer._elements.buffer)
97
+ } else if (type === gl.UNSIGNED_BYTE) {
98
+ elementData = elementBuffer._elements
99
+ } else {
100
+ ctx.setError(gl.INVALID_ENUM)
101
+ return
102
+ }
103
+
104
+ let reducedCount = count
105
+ switch (mode) {
106
+ case gl.TRIANGLES:
107
+ if (count % 3) {
108
+ reducedCount -= (count % 3)
109
+ }
110
+ break
111
+ case gl.LINES:
112
+ if (count % 2) {
113
+ reducedCount -= (count % 2)
114
+ }
115
+ break
116
+ case gl.POINTS:
117
+ break
118
+ case gl.LINE_LOOP:
119
+ case gl.LINE_STRIP:
120
+ if (count < 2) {
121
+ ctx.setError(gl.INVALID_OPERATION)
122
+ return
123
+ }
124
+ break
125
+ case gl.TRIANGLE_FAN:
126
+ case gl.TRIANGLE_STRIP:
127
+ if (count < 3) {
128
+ ctx.setError(gl.INVALID_OPERATION)
129
+ return
130
+ }
131
+ break
132
+ default:
133
+ ctx.setError(gl.INVALID_ENUM)
134
+ return
135
+ }
136
+
137
+ if (!ctx._framebufferOk()) {
138
+ return
139
+ }
140
+
141
+ if (count === 0 || primCount === 0) {
142
+ this.checkInstancedVertexAttribState(0, 0)
143
+ return
144
+ }
145
+
146
+ if ((count + offset) >>> 0 > elementData.length) {
147
+ ctx.setError(gl.INVALID_OPERATION)
148
+ return
149
+ }
150
+
151
+ // Compute max index
152
+ let maxIndex = -1
153
+ for (let i = offset; i < offset + count; ++i) {
154
+ maxIndex = Math.max(maxIndex, elementData[i])
155
+ }
156
+
157
+ if (maxIndex < 0) {
158
+ this.checkInstancedVertexAttribState(0, 0)
159
+ return
160
+ }
161
+
162
+ if (this.checkInstancedVertexAttribState(maxIndex, primCount)) {
163
+ if (reducedCount > 0) {
164
+ this._drawElementsInstanced(mode, reducedCount, type, ioffset, primCount)
165
+ }
166
+ }
167
+ }
168
+
169
+ vertexAttribDivisorANGLE (index: GLuint, divisor: GLuint) {
170
+ const { ctx } = this
171
+ index |= 0
172
+ divisor |= 0
173
+ if (divisor < 0 ||
174
+ index < 0 || index >= ctx._vertexObjectState._attribs.length) {
175
+ ctx.setError(gl.INVALID_VALUE)
176
+ return
177
+ }
178
+ const attrib = ctx._vertexObjectState._attribs[index]
179
+ attrib._divisor = divisor
180
+ this._vertexAttribDivisor(index, divisor)
181
+ }
182
+
183
+ checkInstancedVertexAttribState (maxIndex: number, primCount: GLuint) {
184
+ const { ctx } = this
185
+ const program = ctx._activeProgram
186
+ if (!program) {
187
+ ctx.setError(gl.INVALID_OPERATION)
188
+ return false
189
+ }
190
+
191
+ const attribs = ctx._vertexObjectState._attribs
192
+ let hasZero = false
193
+ for (let i = 0; i < attribs.length; ++i) {
194
+ const attrib = attribs[i]
195
+ if (attrib._isPointer) {
196
+ const buffer = attrib._pointerBuffer
197
+ if (program._attributes.indexOf(i) >= 0) {
198
+ if (!buffer) {
199
+ ctx.setError(gl.INVALID_OPERATION)
200
+ return false
201
+ }
202
+ let maxByte = 0
203
+ if (attrib._divisor === 0) {
204
+ hasZero = true
205
+ maxByte = attrib._pointerStride * maxIndex +
206
+ attrib._pointerSize +
207
+ attrib._pointerOffset
208
+ } else {
209
+ maxByte = attrib._pointerStride * (Math.ceil(primCount / attrib._divisor) - 1) +
210
+ attrib._pointerSize +
211
+ attrib._pointerOffset
212
+ }
213
+ if (maxByte > buffer._size) {
214
+ ctx.setError(gl.INVALID_OPERATION)
215
+ return false
216
+ }
217
+ }
218
+ }
219
+ }
220
+
221
+ if (!hasZero) {
222
+ ctx.setError(gl.INVALID_OPERATION)
223
+ return false
224
+ }
225
+
226
+ return true
227
+ }
228
+ }
229
+
230
+ export function getANGLEInstancedArrays (ctx: GjsifyWebGLRenderingContext) {
231
+ return new ANGLEInstancedArrays(ctx)
232
+ }
@@ -0,0 +1,18 @@
1
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
2
+
3
+ export class EXTBlendMinMax {
4
+ MIN_EXT = 0x8007
5
+ MAX_EXT = 0x8008
6
+ constructor () {}
7
+ }
8
+
9
+ export function getEXTBlendMinMax (context: GjsifyWebGLRenderingContext) {
10
+ let result = null
11
+ const exts = context.getSupportedExtensions()
12
+
13
+ if (exts && exts.indexOf('EXT_blend_minmax') >= 0) {
14
+ result = new EXTBlendMinMax()
15
+ }
16
+
17
+ return result
18
+ }
@@ -0,0 +1,18 @@
1
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
2
+
3
+ export class EXTTextureFilterAnisotropic {
4
+ TEXTURE_MAX_ANISOTROPY_EXT = 0x84FE
5
+ MAX_TEXTURE_MAX_ANISOTROPY_EXT = 0x84FF
6
+ constructor () {}
7
+ }
8
+
9
+ export function getEXTTextureFilterAnisotropic (context: GjsifyWebGLRenderingContext) {
10
+ let result = null
11
+ const exts = context.getSupportedExtensions()
12
+
13
+ if (exts && exts.indexOf('EXT_texture_filter_anisotropic') >= 0) {
14
+ result = new EXTTextureFilterAnisotropic()
15
+ }
16
+
17
+ return result
18
+ }
@@ -0,0 +1,14 @@
1
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
2
+
3
+ export class OESElementIndexUint {}
4
+
5
+ export function getOESElementIndexUint (context: GjsifyWebGLRenderingContext) {
6
+ let result = null
7
+ const exts = context.getSupportedExtensions()
8
+
9
+ if (exts && exts.indexOf('OES_element_index_uint') >= 0) {
10
+ result = new OESElementIndexUint()
11
+ }
12
+
13
+ return result
14
+ }
@@ -0,0 +1,17 @@
1
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
2
+
3
+ export class OESStandardDerivatives {
4
+ FRAGMENT_SHADER_DERIVATIVE_HINT_OES = 0x8B8B
5
+ constructor () {}
6
+ }
7
+
8
+ export function getOESStandardDerivatives (context: GjsifyWebGLRenderingContext) {
9
+ let result = null
10
+ const exts = context.getSupportedExtensions()
11
+
12
+ if (exts && exts.indexOf('OES_standard_derivatives') >= 0) {
13
+ result = new OESStandardDerivatives()
14
+ }
15
+
16
+ return result
17
+ }
@@ -0,0 +1,14 @@
1
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
2
+
3
+ export class OESTextureFloatLinear {}
4
+
5
+ export function getOESTextureFloatLinear (context: GjsifyWebGLRenderingContext) {
6
+ let result = null
7
+ const exts = context.getSupportedExtensions()
8
+
9
+ if (exts && exts.indexOf('OES_texture_float_linear') >= 0) {
10
+ result = new OESTextureFloatLinear()
11
+ }
12
+
13
+ return result
14
+ }
@@ -0,0 +1,14 @@
1
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
2
+
3
+ export class OESTextureFloat {}
4
+
5
+ export function getOESTextureFloat (context: GjsifyWebGLRenderingContext) {
6
+ let result = null
7
+ const exts = context.getSupportedExtensions()
8
+
9
+ if (exts && exts.indexOf('OES_texture_float') >= 0) {
10
+ result = new OESTextureFloat()
11
+ }
12
+
13
+ return result
14
+ }
@@ -0,0 +1,128 @@
1
+ import { Linkable } from '../linkable.js'
2
+ import { gl } from '../native-gl.js'
3
+ import { checkObject } from '../utils.js'
4
+ import { WebGLVertexArrayObjectState } from '../webgl-vertex-attribute.js';
5
+
6
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
7
+
8
+ export class WebGLVertexArrayObjectOES extends Linkable {
9
+ _ctx: GjsifyWebGLRenderingContext;
10
+ _ext: OESVertexArrayObject;
11
+ _vertexState?: WebGLVertexArrayObjectState;
12
+ constructor (_: number, ctx: GjsifyWebGLRenderingContext, ext: OESVertexArrayObject) {
13
+ super(_)
14
+ this._ctx = ctx
15
+ this._ext = ext
16
+ this._vertexState = new WebGLVertexArrayObjectState(ctx)
17
+ }
18
+
19
+ _performDelete () {
20
+ // Clean up the vertex state to release references to buffers.
21
+ this._vertexState?.cleanUp()
22
+
23
+ delete this._vertexState;
24
+ delete this._ext._vaos[this._]
25
+ gl.deleteVertexArrayOES.call(this._ctx, this._ | 0)
26
+ }
27
+ }
28
+
29
+ export class OESVertexArrayObject {
30
+ VERTEX_ARRAY_BINDING_OES = 0x85B5;
31
+ _ctx: GjsifyWebGLRenderingContext;
32
+ _vaos: Record<number, WebGLVertexArrayObjectOES> = {}
33
+ _activeVertexArrayObject: WebGLVertexArrayObjectOES | null = null
34
+
35
+ constructor (ctx: GjsifyWebGLRenderingContext) {
36
+ this.VERTEX_ARRAY_BINDING_OES = 0x85B5
37
+
38
+ this._ctx = ctx
39
+ }
40
+
41
+ createVertexArrayOES () {
42
+ const { _ctx: ctx } = this
43
+ const arrayId = gl.createVertexArrayOES.call(ctx)
44
+ if (arrayId <= 0) return null
45
+ const array = new WebGLVertexArrayObjectOES(arrayId, ctx, this)
46
+ this._vaos[arrayId] = array
47
+ return array
48
+ }
49
+
50
+ deleteVertexArrayOES (array: WebGLVertexArrayObjectOES | null) {
51
+ const { _ctx: ctx } = this
52
+ if (!checkObject(array)) {
53
+ throw new TypeError('deleteVertexArrayOES(WebGLVertexArrayObjectOES)')
54
+ }
55
+
56
+ if (!(array instanceof WebGLVertexArrayObjectOES &&
57
+ ctx._checkOwns(array))) {
58
+ ctx.setError(gl.INVALID_OPERATION)
59
+ return
60
+ }
61
+
62
+ if (array._pendingDelete) {
63
+ return
64
+ }
65
+
66
+ if (this._activeVertexArrayObject === array) {
67
+ this.bindVertexArrayOES(null)
68
+ }
69
+
70
+ array._pendingDelete = true
71
+ array._checkDelete()
72
+ }
73
+
74
+ bindVertexArrayOES (array: WebGLVertexArrayObjectOES | null) {
75
+ const { _ctx: ctx, _activeVertexArrayObject: activeVertexArrayObject } = this
76
+ if (!checkObject(array)) {
77
+ throw new TypeError('bindVertexArrayOES(WebGLVertexArrayObjectOES)')
78
+ }
79
+
80
+ if (!array) {
81
+ array = null
82
+ gl.bindVertexArrayOES.call(ctx, null)
83
+ } else if (array instanceof WebGLVertexArrayObjectOES &&
84
+ array._pendingDelete) {
85
+ ctx.setError(gl.INVALID_OPERATION)
86
+ return
87
+ } else if (ctx._checkWrapper(array, WebGLVertexArrayObjectOES)) {
88
+ gl.bindVertexArrayOES.call(ctx, array._)
89
+ } else {
90
+ return
91
+ }
92
+
93
+ if (activeVertexArrayObject !== array) {
94
+ if (activeVertexArrayObject) {
95
+ activeVertexArrayObject._refCount -= 1
96
+ activeVertexArrayObject._checkDelete()
97
+ }
98
+ if (array) {
99
+ array._refCount += 1
100
+ }
101
+ }
102
+
103
+ if (array === null) {
104
+ ctx._vertexObjectState = ctx._defaultVertexObjectState
105
+ } else if(array._vertexState) {
106
+ ctx._vertexObjectState = array._vertexState
107
+ }
108
+
109
+ // Update the active vertex array object.
110
+ this._activeVertexArrayObject = array
111
+ }
112
+
113
+ isVertexArrayOES (object: WebGLVertexArrayObjectOES) {
114
+ const { _ctx: ctx } = this
115
+ if (!ctx._isObject(object, 'isVertexArrayOES', WebGLVertexArrayObjectOES)) return false
116
+ return gl.isVertexArrayOES.call(ctx, object._ | 0)
117
+ }
118
+ }
119
+
120
+ export function getOESVertexArrayObject (ctx: GjsifyWebGLRenderingContext) {
121
+ const exts = ctx.getSupportedExtensions()
122
+
123
+ if (exts && exts.indexOf('OES_vertex_array_object') >= 0) {
124
+ return new OESVertexArrayObject(ctx)
125
+ } else {
126
+ return null
127
+ }
128
+ }
@@ -0,0 +1,12 @@
1
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
2
+
3
+ export class STACKGLDestroyContext {
4
+ destroy: () => void;
5
+ constructor (ctx: GjsifyWebGLRenderingContext) {
6
+ this.destroy = ctx.destroy.bind(ctx)
7
+ }
8
+ }
9
+
10
+ export function getSTACKGLDestroyContext (ctx: GjsifyWebGLRenderingContext) {
11
+ return new STACKGLDestroyContext(ctx)
12
+ }
@@ -0,0 +1,14 @@
1
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
2
+
3
+ export class STACKGLResizeDrawingBuffer {
4
+
5
+ resize: (width: number, height: number) => void;
6
+
7
+ constructor (ctx: GjsifyWebGLRenderingContext) {
8
+ this.resize = ctx.resize.bind(ctx)
9
+ }
10
+ }
11
+
12
+ export function getSTACKGLResizeDrawingBuffer (ctx: GjsifyWebGLRenderingContext) {
13
+ return new STACKGLResizeDrawingBuffer(ctx)
14
+ }
@@ -0,0 +1,107 @@
1
+ import { gl } from '../native-gl.js';
2
+ import type { GjsifyWebGLRenderingContext } from '../webgl-rendering-context.js';
3
+
4
+ export class WebGLDrawBuffers {
5
+ ctx: GjsifyWebGLRenderingContext;
6
+ _buffersState: number[] = [];
7
+ _maxDrawBuffers = 0;
8
+ _ALL_ATTACHMENTS: GLenum[] = []
9
+ _ALL_COLOR_ATTACHMENTS: GLenum[] = []
10
+
11
+ MAX_DRAW_BUFFERS_WEBGL = 0;
12
+ COLOR_ATTACHMENT0_WEBGL = 0;
13
+ COLOR_ATTACHMENT1_WEBGL = 0;
14
+ COLOR_ATTACHMENT2_WEBGL = 0;
15
+ COLOR_ATTACHMENT3_WEBGL = 0;
16
+ COLOR_ATTACHMENT4_WEBGL = 0;
17
+ COLOR_ATTACHMENT5_WEBGL = 0;
18
+ COLOR_ATTACHMENT6_WEBGL = 0;
19
+ COLOR_ATTACHMENT7_WEBGL = 0;
20
+ COLOR_ATTACHMENT8_WEBGL = 0;
21
+ COLOR_ATTACHMENT9_WEBGL = 0;
22
+ COLOR_ATTACHMENT10_WEBGL = 0;
23
+ COLOR_ATTACHMENT11_WEBGL = 0;
24
+ COLOR_ATTACHMENT12_WEBGL = 0;
25
+ COLOR_ATTACHMENT13_WEBGL = 0;
26
+ COLOR_ATTACHMENT14_WEBGL = 0;
27
+ COLOR_ATTACHMENT15_WEBGL = 0;
28
+
29
+ constructor (ctx: GjsifyWebGLRenderingContext) {
30
+ this.ctx = ctx
31
+ const exts = ctx.getSupportedExtensions()
32
+
33
+ if (exts && exts.indexOf('WEBGL_draw_buffers') >= 0) {
34
+ Object.assign(this, ctx.extWEBGL_draw_buffers())
35
+ this._buffersState = [ctx.BACK]
36
+ this._maxDrawBuffers = ctx._getParameterDirect(this.MAX_DRAW_BUFFERS_WEBGL) as number;
37
+ this._ALL_ATTACHMENTS = []
38
+ this._ALL_COLOR_ATTACHMENTS = []
39
+ const allColorAttachments = [
40
+ this.COLOR_ATTACHMENT0_WEBGL,
41
+ this.COLOR_ATTACHMENT1_WEBGL,
42
+ this.COLOR_ATTACHMENT2_WEBGL,
43
+ this.COLOR_ATTACHMENT3_WEBGL,
44
+ this.COLOR_ATTACHMENT4_WEBGL,
45
+ this.COLOR_ATTACHMENT5_WEBGL,
46
+ this.COLOR_ATTACHMENT6_WEBGL,
47
+ this.COLOR_ATTACHMENT7_WEBGL,
48
+ this.COLOR_ATTACHMENT8_WEBGL,
49
+ this.COLOR_ATTACHMENT9_WEBGL,
50
+ this.COLOR_ATTACHMENT10_WEBGL,
51
+ this.COLOR_ATTACHMENT11_WEBGL,
52
+ this.COLOR_ATTACHMENT12_WEBGL,
53
+ this.COLOR_ATTACHMENT13_WEBGL,
54
+ this.COLOR_ATTACHMENT14_WEBGL,
55
+ this.COLOR_ATTACHMENT15_WEBGL
56
+ ]
57
+ while (this._ALL_ATTACHMENTS.length < this._maxDrawBuffers) {
58
+ const colorAttachment = allColorAttachments.shift()
59
+ if(colorAttachment) {
60
+ this._ALL_ATTACHMENTS.push(colorAttachment)
61
+ this._ALL_COLOR_ATTACHMENTS.push(colorAttachment)
62
+ }
63
+ }
64
+ this._ALL_ATTACHMENTS.push(
65
+ gl.DEPTH_ATTACHMENT,
66
+ gl.STENCIL_ATTACHMENT,
67
+ gl.DEPTH_STENCIL_ATTACHMENT
68
+ )
69
+ }
70
+ }
71
+
72
+ drawBuffersWEBGL (buffers: GLenum[]) {
73
+ const { ctx } = this
74
+ if (buffers.length < 1) {
75
+ ctx.setError(gl.INVALID_OPERATION)
76
+ return
77
+ }
78
+ if (buffers.length === 1 && buffers[0] === gl.BACK) {
79
+ this._buffersState = buffers
80
+ ctx.drawBuffersWEBGL([this.COLOR_ATTACHMENT0_WEBGL])
81
+ return
82
+ } else if (!ctx._activeFramebuffer) {
83
+ if (buffers.length > 1) {
84
+ ctx.setError(gl.INVALID_OPERATION)
85
+ return
86
+ }
87
+ for (let i = 0; i < buffers.length; i++) {
88
+ if (buffers[i] > gl.NONE) {
89
+ ctx.setError(gl.INVALID_OPERATION)
90
+ return
91
+ }
92
+ }
93
+ }
94
+ this._buffersState = buffers
95
+ ctx.drawBuffersWEBGL(buffers)
96
+ }
97
+ }
98
+
99
+ export function getWebGLDrawBuffers (ctx: GjsifyWebGLRenderingContext) {
100
+ const exts = ctx.getSupportedExtensions()
101
+
102
+ if (exts && exts.indexOf('WEBGL_draw_buffers') >= 0) {
103
+ return new WebGLDrawBuffers(ctx)
104
+ } else {
105
+ return null
106
+ }
107
+ }
@@ -0,0 +1,98 @@
1
+ import { warnNotImplemented, notImplemented } from '@gjsify/utils'
2
+ import Gtk from 'gi://Gtk?version=4.0';
3
+ import { GjsifyWebGLRenderingContext } from './webgl-rendering-context.js';
4
+
5
+ // TODO this fakes the implementation of HTMLCanvasElement, create a new package for a real implementation based on https://github.com/capricorn86/happy-dom/tree/master/packages/happy-dom/src/nodes/html-element
6
+ export interface GjsifyHTMLCanvasElement extends HTMLCanvasElement {}
7
+
8
+ export class GjsifyHTMLCanvasElement implements HTMLCanvasElement {
9
+
10
+ _webgl?: WebGLRenderingContext & GjsifyWebGLRenderingContext
11
+
12
+ _getGlArea() {
13
+ return this.gtkGlArea;
14
+ }
15
+
16
+ constructor(protected readonly gtkGlArea: Gtk.GLArea) {
17
+
18
+ }
19
+
20
+ /** Gets the height of a canvas element on a document. */
21
+ get height() {
22
+ return this.gtkGlArea.get_allocated_height()
23
+ // return this.gtkGlArea.get_height()
24
+ }
25
+
26
+ get clientHeight() {
27
+ return this.height;
28
+ }
29
+
30
+ /** Sets the height of a canvas element on a document. */
31
+ set height(_height: number) {
32
+ warnNotImplemented('GjsifyHTMLCanvasElement.set_height');
33
+ }
34
+
35
+ /** Gets the width of a canvas element on a document. */
36
+ get width() {
37
+ return this.gtkGlArea.get_allocated_width()
38
+ // return this.gtkGlArea.get_width()
39
+ }
40
+
41
+ /** Sets the width of a canvas element on a document. */
42
+ set width(_width: number) {
43
+ warnNotImplemented('GjsifyHTMLCanvasElement.set_width');
44
+ }
45
+
46
+ get clientWidth() {
47
+ return this.width;
48
+ }
49
+
50
+ captureStream(_frameRequestRate?: number): MediaStream {
51
+ notImplemented('HTMLCanvasElement.captureStream');
52
+ return new MediaStream();
53
+ }
54
+
55
+ /**
56
+ * Returns an object that provides methods and properties for drawing and manipulating images and graphics on a canvas element in a document. A context object includes information about colors, line widths, fonts, and other graphic parameters that can be drawn on a canvas.
57
+ * @param contextId The identifier (ID) of the type of canvas to create. Internet Explorer 9 and Internet Explorer 10 support only a 2-D context using canvas.getContext("2d"); IE11 Preview also supports 3-D or WebGL context using canvas.getContext("experimental-webgl");
58
+ */
59
+ getContext(contextId: "2d", options?: CanvasRenderingContext2DSettings): CanvasRenderingContext2D | null;
60
+ getContext(contextId: "bitmaprenderer", options?: ImageBitmapRenderingContextSettings): ImageBitmapRenderingContext | null;
61
+ getContext(contextId: "webgl", options?: WebGLContextAttributes): WebGLRenderingContext & GjsifyWebGLRenderingContext | null;
62
+ getContext(contextId: "webgl2", options?: WebGLContextAttributes): WebGL2RenderingContext | null;
63
+ getContext(contextId: string, options?: any): RenderingContext | null {
64
+ switch (contextId) {
65
+ case "webgl":
66
+ if(this._webgl) {
67
+ return this._webgl;
68
+ }
69
+ this._webgl = new GjsifyWebGLRenderingContext(this, options) as WebGLRenderingContext & GjsifyWebGLRenderingContext;
70
+ return this._webgl;
71
+ default:
72
+ warnNotImplemented(`GjsifyHTMLCanvasElement.getContext("${contextId}")`);
73
+ }
74
+ return null;
75
+ }
76
+
77
+ toBlob(_callback: BlobCallback, _type?: string, _quality?: any): void {
78
+ notImplemented('HTMLCanvasElement.toBlob');
79
+ }
80
+ /**
81
+ * Returns the content of the current canvas as an image that you can use as a source for another canvas or an HTML element.
82
+ * @param _type The standard MIME type for the image format to return. If you do not specify this parameter, the default value is a PNG format image.
83
+ */
84
+ toDataURL(_type?: string, _quality?: any): string {
85
+ notImplemented('HTMLCanvasElement.toDataURL');
86
+ return '';
87
+ }
88
+ addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLCanvasElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
89
+ addEventListener(_type: string, _listener: EventListenerOrEventListenerObject, _options?: boolean | AddEventListenerOptions): void {
90
+ notImplemented('HTMLCanvasElement.addEventListener');
91
+ }
92
+ removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLCanvasElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
93
+ removeEventListener(_type: string, _listener: EventListenerOrEventListenerObject, _options?: boolean | EventListenerOptions): void {
94
+ notImplemented('HTMLCanvasElement.removeEventListener');
95
+ }
96
+ }
97
+
98
+ export { GjsifyHTMLCanvasElement as HTMLCanvasElement }