@lightningjs/renderer 2.13.0 → 2.13.1

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 (213) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +147 -147
  4. package/dist/exports/core-api.d.ts +74 -0
  5. package/dist/exports/core-api.js +96 -0
  6. package/dist/exports/core-api.js.map +1 -0
  7. package/dist/exports/main-api.d.ts +30 -0
  8. package/dist/exports/main-api.js +45 -0
  9. package/dist/exports/main-api.js.map +1 -0
  10. package/dist/src/core/CoreExtension.d.ts +12 -0
  11. package/dist/src/core/CoreExtension.js +29 -0
  12. package/dist/src/core/CoreExtension.js.map +1 -0
  13. package/dist/src/core/lib/ImageWorker.js +2 -1
  14. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  15. package/dist/src/core/lib/utils.d.ts +4 -0
  16. package/dist/src/core/lib/utils.js +46 -0
  17. package/dist/src/core/lib/utils.js.map +1 -1
  18. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  19. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  20. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  21. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  22. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  23. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  24. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  25. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  26. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  27. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  28. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  29. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  30. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  31. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  32. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  33. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  34. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  35. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  36. package/dist/src/core/textures/ImageTexture.d.ts +2 -2
  37. package/dist/src/core/textures/ImageTexture.js +24 -4
  38. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  39. package/dist/src/main-api/ICoreDriver.d.ts +24 -0
  40. package/dist/src/main-api/ICoreDriver.js +20 -0
  41. package/dist/src/main-api/ICoreDriver.js.map +1 -0
  42. package/dist/src/main-api/RendererMain.d.ts +378 -0
  43. package/dist/src/main-api/RendererMain.js +367 -0
  44. package/dist/src/main-api/RendererMain.js.map +1 -0
  45. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +9 -0
  46. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +38 -0
  47. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +1 -0
  48. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +56 -0
  49. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +101 -0
  50. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +1 -0
  51. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +32 -0
  52. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js +28 -0
  53. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +1 -0
  54. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +21 -0
  55. package/dist/src/render-drivers/main/MainCoreDriver.js +115 -0
  56. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -0
  57. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +101 -0
  58. package/dist/src/render-drivers/main/MainOnlyNode.js +425 -0
  59. package/dist/src/render-drivers/main/MainOnlyNode.js.map +1 -0
  60. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +47 -0
  61. package/dist/src/render-drivers/main/MainOnlyTextNode.js +204 -0
  62. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +1 -0
  63. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +93 -0
  64. package/dist/src/render-drivers/threadx/NodeStruct.js +290 -0
  65. package/dist/src/render-drivers/threadx/NodeStruct.js.map +1 -0
  66. package/dist/src/render-drivers/threadx/SharedNode.d.ts +40 -0
  67. package/dist/src/render-drivers/threadx/SharedNode.js +61 -0
  68. package/dist/src/render-drivers/threadx/SharedNode.js.map +1 -0
  69. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +44 -0
  70. package/dist/src/render-drivers/threadx/TextNodeStruct.js +203 -0
  71. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +1 -0
  72. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +25 -0
  73. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +232 -0
  74. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -0
  75. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +24 -0
  76. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +113 -0
  77. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +1 -0
  78. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +46 -0
  79. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +160 -0
  80. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +1 -0
  81. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +28 -0
  82. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +55 -0
  83. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +1 -0
  84. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +70 -0
  85. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +32 -0
  86. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -0
  87. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +19 -0
  88. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +184 -0
  89. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +1 -0
  90. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +27 -0
  91. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +109 -0
  92. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +1 -0
  93. package/dist/src/render-drivers/threadx/worker/renderer.d.ts +1 -0
  94. package/dist/src/render-drivers/threadx/worker/renderer.js +147 -0
  95. package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -0
  96. package/dist/src/render-drivers/utils.d.ts +12 -0
  97. package/dist/src/render-drivers/utils.js +74 -0
  98. package/dist/src/render-drivers/utils.js.map +1 -0
  99. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  100. package/dist/tsconfig.tsbuildinfo +1 -0
  101. package/exports/canvas.ts +39 -39
  102. package/exports/index.ts +89 -89
  103. package/exports/inspector.ts +24 -24
  104. package/exports/utils.ts +44 -44
  105. package/exports/webgl.ts +38 -38
  106. package/package.json +2 -1
  107. package/scripts/please-use-pnpm.js +13 -13
  108. package/src/common/CommonTypes.ts +146 -146
  109. package/src/common/EventEmitter.ts +77 -77
  110. package/src/common/IAnimationController.ts +92 -92
  111. package/src/common/IEventEmitter.ts +28 -28
  112. package/src/core/CoreNode.test.ts +202 -202
  113. package/src/core/CoreNode.ts +2455 -2455
  114. package/src/core/CoreShaderManager.ts +292 -292
  115. package/src/core/CoreTextNode.ts +455 -455
  116. package/src/core/CoreTextureManager.ts +597 -597
  117. package/src/core/Stage.ts +743 -743
  118. package/src/core/TextureMemoryManager.ts +395 -395
  119. package/src/core/animations/AnimationManager.ts +38 -38
  120. package/src/core/animations/CoreAnimation.ts +340 -340
  121. package/src/core/animations/CoreAnimationController.ts +157 -157
  122. package/src/core/lib/ContextSpy.ts +41 -41
  123. package/src/core/lib/ImageWorker.ts +280 -279
  124. package/src/core/lib/Matrix3d.ts +244 -244
  125. package/src/core/lib/RenderCoords.ts +86 -86
  126. package/src/core/lib/WebGlContextWrapper.ts +1332 -1332
  127. package/src/core/lib/textureCompression.ts +152 -152
  128. package/src/core/lib/textureSvg.ts +78 -78
  129. package/src/core/lib/utils.ts +384 -310
  130. package/src/core/lib/validateImageBitmap.ts +76 -76
  131. package/src/core/platform.ts +63 -63
  132. package/src/core/renderers/CoreContextTexture.ts +43 -43
  133. package/src/core/renderers/CoreRenderOp.ts +22 -22
  134. package/src/core/renderers/CoreRenderer.ts +115 -115
  135. package/src/core/renderers/CoreShader.ts +41 -41
  136. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +375 -375
  137. package/src/core/renderers/canvas/CanvasCoreTexture.ts +153 -153
  138. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  139. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  140. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  141. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +86 -86
  142. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  143. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +301 -301
  144. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  145. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +815 -815
  146. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  147. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  148. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  149. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  150. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  151. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  152. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  153. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  154. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  155. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  156. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  157. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  158. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  159. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  160. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  161. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  162. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  163. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  164. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  165. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  166. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  167. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  168. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  169. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  170. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  171. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  172. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  173. package/src/core/text-rendering/TrFontManager.ts +183 -183
  174. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  175. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  176. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  177. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  178. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  179. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  180. package/src/core/text-rendering/font-face-types/utils.ts +39 -39
  181. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  182. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +808 -808
  183. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  184. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  185. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  186. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  187. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  188. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  189. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  190. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  191. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +408 -408
  192. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  193. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  194. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  195. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  196. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  197. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  198. package/src/core/textures/ColorTexture.ts +102 -102
  199. package/src/core/textures/ImageTexture.ts +399 -376
  200. package/src/core/textures/NoiseTexture.ts +104 -104
  201. package/src/core/textures/RenderTexture.ts +85 -85
  202. package/src/core/textures/SubTexture.ts +205 -205
  203. package/src/core/textures/Texture.ts +337 -337
  204. package/src/core/utils.ts +227 -227
  205. package/src/env.d.ts +7 -7
  206. package/src/main-api/DynamicShaderController.ts +104 -104
  207. package/src/main-api/INode.ts +101 -101
  208. package/src/main-api/Inspector.ts +522 -522
  209. package/src/main-api/Renderer.ts +751 -751
  210. package/src/main-api/ShaderController.ts +80 -80
  211. package/src/main-api/utils.ts +45 -45
  212. package/src/utils.ts +248 -248
  213. package/COPYING +0 -1
