@lightningjs/renderer 2.13.0 → 3.0.0-beta1

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