@lightningjs/renderer 0.7.5 → 0.7.6

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 (218) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +229 -221
  4. package/dist/src/common/CommonTypes.d.ts +6 -0
  5. package/dist/src/core/CoreNode.d.ts +21 -4
  6. package/dist/src/core/CoreNode.js +145 -34
  7. package/dist/src/core/CoreNode.js.map +1 -1
  8. package/dist/src/core/CoreTextNode.d.ts +1 -1
  9. package/dist/src/core/CoreTextNode.js +18 -10
  10. package/dist/src/core/CoreTextNode.js.map +1 -1
  11. package/dist/src/core/CoreTextureManager.d.ts +3 -1
  12. package/dist/src/core/CoreTextureManager.js +9 -2
  13. package/dist/src/core/CoreTextureManager.js.map +1 -1
  14. package/dist/src/core/Stage.d.ts +2 -0
  15. package/dist/src/core/Stage.js +9 -1
  16. package/dist/src/core/Stage.js.map +1 -1
  17. package/dist/src/core/lib/ImageWorker.d.ts +0 -1
  18. package/dist/src/core/lib/ImageWorker.js +55 -40
  19. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  20. package/dist/src/core/lib/RenderCoords.d.ts +13 -0
  21. package/dist/src/core/lib/RenderCoords.js +63 -0
  22. package/dist/src/core/lib/RenderCoords.js.map +1 -0
  23. package/dist/src/core/lib/WebGlContext.d.ts +414 -0
  24. package/dist/src/core/lib/WebGlContext.js +640 -0
  25. package/dist/src/core/lib/WebGlContext.js.map +1 -0
  26. package/dist/src/core/lib/utils.d.ts +1 -0
  27. package/dist/src/core/lib/utils.js +6 -0
  28. package/dist/src/core/lib/utils.js.map +1 -1
  29. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +4 -1
  30. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  31. package/dist/src/core/renderers/webgl/internal/RendererUtils.d.ts +9 -0
  32. package/dist/src/core/renderers/webgl/internal/RendererUtils.js +14 -0
  33. package/dist/src/core/renderers/webgl/internal/RendererUtils.js.map +1 -1
  34. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +47 -47
  35. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  36. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  37. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  38. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +49 -49
  39. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  40. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +5 -5
  41. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  42. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  43. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  44. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  45. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  46. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  47. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +31 -31
  48. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +13 -13
  49. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  50. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  51. package/dist/src/core/scene/Scene.d.ts +59 -0
  52. package/dist/src/core/scene/Scene.js +106 -0
  53. package/dist/src/core/scene/Scene.js.map +1 -0
  54. package/dist/src/core/text-rendering/TrFontManager.js +30 -25
  55. package/dist/src/core/text-rendering/TrFontManager.js.map +1 -1
  56. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +8 -2
  57. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  58. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +8 -2
  59. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  60. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.d.ts +8 -0
  61. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js +29 -0
  62. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js.map +1 -0
  63. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.d.ts +19 -0
  64. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js +84 -0
  65. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js.map +1 -0
  66. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.d.ts +8 -0
  67. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js +40 -0
  68. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js.map +1 -0
  69. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.d.ts +2 -0
  70. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js +41 -0
  71. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js.map +1 -0
  72. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.d.ts +1 -0
  73. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js +4 -0
  74. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js.map +1 -0
  75. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.d.ts +1 -0
  76. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js +2 -0
  77. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js.map +1 -0
  78. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +20 -0
  79. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +55 -0
  80. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +1 -0
  81. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.d.ts +9 -0
  82. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js +32 -0
  83. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js.map +1 -0
  84. package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +12 -0
  85. package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
  86. package/dist/src/core/textures/ImageTexture.js +16 -2
  87. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  88. package/dist/src/core/textures/Texture.d.ts +1 -1
  89. package/dist/src/core/textures/Texture.js.map +1 -1
  90. package/dist/src/core/utils.d.ts +1 -1
  91. package/dist/src/main-api/RendererMain.d.ts +6 -0
  92. package/dist/src/main-api/RendererMain.js +1 -0
  93. package/dist/src/main-api/RendererMain.js.map +1 -1
  94. package/dist/src/render-drivers/main/MainCoreDriver.js +1 -0
  95. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -1
  96. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +4 -0
  97. package/dist/src/render-drivers/main/MainOnlyNode.js +22 -0
  98. package/dist/src/render-drivers/main/MainOnlyNode.js.map +1 -1
  99. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +1 -0
  100. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -1
  101. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +1 -0
  102. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -1
  103. package/dist/src/render-drivers/threadx/worker/renderer.js +1 -0
  104. package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -1
  105. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  106. package/exports/core-api.ts +102 -102
  107. package/exports/main-api.ts +60 -60
  108. package/exports/utils.ts +41 -41
  109. package/package.json +1 -1
  110. package/scripts/please-use-pnpm.js +13 -13
  111. package/src/common/CommonTypes.ts +125 -113
  112. package/src/common/EventEmitter.ts +77 -77
  113. package/src/common/IAnimationController.ts +29 -29
  114. package/src/core/CoreExtension.ts +32 -32
  115. package/src/core/CoreNode.ts +1114 -955
  116. package/src/core/CoreShaderManager.ts +243 -243
  117. package/src/core/CoreTextNode.ts +399 -391
  118. package/src/core/CoreTextureManager.ts +337 -326
  119. package/src/core/Stage.ts +365 -354
  120. package/src/core/animations/AnimationManager.ts +38 -38
  121. package/src/core/animations/CoreAnimation.ts +181 -181
  122. package/src/core/animations/CoreAnimationController.ts +148 -148
  123. package/src/core/lib/ContextSpy.ts +41 -41
  124. package/src/core/lib/ImageWorker.ts +149 -135
  125. package/src/core/lib/Matrix3d.ts +290 -290
  126. package/src/core/lib/RenderCoords.ts +86 -0
  127. package/src/core/lib/WebGlContextWrapper.ts +992 -992
  128. package/src/core/lib/textureCompression.ts +152 -152
  129. package/src/core/lib/utils.ts +250 -241
  130. package/src/core/platform.ts +46 -46
  131. package/src/core/renderers/CoreContextTexture.ts +30 -30
  132. package/src/core/renderers/CoreRenderOp.ts +22 -22
  133. package/src/core/renderers/CoreRenderer.ts +63 -63
  134. package/src/core/renderers/CoreShader.ts +41 -41
  135. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +37 -37
  136. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +233 -230
  137. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +107 -107
  138. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +520 -520
  139. package/src/core/renderers/webgl/WebGlCoreShader.ts +337 -337
  140. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  141. package/src/core/renderers/webgl/internal/RendererUtils.ts +148 -131
  142. package/src/core/renderers/webgl/internal/ShaderUtils.ts +136 -136
  143. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  144. package/src/core/renderers/webgl/shaders/DefaultShader.ts +95 -95
  145. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  146. package/src/core/renderers/webgl/shaders/DynamicShader.ts +474 -474
  147. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +161 -161
  148. package/src/core/renderers/webgl/shaders/SdfShader.ts +174 -174
  149. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  150. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +86 -86
  151. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  152. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  153. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  154. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +33 -33
  155. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +135 -135
  156. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +145 -145
  157. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  158. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +176 -176
  159. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +159 -159
  160. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +186 -186
  161. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +121 -121
  162. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +114 -114
  163. package/src/core/text-rendering/TextTextureRendererUtils.ts +189 -189
  164. package/src/core/text-rendering/TrFontManager.ts +170 -166
  165. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +141 -141
  166. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  167. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  168. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +169 -169
  169. package/src/core/text-rendering/font-face-types/TrFontFace.ts +105 -105
  170. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +77 -77
  171. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +757 -751
  172. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +741 -741
  173. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +784 -778
  174. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  175. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  176. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  177. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  178. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +84 -84
  179. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  180. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  181. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +393 -393
  182. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  183. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +51 -51
  184. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  185. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  186. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  187. package/src/core/text-rendering/renderers/TextRenderer.ts +516 -504
  188. package/src/core/textures/ColorTexture.ts +86 -86
  189. package/src/core/textures/ImageTexture.ts +154 -140
  190. package/src/core/textures/NoiseTexture.ts +96 -96
  191. package/src/core/textures/SubTexture.ts +143 -143
  192. package/src/core/textures/Texture.ts +224 -218
  193. package/src/core/utils.ts +224 -224
  194. package/src/env.d.ts +7 -7
  195. package/src/main-api/ICoreDriver.ts +66 -66
  196. package/src/main-api/INode.ts +499 -499
  197. package/src/main-api/Inspector.ts +439 -439
  198. package/src/main-api/RendererMain.ts +659 -652
  199. package/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.ts +45 -45
  200. package/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.ts +154 -154
  201. package/src/main-api/texture-usage-trackers/TextureUsageTracker.ts +54 -54
  202. package/src/render-drivers/main/MainCoreDriver.ts +149 -148
  203. package/src/render-drivers/main/MainOnlyNode.ts +494 -466
  204. package/src/render-drivers/main/MainOnlyTextNode.ts +261 -261
  205. package/src/render-drivers/threadx/NodeStruct.ts +300 -300
  206. package/src/render-drivers/threadx/SharedNode.ts +97 -97
  207. package/src/render-drivers/threadx/TextNodeStruct.ts +211 -211
  208. package/src/render-drivers/threadx/ThreadXCoreDriver.ts +286 -285
  209. package/src/render-drivers/threadx/ThreadXMainAnimationController.ts +99 -99
  210. package/src/render-drivers/threadx/ThreadXMainNode.ts +192 -192
  211. package/src/render-drivers/threadx/ThreadXMainTextNode.ts +85 -85
  212. package/src/render-drivers/threadx/ThreadXRendererMessage.ts +111 -110
  213. package/src/render-drivers/threadx/worker/ThreadXRendererNode.ts +238 -238
  214. package/src/render-drivers/threadx/worker/ThreadXRendererTextNode.ts +149 -149
  215. package/src/render-drivers/threadx/worker/renderer.ts +152 -151
  216. package/src/render-drivers/utils.ts +97 -97
  217. package/src/utils.ts +207 -207
  218. package/COPYING +0 -1
