@lightningjs/renderer 2.9.0-beta2 → 2.9.0-beta3

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 (189) 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.d.ts +2 -1
  6. package/dist/src/core/CoreNode.js +54 -43
  7. package/dist/src/core/CoreNode.js.map +1 -1
  8. package/dist/src/core/CoreTextureManager.d.ts +61 -15
  9. package/dist/src/core/CoreTextureManager.js +179 -104
  10. package/dist/src/core/CoreTextureManager.js.map +1 -1
  11. package/dist/src/core/Stage.d.ts +7 -0
  12. package/dist/src/core/Stage.js +33 -1
  13. package/dist/src/core/Stage.js.map +1 -1
  14. package/dist/src/core/TextureMemoryManager.js +2 -5
  15. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  16. package/dist/src/core/lib/ImageWorker.js +31 -28
  17. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  18. package/dist/src/core/lib/WebGlContextWrapper.d.ts +1 -0
  19. package/dist/src/core/lib/WebGlContextWrapper.js +2 -0
  20. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  21. package/dist/src/core/renderers/CoreContextTexture.d.ts +1 -0
  22. package/dist/src/core/renderers/CoreContextTexture.js +1 -0
  23. package/dist/src/core/renderers/CoreContextTexture.js.map +1 -1
  24. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +1 -1
  25. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +13 -5
  26. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -1
  27. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +6 -8
  28. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -1
  29. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +3 -1
  30. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -1
  31. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +1 -2
  32. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +26 -24
  33. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  34. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +0 -1
  35. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +7 -17
  36. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
  37. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  38. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  39. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  40. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  41. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  42. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  43. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  44. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  45. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  46. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  47. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  48. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  49. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  50. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  51. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  52. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  53. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  54. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  55. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +3 -5
  56. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  57. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +2 -1
  58. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  59. package/dist/src/core/textures/ColorTexture.d.ts +1 -1
  60. package/dist/src/core/textures/ColorTexture.js +2 -1
  61. package/dist/src/core/textures/ColorTexture.js.map +1 -1
  62. package/dist/src/core/textures/ImageTexture.d.ts +8 -1
  63. package/dist/src/core/textures/ImageTexture.js +39 -1
  64. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  65. package/dist/src/core/textures/NoiseTexture.d.ts +1 -1
  66. package/dist/src/core/textures/NoiseTexture.js +2 -1
  67. package/dist/src/core/textures/NoiseTexture.js.map +1 -1
  68. package/dist/src/core/textures/RenderTexture.d.ts +1 -1
  69. package/dist/src/core/textures/RenderTexture.js +2 -1
  70. package/dist/src/core/textures/RenderTexture.js.map +1 -1
  71. package/dist/src/core/textures/SubTexture.d.ts +1 -1
  72. package/dist/src/core/textures/SubTexture.js +21 -4
  73. package/dist/src/core/textures/SubTexture.js.map +1 -1
  74. package/dist/src/core/textures/Texture.d.ts +43 -21
  75. package/dist/src/core/textures/Texture.js +105 -33
  76. package/dist/src/core/textures/Texture.js.map +1 -1
  77. package/dist/src/main-api/Renderer.d.ts +18 -0
  78. package/dist/src/main-api/Renderer.js +6 -4
  79. package/dist/src/main-api/Renderer.js.map +1 -1
  80. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  81. package/exports/canvas.ts +39 -39
  82. package/exports/index.ts +89 -89
  83. package/exports/inspector.ts +24 -24
  84. package/exports/utils.ts +44 -44
  85. package/exports/webgl.ts +38 -38
  86. package/package.json +1 -1
  87. package/scripts/please-use-pnpm.js +13 -13
  88. package/src/common/CommonTypes.ts +139 -139
  89. package/src/common/EventEmitter.ts +77 -77
  90. package/src/common/IAnimationController.ts +92 -92
  91. package/src/common/IEventEmitter.ts +28 -28
  92. package/src/core/CoreNode.test.ts +95 -95
  93. package/src/core/CoreNode.ts +2282 -2258
  94. package/src/core/CoreShaderManager.ts +292 -292
  95. package/src/core/CoreTextNode.ts +450 -450
  96. package/src/core/CoreTextureManager.ts +522 -432
  97. package/src/core/Stage.ts +699 -652
  98. package/src/core/TextureMemoryManager.ts +277 -279
  99. package/src/core/animations/AnimationManager.ts +38 -38
  100. package/src/core/animations/CoreAnimation.ts +340 -340
  101. package/src/core/animations/CoreAnimationController.ts +157 -157
  102. package/src/core/lib/ContextSpy.ts +41 -41
  103. package/src/core/lib/ImageWorker.ts +270 -267
  104. package/src/core/lib/Matrix3d.ts +244 -244
  105. package/src/core/lib/RenderCoords.ts +86 -86
  106. package/src/core/lib/WebGlContextWrapper.ts +1322 -1320
  107. package/src/core/lib/textureCompression.ts +152 -152
  108. package/src/core/lib/textureSvg.ts +78 -78
  109. package/src/core/lib/utils.ts +306 -306
  110. package/src/core/platform.ts +61 -61
  111. package/src/core/renderers/CoreContextTexture.ts +43 -42
  112. package/src/core/renderers/CoreRenderOp.ts +22 -22
  113. package/src/core/renderers/CoreRenderer.ts +114 -114
  114. package/src/core/renderers/CoreShader.ts +41 -41
  115. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +364 -353
  116. package/src/core/renderers/canvas/CanvasCoreTexture.ts +150 -149
  117. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  118. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  119. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  120. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -79
  121. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -48
  122. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +298 -290
  123. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  124. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +805 -817
  125. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  126. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  127. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  128. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  129. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  130. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  131. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  132. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  133. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  134. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  135. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  136. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  137. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  138. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  139. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  140. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  141. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  142. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  143. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  144. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  145. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  146. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  147. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  148. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  149. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  150. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  151. package/src/core/text-rendering/TrFontManager.ts +183 -183
  152. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +169 -171
  153. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  154. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  155. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  156. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  157. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +89 -89
  158. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -507
  159. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +798 -798
  160. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  161. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  162. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  163. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  164. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  165. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  166. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  167. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  168. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +403 -403
  169. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  170. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  171. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  172. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  173. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  174. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  175. package/src/core/textures/ColorTexture.ts +102 -100
  176. package/src/core/textures/ImageTexture.ts +378 -337
  177. package/src/core/textures/NoiseTexture.ts +104 -101
  178. package/src/core/textures/RenderTexture.ts +85 -83
  179. package/src/core/textures/SubTexture.ts +171 -146
  180. package/src/core/textures/Texture.ts +407 -318
  181. package/src/core/utils.ts +227 -227
  182. package/src/env.d.ts +7 -7
  183. package/src/main-api/DynamicShaderController.ts +104 -104
  184. package/src/main-api/INode.ts +101 -101
  185. package/src/main-api/Inspector.ts +505 -505
  186. package/src/main-api/Renderer.ts +693 -670
  187. package/src/main-api/ShaderController.ts +80 -80
  188. package/src/main-api/utils.ts +45 -45
  189. package/src/utils.ts +248 -248
