@lightningjs/renderer 2.12.1 → 2.13.0

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 (329) 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 +10 -0
  6. package/dist/src/core/CoreNode.js +90 -14
  7. package/dist/src/core/CoreNode.js.map +1 -1
  8. package/dist/src/core/CoreTextureManager.d.ts +13 -7
  9. package/dist/src/core/CoreTextureManager.js +92 -105
  10. package/dist/src/core/CoreTextureManager.js.map +1 -1
  11. package/dist/src/core/Stage.d.ts +4 -2
  12. package/dist/src/core/Stage.js +23 -7
  13. package/dist/src/core/Stage.js.map +1 -1
  14. package/dist/src/core/TextureMemoryManager.d.ts +16 -6
  15. package/dist/src/core/TextureMemoryManager.js +75 -17
  16. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  17. package/dist/src/core/lib/ImageWorker.d.ts +1 -1
  18. package/dist/src/core/lib/ImageWorker.js +13 -11
  19. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  20. package/dist/src/core/lib/validateImageBitmap.d.ts +6 -0
  21. package/dist/src/core/lib/validateImageBitmap.js +68 -0
  22. package/dist/src/core/lib/validateImageBitmap.js.map +1 -0
  23. package/dist/src/core/platform.js +3 -3
  24. package/dist/src/core/platform.js.map +1 -1
  25. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +7 -12
  26. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  27. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  28. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  29. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  30. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  31. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  32. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  33. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  34. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  35. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  36. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  37. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  38. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  39. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  40. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  41. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  42. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  43. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  44. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  45. package/dist/src/core/textures/ImageTexture.js +10 -16
  46. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  47. package/dist/src/main-api/Inspector.js +16 -2
  48. package/dist/src/main-api/Inspector.js.map +1 -1
  49. package/dist/src/main-api/Renderer.d.ts +27 -1
  50. package/dist/src/main-api/Renderer.js +8 -3
  51. package/dist/src/main-api/Renderer.js.map +1 -1
  52. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  53. package/exports/canvas.ts +39 -39
  54. package/exports/index.ts +89 -89
  55. package/exports/inspector.ts +24 -24
  56. package/exports/utils.ts +44 -44
  57. package/exports/webgl.ts +38 -38
  58. package/package.json +1 -2
  59. package/scripts/please-use-pnpm.js +13 -13
  60. package/src/common/CommonTypes.ts +146 -146
  61. package/src/common/EventEmitter.ts +77 -77
  62. package/src/common/IAnimationController.ts +92 -92
  63. package/src/common/IEventEmitter.ts +28 -28
  64. package/src/core/CoreNode.test.ts +202 -199
  65. package/src/core/CoreNode.ts +2455 -2341
  66. package/src/core/CoreShaderManager.ts +292 -292
  67. package/src/core/CoreTextNode.ts +455 -455
  68. package/src/core/CoreTextureManager.ts +597 -596
  69. package/src/core/Stage.ts +743 -722
  70. package/src/core/TextureMemoryManager.ts +395 -314
  71. package/src/core/animations/AnimationManager.ts +38 -38
  72. package/src/core/animations/CoreAnimation.ts +340 -340
  73. package/src/core/animations/CoreAnimationController.ts +157 -157
  74. package/src/core/lib/ContextSpy.ts +41 -41
  75. package/src/core/lib/ImageWorker.ts +279 -271
  76. package/src/core/lib/Matrix3d.ts +244 -244
  77. package/src/core/lib/RenderCoords.ts +86 -86
  78. package/src/core/lib/WebGlContextWrapper.ts +1332 -1332
  79. package/src/core/lib/textureCompression.ts +152 -152
  80. package/src/core/lib/textureSvg.ts +78 -78
  81. package/src/core/lib/utils.ts +310 -310
  82. package/src/core/lib/validateImageBitmap.ts +76 -0
  83. package/src/core/platform.ts +63 -61
  84. package/src/core/renderers/CoreContextTexture.ts +43 -43
  85. package/src/core/renderers/CoreRenderOp.ts +22 -22
  86. package/src/core/renderers/CoreRenderer.ts +115 -115
  87. package/src/core/renderers/CoreShader.ts +41 -41
  88. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +375 -375
  89. package/src/core/renderers/canvas/CanvasCoreTexture.ts +153 -153
  90. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  91. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  92. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  93. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +86 -86
  94. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  95. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +301 -303
  96. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  97. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +815 -815
  98. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  99. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  100. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  101. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  102. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  103. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  104. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  105. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  106. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  107. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  108. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  109. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  110. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  111. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  112. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  113. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  114. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  115. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  116. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  117. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  118. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  119. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  120. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  121. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  122. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  123. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  124. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  125. package/src/core/text-rendering/TrFontManager.ts +183 -183
  126. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  127. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  128. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  129. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  130. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  131. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  132. package/src/core/text-rendering/font-face-types/utils.ts +39 -39
  133. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  134. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +808 -808
  135. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  136. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  137. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  138. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  139. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  140. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  141. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  142. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  143. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +408 -408
  144. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  145. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  146. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  147. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  148. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  149. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  150. package/src/core/textures/ColorTexture.ts +102 -102
  151. package/src/core/textures/ImageTexture.ts +376 -382
  152. package/src/core/textures/NoiseTexture.ts +104 -104
  153. package/src/core/textures/RenderTexture.ts +85 -85
  154. package/src/core/textures/SubTexture.ts +205 -205
  155. package/src/core/textures/Texture.ts +337 -337
  156. package/src/core/utils.ts +227 -227
  157. package/src/env.d.ts +7 -7
  158. package/src/main-api/DynamicShaderController.ts +104 -104
  159. package/src/main-api/INode.ts +101 -101
  160. package/src/main-api/Inspector.ts +522 -505
  161. package/src/main-api/Renderer.ts +751 -720
  162. package/src/main-api/ShaderController.ts +80 -80
  163. package/src/main-api/utils.ts +45 -45
  164. package/src/utils.ts +248 -248
  165. package/dist/exports/core-api.d.ts +0 -74
  166. package/dist/exports/core-api.js +0 -96
  167. package/dist/exports/core-api.js.map +0 -1
  168. package/dist/exports/main-api.d.ts +0 -30
  169. package/dist/exports/main-api.js +0 -45
  170. package/dist/exports/main-api.js.map +0 -1
  171. package/dist/src/core/CoreExtension.d.ts +0 -12
  172. package/dist/src/core/CoreExtension.js +0 -29
  173. package/dist/src/core/CoreExtension.js.map +0 -1
  174. package/dist/src/core/CoreStuff.d.ts +0 -1
  175. package/dist/src/core/CoreStuff.js +0 -138
  176. package/dist/src/core/CoreStuff.js.map +0 -1
  177. package/dist/src/core/CoreTexturizer.d.ts +0 -14
  178. package/dist/src/core/CoreTexturizer.js +0 -47
  179. package/dist/src/core/CoreTexturizer.js.map +0 -1
  180. package/dist/src/core/LngNode.d.ts +0 -736
  181. package/dist/src/core/LngNode.js +0 -1174
  182. package/dist/src/core/LngNode.js.map +0 -1
  183. package/dist/src/core/Matrix2DContext.d.ts +0 -15
  184. package/dist/src/core/Matrix2DContext.js +0 -45
  185. package/dist/src/core/Matrix2DContext.js.map +0 -1
  186. package/dist/src/core/ShaderNode.d.ts +0 -10
  187. package/dist/src/core/ShaderNode.js +0 -30
  188. package/dist/src/core/ShaderNode.js.map +0 -1
  189. package/dist/src/core/TextNode.d.ts +0 -103
  190. package/dist/src/core/TextNode.js +0 -331
  191. package/dist/src/core/TextNode.js.map +0 -1
  192. package/dist/src/core/lib/Coords.d.ts +0 -14
  193. package/dist/src/core/lib/Coords.js +0 -55
  194. package/dist/src/core/lib/Coords.js.map +0 -1
  195. package/dist/src/core/lib/glm/common.d.ts +0 -162
  196. package/dist/src/core/lib/glm/common.js +0 -81
  197. package/dist/src/core/lib/glm/common.js.map +0 -1
  198. package/dist/src/core/lib/glm/index.d.ts +0 -11
  199. package/dist/src/core/lib/glm/index.js +0 -30
  200. package/dist/src/core/lib/glm/index.js.map +0 -1
  201. package/dist/src/core/lib/glm/mat2.d.ts +0 -219
  202. package/dist/src/core/lib/glm/mat2.js +0 -396
  203. package/dist/src/core/lib/glm/mat2.js.map +0 -1
  204. package/dist/src/core/lib/glm/mat2d.d.ts +0 -237
  205. package/dist/src/core/lib/glm/mat2d.js +0 -442
  206. package/dist/src/core/lib/glm/mat2d.js.map +0 -1
  207. package/dist/src/core/lib/glm/mat3.d.ts +0 -283
  208. package/dist/src/core/lib/glm/mat3.js +0 -680
  209. package/dist/src/core/lib/glm/mat3.js.map +0 -1
  210. package/dist/src/core/lib/glm/mat4.d.ts +0 -550
  211. package/dist/src/core/lib/glm/mat4.js +0 -1802
  212. package/dist/src/core/lib/glm/mat4.js.map +0 -1
  213. package/dist/src/core/lib/glm/quat.d.ts +0 -363
  214. package/dist/src/core/lib/glm/quat.js +0 -693
  215. package/dist/src/core/lib/glm/quat.js.map +0 -1
  216. package/dist/src/core/lib/glm/quat2.d.ts +0 -356
  217. package/dist/src/core/lib/glm/quat2.js +0 -754
  218. package/dist/src/core/lib/glm/quat2.js.map +0 -1
  219. package/dist/src/core/lib/glm/vec2.d.ts +0 -365
  220. package/dist/src/core/lib/glm/vec2.js +0 -569
  221. package/dist/src/core/lib/glm/vec2.js.map +0 -1
  222. package/dist/src/core/lib/glm/vec3.d.ts +0 -406
  223. package/dist/src/core/lib/glm/vec3.js +0 -720
  224. package/dist/src/core/lib/glm/vec3.js.map +0 -1
  225. package/dist/src/core/lib/glm/vec4.d.ts +0 -330
  226. package/dist/src/core/lib/glm/vec4.js +0 -608
  227. package/dist/src/core/lib/glm/vec4.js.map +0 -1
  228. package/dist/src/core/renderers/CoreShaderManager.d.ts +0 -19
  229. package/dist/src/core/renderers/CoreShaderManager.js +0 -33
  230. package/dist/src/core/renderers/CoreShaderManager.js.map +0 -1
  231. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.d.ts +0 -27
  232. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.js +0 -82
  233. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.js.map +0 -1
  234. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.d.ts +0 -11
  235. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.js +0 -34
  236. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.js.map +0 -1
  237. package/dist/src/core/scene/Scene.d.ts +0 -59
  238. package/dist/src/core/scene/Scene.js +0 -106
  239. package/dist/src/core/scene/Scene.js.map +0 -1
  240. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +0 -20
  241. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +0 -55
  242. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +0 -1
  243. package/dist/src/main-api/ICoreDriver.d.ts +0 -27
  244. package/dist/src/main-api/ICoreDriver.js +0 -20
  245. package/dist/src/main-api/ICoreDriver.js.map +0 -1
  246. package/dist/src/main-api/IRenderDriver.d.ts +0 -20
  247. package/dist/src/main-api/IRenderDriver.js +0 -20
  248. package/dist/src/main-api/IRenderDriver.js.map +0 -1
  249. package/dist/src/main-api/IShaderController.d.ts +0 -14
  250. package/dist/src/main-api/IShaderController.js +0 -30
  251. package/dist/src/main-api/IShaderController.js.map +0 -1
  252. package/dist/src/main-api/IShaderNode.d.ts +0 -17
  253. package/dist/src/main-api/IShaderNode.js +0 -19
  254. package/dist/src/main-api/IShaderNode.js.map +0 -1
  255. package/dist/src/main-api/RendererMain.d.ts +0 -375
  256. package/dist/src/main-api/RendererMain.js +0 -365
  257. package/dist/src/main-api/RendererMain.js.map +0 -1
  258. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +0 -9
  259. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +0 -38
  260. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +0 -1
  261. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +0 -56
  262. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +0 -101
  263. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +0 -1
  264. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +0 -32
  265. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js +0 -28
  266. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +0 -1
  267. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +0 -24
  268. package/dist/src/render-drivers/main/MainCoreDriver.js +0 -118
  269. package/dist/src/render-drivers/main/MainCoreDriver.js.map +0 -1
  270. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +0 -99
  271. package/dist/src/render-drivers/main/MainOnlyNode.js +0 -396
  272. package/dist/src/render-drivers/main/MainOnlyNode.js.map +0 -1
  273. package/dist/src/render-drivers/main/MainOnlyShaderController.d.ts +0 -6
  274. package/dist/src/render-drivers/main/MainOnlyShaderController.js +0 -15
  275. package/dist/src/render-drivers/main/MainOnlyShaderController.js.map +0 -1
  276. package/dist/src/render-drivers/main/MainOnlyShaderNode.d.ts +0 -7
  277. package/dist/src/render-drivers/main/MainOnlyShaderNode.js +0 -34
  278. package/dist/src/render-drivers/main/MainOnlyShaderNode.js.map +0 -1
  279. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +0 -47
  280. package/dist/src/render-drivers/main/MainOnlyTextNode.js +0 -205
  281. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +0 -1
  282. package/dist/src/render-drivers/main/MainRenderDriver.d.ts +0 -17
  283. package/dist/src/render-drivers/main/MainRenderDriver.js +0 -88
  284. package/dist/src/render-drivers/main/MainRenderDriver.js.map +0 -1
  285. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +0 -90
  286. package/dist/src/render-drivers/threadx/NodeStruct.js +0 -281
  287. package/dist/src/render-drivers/threadx/NodeStruct.js.map +0 -1
  288. package/dist/src/render-drivers/threadx/SharedNode.d.ts +0 -39
  289. package/dist/src/render-drivers/threadx/SharedNode.js +0 -60
  290. package/dist/src/render-drivers/threadx/SharedNode.js.map +0 -1
  291. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +0 -44
  292. package/dist/src/render-drivers/threadx/TextNodeStruct.js +0 -201
  293. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +0 -1
  294. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +0 -28
  295. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +0 -234
  296. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +0 -1
  297. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +0 -20
  298. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +0 -84
  299. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +0 -1
  300. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +0 -44
  301. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +0 -154
  302. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +0 -1
  303. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.d.ts +0 -6
  304. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.js +0 -16
  305. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.js.map +0 -1
  306. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.d.ts +0 -7
  307. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.js +0 -15
  308. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.js.map +0 -1
  309. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +0 -28
  310. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +0 -55
  311. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +0 -1
  312. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.d.ts +0 -21
  313. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js +0 -198
  314. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js.map +0 -1
  315. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +0 -70
  316. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +0 -32
  317. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +0 -1
  318. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +0 -19
  319. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +0 -177
  320. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +0 -1
  321. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +0 -27
  322. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +0 -108
  323. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +0 -1
  324. package/dist/src/render-drivers/threadx/worker/renderer.d.ts +0 -1
  325. package/dist/src/render-drivers/threadx/worker/renderer.js +0 -145
  326. package/dist/src/render-drivers/threadx/worker/renderer.js.map +0 -1
  327. package/dist/src/render-drivers/utils.d.ts +0 -12
  328. package/dist/src/render-drivers/utils.js +0 -69
  329. package/dist/src/render-drivers/utils.js.map +0 -1
