@lightningjs/renderer 3.0.0-beta10 → 3.0.0-beta11

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 (275) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +133 -133
  4. package/dist/src/core/CoreTextNode.d.ts +3 -0
  5. package/dist/src/core/CoreTextNode.js +45 -10
  6. package/dist/src/core/CoreTextNode.js.map +1 -1
  7. package/dist/src/core/platform.d.ts +10 -0
  8. package/dist/src/core/platform.js +81 -0
  9. package/dist/src/core/platform.js.map +1 -0
  10. package/dist/src/core/renderers/CoreShader.d.ts +9 -0
  11. package/dist/src/core/renderers/CoreShader.js +28 -0
  12. package/dist/src/core/renderers/CoreShader.js.map +1 -0
  13. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +33 -0
  14. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +250 -0
  15. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -0
  16. package/dist/src/core/renderers/canvas/CanvasCoreTexture.d.ts +16 -0
  17. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +124 -0
  18. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -0
  19. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.d.ts +13 -0
  20. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js +113 -192
  21. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js.map +1 -1
  22. package/dist/src/core/renderers/canvas/internal/ColorUtils.d.ts +0 -2
  23. package/dist/src/core/renderers/canvas/internal/ColorUtils.js +0 -14
  24. package/dist/src/core/renderers/canvas/internal/ColorUtils.js.map +1 -1
  25. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.d.ts +10 -0
  26. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js +43 -0
  27. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js.map +1 -0
  28. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.d.ts +12 -0
  29. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js +58 -0
  30. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js.map +1 -0
  31. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.d.ts +9 -0
  32. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +38 -0
  33. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -0
  34. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +56 -0
  35. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +239 -0
  36. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -0
  37. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.d.ts +34 -0
  38. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js +114 -0
  39. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js.map +1 -0
  40. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +133 -0
  41. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +616 -0
  42. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -0
  43. package/dist/src/core/renderers/webgl/WebGlCoreShader.d.ts +83 -0
  44. package/dist/src/core/renderers/webgl/WebGlCoreShader.js +233 -0
  45. package/dist/src/core/renderers/webgl/WebGlCoreShader.js.map +1 -0
  46. package/dist/src/core/renderers/webgl/internal/ShaderUtils.js +35 -35
  47. package/dist/src/core/renderers/webgl/shaders/DefaultShader.d.ts +9 -0
  48. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +87 -0
  49. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js.map +1 -0
  50. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.d.ts +10 -0
  51. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +119 -0
  52. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js.map +1 -0
  53. package/dist/src/core/renderers/webgl/shaders/DynamicShader.d.ts +29 -0
  54. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +413 -0
  55. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js.map +1 -0
  56. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.d.ts +28 -0
  57. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +131 -0
  58. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js.map +1 -0
  59. package/dist/src/core/renderers/webgl/shaders/SdfShader.d.ts +47 -0
  60. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +160 -0
  61. package/dist/src/core/renderers/webgl/shaders/SdfShader.js.map +1 -0
  62. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.d.ts +31 -0
  63. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +71 -0
  64. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js.map +1 -0
  65. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.d.ts +30 -0
  66. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +58 -0
  67. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js.map +1 -0
  68. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.d.ts +31 -0
  69. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +71 -0
  70. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js.map +1 -0
  71. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.d.ts +31 -0
  72. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +71 -0
  73. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js.map +1 -0
  74. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.d.ts +31 -0
  75. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +71 -0
  76. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js.map +1 -0
  77. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.d.ts +9 -0
  78. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js +136 -0
  79. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js.map +1 -0
  80. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.d.ts +36 -0
  81. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +85 -0
  82. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js.map +1 -0
  83. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.d.ts +45 -0
  84. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +104 -0
  85. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js.map +1 -0
  86. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.d.ts +22 -0
  87. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +45 -0
  88. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js.map +1 -0
  89. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.d.ts +58 -0
  90. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +80 -0
  91. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js.map +1 -0
  92. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.d.ts +35 -0
  93. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +129 -0
  94. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js.map +1 -0
  95. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.d.ts +39 -0
  96. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +116 -0
  97. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js.map +1 -0
  98. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.d.ts +61 -0
  99. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +127 -0
  100. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js.map +1 -0
  101. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.d.ts +40 -0
  102. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +71 -0
  103. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js.map +1 -0
  104. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.d.ts +115 -0
  105. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js +61 -0
  106. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js.map +1 -0
  107. package/dist/src/core/shaders/templates/shaderUtils.d.ts +5 -0
  108. package/dist/src/core/shaders/templates/shaderUtils.js +41 -0
  109. package/dist/src/core/shaders/templates/shaderUtils.js.map +1 -0
  110. package/dist/src/core/shaders/webgl/Border.js +82 -82
  111. package/dist/src/core/shaders/webgl/Default.js +47 -47
  112. package/dist/src/core/shaders/webgl/DefaultBatched.js +61 -61
  113. package/dist/src/core/shaders/webgl/HolePunch.js +32 -32
  114. package/dist/src/core/shaders/webgl/LinearGradient.js +36 -36
  115. package/dist/src/core/shaders/webgl/RadialGradient.js +33 -33
  116. package/dist/src/core/shaders/webgl/Rounded.js +71 -71
  117. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +111 -111
  118. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +130 -130
  119. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +54 -54
  120. package/dist/src/core/shaders/webgl/SdfShader.js +62 -62
  121. package/dist/src/core/shaders/webgl/Shadow.js +83 -83
  122. package/dist/src/core/shaders/webgl/Spinner.d.ts +1 -0
  123. package/dist/src/core/shaders/webgl/Spinner.js +2 -0
  124. package/dist/src/core/shaders/webgl/Spinner.js.map +1 -0
  125. package/dist/src/core/text-rendering/CanvasFontHandler.d.ts +16 -0
  126. package/dist/src/core/text-rendering/CanvasFontHandler.js +29 -0
  127. package/dist/src/core/text-rendering/CanvasFontHandler.js.map +1 -1
  128. package/dist/src/core/text-rendering/SdfFontHandler.d.ts +15 -0
  129. package/dist/src/core/text-rendering/SdfFontHandler.js +34 -2
  130. package/dist/src/core/text-rendering/SdfFontHandler.js.map +1 -1
  131. package/dist/src/core/text-rendering/TextRenderer.d.ts +2 -0
  132. package/dist/src/core/text-rendering/TextTextureRendererUtils.js.map +1 -1
  133. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +2 -2
  134. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  135. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +0 -5
  136. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  137. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.d.ts +1 -7
  138. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +2 -50
  139. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
  140. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.d.ts +3 -2
  141. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +83 -42
  142. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  143. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.d.ts +1 -1
  144. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +8 -66
  145. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  146. package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +4 -14
  147. package/dist/src/core/text-rendering/renderers/TextRenderer.js +0 -3
  148. package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
  149. package/dist/src/core/text-rendering/sdf/PeekableGenerator.d.ts +12 -0
  150. package/dist/src/core/text-rendering/sdf/PeekableGenerator.js +61 -0
  151. package/dist/src/core/text-rendering/sdf/PeekableGenerator.js.map +1 -0
  152. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.d.ts +45 -0
  153. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.js +69 -0
  154. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.js.map +1 -0
  155. package/dist/src/main-api/DynamicShaderController.d.ts +29 -0
  156. package/dist/src/main-api/DynamicShaderController.js +58 -0
  157. package/dist/src/main-api/DynamicShaderController.js.map +1 -0
  158. package/dist/src/main-api/ShaderController.d.ts +31 -0
  159. package/dist/src/main-api/ShaderController.js +37 -0
  160. package/dist/src/main-api/ShaderController.js.map +1 -0
  161. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  162. package/exports/canvas-shaders.ts +28 -28
  163. package/exports/canvas.ts +45 -45
  164. package/exports/index.ts +82 -82
  165. package/exports/inspector.ts +24 -24
  166. package/exports/utils.ts +50 -50
  167. package/exports/webgl-shaders.ts +28 -28
  168. package/exports/webgl.ts +52 -52
  169. package/package.json +2 -1
  170. package/src/common/CommonTypes.ts +146 -146
  171. package/src/common/EventEmitter.ts +77 -77
  172. package/src/common/IAnimationController.ts +92 -92
  173. package/src/common/IEventEmitter.ts +28 -28
  174. package/src/core/CoreNode.test.ts +202 -202
  175. package/src/core/CoreNode.ts +2491 -2491
  176. package/src/core/CoreShaderManager.ts +188 -188
  177. package/src/core/CoreTextNode.ts +483 -443
  178. package/src/core/CoreTextureManager.ts +565 -565
  179. package/src/core/Stage.ts +906 -906
  180. package/src/core/TextureMemoryManager.ts +445 -445
  181. package/src/core/animations/AnimationManager.ts +38 -38
  182. package/src/core/animations/CoreAnimation.ts +291 -291
  183. package/src/core/animations/CoreAnimationController.ts +166 -166
  184. package/src/core/lib/ContextSpy.ts +41 -41
  185. package/src/core/lib/ImageWorker.ts +286 -286
  186. package/src/core/lib/Matrix3d.ts +244 -244
  187. package/src/core/lib/RenderCoords.ts +71 -71
  188. package/src/core/lib/WebGlContextWrapper.ts +1381 -1381
  189. package/src/core/lib/colorCache.ts +20 -20
  190. package/src/core/lib/colorParser.ts +85 -85
  191. package/src/core/lib/textureCompression.ts +152 -152
  192. package/src/core/lib/textureSvg.ts +78 -78
  193. package/src/core/lib/utils.ts +412 -412
  194. package/src/core/lib/validateImageBitmap.ts +87 -87
  195. package/src/core/platforms/Platform.ts +77 -77
  196. package/src/core/platforms/web/WebPlatform.ts +121 -121
  197. package/src/core/renderers/CoreContextTexture.ts +43 -43
  198. package/src/core/renderers/CoreRenderOp.ts +22 -22
  199. package/src/core/renderers/CoreRenderer.ts +110 -110
  200. package/src/core/renderers/CoreShaderNode.ts +175 -175
  201. package/src/core/renderers/CoreShaderProgram.ts +23 -23
  202. package/src/core/renderers/canvas/CanvasRenderer.ts +283 -283
  203. package/src/core/renderers/canvas/CanvasShaderNode.ts +96 -96
  204. package/src/core/renderers/canvas/CanvasTexture.ts +156 -156
  205. package/src/core/renderers/webgl/WebGlCtxRenderTexture.ts +91 -91
  206. package/src/core/renderers/webgl/WebGlCtxSubTexture.ts +50 -50
  207. package/src/core/renderers/webgl/WebGlCtxTexture.ts +310 -310
  208. package/src/core/renderers/webgl/WebGlRenderOp.ts +167 -167
  209. package/src/core/renderers/webgl/WebGlRenderer.ts +747 -747
  210. package/src/core/renderers/webgl/WebGlShaderNode.ts +435 -435
  211. package/src/core/renderers/webgl/WebGlShaderProgram.ts +341 -341
  212. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  213. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  214. package/src/core/renderers/webgl/internal/ShaderUtils.ts +281 -281
  215. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  216. package/src/core/shaders/canvas/Border.ts +75 -75
  217. package/src/core/shaders/canvas/HolePunch.ts +62 -62
  218. package/src/core/shaders/canvas/LinearGradient.ts +71 -71
  219. package/src/core/shaders/canvas/RadialGradient.ts +99 -99
  220. package/src/core/shaders/canvas/Rounded.ts +55 -55
  221. package/src/core/shaders/canvas/RoundedWithBorder.ts +74 -74
  222. package/src/core/shaders/canvas/RoundedWithBorderAndShadow.ts +90 -90
  223. package/src/core/shaders/canvas/RoundedWithShadow.ts +70 -70
  224. package/src/core/shaders/canvas/Shadow.ts +52 -52
  225. package/src/core/shaders/canvas/utils/render.ts +151 -151
  226. package/src/core/shaders/templates/BorderTemplate.ts +115 -115
  227. package/src/core/shaders/templates/HolePunchTemplate.ts +82 -82
  228. package/src/core/shaders/templates/LinearGradientTemplate.ts +71 -71
  229. package/src/core/shaders/templates/RadialGradientTemplate.ts +81 -81
  230. package/src/core/shaders/templates/RoundedTemplate.ts +98 -98
  231. package/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.ts +38 -38
  232. package/src/core/shaders/templates/RoundedWithBorderTemplate.ts +35 -35
  233. package/src/core/shaders/templates/RoundedWithShadowTemplate.ts +35 -35
  234. package/src/core/shaders/templates/ShadowTemplate.ts +106 -106
  235. package/src/core/shaders/utils.ts +46 -46
  236. package/src/core/shaders/webgl/Border.ts +116 -116
  237. package/src/core/shaders/webgl/Default.ts +89 -89
  238. package/src/core/shaders/webgl/DefaultBatched.ts +129 -129
  239. package/src/core/shaders/webgl/HolePunch.ts +75 -75
  240. package/src/core/shaders/webgl/LinearGradient.ts +82 -82
  241. package/src/core/shaders/webgl/RadialGradient.ts +85 -85
  242. package/src/core/shaders/webgl/Rounded.ts +117 -117
  243. package/src/core/shaders/webgl/RoundedWithBorder.ts +155 -155
  244. package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +175 -175
  245. package/src/core/shaders/webgl/RoundedWithShadow.ts +98 -98
  246. package/src/core/shaders/webgl/SdfShader.ts +134 -134
  247. package/src/core/shaders/webgl/Shadow.ts +115 -115
  248. package/src/core/text-rendering/CanvasFontHandler.ts +210 -176
  249. package/src/core/text-rendering/CanvasTextRenderer.ts +622 -622
  250. package/src/core/text-rendering/SdfFontHandler.ts +554 -517
  251. package/src/core/text-rendering/SdfTextRenderer.ts +466 -466
  252. package/src/core/text-rendering/TextRenderer.ts +406 -404
  253. package/src/core/text-rendering/Utils.ts +257 -257
  254. package/src/core/text-rendering/canvas/Settings.ts +99 -99
  255. package/src/core/text-rendering/canvas/Utils.test.ts +206 -206
  256. package/src/core/text-rendering/canvas/Utils.ts +178 -178
  257. package/src/core/text-rendering/canvas/calculateRenderInfo.ts +299 -299
  258. package/src/core/text-rendering/canvas/draw.ts +165 -165
  259. package/src/core/text-rendering/sdf/Utils.test.ts +402 -402
  260. package/src/core/text-rendering/sdf/Utils.ts +436 -436
  261. package/src/core/text-rendering/sdf/index.ts +20 -20
  262. package/src/core/textures/ColorTexture.ts +102 -102
  263. package/src/core/textures/ImageTexture.ts +418 -418
  264. package/src/core/textures/NoiseTexture.ts +104 -104
  265. package/src/core/textures/RenderTexture.ts +85 -85
  266. package/src/core/textures/SubTexture.ts +205 -205
  267. package/src/core/textures/Texture.ts +381 -381
  268. package/src/core/utils.ts +227 -227
  269. package/src/env.d.ts +7 -7
  270. package/src/main-api/INode.ts +100 -100
  271. package/src/main-api/Inspector.ts +567 -567
  272. package/src/main-api/Renderer.ts +873 -873
  273. package/src/main-api/utils.ts +45 -45
  274. package/src/utils.ts +267 -267
  275. package/COPYING +0 -1
