@lightningjs/renderer 2.13.5 → 2.14.1

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