@lightningjs/renderer 3.0.0-beta22 → 3.0.0-beta23

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 (166) hide show
  1. package/README.md +93 -0
  2. package/dist/exports/platform.d.ts +7 -0
  3. package/dist/exports/platform.js +27 -0
  4. package/dist/exports/platform.js.map +1 -0
  5. package/dist/src/core/AutosizeManager.d.ts +29 -0
  6. package/dist/src/core/AutosizeManager.js +169 -0
  7. package/dist/src/core/AutosizeManager.js.map +1 -0
  8. package/dist/src/core/CoreNode.js +10 -14
  9. package/dist/src/core/CoreNode.js.map +1 -1
  10. package/dist/src/core/CoreTextureManager.d.ts +0 -13
  11. package/dist/src/core/CoreTextureManager.js +4 -78
  12. package/dist/src/core/CoreTextureManager.js.map +1 -1
  13. package/dist/src/core/Stage.js +2 -12
  14. package/dist/src/core/Stage.js.map +1 -1
  15. package/dist/src/core/animations/Animation.d.ts +21 -0
  16. package/dist/src/core/animations/Animation.js +194 -0
  17. package/dist/src/core/animations/Animation.js.map +1 -0
  18. package/dist/src/core/animations/CoreAnimationController.d.ts +1 -1
  19. package/dist/src/core/animations/CoreAnimationController.js +4 -2
  20. package/dist/src/core/animations/CoreAnimationController.js.map +1 -1
  21. package/dist/src/core/animations/Playback.d.ts +64 -0
  22. package/dist/src/core/animations/Playback.js +169 -0
  23. package/dist/src/core/animations/Playback.js.map +1 -0
  24. package/dist/src/core/animations/Transition.d.ts +27 -0
  25. package/dist/src/core/animations/Transition.js +52 -0
  26. package/dist/src/core/animations/Transition.js.map +1 -0
  27. package/dist/src/core/animations/utils.d.ts +2 -0
  28. package/dist/src/core/animations/utils.js +136 -0
  29. package/dist/src/core/animations/utils.js.map +1 -0
  30. package/dist/src/core/lib/collectionUtils.js +3 -2
  31. package/dist/src/core/lib/collectionUtils.js.map +1 -1
  32. package/dist/src/core/lib/utils.d.ts +0 -5
  33. package/dist/src/core/lib/utils.js +0 -63
  34. package/dist/src/core/lib/utils.js.map +1 -1
  35. package/dist/src/core/platforms/GlContextWrapper.d.ts +136 -0
  36. package/dist/src/core/platforms/GlContextWrapper.js +32 -0
  37. package/dist/src/core/platforms/GlContextWrapper.js.map +1 -0
  38. package/dist/src/core/platforms/Platform.d.ts +74 -13
  39. package/dist/src/core/platforms/Platform.js +18 -0
  40. package/dist/src/core/platforms/Platform.js.map +1 -1
  41. package/dist/src/core/platforms/web/WebGlContextWrapper.d.ts +776 -0
  42. package/dist/src/core/platforms/web/WebGlContextWrapper.js +1208 -0
  43. package/dist/src/core/platforms/web/WebGlContextWrapper.js.map +1 -0
  44. package/dist/src/core/platforms/web/WebPlatform.d.ts +13 -2
  45. package/dist/src/core/platforms/web/WebPlatform.js +109 -8
  46. package/dist/src/core/platforms/web/WebPlatform.js.map +1 -1
  47. package/dist/src/core/platforms/web/WebPlatformChrome50.d.ts +17 -0
  48. package/dist/src/core/platforms/web/WebPlatformChrome50.js +50 -0
  49. package/dist/src/core/platforms/web/WebPlatformChrome50.js.map +1 -0
  50. package/dist/src/core/platforms/web/WebPlatformLegacy.d.ts +18 -0
  51. package/dist/src/core/platforms/web/WebPlatformLegacy.js +99 -0
  52. package/dist/src/core/platforms/web/WebPlatformLegacy.js.map +1 -0
  53. package/dist/src/core/platforms/web/WebPlatformNext.d.ts +21 -0
  54. package/dist/src/core/platforms/web/WebPlatformNext.js +52 -0
  55. package/dist/src/core/platforms/web/WebPlatformNext.js.map +1 -0
  56. package/dist/src/core/platforms/web/lib/ImageWorker.d.ts +15 -0
  57. package/dist/src/core/platforms/web/lib/ImageWorker.js +189 -0
  58. package/dist/src/core/platforms/web/lib/ImageWorker.js.map +1 -0
  59. package/dist/src/core/platforms/web/lib/createImageBitmap.d.ts +1 -0
  60. package/dist/src/core/platforms/web/lib/createImageBitmap.js +27 -0
  61. package/dist/src/core/platforms/web/lib/createImageBitmap.js.map +1 -0
  62. package/dist/src/core/platforms/web/lib/textureCompression.d.ts +26 -0
  63. package/dist/src/core/platforms/web/lib/textureCompression.js +301 -0
  64. package/dist/src/core/platforms/web/lib/textureCompression.js.map +1 -0
  65. package/dist/src/core/platforms/web/lib/textureSvg.d.ts +7 -0
  66. package/dist/src/core/platforms/web/lib/textureSvg.js +51 -0
  67. package/dist/src/core/platforms/web/lib/textureSvg.js.map +1 -0
  68. package/dist/src/core/platforms/web/lib/utils.d.ts +5 -0
  69. package/dist/src/core/platforms/web/lib/utils.js +86 -0
  70. package/dist/src/core/platforms/web/lib/utils.js.map +1 -0
  71. package/dist/src/core/renderers/CoreRenderer.d.ts +1 -9
  72. package/dist/src/core/renderers/CoreRenderer.js +2 -4
  73. package/dist/src/core/renderers/CoreRenderer.js.map +1 -1
  74. package/dist/src/core/renderers/canvas/CanvasRenderer.d.ts +3 -2
  75. package/dist/src/core/renderers/canvas/CanvasRenderer.js +4 -3
  76. package/dist/src/core/renderers/canvas/CanvasRenderer.js.map +1 -1
  77. package/dist/src/core/renderers/webgl/SdfRenderOp.js +3 -2
  78. package/dist/src/core/renderers/webgl/SdfRenderOp.js.map +1 -1
  79. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.d.ts +2 -2
  80. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js.map +1 -1
  81. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.d.ts +2 -2
  82. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js.map +1 -1
  83. package/dist/src/core/renderers/webgl/WebGlCtxTexture.d.ts +3 -3
  84. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js +1 -2
  85. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js.map +1 -1
  86. package/dist/src/core/renderers/webgl/WebGlRenderer.d.ts +5 -5
  87. package/dist/src/core/renderers/webgl/WebGlRenderer.js +7 -8
  88. package/dist/src/core/renderers/webgl/WebGlRenderer.js.map +1 -1
  89. package/dist/src/core/renderers/webgl/WebGlShaderNode.d.ts +2 -2
  90. package/dist/src/core/renderers/webgl/WebGlShaderProgram.d.ts +2 -2
  91. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js.map +1 -1
  92. package/dist/src/core/renderers/webgl/internal/RendererUtils.d.ts +4 -4
  93. package/dist/src/core/renderers/webgl/internal/RendererUtils.js.map +1 -1
  94. package/dist/src/core/renderers/webgl/internal/ShaderUtils.d.ts +3 -3
  95. package/dist/src/core/renderers/webgl/internal/ShaderUtils.js.map +1 -1
  96. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +24 -8
  97. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js.map +1 -1
  98. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +25 -8
  99. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js.map +1 -1
  100. package/dist/src/core/shaders/webgl/Border.js +6 -4
  101. package/dist/src/core/shaders/webgl/Border.js.map +1 -1
  102. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +6 -4
  103. package/dist/src/core/shaders/webgl/RoundedWithBorder.js.map +1 -1
  104. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +6 -4
  105. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js.map +1 -1
  106. package/dist/src/core/shaders/webgl/SdfShadowShader.d.ts +9 -0
  107. package/dist/src/core/shaders/webgl/SdfShadowShader.js +100 -0
  108. package/dist/src/core/shaders/webgl/SdfShadowShader.js.map +1 -0
  109. package/dist/src/core/text-rendering/CanvasFont.d.ts +1 -1
  110. package/dist/src/core/text-rendering/CanvasFont.js +2 -6
  111. package/dist/src/core/text-rendering/CanvasFont.js.map +1 -1
  112. package/dist/src/core/text-rendering/CoreFont.d.ts +1 -1
  113. package/dist/src/core/text-rendering/CoreFont.js +1 -1
  114. package/dist/src/core/text-rendering/CoreFont.js.map +1 -1
  115. package/dist/src/core/text-rendering/FontManager.js +2 -1
  116. package/dist/src/core/text-rendering/FontManager.js.map +1 -1
  117. package/dist/src/core/textures/ImageTexture.d.ts +24 -11
  118. package/dist/src/core/textures/ImageTexture.js +32 -95
  119. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  120. package/dist/src/core/textures/Texture.d.ts +1 -1
  121. package/dist/src/main-api/Renderer.js +18 -21
  122. package/dist/src/main-api/Renderer.js.map +1 -1
  123. package/dist/src/utils.d.ts +0 -2
  124. package/dist/src/utils.js +0 -36
  125. package/dist/src/utils.js.map +1 -1
  126. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  127. package/dist/tsconfig.tsbuildinfo +1 -0
  128. package/exports/platform.ts +31 -0
  129. package/package.json +3 -2
  130. package/src/core/CoreNode.ts +11 -15
  131. package/src/core/CoreTextureManager.ts +10 -103
  132. package/src/core/Stage.ts +1 -14
  133. package/src/core/animations/CoreAnimationController.ts +5 -2
  134. package/src/core/lib/collectionUtils.ts +3 -2
  135. package/src/core/lib/utils.ts +0 -78
  136. package/src/core/platforms/GlContextWrapper.ts +291 -0
  137. package/src/core/platforms/Platform.ts +121 -28
  138. package/src/core/{lib → platforms/web}/WebGlContextWrapper.ts +129 -4
  139. package/src/core/platforms/web/WebPlatform.ts +171 -22
  140. package/src/core/platforms/web/WebPlatformChrome50.ts +57 -0
  141. package/src/core/platforms/web/WebPlatformLegacy.ts +140 -0
  142. package/src/core/platforms/web/WebPlatformNext.ts +57 -0
  143. package/src/core/{lib → platforms/web/lib}/ImageWorker.ts +10 -74
  144. package/src/core/platforms/web/lib/createImageBitmap.ts +40 -0
  145. package/src/core/{lib → platforms/web/lib}/textureCompression.ts +19 -138
  146. package/src/core/{lib → platforms/web/lib}/textureSvg.ts +3 -15
  147. package/src/core/platforms/web/lib/utils.ts +105 -0
  148. package/src/core/renderers/CoreRenderer.ts +2 -11
  149. package/src/core/renderers/canvas/CanvasRenderer.ts +6 -4
  150. package/src/core/renderers/webgl/SdfRenderOp.ts +3 -2
  151. package/src/core/renderers/webgl/WebGlCtxRenderTexture.ts +2 -2
  152. package/src/core/renderers/webgl/WebGlCtxSubTexture.ts +2 -2
  153. package/src/core/renderers/webgl/WebGlCtxTexture.ts +3 -4
  154. package/src/core/renderers/webgl/WebGlRenderer.ts +12 -19
  155. package/src/core/renderers/webgl/WebGlShaderNode.ts +2 -2
  156. package/src/core/renderers/webgl/WebGlShaderProgram.ts +2 -2
  157. package/src/core/renderers/webgl/internal/RendererUtils.ts +4 -8
  158. package/src/core/renderers/webgl/internal/ShaderUtils.ts +3 -3
  159. package/src/core/shaders/webgl/Border.ts +6 -4
  160. package/src/core/shaders/webgl/RoundedWithBorder.ts +6 -4
  161. package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +6 -4
  162. package/src/core/textures/ImageTexture.ts +42 -161
  163. package/src/core/textures/Texture.ts +1 -1
  164. package/src/main-api/Renderer.ts +24 -22
  165. package/src/utils.ts +0 -47
  166. package/src/core/lib/validateImageBitmap.ts +0 -87
