@lightningjs/renderer 2.14.2 → 2.14.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (296) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +147 -147
  4. package/dist/src/core/CoreTextureManager.d.ts +1 -1
  5. package/dist/src/core/CoreTextureManager.js +4 -13
  6. package/dist/src/core/CoreTextureManager.js.map +1 -1
  7. package/dist/src/core/TextureMemoryManager.d.ts +1 -1
  8. package/dist/src/core/TextureMemoryManager.js +25 -2
  9. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  10. package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js +2 -0
  11. package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js.map +1 -0
  12. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  13. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  14. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  15. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  16. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  17. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  18. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  19. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  20. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  21. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  22. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  23. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  24. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  25. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  26. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  27. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  28. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  29. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  30. package/dist/src/core/textures/ImageTexture.d.ts +1 -1
  31. package/dist/src/core/textures/ImageTexture.js +1 -1
  32. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  33. package/dist/src/core/textures/Texture.d.ts +4 -0
  34. package/dist/src/core/textures/Texture.js +9 -3
  35. package/dist/src/core/textures/Texture.js.map +1 -1
  36. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  37. package/exports/canvas.ts +39 -39
  38. package/exports/index.ts +89 -89
  39. package/exports/inspector.ts +24 -24
  40. package/exports/utils.ts +44 -44
  41. package/exports/webgl.ts +38 -38
  42. package/package.json +1 -1
  43. package/scripts/please-use-pnpm.js +13 -13
  44. package/src/common/CommonTypes.ts +146 -146
  45. package/src/common/EventEmitter.ts +77 -77
  46. package/src/common/IAnimationController.ts +92 -92
  47. package/src/common/IEventEmitter.ts +28 -28
  48. package/src/core/CoreNode.test.ts +202 -202
  49. package/src/core/CoreNode.ts +2468 -2468
  50. package/src/core/CoreShaderManager.ts +292 -292
  51. package/src/core/CoreTextNode.ts +455 -455
  52. package/src/core/CoreTextureManager.ts +544 -558
  53. package/src/core/Stage.ts +743 -743
  54. package/src/core/TextureMemoryManager.ts +431 -404
  55. package/src/core/animations/AnimationManager.ts +38 -38
  56. package/src/core/animations/CoreAnimation.ts +347 -347
  57. package/src/core/animations/CoreAnimationController.ts +166 -166
  58. package/src/core/lib/ContextSpy.ts +41 -41
  59. package/src/core/lib/ImageWorker.ts +280 -280
  60. package/src/core/lib/Matrix3d.ts +244 -244
  61. package/src/core/lib/RenderCoords.ts +86 -86
  62. package/src/core/lib/WebGlContextWrapper.ts +1332 -1332
  63. package/src/core/lib/textureCompression.ts +152 -152
  64. package/src/core/lib/textureSvg.ts +78 -78
  65. package/src/core/lib/utils.ts +384 -384
  66. package/src/core/lib/validateImageBitmap.ts +76 -76
  67. package/src/core/platform.ts +63 -63
  68. package/src/core/renderers/CoreContextTexture.ts +43 -43
  69. package/src/core/renderers/CoreRenderOp.ts +22 -22
  70. package/src/core/renderers/CoreRenderer.ts +115 -115
  71. package/src/core/renderers/CoreShader.ts +41 -41
  72. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +375 -375
  73. package/src/core/renderers/canvas/CanvasCoreTexture.ts +153 -153
  74. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  75. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  76. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  77. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +86 -86
  78. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  79. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +301 -301
  80. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  81. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +821 -821
  82. package/src/core/renderers/webgl/WebGlCoreShader.ts +365 -365
  83. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  84. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  85. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  86. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  87. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  88. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  89. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  90. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  91. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  92. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  93. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  94. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  95. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  96. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  97. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  98. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  99. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  100. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  101. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  102. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  103. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  104. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  105. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  106. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  107. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  108. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  109. package/src/core/text-rendering/TrFontManager.ts +183 -183
  110. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  111. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  112. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  113. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  114. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  115. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  116. package/src/core/text-rendering/font-face-types/utils.ts +39 -39
  117. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  118. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +808 -808
  119. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  120. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  121. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  122. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  123. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  124. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  125. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  126. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  127. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +408 -408
  128. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  129. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  130. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  131. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  132. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  133. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  134. package/src/core/textures/ColorTexture.ts +102 -102
  135. package/src/core/textures/ImageTexture.ts +400 -399
  136. package/src/core/textures/NoiseTexture.ts +104 -104
  137. package/src/core/textures/RenderTexture.ts +85 -85
  138. package/src/core/textures/SubTexture.ts +205 -205
  139. package/src/core/textures/Texture.ts +376 -367
  140. package/src/core/utils.ts +227 -227
  141. package/src/env.d.ts +7 -7
  142. package/src/main-api/DynamicShaderController.ts +104 -104
  143. package/src/main-api/INode.ts +101 -101
  144. package/src/main-api/Inspector.ts +522 -522
  145. package/src/main-api/Renderer.ts +751 -751
  146. package/src/main-api/ShaderController.ts +80 -80
  147. package/src/main-api/utils.ts +45 -45
  148. package/src/utils.ts +248 -248
  149. package/COPYING +0 -1
  150. package/dist/exports/canvas-shaders.d.ts +0 -10
  151. package/dist/exports/canvas-shaders.js +0 -27
  152. package/dist/exports/canvas-shaders.js.map +0 -1
  153. package/dist/exports/webgl-shaders.d.ts +0 -11
  154. package/dist/exports/webgl-shaders.js +0 -28
  155. package/dist/exports/webgl-shaders.js.map +0 -1
  156. package/dist/src/core/platforms/Platform.d.ts +0 -37
  157. package/dist/src/core/platforms/Platform.js +0 -22
  158. package/dist/src/core/platforms/Platform.js.map +0 -1
  159. package/dist/src/core/platforms/web/WebPlatform.d.ts +0 -9
  160. package/dist/src/core/platforms/web/WebPlatform.js +0 -58
  161. package/dist/src/core/platforms/web/WebPlatform.js.map +0 -1
  162. package/dist/src/core/renderers/CoreShaderNode.d.ts +0 -58
  163. package/dist/src/core/renderers/CoreShaderNode.js +0 -104
  164. package/dist/src/core/renderers/CoreShaderNode.js.map +0 -1
  165. package/dist/src/core/renderers/CoreShaderProgram.d.ts +0 -4
  166. package/dist/src/core/renderers/CoreShaderProgram.js +0 -20
  167. package/dist/src/core/renderers/CoreShaderProgram.js.map +0 -1
  168. package/dist/src/core/renderers/canvas/CanvasRenderer.d.ts +0 -36
  169. package/dist/src/core/renderers/canvas/CanvasRenderer.js +0 -212
  170. package/dist/src/core/renderers/canvas/CanvasRenderer.js.map +0 -1
  171. package/dist/src/core/renderers/canvas/CanvasShaderNode.d.ts +0 -21
  172. package/dist/src/core/renderers/canvas/CanvasShaderNode.js +0 -60
  173. package/dist/src/core/renderers/canvas/CanvasShaderNode.js.map +0 -1
  174. package/dist/src/core/renderers/canvas/CanvasTexture.d.ts +0 -16
  175. package/dist/src/core/renderers/canvas/CanvasTexture.js +0 -124
  176. package/dist/src/core/renderers/canvas/CanvasTexture.js.map +0 -1
  177. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.d.ts +0 -12
  178. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js +0 -55
  179. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js.map +0 -1
  180. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.d.ts +0 -9
  181. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js +0 -38
  182. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js.map +0 -1
  183. package/dist/src/core/renderers/webgl/WebGlCtxTexture.d.ts +0 -57
  184. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js +0 -227
  185. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js.map +0 -1
  186. package/dist/src/core/renderers/webgl/WebGlRenderOp.d.ts +0 -44
  187. package/dist/src/core/renderers/webgl/WebGlRenderOp.js +0 -118
  188. package/dist/src/core/renderers/webgl/WebGlRenderOp.js.map +0 -1
  189. package/dist/src/core/renderers/webgl/WebGlRenderer.d.ts +0 -135
  190. package/dist/src/core/renderers/webgl/WebGlRenderer.js +0 -571
  191. package/dist/src/core/renderers/webgl/WebGlRenderer.js.map +0 -1
  192. package/dist/src/core/renderers/webgl/WebGlShaderNode.d.ts +0 -222
  193. package/dist/src/core/renderers/webgl/WebGlShaderNode.js +0 -334
  194. package/dist/src/core/renderers/webgl/WebGlShaderNode.js.map +0 -1
  195. package/dist/src/core/renderers/webgl/WebGlShaderProgram.d.ts +0 -35
  196. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js +0 -201
  197. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js.map +0 -1
  198. package/dist/src/core/shaders/canvas/Border.d.ts +0 -9
  199. package/dist/src/core/shaders/canvas/Border.js +0 -57
  200. package/dist/src/core/shaders/canvas/Border.js.map +0 -1
  201. package/dist/src/core/shaders/canvas/HolePunch.d.ts +0 -7
  202. package/dist/src/core/shaders/canvas/HolePunch.js +0 -38
  203. package/dist/src/core/shaders/canvas/HolePunch.js.map +0 -1
  204. package/dist/src/core/shaders/canvas/LinearGradient.d.ts +0 -10
  205. package/dist/src/core/shaders/canvas/LinearGradient.js +0 -46
  206. package/dist/src/core/shaders/canvas/LinearGradient.js.map +0 -1
  207. package/dist/src/core/shaders/canvas/RadialGradient.d.ts +0 -11
  208. package/dist/src/core/shaders/canvas/RadialGradient.js +0 -68
  209. package/dist/src/core/shaders/canvas/RadialGradient.js.map +0 -1
  210. package/dist/src/core/shaders/canvas/Rounded.d.ts +0 -7
  211. package/dist/src/core/shaders/canvas/Rounded.js +0 -33
  212. package/dist/src/core/shaders/canvas/Rounded.js.map +0 -1
  213. package/dist/src/core/shaders/canvas/RoundedWithBorder.d.ts +0 -7
  214. package/dist/src/core/shaders/canvas/RoundedWithBorder.js +0 -41
  215. package/dist/src/core/shaders/canvas/RoundedWithBorder.js.map +0 -1
  216. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.d.ts +0 -8
  217. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js +0 -39
  218. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js.map +0 -1
  219. package/dist/src/core/shaders/canvas/RoundedWithShadow.d.ts +0 -7
  220. package/dist/src/core/shaders/canvas/RoundedWithShadow.js +0 -38
  221. package/dist/src/core/shaders/canvas/RoundedWithShadow.js.map +0 -1
  222. package/dist/src/core/shaders/canvas/Shadow.d.ts +0 -8
  223. package/dist/src/core/shaders/canvas/Shadow.js +0 -31
  224. package/dist/src/core/shaders/canvas/Shadow.js.map +0 -1
  225. package/dist/src/core/shaders/canvas/utils/render.d.ts +0 -5
  226. package/dist/src/core/shaders/canvas/utils/render.js +0 -84
  227. package/dist/src/core/shaders/canvas/utils/render.js.map +0 -1
  228. package/dist/src/core/shaders/templates/BorderTemplate.d.ts +0 -37
  229. package/dist/src/core/shaders/templates/BorderTemplate.js +0 -73
  230. package/dist/src/core/shaders/templates/BorderTemplate.js.map +0 -1
  231. package/dist/src/core/shaders/templates/HolePunchTemplate.d.ts +0 -46
  232. package/dist/src/core/shaders/templates/HolePunchTemplate.js +0 -35
  233. package/dist/src/core/shaders/templates/HolePunchTemplate.js.map +0 -1
  234. package/dist/src/core/shaders/templates/LinearGradientTemplate.d.ts +0 -23
  235. package/dist/src/core/shaders/templates/LinearGradientTemplate.js +0 -47
  236. package/dist/src/core/shaders/templates/LinearGradientTemplate.js.map +0 -1
  237. package/dist/src/core/shaders/templates/RadialGradientTemplate.d.ts +0 -31
  238. package/dist/src/core/shaders/templates/RadialGradientTemplate.js +0 -49
  239. package/dist/src/core/shaders/templates/RadialGradientTemplate.js.map +0 -1
  240. package/dist/src/core/shaders/templates/RoundedTemplate.d.ts +0 -29
  241. package/dist/src/core/shaders/templates/RoundedTemplate.js +0 -67
  242. package/dist/src/core/shaders/templates/RoundedTemplate.js.map +0 -1
  243. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.d.ts +0 -7
  244. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.js +0 -24
  245. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.js.map +0 -1
  246. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.d.ts +0 -6
  247. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.js +0 -23
  248. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.js.map +0 -1
  249. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.d.ts +0 -6
  250. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.js +0 -23
  251. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.js.map +0 -1
  252. package/dist/src/core/shaders/templates/ShadowTemplate.d.ts +0 -34
  253. package/dist/src/core/shaders/templates/ShadowTemplate.js +0 -66
  254. package/dist/src/core/shaders/templates/ShadowTemplate.js.map +0 -1
  255. package/dist/src/core/shaders/templates/shaderUtils.d.ts +0 -5
  256. package/dist/src/core/shaders/templates/shaderUtils.js +0 -41
  257. package/dist/src/core/shaders/templates/shaderUtils.js.map +0 -1
  258. package/dist/src/core/shaders/webgl/Border.d.ts +0 -3
  259. package/dist/src/core/shaders/webgl/Border.js +0 -110
  260. package/dist/src/core/shaders/webgl/Border.js.map +0 -1
  261. package/dist/src/core/shaders/webgl/Default.d.ts +0 -2
  262. package/dist/src/core/shaders/webgl/Default.js +0 -86
  263. package/dist/src/core/shaders/webgl/Default.js.map +0 -1
  264. package/dist/src/core/shaders/webgl/DefaultBatched.d.ts +0 -2
  265. package/dist/src/core/shaders/webgl/DefaultBatched.js +0 -104
  266. package/dist/src/core/shaders/webgl/DefaultBatched.js.map +0 -1
  267. package/dist/src/core/shaders/webgl/HolePunch.d.ts +0 -3
  268. package/dist/src/core/shaders/webgl/HolePunch.js +0 -64
  269. package/dist/src/core/shaders/webgl/HolePunch.js.map +0 -1
  270. package/dist/src/core/shaders/webgl/LinearGradient.d.ts +0 -3
  271. package/dist/src/core/shaders/webgl/LinearGradient.js +0 -75
  272. package/dist/src/core/shaders/webgl/LinearGradient.js.map +0 -1
  273. package/dist/src/core/shaders/webgl/RadialGradient.d.ts +0 -3
  274. package/dist/src/core/shaders/webgl/RadialGradient.js +0 -73
  275. package/dist/src/core/shaders/webgl/RadialGradient.js.map +0 -1
  276. package/dist/src/core/shaders/webgl/Rounded.d.ts +0 -7
  277. package/dist/src/core/shaders/webgl/Rounded.js +0 -86
  278. package/dist/src/core/shaders/webgl/Rounded.js.map +0 -1
  279. package/dist/src/core/shaders/webgl/RoundedWithBorder.d.ts +0 -3
  280. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +0 -129
  281. package/dist/src/core/shaders/webgl/RoundedWithBorder.js.map +0 -1
  282. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.d.ts +0 -3
  283. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +0 -149
  284. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js.map +0 -1
  285. package/dist/src/core/shaders/webgl/RoundedWithShadow.d.ts +0 -3
  286. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +0 -84
  287. package/dist/src/core/shaders/webgl/RoundedWithShadow.js.map +0 -1
  288. package/dist/src/core/shaders/webgl/SdfShader.d.ts +0 -32
  289. package/dist/src/core/shaders/webgl/SdfShader.js +0 -116
  290. package/dist/src/core/shaders/webgl/SdfShader.js.map +0 -1
  291. package/dist/src/core/shaders/webgl/Shadow.d.ts +0 -3
  292. package/dist/src/core/shaders/webgl/Shadow.js +0 -110
  293. package/dist/src/core/shaders/webgl/Shadow.js.map +0 -1
  294. package/dist/src/core/temp.js +0 -77
  295. package/dist/src/core/temp.js.map +0 -1
  296. /package/dist/src/core/{temp.d.ts → renderers/webgl/WebGlCoreShader.destroy.d.ts} +0 -0