@@ -1,1332 +1,1332 @@
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.deleteFramebuffer(framebuffer);
428
- *
429
- * @param framebuffer
430
- */
431
- deleteFramebuffer(framebuffer: WebGLFramebuffer | null) {
432
- this.gl.deleteFramebuffer(framebuffer);
433
- }
434
-
435
- /**
436
- * ```
437
- * gl.viewport(x, y, width, height);
438
- * ```
439
- */
440
- viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
441
- const { gl } = this;
442
- gl.viewport(x, y, width, height);
443
- }
444
-
445
- /**
446
- * ```
447
- * gl.clearColor(red, green, blue, alpha);
448
- * ```
449
- *
450
- * @param red
451
- * @param green
452
- * @param blue
453
- * @param alpha
454
- */
455
- clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
456
- const { gl } = this;
457
- gl.clearColor(red, green, blue, alpha);
458
- }
459
-
460
- /**
461
- * ```
462
- * gl["enable"|"disable"](gl.SCISSOR_TEST);
463
- * ```
464
- * @param enable
465
- */
466
- setScissorTest(enable: boolean) {
467
- const { gl, scissorEnabled } = this;
468
- if (enable === scissorEnabled) {
469
- return;
470
- }
471
- if (enable) {
472
- gl.enable(gl.SCISSOR_TEST);
473
- } else {
474
- gl.disable(gl.SCISSOR_TEST);
475
- }
476
- this.scissorEnabled = enable;
477
- }
478
-
479
- /**
480
- * ```
481
- * gl.scissor(x, y, width, height);
482
- * ```
483
- *
484
- * @param x
485
- * @param y
486
- * @param width
487
- * @param height
488
- */
489
- scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
490
- const { gl, scissorX, scissorY, scissorWidth, scissorHeight } = this;
491
- if (
492
- x !== scissorX ||
493
- y !== scissorY ||
494
- width !== scissorWidth ||
495
- height !== scissorHeight
496
- ) {
497
- gl.scissor(x, y, width, height);
498
- this.scissorX = x;
499
- this.scissorY = y;
500
- this.scissorWidth = width;
501
- this.scissorHeight = height;
502
- }
503
- }
504
-
505
- /**
506
- * ```
507
- * gl["enable"|"disable"](gl.BLEND);
508
- * ```
509
- *
510
- * @param blend
511
- * @returns
512
- */
513
- setBlend(blend: boolean) {
514
- const { gl, blendEnabled } = this;
515
- if (blend === blendEnabled) {
516
- return;
517
- }
518
- if (blend) {
519
- gl.enable(gl.BLEND);
520
- } else {
521
- gl.disable(gl.BLEND);
522
- }
523
- this.blendEnabled = blend;
524
- }
525
-
526
- /**
527
- * ```
528
- * gl.blendFunc(src, dst);
529
- * ```
530
- *
531
- * @param src
532
- * @param dst
533
- */
534
- blendFunc(src: GLenum, dst: GLenum) {
535
- const { gl, blendSrcRgb, blendDstRgb, blendSrcAlpha, blendDstAlpha } = this;
536
- if (
537
- src !== blendSrcRgb ||
538
- dst !== blendDstRgb ||
539
- src !== blendSrcAlpha ||
540
- dst !== blendDstAlpha
541
- ) {
542
- gl.blendFunc(src, dst);
543
- this.blendSrcRgb = src;
544
- this.blendDstRgb = dst;
545
- this.blendSrcAlpha = src;
546
- this.blendDstAlpha = dst;
547
- }
548
- }
549
-
550
- /**
551
- * ```
552
- * gl.createBuffer();
553
- * ```
554
- *
555
- * @returns
556
- */
557
- createBuffer() {
558
- const { gl } = this;
559
- return gl.createBuffer();
560
- }
561
-
562
- /**
563
- * ```
564
- * gl.createFramebuffer();
565
- * ```
566
- * @returns
567
- */
568
- createFramebuffer() {
569
- const { gl } = this;
570
- return gl.createFramebuffer();
571
- }
572
-
573
- /**
574
- * ```
575
- * gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
576
- * ```
577
- *
578
- * @param framebuffer
579
- */
580
- bindFramebuffer(framebuffer: WebGLFramebuffer | null) {
581
- const { gl } = this;
582
- gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
583
- }
584
-
585
- /**
586
- * ```
587
- * gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
588
- * ```
589
- * @remarks
590
- * **WebGL Difference**: Bind target is always `gl.FRAMEBUFFER` and textarget is always `gl.TEXTURE_2D`
591
- */
592
-
593
- framebufferTexture2D(
594
- attachment: GLenum,
595
- texture: WebGLTexture | null,
596
- level: GLint,
597
- ) {
598
- const { gl } = this;
599
- gl.framebufferTexture2D(
600
- gl.FRAMEBUFFER,
601
- attachment,
602
- gl.TEXTURE_2D,
603
- texture,
604
- level,
605
- );
606
- }
607
-
608
- /**
609
- * ```
610
- * gl.clear(gl.COLOR_BUFFER_BIT);
611
- * ```
612
- *
613
- * @remarks
614
- * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
615
- */
616
- clear() {
617
- const { gl } = this;
618
- gl.clear(gl.COLOR_BUFFER_BIT);
619
- }
620
-
621
- /**
622
- * ```
623
- * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
624
- * gl.bufferData(gl.ARRAY_BUFFER, data, usage);
625
- * ```
626
- *
627
- * @remarks
628
- * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
629
- *
630
- * @param buffer
631
- * @param data
632
- * @param usage
633
- */
634
- arrayBufferData(
635
- buffer: WebGLBuffer | null,
636
- data: ArrayBufferView,
637
- usage: GLenum,
638
- ) {
639
- const { gl, boundArrayBuffer } = this;
640
- if (boundArrayBuffer !== buffer) {
641
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
642
- this.boundArrayBuffer = buffer;
643
- }
644
- gl.bufferData(gl.ARRAY_BUFFER, data, usage);
645
- }
646
-
647
- /**
648
- * ```
649
- * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
650
- * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
651
- * ```
652
- * @remarks
653
- * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
654
- *
655
- * @param buffer
656
- * @param data
657
- * @param usage
658
- */
659
- elementArrayBufferData(
660
- buffer: WebGLBuffer | null,
661
- data: ArrayBufferView,
662
- usage: GLenum,
663
- ) {
664
- const { gl, boundElementArrayBuffer } = this;
665
- if (boundElementArrayBuffer !== buffer) {
666
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
667
- this.boundElementArrayBuffer = buffer;
668
- }
669
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
670
- }
671
-
672
- /**
673
- * ```
674
- * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
675
- * gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
676
- * ```
677
- *
678
- * @remarks
679
- * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
680
- *
681
- * @param buffer
682
- * @param index
683
- * @param size
684
- * @param type
685
- * @param normalized
686
- * @param stride
687
- * @param offset
688
- */
689
- vertexAttribPointer(
690
- buffer: WebGLBuffer,
691
- index: GLuint,
692
- size: GLint,
693
- type: GLenum,
694
- normalized: GLboolean,
695
- stride: GLsizei,
696
- offset: GLintptr,
697
- ) {
698
- const { gl, boundArrayBuffer } = this;
699
- if (boundArrayBuffer !== buffer) {
700
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
701
- this.boundArrayBuffer = buffer;
702
- }
703
- gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
704
- }
705
-
706
- /**
707
- * ```
708
- * gl.useProgram(program);
709
- * ```
710
- *
711
- * @param program
712
- * @returns
713
- */
714
- useProgram(program: WebGLProgram | null) {
715
- const { gl, curProgram } = this;
716
- if (curProgram === program) {
717
- return;
718
- }
719
- gl.useProgram(program);
720
- this.curProgram = program;
721
- }
722
-
723
- /**
724
- * Sets the value of a single float uniform variable.
725
- *
726
- * @param location - The location of the uniform variable.
727
- * @param v0 - The value to set.
728
- */
729
- uniform1f(location: WebGLUniformLocation | null, v0: number) {
730
- const { gl } = this;
731
- gl.uniform1f(location, v0);
732
- }
733
-
734
- /**
735
- * Sets the value of a float array uniform variable.
736
- *
737
- * @param location - The location of the uniform variable.
738
- * @param value - The array of values to set.
739
- */
740
- uniform1fv(
741
- location: WebGLUniformLocation | null,
742
- value: Float32Array | number[],
743
- ) {
744
- const { gl } = this;
745
- gl.uniform1fv(location, value);
746
- }
747
-
748
- /**
749
- * Sets the value of a single integer uniform variable.
750
- *
751
- * @param location - The location of the uniform variable.
752
- * @param v0 - The value to set.
753
- */
754
- uniform1i(location: WebGLUniformLocation | null, v0: number) {
755
- const { gl } = this;
756
- gl.uniform1i(location, v0);
757
- }
758
-
759
- /**
760
- * Sets the value of an integer array uniform variable.
761
- *
762
- * @param location - The location of the uniform variable.
763
- * @param value - The array of values to set.
764
- */
765
- uniform1iv(
766
- location: WebGLUniformLocation | null,
767
- value: Int32Array | number[],
768
- ) {
769
- const { gl } = this;
770
- gl.uniform1iv(location, value);
771
- }
772
-
773
- /**
774
- * Sets the value of a vec2 uniform variable.
775
- *
776
- * @param location - The location of the uniform variable.
777
- * @param v0 - The first component of the vector.
778
- * @param v1 - The second component of the vector.
779
- */
780
- uniform2f(location: WebGLUniformLocation | null, v0: number, v1: number) {
781
- const { gl } = this;
782
- gl.uniform2f(location, v0, v1);
783
- }
784
-
785
- /**
786
- * Sets the value of a vec2 array uniform variable.
787
- *
788
- * @param location - The location of the uniform variable.
789
- * @param value - The array of vec2 values to set.
790
- */
791
- uniform2fv(
792
- location: WebGLUniformLocation | null,
793
- value: Float32Array | number[],
794
- ) {
795
- const { gl } = this;
796
- gl.uniform2fv(location, value);
797
- }
798
-
799
- /**
800
- * Sets the value of a ivec2 uniform variable.
801
- *
802
- * @param location - The location of the uniform variable.
803
- * @param v0 - The first component of the vector.
804
- * @param v1 - The second component of the vector.
805
- */
806
- uniform2i(location: WebGLUniformLocation | null, v0: number, v1: number) {
807
- const { gl } = this;
808
- gl.uniform2i(location, v0, v1);
809
- }
810
-
811
- /**
812
- * Sets the value of an ivec2 array uniform variable.
813
- *
814
- * @param location - The location of the uniform variable.
815
- * @param value - The array of ivec2 values to set.
816
- */
817
- uniform2iv(
818
- location: WebGLUniformLocation | null,
819
- value: Int32Array | number[],
820
- ) {
821
- const { gl } = this;
822
- gl.uniform2iv(location, value);
823
- }
824
-
825
- /**
826
- * Sets the value of a vec3 uniform variable.
827
- *
828
- * @param location - The location of the uniform variable.
829
- * @param v0 - The first component of the vector.
830
- * @param v1 - The second component of the vector.
831
- * @param v2 - The third component of the vector.
832
- */
833
- uniform3f(
834
- location: WebGLUniformLocation | null,
835
- v0: number,
836
- v1: number,
837
- v2: number,
838
- ) {
839
- const { gl } = this;
840
- gl.uniform3f(location, v0, v1, v2);
841
- }
842
-
843
- /**
844
- * Sets the value of a vec3 array uniform variable.
845
- *
846
- * @param location - The location of the uniform variable.
847
- * @param value - The array of vec3 values to set.
848
- */
849
- uniform3fv(
850
- location: WebGLUniformLocation | null,
851
- value: Float32Array | number[],
852
- ) {
853
- const { gl } = this;
854
- gl.uniform3fv(location, value);
855
- }
856
-
857
- /**
858
- * Sets the value of a ivec3 uniform variable.
859
- *
860
- * @param location - The location of the uniform variable.
861
- * @param v0 - The first component of the vector.
862
- * @param v1 - The second component of the vector.
863
- * @param v2 - The third component of the vector.
864
- */
865
- uniform3i(
866
- location: WebGLUniformLocation | null,
867
- v0: number,
868
- v1: number,
869
- v2: number,
870
- ) {
871
- const { gl } = this;
872
- gl.uniform3i(location, v0, v1, v2);
873
- }
874
-
875
- /**
876
- * Sets the value of an ivec3 array uniform variable.
877
- *
878
- * @param location - The location of the uniform variable.
879
- * @param value - The array of ivec3 values to set.
880
- */
881
- uniform3iv(
882
- location: WebGLUniformLocation | null,
883
- value: Int32Array | number[],
884
- ) {
885
- const { gl } = this;
886
- gl.uniform3iv(location, value);
887
- }
888
-
889
- /**
890
- * Sets the value of a vec4 uniform variable.
891
- *
892
- * @param location - The location of the uniform variable.
893
- * @param v0 - The first component of the vector.
894
- * @param v1 - The second component of the vector.
895
- * @param v2 - The third component of the vector.
896
- * @param v3 - The fourth component of the vector.
897
- */
898
- uniform4f(
899
- location: WebGLUniformLocation | null,
900
- v0: number,
901
- v1: number,
902
- v2: number,
903
- v3: number,
904
- ) {
905
- const { gl } = this;
906
- gl.uniform4f(location, v0, v1, v2, v3);
907
- }
908
-
909
- /**
910
- * Sets the value of a vec4 array uniform variable.
911
- *
912
- * @param location - The location of the uniform variable.
913
- * @param value - The array of vec4 values to set.
914
- */
915
- uniform4fv(
916
- location: WebGLUniformLocation | null,
917
- value: Float32Array | number[],
918
- ) {
919
- const { gl } = this;
920
- gl.uniform4fv(location, value);
921
- }
922
-
923
- /**
924
- * Sets the value of a ivec4 uniform variable.
925
- *
926
- * @param location - The location of the uniform variable.
927
- * @param v0 - The first component of the vector.
928
- * @param v1 - The second component of the vector.
929
- * @param v2 - The third component of the vector.
930
- * @param v3 - The fourth component of the vector.
931
- */
932
- uniform4i(
933
- location: WebGLUniformLocation | null,
934
- v0: number,
935
- v1: number,
936
- v2: number,
937
- v3: number,
938
- ) {
939
- const { gl } = this;
940
- gl.uniform4i(location, v0, v1, v2, v3);
941
- }
942
-
943
- /**
944
- * Sets the value of an ivec4 array uniform variable.
945
- *
946
- * @param location - The location of the uniform variable.
947
- * @param value - The array of ivec4 values to set.
948
- */
949
- uniform4iv(
950
- location: WebGLUniformLocation | null,
951
- value: Int32Array | number[],
952
- ) {
953
- const { gl } = this;
954
- gl.uniform4iv(location, value);
955
- }
956
-
957
- /**
958
- * Sets the value of a mat2 uniform variable.
959
- *
960
- * @param location - The location of the uniform variable.
961
- * @param transpose - Whether to transpose the matrix.
962
- * @param value - The array of mat2 values to set.
963
- */
964
- uniformMatrix2fv(
965
- location: WebGLUniformLocation | null,
966
- value: Float32Array | number[],
967
- ) {
968
- const { gl } = this;
969
- gl.uniformMatrix2fv(location, false, value);
970
- }
971
-
972
- /**
973
- * Sets the value of a mat2 uniform variable.
974
- * @param location - The location of the uniform variable.
975
- * @param value - The array of mat2 values to set.
976
- */
977
- uniformMatrix3fv(
978
- location: WebGLUniformLocation | null,
979
- value: Float32Array | number[],
980
- ) {
981
- const { gl } = this;
982
- gl.uniformMatrix3fv(location, false, value);
983
- }
984
-
985
- /**
986
- * Sets the value of a mat4 uniform variable.
987
- * @param location - The location of the uniform variable.
988
- * @param value - The array of mat4 values to set.
989
- */
990
- uniformMatrix4fv(
991
- location: WebGLUniformLocation | null,
992
- value: Float32Array | number[],
993
- ) {
994
- const { gl } = this;
995
- gl.uniformMatrix4fv(location, false, value);
996
- }
997
-
998
- /**
999
- * ```
1000
- * gl.getParameter(pname);
1001
- * ```
1002
- *
1003
- * @param pname
1004
- * @returns
1005
- */
1006
- getParameter(pname: GLenum): any {
1007
- const { gl } = this;
1008
- return gl.getParameter(pname);
1009
- }
1010
-
1011
- /**
1012
- * ```
1013
- * gl.drawElements(mode, count, type, offset);
1014
- * ```
1015
- *
1016
- * @param mode
1017
- * @param count
1018
- * @param type
1019
- * @param offset
1020
- */
1021
- drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
1022
- const { gl } = this;
1023
- gl.drawElements(mode, count, type, offset);
1024
- }
1025
-
1026
- /**
1027
- * ```
1028
- * gl.drawArrays(mode, first, count);
1029
- * ```
1030
- *
1031
- * @param name
1032
- * @returns
1033
- */
1034
- getExtension(name: string) {
1035
- const { gl } = this;
1036
- return gl.getExtension(name);
1037
- }
1038
-
1039
- /**
1040
- * ```
1041
- * gl.getError(type);
1042
- * ```
1043
- *
1044
- * @returns
1045
- */
1046
- getError() {
1047
- const { gl } = this;
1048
- return gl.getError();
1049
- }
1050
-
1051
- /**
1052
- * ```
1053
- * gl.createVertexArray();
1054
- * ```
1055
- *
1056
- * @returns
1057
- */
1058
- createVertexArray() {
1059
- const { gl } = this;
1060
- assertTruthy(gl instanceof WebGL2RenderingContext);
1061
- return gl.createVertexArray();
1062
- }
1063
-
1064
- /**
1065
- * ```
1066
- * gl.bindVertexArray(vertexArray);
1067
- * ```
1068
- *
1069
- * @param vertexArray
1070
- */
1071
- bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
1072
- const { gl } = this;
1073
- assertTruthy(gl instanceof WebGL2RenderingContext);
1074
- gl.bindVertexArray(vertexArray);
1075
- }
1076
-
1077
- /**
1078
- * ```
1079
- * gl.getAttribLocation(program, name);
1080
- * ```
1081
- *
1082
- * @param program
1083
- * @param name
1084
- * @returns
1085
- */
1086
- getAttribLocation(program: WebGLProgram, name: string) {
1087
- const { gl } = this;
1088
- return gl.getAttribLocation(program, name);
1089
- }
1090
-
1091
- /**
1092
- * ```
1093
- * gl.getUniformLocation(program, name);
1094
- * ```
1095
- *
1096
- * @param program
1097
- * @param name
1098
- * @returns
1099
- */
1100
- getUniformLocation(program: WebGLProgram, name: string) {
1101
- const { gl } = this;
1102
- return gl.getUniformLocation(program, name);
1103
- }
1104
-
1105
- /**
1106
- * ```
1107
- * gl.enableVertexAttribArray(index);
1108
- * ```
1109
- *
1110
- * @param index
1111
- */
1112
- enableVertexAttribArray(index: number) {
1113
- const { gl } = this;
1114
- gl.enableVertexAttribArray(index);
1115
- }
1116
-
1117
- /**
1118
- * ```
1119
- * gl.disableVertexAttribArray(index);
1120
- * ```
1121
- *
1122
- * @param index
1123
- */
1124
- disableVertexAttribArray(index: number) {
1125
- const { gl } = this;
1126
- gl.disableVertexAttribArray(index);
1127
- }
1128
-
1129
- /**
1130
- * ```
1131
- * gl.createShader(type);
1132
- * ```
1133
- *
1134
- * @param type
1135
- * @returns
1136
- */
1137
- createShader(type: number) {
1138
- const { gl } = this;
1139
- return gl.createShader(type);
1140
- }
1141
-
1142
- /**
1143
- * ```
1144
- * gl.compileShader(shader);
1145
- * ```
1146
- *
1147
- * @param shader
1148
- * @returns
1149
- */
1150
- compileShader(shader: WebGLShader) {
1151
- const { gl } = this;
1152
- gl.compileShader(shader);
1153
- }
1154
-
1155
- /**
1156
- * ```
1157
- * gl.attachShader(program, shader);
1158
- * ```
1159
- *
1160
- * @param program
1161
- * @param shader
1162
- */
1163
- attachShader(program: WebGLProgram, shader: WebGLShader) {
1164
- const { gl } = this;
1165
- gl.attachShader(program, shader);
1166
- }
1167
-
1168
- /**
1169
- * ```
1170
- * gl.linkProgram(program);
1171
- * ```
1172
- *
1173
- * @param program
1174
- */
1175
- linkProgram(program: WebGLProgram) {
1176
- const { gl } = this;
1177
- gl.linkProgram(program);
1178
- }
1179
-
1180
- /**
1181
- * ```
1182
- * gl.deleteProgram(shader);
1183
- * ```
1184
- *
1185
- * @param shader
1186
- */
1187
- deleteProgram(shader: WebGLProgram) {
1188
- const { gl } = this;
1189
- gl.deleteProgram(shader);
1190
- }
1191
-
1192
- /**
1193
- * ```
1194
- * gl.getShaderParameter(shader, pname);
1195
- * ```
1196
- *
1197
- * @param shader
1198
- * @param pname
1199
- */
1200
- getShaderParameter(shader: WebGLShader, pname: GLenum) {
1201
- const { gl } = this;
1202
- return gl.getShaderParameter(shader, pname);
1203
- }
1204
-
1205
- /**
1206
- * ```
1207
- * gl.getShaderInfoLog(shader);
1208
- * ```
1209
- *
1210
- * @param shader
1211
- */
1212
- getShaderInfoLog(shader: WebGLShader) {
1213
- const { gl } = this;
1214
- return gl.getShaderInfoLog(shader);
1215
- }
1216
-
1217
- /**
1218
- * ```
1219
- * gl.createProgram();
1220
- * ```
1221
- *
1222
- * @returns
1223
- */
1224
- createProgram() {
1225
- const { gl } = this;
1226
- return gl.createProgram();
1227
- }
1228
-
1229
- /**
1230
- * ```
1231
- * gl.getProgramParameter(program, pname);
1232
- * ```
1233
- *
1234
- * @param program
1235
- * @param pname
1236
- * @returns
1237
- */
1238
- getProgramParameter(program: WebGLProgram, pname: GLenum) {
1239
- const { gl } = this;
1240
- return gl.getProgramParameter(program, pname);
1241
- }
1242
-
1243
- /**
1244
- * ```
1245
- * gl.getProgramInfoLog(program);
1246
- * ```
1247
- *
1248
- * @param program
1249
- * @returns
1250
- */
1251
- getProgramInfoLog(program: WebGLProgram) {
1252
- const { gl } = this;
1253
- return gl.getProgramInfoLog(program);
1254
- }
1255
-
1256
- /**
1257
- * ```
1258
- * gl.shaderSource(shader, source);
1259
- * ```
1260
- *
1261
- * @param shader
1262
- * @param source
1263
- */
1264
- shaderSource(shader: WebGLShader, source: string) {
1265
- const { gl } = this;
1266
- gl.shaderSource(shader, source);
1267
- }
1268
-
1269
- /**
1270
- * ```
1271
- * gl.deleteShader(shader);
1272
- * ```
1273
- *
1274
- * @param shader
1275
- */
1276
- deleteShader(shader: WebGLShader) {
1277
- const { gl } = this;
1278
- gl.deleteShader(shader);
1279
- }
1280
- }
1281
-
1282
- // prettier-ignore
1283
- type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
1284
- ?
1285
- MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
1286
- ? true
1287
- : false
1288
- : false;
1289
-
1290
- // prettier-ignore
1291
- export type UniformMethodMap = {
1292
- [Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
1293
- location: WebGLUniformLocation | null,
1294
- ...args: infer T
1295
- ) => void
1296
- ? T
1297
- : never;
1298
- };
1299
-
1300
- /**
1301
- * Compare two arrays for equality.
1302
- *
1303
- * @remarks
1304
- * This function will not try to compare nested arrays or Float32Arrays and
1305
- * instead will always return false when they are encountered.
1306
- *
1307
- * @param a
1308
- * @param b
1309
- * @returns
1310
- */
1311
- export function compareArrays<T>(a: T[], b: T[]): boolean {
1312
- if (a.length !== b.length) {
1313
- return false;
1314
- }
1315
-
1316
- let result = false;
1317
- for (let i = 0; i < a.length; i++) {
1318
- if (Array.isArray(a[i]) || a[i] instanceof Float32Array) {
1319
- result = false;
1320
- break;
1321
- }
1322
-
1323
- if (a[i] !== b[i]) {
1324
- result = false;
1325
- break;
1326
- }
1327
-
1328
- result = true;
1329
- }
1330
-
1331
- return result;
1332
- }
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.deleteFramebuffer(framebuffer);
428
+ *
429
+ * @param framebuffer
430
+ */
431
+ deleteFramebuffer(framebuffer: WebGLFramebuffer | null) {
432
+ this.gl.deleteFramebuffer(framebuffer);
433
+ }
434
+
435
+ /**
436
+ * ```
437
+ * gl.viewport(x, y, width, height);
438
+ * ```
439
+ */
440
+ viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
441
+ const { gl } = this;
442
+ gl.viewport(x, y, width, height);
443
+ }
444
+
445
+ /**
446
+ * ```
447
+ * gl.clearColor(red, green, blue, alpha);
448
+ * ```
449
+ *
450
+ * @param red
451
+ * @param green
452
+ * @param blue
453
+ * @param alpha
454
+ */
455
+ clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
456
+ const { gl } = this;
457
+ gl.clearColor(red, green, blue, alpha);
458
+ }
459
+
460
+ /**
461
+ * ```
462
+ * gl["enable"|"disable"](gl.SCISSOR_TEST);
463
+ * ```
464
+ * @param enable
465
+ */
466
+ setScissorTest(enable: boolean) {
467
+ const { gl, scissorEnabled } = this;
468
+ if (enable === scissorEnabled) {
469
+ return;
470
+ }
471
+ if (enable) {
472
+ gl.enable(gl.SCISSOR_TEST);
473
+ } else {
474
+ gl.disable(gl.SCISSOR_TEST);
475
+ }
476
+ this.scissorEnabled = enable;
477
+ }
478
+
479
+ /**
480
+ * ```
481
+ * gl.scissor(x, y, width, height);
482
+ * ```
483
+ *
484
+ * @param x
485
+ * @param y
486
+ * @param width
487
+ * @param height
488
+ */
489
+ scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
490
+ const { gl, scissorX, scissorY, scissorWidth, scissorHeight } = this;
491
+ if (
492
+ x !== scissorX ||
493
+ y !== scissorY ||
494
+ width !== scissorWidth ||
495
+ height !== scissorHeight
496
+ ) {
497
+ gl.scissor(x, y, width, height);
498
+ this.scissorX = x;
499
+ this.scissorY = y;
500
+ this.scissorWidth = width;
501
+ this.scissorHeight = height;
502
+ }
503
+ }
504
+
505
+ /**
506
+ * ```
507
+ * gl["enable"|"disable"](gl.BLEND);
508
+ * ```
509
+ *
510
+ * @param blend
511
+ * @returns
512
+ */
513
+ setBlend(blend: boolean) {
514
+ const { gl, blendEnabled } = this;
515
+ if (blend === blendEnabled) {
516
+ return;
517
+ }
518
+ if (blend) {
519
+ gl.enable(gl.BLEND);
520
+ } else {
521
+ gl.disable(gl.BLEND);
522
+ }
523
+ this.blendEnabled = blend;
524
+ }
525
+
526
+ /**
527
+ * ```
528
+ * gl.blendFunc(src, dst);
529
+ * ```
530
+ *
531
+ * @param src
532
+ * @param dst
533
+ */
534
+ blendFunc(src: GLenum, dst: GLenum) {
535
+ const { gl, blendSrcRgb, blendDstRgb, blendSrcAlpha, blendDstAlpha } = this;
536
+ if (
537
+ src !== blendSrcRgb ||
538
+ dst !== blendDstRgb ||
539
+ src !== blendSrcAlpha ||
540
+ dst !== blendDstAlpha
541
+ ) {
542
+ gl.blendFunc(src, dst);
543
+ this.blendSrcRgb = src;
544
+ this.blendDstRgb = dst;
545
+ this.blendSrcAlpha = src;
546
+ this.blendDstAlpha = dst;
547
+ }
548
+ }
549
+
550
+ /**
551
+ * ```
552
+ * gl.createBuffer();
553
+ * ```
554
+ *
555
+ * @returns
556
+ */
557
+ createBuffer() {
558
+ const { gl } = this;
559
+ return gl.createBuffer();
560
+ }
561
+
562
+ /**
563
+ * ```
564
+ * gl.createFramebuffer();
565
+ * ```
566
+ * @returns
567
+ */
568
+ createFramebuffer() {
569
+ const { gl } = this;
570
+ return gl.createFramebuffer();
571
+ }
572
+
573
+ /**
574
+ * ```
575
+ * gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
576
+ * ```
577
+ *
578
+ * @param framebuffer
579
+ */
580
+ bindFramebuffer(framebuffer: WebGLFramebuffer | null) {
581
+ const { gl } = this;
582
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
583
+ }
584
+
585
+ /**
586
+ * ```
587
+ * gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
588
+ * ```
589
+ * @remarks
590
+ * **WebGL Difference**: Bind target is always `gl.FRAMEBUFFER` and textarget is always `gl.TEXTURE_2D`
591
+ */
592
+
593
+ framebufferTexture2D(
594
+ attachment: GLenum,
595
+ texture: WebGLTexture | null,
596
+ level: GLint,
597
+ ) {
598
+ const { gl } = this;
599
+ gl.framebufferTexture2D(
600
+ gl.FRAMEBUFFER,
601
+ attachment,
602
+ gl.TEXTURE_2D,
603
+ texture,
604
+ level,
605
+ );
606
+ }
607
+
608
+ /**
609
+ * ```
610
+ * gl.clear(gl.COLOR_BUFFER_BIT);
611
+ * ```
612
+ *
613
+ * @remarks
614
+ * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
615
+ */
616
+ clear() {
617
+ const { gl } = this;
618
+ gl.clear(gl.COLOR_BUFFER_BIT);
619
+ }
620
+
621
+ /**
622
+ * ```
623
+ * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
624
+ * gl.bufferData(gl.ARRAY_BUFFER, data, usage);
625
+ * ```
626
+ *
627
+ * @remarks
628
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
629
+ *
630
+ * @param buffer
631
+ * @param data
632
+ * @param usage
633
+ */
634
+ arrayBufferData(
635
+ buffer: WebGLBuffer | null,
636
+ data: ArrayBufferView,
637
+ usage: GLenum,
638
+ ) {
639
+ const { gl, boundArrayBuffer } = this;
640
+ if (boundArrayBuffer !== buffer) {
641
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
642
+ this.boundArrayBuffer = buffer;
643
+ }
644
+ gl.bufferData(gl.ARRAY_BUFFER, data, usage);
645
+ }
646
+
647
+ /**
648
+ * ```
649
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
650
+ * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
651
+ * ```
652
+ * @remarks
653
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
654
+ *
655
+ * @param buffer
656
+ * @param data
657
+ * @param usage
658
+ */
659
+ elementArrayBufferData(
660
+ buffer: WebGLBuffer | null,
661
+ data: ArrayBufferView,
662
+ usage: GLenum,
663
+ ) {
664
+ const { gl, boundElementArrayBuffer } = this;
665
+ if (boundElementArrayBuffer !== buffer) {
666
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
667
+ this.boundElementArrayBuffer = buffer;
668
+ }
669
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
670
+ }
671
+
672
+ /**
673
+ * ```
674
+ * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
675
+ * gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
676
+ * ```
677
+ *
678
+ * @remarks
679
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
680
+ *
681
+ * @param buffer
682
+ * @param index
683
+ * @param size
684
+ * @param type
685
+ * @param normalized
686
+ * @param stride
687
+ * @param offset
688
+ */
689
+ vertexAttribPointer(
690
+ buffer: WebGLBuffer,
691
+ index: GLuint,
692
+ size: GLint,
693
+ type: GLenum,
694
+ normalized: GLboolean,
695
+ stride: GLsizei,
696
+ offset: GLintptr,
697
+ ) {
698
+ const { gl, boundArrayBuffer } = this;
699
+ if (boundArrayBuffer !== buffer) {
700
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
701
+ this.boundArrayBuffer = buffer;
702
+ }
703
+ gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
704
+ }
705
+
706
+ /**
707
+ * ```
708
+ * gl.useProgram(program);
709
+ * ```
710
+ *
711
+ * @param program
712
+ * @returns
713
+ */
714
+ useProgram(program: WebGLProgram | null) {
715
+ const { gl, curProgram } = this;
716
+ if (curProgram === program) {
717
+ return;
718
+ }
719
+ gl.useProgram(program);
720
+ this.curProgram = program;
721
+ }
722
+
723
+ /**
724
+ * Sets the value of a single float uniform variable.
725
+ *
726
+ * @param location - The location of the uniform variable.
727
+ * @param v0 - The value to set.
728
+ */
729
+ uniform1f(location: WebGLUniformLocation | null, v0: number) {
730
+ const { gl } = this;
731
+ gl.uniform1f(location, v0);
732
+ }
733
+
734
+ /**
735
+ * Sets the value of a float array uniform variable.
736
+ *
737
+ * @param location - The location of the uniform variable.
738
+ * @param value - The array of values to set.
739
+ */
740
+ uniform1fv(
741
+ location: WebGLUniformLocation | null,
742
+ value: Float32Array | number[],
743
+ ) {
744
+ const { gl } = this;
745
+ gl.uniform1fv(location, value);
746
+ }
747
+
748
+ /**
749
+ * Sets the value of a single integer uniform variable.
750
+ *
751
+ * @param location - The location of the uniform variable.
752
+ * @param v0 - The value to set.
753
+ */
754
+ uniform1i(location: WebGLUniformLocation | null, v0: number) {
755
+ const { gl } = this;
756
+ gl.uniform1i(location, v0);
757
+ }
758
+
759
+ /**
760
+ * Sets the value of an integer array uniform variable.
761
+ *
762
+ * @param location - The location of the uniform variable.
763
+ * @param value - The array of values to set.
764
+ */
765
+ uniform1iv(
766
+ location: WebGLUniformLocation | null,
767
+ value: Int32Array | number[],
768
+ ) {
769
+ const { gl } = this;
770
+ gl.uniform1iv(location, value);
771
+ }
772
+
773
+ /**
774
+ * Sets the value of a vec2 uniform variable.
775
+ *
776
+ * @param location - The location of the uniform variable.
777
+ * @param v0 - The first component of the vector.
778
+ * @param v1 - The second component of the vector.
779
+ */
780
+ uniform2f(location: WebGLUniformLocation | null, v0: number, v1: number) {
781
+ const { gl } = this;
782
+ gl.uniform2f(location, v0, v1);
783
+ }
784
+
785
+ /**
786
+ * Sets the value of a vec2 array uniform variable.
787
+ *
788
+ * @param location - The location of the uniform variable.
789
+ * @param value - The array of vec2 values to set.
790
+ */
791
+ uniform2fv(
792
+ location: WebGLUniformLocation | null,
793
+ value: Float32Array | number[],
794
+ ) {
795
+ const { gl } = this;
796
+ gl.uniform2fv(location, value);
797
+ }
798
+
799
+ /**
800
+ * Sets the value of a ivec2 uniform variable.
801
+ *
802
+ * @param location - The location of the uniform variable.
803
+ * @param v0 - The first component of the vector.
804
+ * @param v1 - The second component of the vector.
805
+ */
806
+ uniform2i(location: WebGLUniformLocation | null, v0: number, v1: number) {
807
+ const { gl } = this;
808
+ gl.uniform2i(location, v0, v1);
809
+ }
810
+
811
+ /**
812
+ * Sets the value of an ivec2 array uniform variable.
813
+ *
814
+ * @param location - The location of the uniform variable.
815
+ * @param value - The array of ivec2 values to set.
816
+ */
817
+ uniform2iv(
818
+ location: WebGLUniformLocation | null,
819
+ value: Int32Array | number[],
820
+ ) {
821
+ const { gl } = this;
822
+ gl.uniform2iv(location, value);
823
+ }
824
+
825
+ /**
826
+ * Sets the value of a vec3 uniform variable.
827
+ *
828
+ * @param location - The location of the uniform variable.
829
+ * @param v0 - The first component of the vector.
830
+ * @param v1 - The second component of the vector.
831
+ * @param v2 - The third component of the vector.
832
+ */
833
+ uniform3f(
834
+ location: WebGLUniformLocation | null,
835
+ v0: number,
836
+ v1: number,
837
+ v2: number,
838
+ ) {
839
+ const { gl } = this;
840
+ gl.uniform3f(location, v0, v1, v2);
841
+ }
842
+
843
+ /**
844
+ * Sets the value of a vec3 array uniform variable.
845
+ *
846
+ * @param location - The location of the uniform variable.
847
+ * @param value - The array of vec3 values to set.
848
+ */
849
+ uniform3fv(
850
+ location: WebGLUniformLocation | null,
851
+ value: Float32Array | number[],
852
+ ) {
853
+ const { gl } = this;
854
+ gl.uniform3fv(location, value);
855
+ }
856
+
857
+ /**
858
+ * Sets the value of a ivec3 uniform variable.
859
+ *
860
+ * @param location - The location of the uniform variable.
861
+ * @param v0 - The first component of the vector.
862
+ * @param v1 - The second component of the vector.
863
+ * @param v2 - The third component of the vector.
864
+ */
865
+ uniform3i(
866
+ location: WebGLUniformLocation | null,
867
+ v0: number,
868
+ v1: number,
869
+ v2: number,
870
+ ) {
871
+ const { gl } = this;
872
+ gl.uniform3i(location, v0, v1, v2);
873
+ }
874
+
875
+ /**
876
+ * Sets the value of an ivec3 array uniform variable.
877
+ *
878
+ * @param location - The location of the uniform variable.
879
+ * @param value - The array of ivec3 values to set.
880
+ */
881
+ uniform3iv(
882
+ location: WebGLUniformLocation | null,
883
+ value: Int32Array | number[],
884
+ ) {
885
+ const { gl } = this;
886
+ gl.uniform3iv(location, value);
887
+ }
888
+
889
+ /**
890
+ * Sets the value of a vec4 uniform variable.
891
+ *
892
+ * @param location - The location of the uniform variable.
893
+ * @param v0 - The first component of the vector.
894
+ * @param v1 - The second component of the vector.
895
+ * @param v2 - The third component of the vector.
896
+ * @param v3 - The fourth component of the vector.
897
+ */
898
+ uniform4f(
899
+ location: WebGLUniformLocation | null,
900
+ v0: number,
901
+ v1: number,
902
+ v2: number,
903
+ v3: number,
904
+ ) {
905
+ const { gl } = this;
906
+ gl.uniform4f(location, v0, v1, v2, v3);
907
+ }
908
+
909
+ /**
910
+ * Sets the value of a vec4 array uniform variable.
911
+ *
912
+ * @param location - The location of the uniform variable.
913
+ * @param value - The array of vec4 values to set.
914
+ */
915
+ uniform4fv(
916
+ location: WebGLUniformLocation | null,
917
+ value: Float32Array | number[],
918
+ ) {
919
+ const { gl } = this;
920
+ gl.uniform4fv(location, value);
921
+ }
922
+
923
+ /**
924
+ * Sets the value of a ivec4 uniform variable.
925
+ *
926
+ * @param location - The location of the uniform variable.
927
+ * @param v0 - The first component of the vector.
928
+ * @param v1 - The second component of the vector.
929
+ * @param v2 - The third component of the vector.
930
+ * @param v3 - The fourth component of the vector.
931
+ */
932
+ uniform4i(
933
+ location: WebGLUniformLocation | null,
934
+ v0: number,
935
+ v1: number,
936
+ v2: number,
937
+ v3: number,
938
+ ) {
939
+ const { gl } = this;
940
+ gl.uniform4i(location, v0, v1, v2, v3);
941
+ }
942
+
943
+ /**
944
+ * Sets the value of an ivec4 array uniform variable.
945
+ *
946
+ * @param location - The location of the uniform variable.
947
+ * @param value - The array of ivec4 values to set.
948
+ */
949
+ uniform4iv(
950
+ location: WebGLUniformLocation | null,
951
+ value: Int32Array | number[],
952
+ ) {
953
+ const { gl } = this;
954
+ gl.uniform4iv(location, value);
955
+ }
956
+
957
+ /**
958
+ * Sets the value of a mat2 uniform variable.
959
+ *
960
+ * @param location - The location of the uniform variable.
961
+ * @param transpose - Whether to transpose the matrix.
962
+ * @param value - The array of mat2 values to set.
963
+ */
964
+ uniformMatrix2fv(
965
+ location: WebGLUniformLocation | null,
966
+ value: Float32Array | number[],
967
+ ) {
968
+ const { gl } = this;
969
+ gl.uniformMatrix2fv(location, false, value);
970
+ }
971
+
972
+ /**
973
+ * Sets the value of a mat2 uniform variable.
974
+ * @param location - The location of the uniform variable.
975
+ * @param value - The array of mat2 values to set.
976
+ */
977
+ uniformMatrix3fv(
978
+ location: WebGLUniformLocation | null,
979
+ value: Float32Array | number[],
980
+ ) {
981
+ const { gl } = this;
982
+ gl.uniformMatrix3fv(location, false, value);
983
+ }
984
+
985
+ /**
986
+ * Sets the value of a mat4 uniform variable.
987
+ * @param location - The location of the uniform variable.
988
+ * @param value - The array of mat4 values to set.
989
+ */
990
+ uniformMatrix4fv(
991
+ location: WebGLUniformLocation | null,
992
+ value: Float32Array | number[],
993
+ ) {
994
+ const { gl } = this;
995
+ gl.uniformMatrix4fv(location, false, value);
996
+ }
997
+
998
+ /**
999
+ * ```
1000
+ * gl.getParameter(pname);
1001
+ * ```
1002
+ *
1003
+ * @param pname
1004
+ * @returns
1005
+ */
1006
+ getParameter(pname: GLenum): any {
1007
+ const { gl } = this;
1008
+ return gl.getParameter(pname);
1009
+ }
1010
+
1011
+ /**
1012
+ * ```
1013
+ * gl.drawElements(mode, count, type, offset);
1014
+ * ```
1015
+ *
1016
+ * @param mode
1017
+ * @param count
1018
+ * @param type
1019
+ * @param offset
1020
+ */
1021
+ drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
1022
+ const { gl } = this;
1023
+ gl.drawElements(mode, count, type, offset);
1024
+ }
1025
+
1026
+ /**
1027
+ * ```
1028
+ * gl.drawArrays(mode, first, count);
1029
+ * ```
1030
+ *
1031
+ * @param name
1032
+ * @returns
1033
+ */
1034
+ getExtension(name: string) {
1035
+ const { gl } = this;
1036
+ return gl.getExtension(name);
1037
+ }
1038
+
1039
+ /**
1040
+ * ```
1041
+ * gl.getError(type);
1042
+ * ```
1043
+ *
1044
+ * @returns
1045
+ */
1046
+ getError() {
1047
+ const { gl } = this;
1048
+ return gl.getError();
1049
+ }
1050
+
1051
+ /**
1052
+ * ```
1053
+ * gl.createVertexArray();
1054
+ * ```
1055
+ *
1056
+ * @returns
1057
+ */
1058
+ createVertexArray() {
1059
+ const { gl } = this;
1060
+ assertTruthy(gl instanceof WebGL2RenderingContext);
1061
+ return gl.createVertexArray();
1062
+ }
1063
+
1064
+ /**
1065
+ * ```
1066
+ * gl.bindVertexArray(vertexArray);
1067
+ * ```
1068
+ *
1069
+ * @param vertexArray
1070
+ */
1071
+ bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
1072
+ const { gl } = this;
1073
+ assertTruthy(gl instanceof WebGL2RenderingContext);
1074
+ gl.bindVertexArray(vertexArray);
1075
+ }
1076
+
1077
+ /**
1078
+ * ```
1079
+ * gl.getAttribLocation(program, name);
1080
+ * ```
1081
+ *
1082
+ * @param program
1083
+ * @param name
1084
+ * @returns
1085
+ */
1086
+ getAttribLocation(program: WebGLProgram, name: string) {
1087
+ const { gl } = this;
1088
+ return gl.getAttribLocation(program, name);
1089
+ }
1090
+
1091
+ /**
1092
+ * ```
1093
+ * gl.getUniformLocation(program, name);
1094
+ * ```
1095
+ *
1096
+ * @param program
1097
+ * @param name
1098
+ * @returns
1099
+ */
1100
+ getUniformLocation(program: WebGLProgram, name: string) {
1101
+ const { gl } = this;
1102
+ return gl.getUniformLocation(program, name);
1103
+ }
1104
+
1105
+ /**
1106
+ * ```
1107
+ * gl.enableVertexAttribArray(index);
1108
+ * ```
1109
+ *
1110
+ * @param index
1111
+ */
1112
+ enableVertexAttribArray(index: number) {
1113
+ const { gl } = this;
1114
+ gl.enableVertexAttribArray(index);
1115
+ }
1116
+
1117
+ /**
1118
+ * ```
1119
+ * gl.disableVertexAttribArray(index);
1120
+ * ```
1121
+ *
1122
+ * @param index
1123
+ */
1124
+ disableVertexAttribArray(index: number) {
1125
+ const { gl } = this;
1126
+ gl.disableVertexAttribArray(index);
1127
+ }
1128
+
1129
+ /**
1130
+ * ```
1131
+ * gl.createShader(type);
1132
+ * ```
1133
+ *
1134
+ * @param type
1135
+ * @returns
1136
+ */
1137
+ createShader(type: number) {
1138
+ const { gl } = this;
1139
+ return gl.createShader(type);
1140
+ }
1141
+
1142
+ /**
1143
+ * ```
1144
+ * gl.compileShader(shader);
1145
+ * ```
1146
+ *
1147
+ * @param shader
1148
+ * @returns
1149
+ */
1150
+ compileShader(shader: WebGLShader) {
1151
+ const { gl } = this;
1152
+ gl.compileShader(shader);
1153
+ }
1154
+
1155
+ /**
1156
+ * ```
1157
+ * gl.attachShader(program, shader);
1158
+ * ```
1159
+ *
1160
+ * @param program
1161
+ * @param shader
1162
+ */
1163
+ attachShader(program: WebGLProgram, shader: WebGLShader) {
1164
+ const { gl } = this;
1165
+ gl.attachShader(program, shader);
1166
+ }
1167
+
1168
+ /**
1169
+ * ```
1170
+ * gl.linkProgram(program);
1171
+ * ```
1172
+ *
1173
+ * @param program
1174
+ */
1175
+ linkProgram(program: WebGLProgram) {
1176
+ const { gl } = this;
1177
+ gl.linkProgram(program);
1178
+ }
1179
+
1180
+ /**
1181
+ * ```
1182
+ * gl.deleteProgram(shader);
1183
+ * ```
1184
+ *
1185
+ * @param shader
1186
+ */
1187
+ deleteProgram(shader: WebGLProgram) {
1188
+ const { gl } = this;
1189
+ gl.deleteProgram(shader);
1190
+ }
1191
+
1192
+ /**
1193
+ * ```
1194
+ * gl.getShaderParameter(shader, pname);
1195
+ * ```
1196
+ *
1197
+ * @param shader
1198
+ * @param pname
1199
+ */
1200
+ getShaderParameter(shader: WebGLShader, pname: GLenum) {
1201
+ const { gl } = this;
1202
+ return gl.getShaderParameter(shader, pname);
1203
+ }
1204
+
1205
+ /**
1206
+ * ```
1207
+ * gl.getShaderInfoLog(shader);
1208
+ * ```
1209
+ *
1210
+ * @param shader
1211
+ */
1212
+ getShaderInfoLog(shader: WebGLShader) {
1213
+ const { gl } = this;
1214
+ return gl.getShaderInfoLog(shader);
1215
+ }
1216
+
1217
+ /**
1218
+ * ```
1219
+ * gl.createProgram();
1220
+ * ```
1221
+ *
1222
+ * @returns
1223
+ */
1224
+ createProgram() {
1225
+ const { gl } = this;
1226
+ return gl.createProgram();
1227
+ }
1228
+
1229
+ /**
1230
+ * ```
1231
+ * gl.getProgramParameter(program, pname);
1232
+ * ```
1233
+ *
1234
+ * @param program
1235
+ * @param pname
1236
+ * @returns
1237
+ */
1238
+ getProgramParameter(program: WebGLProgram, pname: GLenum) {
1239
+ const { gl } = this;
1240
+ return gl.getProgramParameter(program, pname);
1241
+ }
1242
+
1243
+ /**
1244
+ * ```
1245
+ * gl.getProgramInfoLog(program);
1246
+ * ```
1247
+ *
1248
+ * @param program
1249
+ * @returns
1250
+ */
1251
+ getProgramInfoLog(program: WebGLProgram) {
1252
+ const { gl } = this;
1253
+ return gl.getProgramInfoLog(program);
1254
+ }
1255
+
1256
+ /**
1257
+ * ```
1258
+ * gl.shaderSource(shader, source);
1259
+ * ```
1260
+ *
1261
+ * @param shader
1262
+ * @param source
1263
+ */
1264
+ shaderSource(shader: WebGLShader, source: string) {
1265
+ const { gl } = this;
1266
+ gl.shaderSource(shader, source);
1267
+ }
1268
+
1269
+ /**
1270
+ * ```
1271
+ * gl.deleteShader(shader);
1272
+ * ```
1273
+ *
1274
+ * @param shader
1275
+ */
1276
+ deleteShader(shader: WebGLShader) {
1277
+ const { gl } = this;
1278
+ gl.deleteShader(shader);
1279
+ }
1280
+ }
1281
+
1282
+ // prettier-ignore
1283
+ type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
1284
+ ?
1285
+ MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
1286
+ ? true
1287
+ : false
1288
+ : false;
1289
+
1290
+ // prettier-ignore
1291
+ export type UniformMethodMap = {
1292
+ [Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
1293
+ location: WebGLUniformLocation | null,
1294
+ ...args: infer T
1295
+ ) => void
1296
+ ? T
1297
+ : never;
1298
+ };
1299
+
1300
+ /**
1301
+ * Compare two arrays for equality.
1302
+ *
1303
+ * @remarks
1304
+ * This function will not try to compare nested arrays or Float32Arrays and
1305
+ * instead will always return false when they are encountered.
1306
+ *
1307
+ * @param a
1308
+ * @param b
1309
+ * @returns
1310
+ */
1311
+ export function compareArrays<T>(a: T[], b: T[]): boolean {
1312
+ if (a.length !== b.length) {
1313
+ return false;
1314
+ }
1315
+
1316
+ let result = false;
1317
+ for (let i = 0; i < a.length; i++) {
1318
+ if (Array.isArray(a[i]) || a[i] instanceof Float32Array) {
1319
+ result = false;
1320
+ break;
1321
+ }
1322
+
1323
+ if (a[i] !== b[i]) {
1324
+ result = false;
1325
+ break;
1326
+ }
1327
+
1328
+ result = true;
1329
+ }
1330
+
1331
+ return result;
1332
+ }