@motion-core/motion-gpu 0.3.0 → 0.4.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.
- package/README.md +72 -1
- package/dist/core/material-preprocess.d.ts +5 -5
- package/dist/core/material-preprocess.js +1 -4
- package/dist/core/material.d.ts +32 -23
- package/dist/core/material.js +14 -7
- package/dist/core/types.d.ts +20 -10
- package/dist/react/FragCanvas.d.ts +26 -0
- package/dist/react/FragCanvas.js +218 -0
- package/dist/react/FragCanvas.tsx +345 -0
- package/dist/react/MotionGPUErrorOverlay.d.ts +6 -0
- package/dist/react/MotionGPUErrorOverlay.js +52 -0
- package/dist/react/MotionGPUErrorOverlay.tsx +129 -0
- package/dist/react/Portal.d.ts +6 -0
- package/dist/react/Portal.js +24 -0
- package/dist/react/Portal.tsx +34 -0
- package/dist/react/advanced.d.ts +11 -0
- package/dist/react/advanced.js +6 -0
- package/dist/react/frame-context.d.ts +14 -0
- package/dist/react/frame-context.js +98 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.js +9 -0
- package/dist/react/motiongpu-context.d.ts +73 -0
- package/dist/react/motiongpu-context.js +18 -0
- package/dist/react/use-motiongpu-user-context.d.ts +49 -0
- package/dist/react/use-motiongpu-user-context.js +94 -0
- package/dist/react/use-texture.d.ts +40 -0
- package/dist/react/use-texture.js +162 -0
- package/dist/svelte/FragCanvas.svelte +8 -22
- package/dist/svelte/Portal.svelte +6 -21
- package/dist/svelte/use-motiongpu-user-context.d.ts +9 -1
- package/dist/svelte/use-motiongpu-user-context.js +4 -1
- package/dist/svelte/use-texture.d.ts +6 -2
- package/dist/svelte/use-texture.js +6 -2
- package/package.json +28 -3
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://opensource.org/licenses/MIT)
|
|
4
4
|
[](https://svelte.dev)
|
|
5
|
+
[](https://react.dev)
|
|
5
6
|
[](https://gpuweb.github.io/gpuweb/)
|
|
6
7
|
[](https://www.typescriptlang.org)
|
|
7
8
|
[](https://www.npmjs.com/package/@motion-core/motion-gpu)
|
|
@@ -12,7 +13,7 @@
|
|
|
12
13
|
|
|
13
14
|
**A tiny WebGPU runtime for writing Shadertoy-style fullscreen shaders in pure WGSL.**
|
|
14
15
|
|
|
15
|
-
`@motion-core/motion-gpu` ships a framework-agnostic core plus
|
|
16
|
+
`@motion-core/motion-gpu` ships a framework-agnostic core plus Svelte 5 and React adapters for building fullscreen shader pipelines using WebGPU and WGSL.
|
|
16
17
|
It provides a minimal runtime loop, scheduler, and render graph designed specifically for fragment-driven GPU programs.
|
|
17
18
|
|
|
18
19
|
Unlike general-purpose 3D engines, Motion GPU focuses on a very narrow problem: **running fullscreen fragment shaders and multi-pass GPU pipelines**.
|
|
@@ -119,6 +120,39 @@ Also exports runtime/core types:
|
|
|
119
120
|
|
|
120
121
|
---
|
|
121
122
|
|
|
123
|
+
## React adapter
|
|
124
|
+
|
|
125
|
+
`@motion-core/motion-gpu/react` exposes the runtime API for React:
|
|
126
|
+
|
|
127
|
+
- `FragCanvas`
|
|
128
|
+
- `defineMaterial`
|
|
129
|
+
- `useMotionGPU`
|
|
130
|
+
- `useFrame`
|
|
131
|
+
- `useTexture`
|
|
132
|
+
- `ShaderPass`
|
|
133
|
+
- `BlitPass`
|
|
134
|
+
- `CopyPass`
|
|
135
|
+
|
|
136
|
+
Also exports runtime/core types:
|
|
137
|
+
|
|
138
|
+
- uniforms
|
|
139
|
+
- textures
|
|
140
|
+
- render passes
|
|
141
|
+
- scheduler
|
|
142
|
+
- loader types
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
`@motion-core/motion-gpu/react/advanced` re-exports everything above, plus:
|
|
147
|
+
|
|
148
|
+
- `useMotionGPUUserContext`
|
|
149
|
+
- `useSetMotionGPUUserContext`
|
|
150
|
+
- `setMotionGPUUserContext`
|
|
151
|
+
- `applySchedulerPreset`
|
|
152
|
+
- `captureSchedulerDebugSnapshot`
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
122
156
|
## Framework-agnostic core
|
|
123
157
|
|
|
124
158
|
`@motion-core/motion-gpu` (and explicit alias `@motion-core/motion-gpu/core`) exposes adapter-building primitives:
|
|
@@ -144,6 +178,7 @@ Also exports runtime/core types:
|
|
|
144
178
|
# Requirements
|
|
145
179
|
|
|
146
180
|
- Svelte 5 is required only for the Svelte adapter entrypoints (`/svelte`, `/svelte/advanced`)
|
|
181
|
+
- React 18+ is required only for the React adapter entrypoints (`/react`, `/react/advanced`)
|
|
147
182
|
- A browser/runtime with WebGPU support
|
|
148
183
|
- Secure context (`https://` or `localhost`)
|
|
149
184
|
|
|
@@ -188,6 +223,30 @@ fn frag(uv: vec2f) -> vec4f {
|
|
|
188
223
|
|
|
189
224
|
---
|
|
190
225
|
|
|
226
|
+
### React equivalent
|
|
227
|
+
|
|
228
|
+
```tsx
|
|
229
|
+
import { FragCanvas, defineMaterial } from '@motion-core/motion-gpu/react';
|
|
230
|
+
|
|
231
|
+
const material = defineMaterial({
|
|
232
|
+
fragment: `
|
|
233
|
+
fn frag(uv: vec2f) -> vec4f {
|
|
234
|
+
return vec4f(uv.x, uv.y, 0.25, 1.0);
|
|
235
|
+
}
|
|
236
|
+
`
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
export function App() {
|
|
240
|
+
return (
|
|
241
|
+
<div style={{ width: '100vw', height: '100vh' }}>
|
|
242
|
+
<FragCanvas material={material} />
|
|
243
|
+
</div>
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
---
|
|
249
|
+
|
|
191
250
|
## 2. Add animated uniforms via `useFrame`
|
|
192
251
|
|
|
193
252
|
```svelte
|
|
@@ -225,6 +284,18 @@ fn frag(uv: vec2f) -> vec4f {
|
|
|
225
284
|
</script>
|
|
226
285
|
```
|
|
227
286
|
|
|
287
|
+
```tsx
|
|
288
|
+
import { useFrame } from '@motion-core/motion-gpu/react';
|
|
289
|
+
|
|
290
|
+
export function Runtime() {
|
|
291
|
+
useFrame((state) => {
|
|
292
|
+
state.setUniform('uTime', state.time);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
228
299
|
---
|
|
229
300
|
|
|
230
301
|
# Core Runtime Model
|
|
@@ -44,11 +44,11 @@ export interface PreprocessedMaterialFragment {
|
|
|
44
44
|
/**
|
|
45
45
|
* Validates and normalizes define entries.
|
|
46
46
|
*/
|
|
47
|
-
export declare function normalizeDefines(defines: MaterialDefines | undefined): MaterialDefines
|
|
47
|
+
export declare function normalizeDefines<TDefineKey extends string>(defines: MaterialDefines<TDefineKey> | undefined): MaterialDefines<TDefineKey>;
|
|
48
48
|
/**
|
|
49
49
|
* Validates include map identifiers and source chunks.
|
|
50
50
|
*/
|
|
51
|
-
export declare function normalizeIncludes(includes: MaterialIncludes | undefined): MaterialIncludes
|
|
51
|
+
export declare function normalizeIncludes<TIncludeKey extends string>(includes: MaterialIncludes<TIncludeKey> | undefined): MaterialIncludes<TIncludeKey>;
|
|
52
52
|
/**
|
|
53
53
|
* Converts one define declaration to WGSL `const`.
|
|
54
54
|
*/
|
|
@@ -56,8 +56,8 @@ export declare function toDefineLine(key: string, value: MaterialDefineValue): s
|
|
|
56
56
|
/**
|
|
57
57
|
* Preprocesses material fragment with deterministic define/include expansion and line mapping.
|
|
58
58
|
*/
|
|
59
|
-
export declare function preprocessMaterialFragment(input: {
|
|
59
|
+
export declare function preprocessMaterialFragment<TDefineKey extends string, TIncludeKey extends string>(input: {
|
|
60
60
|
fragment: string;
|
|
61
|
-
defines?: MaterialDefines
|
|
62
|
-
includes?: MaterialIncludes
|
|
61
|
+
defines?: MaterialDefines<TDefineKey>;
|
|
62
|
+
includes?: MaterialIncludes<TIncludeKey>;
|
|
63
63
|
}): PreprocessedMaterialFragment;
|
|
@@ -44,10 +44,7 @@ export function normalizeDefines(defines) {
|
|
|
44
44
|
continue;
|
|
45
45
|
}
|
|
46
46
|
const normalized = normalizeTypedDefine(name, value);
|
|
47
|
-
resolved[name] = Object.freeze(
|
|
48
|
-
type: normalized.type,
|
|
49
|
-
value: normalized.value
|
|
50
|
-
});
|
|
47
|
+
resolved[name] = Object.freeze(normalized);
|
|
51
48
|
}
|
|
52
49
|
return resolved;
|
|
53
50
|
}
|
package/dist/core/material.d.ts
CHANGED
|
@@ -5,16 +5,25 @@ import type { TextureDefinitionMap, UniformMap } from './types.js';
|
|
|
5
5
|
/**
|
|
6
6
|
* Typed compile-time define declaration.
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
8
|
+
export type TypedMaterialDefineValue = {
|
|
9
9
|
/**
|
|
10
10
|
* WGSL scalar type.
|
|
11
11
|
*/
|
|
12
|
-
type: 'bool'
|
|
12
|
+
type: 'bool';
|
|
13
13
|
/**
|
|
14
14
|
* Literal value for the selected WGSL type.
|
|
15
15
|
*/
|
|
16
|
-
value: boolean
|
|
17
|
-
}
|
|
16
|
+
value: boolean;
|
|
17
|
+
} | {
|
|
18
|
+
/**
|
|
19
|
+
* WGSL scalar type.
|
|
20
|
+
*/
|
|
21
|
+
type: 'f32' | 'i32' | 'u32';
|
|
22
|
+
/**
|
|
23
|
+
* Literal value for the selected WGSL type.
|
|
24
|
+
*/
|
|
25
|
+
value: number;
|
|
26
|
+
};
|
|
18
27
|
/**
|
|
19
28
|
* Allowed value types for WGSL `const` define injection.
|
|
20
29
|
*/
|
|
@@ -22,15 +31,15 @@ export type MaterialDefineValue = boolean | number | TypedMaterialDefineValue;
|
|
|
22
31
|
/**
|
|
23
32
|
* Define map keyed by uniform-compatible identifier names.
|
|
24
33
|
*/
|
|
25
|
-
export type MaterialDefines = Record<
|
|
34
|
+
export type MaterialDefines<TKey extends string = string> = Record<TKey, MaterialDefineValue>;
|
|
26
35
|
/**
|
|
27
36
|
* Include map keyed by include identifier used in `#include <name>` directives.
|
|
28
37
|
*/
|
|
29
|
-
export type MaterialIncludes = Record<
|
|
38
|
+
export type MaterialIncludes<TKey extends string = string> = Record<TKey, string>;
|
|
30
39
|
/**
|
|
31
40
|
* External material input accepted by {@link defineMaterial}.
|
|
32
41
|
*/
|
|
33
|
-
export interface FragMaterialInput {
|
|
42
|
+
export interface FragMaterialInput<TUniformKey extends string = string, TTextureKey extends string = string, TDefineKey extends string = string, TIncludeKey extends string = string> {
|
|
34
43
|
/**
|
|
35
44
|
* User WGSL source containing `frag(uv: vec2f) -> vec4f`.
|
|
36
45
|
*/
|
|
@@ -38,24 +47,24 @@ export interface FragMaterialInput {
|
|
|
38
47
|
/**
|
|
39
48
|
* Initial uniform values.
|
|
40
49
|
*/
|
|
41
|
-
uniforms?: UniformMap
|
|
50
|
+
uniforms?: UniformMap<TUniformKey>;
|
|
42
51
|
/**
|
|
43
52
|
* Texture definitions keyed by texture uniform name.
|
|
44
53
|
*/
|
|
45
|
-
textures?: TextureDefinitionMap
|
|
54
|
+
textures?: TextureDefinitionMap<TTextureKey>;
|
|
46
55
|
/**
|
|
47
56
|
* Optional compile-time define constants injected into WGSL.
|
|
48
57
|
*/
|
|
49
|
-
defines?: MaterialDefines
|
|
58
|
+
defines?: MaterialDefines<TDefineKey>;
|
|
50
59
|
/**
|
|
51
60
|
* Optional WGSL include chunks used by `#include <name>` directives.
|
|
52
61
|
*/
|
|
53
|
-
includes?: MaterialIncludes
|
|
62
|
+
includes?: MaterialIncludes<TIncludeKey>;
|
|
54
63
|
}
|
|
55
64
|
/**
|
|
56
65
|
* Normalized and immutable material declaration consumed by `FragCanvas`.
|
|
57
66
|
*/
|
|
58
|
-
export interface FragMaterial {
|
|
67
|
+
export interface FragMaterial<TUniformKey extends string = string, TTextureKey extends string = string, TDefineKey extends string = string, TIncludeKey extends string = string> {
|
|
59
68
|
/**
|
|
60
69
|
* User WGSL source containing `frag(uv: vec2f) -> vec4f`.
|
|
61
70
|
*/
|
|
@@ -63,24 +72,24 @@ export interface FragMaterial {
|
|
|
63
72
|
/**
|
|
64
73
|
* Initial uniform values.
|
|
65
74
|
*/
|
|
66
|
-
readonly uniforms: Readonly<UniformMap
|
|
75
|
+
readonly uniforms: Readonly<UniformMap<TUniformKey>>;
|
|
67
76
|
/**
|
|
68
77
|
* Texture definitions keyed by texture uniform name.
|
|
69
78
|
*/
|
|
70
|
-
readonly textures: Readonly<TextureDefinitionMap
|
|
79
|
+
readonly textures: Readonly<TextureDefinitionMap<TTextureKey>>;
|
|
71
80
|
/**
|
|
72
81
|
* Optional compile-time define constants injected into WGSL.
|
|
73
82
|
*/
|
|
74
|
-
readonly defines: Readonly<MaterialDefines
|
|
83
|
+
readonly defines: Readonly<MaterialDefines<TDefineKey>>;
|
|
75
84
|
/**
|
|
76
85
|
* Optional WGSL include chunks used by `#include <name>` directives.
|
|
77
86
|
*/
|
|
78
|
-
readonly includes: Readonly<MaterialIncludes
|
|
87
|
+
readonly includes: Readonly<MaterialIncludes<TIncludeKey>>;
|
|
79
88
|
}
|
|
80
89
|
/**
|
|
81
90
|
* Fully resolved, immutable material snapshot used for renderer creation/caching.
|
|
82
91
|
*/
|
|
83
|
-
export interface ResolvedMaterial {
|
|
92
|
+
export interface ResolvedMaterial<TUniformKey extends string = string, TTextureKey extends string = string, TIncludeKey extends string = string> {
|
|
84
93
|
/**
|
|
85
94
|
* Final fragment WGSL after define injection.
|
|
86
95
|
*/
|
|
@@ -92,11 +101,11 @@ export interface ResolvedMaterial {
|
|
|
92
101
|
/**
|
|
93
102
|
* Cloned uniforms.
|
|
94
103
|
*/
|
|
95
|
-
uniforms: UniformMap
|
|
104
|
+
uniforms: UniformMap<TUniformKey>;
|
|
96
105
|
/**
|
|
97
106
|
* Cloned texture definitions.
|
|
98
107
|
*/
|
|
99
|
-
textures: TextureDefinitionMap
|
|
108
|
+
textures: TextureDefinitionMap<TTextureKey>;
|
|
100
109
|
/**
|
|
101
110
|
* Resolved packed uniform layout.
|
|
102
111
|
*/
|
|
@@ -104,7 +113,7 @@ export interface ResolvedMaterial {
|
|
|
104
113
|
/**
|
|
105
114
|
* Sorted texture keys.
|
|
106
115
|
*/
|
|
107
|
-
textureKeys:
|
|
116
|
+
textureKeys: TTextureKey[];
|
|
108
117
|
/**
|
|
109
118
|
* Deterministic JSON signature for cache invalidation.
|
|
110
119
|
*/
|
|
@@ -116,7 +125,7 @@ export interface ResolvedMaterial {
|
|
|
116
125
|
/**
|
|
117
126
|
* Normalized include sources map.
|
|
118
127
|
*/
|
|
119
|
-
includeSources: MaterialIncludes
|
|
128
|
+
includeSources: MaterialIncludes<TIncludeKey>;
|
|
120
129
|
/**
|
|
121
130
|
* Deterministic define block source used for diagnostics mapping.
|
|
122
131
|
*/
|
|
@@ -147,11 +156,11 @@ export declare function applyMaterialDefines(fragment: string, defines: Material
|
|
|
147
156
|
* @param input - User material declaration.
|
|
148
157
|
* @returns Frozen material object safe to share and cache.
|
|
149
158
|
*/
|
|
150
|
-
export declare function defineMaterial(input: FragMaterialInput): FragMaterial
|
|
159
|
+
export declare function defineMaterial<TUniformKey extends string = string, TTextureKey extends string = string, TDefineKey extends string = string, TIncludeKey extends string = string>(input: FragMaterialInput<TUniformKey, TTextureKey, TDefineKey, TIncludeKey>): FragMaterial<TUniformKey, TTextureKey, TDefineKey, TIncludeKey>;
|
|
151
160
|
/**
|
|
152
161
|
* Resolves a material to renderer-ready data and a deterministic signature.
|
|
153
162
|
*
|
|
154
163
|
* @param material - Material input created via {@link defineMaterial}.
|
|
155
164
|
* @returns Resolved material with packed uniform layout, sorted texture keys and cache signature.
|
|
156
165
|
*/
|
|
157
|
-
export declare function resolveMaterial(material: FragMaterial): ResolvedMaterial
|
|
166
|
+
export declare function resolveMaterial<TUniformKey extends string = string, TTextureKey extends string = string, TDefineKey extends string = string, TIncludeKey extends string = string>(material: FragMaterial<TUniformKey, TTextureKey, TDefineKey, TIncludeKey>): ResolvedMaterial<TUniformKey, TTextureKey, TIncludeKey>;
|
package/dist/core/material.js
CHANGED
|
@@ -6,12 +6,17 @@ import { normalizeDefines, normalizeIncludes, preprocessMaterialFragment, toDefi
|
|
|
6
6
|
*/
|
|
7
7
|
const FRAGMENT_FUNCTION_SIGNATURE_PATTERN = /\bfn\s+frag\s*\(\s*([^)]*?)\s*\)\s*->\s*([A-Za-z_][A-Za-z0-9_<>\s]*)\s*(?:\{|$)/m;
|
|
8
8
|
const FRAGMENT_FUNCTION_NAME_PATTERN = /\bfn\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(/g;
|
|
9
|
-
/**
|
|
10
|
-
* Cache of resolved material snapshots keyed by immutable material instance.
|
|
11
|
-
*/
|
|
12
9
|
const resolvedMaterialCache = new WeakMap();
|
|
13
10
|
const preprocessedFragmentCache = new WeakMap();
|
|
14
11
|
const materialSourceMetadataCache = new WeakMap();
|
|
12
|
+
function getCachedResolvedMaterial(material) {
|
|
13
|
+
const cached = resolvedMaterialCache.get(material);
|
|
14
|
+
if (!cached) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
// Invariant: the cache key is the same material object used to produce this resolved payload.
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
15
20
|
const STACK_TRACE_CHROME_PATTERN = /^\s*at\s+(?:(.*?)\s+\()?(.+?):(\d+):(\d+)\)?$/;
|
|
16
21
|
const STACK_TRACE_FIREFOX_PATTERN = /^(.*?)@(.+?):(\d+):(\d+)$/;
|
|
17
22
|
function getPathBasename(path) {
|
|
@@ -204,9 +209,11 @@ function resolveTextures(textures) {
|
|
|
204
209
|
const resolved = {};
|
|
205
210
|
for (const [name, definition] of Object.entries(textures ?? {})) {
|
|
206
211
|
assertUniformName(name);
|
|
212
|
+
const source = definition?.source;
|
|
213
|
+
const normalizedSource = cloneTextureValue(source);
|
|
207
214
|
const clonedDefinition = {
|
|
208
215
|
...(definition ?? {}),
|
|
209
|
-
source:
|
|
216
|
+
...(source !== undefined ? { source: normalizedSource } : {})
|
|
210
217
|
};
|
|
211
218
|
resolved[name] = Object.freeze(clonedDefinition);
|
|
212
219
|
}
|
|
@@ -296,8 +303,8 @@ export function defineMaterial(input) {
|
|
|
296
303
|
const source = Object.freeze(resolveSourceMetadata(undefined));
|
|
297
304
|
const preprocessed = preprocessMaterialFragment({
|
|
298
305
|
fragment,
|
|
299
|
-
defines
|
|
300
|
-
includes
|
|
306
|
+
defines,
|
|
307
|
+
includes
|
|
301
308
|
});
|
|
302
309
|
const material = Object.freeze({
|
|
303
310
|
fragment,
|
|
@@ -318,7 +325,7 @@ export function defineMaterial(input) {
|
|
|
318
325
|
*/
|
|
319
326
|
export function resolveMaterial(material) {
|
|
320
327
|
assertDefinedMaterial(material);
|
|
321
|
-
const cached =
|
|
328
|
+
const cached = getCachedResolvedMaterial(material);
|
|
322
329
|
if (cached) {
|
|
323
330
|
return cached;
|
|
324
331
|
}
|
package/dist/core/types.d.ts
CHANGED
|
@@ -11,7 +11,21 @@ export type UniformType = 'f32' | 'vec2f' | 'vec3f' | 'vec4f' | 'mat4x4f';
|
|
|
11
11
|
* @typeParam TType - WGSL type tag.
|
|
12
12
|
* @typeParam TValue - Runtime value shape for the selected type.
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Accepted matrix value formats for `mat4x4f` uniforms.
|
|
16
|
+
*/
|
|
17
|
+
export type UniformMat4Value = number[] | Float32Array;
|
|
18
|
+
/**
|
|
19
|
+
* Runtime value shape by WGSL uniform type tag.
|
|
20
|
+
*/
|
|
21
|
+
export interface UniformValueByType {
|
|
22
|
+
f32: number;
|
|
23
|
+
vec2f: [number, number];
|
|
24
|
+
vec3f: [number, number, number];
|
|
25
|
+
vec4f: [number, number, number, number];
|
|
26
|
+
mat4x4f: UniformMat4Value;
|
|
27
|
+
}
|
|
28
|
+
export interface TypedUniform<TType extends UniformType = UniformType, TValue extends UniformValueByType[TType] = UniformValueByType[TType]> {
|
|
15
29
|
/**
|
|
16
30
|
* WGSL type tag.
|
|
17
31
|
*/
|
|
@@ -21,18 +35,14 @@ export interface TypedUniform<TType extends UniformType = UniformType, TValue =
|
|
|
21
35
|
*/
|
|
22
36
|
value: TValue;
|
|
23
37
|
}
|
|
24
|
-
/**
|
|
25
|
-
* Accepted matrix value formats for `mat4x4f` uniforms.
|
|
26
|
-
*/
|
|
27
|
-
export type UniformMat4Value = number[] | Float32Array;
|
|
28
38
|
/**
|
|
29
39
|
* Supported uniform input shapes accepted by material and render APIs.
|
|
30
40
|
*/
|
|
31
|
-
export type UniformValue = number | [number, number] | [number, number, number] | [number, number, number, number] | TypedUniform<'f32'
|
|
41
|
+
export type UniformValue = number | [number, number] | [number, number, number] | [number, number, number, number] | TypedUniform<'f32'> | TypedUniform<'vec2f'> | TypedUniform<'vec3f'> | TypedUniform<'vec4f'> | TypedUniform<'mat4x4f'>;
|
|
32
42
|
/**
|
|
33
43
|
* Uniform map keyed by WGSL identifier names.
|
|
34
44
|
*/
|
|
35
|
-
export type UniformMap = Record<
|
|
45
|
+
export type UniformMap<TKey extends string = string> = Record<TKey, UniformValue>;
|
|
36
46
|
/**
|
|
37
47
|
* Resolved layout metadata for a single uniform field inside the packed uniform buffer.
|
|
38
48
|
*/
|
|
@@ -168,11 +178,11 @@ export interface TextureDefinition {
|
|
|
168
178
|
/**
|
|
169
179
|
* Texture definition map keyed by uniform-compatible texture names.
|
|
170
180
|
*/
|
|
171
|
-
export type TextureDefinitionMap = Record<
|
|
181
|
+
export type TextureDefinitionMap<TKey extends string = string> = Record<TKey, TextureDefinition>;
|
|
172
182
|
/**
|
|
173
183
|
* Runtime texture value map keyed by texture uniform names.
|
|
174
184
|
*/
|
|
175
|
-
export type TextureMap = Record<
|
|
185
|
+
export type TextureMap<TKey extends string = string> = Record<TKey, TextureValue>;
|
|
176
186
|
/**
|
|
177
187
|
* Output color space requested for final canvas presentation.
|
|
178
188
|
*/
|
|
@@ -226,7 +236,7 @@ export interface RenderTarget {
|
|
|
226
236
|
/**
|
|
227
237
|
* Named render target definitions keyed by output slot names.
|
|
228
238
|
*/
|
|
229
|
-
export type RenderTargetDefinitionMap = Record<
|
|
239
|
+
export type RenderTargetDefinitionMap<TKey extends string = string> = Record<TKey, RenderTargetDefinition>;
|
|
230
240
|
/**
|
|
231
241
|
* User-defined render slot name (mapped to `renderTargets` keys).
|
|
232
242
|
*/
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type MotionGPUErrorReport } from '../core/error-report.js';
|
|
2
|
+
import type { FragMaterial } from '../core/material.js';
|
|
3
|
+
import type { OutputColorSpace, RenderPass, RenderMode, RenderTargetDefinitionMap } from '../core/types.js';
|
|
4
|
+
import { type CSSProperties, type ReactNode } from 'react';
|
|
5
|
+
export interface FragCanvasProps {
|
|
6
|
+
material: FragMaterial;
|
|
7
|
+
renderTargets?: RenderTargetDefinitionMap;
|
|
8
|
+
passes?: RenderPass[];
|
|
9
|
+
clearColor?: [number, number, number, number];
|
|
10
|
+
outputColorSpace?: OutputColorSpace;
|
|
11
|
+
renderMode?: RenderMode;
|
|
12
|
+
autoRender?: boolean;
|
|
13
|
+
maxDelta?: number;
|
|
14
|
+
adapterOptions?: GPURequestAdapterOptions;
|
|
15
|
+
deviceDescriptor?: GPUDeviceDescriptor;
|
|
16
|
+
dpr?: number;
|
|
17
|
+
showErrorOverlay?: boolean;
|
|
18
|
+
errorRenderer?: (report: MotionGPUErrorReport) => ReactNode;
|
|
19
|
+
onError?: (report: MotionGPUErrorReport) => void;
|
|
20
|
+
errorHistoryLimit?: number;
|
|
21
|
+
onErrorHistory?: (history: MotionGPUErrorReport[]) => void;
|
|
22
|
+
className?: string;
|
|
23
|
+
style?: CSSProperties;
|
|
24
|
+
children?: ReactNode;
|
|
25
|
+
}
|
|
26
|
+
export declare function FragCanvas({ material, renderTargets, passes, clearColor, outputColorSpace, renderMode, autoRender, maxDelta, adapterOptions, deviceDescriptor, dpr, showErrorOverlay, errorRenderer, onError, errorHistoryLimit, onErrorHistory, className, style, children }: FragCanvasProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createCurrentWritable as currentWritable } from '../core/current-value.js';
|
|
3
|
+
import { toMotionGPUErrorReport } from '../core/error-report.js';
|
|
4
|
+
import { createFrameRegistry } from '../core/frame-registry.js';
|
|
5
|
+
import { createMotionGPURuntimeLoop } from '../core/runtime-loop.js';
|
|
6
|
+
import { useEffect, useRef, useState } from 'react';
|
|
7
|
+
import { FrameRegistryReactContext } from './frame-context.js';
|
|
8
|
+
import { MotionGPUErrorOverlay } from './MotionGPUErrorOverlay.js';
|
|
9
|
+
import { MotionGPUReactContext } from './motiongpu-context.js';
|
|
10
|
+
function getInitialDpr() {
|
|
11
|
+
if (typeof window === 'undefined') {
|
|
12
|
+
return 1;
|
|
13
|
+
}
|
|
14
|
+
return window.devicePixelRatio ?? 1;
|
|
15
|
+
}
|
|
16
|
+
function createRuntimeState(initialDpr) {
|
|
17
|
+
const registry = createFrameRegistry({ maxDelta: 0.1 });
|
|
18
|
+
const canvasRef = { current: undefined };
|
|
19
|
+
const requestFrameSignalRef = { current: null };
|
|
20
|
+
const requestFrame = () => {
|
|
21
|
+
requestFrameSignalRef.current?.();
|
|
22
|
+
};
|
|
23
|
+
const invalidateFrame = () => {
|
|
24
|
+
registry.invalidate();
|
|
25
|
+
requestFrame();
|
|
26
|
+
};
|
|
27
|
+
const advanceFrame = () => {
|
|
28
|
+
registry.advance();
|
|
29
|
+
requestFrame();
|
|
30
|
+
};
|
|
31
|
+
const size = currentWritable({ width: 0, height: 0 });
|
|
32
|
+
const dprState = currentWritable(initialDpr, requestFrame);
|
|
33
|
+
const maxDeltaState = currentWritable(0.1, (value) => {
|
|
34
|
+
registry.setMaxDelta(value);
|
|
35
|
+
requestFrame();
|
|
36
|
+
});
|
|
37
|
+
const renderModeState = currentWritable('always', (value) => {
|
|
38
|
+
registry.setRenderMode(value);
|
|
39
|
+
requestFrame();
|
|
40
|
+
});
|
|
41
|
+
const autoRenderState = currentWritable(true, (value) => {
|
|
42
|
+
registry.setAutoRender(value);
|
|
43
|
+
requestFrame();
|
|
44
|
+
});
|
|
45
|
+
const userState = currentWritable({});
|
|
46
|
+
const context = {
|
|
47
|
+
get canvas() {
|
|
48
|
+
return canvasRef.current;
|
|
49
|
+
},
|
|
50
|
+
size,
|
|
51
|
+
dpr: dprState,
|
|
52
|
+
maxDelta: maxDeltaState,
|
|
53
|
+
renderMode: renderModeState,
|
|
54
|
+
autoRender: autoRenderState,
|
|
55
|
+
user: userState,
|
|
56
|
+
invalidate: invalidateFrame,
|
|
57
|
+
advance: advanceFrame,
|
|
58
|
+
scheduler: {
|
|
59
|
+
createStage: registry.createStage,
|
|
60
|
+
getStage: registry.getStage,
|
|
61
|
+
setDiagnosticsEnabled: registry.setDiagnosticsEnabled,
|
|
62
|
+
getDiagnosticsEnabled: registry.getDiagnosticsEnabled,
|
|
63
|
+
getLastRunTimings: registry.getLastRunTimings,
|
|
64
|
+
getSchedule: registry.getSchedule,
|
|
65
|
+
setProfilingEnabled: registry.setProfilingEnabled,
|
|
66
|
+
setProfilingWindow: registry.setProfilingWindow,
|
|
67
|
+
resetProfiling: registry.resetProfiling,
|
|
68
|
+
getProfilingEnabled: registry.getProfilingEnabled,
|
|
69
|
+
getProfilingWindow: registry.getProfilingWindow,
|
|
70
|
+
getProfilingSnapshot: registry.getProfilingSnapshot
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
registry,
|
|
75
|
+
context,
|
|
76
|
+
canvasRef,
|
|
77
|
+
size,
|
|
78
|
+
dprState,
|
|
79
|
+
maxDeltaState,
|
|
80
|
+
renderModeState,
|
|
81
|
+
autoRenderState,
|
|
82
|
+
requestFrameSignalRef,
|
|
83
|
+
requestFrame,
|
|
84
|
+
invalidateFrame,
|
|
85
|
+
advanceFrame
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function getNormalizedErrorHistoryLimit(value) {
|
|
89
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
return Math.floor(value);
|
|
93
|
+
}
|
|
94
|
+
export function FragCanvas({ material, renderTargets = {}, passes = [], clearColor = [0, 0, 0, 1], outputColorSpace = 'srgb', renderMode = 'always', autoRender = true, maxDelta = 0.1, adapterOptions = undefined, deviceDescriptor = undefined, dpr = getInitialDpr(), showErrorOverlay = true, errorRenderer, onError = undefined, errorHistoryLimit = 0, onErrorHistory = undefined, className = '', style, children }) {
|
|
95
|
+
const runtimeRef = useRef(null);
|
|
96
|
+
if (!runtimeRef.current) {
|
|
97
|
+
runtimeRef.current = createRuntimeState(getInitialDpr());
|
|
98
|
+
}
|
|
99
|
+
const runtime = runtimeRef.current;
|
|
100
|
+
const runtimePropsRef = useRef({
|
|
101
|
+
material,
|
|
102
|
+
renderTargets,
|
|
103
|
+
passes,
|
|
104
|
+
clearColor,
|
|
105
|
+
outputColorSpace,
|
|
106
|
+
adapterOptions,
|
|
107
|
+
deviceDescriptor,
|
|
108
|
+
onError,
|
|
109
|
+
errorHistoryLimit,
|
|
110
|
+
onErrorHistory
|
|
111
|
+
});
|
|
112
|
+
runtimePropsRef.current = {
|
|
113
|
+
material,
|
|
114
|
+
renderTargets,
|
|
115
|
+
passes,
|
|
116
|
+
clearColor,
|
|
117
|
+
outputColorSpace,
|
|
118
|
+
adapterOptions,
|
|
119
|
+
deviceDescriptor,
|
|
120
|
+
onError,
|
|
121
|
+
errorHistoryLimit,
|
|
122
|
+
onErrorHistory
|
|
123
|
+
};
|
|
124
|
+
const [errorReport, setErrorReport] = useState(null);
|
|
125
|
+
const [errorHistory, setErrorHistory] = useState([]);
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
runtime.renderModeState.set(renderMode);
|
|
128
|
+
}, [renderMode, runtime]);
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
runtime.autoRenderState.set(autoRender);
|
|
131
|
+
}, [autoRender, runtime]);
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
runtime.maxDeltaState.set(maxDelta);
|
|
134
|
+
}, [maxDelta, runtime]);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
runtime.dprState.set(dpr);
|
|
137
|
+
}, [dpr, runtime]);
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
const limit = getNormalizedErrorHistoryLimit(errorHistoryLimit);
|
|
140
|
+
if (limit <= 0) {
|
|
141
|
+
if (errorHistory.length === 0) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
setErrorHistory([]);
|
|
145
|
+
onErrorHistory?.([]);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (errorHistory.length <= limit) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const trimmed = errorHistory.slice(errorHistory.length - limit);
|
|
152
|
+
setErrorHistory(trimmed);
|
|
153
|
+
onErrorHistory?.(trimmed);
|
|
154
|
+
}, [errorHistory, errorHistoryLimit, onErrorHistory]);
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
const canvas = runtime.canvasRef.current;
|
|
157
|
+
if (!canvas) {
|
|
158
|
+
const report = toMotionGPUErrorReport(new Error('Canvas element is not available'), 'initialization');
|
|
159
|
+
setErrorReport(report);
|
|
160
|
+
const historyLimit = getNormalizedErrorHistoryLimit(runtimePropsRef.current.errorHistoryLimit);
|
|
161
|
+
if (historyLimit > 0) {
|
|
162
|
+
const nextHistory = [report].slice(-historyLimit);
|
|
163
|
+
setErrorHistory(nextHistory);
|
|
164
|
+
runtimePropsRef.current.onErrorHistory?.(nextHistory);
|
|
165
|
+
}
|
|
166
|
+
runtimePropsRef.current.onError?.(report);
|
|
167
|
+
return () => {
|
|
168
|
+
runtime.registry.clear();
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
const runtimeLoop = createMotionGPURuntimeLoop({
|
|
172
|
+
canvas,
|
|
173
|
+
registry: runtime.registry,
|
|
174
|
+
size: runtime.size,
|
|
175
|
+
dpr: runtime.dprState,
|
|
176
|
+
maxDelta: runtime.maxDeltaState,
|
|
177
|
+
getMaterial: () => runtimePropsRef.current.material,
|
|
178
|
+
getRenderTargets: () => runtimePropsRef.current.renderTargets,
|
|
179
|
+
getPasses: () => runtimePropsRef.current.passes,
|
|
180
|
+
getClearColor: () => runtimePropsRef.current.clearColor,
|
|
181
|
+
getOutputColorSpace: () => runtimePropsRef.current.outputColorSpace,
|
|
182
|
+
getAdapterOptions: () => runtimePropsRef.current.adapterOptions,
|
|
183
|
+
getDeviceDescriptor: () => runtimePropsRef.current.deviceDescriptor,
|
|
184
|
+
getOnError: () => runtimePropsRef.current.onError,
|
|
185
|
+
getErrorHistoryLimit: () => runtimePropsRef.current.errorHistoryLimit,
|
|
186
|
+
getOnErrorHistory: () => runtimePropsRef.current.onErrorHistory,
|
|
187
|
+
reportErrorHistory: (history) => {
|
|
188
|
+
setErrorHistory(history);
|
|
189
|
+
},
|
|
190
|
+
reportError: (report) => {
|
|
191
|
+
setErrorReport(report);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
runtime.requestFrameSignalRef.current = runtimeLoop.requestFrame;
|
|
195
|
+
return () => {
|
|
196
|
+
runtime.requestFrameSignalRef.current = null;
|
|
197
|
+
runtimeLoop.destroy();
|
|
198
|
+
};
|
|
199
|
+
}, [runtime]);
|
|
200
|
+
const canvasStyle = {
|
|
201
|
+
position: 'absolute',
|
|
202
|
+
inset: 0,
|
|
203
|
+
display: 'block',
|
|
204
|
+
width: '100%',
|
|
205
|
+
height: '100%',
|
|
206
|
+
...style
|
|
207
|
+
};
|
|
208
|
+
return (_jsx(FrameRegistryReactContext.Provider, { value: runtime.registry, children: _jsx(MotionGPUReactContext.Provider, { value: runtime.context, children: _jsxs("div", { className: "motiongpu-canvas-wrap", style: {
|
|
209
|
+
position: 'relative',
|
|
210
|
+
width: '100%',
|
|
211
|
+
height: '100%',
|
|
212
|
+
minWidth: 0,
|
|
213
|
+
minHeight: 0,
|
|
214
|
+
overflow: 'hidden'
|
|
215
|
+
}, children: [_jsx("canvas", { className: className, style: canvasStyle, ref: (node) => {
|
|
216
|
+
runtime.canvasRef.current = node ?? undefined;
|
|
217
|
+
} }), showErrorOverlay && errorReport ? (errorRenderer ? (errorRenderer(errorReport)) : (_jsx(MotionGPUErrorOverlay, { report: errorReport }))) : null, children] }) }) }));
|
|
218
|
+
}
|