@threenative/three-native 0.0.0-stage → 0.3.5
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 +24 -2
- package/package.json +42 -3
- package/src/addons/buffer-geometry-utils.ts +14 -0
- package/src/addons/data-utils.ts +63 -0
- package/src/addons/decal-geometry.ts +25 -0
- package/src/addons/decal.ts +218 -0
- package/src/addons/gltf-loader-web.ts +323 -0
- package/src/addons/hdr-loader.ts +71 -0
- package/src/addons/hdr.ts +139 -0
- package/src/addons/lut-cube-loader-web.ts +17 -0
- package/src/addons/lut-cube-loader.ts +149 -0
- package/src/addons/merge-geometries.ts +374 -0
- package/src/addons/mesh-bvh-webgpu.ts +13 -0
- package/src/addons/mesh-bvh.ts +50 -0
- package/src/addons/post-effects-web.ts +22 -0
- package/src/addons/post-effects.ts +173 -0
- package/src/audio.ts +564 -0
- package/src/blend-mode.ts +33 -0
- package/src/browser-backend.ts +2156 -0
- package/src/browser-entry.ts +305 -0
- package/src/browser-math.ts +448 -0
- package/src/browser-renderer.ts +527 -0
- package/src/browser-tsl.ts +650 -0
- package/src/catalog.ts +497 -0
- package/src/fixture-format.ts +721 -0
- package/src/fixture-native.ts +426 -0
- package/src/fixture-protocol.ts +315 -0
- package/src/math-utils.ts +65 -0
- package/src/object-surface.ts +250 -0
- package/src/pass-node.ts +286 -0
- package/src/property-binding.ts +93 -0
- package/src/quad-mesh.ts +54 -0
- package/src/reflector.ts +89 -0
- package/src/refused.ts +27 -0
- package/src/render-target.ts +107 -0
- package/src/rtt.ts +158 -0
- package/src/shadow-map.ts +21 -0
- package/src/storage.ts +206 -0
- package/src/texture-sources.ts +252 -0
- package/src/tsl-extras.ts +51 -0
- package/src/tsl-struct.ts +20 -0
- package/src/tsl-uniforms.ts +234 -0
- package/src/update-ranges.ts +109 -0
- package/src/wgsl-fn.ts +55 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Three's struct layout and positional constructor over the shared native struct operations. */
|
|
2
|
+
interface IStructNode {
|
|
3
|
+
newStruct(...values: unknown[]): IStructNode;
|
|
4
|
+
}
|
|
5
|
+
export function defineStruct(make: (...args: string[]) => IStructNode) {
|
|
6
|
+
let serial = 0;
|
|
7
|
+
return (layout: Readonly<Record<string, string>>, name?: string) => {
|
|
8
|
+
if (layout === null || typeof layout !== "object" || Array.isArray(layout))
|
|
9
|
+
throw new TypeError("TN_TSL_STRUCT_LAYOUT: expected named scalar or vector members");
|
|
10
|
+
const members = Object.entries(layout);
|
|
11
|
+
if (members.length === 0 || members.some(([, type]) => typeof type !== "string"))
|
|
12
|
+
throw new TypeError("TN_TSL_STRUCT_LAYOUT: expected named scalar or vector members");
|
|
13
|
+
const schema = make(name ?? `NativeStruct${++serial}`, ...members.flat());
|
|
14
|
+
return (...values: unknown[]) => {
|
|
15
|
+
if (values.length > 0 && values.length !== members.length)
|
|
16
|
+
throw new TypeError("TN_TSL_STRUCT_VALUES: expected one value per member");
|
|
17
|
+
return schema.newStruct(...values);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* three's `uniform(value).value` for both engine back ends (PRD-540). As r185's InputNode, `.value = x`
|
|
3
|
+
* replaces the value and the getter returns what was written; a Color or VectorN value can then be
|
|
4
|
+
* edited in place (`origin.value.set(x, z)`, as Midway's ocean does), and `sync()` pushes the edited
|
|
5
|
+
* objects' lanes before a frame draws. A write goes through at once, and the engine refuses one whose
|
|
6
|
+
* lanes do not fit the uniform's type. Either way the engine keeps the program.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** three's update `frame`: the rendering camera and scene, with the frame counter. */
|
|
10
|
+
export interface IUpdateFrame {
|
|
11
|
+
frameId: number;
|
|
12
|
+
camera?: unknown;
|
|
13
|
+
scene?: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface IUniformState {
|
|
17
|
+
value: unknown;
|
|
18
|
+
/** three's `node.update`, set by onRenderUpdate/onFrameUpdate; its result becomes `.value`. */
|
|
19
|
+
update?: (frame: IUpdateFrame) => unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** One float per lane: a number, a Color's r, g, b, or a VectorN's x, y, z, w. */
|
|
23
|
+
export function uniformLanes(value: unknown): number[] {
|
|
24
|
+
if (typeof value === "number") return [value];
|
|
25
|
+
if (typeof value === "object" && value !== null) {
|
|
26
|
+
const v = value as Record<string, unknown>;
|
|
27
|
+
if (typeof v.x === "number" && typeof v.y === "number")
|
|
28
|
+
return [v.x, v.y, v.z, v.w].filter((lane): lane is number => typeof lane === "number");
|
|
29
|
+
if (typeof v.r === "number" && typeof v.g === "number" && typeof v.b === "number")
|
|
30
|
+
return [v.r, v.g, v.b];
|
|
31
|
+
}
|
|
32
|
+
throw new TypeError(`TN_TSL_UNIFORM_VALUE: ${String(value)} is not a number, Color or vector`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function referenceValue(property: string, type: string, object: unknown): unknown {
|
|
36
|
+
const types: Readonly<Record<string, number>> = { float: 1, color: 3, vec2: 2, vec3: 3, vec4: 4 };
|
|
37
|
+
const lanes = types[type];
|
|
38
|
+
if (lanes === undefined)
|
|
39
|
+
throw new TypeError(
|
|
40
|
+
`TN_TSL_REFERENCE_TYPE: reference '${type}' needs typed integer, boolean, matrix or buffer uniforms`,
|
|
41
|
+
);
|
|
42
|
+
if (typeof object !== "object" || object === null)
|
|
43
|
+
throw new TypeError(
|
|
44
|
+
"TN_TSL_REFERENCE_OBJECT: reference needs an explicit object; per-draw object references are unbound",
|
|
45
|
+
);
|
|
46
|
+
let value: unknown = object;
|
|
47
|
+
for (const key of property.split(".")) {
|
|
48
|
+
if (typeof value !== "object" || value === null)
|
|
49
|
+
throw new TypeError(`TN_TSL_REFERENCE_VALUE: missing ${property}`);
|
|
50
|
+
value = Reflect.get(value, key);
|
|
51
|
+
}
|
|
52
|
+
const values = uniformLanes(value);
|
|
53
|
+
if (values.length !== lanes || !values.every(Number.isFinite))
|
|
54
|
+
throw new TypeError(
|
|
55
|
+
`TN_TSL_REFERENCE_VALUE: ${property} must match ${type} with finite values`,
|
|
56
|
+
);
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Wraps a back end's `uniform` so each uniform node it returns answers `.value` (an accessor on the
|
|
62
|
+
* node itself: a V8 node has no prototype of its own to carry it) and three's update hooks. `setValues`
|
|
63
|
+
* writes a node's lanes into the engine. `sync()` runs once per render: the update callbacks first,
|
|
64
|
+
* then the edited objects' lanes.
|
|
65
|
+
*/
|
|
66
|
+
export function liveUniforms<TNode extends object>(
|
|
67
|
+
uniform: (value: unknown, type?: unknown) => TNode,
|
|
68
|
+
setValues: (node: TNode, lanes: readonly number[]) => void,
|
|
69
|
+
): {
|
|
70
|
+
uniform: (value: unknown, type?: unknown) => TNode;
|
|
71
|
+
uniformArray: (
|
|
72
|
+
values: unknown[],
|
|
73
|
+
type?: string,
|
|
74
|
+
) => { array: unknown[]; element(index: unknown): TNode };
|
|
75
|
+
reference(property: string, type: string, object?: unknown): TNode;
|
|
76
|
+
sync(frame?: { camera?: unknown; scene?: unknown }): void;
|
|
77
|
+
} {
|
|
78
|
+
const states = new WeakMap<object, IUniformState>();
|
|
79
|
+
const objects = new Set<WeakRef<TNode>>();
|
|
80
|
+
// Strong: a hook's node is chained into a material and dropped, so the wrapper is all JS holds, and a
|
|
81
|
+
// collected wrapper silently ended its updates. ponytail: kept for the page's life; the engine release
|
|
82
|
+
// of such a node waits too. Release with the owning material if a game builds hooked uniforms in a loop.
|
|
83
|
+
const updating = new Set<TNode>();
|
|
84
|
+
let frameId = 0;
|
|
85
|
+
const live = {
|
|
86
|
+
uniform(value: unknown, type?: unknown): TNode {
|
|
87
|
+
const node = type === undefined ? uniform(value) : uniform(value, type);
|
|
88
|
+
const state: IUniformState = { value };
|
|
89
|
+
states.set(node, state);
|
|
90
|
+
Object.defineProperty(node, "value", {
|
|
91
|
+
configurable: true,
|
|
92
|
+
get: () => state.value,
|
|
93
|
+
set: (next: unknown) => {
|
|
94
|
+
setValues(node, uniformLanes(next));
|
|
95
|
+
state.value = next;
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
if (typeof value === "object" && value !== null) objects.add(new WeakRef(node));
|
|
99
|
+
// three's UniformNode.onUpdate: the callback runs with the node as `this` and its result,
|
|
100
|
+
// unless undefined, becomes the value. ponytail: FRAME and RENDER both run once per sync (one
|
|
101
|
+
// per render call); a game that renders twice a frame runs a FRAME callback twice.
|
|
102
|
+
const onUpdate = (
|
|
103
|
+
callback: (this: TNode, frame: unknown, node: TNode) => unknown,
|
|
104
|
+
updateType: string,
|
|
105
|
+
) => {
|
|
106
|
+
if (updateType !== "frame" && updateType !== "render")
|
|
107
|
+
throw new Error(
|
|
108
|
+
`TN_TSL_UPDATE_UNSUPPORTED: a native uniform updates per frame or render, not per '${updateType}'`,
|
|
109
|
+
);
|
|
110
|
+
if (state.update === undefined) updating.add(node);
|
|
111
|
+
state.update = (frame) => callback.call(node, frame, node);
|
|
112
|
+
return node;
|
|
113
|
+
};
|
|
114
|
+
Object.defineProperties(node, {
|
|
115
|
+
onUpdate: { configurable: true, value: onUpdate },
|
|
116
|
+
onFrameUpdate: {
|
|
117
|
+
configurable: true,
|
|
118
|
+
value: (callback: never) => onUpdate(callback, "frame"),
|
|
119
|
+
},
|
|
120
|
+
onRenderUpdate: {
|
|
121
|
+
configurable: true,
|
|
122
|
+
value: (callback: never) => onUpdate(callback, "render"),
|
|
123
|
+
},
|
|
124
|
+
onObjectUpdate: {
|
|
125
|
+
configurable: true,
|
|
126
|
+
value: (callback: never) => onUpdate(callback, "object"),
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
return node;
|
|
130
|
+
},
|
|
131
|
+
reference(property: string, type: string, object?: unknown): TNode {
|
|
132
|
+
if (typeof property !== "string" || property.split(".").some((key) => key.length === 0))
|
|
133
|
+
throw new TypeError("TN_TSL_REFERENCE_PROPERTY: expected a nonempty property path");
|
|
134
|
+
const node = live.uniform(referenceValue(property, type, object)) as TNode & {
|
|
135
|
+
object: unknown;
|
|
136
|
+
onRenderUpdate(callback: () => unknown): TNode;
|
|
137
|
+
};
|
|
138
|
+
Object.defineProperty(node, "object", { configurable: true, writable: true, value: object });
|
|
139
|
+
node.onRenderUpdate(() => referenceValue(property, type, node.object));
|
|
140
|
+
return node;
|
|
141
|
+
},
|
|
142
|
+
/**
|
|
143
|
+
* three's uniformArray(values): `element(i)` at a constant index is a uniform that reads
|
|
144
|
+
* `values[i]` before each render, as UniformArrayNode copies its array per update. ponytail: a
|
|
145
|
+
* node index needs an engine uniform array; it is refused by name.
|
|
146
|
+
*/
|
|
147
|
+
uniformArray(values: unknown[]) {
|
|
148
|
+
if (!Array.isArray(values))
|
|
149
|
+
throw new TypeError("TN_TSL_UNIFORM_ARRAY: uniformArray takes an array");
|
|
150
|
+
const elements = new Map<number, TNode>();
|
|
151
|
+
return {
|
|
152
|
+
array: values,
|
|
153
|
+
element(index: unknown): TNode {
|
|
154
|
+
if (
|
|
155
|
+
typeof index !== "number" ||
|
|
156
|
+
!Number.isInteger(index) ||
|
|
157
|
+
index < 0 ||
|
|
158
|
+
index >= values.length
|
|
159
|
+
)
|
|
160
|
+
throw new RangeError(
|
|
161
|
+
`TN_TSL_UNIFORM_ARRAY: element needs a constant index in [0, ${values.length})`,
|
|
162
|
+
);
|
|
163
|
+
let node = elements.get(index);
|
|
164
|
+
if (node === undefined) {
|
|
165
|
+
node = live.uniform(values[index]);
|
|
166
|
+
// quality-allow: TNode does not expose internal onRenderUpdate hook from WebGPU node system.
|
|
167
|
+
const updatable = node as unknown as {
|
|
168
|
+
onRenderUpdate: (callback: () => unknown) => void;
|
|
169
|
+
};
|
|
170
|
+
updatable.onRenderUpdate(() => values[index]);
|
|
171
|
+
elements.set(index, node);
|
|
172
|
+
}
|
|
173
|
+
return node;
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
},
|
|
177
|
+
sync(extra?: { camera?: unknown; scene?: unknown }) {
|
|
178
|
+
const frame: IUpdateFrame = { ...extra, frameId: ++frameId };
|
|
179
|
+
for (const node of updating) {
|
|
180
|
+
const next = states.get(node)?.update?.(frame);
|
|
181
|
+
if (next !== undefined) (node as { value: unknown }).value = next;
|
|
182
|
+
}
|
|
183
|
+
for (const ref of objects) {
|
|
184
|
+
const node = ref.deref();
|
|
185
|
+
if (node === undefined) {
|
|
186
|
+
objects.delete(ref);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
setValues(node, uniformLanes(states.get(node)?.value));
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
return live;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** The three classes a constant's value is made of. */
|
|
197
|
+
export type ConstClasses = Readonly<
|
|
198
|
+
Record<"Vector2" | "Vector3" | "Vector4" | "Color", new (...lanes: number[]) => object>
|
|
199
|
+
>;
|
|
200
|
+
|
|
201
|
+
const CONSTANTS = ["float", "int", "uint", "vec2", "vec3", "vec4", "color"] as const;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* r185's ConstNode.value on the constant constructors, as getValueFromType makes it: float(2).value is
|
|
205
|
+
* 2, vec3(1, 2, 3).value a Vector3 (one number fills every lane) and color(0xff0000).value a Color. A
|
|
206
|
+
* call with a node argument is a conversion and keeps no value. Replaces the functions in `fns`.
|
|
207
|
+
*/
|
|
208
|
+
export function withConstValues(fns: Record<string, unknown>, classes: ConstClasses): void {
|
|
209
|
+
for (const name of CONSTANTS) {
|
|
210
|
+
const make = fns[name];
|
|
211
|
+
if (typeof make !== "function") continue;
|
|
212
|
+
const lanes = name.startsWith("vec") ? Number(name.slice(3)) : 0;
|
|
213
|
+
const cls =
|
|
214
|
+
name === "color"
|
|
215
|
+
? classes.Color
|
|
216
|
+
: lanes > 0
|
|
217
|
+
? classes[`Vector${lanes}` as "Vector2"]
|
|
218
|
+
: undefined;
|
|
219
|
+
fns[name] = (...args: unknown[]) => {
|
|
220
|
+
const node = (make as (...a: unknown[]) => object)(...args);
|
|
221
|
+
if (args.length > 0 && args.every((arg) => typeof arg === "number")) {
|
|
222
|
+
const numbers = args as number[];
|
|
223
|
+
const value =
|
|
224
|
+
cls === undefined
|
|
225
|
+
? numbers[0]
|
|
226
|
+
: new cls(
|
|
227
|
+
...(numbers.length === 1 && lanes > 0 ? Array(lanes).fill(numbers[0]) : numbers),
|
|
228
|
+
);
|
|
229
|
+
Object.defineProperty(node, "value", { configurable: true, value });
|
|
230
|
+
}
|
|
231
|
+
return node;
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `BufferAttribute.updateRanges` as three's live array of `{ start, count }` records, on the web facade.
|
|
3
|
+
* The engine hands the getter canonical JSON and takes ranges through `addUpdateRange` and
|
|
4
|
+
* `clearUpdateRanges`, so a mutation (`push`, `splice`, an index write) is written back as a clear plus an
|
|
5
|
+
* add per record. The V8 adapter installs the same behaviour (adapters/v8/adapter.cpp, rangesSource).
|
|
6
|
+
*/
|
|
7
|
+
interface IRange {
|
|
8
|
+
start: number;
|
|
9
|
+
count: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const MUTATORS = new Set([
|
|
13
|
+
"push",
|
|
14
|
+
"pop",
|
|
15
|
+
"shift",
|
|
16
|
+
"unshift",
|
|
17
|
+
"splice",
|
|
18
|
+
"sort",
|
|
19
|
+
"reverse",
|
|
20
|
+
"fill",
|
|
21
|
+
"copyWithin",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
export function withUpdateRanges(prototype: object): void {
|
|
25
|
+
const read = Object.getOwnPropertyDescriptor(prototype, "updateRanges")?.get;
|
|
26
|
+
const proto = prototype as {
|
|
27
|
+
addUpdateRange?: (start: number, count: number) => void;
|
|
28
|
+
clearUpdateRanges?: () => void;
|
|
29
|
+
};
|
|
30
|
+
const clear = proto.clearUpdateRanges;
|
|
31
|
+
const add = proto.addUpdateRange;
|
|
32
|
+
if (read === undefined || clear === undefined || add === undefined) return;
|
|
33
|
+
const proxies = new WeakMap<object, IRange[]>();
|
|
34
|
+
Object.defineProperty(prototype, "updateRanges", {
|
|
35
|
+
configurable: true,
|
|
36
|
+
get(this: object) {
|
|
37
|
+
const cached = proxies.get(this);
|
|
38
|
+
if (cached !== undefined) return cached;
|
|
39
|
+
const target: IRange[] = [];
|
|
40
|
+
let snapshot: unknown;
|
|
41
|
+
const refresh = () => {
|
|
42
|
+
const current = read.call(this) as unknown;
|
|
43
|
+
if (current === snapshot) return;
|
|
44
|
+
target.splice(0, target.length, ...(JSON.parse(String(current)) as IRange[]));
|
|
45
|
+
snapshot = current;
|
|
46
|
+
};
|
|
47
|
+
const write = () => {
|
|
48
|
+
for (const range of target)
|
|
49
|
+
if (
|
|
50
|
+
!range ||
|
|
51
|
+
!Number.isSafeInteger(range.start) ||
|
|
52
|
+
range.start < 0 ||
|
|
53
|
+
!Number.isSafeInteger(range.count) ||
|
|
54
|
+
range.count < 0
|
|
55
|
+
)
|
|
56
|
+
throw new TypeError(
|
|
57
|
+
"TN_NATIVE_UNSUPPORTED updateRanges: expected nonnegative integer start and count",
|
|
58
|
+
);
|
|
59
|
+
clear.call(this);
|
|
60
|
+
for (const range of target) add.call(this, range.start, range.count);
|
|
61
|
+
snapshot = read.call(this);
|
|
62
|
+
};
|
|
63
|
+
const guarded = <T>(change: () => T): T => {
|
|
64
|
+
refresh();
|
|
65
|
+
const previous = target.slice();
|
|
66
|
+
try {
|
|
67
|
+
return change();
|
|
68
|
+
} catch (error) {
|
|
69
|
+
target.splice(0, target.length, ...previous);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const proxy: IRange[] = new Proxy(target, {
|
|
74
|
+
get(array, key, receiver) {
|
|
75
|
+
refresh();
|
|
76
|
+
if (typeof key === "string" && MUTATORS.has(key))
|
|
77
|
+
return (...args: unknown[]) =>
|
|
78
|
+
guarded(() => {
|
|
79
|
+
const result: unknown = Reflect.apply(
|
|
80
|
+
Reflect.get(array, key) as () => unknown,
|
|
81
|
+
array,
|
|
82
|
+
args,
|
|
83
|
+
);
|
|
84
|
+
write();
|
|
85
|
+
return result === array ? proxy : result;
|
|
86
|
+
});
|
|
87
|
+
return Reflect.get(array, key, receiver);
|
|
88
|
+
},
|
|
89
|
+
set(array, key, value) {
|
|
90
|
+
return guarded(() => {
|
|
91
|
+
Reflect.set(array, key, value);
|
|
92
|
+
write();
|
|
93
|
+
return true;
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
ownKeys(array) {
|
|
97
|
+
refresh();
|
|
98
|
+
return Reflect.ownKeys(array);
|
|
99
|
+
},
|
|
100
|
+
getOwnPropertyDescriptor(array, key) {
|
|
101
|
+
refresh();
|
|
102
|
+
return Reflect.getOwnPropertyDescriptor(array, key);
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
proxies.set(this, proxy);
|
|
106
|
+
return proxy;
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
package/src/wgsl-fn.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** FunctionNode's typed WGSL call surface; the engine links its source into the emitted stage. */
|
|
2
|
+
interface IWgslFunction {
|
|
3
|
+
invoke(...args: unknown[]): unknown;
|
|
4
|
+
}
|
|
5
|
+
const scalar: Readonly<Record<string, string>> = {
|
|
6
|
+
f32: "float",
|
|
7
|
+
i32: "int",
|
|
8
|
+
u32: "uint",
|
|
9
|
+
bool: "bool",
|
|
10
|
+
};
|
|
11
|
+
function nodeType(source: string): string {
|
|
12
|
+
const name = source.replace(/\s/gu, "");
|
|
13
|
+
if (scalar[name]) return scalar[name];
|
|
14
|
+
const vector = /^vec([234])(?:<f32>|f)$/u.exec(name);
|
|
15
|
+
if (vector) return `vec${vector[1]}`;
|
|
16
|
+
const matrix = /^mat([234])x\1(?:<f32>|f)$/u.exec(name);
|
|
17
|
+
if (matrix) return `mat${matrix[1]}`;
|
|
18
|
+
throw new TypeError(`TN_TSL_WGSL_TYPE: unsupported parameter type ${source}`);
|
|
19
|
+
}
|
|
20
|
+
export function defineWgslFn(make: (...args: string[]) => IWgslFunction) {
|
|
21
|
+
return (source: string, includes: readonly unknown[] = []) => {
|
|
22
|
+
if (includes.length)
|
|
23
|
+
throw new TypeError("TN_TSL_WGSL_INCLUDES: function dependencies are not bound");
|
|
24
|
+
const declaration = /^\s*fn\s+([a-zA-Z_]\w*)\s*\(([^)]*)\)\s*->\s*([^{}]+)\s*\{/u.exec(source);
|
|
25
|
+
if (!declaration)
|
|
26
|
+
throw new TypeError("TN_TSL_WGSL_DECLARATION: expected a typed WGSL function");
|
|
27
|
+
const [, name = "", signature = "", result = ""] = declaration;
|
|
28
|
+
const inputs = signature.trim()
|
|
29
|
+
? signature.split(",").map((input) => {
|
|
30
|
+
const member = /^\s*([a-zA-Z_]\w*)\s*:\s*(.+?)\s*$/u.exec(input);
|
|
31
|
+
if (!member)
|
|
32
|
+
throw new TypeError("TN_TSL_WGSL_DECLARATION: expected named typed arguments");
|
|
33
|
+
return { name: member[1] ?? "", type: nodeType(member[2] ?? "") };
|
|
34
|
+
})
|
|
35
|
+
: [];
|
|
36
|
+
if (inputs.length > 4 || new Set(inputs.map(({ name }) => name)).size !== inputs.length)
|
|
37
|
+
throw new TypeError("TN_TSL_WGSL_ARGUMENTS: expected up to four distinct arguments");
|
|
38
|
+
const functionNode = make(source, name, nodeType(result), ...inputs.map(({ type }) => type));
|
|
39
|
+
return (...values: unknown[]) => {
|
|
40
|
+
let args = values;
|
|
41
|
+
if (
|
|
42
|
+
args.length === 1 &&
|
|
43
|
+
typeof args[0] === "object" &&
|
|
44
|
+
args[0] !== null &&
|
|
45
|
+
inputs.every(({ name }) => Object.hasOwn(args[0] as object, name))
|
|
46
|
+
) {
|
|
47
|
+
const named = args[0] as Record<string, unknown>;
|
|
48
|
+
args = inputs.map(({ name }) => named[name]);
|
|
49
|
+
}
|
|
50
|
+
if (args.length !== inputs.length)
|
|
51
|
+
throw new TypeError("TN_TSL_WGSL_ARGUMENTS: arguments must match the declaration");
|
|
52
|
+
return functionNode.invoke(...args);
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
}
|