@@ -19,8 +19,6 @@
19
19
 
20
20
  import type { Vec4 } from '../renderers/webgl/internal/ShaderUtils.js';
21
21
 
22
- export const PROTOCOL_REGEX = /^(data|ftps?|https?):/;
23
-
24
22
  export type RGBA = [r: number, g: number, b: number, a: number];
25
23
 
26
24
  export const getNormalizedRgbaComponents = (rgba: number): RGBA => {
@@ -289,35 +287,6 @@ export function createPreloadBounds(
289
287
  );
290
288
  }
291
289
 
292
- export function convertUrlToAbsolute(url: string): string {
293
- // handle local file imports if the url isn't remote resource or data blob
294
- if (self.location.protocol === 'file:' && !PROTOCOL_REGEX.test(url)) {
295
- const path = self.location.pathname.split('/');
296
- path.pop();
297
- const basePath = path.join('/');
298
- const baseUrl = self.location.protocol + '//' + basePath;
299
-
300
- // check if url has a leading dot
301
- if (url.charAt(0) === '.') {
302
- url = url.slice(1);
303
- }
304
-
305
- // check if url has a leading slash
306
- if (url.charAt(0) === '/') {
307
- url = url.slice(1);
308
- }
309
-
310
- return baseUrl + '/' + url;
311
- }
312
-
313
- const absoluteUrl = new URL(url, self.location.href);
314
- return absoluteUrl.href;
315
- }
316
-
317
- export function isBase64Image(src: string) {
318
- return src.startsWith('data:') === true;
319
- }
320
-
321
290
  export function calcFactoredRadius(
322
291
  radius: number,
323
292
  width: number,
@@ -366,50 +335,3 @@ export function calcFactoredRadiusArray(
366
335
  result[3] *= factor;
367
336
  return result;
368
337
  }
369
-
370
- export function dataURIToBlob(dataURI: string): Blob {
371
- dataURI = dataURI.replace(/^data:/, '');
372
-
373
- const type = dataURI.match(/image\/[^;]+/)?.[0] || '';
374
- const base64 = dataURI.replace(/^[^,]+,/, '');
375
-
376
- const sliceSize = 1024;
377
- const byteCharacters = atob(base64);
378
- const bytesLength = byteCharacters.length;
379
- const slicesCount = Math.ceil(bytesLength / sliceSize);
380
- const byteArrays = new Array(slicesCount);
381
-
382
- for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
383
- const begin = sliceIndex * sliceSize;
384
- const end = Math.min(begin + sliceSize, bytesLength);
385
-
386
- const bytes = new Array(end - begin);
387
- for (let offset = begin, i = 0; offset < end; ++i, ++offset) {
388
- bytes[i] = byteCharacters[offset]?.charCodeAt(0);
389
- }
390
- byteArrays[sliceIndex] = new Uint8Array(bytes);
391
- }
392
- return new Blob(byteArrays, { type });
393
- }
394
-
395
- export function fetchJson(
396
- url: string,
397
- responseType: XMLHttpRequestResponseType = '',
398
- ): Promise<unknown> {
399
- return new Promise((resolve, reject) => {
400
- const xhr = new XMLHttpRequest();
401
- xhr.responseType = responseType;
402
- xhr.onreadystatechange = function () {
403
- if (xhr.readyState == XMLHttpRequest.DONE) {
404
- // On most devices like WebOS and Tizen, the file protocol returns 0 while http(s) protocol returns 200
405
- if (xhr.status === 0 || xhr.status === 200) {
406
- resolve(xhr.response);
407
- } else {
408
- reject(xhr.statusText);
409
- }
410
- }
411
- };
412
- xhr.open('GET', url, true);
413
- xhr.send(null);
414
- });
415
- }
@@ -0,0 +1,291 @@
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 2026 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
+ /* eslint-disable @typescript-eslint/no-explicit-any */
21
+
22
+ import type {
23
+ Vec2,
24
+ Vec3,
25
+ Vec4,
26
+ } from '../renderers/webgl/internal/ShaderUtils.js';
27
+
28
+ import type { CompressedData } from '../textures/Texture.js';
29
+
30
+ /**
31
+ * Abstract GL Context Wrapper
32
+ *
33
+ * @remarks
34
+ * This abstract class defines the interface for rendering context wrappers.
35
+ * Implementations should provide optimized state management and caching
36
+ * to avoid redundant API calls when the state is already set to the desired value.
37
+ *
38
+ * The interface is designed to be compatible with WebGL-like rendering contexts
39
+ * but can be adapted for other rendering backends (Canvas2D, WebGPU, Native, etc.).
40
+ */
41
+ export abstract class GlContextWrapper {
42
+ //#region Canvas
43
+ abstract readonly canvas: HTMLCanvasElement | OffscreenCanvas;
44
+ //#endregion Canvas
45
+
46
+ //#region GL Constants
47
+ abstract readonly MAX_RENDERBUFFER_SIZE: number;
48
+ abstract readonly MAX_TEXTURE_SIZE: number;
49
+ abstract readonly MAX_VIEWPORT_DIMS: number;
50
+ abstract readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS: number;
51
+ abstract readonly MAX_TEXTURE_IMAGE_UNITS: number;
52
+ abstract readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS: number;
53
+ abstract readonly MAX_VERTEX_ATTRIBS: number;
54
+ abstract readonly MAX_VARYING_VECTORS: number;
55
+ abstract readonly MAX_VERTEX_UNIFORM_VECTORS: number;
56
+ abstract readonly MAX_FRAGMENT_UNIFORM_VECTORS: number;
57
+ abstract readonly TEXTURE_MAG_FILTER: number;
58
+ abstract readonly TEXTURE_MIN_FILTER: number;
59
+ abstract readonly TEXTURE_WRAP_S: number;
60
+ abstract readonly TEXTURE_WRAP_T: number;
61
+ abstract readonly LINEAR: number;
62
+ abstract readonly LINEAR_MIPMAP_LINEAR: number;
63
+ abstract readonly CLAMP_TO_EDGE: number;
64
+ abstract readonly RGB: number;
65
+ abstract readonly RGBA: number;
66
+ abstract readonly UNSIGNED_BYTE: number;
67
+ abstract readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL: number;
68
+ abstract readonly UNPACK_FLIP_Y_WEBGL: number;
69
+ abstract readonly FLOAT: number;
70
+ abstract readonly TRIANGLES: number;
71
+ abstract readonly UNSIGNED_SHORT: number;
72
+ abstract readonly ONE: number;
73
+ abstract readonly ONE_MINUS_SRC_ALPHA: number;
74
+ abstract readonly VERTEX_SHADER: number;
75
+ abstract readonly FRAGMENT_SHADER: number;
76
+ abstract readonly STATIC_DRAW: number;
77
+ abstract readonly COMPILE_STATUS: number;
78
+ abstract readonly LINK_STATUS: number;
79
+ abstract readonly DYNAMIC_DRAW: number;
80
+ abstract readonly COLOR_ATTACHMENT0: number;
81
+ abstract readonly INVALID_ENUM: number;
82
+ abstract readonly INVALID_OPERATION: number;
83
+ //#endregion GL Constants
84
+
85
+ //#region Texture Management
86
+ abstract activeTexture(textureUnit: number): void;
87
+ abstract bindTexture(texture: any | null): void;
88
+ abstract texParameteri(pname: number, param: number): void;
89
+ abstract texImage2D(
90
+ level: GLint,
91
+ internalformat: GLint,
92
+ width: GLsizei,
93
+ height: GLsizei,
94
+ border: GLint,
95
+ format: GLenum,
96
+ type: GLenum,
97
+ pixels: ArrayBufferView | null,
98
+ ): void;
99
+ abstract texImage2D(
100
+ level: GLint,
101
+ internalformat: GLint,
102
+ format: GLenum,
103
+ type: GLenum,
104
+ source: TexImageSource | Uint8Array,
105
+ ): void;
106
+ abstract compressedTexImage2D(
107
+ level: GLint,
108
+ internalformat: GLenum,
109
+ width: GLsizei,
110
+ height: GLsizei,
111
+ border: GLint,
112
+ data?: ArrayBufferView,
113
+ ): void;
114
+ abstract pixelStorei(pname: GLenum, param: GLint | GLboolean): void;
115
+ abstract generateMipmap(): void;
116
+ abstract createTexture(): any;
117
+ abstract deleteTexture(texture: any | null): void;
118
+ //#endregion Texture Management
119
+
120
+ //#region Viewport & Clear
121
+ abstract viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei): void;
122
+ abstract clearColor(
123
+ red: GLclampf,
124
+ green: GLclampf,
125
+ blue: GLclampf,
126
+ alpha: GLclampf,
127
+ ): void;
128
+ abstract clear(): void;
129
+ //#endregion Viewport & Clear
130
+
131
+ //#region Scissor & Blend
132
+ abstract setScissorTest(enable: boolean): void;
133
+ abstract scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei): void;
134
+ abstract setBlend(blend: boolean): void;
135
+ abstract blendFunc(src: GLenum, dst: GLenum): void;
136
+ //#endregion Scissor & Blend
137
+
138
+ //#region Buffer Management
139
+ abstract createBuffer(): any;
140
+ abstract arrayBufferData(
141
+ buffer: any | null,
142
+ data: ArrayBufferView,
143
+ usage: GLenum,
144
+ ): void;
145
+ abstract elementArrayBufferData(
146
+ buffer: any | null,
147
+ data: ArrayBufferView,
148
+ usage: GLenum,
149
+ ): void;
150
+ abstract vertexAttribPointer(
151
+ buffer: any,
152
+ index: GLuint,
153
+ size: GLint,
154
+ type: GLenum,
155
+ normalized: GLboolean,
156
+ stride: GLsizei,
157
+ offset: GLintptr,
158
+ ): void;
159
+ abstract deleteBuffer(buffer: any): void;
160
+ //#endregion Buffer Management
161
+
162
+ //#region Framebuffer Management
163
+ abstract createFramebuffer(): any;
164
+ abstract deleteFramebuffer(framebuffer: any | null): void;
165
+ abstract bindFramebuffer(framebuffer: any | null): void;
166
+ abstract framebufferTexture2D(
167
+ attachment: GLenum,
168
+ texture: any | null,
169
+ level: GLint,
170
+ ): void;
171
+ //#endregion Framebuffer Management
172
+
173
+ //#region Shader & Program Management
174
+ abstract createShader(type: number): any;
175
+ abstract compileShader(shader: any): void;
176
+ abstract attachShader(program: any, shader: any): void;
177
+ abstract linkProgram(program: any): void;
178
+ abstract deleteProgram(shader: any): void;
179
+ abstract getShaderParameter(shader: any, pname: GLenum): any;
180
+ abstract getShaderInfoLog(shader: any): string | null;
181
+ abstract createProgram(): any;
182
+ abstract getProgramParameter(program: any, pname: GLenum): any;
183
+ abstract getProgramInfoLog(program: any): string | null;
184
+ abstract shaderSource(shader: any, source: string): void;
185
+ abstract deleteShader(shader: any): void;
186
+ abstract useProgram(
187
+ program: any | null,
188
+ uniformLocations: Record<string, any>,
189
+ ): void;
190
+ abstract getUniformLocations(program: any): Record<string, any>;
191
+ abstract getAttributeLocations(program: any): string[];
192
+ abstract getAttribLocation(program: any, name: string): number;
193
+ abstract getUniformLocation(program: any, name: string): any;
194
+ abstract enableVertexAttribArray(index: number): void;
195
+ abstract disableVertexAttribArray(index: number): void;
196
+ //#endregion Shader & Program Management
197
+
198
+ //#region Uniform Setters
199
+ abstract uniform1f(location: string, v0: number): void;
200
+ abstract uniform1fv(location: string, value: Float32Array): void;
201
+ abstract uniform1i(location: string, v0: number): void;
202
+ abstract uniform1iv(location: string, value: Int32Array): void;
203
+ abstract uniform2f(location: string, v0: number, v1: number): void;
204
+ abstract uniform2fa(location: string, value: Vec2): void;
205
+ abstract uniform2fv(location: string, value: Float32Array): void;
206
+ abstract uniform2i(location: string, v0: number, v1: number): void;
207
+ abstract uniform2iv(location: string, value: Int32Array): void;
208
+ abstract uniform3f(
209
+ location: string,
210
+ v0: number,
211
+ v1: number,
212
+ v2: number,
213
+ ): void;
214
+ abstract uniform3fa(location: string, value: Vec3): void;
215
+ abstract uniform3fv(location: string, value: Float32Array): void;
216
+ abstract uniform3i(
217
+ location: string,
218
+ v0: number,
219
+ v1: number,
220
+ v2: number,
221
+ ): void;
222
+ abstract uniform3iv(location: string, value: Int32Array): void;
223
+ abstract uniform4f(
224
+ location: string,
225
+ v0: number,
226
+ v1: number,
227
+ v2: number,
228
+ v3: number,
229
+ ): void;
230
+ abstract uniform4fa(location: string, value: Vec4): void;
231
+ abstract uniform4fv(location: string, value: Float32Array): void;
232
+ abstract uniform4i(
233
+ location: string,
234
+ v0: number,
235
+ v1: number,
236
+ v2: number,
237
+ v3: number,
238
+ ): void;
239
+ abstract uniform4iv(location: string, value: Int32Array): void;
240
+ abstract uniformMatrix2fv(location: string, value: Float32Array): void;
241
+ abstract uniformMatrix3fv(location: string, value: Float32Array): void;
242
+ abstract uniformMatrix4fv(location: string, value: Float32Array): void;
243
+ //#endregion Uniform Setters
244
+
245
+ //#region Vertex Array Objects
246
+ abstract createVertexArray(): any;
247
+ abstract bindVertexArray(vertexArray: any | null): void;
248
+ abstract deleteVertexArray(vertexArray: any): void;
249
+ //#endregion Vertex Array Objects
250
+
251
+ //#region Drawing
252
+ abstract drawElements(
253
+ mode: GLenum,
254
+ count: GLsizei,
255
+ type: GLenum,
256
+ offset: GLintptr,
257
+ ): void;
258
+ abstract drawArrays(mode: GLenum, first: GLint, count: GLsizei): void;
259
+ //#endregion Drawing
260
+
261
+ //#region Context Info & Extensions
262
+ abstract getParameter(pname: GLenum): any;
263
+ abstract getExtension(name: string): any;
264
+ abstract getError(): number;
265
+ abstract checkError(
266
+ operation: string,
267
+ ): { error: number; errorName: string; message: string } | null;
268
+ //#endregion Context Info & Extensions
269
+
270
+ //#region compressed texture support
271
+ abstract uploadKTX(
272
+ glw: GlContextWrapper,
273
+ texture: WebGLTexture,
274
+ data: CompressedData,
275
+ ): void;
276
+ abstract uploadPVR(
277
+ glw: GlContextWrapper,
278
+ texture: WebGLTexture,
279
+ data: CompressedData,
280
+ ): void;
281
+ abstract uploadASTC(
282
+ glw: GlContextWrapper,
283
+ texture: WebGLTexture,
284
+ data: CompressedData,
285
+ ): void;
286
+ //#endregion compressed texture support
287
+
288
+ //#region WebGL 2 specific methods (optional)
289
+ abstract isWebGl2(): boolean;
290
+ //#endregion WebGL 2 specific methods
291
+ }
@@ -18,14 +18,65 @@
18
18
  */
