@solidtv/renderer 1.0.0

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