@lightningjs/renderer 2.9.0-beta2 → 2.9.0-beta3

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 (189) hide show
  1. package/COPYING +1 -0
  2. package/LICENSE +202 -202
  3. package/NOTICE +3 -3
  4. package/README.md +147 -147
  5. package/dist/src/core/CoreNode.d.ts +2 -1
  6. package/dist/src/core/CoreNode.js +54 -43
  7. package/dist/src/core/CoreNode.js.map +1 -1
  8. package/dist/src/core/CoreTextureManager.d.ts +61 -15
  9. package/dist/src/core/CoreTextureManager.js +179 -104
  10. package/dist/src/core/CoreTextureManager.js.map +1 -1
  11. package/dist/src/core/Stage.d.ts +7 -0
  12. package/dist/src/core/Stage.js +33 -1
  13. package/dist/src/core/Stage.js.map +1 -1
  14. package/dist/src/core/TextureMemoryManager.js +2 -5
  15. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  16. package/dist/src/core/lib/ImageWorker.js +31 -28
  17. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  18. package/dist/src/core/lib/WebGlContextWrapper.d.ts +1 -0
  19. package/dist/src/core/lib/WebGlContextWrapper.js +2 -0
  20. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  21. package/dist/src/core/renderers/CoreContextTexture.d.ts +1 -0
  22. package/dist/src/core/renderers/CoreContextTexture.js +1 -0
  23. package/dist/src/core/renderers/CoreContextTexture.js.map +1 -1
  24. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +1 -1
  25. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +13 -5
  26. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -1
  27. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +6 -8
  28. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -1
  29. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +3 -1
  30. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -1
  31. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +1 -2
  32. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +26 -24
  33. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  34. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +0 -1
  35. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +7 -17
  36. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
  37. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  38. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  39. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  40. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  41. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  42. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  43. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  44. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  45. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  46. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  47. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  48. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  49. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  50. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  51. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  52. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  53. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  54. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  55. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +3 -5
  56. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  57. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +2 -1
  58. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  59. package/dist/src/core/textures/ColorTexture.d.ts +1 -1
  60. package/dist/src/core/textures/ColorTexture.js +2 -1
  61. package/dist/src/core/textures/ColorTexture.js.map +1 -1
  62. package/dist/src/core/textures/ImageTexture.d.ts +8 -1
  63. package/dist/src/core/textures/ImageTexture.js +39 -1
  64. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  65. package/dist/src/core/textures/NoiseTexture.d.ts +1 -1
  66. package/dist/src/core/textures/NoiseTexture.js +2 -1
  67. package/dist/src/core/textures/NoiseTexture.js.map +1 -1
  68. package/dist/src/core/textures/RenderTexture.d.ts +1 -1
  69. package/dist/src/core/textures/RenderTexture.js +2 -1
  70. package/dist/src/core/textures/RenderTexture.js.map +1 -1
  71. package/dist/src/core/textures/SubTexture.d.ts +1 -1
  72. package/dist/src/core/textures/SubTexture.js +21 -4
  73. package/dist/src/core/textures/SubTexture.js.map +1 -1
  74. package/dist/src/core/textures/Texture.d.ts +43 -21
  75. package/dist/src/core/textures/Texture.js +105 -33
  76. package/dist/src/core/textures/Texture.js.map +1 -1
  77. package/dist/src/main-api/Renderer.d.ts +18 -0
  78. package/dist/src/main-api/Renderer.js +6 -4
  79. package/dist/src/main-api/Renderer.js.map +1 -1
  80. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  81. package/exports/canvas.ts +39 -39
  82. package/exports/index.ts +89 -89
  83. package/exports/inspector.ts +24 -24
  84. package/exports/utils.ts +44 -44
  85. package/exports/webgl.ts +38 -38
  86. package/package.json +1 -1
  87. package/scripts/please-use-pnpm.js +13 -13
  88. package/src/common/CommonTypes.ts +139 -139
  89. package/src/common/EventEmitter.ts +77 -77
  90. package/src/common/IAnimationController.ts +92 -92
  91. package/src/common/IEventEmitter.ts +28 -28
  92. package/src/core/CoreNode.test.ts +95 -95
  93. package/src/core/CoreNode.ts +2282 -2258
  94. package/src/core/CoreShaderManager.ts +292 -292
  95. package/src/core/CoreTextNode.ts +450 -450
  96. package/src/core/CoreTextureManager.ts +522 -432
  97. package/src/core/Stage.ts +699 -652
  98. package/src/core/TextureMemoryManager.ts +277 -279
  99. package/src/core/animations/AnimationManager.ts +38 -38
  100. package/src/core/animations/CoreAnimation.ts +340 -340
  101. package/src/core/animations/CoreAnimationController.ts +157 -157
  102. package/src/core/lib/ContextSpy.ts +41 -41
  103. package/src/core/lib/ImageWorker.ts +270 -267
  104. package/src/core/lib/Matrix3d.ts +244 -244
  105. package/src/core/lib/RenderCoords.ts +86 -86
  106. package/src/core/lib/WebGlContextWrapper.ts +1322 -1320
  107. package/src/core/lib/textureCompression.ts +152 -152
  108. package/src/core/lib/textureSvg.ts +78 -78
  109. package/src/core/lib/utils.ts +306 -306
  110. package/src/core/platform.ts +61 -61
  111. package/src/core/renderers/CoreContextTexture.ts +43 -42
  112. package/src/core/renderers/CoreRenderOp.ts +22 -22
  113. package/src/core/renderers/CoreRenderer.ts +114 -114
  114. package/src/core/renderers/CoreShader.ts +41 -41
  115. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +364 -353
  116. package/src/core/renderers/canvas/CanvasCoreTexture.ts +150 -149
  117. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  118. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  119. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  120. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -79
  121. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -48
  122. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +298 -290
  123. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  124. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +805 -817
  125. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  126. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  127. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  128. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  129. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  130. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  131. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  132. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  133. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  134. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  135. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  136. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  137. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  138. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  139. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  140. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  141. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  142. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  143. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  144. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  145. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  146. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  147. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  148. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  149. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  150. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  151. package/src/core/text-rendering/TrFontManager.ts +183 -183
  152. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +169 -171
  153. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  154. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  155. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  156. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  157. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +89 -89
  158. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -507
  159. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +798 -798
  160. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  161. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  162. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  163. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  164. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  165. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  166. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  167. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  168. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +403 -403
  169. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  170. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  171. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  172. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  173. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  174. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  175. package/src/core/textures/ColorTexture.ts +102 -100
  176. package/src/core/textures/ImageTexture.ts +378 -337
  177. package/src/core/textures/NoiseTexture.ts +104 -101
  178. package/src/core/textures/RenderTexture.ts +85 -83
  179. package/src/core/textures/SubTexture.ts +171 -146
  180. package/src/core/textures/Texture.ts +407 -318
  181. package/src/core/utils.ts +227 -227
  182. package/src/env.d.ts +7 -7
  183. package/src/main-api/DynamicShaderController.ts +104 -104
  184. package/src/main-api/INode.ts +101 -101
  185. package/src/main-api/Inspector.ts +505 -505
  186. package/src/main-api/Renderer.ts +693 -670
  187. package/src/main-api/ShaderController.ts +80 -80
  188. package/src/main-api/utils.ts +45 -45
  189. package/src/utils.ts +248 -248
