@motion-core/motion-gpu 0.4.1 → 0.5.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 (228) hide show
  1. package/README.md +99 -0
  2. package/dist/advanced.d.ts +1 -0
  3. package/dist/advanced.d.ts.map +1 -0
  4. package/dist/advanced.js +14 -6
  5. package/dist/core/advanced.d.ts +1 -0
  6. package/dist/core/advanced.d.ts.map +1 -0
  7. package/dist/core/advanced.js +14 -5
  8. package/dist/core/compute-shader.d.ts +87 -0
  9. package/dist/core/compute-shader.d.ts.map +1 -0
  10. package/dist/core/compute-shader.js +205 -0
  11. package/dist/core/compute-shader.js.map +1 -0
  12. package/dist/core/current-value.d.ts +1 -0
  13. package/dist/core/current-value.d.ts.map +1 -0
  14. package/dist/core/current-value.js +35 -34
  15. package/dist/core/current-value.js.map +1 -0
  16. package/dist/core/error-diagnostics.d.ts +1 -0
  17. package/dist/core/error-diagnostics.d.ts.map +1 -0
  18. package/dist/core/error-diagnostics.js +70 -137
  19. package/dist/core/error-diagnostics.js.map +1 -0
  20. package/dist/core/error-report.d.ts +2 -1
  21. package/dist/core/error-report.d.ts.map +1 -0
  22. package/dist/core/error-report.js +247 -233
  23. package/dist/core/error-report.js.map +1 -0
  24. package/dist/core/frame-registry.d.ts +1 -0
  25. package/dist/core/frame-registry.d.ts.map +1 -0
  26. package/dist/core/frame-registry.js +546 -662
  27. package/dist/core/frame-registry.js.map +1 -0
  28. package/dist/core/index.d.ts +6 -2
  29. package/dist/core/index.d.ts.map +1 -0
  30. package/dist/core/index.js +13 -12
  31. package/dist/core/material-preprocess.d.ts +1 -0
  32. package/dist/core/material-preprocess.d.ts.map +1 -0
  33. package/dist/core/material-preprocess.js +131 -152
  34. package/dist/core/material-preprocess.js.map +1 -0
  35. package/dist/core/material.d.ts +23 -6
  36. package/dist/core/material.d.ts.map +1 -0
  37. package/dist/core/material.js +290 -317
  38. package/dist/core/material.js.map +1 -0
  39. package/dist/core/recompile-policy.d.ts +1 -0
  40. package/dist/core/recompile-policy.d.ts.map +1 -0
  41. package/dist/core/recompile-policy.js +18 -13
  42. package/dist/core/recompile-policy.js.map +1 -0
  43. package/dist/core/render-graph.d.ts +8 -3
  44. package/dist/core/render-graph.d.ts.map +1 -0
  45. package/dist/core/render-graph.js +77 -68
  46. package/dist/core/render-graph.js.map +1 -0
  47. package/dist/core/render-targets.d.ts +1 -0
  48. package/dist/core/render-targets.d.ts.map +1 -0
  49. package/dist/core/render-targets.js +52 -53
  50. package/dist/core/render-targets.js.map +1 -0
  51. package/dist/core/renderer.d.ts +1 -0
  52. package/dist/core/renderer.d.ts.map +1 -0
  53. package/dist/core/renderer.js +1337 -1081
  54. package/dist/core/renderer.js.map +1 -0
  55. package/dist/core/runtime-loop.d.ts +3 -2
  56. package/dist/core/runtime-loop.d.ts.map +1 -0
  57. package/dist/core/runtime-loop.js +353 -362
  58. package/dist/core/runtime-loop.js.map +1 -0
  59. package/dist/core/scheduler-helpers.d.ts +1 -0
  60. package/dist/core/scheduler-helpers.d.ts.map +1 -0
  61. package/dist/core/scheduler-helpers.js +52 -51
  62. package/dist/core/scheduler-helpers.js.map +1 -0
  63. package/dist/core/shader.d.ts +10 -1
  64. package/dist/core/shader.d.ts.map +1 -0
  65. package/dist/core/shader.js +109 -115
  66. package/dist/core/shader.js.map +1 -0
  67. package/dist/core/storage-buffers.d.ts +37 -0
  68. package/dist/core/storage-buffers.d.ts.map +1 -0
  69. package/dist/core/storage-buffers.js +95 -0
  70. package/dist/core/storage-buffers.js.map +1 -0
  71. package/dist/core/texture-loader.d.ts +1 -0
  72. package/dist/core/texture-loader.d.ts.map +1 -0
  73. package/dist/core/texture-loader.js +209 -273
  74. package/dist/core/texture-loader.js.map +1 -0
  75. package/dist/core/textures.d.ts +13 -0
  76. package/dist/core/textures.d.ts.map +1 -0
  77. package/dist/core/textures.js +111 -116
  78. package/dist/core/textures.js.map +1 -0
  79. package/dist/core/types.d.ts +147 -4
  80. package/dist/core/types.d.ts.map +1 -0
  81. package/dist/core/types.js +0 -4
  82. package/dist/core/uniforms.d.ts +1 -0
  83. package/dist/core/uniforms.d.ts.map +1 -0
  84. package/dist/core/uniforms.js +170 -191
  85. package/dist/core/uniforms.js.map +1 -0
  86. package/dist/index.d.ts +1 -0
  87. package/dist/index.d.ts.map +1 -0
  88. package/dist/index.js +13 -6
  89. package/dist/passes/BlitPass.d.ts +1 -0
  90. package/dist/passes/BlitPass.d.ts.map +1 -0
  91. package/dist/passes/BlitPass.js +23 -18
  92. package/dist/passes/BlitPass.js.map +1 -0
  93. package/dist/passes/ComputePass.d.ts +83 -0
  94. package/dist/passes/ComputePass.d.ts.map +1 -0
  95. package/dist/passes/ComputePass.js +92 -0
  96. package/dist/passes/ComputePass.js.map +1 -0
  97. package/dist/passes/CopyPass.d.ts +1 -0
  98. package/dist/passes/CopyPass.d.ts.map +1 -0
  99. package/dist/passes/CopyPass.js +58 -52
  100. package/dist/passes/CopyPass.js.map +1 -0
  101. package/dist/passes/FullscreenPass.d.ts +1 -0
  102. package/dist/passes/FullscreenPass.d.ts.map +1 -0
  103. package/dist/passes/FullscreenPass.js +127 -130
  104. package/dist/passes/FullscreenPass.js.map +1 -0
  105. package/dist/passes/PingPongComputePass.d.ts +104 -0
  106. package/dist/passes/PingPongComputePass.d.ts.map +1 -0
  107. package/dist/passes/PingPongComputePass.js +132 -0
  108. package/dist/passes/PingPongComputePass.js.map +1 -0
  109. package/dist/passes/ShaderPass.d.ts +1 -0
  110. package/dist/passes/ShaderPass.d.ts.map +1 -0
  111. package/dist/passes/ShaderPass.js +41 -37
  112. package/dist/passes/ShaderPass.js.map +1 -0
  113. package/dist/passes/index.d.ts +3 -0
  114. package/dist/passes/index.d.ts.map +1 -0
  115. package/dist/passes/index.js +6 -3
  116. package/dist/react/FragCanvas.d.ts +3 -2
  117. package/dist/react/FragCanvas.d.ts.map +1 -0
  118. package/dist/react/FragCanvas.js +234 -211
  119. package/dist/react/FragCanvas.js.map +1 -0
  120. package/dist/react/MotionGPUErrorOverlay.d.ts +1 -0
  121. package/dist/react/MotionGPUErrorOverlay.d.ts.map +1 -0
  122. package/dist/react/MotionGPUErrorOverlay.js +200 -14
  123. package/dist/react/MotionGPUErrorOverlay.js.map +1 -0
  124. package/dist/react/Portal.d.ts +1 -0
  125. package/dist/react/Portal.d.ts.map +1 -0
  126. package/dist/react/Portal.js +18 -21
  127. package/dist/react/Portal.js.map +1 -0
  128. package/dist/react/advanced.d.ts +1 -0
  129. package/dist/react/advanced.d.ts.map +1 -0
  130. package/dist/react/advanced.js +14 -6
  131. package/dist/react/frame-context.d.ts +1 -0
  132. package/dist/react/frame-context.d.ts.map +1 -0
  133. package/dist/react/frame-context.js +88 -94
  134. package/dist/react/frame-context.js.map +1 -0
  135. package/dist/react/index.d.ts +6 -2
  136. package/dist/react/index.d.ts.map +1 -0
  137. package/dist/react/index.js +12 -9
  138. package/dist/react/motiongpu-context.d.ts +1 -0
  139. package/dist/react/motiongpu-context.d.ts.map +1 -0
  140. package/dist/react/motiongpu-context.js +18 -15
  141. package/dist/react/motiongpu-context.js.map +1 -0
  142. package/dist/react/use-motiongpu-user-context.d.ts +1 -0
  143. package/dist/react/use-motiongpu-user-context.d.ts.map +1 -0
  144. package/dist/react/use-motiongpu-user-context.js +83 -82
  145. package/dist/react/use-motiongpu-user-context.js.map +1 -0
  146. package/dist/react/use-texture.d.ts +1 -0
  147. package/dist/react/use-texture.d.ts.map +1 -0
  148. package/dist/react/use-texture.js +132 -152
  149. package/dist/react/use-texture.js.map +1 -0
  150. package/dist/svelte/FragCanvas.svelte +2 -2
  151. package/dist/svelte/FragCanvas.svelte.d.ts +3 -2
  152. package/dist/svelte/FragCanvas.svelte.d.ts.map +1 -0
  153. package/dist/svelte/MotionGPUErrorOverlay.svelte +137 -7
  154. package/dist/svelte/MotionGPUErrorOverlay.svelte.d.ts +1 -0
  155. package/dist/svelte/MotionGPUErrorOverlay.svelte.d.ts.map +1 -0
  156. package/dist/svelte/Portal.svelte.d.ts +1 -0
  157. package/dist/svelte/Portal.svelte.d.ts.map +1 -0
  158. package/dist/svelte/advanced.d.ts +1 -0
  159. package/dist/svelte/advanced.d.ts.map +1 -0
  160. package/dist/svelte/advanced.js +13 -6
  161. package/dist/svelte/frame-context.d.ts +1 -0
  162. package/dist/svelte/frame-context.d.ts.map +1 -0
  163. package/dist/svelte/frame-context.js +27 -27
  164. package/dist/svelte/frame-context.js.map +1 -0
  165. package/dist/svelte/index.d.ts +6 -2
  166. package/dist/svelte/index.d.ts.map +1 -0
  167. package/dist/svelte/index.js +12 -9
  168. package/dist/svelte/motiongpu-context.d.ts +1 -0
  169. package/dist/svelte/motiongpu-context.d.ts.map +1 -0
  170. package/dist/svelte/motiongpu-context.js +24 -21
  171. package/dist/svelte/motiongpu-context.js.map +1 -0
  172. package/dist/svelte/use-motiongpu-user-context.d.ts +1 -0
  173. package/dist/svelte/use-motiongpu-user-context.d.ts.map +1 -0
  174. package/dist/svelte/use-motiongpu-user-context.js +69 -70
  175. package/dist/svelte/use-motiongpu-user-context.js.map +1 -0
  176. package/dist/svelte/use-texture.d.ts +1 -0
  177. package/dist/svelte/use-texture.d.ts.map +1 -0
  178. package/dist/svelte/use-texture.js +125 -147
  179. package/dist/svelte/use-texture.js.map +1 -0
  180. package/package.json +12 -7
  181. package/src/lib/advanced.ts +6 -0
  182. package/src/lib/core/advanced.ts +12 -0
  183. package/src/lib/core/compute-shader.ts +326 -0
  184. package/src/lib/core/current-value.ts +64 -0
  185. package/src/lib/core/error-diagnostics.ts +236 -0
  186. package/src/lib/core/error-report.ts +535 -0
  187. package/src/lib/core/frame-registry.ts +1190 -0
  188. package/src/lib/core/index.ts +94 -0
  189. package/src/lib/core/material-preprocess.ts +295 -0
  190. package/src/lib/core/material.ts +748 -0
  191. package/src/lib/core/recompile-policy.ts +31 -0
  192. package/src/lib/core/render-graph.ts +173 -0
  193. package/src/lib/core/render-targets.ts +107 -0
  194. package/src/lib/core/renderer.ts +2161 -0
  195. package/src/lib/core/runtime-loop.ts +537 -0
  196. package/src/lib/core/scheduler-helpers.ts +136 -0
  197. package/src/lib/core/shader.ts +301 -0
  198. package/src/lib/core/storage-buffers.ts +142 -0
  199. package/src/lib/core/texture-loader.ts +482 -0
  200. package/src/lib/core/textures.ts +257 -0
  201. package/src/lib/core/types.ts +743 -0
  202. package/src/lib/core/uniforms.ts +282 -0
  203. package/src/lib/index.ts +6 -0
  204. package/src/lib/passes/BlitPass.ts +54 -0
  205. package/src/lib/passes/ComputePass.ts +136 -0
  206. package/src/lib/passes/CopyPass.ts +80 -0
  207. package/src/lib/passes/FullscreenPass.ts +173 -0
  208. package/src/lib/passes/PingPongComputePass.ts +180 -0
  209. package/src/lib/passes/ShaderPass.ts +89 -0
  210. package/src/lib/passes/index.ts +9 -0
  211. package/src/lib/react/FragCanvas.tsx +345 -0
  212. package/src/lib/react/MotionGPUErrorOverlay.tsx +524 -0
  213. package/src/lib/react/Portal.tsx +34 -0
  214. package/src/lib/react/advanced.ts +36 -0
  215. package/src/lib/react/frame-context.ts +169 -0
  216. package/src/lib/react/index.ts +68 -0
  217. package/src/lib/react/motiongpu-context.ts +88 -0
  218. package/src/lib/react/use-motiongpu-user-context.ts +186 -0
  219. package/src/lib/react/use-texture.ts +233 -0
  220. package/src/lib/svelte/FragCanvas.svelte +249 -0
  221. package/src/lib/svelte/MotionGPUErrorOverlay.svelte +512 -0
  222. package/src/lib/svelte/Portal.svelte +31 -0
  223. package/src/lib/svelte/advanced.ts +32 -0
  224. package/src/lib/svelte/frame-context.ts +87 -0
  225. package/src/lib/svelte/index.ts +68 -0
  226. package/src/lib/svelte/motiongpu-context.ts +97 -0
  227. package/src/lib/svelte/use-motiongpu-user-context.ts +145 -0
  228. package/src/lib/svelte/use-texture.ts +232 -0
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Svelte adapter entrypoint for MotionGPU.
3
+ */
4
+ export { default as FragCanvas } from './FragCanvas.svelte';
5
+ export { defineMaterial } from '../core/material.js';
6
+ export {
7
+ BlitPass,
8
+ CopyPass,
9
+ ShaderPass,
10
+ ComputePass,
11
+ PingPongComputePass
12
+ } from '../passes/index.js';
13
+ export { useMotionGPU } from './motiongpu-context.js';
14
+ export { useFrame } from './frame-context.js';
15
+ export { useTexture } from './use-texture.js';
16
+ export type {
17
+ FrameInvalidationToken,
18
+ FrameState,
19
+ OutputColorSpace,
20
+ AnyPass,
21
+ ComputePassLike,
22
+ RenderPass,
23
+ RenderPassContext,
24
+ RenderPassFlags,
25
+ RenderPassInputSlot,
26
+ RenderPassOutputSlot,
27
+ RenderMode,
28
+ RenderTarget,
29
+ RenderTargetDefinition,
30
+ RenderTargetDefinitionMap,
31
+ TextureData,
32
+ TextureDefinition,
33
+ TextureDefinitionMap,
34
+ TextureUpdateMode,
35
+ TextureMap,
36
+ TextureSource,
37
+ TextureValue,
38
+ TypedUniform,
39
+ UniformMat4Value,
40
+ UniformMap,
41
+ UniformType,
42
+ UniformValue
43
+ } from '../core/types.js';
44
+ export type {
45
+ LoadedTexture,
46
+ TextureDecodeOptions,
47
+ TextureLoadOptions
48
+ } from '../core/texture-loader.js';
49
+ export type {
50
+ FragMaterial,
51
+ FragMaterialInput,
52
+ MaterialIncludes,
53
+ MaterialDefineValue,
54
+ MaterialDefines,
55
+ TypedMaterialDefineValue
56
+ } from '../core/material.js';
57
+ export type { MotionGPUContext } from './motiongpu-context.js';
58
+ export type { UseFrameOptions, UseFrameResult } from './frame-context.js';
59
+ export type { TextureUrlInput, UseTextureResult } from './use-texture.js';
60
+ export type {
61
+ StorageBufferAccess,
62
+ StorageBufferDefinition,
63
+ StorageBufferDefinitionMap,
64
+ StorageBufferType,
65
+ ComputePassContext
66
+ } from '../core/types.js';
67
+ export type { ComputePassOptions, ComputeDispatchContext } from '../passes/ComputePass.js';
68
+ export type { PingPongComputePassOptions } from '../passes/PingPongComputePass.js';
@@ -0,0 +1,97 @@
1
+ import { getContext, setContext } from 'svelte';
2
+ import type { RenderMode } from '../core/types.js';
3
+ import type { CurrentReadable, CurrentWritable } from '../core/current-value.js';
4
+ import type {
5
+ FrameProfilingSnapshot,
6
+ FrameRunTimings,
7
+ FrameScheduleSnapshot
8
+ } from '../core/frame-registry.js';
9
+ import type { MotionGPUScheduler as CoreMotionGPUScheduler } from '../core/scheduler-helpers.js';
10
+
11
+ /**
12
+ * Svelte context key used to expose `FragCanvas` runtime state.
13
+ */
14
+ const MOTIONGPU_CONTEXT_KEY = Symbol('motiongpu.context');
15
+
16
+ export type MotionGPUScheduler = CoreMotionGPUScheduler;
17
+ export type { FrameProfilingSnapshot, FrameRunTimings, FrameScheduleSnapshot };
18
+
19
+ /**
20
+ * Namespace identifier for user-owned context entries.
21
+ */
22
+ export type MotionGPUUserNamespace = string | symbol;
23
+
24
+ /**
25
+ * Shared user context store exposed by `FragCanvas`.
26
+ */
27
+ export type MotionGPUUserContext = CurrentWritable<Record<MotionGPUUserNamespace, unknown>>;
28
+
29
+ /**
30
+ * Public `FragCanvas` runtime context available to hooks and user components.
31
+ */
32
+ export interface MotionGPUContext {
33
+ /**
34
+ * Underlying canvas element used by the renderer.
35
+ */
36
+ canvas: HTMLCanvasElement | undefined;
37
+ /**
38
+ * Reactive canvas pixel size.
39
+ */
40
+ size: CurrentReadable<{ width: number; height: number }>;
41
+ /**
42
+ * Device pixel ratio multiplier.
43
+ */
44
+ dpr: CurrentWritable<number>;
45
+ /**
46
+ * Max frame delta clamp passed to scheduled callbacks.
47
+ */
48
+ maxDelta: CurrentWritable<number>;
49
+ /**
50
+ * Scheduler render mode (`always`, `on-demand`, `manual`).
51
+ */
52
+ renderMode: CurrentWritable<RenderMode>;
53
+ /**
54
+ * Global toggle for automatic rendering.
55
+ */
56
+ autoRender: CurrentWritable<boolean>;
57
+ /**
58
+ * Namespaced user context store shared within the canvas subtree.
59
+ */
60
+ user: MotionGPUUserContext;
61
+ /**
62
+ * Marks current frame as invalidated.
63
+ */
64
+ invalidate: () => void;
65
+ /**
66
+ * Requests one manual frame advance.
67
+ */
68
+ advance: () => void;
69
+ /**
70
+ * Public scheduler API.
71
+ */
72
+ scheduler: MotionGPUScheduler;
73
+ }
74
+
75
+ /**
76
+ * Registers the motiongpu context in the current Svelte component tree.
77
+ *
78
+ * @param context - Context payload to provide.
79
+ */
80
+ export function provideMotionGPUContext(context: MotionGPUContext): void {
81
+ setContext(MOTIONGPU_CONTEXT_KEY, context);
82
+ }
83
+
84
+ /**
85
+ * Returns the active motiongpu context.
86
+ *
87
+ * @returns Active context.
88
+ * @throws {Error} When called outside `<FragCanvas>`.
89
+ */
90
+ export function useMotionGPU(): MotionGPUContext {
91
+ const context = getContext<MotionGPUContext>(MOTIONGPU_CONTEXT_KEY);
92
+ if (!context) {
93
+ throw new Error('useMotionGPU must be used inside <FragCanvas>');
94
+ }
95
+
96
+ return context;
97
+ }
@@ -0,0 +1,145 @@
1
+ import type { CurrentReadable } from '../core/current-value.js';
2
+ import { useMotionGPU, type MotionGPUUserNamespace } from './motiongpu-context.js';
3
+
4
+ /**
5
+ * Internal shape of the user context store.
6
+ */
7
+ type UserContextStore = Record<MotionGPUUserNamespace, unknown>;
8
+
9
+ /**
10
+ * Object-like context payload used by merge semantics.
11
+ */
12
+ type UserContextEntry = Record<string, unknown>;
13
+
14
+ /**
15
+ * Controls how a namespaced user context value behaves when already present.
16
+ */
17
+ export interface SetMotionGPUUserContextOptions {
18
+ /**
19
+ * Conflict strategy when namespace already exists:
20
+ * - `skip`: keep current value
21
+ * - `replace`: replace current value
22
+ * - `merge`: shallow merge object values, fallback to replace otherwise
23
+ *
24
+ * @default 'skip'
25
+ */
26
+ existing?: 'merge' | 'replace' | 'skip';
27
+ /**
28
+ * How function inputs should be interpreted:
29
+ * - `factory`: call function and store its return value
30
+ * - `value`: store function itself
31
+ *
32
+ * @default 'factory'
33
+ */
34
+ functionValue?: 'factory' | 'value';
35
+ }
36
+
37
+ /**
38
+ * Checks whether a value is a non-array object suitable for shallow merge.
39
+ */
40
+ function isObjectEntry(value: unknown): value is UserContextEntry {
41
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
42
+ }
43
+
44
+ /**
45
+ * Returns a read-only view of the entire motiongpu user context store.
46
+ */
47
+ export function useMotionGPUUserContext<
48
+ UC extends UserContextStore = UserContextStore
49
+ >(): CurrentReadable<UC>;
50
+
51
+ /**
52
+ * Reads a namespaced user context value as a reactive readable store.
53
+ */
54
+ export function useMotionGPUUserContext<
55
+ UC extends UserContextStore = UserContextStore,
56
+ K extends keyof UC & MotionGPUUserNamespace = keyof UC & MotionGPUUserNamespace
57
+ >(namespace: K): CurrentReadable<UC[K] | undefined>;
58
+
59
+ /**
60
+ * Read-only user context hook:
61
+ * - no args: returns full user context store
62
+ * - namespace: returns namespaced store view
63
+ *
64
+ * @param namespace - Optional namespace key.
65
+ */
66
+ export function useMotionGPUUserContext<
67
+ UC extends UserContextStore = UserContextStore,
68
+ K extends keyof UC & MotionGPUUserNamespace = keyof UC & MotionGPUUserNamespace
69
+ >(namespace?: K): CurrentReadable<UC> | CurrentReadable<UC[K] | undefined> {
70
+ const userStore = useMotionGPU().user;
71
+
72
+ if (namespace === undefined) {
73
+ const allStore: CurrentReadable<UC> = {
74
+ get current() {
75
+ return userStore.current as UC;
76
+ },
77
+ subscribe(run) {
78
+ return userStore.subscribe((context) => run(context as UC));
79
+ }
80
+ };
81
+
82
+ return allStore;
83
+ }
84
+
85
+ const scopedStore: CurrentReadable<UC[K] | undefined> = {
86
+ get current() {
87
+ return userStore.current[namespace] as UC[K] | undefined;
88
+ },
89
+ subscribe(run) {
90
+ return userStore.subscribe((context) => run(context[namespace] as UC[K] | undefined));
91
+ }
92
+ };
93
+
94
+ return scopedStore;
95
+ }
96
+
97
+ /**
98
+ * Sets a namespaced user context value with explicit write semantics.
99
+ *
100
+ * Returns the effective value stored under the namespace.
101
+ */
102
+ export function setMotionGPUUserContext<UCT = unknown>(
103
+ namespace: MotionGPUUserNamespace,
104
+ value: UCT | (() => UCT),
105
+ options?: SetMotionGPUUserContextOptions
106
+ ): UCT | undefined {
107
+ const userStore = useMotionGPU().user;
108
+ const mode = options?.existing ?? 'skip';
109
+ const functionValueMode = options?.functionValue ?? 'factory';
110
+ let resolvedValue: UCT | undefined;
111
+
112
+ userStore.update((context) => {
113
+ const hasExisting = namespace in context;
114
+ if (hasExisting && mode === 'skip') {
115
+ resolvedValue = context[namespace] as UCT | undefined;
116
+ return context;
117
+ }
118
+
119
+ const nextValue =
120
+ typeof value === 'function' && functionValueMode === 'factory'
121
+ ? (value as () => UCT)()
122
+ : (value as UCT);
123
+ if (hasExisting && mode === 'merge') {
124
+ const currentValue = context[namespace];
125
+ if (isObjectEntry(currentValue) && isObjectEntry(nextValue)) {
126
+ resolvedValue = {
127
+ ...currentValue,
128
+ ...nextValue
129
+ } as UCT;
130
+ return {
131
+ ...context,
132
+ [namespace]: resolvedValue
133
+ };
134
+ }
135
+ }
136
+
137
+ resolvedValue = nextValue;
138
+ return {
139
+ ...context,
140
+ [namespace]: nextValue
141
+ };
142
+ });
143
+
144
+ return resolvedValue;
145
+ }
@@ -0,0 +1,232 @@
1
+ import { onDestroy } from 'svelte';
2
+ import {
3
+ createCurrentWritable as currentWritable,
4
+ type CurrentReadable
5
+ } from '../core/current-value.js';
6
+ import {
7
+ isAbortError,
8
+ loadTexturesFromUrls,
9
+ type LoadedTexture,
10
+ type TextureLoadOptions
11
+ } from '../core/texture-loader.js';
12
+ import { toMotionGPUErrorReport, type MotionGPUErrorReport } from '../core/error-report.js';
13
+
14
+ /**
15
+ * Reactive state returned by {@link useTexture}.
16
+ */
17
+ export interface UseTextureResult {
18
+ /**
19
+ * Loaded textures or `null` when unavailable/failed.
20
+ */
21
+ textures: CurrentReadable<LoadedTexture[] | null>;
22
+ /**
23
+ * `true` while an active load request is running.
24
+ */
25
+ loading: CurrentReadable<boolean>;
26
+ /**
27
+ * Last loading error.
28
+ */
29
+ error: CurrentReadable<Error | null>;
30
+ /**
31
+ * Last loading error normalized to MotionGPU diagnostics report shape.
32
+ */
33
+ errorReport: CurrentReadable<MotionGPUErrorReport | null>;
34
+ /**
35
+ * Reloads all textures using current URL input.
36
+ */
37
+ reload: () => Promise<void>;
38
+ }
39
+
40
+ /**
41
+ * Supported URL input variants for `useTexture`.
42
+ */
43
+ export type TextureUrlInput = string[] | (() => string[]);
44
+
45
+ /**
46
+ * Supported options input variants for `useTexture`.
47
+ */
48
+ export type TextureOptionsInput = TextureLoadOptions | (() => TextureLoadOptions);
49
+
50
+ /**
51
+ * Normalizes unknown thrown values to an `Error` instance.
52
+ */
53
+ function toError(error: unknown): Error {
54
+ if (error instanceof Error) {
55
+ return error;
56
+ }
57
+
58
+ return new Error('Unknown texture loading error');
59
+ }
60
+
61
+ /**
62
+ * Releases GPU-side resources for a list of loaded textures.
63
+ */
64
+ function disposeTextures(list: LoadedTexture[] | null): void {
65
+ for (const texture of list ?? []) {
66
+ texture.dispose();
67
+ }
68
+ }
69
+
70
+ interface MergedAbortSignal {
71
+ signal: AbortSignal;
72
+ dispose: () => void;
73
+ }
74
+
75
+ function mergeAbortSignals(
76
+ primary: AbortSignal,
77
+ secondary: AbortSignal | undefined
78
+ ): MergedAbortSignal {
79
+ if (!secondary) {
80
+ return {
81
+ signal: primary,
82
+ dispose: () => {}
83
+ };
84
+ }
85
+
86
+ if (typeof AbortSignal.any === 'function') {
87
+ return {
88
+ signal: AbortSignal.any([primary, secondary]),
89
+ dispose: () => {}
90
+ };
91
+ }
92
+
93
+ const fallback = new AbortController();
94
+ let disposed = false;
95
+ const cleanup = (): void => {
96
+ if (disposed) {
97
+ return;
98
+ }
99
+ disposed = true;
100
+ primary.removeEventListener('abort', abort);
101
+ secondary.removeEventListener('abort', abort);
102
+ };
103
+ const abort = (): void => fallback.abort();
104
+
105
+ primary.addEventListener('abort', abort, { once: true });
106
+ secondary.addEventListener('abort', abort, { once: true });
107
+
108
+ return {
109
+ signal: fallback.signal,
110
+ dispose: cleanup
111
+ };
112
+ }
113
+
114
+ /**
115
+ * Loads textures from URLs and exposes reactive loading/error state.
116
+ *
117
+ * @param urlInput - URLs array or lazy URL provider.
118
+ * @param optionsInput - Loader options object or lazy options provider.
119
+ * @returns Reactive texture loading state with reload support.
120
+ */
121
+ export function useTexture(
122
+ urlInput: TextureUrlInput,
123
+ optionsInput: TextureOptionsInput = {}
124
+ ): UseTextureResult {
125
+ const textures = currentWritable<LoadedTexture[] | null>(null);
126
+ const loading = currentWritable(true);
127
+ const error = currentWritable<Error | null>(null);
128
+ const errorReport = currentWritable<MotionGPUErrorReport | null>(null);
129
+ let disposed = false;
130
+ let requestVersion = 0;
131
+ let activeController: AbortController | null = null;
132
+ let runningLoad: Promise<void> | null = null;
133
+ let reloadQueued = false;
134
+ const getUrls = typeof urlInput === 'function' ? urlInput : () => urlInput;
135
+ const getOptions =
136
+ typeof optionsInput === 'function'
137
+ ? (optionsInput as () => TextureLoadOptions)
138
+ : () => optionsInput;
139
+
140
+ const executeLoad = async (): Promise<void> => {
141
+ if (disposed) {
142
+ return;
143
+ }
144
+
145
+ const version = ++requestVersion;
146
+ const controller = new AbortController();
147
+ activeController = controller;
148
+ loading.set(true);
149
+ error.set(null);
150
+ errorReport.set(null);
151
+
152
+ const previous = textures.current;
153
+ const options = getOptions() ?? {};
154
+ const mergedSignal = mergeAbortSignals(controller.signal, options.signal);
155
+ try {
156
+ const loaded = await loadTexturesFromUrls(getUrls(), {
157
+ ...options,
158
+ signal: mergedSignal.signal
159
+ });
160
+ if (disposed || version !== requestVersion) {
161
+ disposeTextures(loaded);
162
+ return;
163
+ }
164
+
165
+ textures.set(loaded);
166
+ disposeTextures(previous);
167
+ } catch (nextError) {
168
+ if (disposed || version !== requestVersion) {
169
+ return;
170
+ }
171
+
172
+ if (isAbortError(nextError)) {
173
+ return;
174
+ }
175
+
176
+ disposeTextures(previous);
177
+ textures.set(null);
178
+ const normalizedError = toError(nextError);
179
+ error.set(normalizedError);
180
+ errorReport.set(toMotionGPUErrorReport(normalizedError, 'initialization'));
181
+ } finally {
182
+ if (!disposed && version === requestVersion) {
183
+ loading.set(false);
184
+ }
185
+ if (activeController === controller) {
186
+ activeController = null;
187
+ }
188
+ mergedSignal.dispose();
189
+ }
190
+ };
191
+
192
+ const runLoadLoop = async (): Promise<void> => {
193
+ do {
194
+ reloadQueued = false;
195
+ await executeLoad();
196
+ } while (reloadQueued && !disposed);
197
+ };
198
+
199
+ const load = (): Promise<void> => {
200
+ activeController?.abort();
201
+ if (runningLoad) {
202
+ reloadQueued = true;
203
+ return runningLoad;
204
+ }
205
+
206
+ const pending = runLoadLoop();
207
+ const trackedPending = pending.finally(() => {
208
+ if (runningLoad === trackedPending) {
209
+ runningLoad = null;
210
+ }
211
+ });
212
+ runningLoad = trackedPending;
213
+ return trackedPending;
214
+ };
215
+
216
+ void load();
217
+
218
+ onDestroy(() => {
219
+ disposed = true;
220
+ requestVersion += 1;
221
+ activeController?.abort();
222
+ disposeTextures(textures.current);
223
+ });
224
+
225
+ return {
226
+ textures,
227
+ loading,
228
+ error,
229
+ errorReport,
230
+ reload: load
231
+ };
232
+ }