@@ -1,821 +1,821 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2023 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
-
20
- import { assertTruthy, createWebGLContext, hasOwn } from '../../../utils.js';
21
- import {
22
- CoreRenderer,
23
- type BufferInfo,
24
- type CoreRendererOptions,
25
- type QuadOptions,
26
- } from '../CoreRenderer.js';
27
- import { WebGlCoreRenderOp } from './WebGlCoreRenderOp.js';
28
- import type { CoreContextTexture } from '../CoreContextTexture.js';
29
- import {
30
- createIndexBuffer,
31
- type CoreWebGlParameters,
32
- type CoreWebGlExtensions,
33
- getWebGlParameters,
34
- getWebGlExtensions,
35
- type WebGlColor,
36
- } from './internal/RendererUtils.js';
37
- import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js';
38
- import { Texture, TextureType } from '../../textures/Texture.js';
39
- import { SubTexture } from '../../textures/SubTexture.js';
40
- import { WebGlCoreCtxSubTexture } from './WebGlCoreCtxSubTexture.js';
41
- import { CoreShaderManager } from '../../CoreShaderManager.js';
42
- import { BufferCollection } from './internal/BufferCollection.js';
43
- import {
44
- compareRect,
45
- getNormalizedRgbaComponents,
46
- type RectWithValid,
47
- } from '../../lib/utils.js';
48
- import type { Dimensions } from '../../../common/CommonTypes.js';
49
- import { WebGlCoreShader } from './WebGlCoreShader.js';
50
- import { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
51
- import { RenderTexture } from '../../textures/RenderTexture.js';
52
- import { CoreNodeRenderState, type CoreNode } from '../../CoreNode.js';
53
- import { WebGlCoreCtxRenderTexture } from './WebGlCoreCtxRenderTexture.js';
54
- import type { BaseShaderController } from '../../../main-api/ShaderController.js';
55
-
56
- const WORDS_PER_QUAD = 24;
57
- // const BYTES_PER_QUAD = WORDS_PER_QUAD * 4;
58
-
59
- export type WebGlCoreRendererOptions = CoreRendererOptions;
60
-
61
- interface CoreWebGlSystem {
62
- parameters: CoreWebGlParameters;
63
- extensions: CoreWebGlExtensions;
64
- }
65
-
66
- export class WebGlCoreRenderer extends CoreRenderer {
67
- //// WebGL Native Context and Data
68
- glw: WebGlContextWrapper;
69
- system: CoreWebGlSystem;
70
-
71
- //// Persistent data
72
- quadBuffer: ArrayBuffer;
73
- fQuadBuffer: Float32Array;
74
- uiQuadBuffer: Uint32Array;
75
- renderOps: WebGlCoreRenderOp[] = [];
76
-
77
- //// Render Op / Buffer Filling State
78
- curBufferIdx = 0;
79
- curRenderOp: WebGlCoreRenderOp | null = null;
80
- override rttNodes: CoreNode[] = [];
81
- activeRttNode: CoreNode | null = null;
82
-
83
- //// Default Shader
84
- defShaderCtrl: BaseShaderController;
85
- defaultShader: WebGlCoreShader;
86
- quadBufferCollection: BufferCollection;
87
-
88
- clearColor: WebGlColor = {
89
- raw: 0x00000000,
90
- normalized: [0, 0, 0, 0],
91
- };
92
-
93
- /**
94
- * White pixel texture used by default when no texture is specified.
95
- */
96
-
97
- quadBufferUsage = 0;
98
- numQuadsRendered = 0;
99
- /**
100
- * Whether the renderer is currently rendering to a texture.
101
- */
102
- public renderToTextureActive = false;
103
-
104
- constructor(options: WebGlCoreRendererOptions) {
105
- super(options);
106
-
107
- this.quadBuffer = new ArrayBuffer(this.stage.options.quadBufferSize);
108
- this.fQuadBuffer = new Float32Array(this.quadBuffer);
109
- this.uiQuadBuffer = new Uint32Array(this.quadBuffer);
110
-
111
- this.mode = 'webgl';
112
-
113
- const { canvas, clearColor, bufferMemory } = options;
114
-
115
- const gl = createWebGLContext(
116
- canvas,
117
- options.forceWebGL2,
118
- options.contextSpy,
119
- );
120
- const glw = (this.glw = new WebGlContextWrapper(gl));
121
- glw.viewport(0, 0, canvas.width, canvas.height);
122
-
123
- this.updateClearColor(clearColor);
124
-
125
- glw.setBlend(true);
126
- glw.blendFunc(glw.ONE, glw.ONE_MINUS_SRC_ALPHA);
127
-
128
- createIndexBuffer(glw, bufferMemory);
129
-
130
- this.system = {
131
- parameters: getWebGlParameters(this.glw),
132
- extensions: getWebGlExtensions(this.glw),
133
- };
134
- this.shManager.renderer = this;
135
- this.defShaderCtrl = this.shManager.loadShader('DefaultShader');
136
- this.defaultShader = this.defShaderCtrl.shader as WebGlCoreShader;
137
- const quadBuffer = glw.createBuffer();
138
- assertTruthy(quadBuffer);
139
- const stride = 6 * Float32Array.BYTES_PER_ELEMENT;
140
- this.quadBufferCollection = new BufferCollection([
141
- {
142
- buffer: quadBuffer,
143
- attributes: {
144
- a_position: {
145
- name: 'a_position',
146
- size: 2, // 2 components per iteration
147
- type: glw.FLOAT, // the data is 32bit floats
148
- normalized: false, // don't normalize the data
149
- stride, // 0 = move forward size * sizeof(type) each iteration to get the next position
150
- offset: 0, // start at the beginning of the buffer
151
- },
152
- a_textureCoordinate: {
153
- name: 'a_textureCoordinate',
154
- size: 2,
155
- type: glw.FLOAT,
156
- normalized: false,
157
- stride,
158
- offset: 2 * Float32Array.BYTES_PER_ELEMENT,
159
- },
160
- a_color: {
161
- name: 'a_color',
162
- size: 4,
163
- type: glw.UNSIGNED_BYTE,
164
- normalized: true,
165
- stride,
166
- offset: 4 * Float32Array.BYTES_PER_ELEMENT,
167
- },
168
- a_textureIndex: {
169
- name: 'a_textureIndex',
170
- size: 1,
171
- type: glw.FLOAT,
172
- normalized: false,
173
- stride,
174
- offset: 5 * Float32Array.BYTES_PER_ELEMENT,
175
- },
176
- },
177
- },
178
- ]);
179
- }
180
-
181
- reset() {
182
- const { glw } = this;
183
- this.curBufferIdx = 0;
184
- this.curRenderOp = null;
185
- this.renderOps.length = 0;
186
- glw.setScissorTest(false);
187
- glw.clear();
188
- }
189
-
190
- override getShaderManager(): CoreShaderManager {
191
- return this.shManager;
192
- }
193
-
194
- override createCtxTexture(textureSource: Texture): CoreContextTexture {
195
- if (textureSource instanceof SubTexture) {
196
- return new WebGlCoreCtxSubTexture(
197
- this.glw,
198
- this.txMemManager,
199
- textureSource,
200
- );
201
- } else if (textureSource instanceof RenderTexture) {
202
- return new WebGlCoreCtxRenderTexture(
203
- this.glw,
204
- this.txMemManager,
205
- textureSource,
206
- );
207
- }
208
- return new WebGlCoreCtxTexture(this.glw, this.txMemManager, textureSource);
209
- }
210
-
211
- /**
212
- * This function adds a quad (a rectangle composed of two triangles) to the WebGL rendering pipeline.
213
- *
214
- * It takes a set of options that define the quad's properties, such as its dimensions, colors, texture, shader, and transformation matrix.
215
- * The function first updates the shader properties with the current dimensions if necessary, then sets the default texture if none is provided.
216
- * It then checks if a new render operation is needed, based on the current shader and clipping rectangle.
217
- * If a new render operation is needed, it creates one and updates the current render operation.
218
- * The function then adjusts the texture coordinates based on the texture options and adds the texture to the texture manager.
219
- *
220
- * Finally, it calculates the vertices for the quad, taking into account any transformations, and adds them to the quad buffer.
221
- * The function updates the length and number of quads in the current render operation, and updates the current buffer index.
222
- */
223
- addQuad(params: QuadOptions) {
224
- const { fQuadBuffer, uiQuadBuffer } = this;
225
- let texture = params.texture;
226
-
227
- assertTruthy(texture !== null, 'Texture is required');
228
-
229
- /**
230
- * If the shader props contain any automatic properties, update it with the
231
- * current dimensions and or alpha that will be used to render the quad.
232
- */
233
- if (params.shaderProps !== null) {
234
- if (hasOwn(params.shaderProps, '$dimensions') == true) {
235
- const dimensions = params.shaderProps.$dimensions as Dimensions;
236
- dimensions.width = params.width;
237
- dimensions.height = params.height;
238
- }
239
-
240
- if (hasOwn(params.shaderProps, '$alpha') === true) {
241
- params.shaderProps.$alpha = params.alpha;
242
- }
243
- }
244
-
245
- let { curBufferIdx: bufferIdx, curRenderOp } = this;
246
- const targetDims = { width: -1, height: -1 };
247
- targetDims.width = params.width;
248
- targetDims.height = params.height;
249
-
250
- const targetShader =
251
- (params.shader as WebGlCoreShader) || this.defaultShader;
252
- assertTruthy(
253
- targetShader.getUniformLocation !== undefined,
254
- 'Invalid WebGL shader',
255
- );
256
-
257
- if (this.reuseRenderOp(params) === false) {
258
- this.newRenderOp(
259
- targetShader,
260
- params.shaderProps as Record<string, unknown>,
261
- params.alpha,
262
- targetDims,
263
- params.clippingRect,
264
- bufferIdx,
265
- params.rtt,
266
- params.parentHasRenderTexture,
267
- params.framebufferDimensions,
268
- );
269
- curRenderOp = this.curRenderOp;
270
- assertTruthy(curRenderOp);
271
- }
272
-
273
- let texCoordX1 = 0;
274
- let texCoordY1 = 0;
275
- let texCoordX2 = 1;
276
- let texCoordY2 = 1;
277
-
278
- if (texture.type === TextureType.subTexture) {
279
- const {
280
- x: tx,
281
- y: ty,
282
- width: tw,
283
- height: th,
284
- } = (texture as SubTexture).props;
285
- const { width: parentW = 0, height: parentH = 0 } = (
286
- texture as SubTexture
287
- ).parentTexture.dimensions || { width: 0, height: 0 };
288
- texCoordX1 = tx / parentW;
289
- texCoordX2 = texCoordX1 + tw / parentW;
290
- texCoordY1 = ty / parentH;
291
- texCoordY2 = texCoordY1 + th / parentH;
292
- texture = (texture as SubTexture).parentTexture;
293
- }
294
-
295
- if (
296
- texture.type === TextureType.image &&
297
- params.textureOptions !== null &&
298
- params.textureOptions.resizeMode !== undefined &&
299
- texture.dimensions !== null
300
- ) {
301
- const resizeMode = params.textureOptions.resizeMode;
302
- const { width: tw, height: th } = texture.dimensions;
303
- if (resizeMode.type === 'cover') {
304
- const scaleX = params.width / tw;
305
- const scaleY = params.height / th;
306
- const scale = Math.max(scaleX, scaleY);
307
- const precision = 1 / scale;
308
- // Determine based on width
309
- if (scale && scaleX && scaleX < scale) {
310
- const desiredSize = precision * params.width;
311
- texCoordX1 = (1 - desiredSize / tw) * (resizeMode.clipX ?? 0.5);
312
- texCoordX2 = texCoordX1 + desiredSize / tw;
313
- }
314
- // Determine based on height
315
- if (scale && scaleY && scaleY < scale) {
316
- const desiredSize = precision * params.height;
317
- texCoordY1 = (1 - desiredSize / th) * (resizeMode.clipY ?? 0.5);
318
- texCoordY2 = texCoordY1 + desiredSize / th;
319
- }
320
- }
321
- }
322
-
323
- // Flip texture coordinates if dictated by texture options
324
- let flipY = 0;
325
- if (params.textureOptions !== null) {
326
- if (params.textureOptions.flipX === true) {
327
- [texCoordX1, texCoordX2] = [texCoordX2, texCoordX1];
328
- }
329
-
330
- // convert to integer for bitwise operation below
331
- flipY = +(params.textureOptions.flipY || false);
332
- }
333
-
334
- // Eitherone should be true
335
- if (flipY ^ +(texture.type === TextureType.renderToTexture)) {
336
- [texCoordY1, texCoordY2] = [texCoordY2, texCoordY1];
337
- }
338
-
339
- let ctxTexture = texture.ctxTexture as WebGlCoreCtxTexture;
340
- if (ctxTexture === undefined) {
341
- ctxTexture = this.stage.defaultTexture?.ctxTexture as WebGlCoreCtxTexture;
342
- console.warn(
343
- 'WebGL Renderer: Texture does not have a ctxTexture, using default texture instead',
344
- );
345
- }
346
-
347
- const textureIdx = this.addTexture(ctxTexture, bufferIdx);
348
-
349
- assertTruthy(this.curRenderOp !== null);
350
- if (params.renderCoords) {
351
- // Upper-Left
352
- fQuadBuffer[bufferIdx++] = params.renderCoords.x1; // vertexX
353
- fQuadBuffer[bufferIdx++] = params.renderCoords.y1; // vertexY
354
- fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
355
- fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
356
- uiQuadBuffer[bufferIdx++] = params.colorTl; // color
357
- fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
358
-
359
- // Upper-Right
360
- fQuadBuffer[bufferIdx++] = params.renderCoords.x2;
361
- fQuadBuffer[bufferIdx++] = params.renderCoords.y2;
362
- fQuadBuffer[bufferIdx++] = texCoordX2;
363
- fQuadBuffer[bufferIdx++] = texCoordY1;
364
- uiQuadBuffer[bufferIdx++] = params.colorTr;
365
- fQuadBuffer[bufferIdx++] = textureIdx;
366
-
367
- // Lower-Left
368
- fQuadBuffer[bufferIdx++] = params.renderCoords.x4;
369
- fQuadBuffer[bufferIdx++] = params.renderCoords.y4;
370
- fQuadBuffer[bufferIdx++] = texCoordX1;
371
- fQuadBuffer[bufferIdx++] = texCoordY2;
372
- uiQuadBuffer[bufferIdx++] = params.colorBl;
373
- fQuadBuffer[bufferIdx++] = textureIdx;
374
-
375
- // Lower-Right
376
- fQuadBuffer[bufferIdx++] = params.renderCoords.x3;
377
- fQuadBuffer[bufferIdx++] = params.renderCoords.y3;
378
- fQuadBuffer[bufferIdx++] = texCoordX2;
379
- fQuadBuffer[bufferIdx++] = texCoordY2;
380
- uiQuadBuffer[bufferIdx++] = params.colorBr;
381
- fQuadBuffer[bufferIdx++] = textureIdx;
382
- } else if (params.tb !== 0 || params.tc !== 0) {
383
- // Upper-Left
384
- fQuadBuffer[bufferIdx++] = params.tx; // vertexX
385
- fQuadBuffer[bufferIdx++] = params.ty; // vertexY
386
- fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
387
- fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
388
- uiQuadBuffer[bufferIdx++] = params.colorTl; // color
389
- fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
390
-
391
- // Upper-Right
392
- fQuadBuffer[bufferIdx++] = params.tx + params.width * params.ta;
393
- fQuadBuffer[bufferIdx++] = params.ty + params.width * params.tc;
394
- fQuadBuffer[bufferIdx++] = texCoordX2;
395
- fQuadBuffer[bufferIdx++] = texCoordY1;
396
- uiQuadBuffer[bufferIdx++] = params.colorTr;
397
- fQuadBuffer[bufferIdx++] = textureIdx;
398
-
399
- // Lower-Left
400
- fQuadBuffer[bufferIdx++] = params.tx + params.height * params.tb;
401
- fQuadBuffer[bufferIdx++] = params.ty + params.height * params.td;
402
- fQuadBuffer[bufferIdx++] = texCoordX1;
403
- fQuadBuffer[bufferIdx++] = texCoordY2;
404
- uiQuadBuffer[bufferIdx++] = params.colorBl;
405
- fQuadBuffer[bufferIdx++] = textureIdx;
406
-
407
- // Lower-Right
408
- fQuadBuffer[bufferIdx++] =
409
- params.tx + params.width * params.ta + params.height * params.tb;
410
- fQuadBuffer[bufferIdx++] =
411
- params.ty + params.width * params.tc + params.height * params.td;
412
- fQuadBuffer[bufferIdx++] = texCoordX2;
413
- fQuadBuffer[bufferIdx++] = texCoordY2;
414
- uiQuadBuffer[bufferIdx++] = params.colorBr;
415
- fQuadBuffer[bufferIdx++] = textureIdx;
416
- } else {
417
- // Calculate the right corner of the quad
418
- // multiplied by the scale
419
- const rightCornerX = params.tx + params.width * params.ta;
420
- const rightCornerY = params.ty + params.height * params.td;
421
-
422
- // Upper-Left
423
- fQuadBuffer[bufferIdx++] = params.tx; // vertexX
424
- fQuadBuffer[bufferIdx++] = params.ty; // vertexY
425
- fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
426
- fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
427
- uiQuadBuffer[bufferIdx++] = params.colorTl; // color
428
- fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
429
-
430
- // Upper-Right
431
- fQuadBuffer[bufferIdx++] = rightCornerX;
432
- fQuadBuffer[bufferIdx++] = params.ty;
433
- fQuadBuffer[bufferIdx++] = texCoordX2;
434
- fQuadBuffer[bufferIdx++] = texCoordY1;
435
- uiQuadBuffer[bufferIdx++] = params.colorTr;
436
- fQuadBuffer[bufferIdx++] = textureIdx;
437
-
438
- // Lower-Left
439
- fQuadBuffer[bufferIdx++] = params.tx;
440
- fQuadBuffer[bufferIdx++] = rightCornerY;
441
- fQuadBuffer[bufferIdx++] = texCoordX1;
442
- fQuadBuffer[bufferIdx++] = texCoordY2;
443
- uiQuadBuffer[bufferIdx++] = params.colorBl;
444
- fQuadBuffer[bufferIdx++] = textureIdx;
445
-
446
- // Lower-Right
447
- fQuadBuffer[bufferIdx++] = rightCornerX;
448
- fQuadBuffer[bufferIdx++] = rightCornerY;
449
- fQuadBuffer[bufferIdx++] = texCoordX2;
450
- fQuadBuffer[bufferIdx++] = texCoordY2;
451
- uiQuadBuffer[bufferIdx++] = params.colorBr;
452
- fQuadBuffer[bufferIdx++] = textureIdx;
453
- }
454
- // Update the length of the current render op
455
- this.curRenderOp.length += WORDS_PER_QUAD;
456
- this.curRenderOp.numQuads++;
457
- this.curBufferIdx = bufferIdx;
458
- }
459
-
460
- /**
461
- * Replace the existing RenderOp with a new one that uses the specified Shader
462
- * and starts at the specified buffer index.
463
- *
464
- * @param shader
465
- * @param bufferIdx
466
- */
467
- private newRenderOp(
468
- shader: WebGlCoreShader,
469
- shaderProps: Record<string, unknown>,
470
- alpha: number,
471
- dimensions: Dimensions,
472
- clippingRect: RectWithValid,
473
- bufferIdx: number,
474
- renderToTexture?: boolean,
475
- parentHasRenderTexture?: boolean,
476
- framebufferDimensions?: Dimensions,
477
- ) {
478
- const curRenderOp = new WebGlCoreRenderOp(
479
- this.glw,
480
- this.options,
481
- this.quadBufferCollection,
482
- shader,
483
- shaderProps,
484
- alpha,
485
- clippingRect,
486
- dimensions,
487
- bufferIdx,
488
- 0, // Z-Index is only used for explictly added Render Ops
489
- renderToTexture,
490
- parentHasRenderTexture,
491
- framebufferDimensions,
492
- );
493
- this.curRenderOp = curRenderOp;
494
- this.renderOps.push(curRenderOp);
495
- }
496
-
497
- /**
498
- * Add a texture to the current RenderOp. If the texture cannot be added to the
499
- * current RenderOp, a new RenderOp will be created and the texture will be added
500
- * to that one.
501
- *
502
- * If the texture cannot be added to the new RenderOp, an error will be thrown.
503
- *
504
- * @param texture
505
- * @param bufferIdx
506
- * @param recursive
507
- * @returns Assigned Texture Index of the texture in the render op
508
- */
509
- private addTexture(
510
- texture: WebGlCoreCtxTexture,
511
- bufferIdx: number,
512
- recursive?: boolean,
513
- ): number {
514
- const { curRenderOp } = this;
515
- assertTruthy(curRenderOp);
516
- const textureIdx = curRenderOp.addTexture(texture);
517
- // TODO: Refactor to be more DRY
518
- if (textureIdx === 0xffffffff) {
519
- if (recursive) {
520
- throw new Error('Unable to add texture to render op');
521
- }
522
-
523
- this.newRenderOp(
524
- curRenderOp.shader,
525
- curRenderOp.shaderProps,
526
- curRenderOp.alpha,
527
- curRenderOp.dimensions,
528
- curRenderOp.clippingRect,
529
- bufferIdx,
530
- );
531
- return this.addTexture(texture, bufferIdx, true);
532
- }
533
- return textureIdx;
534
- }
535
-
536
- /**
537
- * Test if the current Render operation can be reused for the specified parameters.
538
- * @param params
539
- * @returns
540
- */
541
- reuseRenderOp(params: QuadOptions): boolean {
542
- const { shader, shaderProps, parentHasRenderTexture, rtt, clippingRect } =
543
- params;
544
-
545
- const targetShader = shader || this.defaultShader;
546
-
547
- // Switching shader program will require a new render operation
548
- if (this.curRenderOp?.shader !== targetShader) {
549
- return false;
550
- }
551
-
552
- // Switching clipping rect will require a new render operation
553
- if (compareRect(this.curRenderOp.clippingRect, clippingRect) === false) {
554
- return false;
555
- }
556
-
557
- // Force new render operation if rendering to texture
558
- // @todo: This needs to be improved, render operations could also be reused
559
- // for rendering to texture
560
- if (parentHasRenderTexture !== undefined || rtt !== undefined) {
561
- return false;
562
- }
563
-
564
- // Check if the shader can batch the shader properties
565
- if (
566
- this.curRenderOp.shader !== this.defaultShader &&
567
- (shaderProps === null ||
568
- this.curRenderOp.shader.canBatchShaderProps(
569
- this.curRenderOp.shaderProps,
570
- shaderProps,
571
- ) === false)
572
- ) {
573
- return false;
574
- }
575
-
576
- // Render operation can be reused
577
- return true;
578
- }
579
-
580
- /**
581
- * add RenderOp to the render pipeline
582
- */
583
- addRenderOp(renderable: WebGlCoreRenderOp) {
584
- this.renderOps.push(renderable);
585
- this.curRenderOp = null;
586
- }
587
-
588
- /**
589
- * Render the current set of RenderOps to render to the specified surface.
590
- *
591
- * TODO: 'screen' is the only supported surface at the moment.
592
- *
593
- * @param surface
594
- */
595
- render(surface: 'screen' | CoreContextTexture = 'screen'): void {
596
- const { glw, quadBuffer } = this;
597
-
598
- const arr = new Float32Array(quadBuffer, 0, this.curBufferIdx);
599
-
600
- const buffer = this.quadBufferCollection.getBuffer('a_position') || null;
601
- glw.arrayBufferData(buffer, arr, glw.STATIC_DRAW);
602
-
603
- for (let i = 0, length = this.renderOps.length; i < length; i++) {
604
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
605
- this.renderOps[i]!.draw();
606
- }
607
- this.quadBufferUsage = this.curBufferIdx * arr.BYTES_PER_ELEMENT;
608
-
609
- // Calculate the size of each quad in bytes (4 vertices per quad) times the size of each vertex in bytes
610
- const QUAD_SIZE_IN_BYTES = 4 * (6 * arr.BYTES_PER_ELEMENT); // 6 attributes per vertex
611
- this.numQuadsRendered = this.quadBufferUsage / QUAD_SIZE_IN_BYTES;
612
- }
613
-
614
- getQuadCount(): number {
615
- return this.numQuadsRendered;
616
- }
617
-
618
- renderToTexture(node: CoreNode) {
619
- for (let i = 0; i < this.rttNodes.length; i++) {
620
- if (this.rttNodes[i] === node) {
621
- return;
622
- }
623
- }
624
-
625
- this.insertRTTNodeInOrder(node);
626
- }
627
-
628
- /**
629
- * Inserts an RTT node into `this.rttNodes` while maintaining the correct rendering order based on hierarchy.
630
- *
631
- * Rendering order for RTT nodes is critical when nested RTT nodes exist in a parent-child relationship.
632
- * Specifically:
633
- * - Child RTT nodes must be rendered before their RTT-enabled parents to ensure proper texture composition.
634
- * - If an RTT node is added and it has existing RTT children, it should be rendered after those children.
635
- *
636
- * This function addresses both cases by:
637
- * 1. **Checking Upwards**: It traverses the node's hierarchy upwards to identify any RTT parent
638
- * already in `rttNodes`. If an RTT parent is found, the new node is placed before this parent.
639
- * 2. **Checking Downwards**: It traverses the node’s children recursively to find any RTT-enabled
640
- * children that are already in `rttNodes`. If such children are found, the new node is inserted
641
- * after the last (highest index) RTT child node.
642
- *
643
- * The final calculated insertion index ensures the new node is positioned in `rttNodes` to respect
644
- * both parent-before-child and child-before-parent rendering rules, preserving the correct order
645
- * for the WebGL renderer.
646
- *
647
- * @param node - The RTT-enabled CoreNode to be added to `rttNodes` in the appropriate hierarchical position.
648
- */
649
- private insertRTTNodeInOrder(node: CoreNode) {
650
- let insertIndex = this.rttNodes.length; // Default to the end of the array
651
-
652
- // 1. Traverse upwards to ensure the node is placed before its RTT parent (if any).
653
- let currentNode: CoreNode = node;
654
- while (currentNode) {
655
- if (!currentNode.parent) {
656
- break;
657
- }
658
-
659
- const parentIndex = this.rttNodes.indexOf(currentNode.parent);
660
- if (parentIndex !== -1) {
661
- // Found an RTT parent in the list; set insertIndex to place node before the parent
662
- insertIndex = parentIndex;
663
- break;
664
- }
665
-
666
- currentNode = currentNode.parent;
667
- }
668
-
669
- // 2. Traverse downwards to ensure the node is placed after any RTT children.
670
- // Look through each child recursively to see if any are already in rttNodes.
671
- const maxChildIndex = this.findMaxChildRTTIndex(node);
672
- if (maxChildIndex !== -1) {
673
- // Adjust insertIndex to be after the last child RTT node
674
- insertIndex = Math.max(insertIndex, maxChildIndex + 1);
675
- }
676
-
677
- // 3. Insert the node at the calculated position
678
- this.rttNodes.splice(insertIndex, 0, node);
679
- }
680
-
681
- // Helper function to find the highest index of any RTT children of a node within rttNodes
682
- private findMaxChildRTTIndex(node: CoreNode): number {
683
- let maxIndex = -1;
684
-
685
- const traverseChildren = (currentNode: CoreNode) => {
686
- const currentIndex = this.rttNodes.indexOf(currentNode);
687
- if (currentIndex !== -1) {
688
- maxIndex = Math.max(maxIndex, currentIndex);
689
- }
690
-
691
- // Recursively check all children of the current node
692
- for (const child of currentNode.children) {
693
- traverseChildren(child);
694
- }
695
- };
696
-
697
- // Start traversal directly with the provided node
698
- traverseChildren(node);
699
-
700
- return maxIndex;
701
- }
702
-
703
- renderRTTNodes() {
704
- const { glw } = this;
705
- const { txManager } = this.stage;
706
-
707
- // Render all associated RTT nodes to their textures
708
- for (let i = 0; i < this.rttNodes.length; i++) {
709
- const node = this.rttNodes[i];
710
-
711
- // Skip nodes that don't have RTT updates
712
- if (node === undefined || node.hasRTTupdates === false) {
713
- continue;
714
- }
715
-
716
- // Skip nodes that are not visible
717
- if (
718
- node.worldAlpha === 0 ||
719
- (node.strictBounds === true &&
720
- node.renderState === CoreNodeRenderState.OutOfBounds)
721
- ) {
722
- continue;
723
- }
724
-
725
- // Skip nodes that do not have a loaded texture
726
- if (node.texture === null || node.texture.state !== 'loaded') {
727
- continue;
728
- }
729
-
730
- // Set the active RTT node to the current node
731
- // So we can prevent rendering children of nested RTT nodes
732
- this.activeRttNode = node;
733
-
734
- assertTruthy(node.texture, 'RTT node missing texture');
735
- const ctxTexture = node.texture.ctxTexture;
736
- assertTruthy(ctxTexture instanceof WebGlCoreCtxRenderTexture);
737
- this.renderToTextureActive = true;
738
-
739
- // Bind the the texture's framebuffer
740
- glw.bindFramebuffer(ctxTexture.framebuffer);
741
-
742
- glw.viewport(0, 0, ctxTexture.w, ctxTexture.h);
743
- // Set the clear color to transparent
744
- glw.clearColor(0, 0, 0, 0);
745
- glw.clear();
746
-
747
- // Render all associated quads to the texture
748
- for (let i = 0; i < node.children.length; i++) {
749
- const child = node.children[i];
750
-
751
- if (child === undefined) {
752
- continue;
753
- }
754
-
755
- this.stage.addQuads(child);
756
- child.hasRTTupdates = false;
757
- }
758
-
759
- // Render all associated quads to the texture
760
- this.render();
761
-
762
- // Reset render operations
763
- this.renderOps.length = 0;
764
- node.hasRTTupdates = false;
765
- }
766
-
767
- const clearColor = this.clearColor.normalized;
768
- // Restore the default clear color
769
- glw.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
770
-
771
- // Bind the default framebuffer
772
- glw.bindFramebuffer(null);
773
-
774
- glw.viewport(0, 0, this.glw.canvas.width, this.glw.canvas.height);
775
- this.renderToTextureActive = false;
776
- }
777
-
778
- removeRTTNode(node: CoreNode) {
779
- const index = this.rttNodes.indexOf(node);
780
- if (index === -1) {
781
- return;
782
- }
783
- this.rttNodes.splice(index, 1);
784
- }
785
-
786
- getBufferInfo(): BufferInfo | null {
787
- const bufferInfo: BufferInfo = {
788
- totalAvailable: this.stage.options.quadBufferSize,
789
- totalUsed: this.quadBufferUsage,
790
- };
791
- return bufferInfo;
792
- }
793
-
794
- override getDefShaderCtr(): BaseShaderController {
795
- return this.defShaderCtrl;
796
- }
797
-
798
- /**
799
- * Updates the WebGL context's clear color and clears the color buffer.
800
- *
801
- * @param color - The color to set as the clear color, represented as a 32-bit integer.
802
- */
803
- updateClearColor(color: number) {
804
- if (this.clearColor.raw === color) {
805
- return;
806
- }
807
- const glw = this.glw;
808
- const normalizedColor = getNormalizedRgbaComponents(color);
809
- glw.clearColor(
810
- normalizedColor[0],
811
- normalizedColor[1],
812
- normalizedColor[2],
813
- normalizedColor[3],
814
- );
815
- this.clearColor = {
816
- raw: color,
817
- normalized: normalizedColor,
818
- };
819
- glw.clear();
820
- }
821
- }
1
+ /*
2
+ * If not stated otherwise in this file or this component's LICENSE file the
3
+ * following copyright and licenses apply:
4
+ *
5
+ * Copyright 2023 Comcast Cable Communications Management, LLC.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the License);
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ import { assertTruthy, createWebGLContext, hasOwn } from '../../../utils.js';
21
+ import {
22
+ CoreRenderer,
23
+ type BufferInfo,
24
+ type CoreRendererOptions,
25
+ type QuadOptions,
26
+ } from '../CoreRenderer.js';
27
+ import { WebGlCoreRenderOp } from './WebGlCoreRenderOp.js';
28
+ import type { CoreContextTexture } from '../CoreContextTexture.js';
29
+ import {
30
+ createIndexBuffer,
31
+ type CoreWebGlParameters,
32
+ type CoreWebGlExtensions,
33
+ getWebGlParameters,
34
+ getWebGlExtensions,
35
+ type WebGlColor,
36
+ } from './internal/RendererUtils.js';
37
+ import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js';
38
+ import { Texture, TextureType } from '../../textures/Texture.js';
39
+ import { SubTexture } from '../../textures/SubTexture.js';
40
+ import { WebGlCoreCtxSubTexture } from './WebGlCoreCtxSubTexture.js';
41
+ import { CoreShaderManager } from '../../CoreShaderManager.js';
42
+ import { BufferCollection } from './internal/BufferCollection.js';
43
+ import {
44
+ compareRect,
45
+ getNormalizedRgbaComponents,
46
+ type RectWithValid,
47
+ } from '../../lib/utils.js';
48
+ import type { Dimensions } from '../../../common/CommonTypes.js';
49
+ import { WebGlCoreShader } from './WebGlCoreShader.js';
50
+ import { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
51
+ import { RenderTexture } from '../../textures/RenderTexture.js';
52
+ import { CoreNodeRenderState, type CoreNode } from '../../CoreNode.js';
53
+ import { WebGlCoreCtxRenderTexture } from './WebGlCoreCtxRenderTexture.js';
54
+ import type { BaseShaderController } from '../../../main-api/ShaderController.js';
55
+
56
+ const WORDS_PER_QUAD = 24;
57
+ // const BYTES_PER_QUAD = WORDS_PER_QUAD * 4;
58
+
59
+ export type WebGlCoreRendererOptions = CoreRendererOptions;
60
+
61
+ interface CoreWebGlSystem {
62
+ parameters: CoreWebGlParameters;
63
+ extensions: CoreWebGlExtensions;
64
+ }
65
+
66
+ export class WebGlCoreRenderer extends CoreRenderer {
67
+ //// WebGL Native Context and Data
68
+ glw: WebGlContextWrapper;
69
+ system: CoreWebGlSystem;
70
+
71
+ //// Persistent data
72
+ quadBuffer: ArrayBuffer;
73
+ fQuadBuffer: Float32Array;
74
+ uiQuadBuffer: Uint32Array;
75
+ renderOps: WebGlCoreRenderOp[] = [];
76
+
77
+ //// Render Op / Buffer Filling State
78
+ curBufferIdx = 0;
79
+ curRenderOp: WebGlCoreRenderOp | null = null;
80
+ override rttNodes: CoreNode[] = [];
81
+ activeRttNode: CoreNode | null = null;
82
+
83
+ //// Default Shader
84
+ defShaderCtrl: BaseShaderController;
85
+ defaultShader: WebGlCoreShader;
86
+ quadBufferCollection: BufferCollection;
87
+
88
+ clearColor: WebGlColor = {
89
+ raw: 0x00000000,
90
+ normalized: [0, 0, 0, 0],
91
+ };
92
+
93
+ /**
94
+ * White pixel texture used by default when no texture is specified.
95
+ */
96
+
97
+ quadBufferUsage = 0;
98
+ numQuadsRendered = 0;
99
+ /**
100
+ * Whether the renderer is currently rendering to a texture.
101
+ */
102
+ public renderToTextureActive = false;
103
+
104
+ constructor(options: WebGlCoreRendererOptions) {
105
+ super(options);
106
+
107
+ this.quadBuffer = new ArrayBuffer(this.stage.options.quadBufferSize);
108
+ this.fQuadBuffer = new Float32Array(this.quadBuffer);
109
+ this.uiQuadBuffer = new Uint32Array(this.quadBuffer);
110
+
111
+ this.mode = 'webgl';
112
+
113
+ const { canvas, clearColor, bufferMemory } = options;
114
+
115
+ const gl = createWebGLContext(
116
+ canvas,
117
+ options.forceWebGL2,
118
+ options.contextSpy,
119
+ );
120
+ const glw = (this.glw = new WebGlContextWrapper(gl));
121
+ glw.viewport(0, 0, canvas.width, canvas.height);
122
+
123
+ this.updateClearColor(clearColor);
124
+
125
+ glw.setBlend(true);
126
+ glw.blendFunc(glw.ONE, glw.ONE_MINUS_SRC_ALPHA);
127
+
128
+ createIndexBuffer(glw, bufferMemory);
129
+
130
+ this.system = {
131
+ parameters: getWebGlParameters(this.glw),
132
+ extensions: getWebGlExtensions(this.glw),
133
+ };
134
+ this.shManager.renderer = this;
135
+ this.defShaderCtrl = this.shManager.loadShader('DefaultShader');
136
+ this.defaultShader = this.defShaderCtrl.shader as WebGlCoreShader;
137
+ const quadBuffer = glw.createBuffer();
138
+ assertTruthy(quadBuffer);
139
+ const stride = 6 * Float32Array.BYTES_PER_ELEMENT;
140
+ this.quadBufferCollection = new BufferCollection([
141
+ {
142
+ buffer: quadBuffer,
143
+ attributes: {
144
+ a_position: {
145
+ name: 'a_position',
146
+ size: 2, // 2 components per iteration
147
+ type: glw.FLOAT, // the data is 32bit floats
148
+ normalized: false, // don't normalize the data
149
+ stride, // 0 = move forward size * sizeof(type) each iteration to get the next position
150
+ offset: 0, // start at the beginning of the buffer
151
+ },
152
+ a_textureCoordinate: {
153
+ name: 'a_textureCoordinate',
154
+ size: 2,
155
+ type: glw.FLOAT,
156
+ normalized: false,
157
+ stride,
158
+ offset: 2 * Float32Array.BYTES_PER_ELEMENT,
159
+ },
160
+ a_color: {
161
+ name: 'a_color',
162
+ size: 4,
163
+ type: glw.UNSIGNED_BYTE,
164
+ normalized: true,
165
+ stride,
166
+ offset: 4 * Float32Array.BYTES_PER_ELEMENT,
167
+ },
168
+ a_textureIndex: {
169
+ name: 'a_textureIndex',
170
+ size: 1,
171
+ type: glw.FLOAT,
172
+ normalized: false,
173
+ stride,
174
+ offset: 5 * Float32Array.BYTES_PER_ELEMENT,
175
+ },
176
+ },
177
+ },
178
+ ]);
179
+ }
180
+
181
+ reset() {
182
+ const { glw } = this;
183
+ this.curBufferIdx = 0;
184
+ this.curRenderOp = null;
185
+ this.renderOps.length = 0;
186
+ glw.setScissorTest(false);
187
+ glw.clear();
188
+ }
189
+
190
+ override getShaderManager(): CoreShaderManager {
191
+ return this.shManager;
192
+ }
193
+
194
+ override createCtxTexture(textureSource: Texture): CoreContextTexture {
195
+ if (textureSource instanceof SubTexture) {
196
+ return new WebGlCoreCtxSubTexture(
197
+ this.glw,
198
+ this.txMemManager,
199
+ textureSource,
200
+ );
201
+ } else if (textureSource instanceof RenderTexture) {
202
+ return new WebGlCoreCtxRenderTexture(
203
+ this.glw,
204
+ this.txMemManager,
205
+ textureSource,
206
+ );
207
+ }
208
+ return new WebGlCoreCtxTexture(this.glw, this.txMemManager, textureSource);
209
+ }
210
+
211
+ /**
212
+ * This function adds a quad (a rectangle composed of two triangles) to the WebGL rendering pipeline.
213
+ *
214
+ * It takes a set of options that define the quad's properties, such as its dimensions, colors, texture, shader, and transformation matrix.
215
+ * The function first updates the shader properties with the current dimensions if necessary, then sets the default texture if none is provided.
216
+ * It then checks if a new render operation is needed, based on the current shader and clipping rectangle.
217
+ * If a new render operation is needed, it creates one and updates the current render operation.
218
+ * The function then adjusts the texture coordinates based on the texture options and adds the texture to the texture manager.
219
+ *
220
+ * Finally, it calculates the vertices for the quad, taking into account any transformations, and adds them to the quad buffer.
221
+ * The function updates the length and number of quads in the current render operation, and updates the current buffer index.
222
+ */
223
+ addQuad(params: QuadOptions) {
224
+ const { fQuadBuffer, uiQuadBuffer } = this;
225
+ let texture = params.texture;
226
+
227
+ assertTruthy(texture !== null, 'Texture is required');
228
+
229
+ /**
230
+ * If the shader props contain any automatic properties, update it with the
231
+ * current dimensions and or alpha that will be used to render the quad.
232
+ */
233
+ if (params.shaderProps !== null) {
234
+ if (hasOwn(params.shaderProps, '$dimensions') == true) {
235
+ const dimensions = params.shaderProps.$dimensions as Dimensions;
236
+ dimensions.width = params.width;
237
+ dimensions.height = params.height;
238
+ }
239
+
240
+ if (hasOwn(params.shaderProps, '$alpha') === true) {
241
+ params.shaderProps.$alpha = params.alpha;
242
+ }
243
+ }
244
+
245
+ let { curBufferIdx: bufferIdx, curRenderOp } = this;
246
+ const targetDims = { width: -1, height: -1 };
247
+ targetDims.width = params.width;
248
+ targetDims.height = params.height;
249
+
250
+ const targetShader =
251
+ (params.shader as WebGlCoreShader) || this.defaultShader;
252
+ assertTruthy(
253
+ targetShader.getUniformLocation !== undefined,
254
+ 'Invalid WebGL shader',
255
+ );
256
+
257
+ if (this.reuseRenderOp(params) === false) {
258
+ this.newRenderOp(
259
+ targetShader,
260
+ params.shaderProps as Record<string, unknown>,
261
+ params.alpha,
262
+ targetDims,
263
+ params.clippingRect,
264
+ bufferIdx,
265
+ params.rtt,
266
+ params.parentHasRenderTexture,
267
+ params.framebufferDimensions,
268
+ );
269
+ curRenderOp = this.curRenderOp;
270
+ assertTruthy(curRenderOp);
271
+ }
272
+
273
+ let texCoordX1 = 0;
274
+ let texCoordY1 = 0;
275
+ let texCoordX2 = 1;
276
+ let texCoordY2 = 1;
277
+
278
+ if (texture.type === TextureType.subTexture) {
279
+ const {
280
+ x: tx,
281
+ y: ty,
282
+ width: tw,
283
+ height: th,
284
+ } = (texture as SubTexture).props;
285
+ const { width: parentW = 0, height: parentH = 0 } = (
286
+ texture as SubTexture
287
+ ).parentTexture.dimensions || { width: 0, height: 0 };
288
+ texCoordX1 = tx / parentW;
289
+ texCoordX2 = texCoordX1 + tw / parentW;
290
+ texCoordY1 = ty / parentH;
291
+ texCoordY2 = texCoordY1 + th / parentH;
292
+ texture = (texture as SubTexture).parentTexture;
293
+ }
294
+
295
+ if (
296
+ texture.type === TextureType.image &&
297
+ params.textureOptions !== null &&
298
+ params.textureOptions.resizeMode !== undefined &&
299
+ texture.dimensions !== null
300
+ ) {
301
+ const resizeMode = params.textureOptions.resizeMode;
302
+ const { width: tw, height: th } = texture.dimensions;
303
+ if (resizeMode.type === 'cover') {
304
+ const scaleX = params.width / tw;
305
+ const scaleY = params.height / th;
306
+ const scale = Math.max(scaleX, scaleY);
307
+ const precision = 1 / scale;
308
+ // Determine based on width
309
+ if (scale && scaleX && scaleX < scale) {
310
+ const desiredSize = precision * params.width;
311
+ texCoordX1 = (1 - desiredSize / tw) * (resizeMode.clipX ?? 0.5);
312
+ texCoordX2 = texCoordX1 + desiredSize / tw;
313
+ }
314
+ // Determine based on height
315
+ if (scale && scaleY && scaleY < scale) {
316
+ const desiredSize = precision * params.height;
317
+ texCoordY1 = (1 - desiredSize / th) * (resizeMode.clipY ?? 0.5);
318
+ texCoordY2 = texCoordY1 + desiredSize / th;
319
+ }
320
+ }
321
+ }
322
+
323
+ // Flip texture coordinates if dictated by texture options
324
+ let flipY = 0;
325
+ if (params.textureOptions !== null) {
326
+ if (params.textureOptions.flipX === true) {
327
+ [texCoordX1, texCoordX2] = [texCoordX2, texCoordX1];
328
+ }
329
+
330
+ // convert to integer for bitwise operation below
331
+ flipY = +(params.textureOptions.flipY || false);
332
+ }
333
+
334
+ // Eitherone should be true
335
+ if (flipY ^ +(texture.type === TextureType.renderToTexture)) {
336
+ [texCoordY1, texCoordY2] = [texCoordY2, texCoordY1];
337
+ }
338
+
339
+ let ctxTexture = texture.ctxTexture as WebGlCoreCtxTexture;
340
+ if (ctxTexture === undefined) {
341
+ ctxTexture = this.stage.defaultTexture?.ctxTexture as WebGlCoreCtxTexture;
342
+ console.warn(
343
+ 'WebGL Renderer: Texture does not have a ctxTexture, using default texture instead',
344
+ );
345
+ }
346
+
347
+ const textureIdx = this.addTexture(ctxTexture, bufferIdx);
348
+
349
+ assertTruthy(this.curRenderOp !== null);
350
+ if (params.renderCoords) {
351
+ // Upper-Left
352
+ fQuadBuffer[bufferIdx++] = params.renderCoords.x1; // vertexX
353
+ fQuadBuffer[bufferIdx++] = params.renderCoords.y1; // vertexY
354
+ fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
355
+ fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
356
+ uiQuadBuffer[bufferIdx++] = params.colorTl; // color
357
+ fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
358
+
359
+ // Upper-Right
360
+ fQuadBuffer[bufferIdx++] = params.renderCoords.x2;
361
+ fQuadBuffer[bufferIdx++] = params.renderCoords.y2;
362
+ fQuadBuffer[bufferIdx++] = texCoordX2;
363
+ fQuadBuffer[bufferIdx++] = texCoordY1;
364
+ uiQuadBuffer[bufferIdx++] = params.colorTr;
365
+ fQuadBuffer[bufferIdx++] = textureIdx;
366
+
367
+ // Lower-Left
368
+ fQuadBuffer[bufferIdx++] = params.renderCoords.x4;
369
+ fQuadBuffer[bufferIdx++] = params.renderCoords.y4;
370
+ fQuadBuffer[bufferIdx++] = texCoordX1;
371
+ fQuadBuffer[bufferIdx++] = texCoordY2;
372
+ uiQuadBuffer[bufferIdx++] = params.colorBl;
373
+ fQuadBuffer[bufferIdx++] = textureIdx;
374
+
375
+ // Lower-Right
376
+ fQuadBuffer[bufferIdx++] = params.renderCoords.x3;
377
+ fQuadBuffer[bufferIdx++] = params.renderCoords.y3;
378
+ fQuadBuffer[bufferIdx++] = texCoordX2;
379
+ fQuadBuffer[bufferIdx++] = texCoordY2;
380
+ uiQuadBuffer[bufferIdx++] = params.colorBr;
381
+ fQuadBuffer[bufferIdx++] = textureIdx;
382
+ } else if (params.tb !== 0 || params.tc !== 0) {
383
+ // Upper-Left
384
+ fQuadBuffer[bufferIdx++] = params.tx; // vertexX
385
+ fQuadBuffer[bufferIdx++] = params.ty; // vertexY
386
+ fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
387
+ fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
388
+ uiQuadBuffer[bufferIdx++] = params.colorTl; // color
389
+ fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
390
+
391
+ // Upper-Right
392
+ fQuadBuffer[bufferIdx++] = params.tx + params.width * params.ta;
393
+ fQuadBuffer[bufferIdx++] = params.ty + params.width * params.tc;
394
+ fQuadBuffer[bufferIdx++] = texCoordX2;
395
+ fQuadBuffer[bufferIdx++] = texCoordY1;
396
+ uiQuadBuffer[bufferIdx++] = params.colorTr;
397
+ fQuadBuffer[bufferIdx++] = textureIdx;
398
+
399
+ // Lower-Left
400
+ fQuadBuffer[bufferIdx++] = params.tx + params.height * params.tb;
401
+ fQuadBuffer[bufferIdx++] = params.ty + params.height * params.td;
402
+ fQuadBuffer[bufferIdx++] = texCoordX1;
403
+ fQuadBuffer[bufferIdx++] = texCoordY2;
404
+ uiQuadBuffer[bufferIdx++] = params.colorBl;
405
+ fQuadBuffer[bufferIdx++] = textureIdx;
406
+
407
+ // Lower-Right
408
+ fQuadBuffer[bufferIdx++] =
409
+ params.tx + params.width * params.ta + params.height * params.tb;
410
+ fQuadBuffer[bufferIdx++] =
411
+ params.ty + params.width * params.tc + params.height * params.td;
412
+ fQuadBuffer[bufferIdx++] = texCoordX2;
413
+ fQuadBuffer[bufferIdx++] = texCoordY2;
414
+ uiQuadBuffer[bufferIdx++] = params.colorBr;
415
+ fQuadBuffer[bufferIdx++] = textureIdx;
416
+ } else {
417
+ // Calculate the right corner of the quad
418
+ // multiplied by the scale
419
+ const rightCornerX = params.tx + params.width * params.ta;
420
+ const rightCornerY = params.ty + params.height * params.td;
421
+
422
+ // Upper-Left
423
+ fQuadBuffer[bufferIdx++] = params.tx; // vertexX
424
+ fQuadBuffer[bufferIdx++] = params.ty; // vertexY
425
+ fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
426
+ fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
427
+ uiQuadBuffer[bufferIdx++] = params.colorTl; // color
428
+ fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
429
+
430
+ // Upper-Right
431
+ fQuadBuffer[bufferIdx++] = rightCornerX;
432
+ fQuadBuffer[bufferIdx++] = params.ty;
433
+ fQuadBuffer[bufferIdx++] = texCoordX2;
434
+ fQuadBuffer[bufferIdx++] = texCoordY1;
435
+ uiQuadBuffer[bufferIdx++] = params.colorTr;
436
+ fQuadBuffer[bufferIdx++] = textureIdx;
437
+
438
+ // Lower-Left
439
+ fQuadBuffer[bufferIdx++] = params.tx;
440
+ fQuadBuffer[bufferIdx++] = rightCornerY;
441
+ fQuadBuffer[bufferIdx++] = texCoordX1;
442
+ fQuadBuffer[bufferIdx++] = texCoordY2;
443
+ uiQuadBuffer[bufferIdx++] = params.colorBl;
444
+ fQuadBuffer[bufferIdx++] = textureIdx;
445
+
446
+ // Lower-Right
447
+ fQuadBuffer[bufferIdx++] = rightCornerX;
448
+ fQuadBuffer[bufferIdx++] = rightCornerY;
449
+ fQuadBuffer[bufferIdx++] = texCoordX2;
450
+ fQuadBuffer[bufferIdx++] = texCoordY2;
451
+ uiQuadBuffer[bufferIdx++] = params.colorBr;
452
+ fQuadBuffer[bufferIdx++] = textureIdx;
453
+ }
454
+ // Update the length of the current render op
455
+ this.curRenderOp.length += WORDS_PER_QUAD;
456
+ this.curRenderOp.numQuads++;
457
+ this.curBufferIdx = bufferIdx;
458
+ }
459
+
460
+ /**
461
+ * Replace the existing RenderOp with a new one that uses the specified Shader
462
+ * and starts at the specified buffer index.
463
+ *
464
+ * @param shader
465
+ * @param bufferIdx
466
+ */
467
+ private newRenderOp(
468
+ shader: WebGlCoreShader,
469
+ shaderProps: Record<string, unknown>,
470
+ alpha: number,
471
+ dimensions: Dimensions,
472
+ clippingRect: RectWithValid,
473
+ bufferIdx: number,
474
+ renderToTexture?: boolean,
475
+ parentHasRenderTexture?: boolean,
476
+ framebufferDimensions?: Dimensions,
477
+ ) {
478
+ const curRenderOp = new WebGlCoreRenderOp(
479
+ this.glw,
480
+ this.options,
481
+ this.quadBufferCollection,
482
+ shader,
483
+ shaderProps,
484
+ alpha,
485
+ clippingRect,
486
+ dimensions,
487
+ bufferIdx,
488
+ 0, // Z-Index is only used for explictly added Render Ops
489
+ renderToTexture,
490
+ parentHasRenderTexture,
491
+ framebufferDimensions,
492
+ );
493
+ this.curRenderOp = curRenderOp;
494
+ this.renderOps.push(curRenderOp);
495
+ }
496
+
497
+ /**
498
+ * Add a texture to the current RenderOp. If the texture cannot be added to the
499
+ * current RenderOp, a new RenderOp will be created and the texture will be added
500
+ * to that one.
501
+ *
502
+ * If the texture cannot be added to the new RenderOp, an error will be thrown.
503
+ *
504
+ * @param texture
505
+ * @param bufferIdx
506
+ * @param recursive
507
+ * @returns Assigned Texture Index of the texture in the render op
508
+ */
509
+ private addTexture(
510
+ texture: WebGlCoreCtxTexture,
511
+ bufferIdx: number,
512
+ recursive?: boolean,
513
+ ): number {
514
+ const { curRenderOp } = this;
515
+ assertTruthy(curRenderOp);
516
+ const textureIdx = curRenderOp.addTexture(texture);
517
+ // TODO: Refactor to be more DRY
518
+ if (textureIdx === 0xffffffff) {
519
+ if (recursive) {
520
+ throw new Error('Unable to add texture to render op');
521
+ }
522
+
523
+ this.newRenderOp(
524
+ curRenderOp.shader,
525
+ curRenderOp.shaderProps,
526
+ curRenderOp.alpha,
527
+ curRenderOp.dimensions,
528
+ curRenderOp.clippingRect,
529
+ bufferIdx,
530
+ );
531
+ return this.addTexture(texture, bufferIdx, true);
532
+ }
533
+ return textureIdx;
534
+ }
535
+
536
+ /**
537
+ * Test if the current Render operation can be reused for the specified parameters.
538
+ * @param params
539
+ * @returns
540
+ */
541
+ reuseRenderOp(params: QuadOptions): boolean {
542
+ const { shader, shaderProps, parentHasRenderTexture, rtt, clippingRect } =
543
+ params;
544
+
545
+ const targetShader = shader || this.defaultShader;
546
+
547
+ // Switching shader program will require a new render operation
548
+ if (this.curRenderOp?.shader !== targetShader) {
549
+ return false;
550
+ }
551
+
552
+ // Switching clipping rect will require a new render operation
553
+ if (compareRect(this.curRenderOp.clippingRect, clippingRect) === false) {
554
+ return false;
555
+ }
556
+
557
+ // Force new render operation if rendering to texture
558
+ // @todo: This needs to be improved, render operations could also be reused
559
+ // for rendering to texture
560
+ if (parentHasRenderTexture !== undefined || rtt !== undefined) {
561
+ return false;
562
+ }
563
+
564
+ // Check if the shader can batch the shader properties
565
+ if (
566
+ this.curRenderOp.shader !== this.defaultShader &&
567
+ (shaderProps === null ||
568
+ this.curRenderOp.shader.canBatchShaderProps(
569
+ this.curRenderOp.shaderProps,
570
+ shaderProps,
571
+ ) === false)
572
+ ) {
573
+ return false;
574
+ }
575
+
576
+ // Render operation can be reused
577
+ return true;
578
+ }
579
+
580
+ /**
581
+ * add RenderOp to the render pipeline
582
+ */
583
+ addRenderOp(renderable: WebGlCoreRenderOp) {
584
+ this.renderOps.push(renderable);
585
+ this.curRenderOp = null;
586
+ }
587
+
588
+ /**
589
+ * Render the current set of RenderOps to render to the specified surface.
590
+ *
591
+ * TODO: 'screen' is the only supported surface at the moment.
592
+ *
593
+ * @param surface
594
+ */
595
+ render(surface: 'screen' | CoreContextTexture = 'screen'): void {
596
+ const { glw, quadBuffer } = this;
597
+
598
+ const arr = new Float32Array(quadBuffer, 0, this.curBufferIdx);
599
+
600
+ const buffer = this.quadBufferCollection.getBuffer('a_position') || null;
601
+ glw.arrayBufferData(buffer, arr, glw.STATIC_DRAW);
602
+
603
+ for (let i = 0, length = this.renderOps.length; i < length; i++) {
604
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
605
+ this.renderOps[i]!.draw();
606
+ }
607
+ this.quadBufferUsage = this.curBufferIdx * arr.BYTES_PER_ELEMENT;
608
+
609
+ // Calculate the size of each quad in bytes (4 vertices per quad) times the size of each vertex in bytes
610
+ const QUAD_SIZE_IN_BYTES = 4 * (6 * arr.BYTES_PER_ELEMENT); // 6 attributes per vertex
611
+ this.numQuadsRendered = this.quadBufferUsage / QUAD_SIZE_IN_BYTES;
612
+ }
613
+
614
+ getQuadCount(): number {
615
+ return this.numQuadsRendered;
616
+ }
617
+
618
+ renderToTexture(node: CoreNode) {
619
+ for (let i = 0; i < this.rttNodes.length; i++) {
620
+ if (this.rttNodes[i] === node) {
621
+ return;
622
+ }
623
+ }
624
+
625
+ this.insertRTTNodeInOrder(node);
626
+ }
627
+
628
+ /**
629
+ * Inserts an RTT node into `this.rttNodes` while maintaining the correct rendering order based on hierarchy.
630
+ *
631
+ * Rendering order for RTT nodes is critical when nested RTT nodes exist in a parent-child relationship.
632
+ * Specifically:
633
+ * - Child RTT nodes must be rendered before their RTT-enabled parents to ensure proper texture composition.
634
+ * - If an RTT node is added and it has existing RTT children, it should be rendered after those children.
635
+ *
636
+ * This function addresses both cases by:
637
+ * 1. **Checking Upwards**: It traverses the node's hierarchy upwards to identify any RTT parent
638
+ * already in `rttNodes`. If an RTT parent is found, the new node is placed before this parent.
639
+ * 2. **Checking Downwards**: It traverses the node’s children recursively to find any RTT-enabled
640
+ * children that are already in `rttNodes`. If such children are found, the new node is inserted
641
+ * after the last (highest index) RTT child node.
642
+ *
643
+ * The final calculated insertion index ensures the new node is positioned in `rttNodes` to respect
644
+ * both parent-before-child and child-before-parent rendering rules, preserving the correct order
645
+ * for the WebGL renderer.
646
+ *
647
+ * @param node - The RTT-enabled CoreNode to be added to `rttNodes` in the appropriate hierarchical position.
648
+ */
649
+ private insertRTTNodeInOrder(node: CoreNode) {
650
+ let insertIndex = this.rttNodes.length; // Default to the end of the array
651
+
652
+ // 1. Traverse upwards to ensure the node is placed before its RTT parent (if any).
653
+ let currentNode: CoreNode = node;
654
+ while (currentNode) {
655
+ if (!currentNode.parent) {
656
+ break;
657
+ }
658
+
659
+ const parentIndex = this.rttNodes.indexOf(currentNode.parent);
660
+ if (parentIndex !== -1) {
661
+ // Found an RTT parent in the list; set insertIndex to place node before the parent
662
+ insertIndex = parentIndex;
663
+ break;
664
+ }
665
+
666
+ currentNode = currentNode.parent;
667
+ }
668
+
669
+ // 2. Traverse downwards to ensure the node is placed after any RTT children.
670
+ // Look through each child recursively to see if any are already in rttNodes.
671
+ const maxChildIndex = this.findMaxChildRTTIndex(node);
672
+ if (maxChildIndex !== -1) {
673
+ // Adjust insertIndex to be after the last child RTT node
674
+ insertIndex = Math.max(insertIndex, maxChildIndex + 1);
675
+ }
676
+
677
+ // 3. Insert the node at the calculated position
678
+ this.rttNodes.splice(insertIndex, 0, node);
679
+ }
680
+
681
+ // Helper function to find the highest index of any RTT children of a node within rttNodes
682
+ private findMaxChildRTTIndex(node: CoreNode): number {
683
+ let maxIndex = -1;
684
+
685
+ const traverseChildren = (currentNode: CoreNode) => {
686
+ const currentIndex = this.rttNodes.indexOf(currentNode);
687
+ if (currentIndex !== -1) {
688
+ maxIndex = Math.max(maxIndex, currentIndex);
689
+ }
690
+
691
+ // Recursively check all children of the current node
692
+ for (const child of currentNode.children) {
693
+ traverseChildren(child);
694
+ }
695
+ };
696
+
697
+ // Start traversal directly with the provided node
698
+ traverseChildren(node);
699
+
700
+ return maxIndex;
701
+ }
702
+
703
+ renderRTTNodes() {
704
+ const { glw } = this;
705
+ const { txManager } = this.stage;
706
+
707
+ // Render all associated RTT nodes to their textures
708
+ for (let i = 0; i < this.rttNodes.length; i++) {
709
+ const node = this.rttNodes[i];
710
+
711
+ // Skip nodes that don't have RTT updates
712
+ if (node === undefined || node.hasRTTupdates === false) {
713
+ continue;
714
+ }
715
+
716
+ // Skip nodes that are not visible
717
+ if (
718
+ node.worldAlpha === 0 ||
719
+ (node.strictBounds === true &&
720
+ node.renderState === CoreNodeRenderState.OutOfBounds)
721
+ ) {
722
+ continue;
723
+ }
724
+
725
+ // Skip nodes that do not have a loaded texture
726
+ if (node.texture === null || node.texture.state !== 'loaded') {
727
+ continue;
728
+ }
729
+
730
+ // Set the active RTT node to the current node
731
+ // So we can prevent rendering children of nested RTT nodes
732
+ this.activeRttNode = node;
733
+
734
+ assertTruthy(node.texture, 'RTT node missing texture');
735
+ const ctxTexture = node.texture.ctxTexture;
736
+ assertTruthy(ctxTexture instanceof WebGlCoreCtxRenderTexture);
737
+ this.renderToTextureActive = true;
738
+
739
+ // Bind the the texture's framebuffer
740
+ glw.bindFramebuffer(ctxTexture.framebuffer);
741
+
742
+ glw.viewport(0, 0, ctxTexture.w, ctxTexture.h);
743
+ // Set the clear color to transparent
744
+ glw.clearColor(0, 0, 0, 0);
745
+ glw.clear();
746
+
747
+ // Render all associated quads to the texture
748
+ for (let i = 0; i < node.children.length; i++) {
749
+ const child = node.children[i];
750
+
751
+ if (child === undefined) {
752
+ continue;
753
+ }
754
+
755
+ this.stage.addQuads(child);
756
+ child.hasRTTupdates = false;
757
+ }
758
+
759
+ // Render all associated quads to the texture
760
+ this.render();
761
+
762
+ // Reset render operations
763
+ this.renderOps.length = 0;
764
+ node.hasRTTupdates = false;
765
+ }
766
+
767
+ const clearColor = this.clearColor.normalized;
768
+ // Restore the default clear color
769
+ glw.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
770
+
771
+ // Bind the default framebuffer
772
+ glw.bindFramebuffer(null);
773
+
774
+ glw.viewport(0, 0, this.glw.canvas.width, this.glw.canvas.height);
775
+ this.renderToTextureActive = false;
776
+ }
777
+
778
+ removeRTTNode(node: CoreNode) {
779
+ const index = this.rttNodes.indexOf(node);
780
+ if (index === -1) {
781
+ return;
782
+ }
783
+ this.rttNodes.splice(index, 1);
784
+ }
785
+
786
+ getBufferInfo(): BufferInfo | null {
787
+ const bufferInfo: BufferInfo = {
788
+ totalAvailable: this.stage.options.quadBufferSize,
789
+ totalUsed: this.quadBufferUsage,
790
+ };
791
+ return bufferInfo;
792
+ }
793
+
794
+ override getDefShaderCtr(): BaseShaderController {
795
+ return this.defShaderCtrl;
796
+ }
797
+
798
+ /**
799
+ * Updates the WebGL context's clear color and clears the color buffer.
800
+ *
801
+ * @param color - The color to set as the clear color, represented as a 32-bit integer.
802
+ */
803
+ updateClearColor(color: number) {
804
+ if (this.clearColor.raw === color) {
805
+ return;
806
+ }
807
+ const glw = this.glw;
808
+ const normalizedColor = getNormalizedRgbaComponents(color);
809
+ glw.clearColor(
810
+ normalizedColor[0],
811
+ normalizedColor[1],
812
+ normalizedColor[2],
813
+ normalizedColor[3],
814
+ );
815
+ this.clearColor = {
816
+ raw: color,
817
+ normalized: normalizedColor,
818
+ };
819
+ glw.clear();
820
+ }
821
+ }