@motion-core/motion-gpu 0.1.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/LICENSE +21 -0
- package/README.md +325 -0
- package/dist/FragCanvas.svelte +511 -0
- package/dist/FragCanvas.svelte.d.ts +26 -0
- package/dist/MotionGPUErrorOverlay.svelte +394 -0
- package/dist/MotionGPUErrorOverlay.svelte.d.ts +7 -0
- package/dist/Portal.svelte +46 -0
- package/dist/Portal.svelte.d.ts +8 -0
- package/dist/advanced-scheduler.d.ts +44 -0
- package/dist/advanced-scheduler.js +58 -0
- package/dist/advanced.d.ts +14 -0
- package/dist/advanced.js +9 -0
- package/dist/core/error-diagnostics.d.ts +40 -0
- package/dist/core/error-diagnostics.js +111 -0
- package/dist/core/error-report.d.ts +67 -0
- package/dist/core/error-report.js +190 -0
- package/dist/core/material-preprocess.d.ts +63 -0
- package/dist/core/material-preprocess.js +166 -0
- package/dist/core/material.d.ts +157 -0
- package/dist/core/material.js +358 -0
- package/dist/core/recompile-policy.d.ts +27 -0
- package/dist/core/recompile-policy.js +15 -0
- package/dist/core/render-graph.d.ts +55 -0
- package/dist/core/render-graph.js +73 -0
- package/dist/core/render-targets.d.ts +39 -0
- package/dist/core/render-targets.js +63 -0
- package/dist/core/renderer.d.ts +9 -0
- package/dist/core/renderer.js +1097 -0
- package/dist/core/shader.d.ts +42 -0
- package/dist/core/shader.js +196 -0
- package/dist/core/texture-loader.d.ts +129 -0
- package/dist/core/texture-loader.js +295 -0
- package/dist/core/textures.d.ts +114 -0
- package/dist/core/textures.js +136 -0
- package/dist/core/types.d.ts +523 -0
- package/dist/core/types.js +4 -0
- package/dist/core/uniforms.d.ts +48 -0
- package/dist/core/uniforms.js +222 -0
- package/dist/current-writable.d.ts +31 -0
- package/dist/current-writable.js +27 -0
- package/dist/frame-context.d.ts +287 -0
- package/dist/frame-context.js +731 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +11 -0
- package/dist/motiongpu-context.d.ts +77 -0
- package/dist/motiongpu-context.js +26 -0
- package/dist/passes/BlitPass.d.ts +32 -0
- package/dist/passes/BlitPass.js +158 -0
- package/dist/passes/CopyPass.d.ts +25 -0
- package/dist/passes/CopyPass.js +53 -0
- package/dist/passes/ShaderPass.d.ts +40 -0
- package/dist/passes/ShaderPass.js +182 -0
- package/dist/passes/index.d.ts +3 -0
- package/dist/passes/index.js +3 -0
- package/dist/use-motiongpu-user-context.d.ts +35 -0
- package/dist/use-motiongpu-user-context.js +74 -0
- package/dist/use-texture.d.ts +35 -0
- package/dist/use-texture.js +147 -0
- package/package.json +94 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { MaterialSourceMetadata } from './error-diagnostics';
|
|
2
|
+
import { resolveUniformLayout } from './uniforms';
|
|
3
|
+
import { type MaterialLineMap } from './material-preprocess';
|
|
4
|
+
import type { TextureDefinitionMap, UniformMap } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* Typed compile-time define declaration.
|
|
7
|
+
*/
|
|
8
|
+
export interface TypedMaterialDefineValue {
|
|
9
|
+
/**
|
|
10
|
+
* WGSL scalar type.
|
|
11
|
+
*/
|
|
12
|
+
type: 'bool' | 'f32' | 'i32' | 'u32';
|
|
13
|
+
/**
|
|
14
|
+
* Literal value for the selected WGSL type.
|
|
15
|
+
*/
|
|
16
|
+
value: boolean | number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Allowed value types for WGSL `const` define injection.
|
|
20
|
+
*/
|
|
21
|
+
export type MaterialDefineValue = boolean | number | TypedMaterialDefineValue;
|
|
22
|
+
/**
|
|
23
|
+
* Define map keyed by uniform-compatible identifier names.
|
|
24
|
+
*/
|
|
25
|
+
export type MaterialDefines = Record<string, MaterialDefineValue>;
|
|
26
|
+
/**
|
|
27
|
+
* Include map keyed by include identifier used in `#include <name>` directives.
|
|
28
|
+
*/
|
|
29
|
+
export type MaterialIncludes = Record<string, string>;
|
|
30
|
+
/**
|
|
31
|
+
* External material input accepted by {@link defineMaterial}.
|
|
32
|
+
*/
|
|
33
|
+
export interface FragMaterialInput {
|
|
34
|
+
/**
|
|
35
|
+
* User WGSL source containing `frag(uv: vec2f) -> vec4f`.
|
|
36
|
+
*/
|
|
37
|
+
fragment: string;
|
|
38
|
+
/**
|
|
39
|
+
* Initial uniform values.
|
|
40
|
+
*/
|
|
41
|
+
uniforms?: UniformMap;
|
|
42
|
+
/**
|
|
43
|
+
* Texture definitions keyed by texture uniform name.
|
|
44
|
+
*/
|
|
45
|
+
textures?: TextureDefinitionMap;
|
|
46
|
+
/**
|
|
47
|
+
* Optional compile-time define constants injected into WGSL.
|
|
48
|
+
*/
|
|
49
|
+
defines?: MaterialDefines;
|
|
50
|
+
/**
|
|
51
|
+
* Optional WGSL include chunks used by `#include <name>` directives.
|
|
52
|
+
*/
|
|
53
|
+
includes?: MaterialIncludes;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Normalized and immutable material declaration consumed by `FragCanvas`.
|
|
57
|
+
*/
|
|
58
|
+
export interface FragMaterial {
|
|
59
|
+
/**
|
|
60
|
+
* User WGSL source containing `frag(uv: vec2f) -> vec4f`.
|
|
61
|
+
*/
|
|
62
|
+
readonly fragment: string;
|
|
63
|
+
/**
|
|
64
|
+
* Initial uniform values.
|
|
65
|
+
*/
|
|
66
|
+
readonly uniforms: Readonly<UniformMap>;
|
|
67
|
+
/**
|
|
68
|
+
* Texture definitions keyed by texture uniform name.
|
|
69
|
+
*/
|
|
70
|
+
readonly textures: Readonly<TextureDefinitionMap>;
|
|
71
|
+
/**
|
|
72
|
+
* Optional compile-time define constants injected into WGSL.
|
|
73
|
+
*/
|
|
74
|
+
readonly defines: Readonly<MaterialDefines>;
|
|
75
|
+
/**
|
|
76
|
+
* Optional WGSL include chunks used by `#include <name>` directives.
|
|
77
|
+
*/
|
|
78
|
+
readonly includes: Readonly<MaterialIncludes>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Fully resolved, immutable material snapshot used for renderer creation/caching.
|
|
82
|
+
*/
|
|
83
|
+
export interface ResolvedMaterial {
|
|
84
|
+
/**
|
|
85
|
+
* Final fragment WGSL after define injection.
|
|
86
|
+
*/
|
|
87
|
+
fragmentWgsl: string;
|
|
88
|
+
/**
|
|
89
|
+
* 1-based map from generated fragment lines to user source lines.
|
|
90
|
+
*/
|
|
91
|
+
fragmentLineMap: MaterialLineMap;
|
|
92
|
+
/**
|
|
93
|
+
* Cloned uniforms.
|
|
94
|
+
*/
|
|
95
|
+
uniforms: UniformMap;
|
|
96
|
+
/**
|
|
97
|
+
* Cloned texture definitions.
|
|
98
|
+
*/
|
|
99
|
+
textures: TextureDefinitionMap;
|
|
100
|
+
/**
|
|
101
|
+
* Resolved packed uniform layout.
|
|
102
|
+
*/
|
|
103
|
+
uniformLayout: ReturnType<typeof resolveUniformLayout>;
|
|
104
|
+
/**
|
|
105
|
+
* Sorted texture keys.
|
|
106
|
+
*/
|
|
107
|
+
textureKeys: string[];
|
|
108
|
+
/**
|
|
109
|
+
* Deterministic JSON signature for cache invalidation.
|
|
110
|
+
*/
|
|
111
|
+
signature: string;
|
|
112
|
+
/**
|
|
113
|
+
* Original user fragment source before preprocessing.
|
|
114
|
+
*/
|
|
115
|
+
fragmentSource: string;
|
|
116
|
+
/**
|
|
117
|
+
* Normalized include sources map.
|
|
118
|
+
*/
|
|
119
|
+
includeSources: MaterialIncludes;
|
|
120
|
+
/**
|
|
121
|
+
* Deterministic define block source used for diagnostics mapping.
|
|
122
|
+
*/
|
|
123
|
+
defineBlockSource: string;
|
|
124
|
+
/**
|
|
125
|
+
* Source metadata used for diagnostics.
|
|
126
|
+
*/
|
|
127
|
+
source: Readonly<MaterialSourceMetadata> | null;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Creates a stable WGSL define block from the provided map.
|
|
131
|
+
*
|
|
132
|
+
* @param defines - Optional material defines.
|
|
133
|
+
* @returns Joined WGSL const declarations ordered by key.
|
|
134
|
+
*/
|
|
135
|
+
export declare function buildDefinesBlock(defines: MaterialDefines | undefined): string;
|
|
136
|
+
/**
|
|
137
|
+
* Prepends resolved defines to a fragment shader.
|
|
138
|
+
*
|
|
139
|
+
* @param fragment - Raw WGSL fragment source.
|
|
140
|
+
* @param defines - Optional define map.
|
|
141
|
+
* @returns Fragment source with a leading define block when defines are present.
|
|
142
|
+
*/
|
|
143
|
+
export declare function applyMaterialDefines(fragment: string, defines: MaterialDefines | undefined): string;
|
|
144
|
+
/**
|
|
145
|
+
* Creates an immutable material object with validated shader/uniform/texture contracts.
|
|
146
|
+
*
|
|
147
|
+
* @param input - User material declaration.
|
|
148
|
+
* @returns Frozen material object safe to share and cache.
|
|
149
|
+
*/
|
|
150
|
+
export declare function defineMaterial(input: FragMaterialInput): FragMaterial;
|
|
151
|
+
/**
|
|
152
|
+
* Resolves a material to renderer-ready data and a deterministic signature.
|
|
153
|
+
*
|
|
154
|
+
* @param material - Material input created via {@link defineMaterial}.
|
|
155
|
+
* @returns Resolved material with packed uniform layout, sorted texture keys and cache signature.
|
|
156
|
+
*/
|
|
157
|
+
export declare function resolveMaterial(material: FragMaterial): ResolvedMaterial;
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { normalizeTextureDefinition } from './textures';
|
|
2
|
+
import { assertUniformName, assertUniformValueForType, inferUniformType, resolveUniformLayout } from './uniforms';
|
|
3
|
+
import { normalizeDefines, normalizeIncludes, preprocessMaterialFragment, toDefineLine } from './material-preprocess';
|
|
4
|
+
/**
|
|
5
|
+
* Strict fragment contract used by MotionGPU.
|
|
6
|
+
*/
|
|
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
|
+
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
|
+
const resolvedMaterialCache = new WeakMap();
|
|
13
|
+
const preprocessedFragmentCache = new WeakMap();
|
|
14
|
+
const materialSourceMetadataCache = new WeakMap();
|
|
15
|
+
const STACK_TRACE_CHROME_PATTERN = /^\s*at\s+(?:(.*?)\s+\()?(.+?):(\d+):(\d+)\)?$/;
|
|
16
|
+
const STACK_TRACE_FIREFOX_PATTERN = /^(.*?)@(.+?):(\d+):(\d+)$/;
|
|
17
|
+
function getPathBasename(path) {
|
|
18
|
+
const normalized = path.split(/[?#]/)[0] ?? path;
|
|
19
|
+
const parts = normalized.split(/[\\/]/);
|
|
20
|
+
const last = parts[parts.length - 1];
|
|
21
|
+
return last && last.length > 0 ? last : path;
|
|
22
|
+
}
|
|
23
|
+
function normalizeSignaturePart(value) {
|
|
24
|
+
return value.replace(/\s+/g, ' ').trim();
|
|
25
|
+
}
|
|
26
|
+
function listFunctionNames(fragment) {
|
|
27
|
+
const names = new Set();
|
|
28
|
+
for (const match of fragment.matchAll(FRAGMENT_FUNCTION_NAME_PATTERN)) {
|
|
29
|
+
const name = match[1];
|
|
30
|
+
if (!name) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
names.add(name);
|
|
34
|
+
}
|
|
35
|
+
return Array.from(names);
|
|
36
|
+
}
|
|
37
|
+
function captureMaterialSourceFromStack() {
|
|
38
|
+
const stack = new Error().stack;
|
|
39
|
+
if (!stack) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const stackLines = stack.split('\n').slice(1);
|
|
43
|
+
for (const rawLine of stackLines) {
|
|
44
|
+
const line = rawLine.trim();
|
|
45
|
+
if (line.length === 0) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const chromeMatch = line.match(STACK_TRACE_CHROME_PATTERN);
|
|
49
|
+
const firefoxMatch = line.match(STACK_TRACE_FIREFOX_PATTERN);
|
|
50
|
+
const functionName = chromeMatch?.[1] ?? firefoxMatch?.[1] ?? undefined;
|
|
51
|
+
const file = chromeMatch?.[2] ?? firefoxMatch?.[2];
|
|
52
|
+
const lineValue = chromeMatch?.[3] ?? firefoxMatch?.[3];
|
|
53
|
+
const columnValue = chromeMatch?.[4] ?? firefoxMatch?.[4];
|
|
54
|
+
if (!file || !lineValue || !columnValue) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (file.includes('/core/material') || file.includes('\\core\\material')) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const parsedLine = Number.parseInt(lineValue, 10);
|
|
61
|
+
const parsedColumn = Number.parseInt(columnValue, 10);
|
|
62
|
+
if (!Number.isFinite(parsedLine) || !Number.isFinite(parsedColumn)) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
component: getPathBasename(file),
|
|
67
|
+
file,
|
|
68
|
+
line: parsedLine,
|
|
69
|
+
column: parsedColumn,
|
|
70
|
+
...(functionName ? { functionName } : {})
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
function resolveSourceMetadata(source) {
|
|
76
|
+
const captured = captureMaterialSourceFromStack();
|
|
77
|
+
const component = source?.component ?? captured?.component;
|
|
78
|
+
const file = source?.file ?? captured?.file;
|
|
79
|
+
const line = source?.line ?? captured?.line;
|
|
80
|
+
const column = source?.column ?? captured?.column;
|
|
81
|
+
const functionName = source?.functionName ?? captured?.functionName;
|
|
82
|
+
if (component === undefined &&
|
|
83
|
+
file === undefined &&
|
|
84
|
+
line === undefined &&
|
|
85
|
+
column === undefined &&
|
|
86
|
+
functionName === undefined) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
...(component !== undefined ? { component } : {}),
|
|
91
|
+
...(file !== undefined ? { file } : {}),
|
|
92
|
+
...(line !== undefined ? { line } : {}),
|
|
93
|
+
...(column !== undefined ? { column } : {}),
|
|
94
|
+
...(functionName !== undefined ? { functionName } : {})
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Asserts that material has been normalized by {@link defineMaterial}.
|
|
99
|
+
*/
|
|
100
|
+
function assertDefinedMaterial(material) {
|
|
101
|
+
if (!Object.isFrozen(material) ||
|
|
102
|
+
!material.uniforms ||
|
|
103
|
+
!material.textures ||
|
|
104
|
+
!material.defines ||
|
|
105
|
+
!material.includes) {
|
|
106
|
+
throw new Error('Invalid material instance. Create materials with defineMaterial(...) before passing them to <FragCanvas>.');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Clones uniform value input to decouple material instances from external objects.
|
|
111
|
+
*/
|
|
112
|
+
function cloneUniformValue(value) {
|
|
113
|
+
if (typeof value === 'number') {
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
if (Array.isArray(value)) {
|
|
117
|
+
return Object.freeze([...value]);
|
|
118
|
+
}
|
|
119
|
+
if (typeof value === 'object' && value !== null && 'type' in value && 'value' in value) {
|
|
120
|
+
const typed = value;
|
|
121
|
+
const typedValue = typed.value;
|
|
122
|
+
let clonedTypedValue = typedValue;
|
|
123
|
+
if (typedValue instanceof Float32Array) {
|
|
124
|
+
clonedTypedValue = new Float32Array(typedValue);
|
|
125
|
+
}
|
|
126
|
+
else if (Array.isArray(typedValue)) {
|
|
127
|
+
clonedTypedValue = Object.freeze([...typedValue]);
|
|
128
|
+
}
|
|
129
|
+
return Object.freeze({
|
|
130
|
+
type: typed.type,
|
|
131
|
+
value: clonedTypedValue
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Clones optional texture value payload.
|
|
138
|
+
*/
|
|
139
|
+
function cloneTextureValue(value) {
|
|
140
|
+
if (value === undefined || value === null) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
if (typeof value === 'object' && 'source' in value) {
|
|
144
|
+
const data = value;
|
|
145
|
+
return {
|
|
146
|
+
source: data.source,
|
|
147
|
+
...(data.width !== undefined ? { width: data.width } : {}),
|
|
148
|
+
...(data.height !== undefined ? { height: data.height } : {}),
|
|
149
|
+
...(data.colorSpace !== undefined ? { colorSpace: data.colorSpace } : {}),
|
|
150
|
+
...(data.flipY !== undefined ? { flipY: data.flipY } : {}),
|
|
151
|
+
...(data.premultipliedAlpha !== undefined
|
|
152
|
+
? { premultipliedAlpha: data.premultipliedAlpha }
|
|
153
|
+
: {}),
|
|
154
|
+
...(data.generateMipmaps !== undefined ? { generateMipmaps: data.generateMipmaps } : {}),
|
|
155
|
+
...(data.update !== undefined ? { update: data.update } : {})
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Clones and validates fragment source contract.
|
|
162
|
+
*/
|
|
163
|
+
function resolveFragment(fragment) {
|
|
164
|
+
if (typeof fragment !== 'string' || fragment.trim().length === 0) {
|
|
165
|
+
throw new Error('Material fragment shader must be a non-empty WGSL string.');
|
|
166
|
+
}
|
|
167
|
+
const signature = fragment.match(FRAGMENT_FUNCTION_SIGNATURE_PATTERN);
|
|
168
|
+
if (!signature) {
|
|
169
|
+
const discoveredFunctions = listFunctionNames(fragment).slice(0, 4);
|
|
170
|
+
const discoveredLabel = discoveredFunctions.length > 0
|
|
171
|
+
? `Found: ${discoveredFunctions.map((name) => `\`${name}(...)\``).join(', ')}.`
|
|
172
|
+
: 'No WGSL function declarations were found.';
|
|
173
|
+
throw new Error(`Material fragment contract mismatch: missing entrypoint \`fn frag(uv: vec2f) -> vec4f\`. ${discoveredLabel}`);
|
|
174
|
+
}
|
|
175
|
+
const params = normalizeSignaturePart(signature[1] ?? '');
|
|
176
|
+
const returnType = normalizeSignaturePart(signature[2] ?? '');
|
|
177
|
+
if (params !== 'uv: vec2f') {
|
|
178
|
+
throw new Error(`Material fragment contract mismatch for \`frag\`: expected parameter list \`(uv: vec2f)\`, received \`(${params || '...'})\`.`);
|
|
179
|
+
}
|
|
180
|
+
if (returnType !== 'vec4f') {
|
|
181
|
+
throw new Error(`Material fragment contract mismatch for \`frag\`: expected return type \`vec4f\`, received \`${returnType}\`.`);
|
|
182
|
+
}
|
|
183
|
+
return fragment;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Clones and validates uniform declarations.
|
|
187
|
+
*/
|
|
188
|
+
function resolveUniforms(uniforms) {
|
|
189
|
+
const resolved = {};
|
|
190
|
+
for (const [name, value] of Object.entries(uniforms ?? {})) {
|
|
191
|
+
assertUniformName(name);
|
|
192
|
+
const clonedValue = cloneUniformValue(value);
|
|
193
|
+
const type = inferUniformType(clonedValue);
|
|
194
|
+
assertUniformValueForType(type, clonedValue);
|
|
195
|
+
resolved[name] = clonedValue;
|
|
196
|
+
}
|
|
197
|
+
resolveUniformLayout(resolved);
|
|
198
|
+
return resolved;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Clones and validates texture declarations.
|
|
202
|
+
*/
|
|
203
|
+
function resolveTextures(textures) {
|
|
204
|
+
const resolved = {};
|
|
205
|
+
for (const [name, definition] of Object.entries(textures ?? {})) {
|
|
206
|
+
assertUniformName(name);
|
|
207
|
+
const clonedDefinition = {
|
|
208
|
+
...(definition ?? {}),
|
|
209
|
+
source: cloneTextureValue(definition?.source)
|
|
210
|
+
};
|
|
211
|
+
resolved[name] = Object.freeze(clonedDefinition);
|
|
212
|
+
}
|
|
213
|
+
return resolved;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Clones and validates define declarations.
|
|
217
|
+
*/
|
|
218
|
+
function resolveDefines(defines) {
|
|
219
|
+
return normalizeDefines(defines);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Clones and validates include declarations.
|
|
223
|
+
*/
|
|
224
|
+
function resolveIncludes(includes) {
|
|
225
|
+
return normalizeIncludes(includes);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Builds a deterministic texture-config signature map used in material cache signatures.
|
|
229
|
+
*
|
|
230
|
+
* @param textures - Raw texture definitions from material input.
|
|
231
|
+
* @param textureKeys - Sorted texture keys.
|
|
232
|
+
* @returns Compact signature entries describing effective texture config per key.
|
|
233
|
+
*/
|
|
234
|
+
function buildTextureConfigSignature(textures, textureKeys) {
|
|
235
|
+
const signature = {};
|
|
236
|
+
for (const key of textureKeys) {
|
|
237
|
+
const normalized = normalizeTextureDefinition(textures[key]);
|
|
238
|
+
signature[key] = [
|
|
239
|
+
normalized.colorSpace,
|
|
240
|
+
normalized.flipY ? '1' : '0',
|
|
241
|
+
normalized.generateMipmaps ? '1' : '0',
|
|
242
|
+
normalized.premultipliedAlpha ? '1' : '0',
|
|
243
|
+
normalized.anisotropy,
|
|
244
|
+
normalized.filter,
|
|
245
|
+
normalized.addressModeU,
|
|
246
|
+
normalized.addressModeV
|
|
247
|
+
].join(':');
|
|
248
|
+
}
|
|
249
|
+
return signature;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Creates a stable WGSL define block from the provided map.
|
|
253
|
+
*
|
|
254
|
+
* @param defines - Optional material defines.
|
|
255
|
+
* @returns Joined WGSL const declarations ordered by key.
|
|
256
|
+
*/
|
|
257
|
+
export function buildDefinesBlock(defines) {
|
|
258
|
+
const normalizedDefines = normalizeDefines(defines);
|
|
259
|
+
if (Object.keys(normalizedDefines).length === 0) {
|
|
260
|
+
return '';
|
|
261
|
+
}
|
|
262
|
+
return Object.entries(normalizedDefines)
|
|
263
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
264
|
+
.map(([key, value]) => {
|
|
265
|
+
assertUniformName(key);
|
|
266
|
+
return toDefineLine(key, value);
|
|
267
|
+
})
|
|
268
|
+
.join('\n');
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Prepends resolved defines to a fragment shader.
|
|
272
|
+
*
|
|
273
|
+
* @param fragment - Raw WGSL fragment source.
|
|
274
|
+
* @param defines - Optional define map.
|
|
275
|
+
* @returns Fragment source with a leading define block when defines are present.
|
|
276
|
+
*/
|
|
277
|
+
export function applyMaterialDefines(fragment, defines) {
|
|
278
|
+
const defineBlock = buildDefinesBlock(defines);
|
|
279
|
+
if (defineBlock.length === 0) {
|
|
280
|
+
return fragment;
|
|
281
|
+
}
|
|
282
|
+
return `${defineBlock}\n\n${fragment}`;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Creates an immutable material object with validated shader/uniform/texture contracts.
|
|
286
|
+
*
|
|
287
|
+
* @param input - User material declaration.
|
|
288
|
+
* @returns Frozen material object safe to share and cache.
|
|
289
|
+
*/
|
|
290
|
+
export function defineMaterial(input) {
|
|
291
|
+
const fragment = resolveFragment(input.fragment);
|
|
292
|
+
const uniforms = Object.freeze(resolveUniforms(input.uniforms));
|
|
293
|
+
const textures = Object.freeze(resolveTextures(input.textures));
|
|
294
|
+
const defines = Object.freeze(resolveDefines(input.defines));
|
|
295
|
+
const includes = Object.freeze(resolveIncludes(input.includes));
|
|
296
|
+
const source = Object.freeze(resolveSourceMetadata(undefined));
|
|
297
|
+
const preprocessed = preprocessMaterialFragment({
|
|
298
|
+
fragment,
|
|
299
|
+
defines: defines,
|
|
300
|
+
includes: includes
|
|
301
|
+
});
|
|
302
|
+
const material = Object.freeze({
|
|
303
|
+
fragment,
|
|
304
|
+
uniforms,
|
|
305
|
+
textures,
|
|
306
|
+
defines,
|
|
307
|
+
includes
|
|
308
|
+
});
|
|
309
|
+
preprocessedFragmentCache.set(material, preprocessed);
|
|
310
|
+
materialSourceMetadataCache.set(material, source);
|
|
311
|
+
return material;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Resolves a material to renderer-ready data and a deterministic signature.
|
|
315
|
+
*
|
|
316
|
+
* @param material - Material input created via {@link defineMaterial}.
|
|
317
|
+
* @returns Resolved material with packed uniform layout, sorted texture keys and cache signature.
|
|
318
|
+
*/
|
|
319
|
+
export function resolveMaterial(material) {
|
|
320
|
+
assertDefinedMaterial(material);
|
|
321
|
+
const cached = resolvedMaterialCache.get(material);
|
|
322
|
+
if (cached) {
|
|
323
|
+
return cached;
|
|
324
|
+
}
|
|
325
|
+
const uniforms = material.uniforms;
|
|
326
|
+
const textures = material.textures;
|
|
327
|
+
const uniformLayout = resolveUniformLayout(uniforms);
|
|
328
|
+
const textureKeys = Object.keys(textures).sort();
|
|
329
|
+
const preprocessed = preprocessedFragmentCache.get(material) ??
|
|
330
|
+
preprocessMaterialFragment({
|
|
331
|
+
fragment: material.fragment,
|
|
332
|
+
defines: material.defines,
|
|
333
|
+
includes: material.includes
|
|
334
|
+
});
|
|
335
|
+
const fragmentWgsl = preprocessed.fragment;
|
|
336
|
+
const textureConfig = buildTextureConfigSignature(textures, textureKeys);
|
|
337
|
+
const signature = JSON.stringify({
|
|
338
|
+
fragmentWgsl,
|
|
339
|
+
uniforms: uniformLayout.entries.map((entry) => `${entry.name}:${entry.type}`),
|
|
340
|
+
textureKeys,
|
|
341
|
+
textureConfig
|
|
342
|
+
});
|
|
343
|
+
const resolved = {
|
|
344
|
+
fragmentWgsl,
|
|
345
|
+
fragmentLineMap: preprocessed.lineMap,
|
|
346
|
+
uniforms,
|
|
347
|
+
textures,
|
|
348
|
+
uniformLayout,
|
|
349
|
+
textureKeys,
|
|
350
|
+
signature,
|
|
351
|
+
fragmentSource: material.fragment,
|
|
352
|
+
includeSources: material.includes,
|
|
353
|
+
defineBlockSource: preprocessed.defineBlockSource,
|
|
354
|
+
source: materialSourceMetadataCache.get(material) ?? null
|
|
355
|
+
};
|
|
356
|
+
resolvedMaterialCache.set(material, resolved);
|
|
357
|
+
return resolved;
|
|
358
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { OutputColorSpace } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Inputs that affect renderer pipeline compilation.
|
|
4
|
+
*/
|
|
5
|
+
export interface RendererPipelineSignatureInput {
|
|
6
|
+
/**
|
|
7
|
+
* Material pipeline signature (fragment preprocess + uniform/texture layout).
|
|
8
|
+
*/
|
|
9
|
+
materialSignature: string;
|
|
10
|
+
/**
|
|
11
|
+
* Output color-space transform mode.
|
|
12
|
+
*/
|
|
13
|
+
outputColorSpace: OutputColorSpace;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Returns deterministic renderer pipeline signature.
|
|
17
|
+
*
|
|
18
|
+
* Rebuild triggers:
|
|
19
|
+
* - material signature changes (shader/layout related)
|
|
20
|
+
* - output color space changes
|
|
21
|
+
*
|
|
22
|
+
* Non-triggers:
|
|
23
|
+
* - runtime uniform values
|
|
24
|
+
* - runtime texture sources
|
|
25
|
+
* - clear color changes
|
|
26
|
+
*/
|
|
27
|
+
export declare function buildRendererPipelineSignature(input: RendererPipelineSignatureInput): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns deterministic renderer pipeline signature.
|
|
3
|
+
*
|
|
4
|
+
* Rebuild triggers:
|
|
5
|
+
* - material signature changes (shader/layout related)
|
|
6
|
+
* - output color space changes
|
|
7
|
+
*
|
|
8
|
+
* Non-triggers:
|
|
9
|
+
* - runtime uniform values
|
|
10
|
+
* - runtime texture sources
|
|
11
|
+
* - clear color changes
|
|
12
|
+
*/
|
|
13
|
+
export function buildRendererPipelineSignature(input) {
|
|
14
|
+
return `${input.materialSignature}|${input.outputColorSpace}`;
|
|
15
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { RenderPass, RenderPassInputSlot, RenderPassOutputSlot } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Resolved render-pass step with defaults applied.
|
|
4
|
+
*/
|
|
5
|
+
export interface RenderGraphStep {
|
|
6
|
+
/**
|
|
7
|
+
* User pass instance.
|
|
8
|
+
*/
|
|
9
|
+
pass: RenderPass;
|
|
10
|
+
/**
|
|
11
|
+
* Resolved input slot.
|
|
12
|
+
*/
|
|
13
|
+
input: RenderPassInputSlot;
|
|
14
|
+
/**
|
|
15
|
+
* Resolved output slot.
|
|
16
|
+
*/
|
|
17
|
+
output: RenderPassOutputSlot;
|
|
18
|
+
/**
|
|
19
|
+
* Whether ping-pong swap should be performed after render.
|
|
20
|
+
*/
|
|
21
|
+
needsSwap: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Whether pass should clear output before drawing.
|
|
24
|
+
*/
|
|
25
|
+
clear: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Effective clear color.
|
|
28
|
+
*/
|
|
29
|
+
clearColor: [number, number, number, number];
|
|
30
|
+
/**
|
|
31
|
+
* Whether output should be preserved after pass ends.
|
|
32
|
+
*/
|
|
33
|
+
preserve: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Immutable render-graph execution plan for one frame.
|
|
37
|
+
*/
|
|
38
|
+
export interface RenderGraphPlan {
|
|
39
|
+
/**
|
|
40
|
+
* Resolved enabled steps in execution order.
|
|
41
|
+
*/
|
|
42
|
+
steps: RenderGraphStep[];
|
|
43
|
+
/**
|
|
44
|
+
* Output slot holding final frame result before presentation.
|
|
45
|
+
*/
|
|
46
|
+
finalOutput: RenderPassOutputSlot;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Builds validated render graph plan from runtime pass list.
|
|
50
|
+
*
|
|
51
|
+
* @param passes - Runtime passes.
|
|
52
|
+
* @param defaultClearColor - Global clear color fallback.
|
|
53
|
+
* @returns Resolved render graph plan.
|
|
54
|
+
*/
|
|
55
|
+
export declare function planRenderGraph(passes: RenderPass[] | undefined, defaultClearColor: [number, number, number, number], renderTargetSlots?: Iterable<string>): RenderGraphPlan;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a copy of RGBA clear color.
|
|
3
|
+
*/
|
|
4
|
+
function cloneClearColor(color) {
|
|
5
|
+
return [color[0], color[1], color[2], color[3]];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Builds validated render graph plan from runtime pass list.
|
|
9
|
+
*
|
|
10
|
+
* @param passes - Runtime passes.
|
|
11
|
+
* @param defaultClearColor - Global clear color fallback.
|
|
12
|
+
* @returns Resolved render graph plan.
|
|
13
|
+
*/
|
|
14
|
+
export function planRenderGraph(passes, defaultClearColor, renderTargetSlots) {
|
|
15
|
+
const steps = [];
|
|
16
|
+
const declaredTargets = new Set(renderTargetSlots ?? []);
|
|
17
|
+
const availableSlots = new Set(['source']);
|
|
18
|
+
let finalOutput = 'canvas';
|
|
19
|
+
let enabledIndex = 0;
|
|
20
|
+
for (const pass of passes ?? []) {
|
|
21
|
+
if (pass.enabled === false) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const needsSwap = pass.needsSwap ?? true;
|
|
25
|
+
const input = pass.input ?? 'source';
|
|
26
|
+
const output = pass.output ?? (needsSwap ? 'target' : 'source');
|
|
27
|
+
if (input === 'canvas') {
|
|
28
|
+
throw new Error(`Render pass #${enabledIndex} cannot read from "canvas".`);
|
|
29
|
+
}
|
|
30
|
+
const inputIsNamed = input !== 'source' && input !== 'target';
|
|
31
|
+
if (inputIsNamed && !declaredTargets.has(input)) {
|
|
32
|
+
throw new Error(`Render pass #${enabledIndex} reads unknown target "${input}".`);
|
|
33
|
+
}
|
|
34
|
+
const outputIsNamed = output !== 'source' && output !== 'target' && output !== 'canvas';
|
|
35
|
+
if (outputIsNamed && !declaredTargets.has(output)) {
|
|
36
|
+
throw new Error(`Render pass #${enabledIndex} writes unknown target "${output}".`);
|
|
37
|
+
}
|
|
38
|
+
if (needsSwap && (input !== 'source' || output !== 'target')) {
|
|
39
|
+
throw new Error(`Render pass #${enabledIndex} uses needsSwap=true but does not follow source->target flow.`);
|
|
40
|
+
}
|
|
41
|
+
if (!availableSlots.has(input)) {
|
|
42
|
+
throw new Error(`Render pass #${enabledIndex} reads "${input}" before it is written.`);
|
|
43
|
+
}
|
|
44
|
+
const clear = pass.clear ?? false;
|
|
45
|
+
const clearColor = cloneClearColor(pass.clearColor ?? defaultClearColor);
|
|
46
|
+
const preserve = pass.preserve ?? true;
|
|
47
|
+
steps.push({
|
|
48
|
+
pass,
|
|
49
|
+
input,
|
|
50
|
+
output,
|
|
51
|
+
needsSwap,
|
|
52
|
+
clear,
|
|
53
|
+
clearColor,
|
|
54
|
+
preserve
|
|
55
|
+
});
|
|
56
|
+
if (needsSwap) {
|
|
57
|
+
availableSlots.add('target');
|
|
58
|
+
availableSlots.add('source');
|
|
59
|
+
finalOutput = 'source';
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
if (output !== 'canvas') {
|
|
63
|
+
availableSlots.add(output);
|
|
64
|
+
}
|
|
65
|
+
finalOutput = output;
|
|
66
|
+
}
|
|
67
|
+
enabledIndex += 1;
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
steps,
|
|
71
|
+
finalOutput
|
|
72
|
+
};
|
|
73
|
+
}
|