@lightningjs/renderer 0.7.1 → 0.7.3

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 (215) hide show
  1. package/COPYING +1 -0
  2. package/LICENSE +202 -202
  3. package/NOTICE +3 -3
  4. package/README.md +221 -221
  5. package/dist/exports/main-api.d.ts +1 -0
  6. package/dist/src/common/CommonTypes.d.ts +7 -0
  7. package/dist/src/core/CoreShaderManager.d.ts +13 -6
  8. package/dist/src/core/CoreShaderManager.js +6 -6
  9. package/dist/src/core/CoreShaderManager.js.map +1 -1
  10. package/dist/src/core/Stage.d.ts +2 -1
  11. package/dist/src/core/Stage.js +4 -0
  12. package/dist/src/core/Stage.js.map +1 -1
  13. package/dist/src/core/animations/CoreAnimation.js +3 -0
  14. package/dist/src/core/animations/CoreAnimation.js.map +1 -1
  15. package/dist/src/core/animations/CoreAnimationController.d.ts +8 -0
  16. package/dist/src/core/animations/CoreAnimationController.js +26 -0
  17. package/dist/src/core/animations/CoreAnimationController.js.map +1 -1
  18. package/dist/src/core/lib/ImageWorker.js +21 -21
  19. package/dist/src/core/lib/WebGlContextWrapper.d.ts +9 -0
  20. package/dist/src/core/lib/WebGlContextWrapper.js +12 -0
  21. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  22. package/dist/src/core/lib/textureCompression.d.ts +16 -0
  23. package/dist/src/core/lib/textureCompression.js +129 -0
  24. package/dist/src/core/lib/textureCompression.js.map +1 -0
  25. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +12 -0
  26. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  27. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +47 -47
  28. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  29. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  30. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  31. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +49 -49
  32. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  33. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +5 -5
  34. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  35. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  36. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  37. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  38. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  39. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  40. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +31 -31
  41. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +13 -13
  42. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  43. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  44. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.d.ts +4 -0
  45. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +11 -0
  46. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  47. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.js.map +1 -1
  48. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +10 -1
  49. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  50. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +21 -7
  51. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
  52. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  53. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +2 -1
  54. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  55. package/dist/src/core/textures/ImageTexture.js +5 -0
  56. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  57. package/dist/src/core/textures/Texture.d.ts +28 -1
  58. package/dist/src/core/textures/Texture.js.map +1 -1
  59. package/dist/src/main-api/ICoreDriver.d.ts +2 -1
  60. package/dist/src/main-api/Inspector.d.ts +21 -0
  61. package/dist/src/main-api/Inspector.js +302 -0
  62. package/dist/src/main-api/Inspector.js.map +1 -0
  63. package/dist/src/main-api/RendererMain.d.ts +12 -0
  64. package/dist/src/main-api/RendererMain.js +19 -1
  65. package/dist/src/main-api/RendererMain.js.map +1 -1
  66. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +2 -1
  67. package/dist/src/render-drivers/main/MainCoreDriver.js +7 -1
  68. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -1
  69. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +2 -1
  70. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +7 -0
  71. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -1
  72. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +9 -1
  73. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -1
  74. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  75. package/exports/core-api.ts +102 -102
  76. package/exports/main-api.ts +60 -51
  77. package/exports/utils.ts +41 -41
  78. package/package.json +5 -3
  79. package/scripts/please-use-pnpm.js +13 -0
  80. package/src/common/CommonTypes.ts +113 -105
  81. package/src/common/EventEmitter.ts +77 -77
  82. package/src/common/IAnimationController.ts +29 -29
  83. package/src/core/CoreExtension.ts +32 -32
  84. package/src/core/CoreNode.ts +926 -926
  85. package/src/core/CoreShaderManager.ts +243 -210
  86. package/src/core/CoreTextNode.ts +391 -391
  87. package/src/core/CoreTextureManager.ts +326 -326
  88. package/src/core/Stage.ts +354 -342
  89. package/src/core/animations/AnimationManager.ts +38 -38
  90. package/src/core/animations/CoreAnimation.ts +181 -177
  91. package/src/core/animations/CoreAnimationController.ts +148 -117
  92. package/src/core/lib/ContextSpy.ts +41 -41
  93. package/src/core/lib/ImageWorker.ts +124 -124
  94. package/src/core/lib/Matrix3d.ts +290 -290
  95. package/src/core/lib/WebGlContextWrapper.ts +992 -965
  96. package/src/core/lib/textureCompression.ts +152 -0
  97. package/src/core/lib/utils.ts +241 -241
  98. package/src/core/platform.ts +46 -46
  99. package/src/core/renderers/CoreContextTexture.ts +30 -30
  100. package/src/core/renderers/CoreRenderOp.ts +22 -22
  101. package/src/core/renderers/CoreRenderer.ts +63 -63
  102. package/src/core/renderers/CoreShader.ts +41 -41
  103. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +37 -37
  104. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +231 -211
  105. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +107 -107
  106. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +520 -520
  107. package/src/core/renderers/webgl/WebGlCoreShader.ts +337 -337
  108. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  109. package/src/core/renderers/webgl/internal/RendererUtils.ts +131 -131
  110. package/src/core/renderers/webgl/internal/ShaderUtils.ts +136 -136
  111. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  112. package/src/core/renderers/webgl/shaders/DefaultShader.ts +95 -95
  113. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  114. package/src/core/renderers/webgl/shaders/DynamicShader.ts +474 -474
  115. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +161 -161
  116. package/src/core/renderers/webgl/shaders/SdfShader.ts +174 -174
  117. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  118. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +86 -86
  119. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  120. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  121. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  122. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +33 -33
  123. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +135 -135
  124. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +145 -145
  125. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  126. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +176 -176
  127. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +159 -159
  128. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +186 -186
  129. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +121 -121
  130. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +114 -114
  131. package/src/core/text-rendering/TextTextureRendererUtils.ts +189 -189
  132. package/src/core/text-rendering/TrFontManager.ts +96 -96
  133. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +141 -127
  134. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  135. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -165
  136. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +169 -166
  137. package/src/core/text-rendering/font-face-types/TrFontFace.ts +105 -105
  138. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +77 -77
  139. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +751 -738
  140. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +741 -707
  141. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +775 -777
  142. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  143. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  144. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  145. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  146. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +84 -84
  147. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  148. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  149. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +393 -392
  150. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -46
  151. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +51 -51
  152. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  153. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  154. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  155. package/src/core/text-rendering/renderers/TextRenderer.ts +504 -504
  156. package/src/core/textures/ColorTexture.ts +86 -86
  157. package/src/core/textures/ImageTexture.ts +133 -124
  158. package/src/core/textures/NoiseTexture.ts +96 -96
  159. package/src/core/textures/SubTexture.ts +143 -143
  160. package/src/core/textures/Texture.ts +218 -186
  161. package/src/core/utils.ts +224 -224
  162. package/src/env.d.ts +7 -7
  163. package/src/main-api/ICoreDriver.ts +66 -61
  164. package/src/main-api/INode.ts +470 -470
  165. package/src/main-api/Inspector.ts +432 -0
  166. package/src/main-api/RendererMain.ts +649 -610
  167. package/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.ts +45 -45
  168. package/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.ts +154 -154
  169. package/src/main-api/texture-usage-trackers/TextureUsageTracker.ts +54 -54
  170. package/src/render-drivers/main/MainCoreDriver.ts +148 -133
  171. package/src/render-drivers/main/MainOnlyNode.ts +453 -453
  172. package/src/render-drivers/main/MainOnlyTextNode.ts +261 -261
  173. package/src/render-drivers/threadx/NodeStruct.ts +300 -300
  174. package/src/render-drivers/threadx/SharedNode.ts +97 -97
  175. package/src/render-drivers/threadx/TextNodeStruct.ts +211 -211
  176. package/src/render-drivers/threadx/ThreadXCoreDriver.ts +285 -273
  177. package/src/render-drivers/threadx/ThreadXMainAnimationController.ts +99 -99
  178. package/src/render-drivers/threadx/ThreadXMainNode.ts +178 -178
  179. package/src/render-drivers/threadx/ThreadXMainTextNode.ts +85 -85
  180. package/src/render-drivers/threadx/ThreadXRendererMessage.ts +110 -97
  181. package/src/render-drivers/threadx/worker/ThreadXRendererNode.ts +238 -238
  182. package/src/render-drivers/threadx/worker/ThreadXRendererTextNode.ts +149 -149
  183. package/src/render-drivers/threadx/worker/renderer.ts +151 -151
  184. package/src/render-drivers/utils.ts +57 -57
  185. package/src/utils.ts +207 -207
  186. package/dist/src/core/lib/WebGlContext.d.ts +0 -414
  187. package/dist/src/core/lib/WebGlContext.js +0 -640
  188. package/dist/src/core/lib/WebGlContext.js.map +0 -1
  189. package/dist/src/core/scene/Scene.d.ts +0 -59
  190. package/dist/src/core/scene/Scene.js +0 -106
  191. package/dist/src/core/scene/Scene.js.map +0 -1
  192. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.d.ts +0 -8
  193. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js +0 -29
  194. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js.map +0 -1
  195. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.d.ts +0 -19
  196. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js +0 -84
  197. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js.map +0 -1
  198. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.d.ts +0 -8
  199. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js +0 -40
  200. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js.map +0 -1
  201. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.d.ts +0 -2
  202. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js +0 -41
  203. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js.map +0 -1
  204. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.d.ts +0 -1
  205. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js +0 -4
  206. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js.map +0 -1
  207. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.d.ts +0 -1
  208. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js +0 -2
  209. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js.map +0 -1
  210. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +0 -20
  211. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +0 -55
  212. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +0 -1
  213. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.d.ts +0 -9
  214. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js +0 -32
  215. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js.map +0 -1
