@threenative/three-native 0.3.12 → 0.3.15
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 +1 -1
- package/src/addons/ccd-ik-solver.ts +170 -0
- package/src/addons/gltf-loader-web.ts +104 -17
- package/src/addons/screen-space.ts +43 -21
- package/src/animation-utils.ts +366 -0
- package/src/box2.ts +172 -0
- package/src/bridge-meter.ts +23 -9
- package/src/browser-abi-types.ts +260 -43
- package/src/browser-attribute-sync.ts +69 -0
- package/src/browser-backend.ts +75 -8
- package/src/browser-entry.ts +51 -12
- package/src/browser-loaders.ts +12 -0
- package/src/browser-renderer.ts +194 -118
- package/src/browser-tsl.ts +93 -16
- package/src/browser-value-views.ts +18 -2
- package/src/browser-wasm-runtime.ts +72 -30
- package/src/buffer-attributes.ts +19 -0
- package/src/compressed-texture.ts +116 -0
- package/src/debug-telemetry.ts +20 -6
- package/src/edges-geometry.ts +111 -0
- package/src/generated/console-markers.ts +14 -0
- package/src/generated/web-frame-fields.ts +85 -0
- package/src/gpu-stages.ts +17 -16
- package/src/grid-helper.ts +49 -0
- package/src/object-surface.ts +49 -6
- package/src/pass-node.ts +26 -6
- package/src/render-target.ts +478 -12
- package/src/shadow-node.ts +24 -1
- package/src/storage.ts +16 -5
- package/src/texture-sources.ts +12 -6
- package/src/tsl-uniforms.ts +146 -57
- package/src/update-ranges.ts +27 -1
package/src/browser-abi-types.ts
CHANGED
|
@@ -14,6 +14,18 @@ export interface IPackedRef extends IEngineRef {
|
|
|
14
14
|
w2: number;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* The ref's handle words. A ref the runtime made carries them; any other ref is parsed once and keeps them, so
|
|
19
|
+
* writing a handle is three stores and never a key parse per call.
|
|
20
|
+
*/
|
|
21
|
+
export function packedRef(ref: IEngineRef): IPackedRef {
|
|
22
|
+
const known = ref as Partial<IPackedRef>;
|
|
23
|
+
if (known.w0 !== undefined) return known as IPackedRef;
|
|
24
|
+
const [type = 0, context = 0, index = 0, generation = 0] = ref.key.split(":").map(Number);
|
|
25
|
+
const words = { w0: (type | (context << 16)) >>> 0, w1: index, w2: generation };
|
|
26
|
+
return Object.isExtensible(ref) ? Object.assign(ref, words) : { ...ref, ...words };
|
|
27
|
+
}
|
|
28
|
+
|
|
17
29
|
/** An engine object as the back end sees it: an opaque key and its catalog type id. */
|
|
18
30
|
export interface IEngineRef {
|
|
19
31
|
readonly key: string;
|
|
@@ -35,6 +47,21 @@ export type EngineValue =
|
|
|
35
47
|
| IEngineRef
|
|
36
48
|
| { readonly [key: string]: EngineValue };
|
|
37
49
|
|
|
50
|
+
/** PRD-828: one rig of an animation tick batch, as the backend takes it. */
|
|
51
|
+
export interface IAnimationTickBatch {
|
|
52
|
+
/** One entry per rig: the mixer, the rig's action (null when nothing plays), the body it strides from. */
|
|
53
|
+
readonly mixers: readonly IEngineRef[];
|
|
54
|
+
readonly actions: readonly (IEngineRef | null)[];
|
|
55
|
+
readonly roots: readonly (IEngineRef | null)[];
|
|
56
|
+
/**
|
|
57
|
+
* Five per rig: the stride interval, this tick's own time, the clip's ground speed, the rate this rule
|
|
58
|
+
* last applied (0 for none), and the flags word.
|
|
59
|
+
*/
|
|
60
|
+
readonly values: Float64Array;
|
|
61
|
+
/** Three per rig, written by the call: stride rate, measured ground speed, flags. */
|
|
62
|
+
readonly status: Float64Array;
|
|
63
|
+
}
|
|
64
|
+
|
|
38
65
|
/** What the classes call; the Wasm ABI implements it, and a surface-only runtime refuses every call. */
|
|
39
66
|
export interface IBrowserRuntime {
|
|
40
67
|
readonly debugTelemetry?: DebugTelemetry;
|
|
@@ -46,15 +73,20 @@ export interface IBrowserRuntime {
|
|
|
46
73
|
release(self: IEngineRef): void;
|
|
47
74
|
/** For each ref, how many references other engine objects hold to its object; one call a batch. */
|
|
48
75
|
engineReferences?(refs: readonly IEngineRef[]): ArrayLike<number>;
|
|
76
|
+
/**
|
|
77
|
+
* PRD-828: advances a batch of animation rigs in one crossing — the weight ramp, the stride rate and
|
|
78
|
+
* one mixer step each. A backend that cannot batch leaves it out and the JS loop keeps running.
|
|
79
|
+
*/
|
|
80
|
+
animTick?(batch: IAnimationTickBatch): void;
|
|
49
81
|
/** A numeric typed array argument crosses as is, no boxed copy; Wasm only. */
|
|
50
82
|
readonly typedArrays?: boolean;
|
|
51
83
|
/** Doubles at `address` in the engine's memory (one as a number, more as an array); Wasm only. */
|
|
52
84
|
readDoubles?(address: number, count: number): number | number[];
|
|
53
85
|
/**
|
|
54
|
-
* The engine's memory as doubles, current
|
|
55
|
-
* reads it per operation and never keeps it. Wasm only.
|
|
86
|
+
* The engine's memory as doubles, current and reaching at least byte `end`: the view is replaced
|
|
87
|
+
* when the memory grows, so a caller reads it per operation and never keeps it. Wasm only.
|
|
56
88
|
*/
|
|
57
|
-
heapF64?(): Float64Array;
|
|
89
|
+
heapF64?(end: number): Float64Array;
|
|
58
90
|
/** The byte at `address` in the engine's memory; Wasm only. A registry field `[offset, 0]` is a byte flag. */
|
|
59
91
|
readByte?(address: number): number;
|
|
60
92
|
/** The 32-bit word at `address` in the engine's memory (the hierarchy counter's low word); Wasm only. */
|
|
@@ -85,7 +117,22 @@ export interface IBrowserRuntime {
|
|
|
85
117
|
loadGltf?(
|
|
86
118
|
bytes: Uint8Array,
|
|
87
119
|
files?: ReadonlyMap<string, Uint8Array>,
|
|
88
|
-
): {
|
|
120
|
+
): {
|
|
121
|
+
readonly scene: IEngineRef;
|
|
122
|
+
readonly animations: IEngineRef[];
|
|
123
|
+
/** Every scene in file order; the default scene is one of them. */
|
|
124
|
+
readonly scenes: IEngineRef[];
|
|
125
|
+
};
|
|
126
|
+
/** `loadGltf` on the engine's loader thread (PRD-860), when the module carries it. */
|
|
127
|
+
loadGltfAsync?(
|
|
128
|
+
bytes: Uint8Array,
|
|
129
|
+
files?: ReadonlyMap<string, Uint8Array>,
|
|
130
|
+
): Promise<{
|
|
131
|
+
readonly scene: IEngineRef;
|
|
132
|
+
readonly animations: IEngineRef[];
|
|
133
|
+
/** Every scene in file order; the default scene is one of them. */
|
|
134
|
+
readonly scenes: IEngineRef[];
|
|
135
|
+
}>;
|
|
89
136
|
}
|
|
90
137
|
|
|
91
138
|
/**
|
|
@@ -105,7 +152,8 @@ type AbiCall =
|
|
|
105
152
|
| "_tn_get"
|
|
106
153
|
| "_tn_set"
|
|
107
154
|
| "_tn_set_callback"
|
|
108
|
-
| "_tn_diagnostic_release"
|
|
155
|
+
| "_tn_diagnostic_release"
|
|
156
|
+
| "_tn_anim_tick";
|
|
109
157
|
export type TnAbiModule = Record<AbiCall, (...args: number[]) => number> &
|
|
110
158
|
Partial<
|
|
111
159
|
Record<"_tnw_attribute_view" | "_tnw_attribute_view_release", (...args: number[]) => number>
|
|
@@ -121,6 +169,28 @@ export type TnAbiModule = Record<AbiCall, (...args: number[]) => number> &
|
|
|
121
169
|
lengthBytesUTF8(text: string): number;
|
|
122
170
|
};
|
|
123
171
|
|
|
172
|
+
/**
|
|
173
|
+
* The engine's memory as bytes, reaching at least `end`. Shared memory (the threaded build) grows from
|
|
174
|
+
* any thread, and Emscripten refreshes a thread's views only inside its own glue: after a pool worker
|
|
175
|
+
* grew it, HEAPU8 here stayed short and a pointer past it threw "Offset is outside the bounds of the
|
|
176
|
+
* DataView". A shared buffer never detaches, so a short view is right below its end: only an access
|
|
177
|
+
* past it reads `memory.buffer`, and the fresh views go onto the module for every other reader.
|
|
178
|
+
*/
|
|
179
|
+
export function heapFor(abi: TnAbiModule, end: number): Uint8Array {
|
|
180
|
+
if (end <= abi.HEAPU8.byteLength) return abi.HEAPU8;
|
|
181
|
+
const buffer = (abi as { wasmMemory?: WebAssembly.Memory }).wasmMemory?.buffer;
|
|
182
|
+
if (
|
|
183
|
+
buffer !== undefined &&
|
|
184
|
+
buffer.byteLength > abi.HEAPU8.byteLength &&
|
|
185
|
+
typeof SharedArrayBuffer !== "undefined" &&
|
|
186
|
+
buffer instanceof SharedArrayBuffer
|
|
187
|
+
) {
|
|
188
|
+
abi.HEAPU8 = new Uint8Array(buffer);
|
|
189
|
+
abi.HEAPF64 = new Float64Array(buffer);
|
|
190
|
+
}
|
|
191
|
+
return abi.HEAPU8;
|
|
192
|
+
}
|
|
193
|
+
|
|
124
194
|
// wasm32 layouts of tn_abi.h, measured with offsetof under emcc: tn_handle_t 12 bytes; tn_value_t
|
|
125
195
|
// 56 bytes (kind 0, boolean 4, number 8, handle 16, text 32, count 40, numbers 48); tn_diagnostic_t
|
|
126
196
|
// 8 bytes (message 0, code 4); tn_version_info_t 32 bytes. A struct passed by value goes by pointer.
|
|
@@ -157,6 +227,8 @@ type TslCall =
|
|
|
157
227
|
| "_tn_tsl_release"
|
|
158
228
|
| "_tn_tsl_set"
|
|
159
229
|
| "_tn_tsl_set_uniform"
|
|
230
|
+
| "_tn_tsl_set_uniforms"
|
|
231
|
+
| "_tn_tsl_reads"
|
|
160
232
|
| "_tn_tsl_effect_parameter"
|
|
161
233
|
| "_tnw_web_set_post"
|
|
162
234
|
| "_tnw_web_compute"
|
|
@@ -176,7 +248,18 @@ export interface IAbiHelpers {
|
|
|
176
248
|
check(status: number, diag: number, what: string): void;
|
|
177
249
|
handleOf(ref: IEngineRef): number;
|
|
178
250
|
writeHandle(pointer: number, ref: IEngineRef): void;
|
|
179
|
-
|
|
251
|
+
/** The heap's DataView, reaching at least byte `end` (pass it for a pointer `alloc` did not make). */
|
|
252
|
+
view(end?: number): DataView;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** `tnw_web_load_gltf`'s handles: the default scene, the clips, then every scene in file order. */
|
|
256
|
+
function splitModel(refs: IEngineRef[], scenes: number) {
|
|
257
|
+
const clipsEnd = refs.length - scenes;
|
|
258
|
+
return {
|
|
259
|
+
scene: refs[0] as IEngineRef,
|
|
260
|
+
animations: refs.slice(1, clipsEnd),
|
|
261
|
+
scenes: refs.slice(clipsEnd),
|
|
262
|
+
};
|
|
180
263
|
}
|
|
181
264
|
|
|
182
265
|
/** The engine glTF loader over the product web host's `tnw_web_load_gltf`, when the module has it. */
|
|
@@ -184,11 +267,14 @@ export function gltfOf(
|
|
|
184
267
|
abi: TnAbiModule,
|
|
185
268
|
context: number,
|
|
186
269
|
h: Pick<IAbiHelpers, "scoped" | "alloc"> & { keyOf(pointer: number): IEngineRef },
|
|
187
|
-
): Pick<IBrowserRuntime, "loadGltf"> {
|
|
270
|
+
): Pick<IBrowserRuntime, "loadGltf" | "loadGltfAsync"> {
|
|
188
271
|
// quality-allow: the Emscripten module is read through the exports this call site names, not its full surface.
|
|
189
272
|
const host = abi as unknown as Partial<
|
|
190
273
|
Record<
|
|
191
274
|
| "_tnw_web_load_gltf"
|
|
275
|
+
| "_tnw_web_load_gltf_begin"
|
|
276
|
+
| "_tnw_web_load_gltf_done"
|
|
277
|
+
| "_tnw_web_load_gltf_finish"
|
|
192
278
|
| "_tnw_web_load_error"
|
|
193
279
|
| "_tnw_web_gltf_file"
|
|
194
280
|
| "_tnw_web_gltf_files_clear",
|
|
@@ -197,56 +283,99 @@ export function gltfOf(
|
|
|
197
283
|
>;
|
|
198
284
|
const {
|
|
199
285
|
_tnw_web_load_gltf: load,
|
|
286
|
+
_tnw_web_load_gltf_begin: begin,
|
|
287
|
+
_tnw_web_load_gltf_done: done,
|
|
288
|
+
_tnw_web_load_gltf_finish: finish,
|
|
200
289
|
_tnw_web_load_error: error,
|
|
201
290
|
_tnw_web_gltf_file: file,
|
|
202
291
|
_tnw_web_gltf_files_clear: clearFiles,
|
|
203
292
|
} = host;
|
|
204
293
|
if (load === undefined || error === undefined) return {};
|
|
205
|
-
|
|
206
|
-
|
|
294
|
+
/** Files the model names by relative uri (shared images), kept for the engine loader's next load. */
|
|
295
|
+
const stageFiles = (files: ReadonlyMap<string, Uint8Array> | undefined): void => {
|
|
296
|
+
if (files === undefined || files.size === 0) return;
|
|
297
|
+
if (file === undefined || clearFiles === undefined)
|
|
298
|
+
throw new Error(
|
|
299
|
+
"TN_WASM_GLTF_FILES_UNAVAILABLE: this web engine cannot read a model's image files",
|
|
300
|
+
);
|
|
301
|
+
for (const [uri, content] of files) {
|
|
302
|
+
// tnw_web_gltf_file keeps its own copy, so this scratch is dead once it returns. One scope
|
|
303
|
+
// per file frees it at once: the peak is one file, not the sum of all of them.
|
|
207
304
|
h.scoped(() => {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
305
|
+
const name = h.alloc(abi.lengthBytesUTF8(uri) + 1);
|
|
306
|
+
abi.stringToUTF8(uri, name, abi.lengthBytesUTF8(uri) + 1);
|
|
307
|
+
const copy = h.alloc(Math.max(1, content.byteLength));
|
|
308
|
+
abi.HEAPU8.set(content, copy);
|
|
309
|
+
file(name, copy, content.byteLength);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
/**
|
|
314
|
+
* The handles `share(out, capacity, count, scenes)` writes, retried once at the exact size for a
|
|
315
|
+
* model with many clips.
|
|
316
|
+
*/
|
|
317
|
+
const handles = (
|
|
318
|
+
share: (out: number, capacity: number, count: number, scenes: number) => number,
|
|
319
|
+
) =>
|
|
320
|
+
h.scoped(() => {
|
|
321
|
+
const count = h.alloc(4);
|
|
322
|
+
const sceneCount = h.alloc(4);
|
|
323
|
+
for (let capacity = 64; ; ) {
|
|
324
|
+
const out = h.alloc(HANDLE * capacity);
|
|
325
|
+
const status = share(out, capacity, count, sceneCount);
|
|
326
|
+
const needed = new DataView(abi.HEAPU8.buffer).getUint32(count, true);
|
|
327
|
+
if (status === 0)
|
|
328
|
+
return splitModel(
|
|
329
|
+
Array.from({ length: needed }, (_, i) => h.keyOf(out + i * HANDLE)),
|
|
330
|
+
new DataView(abi.HEAPU8.buffer).getUint32(sceneCount, true),
|
|
331
|
+
);
|
|
332
|
+
const reason = abi.UTF8ToString(error());
|
|
333
|
+
if (reason !== "TN_WASM_GLTF_CAPACITY" || needed <= capacity) throw new Error(reason);
|
|
334
|
+
capacity = needed;
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
const loadGltf: NonNullable<IBrowserRuntime["loadGltf"]> = (bytes, files) =>
|
|
338
|
+
h.scoped(() => {
|
|
339
|
+
try {
|
|
340
|
+
stageFiles(files);
|
|
341
|
+
const data = h.alloc(Math.max(1, bytes.byteLength));
|
|
342
|
+
abi.HEAPU8.set(bytes, data);
|
|
343
|
+
return handles((out, capacity, count, scenes) =>
|
|
344
|
+
load(context, data, bytes.byteLength, out, capacity, count, scenes),
|
|
345
|
+
);
|
|
346
|
+
} finally {
|
|
347
|
+
clearFiles?.();
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
if (begin === undefined || done === undefined || finish === undefined) return { loadGltf };
|
|
351
|
+
return {
|
|
352
|
+
loadGltf,
|
|
353
|
+
// PRD-860: the engine parses on its loader thread, so a model that arrives during play costs the
|
|
354
|
+
// main thread a poll and the hand-over, not the 100-500 ms parse Midway's late fleet froze on.
|
|
355
|
+
loadGltfAsync: async (bytes, files) => {
|
|
356
|
+
const ticket = h.scoped(() => {
|
|
226
357
|
try {
|
|
358
|
+
stageFiles(files);
|
|
227
359
|
const data = h.alloc(Math.max(1, bytes.byteLength));
|
|
228
360
|
abi.HEAPU8.set(bytes, data);
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
for (let capacity = 64; ; ) {
|
|
232
|
-
const out = h.alloc(HANDLE * capacity);
|
|
233
|
-
const status = load(context, data, bytes.byteLength, out, capacity, count);
|
|
234
|
-
const needed = new DataView(abi.HEAPU8.buffer).getUint32(count, true);
|
|
235
|
-
if (status === 0) {
|
|
236
|
-
const refs = Array.from({ length: needed }, (_, i) => h.keyOf(out + i * HANDLE));
|
|
237
|
-
return { scene: refs[0] as IEngineRef, animations: refs.slice(1) };
|
|
238
|
-
}
|
|
239
|
-
const reason = abi.UTF8ToString(error());
|
|
240
|
-
if (reason !== "TN_WASM_GLTF_CAPACITY" || needed <= capacity) throw new Error(reason);
|
|
241
|
-
capacity = needed;
|
|
242
|
-
}
|
|
361
|
+
// begin copies the bytes and takes the staged files with it.
|
|
362
|
+
return begin(data, bytes.byteLength);
|
|
243
363
|
} finally {
|
|
244
364
|
clearFiles?.();
|
|
245
365
|
}
|
|
246
|
-
})
|
|
366
|
+
});
|
|
367
|
+
if (ticket === 0) throw new Error("TN_WASM_GLTF: the engine refused the load");
|
|
368
|
+
while (done(ticket) === 0) await new Promise((resolve) => setTimeout(resolve, GLTF_POLL_MS));
|
|
369
|
+
return handles((out, capacity, count, scenes) =>
|
|
370
|
+
finish(context, ticket, out, capacity, count, scenes),
|
|
371
|
+
);
|
|
372
|
+
},
|
|
247
373
|
};
|
|
248
374
|
}
|
|
249
375
|
|
|
376
|
+
/** How often a pending engine glTF load is polled: a 4 ms timer costs nothing beside a 100 ms parse. */
|
|
377
|
+
const GLTF_POLL_MS = 4;
|
|
378
|
+
|
|
250
379
|
/** TSL by name over `tn_tsl_call`, when the module exports it (the product web host does). */
|
|
251
380
|
export function tslOf(
|
|
252
381
|
abi: TnAbiModule,
|
|
@@ -263,6 +392,8 @@ export function tslOf(
|
|
|
263
392
|
_tn_tsl_release: release,
|
|
264
393
|
_tn_tsl_set: set,
|
|
265
394
|
_tn_tsl_set_uniform: setUniform,
|
|
395
|
+
_tn_tsl_set_uniforms: setUniforms,
|
|
396
|
+
_tn_tsl_reads: reads,
|
|
266
397
|
_tn_tsl_effect_parameter: effectParameter,
|
|
267
398
|
_tnw_web_set_post: setPost,
|
|
268
399
|
_tnw_web_compute: computeNode,
|
|
@@ -282,6 +413,11 @@ export function tslOf(
|
|
|
282
413
|
effectParameter === undefined
|
|
283
414
|
)
|
|
284
415
|
return undefined;
|
|
416
|
+
// PRD-878: the hooked uniform ids `reads` checks, kept in engine memory: the facade only appends to its list, so a
|
|
417
|
+
// query writes just the ids added since the last one.
|
|
418
|
+
let candidateAt = 0;
|
|
419
|
+
let candidateRoom = 0;
|
|
420
|
+
let candidatesWritten = 0;
|
|
285
421
|
// Writes tn_tsl_arg_t values; text is allocated first, since an allocation can grow the memory.
|
|
286
422
|
const writeArgs = (args: readonly TslArgValue[]): number => {
|
|
287
423
|
const pointer = h.alloc(Math.max(TSL_ARG, args.length * TSL_ARG));
|
|
@@ -395,7 +531,7 @@ export function tslOf(
|
|
|
395
531
|
const out = abi._malloc(size) >>> 0;
|
|
396
532
|
try {
|
|
397
533
|
if (readStorageTake(id, out, size) === 1)
|
|
398
|
-
return abi.
|
|
534
|
+
return heapFor(abi, out + size).slice(out, out + size).buffer;
|
|
399
535
|
} finally {
|
|
400
536
|
abi._free(out);
|
|
401
537
|
}
|
|
@@ -422,6 +558,87 @@ export function tslOf(
|
|
|
422
558
|
const diag = h.diagnostic();
|
|
423
559
|
h.check(setUniform(context, pointer, values, lanes.length, diag), diag, "uniform.value");
|
|
424
560
|
}),
|
|
561
|
+
...(reads === undefined
|
|
562
|
+
? {}
|
|
563
|
+
: {
|
|
564
|
+
reads: (
|
|
565
|
+
root: IEngineRef | undefined,
|
|
566
|
+
kernels: readonly number[],
|
|
567
|
+
candidates: readonly number[],
|
|
568
|
+
): number[] => {
|
|
569
|
+
if (candidates.length > candidateRoom) {
|
|
570
|
+
if (candidateAt !== 0) abi._free(candidateAt);
|
|
571
|
+
candidateRoom = Math.max(16, candidates.length * 2);
|
|
572
|
+
// The ids, then room for as many u32 answers.
|
|
573
|
+
candidateAt = abi._malloc(candidateRoom * 12) >>> 0;
|
|
574
|
+
candidatesWritten = 0;
|
|
575
|
+
}
|
|
576
|
+
const ids = h.view(candidateAt + candidateRoom * 12);
|
|
577
|
+
for (; candidatesWritten < candidates.length; candidatesWritten += 1)
|
|
578
|
+
ids.setBigUint64(
|
|
579
|
+
candidateAt + candidatesWritten * 8,
|
|
580
|
+
BigInt(candidates[candidatesWritten] as number),
|
|
581
|
+
true,
|
|
582
|
+
);
|
|
583
|
+
return h.scoped(() => {
|
|
584
|
+
const rootAt = root === undefined ? 0 : h.alloc(HANDLE);
|
|
585
|
+
if (root !== undefined) h.writeHandle(rootAt, root);
|
|
586
|
+
const kernelAt = h.alloc(Math.max(8, kernels.length * 8));
|
|
587
|
+
const countAt = h.alloc(4);
|
|
588
|
+
const v = h.view();
|
|
589
|
+
kernels.forEach((kernel, i) =>
|
|
590
|
+
v.setBigUint64(kernelAt + i * 8, BigInt(kernel), true),
|
|
591
|
+
);
|
|
592
|
+
const out = candidateAt + candidateRoom * 8;
|
|
593
|
+
const diag = h.diagnostic();
|
|
594
|
+
h.check(
|
|
595
|
+
reads(
|
|
596
|
+
context,
|
|
597
|
+
rootAt,
|
|
598
|
+
kernelAt,
|
|
599
|
+
kernels.length,
|
|
600
|
+
candidateAt,
|
|
601
|
+
candidates.length,
|
|
602
|
+
out,
|
|
603
|
+
countAt,
|
|
604
|
+
diag,
|
|
605
|
+
),
|
|
606
|
+
diag,
|
|
607
|
+
"uniform reads",
|
|
608
|
+
);
|
|
609
|
+
const after = h.view();
|
|
610
|
+
const picked: number[] = [];
|
|
611
|
+
for (let i = 0, count = after.getUint32(countAt, true); i < count; i += 1)
|
|
612
|
+
picked.push(after.getUint32(out + i * 4, true));
|
|
613
|
+
return picked;
|
|
614
|
+
});
|
|
615
|
+
},
|
|
616
|
+
}),
|
|
617
|
+
// PRD-828: a sync's changed uniforms as one `tn_tsl_set_uniforms` span; absent on an older module.
|
|
618
|
+
...(setUniforms === undefined
|
|
619
|
+
? {}
|
|
620
|
+
: {
|
|
621
|
+
setUniforms: (
|
|
622
|
+
nodes: readonly number[],
|
|
623
|
+
counts: readonly number[],
|
|
624
|
+
lanes: readonly number[],
|
|
625
|
+
) =>
|
|
626
|
+
h.scoped(() => {
|
|
627
|
+
const ids = h.alloc(Math.max(8, nodes.length * 8));
|
|
628
|
+
const widths = h.alloc(Math.max(4, counts.length * 4));
|
|
629
|
+
const values = h.alloc(Math.max(8, lanes.length * 8));
|
|
630
|
+
const v = h.view();
|
|
631
|
+
nodes.forEach((node, i) => v.setBigUint64(ids + i * 8, BigInt(node), true));
|
|
632
|
+
counts.forEach((count, i) => v.setUint32(widths + i * 4, count, true));
|
|
633
|
+
lanes.forEach((lane, i) => v.setFloat64(values + i * 8, lane, true));
|
|
634
|
+
const diag = h.diagnostic();
|
|
635
|
+
h.check(
|
|
636
|
+
setUniforms(context, ids, widths, values, nodes.length, diag),
|
|
637
|
+
diag,
|
|
638
|
+
"uniform.value",
|
|
639
|
+
);
|
|
640
|
+
}),
|
|
641
|
+
}),
|
|
425
642
|
},
|
|
426
643
|
};
|
|
427
644
|
}
|
|
@@ -42,6 +42,8 @@ export interface IAttributeSync {
|
|
|
42
42
|
/** Shape survives data uploads and transform calls; only copy/replacement changes it. */
|
|
43
43
|
readonly shapes: WeakMap<object, Map<string, unknown>>;
|
|
44
44
|
readonly pending: Set<object>;
|
|
45
|
+
/** Item ranges a mesh-level write put in an attribute's array, sent with `pending` (PRD-860). */
|
|
46
|
+
readonly dirty: Map<object, { from: number; to: number }>;
|
|
45
47
|
writeBack(): void;
|
|
46
48
|
itemSizeOf(attribute: object): number;
|
|
47
49
|
rangeOf(method: string, args: readonly unknown[], itemSize: number): readonly [number, number];
|
|
@@ -53,6 +55,12 @@ export interface IAttributeSync {
|
|
|
53
55
|
index: unknown,
|
|
54
56
|
argument: unknown,
|
|
55
57
|
): { array: TypedArray; at: number; values: ArrayLike<number> } | undefined;
|
|
58
|
+
writeItemAt(
|
|
59
|
+
mesh: object,
|
|
60
|
+
name: "instanceMatrix" | "instanceColor",
|
|
61
|
+
index: unknown,
|
|
62
|
+
value: unknown,
|
|
63
|
+
): boolean;
|
|
56
64
|
}
|
|
57
65
|
|
|
58
66
|
export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
@@ -70,7 +78,14 @@ export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
|
70
78
|
// Shape survives data uploads and transform calls; only copy/replacement changes it.
|
|
71
79
|
const shapes = new WeakMap<object, Map<string, unknown>>();
|
|
72
80
|
const pending = new Set<object>();
|
|
81
|
+
const dirty = new Map<object, { from: number; to: number }>();
|
|
73
82
|
const writeBack = (): void => {
|
|
83
|
+
for (const [attribute, { from, to }] of dirty) {
|
|
84
|
+
const entry = arrays.get(attribute);
|
|
85
|
+
if (entry !== undefined && !pending.has(attribute))
|
|
86
|
+
attributeWrite?.call(runtime, refOf(attribute), entry.array.subarray(from, to), from);
|
|
87
|
+
}
|
|
88
|
+
dirty.clear();
|
|
74
89
|
for (const attribute of pending) {
|
|
75
90
|
const entry = arrays.get(attribute);
|
|
76
91
|
if (entry !== undefined) attributeWrite?.call(runtime, refOf(attribute), entry.array);
|
|
@@ -163,6 +178,58 @@ export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
|
163
178
|
return undefined;
|
|
164
179
|
return { array: entry.array, at, values };
|
|
165
180
|
};
|
|
181
|
+
// three's `setMatrixAt` and `setColorAt` write the item into the attribute's array, and so does
|
|
182
|
+
// this: one engine call an instance made a meadow of a few thousand tufts cost 60 ms of a laptop
|
|
183
|
+
// load (PRD-860). The written range goes to the engine once, before the next engine call, which is
|
|
184
|
+
// the first thing that can read it. Anything three would not write (a bad index, no value) and the
|
|
185
|
+
// first colour (the engine creates `instanceColor` then) take the engine's call.
|
|
186
|
+
const writeItemAt = (
|
|
187
|
+
mesh: object,
|
|
188
|
+
name: "instanceMatrix" | "instanceColor",
|
|
189
|
+
index: unknown,
|
|
190
|
+
value: unknown,
|
|
191
|
+
): boolean => {
|
|
192
|
+
const source = value as {
|
|
193
|
+
elements?: ArrayLike<number>;
|
|
194
|
+
r?: unknown;
|
|
195
|
+
g?: unknown;
|
|
196
|
+
b?: unknown;
|
|
197
|
+
} | null;
|
|
198
|
+
const size = name === "instanceMatrix" ? 16 : 3;
|
|
199
|
+
const values =
|
|
200
|
+
name === "instanceMatrix"
|
|
201
|
+
? source?.elements
|
|
202
|
+
: typeof source?.r === "number" &&
|
|
203
|
+
typeof source.g === "number" &&
|
|
204
|
+
typeof source.b === "number"
|
|
205
|
+
? [source.r, source.g, source.b]
|
|
206
|
+
: undefined;
|
|
207
|
+
const attribute = (mesh as Record<string, unknown>)[name];
|
|
208
|
+
const at = Number(index) * size;
|
|
209
|
+
if (
|
|
210
|
+
attributeArray === undefined ||
|
|
211
|
+
values?.length !== size ||
|
|
212
|
+
typeof attribute !== "object" ||
|
|
213
|
+
attribute === null ||
|
|
214
|
+
!Number.isSafeInteger(at) ||
|
|
215
|
+
at < 0
|
|
216
|
+
)
|
|
217
|
+
return false;
|
|
218
|
+
let entry = arrays.get(attribute);
|
|
219
|
+
if (entry === undefined) {
|
|
220
|
+
entry = { array: attributeArray.call(runtime, refOf(attribute)) };
|
|
221
|
+
arrays.set(attribute, entry);
|
|
222
|
+
}
|
|
223
|
+
if (at + size > entry.array.length) return false;
|
|
224
|
+
entry.array.set(values, at);
|
|
225
|
+
const range = dirty.get(attribute);
|
|
226
|
+
if (range === undefined) dirty.set(attribute, { from: at, to: at + size });
|
|
227
|
+
else {
|
|
228
|
+
range.from = Math.min(range.from, at);
|
|
229
|
+
range.to = Math.max(range.to, at + size);
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
};
|
|
166
233
|
return {
|
|
167
234
|
attributeArray,
|
|
168
235
|
attributeWrite,
|
|
@@ -170,11 +237,13 @@ export function defineAttributeSync(host: IAttributeSyncHost): IAttributeSync {
|
|
|
170
237
|
itemSizes,
|
|
171
238
|
shapes,
|
|
172
239
|
pending,
|
|
240
|
+
dirty,
|
|
173
241
|
writeBack,
|
|
174
242
|
itemSizeOf,
|
|
175
243
|
rangeOf,
|
|
176
244
|
syncRange,
|
|
177
245
|
syncFull,
|
|
178
246
|
instanceItem,
|
|
247
|
+
writeItemAt,
|
|
179
248
|
};
|
|
180
249
|
}
|