@@ -1,596 +1,597 @@
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 { TextureType, type Texture } from './textures/Texture.js';
28
- import { EventEmitter } from '../common/EventEmitter.js';
29
- import { getTimeStamp } from './platform.js';
30
- import type { Stage } from './Stage.js';
31
-
32
- /**
33
- * Augmentable map of texture class types
34
- *
35
- * @remarks
36
- * This interface can be augmented by other modules/apps to add additional
37
- * texture types. The ones included directly here are the ones that are
38
- * included in the core library.
39
- */
40
- export interface TextureMap {
41
- ColorTexture: typeof ColorTexture;
42
- ImageTexture: typeof ImageTexture;
43
- NoiseTexture: typeof NoiseTexture;
44
- SubTexture: typeof SubTexture;
45
- RenderTexture: typeof RenderTexture;
46
- }
47
-
48
- export interface CreateImageBitmapSupport {
49
- basic: boolean; // Supports createImageBitmap(image)
50
- options: boolean; // Supports createImageBitmap(image, options)
51
- full: boolean; // Supports createImageBitmap(image, sx, sy, sw, sh, options)
52
- }
53
-
54
- export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
55
- ? Props
56
- : never;
57
-
58
- /**
59
- * Contains information about the texture manager's internal state
60
- * for debugging purposes.
61
- */
62
- export interface TextureManagerDebugInfo {
63
- keyCacheSize: number;
64
- }
65
-
66
- export type ResizeModeOptions =
67
- | {
68
- /**
69
- * Specifies that the image should be resized to cover the specified dimensions.
70
- */
71
- type: 'cover';
72
- /**
73
- * The horizontal clipping position
74
- * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
75
- * clipX 0.5 will clip a equal amount from left and right
76
- *
77
- * @defaultValue 0.5
78
- */
79
- clipX?: number;
80
- /**
81
- * The vertical clipping position
82
- * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
83
- * clipY 0.5 will clip a equal amount from top and bottom
84
- *
85
- * @defaultValue 0.5
86
- */
87
- clipY?: number;
88
- }
89
- | {
90
- /**
91
- * Specifies that the image should be resized to fit within the specified dimensions.
92
- */
93
- type: 'contain';
94
- };
95
-
96
- /**
97
- * Universal options for all texture types
98
- *
99
- * @remarks
100
- * Texture Options provide a way to specify options that are relevant to the
101
- * texture loading process (including caching) and specifically for how a
102
- * texture is rendered within a specific Node (or set of Nodes).
103
- *
104
- * They are not used in determining the cache key for a texture (except if
105
- * the `cacheKey` option is provided explicitly to oveerride the default
106
- * cache key for the texture instance) nor are they stored/referenced within
107
- * the texture instance itself. Instead, the options are stored/referenced
108
- * within individual Nodes. So a single texture instance can be used in
109
- * multiple Nodes each using a different set of options.
110
- */
111
- export interface TextureOptions {
112
- /**
113
- * Preload the texture immediately even if it's not being rendered to the
114
- * screen.
115
- *
116
- * @remarks
117
- * This allows the texture to be used immediately without any delay when it
118
- * is first needed for rendering. Otherwise the loading process will start
119
- * when the texture is first rendered, which may cause a delay in that texture
120
- * being shown properly.
121
- *
122
- * @defaultValue `false`
123
- */
124
- preload?: boolean;
125
-
126
- /**
127
- * Prevent clean up of the texture when it is no longer being used.
128
- *
129
- * @remarks
130
- * This is useful when you want to keep the texture in memory for later use.
131
- * Regardless of whether the texture is being used or not, it will not be
132
- * cleaned up.
133
- *
134
- * @defaultValue `false`
135
- */
136
- preventCleanup?: boolean;
137
-
138
- /**
139
- * Flip the texture horizontally when rendering
140
- *
141
- * @defaultValue `false`
142
- */
143
- flipX?: boolean;
144
-
145
- /**
146
- * Flip the texture vertically when rendering
147
- *
148
- * @defaultValue `false`
149
- */
150
- flipY?: boolean;
151
-
152
- /**
153
- * You can use resizeMode to determine the clipping automatically from the width
154
- * and height of the source texture. This can be convenient if you are unsure about
155
- * the exact image sizes but want the image to cover a specific area.
156
- *
157
- * The resize modes cover and contain are supported
158
- */
159
- resizeMode?: ResizeModeOptions;
160
- }
161
-
162
- export class CoreTextureManager extends EventEmitter {
163
- /**
164
- * Map of textures by cache key
165
- */
166
- keyCache: Map<string, Texture> = new Map();
167
-
168
- /**
169
- * Map of cache keys by texture
170
- */
171
- inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
172
-
173
- /**
174
- * Map of texture constructors by their type name
175
- */
176
- txConstructors: Partial<TextureMap> = {};
177
-
178
- private downloadTextureSourceQueue: Array<Texture> = [];
179
- private priorityQueue: Array<Texture> = [];
180
- private uploadTextureQueue: Array<Texture> = [];
181
- private initialized = false;
182
- private stage: Stage;
183
-
184
- imageWorkerManager: ImageWorkerManager | null = null;
185
- hasCreateImageBitmap = !!self.createImageBitmap;
186
- imageBitmapSupported = {
187
- basic: false,
188
- options: false,
189
- full: false,
190
- };
191
-
192
- hasWorker = !!self.Worker;
193
- /**
194
- * Renderer that this texture manager is associated with
195
- *
196
- * @remarks
197
- * This MUST be set before the texture manager is used. Otherwise errors
198
- * will occur when using the texture manager.
199
- */
200
- renderer!: CoreRenderer;
201
-
202
- /**
203
- * The current frame time in milliseconds
204
- *
205
- * @remarks
206
- * This is used to populate the `lastRenderableChangeTime` property of
207
- * {@link Texture} instances when their renderable state changes.
208
- *
209
- * Set by stage via `updateFrameTime` method.
210
- */
211
- frameTime = 0;
212
-
213
- constructor(stage: Stage, numImageWorkers: number) {
214
- super();
215
- this.stage = stage;
216
- this.validateCreateImageBitmap()
217
- .then((result) => {
218
- this.hasCreateImageBitmap =
219
- result.basic || result.options || result.full;
220
- this.imageBitmapSupported = result;
221
-
222
- if (!this.hasCreateImageBitmap) {
223
- console.warn(
224
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
225
- );
226
- }
227
-
228
- if (
229
- this.hasCreateImageBitmap &&
230
- this.hasWorker &&
231
- numImageWorkers > 0
232
- ) {
233
- this.imageWorkerManager = new ImageWorkerManager(
234
- numImageWorkers,
235
- result,
236
- );
237
- } else {
238
- console.warn(
239
- '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
240
- );
241
- }
242
-
243
- this.initialized = true;
244
- this.emit('initialized');
245
- })
246
- .catch((e) => {
247
- console.warn(
248
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
249
- );
250
-
251
- // initialized without image worker manager and createImageBitmap
252
- this.initialized = true;
253
- this.emit('initialized');
254
- });
255
-
256
- this.registerTextureType('ImageTexture', ImageTexture);
257
- this.registerTextureType('ColorTexture', ColorTexture);
258
- this.registerTextureType('NoiseTexture', NoiseTexture);
259
- this.registerTextureType('SubTexture', SubTexture);
260
- this.registerTextureType('RenderTexture', RenderTexture);
261
- }
262
-
263
- private async validateCreateImageBitmap(): Promise<CreateImageBitmapSupport> {
264
- // Test if createImageBitmap is supported using a simple 1x1 PNG image
265
- // prettier-ignore
266
- const pngBinaryData = new Uint8Array([
267
- 0x89, 0x50, 0x4e, 0x47,
268
- 0x0d, 0x0a, 0x1a, 0x0a, // PNG signature
269
- 0x00, 0x00, 0x00, 0x0d, // IHDR chunk length
270
- 0x49, 0x48, 0x44, 0x52, // "IHDR" chunk type
271
- 0x00, 0x00, 0x00, 0x01, // Width: 1
272
- 0x00, 0x00, 0x00, 0x01, // Height: 1
273
- 0x01, // Bit depth: 1
274
- 0x03, // Color type: Indexed
275
- 0x00, // Compression method: Deflate
276
- 0x00, // Filter method: None
277
- 0x00, // Interlace method: None
278
- 0x25, 0xdb, 0x56, 0xca, // CRC for IHDR
279
- 0x00, 0x00, 0x00, 0x03, // PLTE chunk length
280
- 0x50, 0x4c, 0x54, 0x45, // "PLTE" chunk type
281
- 0x00, 0x00, 0x00, // Palette entry: Black
282
- 0xa7, 0x7a, 0x3d, 0xda, // CRC for PLTE
283
- 0x00, 0x00, 0x00, 0x01, // tRNS chunk length
284
- 0x74, 0x52, 0x4e, 0x53, // "tRNS" chunk type
285
- 0x00, // Transparency for black: Fully transparent
286
- 0x40, 0xe6, 0xd8, 0x66, // CRC for tRNS
287
- 0x00, 0x00, 0x00, 0x0a, // IDAT chunk length
288
- 0x49, 0x44, 0x41, 0x54, // "IDAT" chunk type
289
- 0x08, 0xd7, // Deflate header
290
- 0x63, 0x60, 0x00, 0x00,
291
- 0x00, 0x02, 0x00, 0x01, // Zlib-compressed data
292
- 0xe2, 0x21, 0xbc, 0x33, // CRC for IDAT
293
- 0x00, 0x00, 0x00, 0x00, // IEND chunk length
294
- 0x49, 0x45, 0x4e, 0x44, // "IEND" chunk type
295
- 0xae, 0x42, 0x60, 0x82, // CRC for IEND
296
- ]);
297
-
298
- const support: CreateImageBitmapSupport = {
299
- basic: false,
300
- options: false,
301
- full: false,
302
- };
303
-
304
- // Test basic createImageBitmap support
305
- const blob = new Blob([pngBinaryData], { type: 'image/png' });
306
- const bitmap = await createImageBitmap(blob);
307
- bitmap.close?.();
308
- support.basic = true;
309
-
310
- // Test createImageBitmap with options support
311
- try {
312
- const options = { premultiplyAlpha: 'none' as const };
313
- const bitmapWithOptions = await createImageBitmap(blob, options);
314
- bitmapWithOptions.close?.();
315
- support.options = true;
316
- } catch (e) {
317
- /* ignore */
318
- }
319
-
320
- // Test createImageBitmap with full options support
321
- try {
322
- const bitmapWithFullOptions = await createImageBitmap(blob, 0, 0, 1, 1, {
323
- premultiplyAlpha: 'none',
324
- });
325
- bitmapWithFullOptions.close?.();
326
- support.full = true;
327
- } catch (e) {
328
- /* ignore */
329
- }
330
-
331
- return support;
332
- }
333
-
334
- registerTextureType<Type extends keyof TextureMap>(
335
- textureType: Type,
336
- textureClass: TextureMap[Type],
337
- ): void {
338
- this.txConstructors[textureType] = textureClass;
339
- }
340
-
341
- /**
342
- * Enqueue a texture for downloading its source image.
343
- */
344
- enqueueDownloadTextureSource(texture: Texture): void {
345
- if (!this.downloadTextureSourceQueue.includes(texture)) {
346
- this.downloadTextureSourceQueue.push(texture);
347
- }
348
- }
349
-
350
- /**
351
- * Enqueue a texture for uploading to the GPU.
352
- *
353
- * @param texture - The texture to upload
354
- */
355
- enqueueUploadTexture(texture: Texture): void {
356
- if (this.uploadTextureQueue.includes(texture) === false) {
357
- this.uploadTextureQueue.push(texture);
358
- }
359
- }
360
-
361
- /**
362
- * Create a texture
363
- *
364
- * @param textureType - The type of texture to create
365
- * @param props - The properties to use for the texture
366
- */
367
- createTexture<Type extends keyof TextureMap>(
368
- textureType: Type,
369
- props: ExtractProps<TextureMap[Type]>,
370
- ): InstanceType<TextureMap[Type]> {
371
- let texture: Texture | undefined;
372
- const TextureClass = this.txConstructors[textureType];
373
- if (!TextureClass) {
374
- throw new Error(`Texture type "${textureType}" is not registered`);
375
- }
376
-
377
- const cacheKey = TextureClass.makeCacheKey(props as any);
378
- if (cacheKey && this.keyCache.has(cacheKey)) {
379
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
380
- texture = this.keyCache.get(cacheKey)!;
381
- } else {
382
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
383
- texture = new TextureClass(this, props as any);
384
-
385
- if (cacheKey) {
386
- this.initTextureToCache(texture, cacheKey);
387
- }
388
- }
389
-
390
- return texture as InstanceType<TextureMap[Type]>;
391
- }
392
-
393
- orphanTexture(texture: Texture): void {
394
- // if it is part of the download or upload queue, remove it
395
- const downloadIndex = this.downloadTextureSourceQueue.indexOf(texture);
396
- if (downloadIndex !== -1) {
397
- this.downloadTextureSourceQueue.splice(downloadIndex, 1);
398
- }
399
-
400
- const uploadIndex = this.uploadTextureQueue.indexOf(texture);
401
- if (uploadIndex !== -1) {
402
- this.uploadTextureQueue.splice(uploadIndex, 1);
403
- }
404
-
405
- if (texture.type === TextureType.subTexture) {
406
- // ignore subtextures
407
- return;
408
- }
409
-
410
- this.stage.txMemManager.addToOrphanedTextures(texture);
411
- }
412
-
413
- /**
414
- * Override loadTexture to use the batched approach.
415
- *
416
- * @param texture - The texture to load
417
- * @param immediate - Whether to prioritize the texture for immediate loading
418
- */
419
- loadTexture(texture: Texture, priority?: boolean): void {
420
- this.stage.txMemManager.removeFromOrphanedTextures(texture);
421
-
422
- if (texture.state !== 'initial' && texture.state !== 'freed') {
423
- return;
424
- }
425
-
426
- texture.setState('loading');
427
-
428
- // if we're not initialized, just queue the texture into the priority queue
429
- if (this.initialized === false) {
430
- this.priorityQueue.push(texture);
431
- return;
432
- }
433
-
434
- // these types of textures don't need to be downloaded
435
- // Technically the noise texture shouldn't either, but it's a special case
436
- // and not really used in production so who cares ¯\_(ツ)_/¯
437
- if (
438
- texture.type === TextureType.subTexture ||
439
- texture.type === TextureType.color ||
440
- texture.type === TextureType.renderToTexture
441
- ) {
442
- texture.setState('fetched');
443
- this.enqueueUploadTexture(texture);
444
- return;
445
- }
446
-
447
- // prioritize the texture for immediate loading
448
- if (priority === true) {
449
- texture
450
- .getTextureData()
451
- .then(() => {
452
- this.uploadTexture(texture);
453
- })
454
- .catch((err) => {
455
- console.error(err);
456
- });
457
- }
458
-
459
- // enqueue the texture for download and upload
460
- this.enqueueDownloadTextureSource(texture);
461
- }
462
-
463
- /**
464
- * Upload a texture to the GPU
465
- *
466
- * @param texture Texture to upload
467
- */
468
- uploadTexture(texture: Texture): void {
469
- if (texture.state !== 'fetched') {
470
- return;
471
- }
472
-
473
- const coreContext = texture.loadCtxTexture();
474
- coreContext.load();
475
- }
476
-
477
- /**
478
- * Check if a texture is being processed
479
- */
480
- isProcessingTexture(texture: Texture): boolean {
481
- return (
482
- this.downloadTextureSourceQueue.includes(texture) === true ||
483
- this.uploadTextureQueue.includes(texture) === true
484
- );
485
- }
486
-
487
- /**
488
- * Process a limited number of downloads and uploads.
489
- *
490
- * @param maxItems - The maximum number of items to process
491
- */
492
- processSome(maxProcessingTime: number): void {
493
- if (this.initialized === false) {
494
- return;
495
- }
496
-
497
- const startTime = getTimeStamp();
498
-
499
- // Process priority queue
500
- while (
501
- this.priorityQueue.length > 0 &&
502
- getTimeStamp() - startTime < maxProcessingTime
503
- ) {
504
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
505
- const texture = this.priorityQueue.pop()!;
506
- texture.getTextureData().then(() => {
507
- this.uploadTexture(texture);
508
- });
509
- }
510
-
511
- // Process uploads
512
- while (
513
- this.uploadTextureQueue.length > 0 &&
514
- getTimeStamp() - startTime < maxProcessingTime
515
- ) {
516
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
517
- this.uploadTexture(this.uploadTextureQueue.pop()!);
518
- }
519
-
520
- // Process downloads
521
- while (
522
- this.downloadTextureSourceQueue.length > 0 &&
523
- getTimeStamp() - startTime < maxProcessingTime
524
- ) {
525
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
526
- const texture = this.downloadTextureSourceQueue.shift()!;
527
- texture.getTextureData().then(() => {
528
- if (texture.state === 'fetched') {
529
- this.enqueueUploadTexture(texture);
530
- }
531
- });
532
- }
533
- }
534
-
535
- public hasUpdates(): boolean {
536
- return (
537
- this.downloadTextureSourceQueue.length > 0 ||
538
- this.uploadTextureQueue.length > 0
539
- );
540
- }
541
-
542
- /**
543
- * Initialize a texture to the cache
544
- *
545
- * @param texture Texture to cache
546
- * @param cacheKey Cache key for the texture
547
- */
548
- initTextureToCache(texture: Texture, cacheKey: string) {
549
- const { keyCache, inverseKeyCache } = this;
550
- keyCache.set(cacheKey, texture);
551
- inverseKeyCache.set(texture, cacheKey);
552
- }
553
-
554
- /**
555
- * Get a texture from the cache
556
- *
557
- * @param cacheKey
558
- */
559
- getTextureFromCache(cacheKey: string): Texture | undefined {
560
- return this.keyCache.get(cacheKey);
561
- }
562
-
563
- /**
564
- * Remove a texture from the cache
565
- *
566
- * @remarks
567
- * Called by Texture Cleanup when a texture is freed.
568
- *
569
- * @param texture
570
- */
571
- removeTextureFromCache(texture: Texture) {
572
- const { inverseKeyCache, keyCache } = this;
573
- const cacheKey = inverseKeyCache.get(texture);
574
- if (cacheKey) {
575
- keyCache.delete(cacheKey);
576
- }
577
- }
578
-
579
- /**
580
- * Resolve a parent texture from the cache or fallback to the provided texture.
581
- *
582
- * @param texture - The provided texture to resolve.
583
- * @returns The cached or provided texture.
584
- */
585
- resolveParentTexture(texture: ImageTexture): Texture {
586
- if (!texture?.props) {
587
- return texture;
588
- }
589
-
590
- const cacheKey = ImageTexture.makeCacheKey(texture.props);
591
- const cachedTexture = cacheKey
592
- ? this.getTextureFromCache(cacheKey)
593
- : undefined;
594
- return cachedTexture ?? texture;
595
- }
596
- }
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 { TextureType, type Texture } from './textures/Texture.js';
28
+ import { EventEmitter } from '../common/EventEmitter.js';
29
+ import { getTimeStamp } from './platform.js';
30
+ import type { Stage } from './Stage.js';
31
+ import {
32
+ validateCreateImageBitmap,
33
+ type CreateImageBitmapSupport,
34
+ } from './lib/validateImageBitmap.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 downloadTextureSourceQueue: Array<Texture> = [];
182
+ private priorityQueue: Array<Texture> = [];
183
+ private uploadTextureQueue: Array<Texture> = [];
184
+ private initialized = false;
185
+ private stage: Stage;
186
+ private numImageWorkers: number;
187
+
188
+ imageWorkerManager: ImageWorkerManager | null = null;
189
+ hasCreateImageBitmap = !!self.createImageBitmap;
190
+ imageBitmapSupported = {
191
+ basic: false,
192
+ options: false,
193
+ full: false,
194
+ };
195
+
196
+ hasWorker = !!self.Worker;
197
+ /**
198
+ * Renderer that this texture manager is associated with
199
+ *
200
+ * @remarks
201
+ * This MUST be set before the texture manager is used. Otherwise errors
202
+ * will occur when using the texture manager.
203
+ */
204
+ renderer!: CoreRenderer;
205
+
206
+ /**
207
+ * The current frame time in milliseconds
208
+ *
209
+ * @remarks
210
+ * This is used to populate the `lastRenderableChangeTime` property of
211
+ * {@link Texture} instances when their renderable state changes.
212
+ *
213
+ * Set by stage via `updateFrameTime` method.
214
+ */
215
+ frameTime = 0;
216
+
217
+ constructor(stage: Stage, settings: TextureManagerSettings) {
218
+ super();
219
+
220
+ const { numImageWorkers, createImageBitmapSupport } = settings;
221
+ this.stage = stage;
222
+ this.numImageWorkers = numImageWorkers;
223
+
224
+ if (createImageBitmapSupport === 'auto') {
225
+ validateCreateImageBitmap()
226
+ .then((result) => {
227
+ this.initialize(result);
228
+ })
229
+ .catch((e) => {
230
+ console.warn(
231
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
232
+ );
233
+
234
+ // initialized without image worker manager and createImageBitmap
235
+ this.initialized = true;
236
+ this.emit('initialized');
237
+ });
238
+ } else {
239
+ this.initialize({
240
+ basic: createImageBitmapSupport === 'basic',
241
+ options: createImageBitmapSupport === 'options',
242
+ full: createImageBitmapSupport === 'full',
243
+ });
244
+ }
245
+
246
+ this.registerTextureType('ImageTexture', ImageTexture);
247
+ this.registerTextureType('ColorTexture', ColorTexture);
248
+ this.registerTextureType('NoiseTexture', NoiseTexture);
249
+ this.registerTextureType('SubTexture', SubTexture);
250
+ this.registerTextureType('RenderTexture', RenderTexture);
251
+ }
252
+
253
+ registerTextureType<Type extends keyof TextureMap>(
254
+ textureType: Type,
255
+ textureClass: TextureMap[Type],
256
+ ): void {
257
+ this.txConstructors[textureType] = textureClass;
258
+ }
259
+
260
+ private initialize(support: CreateImageBitmapSupport) {
261
+ this.hasCreateImageBitmap =
262
+ support.basic || support.options || support.full;
263
+ this.imageBitmapSupported = support;
264
+
265
+ if (!this.hasCreateImageBitmap) {
266
+ console.warn(
267
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
268
+ );
269
+ }
270
+
271
+ if (
272
+ this.hasCreateImageBitmap &&
273
+ this.hasWorker &&
274
+ this.numImageWorkers > 0
275
+ ) {
276
+ this.imageWorkerManager = new ImageWorkerManager(
277
+ this.numImageWorkers,
278
+ support,
279
+ );
280
+ } else {
281
+ console.warn(
282
+ '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
283
+ );
284
+ }
285
+
286
+ this.initialized = true;
287
+ this.emit('initialized');
288
+ }
289
+
290
+ /**
291
+ * Enqueue a texture for downloading its source image.
292
+ */
293
+ enqueueDownloadTextureSource(texture: Texture): void {
294
+ if (!this.downloadTextureSourceQueue.includes(texture)) {
295
+ this.downloadTextureSourceQueue.push(texture);
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Enqueue a texture for uploading to the GPU.
301
+ *
302
+ * @param texture - The texture to upload
303
+ */
304
+ enqueueUploadTexture(texture: Texture): void {
305
+ if (this.uploadTextureQueue.includes(texture) === false) {
306
+ this.uploadTextureQueue.push(texture);
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Create a texture
312
+ *
313
+ * @param textureType - The type of texture to create
314
+ * @param props - The properties to use for the texture
315
+ */
316
+ createTexture<Type extends keyof TextureMap>(
317
+ textureType: Type,
318
+ props: ExtractProps<TextureMap[Type]>,
319
+ ): InstanceType<TextureMap[Type]> {
320
+ let texture: Texture | undefined;
321
+ const TextureClass = this.txConstructors[textureType];
322
+ if (!TextureClass) {
323
+ throw new Error(`Texture type "${textureType}" is not registered`);
324
+ }
325
+
326
+ const cacheKey = TextureClass.makeCacheKey(props as any);
327
+ if (cacheKey && this.keyCache.has(cacheKey)) {
328
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
329
+ texture = this.keyCache.get(cacheKey)!;
330
+ } else {
331
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
332
+ texture = new TextureClass(this, props as any);
333
+
334
+ if (cacheKey) {
335
+ this.initTextureToCache(texture, cacheKey);
336
+ }
337
+ }
338
+
339
+ return texture as InstanceType<TextureMap[Type]>;
340
+ }
341
+
342
+ orphanTexture(texture: Texture): void {
343
+ // if it is part of the download or upload queue, remove it
344
+ this.removeTextureFromQueue(texture);
345
+
346
+ if (texture.type === TextureType.subTexture) {
347
+ // ignore subtextures
348
+ return;
349
+ }
350
+
351
+ this.stage.txMemManager.addToOrphanedTextures(texture);
352
+ }
353
+
354
+ /**
355
+ * Override loadTexture to use the batched approach.
356
+ *
357
+ * @param texture - The texture to load
358
+ * @param immediate - Whether to prioritize the texture for immediate loading
359
+ */
360
+ loadTexture(texture: Texture, priority?: boolean): void {
361
+ this.stage.txMemManager.removeFromOrphanedTextures(texture);
362
+
363
+ if (texture.type === TextureType.subTexture) {
364
+ // ignore subtextures - they get loaded through their parent
365
+ return;
366
+ }
367
+
368
+ // if the texture is already loaded, don't load it again
369
+ if (
370
+ texture.ctxTexture !== undefined &&
371
+ texture.ctxTexture.state === 'loaded'
372
+ ) {
373
+ texture.setState('loaded');
374
+ return;
375
+ }
376
+
377
+ // if the texture is already being processed, don't load it again
378
+ if (
379
+ this.downloadTextureSourceQueue.includes(texture) === true ||
380
+ this.uploadTextureQueue.includes(texture) === true
381
+ ) {
382
+ return;
383
+ }
384
+
385
+ // if the texture is already loading, free it, this can happen if the texture is
386
+ // orphaned and then reloaded
387
+ if (
388
+ texture.ctxTexture !== undefined &&
389
+ texture.ctxTexture.state === 'loading'
390
+ ) {
391
+ // if the texture has texture data, queue it for upload
392
+ if (texture.textureData !== null) {
393
+ this.enqueueUploadTexture(texture);
394
+ }
395
+
396
+ // else we will have to re-download the texture
397
+ texture.free();
398
+ }
399
+
400
+ // if we're not initialized, just queue the texture into the priority queue
401
+ if (this.initialized === false) {
402
+ this.priorityQueue.push(texture);
403
+ return;
404
+ }
405
+
406
+ // these types of textures don't need to be downloaded
407
+ // Technically the noise texture shouldn't either, but it's a special case
408
+ // and not really used in production so who cares ¯\_(ツ)_/¯
409
+ if (
410
+ (texture.type === TextureType.color ||
411
+ texture.type === TextureType.renderToTexture) &&
412
+ texture.state !== 'initial'
413
+ ) {
414
+ texture.setState('fetched');
415
+ this.enqueueUploadTexture(texture);
416
+ return;
417
+ }
418
+
419
+ texture.setState('loading');
420
+
421
+ // prioritize the texture for immediate loading
422
+ if (priority === true) {
423
+ texture
424
+ .getTextureData()
425
+ .then(() => {
426
+ this.uploadTexture(texture);
427
+ })
428
+ .catch((err) => {
429
+ console.error(err);
430
+ });
431
+ }
432
+
433
+ // enqueue the texture for download and upload
434
+ this.enqueueDownloadTextureSource(texture);
435
+ }
436
+
437
+ /**
438
+ * Upload a texture to the GPU
439
+ *
440
+ * @param texture Texture to upload
441
+ */
442
+ uploadTexture(texture: Texture): void {
443
+ if (
444
+ this.stage.txMemManager.doNotExceedCriticalThreshold === true &&
445
+ this.stage.txMemManager.criticalCleanupRequested === true
446
+ ) {
447
+ // we're at a critical memory threshold, don't upload textures
448
+ this.enqueueUploadTexture(texture);
449
+ return;
450
+ }
451
+
452
+ const coreContext = texture.loadCtxTexture();
453
+ if (coreContext !== null && coreContext.state === 'loaded') {
454
+ texture.setState('loaded');
455
+ return;
456
+ }
457
+
458
+ coreContext.load();
459
+ }
460
+
461
+ /**
462
+ * Check if a texture is being processed
463
+ */
464
+ isProcessingTexture(texture: Texture): boolean {
465
+ return (
466
+ this.downloadTextureSourceQueue.includes(texture) === true ||
467
+ this.uploadTextureQueue.includes(texture) === true
468
+ );
469
+ }
470
+
471
+ /**
472
+ * Process a limited number of downloads and uploads.
473
+ *
474
+ * @param maxItems - The maximum number of items to process
475
+ */
476
+ processSome(maxProcessingTime: number): void {
477
+ if (this.initialized === false) {
478
+ return;
479
+ }
480
+
481
+ const startTime = getTimeStamp();
482
+
483
+ // Process priority queue
484
+ while (
485
+ this.priorityQueue.length > 0 &&
486
+ getTimeStamp() - startTime < maxProcessingTime
487
+ ) {
488
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
489
+ const texture = this.priorityQueue.pop()!;
490
+ texture.getTextureData().then(() => {
491
+ this.uploadTexture(texture);
492
+ });
493
+ }
494
+
495
+ // Process uploads
496
+ while (
497
+ this.uploadTextureQueue.length > 0 &&
498
+ getTimeStamp() - startTime < maxProcessingTime
499
+ ) {
500
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
501
+ this.uploadTexture(this.uploadTextureQueue.pop()!);
502
+ }
503
+
504
+ // Process downloads
505
+ while (
506
+ this.downloadTextureSourceQueue.length > 0 &&
507
+ getTimeStamp() - startTime < maxProcessingTime
508
+ ) {
509
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
510
+ const texture = this.downloadTextureSourceQueue.shift()!;
511
+ texture.getTextureData().then(() => {
512
+ if (texture.state === 'fetched') {
513
+ this.enqueueUploadTexture(texture);
514
+ }
515
+ });
516
+ }
517
+ }
518
+
519
+ public hasUpdates(): boolean {
520
+ return (
521
+ this.downloadTextureSourceQueue.length > 0 ||
522
+ this.uploadTextureQueue.length > 0
523
+ );
524
+ }
525
+
526
+ /**
527
+ * Initialize a texture to the cache
528
+ *
529
+ * @param texture Texture to cache
530
+ * @param cacheKey Cache key for the texture
531
+ */
532
+ initTextureToCache(texture: Texture, cacheKey: string) {
533
+ const { keyCache, inverseKeyCache } = this;
534
+ keyCache.set(cacheKey, texture);
535
+ inverseKeyCache.set(texture, cacheKey);
536
+ }
537
+
538
+ /**
539
+ * Get a texture from the cache
540
+ *
541
+ * @param cacheKey
542
+ */
543
+ getTextureFromCache(cacheKey: string): Texture | undefined {
544
+ return this.keyCache.get(cacheKey);
545
+ }
546
+
547
+ /**
548
+ * Remove a texture from the cache
549
+ *
550
+ * @remarks
551
+ * Called by Texture Cleanup when a texture is freed.
552
+ *
553
+ * @param texture
554
+ */
555
+ removeTextureFromCache(texture: Texture) {
556
+ const { inverseKeyCache, keyCache } = this;
557
+ const cacheKey = inverseKeyCache.get(texture);
558
+ if (cacheKey) {
559
+ keyCache.delete(cacheKey);
560
+ }
561
+ }
562
+
563
+ /**
564
+ * Remove texture from the queue's
565
+ *
566
+ * @param texture - The texture to remove
567
+ */
568
+ removeTextureFromQueue(texture: Texture): void {
569
+ const downloadIndex = this.downloadTextureSourceQueue.indexOf(texture);
570
+ if (downloadIndex !== -1) {
571
+ this.downloadTextureSourceQueue.splice(downloadIndex, 1);
572
+ }
573
+
574
+ const uploadIndex = this.uploadTextureQueue.indexOf(texture);
575
+ if (uploadIndex !== -1) {
576
+ this.uploadTextureQueue.splice(uploadIndex, 1);
577
+ }
578
+ }
579
+
580
+ /**
581
+ * Resolve a parent texture from the cache or fallback to the provided texture.
582
+ *
583
+ * @param texture - The provided texture to resolve.
584
+ * @returns The cached or provided texture.
585
+ */
586
+ resolveParentTexture(texture: ImageTexture): Texture {
587
+ if (!texture?.props) {
588
+ return texture;
589
+ }
590
+
591
+ const cacheKey = ImageTexture.makeCacheKey(texture.props);
592
+ const cachedTexture = cacheKey
593
+ ? this.getTextureFromCache(cacheKey)
594
+ : undefined;
595
+ return cachedTexture ?? texture;
596
+ }
597
+ }