@@ -1,520 +1,520 @@
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 {
21
- assertTruthy,
22
- createWebGLContext,
23
- hasOwn,
24
- mergeColorAlphaPremultiplied,
25
- } from '../../../utils.js';
26
- import { CoreRenderer, type QuadOptions } 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
- } from './internal/RendererUtils.js';
36
- import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js';
37
- import { Texture } from '../../textures/Texture.js';
38
- import { ColorTexture } from '../../textures/ColorTexture.js';
39
- import type { Stage } from '../../Stage.js';
40
- import { SubTexture } from '../../textures/SubTexture.js';
41
- import { WebGlCoreCtxSubTexture } from './WebGlCoreCtxSubTexture.js';
42
- import type {
43
- CoreTextureManager,
44
- TextureOptions,
45
- } from '../../CoreTextureManager.js';
46
- import { CoreShaderManager } from '../../CoreShaderManager.js';
47
- import type { CoreShader } from '../CoreShader.js';
48
- import { BufferCollection } from './internal/BufferCollection.js';
49
- import {
50
- compareRect,
51
- getNormalizedRgbaComponents,
52
- type Rect,
53
- type RectWithValid,
54
- } from '../../lib/utils.js';
55
- import type { Dimensions } from '../../../common/CommonTypes.js';
56
- import { WebGlCoreShader } from './WebGlCoreShader.js';
57
- import { RoundedRectangle } from './shaders/RoundedRectangle.js';
58
- import { ContextSpy } from '../../lib/ContextSpy.js';
59
- import { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
60
-
61
- const WORDS_PER_QUAD = 24;
62
- const BYTES_PER_QUAD = WORDS_PER_QUAD * 4;
63
-
64
- export interface WebGlCoreRendererOptions {
65
- stage: Stage;
66
- canvas: HTMLCanvasElement | OffscreenCanvas;
67
- pixelRatio: number;
68
- txManager: CoreTextureManager;
69
- shManager: CoreShaderManager;
70
- clearColor: number;
71
- bufferMemory: number;
72
- contextSpy: ContextSpy | null;
73
- }
74
-
75
- interface CoreWebGlSystem {
76
- parameters: CoreWebGlParameters;
77
- extensions: CoreWebGlExtensions;
78
- }
79
-
80
- export class WebGlCoreRenderer extends CoreRenderer {
81
- //// WebGL Native Context and Data
82
- glw: WebGlContextWrapper;
83
- system: CoreWebGlSystem;
84
-
85
- //// Core Managers
86
- txManager: CoreTextureManager;
87
- shManager: CoreShaderManager;
88
-
89
- //// Options
90
- options: Required<WebGlCoreRendererOptions>;
91
-
92
- //// Persistent data
93
- quadBuffer: ArrayBuffer = new ArrayBuffer(1024 * 1024 * 4);
94
- fQuadBuffer: Float32Array = new Float32Array(this.quadBuffer);
95
- uiQuadBuffer: Uint32Array = new Uint32Array(this.quadBuffer);
96
- renderOps: WebGlCoreRenderOp[] = [];
97
-
98
- //// Render Op / Buffer Filling State
99
- curBufferIdx = 0;
100
- curRenderOp: WebGlCoreRenderOp | null = null;
101
- renderables: Array<QuadOptions | WebGlCoreRenderOp> = [];
102
-
103
- //// Default Shader
104
- defaultShader: WebGlCoreShader;
105
- quadBufferCollection: BufferCollection;
106
-
107
- /**
108
- * White pixel texture used by default when no texture is specified.
109
- */
110
- defaultTexture: Texture;
111
-
112
- constructor(options: WebGlCoreRendererOptions) {
113
- super(options.stage);
114
- const { canvas, clearColor, bufferMemory } = options;
115
- this.options = options;
116
- this.txManager = options.txManager;
117
- this.shManager = options.shManager;
118
- this.defaultTexture = new ColorTexture(this.txManager);
119
- // When the default texture is loaded, request a render in case the
120
- // RAF is paused. Fixes: https://github.com/lightning-js/renderer/issues/123
121
- this.defaultTexture.once('loaded', () => {
122
- this.stage.requestRender();
123
- });
124
-
125
- const gl = createWebGLContext(canvas, options.contextSpy);
126
- const glw = (this.glw = new WebGlContextWrapper(gl));
127
-
128
- const color = getNormalizedRgbaComponents(clearColor);
129
- glw.viewport(0, 0, canvas.width, canvas.height);
130
- glw.clearColor(color[0]!, color[1]!, color[2]!, color[3]!);
131
- glw.setBlend(true);
132
- glw.blendFunc(glw.ONE, glw.ONE_MINUS_SRC_ALPHA);
133
-
134
- createIndexBuffer(glw, bufferMemory);
135
-
136
- this.system = {
137
- parameters: getWebGlParameters(this.glw),
138
- extensions: getWebGlExtensions(this.glw),
139
- };
140
- this.shManager.renderer = this;
141
- this.defaultShader = this.shManager.loadShader('DefaultShader').shader;
142
- const quadBuffer = glw.createBuffer();
143
- assertTruthy(quadBuffer);
144
- const stride = 6 * Float32Array.BYTES_PER_ELEMENT;
145
- this.quadBufferCollection = new BufferCollection([
146
- {
147
- buffer: quadBuffer,
148
- attributes: {
149
- a_position: {
150
- name: 'a_position',
151
- size: 2, // 2 components per iteration
152
- type: glw.FLOAT, // the data is 32bit floats
153
- normalized: false, // don't normalize the data
154
- stride, // 0 = move forward size * sizeof(type) each iteration to get the next position
155
- offset: 0, // start at the beginning of the buffer
156
- },
157
- a_textureCoordinate: {
158
- name: 'a_textureCoordinate',
159
- size: 2,
160
- type: glw.FLOAT,
161
- normalized: false,
162
- stride,
163
- offset: 2 * Float32Array.BYTES_PER_ELEMENT,
164
- },
165
- a_color: {
166
- name: 'a_color',
167
- size: 4,
168
- type: glw.UNSIGNED_BYTE,
169
- normalized: true,
170
- stride,
171
- offset: 4 * Float32Array.BYTES_PER_ELEMENT,
172
- },
173
- a_textureIndex: {
174
- name: 'a_textureIndex',
175
- size: 1,
176
- type: glw.FLOAT,
177
- normalized: false,
178
- stride,
179
- offset: 5 * Float32Array.BYTES_PER_ELEMENT,
180
- },
181
- },
182
- },
183
- ]);
184
- }
185
-
186
- reset() {
187
- const { glw } = this;
188
- this.curBufferIdx = 0;
189
- this.curRenderOp = null;
190
- this.renderOps.length = 0;
191
- glw.setScissorTest(false);
192
- glw.clear();
193
- }
194
-
195
- override getShaderManager(): CoreShaderManager {
196
- return this.shManager;
197
- }
198
-
199
- override createCtxTexture(textureSource: Texture): CoreContextTexture {
200
- if (textureSource instanceof SubTexture) {
201
- return new WebGlCoreCtxSubTexture(this.glw, textureSource);
202
- }
203
- return new WebGlCoreCtxTexture(this.glw, textureSource);
204
- }
205
-
206
- /**
207
- * This function adds a quad (a rectangle composed of two triangles) to the WebGL rendering pipeline.
208
- *
209
- * It takes a set of options that define the quad's properties, such as its dimensions, colors, texture, shader, and transformation matrix.
210
- * The function first updates the shader properties with the current dimensions if necessary, then sets the default texture if none is provided.
211
- * It then checks if a new render operation is needed, based on the current shader and clipping rectangle.
212
- * If a new render operation is needed, it creates one and updates the current render operation.
213
- * The function then adjusts the texture coordinates based on the texture options and adds the texture to the texture manager.
214
- *
215
- * Finally, it calculates the vertices for the quad, taking into account any transformations, and adds them to the quad buffer.
216
- * The function updates the length and number of quads in the current render operation, and updates the current buffer index.
217
- */
218
- addQuad(params: QuadOptions) {
219
- const { fQuadBuffer, uiQuadBuffer } = this;
220
- const {
221
- width,
222
- height,
223
- colorTl,
224
- colorTr,
225
- colorBl,
226
- colorBr,
227
- textureOptions,
228
- shader,
229
- shaderProps,
230
- alpha,
231
- clippingRect,
232
- tx,
233
- ty,
234
- ta,
235
- tb,
236
- tc,
237
- td,
238
- } = params;
239
- let { texture } = params;
240
-
241
- /**
242
- * If the shader props contain any automatic properties, update it with the
243
- * current dimensions that will be used to render the quad.
244
- */
245
- if (shaderProps && hasOwn(shaderProps, '$dimensions')) {
246
- const dimensions = shaderProps.$dimensions as Dimensions;
247
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
248
- dimensions.width = width;
249
- dimensions.height = height;
250
- }
251
-
252
- texture = texture ?? this.defaultTexture;
253
- assertTruthy(texture instanceof Texture, 'Invalid texture type');
254
-
255
- let { curBufferIdx: bufferIdx, curRenderOp } = this;
256
- const targetDims = {
257
- width,
258
- height,
259
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
260
- };
261
- const targetShader = shader || this.defaultShader;
262
- assertTruthy(targetShader instanceof WebGlCoreShader);
263
- if (curRenderOp) {
264
- // If the current render op is not the same shader, create a new one
265
- // If the current render op's shader props are not compatible with the
266
- // the new shader props, create a new one render op.
267
- if (
268
- curRenderOp.shader !== targetShader ||
269
- !compareRect(curRenderOp.clippingRect, clippingRect) ||
270
- (curRenderOp.shader !== this.defaultShader &&
271
- (!shaderProps ||
272
- !curRenderOp.shader.canBatchShaderProps(
273
- curRenderOp.shaderProps,
274
- shaderProps,
275
- )))
276
- ) {
277
- curRenderOp = null;
278
- }
279
- }
280
- assertTruthy(targetShader instanceof WebGlCoreShader);
281
-
282
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
283
- if (!curRenderOp) {
284
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
285
- this.newRenderOp(
286
- targetShader,
287
- shaderProps as any,
288
- alpha,
289
- targetDims,
290
- clippingRect,
291
- bufferIdx,
292
- );
293
- curRenderOp = this.curRenderOp;
294
- assertTruthy(curRenderOp);
295
- }
296
-
297
- const flipX = textureOptions?.flipX ?? false;
298
- const flipY = textureOptions?.flipY ?? false;
299
-
300
- let texCoordX1 = 0;
301
- let texCoordY1 = 0;
302
- let texCoordX2 = 1;
303
- let texCoordY2 = 1;
304
-
305
- if (texture instanceof SubTexture) {
306
- const { x: tx, y: ty, width: tw, height: th } = texture.props;
307
- const { width: parentW = 0, height: parentH = 0 } = texture.parentTexture
308
- .dimensions || { width: 0, height: 0 };
309
- texCoordX1 = tx / parentW;
310
- texCoordX2 = texCoordX1 + tw / parentW;
311
- texCoordY1 = ty / parentH;
312
- texCoordY2 = texCoordY1 + th / parentH;
313
- texture = texture.parentTexture;
314
- }
315
-
316
- // Flip texture coordinates if dictated by texture options
317
- if (flipX) {
318
- [texCoordX1, texCoordX2] = [texCoordX2, texCoordX1];
319
- }
320
- if (flipY) {
321
- [texCoordY1, texCoordY2] = [texCoordY2, texCoordY1];
322
- }
323
-
324
- const { txManager } = this.stage;
325
- const ctxTexture = txManager.getCtxTexture(texture);
326
- assertTruthy(ctxTexture instanceof WebGlCoreCtxTexture);
327
- const textureIdx = this.addTexture(ctxTexture, bufferIdx);
328
- curRenderOp = this.curRenderOp;
329
- assertTruthy(curRenderOp);
330
-
331
- // render quad advanced
332
- if (tb !== 0 || tc !== 0) {
333
- // Upper-Left
334
- fQuadBuffer[bufferIdx++] = tx; // vertexX
335
- fQuadBuffer[bufferIdx++] = ty; // vertexY
336
- fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
337
- fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
338
- uiQuadBuffer[bufferIdx++] = colorTl; // color
339
- fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
340
-
341
- // Upper-Right
342
- fQuadBuffer[bufferIdx++] = tx + width * ta;
343
- fQuadBuffer[bufferIdx++] = ty + width * tc;
344
- fQuadBuffer[bufferIdx++] = texCoordX2;
345
- fQuadBuffer[bufferIdx++] = texCoordY1;
346
- uiQuadBuffer[bufferIdx++] = colorTr;
347
- fQuadBuffer[bufferIdx++] = textureIdx;
348
-
349
- // Lower-Left
350
- fQuadBuffer[bufferIdx++] = tx + height * tb;
351
- fQuadBuffer[bufferIdx++] = ty + height * td;
352
- fQuadBuffer[bufferIdx++] = texCoordX1;
353
- fQuadBuffer[bufferIdx++] = texCoordY2;
354
- uiQuadBuffer[bufferIdx++] = colorBl;
355
- fQuadBuffer[bufferIdx++] = textureIdx;
356
-
357
- // Lower-Right
358
- fQuadBuffer[bufferIdx++] = tx + width * ta + height * tb;
359
- fQuadBuffer[bufferIdx++] = ty + width * tc + height * td;
360
- fQuadBuffer[bufferIdx++] = texCoordX2;
361
- fQuadBuffer[bufferIdx++] = texCoordY2;
362
- uiQuadBuffer[bufferIdx++] = colorBr;
363
- fQuadBuffer[bufferIdx++] = textureIdx;
364
- } else {
365
- // Calculate the right corner of the quad
366
- // multiplied by the scale
367
- const rightCornerX = tx + width * ta;
368
- const rightCornerY = ty + height * td;
369
-
370
- // Upper-Left
371
- fQuadBuffer[bufferIdx++] = tx; // vertexX
372
- fQuadBuffer[bufferIdx++] = ty; // vertexY
373
- fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
374
- fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
375
- uiQuadBuffer[bufferIdx++] = colorTl; // color
376
- fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
377
-
378
- // Upper-Right
379
- fQuadBuffer[bufferIdx++] = rightCornerX;
380
- fQuadBuffer[bufferIdx++] = ty;
381
- fQuadBuffer[bufferIdx++] = texCoordX2;
382
- fQuadBuffer[bufferIdx++] = texCoordY1;
383
- uiQuadBuffer[bufferIdx++] = colorTr;
384
- fQuadBuffer[bufferIdx++] = textureIdx;
385
-
386
- // Lower-Left
387
- fQuadBuffer[bufferIdx++] = tx;
388
- fQuadBuffer[bufferIdx++] = rightCornerY;
389
- fQuadBuffer[bufferIdx++] = texCoordX1;
390
- fQuadBuffer[bufferIdx++] = texCoordY2;
391
- uiQuadBuffer[bufferIdx++] = colorBl;
392
- fQuadBuffer[bufferIdx++] = textureIdx;
393
-
394
- // Lower-Right
395
- fQuadBuffer[bufferIdx++] = rightCornerX;
396
- fQuadBuffer[bufferIdx++] = rightCornerY;
397
- fQuadBuffer[bufferIdx++] = texCoordX2;
398
- fQuadBuffer[bufferIdx++] = texCoordY2;
399
- uiQuadBuffer[bufferIdx++] = colorBr;
400
- fQuadBuffer[bufferIdx++] = textureIdx;
401
- }
402
-
403
- // Update the length of the current render op
404
- curRenderOp.length += WORDS_PER_QUAD;
405
- curRenderOp.numQuads++;
406
- this.curBufferIdx = bufferIdx;
407
- }
408
-
409
- /**
410
- * Replace the existing RenderOp with a new one that uses the specified Shader
411
- * and starts at the specified buffer index.
412
- *
413
- * @param shader
414
- * @param bufferIdx
415
- */
416
- private newRenderOp(
417
- shader: WebGlCoreShader,
418
- shaderProps: Record<string, unknown>,
419
- alpha: number,
420
- dimensions: Dimensions,
421
- clippingRect: RectWithValid,
422
- bufferIdx: number,
423
- ) {
424
- const curRenderOp = new WebGlCoreRenderOp(
425
- this.glw,
426
- this.options,
427
- this.quadBufferCollection,
428
- shader,
429
- shaderProps,
430
- alpha,
431
- clippingRect,
432
- dimensions,
433
- bufferIdx,
434
- 0, // Z-Index is only used for explictly added Render Ops
435
- );
436
- this.curRenderOp = curRenderOp;
437
- this.renderOps.push(curRenderOp);
438
- }
439
-
440
- /**
441
- * Add a texture to the current RenderOp. If the texture cannot be added to the
442
- * current RenderOp, a new RenderOp will be created and the texture will be added
443
- * to that one.
444
- *
445
- * If the texture cannot be added to the new RenderOp, an error will be thrown.
446
- *
447
- * @param texture
448
- * @param bufferIdx
449
- * @param recursive
450
- * @returns Assigned Texture Index of the texture in the render op
451
- */
452
- private addTexture(
453
- texture: WebGlCoreCtxTexture,
454
- bufferIdx: number,
455
- recursive?: boolean,
456
- ): number {
457
- const { curRenderOp } = this;
458
- assertTruthy(curRenderOp);
459
- const textureIdx = curRenderOp.addTexture(texture);
460
- // TODO: Refactor to be more DRY
461
- if (textureIdx === 0xffffffff) {
462
- if (recursive) {
463
- throw new Error('Unable to add texture to render op');
464
- }
465
-
466
- const { shader, shaderProps, dimensions, clippingRect, alpha } =
467
- curRenderOp;
468
- this.newRenderOp(
469
- shader,
470
- shaderProps,
471
- alpha,
472
- dimensions,
473
- clippingRect,
474
- bufferIdx,
475
- );
476
- return this.addTexture(texture, bufferIdx, true);
477
- }
478
- return textureIdx;
479
- }
480
-
481
- /**
482
- * add RenderOp to the render pipeline
483
- */
484
- addRenderOp(renderable: WebGlCoreRenderOp) {
485
- this.renderOps.push(renderable);
486
- this.curRenderOp = null;
487
- }
488
-
489
- /**
490
- * Render the current set of RenderOps to render to the specified surface.
491
- *
492
- * TODO: 'screen' is the only supported surface at the moment.
493
- *
494
- * @param surface
495
- */
496
- render(surface: 'screen' | CoreContextTexture = 'screen'): void {
497
- const { glw, quadBuffer } = this;
498
-
499
- const arr = new Float32Array(quadBuffer, 0, this.curBufferIdx);
500
-
501
- const buffer = this.quadBufferCollection.getBuffer('a_position') ?? null;
502
- glw.arrayBufferData(buffer, arr, glw.STATIC_DRAW);
503
-
504
- const doLog = false; // idx++ % 100 === 0;
505
- if (doLog) {
506
- console.log('renderOps', this.renderOps.length);
507
- }
508
- this.renderOps.forEach((renderOp, i) => {
509
- if (doLog) {
510
- console.log('renderOp', i, renderOp.numQuads);
511
- }
512
- renderOp.draw();
513
- });
514
-
515
- // clean up
516
- this.renderables = [];
517
- }
518
- }
519
-
520
- const idx = 0;
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 {
21
+ assertTruthy,
22
+ createWebGLContext,
23
+ hasOwn,
24
+ mergeColorAlphaPremultiplied,
25
+ } from '../../../utils.js';
26
+ import { CoreRenderer, type QuadOptions } 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
+ } from './internal/RendererUtils.js';
36
+ import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js';
37
+ import { Texture } from '../../textures/Texture.js';
38
+ import { ColorTexture } from '../../textures/ColorTexture.js';
39
+ import type { Stage } from '../../Stage.js';
40
+ import { SubTexture } from '../../textures/SubTexture.js';
41
+ import { WebGlCoreCtxSubTexture } from './WebGlCoreCtxSubTexture.js';
42
+ import type {
43
+ CoreTextureManager,
44
+ TextureOptions,
45
+ } from '../../CoreTextureManager.js';
46
+ import { CoreShaderManager } from '../../CoreShaderManager.js';
47
+ import type { CoreShader } from '../CoreShader.js';
48
+ import { BufferCollection } from './internal/BufferCollection.js';
49
+ import {
50
+ compareRect,
51
+ getNormalizedRgbaComponents,
52
+ type Rect,
53
+ type RectWithValid,
54
+ } from '../../lib/utils.js';
55
+ import type { Dimensions } from '../../../common/CommonTypes.js';
56
+ import { WebGlCoreShader } from './WebGlCoreShader.js';
57
+ import { RoundedRectangle } from './shaders/RoundedRectangle.js';
58
+ import { ContextSpy } from '../../lib/ContextSpy.js';
59
+ import { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
60
+
61
+ const WORDS_PER_QUAD = 24;
62
+ const BYTES_PER_QUAD = WORDS_PER_QUAD * 4;
63
+
64
+ export interface WebGlCoreRendererOptions {
65
+ stage: Stage;
66
+ canvas: HTMLCanvasElement | OffscreenCanvas;
67
+ pixelRatio: number;
68
+ txManager: CoreTextureManager;
69
+ shManager: CoreShaderManager;
70
+ clearColor: number;
71
+ bufferMemory: number;
72
+ contextSpy: ContextSpy | null;
73
+ }
74
+
75
+ interface CoreWebGlSystem {
76
+ parameters: CoreWebGlParameters;
77
+ extensions: CoreWebGlExtensions;
78
+ }
79
+
80
+ export class WebGlCoreRenderer extends CoreRenderer {
81
+ //// WebGL Native Context and Data
82
+ glw: WebGlContextWrapper;
83
+ system: CoreWebGlSystem;
84
+
85
+ //// Core Managers
86
+ txManager: CoreTextureManager;
87
+ shManager: CoreShaderManager;
88
+
89
+ //// Options
90
+ options: Required<WebGlCoreRendererOptions>;
91
+
92
+ //// Persistent data
93
+ quadBuffer: ArrayBuffer = new ArrayBuffer(1024 * 1024 * 4);
94
+ fQuadBuffer: Float32Array = new Float32Array(this.quadBuffer);
95
+ uiQuadBuffer: Uint32Array = new Uint32Array(this.quadBuffer);
96
+ renderOps: WebGlCoreRenderOp[] = [];
97
+
98
+ //// Render Op / Buffer Filling State
99
+ curBufferIdx = 0;
100
+ curRenderOp: WebGlCoreRenderOp | null = null;
101
+ renderables: Array<QuadOptions | WebGlCoreRenderOp> = [];
102
+
103
+ //// Default Shader
104
+ defaultShader: WebGlCoreShader;
105
+ quadBufferCollection: BufferCollection;
106
+
107
+ /**
108
+ * White pixel texture used by default when no texture is specified.
109
+ */
110
+ defaultTexture: Texture;
111
+
112
+ constructor(options: WebGlCoreRendererOptions) {
113
+ super(options.stage);
114
+ const { canvas, clearColor, bufferMemory } = options;
115
+ this.options = options;
116
+ this.txManager = options.txManager;
117
+ this.shManager = options.shManager;
118
+ this.defaultTexture = new ColorTexture(this.txManager);
119
+ // When the default texture is loaded, request a render in case the
120
+ // RAF is paused. Fixes: https://github.com/lightning-js/renderer/issues/123
121
+ this.defaultTexture.once('loaded', () => {
122
+ this.stage.requestRender();
123
+ });
124
+
125
+ const gl = createWebGLContext(canvas, options.contextSpy);
126
+ const glw = (this.glw = new WebGlContextWrapper(gl));
127
+
128
+ const color = getNormalizedRgbaComponents(clearColor);
129
+ glw.viewport(0, 0, canvas.width, canvas.height);
130
+ glw.clearColor(color[0]!, color[1]!, color[2]!, color[3]!);
131
+ glw.setBlend(true);
132
+ glw.blendFunc(glw.ONE, glw.ONE_MINUS_SRC_ALPHA);
133
+
134
+ createIndexBuffer(glw, bufferMemory);
135
+
136
+ this.system = {
137
+ parameters: getWebGlParameters(this.glw),
138
+ extensions: getWebGlExtensions(this.glw),
139
+ };
140
+ this.shManager.renderer = this;
141
+ this.defaultShader = this.shManager.loadShader('DefaultShader').shader;
142
+ const quadBuffer = glw.createBuffer();
143
+ assertTruthy(quadBuffer);
144
+ const stride = 6 * Float32Array.BYTES_PER_ELEMENT;
145
+ this.quadBufferCollection = new BufferCollection([
146
+ {
147
+ buffer: quadBuffer,
148
+ attributes: {
149
+ a_position: {
150
+ name: 'a_position',
151
+ size: 2, // 2 components per iteration
152
+ type: glw.FLOAT, // the data is 32bit floats
153
+ normalized: false, // don't normalize the data
154
+ stride, // 0 = move forward size * sizeof(type) each iteration to get the next position
155
+ offset: 0, // start at the beginning of the buffer
156
+ },
157
+ a_textureCoordinate: {
158
+ name: 'a_textureCoordinate',
159
+ size: 2,
160
+ type: glw.FLOAT,
161
+ normalized: false,
162
+ stride,
163
+ offset: 2 * Float32Array.BYTES_PER_ELEMENT,
164
+ },
165
+ a_color: {
166
+ name: 'a_color',
167
+ size: 4,
168
+ type: glw.UNSIGNED_BYTE,
169
+ normalized: true,
170
+ stride,
171
+ offset: 4 * Float32Array.BYTES_PER_ELEMENT,
172
+ },
173
+ a_textureIndex: {
174
+ name: 'a_textureIndex',
175
+ size: 1,
176
+ type: glw.FLOAT,
177
+ normalized: false,
178
+ stride,
179
+ offset: 5 * Float32Array.BYTES_PER_ELEMENT,
180
+ },
181
+ },
182
+ },
183
+ ]);
184
+ }
185
+
186
+ reset() {
187
+ const { glw } = this;
188
+ this.curBufferIdx = 0;
189
+ this.curRenderOp = null;
190
+ this.renderOps.length = 0;
191
+ glw.setScissorTest(false);
192
+ glw.clear();
193
+ }
194
+
195
+ override getShaderManager(): CoreShaderManager {
196
+ return this.shManager;
197
+ }
198
+
199
+ override createCtxTexture(textureSource: Texture): CoreContextTexture {
200
+ if (textureSource instanceof SubTexture) {
201
+ return new WebGlCoreCtxSubTexture(this.glw, textureSource);
202
+ }
203
+ return new WebGlCoreCtxTexture(this.glw, textureSource);
204
+ }
205
+
206
+ /**
207
+ * This function adds a quad (a rectangle composed of two triangles) to the WebGL rendering pipeline.
208
+ *
209
+ * It takes a set of options that define the quad's properties, such as its dimensions, colors, texture, shader, and transformation matrix.
210
+ * The function first updates the shader properties with the current dimensions if necessary, then sets the default texture if none is provided.
211
+ * It then checks if a new render operation is needed, based on the current shader and clipping rectangle.
212
+ * If a new render operation is needed, it creates one and updates the current render operation.
213
+ * The function then adjusts the texture coordinates based on the texture options and adds the texture to the texture manager.
214
+ *
215
+ * Finally, it calculates the vertices for the quad, taking into account any transformations, and adds them to the quad buffer.
216
+ * The function updates the length and number of quads in the current render operation, and updates the current buffer index.
217
+ */
218
+ addQuad(params: QuadOptions) {
219
+ const { fQuadBuffer, uiQuadBuffer } = this;
220
+ const {
221
+ width,
222
+ height,
223
+ colorTl,
224
+ colorTr,
225
+ colorBl,
226
+ colorBr,
227
+ textureOptions,
228
+ shader,
229
+ shaderProps,
230
+ alpha,
231
+ clippingRect,
232
+ tx,
233
+ ty,
234
+ ta,
235
+ tb,
236
+ tc,
237
+ td,
238
+ } = params;
239
+ let { texture } = params;
240
+
241
+ /**
242
+ * If the shader props contain any automatic properties, update it with the
243
+ * current dimensions that will be used to render the quad.
244
+ */
245
+ if (shaderProps && hasOwn(shaderProps, '$dimensions')) {
246
+ const dimensions = shaderProps.$dimensions as Dimensions;
247
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
248
+ dimensions.width = width;
249
+ dimensions.height = height;
250
+ }
251
+
252
+ texture = texture ?? this.defaultTexture;
253
+ assertTruthy(texture instanceof Texture, 'Invalid texture type');
254
+
255
+ let { curBufferIdx: bufferIdx, curRenderOp } = this;
256
+ const targetDims = {
257
+ width,
258
+ height,
259
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
260
+ };
261
+ const targetShader = shader || this.defaultShader;
262
+ assertTruthy(targetShader instanceof WebGlCoreShader);
263
+ if (curRenderOp) {
264
+ // If the current render op is not the same shader, create a new one
265
+ // If the current render op's shader props are not compatible with the
266
+ // the new shader props, create a new one render op.
267
+ if (
268
+ curRenderOp.shader !== targetShader ||
269
+ !compareRect(curRenderOp.clippingRect, clippingRect) ||
270
+ (curRenderOp.shader !== this.defaultShader &&
271
+ (!shaderProps ||
272
+ !curRenderOp.shader.canBatchShaderProps(
273
+ curRenderOp.shaderProps,
274
+ shaderProps,
275
+ )))
276
+ ) {
277
+ curRenderOp = null;
278
+ }
279
+ }
280
+ assertTruthy(targetShader instanceof WebGlCoreShader);
281
+
282
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
283
+ if (!curRenderOp) {
284
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
285
+ this.newRenderOp(
286
+ targetShader,
287
+ shaderProps as any,
288
+ alpha,
289
+ targetDims,
290
+ clippingRect,
291
+ bufferIdx,
292
+ );
293
+ curRenderOp = this.curRenderOp;
294
+ assertTruthy(curRenderOp);
295
+ }
296
+
297
+ const flipX = textureOptions?.flipX ?? false;
298
+ const flipY = textureOptions?.flipY ?? false;
299
+
300
+ let texCoordX1 = 0;
301
+ let texCoordY1 = 0;
302
+ let texCoordX2 = 1;
303
+ let texCoordY2 = 1;
304
+
305
+ if (texture instanceof SubTexture) {
306
+ const { x: tx, y: ty, width: tw, height: th } = texture.props;
307
+ const { width: parentW = 0, height: parentH = 0 } = texture.parentTexture
308
+ .dimensions || { width: 0, height: 0 };
309
+ texCoordX1 = tx / parentW;
310
+ texCoordX2 = texCoordX1 + tw / parentW;
311
+ texCoordY1 = ty / parentH;
312
+ texCoordY2 = texCoordY1 + th / parentH;
313
+ texture = texture.parentTexture;
314
+ }
315
+
316
+ // Flip texture coordinates if dictated by texture options
317
+ if (flipX) {
318
+ [texCoordX1, texCoordX2] = [texCoordX2, texCoordX1];
319
+ }
320
+ if (flipY) {
321
+ [texCoordY1, texCoordY2] = [texCoordY2, texCoordY1];
322
+ }
323
+
324
+ const { txManager } = this.stage;
325
+ const ctxTexture = txManager.getCtxTexture(texture);
326
+ assertTruthy(ctxTexture instanceof WebGlCoreCtxTexture);
327
+ const textureIdx = this.addTexture(ctxTexture, bufferIdx);
328
+ curRenderOp = this.curRenderOp;
329
+ assertTruthy(curRenderOp);
330
+
331
+ // render quad advanced
332
+ if (tb !== 0 || tc !== 0) {
333
+ // Upper-Left
334
+ fQuadBuffer[bufferIdx++] = tx; // vertexX
335
+ fQuadBuffer[bufferIdx++] = ty; // vertexY
336
+ fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
337
+ fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
338
+ uiQuadBuffer[bufferIdx++] = colorTl; // color
339
+ fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
340
+
341
+ // Upper-Right
342
+ fQuadBuffer[bufferIdx++] = tx + width * ta;
343
+ fQuadBuffer[bufferIdx++] = ty + width * tc;
344
+ fQuadBuffer[bufferIdx++] = texCoordX2;
345
+ fQuadBuffer[bufferIdx++] = texCoordY1;
346
+ uiQuadBuffer[bufferIdx++] = colorTr;
347
+ fQuadBuffer[bufferIdx++] = textureIdx;
348
+
349
+ // Lower-Left
350
+ fQuadBuffer[bufferIdx++] = tx + height * tb;
351
+ fQuadBuffer[bufferIdx++] = ty + height * td;
352
+ fQuadBuffer[bufferIdx++] = texCoordX1;
353
+ fQuadBuffer[bufferIdx++] = texCoordY2;
354
+ uiQuadBuffer[bufferIdx++] = colorBl;
355
+ fQuadBuffer[bufferIdx++] = textureIdx;
356
+
357
+ // Lower-Right
358
+ fQuadBuffer[bufferIdx++] = tx + width * ta + height * tb;
359
+ fQuadBuffer[bufferIdx++] = ty + width * tc + height * td;
360
+ fQuadBuffer[bufferIdx++] = texCoordX2;
361
+ fQuadBuffer[bufferIdx++] = texCoordY2;
362
+ uiQuadBuffer[bufferIdx++] = colorBr;
363
+ fQuadBuffer[bufferIdx++] = textureIdx;
364
+ } else {
365
+ // Calculate the right corner of the quad
366
+ // multiplied by the scale
367
+ const rightCornerX = tx + width * ta;
368
+ const rightCornerY = ty + height * td;
369
+
370
+ // Upper-Left
371
+ fQuadBuffer[bufferIdx++] = tx; // vertexX
372
+ fQuadBuffer[bufferIdx++] = ty; // vertexY
373
+ fQuadBuffer[bufferIdx++] = texCoordX1; // texCoordX
374
+ fQuadBuffer[bufferIdx++] = texCoordY1; // texCoordY
375
+ uiQuadBuffer[bufferIdx++] = colorTl; // color
376
+ fQuadBuffer[bufferIdx++] = textureIdx; // texIndex
377
+
378
+ // Upper-Right
379
+ fQuadBuffer[bufferIdx++] = rightCornerX;
380
+ fQuadBuffer[bufferIdx++] = ty;
381
+ fQuadBuffer[bufferIdx++] = texCoordX2;
382
+ fQuadBuffer[bufferIdx++] = texCoordY1;
383
+ uiQuadBuffer[bufferIdx++] = colorTr;
384
+ fQuadBuffer[bufferIdx++] = textureIdx;
385
+
386
+ // Lower-Left
387
+ fQuadBuffer[bufferIdx++] = tx;
388
+ fQuadBuffer[bufferIdx++] = rightCornerY;
389
+ fQuadBuffer[bufferIdx++] = texCoordX1;
390
+ fQuadBuffer[bufferIdx++] = texCoordY2;
391
+ uiQuadBuffer[bufferIdx++] = colorBl;
392
+ fQuadBuffer[bufferIdx++] = textureIdx;
393
+
394
+ // Lower-Right
395
+ fQuadBuffer[bufferIdx++] = rightCornerX;
396
+ fQuadBuffer[bufferIdx++] = rightCornerY;
397
+ fQuadBuffer[bufferIdx++] = texCoordX2;
398
+ fQuadBuffer[bufferIdx++] = texCoordY2;
399
+ uiQuadBuffer[bufferIdx++] = colorBr;
400
+ fQuadBuffer[bufferIdx++] = textureIdx;
401
+ }
402
+
403
+ // Update the length of the current render op
404
+ curRenderOp.length += WORDS_PER_QUAD;
405
+ curRenderOp.numQuads++;
406
+ this.curBufferIdx = bufferIdx;
407
+ }
408
+
409
+ /**
410
+ * Replace the existing RenderOp with a new one that uses the specified Shader
411
+ * and starts at the specified buffer index.
412
+ *
413
+ * @param shader
414
+ * @param bufferIdx
415
+ */
416
+ private newRenderOp(
417
+ shader: WebGlCoreShader,
418
+ shaderProps: Record<string, unknown>,
419
+ alpha: number,
420
+ dimensions: Dimensions,
421
+ clippingRect: RectWithValid,
422
+ bufferIdx: number,
423
+ ) {
424
+ const curRenderOp = new WebGlCoreRenderOp(
425
+ this.glw,
426
+ this.options,
427
+ this.quadBufferCollection,
428
+ shader,
429
+ shaderProps,
430
+ alpha,
431
+ clippingRect,
432
+ dimensions,
433
+ bufferIdx,
434
+ 0, // Z-Index is only used for explictly added Render Ops
435
+ );
436
+ this.curRenderOp = curRenderOp;
437
+ this.renderOps.push(curRenderOp);
438
+ }
439
+
440
+ /**
441
+ * Add a texture to the current RenderOp. If the texture cannot be added to the
442
+ * current RenderOp, a new RenderOp will be created and the texture will be added
443
+ * to that one.
444
+ *
445
+ * If the texture cannot be added to the new RenderOp, an error will be thrown.
446
+ *
447
+ * @param texture
448
+ * @param bufferIdx
449
+ * @param recursive
450
+ * @returns Assigned Texture Index of the texture in the render op
451
+ */
452
+ private addTexture(
453
+ texture: WebGlCoreCtxTexture,
454
+ bufferIdx: number,
455
+ recursive?: boolean,
456
+ ): number {
457
+ const { curRenderOp } = this;
458
+ assertTruthy(curRenderOp);
459
+ const textureIdx = curRenderOp.addTexture(texture);
460
+ // TODO: Refactor to be more DRY
461
+ if (textureIdx === 0xffffffff) {
462
+ if (recursive) {
463
+ throw new Error('Unable to add texture to render op');
464
+ }
465
+
466
+ const { shader, shaderProps, dimensions, clippingRect, alpha } =
467
+ curRenderOp;
468
+ this.newRenderOp(
469
+ shader,
470
+ shaderProps,
471
+ alpha,
472
+ dimensions,
473
+ clippingRect,
474
+ bufferIdx,
475
+ );
476
+ return this.addTexture(texture, bufferIdx, true);
477
+ }
478
+ return textureIdx;
479
+ }
480
+
481
+ /**
482
+ * add RenderOp to the render pipeline
483
+ */
484
+ addRenderOp(renderable: WebGlCoreRenderOp) {
485
+ this.renderOps.push(renderable);
486
+ this.curRenderOp = null;
487
+ }
488
+
489
+ /**
490
+ * Render the current set of RenderOps to render to the specified surface.
491
+ *
492
+ * TODO: 'screen' is the only supported surface at the moment.
493
+ *
494
+ * @param surface
495
+ */
496
+ render(surface: 'screen' | CoreContextTexture = 'screen'): void {
497
+ const { glw, quadBuffer } = this;
498
+
499
+ const arr = new Float32Array(quadBuffer, 0, this.curBufferIdx);
500
+
501
+ const buffer = this.quadBufferCollection.getBuffer('a_position') ?? null;
502
+ glw.arrayBufferData(buffer, arr, glw.STATIC_DRAW);
503
+
504
+ const doLog = false; // idx++ % 100 === 0;
505
+ if (doLog) {
506
+ console.log('renderOps', this.renderOps.length);
507
+ }
508
+ this.renderOps.forEach((renderOp, i) => {
509
+ if (doLog) {
510
+ console.log('renderOp', i, renderOp.numQuads);
511
+ }
512
+ renderOp.draw();
513
+ });
514
+
515
+ // clean up
516
+ this.renderables = [];
517
+ }
518
+ }
519
+
520
+ const idx = 0;