@@ -1,965 +1,992 @@
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
- private programUniforms: WeakMap<
49
- WebGLProgram,
50
- Map<WebGLUniformLocation, any[]>
51
- > = new WeakMap();
52
- //#endregion Cached WebGL State
53
-
54
- //#region Canvas
55
- public readonly canvas;
56
- //#endregion Canvas
57
-
58
- //#region WebGL Enums
59
- public readonly MAX_RENDERBUFFER_SIZE;
60
- public readonly MAX_TEXTURE_SIZE;
61
- public readonly MAX_VIEWPORT_DIMS;
62
- public readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS;
63
- public readonly MAX_TEXTURE_IMAGE_UNITS;
64
- public readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS;
65
- public readonly MAX_VERTEX_ATTRIBS;
66
- public readonly MAX_VARYING_VECTORS;
67
- public readonly MAX_VERTEX_UNIFORM_VECTORS;
68
- public readonly MAX_FRAGMENT_UNIFORM_VECTORS;
69
- public readonly TEXTURE_MAG_FILTER;
70
- public readonly TEXTURE_MIN_FILTER;
71
- public readonly TEXTURE_WRAP_S;
72
- public readonly TEXTURE_WRAP_T;
73
- public readonly LINEAR;
74
- public readonly CLAMP_TO_EDGE;
75
- public readonly RGBA;
76
- public readonly UNSIGNED_BYTE;
77
- public readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL;
78
- public readonly FLOAT;
79
- public readonly TRIANGLES;
80
- public readonly UNSIGNED_SHORT;
81
- public readonly ONE;
82
- public readonly ONE_MINUS_SRC_ALPHA;
83
- public readonly VERTEX_SHADER;
84
- public readonly FRAGMENT_SHADER;
85
- public readonly STATIC_DRAW;
86
- public readonly COMPILE_STATUS;
87
- public readonly LINK_STATUS;
88
- public readonly DYNAMIC_DRAW;
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.FLOAT = gl.FLOAT;
165
- this.TRIANGLES = gl.TRIANGLES;
166
- this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
167
- this.ONE = gl.ONE;
168
- this.ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
169
- this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
170
- this.TRIANGLES = gl.TRIANGLES;
171
- this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
172
- this.VERTEX_SHADER = gl.VERTEX_SHADER;
173
- this.FRAGMENT_SHADER = gl.FRAGMENT_SHADER;
174
- this.STATIC_DRAW = gl.STATIC_DRAW;
175
- this.COMPILE_STATUS = gl.COMPILE_STATUS;
176
- this.LINK_STATUS = gl.LINK_STATUS;
177
- this.DYNAMIC_DRAW = gl.DYNAMIC_DRAW;
178
- }
179
- /**
180
- * Returns true if the WebGL context is WebGL2
181
- *
182
- * @returns
183
- */
184
- isWebGl2() {
185
- return isWebGl2(this.gl);
186
- }
187
-
188
- /**
189
- * ```
190
- * gl.activeTexture(textureUnit + gl.TEXTURE0);
191
- * ```
192
- *
193
- * @remarks
194
- * **WebGL Difference**: `textureUnit` is based from 0, not `gl.TEXTURE0`.
195
- *
196
- * @param textureUnit
197
- */
198
- activeTexture(textureUnit: number) {
199
- const { gl } = this;
200
- if (this.activeTextureUnit !== textureUnit) {
201
- gl.activeTexture(textureUnit + gl.TEXTURE0);
202
- this.activeTextureUnit = textureUnit;
203
- }
204
- }
205
-
206
- /**
207
- * ```
208
- * gl.bindTexture(gl.TEXTURE_2D, texture);
209
- * ```
210
- * @remarks
211
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
212
- *
213
- * @param texture
214
- */
215
- bindTexture(texture: WebGLTexture | null) {
216
- const { gl, activeTextureUnit, texture2dUnits } = this;
217
-
218
- if (texture2dUnits[activeTextureUnit] === texture) {
219
- return;
220
- }
221
- texture2dUnits[activeTextureUnit] = texture;
222
-
223
- gl.bindTexture(this.gl.TEXTURE_2D, texture);
224
- }
225
-
226
- private _getActiveTexture(): WebGLTexture | null {
227
- const { activeTextureUnit, texture2dUnits } = this;
228
- return texture2dUnits[activeTextureUnit]!;
229
- }
230
-
231
- /**
232
- * ```
233
- * gl.texParameteri(gl.TEXTURE_2D, pname, param);
234
- * ```
235
- * @remarks
236
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
237
- *
238
- * @param pname
239
- * @param param
240
- * @returns
241
- */
242
- texParameteri(pname: number, param: number) {
243
- const { gl, texture2dParams } = this;
244
-
245
- const activeTexture = this._getActiveTexture();
246
- if (!activeTexture) {
247
- throw new Error('No active texture');
248
- }
249
- let textureParams = texture2dParams.get(activeTexture);
250
- if (!textureParams) {
251
- textureParams = {};
252
- texture2dParams.set(activeTexture, textureParams);
253
- }
254
- if (textureParams[pname] === param) {
255
- return;
256
- }
257
- textureParams[pname] = param;
258
- gl.texParameteri(gl.TEXTURE_2D, pname, param);
259
- }
260
-
261
- /**
262
- * ```
263
- * gl.texImage2D(
264
- * gl.TEXTURE_2D,
265
- * level,
266
- * internalFormat,
267
- * width,
268
- * height,
269
- * border,
270
- * format,
271
- * type,
272
- * pixels,
273
- * );
274
- * ```
275
- * @remarks
276
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
277
- *
278
- * @param level
279
- * @param internalFormat
280
- * @param width
281
- * @param height
282
- * @param border
283
- * @param format
284
- * @param type
285
- * @param pixels
286
- */
287
- texImage2D(
288
- level: GLint,
289
- internalformat: GLint,
290
- width: GLsizei,
291
- height: GLsizei,
292
- border: GLint,
293
- format: GLenum,
294
- type: GLenum,
295
- pixels: ArrayBufferView | null,
296
- ): void;
297
- texImage2D(
298
- level: GLint,
299
- internalformat: GLint,
300
- format: GLenum,
301
- type: GLenum,
302
- source: TexImageSource,
303
- ): void;
304
- texImage2D(
305
- level: any,
306
- internalFormat: any,
307
- widthOrFormat: any,
308
- heightOrType: any,
309
- borderOrSource: any,
310
- format?: any,
311
- type?: any,
312
- pixels?: any,
313
- ) {
314
- const { gl } = this;
315
- if (format) {
316
- gl.texImage2D(
317
- gl.TEXTURE_2D,
318
- level,
319
- internalFormat,
320
- widthOrFormat,
321
- heightOrType,
322
- borderOrSource,
323
- format,
324
- type,
325
- pixels,
326
- );
327
- } else {
328
- gl.texImage2D(
329
- gl.TEXTURE_2D,
330
- level,
331
- internalFormat,
332
- widthOrFormat,
333
- heightOrType,
334
- borderOrSource,
335
- );
336
- }
337
- }
338
-
339
- /**
340
- * ```
341
- * gl.pixelStorei(pname, param);
342
- * ```
343
- *
344
- * @param pname
345
- * @param param
346
- */
347
- pixelStorei(pname: GLenum, param: GLint | GLboolean) {
348
- const { gl } = this;
349
- gl.pixelStorei(pname, param);
350
- }
351
-
352
- /**
353
- * ```
354
- * gl.generateMipmap(gl.TEXTURE_2D);
355
- * ```
356
- *
357
- * @remarks
358
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
359
- */
360
- generateMipmap() {
361
- const { gl } = this;
362
- gl.generateMipmap(gl.TEXTURE_2D);
363
- }
364
-
365
- /**
366
- * ```
367
- * gl.createTexture();
368
- * ```
369
- *
370
- * @returns
371
- */
372
- createTexture() {
373
- const { gl } = this;
374
- return gl.createTexture();
375
- }
376
-
377
- /**
378
- * ```
379
- * gl.deleteTexture(texture);
380
- * ```
381
- *
382
- * @param texture
383
- */
384
- deleteTexture(texture: WebGLTexture | null) {
385
- const { gl } = this;
386
- if (texture) {
387
- this.texture2dParams.delete(texture);
388
- }
389
- gl.deleteTexture(texture);
390
- }
391
-
392
- /**
393
- * ```
394
- * gl.viewport(x, y, width, height);
395
- * ```
396
- */
397
- viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
398
- const { gl } = this;
399
- gl.viewport(x, y, width, height);
400
- }
401
-
402
- /**
403
- * ```
404
- * gl.clearColor(red, green, blue, alpha);
405
- * ```
406
- *
407
- * @param red
408
- * @param green
409
- * @param blue
410
- * @param alpha
411
- */
412
- clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
413
- const { gl } = this;
414
- gl.clearColor(red, green, blue, alpha);
415
- }
416
-
417
- /**
418
- * ```
419
- * gl["enable"|"disable"](gl.SCISSOR_TEST);
420
- * ```
421
- * @param enable
422
- */
423
- setScissorTest(enable: boolean) {
424
- const { gl, scissorEnabled } = this;
425
- if (enable === scissorEnabled) {
426
- return;
427
- }
428
- if (enable) {
429
- gl.enable(gl.SCISSOR_TEST);
430
- } else {
431
- gl.disable(gl.SCISSOR_TEST);
432
- }
433
- this.scissorEnabled = enable;
434
- }
435
-
436
- /**
437
- * ```
438
- * gl.scissor(x, y, width, height);
439
- * ```
440
- *
441
- * @param x
442
- * @param y
443
- * @param width
444
- * @param height
445
- */
446
- scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
447
- const { gl, scissorX, scissorY, scissorWidth, scissorHeight } = this;
448
- if (
449
- x !== scissorX ||
450
- y !== scissorY ||
451
- width !== scissorWidth ||
452
- height !== scissorHeight
453
- ) {
454
- gl.scissor(x, y, width, height);
455
- this.scissorX = x;
456
- this.scissorY = y;
457
- this.scissorWidth = width;
458
- this.scissorHeight = height;
459
- }
460
- }
461
-
462
- /**
463
- * ```
464
- * gl["enable"|"disable"](gl.BLEND);
465
- * ```
466
- *
467
- * @param blend
468
- * @returns
469
- */
470
- setBlend(blend: boolean) {
471
- const { gl, blendEnabled } = this;
472
- if (blend === blendEnabled) {
473
- return;
474
- }
475
- if (blend) {
476
- gl.enable(gl.BLEND);
477
- } else {
478
- gl.disable(gl.BLEND);
479
- }
480
- this.blendEnabled = blend;
481
- }
482
-
483
- /**
484
- * ```
485
- * gl.blendFunc(src, dst);
486
- * ```
487
- *
488
- * @param src
489
- * @param dst
490
- */
491
- blendFunc(src: GLenum, dst: GLenum) {
492
- const { gl, blendSrcRgb, blendDstRgb, blendSrcAlpha, blendDstAlpha } = this;
493
- if (
494
- src !== blendSrcRgb ||
495
- dst !== blendDstRgb ||
496
- src !== blendSrcAlpha ||
497
- dst !== blendDstAlpha
498
- ) {
499
- gl.blendFunc(src, dst);
500
- this.blendSrcRgb = src;
501
- this.blendDstRgb = dst;
502
- this.blendSrcAlpha = src;
503
- this.blendDstAlpha = dst;
504
- }
505
- }
506
-
507
- /**
508
- * ```
509
- * createBuffer();
510
- * ```
511
- *
512
- * @returns
513
- */
514
- createBuffer() {
515
- const { gl } = this;
516
- return gl.createBuffer();
517
- }
518
-
519
- /**
520
- * ```
521
- * gl.clear(gl.COLOR_BUFFER_BIT);
522
- * ```
523
- *
524
- * @remarks
525
- * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
526
- */
527
- clear() {
528
- const { gl } = this;
529
- gl.clear(gl.COLOR_BUFFER_BIT);
530
- }
531
-
532
- /**
533
- * ```
534
- * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
535
- * gl.bufferData(gl.ARRAY_BUFFER, data, usage);
536
- * ```
537
- *
538
- * @remarks
539
- * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
540
- *
541
- * @param buffer
542
- * @param data
543
- * @param usage
544
- */
545
- arrayBufferData(
546
- buffer: WebGLBuffer | null,
547
- data: ArrayBufferView,
548
- usage: GLenum,
549
- ) {
550
- const { gl, boundArrayBuffer } = this;
551
- if (boundArrayBuffer !== buffer) {
552
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
553
- this.boundArrayBuffer = buffer;
554
- }
555
- gl.bufferData(gl.ARRAY_BUFFER, data, usage);
556
- }
557
-
558
- /**
559
- * ```
560
- * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
561
- * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
562
- * ```
563
- * @remarks
564
- * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
565
- *
566
- * @param buffer
567
- * @param data
568
- * @param usage
569
- */
570
- elementArrayBufferData(
571
- buffer: WebGLBuffer | null,
572
- data: ArrayBufferView,
573
- usage: GLenum,
574
- ) {
575
- const { gl, boundElementArrayBuffer } = this;
576
- if (boundElementArrayBuffer !== buffer) {
577
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
578
- this.boundElementArrayBuffer = buffer;
579
- }
580
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
581
- }
582
-
583
- /**
584
- * ```
585
- * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
586
- * gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
587
- * ```
588
- *
589
- * @remarks
590
- * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
591
- *
592
- * @param buffer
593
- * @param index
594
- * @param size
595
- * @param type
596
- * @param normalized
597
- * @param stride
598
- * @param offset
599
- */
600
- vertexAttribPointer(
601
- buffer: WebGLBuffer,
602
- index: GLuint,
603
- size: GLint,
604
- type: GLenum,
605
- normalized: GLboolean,
606
- stride: GLsizei,
607
- offset: GLintptr,
608
- ) {
609
- const { gl, boundArrayBuffer } = this;
610
- if (boundArrayBuffer !== buffer) {
611
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
612
- this.boundArrayBuffer = buffer;
613
- }
614
- gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
615
- }
616
-
617
- /**
618
- * ```
619
- * gl.useProgram(program);
620
- * ```
621
- *
622
- * @param program
623
- * @returns
624
- */
625
- useProgram(program: WebGLProgram | null) {
626
- const { gl, curProgram } = this;
627
- if (curProgram === program) {
628
- return;
629
- }
630
- gl.useProgram(program);
631
- this.curProgram = program;
632
- }
633
-
634
- setUniform<T extends keyof UniformMethodMap>(
635
- type: T,
636
- location: WebGLUniformLocation,
637
- ...args: UniformMethodMap[T]
638
- ) {
639
- const { gl, programUniforms } = this;
640
- let uniforms = programUniforms.get(this.curProgram!);
641
- if (!uniforms) {
642
- uniforms = new Map();
643
- programUniforms.set(this.curProgram!, uniforms);
644
- }
645
- const uniformArgs = uniforms.get(location);
646
- if (!uniformArgs || !compareArrays(uniformArgs, args)) {
647
- uniforms.set(location, args);
648
- gl[type](location, ...(args as [never, never, never, never]));
649
- }
650
- }
651
-
652
- /**
653
- * ```
654
- * gl.getParameter(pname);
655
- * ```
656
- *
657
- * @param pname
658
- * @returns
659
- */
660
- getParameter(pname: GLenum): any {
661
- const { gl } = this;
662
- return gl.getParameter(pname);
663
- }
664
-
665
- /**
666
- * ```
667
- * gl.drawElements(mode, count, type, offset);
668
- * ```
669
- *
670
- * @param mode
671
- * @param count
672
- * @param type
673
- * @param offset
674
- */
675
- drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
676
- const { gl } = this;
677
- gl.drawElements(mode, count, type, offset);
678
- }
679
-
680
- /**
681
- * ```
682
- * gl.drawArrays(mode, first, count);
683
- * ```
684
- *
685
- * @param name
686
- * @returns
687
- */
688
- getExtension(name: string) {
689
- const { gl } = this;
690
- return gl.getExtension(name);
691
- }
692
-
693
- /**
694
- * ```
695
- * gl.createVertexArray();
696
- * ```
697
- *
698
- * @returns
699
- */
700
- createVertexArray() {
701
- const { gl } = this;
702
- assertTruthy(gl instanceof WebGL2RenderingContext);
703
- return gl.createVertexArray();
704
- }
705
-
706
- /**
707
- * ```
708
- * gl.bindVertexArray(vertexArray);
709
- * ```
710
- *
711
- * @param vertexArray
712
- */
713
- bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
714
- const { gl } = this;
715
- assertTruthy(gl instanceof WebGL2RenderingContext);
716
- gl.bindVertexArray(vertexArray);
717
- }
718
-
719
- /**
720
- * ```
721
- * gl.getAttribLocation(program, name);
722
- * ```
723
- *
724
- * @param program
725
- * @param name
726
- * @returns
727
- */
728
- getAttribLocation(program: WebGLProgram, name: string) {
729
- const { gl } = this;
730
- return gl.getAttribLocation(program, name);
731
- }
732
-
733
- /**
734
- * ```
735
- * gl.getUniformLocation(program, name);
736
- * ```
737
- *
738
- * @param program
739
- * @param name
740
- * @returns
741
- */
742
- getUniformLocation(program: WebGLProgram, name: string) {
743
- const { gl } = this;
744
- return gl.getUniformLocation(program, name);
745
- }
746
-
747
- /**
748
- * ```
749
- * gl.enableVertexAttribArray(index);
750
- * ```
751
- *
752
- * @param index
753
- */
754
- enableVertexAttribArray(index: number) {
755
- const { gl } = this;
756
- gl.enableVertexAttribArray(index);
757
- }
758
-
759
- /**
760
- * ```
761
- * gl.disableVertexAttribArray(index);
762
- * ```
763
- *
764
- * @param index
765
- */
766
- disableVertexAttribArray(index: number) {
767
- const { gl } = this;
768
- gl.disableVertexAttribArray(index);
769
- }
770
-
771
- /**
772
- * ```
773
- * gl.createShader(type);
774
- * ```
775
- *
776
- * @param type
777
- * @returns
778
- */
779
- createShader(type: number) {
780
- const { gl } = this;
781
- return gl.createShader(type);
782
- }
783
-
784
- /**
785
- * ```
786
- * gl.compileShader(shader);
787
- * ```
788
- *
789
- * @param shader
790
- * @returns
791
- */
792
- compileShader(shader: WebGLShader) {
793
- const { gl } = this;
794
- gl.compileShader(shader);
795
- }
796
-
797
- /**
798
- * ```
799
- * gl.attachShader(program, shader);
800
- * ```
801
- *
802
- * @param program
803
- * @param shader
804
- */
805
- attachShader(program: WebGLProgram, shader: WebGLShader) {
806
- const { gl } = this;
807
- gl.attachShader(program, shader);
808
- }
809
-
810
- /**
811
- * ```
812
- * gl.linkProgram(program);
813
- * ```
814
- *
815
- * @param program
816
- */
817
- linkProgram(program: WebGLProgram) {
818
- const { gl } = this;
819
- gl.linkProgram(program);
820
- }
821
-
822
- /**
823
- * ```
824
- * gl.deleteProgram(shader);
825
- * ```
826
- *
827
- * @param shader
828
- */
829
- deleteProgram(shader: WebGLProgram) {
830
- const { gl } = this;
831
- gl.deleteProgram(shader);
832
- }
833
-
834
- /**
835
- * ```
836
- * gl.getShaderParameter(shader, pname);
837
- * ```
838
- *
839
- * @param shader
840
- * @param pname
841
- */
842
- getShaderParameter(shader: WebGLShader, pname: GLenum) {
843
- const { gl } = this;
844
- return gl.getShaderParameter(shader, pname);
845
- }
846
-
847
- /**
848
- * ```
849
- * gl.getShaderInfoLog(shader);
850
- * ```
851
- *
852
- * @param shader
853
- */
854
- getShaderInfoLog(shader: WebGLShader) {
855
- const { gl } = this;
856
- return gl.getShaderInfoLog(shader);
857
- }
858
-
859
- /**
860
- * ```
861
- * gl.createProgram();
862
- * ```
863
- *
864
- * @returns
865
- */
866
- createProgram() {
867
- const { gl } = this;
868
- return gl.createProgram();
869
- }
870
-
871
- /**
872
- * ```
873
- * gl.getProgramParameter(program, pname);
874
- * ```
875
- *
876
- * @param program
877
- * @param pname
878
- * @returns
879
- */
880
- getProgramParameter(program: WebGLProgram, pname: GLenum) {
881
- const { gl } = this;
882
- return gl.getProgramParameter(program, pname);
883
- }
884
-
885
- /**
886
- * ```
887
- * gl.getProgramInfoLog(program);
888
- * ```
889
- *
890
- * @param program
891
- * @returns
892
- */
893
- getProgramInfoLog(program: WebGLProgram) {
894
- const { gl } = this;
895
- return gl.getProgramInfoLog(program);
896
- }
897
-
898
- /**
899
- * ```
900
- * gl.shaderSource(shader, source);
901
- * ```
902
- *
903
- * @param shader
904
- * @param source
905
- */
906
- shaderSource(shader: WebGLShader, source: string) {
907
- const { gl } = this;
908
- gl.shaderSource(shader, source);
909
- }
910
-
911
- /**
912
- * ```
913
- * gl.deleteShader(shader);
914
- * ```
915
- *
916
- * @param shader
917
- */
918
- deleteShader(shader: WebGLShader) {
919
- const { gl } = this;
920
- gl.deleteShader(shader);
921
- }
922
- }
923
-
924
- // prettier-ignore
925
- type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
926
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
927
- MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
928
- ? true
929
- : false
930
- : false;
931
-
932
- // prettier-ignore
933
- export type UniformMethodMap = {
934
- [Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
935
- location: WebGLUniformLocation | null,
936
- ...args: infer T
937
- ) => void
938
- ? T
939
- : never;
940
- };
941
-
942
- /**
943
- * Compare two arrays for equality.
944
- *
945
- * @remarks
946
- * This function will not try to compare nested arrays or Float32Arrays and
947
- * instead will always return false when they are encountered.
948
- *
949
- * @param a
950
- * @param b
951
- * @returns
952
- */
953
- export function compareArrays<T>(a: T[], b: T[]): boolean {
954
- if (a.length !== b.length) {
955
- return false;
956
- }
957
- return a.every((v, i) => {
958
- // Don't bother to compare nested arrays or Float32Arrays
959
- if (Array.isArray(v) || v instanceof Float32Array) {
960
- return false;
961
- } else {
962
- return v === b[i];
963
- }
964
- });
965
- }
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
+ private programUniforms: WeakMap<
49
+ WebGLProgram,
50
+ Map<WebGLUniformLocation, any[]>
51
+ > = new WeakMap();
52
+ //#endregion Cached WebGL State
53
+
54
+ //#region Canvas
55
+ public readonly canvas;
56
+ //#endregion Canvas
57
+
58
+ //#region WebGL Enums
59
+ public readonly MAX_RENDERBUFFER_SIZE;
60
+ public readonly MAX_TEXTURE_SIZE;
61
+ public readonly MAX_VIEWPORT_DIMS;
62
+ public readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS;
63
+ public readonly MAX_TEXTURE_IMAGE_UNITS;
64
+ public readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS;
65
+ public readonly MAX_VERTEX_ATTRIBS;
66
+ public readonly MAX_VARYING_VECTORS;
67
+ public readonly MAX_VERTEX_UNIFORM_VECTORS;
68
+ public readonly MAX_FRAGMENT_UNIFORM_VECTORS;
69
+ public readonly TEXTURE_MAG_FILTER;
70
+ public readonly TEXTURE_MIN_FILTER;
71
+ public readonly TEXTURE_WRAP_S;
72
+ public readonly TEXTURE_WRAP_T;
73
+ public readonly LINEAR;
74
+ public readonly CLAMP_TO_EDGE;
75
+ public readonly RGBA;
76
+ public readonly UNSIGNED_BYTE;
77
+ public readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL;
78
+ public readonly FLOAT;
79
+ public readonly TRIANGLES;
80
+ public readonly UNSIGNED_SHORT;
81
+ public readonly ONE;
82
+ public readonly ONE_MINUS_SRC_ALPHA;
83
+ public readonly VERTEX_SHADER;
84
+ public readonly FRAGMENT_SHADER;
85
+ public readonly STATIC_DRAW;
86
+ public readonly COMPILE_STATUS;
87
+ public readonly LINK_STATUS;
88
+ public readonly DYNAMIC_DRAW;
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.FLOAT = gl.FLOAT;
165
+ this.TRIANGLES = gl.TRIANGLES;
166
+ this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
167
+ this.ONE = gl.ONE;
168
+ this.ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
169
+ this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
170
+ this.TRIANGLES = gl.TRIANGLES;
171
+ this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
172
+ this.VERTEX_SHADER = gl.VERTEX_SHADER;
173
+ this.FRAGMENT_SHADER = gl.FRAGMENT_SHADER;
174
+ this.STATIC_DRAW = gl.STATIC_DRAW;
175
+ this.COMPILE_STATUS = gl.COMPILE_STATUS;
176
+ this.LINK_STATUS = gl.LINK_STATUS;
177
+ this.DYNAMIC_DRAW = gl.DYNAMIC_DRAW;
178
+ }
179
+ /**
180
+ * Returns true if the WebGL context is WebGL2
181
+ *
182
+ * @returns
183
+ */
184
+ isWebGl2() {
185
+ return isWebGl2(this.gl);
186
+ }
187
+
188
+ /**
189
+ * ```
190
+ * gl.activeTexture(textureUnit + gl.TEXTURE0);
191
+ * ```
192
+ *
193
+ * @remarks
194
+ * **WebGL Difference**: `textureUnit` is based from 0, not `gl.TEXTURE0`.
195
+ *
196
+ * @param textureUnit
197
+ */
198
+ activeTexture(textureUnit: number) {
199
+ const { gl } = this;
200
+ if (this.activeTextureUnit !== textureUnit) {
201
+ gl.activeTexture(textureUnit + gl.TEXTURE0);
202
+ this.activeTextureUnit = textureUnit;
203
+ }
204
+ }
205
+
206
+ /**
207
+ * ```
208
+ * gl.bindTexture(gl.TEXTURE_2D, texture);
209
+ * ```
210
+ * @remarks
211
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
212
+ *
213
+ * @param texture
214
+ */
215
+ bindTexture(texture: WebGLTexture | null) {
216
+ const { gl, activeTextureUnit, texture2dUnits } = this;
217
+
218
+ if (texture2dUnits[activeTextureUnit] === texture) {
219
+ return;
220
+ }
221
+ texture2dUnits[activeTextureUnit] = texture;
222
+
223
+ gl.bindTexture(this.gl.TEXTURE_2D, texture);
224
+ }
225
+
226
+ private _getActiveTexture(): WebGLTexture | null {
227
+ const { activeTextureUnit, texture2dUnits } = this;
228
+ return texture2dUnits[activeTextureUnit]!;
229
+ }
230
+
231
+ /**
232
+ * ```
233
+ * gl.texParameteri(gl.TEXTURE_2D, pname, param);
234
+ * ```
235
+ * @remarks
236
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
237
+ *
238
+ * @param pname
239
+ * @param param
240
+ * @returns
241
+ */
242
+ texParameteri(pname: number, param: number) {
243
+ const { gl, texture2dParams } = this;
244
+
245
+ const activeTexture = this._getActiveTexture();
246
+ if (!activeTexture) {
247
+ throw new Error('No active texture');
248
+ }
249
+ let textureParams = texture2dParams.get(activeTexture);
250
+ if (!textureParams) {
251
+ textureParams = {};
252
+ texture2dParams.set(activeTexture, textureParams);
253
+ }
254
+ if (textureParams[pname] === param) {
255
+ return;
256
+ }
257
+ textureParams[pname] = param;
258
+ gl.texParameteri(gl.TEXTURE_2D, pname, param);
259
+ }
260
+
261
+ /**
262
+ * ```
263
+ * gl.texImage2D(
264
+ * gl.TEXTURE_2D,
265
+ * level,
266
+ * internalFormat,
267
+ * width,
268
+ * height,
269
+ * border,
270
+ * format,
271
+ * type,
272
+ * pixels,
273
+ * );
274
+ * ```
275
+ * @remarks
276
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
277
+ *
278
+ * @param level
279
+ * @param internalFormat
280
+ * @param width
281
+ * @param height
282
+ * @param border
283
+ * @param format
284
+ * @param type
285
+ * @param pixels
286
+ */
287
+ texImage2D(
288
+ level: GLint,
289
+ internalformat: GLint,
290
+ width: GLsizei,
291
+ height: GLsizei,
292
+ border: GLint,
293
+ format: GLenum,
294
+ type: GLenum,
295
+ pixels: ArrayBufferView | null,
296
+ ): void;
297
+ texImage2D(
298
+ level: GLint,
299
+ internalformat: GLint,
300
+ format: GLenum,
301
+ type: GLenum,
302
+ source: TexImageSource,
303
+ ): void;
304
+ texImage2D(
305
+ level: any,
306
+ internalFormat: any,
307
+ widthOrFormat: any,
308
+ heightOrType: any,
309
+ borderOrSource: any,
310
+ format?: any,
311
+ type?: any,
312
+ pixels?: any,
313
+ ) {
314
+ const { gl } = this;
315
+ if (format) {
316
+ gl.texImage2D(
317
+ gl.TEXTURE_2D,
318
+ level,
319
+ internalFormat,
320
+ widthOrFormat,
321
+ heightOrType,
322
+ borderOrSource,
323
+ format,
324
+ type,
325
+ pixels,
326
+ );
327
+ } else {
328
+ gl.texImage2D(
329
+ gl.TEXTURE_2D,
330
+ level,
331
+ internalFormat,
332
+ widthOrFormat,
333
+ heightOrType,
334
+ borderOrSource,
335
+ );
336
+ }
337
+ }
338
+ /**
339
+ * ```
340
+ * gl.compressedTexImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, data);
341
+ * ```
342
+ *
343
+ * @remarks
344
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
345
+ */
346
+
347
+ compressedTexImage2D(
348
+ level: GLint,
349
+ internalformat: GLenum,
350
+ width: GLsizei,
351
+ height: GLsizei,
352
+ border: GLint,
353
+ data?: ArrayBufferView,
354
+ ): void {
355
+ const { gl } = this;
356
+ gl.compressedTexImage2D(
357
+ gl.TEXTURE_2D,
358
+ level,
359
+ internalformat,
360
+ width,
361
+ height,
362
+ border,
363
+ data as ArrayBufferView,
364
+ );
365
+ }
366
+ /**
367
+ * ```
368
+ * gl.pixelStorei(pname, param);
369
+ * ```
370
+ *
371
+ * @param pname
372
+ * @param param
373
+ */
374
+ pixelStorei(pname: GLenum, param: GLint | GLboolean) {
375
+ const { gl } = this;
376
+ gl.pixelStorei(pname, param);
377
+ }
378
+
379
+ /**
380
+ * ```
381
+ * gl.generateMipmap(gl.TEXTURE_2D);
382
+ * ```
383
+ *
384
+ * @remarks
385
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
386
+ */
387
+ generateMipmap() {
388
+ const { gl } = this;
389
+ gl.generateMipmap(gl.TEXTURE_2D);
390
+ }
391
+
392
+ /**
393
+ * ```
394
+ * gl.createTexture();
395
+ * ```
396
+ *
397
+ * @returns
398
+ */
399
+ createTexture() {
400
+ const { gl } = this;
401
+ return gl.createTexture();
402
+ }
403
+
404
+ /**
405
+ * ```
406
+ * gl.deleteTexture(texture);
407
+ * ```
408
+ *
409
+ * @param texture
410
+ */
411
+ deleteTexture(texture: WebGLTexture | null) {
412
+ const { gl } = this;
413
+ if (texture) {
414
+ this.texture2dParams.delete(texture);
415
+ }
416
+ gl.deleteTexture(texture);
417
+ }
418
+
419
+ /**
420
+ * ```
421
+ * gl.viewport(x, y, width, height);
422
+ * ```
423
+ */
424
+ viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
425
+ const { gl } = this;
426
+ gl.viewport(x, y, width, height);
427
+ }
428
+
429
+ /**
430
+ * ```
431
+ * gl.clearColor(red, green, blue, alpha);
432
+ * ```
433
+ *
434
+ * @param red
435
+ * @param green
436
+ * @param blue
437
+ * @param alpha
438
+ */
439
+ clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
440
+ const { gl } = this;
441
+ gl.clearColor(red, green, blue, alpha);
442
+ }
443
+
444
+ /**
445
+ * ```
446
+ * gl["enable"|"disable"](gl.SCISSOR_TEST);
447
+ * ```
448
+ * @param enable
449
+ */
450
+ setScissorTest(enable: boolean) {
451
+ const { gl, scissorEnabled } = this;
452
+ if (enable === scissorEnabled) {
453
+ return;
454
+ }
455
+ if (enable) {
456
+ gl.enable(gl.SCISSOR_TEST);
457
+ } else {
458
+ gl.disable(gl.SCISSOR_TEST);
459
+ }
460
+ this.scissorEnabled = enable;
461
+ }
462
+
463
+ /**
464
+ * ```
465
+ * gl.scissor(x, y, width, height);
466
+ * ```
467
+ *
468
+ * @param x
469
+ * @param y
470
+ * @param width
471
+ * @param height
472
+ */
473
+ scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
474
+ const { gl, scissorX, scissorY, scissorWidth, scissorHeight } = this;
475
+ if (
476
+ x !== scissorX ||
477
+ y !== scissorY ||
478
+ width !== scissorWidth ||
479
+ height !== scissorHeight
480
+ ) {
481
+ gl.scissor(x, y, width, height);
482
+ this.scissorX = x;
483
+ this.scissorY = y;
484
+ this.scissorWidth = width;
485
+ this.scissorHeight = height;
486
+ }
487
+ }
488
+
489
+ /**
490
+ * ```
491
+ * gl["enable"|"disable"](gl.BLEND);
492
+ * ```
493
+ *
494
+ * @param blend
495
+ * @returns
496
+ */
497
+ setBlend(blend: boolean) {
498
+ const { gl, blendEnabled } = this;
499
+ if (blend === blendEnabled) {
500
+ return;
501
+ }
502
+ if (blend) {
503
+ gl.enable(gl.BLEND);
504
+ } else {
505
+ gl.disable(gl.BLEND);
506
+ }
507
+ this.blendEnabled = blend;
508
+ }
509
+
510
+ /**
511
+ * ```
512
+ * gl.blendFunc(src, dst);
513
+ * ```
514
+ *
515
+ * @param src
516
+ * @param dst
517
+ */
518
+ blendFunc(src: GLenum, dst: GLenum) {
519
+ const { gl, blendSrcRgb, blendDstRgb, blendSrcAlpha, blendDstAlpha } = this;
520
+ if (
521
+ src !== blendSrcRgb ||
522
+ dst !== blendDstRgb ||
523
+ src !== blendSrcAlpha ||
524
+ dst !== blendDstAlpha
525
+ ) {
526
+ gl.blendFunc(src, dst);
527
+ this.blendSrcRgb = src;
528
+ this.blendDstRgb = dst;
529
+ this.blendSrcAlpha = src;
530
+ this.blendDstAlpha = dst;
531
+ }
532
+ }
533
+
534
+ /**
535
+ * ```
536
+ * createBuffer();
537
+ * ```
538
+ *
539
+ * @returns
540
+ */
541
+ createBuffer() {
542
+ const { gl } = this;
543
+ return gl.createBuffer();
544
+ }
545
+
546
+ /**
547
+ * ```
548
+ * gl.clear(gl.COLOR_BUFFER_BIT);
549
+ * ```
550
+ *
551
+ * @remarks
552
+ * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
553
+ */
554
+ clear() {
555
+ const { gl } = this;
556
+ gl.clear(gl.COLOR_BUFFER_BIT);
557
+ }
558
+
559
+ /**
560
+ * ```
561
+ * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
562
+ * gl.bufferData(gl.ARRAY_BUFFER, data, usage);
563
+ * ```
564
+ *
565
+ * @remarks
566
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
567
+ *
568
+ * @param buffer
569
+ * @param data
570
+ * @param usage
571
+ */
572
+ arrayBufferData(
573
+ buffer: WebGLBuffer | null,
574
+ data: ArrayBufferView,
575
+ usage: GLenum,
576
+ ) {
577
+ const { gl, boundArrayBuffer } = this;
578
+ if (boundArrayBuffer !== buffer) {
579
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
580
+ this.boundArrayBuffer = buffer;
581
+ }
582
+ gl.bufferData(gl.ARRAY_BUFFER, data, usage);
583
+ }
584
+
585
+ /**
586
+ * ```
587
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
588
+ * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
589
+ * ```
590
+ * @remarks
591
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
592
+ *
593
+ * @param buffer
594
+ * @param data
595
+ * @param usage
596
+ */
597
+ elementArrayBufferData(
598
+ buffer: WebGLBuffer | null,
599
+ data: ArrayBufferView,
600
+ usage: GLenum,
601
+ ) {
602
+ const { gl, boundElementArrayBuffer } = this;
603
+ if (boundElementArrayBuffer !== buffer) {
604
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
605
+ this.boundElementArrayBuffer = buffer;
606
+ }
607
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
608
+ }
609
+
610
+ /**
611
+ * ```
612
+ * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
613
+ * gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
614
+ * ```
615
+ *
616
+ * @remarks
617
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
618
+ *
619
+ * @param buffer
620
+ * @param index
621
+ * @param size
622
+ * @param type
623
+ * @param normalized
624
+ * @param stride
625
+ * @param offset
626
+ */
627
+ vertexAttribPointer(
628
+ buffer: WebGLBuffer,
629
+ index: GLuint,
630
+ size: GLint,
631
+ type: GLenum,
632
+ normalized: GLboolean,
633
+ stride: GLsizei,
634
+ offset: GLintptr,
635
+ ) {
636
+ const { gl, boundArrayBuffer } = this;
637
+ if (boundArrayBuffer !== buffer) {
638
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
639
+ this.boundArrayBuffer = buffer;
640
+ }
641
+ gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
642
+ }
643
+
644
+ /**
645
+ * ```
646
+ * gl.useProgram(program);
647
+ * ```
648
+ *
649
+ * @param program
650
+ * @returns
651
+ */
652
+ useProgram(program: WebGLProgram | null) {
653
+ const { gl, curProgram } = this;
654
+ if (curProgram === program) {
655
+ return;
656
+ }
657
+ gl.useProgram(program);
658
+ this.curProgram = program;
659
+ }
660
+
661
+ setUniform<T extends keyof UniformMethodMap>(
662
+ type: T,
663
+ location: WebGLUniformLocation,
664
+ ...args: UniformMethodMap[T]
665
+ ) {
666
+ const { gl, programUniforms } = this;
667
+ let uniforms = programUniforms.get(this.curProgram!);
668
+ if (!uniforms) {
669
+ uniforms = new Map();
670
+ programUniforms.set(this.curProgram!, uniforms);
671
+ }
672
+ const uniformArgs = uniforms.get(location);
673
+ if (!uniformArgs || !compareArrays(uniformArgs, args)) {
674
+ uniforms.set(location, args);
675
+ gl[type](location, ...(args as [never, never, never, never]));
676
+ }
677
+ }
678
+
679
+ /**
680
+ * ```
681
+ * gl.getParameter(pname);
682
+ * ```
683
+ *
684
+ * @param pname
685
+ * @returns
686
+ */
687
+ getParameter(pname: GLenum): any {
688
+ const { gl } = this;
689
+ return gl.getParameter(pname);
690
+ }
691
+
692
+ /**
693
+ * ```
694
+ * gl.drawElements(mode, count, type, offset);
695
+ * ```
696
+ *
697
+ * @param mode
698
+ * @param count
699
+ * @param type
700
+ * @param offset
701
+ */
702
+ drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
703
+ const { gl } = this;
704
+ gl.drawElements(mode, count, type, offset);
705
+ }
706
+
707
+ /**
708
+ * ```
709
+ * gl.drawArrays(mode, first, count);
710
+ * ```
711
+ *
712
+ * @param name
713
+ * @returns
714
+ */
715
+ getExtension(name: string) {
716
+ const { gl } = this;
717
+ return gl.getExtension(name);
718
+ }
719
+
720
+ /**
721
+ * ```
722
+ * gl.createVertexArray();
723
+ * ```
724
+ *
725
+ * @returns
726
+ */
727
+ createVertexArray() {
728
+ const { gl } = this;
729
+ assertTruthy(gl instanceof WebGL2RenderingContext);
730
+ return gl.createVertexArray();
731
+ }
732
+
733
+ /**
734
+ * ```
735
+ * gl.bindVertexArray(vertexArray);
736
+ * ```
737
+ *
738
+ * @param vertexArray
739
+ */
740
+ bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
741
+ const { gl } = this;
742
+ assertTruthy(gl instanceof WebGL2RenderingContext);
743
+ gl.bindVertexArray(vertexArray);
744
+ }
745
+
746
+ /**
747
+ * ```
748
+ * gl.getAttribLocation(program, name);
749
+ * ```
750
+ *
751
+ * @param program
752
+ * @param name
753
+ * @returns
754
+ */
755
+ getAttribLocation(program: WebGLProgram, name: string) {
756
+ const { gl } = this;
757
+ return gl.getAttribLocation(program, name);
758
+ }
759
+
760
+ /**
761
+ * ```
762
+ * gl.getUniformLocation(program, name);
763
+ * ```
764
+ *
765
+ * @param program
766
+ * @param name
767
+ * @returns
768
+ */
769
+ getUniformLocation(program: WebGLProgram, name: string) {
770
+ const { gl } = this;
771
+ return gl.getUniformLocation(program, name);
772
+ }
773
+
774
+ /**
775
+ * ```
776
+ * gl.enableVertexAttribArray(index);
777
+ * ```
778
+ *
779
+ * @param index
780
+ */
781
+ enableVertexAttribArray(index: number) {
782
+ const { gl } = this;
783
+ gl.enableVertexAttribArray(index);
784
+ }
785
+
786
+ /**
787
+ * ```
788
+ * gl.disableVertexAttribArray(index);
789
+ * ```
790
+ *
791
+ * @param index
792
+ */
793
+ disableVertexAttribArray(index: number) {
794
+ const { gl } = this;
795
+ gl.disableVertexAttribArray(index);
796
+ }
797
+
798
+ /**
799
+ * ```
800
+ * gl.createShader(type);
801
+ * ```
802
+ *
803
+ * @param type
804
+ * @returns
805
+ */
806
+ createShader(type: number) {
807
+ const { gl } = this;
808
+ return gl.createShader(type);
809
+ }
810
+
811
+ /**
812
+ * ```
813
+ * gl.compileShader(shader);
814
+ * ```
815
+ *
816
+ * @param shader
817
+ * @returns
818
+ */
819
+ compileShader(shader: WebGLShader) {
820
+ const { gl } = this;
821
+ gl.compileShader(shader);
822
+ }
823
+
824
+ /**
825
+ * ```
826
+ * gl.attachShader(program, shader);
827
+ * ```
828
+ *
829
+ * @param program
830
+ * @param shader
831
+ */
832
+ attachShader(program: WebGLProgram, shader: WebGLShader) {
833
+ const { gl } = this;
834
+ gl.attachShader(program, shader);
835
+ }
836
+
837
+ /**
838
+ * ```
839
+ * gl.linkProgram(program);
840
+ * ```
841
+ *
842
+ * @param program
843
+ */
844
+ linkProgram(program: WebGLProgram) {
845
+ const { gl } = this;
846
+ gl.linkProgram(program);
847
+ }
848
+
849
+ /**
850
+ * ```
851
+ * gl.deleteProgram(shader);
852
+ * ```
853
+ *
854
+ * @param shader
855
+ */
856
+ deleteProgram(shader: WebGLProgram) {
857
+ const { gl } = this;
858
+ gl.deleteProgram(shader);
859
+ }
860
+
861
+ /**
862
+ * ```
863
+ * gl.getShaderParameter(shader, pname);
864
+ * ```
865
+ *
866
+ * @param shader
867
+ * @param pname
868
+ */
869
+ getShaderParameter(shader: WebGLShader, pname: GLenum) {
870
+ const { gl } = this;
871
+ return gl.getShaderParameter(shader, pname);
872
+ }
873
+
874
+ /**
875
+ * ```
876
+ * gl.getShaderInfoLog(shader);
877
+ * ```
878
+ *
879
+ * @param shader
880
+ */
881
+ getShaderInfoLog(shader: WebGLShader) {
882
+ const { gl } = this;
883
+ return gl.getShaderInfoLog(shader);
884
+ }
885
+
886
+ /**
887
+ * ```
888
+ * gl.createProgram();
889
+ * ```
890
+ *
891
+ * @returns
892
+ */
893
+ createProgram() {
894
+ const { gl } = this;
895
+ return gl.createProgram();
896
+ }
897
+
898
+ /**
899
+ * ```
900
+ * gl.getProgramParameter(program, pname);
901
+ * ```
902
+ *
903
+ * @param program
904
+ * @param pname
905
+ * @returns
906
+ */
907
+ getProgramParameter(program: WebGLProgram, pname: GLenum) {
908
+ const { gl } = this;
909
+ return gl.getProgramParameter(program, pname);
910
+ }
911
+
912
+ /**
913
+ * ```
914
+ * gl.getProgramInfoLog(program);
915
+ * ```
916
+ *
917
+ * @param program
918
+ * @returns
919
+ */
920
+ getProgramInfoLog(program: WebGLProgram) {
921
+ const { gl } = this;
922
+ return gl.getProgramInfoLog(program);
923
+ }
924
+
925
+ /**
926
+ * ```
927
+ * gl.shaderSource(shader, source);
928
+ * ```
929
+ *
930
+ * @param shader
931
+ * @param source
932
+ */
933
+ shaderSource(shader: WebGLShader, source: string) {
934
+ const { gl } = this;
935
+ gl.shaderSource(shader, source);
936
+ }
937
+
938
+ /**
939
+ * ```
940
+ * gl.deleteShader(shader);
941
+ * ```
942
+ *
943
+ * @param shader
944
+ */
945
+ deleteShader(shader: WebGLShader) {
946
+ const { gl } = this;
947
+ gl.deleteShader(shader);
948
+ }
949
+ }
950
+
951
+ // prettier-ignore
952
+ type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
953
+ ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
954
+ MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
955
+ ? true
956
+ : false
957
+ : false;
958
+
959
+ // prettier-ignore
960
+ export type UniformMethodMap = {
961
+ [Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
962
+ location: WebGLUniformLocation | null,
963
+ ...args: infer T
964
+ ) => void
965
+ ? T
966
+ : never;
967
+ };
968
+
969
+ /**
970
+ * Compare two arrays for equality.
971
+ *
972
+ * @remarks
973
+ * This function will not try to compare nested arrays or Float32Arrays and
974
+ * instead will always return false when they are encountered.
975
+ *
976
+ * @param a
977
+ * @param b
978
+ * @returns
979
+ */
980
+ export function compareArrays<T>(a: T[], b: T[]): boolean {
981
+ if (a.length !== b.length) {
982
+ return false;
983
+ }
984
+ return a.every((v, i) => {
985
+ // Don't bother to compare nested arrays or Float32Arrays
986
+ if (Array.isArray(v) || v instanceof Float32Array) {
987
+ return false;
988
+ } else {
989
+ return v === b[i];
990
+ }
991
+ });
992
+ }