@@ -1,565 +1,565 @@
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 { ImageWorkerManager } from './lib/ImageWorker.js';
21
- import type { CoreRenderer } from './renderers/CoreRenderer.js';
22
- import { ColorTexture } from './textures/ColorTexture.js';
23
- import { ImageTexture } from './textures/ImageTexture.js';
24
- import { NoiseTexture } from './textures/NoiseTexture.js';
25
- import { SubTexture } from './textures/SubTexture.js';
26
- import { RenderTexture } from './textures/RenderTexture.js';
27
- import { Texture, TextureType } from './textures/Texture.js';
28
- import { EventEmitter } from '../common/EventEmitter.js';
29
- import type { Stage } from './Stage.js';
30
- import {
31
- validateCreateImageBitmap,
32
- type CreateImageBitmapSupport,
33
- } from './lib/validateImageBitmap.js';
34
- import type { Platform } from './platforms/Platform.js';
35
-
36
- /**
37
- * Augmentable map of texture class types
38
- *
39
- * @remarks
40
- * This interface can be augmented by other modules/apps to add additional
41
- * texture types. The ones included directly here are the ones that are
42
- * included in the core library.
43
- */
44
- export interface TextureMap {
45
- ColorTexture: typeof ColorTexture;
46
- ImageTexture: typeof ImageTexture;
47
- NoiseTexture: typeof NoiseTexture;
48
- SubTexture: typeof SubTexture;
49
- RenderTexture: typeof RenderTexture;
50
- }
51
-
52
- export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
53
- ? Props
54
- : never;
55
-
56
- /**
57
- * Contains information about the texture manager's internal state
58
- * for debugging purposes.
59
- */
60
- export interface TextureManagerDebugInfo {
61
- keyCacheSize: number;
62
- }
63
-
64
- export interface TextureManagerSettings {
65
- numImageWorkers: number;
66
- createImageBitmapSupport: 'auto' | 'basic' | 'options' | 'full';
67
- }
68
-
69
- export type ResizeModeOptions =
70
- | {
71
- /**
72
- * Specifies that the image should be resized to cover the specified dimensions.
73
- */
74
- type: 'cover';
75
- /**
76
- * The horizontal clipping position
77
- * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
78
- * clipX 0.5 will clip a equal amount from left and right
79
- *
80
- * @defaultValue 0.5
81
- */
82
- clipX?: number;
83
- /**
84
- * The vertical clipping position
85
- * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
86
- * clipY 0.5 will clip a equal amount from top and bottom
87
- *
88
- * @defaultValue 0.5
89
- */
90
- clipY?: number;
91
- }
92
- | {
93
- /**
94
- * Specifies that the image should be resized to fit within the specified dimensions.
95
- */
96
- type: 'contain';
97
- };
98
-
99
- /**
100
- * Universal options for all texture types
101
- *
102
- * @remarks
103
- * Texture Options provide a way to specify options that are relevant to the
104
- * texture loading process (including caching) and specifically for how a
105
- * texture is rendered within a specific Node (or set of Nodes).
106
- *
107
- * They are not used in determining the cache key for a texture (except if
108
- * the `cacheKey` option is provided explicitly to oveerride the default
109
- * cache key for the texture instance) nor are they stored/referenced within
110
- * the texture instance itself. Instead, the options are stored/referenced
111
- * within individual Nodes. So a single texture instance can be used in
112
- * multiple Nodes each using a different set of options.
113
- */
114
- export interface TextureOptions {
115
- /**
116
- * Preload the texture immediately even if it's not being rendered to the
117
- * screen.
118
- *
119
- * @remarks
120
- * This allows the texture to be used immediately without any delay when it
121
- * is first needed for rendering. Otherwise the loading process will start
122
- * when the texture is first rendered, which may cause a delay in that texture
123
- * being shown properly.
124
- *
125
- * @defaultValue `false`
126
- */
127
- preload?: boolean;
128
-
129
- /**
130
- * Prevent clean up of the texture when it is no longer being used.
131
- *
132
- * @remarks
133
- * This is useful when you want to keep the texture in memory for later use.
134
- * Regardless of whether the texture is being used or not, it will not be
135
- * cleaned up.
136
- *
137
- * @defaultValue `false`
138
- */
139
- preventCleanup?: boolean;
140
-
141
- /**
142
- * Flip the texture horizontally when rendering
143
- *
144
- * @defaultValue `false`
145
- */
146
- flipX?: boolean;
147
-
148
- /**
149
- * Flip the texture vertically when rendering
150
- *
151
- * @defaultValue `false`
152
- */
153
- flipY?: boolean;
154
-
155
- /**
156
- * You can use resizeMode to determine the clipping automatically from the width
157
- * and height of the source texture. This can be convenient if you are unsure about
158
- * the exact image sizes but want the image to cover a specific area.
159
- *
160
- * The resize modes cover and contain are supported
161
- */
162
- resizeMode?: ResizeModeOptions;
163
- }
164
-
165
- export class CoreTextureManager extends EventEmitter {
166
- /**
167
- * Map of textures by cache key
168
- */
169
- keyCache: Map<string, Texture> = new Map();
170
-
171
- /**
172
- * Map of cache keys by texture
173
- */
174
- inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
175
-
176
- /**
177
- * Map of texture constructors by their type name
178
- */
179
- txConstructors: Partial<TextureMap> = {};
180
-
181
- private priorityQueue: Array<Texture> = [];
182
- private uploadTextureQueue: Array<Texture> = [];
183
- private initialized = false;
184
- private stage: Stage;
185
- private numImageWorkers: number;
186
-
187
- public platform: Platform;
188
-
189
- imageWorkerManager: ImageWorkerManager | null = null;
190
- hasCreateImageBitmap = false;
191
- imageBitmapSupported = {
192
- basic: false,
193
- options: false,
194
- full: false,
195
- };
196
-
197
- hasWorker = !!self.Worker;
198
- /**
199
- * Renderer that this texture manager is associated with
200
- *
201
- * @remarks
202
- * This MUST be set before the texture manager is used. Otherwise errors
203
- * will occur when using the texture manager.
204
- */
205
- renderer!: CoreRenderer;
206
-
207
- /**
208
- * The current frame time in milliseconds
209
- *
210
- * @remarks
211
- * This is used to populate the `lastRenderableChangeTime` property of
212
- * {@link Texture} instances when their renderable state changes.
213
- *
214
- * Set by stage via `updateFrameTime` method.
215
- */
216
- frameTime = 0;
217
-
218
- constructor(stage: Stage, settings: TextureManagerSettings) {
219
- super();
220
-
221
- const { numImageWorkers, createImageBitmapSupport } = settings;
222
- this.stage = stage;
223
- this.platform = stage.platform;
224
- this.numImageWorkers = numImageWorkers;
225
-
226
- if (createImageBitmapSupport === 'auto') {
227
- validateCreateImageBitmap(this.platform)
228
- .then((result) => {
229
- this.initialize(result);
230
- })
231
- .catch(() => {
232
- console.warn(
233
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
234
- );
235
-
236
- // initialized without image worker manager and createImageBitmap
237
- this.initialized = true;
238
- this.emit('initialized');
239
- });
240
- } else {
241
- this.initialize({
242
- basic: createImageBitmapSupport === 'basic',
243
- options: createImageBitmapSupport === 'options',
244
- full: createImageBitmapSupport === 'full',
245
- });
246
- }
247
-
248
- this.registerTextureType('ImageTexture', ImageTexture);
249
- this.registerTextureType('ColorTexture', ColorTexture);
250
- this.registerTextureType('NoiseTexture', NoiseTexture);
251
- this.registerTextureType('SubTexture', SubTexture);
252
- this.registerTextureType('RenderTexture', RenderTexture);
253
- }
254
-
255
- registerTextureType<Type extends keyof TextureMap>(
256
- textureType: Type,
257
- textureClass: TextureMap[Type],
258
- ): void {
259
- this.txConstructors[textureType] = textureClass;
260
- }
261
-
262
- private initialize(support: CreateImageBitmapSupport) {
263
- this.hasCreateImageBitmap =
264
- support.basic || support.options || support.full;
265
- this.imageBitmapSupported = support;
266
-
267
- if (this.hasCreateImageBitmap === false) {
268
- console.warn(
269
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
270
- );
271
- }
272
-
273
- if (
274
- this.hasCreateImageBitmap === true &&
275
- this.hasWorker === true &&
276
- this.numImageWorkers > 0
277
- ) {
278
- this.imageWorkerManager = new ImageWorkerManager(
279
- this.numImageWorkers,
280
- support,
281
- );
282
- } else {
283
- console.warn(
284
- '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
285
- );
286
- }
287
-
288
- this.initialized = true;
289
- this.emit('initialized');
290
- }
291
-
292
- /**
293
- * Enqueue a texture for uploading to the GPU.
294
- *
295
- * @param texture - The texture to upload
296
- */
297
- enqueueUploadTexture(texture: Texture): void {
298
- if (this.uploadTextureQueue.includes(texture) === false) {
299
- this.uploadTextureQueue.push(texture);
300
- }
301
- }
302
-
303
- /**
304
- * Create a texture
305
- *
306
- * @param textureType - The type of texture to create
307
- * @param props - The properties to use for the texture
308
- */
309
- createTexture<Type extends keyof TextureMap>(
310
- textureType: Type,
311
- props: ExtractProps<TextureMap[Type]>,
312
- ): InstanceType<TextureMap[Type]> {
313
- let texture: Texture | undefined;
314
- const TextureClass = this.txConstructors[textureType];
315
- if (!TextureClass) {
316
- throw new Error(`Texture type "${textureType}" is not registered`);
317
- }
318
-
319
- const cacheKey = TextureClass.makeCacheKey(props as any);
320
- if (cacheKey && this.keyCache.has(cacheKey)) {
321
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
322
- texture = this.keyCache.get(cacheKey)!;
323
- } else {
324
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
325
- texture = new TextureClass(this, props as any);
326
-
327
- if (cacheKey) {
328
- this.initTextureToCache(texture, cacheKey);
329
- }
330
- }
331
-
332
- return texture as InstanceType<TextureMap[Type]>;
333
- }
334
-
335
- orphanTexture(texture: Texture): void {
336
- // if it is part of the download or upload queue, remove it
337
- this.removeTextureFromQueue(texture);
338
-
339
- if (texture.type === TextureType.subTexture) {
340
- // ignore subtextures
341
- return;
342
- }
343
-
344
- this.stage.txMemManager.addToOrphanedTextures(texture);
345
- }
346
-
347
- /**
348
- * Override loadTexture to use the batched approach.
349
- *
350
- * @param texture - The texture to load
351
- * @param immediate - Whether to prioritize the texture for immediate loading
352
- */
353
- loadTexture(texture: Texture, priority?: boolean): void {
354
- this.stage.txMemManager.removeFromOrphanedTextures(texture);
355
-
356
- if (texture.type === TextureType.subTexture) {
357
- // ignore subtextures - they get loaded through their parent
358
- return;
359
- }
360
-
361
- if (texture.state === 'loaded') {
362
- // if the texture is already loaded, just return
363
- return;
364
- }
365
-
366
- if (Texture.TRANSITIONAL_TEXTURE_STATES.includes(texture.state)) {
367
- return;
368
- }
369
-
370
- // if we're not initialized, just queue the texture into the priority queue
371
- if (this.initialized === false) {
372
- this.priorityQueue.push(texture);
373
- return;
374
- }
375
-
376
- // If the texture failed to load, we need to re-download it.
377
- if (texture.state === 'failed') {
378
- texture.free();
379
- texture.freeTextureData();
380
- }
381
-
382
- texture.setState('loading');
383
-
384
- // Get the texture data
385
- texture
386
- .getTextureData()
387
- .then(() => {
388
- if (texture.state !== 'fetched') {
389
- texture.setState('failed');
390
- return;
391
- }
392
-
393
- // For non-image textures, upload immediately
394
- if (texture.type !== TextureType.image) {
395
- this.uploadTexture(texture).catch((err) => {
396
- console.error('Failed to upload non-image texture:', err);
397
- texture.setState('failed');
398
- });
399
- } else {
400
- // For image textures, queue for throttled upload
401
- // If it's a priority texture, upload it immediately
402
- if (priority === true) {
403
- this.uploadTexture(texture).catch((err) => {
404
- console.error('Failed to upload priority texture:', err);
405
- texture.setState('failed');
406
- });
407
- } else {
408
- this.enqueueUploadTexture(texture);
409
- }
410
- }
411
- })
412
- .catch((err) => {
413
- console.error(err);
414
- texture.setState('failed');
415
- });
416
- }
417
-
418
- /**
419
- * Upload a texture to the GPU
420
- *
421
- * @param texture Texture to upload
422
- * @returns Promise that resolves when the texture is fully loaded
423
- */
424
- async uploadTexture(texture: Texture): Promise<void> {
425
- if (
426
- this.stage.txMemManager.doNotExceedCriticalThreshold === true &&
427
- this.stage.txMemManager.criticalCleanupRequested === true
428
- ) {
429
- // we're at a critical memory threshold, don't upload textures
430
- texture.setState('failed');
431
- return;
432
- }
433
-
434
- const coreContext = texture.loadCtxTexture();
435
- if (coreContext !== null && coreContext.state === 'loaded') {
436
- texture.setState('loaded');
437
- return;
438
- }
439
-
440
- await coreContext.load();
441
- }
442
-
443
- /**
444
- * Check if a texture is being processed
445
- */
446
- isProcessingTexture(texture: Texture): boolean {
447
- return this.uploadTextureQueue.includes(texture) === true;
448
- }
449
-
450
- /**
451
- * Process a limited number of uploads.
452
- *
453
- * @param maxProcessingTime - The maximum processing time in milliseconds
454
- */
455
- async processSome(maxProcessingTime: number): Promise<void> {
456
- if (this.initialized === false) {
457
- return;
458
- }
459
-
460
- const platform = this.platform;
461
- const startTime = platform.getTimeStamp();
462
-
463
- // Process priority queue
464
- while (
465
- this.priorityQueue.length > 0 &&
466
- platform.getTimeStamp() - startTime < maxProcessingTime
467
- ) {
468
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
469
- const texture = this.priorityQueue.pop()!;
470
- try {
471
- await texture.getTextureData();
472
- await this.uploadTexture(texture);
473
- } catch (error) {
474
- console.error('Failed to process priority texture:', error);
475
- // Continue with next texture instead of stopping entire queue
476
- }
477
- }
478
-
479
- // Process uploads - await each upload to prevent GPU overload
480
- while (
481
- this.uploadTextureQueue.length > 0 &&
482
- platform.getTimeStamp() - startTime < maxProcessingTime
483
- ) {
484
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
485
- const texture = this.uploadTextureQueue.shift()!;
486
- try {
487
- await this.uploadTexture(texture);
488
- } catch (error) {
489
- console.error('Failed to upload texture:', error);
490
- // Continue with next texture instead of stopping entire queue
491
- }
492
- }
493
- }
494
-
495
- public hasUpdates(): boolean {
496
- return this.uploadTextureQueue.length > 0;
497
- }
498
-
499
- /**
500
- * Initialize a texture to the cache
501
- *
502
- * @param texture Texture to cache
503
- * @param cacheKey Cache key for the texture
504
- */
505
- initTextureToCache(texture: Texture, cacheKey: string) {
506
- const { keyCache, inverseKeyCache } = this;
507
- keyCache.set(cacheKey, texture);
508
- inverseKeyCache.set(texture, cacheKey);
509
- }
510
-
511
- /**
512
- * Get a texture from the cache
513
- *
514
- * @param cacheKey
515
- */
516
- getTextureFromCache(cacheKey: string): Texture | undefined {
517
- return this.keyCache.get(cacheKey);
518
- }
519
-
520
- /**
521
- * Remove a texture from the cache
522
- *
523
- * @remarks
524
- * Called by Texture Cleanup when a texture is freed.
525
- *
526
- * @param texture
527
- */
528
- removeTextureFromCache(texture: Texture) {
529
- const { inverseKeyCache, keyCache } = this;
530
- const cacheKey = inverseKeyCache.get(texture);
531
- if (cacheKey) {
532
- keyCache.delete(cacheKey);
533
- }
534
- }
535
-
536
- /**
537
- * Remove texture from the upload queue
538
- *
539
- * @param texture - The texture to remove
540
- */
541
- removeTextureFromQueue(texture: Texture): void {
542
- const uploadIndex = this.uploadTextureQueue.indexOf(texture);
543
- if (uploadIndex !== -1) {
544
- this.uploadTextureQueue.splice(uploadIndex, 1);
545
- }
546
- }
547
-
548
- /**
549
- * Resolve a parent texture from the cache or fallback to the provided texture.
550
- *
551
- * @param texture - The provided texture to resolve.
552
- * @returns The cached or provided texture.
553
- */
554
- resolveParentTexture(texture: ImageTexture): Texture {
555
- if (!texture?.props) {
556
- return texture;
557
- }
558
-
559
- const cacheKey = ImageTexture.makeCacheKey(texture.props);
560
- const cachedTexture = cacheKey
561
- ? this.getTextureFromCache(cacheKey)
562
- : undefined;
563
- return cachedTexture ?? texture;
564
- }
565
- }
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 { ImageWorkerManager } from './lib/ImageWorker.js';
21
+ import type { CoreRenderer } from './renderers/CoreRenderer.js';
22
+ import { ColorTexture } from './textures/ColorTexture.js';
23
+ import { ImageTexture } from './textures/ImageTexture.js';
24
+ import { NoiseTexture } from './textures/NoiseTexture.js';
25
+ import { SubTexture } from './textures/SubTexture.js';
26
+ import { RenderTexture } from './textures/RenderTexture.js';
27
+ import { Texture, TextureType } from './textures/Texture.js';
28
+ import { EventEmitter } from '../common/EventEmitter.js';
29
+ import type { Stage } from './Stage.js';
30
+ import {
31
+ validateCreateImageBitmap,
32
+ type CreateImageBitmapSupport,
33
+ } from './lib/validateImageBitmap.js';
34
+ import type { Platform } from './platforms/Platform.js';
35
+
36
+ /**
37
+ * Augmentable map of texture class types
38
+ *
39
+ * @remarks
40
+ * This interface can be augmented by other modules/apps to add additional
41
+ * texture types. The ones included directly here are the ones that are
42
+ * included in the core library.
43
+ */
44
+ export interface TextureMap {
45
+ ColorTexture: typeof ColorTexture;
46
+ ImageTexture: typeof ImageTexture;
47
+ NoiseTexture: typeof NoiseTexture;
48
+ SubTexture: typeof SubTexture;
49
+ RenderTexture: typeof RenderTexture;
50
+ }
51
+
52
+ export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
53
+ ? Props
54
+ : never;
55
+
56
+ /**
57
+ * Contains information about the texture manager's internal state
58
+ * for debugging purposes.
59
+ */
60
+ export interface TextureManagerDebugInfo {
61
+ keyCacheSize: number;
62
+ }
63
+
64
+ export interface TextureManagerSettings {
65
+ numImageWorkers: number;
66
+ createImageBitmapSupport: 'auto' | 'basic' | 'options' | 'full';
67
+ }
68
+
69
+ export type ResizeModeOptions =
70
+ | {
71
+ /**
72
+ * Specifies that the image should be resized to cover the specified dimensions.
73
+ */
74
+ type: 'cover';
75
+ /**
76
+ * The horizontal clipping position
77
+ * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
78
+ * clipX 0.5 will clip a equal amount from left and right
79
+ *
80
+ * @defaultValue 0.5
81
+ */
82
+ clipX?: number;
83
+ /**
84
+ * The vertical clipping position
85
+ * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
86
+ * clipY 0.5 will clip a equal amount from top and bottom
87
+ *
88
+ * @defaultValue 0.5
89
+ */
90
+ clipY?: number;
91
+ }
92
+ | {
93
+ /**
94
+ * Specifies that the image should be resized to fit within the specified dimensions.
95
+ */
96
+ type: 'contain';
97
+ };
98
+
99
+ /**
100
+ * Universal options for all texture types
101
+ *
102
+ * @remarks
103
+ * Texture Options provide a way to specify options that are relevant to the
104
+ * texture loading process (including caching) and specifically for how a
105
+ * texture is rendered within a specific Node (or set of Nodes).
106
+ *
107
+ * They are not used in determining the cache key for a texture (except if
108
+ * the `cacheKey` option is provided explicitly to oveerride the default
109
+ * cache key for the texture instance) nor are they stored/referenced within
110
+ * the texture instance itself. Instead, the options are stored/referenced
111
+ * within individual Nodes. So a single texture instance can be used in
112
+ * multiple Nodes each using a different set of options.
113
+ */
114
+ export interface TextureOptions {
115
+ /**
116
+ * Preload the texture immediately even if it's not being rendered to the
117
+ * screen.
118
+ *
119
+ * @remarks
120
+ * This allows the texture to be used immediately without any delay when it
121
+ * is first needed for rendering. Otherwise the loading process will start
122
+ * when the texture is first rendered, which may cause a delay in that texture
123
+ * being shown properly.
124
+ *
125
+ * @defaultValue `false`
126
+ */
127
+ preload?: boolean;
128
+
129
+ /**
130
+ * Prevent clean up of the texture when it is no longer being used.
131
+ *
132
+ * @remarks
133
+ * This is useful when you want to keep the texture in memory for later use.
134
+ * Regardless of whether the texture is being used or not, it will not be
135
+ * cleaned up.
136
+ *
137
+ * @defaultValue `false`
138
+ */
139
+ preventCleanup?: boolean;
140
+
141
+ /**
142
+ * Flip the texture horizontally when rendering
143
+ *
144
+ * @defaultValue `false`
145
+ */
146
+ flipX?: boolean;
147
+
148
+ /**
149
+ * Flip the texture vertically when rendering
150
+ *
151
+ * @defaultValue `false`
152
+ */
153
+ flipY?: boolean;
154
+
155
+ /**
156
+ * You can use resizeMode to determine the clipping automatically from the width
157
+ * and height of the source texture. This can be convenient if you are unsure about
158
+ * the exact image sizes but want the image to cover a specific area.
159
+ *
160
+ * The resize modes cover and contain are supported
161
+ */
162
+ resizeMode?: ResizeModeOptions;
163
+ }
164
+
165
+ export class CoreTextureManager extends EventEmitter {
166
+ /**
167
+ * Map of textures by cache key
168
+ */
169
+ keyCache: Map<string, Texture> = new Map();
170
+
171
+ /**
172
+ * Map of cache keys by texture
173
+ */
174
+ inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
175
+
176
+ /**
177
+ * Map of texture constructors by their type name
178
+ */
179
+ txConstructors: Partial<TextureMap> = {};
180
+
181
+ private priorityQueue: Array<Texture> = [];
182
+ private uploadTextureQueue: Array<Texture> = [];
183
+ private initialized = false;
184
+ private stage: Stage;
185
+ private numImageWorkers: number;
186
+
187
+ public platform: Platform;
188
+
189
+ imageWorkerManager: ImageWorkerManager | null = null;
190
+ hasCreateImageBitmap = false;
191
+ imageBitmapSupported = {
192
+ basic: false,
193
+ options: false,
194
+ full: false,
195
+ };
196
+
197
+ hasWorker = !!self.Worker;
198
+ /**
199
+ * Renderer that this texture manager is associated with
200
+ *
201
+ * @remarks
202
+ * This MUST be set before the texture manager is used. Otherwise errors
203
+ * will occur when using the texture manager.
204
+ */
205
+ renderer!: CoreRenderer;
206
+
207
+ /**
208
+ * The current frame time in milliseconds
209
+ *
210
+ * @remarks
211
+ * This is used to populate the `lastRenderableChangeTime` property of
212
+ * {@link Texture} instances when their renderable state changes.
213
+ *
214
+ * Set by stage via `updateFrameTime` method.
215
+ */
216
+ frameTime = 0;
217
+
218
+ constructor(stage: Stage, settings: TextureManagerSettings) {
219
+ super();
220
+
221
+ const { numImageWorkers, createImageBitmapSupport } = settings;
222
+ this.stage = stage;
223
+ this.platform = stage.platform;
224
+ this.numImageWorkers = numImageWorkers;
225
+
226
+ if (createImageBitmapSupport === 'auto') {
227
+ validateCreateImageBitmap(this.platform)
228
+ .then((result) => {
229
+ this.initialize(result);
230
+ })
231
+ .catch(() => {
232
+ console.warn(
233
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
234
+ );
235
+
236
+ // initialized without image worker manager and createImageBitmap
237
+ this.initialized = true;
238
+ this.emit('initialized');
239
+ });
240
+ } else {
241
+ this.initialize({
242
+ basic: createImageBitmapSupport === 'basic',
243
+ options: createImageBitmapSupport === 'options',
244
+ full: createImageBitmapSupport === 'full',
245
+ });
246
+ }
247
+
248
+ this.registerTextureType('ImageTexture', ImageTexture);
249
+ this.registerTextureType('ColorTexture', ColorTexture);
250
+ this.registerTextureType('NoiseTexture', NoiseTexture);
251
+ this.registerTextureType('SubTexture', SubTexture);
252
+ this.registerTextureType('RenderTexture', RenderTexture);
253
+ }
254
+
255
+ registerTextureType<Type extends keyof TextureMap>(
256
+ textureType: Type,
257
+ textureClass: TextureMap[Type],
258
+ ): void {
259
+ this.txConstructors[textureType] = textureClass;
260
+ }
261
+
262
+ private initialize(support: CreateImageBitmapSupport) {
263
+ this.hasCreateImageBitmap =
264
+ support.basic || support.options || support.full;
265
+ this.imageBitmapSupported = support;
266
+
267
+ if (this.hasCreateImageBitmap === false) {
268
+ console.warn(
269
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
270
+ );
271
+ }
272
+
273
+ if (
274
+ this.hasCreateImageBitmap === true &&
275
+ this.hasWorker === true &&
276
+ this.numImageWorkers > 0
277
+ ) {
278
+ this.imageWorkerManager = new ImageWorkerManager(
279
+ this.numImageWorkers,
280
+ support,
281
+ );
282
+ } else {
283
+ console.warn(
284
+ '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
285
+ );
286
+ }
287
+
288
+ this.initialized = true;
289
+ this.emit('initialized');
290
+ }
291
+
292
+ /**
293
+ * Enqueue a texture for uploading to the GPU.
294
+ *
295
+ * @param texture - The texture to upload
296
+ */
297
+ enqueueUploadTexture(texture: Texture): void {
298
+ if (this.uploadTextureQueue.includes(texture) === false) {
299
+ this.uploadTextureQueue.push(texture);
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Create a texture
305
+ *
306
+ * @param textureType - The type of texture to create
307
+ * @param props - The properties to use for the texture
308
+ */
309
+ createTexture<Type extends keyof TextureMap>(
310
+ textureType: Type,
311
+ props: ExtractProps<TextureMap[Type]>,
312
+ ): InstanceType<TextureMap[Type]> {
313
+ let texture: Texture | undefined;
314
+ const TextureClass = this.txConstructors[textureType];
315
+ if (!TextureClass) {
316
+ throw new Error(`Texture type "${textureType}" is not registered`);
317
+ }
318
+
319
+ const cacheKey = TextureClass.makeCacheKey(props as any);
320
+ if (cacheKey && this.keyCache.has(cacheKey)) {
321
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
322
+ texture = this.keyCache.get(cacheKey)!;
323
+ } else {
324
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
325
+ texture = new TextureClass(this, props as any);
326
+
327
+ if (cacheKey) {
328
+ this.initTextureToCache(texture, cacheKey);
329
+ }
330
+ }
331
+
332
+ return texture as InstanceType<TextureMap[Type]>;
333
+ }
334
+
335
+ orphanTexture(texture: Texture): void {
336
+ // if it is part of the download or upload queue, remove it
337
+ this.removeTextureFromQueue(texture);
338
+
339
+ if (texture.type === TextureType.subTexture) {
340
+ // ignore subtextures
341
+ return;
342
+ }
343
+
344
+ this.stage.txMemManager.addToOrphanedTextures(texture);
345
+ }
346
+
347
+ /**
348
+ * Override loadTexture to use the batched approach.
349
+ *
350
+ * @param texture - The texture to load
351
+ * @param immediate - Whether to prioritize the texture for immediate loading
352
+ */
353
+ loadTexture(texture: Texture, priority?: boolean): void {
354
+ this.stage.txMemManager.removeFromOrphanedTextures(texture);
355
+
356
+ if (texture.type === TextureType.subTexture) {
357
+ // ignore subtextures - they get loaded through their parent
358
+ return;
359
+ }
360
+
361
+ if (texture.state === 'loaded') {
362
+ // if the texture is already loaded, just return
363
+ return;
364
+ }
365
+
366
+ if (Texture.TRANSITIONAL_TEXTURE_STATES.includes(texture.state)) {
367
+ return;
368
+ }
369
+
370
+ // if we're not initialized, just queue the texture into the priority queue
371
+ if (this.initialized === false) {
372
+ this.priorityQueue.push(texture);
373
+ return;
374
+ }
375
+
376
+ // If the texture failed to load, we need to re-download it.
377
+ if (texture.state === 'failed') {
378
+ texture.free();
379
+ texture.freeTextureData();
380
+ }
381
+
382
+ texture.setState('loading');
383
+
384
+ // Get the texture data
385
+ texture
386
+ .getTextureData()
387
+ .then(() => {
388
+ if (texture.state !== 'fetched') {
389
+ texture.setState('failed');
390
+ return;
391
+ }
392
+
393
+ // For non-image textures, upload immediately
394
+ if (texture.type !== TextureType.image) {
395
+ this.uploadTexture(texture).catch((err) => {
396
+ console.error('Failed to upload non-image texture:', err);
397
+ texture.setState('failed');
398
+ });
399
+ } else {
400
+ // For image textures, queue for throttled upload
401
+ // If it's a priority texture, upload it immediately
402
+ if (priority === true) {
403
+ this.uploadTexture(texture).catch((err) => {
404
+ console.error('Failed to upload priority texture:', err);
405
+ texture.setState('failed');
406
+ });
407
+ } else {
408
+ this.enqueueUploadTexture(texture);
409
+ }
410
+ }
411
+ })
412
+ .catch((err) => {
413
+ console.error(err);
414
+ texture.setState('failed');
415
+ });
416
+ }
417
+
418
+ /**
419
+ * Upload a texture to the GPU
420
+ *
421
+ * @param texture Texture to upload
422
+ * @returns Promise that resolves when the texture is fully loaded
423
+ */
424
+ async uploadTexture(texture: Texture): Promise<void> {
425
+ if (
426
+ this.stage.txMemManager.doNotExceedCriticalThreshold === true &&
427
+ this.stage.txMemManager.criticalCleanupRequested === true
428
+ ) {
429
+ // we're at a critical memory threshold, don't upload textures
430
+ texture.setState('failed');
431
+ return;
432
+ }
433
+
434
+ const coreContext = texture.loadCtxTexture();
435
+ if (coreContext !== null && coreContext.state === 'loaded') {
436
+ texture.setState('loaded');
437
+ return;
438
+ }
439
+
440
+ await coreContext.load();
441
+ }
442
+
443
+ /**
444
+ * Check if a texture is being processed
445
+ */
446
+ isProcessingTexture(texture: Texture): boolean {
447
+ return this.uploadTextureQueue.includes(texture) === true;
448
+ }
449
+
450
+ /**
451
+ * Process a limited number of uploads.
452
+ *
453
+ * @param maxProcessingTime - The maximum processing time in milliseconds
454
+ */
455
+ async processSome(maxProcessingTime: number): Promise<void> {
456
+ if (this.initialized === false) {
457
+ return;
458
+ }
459
+
460
+ const platform = this.platform;
461
+ const startTime = platform.getTimeStamp();
462
+
463
+ // Process priority queue
464
+ while (
465
+ this.priorityQueue.length > 0 &&
466
+ platform.getTimeStamp() - startTime < maxProcessingTime
467
+ ) {
468
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
469
+ const texture = this.priorityQueue.pop()!;
470
+ try {
471
+ await texture.getTextureData();
472
+ await this.uploadTexture(texture);
473
+ } catch (error) {
474
+ console.error('Failed to process priority texture:', error);
475
+ // Continue with next texture instead of stopping entire queue
476
+ }
477
+ }
478
+
479
+ // Process uploads - await each upload to prevent GPU overload
480
+ while (
481
+ this.uploadTextureQueue.length > 0 &&
482
+ platform.getTimeStamp() - startTime < maxProcessingTime
483
+ ) {
484
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
485
+ const texture = this.uploadTextureQueue.shift()!;
486
+ try {
487
+ await this.uploadTexture(texture);
488
+ } catch (error) {
489
+ console.error('Failed to upload texture:', error);
490
+ // Continue with next texture instead of stopping entire queue
491
+ }
492
+ }
493
+ }
494
+
495
+ public hasUpdates(): boolean {
496
+ return this.uploadTextureQueue.length > 0;
497
+ }
498
+
499
+ /**
500
+ * Initialize a texture to the cache
501
+ *
502
+ * @param texture Texture to cache
503
+ * @param cacheKey Cache key for the texture
504
+ */
505
+ initTextureToCache(texture: Texture, cacheKey: string) {
506
+ const { keyCache, inverseKeyCache } = this;
507
+ keyCache.set(cacheKey, texture);
508
+ inverseKeyCache.set(texture, cacheKey);
509
+ }
510
+
511
+ /**
512
+ * Get a texture from the cache
513
+ *
514
+ * @param cacheKey
515
+ */
516
+ getTextureFromCache(cacheKey: string): Texture | undefined {
517
+ return this.keyCache.get(cacheKey);
518
+ }
519
+
520
+ /**
521
+ * Remove a texture from the cache
522
+ *
523
+ * @remarks
524
+ * Called by Texture Cleanup when a texture is freed.
525
+ *
526
+ * @param texture
527
+ */
528
+ removeTextureFromCache(texture: Texture) {
529
+ const { inverseKeyCache, keyCache } = this;
530
+ const cacheKey = inverseKeyCache.get(texture);
531
+ if (cacheKey) {
532
+ keyCache.delete(cacheKey);
533
+ }
534
+ }
535
+
536
+ /**
537
+ * Remove texture from the upload queue
538
+ *
539
+ * @param texture - The texture to remove
540
+ */
541
+ removeTextureFromQueue(texture: Texture): void {
542
+ const uploadIndex = this.uploadTextureQueue.indexOf(texture);
543
+ if (uploadIndex !== -1) {
544
+ this.uploadTextureQueue.splice(uploadIndex, 1);
545
+ }
546
+ }
547
+
548
+ /**
549
+ * Resolve a parent texture from the cache or fallback to the provided texture.
550
+ *
551
+ * @param texture - The provided texture to resolve.
552
+ * @returns The cached or provided texture.
553
+ */
554
+ resolveParentTexture(texture: ImageTexture): Texture {
555
+ if (!texture?.props) {
556
+ return texture;
557
+ }
558
+
559
+ const cacheKey = ImageTexture.makeCacheKey(texture.props);
560
+ const cachedTexture = cacheKey
561
+ ? this.getTextureFromCache(cacheKey)
562
+ : undefined;
563
+ return cachedTexture ?? texture;
564
+ }
565
+ }