@threenative/three-native 0.3.11 → 0.3.13
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/package.json +2 -2
- package/src/addons/ccd-ik-solver.ts +170 -0
- package/src/addons/gltf-loader-web.ts +132 -26
- package/src/addons/screen-space.ts +61 -24
- package/src/animation-utils.ts +366 -0
- package/src/box2.ts +172 -0
- package/src/bridge-meter.ts +33 -12
- package/src/browser-abi-types.ts +621 -0
- package/src/browser-attribute-sync.ts +249 -0
- package/src/browser-backend.ts +266 -1650
- package/src/browser-entry.ts +79 -22
- package/src/browser-loaders.ts +12 -0
- package/src/browser-marshal.ts +79 -0
- package/src/browser-renderer.ts +297 -87
- package/src/browser-tsl.ts +146 -23
- package/src/browser-value-views.ts +634 -0
- package/src/browser-wasm-runtime.ts +525 -0
- package/src/buffer-attributes.ts +19 -0
- package/src/compressed-texture.ts +116 -0
- package/src/debug-telemetry.ts +23 -6
- package/src/edges-geometry.ts +111 -0
- package/src/generated/console-markers.ts +13 -0
- package/src/generated/web-frame-fields.ts +84 -0
- package/src/gpu-stages.ts +17 -16
- package/src/grid-helper.ts +49 -0
- package/src/object-surface.ts +47 -6
- package/src/pass-node.ts +354 -10
- package/src/render-target.ts +515 -16
- package/src/shadow-node.ts +24 -1
- package/src/storage.ts +16 -5
- package/src/texture-sources.ts +80 -23
- package/src/tsl-extras.ts +50 -0
- package/src/tsl-uniforms.ts +294 -63
- package/src/update-ranges.ts +50 -1
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser back end's runtime over a loaded ABI module (PRD-532, PRD-803): one engine context,
|
|
3
|
+
* every call checked, and each call's scratch taken from an arena a scope rewinds.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
type EngineValue,
|
|
7
|
+
HANDLE,
|
|
8
|
+
type IAbiHelpers,
|
|
9
|
+
type IBrowserRuntime,
|
|
10
|
+
type IEngineRef,
|
|
11
|
+
type IPackedRef,
|
|
12
|
+
KIND,
|
|
13
|
+
type TnAbiModule,
|
|
14
|
+
VALUE,
|
|
15
|
+
gltfOf,
|
|
16
|
+
isRef,
|
|
17
|
+
packedRef,
|
|
18
|
+
tslOf,
|
|
19
|
+
} from "./browser-abi-types.js";
|
|
20
|
+
import { SCALARS, TYPED, type TypedArray } from "./browser-marshal.js";
|
|
21
|
+
import { debugTelemetry } from "./debug-telemetry.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Opt-in boundary census for `pnpm profile:wasm-page --calls`: while the page sets
|
|
25
|
+
* `globalThis.__tnCallCounts` to a Map, every engine get, set and invoke counts under
|
|
26
|
+
* "<kind> <type id>.<name>" (`__tnEngineTypes` names the type ids). Off, it costs one global read.
|
|
27
|
+
*/
|
|
28
|
+
function countCall(kind: string, self: IEngineRef, name: string): void {
|
|
29
|
+
const counts = (globalThis as { __tnCallCounts?: Map<string, number> }).__tnCallCounts;
|
|
30
|
+
if (counts === undefined) return;
|
|
31
|
+
const key = `${kind} ${self.type}.${name}`;
|
|
32
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createWasmRuntime(abi: TnAbiModule): IBrowserRuntime {
|
|
36
|
+
const debug = debugTelemetry(abi);
|
|
37
|
+
let encoding = "arguments";
|
|
38
|
+
// The heap's views, made again only when the memory grows: Emscripten replaces HEAPU8 then, so one
|
|
39
|
+
// identity test per use stands in for reading two `.buffer`s, which every crossing paid (PRD-748).
|
|
40
|
+
let heap = abi.HEAPU8;
|
|
41
|
+
let heapBytes = heap.byteLength;
|
|
42
|
+
let dataView = new DataView(heap.buffer);
|
|
43
|
+
let words = new Uint32Array(heap.buffer);
|
|
44
|
+
const grown = () => {
|
|
45
|
+
debug?.add("wasmGrowthEvents", 1, "bridge");
|
|
46
|
+
debug?.add("wasmGrowthBytes", Math.max(0, abi.HEAPU8.byteLength - heapBytes), "memory");
|
|
47
|
+
heap = abi.HEAPU8;
|
|
48
|
+
heapBytes = heap.byteLength;
|
|
49
|
+
dataView = new DataView(heap.buffer);
|
|
50
|
+
words = new Uint32Array(heap.buffer);
|
|
51
|
+
};
|
|
52
|
+
const view = () => {
|
|
53
|
+
if (abi.HEAPU8 !== heap) grown();
|
|
54
|
+
return dataView;
|
|
55
|
+
};
|
|
56
|
+
const wordView = () => {
|
|
57
|
+
if (abi.HEAPU8 !== heap) grown();
|
|
58
|
+
return words;
|
|
59
|
+
};
|
|
60
|
+
// Each call frees what it allocated, and only that: a callback can run a nested call.
|
|
61
|
+
// A call's scratch (its handle, arguments, result and diagnostic) comes from one arena that each
|
|
62
|
+
// scope rewinds, a stack as nested calls (a callback inside an invoke) need; a scope that outgrows
|
|
63
|
+
// it falls back to malloc and frees on exit. Every engine call crosses here, many per frame.
|
|
64
|
+
const arenaSize = 64 * 1024;
|
|
65
|
+
const arenaBase = abi._malloc(arenaSize) >>> 0;
|
|
66
|
+
let arenaTop = arenaBase;
|
|
67
|
+
const allocations: number[] = [];
|
|
68
|
+
const alloc = (size: number): number => {
|
|
69
|
+
const aligned = (size + 7) & ~7;
|
|
70
|
+
debug?.add("bridgeScratchBytes", aligned, "bridge");
|
|
71
|
+
let pointer: number;
|
|
72
|
+
if (arenaTop + aligned <= arenaBase + arenaSize) {
|
|
73
|
+
pointer = arenaTop;
|
|
74
|
+
arenaTop += aligned;
|
|
75
|
+
} else {
|
|
76
|
+
// `>>> 0`: the module addresses up to 4 GB, and an export returns a pointer as a signed i32.
|
|
77
|
+
pointer = abi._malloc(aligned) >>> 0;
|
|
78
|
+
allocations.push(pointer);
|
|
79
|
+
debug?.add("bridgeOverflowAllocations", 1, "bridge");
|
|
80
|
+
debug?.add("bridgeOverflowBytes", aligned, "bridge");
|
|
81
|
+
}
|
|
82
|
+
// A call's scratch is a few words (a handle, a value, a diagnostic): word stores beat a fill call.
|
|
83
|
+
// `wordView` first: a `_malloc` above may have grown the memory.
|
|
84
|
+
const w = wordView();
|
|
85
|
+
if (aligned > 64) heap.fill(0, pointer, pointer + aligned);
|
|
86
|
+
else for (let at = pointer >>> 2, end = (pointer + aligned) >>> 2; at < end; at++) w[at] = 0;
|
|
87
|
+
return pointer;
|
|
88
|
+
};
|
|
89
|
+
const scoped = <T>(work: () => T): T => {
|
|
90
|
+
const mark = allocations.length;
|
|
91
|
+
const top = arenaTop;
|
|
92
|
+
try {
|
|
93
|
+
return work();
|
|
94
|
+
} finally {
|
|
95
|
+
while (allocations.length > mark) abi._free(allocations.pop() as number);
|
|
96
|
+
arenaTop = top;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const string = (text: string): { pointer: number; bytes: number } => {
|
|
100
|
+
const bytes = abi.lengthBytesUTF8(text);
|
|
101
|
+
const pointer = alloc(bytes + 1);
|
|
102
|
+
abi.stringToUTF8(text, pointer, bytes + 1);
|
|
103
|
+
return { pointer, bytes };
|
|
104
|
+
};
|
|
105
|
+
// Member, method and class names: a bounded set, encoded once and kept for the module's life.
|
|
106
|
+
const names = new Map<string, number>();
|
|
107
|
+
const name = (text: string): number => {
|
|
108
|
+
let pointer = names.get(text);
|
|
109
|
+
if (pointer === undefined) {
|
|
110
|
+
const bytes = abi.lengthBytesUTF8(text);
|
|
111
|
+
pointer = abi._malloc(bytes + 1) >>> 0;
|
|
112
|
+
abi.stringToUTF8(text, pointer, bytes + 1);
|
|
113
|
+
names.set(text, pointer);
|
|
114
|
+
}
|
|
115
|
+
return pointer;
|
|
116
|
+
};
|
|
117
|
+
const diagnostic = () => alloc(8);
|
|
118
|
+
const check = (status: number, diag: number, what: string) => {
|
|
119
|
+
if (status === 0) return;
|
|
120
|
+
const message = view().getUint32(diag, true);
|
|
121
|
+
const text = message === 0 ? "" : abi.UTF8ToString(message);
|
|
122
|
+
abi._tn_diagnostic_release(diag);
|
|
123
|
+
throw new Error(`TN_ABI_${status}: ${what}${text ? `: ${text}` : ""}`);
|
|
124
|
+
};
|
|
125
|
+
// A handle is three words in memory: type and context as two halves of the first, then index and
|
|
126
|
+
// generation. A ref made here keeps those words, so writing one back is three stores, no parse.
|
|
127
|
+
const keyOf = (pointer: number): IEngineRef => {
|
|
128
|
+
const v = view();
|
|
129
|
+
const type = v.getUint16(pointer, true);
|
|
130
|
+
const context = v.getUint16(pointer + 2, true);
|
|
131
|
+
const index = v.getUint32(pointer + 4, true);
|
|
132
|
+
const generation = v.getUint32(pointer + 8, true);
|
|
133
|
+
const ref: IPackedRef = {
|
|
134
|
+
key: `${type}:${context}:${index}:${generation}`,
|
|
135
|
+
type,
|
|
136
|
+
w0: (type | (context << 16)) >>> 0,
|
|
137
|
+
w1: index,
|
|
138
|
+
w2: generation,
|
|
139
|
+
};
|
|
140
|
+
return ref;
|
|
141
|
+
};
|
|
142
|
+
const writeHandle = (pointer: number, ref: IEngineRef) => {
|
|
143
|
+
const packed = packedRef(ref);
|
|
144
|
+
if ((pointer & 3) === 0) {
|
|
145
|
+
const at = pointer >>> 2;
|
|
146
|
+
const w = wordView();
|
|
147
|
+
w[at] = packed.w0;
|
|
148
|
+
w[at + 1] = packed.w1;
|
|
149
|
+
w[at + 2] = packed.w2;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const v = view();
|
|
153
|
+
v.setUint32(pointer, packed.w0, true);
|
|
154
|
+
v.setUint32(pointer + 4, packed.w1, true);
|
|
155
|
+
v.setUint32(pointer + 8, packed.w2, true);
|
|
156
|
+
};
|
|
157
|
+
const handleOf = (ref: IEngineRef): number => {
|
|
158
|
+
const pointer = alloc(HANDLE);
|
|
159
|
+
writeHandle(pointer, ref);
|
|
160
|
+
return pointer;
|
|
161
|
+
};
|
|
162
|
+
const writeValue = (pointer: number, value: EngineValue) => {
|
|
163
|
+
const v = view();
|
|
164
|
+
if (value === null) return v.setUint32(pointer, KIND.null, true);
|
|
165
|
+
if (value === undefined) return v.setUint32(pointer, KIND.undefined, true);
|
|
166
|
+
if (typeof value === "number") {
|
|
167
|
+
v.setUint32(pointer, KIND.number, true);
|
|
168
|
+
return v.setFloat64(pointer + 8, value, true);
|
|
169
|
+
}
|
|
170
|
+
if (typeof value === "boolean") {
|
|
171
|
+
v.setUint32(pointer, KIND.bool, true);
|
|
172
|
+
return v.setUint32(pointer + 4, value ? 1 : 0, true);
|
|
173
|
+
}
|
|
174
|
+
if (typeof value === "string") {
|
|
175
|
+
const text = string(value);
|
|
176
|
+
const w = view();
|
|
177
|
+
w.setUint32(pointer, KIND.string, true);
|
|
178
|
+
w.setUint32(pointer + 32, text.pointer, true);
|
|
179
|
+
return w.setBigUint64(pointer + 40, BigInt(text.bytes), true);
|
|
180
|
+
}
|
|
181
|
+
if (isRef(value)) {
|
|
182
|
+
v.setUint32(pointer, KIND.handle, true);
|
|
183
|
+
return writeHandle(pointer + 16, value);
|
|
184
|
+
}
|
|
185
|
+
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
|
|
186
|
+
// A forwarded typed array (see `toEngine`): one heap copy, its view bounds kept, no boxed list.
|
|
187
|
+
const typed = value as TypedArray;
|
|
188
|
+
debug?.widen(typed, encoding, typed.byteLength);
|
|
189
|
+
const numbers = alloc(Math.max(8, typed.byteLength));
|
|
190
|
+
abi.HEAPU8.set(new Uint8Array(typed.buffer, typed.byteOffset, typed.byteLength), numbers);
|
|
191
|
+
const name = string(typed.constructor.name).pointer;
|
|
192
|
+
const w = view();
|
|
193
|
+
w.setUint32(pointer, KIND.bytes, true);
|
|
194
|
+
w.setUint32(pointer + 32, name, true);
|
|
195
|
+
w.setUint32(pointer + 48, numbers, true);
|
|
196
|
+
return w.setBigUint64(pointer + 40, BigInt(typed.length), true);
|
|
197
|
+
}
|
|
198
|
+
if (!Array.isArray(value)) {
|
|
199
|
+
// A record: count key/value pairs, each key a string value, as the engine returns one.
|
|
200
|
+
const entries = Object.entries(value as Record<string, EngineValue>);
|
|
201
|
+
const children = values(entries.flat());
|
|
202
|
+
const w = view();
|
|
203
|
+
w.setUint32(pointer, KIND.record, true);
|
|
204
|
+
w.setUint32(pointer + 48, children, true);
|
|
205
|
+
return w.setBigUint64(pointer + 40, BigInt(entries.length), true);
|
|
206
|
+
}
|
|
207
|
+
if (value.every((item) => typeof item === "number")) {
|
|
208
|
+
const numbers = alloc(Math.max(8, value.length * 8));
|
|
209
|
+
abi.HEAPF64.set(value as number[], numbers / 8);
|
|
210
|
+
const typed = (value as { [TYPED]?: string })[TYPED];
|
|
211
|
+
const name = typed === undefined ? 0 : string(typed).pointer;
|
|
212
|
+
const w = view();
|
|
213
|
+
w.setUint32(pointer, KIND.numbers, true);
|
|
214
|
+
w.setUint32(pointer + 32, name, true);
|
|
215
|
+
w.setUint32(pointer + 48, numbers, true);
|
|
216
|
+
w.setBigUint64(pointer + 40, BigInt(value.length), true);
|
|
217
|
+
} else {
|
|
218
|
+
const children = values(value);
|
|
219
|
+
const w = view();
|
|
220
|
+
w.setUint32(pointer, KIND.array, true);
|
|
221
|
+
w.setUint32(pointer + 48, children, true);
|
|
222
|
+
w.setBigUint64(pointer + 40, BigInt(value.length), true);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
const values = (args: readonly EngineValue[]): number => {
|
|
226
|
+
const pointer = alloc(Math.max(VALUE, args.length * VALUE));
|
|
227
|
+
args.forEach((arg, i) => writeValue(pointer + i * VALUE, arg));
|
|
228
|
+
return pointer;
|
|
229
|
+
};
|
|
230
|
+
const readValue = (pointer: number): EngineValue => {
|
|
231
|
+
const v = view();
|
|
232
|
+
switch (v.getUint32(pointer, true)) {
|
|
233
|
+
case KIND.undefined:
|
|
234
|
+
return undefined;
|
|
235
|
+
case KIND.number:
|
|
236
|
+
return v.getFloat64(pointer + 8, true);
|
|
237
|
+
case KIND.bool:
|
|
238
|
+
return v.getUint32(pointer + 4, true) !== 0;
|
|
239
|
+
case KIND.string:
|
|
240
|
+
return abi.UTF8ToString(
|
|
241
|
+
v.getUint32(pointer + 32, true),
|
|
242
|
+
Number(v.getBigUint64(pointer + 40, true)),
|
|
243
|
+
);
|
|
244
|
+
case KIND.handle:
|
|
245
|
+
return keyOf(pointer + 16);
|
|
246
|
+
case KIND.array: {
|
|
247
|
+
const at = v.getUint32(pointer + 48, true);
|
|
248
|
+
return Array.from({ length: Number(v.getBigUint64(pointer + 40, true)) }, (_, i) =>
|
|
249
|
+
readValue(at + i * VALUE),
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
case KIND.record: {
|
|
253
|
+
const at = v.getUint32(pointer + 48, true);
|
|
254
|
+
return Object.fromEntries(
|
|
255
|
+
Array.from({ length: Number(v.getBigUint64(pointer + 40, true)) }, (_, i) => {
|
|
256
|
+
const key = readValue(at + i * 2 * VALUE);
|
|
257
|
+
if (typeof key !== "string") throw new TypeError("TN_ABI_RECORD_KEY: expected string");
|
|
258
|
+
return [key, readValue(at + (i * 2 + 1) * VALUE)];
|
|
259
|
+
}),
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
case KIND.numbers: {
|
|
263
|
+
const at = v.getUint32(pointer + 48, true) / 8;
|
|
264
|
+
return Array.from(
|
|
265
|
+
abi.HEAPF64.subarray(at, at + Number(v.getBigUint64(pointer + 40, true))),
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
default:
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// Callbacks: one invoke and one release trampoline in the function table; the context is a
|
|
274
|
+
// handler id, and the engine's release (once per pair) forgets it.
|
|
275
|
+
const handlers = new Map<number, (args: readonly EngineValue[]) => void>();
|
|
276
|
+
let nextHandler = 1;
|
|
277
|
+
let invokeTrampoline = 0;
|
|
278
|
+
let releaseTrampoline = 0;
|
|
279
|
+
const trampolines = () => {
|
|
280
|
+
if (invokeTrampoline !== 0) return;
|
|
281
|
+
invokeTrampoline = abi.addFunction((id, rawArgs, count, rawError, capacity) => {
|
|
282
|
+
// Pointers arrive as signed i32; the module addresses up to 4 GB.
|
|
283
|
+
const args = (rawArgs ?? 0) >>> 0;
|
|
284
|
+
const error = (rawError ?? 0) >>> 0;
|
|
285
|
+
const handler = handlers.get(id ?? 0);
|
|
286
|
+
if (handler === undefined) return 0;
|
|
287
|
+
const decoded: EngineValue[] = [];
|
|
288
|
+
for (let i = 0; i < (count ?? 0); i++) decoded.push(readValue(args + i * VALUE));
|
|
289
|
+
try {
|
|
290
|
+
handler(decoded);
|
|
291
|
+
return 0;
|
|
292
|
+
} catch (thrown) {
|
|
293
|
+
abi.stringToUTF8(
|
|
294
|
+
thrown instanceof Error ? thrown.message : String(thrown),
|
|
295
|
+
error,
|
|
296
|
+
capacity ?? 0,
|
|
297
|
+
);
|
|
298
|
+
return 8; // TN_ERROR_INVALID_STATE: the engine records it and goes on
|
|
299
|
+
}
|
|
300
|
+
}, "iiiiii");
|
|
301
|
+
releaseTrampoline = abi.addFunction((id) => {
|
|
302
|
+
handlers.delete(id ?? 0);
|
|
303
|
+
return undefined;
|
|
304
|
+
}, "vi");
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// A view over the attribute's storage, used only inside one call: the lease pins the storage and
|
|
308
|
+
// nothing in between can grow the memory and detach the view. A kept view would detach on growth,
|
|
309
|
+
// so the back end keeps JS arrays and copies through this (see defineBrowserClasses).
|
|
310
|
+
const withAttributeView = <T>(self: IEngineRef, use: (view: TypedArray) => T): T =>
|
|
311
|
+
scoped(() => {
|
|
312
|
+
const call = abi._tnw_attribute_view;
|
|
313
|
+
if (call === undefined)
|
|
314
|
+
throw new TypeError("TN_WASM_MODULE: this module exports no attribute views");
|
|
315
|
+
const out = alloc(24);
|
|
316
|
+
const lease = call(handleOf(self), out);
|
|
317
|
+
if (lease === 0) throw new TypeError("TN_NATIVE_UNSUPPORTED array: not an attribute");
|
|
318
|
+
try {
|
|
319
|
+
const v = view();
|
|
320
|
+
const [address, count, scalar] = [0, 8, 16].map((at) =>
|
|
321
|
+
Number(v.getBigUint64(out + at, true)),
|
|
322
|
+
);
|
|
323
|
+
const Typed = SCALARS[scalar ?? -1];
|
|
324
|
+
if (Typed === undefined)
|
|
325
|
+
throw new TypeError(`TN_NATIVE_UNSUPPORTED array: scalar ${String(scalar)}`);
|
|
326
|
+
return use(new Typed(abi.HEAPU8.buffer as ArrayBuffer, address ?? 0, count ?? 0));
|
|
327
|
+
} finally {
|
|
328
|
+
abi._tnw_attribute_view_release?.(lease);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const context = scoped(() => {
|
|
333
|
+
const version = alloc(32);
|
|
334
|
+
abi._tn_engine_version(version);
|
|
335
|
+
const contextOut = alloc(4);
|
|
336
|
+
const diag = diagnostic();
|
|
337
|
+
check(abi._tn_context_create(contextOut, version, diag), diag, "tn_context_create");
|
|
338
|
+
return view().getUint32(contextOut, true);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
return {
|
|
342
|
+
...(tslOf(abi, context, {
|
|
343
|
+
scoped,
|
|
344
|
+
alloc,
|
|
345
|
+
string,
|
|
346
|
+
diagnostic,
|
|
347
|
+
check,
|
|
348
|
+
handleOf,
|
|
349
|
+
writeHandle,
|
|
350
|
+
view,
|
|
351
|
+
}) ?? {}),
|
|
352
|
+
...gltfOf(abi, context, { scoped, alloc, keyOf }),
|
|
353
|
+
...(debug === undefined ? {} : { debugTelemetry: debug }),
|
|
354
|
+
typedArrays: true,
|
|
355
|
+
typeId: (className) => abi._tn_type_id(name(className)),
|
|
356
|
+
construct: (className, args) =>
|
|
357
|
+
scoped(() => {
|
|
358
|
+
const out = alloc(HANDLE);
|
|
359
|
+
const diag = diagnostic();
|
|
360
|
+
check(
|
|
361
|
+
abi._tn_construct(
|
|
362
|
+
context,
|
|
363
|
+
string(className).pointer,
|
|
364
|
+
values(args),
|
|
365
|
+
args.length,
|
|
366
|
+
out,
|
|
367
|
+
diag,
|
|
368
|
+
),
|
|
369
|
+
diag,
|
|
370
|
+
`new ${className}`,
|
|
371
|
+
);
|
|
372
|
+
return keyOf(out);
|
|
373
|
+
}),
|
|
374
|
+
invoke: (self, method, args) =>
|
|
375
|
+
scoped(() => {
|
|
376
|
+
countCall("call", self, method);
|
|
377
|
+
if (debug !== undefined) encoding = method;
|
|
378
|
+
const out = alloc(VALUE);
|
|
379
|
+
const diag = diagnostic();
|
|
380
|
+
check(
|
|
381
|
+
abi._tn_invoke(handleOf(self), name(method), values(args), args.length, out, diag),
|
|
382
|
+
diag,
|
|
383
|
+
`${method}()`,
|
|
384
|
+
);
|
|
385
|
+
return readValue(out);
|
|
386
|
+
}),
|
|
387
|
+
engineReferences: (refs) =>
|
|
388
|
+
scoped(() => {
|
|
389
|
+
const handles = alloc(Math.max(1, refs.length) * HANDLE);
|
|
390
|
+
refs.forEach((ref, i) => writeHandle(handles + i * HANDLE, ref));
|
|
391
|
+
const out = alloc(Math.max(1, refs.length) * 4);
|
|
392
|
+
abi._tn_object_engine_references(handles, refs.length, out);
|
|
393
|
+
const v = view();
|
|
394
|
+
return refs.map((_, i) => v.getUint32(out + i * 4, true));
|
|
395
|
+
}),
|
|
396
|
+
// PRD-828: the rig batch, one crossing. Handles are three words each; a rig without an action or
|
|
397
|
+
// a stride body repeats its mixer's handle there, and the flags word says which of the two exist.
|
|
398
|
+
animTick: (batch) => {
|
|
399
|
+
const count = batch.mixers.length;
|
|
400
|
+
if (count === 0) return;
|
|
401
|
+
scoped(() => {
|
|
402
|
+
const triples = alloc(count * 3 * HANDLE);
|
|
403
|
+
for (let i = 0; i < count; i += 1) {
|
|
404
|
+
const at = triples + i * 3 * HANDLE;
|
|
405
|
+
const mixer = batch.mixers[i] as IEngineRef;
|
|
406
|
+
writeHandle(at, mixer);
|
|
407
|
+
writeHandle(at + HANDLE, batch.actions[i] ?? mixer);
|
|
408
|
+
writeHandle(at + 2 * HANDLE, batch.roots[i] ?? mixer);
|
|
409
|
+
}
|
|
410
|
+
const values = alloc(count * 5 * 8);
|
|
411
|
+
const status = alloc(count * 3 * 8);
|
|
412
|
+
const v = view();
|
|
413
|
+
for (let i = 0; i < count * 5; i += 1)
|
|
414
|
+
v.setFloat64(values + i * 8, batch.values[i] as number, true);
|
|
415
|
+
const diag = diagnostic();
|
|
416
|
+
check(
|
|
417
|
+
abi._tn_anim_tick(context, triples, values, count, status, diag),
|
|
418
|
+
diag,
|
|
419
|
+
"tn_anim_tick",
|
|
420
|
+
);
|
|
421
|
+
const after = view();
|
|
422
|
+
for (let i = 0; i < count * 3; i += 1)
|
|
423
|
+
batch.status[i] = after.getFloat64(status + i * 8, true);
|
|
424
|
+
});
|
|
425
|
+
},
|
|
426
|
+
readByte: (address) => abi.HEAPU8[address] as number,
|
|
427
|
+
readWord: (address) => view().getUint32(address, true),
|
|
428
|
+
writeDouble: (address, value) => {
|
|
429
|
+
abi.HEAPF64[address / 8] = value;
|
|
430
|
+
},
|
|
431
|
+
writeByte: (address, value) => {
|
|
432
|
+
abi.HEAPU8[address] = value;
|
|
433
|
+
},
|
|
434
|
+
heapF64: () => abi.HEAPF64,
|
|
435
|
+
readDoubles: (address, count) => {
|
|
436
|
+
const at = address / 8;
|
|
437
|
+
return count === 1
|
|
438
|
+
? (abi.HEAPF64[at] as number)
|
|
439
|
+
: Array.from(abi.HEAPF64.subarray(at, at + count));
|
|
440
|
+
},
|
|
441
|
+
get: (self, path) =>
|
|
442
|
+
scoped(() => {
|
|
443
|
+
countCall("get", self, path);
|
|
444
|
+
const out = alloc(VALUE);
|
|
445
|
+
const diag = diagnostic();
|
|
446
|
+
check(abi._tn_get(handleOf(self), name(path), out, diag), diag, `get ${path}`);
|
|
447
|
+
return readValue(out);
|
|
448
|
+
}),
|
|
449
|
+
set: (self, path, value) =>
|
|
450
|
+
scoped(() => {
|
|
451
|
+
countCall("set", self, path);
|
|
452
|
+
if (debug !== undefined) encoding = path;
|
|
453
|
+
// One value written in place: no one-item list per write.
|
|
454
|
+
const pointer = alloc(VALUE);
|
|
455
|
+
writeValue(pointer, value);
|
|
456
|
+
const diag = diagnostic();
|
|
457
|
+
check(abi._tn_set(handleOf(self), name(path), pointer, diag), diag, `set ${path}`);
|
|
458
|
+
}),
|
|
459
|
+
release: (self) =>
|
|
460
|
+
scoped(() => {
|
|
461
|
+
const diag = diagnostic();
|
|
462
|
+
abi._tn_object_release(handleOf(self), diag);
|
|
463
|
+
abi._tn_diagnostic_release(diag);
|
|
464
|
+
}),
|
|
465
|
+
attributeArray: (self, offset, count) =>
|
|
466
|
+
withAttributeView(self, (target) => {
|
|
467
|
+
const from = offset === undefined ? 0 : offset;
|
|
468
|
+
const length = count === undefined ? target.length - from : count;
|
|
469
|
+
if (
|
|
470
|
+
!Number.isSafeInteger(from) ||
|
|
471
|
+
!Number.isSafeInteger(length) ||
|
|
472
|
+
from < 0 ||
|
|
473
|
+
length < 0 ||
|
|
474
|
+
from + length > target.length
|
|
475
|
+
)
|
|
476
|
+
throw new RangeError(
|
|
477
|
+
`TN_NATIVE_ATTRIBUTE_RANGE: [${String(from)}, ${String(length)}) is outside ${String(target.length)} elements`,
|
|
478
|
+
);
|
|
479
|
+
return target.subarray(from, from + length).slice();
|
|
480
|
+
}),
|
|
481
|
+
attributeWrite: (self, array, offset) =>
|
|
482
|
+
withAttributeView(self, (target) => {
|
|
483
|
+
// An omitted offset writes the whole attribute, so the array must be its exact data; an
|
|
484
|
+
// explicit offset (including 0) writes that subrange.
|
|
485
|
+
if (offset === undefined) {
|
|
486
|
+
if (target.length !== array.length)
|
|
487
|
+
throw new RangeError(
|
|
488
|
+
`TN_NATIVE_ATTRIBUTE_LENGTH: attribute.array has ${String(array.length)} elements, the attribute ${String(target.length)}`,
|
|
489
|
+
);
|
|
490
|
+
target.set(array);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset + array.length > target.length)
|
|
494
|
+
throw new RangeError(
|
|
495
|
+
`TN_NATIVE_ATTRIBUTE_LENGTH: attribute.array has ${String(array.length)} elements at ${String(offset)}, the attribute ${String(target.length)}`,
|
|
496
|
+
);
|
|
497
|
+
target.set(array, offset);
|
|
498
|
+
}),
|
|
499
|
+
setCallback: (self, name, handler) =>
|
|
500
|
+
scoped(() => {
|
|
501
|
+
const diag = diagnostic();
|
|
502
|
+
if (handler === null) {
|
|
503
|
+
check(
|
|
504
|
+
abi._tn_set_callback(handleOf(self), string(name).pointer, 0, 0, 0, diag),
|
|
505
|
+
diag,
|
|
506
|
+
`clear ${name}`,
|
|
507
|
+
);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
trampolines();
|
|
511
|
+
const id = nextHandler++;
|
|
512
|
+
handlers.set(id, handler);
|
|
513
|
+
const status = abi._tn_set_callback(
|
|
514
|
+
handleOf(self),
|
|
515
|
+
string(name).pointer,
|
|
516
|
+
invokeTrampoline,
|
|
517
|
+
id,
|
|
518
|
+
releaseTrampoline,
|
|
519
|
+
diag,
|
|
520
|
+
);
|
|
521
|
+
if (status !== 0) handlers.delete(id); // a refused pair was never taken
|
|
522
|
+
check(status, diag, `set ${name}`);
|
|
523
|
+
}),
|
|
524
|
+
};
|
|
525
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// three's typed BufferAttribute subclasses, ported from three r187 src/core/BufferAttribute.js (MIT).
|
|
2
|
+
// Each one only converts its array: the engine BufferAttribute keeps a Uint8Array, Uint16Array or
|
|
3
|
+
// Uint32Array in its own storage type. Int16BufferAttribute stays unbound: the engine stores an
|
|
4
|
+
// Int16Array as float32, so a normalized Int16 attribute would read wrong on the GPU.
|
|
5
|
+
|
|
6
|
+
type Attribute = new (array: ArrayLike<number>, itemSize: number, normalized?: boolean) => object;
|
|
7
|
+
|
|
8
|
+
/** `Uint16BufferAttribute` is `defineTypedBufferAttribute(BufferAttribute, Uint16Array)`. */
|
|
9
|
+
export function defineTypedBufferAttribute(
|
|
10
|
+
base: Attribute,
|
|
11
|
+
storage: new (array: ArrayLike<number>) => ArrayLike<number>,
|
|
12
|
+
) {
|
|
13
|
+
return class extends base {
|
|
14
|
+
constructor(array: ArrayLike<number>, itemSize: number, normalized = false) {
|
|
15
|
+
// three's default, explicit: the engine constructor refuses an undefined `normalized`.
|
|
16
|
+
super(new storage(array), itemSize, normalized);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* three's CompressedTexture on the browser back end (PRD-867): BC7 or ETC2 RGBA levels built from
|
|
3
|
+
* three's `mipmaps`, each crossing to the engine once, coarse first, so a streamed finer level goes
|
|
4
|
+
* up alone.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
type EngineValue,
|
|
8
|
+
type IBrowserRuntime,
|
|
9
|
+
type IEngineRef,
|
|
10
|
+
engineRef,
|
|
11
|
+
} from "./browser-backend.js";
|
|
12
|
+
|
|
13
|
+
type EngineClass = new (...args: unknown[]) => object;
|
|
14
|
+
|
|
15
|
+
/** One of three's `CompressedTexture.mipmaps`: a level's blocks, or null while it is not resident. */
|
|
16
|
+
interface ICompressedLevel {
|
|
17
|
+
data: ArrayLike<number> | null;
|
|
18
|
+
readonly width: number;
|
|
19
|
+
readonly height: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* three's CompressedTexture(mipmaps, width, height, format) over the engine's (PRD-867), or undefined
|
|
24
|
+
* when the engine binds none. The engine takes the level count; each level then crosses as one
|
|
25
|
+
* `mipmaps.N.data`, coarse first.
|
|
26
|
+
*/
|
|
27
|
+
export function defineCompressedTexture(
|
|
28
|
+
BaseCompressed: EngineClass | undefined,
|
|
29
|
+
runtime: IBrowserRuntime,
|
|
30
|
+
): unknown {
|
|
31
|
+
if (BaseCompressed === undefined) return undefined;
|
|
32
|
+
const Base: EngineClass = BaseCompressed;
|
|
33
|
+
/** A typed array as the runtime takes it: itself when typed arrays cross, else its numbers. */
|
|
34
|
+
const crossing = (data: ArrayLike<number>): EngineValue =>
|
|
35
|
+
runtime.typedArrays === true && ArrayBuffer.isView(data) && !(data instanceof DataView)
|
|
36
|
+
? (data as EngineValue)
|
|
37
|
+
: Array.from(data);
|
|
38
|
+
// Each CompressedTexture's `mipmaps`, and the `data` of each level the engine already holds.
|
|
39
|
+
const levels = new WeakMap<object, ICompressedLevel[]>();
|
|
40
|
+
const sent = new WeakMap<object, unknown[]>();
|
|
41
|
+
/** Sends the levels whose `data` changed, coarse first: the engine keeps the finest resident level as the base. */
|
|
42
|
+
const sendLevels = (texture: object): void => {
|
|
43
|
+
const mipmaps = levels.get(texture) ?? [];
|
|
44
|
+
const held = sent.get(texture) ?? [];
|
|
45
|
+
for (let level = mipmaps.length - 1; level >= 0; level -= 1) {
|
|
46
|
+
const data = mipmaps[level]?.data;
|
|
47
|
+
if (data == null || held[level] === data) continue;
|
|
48
|
+
runtime.set(
|
|
49
|
+
engineRef(texture) as IEngineRef,
|
|
50
|
+
`mipmaps.${String(level)}.data`,
|
|
51
|
+
crossing(data),
|
|
52
|
+
);
|
|
53
|
+
held[level] = data;
|
|
54
|
+
}
|
|
55
|
+
sent.set(texture, held);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* three's CompressedTexture(mipmaps, width, height, format) over the engine's (PRD-867): BC7
|
|
60
|
+
* (`RGBA_BPTC_Format`) or ETC2 RGBA (`RGBA_ETC2_EAC_Format`) levels, finest first. A level whose `data`
|
|
61
|
+
* is null is not resident yet: set it and `needsUpdate`, and that level alone goes up, as a stream does.
|
|
62
|
+
*/
|
|
63
|
+
function CompressedTexture(
|
|
64
|
+
this: unknown,
|
|
65
|
+
mipmaps: ICompressedLevel[] = [],
|
|
66
|
+
width = 1,
|
|
67
|
+
height = 1,
|
|
68
|
+
format = 1023,
|
|
69
|
+
) {
|
|
70
|
+
const texture = Reflect.construct(
|
|
71
|
+
Base,
|
|
72
|
+
[mipmaps.length, width, height, format],
|
|
73
|
+
new.target ?? CompressedTexture,
|
|
74
|
+
) as object;
|
|
75
|
+
levels.set(texture, mipmaps);
|
|
76
|
+
sendLevels(texture);
|
|
77
|
+
return texture;
|
|
78
|
+
}
|
|
79
|
+
{
|
|
80
|
+
CompressedTexture.prototype = BaseCompressed.prototype;
|
|
81
|
+
const needsUpdate = Object.getOwnPropertyDescriptor(
|
|
82
|
+
BaseCompressed.prototype,
|
|
83
|
+
"needsUpdate",
|
|
84
|
+
)?.set;
|
|
85
|
+
if (needsUpdate === undefined)
|
|
86
|
+
throw new TypeError("TN_BROWSER_UNBOUND: CompressedTexture.needsUpdate");
|
|
87
|
+
Object.defineProperties(BaseCompressed.prototype, {
|
|
88
|
+
mipmaps: {
|
|
89
|
+
configurable: true,
|
|
90
|
+
get(this: object) {
|
|
91
|
+
return levels.get(this) ?? [];
|
|
92
|
+
},
|
|
93
|
+
set(this: object, mipmaps: ICompressedLevel[]) {
|
|
94
|
+
levels.set(this, mipmaps);
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
image: {
|
|
98
|
+
configurable: true,
|
|
99
|
+
get(this: object) {
|
|
100
|
+
const base = levels.get(this)?.[0];
|
|
101
|
+
return { width: base?.width ?? 0, height: base?.height ?? 0 };
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
needsUpdate: {
|
|
105
|
+
configurable: true,
|
|
106
|
+
set(this: object, value: unknown) {
|
|
107
|
+
// A level the engine holds already is not re-sent: after one new level, the engine writes that one.
|
|
108
|
+
if (value) sendLevels(this);
|
|
109
|
+
needsUpdate.call(this, value);
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return CompressedTexture;
|
|
116
|
+
}
|