@motion-core/motion-gpu 0.4.0 → 0.4.2
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/dist/advanced.d.ts +1 -0
- package/dist/advanced.d.ts.map +1 -0
- package/dist/advanced.js +12 -6
- package/dist/core/advanced.d.ts +1 -0
- package/dist/core/advanced.d.ts.map +1 -0
- package/dist/core/advanced.js +12 -5
- package/dist/core/current-value.d.ts +1 -0
- package/dist/core/current-value.d.ts.map +1 -0
- package/dist/core/current-value.js +35 -34
- package/dist/core/current-value.js.map +1 -0
- package/dist/core/error-diagnostics.d.ts +1 -0
- package/dist/core/error-diagnostics.d.ts.map +1 -0
- package/dist/core/error-diagnostics.js +70 -137
- package/dist/core/error-diagnostics.js.map +1 -0
- package/dist/core/error-report.d.ts +1 -0
- package/dist/core/error-report.d.ts.map +1 -0
- package/dist/core/error-report.js +184 -233
- package/dist/core/error-report.js.map +1 -0
- package/dist/core/frame-registry.d.ts +1 -0
- package/dist/core/frame-registry.d.ts.map +1 -0
- package/dist/core/frame-registry.js +546 -662
- package/dist/core/frame-registry.js.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +11 -12
- package/dist/core/material-preprocess.d.ts +1 -0
- package/dist/core/material-preprocess.d.ts.map +1 -0
- package/dist/core/material-preprocess.js +128 -151
- package/dist/core/material-preprocess.js.map +1 -0
- package/dist/core/material.d.ts +1 -0
- package/dist/core/material.d.ts.map +1 -0
- package/dist/core/material.js +263 -317
- package/dist/core/material.js.map +1 -0
- package/dist/core/recompile-policy.d.ts +1 -0
- package/dist/core/recompile-policy.d.ts.map +1 -0
- package/dist/core/recompile-policy.js +18 -13
- package/dist/core/recompile-policy.js.map +1 -0
- package/dist/core/render-graph.d.ts +1 -0
- package/dist/core/render-graph.d.ts.map +1 -0
- package/dist/core/render-graph.js +61 -68
- package/dist/core/render-graph.js.map +1 -0
- package/dist/core/render-targets.d.ts +2 -0
- package/dist/core/render-targets.d.ts.map +1 -0
- package/dist/core/render-targets.js +52 -53
- package/dist/core/render-targets.js.map +1 -0
- package/dist/core/renderer.d.ts +1 -0
- package/dist/core/renderer.d.ts.map +1 -0
- package/dist/core/renderer.js +942 -1081
- package/dist/core/renderer.js.map +1 -0
- package/dist/core/runtime-loop.d.ts +2 -0
- package/dist/core/runtime-loop.d.ts.map +1 -0
- package/dist/core/runtime-loop.js +305 -362
- package/dist/core/runtime-loop.js.map +1 -0
- package/dist/core/scheduler-helpers.d.ts +1 -0
- package/dist/core/scheduler-helpers.d.ts.map +1 -0
- package/dist/core/scheduler-helpers.js +52 -51
- package/dist/core/scheduler-helpers.js.map +1 -0
- package/dist/core/shader.d.ts +1 -0
- package/dist/core/shader.d.ts.map +1 -0
- package/dist/core/shader.js +92 -117
- package/dist/core/shader.js.map +1 -0
- package/dist/core/texture-loader.d.ts +1 -0
- package/dist/core/texture-loader.d.ts.map +1 -0
- package/dist/core/texture-loader.js +205 -273
- package/dist/core/texture-loader.js.map +1 -0
- package/dist/core/textures.d.ts +2 -0
- package/dist/core/textures.d.ts.map +1 -0
- package/dist/core/textures.js +106 -116
- package/dist/core/textures.js.map +1 -0
- package/dist/core/types.d.ts +2 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +0 -4
- package/dist/core/uniforms.d.ts +1 -0
- package/dist/core/uniforms.d.ts.map +1 -0
- package/dist/core/uniforms.js +170 -191
- package/dist/core/uniforms.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -6
- package/dist/passes/BlitPass.d.ts +1 -0
- package/dist/passes/BlitPass.d.ts.map +1 -0
- package/dist/passes/BlitPass.js +23 -18
- package/dist/passes/BlitPass.js.map +1 -0
- package/dist/passes/CopyPass.d.ts +2 -0
- package/dist/passes/CopyPass.d.ts.map +1 -0
- package/dist/passes/CopyPass.js +58 -52
- package/dist/passes/CopyPass.js.map +1 -0
- package/dist/passes/FullscreenPass.d.ts +2 -0
- package/dist/passes/FullscreenPass.d.ts.map +1 -0
- package/dist/passes/FullscreenPass.js +127 -130
- package/dist/passes/FullscreenPass.js.map +1 -0
- package/dist/passes/ShaderPass.d.ts +1 -0
- package/dist/passes/ShaderPass.d.ts.map +1 -0
- package/dist/passes/ShaderPass.js +40 -37
- package/dist/passes/ShaderPass.js.map +1 -0
- package/dist/passes/index.d.ts +1 -0
- package/dist/passes/index.d.ts.map +1 -0
- package/dist/passes/index.js +4 -3
- package/dist/react/FragCanvas.d.ts +2 -0
- package/dist/react/FragCanvas.d.ts.map +1 -0
- package/dist/react/FragCanvas.js +234 -211
- package/dist/react/FragCanvas.js.map +1 -0
- package/dist/react/MotionGPUErrorOverlay.d.ts +1 -0
- package/dist/react/MotionGPUErrorOverlay.d.ts.map +1 -0
- package/dist/react/MotionGPUErrorOverlay.js +384 -48
- package/dist/react/MotionGPUErrorOverlay.js.map +1 -0
- package/dist/react/Portal.d.ts +1 -0
- package/dist/react/Portal.d.ts.map +1 -0
- package/dist/react/Portal.js +18 -21
- package/dist/react/Portal.js.map +1 -0
- package/dist/react/advanced.d.ts +1 -0
- package/dist/react/advanced.d.ts.map +1 -0
- package/dist/react/advanced.js +12 -6
- package/dist/react/frame-context.d.ts +1 -0
- package/dist/react/frame-context.d.ts.map +1 -0
- package/dist/react/frame-context.js +88 -94
- package/dist/react/frame-context.js.map +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +10 -9
- package/dist/react/motiongpu-context.d.ts +1 -0
- package/dist/react/motiongpu-context.d.ts.map +1 -0
- package/dist/react/motiongpu-context.js +18 -15
- package/dist/react/motiongpu-context.js.map +1 -0
- package/dist/react/use-motiongpu-user-context.d.ts +1 -0
- package/dist/react/use-motiongpu-user-context.d.ts.map +1 -0
- package/dist/react/use-motiongpu-user-context.js +83 -82
- package/dist/react/use-motiongpu-user-context.js.map +1 -0
- package/dist/react/use-texture.d.ts +1 -0
- package/dist/react/use-texture.d.ts.map +1 -0
- package/dist/react/use-texture.js +132 -152
- package/dist/react/use-texture.js.map +1 -0
- package/dist/svelte/FragCanvas.svelte.d.ts +2 -0
- package/dist/svelte/FragCanvas.svelte.d.ts.map +1 -0
- package/dist/svelte/MotionGPUErrorOverlay.svelte +17 -20
- package/dist/svelte/MotionGPUErrorOverlay.svelte.d.ts +1 -0
- package/dist/svelte/MotionGPUErrorOverlay.svelte.d.ts.map +1 -0
- package/dist/svelte/Portal.svelte.d.ts +1 -0
- package/dist/svelte/Portal.svelte.d.ts.map +1 -0
- package/dist/svelte/advanced.d.ts +1 -0
- package/dist/svelte/advanced.d.ts.map +1 -0
- package/dist/svelte/advanced.js +11 -6
- package/dist/svelte/frame-context.d.ts +1 -0
- package/dist/svelte/frame-context.d.ts.map +1 -0
- package/dist/svelte/frame-context.js +27 -27
- package/dist/svelte/frame-context.js.map +1 -0
- package/dist/svelte/index.d.ts +1 -0
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +10 -9
- package/dist/svelte/motiongpu-context.d.ts +1 -0
- package/dist/svelte/motiongpu-context.d.ts.map +1 -0
- package/dist/svelte/motiongpu-context.js +24 -21
- package/dist/svelte/motiongpu-context.js.map +1 -0
- package/dist/svelte/use-motiongpu-user-context.d.ts +1 -0
- package/dist/svelte/use-motiongpu-user-context.d.ts.map +1 -0
- package/dist/svelte/use-motiongpu-user-context.js +69 -70
- package/dist/svelte/use-motiongpu-user-context.js.map +1 -0
- package/dist/svelte/use-texture.d.ts +1 -0
- package/dist/svelte/use-texture.d.ts.map +1 -0
- package/dist/svelte/use-texture.js +125 -147
- package/dist/svelte/use-texture.js.map +1 -0
- package/package.json +15 -7
- package/src/lib/advanced.ts +6 -0
- package/src/lib/core/advanced.ts +12 -0
- package/src/lib/core/current-value.ts +64 -0
- package/src/lib/core/error-diagnostics.ts +236 -0
- package/src/lib/core/error-report.ts +406 -0
- package/src/lib/core/frame-registry.ts +1189 -0
- package/src/lib/core/index.ts +77 -0
- package/src/lib/core/material-preprocess.ts +284 -0
- package/src/lib/core/material.ts +667 -0
- package/src/lib/core/recompile-policy.ts +31 -0
- package/src/lib/core/render-graph.ts +143 -0
- package/src/lib/core/render-targets.ts +107 -0
- package/src/lib/core/renderer.ts +1547 -0
- package/src/lib/core/runtime-loop.ts +458 -0
- package/src/lib/core/scheduler-helpers.ts +136 -0
- package/src/lib/core/shader.ts +258 -0
- package/src/lib/core/texture-loader.ts +476 -0
- package/src/lib/core/textures.ts +235 -0
- package/src/lib/core/types.ts +582 -0
- package/src/lib/core/uniforms.ts +282 -0
- package/src/lib/index.ts +6 -0
- package/src/lib/passes/BlitPass.ts +54 -0
- package/src/lib/passes/CopyPass.ts +80 -0
- package/src/lib/passes/FullscreenPass.ts +173 -0
- package/src/lib/passes/ShaderPass.ts +88 -0
- package/src/lib/passes/index.ts +3 -0
- package/src/lib/react/MotionGPUErrorOverlay.tsx +392 -0
- package/src/lib/react/advanced.ts +36 -0
- package/src/lib/react/frame-context.ts +169 -0
- package/src/lib/react/index.ts +51 -0
- package/src/lib/react/motiongpu-context.ts +88 -0
- package/src/lib/react/use-motiongpu-user-context.ts +186 -0
- package/src/lib/react/use-texture.ts +233 -0
- package/src/lib/svelte/FragCanvas.svelte +249 -0
- package/src/lib/svelte/MotionGPUErrorOverlay.svelte +382 -0
- package/src/lib/svelte/Portal.svelte +31 -0
- package/src/lib/svelte/advanced.ts +32 -0
- package/src/lib/svelte/frame-context.ts +87 -0
- package/src/lib/svelte/index.ts +51 -0
- package/src/lib/svelte/motiongpu-context.ts +97 -0
- package/src/lib/svelte/use-motiongpu-user-context.ts +145 -0
- package/src/lib/svelte/use-texture.ts +232 -0
- package/dist/react/MotionGPUErrorOverlay.tsx +0 -129
- /package/{dist → src/lib}/react/FragCanvas.tsx +0 -0
- /package/{dist → src/lib}/react/Portal.tsx +0 -0
package/dist/core/material.js
CHANGED
|
@@ -1,365 +1,311 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { normalizeDefines, normalizeIncludes, preprocessMaterialFragment, toDefineLine } from
|
|
1
|
+
import { assertUniformName, assertUniformValueForType, inferUniformType, resolveUniformLayout } from "./uniforms.js";
|
|
2
|
+
import { normalizeTextureDefinition } from "./textures.js";
|
|
3
|
+
import { normalizeDefines, normalizeIncludes, preprocessMaterialFragment, toDefineLine } from "./material-preprocess.js";
|
|
4
|
+
//#region src/lib/core/material.ts
|
|
4
5
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
* Strict fragment contract used by MotionGPU.
|
|
7
|
+
*/
|
|
8
|
+
var FRAGMENT_FUNCTION_SIGNATURE_PATTERN = /\bfn\s+frag\s*\(\s*([^)]*?)\s*\)\s*->\s*([A-Za-z_][A-Za-z0-9_<>\s]*)\s*(?:\{|$)/m;
|
|
9
|
+
var FRAGMENT_FUNCTION_NAME_PATTERN = /\bfn\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(/g;
|
|
10
|
+
var resolvedMaterialCache = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
var preprocessedFragmentCache = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
var materialSourceMetadataCache = /* @__PURE__ */ new WeakMap();
|
|
12
13
|
function getCachedResolvedMaterial(material) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
// Invariant: the cache key is the same material object used to produce this resolved payload.
|
|
18
|
-
return cached;
|
|
14
|
+
const cached = resolvedMaterialCache.get(material);
|
|
15
|
+
if (!cached) return null;
|
|
16
|
+
return cached;
|
|
19
17
|
}
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
var STACK_TRACE_CHROME_PATTERN = /^\s*at\s+(?:(.*?)\s+\()?(.+?):(\d+):(\d+)\)?$/;
|
|
19
|
+
var STACK_TRACE_FIREFOX_PATTERN = /^(.*?)@(.+?):(\d+):(\d+)$/;
|
|
22
20
|
function getPathBasename(path) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return last && last.length > 0 ? last : path;
|
|
21
|
+
const parts = (path.split(/[?#]/)[0] ?? path).split(/[\\/]/);
|
|
22
|
+
const last = parts[parts.length - 1];
|
|
23
|
+
return last && last.length > 0 ? last : path;
|
|
27
24
|
}
|
|
28
25
|
function normalizeSignaturePart(value) {
|
|
29
|
-
|
|
26
|
+
return value.replace(/\s+/g, " ").trim();
|
|
30
27
|
}
|
|
31
28
|
function listFunctionNames(fragment) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
return Array.from(names);
|
|
29
|
+
const names = /* @__PURE__ */ new Set();
|
|
30
|
+
for (const match of fragment.matchAll(FRAGMENT_FUNCTION_NAME_PATTERN)) {
|
|
31
|
+
const name = match[1];
|
|
32
|
+
if (!name) continue;
|
|
33
|
+
names.add(name);
|
|
34
|
+
}
|
|
35
|
+
return Array.from(names);
|
|
41
36
|
}
|
|
42
37
|
function captureMaterialSourceFromStack() {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
return {
|
|
71
|
-
component: getPathBasename(file),
|
|
72
|
-
file,
|
|
73
|
-
line: parsedLine,
|
|
74
|
-
column: parsedColumn,
|
|
75
|
-
...(functionName ? { functionName } : {})
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
return null;
|
|
38
|
+
const stack = (/* @__PURE__ */ new Error()).stack;
|
|
39
|
+
if (!stack) return null;
|
|
40
|
+
const stackLines = stack.split("\n").slice(1);
|
|
41
|
+
for (const rawLine of stackLines) {
|
|
42
|
+
const line = rawLine.trim();
|
|
43
|
+
if (line.length === 0) continue;
|
|
44
|
+
const chromeMatch = line.match(STACK_TRACE_CHROME_PATTERN);
|
|
45
|
+
const firefoxMatch = line.match(STACK_TRACE_FIREFOX_PATTERN);
|
|
46
|
+
const functionName = chromeMatch?.[1] ?? firefoxMatch?.[1] ?? void 0;
|
|
47
|
+
const file = chromeMatch?.[2] ?? firefoxMatch?.[2];
|
|
48
|
+
const lineValue = chromeMatch?.[3] ?? firefoxMatch?.[3];
|
|
49
|
+
const columnValue = chromeMatch?.[4] ?? firefoxMatch?.[4];
|
|
50
|
+
if (!file || !lineValue || !columnValue) continue;
|
|
51
|
+
if (file.includes("/core/material") || file.includes("\\core\\material")) continue;
|
|
52
|
+
const parsedLine = Number.parseInt(lineValue, 10);
|
|
53
|
+
const parsedColumn = Number.parseInt(columnValue, 10);
|
|
54
|
+
if (!Number.isFinite(parsedLine) || !Number.isFinite(parsedColumn)) continue;
|
|
55
|
+
return {
|
|
56
|
+
component: getPathBasename(file),
|
|
57
|
+
file,
|
|
58
|
+
line: parsedLine,
|
|
59
|
+
column: parsedColumn,
|
|
60
|
+
...functionName ? { functionName } : {}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
79
64
|
}
|
|
80
65
|
function resolveSourceMetadata(source) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
...(component !== undefined ? { component } : {}),
|
|
96
|
-
...(file !== undefined ? { file } : {}),
|
|
97
|
-
...(line !== undefined ? { line } : {}),
|
|
98
|
-
...(column !== undefined ? { column } : {}),
|
|
99
|
-
...(functionName !== undefined ? { functionName } : {})
|
|
100
|
-
};
|
|
66
|
+
const captured = captureMaterialSourceFromStack();
|
|
67
|
+
const component = source?.component ?? captured?.component;
|
|
68
|
+
const file = source?.file ?? captured?.file;
|
|
69
|
+
const line = source?.line ?? captured?.line;
|
|
70
|
+
const column = source?.column ?? captured?.column;
|
|
71
|
+
const functionName = source?.functionName ?? captured?.functionName;
|
|
72
|
+
if (component === void 0 && file === void 0 && line === void 0 && column === void 0 && functionName === void 0) return null;
|
|
73
|
+
return {
|
|
74
|
+
...component !== void 0 ? { component } : {},
|
|
75
|
+
...file !== void 0 ? { file } : {},
|
|
76
|
+
...line !== void 0 ? { line } : {},
|
|
77
|
+
...column !== void 0 ? { column } : {},
|
|
78
|
+
...functionName !== void 0 ? { functionName } : {}
|
|
79
|
+
};
|
|
101
80
|
}
|
|
102
81
|
/**
|
|
103
|
-
|
|
104
|
-
|
|
82
|
+
* Asserts that material has been normalized by {@link defineMaterial}.
|
|
83
|
+
*/
|
|
105
84
|
function assertDefinedMaterial(material) {
|
|
106
|
-
|
|
107
|
-
!material.uniforms ||
|
|
108
|
-
!material.textures ||
|
|
109
|
-
!material.defines ||
|
|
110
|
-
!material.includes) {
|
|
111
|
-
throw new Error('Invalid material instance. Create materials with defineMaterial(...) before passing them to <FragCanvas>.');
|
|
112
|
-
}
|
|
85
|
+
if (!Object.isFrozen(material) || !material.uniforms || !material.textures || !material.defines || !material.includes) throw new Error("Invalid material instance. Create materials with defineMaterial(...) before passing them to <FragCanvas>.");
|
|
113
86
|
}
|
|
114
87
|
/**
|
|
115
|
-
|
|
116
|
-
|
|
88
|
+
* Clones uniform value input to decouple material instances from external objects.
|
|
89
|
+
*/
|
|
117
90
|
function cloneUniformValue(value) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
clonedTypedValue = Object.freeze([...typedValue]);
|
|
133
|
-
}
|
|
134
|
-
return Object.freeze({
|
|
135
|
-
type: typed.type,
|
|
136
|
-
value: clonedTypedValue
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
return value;
|
|
91
|
+
if (typeof value === "number") return value;
|
|
92
|
+
if (Array.isArray(value)) return Object.freeze([...value]);
|
|
93
|
+
if (typeof value === "object" && value !== null && "type" in value && "value" in value) {
|
|
94
|
+
const typed = value;
|
|
95
|
+
const typedValue = typed.value;
|
|
96
|
+
let clonedTypedValue = typedValue;
|
|
97
|
+
if (typedValue instanceof Float32Array) clonedTypedValue = new Float32Array(typedValue);
|
|
98
|
+
else if (Array.isArray(typedValue)) clonedTypedValue = Object.freeze([...typedValue]);
|
|
99
|
+
return Object.freeze({
|
|
100
|
+
type: typed.type,
|
|
101
|
+
value: clonedTypedValue
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return value;
|
|
140
105
|
}
|
|
141
106
|
/**
|
|
142
|
-
|
|
143
|
-
|
|
107
|
+
* Clones optional texture value payload.
|
|
108
|
+
*/
|
|
144
109
|
function cloneTextureValue(value) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
...(data.update !== undefined ? { update: data.update } : {})
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
return value;
|
|
110
|
+
if (value === void 0 || value === null) return null;
|
|
111
|
+
if (typeof value === "object" && "source" in value) {
|
|
112
|
+
const data = value;
|
|
113
|
+
return {
|
|
114
|
+
source: data.source,
|
|
115
|
+
...data.width !== void 0 ? { width: data.width } : {},
|
|
116
|
+
...data.height !== void 0 ? { height: data.height } : {},
|
|
117
|
+
...data.colorSpace !== void 0 ? { colorSpace: data.colorSpace } : {},
|
|
118
|
+
...data.flipY !== void 0 ? { flipY: data.flipY } : {},
|
|
119
|
+
...data.premultipliedAlpha !== void 0 ? { premultipliedAlpha: data.premultipliedAlpha } : {},
|
|
120
|
+
...data.generateMipmaps !== void 0 ? { generateMipmaps: data.generateMipmaps } : {},
|
|
121
|
+
...data.update !== void 0 ? { update: data.update } : {}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
164
125
|
}
|
|
165
126
|
/**
|
|
166
|
-
|
|
167
|
-
|
|
127
|
+
* Clones and validates fragment source contract.
|
|
128
|
+
*/
|
|
168
129
|
function resolveFragment(fragment) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const returnType = normalizeSignaturePart(signature[2] ?? '');
|
|
182
|
-
if (params !== 'uv: vec2f') {
|
|
183
|
-
throw new Error(`Material fragment contract mismatch for \`frag\`: expected parameter list \`(uv: vec2f)\`, received \`(${params || '...'})\`.`);
|
|
184
|
-
}
|
|
185
|
-
if (returnType !== 'vec4f') {
|
|
186
|
-
throw new Error(`Material fragment contract mismatch for \`frag\`: expected return type \`vec4f\`, received \`${returnType}\`.`);
|
|
187
|
-
}
|
|
188
|
-
return fragment;
|
|
130
|
+
if (typeof fragment !== "string" || fragment.trim().length === 0) throw new Error("Material fragment shader must be a non-empty WGSL string.");
|
|
131
|
+
const signature = fragment.match(FRAGMENT_FUNCTION_SIGNATURE_PATTERN);
|
|
132
|
+
if (!signature) {
|
|
133
|
+
const discoveredFunctions = listFunctionNames(fragment).slice(0, 4);
|
|
134
|
+
const discoveredLabel = discoveredFunctions.length > 0 ? `Found: ${discoveredFunctions.map((name) => `\`${name}(...)\``).join(", ")}.` : "No WGSL function declarations were found.";
|
|
135
|
+
throw new Error(`Material fragment contract mismatch: missing entrypoint \`fn frag(uv: vec2f) -> vec4f\`. ${discoveredLabel}`);
|
|
136
|
+
}
|
|
137
|
+
const params = normalizeSignaturePart(signature[1] ?? "");
|
|
138
|
+
const returnType = normalizeSignaturePart(signature[2] ?? "");
|
|
139
|
+
if (params !== "uv: vec2f") throw new Error(`Material fragment contract mismatch for \`frag\`: expected parameter list \`(uv: vec2f)\`, received \`(${params || "..."})\`.`);
|
|
140
|
+
if (returnType !== "vec4f") throw new Error(`Material fragment contract mismatch for \`frag\`: expected return type \`vec4f\`, received \`${returnType}\`.`);
|
|
141
|
+
return fragment;
|
|
189
142
|
}
|
|
190
143
|
/**
|
|
191
|
-
|
|
192
|
-
|
|
144
|
+
* Clones and validates uniform declarations.
|
|
145
|
+
*/
|
|
193
146
|
function resolveUniforms(uniforms) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return resolved;
|
|
147
|
+
const resolved = {};
|
|
148
|
+
for (const [name, value] of Object.entries(uniforms ?? {})) {
|
|
149
|
+
assertUniformName(name);
|
|
150
|
+
const clonedValue = cloneUniformValue(value);
|
|
151
|
+
assertUniformValueForType(inferUniformType(clonedValue), clonedValue);
|
|
152
|
+
resolved[name] = clonedValue;
|
|
153
|
+
}
|
|
154
|
+
resolveUniformLayout(resolved);
|
|
155
|
+
return resolved;
|
|
204
156
|
}
|
|
205
157
|
/**
|
|
206
|
-
|
|
207
|
-
|
|
158
|
+
* Clones and validates texture declarations.
|
|
159
|
+
*/
|
|
208
160
|
function resolveTextures(textures) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
161
|
+
const resolved = {};
|
|
162
|
+
for (const [name, definition] of Object.entries(textures ?? {})) {
|
|
163
|
+
assertUniformName(name);
|
|
164
|
+
const source = definition?.source;
|
|
165
|
+
const normalizedSource = cloneTextureValue(source);
|
|
166
|
+
const clonedDefinition = {
|
|
167
|
+
...definition ?? {},
|
|
168
|
+
...source !== void 0 ? { source: normalizedSource } : {}
|
|
169
|
+
};
|
|
170
|
+
resolved[name] = Object.freeze(clonedDefinition);
|
|
171
|
+
}
|
|
172
|
+
return resolved;
|
|
221
173
|
}
|
|
222
174
|
/**
|
|
223
|
-
|
|
224
|
-
|
|
175
|
+
* Clones and validates define declarations.
|
|
176
|
+
*/
|
|
225
177
|
function resolveDefines(defines) {
|
|
226
|
-
|
|
178
|
+
return normalizeDefines(defines);
|
|
227
179
|
}
|
|
228
180
|
/**
|
|
229
|
-
|
|
230
|
-
|
|
181
|
+
* Clones and validates include declarations.
|
|
182
|
+
*/
|
|
231
183
|
function resolveIncludes(includes) {
|
|
232
|
-
|
|
184
|
+
return normalizeIncludes(includes);
|
|
233
185
|
}
|
|
234
186
|
/**
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
187
|
+
* Builds a deterministic texture-config signature map used in material cache signatures.
|
|
188
|
+
*
|
|
189
|
+
* @param textures - Raw texture definitions from material input.
|
|
190
|
+
* @param textureKeys - Sorted texture keys.
|
|
191
|
+
* @returns Compact signature entries describing effective texture config per key.
|
|
192
|
+
*/
|
|
241
193
|
function buildTextureConfigSignature(textures, textureKeys) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
194
|
+
const signature = {};
|
|
195
|
+
for (const key of textureKeys) {
|
|
196
|
+
const normalized = normalizeTextureDefinition(textures[key]);
|
|
197
|
+
signature[key] = [
|
|
198
|
+
normalized.colorSpace,
|
|
199
|
+
normalized.flipY ? "1" : "0",
|
|
200
|
+
normalized.generateMipmaps ? "1" : "0",
|
|
201
|
+
normalized.premultipliedAlpha ? "1" : "0",
|
|
202
|
+
normalized.anisotropy,
|
|
203
|
+
normalized.filter,
|
|
204
|
+
normalized.addressModeU,
|
|
205
|
+
normalized.addressModeV
|
|
206
|
+
].join(":");
|
|
207
|
+
}
|
|
208
|
+
return signature;
|
|
257
209
|
}
|
|
258
210
|
/**
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
.map(([key, value]) => {
|
|
272
|
-
assertUniformName(key);
|
|
273
|
-
return toDefineLine(key, value);
|
|
274
|
-
})
|
|
275
|
-
.join('\n');
|
|
211
|
+
* Creates a stable WGSL define block from the provided map.
|
|
212
|
+
*
|
|
213
|
+
* @param defines - Optional material defines.
|
|
214
|
+
* @returns Joined WGSL const declarations ordered by key.
|
|
215
|
+
*/
|
|
216
|
+
function buildDefinesBlock(defines) {
|
|
217
|
+
const normalizedDefines = normalizeDefines(defines);
|
|
218
|
+
if (Object.keys(normalizedDefines).length === 0) return "";
|
|
219
|
+
return Object.entries(normalizedDefines).sort(([a], [b]) => a.localeCompare(b)).map(([key, value]) => {
|
|
220
|
+
assertUniformName(key);
|
|
221
|
+
return toDefineLine(key, value);
|
|
222
|
+
}).join("\n");
|
|
276
223
|
}
|
|
277
224
|
/**
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
return `${defineBlock}\n\n${fragment}`;
|
|
225
|
+
* Prepends resolved defines to a fragment shader.
|
|
226
|
+
*
|
|
227
|
+
* @param fragment - Raw WGSL fragment source.
|
|
228
|
+
* @param defines - Optional define map.
|
|
229
|
+
* @returns Fragment source with a leading define block when defines are present.
|
|
230
|
+
*/
|
|
231
|
+
function applyMaterialDefines(fragment, defines) {
|
|
232
|
+
const defineBlock = buildDefinesBlock(defines);
|
|
233
|
+
if (defineBlock.length === 0) return fragment;
|
|
234
|
+
return `${defineBlock}\n\n${fragment}`;
|
|
290
235
|
}
|
|
291
236
|
/**
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
237
|
+
* Creates an immutable material object with validated shader/uniform/texture contracts.
|
|
238
|
+
*
|
|
239
|
+
* @param input - User material declaration.
|
|
240
|
+
* @returns Frozen material object safe to share and cache.
|
|
241
|
+
*/
|
|
242
|
+
function defineMaterial(input) {
|
|
243
|
+
const fragment = resolveFragment(input.fragment);
|
|
244
|
+
const uniforms = Object.freeze(resolveUniforms(input.uniforms));
|
|
245
|
+
const textures = Object.freeze(resolveTextures(input.textures));
|
|
246
|
+
const defines = Object.freeze(resolveDefines(input.defines));
|
|
247
|
+
const includes = Object.freeze(resolveIncludes(input.includes));
|
|
248
|
+
const source = Object.freeze(resolveSourceMetadata(void 0));
|
|
249
|
+
const preprocessed = preprocessMaterialFragment({
|
|
250
|
+
fragment,
|
|
251
|
+
defines,
|
|
252
|
+
includes
|
|
253
|
+
});
|
|
254
|
+
const material = Object.freeze({
|
|
255
|
+
fragment,
|
|
256
|
+
uniforms,
|
|
257
|
+
textures,
|
|
258
|
+
defines,
|
|
259
|
+
includes
|
|
260
|
+
});
|
|
261
|
+
preprocessedFragmentCache.set(material, preprocessed);
|
|
262
|
+
materialSourceMetadataCache.set(material, source);
|
|
263
|
+
return material;
|
|
319
264
|
}
|
|
320
265
|
/**
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
};
|
|
363
|
-
resolvedMaterialCache.set(material, resolved);
|
|
364
|
-
return resolved;
|
|
266
|
+
* Resolves a material to renderer-ready data and a deterministic signature.
|
|
267
|
+
*
|
|
268
|
+
* @param material - Material input created via {@link defineMaterial}.
|
|
269
|
+
* @returns Resolved material with packed uniform layout, sorted texture keys and cache signature.
|
|
270
|
+
*/
|
|
271
|
+
function resolveMaterial(material) {
|
|
272
|
+
assertDefinedMaterial(material);
|
|
273
|
+
const cached = getCachedResolvedMaterial(material);
|
|
274
|
+
if (cached) return cached;
|
|
275
|
+
const uniforms = material.uniforms;
|
|
276
|
+
const textures = material.textures;
|
|
277
|
+
const uniformLayout = resolveUniformLayout(uniforms);
|
|
278
|
+
const textureKeys = Object.keys(textures).sort();
|
|
279
|
+
const preprocessed = preprocessedFragmentCache.get(material) ?? preprocessMaterialFragment({
|
|
280
|
+
fragment: material.fragment,
|
|
281
|
+
defines: material.defines,
|
|
282
|
+
includes: material.includes
|
|
283
|
+
});
|
|
284
|
+
const fragmentWgsl = preprocessed.fragment;
|
|
285
|
+
const textureConfig = buildTextureConfigSignature(textures, textureKeys);
|
|
286
|
+
const signature = JSON.stringify({
|
|
287
|
+
fragmentWgsl,
|
|
288
|
+
uniforms: uniformLayout.entries.map((entry) => `${entry.name}:${entry.type}`),
|
|
289
|
+
textureKeys,
|
|
290
|
+
textureConfig
|
|
291
|
+
});
|
|
292
|
+
const resolved = {
|
|
293
|
+
fragmentWgsl,
|
|
294
|
+
fragmentLineMap: preprocessed.lineMap,
|
|
295
|
+
uniforms,
|
|
296
|
+
textures,
|
|
297
|
+
uniformLayout,
|
|
298
|
+
textureKeys,
|
|
299
|
+
signature,
|
|
300
|
+
fragmentSource: material.fragment,
|
|
301
|
+
includeSources: material.includes,
|
|
302
|
+
defineBlockSource: preprocessed.defineBlockSource,
|
|
303
|
+
source: materialSourceMetadataCache.get(material) ?? null
|
|
304
|
+
};
|
|
305
|
+
resolvedMaterialCache.set(material, resolved);
|
|
306
|
+
return resolved;
|
|
365
307
|
}
|
|
308
|
+
//#endregion
|
|
309
|
+
export { applyMaterialDefines, buildDefinesBlock, defineMaterial, resolveMaterial };
|
|
310
|
+
|
|
311
|
+
//# sourceMappingURL=material.js.map
|