@lightningjs/renderer 2.9.0 → 2.9.2

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