19
19
 
20
20
  import { type Stage } from '../Stage.js';
21
+ import type { WebGlContextWrapper } from './web/WebGlContextWrapper.js';
22
+ import type { ImageResponse } from '../textures/ImageTexture.js';
23
+ import type { GlContextWrapper } from './GlContextWrapper.js';
24
+
25
+ /**
26
+ * Settings for the Platform
27
+ */
28
+ export interface PlatformSettings {
29
+ /**
30
+ * Number of image workers to spawn for parallel image processing
31
+ * @default 2
32
+ */
33
+ numImageWorkers?: number;
34
+
35
+ /**
36
+ * Whether to force the use of WebGL2 (if supported by the platform) or WebGL1. By default, the platform will use WebGL2 if it is available, and fall back to WebGL1 if it is not.
37
+ * @default false
38
+ */
39
+ forceWebGL2?: boolean;
40
+
41
+ /**
42
+ * Optional provided canvas element to use for rendering. If not provided, the platform will create its own canvas element.
43
+ */
44
+ canvas?: HTMLCanvasElement | null;
45
+ }
21
46
 
22
47
  export abstract class Platform {
48
+ public readonly settings: Required<PlatformSettings>;
49
+
50
+ public glw: GlContextWrapper | null = null;
51
+ public canvas: HTMLCanvasElement | null = null;
52
+
53
+ constructor(settings: PlatformSettings = {}) {
54
+ // Apply default settings
55
+ this.settings = {
56
+ numImageWorkers: settings.numImageWorkers ?? 2,
57
+ forceWebGL2: settings.forceWebGL2 ?? false,
58
+ canvas: settings.canvas ?? null,
59
+ };
60
+
61
+ // If a canvas was provided in the settings, use it. Otherwise, create a new one.
62
+ if (this.settings.canvas !== null) {
63
+ this.canvas = this.settings.canvas;
64
+ } else {
65
+ this.canvas = this.createCanvas();
66
+ }
67
+ }
68
+
23
69
  /**
24
70
  * Creates a new canvas element.
25
71
  * @returns The created HTMLCanvasElement.
26
72
  */
27
73
  abstract createCanvas(): HTMLCanvasElement;
28
74
 
75
+ /**
76
+ * Create new rendering context (only for WebGL, Canvas does not require a context)
77
+ */
78
+ abstract createContext(): GlContextWrapper;
79
+
29
80
  /**
30
81
  * Get a DOM element by ID
31
82
  * @returns The DOM element (or null)
@@ -39,35 +90,77 @@ export abstract class Platform {
39
90
  abstract startLoop(stage: Stage): void;
40
91
 
41
92
  /**
42
- * Abstracted createImageBitmap method.
43
- * @param blob - The image source to create the ImageBitmap from.
44
- * @param sxOrOptions - The source rectangle x coordinate or ImageBitmapOptions.
45
- * @param sy - The source rectangle y coordinate.
46
- * @param sw - The source rectangle width.
47
- * @param sh - The source rectangle height.
48
- * @param options - The ImageBitmapOptions.
49
- * @returns A promise that resolves with the created ImageBitmap.
93
+ * Fetches a resource from the network.
94
+ * @param url - The URL of the resource to fetch.
95
+ * @returns A promise that resolves with the response.
96
+ */
97
+ abstract fetch(url: string): Promise<unknown>;
98
+
99
+ /**
100
+ * Loads an image from a UR and returns it as an ImageBitmap or HTMLImageElement.
101
+ * @param src - The source URL or Blob of the image to load.
102
+ * @param premultiplyAlpha - Whether to premultiply alpha (if supported by the platform).
103
+ * @param sx - The x coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
104
+ * @param sy - The y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
105
+ * @param sw - The width of the sub-rectangle of the source image to draw into the destination context.
106
+ * @param sh - The height of the sub-rectangle of the source image to draw into the destination context.
107
+ * @returns A promise that resolves with an object containing the loaded image and whether alpha was premultiplied.
108
+ */
109
+ abstract loadImage(
110
+ src: string,
111
+ premultiplyAlpha: boolean | null,
112
+ sx?: number | null,
113
+ sy?: number | null,
114
+ sw?: number | null,
115
+ sh?: number | null,
116
+ ): Promise<ImageResponse>;
117
+
118
+ /**
119
+ * Create an image out of a Blob
120
+ *
121
+ * @param blob - The Blob to create an image from
122
+ * @param premultiplyAlpha - Whether to premultiply alpha (if supported by the platform).
123
+ * @param sx - The x coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
124
+ * @param sy - The y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
125
+ * @param sw - The width of the sub-rectangle of the source image to draw into the destination context.
126
+ * @param sh - The height of the sub-rectangle of the source image to draw into the destination context.
127
+ * @returns A promise that resolves with an object containing the loaded image and whether alpha was premultiplied.
128
+ */
129
+ abstract createImage(
130
+ blob: Blob,
131
+ premultiplyAlpha: boolean | null,
132
+ sx?: number | null,
133
+ sy?: number | null,
134
+ sw?: number | null,
135
+ sh?: number | null,
136
+ ): Promise<ImageResponse>;
137
+
138
+ /**
139
+ * Loads an SVG image from a URL and returns it as an ImageBitmap or HTMLImageElement.
140
+ * @param src - The source URL of the SVG image to load.
141
+ * @param width - The width to render the SVG image.
142
+ * @param height - The height to render the SVG image.
143
+ * @param sx - The x coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
144
+ * @param sy - The y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
145
+ * @param sw - The width of the sub-rectangle of the source image to draw into the destination context.
146
+ * @param sh - The height of the sub-rectangle of the source image to draw into the destination context.
147
+ */
148
+ abstract loadSvg(
149
+ src: string,
150
+ width: number | null,
151
+ height: number | null,
152
+ sx?: number | null,
153
+ sy?: number | null,
154
+ sw?: number | null,
155
+ sh?: number | null,
156
+ ): Promise<ImageResponse>;
157
+
158
+ /**
159
+ * Loads a compressed texture from a URL and returns it as an ImageBitmap or HTMLImageElement.
160
+ * @param src - The source URL of the compressed texture to load.
161
+ * @returns A promise that resolves with an object containing the loaded image and whether alpha was premultiplied.
50
162
  */
51
- abstract createImageBitmap(blob: ImageBitmapSource): Promise<ImageBitmap>;
52
- abstract createImageBitmap(
53
- blob: ImageBitmapSource,
54
- options: ImageBitmapOptions,
55
- ): Promise<ImageBitmap>;
56
- abstract createImageBitmap(
57
- blob: ImageBitmapSource,
58
- sx: number,
59
- sy: number,
60
- sw: number,
61
- sh: number,
62
- ): Promise<ImageBitmap>;
63
- abstract createImageBitmap(
64
- blob: ImageBitmapSource,
65
- sx: number,
66
- sy: number,
67
- sw: number,
68
- sh: number,
69
- options: ImageBitmapOptions,
70
- ): Promise<ImageBitmap>;
163
+ abstract loadCompressedTexture(src: string): Promise<ImageResponse>;
71
164
 
72
165
  /**
73
166
  * Retrieves the current timestamp.