@lightningjs/renderer 2.12.0 → 2.13.0

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