@@ -1,1320 +1,1322 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-return */
2
- /* eslint-disable @typescript-eslint/no-explicit-any */
3
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
4
-
5
- import { assertTruthy } from '../../utils.js';
6
- import { isWebGl2 } from '../renderers/webgl/internal/WebGlUtils.js';
7
-
8
- /**
9
- * Optimized WebGL Context Wrapper
10
- *
11
- * @remarks
12
- * This class contains the subset of the WebGLRenderingContext & WebGL2RenderingContext
13
- * API that is used by the renderer. Select high volume WebGL methods include
14
- * caching optimizations to avoid making WebGL calls if the state is already set
15
- * to the desired value.
16
- *
17
- * While most methods contained are direct passthroughs to the WebGL context,
18
- * some methods combine multiple WebGL calls into one for convenience, modify
19
- * arguments to be more convenient, or are replaced by more specific methods.
20
- *
21
- * Not all methods are optimized. Only methods that are called frequently
22
- * and/or have a high cost are optimized.
23
- *
24
- * A subset of GLenum constants are also exposed as properties on this class
25
- * for convenience.
26
- */
27
- export class WebGlContextWrapper {
28
- //#region Cached WebGL State
29
- private activeTextureUnit = 0;
30
- private texture2dUnits: Array<WebGLTexture | null>;
31
- private texture2dParams: WeakMap<
32
- WebGLTexture,
33
- Record<number, number | undefined>
34
- > = new WeakMap();
35
- private scissorEnabled;
36
- private scissorX: number;
37
- private scissorY: number;
38
- private scissorWidth: number;
39
- private scissorHeight: number;
40
- private blendEnabled;
41
- private blendSrcRgb: number;
42
- private blendDstRgb: number;
43
- private blendSrcAlpha: number;
44
- private blendDstAlpha: number;
45
- private boundArrayBuffer: WebGLBuffer | null;
46
- private boundElementArrayBuffer: WebGLBuffer | null;
47
- private curProgram: WebGLProgram | null;
48
- //#endregion Cached WebGL State
49
-
50
- //#region Canvas
51
- public readonly canvas;
52
- //#endregion Canvas
53
-
54
- //#region WebGL Enums
55
- public readonly MAX_RENDERBUFFER_SIZE;
56
- public readonly MAX_TEXTURE_SIZE;
57
- public readonly MAX_VIEWPORT_DIMS;
58
- public readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS;
59
- public readonly MAX_TEXTURE_IMAGE_UNITS;
60
- public readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS;
61
- public readonly MAX_VERTEX_ATTRIBS;
62
- public readonly MAX_VARYING_VECTORS;
63
- public readonly MAX_VERTEX_UNIFORM_VECTORS;
64
- public readonly MAX_FRAGMENT_UNIFORM_VECTORS;
65
- public readonly TEXTURE_MAG_FILTER;
66
- public readonly TEXTURE_MIN_FILTER;
67
- public readonly TEXTURE_WRAP_S;
68
- public readonly TEXTURE_WRAP_T;
69
- public readonly LINEAR;
70
- public readonly CLAMP_TO_EDGE;
71
- public readonly RGBA;
72
- public readonly UNSIGNED_BYTE;
73
- public readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL;
74
- public readonly UNPACK_FLIP_Y_WEBGL;
75
- public readonly FLOAT;
76
- public readonly TRIANGLES;
77
- public readonly UNSIGNED_SHORT;
78
- public readonly ONE;
79
- public readonly ONE_MINUS_SRC_ALPHA;
80
- public readonly VERTEX_SHADER;
81
- public readonly FRAGMENT_SHADER;
82
- public readonly STATIC_DRAW;
83
- public readonly COMPILE_STATUS;
84
- public readonly LINK_STATUS;
85
- public readonly DYNAMIC_DRAW;
86
- public readonly COLOR_ATTACHMENT0;
87
- public readonly INVALID_ENUM: number;
88
- public readonly INVALID_OPERATION: number;
89
- //#endregion WebGL Enums
90
-
91
- constructor(private gl: WebGLRenderingContext | WebGL2RenderingContext) {
92
- // The following code extracts the current state of the WebGL context
93
- // to our local JavaScript cached version of it. This is so we can
94
- // avoid making WebGL calls if we don't need to.
95
- // We could assume that the WebGL context is in a default state, but
96
- // in the future we may want to support restoring a broken WebGL context
97
- // and this will help with that.
98
- this.activeTextureUnit =
99
- (gl.getParameter(gl.ACTIVE_TEXTURE) as number) - gl.TEXTURE0;
100
- const maxTextureUnits = gl.getParameter(
101
- gl.MAX_TEXTURE_IMAGE_UNITS,
102
- ) as number;
103
- // save current texture units
104
- this.texture2dUnits = new Array<undefined>(maxTextureUnits)
105
- .fill(undefined)
106
- .map((_, i) => {
107
- this.activeTexture(i);
108
- return gl.getParameter(gl.TEXTURE_BINDING_2D) as WebGLTexture;
109
- });
110
- // restore active texture unit
111
- this.activeTexture(this.activeTextureUnit);
112
- this.scissorEnabled = gl.isEnabled(gl.SCISSOR_TEST);
113
-
114
- const scissorBox = gl.getParameter(gl.SCISSOR_BOX) as [
115
- number,
116
- number,
117
- number,
118
- number,
119
- ];
120
- this.scissorX = scissorBox[0];
121
- this.scissorY = scissorBox[1];
122
- this.scissorWidth = scissorBox[2];
123
- this.scissorHeight = scissorBox[3];
124
-
125
- this.blendEnabled = gl.isEnabled(gl.BLEND);
126
- this.blendSrcRgb = gl.getParameter(gl.BLEND_SRC_RGB) as number;
127
- this.blendDstRgb = gl.getParameter(gl.BLEND_DST_RGB) as number;
128
- this.blendSrcAlpha = gl.getParameter(gl.BLEND_SRC_ALPHA) as number;
129
- this.blendDstAlpha = gl.getParameter(gl.BLEND_DST_ALPHA) as number;
130
-
131
- this.boundArrayBuffer = gl.getParameter(
132
- gl.ARRAY_BUFFER_BINDING,
133
- ) as WebGLBuffer;
134
- this.boundElementArrayBuffer = gl.getParameter(
135
- gl.ELEMENT_ARRAY_BUFFER_BINDING,
136
- ) as WebGLBuffer;
137
-
138
- this.curProgram = gl.getParameter(
139
- gl.CURRENT_PROGRAM,
140
- ) as WebGLProgram | null;
141
-
142
- this.canvas = gl.canvas;
143
-
144
- // Extract GLenums
145
- this.MAX_RENDERBUFFER_SIZE = gl.MAX_RENDERBUFFER_SIZE;
146
- this.MAX_TEXTURE_SIZE = gl.MAX_TEXTURE_SIZE;
147
- this.MAX_VIEWPORT_DIMS = gl.MAX_VIEWPORT_DIMS;
148
- this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
149
- this.MAX_TEXTURE_IMAGE_UNITS = gl.MAX_TEXTURE_IMAGE_UNITS;
150
- this.MAX_COMBINED_TEXTURE_IMAGE_UNITS = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS;
151
- this.MAX_VERTEX_ATTRIBS = gl.MAX_VERTEX_ATTRIBS;
152
- this.MAX_VARYING_VECTORS = gl.MAX_VARYING_VECTORS;
153
- this.MAX_VERTEX_UNIFORM_VECTORS = gl.MAX_VERTEX_UNIFORM_VECTORS;
154
- this.MAX_FRAGMENT_UNIFORM_VECTORS = gl.MAX_FRAGMENT_UNIFORM_VECTORS;
155
- this.TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER;
156
- this.TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER;
157
- this.TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S;
158
- this.TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T;
159
- this.LINEAR = gl.LINEAR;
160
- this.CLAMP_TO_EDGE = gl.CLAMP_TO_EDGE;
161
- this.RGBA = gl.RGBA;
162
- this.UNSIGNED_BYTE = gl.UNSIGNED_BYTE;
163
- this.UNPACK_PREMULTIPLY_ALPHA_WEBGL = gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL;
164
- this.UNPACK_FLIP_Y_WEBGL = gl.UNPACK_FLIP_Y_WEBGL;
165
- this.FLOAT = gl.FLOAT;
166
- this.TRIANGLES = gl.TRIANGLES;
167
- this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
168
- this.ONE = gl.ONE;
169
- this.ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
170
- this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
171
- this.TRIANGLES = gl.TRIANGLES;
172
- this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
173
- this.VERTEX_SHADER = gl.VERTEX_SHADER;
174
- this.FRAGMENT_SHADER = gl.FRAGMENT_SHADER;
175
- this.STATIC_DRAW = gl.STATIC_DRAW;
176
- this.COMPILE_STATUS = gl.COMPILE_STATUS;
177
- this.LINK_STATUS = gl.LINK_STATUS;
178
- this.DYNAMIC_DRAW = gl.DYNAMIC_DRAW;
179
- this.COLOR_ATTACHMENT0 = gl.COLOR_ATTACHMENT0;
180
- this.INVALID_ENUM = gl.INVALID_ENUM;
181
- this.INVALID_OPERATION = gl.INVALID_OPERATION;
182
- }
183
- /**
184
- * Returns true if the WebGL context is WebGL2
185
- *
186
- * @returns
187
- */
188
- isWebGl2() {
189
- return isWebGl2(this.gl);
190
- }
191
-
192
- /**
193
- * ```
194
- * gl.activeTexture(textureUnit + gl.TEXTURE0);
195
- * ```
196
- *
197
- * @remarks
198
- * **WebGL Difference**: `textureUnit` is based from 0, not `gl.TEXTURE0`.
199
- *
200
- * @param textureUnit
201
- */
202
- activeTexture(textureUnit: number) {
203
- const { gl } = this;
204
- if (this.activeTextureUnit !== textureUnit) {
205
- gl.activeTexture(textureUnit + gl.TEXTURE0);
206
- this.activeTextureUnit = textureUnit;
207
- }
208
- }
209
-
210
- /**
211
- * ```
212
- * gl.bindTexture(gl.TEXTURE_2D, texture);
213
- * ```
214
- * @remarks
215
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
216
- *
217
- * @param texture
218
- */
219
- bindTexture(texture: WebGLTexture | null) {
220
- const { gl, activeTextureUnit, texture2dUnits } = this;
221
-
222
- if (texture2dUnits[activeTextureUnit] === texture) {
223
- return;
224
- }
225
- texture2dUnits[activeTextureUnit] = texture;
226
-
227
- gl.bindTexture(this.gl.TEXTURE_2D, texture);
228
- }
229
-
230
- private _getActiveTexture(): WebGLTexture | null {
231
- const { activeTextureUnit, texture2dUnits } = this;
232
- return texture2dUnits[activeTextureUnit]!;
233
- }
234
-
235
- /**
236
- * ```
237
- * gl.texParameteri(gl.TEXTURE_2D, pname, param);
238
- * ```
239
- * @remarks
240
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
241
- *
242
- * @param pname
243
- * @param param
244
- * @returns
245
- */
246
- texParameteri(pname: number, param: number) {
247
- const { gl, texture2dParams } = this;
248
-
249
- const activeTexture = this._getActiveTexture();
250
- if (!activeTexture) {
251
- throw new Error('No active texture');
252
- }
253
- let textureParams = texture2dParams.get(activeTexture);
254
- if (!textureParams) {
255
- textureParams = {};
256
- texture2dParams.set(activeTexture, textureParams);
257
- }
258
- if (textureParams[pname] === param) {
259
- return;
260
- }
261
- textureParams[pname] = param;
262
- gl.texParameteri(gl.TEXTURE_2D, pname, param);
263
- }
264
-
265
- /**
266
- * ```
267
- * gl.texImage2D(
268
- * gl.TEXTURE_2D,
269
- * level,
270
- * internalFormat,
271
- * width,
272
- * height,
273
- * border,
274
- * format,
275
- * type,
276
- * pixels,
277
- * );
278
- * ```
279
- * @remarks
280
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
281
- *
282
- * @param level
283
- * @param internalFormat
284
- * @param width
285
- * @param height
286
- * @param border
287
- * @param format
288
- * @param type
289
- * @param pixels
290
- */
291
- texImage2D(
292
- level: GLint,
293
- internalformat: GLint,
294
- width: GLsizei,
295
- height: GLsizei,
296
- border: GLint,
297
- format: GLenum,
298
- type: GLenum,
299
- pixels: ArrayBufferView | null,
300
- ): void;
301
- texImage2D(
302
- level: GLint,
303
- internalformat: GLint,
304
- format: GLenum,
305
- type: GLenum,
306
- source: TexImageSource | Uint8Array,
307
- ): void;
308
- texImage2D(
309
- level: any,
310
- internalFormat: any,
311
- widthOrFormat: any,
312
- heightOrType: any,
313
- borderOrSource: any,
314
- format?: any,
315
- type?: any,
316
- pixels?: any,
317
- ) {
318
- const { gl } = this;
319
- if (format) {
320
- gl.texImage2D(
321
- gl.TEXTURE_2D,
322
- level,
323
- internalFormat,
324
- widthOrFormat,
325
- heightOrType,
326
- borderOrSource,
327
- format,
328
- type,
329
- pixels,
330
- );
331
- } else {
332
- gl.texImage2D(
333
- gl.TEXTURE_2D,
334
- level,
335
- internalFormat,
336
- widthOrFormat,
337
- heightOrType,
338
- borderOrSource,
339
- );
340
- }
341
- }
342
- /**
343
- * ```
344
- * gl.compressedTexImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, data);
345
- * ```
346
- *
347
- * @remarks
348
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
349
- */
350
-
351
- compressedTexImage2D(
352
- level: GLint,
353
- internalformat: GLenum,
354
- width: GLsizei,
355
- height: GLsizei,
356
- border: GLint,
357
- data?: ArrayBufferView,
358
- ): void {
359
- const { gl } = this;
360
- gl.compressedTexImage2D(
361
- gl.TEXTURE_2D,
362
- level,
363
- internalformat,
364
- width,
365
- height,
366
- border,
367
- data as ArrayBufferView,
368
- );
369
- }
370
- /**
371
- * ```
372
- * gl.pixelStorei(pname, param);
373
- * ```
374
- *
375
- * @param pname
376
- * @param param
377
- */
378
- pixelStorei(pname: GLenum, param: GLint | GLboolean) {
379
- const { gl } = this;
380
- gl.pixelStorei(pname, param);
381
- }
382
-
383
- /**
384
- * ```
385
- * gl.generateMipmap(gl.TEXTURE_2D);
386
- * ```
387
- *
388
- * @remarks
389
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
390
- */
391
- generateMipmap() {
392
- const { gl } = this;
393
- gl.generateMipmap(gl.TEXTURE_2D);
394
- }
395
-
396
- /**
397
- * ```
398
- * gl.createTexture();
399
- * ```
400
- *
401
- * @returns
402
- */
403
- createTexture() {
404
- const { gl } = this;
405
- return gl.createTexture();
406
- }
407
-
408
- /**
409
- * ```
410
- * gl.deleteTexture(texture);
411
- * ```
412
- *
413
- * @param texture
414
- */
415
- deleteTexture(texture: WebGLTexture | null) {
416
- const { gl } = this;
417
- if (texture) {
418
- this.texture2dParams.delete(texture);
419
- }
420
- gl.deleteTexture(texture);
421
- }
422
-
423
- /**
424
- * ```
425
- * gl.viewport(x, y, width, height);
426
- * ```
427
- */
428
- viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
429
- const { gl } = this;
430
- gl.viewport(x, y, width, height);
431
- }
432
-
433
- /**
434
- * ```
435
- * gl.clearColor(red, green, blue, alpha);
436
- * ```
437
- *
438
- * @param red
439
- * @param green
440
- * @param blue
441
- * @param alpha
442
- */
443
- clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
444
- const { gl } = this;
445
- gl.clearColor(red, green, blue, alpha);
446
- }
447
-
448
- /**
449
- * ```
450
- * gl["enable"|"disable"](gl.SCISSOR_TEST);
451
- * ```
452
- * @param enable
453
- */
454
- setScissorTest(enable: boolean) {
455
- const { gl, scissorEnabled } = this;
456
- if (enable === scissorEnabled) {
457
- return;
458
- }
459
- if (enable) {
460
- gl.enable(gl.SCISSOR_TEST);
461
- } else {
462
- gl.disable(gl.SCISSOR_TEST);
463
- }
464
- this.scissorEnabled = enable;
465
- }
466
-
467
- /**
468
- * ```
469
- * gl.scissor(x, y, width, height);
470
- * ```
471
- *
472
- * @param x
473
- * @param y
474
- * @param width
475
- * @param height
476
- */
477
- scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
478
- const { gl, scissorX, scissorY, scissorWidth, scissorHeight } = this;
479
- if (
480
- x !== scissorX ||
481
- y !== scissorY ||
482
- width !== scissorWidth ||
483
- height !== scissorHeight
484
- ) {
485
- gl.scissor(x, y, width, height);
486
- this.scissorX = x;
487
- this.scissorY = y;
488
- this.scissorWidth = width;
489
- this.scissorHeight = height;
490
- }
491
- }
492
-
493
- /**
494
- * ```
495
- * gl["enable"|"disable"](gl.BLEND);
496
- * ```
497
- *
498
- * @param blend
499
- * @returns
500
- */
501
- setBlend(blend: boolean) {
502
- const { gl, blendEnabled } = this;
503
- if (blend === blendEnabled) {
504
- return;
505
- }
506
- if (blend) {
507
- gl.enable(gl.BLEND);
508
- } else {
509
- gl.disable(gl.BLEND);
510
- }
511
- this.blendEnabled = blend;
512
- }
513
-
514
- /**
515
- * ```
516
- * gl.blendFunc(src, dst);
517
- * ```
518
- *
519
- * @param src
520
- * @param dst
521
- */
522
- blendFunc(src: GLenum, dst: GLenum) {
523
- const { gl, blendSrcRgb, blendDstRgb, blendSrcAlpha, blendDstAlpha } = this;
524
- if (
525
- src !== blendSrcRgb ||
526
- dst !== blendDstRgb ||
527
- src !== blendSrcAlpha ||
528
- dst !== blendDstAlpha
529
- ) {
530
- gl.blendFunc(src, dst);
531
- this.blendSrcRgb = src;
532
- this.blendDstRgb = dst;
533
- this.blendSrcAlpha = src;
534
- this.blendDstAlpha = dst;
535
- }
536
- }
537
-
538
- /**
539
- * ```
540
- * gl.createBuffer();
541
- * ```
542
- *
543
- * @returns
544
- */
545
- createBuffer() {
546
- const { gl } = this;
547
- return gl.createBuffer();
548
- }
549
-
550
- /**
551
- * ```
552
- * gl.createFramebuffer();
553
- * ```
554
- * @returns
555
- */
556
- createFramebuffer() {
557
- const { gl } = this;
558
- return gl.createFramebuffer();
559
- }
560
-
561
- /**
562
- * ```
563
- * gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
564
- * ```
565
- *
566
- * @param framebuffer
567
- */
568
- bindFramebuffer(framebuffer: WebGLFramebuffer | null) {
569
- const { gl } = this;
570
- gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
571
- }
572
-
573
- /**
574
- * ```
575
- * gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
576
- * ```
577
- * @remarks
578
- * **WebGL Difference**: Bind target is always `gl.FRAMEBUFFER` and textarget is always `gl.TEXTURE_2D`
579
- */
580
-
581
- framebufferTexture2D(
582
- attachment: GLenum,
583
- texture: WebGLTexture | null,
584
- level: GLint,
585
- ) {
586
- const { gl } = this;
587
- gl.framebufferTexture2D(
588
- gl.FRAMEBUFFER,
589
- attachment,
590
- gl.TEXTURE_2D,
591
- texture,
592
- level,
593
- );
594
- }
595
-
596
- /**
597
- * ```
598
- * gl.clear(gl.COLOR_BUFFER_BIT);
599
- * ```
600
- *
601
- * @remarks
602
- * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
603
- */
604
- clear() {
605
- const { gl } = this;
606
- gl.clear(gl.COLOR_BUFFER_BIT);
607
- }
608
-
609
- /**
610
- * ```
611
- * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
612
- * gl.bufferData(gl.ARRAY_BUFFER, data, usage);
613
- * ```
614
- *
615
- * @remarks
616
- * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
617
- *
618
- * @param buffer
619
- * @param data
620
- * @param usage
621
- */
622
- arrayBufferData(
623
- buffer: WebGLBuffer | null,
624
- data: ArrayBufferView,
625
- usage: GLenum,
626
- ) {
627
- const { gl, boundArrayBuffer } = this;
628
- if (boundArrayBuffer !== buffer) {
629
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
630
- this.boundArrayBuffer = buffer;
631
- }
632
- gl.bufferData(gl.ARRAY_BUFFER, data, usage);
633
- }
634
-
635
- /**
636
- * ```
637
- * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
638
- * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
639
- * ```
640
- * @remarks
641
- * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
642
- *
643
- * @param buffer
644
- * @param data
645
- * @param usage
646
- */
647
- elementArrayBufferData(
648
- buffer: WebGLBuffer | null,
649
- data: ArrayBufferView,
650
- usage: GLenum,
651
- ) {
652
- const { gl, boundElementArrayBuffer } = this;
653
- if (boundElementArrayBuffer !== buffer) {
654
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
655
- this.boundElementArrayBuffer = buffer;
656
- }
657
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
658
- }
659
-
660
- /**
661
- * ```
662
- * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
663
- * gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
664
- * ```
665
- *
666
- * @remarks
667
- * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
668
- *
669
- * @param buffer
670
- * @param index
671
- * @param size
672
- * @param type
673
- * @param normalized
674
- * @param stride
675
- * @param offset
676
- */
677
- vertexAttribPointer(
678
- buffer: WebGLBuffer,
679
- index: GLuint,
680
- size: GLint,
681
- type: GLenum,
682
- normalized: GLboolean,
683
- stride: GLsizei,
684
- offset: GLintptr,
685
- ) {
686
- const { gl, boundArrayBuffer } = this;
687
- if (boundArrayBuffer !== buffer) {
688
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
689
- this.boundArrayBuffer = buffer;
690
- }
691
- gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
692
- }
693
-
694
- /**
695
- * ```
696
- * gl.useProgram(program);
697
- * ```
698
- *
699
- * @param program
700
- * @returns
701
- */
702
- useProgram(program: WebGLProgram | null) {
703
- const { gl, curProgram } = this;
704
- if (curProgram === program) {
705
- return;
706
- }
707
- gl.useProgram(program);
708
- this.curProgram = program;
709
- }
710
-
711
- /**
712
- * Sets the value of a single float uniform variable.
713
- *
714
- * @param location - The location of the uniform variable.
715
- * @param v0 - The value to set.
716
- */
717
- uniform1f(location: WebGLUniformLocation | null, v0: number) {
718
- const { gl } = this;
719
- gl.uniform1f(location, v0);
720
- }
721
-
722
- /**
723
- * Sets the value of a float array uniform variable.
724
- *
725
- * @param location - The location of the uniform variable.
726
- * @param value - The array of values to set.
727
- */
728
- uniform1fv(
729
- location: WebGLUniformLocation | null,
730
- value: Float32Array | number[],
731
- ) {
732
- const { gl } = this;
733
- gl.uniform1fv(location, value);
734
- }
735
-
736
- /**
737
- * Sets the value of a single integer uniform variable.
738
- *
739
- * @param location - The location of the uniform variable.
740
- * @param v0 - The value to set.
741
- */
742
- uniform1i(location: WebGLUniformLocation | null, v0: number) {
743
- const { gl } = this;
744
- gl.uniform1i(location, v0);
745
- }
746
-
747
- /**
748
- * Sets the value of an integer array uniform variable.
749
- *
750
- * @param location - The location of the uniform variable.
751
- * @param value - The array of values to set.
752
- */
753
- uniform1iv(
754
- location: WebGLUniformLocation | null,
755
- value: Int32Array | number[],
756
- ) {
757
- const { gl } = this;
758
- gl.uniform1iv(location, value);
759
- }
760
-
761
- /**
762
- * Sets the value of a vec2 uniform variable.
763
- *
764
- * @param location - The location of the uniform variable.
765
- * @param v0 - The first component of the vector.
766
- * @param v1 - The second component of the vector.
767
- */
768
- uniform2f(location: WebGLUniformLocation | null, v0: number, v1: number) {
769
- const { gl } = this;
770
- gl.uniform2f(location, v0, v1);
771
- }
772
-
773
- /**
774
- * Sets the value of a vec2 array uniform variable.
775
- *
776
- * @param location - The location of the uniform variable.
777
- * @param value - The array of vec2 values to set.
778
- */
779
- uniform2fv(
780
- location: WebGLUniformLocation | null,
781
- value: Float32Array | number[],
782
- ) {
783
- const { gl } = this;
784
- gl.uniform2fv(location, value);
785
- }
786
-
787
- /**
788
- * Sets the value of a ivec2 uniform variable.
789
- *
790
- * @param location - The location of the uniform variable.
791
- * @param v0 - The first component of the vector.
792
- * @param v1 - The second component of the vector.
793
- */
794
- uniform2i(location: WebGLUniformLocation | null, v0: number, v1: number) {
795
- const { gl } = this;
796
- gl.uniform2i(location, v0, v1);
797
- }
798
-
799
- /**
800
- * Sets the value of an ivec2 array uniform variable.
801
- *
802
- * @param location - The location of the uniform variable.
803
- * @param value - The array of ivec2 values to set.
804
- */
805
- uniform2iv(
806
- location: WebGLUniformLocation | null,
807
- value: Int32Array | number[],
808
- ) {
809
- const { gl } = this;
810
- gl.uniform2iv(location, value);
811
- }
812
-
813
- /**
814
- * Sets the value of a vec3 uniform variable.
815
- *
816
- * @param location - The location of the uniform variable.
817
- * @param v0 - The first component of the vector.
818
- * @param v1 - The second component of the vector.
819
- * @param v2 - The third component of the vector.
820
- */
821
- uniform3f(
822
- location: WebGLUniformLocation | null,
823
- v0: number,
824
- v1: number,
825
- v2: number,
826
- ) {
827
- const { gl } = this;
828
- gl.uniform3f(location, v0, v1, v2);
829
- }
830
-
831
- /**
832
- * Sets the value of a vec3 array uniform variable.
833
- *
834
- * @param location - The location of the uniform variable.
835
- * @param value - The array of vec3 values to set.
836
- */
837
- uniform3fv(
838
- location: WebGLUniformLocation | null,
839
- value: Float32Array | number[],
840
- ) {
841
- const { gl } = this;
842
- gl.uniform3fv(location, value);
843
- }
844
-
845
- /**
846
- * Sets the value of a ivec3 uniform variable.
847
- *
848
- * @param location - The location of the uniform variable.
849
- * @param v0 - The first component of the vector.
850
- * @param v1 - The second component of the vector.
851
- * @param v2 - The third component of the vector.
852
- */
853
- uniform3i(
854
- location: WebGLUniformLocation | null,
855
- v0: number,
856
- v1: number,
857
- v2: number,
858
- ) {
859
- const { gl } = this;
860
- gl.uniform3i(location, v0, v1, v2);
861
- }
862
-
863
- /**
864
- * Sets the value of an ivec3 array uniform variable.
865
- *
866
- * @param location - The location of the uniform variable.
867
- * @param value - The array of ivec3 values to set.
868
- */
869
- uniform3iv(
870
- location: WebGLUniformLocation | null,
871
- value: Int32Array | number[],
872
- ) {
873
- const { gl } = this;
874
- gl.uniform3iv(location, value);
875
- }
876
-
877
- /**
878
- * Sets the value of a vec4 uniform variable.
879
- *
880
- * @param location - The location of the uniform variable.
881
- * @param v0 - The first component of the vector.
882
- * @param v1 - The second component of the vector.
883
- * @param v2 - The third component of the vector.
884
- * @param v3 - The fourth component of the vector.
885
- */
886
- uniform4f(
887
- location: WebGLUniformLocation | null,
888
- v0: number,
889
- v1: number,
890
- v2: number,
891
- v3: number,
892
- ) {
893
- const { gl } = this;
894
- gl.uniform4f(location, v0, v1, v2, v3);
895
- }
896
-
897
- /**
898
- * Sets the value of a vec4 array uniform variable.
899
- *
900
- * @param location - The location of the uniform variable.
901
- * @param value - The array of vec4 values to set.
902
- */
903
- uniform4fv(
904
- location: WebGLUniformLocation | null,
905
- value: Float32Array | number[],
906
- ) {
907
- const { gl } = this;
908
- gl.uniform4fv(location, value);
909
- }
910
-
911
- /**
912
- * Sets the value of a ivec4 uniform variable.
913
- *
914
- * @param location - The location of the uniform variable.
915
- * @param v0 - The first component of the vector.
916
- * @param v1 - The second component of the vector.
917
- * @param v2 - The third component of the vector.
918
- * @param v3 - The fourth component of the vector.
919
- */
920
- uniform4i(
921
- location: WebGLUniformLocation | null,
922
- v0: number,
923
- v1: number,
924
- v2: number,
925
- v3: number,
926
- ) {
927
- const { gl } = this;
928
- gl.uniform4i(location, v0, v1, v2, v3);
929
- }
930
-
931
- /**
932
- * Sets the value of an ivec4 array uniform variable.
933
- *
934
- * @param location - The location of the uniform variable.
935
- * @param value - The array of ivec4 values to set.
936
- */
937
- uniform4iv(
938
- location: WebGLUniformLocation | null,
939
- value: Int32Array | number[],
940
- ) {
941
- const { gl } = this;
942
- gl.uniform4iv(location, value);
943
- }
944
-
945
- /**
946
- * Sets the value of a mat2 uniform variable.
947
- *
948
- * @param location - The location of the uniform variable.
949
- * @param transpose - Whether to transpose the matrix.
950
- * @param value - The array of mat2 values to set.
951
- */
952
- uniformMatrix2fv(
953
- location: WebGLUniformLocation | null,
954
- value: Float32Array | number[],
955
- ) {
956
- const { gl } = this;
957
- gl.uniformMatrix2fv(location, false, value);
958
- }
959
-
960
- /**
961
- * Sets the value of a mat2 uniform variable.
962
- * @param location - The location of the uniform variable.
963
- * @param value - The array of mat2 values to set.
964
- */
965
- uniformMatrix3fv(
966
- location: WebGLUniformLocation | null,
967
- value: Float32Array | number[],
968
- ) {
969
- const { gl } = this;
970
- gl.uniformMatrix3fv(location, false, value);
971
- }
972
-
973
- /**
974
- * Sets the value of a mat4 uniform variable.
975
- * @param location - The location of the uniform variable.
976
- * @param value - The array of mat4 values to set.
977
- */
978
- uniformMatrix4fv(
979
- location: WebGLUniformLocation | null,
980
- value: Float32Array | number[],
981
- ) {
982
- const { gl } = this;
983
- gl.uniformMatrix4fv(location, false, value);
984
- }
985
-
986
- /**
987
- * ```
988
- * gl.getParameter(pname);
989
- * ```
990
- *
991
- * @param pname
992
- * @returns
993
- */
994
- getParameter(pname: GLenum): any {
995
- const { gl } = this;
996
- return gl.getParameter(pname);
997
- }
998
-
999
- /**
1000
- * ```
1001
- * gl.drawElements(mode, count, type, offset);
1002
- * ```
1003
- *
1004
- * @param mode
1005
- * @param count
1006
- * @param type
1007
- * @param offset
1008
- */
1009
- drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
1010
- const { gl } = this;
1011
- gl.drawElements(mode, count, type, offset);
1012
- }
1013
-
1014
- /**
1015
- * ```
1016
- * gl.drawArrays(mode, first, count);
1017
- * ```
1018
- *
1019
- * @param name
1020
- * @returns
1021
- */
1022
- getExtension(name: string) {
1023
- const { gl } = this;
1024
- return gl.getExtension(name);
1025
- }
1026
-
1027
- /**
1028
- * ```
1029
- * gl.getError(type);
1030
- * ```
1031
- *
1032
- * @returns
1033
- */
1034
- getError() {
1035
- const { gl } = this;
1036
- return gl.getError();
1037
- }
1038
-
1039
- /**
1040
- * ```
1041
- * gl.createVertexArray();
1042
- * ```
1043
- *
1044
- * @returns
1045
- */
1046
- createVertexArray() {
1047
- const { gl } = this;
1048
- assertTruthy(gl instanceof WebGL2RenderingContext);
1049
- return gl.createVertexArray();
1050
- }
1051
-
1052
- /**
1053
- * ```
1054
- * gl.bindVertexArray(vertexArray);
1055
- * ```
1056
- *
1057
- * @param vertexArray
1058
- */
1059
- bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
1060
- const { gl } = this;
1061
- assertTruthy(gl instanceof WebGL2RenderingContext);
1062
- gl.bindVertexArray(vertexArray);
1063
- }
1064
-
1065
- /**
1066
- * ```
1067
- * gl.getAttribLocation(program, name);
1068
- * ```
1069
- *
1070
- * @param program
1071
- * @param name
1072
- * @returns
1073
- */
1074
- getAttribLocation(program: WebGLProgram, name: string) {
1075
- const { gl } = this;
1076
- return gl.getAttribLocation(program, name);
1077
- }
1078
-
1079
- /**
1080
- * ```
1081
- * gl.getUniformLocation(program, name);
1082
- * ```
1083
- *
1084
- * @param program
1085
- * @param name
1086
- * @returns
1087
- */
1088
- getUniformLocation(program: WebGLProgram, name: string) {
1089
- const { gl } = this;
1090
- return gl.getUniformLocation(program, name);
1091
- }
1092
-
1093
- /**
1094
- * ```
1095
- * gl.enableVertexAttribArray(index);
1096
- * ```
1097
- *
1098
- * @param index
1099
- */
1100
- enableVertexAttribArray(index: number) {
1101
- const { gl } = this;
1102
- gl.enableVertexAttribArray(index);
1103
- }
1104
-
1105
- /**
1106
- * ```
1107
- * gl.disableVertexAttribArray(index);
1108
- * ```
1109
- *
1110
- * @param index
1111
- */
1112
- disableVertexAttribArray(index: number) {
1113
- const { gl } = this;
1114
- gl.disableVertexAttribArray(index);
1115
- }
1116
-
1117
- /**
1118
- * ```
1119
- * gl.createShader(type);
1120
- * ```
1121
- *
1122
- * @param type
1123
- * @returns
1124
- */
1125
- createShader(type: number) {
1126
- const { gl } = this;
1127
- return gl.createShader(type);
1128
- }
1129
-
1130
- /**
1131
- * ```
1132
- * gl.compileShader(shader);
1133
- * ```
1134
- *
1135
- * @param shader
1136
- * @returns
1137
- */
1138
- compileShader(shader: WebGLShader) {
1139
- const { gl } = this;
1140
- gl.compileShader(shader);
1141
- }
1142
-
1143
- /**
1144
- * ```
1145
- * gl.attachShader(program, shader);
1146
- * ```
1147
- *
1148
- * @param program
1149
- * @param shader
1150
- */
1151
- attachShader(program: WebGLProgram, shader: WebGLShader) {
1152
- const { gl } = this;
1153
- gl.attachShader(program, shader);
1154
- }
1155
-
1156
- /**
1157
- * ```
1158
- * gl.linkProgram(program);
1159
- * ```
1160
- *
1161
- * @param program
1162
- */
1163
- linkProgram(program: WebGLProgram) {
1164
- const { gl } = this;
1165
- gl.linkProgram(program);
1166
- }
1167
-
1168
- /**
1169
- * ```
1170
- * gl.deleteProgram(shader);
1171
- * ```
1172
- *
1173
- * @param shader
1174
- */
1175
- deleteProgram(shader: WebGLProgram) {
1176
- const { gl } = this;
1177
- gl.deleteProgram(shader);
1178
- }
1179
-
1180
- /**
1181
- * ```
1182
- * gl.getShaderParameter(shader, pname);
1183
- * ```
1184
- *
1185
- * @param shader
1186
- * @param pname
1187
- */
1188
- getShaderParameter(shader: WebGLShader, pname: GLenum) {
1189
- const { gl } = this;
1190
- return gl.getShaderParameter(shader, pname);
1191
- }
1192
-
1193
- /**
1194
- * ```
1195
- * gl.getShaderInfoLog(shader);
1196
- * ```
1197
- *
1198
- * @param shader
1199
- */
1200
- getShaderInfoLog(shader: WebGLShader) {
1201
- const { gl } = this;
1202
- return gl.getShaderInfoLog(shader);
1203
- }
1204
-
1205
- /**
1206
- * ```
1207
- * gl.createProgram();
1208
- * ```
1209
- *
1210
- * @returns
1211
- */
1212
- createProgram() {
1213
- const { gl } = this;
1214
- return gl.createProgram();
1215
- }
1216
-
1217
- /**
1218
- * ```
1219
- * gl.getProgramParameter(program, pname);
1220
- * ```
1221
- *
1222
- * @param program
1223
- * @param pname
1224
- * @returns
1225
- */
1226
- getProgramParameter(program: WebGLProgram, pname: GLenum) {
1227
- const { gl } = this;
1228
- return gl.getProgramParameter(program, pname);
1229
- }
1230
-
1231
- /**
1232
- * ```
1233
- * gl.getProgramInfoLog(program);
1234
- * ```
1235
- *
1236
- * @param program
1237
- * @returns
1238
- */
1239
- getProgramInfoLog(program: WebGLProgram) {
1240
- const { gl } = this;
1241
- return gl.getProgramInfoLog(program);
1242
- }
1243
-
1244
- /**
1245
- * ```
1246
- * gl.shaderSource(shader, source);
1247
- * ```
1248
- *
1249
- * @param shader
1250
- * @param source
1251
- */
1252
- shaderSource(shader: WebGLShader, source: string) {
1253
- const { gl } = this;
1254
- gl.shaderSource(shader, source);
1255
- }
1256
-
1257
- /**
1258
- * ```
1259
- * gl.deleteShader(shader);
1260
- * ```
1261
- *
1262
- * @param shader
1263
- */
1264
- deleteShader(shader: WebGLShader) {
1265
- const { gl } = this;
1266
- gl.deleteShader(shader);
1267
- }
1268
- }
1269
-
1270
- // prettier-ignore
1271
- type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
1272
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
1273
- MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
1274
- ? true
1275
- : false
1276
- : false;
1277
-
1278
- // prettier-ignore
1279
- export type UniformMethodMap = {
1280
- [Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
1281
- location: WebGLUniformLocation | null,
1282
- ...args: infer T
1283
- ) => void
1284
- ? T
1285
- : never;
1286
- };
1287
-
1288
- /**
1289
- * Compare two arrays for equality.
1290
- *
1291
- * @remarks
1292
- * This function will not try to compare nested arrays or Float32Arrays and
1293
- * instead will always return false when they are encountered.
1294
- *
1295
- * @param a
1296
- * @param b
1297
- * @returns
1298
- */
1299
- export function compareArrays<T>(a: T[], b: T[]): boolean {
1300
- if (a.length !== b.length) {
1301
- return false;
1302
- }
1303
-
1304
- let result = false;
1305
- for (let i = 0; i < a.length; i++) {
1306
- if (Array.isArray(a[i]) || a[i] instanceof Float32Array) {
1307
- result = false;
1308
- break;
1309
- }
1310
-
1311
- if (a[i] !== b[i]) {
1312
- result = false;
1313
- break;
1314
- }
1315
-
1316
- result = true;
1317
- }
1318
-
1319
- return result;
1320
- }
1
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
4
+
5
+ import { assertTruthy } from '../../utils.js';
6
+ import { isWebGl2 } from '../renderers/webgl/internal/WebGlUtils.js';
7
+
8
+ /**
9
+ * Optimized WebGL Context Wrapper
10
+ *
11
+ * @remarks
12
+ * This class contains the subset of the WebGLRenderingContext & WebGL2RenderingContext
13
+ * API that is used by the renderer. Select high volume WebGL methods include
14
+ * caching optimizations to avoid making WebGL calls if the state is already set
15
+ * to the desired value.
16
+ *
17
+ * While most methods contained are direct passthroughs to the WebGL context,
18
+ * some methods combine multiple WebGL calls into one for convenience, modify
19
+ * arguments to be more convenient, or are replaced by more specific methods.
20
+ *
21
+ * Not all methods are optimized. Only methods that are called frequently
22
+ * and/or have a high cost are optimized.
23
+ *
24
+ * A subset of GLenum constants are also exposed as properties on this class
25
+ * for convenience.
26
+ */
27
+ export class WebGlContextWrapper {
28
+ //#region Cached WebGL State
29
+ private activeTextureUnit = 0;
30
+ private texture2dUnits: Array<WebGLTexture | null>;
31
+ private texture2dParams: WeakMap<
32
+ WebGLTexture,
33
+ Record<number, number | undefined>
34
+ > = new WeakMap();
35
+ private scissorEnabled;
36
+ private scissorX: number;
37
+ private scissorY: number;
38
+ private scissorWidth: number;
39
+ private scissorHeight: number;
40
+ private blendEnabled;
41
+ private blendSrcRgb: number;
42
+ private blendDstRgb: number;
43
+ private blendSrcAlpha: number;
44
+ private blendDstAlpha: number;
45
+ private boundArrayBuffer: WebGLBuffer | null;
46
+ private boundElementArrayBuffer: WebGLBuffer | null;
47
+ private curProgram: WebGLProgram | null;
48
+ //#endregion Cached WebGL State
49
+
50
+ //#region Canvas
51
+ public readonly canvas;
52
+ //#endregion Canvas
53
+
54
+ //#region WebGL Enums
55
+ public readonly MAX_RENDERBUFFER_SIZE;
56
+ public readonly MAX_TEXTURE_SIZE;
57
+ public readonly MAX_VIEWPORT_DIMS;
58
+ public readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS;
59
+ public readonly MAX_TEXTURE_IMAGE_UNITS;
60
+ public readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS;
61
+ public readonly MAX_VERTEX_ATTRIBS;
62
+ public readonly MAX_VARYING_VECTORS;
63
+ public readonly MAX_VERTEX_UNIFORM_VECTORS;
64
+ public readonly MAX_FRAGMENT_UNIFORM_VECTORS;
65
+ public readonly TEXTURE_MAG_FILTER;
66
+ public readonly TEXTURE_MIN_FILTER;
67
+ public readonly TEXTURE_WRAP_S;
68
+ public readonly TEXTURE_WRAP_T;
69
+ public readonly LINEAR;
70
+ public readonly CLAMP_TO_EDGE;
71
+ public readonly RGB;
72
+ public readonly RGBA;
73
+ public readonly UNSIGNED_BYTE;
74
+ public readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL;
75
+ public readonly UNPACK_FLIP_Y_WEBGL;
76
+ public readonly FLOAT;
77
+ public readonly TRIANGLES;
78
+ public readonly UNSIGNED_SHORT;
79
+ public readonly ONE;
80
+ public readonly ONE_MINUS_SRC_ALPHA;
81
+ public readonly VERTEX_SHADER;
82
+ public readonly FRAGMENT_SHADER;
83
+ public readonly STATIC_DRAW;
84
+ public readonly COMPILE_STATUS;
85
+ public readonly LINK_STATUS;
86
+ public readonly DYNAMIC_DRAW;
87
+ public readonly COLOR_ATTACHMENT0;
88
+ public readonly INVALID_ENUM: number;
89
+ public readonly INVALID_OPERATION: number;
90
+ //#endregion WebGL Enums
91
+
92
+ constructor(private gl: WebGLRenderingContext | WebGL2RenderingContext) {
93
+ // The following code extracts the current state of the WebGL context
94
+ // to our local JavaScript cached version of it. This is so we can
95
+ // avoid making WebGL calls if we don't need to.
96
+ // We could assume that the WebGL context is in a default state, but
97
+ // in the future we may want to support restoring a broken WebGL context
98
+ // and this will help with that.
99
+ this.activeTextureUnit =
100
+ (gl.getParameter(gl.ACTIVE_TEXTURE) as number) - gl.TEXTURE0;
101
+ const maxTextureUnits = gl.getParameter(
102
+ gl.MAX_TEXTURE_IMAGE_UNITS,
103
+ ) as number;
104
+ // save current texture units
105
+ this.texture2dUnits = new Array<undefined>(maxTextureUnits)
106
+ .fill(undefined)
107
+ .map((_, i) => {
108
+ this.activeTexture(i);
109
+ return gl.getParameter(gl.TEXTURE_BINDING_2D) as WebGLTexture;
110
+ });
111
+ // restore active texture unit
112
+ this.activeTexture(this.activeTextureUnit);
113
+ this.scissorEnabled = gl.isEnabled(gl.SCISSOR_TEST);
114
+
115
+ const scissorBox = gl.getParameter(gl.SCISSOR_BOX) as [
116
+ number,
117
+ number,
118
+ number,
119
+ number,
120
+ ];
121
+ this.scissorX = scissorBox[0];
122
+ this.scissorY = scissorBox[1];
123
+ this.scissorWidth = scissorBox[2];
124
+ this.scissorHeight = scissorBox[3];
125
+
126
+ this.blendEnabled = gl.isEnabled(gl.BLEND);
127
+ this.blendSrcRgb = gl.getParameter(gl.BLEND_SRC_RGB) as number;
128
+ this.blendDstRgb = gl.getParameter(gl.BLEND_DST_RGB) as number;
129
+ this.blendSrcAlpha = gl.getParameter(gl.BLEND_SRC_ALPHA) as number;
130
+ this.blendDstAlpha = gl.getParameter(gl.BLEND_DST_ALPHA) as number;
131
+
132
+ this.boundArrayBuffer = gl.getParameter(
133
+ gl.ARRAY_BUFFER_BINDING,
134
+ ) as WebGLBuffer;
135
+ this.boundElementArrayBuffer = gl.getParameter(
136
+ gl.ELEMENT_ARRAY_BUFFER_BINDING,
137
+ ) as WebGLBuffer;
138
+
139
+ this.curProgram = gl.getParameter(
140
+ gl.CURRENT_PROGRAM,
141
+ ) as WebGLProgram | null;
142
+
143
+ this.canvas = gl.canvas;
144
+
145
+ // Extract GLenums
146
+ this.MAX_RENDERBUFFER_SIZE = gl.MAX_RENDERBUFFER_SIZE;
147
+ this.MAX_TEXTURE_SIZE = gl.MAX_TEXTURE_SIZE;
148
+ this.MAX_VIEWPORT_DIMS = gl.MAX_VIEWPORT_DIMS;
149
+ this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
150
+ this.MAX_TEXTURE_IMAGE_UNITS = gl.MAX_TEXTURE_IMAGE_UNITS;
151
+ this.MAX_COMBINED_TEXTURE_IMAGE_UNITS = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS;
152
+ this.MAX_VERTEX_ATTRIBS = gl.MAX_VERTEX_ATTRIBS;
153
+ this.MAX_VARYING_VECTORS = gl.MAX_VARYING_VECTORS;
154
+ this.MAX_VERTEX_UNIFORM_VECTORS = gl.MAX_VERTEX_UNIFORM_VECTORS;
155
+ this.MAX_FRAGMENT_UNIFORM_VECTORS = gl.MAX_FRAGMENT_UNIFORM_VECTORS;
156
+ this.TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER;
157
+ this.TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER;
158
+ this.TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S;
159
+ this.TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T;
160
+ this.LINEAR = gl.LINEAR;
161
+ this.CLAMP_TO_EDGE = gl.CLAMP_TO_EDGE;
162
+ this.RGB = gl.RGB;
163
+ this.RGBA = gl.RGBA;
164
+ this.UNSIGNED_BYTE = gl.UNSIGNED_BYTE;
165
+ this.UNPACK_PREMULTIPLY_ALPHA_WEBGL = gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL;
166
+ this.UNPACK_FLIP_Y_WEBGL = gl.UNPACK_FLIP_Y_WEBGL;
167
+ this.FLOAT = gl.FLOAT;
168
+ this.TRIANGLES = gl.TRIANGLES;
169
+ this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
170
+ this.ONE = gl.ONE;
171
+ this.ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
172
+ this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
173
+ this.TRIANGLES = gl.TRIANGLES;
174
+ this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
175
+ this.VERTEX_SHADER = gl.VERTEX_SHADER;
176
+ this.FRAGMENT_SHADER = gl.FRAGMENT_SHADER;
177
+ this.STATIC_DRAW = gl.STATIC_DRAW;
178
+ this.COMPILE_STATUS = gl.COMPILE_STATUS;
179
+ this.LINK_STATUS = gl.LINK_STATUS;
180
+ this.DYNAMIC_DRAW = gl.DYNAMIC_DRAW;
181
+ this.COLOR_ATTACHMENT0 = gl.COLOR_ATTACHMENT0;
182
+ this.INVALID_ENUM = gl.INVALID_ENUM;
183
+ this.INVALID_OPERATION = gl.INVALID_OPERATION;
184
+ }
185
+ /**
186
+ * Returns true if the WebGL context is WebGL2
187
+ *
188
+ * @returns
189
+ */
190
+ isWebGl2() {
191
+ return isWebGl2(this.gl);
192
+ }
193
+
194
+ /**
195
+ * ```
196
+ * gl.activeTexture(textureUnit + gl.TEXTURE0);
197
+ * ```
198
+ *
199
+ * @remarks
200
+ * **WebGL Difference**: `textureUnit` is based from 0, not `gl.TEXTURE0`.
201
+ *
202
+ * @param textureUnit
203
+ */
204
+ activeTexture(textureUnit: number) {
205
+ const { gl } = this;
206
+ if (this.activeTextureUnit !== textureUnit) {
207
+ gl.activeTexture(textureUnit + gl.TEXTURE0);
208
+ this.activeTextureUnit = textureUnit;
209
+ }
210
+ }
211
+
212
+ /**
213
+ * ```
214
+ * gl.bindTexture(gl.TEXTURE_2D, texture);
215
+ * ```
216
+ * @remarks
217
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
218
+ *
219
+ * @param texture
220
+ */
221
+ bindTexture(texture: WebGLTexture | null) {
222
+ const { gl, activeTextureUnit, texture2dUnits } = this;
223
+
224
+ if (texture2dUnits[activeTextureUnit] === texture) {
225
+ return;
226
+ }
227
+ texture2dUnits[activeTextureUnit] = texture;
228
+
229
+ gl.bindTexture(this.gl.TEXTURE_2D, texture);
230
+ }
231
+
232
+ private _getActiveTexture(): WebGLTexture | null {
233
+ const { activeTextureUnit, texture2dUnits } = this;
234
+ return texture2dUnits[activeTextureUnit]!;
235
+ }
236
+
237
+ /**
238
+ * ```
239
+ * gl.texParameteri(gl.TEXTURE_2D, pname, param);
240
+ * ```
241
+ * @remarks
242
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
243
+ *
244
+ * @param pname
245
+ * @param param
246
+ * @returns
247
+ */
248
+ texParameteri(pname: number, param: number) {
249
+ const { gl, texture2dParams } = this;
250
+
251
+ const activeTexture = this._getActiveTexture();
252
+ if (!activeTexture) {
253
+ throw new Error('No active texture');
254
+ }
255
+ let textureParams = texture2dParams.get(activeTexture);
256
+ if (!textureParams) {
257
+ textureParams = {};
258
+ texture2dParams.set(activeTexture, textureParams);
259
+ }
260
+ if (textureParams[pname] === param) {
261
+ return;
262
+ }
263
+ textureParams[pname] = param;
264
+ gl.texParameteri(gl.TEXTURE_2D, pname, param);
265
+ }
266
+
267
+ /**
268
+ * ```
269
+ * gl.texImage2D(
270
+ * gl.TEXTURE_2D,
271
+ * level,
272
+ * internalFormat,
273
+ * width,
274
+ * height,
275
+ * border,
276
+ * format,
277
+ * type,
278
+ * pixels,
279
+ * );
280
+ * ```
281
+ * @remarks
282
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
283
+ *
284
+ * @param level
285
+ * @param internalFormat
286
+ * @param width
287
+ * @param height
288
+ * @param border
289
+ * @param format
290
+ * @param type
291
+ * @param pixels
292
+ */
293
+ texImage2D(
294
+ level: GLint,
295
+ internalformat: GLint,
296
+ width: GLsizei,
297
+ height: GLsizei,
298
+ border: GLint,
299
+ format: GLenum,
300
+ type: GLenum,
301
+ pixels: ArrayBufferView | null,
302
+ ): void;
303
+ texImage2D(
304
+ level: GLint,
305
+ internalformat: GLint,
306
+ format: GLenum,
307
+ type: GLenum,
308
+ source: TexImageSource | Uint8Array,
309
+ ): void;
310
+ texImage2D(
311
+ level: any,
312
+ internalFormat: any,
313
+ widthOrFormat: any,
314
+ heightOrType: any,
315
+ borderOrSource: any,
316
+ format?: any,
317
+ type?: any,
318
+ pixels?: any,
319
+ ) {
320
+ const { gl } = this;
321
+ if (format) {
322
+ gl.texImage2D(
323
+ gl.TEXTURE_2D,
324
+ level,
325
+ internalFormat,
326
+ widthOrFormat,
327
+ heightOrType,
328
+ borderOrSource,
329
+ format,
330
+ type,
331
+ pixels,
332
+ );
333
+ } else {
334
+ gl.texImage2D(
335
+ gl.TEXTURE_2D,
336
+ level,
337
+ internalFormat,
338
+ widthOrFormat,
339
+ heightOrType,
340
+ borderOrSource,
341
+ );
342
+ }
343
+ }
344
+ /**
345
+ * ```
346
+ * gl.compressedTexImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, data);
347
+ * ```
348
+ *
349
+ * @remarks
350
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
351
+ */
352
+
353
+ compressedTexImage2D(
354
+ level: GLint,
355
+ internalformat: GLenum,
356
+ width: GLsizei,
357
+ height: GLsizei,
358
+ border: GLint,
359
+ data?: ArrayBufferView,
360
+ ): void {
361
+ const { gl } = this;
362
+ gl.compressedTexImage2D(
363
+ gl.TEXTURE_2D,
364
+ level,
365
+ internalformat,
366
+ width,
367
+ height,
368
+ border,
369
+ data as ArrayBufferView,
370
+ );
371
+ }
372
+ /**
373
+ * ```
374
+ * gl.pixelStorei(pname, param);
375
+ * ```
376
+ *
377
+ * @param pname
378
+ * @param param
379
+ */
380
+ pixelStorei(pname: GLenum, param: GLint | GLboolean) {
381
+ const { gl } = this;
382
+ gl.pixelStorei(pname, param);
383
+ }
384
+
385
+ /**
386
+ * ```
387
+ * gl.generateMipmap(gl.TEXTURE_2D);
388
+ * ```
389
+ *
390
+ * @remarks
391
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
392
+ */
393
+ generateMipmap() {
394
+ const { gl } = this;
395
+ gl.generateMipmap(gl.TEXTURE_2D);
396
+ }
397
+
398
+ /**
399
+ * ```
400
+ * gl.createTexture();
401
+ * ```
402
+ *
403
+ * @returns
404
+ */
405
+ createTexture() {
406
+ const { gl } = this;
407
+ return gl.createTexture();
408
+ }
409
+
410
+ /**
411
+ * ```
412
+ * gl.deleteTexture(texture);
413
+ * ```
414
+ *
415
+ * @param texture
416
+ */
417
+ deleteTexture(texture: WebGLTexture | null) {
418
+ const { gl } = this;
419
+ if (texture) {
420
+ this.texture2dParams.delete(texture);
421
+ }
422
+ gl.deleteTexture(texture);
423
+ }
424
+
425
+ /**
426
+ * ```
427
+ * gl.viewport(x, y, width, height);
428
+ * ```
429
+ */
430
+ viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
431
+ const { gl } = this;
432
+ gl.viewport(x, y, width, height);
433
+ }
434
+
435
+ /**
436
+ * ```
437
+ * gl.clearColor(red, green, blue, alpha);
438
+ * ```
439
+ *
440
+ * @param red
441
+ * @param green
442
+ * @param blue
443
+ * @param alpha
444
+ */
445
+ clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
446
+ const { gl } = this;
447
+ gl.clearColor(red, green, blue, alpha);
448
+ }
449
+
450
+ /**
451
+ * ```
452
+ * gl["enable"|"disable"](gl.SCISSOR_TEST);
453
+ * ```
454
+ * @param enable
455
+ */
456
+ setScissorTest(enable: boolean) {
457
+ const { gl, scissorEnabled } = this;
458
+ if (enable === scissorEnabled) {
459
+ return;
460
+ }
461
+ if (enable) {
462
+ gl.enable(gl.SCISSOR_TEST);
463
+ } else {
464
+ gl.disable(gl.SCISSOR_TEST);
465
+ }
466
+ this.scissorEnabled = enable;
467
+ }
468
+
469
+ /**
470
+ * ```
471
+ * gl.scissor(x, y, width, height);
472
+ * ```
473
+ *
474
+ * @param x
475
+ * @param y
476
+ * @param width
477
+ * @param height
478
+ */
479
+ scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
480
+ const { gl, scissorX, scissorY, scissorWidth, scissorHeight } = this;
481
+ if (
482
+ x !== scissorX ||
483
+ y !== scissorY ||
484
+ width !== scissorWidth ||
485
+ height !== scissorHeight
486
+ ) {
487
+ gl.scissor(x, y, width, height);
488
+ this.scissorX = x;
489
+ this.scissorY = y;
490
+ this.scissorWidth = width;
491
+ this.scissorHeight = height;
492
+ }
493
+ }
494
+
495
+ /**
496
+ * ```
497
+ * gl["enable"|"disable"](gl.BLEND);
498
+ * ```
499
+ *
500
+ * @param blend
501
+ * @returns
502
+ */
503
+ setBlend(blend: boolean) {
504
+ const { gl, blendEnabled } = this;
505
+ if (blend === blendEnabled) {
506
+ return;
507
+ }
508
+ if (blend) {
509
+ gl.enable(gl.BLEND);
510
+ } else {
511
+ gl.disable(gl.BLEND);
512
+ }
513
+ this.blendEnabled = blend;
514
+ }
515
+
516
+ /**
517
+ * ```
518
+ * gl.blendFunc(src, dst);
519
+ * ```
520
+ *
521
+ * @param src
522
+ * @param dst
523
+ */
524
+ blendFunc(src: GLenum, dst: GLenum) {
525
+ const { gl, blendSrcRgb, blendDstRgb, blendSrcAlpha, blendDstAlpha } = this;
526
+ if (
527
+ src !== blendSrcRgb ||
528
+ dst !== blendDstRgb ||
529
+ src !== blendSrcAlpha ||
530
+ dst !== blendDstAlpha
531
+ ) {
532
+ gl.blendFunc(src, dst);
533
+ this.blendSrcRgb = src;
534
+ this.blendDstRgb = dst;
535
+ this.blendSrcAlpha = src;
536
+ this.blendDstAlpha = dst;
537
+ }
538
+ }
539
+
540
+ /**
541
+ * ```
542
+ * gl.createBuffer();
543
+ * ```
544
+ *
545
+ * @returns
546
+ */
547
+ createBuffer() {
548
+ const { gl } = this;
549
+ return gl.createBuffer();
550
+ }
551
+
552
+ /**
553
+ * ```
554
+ * gl.createFramebuffer();
555
+ * ```
556
+ * @returns
557
+ */
558
+ createFramebuffer() {
559
+ const { gl } = this;
560
+ return gl.createFramebuffer();
561
+ }
562
+
563
+ /**
564
+ * ```
565
+ * gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
566
+ * ```
567
+ *
568
+ * @param framebuffer
569
+ */
570
+ bindFramebuffer(framebuffer: WebGLFramebuffer | null) {
571
+ const { gl } = this;
572
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
573
+ }
574
+
575
+ /**
576
+ * ```
577
+ * gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
578
+ * ```
579
+ * @remarks
580
+ * **WebGL Difference**: Bind target is always `gl.FRAMEBUFFER` and textarget is always `gl.TEXTURE_2D`
581
+ */
582
+
583
+ framebufferTexture2D(
584
+ attachment: GLenum,
585
+ texture: WebGLTexture | null,
586
+ level: GLint,
587
+ ) {
588
+ const { gl } = this;
589
+ gl.framebufferTexture2D(
590
+ gl.FRAMEBUFFER,
591
+ attachment,
592
+ gl.TEXTURE_2D,
593
+ texture,
594
+ level,
595
+ );
596
+ }
597
+
598
+ /**
599
+ * ```
600
+ * gl.clear(gl.COLOR_BUFFER_BIT);
601
+ * ```
602
+ *
603
+ * @remarks
604
+ * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
605
+ */
606
+ clear() {
607
+ const { gl } = this;
608
+ gl.clear(gl.COLOR_BUFFER_BIT);
609
+ }
610
+
611
+ /**
612
+ * ```
613
+ * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
614
+ * gl.bufferData(gl.ARRAY_BUFFER, data, usage);
615
+ * ```
616
+ *
617
+ * @remarks
618
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
619
+ *
620
+ * @param buffer
621
+ * @param data
622
+ * @param usage
623
+ */
624
+ arrayBufferData(
625
+ buffer: WebGLBuffer | null,
626
+ data: ArrayBufferView,
627
+ usage: GLenum,
628
+ ) {
629
+ const { gl, boundArrayBuffer } = this;
630
+ if (boundArrayBuffer !== buffer) {
631
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
632
+ this.boundArrayBuffer = buffer;
633
+ }
634
+ gl.bufferData(gl.ARRAY_BUFFER, data, usage);
635
+ }
636
+
637
+ /**
638
+ * ```
639
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
640
+ * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
641
+ * ```
642
+ * @remarks
643
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
644
+ *
645
+ * @param buffer
646
+ * @param data
647
+ * @param usage
648
+ */
649
+ elementArrayBufferData(
650
+ buffer: WebGLBuffer | null,
651
+ data: ArrayBufferView,
652
+ usage: GLenum,
653
+ ) {
654
+ const { gl, boundElementArrayBuffer } = this;
655
+ if (boundElementArrayBuffer !== buffer) {
656
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
657
+ this.boundElementArrayBuffer = buffer;
658
+ }
659
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
660
+ }
661
+
662
+ /**
663
+ * ```
664
+ * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
665
+ * gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
666
+ * ```
667
+ *
668
+ * @remarks
669
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
670
+ *
671
+ * @param buffer
672
+ * @param index
673
+ * @param size
674
+ * @param type
675
+ * @param normalized
676
+ * @param stride
677
+ * @param offset
678
+ */
679
+ vertexAttribPointer(
680
+ buffer: WebGLBuffer,
681
+ index: GLuint,
682
+ size: GLint,
683
+ type: GLenum,
684
+ normalized: GLboolean,
685
+ stride: GLsizei,
686
+ offset: GLintptr,
687
+ ) {
688
+ const { gl, boundArrayBuffer } = this;
689
+ if (boundArrayBuffer !== buffer) {
690
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
691
+ this.boundArrayBuffer = buffer;
692
+ }
693
+ gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
694
+ }
695
+
696
+ /**
697
+ * ```
698
+ * gl.useProgram(program);
699
+ * ```
700
+ *
701
+ * @param program
702
+ * @returns
703
+ */
704
+ useProgram(program: WebGLProgram | null) {
705
+ const { gl, curProgram } = this;
706
+ if (curProgram === program) {
707
+ return;
708
+ }
709
+ gl.useProgram(program);
710
+ this.curProgram = program;
711
+ }
712
+
713
+ /**
714
+ * Sets the value of a single float uniform variable.
715
+ *
716
+ * @param location - The location of the uniform variable.
717
+ * @param v0 - The value to set.
718
+ */
719
+ uniform1f(location: WebGLUniformLocation | null, v0: number) {
720
+ const { gl } = this;
721
+ gl.uniform1f(location, v0);
722
+ }
723
+
724
+ /**
725
+ * Sets the value of a float array uniform variable.
726
+ *
727
+ * @param location - The location of the uniform variable.
728
+ * @param value - The array of values to set.
729
+ */
730
+ uniform1fv(
731
+ location: WebGLUniformLocation | null,
732
+ value: Float32Array | number[],
733
+ ) {
734
+ const { gl } = this;
735
+ gl.uniform1fv(location, value);
736
+ }
737
+
738
+ /**
739
+ * Sets the value of a single integer uniform variable.
740
+ *
741
+ * @param location - The location of the uniform variable.
742
+ * @param v0 - The value to set.
743
+ */
744
+ uniform1i(location: WebGLUniformLocation | null, v0: number) {
745
+ const { gl } = this;
746
+ gl.uniform1i(location, v0);
747
+ }
748
+
749
+ /**
750
+ * Sets the value of an integer array uniform variable.
751
+ *
752
+ * @param location - The location of the uniform variable.
753
+ * @param value - The array of values to set.
754
+ */
755
+ uniform1iv(
756
+ location: WebGLUniformLocation | null,
757
+ value: Int32Array | number[],
758
+ ) {
759
+ const { gl } = this;
760
+ gl.uniform1iv(location, value);
761
+ }
762
+
763
+ /**
764
+ * Sets the value of a vec2 uniform variable.
765
+ *
766
+ * @param location - The location of the uniform variable.
767
+ * @param v0 - The first component of the vector.
768
+ * @param v1 - The second component of the vector.
769
+ */
770
+ uniform2f(location: WebGLUniformLocation | null, v0: number, v1: number) {
771
+ const { gl } = this;
772
+ gl.uniform2f(location, v0, v1);
773
+ }
774
+
775
+ /**
776
+ * Sets the value of a vec2 array uniform variable.
777
+ *
778
+ * @param location - The location of the uniform variable.
779
+ * @param value - The array of vec2 values to set.
780
+ */
781
+ uniform2fv(
782
+ location: WebGLUniformLocation | null,
783
+ value: Float32Array | number[],
784
+ ) {
785
+ const { gl } = this;
786
+ gl.uniform2fv(location, value);
787
+ }
788
+
789
+ /**
790
+ * Sets the value of a ivec2 uniform variable.
791
+ *
792
+ * @param location - The location of the uniform variable.
793
+ * @param v0 - The first component of the vector.
794
+ * @param v1 - The second component of the vector.
795
+ */
796
+ uniform2i(location: WebGLUniformLocation | null, v0: number, v1: number) {
797
+ const { gl } = this;
798
+ gl.uniform2i(location, v0, v1);
799
+ }
800
+
801
+ /**
802
+ * Sets the value of an ivec2 array uniform variable.
803
+ *
804
+ * @param location - The location of the uniform variable.
805
+ * @param value - The array of ivec2 values to set.
806
+ */
807
+ uniform2iv(
808
+ location: WebGLUniformLocation | null,
809
+ value: Int32Array | number[],
810
+ ) {
811
+ const { gl } = this;
812
+ gl.uniform2iv(location, value);
813
+ }
814
+
815
+ /**
816
+ * Sets the value of a vec3 uniform variable.
817
+ *
818
+ * @param location - The location of the uniform variable.
819
+ * @param v0 - The first component of the vector.
820
+ * @param v1 - The second component of the vector.
821
+ * @param v2 - The third component of the vector.
822
+ */
823
+ uniform3f(
824
+ location: WebGLUniformLocation | null,
825
+ v0: number,
826
+ v1: number,
827
+ v2: number,
828
+ ) {
829
+ const { gl } = this;
830
+ gl.uniform3f(location, v0, v1, v2);
831
+ }
832
+
833
+ /**
834
+ * Sets the value of a vec3 array uniform variable.
835
+ *
836
+ * @param location - The location of the uniform variable.
837
+ * @param value - The array of vec3 values to set.
838
+ */
839
+ uniform3fv(
840
+ location: WebGLUniformLocation | null,
841
+ value: Float32Array | number[],
842
+ ) {
843
+ const { gl } = this;
844
+ gl.uniform3fv(location, value);
845
+ }
846
+
847
+ /**
848
+ * Sets the value of a ivec3 uniform variable.
849
+ *
850
+ * @param location - The location of the uniform variable.
851
+ * @param v0 - The first component of the vector.
852
+ * @param v1 - The second component of the vector.
853
+ * @param v2 - The third component of the vector.
854
+ */
855
+ uniform3i(
856
+ location: WebGLUniformLocation | null,
857
+ v0: number,
858
+ v1: number,
859
+ v2: number,
860
+ ) {
861
+ const { gl } = this;
862
+ gl.uniform3i(location, v0, v1, v2);
863
+ }
864
+
865
+ /**
866
+ * Sets the value of an ivec3 array uniform variable.
867
+ *
868
+ * @param location - The location of the uniform variable.
869
+ * @param value - The array of ivec3 values to set.
870
+ */
871
+ uniform3iv(
872
+ location: WebGLUniformLocation | null,
873
+ value: Int32Array | number[],
874
+ ) {
875
+ const { gl } = this;
876
+ gl.uniform3iv(location, value);
877
+ }
878
+
879
+ /**
880
+ * Sets the value of a vec4 uniform variable.
881
+ *
882
+ * @param location - The location of the uniform variable.
883
+ * @param v0 - The first component of the vector.
884
+ * @param v1 - The second component of the vector.
885
+ * @param v2 - The third component of the vector.
886
+ * @param v3 - The fourth component of the vector.
887
+ */
888
+ uniform4f(
889
+ location: WebGLUniformLocation | null,
890
+ v0: number,
891
+ v1: number,
892
+ v2: number,
893
+ v3: number,
894
+ ) {
895
+ const { gl } = this;
896
+ gl.uniform4f(location, v0, v1, v2, v3);
897
+ }
898
+
899
+ /**
900
+ * Sets the value of a vec4 array uniform variable.
901
+ *
902
+ * @param location - The location of the uniform variable.
903
+ * @param value - The array of vec4 values to set.
904
+ */
905
+ uniform4fv(
906
+ location: WebGLUniformLocation | null,
907
+ value: Float32Array | number[],
908
+ ) {
909
+ const { gl } = this;
910
+ gl.uniform4fv(location, value);
911
+ }
912
+
913
+ /**
914
+ * Sets the value of a ivec4 uniform variable.
915
+ *
916
+ * @param location - The location of the uniform variable.
917
+ * @param v0 - The first component of the vector.
918
+ * @param v1 - The second component of the vector.
919
+ * @param v2 - The third component of the vector.
920
+ * @param v3 - The fourth component of the vector.
921
+ */
922
+ uniform4i(
923
+ location: WebGLUniformLocation | null,
924
+ v0: number,
925
+ v1: number,
926
+ v2: number,
927
+ v3: number,
928
+ ) {
929
+ const { gl } = this;
930
+ gl.uniform4i(location, v0, v1, v2, v3);
931
+ }
932
+
933
+ /**
934
+ * Sets the value of an ivec4 array uniform variable.
935
+ *
936
+ * @param location - The location of the uniform variable.
937
+ * @param value - The array of ivec4 values to set.
938
+ */
939
+ uniform4iv(
940
+ location: WebGLUniformLocation | null,
941
+ value: Int32Array | number[],
942
+ ) {
943
+ const { gl } = this;
944
+ gl.uniform4iv(location, value);
945
+ }
946
+
947
+ /**
948
+ * Sets the value of a mat2 uniform variable.
949
+ *
950
+ * @param location - The location of the uniform variable.
951
+ * @param transpose - Whether to transpose the matrix.
952
+ * @param value - The array of mat2 values to set.
953
+ */
954
+ uniformMatrix2fv(
955
+ location: WebGLUniformLocation | null,
956
+ value: Float32Array | number[],
957
+ ) {
958
+ const { gl } = this;
959
+ gl.uniformMatrix2fv(location, false, value);
960
+ }
961
+
962
+ /**
963
+ * Sets the value of a mat2 uniform variable.
964
+ * @param location - The location of the uniform variable.
965
+ * @param value - The array of mat2 values to set.
966
+ */
967
+ uniformMatrix3fv(
968
+ location: WebGLUniformLocation | null,
969
+ value: Float32Array | number[],
970
+ ) {
971
+ const { gl } = this;
972
+ gl.uniformMatrix3fv(location, false, value);
973
+ }
974
+
975
+ /**
976
+ * Sets the value of a mat4 uniform variable.
977
+ * @param location - The location of the uniform variable.
978
+ * @param value - The array of mat4 values to set.
979
+ */
980
+ uniformMatrix4fv(
981
+ location: WebGLUniformLocation | null,
982
+ value: Float32Array | number[],
983
+ ) {
984
+ const { gl } = this;
985
+ gl.uniformMatrix4fv(location, false, value);
986
+ }
987
+
988
+ /**
989
+ * ```
990
+ * gl.getParameter(pname);
991
+ * ```
992
+ *
993
+ * @param pname
994
+ * @returns
995
+ */
996
+ getParameter(pname: GLenum): any {
997
+ const { gl } = this;
998
+ return gl.getParameter(pname);
999
+ }
1000
+
1001
+ /**
1002
+ * ```
1003
+ * gl.drawElements(mode, count, type, offset);
1004
+ * ```
1005
+ *
1006
+ * @param mode
1007
+ * @param count
1008
+ * @param type
1009
+ * @param offset
1010
+ */
1011
+ drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
1012
+ const { gl } = this;
1013
+ gl.drawElements(mode, count, type, offset);
1014
+ }
1015
+
1016
+ /**
1017
+ * ```
1018
+ * gl.drawArrays(mode, first, count);
1019
+ * ```
1020
+ *
1021
+ * @param name
1022
+ * @returns
1023
+ */
1024
+ getExtension(name: string) {
1025
+ const { gl } = this;
1026
+ return gl.getExtension(name);
1027
+ }
1028
+
1029
+ /**
1030
+ * ```
1031
+ * gl.getError(type);
1032
+ * ```
1033
+ *
1034
+ * @returns
1035
+ */
1036
+ getError() {
1037
+ const { gl } = this;
1038
+ return gl.getError();
1039
+ }
1040
+
1041
+ /**
1042
+ * ```
1043
+ * gl.createVertexArray();
1044
+ * ```
1045
+ *
1046
+ * @returns
1047
+ */
1048
+ createVertexArray() {
1049
+ const { gl } = this;
1050
+ assertTruthy(gl instanceof WebGL2RenderingContext);
1051
+ return gl.createVertexArray();
1052
+ }
1053
+
1054
+ /**
1055
+ * ```
1056
+ * gl.bindVertexArray(vertexArray);
1057
+ * ```
1058
+ *
1059
+ * @param vertexArray
1060
+ */
1061
+ bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
1062
+ const { gl } = this;
1063
+ assertTruthy(gl instanceof WebGL2RenderingContext);
1064
+ gl.bindVertexArray(vertexArray);
1065
+ }
1066
+
1067
+ /**
1068
+ * ```
1069
+ * gl.getAttribLocation(program, name);
1070
+ * ```
1071
+ *
1072
+ * @param program
1073
+ * @param name
1074
+ * @returns
1075
+ */
1076
+ getAttribLocation(program: WebGLProgram, name: string) {
1077
+ const { gl } = this;
1078
+ return gl.getAttribLocation(program, name);
1079
+ }
1080
+
1081
+ /**
1082
+ * ```
1083
+ * gl.getUniformLocation(program, name);
1084
+ * ```
1085
+ *
1086
+ * @param program
1087
+ * @param name
1088
+ * @returns
1089
+ */
1090
+ getUniformLocation(program: WebGLProgram, name: string) {
1091
+ const { gl } = this;
1092
+ return gl.getUniformLocation(program, name);
1093
+ }
1094
+
1095
+ /**
1096
+ * ```
1097
+ * gl.enableVertexAttribArray(index);
1098
+ * ```
1099
+ *
1100
+ * @param index
1101
+ */
1102
+ enableVertexAttribArray(index: number) {
1103
+ const { gl } = this;
1104
+ gl.enableVertexAttribArray(index);
1105
+ }
1106
+
1107
+ /**
1108
+ * ```
1109
+ * gl.disableVertexAttribArray(index);
1110
+ * ```
1111
+ *
1112
+ * @param index
1113
+ */
1114
+ disableVertexAttribArray(index: number) {
1115
+ const { gl } = this;
1116
+ gl.disableVertexAttribArray(index);
1117
+ }
1118
+
1119
+ /**
1120
+ * ```
1121
+ * gl.createShader(type);
1122
+ * ```
1123
+ *
1124
+ * @param type
1125
+ * @returns
1126
+ */
1127
+ createShader(type: number) {
1128
+ const { gl } = this;
1129
+ return gl.createShader(type);
1130
+ }
1131
+
1132
+ /**
1133
+ * ```
1134
+ * gl.compileShader(shader);
1135
+ * ```
1136
+ *
1137
+ * @param shader
1138
+ * @returns
1139
+ */
1140
+ compileShader(shader: WebGLShader) {
1141
+ const { gl } = this;
1142
+ gl.compileShader(shader);
1143
+ }
1144
+
1145
+ /**
1146
+ * ```
1147
+ * gl.attachShader(program, shader);
1148
+ * ```
1149
+ *
1150
+ * @param program
1151
+ * @param shader
1152
+ */
1153
+ attachShader(program: WebGLProgram, shader: WebGLShader) {
1154
+ const { gl } = this;
1155
+ gl.attachShader(program, shader);
1156
+ }
1157
+
1158
+ /**
1159
+ * ```
1160
+ * gl.linkProgram(program);
1161
+ * ```
1162
+ *
1163
+ * @param program
1164
+ */
1165
+ linkProgram(program: WebGLProgram) {
1166
+ const { gl } = this;
1167
+ gl.linkProgram(program);
1168
+ }
1169
+
1170
+ /**
1171
+ * ```
1172
+ * gl.deleteProgram(shader);
1173
+ * ```
1174
+ *
1175
+ * @param shader
1176
+ */
1177
+ deleteProgram(shader: WebGLProgram) {
1178
+ const { gl } = this;
1179
+ gl.deleteProgram(shader);
1180
+ }
1181
+
1182
+ /**
1183
+ * ```
1184
+ * gl.getShaderParameter(shader, pname);
1185
+ * ```
1186
+ *
1187
+ * @param shader
1188
+ * @param pname
1189
+ */
1190
+ getShaderParameter(shader: WebGLShader, pname: GLenum) {
1191
+ const { gl } = this;
1192
+ return gl.getShaderParameter(shader, pname);
1193
+ }
1194
+
1195
+ /**
1196
+ * ```
1197
+ * gl.getShaderInfoLog(shader);
1198
+ * ```
1199
+ *
1200
+ * @param shader
1201
+ */
1202
+ getShaderInfoLog(shader: WebGLShader) {
1203
+ const { gl } = this;
1204
+ return gl.getShaderInfoLog(shader);
1205
+ }
1206
+
1207
+ /**
1208
+ * ```
1209
+ * gl.createProgram();
1210
+ * ```
1211
+ *
1212
+ * @returns
1213
+ */
1214
+ createProgram() {
1215
+ const { gl } = this;
1216
+ return gl.createProgram();
1217
+ }
1218
+
1219
+ /**
1220
+ * ```
1221
+ * gl.getProgramParameter(program, pname);
1222
+ * ```
1223
+ *
1224
+ * @param program
1225
+ * @param pname
1226
+ * @returns
1227
+ */
1228
+ getProgramParameter(program: WebGLProgram, pname: GLenum) {
1229
+ const { gl } = this;
1230
+ return gl.getProgramParameter(program, pname);
1231
+ }
1232
+
1233
+ /**
1234
+ * ```
1235
+ * gl.getProgramInfoLog(program);
1236
+ * ```
1237
+ *
1238
+ * @param program
1239
+ * @returns
1240
+ */
1241
+ getProgramInfoLog(program: WebGLProgram) {
1242
+ const { gl } = this;
1243
+ return gl.getProgramInfoLog(program);
1244
+ }
1245
+
1246
+ /**
1247
+ * ```
1248
+ * gl.shaderSource(shader, source);
1249
+ * ```
1250
+ *
1251
+ * @param shader
1252
+ * @param source
1253
+ */
1254
+ shaderSource(shader: WebGLShader, source: string) {
1255
+ const { gl } = this;
1256
+ gl.shaderSource(shader, source);
1257
+ }
1258
+
1259
+ /**
1260
+ * ```
1261
+ * gl.deleteShader(shader);
1262
+ * ```
1263
+ *
1264
+ * @param shader
1265
+ */
1266
+ deleteShader(shader: WebGLShader) {
1267
+ const { gl } = this;
1268
+ gl.deleteShader(shader);
1269
+ }
1270
+ }
1271
+
1272
+ // prettier-ignore
1273
+ type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
1274
+ ?
1275
+ MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
1276
+ ? true
1277
+ : false
1278
+ : false;
1279
+
1280
+ // prettier-ignore
1281
+ export type UniformMethodMap = {
1282
+ [Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
1283
+ location: WebGLUniformLocation | null,
1284
+ ...args: infer T
1285
+ ) => void
1286
+ ? T
1287
+ : never;
1288
+ };
1289
+
1290
+ /**
1291
+ * Compare two arrays for equality.
1292
+ *
1293
+ * @remarks
1294
+ * This function will not try to compare nested arrays or Float32Arrays and
1295
+ * instead will always return false when they are encountered.
1296
+ *
1297
+ * @param a
1298
+ * @param b
1299
+ * @returns
1300
+ */
1301
+ export function compareArrays<T>(a: T[], b: T[]): boolean {
1302
+ if (a.length !== b.length) {
1303
+ return false;
1304
+ }
1305
+
1306
+ let result = false;
1307
+ for (let i = 0; i < a.length; i++) {
1308
+ if (Array.isArray(a[i]) || a[i] instanceof Float32Array) {
1309
+ result = false;
1310
+ break;
1311
+ }
1312
+
1313
+ if (a[i] !== b[i]) {
1314
+ result = false;
1315
+ break;
1316
+ }
1317
+
1318
+ result = true;
1319
+ }
1320
+
1321
+ return result;
1322
+ }