@@ -1,362 +1,362 @@
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 type { Dimensions } from '../../../common/CommonTypes.js';
21
- import { assertTruthy, hasOwn } from '../../../utils.js';
22
- import type { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
23
- import { CoreShader } from '../CoreShader.js';
24
- import type { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js';
25
- import type { WebGlCoreRenderOp } from './WebGlCoreRenderOp.js';
26
- import type { WebGlCoreRenderer } from './WebGlCoreRenderer.js';
27
- import type { BufferCollection } from './internal/BufferCollection.js';
28
- import {
29
- createProgram,
30
- createShader,
31
- type AttributeInfo,
32
- type ShaderOptions,
33
- type UniformInfo,
34
- type UniformMethodMap,
35
- type ShaderProgramSources,
36
- } from './internal/ShaderUtils.js';
37
-
38
- /**
39
- * Automatic shader prop for the dimensions of the Node being rendered
40
- *
41
- * @remarks
42
- * Shader's who's rendering depends on the dimensions of the Node being rendered
43
- * should extend this interface from their Prop interface type.
44
- */
45
- export interface DimensionsShaderProp {
46
- /**
47
- * Dimensions of the Node being rendered (Auto-set by the renderer)
48
- *
49
- * @remarks
50
- * DO NOT SET THIS. It is set automatically by the renderer.
51
- * Any values set here will be ignored.
52
- */
53
- $dimensions?: Dimensions;
54
- }
55
-
56
- export interface AlphaShaderProp {
57
- /**
58
- * Alpha of the Node being rendered (Auto-set by the renderer)
59
- *
60
- * @remarks
61
- * DO NOT SET THIS. It is set automatically by the renderer.
62
- * Any values set here will be ignored.
63
- */
64
- $alpha?: number;
65
- }
66
-
67
- export abstract class WebGlCoreShader extends CoreShader {
68
- protected boundBufferCollection: BufferCollection | null = null;
69
- protected buffersBound = false;
70
- protected program: WebGLProgram;
71
- /**
72
- * Vertex Array Object
73
- *
74
- * @remarks
75
- * Used by WebGL2 Only
76
- */
77
- protected vao: WebGLVertexArrayObject | undefined;
78
- protected renderer: WebGlCoreRenderer;
79
- protected glw: WebGlContextWrapper;
80
- protected attributeBuffers: Record<string, WebGLBuffer>;
81
- protected attributeLocations: Record<string, number>;
82
- protected attributeNames: string[];
83
- protected uniformLocations: Record<string, WebGLUniformLocation>;
84
- protected uniformTypes: Record<string, keyof UniformMethodMap>;
85
- readonly supportsIndexedTextures: boolean;
86
-
87
- constructor(options: ShaderOptions) {
88
- super();
89
- const renderer = (this.renderer = options.renderer);
90
- const glw = (this.glw = this.renderer.glw);
91
- this.supportsIndexedTextures = options.supportsIndexedTextures || false;
92
-
93
- // Check that extensions are supported
94
- const webGl2 = glw.isWebGl2();
95
- const requiredExtensions =
96
- (webGl2 && options.webgl2Extensions) ||
97
- (!webGl2 && options.webgl1Extensions) ||
98
- [];
99
- const glVersion = webGl2 ? '2.0' : '1.0';
100
- requiredExtensions.forEach((extensionName) => {
101
- if (!glw.getExtension(extensionName)) {
102
- throw new Error(
103
- `Shader "${this.constructor.name}" requires extension "${extensionName}" for WebGL ${glVersion} but wasn't found`,
104
- );
105
- }
106
- });
107
-
108
- // Gather shader sources
109
- // - If WebGL 2 and special WebGL 2 sources are provided, we copy those sources and delete
110
- // the extra copy of them to save memory.
111
- // TODO: This could be further made optimal by just caching the compiled shaders and completely deleting
112
- // the source code
113
- const shaderSources =
114
- options.shaderSources ||
115
- (this.constructor as typeof WebGlCoreShader).shaderSources;
116
- if (!shaderSources) {
117
- throw new Error(
118
- `Shader "${this.constructor.name}" is missing shaderSources.`,
119
- );
120
- } else if (webGl2 && shaderSources?.webGl2) {
121
- shaderSources.fragment = shaderSources.webGl2.fragment;
122
- shaderSources.vertex = shaderSources.webGl2.vertex;
123
- delete shaderSources.webGl2;
124
- }
125
-
126
- const textureUnits =
127
- renderer.system.parameters.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
128
-
129
- const vertexSource =
130
- shaderSources.vertex instanceof Function
131
- ? shaderSources.vertex(textureUnits)
132
- : shaderSources.vertex;
133
-
134
- const fragmentSource =
135
- shaderSources.fragment instanceof Function
136
- ? shaderSources.fragment(textureUnits)
137
- : shaderSources.fragment;
138
-
139
- const vertexShader = createShader(glw, glw.VERTEX_SHADER, vertexSource);
140
- const fragmentShader = createShader(
141
- glw,
142
- glw.FRAGMENT_SHADER,
143
- fragmentSource,
144
- );
145
-
146
- if (!vertexShader || !fragmentShader) {
147
- throw new Error(
148
- `Unable to create the following shader(s): ${[
149
- !vertexShader && 'VERTEX_SHADER',
150
- !fragmentShader && 'FRAGMENT_SHADER',
151
- ]
152
- .filter(Boolean)
153
- .join(' and ')}`,
154
- );
155
- }
156
-
157
- const program = createProgram(glw, vertexShader, fragmentShader);
158
- if (!program) {
159
- throw new Error('Unable to create program');
160
- }
161
- this.program = program;
162
-
163
- this.attributeLocations = {} as Record<string, number>;
164
- this.attributeBuffers = {} as Record<string, number>;
165
- this.attributeNames = [];
166
-
167
- [...options.attributes].forEach((attributeName) => {
168
- const location = glw.getAttribLocation(this.program, attributeName);
169
- if (location < 0) {
170
- throw new Error(
171
- `${this.constructor.name}: Vertex shader must have an attribute "${attributeName}"!`,
172
- );
173
- }
174
- const buffer = glw.createBuffer();
175
- if (!buffer) {
176
- throw new Error(
177
- `${this.constructor.name}: Could not create buffer for attribute "${attributeName}"`,
178
- );
179
- }
180
-
181
- this.attributeLocations[attributeName] = location;
182
- this.attributeBuffers[attributeName] = buffer;
183
- this.attributeNames.push(attributeName);
184
- });
185
-
186
- this.uniformLocations = {} as Record<string, WebGLRenderingContext>;
187
- this.uniformTypes = {} as Record<string, keyof UniformMethodMap>;
188
- options.uniforms.forEach((uniform: UniformInfo) => {
189
- const location = glw.getUniformLocation(this.program, uniform.name);
190
- this.uniformTypes[uniform.name] = uniform.uniform;
191
- if (!location) {
192
- console.warn(
193
- `Shader "${this.constructor.name}" could not get uniform location for "${uniform.name}"`,
194
- );
195
- return;
196
- }
197
- this.uniformLocations[uniform.name] = location;
198
- });
199
- }
200
-
201
- private bindBufferAttribute(
202
- location: number,
203
- buffer: WebGLBuffer,
204
- attribute: AttributeInfo,
205
- ) {
206
- const { glw } = this;
207
- glw.enableVertexAttribArray(location);
208
-
209
- glw.vertexAttribPointer(
210
- buffer,
211
- location,
212
- attribute.size,
213
- attribute.type,
214
- attribute.normalized,
215
- attribute.stride,
216
- attribute.offset,
217
- );
218
- }
219
-
220
- disableAttribute(location: number) {
221
- this.glw.disableVertexAttribArray(location);
222
- }
223
-
224
- disableAttributes() {
225
- for (const loc in this.attributeLocations) {
226
- this.disableAttribute(this.attributeLocations[loc] as number);
227
- }
228
- this.boundBufferCollection = null;
229
- }
230
-
231
- /**
232
- * Given two sets of Shader props destined for this Shader, determine if they can be batched together
233
- * to reduce the number of draw calls.
234
- *
235
- * @remarks
236
- * This is used by the {@link WebGlCoreRenderer} to determine if it can batch multiple consecutive draw
237
- * calls into a single draw call.
238
- *
239
- * By default, this returns false (meaning no batching is allowed), but can be
240
- * overridden by child classes to provide more efficient batching.
241
- *
242
- * @param propsA
243
- * @param propsB
244
- * @returns
245
- */
246
- canBatchShaderProps(
247
- propsA: Record<string, unknown>,
248
- propsB: Record<string, unknown>,
249
- ): boolean {
250
- return false;
251
- }
252
-
253
- bindRenderOp(
254
- renderOp: WebGlCoreRenderOp,
255
- props: Record<string, unknown> | null,
256
- ) {
257
- this.bindBufferCollection(renderOp.buffers);
258
- if (renderOp.textures.length > 0) {
259
- this.bindTextures(renderOp.textures);
260
- }
261
-
262
- const { glw, parentHasRenderTexture, renderToTexture } = renderOp;
263
-
264
- // Skip if the parent and current operation both have render textures
265
- if (renderToTexture && parentHasRenderTexture) {
266
- return;
267
- }
268
-
269
- // Bind render texture framebuffer dimensions as resolution
270
- // if the parent has a render texture
271
- if (parentHasRenderTexture) {
272
- const { width, height } = renderOp.framebufferDimensions || {};
273
- // Force pixel ratio to 1.0 for render textures since they are always 1:1
274
- // the final render texture will be rendered to the screen with the correct pixel ratio
275
- glw.uniform1f(this.getUniformLocation('u_pixelRatio'), 1.0);
276
-
277
- // Set resolution to the framebuffer dimensions
278
- glw.uniform2f(
279
- this.getUniformLocation('u_resolution'),
280
- width ?? 0,
281
- height ?? 0,
282
- );
283
- } else {
284
- glw.uniform1f(
285
- this.getUniformLocation('u_pixelRatio'),
286
- renderOp.options.pixelRatio,
287
- );
288
- glw.uniform2f(
289
- this.getUniformLocation('u_resolution'),
290
- glw.canvas.width,
291
- glw.canvas.height,
292
- );
293
- }
294
-
295
- if (props) {
296
- // Bind optional automatic uniforms
297
- // These are only bound if their keys are present in the props.
298
- if (hasOwn(props, '$dimensions')) {
299
- let dimensions = props.$dimensions as Dimensions | null;
300
- if (!dimensions) {
301
- dimensions = renderOp.dimensions;
302
- }
303
- glw.uniform2f(
304
- this.getUniformLocation('u_dimensions'),
305
- dimensions.width,
306
- dimensions.height,
307
- );
308
- }
309
- if (hasOwn(props, '$alpha')) {
310
- let alpha = props.$alpha as number | null;
311
- if (!alpha) {
312
- alpha = renderOp.alpha;
313
- }
314
- glw.uniform1f(this.getUniformLocation('u_alpha'), alpha);
315
- }
316
- this.bindProps(props);
317
- }
318
- }
319
-
320
- getUniformLocation(name: string): WebGLUniformLocation | null {
321
- return this.uniformLocations[name] || null;
322
- }
323
-
324
- bindBufferCollection(buffer: BufferCollection) {
325
- if (this.boundBufferCollection === buffer) {
326
- return;
327
- }
328
- for (const attributeName in this.attributeLocations) {
329
- const resolvedBuffer = buffer.getBuffer(attributeName);
330
- const resolvedInfo = buffer.getAttributeInfo(attributeName);
331
- assertTruthy(resolvedBuffer, `Buffer for "${attributeName}" not found`);
332
- assertTruthy(resolvedInfo);
333
- this.bindBufferAttribute(
334
- this.attributeLocations[attributeName]!,
335
- resolvedBuffer,
336
- resolvedInfo,
337
- );
338
- }
339
- this.boundBufferCollection = buffer;
340
- }
341
-
342
- protected override bindProps(props: Record<string, unknown>) {
343
- // Implement in child class
344
- }
345
-
346
- bindTextures(textures: WebGlCoreCtxTexture[]) {
347
- // no defaults
348
- }
349
-
350
- override attach(): void {
351
- this.glw.useProgram(this.program);
352
- if (this.glw.isWebGl2() && this.vao) {
353
- this.glw.bindVertexArray(this.vao);
354
- }
355
- }
356
-
357
- override detach(): void {
358
- this.disableAttributes();
359
- }
360
-
361
- protected static shaderSources?: ShaderProgramSources;
362
- }
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 type { Dimensions } from '../../../common/CommonTypes.js';
21
+ import { assertTruthy, hasOwn } from '../../../utils.js';
22
+ import type { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
23
+ import { CoreShader } from '../CoreShader.js';
24
+ import type { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js';
25
+ import type { WebGlCoreRenderOp } from './WebGlCoreRenderOp.js';
26
+ import type { WebGlCoreRenderer } from './WebGlCoreRenderer.js';
27
+ import type { BufferCollection } from './internal/BufferCollection.js';
28
+ import {
29
+ createProgram,
30
+ createShader,
31
+ type AttributeInfo,
32
+ type ShaderOptions,
33
+ type UniformInfo,
34
+ type UniformMethodMap,
35
+ type ShaderProgramSources,
36
+ } from './internal/ShaderUtils.js';
37
+
38
+ /**
39
+ * Automatic shader prop for the dimensions of the Node being rendered
40
+ *
41
+ * @remarks
42
+ * Shader's who's rendering depends on the dimensions of the Node being rendered
43
+ * should extend this interface from their Prop interface type.
44
+ */
45
+ export interface DimensionsShaderProp {
46
+ /**
47
+ * Dimensions of the Node being rendered (Auto-set by the renderer)
48
+ *
49
+ * @remarks
50
+ * DO NOT SET THIS. It is set automatically by the renderer.
51
+ * Any values set here will be ignored.
52
+ */
53
+ $dimensions?: Dimensions;
54
+ }
55
+
56
+ export interface AlphaShaderProp {
57
+ /**
58
+ * Alpha of the Node being rendered (Auto-set by the renderer)
59
+ *
60
+ * @remarks
61
+ * DO NOT SET THIS. It is set automatically by the renderer.
62
+ * Any values set here will be ignored.
63
+ */
64
+ $alpha?: number;
65
+ }
66
+
67
+ export abstract class WebGlCoreShader extends CoreShader {
68
+ protected boundBufferCollection: BufferCollection | null = null;
69
+ protected buffersBound = false;
70
+ protected program: WebGLProgram;
71
+ /**
72
+ * Vertex Array Object
73
+ *
74
+ * @remarks
75
+ * Used by WebGL2 Only
76
+ */
77
+ protected vao: WebGLVertexArrayObject | undefined;
78
+ protected renderer: WebGlCoreRenderer;
79
+ protected glw: WebGlContextWrapper;
80
+ protected attributeBuffers: Record<string, WebGLBuffer>;
81
+ protected attributeLocations: Record<string, number>;
82
+ protected attributeNames: string[];
83
+ protected uniformLocations: Record<string, WebGLUniformLocation>;
84
+ protected uniformTypes: Record<string, keyof UniformMethodMap>;
85
+ readonly supportsIndexedTextures: boolean;
86
+
87
+ constructor(options: ShaderOptions) {
88
+ super();
89
+ const renderer = (this.renderer = options.renderer);
90
+ const glw = (this.glw = this.renderer.glw);
91
+ this.supportsIndexedTextures = options.supportsIndexedTextures || false;
92
+
93
+ // Check that extensions are supported
94
+ const webGl2 = glw.isWebGl2();
95
+ const requiredExtensions =
96
+ (webGl2 && options.webgl2Extensions) ||
97
+ (!webGl2 && options.webgl1Extensions) ||
98
+ [];
99
+ const glVersion = webGl2 ? '2.0' : '1.0';
100
+ requiredExtensions.forEach((extensionName) => {
101
+ if (!glw.getExtension(extensionName)) {
102
+ throw new Error(
103
+ `Shader "${this.constructor.name}" requires extension "${extensionName}" for WebGL ${glVersion} but wasn't found`,
104
+ );
105
+ }
106
+ });
107
+
108
+ // Gather shader sources
109
+ // - If WebGL 2 and special WebGL 2 sources are provided, we copy those sources and delete
110
+ // the extra copy of them to save memory.
111
+ // TODO: This could be further made optimal by just caching the compiled shaders and completely deleting
112
+ // the source code
113
+ const shaderSources =
114
+ options.shaderSources ||
115
+ (this.constructor as typeof WebGlCoreShader).shaderSources;
116
+ if (!shaderSources) {
117
+ throw new Error(
118
+ `Shader "${this.constructor.name}" is missing shaderSources.`,
119
+ );
120
+ } else if (webGl2 && shaderSources?.webGl2) {
121
+ shaderSources.fragment = shaderSources.webGl2.fragment;
122
+ shaderSources.vertex = shaderSources.webGl2.vertex;
123
+ delete shaderSources.webGl2;
124
+ }
125
+
126
+ const textureUnits =
127
+ renderer.system.parameters.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
128
+
129
+ const vertexSource =
130
+ shaderSources.vertex instanceof Function
131
+ ? shaderSources.vertex(textureUnits)
132
+ : shaderSources.vertex;
133
+
134
+ const fragmentSource =
135
+ shaderSources.fragment instanceof Function
136
+ ? shaderSources.fragment(textureUnits)
137
+ : shaderSources.fragment;
138
+
139
+ const vertexShader = createShader(glw, glw.VERTEX_SHADER, vertexSource);
140
+ const fragmentShader = createShader(
141
+ glw,
142
+ glw.FRAGMENT_SHADER,
143
+ fragmentSource,
144
+ );
145
+
146
+ if (!vertexShader || !fragmentShader) {
147
+ throw new Error(
148
+ `Unable to create the following shader(s): ${[
149
+ !vertexShader && 'VERTEX_SHADER',
150
+ !fragmentShader && 'FRAGMENT_SHADER',
151
+ ]
152
+ .filter(Boolean)
153
+ .join(' and ')}`,
154
+ );
155
+ }
156
+
157
+ const program = createProgram(glw, vertexShader, fragmentShader);
158
+ if (!program) {
159
+ throw new Error('Unable to create program');
160
+ }
161
+ this.program = program;
162
+
163
+ this.attributeLocations = {} as Record<string, number>;
164
+ this.attributeBuffers = {} as Record<string, number>;
165
+ this.attributeNames = [];
166
+
167
+ [...options.attributes].forEach((attributeName) => {
168
+ const location = glw.getAttribLocation(this.program, attributeName);
169
+ if (location < 0) {
170
+ throw new Error(
171
+ `${this.constructor.name}: Vertex shader must have an attribute "${attributeName}"!`,
172
+ );
173
+ }
174
+ const buffer = glw.createBuffer();
175
+ if (!buffer) {
176
+ throw new Error(
177
+ `${this.constructor.name}: Could not create buffer for attribute "${attributeName}"`,
178
+ );
179
+ }
180
+
181
+ this.attributeLocations[attributeName] = location;
182
+ this.attributeBuffers[attributeName] = buffer;
183
+ this.attributeNames.push(attributeName);
184
+ });
185
+
186
+ this.uniformLocations = {} as Record<string, WebGLRenderingContext>;
187
+ this.uniformTypes = {} as Record<string, keyof UniformMethodMap>;
188
+ options.uniforms.forEach((uniform: UniformInfo) => {
189
+ const location = glw.getUniformLocation(this.program, uniform.name);
190
+ this.uniformTypes[uniform.name] = uniform.uniform;
191
+ if (!location) {
192
+ console.warn(
193
+ `Shader "${this.constructor.name}" could not get uniform location for "${uniform.name}"`,
194
+ );
195
+ return;
196
+ }
197
+ this.uniformLocations[uniform.name] = location;
198
+ });
199
+ }
200
+
201
+ private bindBufferAttribute(
202
+ location: number,
203
+ buffer: WebGLBuffer,
204
+ attribute: AttributeInfo,
205
+ ) {
206
+ const { glw } = this;
207
+ glw.enableVertexAttribArray(location);
208
+
209
+ glw.vertexAttribPointer(
210
+ buffer,
211
+ location,
212
+ attribute.size,
213
+ attribute.type,
214
+ attribute.normalized,
215
+ attribute.stride,
216
+ attribute.offset,
217
+ );
218
+ }
219
+
220
+ disableAttribute(location: number) {
221
+ this.glw.disableVertexAttribArray(location);
222
+ }
223
+
224
+ disableAttributes() {
225
+ for (const loc in this.attributeLocations) {
226
+ this.disableAttribute(this.attributeLocations[loc] as number);
227
+ }
228
+ this.boundBufferCollection = null;
229
+ }
230
+
231
+ /**
232
+ * Given two sets of Shader props destined for this Shader, determine if they can be batched together
233
+ * to reduce the number of draw calls.
234
+ *
235
+ * @remarks
236
+ * This is used by the {@link WebGlCoreRenderer} to determine if it can batch multiple consecutive draw
237
+ * calls into a single draw call.
238
+ *
239
+ * By default, this returns false (meaning no batching is allowed), but can be
240
+ * overridden by child classes to provide more efficient batching.
241
+ *
242
+ * @param propsA
243
+ * @param propsB
244
+ * @returns
245
+ */
246
+ canBatchShaderProps(
247
+ propsA: Record<string, unknown>,
248
+ propsB: Record<string, unknown>,
249
+ ): boolean {
250
+ return false;
251
+ }
252
+
253
+ bindRenderOp(
254
+ renderOp: WebGlCoreRenderOp,
255
+ props: Record<string, unknown> | null,
256
+ ) {
257
+ this.bindBufferCollection(renderOp.buffers);
258
+ if (renderOp.textures.length > 0) {
259
+ this.bindTextures(renderOp.textures);
260
+ }
261
+
262
+ const { glw, parentHasRenderTexture, renderToTexture } = renderOp;
263
+
264
+ // Skip if the parent and current operation both have render textures
265
+ if (renderToTexture && parentHasRenderTexture) {
266
+ return;
267
+ }
268
+
269
+ // Bind render texture framebuffer dimensions as resolution
270
+ // if the parent has a render texture
271
+ if (parentHasRenderTexture) {
272
+ const { width, height } = renderOp.framebufferDimensions || {};
273
+ // Force pixel ratio to 1.0 for render textures since they are always 1:1
274
+ // the final render texture will be rendered to the screen with the correct pixel ratio
275
+ glw.uniform1f(this.getUniformLocation('u_pixelRatio'), 1.0);
276
+
277
+ // Set resolution to the framebuffer dimensions
278
+ glw.uniform2f(
279
+ this.getUniformLocation('u_resolution'),
280
+ width ?? 0,
281
+ height ?? 0,
282
+ );
283
+ } else {
284
+ glw.uniform1f(
285
+ this.getUniformLocation('u_pixelRatio'),
286
+ renderOp.options.pixelRatio,
287
+ );
288
+ glw.uniform2f(
289
+ this.getUniformLocation('u_resolution'),
290
+ glw.canvas.width,
291
+ glw.canvas.height,
292
+ );
293
+ }
294
+
295
+ if (props) {
296
+ // Bind optional automatic uniforms
297
+ // These are only bound if their keys are present in the props.
298
+ if (hasOwn(props, '$dimensions')) {
299
+ let dimensions = props.$dimensions as Dimensions | null;
300
+ if (!dimensions) {
301
+ dimensions = renderOp.dimensions;
302
+ }
303
+ glw.uniform2f(
304
+ this.getUniformLocation('u_dimensions'),
305
+ dimensions.width,
306
+ dimensions.height,
307
+ );
308
+ }
309
+ if (hasOwn(props, '$alpha')) {
310
+ let alpha = props.$alpha as number | null;
311
+ if (!alpha) {
312
+ alpha = renderOp.alpha;
313
+ }
314
+ glw.uniform1f(this.getUniformLocation('u_alpha'), alpha);
315
+ }
316
+ this.bindProps(props);
317
+ }
318
+ }
319
+
320
+ getUniformLocation(name: string): WebGLUniformLocation | null {
321
+ return this.uniformLocations[name] || null;
322
+ }
323
+
324
+ bindBufferCollection(buffer: BufferCollection) {
325
+ if (this.boundBufferCollection === buffer) {
326
+ return;
327
+ }
328
+ for (const attributeName in this.attributeLocations) {
329
+ const resolvedBuffer = buffer.getBuffer(attributeName);
330
+ const resolvedInfo = buffer.getAttributeInfo(attributeName);
331
+ assertTruthy(resolvedBuffer, `Buffer for "${attributeName}" not found`);
332
+ assertTruthy(resolvedInfo);
333
+ this.bindBufferAttribute(
334
+ this.attributeLocations[attributeName]!,
335
+ resolvedBuffer,
336
+ resolvedInfo,
337
+ );
338
+ }
339
+ this.boundBufferCollection = buffer;
340
+ }
341
+
342
+ protected override bindProps(props: Record<string, unknown>) {
343
+ // Implement in child class
344
+ }
345
+
346
+ bindTextures(textures: WebGlCoreCtxTexture[]) {
347
+ // no defaults
348
+ }
349
+
350
+ override attach(): void {
351
+ this.glw.useProgram(this.program);
352
+ if (this.glw.isWebGl2() && this.vao) {
353
+ this.glw.bindVertexArray(this.vao);
354
+ }
355
+ }
356
+
357
+ override detach(): void {
358
+ this.disableAttributes();
359
+ }
360
+
361
+ protected static shaderSources?: ShaderProgramSources;
362
+ }