@lightningjs/renderer 2.16.0 → 3.0.0-beta10

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