ugly-game 0.14.34 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/gpuCrowd.d.ts +181 -0
- package/dist/assets/gpuCrowd.js +497 -2
- package/package.json +1 -1
|
@@ -46,6 +46,18 @@ export interface CrowdAssets {
|
|
|
46
46
|
lods: CrowdLod[];
|
|
47
47
|
/** `targets[targetId]` — that target's gathers, at most one per rung. */
|
|
48
48
|
targets: CrowdTarget[][];
|
|
49
|
+
/**
|
|
50
|
+
* First target id belonging to the FACE, if this kit has per-frame face channels.
|
|
51
|
+
*
|
|
52
|
+
* Targets from here up are lifted out of the CPU-baked `mergeTargets` path and made GPU-resident,
|
|
53
|
+
* driven by a per-frame weight row (see `enableFaceRig`). They must be the LAST ids and
|
|
54
|
+
* contiguous, which is how the cook lays them out.
|
|
55
|
+
*
|
|
56
|
+
* ⚠️ OMITTED ⇒ NO FACE RIG, and the renderer behaves exactly as it did before this existed. That
|
|
57
|
+
* default is deliberate: every kit cooked without face channels keeps working untouched, and a
|
|
58
|
+
* caller that forgets this gets today's cost rather than a broken face.
|
|
59
|
+
*/
|
|
60
|
+
faceFirst?: number;
|
|
49
61
|
boneCount: number;
|
|
50
62
|
/** Shared skin atlas. The CALLER owns and uploads it; this renderer never decodes anything. */
|
|
51
63
|
skin: GPUTexture;
|
|
@@ -248,6 +260,81 @@ export interface MergedMorph {
|
|
|
248
260
|
delta: Float32Array;
|
|
249
261
|
count: number;
|
|
250
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Split the target library at `faceFirst` — body below, face at and above.
|
|
265
|
+
*
|
|
266
|
+
* The face channels are cooked LAST and contiguous (ids 662..708 in the shipped human kit), so
|
|
267
|
+
* separating them is a range check rather than an index table. The body half keeps the CPU-baked
|
|
268
|
+
* `mergeTargets` path; the face half becomes GPU-resident and is driven by a per-frame weight row.
|
|
269
|
+
*
|
|
270
|
+
* ⚠️ THE FACE HALF IS RE-INDEXED FROM 0, deliberately. A weight row is 47 floats indexed 0..46; if
|
|
271
|
+
* the face half kept its shipped ids every lookup would need the offset subtracted at exactly the
|
|
272
|
+
* right place, and getting it wrong produces a face that moves nothing while every counter stays
|
|
273
|
+
* healthy.
|
|
274
|
+
*/
|
|
275
|
+
export declare function splitFaceTargets(targets: CrowdTarget[][], faceFirst: number): {
|
|
276
|
+
body: CrowdTarget[][];
|
|
277
|
+
face: CrowdTarget[][];
|
|
278
|
+
};
|
|
279
|
+
/** One rung's face targets, transposed so each VERTEX names the channels that move it. */
|
|
280
|
+
export interface FaceCsr {
|
|
281
|
+
/** Ascending vertex ids this rung's face channels touch. */
|
|
282
|
+
verts: Uint32Array;
|
|
283
|
+
/** CSR row offsets into `chan`/`delta`; length `verts.length + 1`. */
|
|
284
|
+
offset: Uint32Array;
|
|
285
|
+
/** Channel index 0..channelCount-1, one per entry. */
|
|
286
|
+
chan: Uint32Array;
|
|
287
|
+
/** `4·n` vec4-padded deltas, in the same order as `chan`. */
|
|
288
|
+
delta: Float32Array;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Transpose the face targets into CSR keyed by VERTEX, once per rung at load.
|
|
292
|
+
*
|
|
293
|
+
* ⚠️ THIS TRANSPOSE IS WHY THE PER-FRAME PASS IS RACE-FREE, and it is the same problem
|
|
294
|
+
* `mergeTargets` solves a different way. One thread per (channel, entry) doing `pos += delta*w` is
|
|
295
|
+
* a data race the moment two channels name the same vertex — and for a face that is the common
|
|
296
|
+
* case, not an edge case: `mouth-open` and `mouth-pursing` overlap almost entirely on the lips.
|
|
297
|
+
* `mergeTargets` escapes it by collapsing on the CPU, but that bakes the weights in, so it must
|
|
298
|
+
* re-run whenever a weight moves — exactly what a per-frame face cannot afford. Transposing keeps
|
|
299
|
+
* the weights out of the data: each vertex appears ONCE, one thread owns it, and the weights are
|
|
300
|
+
* looked up per entry inside that thread.
|
|
301
|
+
*
|
|
302
|
+
* ⚠️ Gathers are FILTERED by `g.lod`, not indexed by it — the same convention `mergeTargets` uses.
|
|
303
|
+
* A target may have several gathers or none at a given rung.
|
|
304
|
+
*/
|
|
305
|
+
export declare function buildFaceCsr(face: CrowdTarget[][], lod: number, channelCount: number): FaceCsr;
|
|
306
|
+
/** The face channel set: 34 MakeHuman expression units, with the mouth units split left/right. */
|
|
307
|
+
export declare const FACE_CHANNEL_COUNT = 47;
|
|
308
|
+
/**
|
|
309
|
+
* Weight-row stride in FLOATS.
|
|
310
|
+
*
|
|
311
|
+
* ⚠️ 48, not 47. The WGSL indexes a row as `weights[row * FACE_ROW_FLOATS + chan]`, and a stride
|
|
312
|
+
* that is not a multiple of 4 puts consecutive rows on unaligned vec4 boundaries. The wasted float
|
|
313
|
+
* costs 4 bytes per face; getting it wrong reads every face's weights one channel out of phase,
|
|
314
|
+
* which renders as a face making almost-right shapes.
|
|
315
|
+
*/
|
|
316
|
+
export declare const FACE_ROW_FLOATS = 48;
|
|
317
|
+
/** The face pass's 16-byte uniform, written in one place so CPU and WGSL cannot disagree. */
|
|
318
|
+
export declare function faceParams(faceCount: number, vertCount: number, channelCount: number): Uint32Array;
|
|
319
|
+
/** Bytes for `rows` weight rows at the padded stride. */
|
|
320
|
+
export declare function faceBufferBytes(rows: number): number;
|
|
321
|
+
/**
|
|
322
|
+
* ⚠️ REJECTS rather than padding or truncating. A 46-float array shifts every channel by one, which
|
|
323
|
+
* renders as a face making the wrong shapes — indistinguishable from a bad viseme table, and
|
|
324
|
+
* therefore debugged in the wrong repo.
|
|
325
|
+
*/
|
|
326
|
+
export declare function checkFaceWeights(w: Float32Array): void;
|
|
327
|
+
/**
|
|
328
|
+
* The CPU oracle for the face pass: `out[v] = rest[v] + Σ w[chan]·delta` over one face's CSR.
|
|
329
|
+
*
|
|
330
|
+
* This is the specification the WGSL is checked against on the WebGPU harness, which is why it is a
|
|
331
|
+
* plain function with its own tests rather than the same formula trusted twice.
|
|
332
|
+
*
|
|
333
|
+
* ⚠️ READS `rest`, WRITES `out`, and they must be different buffers. That separation is the whole
|
|
334
|
+
* idempotence argument: recomputing the absolute position from the pristine slot every frame means
|
|
335
|
+
* running the pass twice changes nothing, where accumulating in place drifts.
|
|
336
|
+
*/
|
|
337
|
+
export declare function applyFaceCsr(csr: FaceCsr, weights: Float32Array, row: number, rest: Float32Array, out: Float32Array): void;
|
|
251
338
|
/**
|
|
252
339
|
* Collapse every weighted target at `lod` into ONE sparse (vertex, delta) list, on the CPU.
|
|
253
340
|
*
|
|
@@ -575,6 +662,29 @@ export declare const CROWD_PIPELINE_STATE: {
|
|
|
575
662
|
};
|
|
576
663
|
/** Floats in the draw pass's scene uniform. See `frame` for the field-by-field layout. */
|
|
577
664
|
export declare const CROWD_U_FLOATS: number;
|
|
665
|
+
/**
|
|
666
|
+
* THE PER-FRAME FACE PASS. Two entry points, both one thread per (animated face, face vertex).
|
|
667
|
+
*
|
|
668
|
+
* ⚠️ ONE THREAD PER VERTEX, NOT PER DELTA ENTRY — the same correctness argument mergeTargets makes
|
|
669
|
+
* for the body. Two face channels routinely move the same lip vertex, so a per-entry thread doing
|
|
670
|
+
* a read-modify-write is a data race, and f32 atomics are not in core WGSL. buildFaceCsr is what
|
|
671
|
+
* makes each vertex the property of exactly one thread.
|
|
672
|
+
*
|
|
673
|
+
* ⚠️ READS srcSlot, WRITES dstSlot. Recomputing the absolute position from the pristine
|
|
674
|
+
* body-morphed slot every frame is what makes the pass idempotent; accumulating in place drifts,
|
|
675
|
+
* and a drifting face reads as a rig bug rather than as a missing reset.
|
|
676
|
+
*
|
|
677
|
+
* ⚠️ THE WEIGHT ROW COMES FROM faceTable[f].z, not from f. Faces are grouped by rung for dispatch, so
|
|
678
|
+
* f is a position within this rung's group; using it as the weight row directly would hand a face
|
|
679
|
+
* whichever mouth happened to be at that index.
|
|
680
|
+
*
|
|
681
|
+
* ⚠️ srcSlot, dstSlot AND the weight row SHARE ONE vec4u BUFFER, and that is a hard requirement, not
|
|
682
|
+
* tidiness. WebGPU's default maxStorageBuffersPerShaderStage is 8, and MORPH above already uses
|
|
683
|
+
* exactly 8 — three separate per-face tables put faceMorph at 9, which fails pipeline creation on
|
|
684
|
+
* any adapter reporting the default limit. That failure is a BLACK SCREEN at runtime, and no
|
|
685
|
+
* headless test can see it: demo/cli/faceWgsl.check.mts is what caught it.
|
|
686
|
+
*/
|
|
687
|
+
export declare const FACE_WGSL = "\nstruct FP { faceCount: u32, vertCount: u32, channelCount: u32, pad: u32 };\n@group(0) @binding(0) var<storage,read_write> rest: array<vec4f>;\n@group(0) @binding(1) var<storage,read_write> restN: array<vec4f>;\n@group(0) @binding(2) var<storage,read> fVerts: array<u32>;\n@group(0) @binding(3) var<storage,read> fOffset: array<u32>;\n@group(0) @binding(4) var<storage,read> fChan: array<u32>;\n@group(0) @binding(5) var<storage,read> fDelta: array<vec4f>;\n@group(0) @binding(6) var<storage,read> weights: array<f32>;\n// x = source (pristine) slot, y = destination (animated) slot, z = weight row, w = unused.\n@group(0) @binding(7) var<storage,read> faceTable: array<vec4u>;\n@group(0) @binding(8) var<storage,read> adjOffset: array<u32>;\n@group(0) @binding(9) var<storage,read> adjTri: array<u32>;\n@group(0) @binding(10) var<storage,read> idx: array<u32>;\n@group(0) @binding(11) var<uniform> p: FP;\n\n// PASS A \u2014 position. Every face at this rung shares the same vertex list, so ONE dispatch covers\n// all of them: thread -> (face within the group, vertex within the face).\n@compute @workgroup_size(64) fn faceMorph(@builtin(global_invocation_id) g: vec3u) {\n if (g.x >= p.faceCount * p.vertCount) { return; }\n let f = g.x / p.vertCount;\n let i = g.x % p.vertCount;\n let v = fVerts[i];\n let tbl = faceTable[f];\n var q = rest[tbl.x + v].xyz;\n let row = tbl.z * 48u;\n let a = fOffset[i];\n let b = fOffset[i + 1u];\n for (var k = a; k < b; k = k + 1u) {\n q = q + fDelta[k].xyz * weights[row + fChan[k]];\n }\n rest[tbl.y + v] = vec4f(q, 1.0);\n}\n\n// PASS B \u2014 normals over the face subset only, area-weighted exactly like the body's PASS 3. A\n// SEPARATE dispatch, so every position PASS A moved is visible before any normal reads it.\n@compute @workgroup_size(64) fn faceNormals(@builtin(global_invocation_id) g: vec3u) {\n if (g.x >= p.faceCount * p.vertCount) { return; }\n let f = g.x / p.vertCount;\n let i = g.x % p.vertCount;\n let v = fVerts[i];\n let s = faceTable[f].y;\n var n = vec3f(0.0, 0.0, 0.0);\n let a = adjOffset[v];\n let b = adjOffset[v + 1u];\n for (var k = a; k < b; k = k + 1u) {\n let t = adjTri[k] * 3u;\n let p0 = rest[s + idx[t]].xyz;\n let p1 = rest[s + idx[t + 1u]].xyz;\n let p2 = rest[s + idx[t + 2u]].xyz;\n n = n + cross(p1 - p0, p2 - p0);\n }\n let len = length(n);\n restN[s + v] = vec4f(select(vec3f(0.0, 1.0, 0.0), n / len, len > 1e-12), 0.0);\n}";
|
|
578
688
|
/** The four vertex pools. rest/restN are morph output; def/defN are the per-frame skin output. */
|
|
579
689
|
export interface CrowdPools {
|
|
580
690
|
rest: GPUBuffer;
|
|
@@ -597,6 +707,29 @@ export declare class GpuCrowdRenderer {
|
|
|
597
707
|
*/
|
|
598
708
|
private morphBGL;
|
|
599
709
|
private morphLayout;
|
|
710
|
+
private facePipe;
|
|
711
|
+
private faceNormalPipe;
|
|
712
|
+
private faceParamBuf;
|
|
713
|
+
/**
|
|
714
|
+
* The coarsest rung that still gets a face rig. **LOD0 is the finest**, so a character is rigged
|
|
715
|
+
* when `rung <= faceRigLod`; the default 1 rigs LOD0 and LOD1 and skips LOD2/LOD3.
|
|
716
|
+
*
|
|
717
|
+
* ⚠️ A CALLER SHOULD DRIVE ITS AUDIO THRESHOLD FROM THE SAME SIGNAL. A face moving silently, and a
|
|
718
|
+
* voice arriving from a frozen face, are both worse than either being off.
|
|
719
|
+
*/
|
|
720
|
+
faceRigLod: number;
|
|
721
|
+
/** Per-rung CSR of the face channels, built once in `setAssets`. Null where a rung has none. */
|
|
722
|
+
private faceCsr;
|
|
723
|
+
/** Per-rung GPU copies of that CSR, plus the two bind groups (one per face pipeline). */
|
|
724
|
+
private faceLod;
|
|
725
|
+
/** slot → its weight row, rung and destination slot. Only rigged characters appear. */
|
|
726
|
+
private rigged;
|
|
727
|
+
/** Rows handed out so far; a freed row is reused rather than compacted (see `disableFaceRig`). */
|
|
728
|
+
private faceRows;
|
|
729
|
+
private wCpu;
|
|
730
|
+
private wBuf;
|
|
731
|
+
private faceTableBuf;
|
|
732
|
+
private faceTableCpu;
|
|
600
733
|
/** The per-frame skin pass. One dispatch per BUCKET, selected by a dynamic uniform offset. */
|
|
601
734
|
private skinPipe;
|
|
602
735
|
private skinBGL;
|
|
@@ -695,6 +828,14 @@ export declare class GpuCrowdRenderer {
|
|
|
695
828
|
* per-character or per-frame; a character is a slot plus a record, and that is the whole point.
|
|
696
829
|
*/
|
|
697
830
|
setAssets(a: CrowdAssets): void;
|
|
831
|
+
/**
|
|
832
|
+
* Transpose this kit's face channels once per rung and upload them.
|
|
833
|
+
*
|
|
834
|
+
* ⚠️ ONCE, AT LOAD. The transpose is the thing that makes the per-frame pass race-free, and it
|
|
835
|
+
* depends only on the geometry — not on any weight — so paying for it per frame would be paying
|
|
836
|
+
* repeatedly for an answer that never changes.
|
|
837
|
+
*/
|
|
838
|
+
private buildFaceLods;
|
|
698
839
|
/**
|
|
699
840
|
* Group 2 — the two atlases and the scene copy.
|
|
700
841
|
*
|
|
@@ -737,6 +878,46 @@ export declare class GpuCrowdRenderer {
|
|
|
737
878
|
* problem, and is a silent corruption if faked before then.
|
|
738
879
|
*/
|
|
739
880
|
morph(slot: number, lod: number, weights: Float32Array): void;
|
|
881
|
+
/** Grow the weight and face-table buffers to hold `rows`, and rebuild the bind groups on them. */
|
|
882
|
+
private allocFaceBuffers;
|
|
883
|
+
/** One bind group per face pipeline — see the `layout: 'auto'` note in the constructor. */
|
|
884
|
+
private buildFaceBindGroups;
|
|
885
|
+
/**
|
|
886
|
+
* Turn on per-frame face animation for `slot`, drawing into `dstSlot`.
|
|
887
|
+
*
|
|
888
|
+
* ⚠️ `dstSlot` STARTS AS A COPY of `slot`, positions AND normals. Only face vertices are rewritten
|
|
889
|
+
* per frame; every other vertex is correct from this copy and never touched again. Skipping the
|
|
890
|
+
* copy leaves whatever the pool last held there, which draws as a head animating on a stranger's
|
|
891
|
+
* body.
|
|
892
|
+
*
|
|
893
|
+
* ⚠️ THE CALLER MUST POINT THE INSTANCE RECORD AT `dstSlot`, not at `slot`. `slot` stays pristine
|
|
894
|
+
* precisely so the pass can recompute from it, so a record still naming `slot` shows a face that
|
|
895
|
+
* never moves — with every counter healthy.
|
|
896
|
+
*
|
|
897
|
+
* Re-entrant: called again with a different rung it re-copies, because the face vertex set differs
|
|
898
|
+
* per rung. Beyond `faceRigLod` it disables instead, so a caller can drive it straight from the
|
|
899
|
+
* character's current rung each frame without branching.
|
|
900
|
+
*/
|
|
901
|
+
enableFaceRig(slot: number, lod: number, dstSlot: number): void;
|
|
902
|
+
/** Stop dispatching for this slot — it costs exactly what it cost before the rig existed. */
|
|
903
|
+
disableFaceRig(slot: number): void;
|
|
904
|
+
/**
|
|
905
|
+
* Write one face's 47 channel weights. Cheap enough for every rigged face every frame.
|
|
906
|
+
*
|
|
907
|
+
* ⚠️ A console.error PLUS a no-op when the slot has no rig — never silence. A silently dropped
|
|
908
|
+
* write gives a still face with healthy counters, which reads as a broken driver in the app.
|
|
909
|
+
*/
|
|
910
|
+
setFaceWeights(slot: number, w: Float32Array): void;
|
|
911
|
+
/** How many faces are currently rigged — for a caller's HUD and for the benchmark. */
|
|
912
|
+
get faceRigCount(): number;
|
|
913
|
+
/**
|
|
914
|
+
* Run both face passes for every rigged face. Call once per frame, before `frame()`.
|
|
915
|
+
*
|
|
916
|
+
* ⚠️ ONE DISPATCH PAIR PER RUNG, never one per face. Faces at a rung share the same vertex list,
|
|
917
|
+
* which is the whole reason the CSR is per rung rather than per character; at fifty faces, a
|
|
918
|
+
* dispatch each would cost more in launches than the work being launched.
|
|
919
|
+
*/
|
|
920
|
+
faceFrame(): void;
|
|
740
921
|
/**
|
|
741
922
|
* Upload this frame's instance records and their per-LOD buckets.
|
|
742
923
|
*
|
package/dist/assets/gpuCrowd.js
CHANGED
|
@@ -288,6 +288,135 @@ export function makeMergeScratch(vertCount) {
|
|
|
288
288
|
dirty: new Uint8Array(vertCount),
|
|
289
289
|
};
|
|
290
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Split the target library at `faceFirst` — body below, face at and above.
|
|
293
|
+
*
|
|
294
|
+
* The face channels are cooked LAST and contiguous (ids 662..708 in the shipped human kit), so
|
|
295
|
+
* separating them is a range check rather than an index table. The body half keeps the CPU-baked
|
|
296
|
+
* `mergeTargets` path; the face half becomes GPU-resident and is driven by a per-frame weight row.
|
|
297
|
+
*
|
|
298
|
+
* ⚠️ THE FACE HALF IS RE-INDEXED FROM 0, deliberately. A weight row is 47 floats indexed 0..46; if
|
|
299
|
+
* the face half kept its shipped ids every lookup would need the offset subtracted at exactly the
|
|
300
|
+
* right place, and getting it wrong produces a face that moves nothing while every counter stays
|
|
301
|
+
* healthy.
|
|
302
|
+
*/
|
|
303
|
+
export function splitFaceTargets(targets, faceFirst) {
|
|
304
|
+
const cut = Math.max(0, Math.min(faceFirst, targets.length));
|
|
305
|
+
return { body: targets.slice(0, cut), face: targets.slice(cut) };
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Transpose the face targets into CSR keyed by VERTEX, once per rung at load.
|
|
309
|
+
*
|
|
310
|
+
* ⚠️ THIS TRANSPOSE IS WHY THE PER-FRAME PASS IS RACE-FREE, and it is the same problem
|
|
311
|
+
* `mergeTargets` solves a different way. One thread per (channel, entry) doing `pos += delta*w` is
|
|
312
|
+
* a data race the moment two channels name the same vertex — and for a face that is the common
|
|
313
|
+
* case, not an edge case: `mouth-open` and `mouth-pursing` overlap almost entirely on the lips.
|
|
314
|
+
* `mergeTargets` escapes it by collapsing on the CPU, but that bakes the weights in, so it must
|
|
315
|
+
* re-run whenever a weight moves — exactly what a per-frame face cannot afford. Transposing keeps
|
|
316
|
+
* the weights out of the data: each vertex appears ONCE, one thread owns it, and the weights are
|
|
317
|
+
* looked up per entry inside that thread.
|
|
318
|
+
*
|
|
319
|
+
* ⚠️ Gathers are FILTERED by `g.lod`, not indexed by it — the same convention `mergeTargets` uses.
|
|
320
|
+
* A target may have several gathers or none at a given rung.
|
|
321
|
+
*/
|
|
322
|
+
export function buildFaceCsr(face, lod, channelCount) {
|
|
323
|
+
const byVert = new Map();
|
|
324
|
+
for (let c = 0; c < Math.min(channelCount, face.length); c++) {
|
|
325
|
+
const gathers = maybe(face, c);
|
|
326
|
+
if (!gathers)
|
|
327
|
+
continue;
|
|
328
|
+
for (const g of gathers) {
|
|
329
|
+
if (g.lod !== lod)
|
|
330
|
+
continue;
|
|
331
|
+
for (let e = 0; e < g.index.length; e++) {
|
|
332
|
+
const v = g.index[e];
|
|
333
|
+
const row = byVert.get(v) ?? [];
|
|
334
|
+
row.push({
|
|
335
|
+
chan: c,
|
|
336
|
+
d: [g.delta[e * 3], g.delta[e * 3 + 1], g.delta[e * 3 + 2]],
|
|
337
|
+
});
|
|
338
|
+
byVert.set(v, row);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const verts = new Uint32Array([...byVert.keys()].sort((a, b) => a - b));
|
|
343
|
+
const offset = new Uint32Array(verts.length + 1);
|
|
344
|
+
let n = 0;
|
|
345
|
+
for (let i = 0; i < verts.length; i++) {
|
|
346
|
+
offset[i] = n;
|
|
347
|
+
n += byVert.get(verts[i]).length;
|
|
348
|
+
}
|
|
349
|
+
offset[verts.length] = n;
|
|
350
|
+
const chan = new Uint32Array(n);
|
|
351
|
+
const delta = new Float32Array(n * 4);
|
|
352
|
+
let k = 0;
|
|
353
|
+
for (const v of verts) {
|
|
354
|
+
for (const e of byVert.get(v)) {
|
|
355
|
+
chan[k] = e.chan;
|
|
356
|
+
delta[k * 4] = e.d[0];
|
|
357
|
+
delta[k * 4 + 1] = e.d[1];
|
|
358
|
+
delta[k * 4 + 2] = e.d[2];
|
|
359
|
+
k++;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return { verts, offset, chan, delta };
|
|
363
|
+
}
|
|
364
|
+
/** The face channel set: 34 MakeHuman expression units, with the mouth units split left/right. */
|
|
365
|
+
export const FACE_CHANNEL_COUNT = 47;
|
|
366
|
+
/**
|
|
367
|
+
* Weight-row stride in FLOATS.
|
|
368
|
+
*
|
|
369
|
+
* ⚠️ 48, not 47. The WGSL indexes a row as `weights[row * FACE_ROW_FLOATS + chan]`, and a stride
|
|
370
|
+
* that is not a multiple of 4 puts consecutive rows on unaligned vec4 boundaries. The wasted float
|
|
371
|
+
* costs 4 bytes per face; getting it wrong reads every face's weights one channel out of phase,
|
|
372
|
+
* which renders as a face making almost-right shapes.
|
|
373
|
+
*/
|
|
374
|
+
export const FACE_ROW_FLOATS = 48;
|
|
375
|
+
/** The face pass's 16-byte uniform, written in one place so CPU and WGSL cannot disagree. */
|
|
376
|
+
export function faceParams(faceCount, vertCount, channelCount) {
|
|
377
|
+
return new Uint32Array([faceCount, vertCount, channelCount, 0]);
|
|
378
|
+
}
|
|
379
|
+
/** Bytes for `rows` weight rows at the padded stride. */
|
|
380
|
+
export function faceBufferBytes(rows) {
|
|
381
|
+
return rows * FACE_ROW_FLOATS * 4;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* ⚠️ REJECTS rather than padding or truncating. A 46-float array shifts every channel by one, which
|
|
385
|
+
* renders as a face making the wrong shapes — indistinguishable from a bad viseme table, and
|
|
386
|
+
* therefore debugged in the wrong repo.
|
|
387
|
+
*/
|
|
388
|
+
export function checkFaceWeights(w) {
|
|
389
|
+
if (w.length !== FACE_CHANNEL_COUNT)
|
|
390
|
+
throw new Error(`setFaceWeights: expected ${FACE_CHANNEL_COUNT} channels, got ${w.length}`);
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* The CPU oracle for the face pass: `out[v] = rest[v] + Σ w[chan]·delta` over one face's CSR.
|
|
394
|
+
*
|
|
395
|
+
* This is the specification the WGSL is checked against on the WebGPU harness, which is why it is a
|
|
396
|
+
* plain function with its own tests rather than the same formula trusted twice.
|
|
397
|
+
*
|
|
398
|
+
* ⚠️ READS `rest`, WRITES `out`, and they must be different buffers. That separation is the whole
|
|
399
|
+
* idempotence argument: recomputing the absolute position from the pristine slot every frame means
|
|
400
|
+
* running the pass twice changes nothing, where accumulating in place drifts.
|
|
401
|
+
*/
|
|
402
|
+
export function applyFaceCsr(csr, weights, row, rest, out) {
|
|
403
|
+
const base = row * FACE_ROW_FLOATS;
|
|
404
|
+
for (let i = 0; i < csr.verts.length; i++) {
|
|
405
|
+
const v = csr.verts[i];
|
|
406
|
+
let x = rest[v * 4], y = rest[v * 4 + 1], z = rest[v * 4 + 2];
|
|
407
|
+
for (let k = csr.offset[i]; k < csr.offset[i + 1]; k++) {
|
|
408
|
+
const w = weights[base + csr.chan[k]] ?? 0;
|
|
409
|
+
if (w === 0)
|
|
410
|
+
continue;
|
|
411
|
+
x += csr.delta[k * 4] * w;
|
|
412
|
+
y += csr.delta[k * 4 + 1] * w;
|
|
413
|
+
z += csr.delta[k * 4 + 2] * w;
|
|
414
|
+
}
|
|
415
|
+
out[v * 4] = x;
|
|
416
|
+
out[v * 4 + 1] = y;
|
|
417
|
+
out[v * 4 + 2] = z;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
291
420
|
/**
|
|
292
421
|
* Collapse every weighted target at `lod` into ONE sparse (vertex, delta) list, on the CPU.
|
|
293
422
|
*
|
|
@@ -705,6 +834,83 @@ export const CROWD_U_FLOATS = 40 + 16 * MAX_CASCADES;
|
|
|
705
834
|
// ⚠️ NO BACKTICKS ANYWHERE BELOW, including inside WGSL comments — this is a JS template literal
|
|
706
835
|
// and one stray backtick terminates the shader string and fails the build a hundred lines away.
|
|
707
836
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
837
|
+
/**
|
|
838
|
+
* THE PER-FRAME FACE PASS. Two entry points, both one thread per (animated face, face vertex).
|
|
839
|
+
*
|
|
840
|
+
* ⚠️ ONE THREAD PER VERTEX, NOT PER DELTA ENTRY — the same correctness argument mergeTargets makes
|
|
841
|
+
* for the body. Two face channels routinely move the same lip vertex, so a per-entry thread doing
|
|
842
|
+
* a read-modify-write is a data race, and f32 atomics are not in core WGSL. buildFaceCsr is what
|
|
843
|
+
* makes each vertex the property of exactly one thread.
|
|
844
|
+
*
|
|
845
|
+
* ⚠️ READS srcSlot, WRITES dstSlot. Recomputing the absolute position from the pristine
|
|
846
|
+
* body-morphed slot every frame is what makes the pass idempotent; accumulating in place drifts,
|
|
847
|
+
* and a drifting face reads as a rig bug rather than as a missing reset.
|
|
848
|
+
*
|
|
849
|
+
* ⚠️ THE WEIGHT ROW COMES FROM faceTable[f].z, not from f. Faces are grouped by rung for dispatch, so
|
|
850
|
+
* f is a position within this rung's group; using it as the weight row directly would hand a face
|
|
851
|
+
* whichever mouth happened to be at that index.
|
|
852
|
+
*
|
|
853
|
+
* ⚠️ srcSlot, dstSlot AND the weight row SHARE ONE vec4u BUFFER, and that is a hard requirement, not
|
|
854
|
+
* tidiness. WebGPU's default maxStorageBuffersPerShaderStage is 8, and MORPH above already uses
|
|
855
|
+
* exactly 8 — three separate per-face tables put faceMorph at 9, which fails pipeline creation on
|
|
856
|
+
* any adapter reporting the default limit. That failure is a BLACK SCREEN at runtime, and no
|
|
857
|
+
* headless test can see it: demo/cli/faceWgsl.check.mts is what caught it.
|
|
858
|
+
*/
|
|
859
|
+
export const FACE_WGSL = /* wgsl */ `
|
|
860
|
+
struct FP { faceCount: u32, vertCount: u32, channelCount: u32, pad: u32 };
|
|
861
|
+
@group(0) @binding(0) var<storage,read_write> rest: array<vec4f>;
|
|
862
|
+
@group(0) @binding(1) var<storage,read_write> restN: array<vec4f>;
|
|
863
|
+
@group(0) @binding(2) var<storage,read> fVerts: array<u32>;
|
|
864
|
+
@group(0) @binding(3) var<storage,read> fOffset: array<u32>;
|
|
865
|
+
@group(0) @binding(4) var<storage,read> fChan: array<u32>;
|
|
866
|
+
@group(0) @binding(5) var<storage,read> fDelta: array<vec4f>;
|
|
867
|
+
@group(0) @binding(6) var<storage,read> weights: array<f32>;
|
|
868
|
+
// x = source (pristine) slot, y = destination (animated) slot, z = weight row, w = unused.
|
|
869
|
+
@group(0) @binding(7) var<storage,read> faceTable: array<vec4u>;
|
|
870
|
+
@group(0) @binding(8) var<storage,read> adjOffset: array<u32>;
|
|
871
|
+
@group(0) @binding(9) var<storage,read> adjTri: array<u32>;
|
|
872
|
+
@group(0) @binding(10) var<storage,read> idx: array<u32>;
|
|
873
|
+
@group(0) @binding(11) var<uniform> p: FP;
|
|
874
|
+
|
|
875
|
+
// PASS A — position. Every face at this rung shares the same vertex list, so ONE dispatch covers
|
|
876
|
+
// all of them: thread -> (face within the group, vertex within the face).
|
|
877
|
+
@compute @workgroup_size(64) fn faceMorph(@builtin(global_invocation_id) g: vec3u) {
|
|
878
|
+
if (g.x >= p.faceCount * p.vertCount) { return; }
|
|
879
|
+
let f = g.x / p.vertCount;
|
|
880
|
+
let i = g.x % p.vertCount;
|
|
881
|
+
let v = fVerts[i];
|
|
882
|
+
let tbl = faceTable[f];
|
|
883
|
+
var q = rest[tbl.x + v].xyz;
|
|
884
|
+
let row = tbl.z * ${FACE_ROW_FLOATS}u;
|
|
885
|
+
let a = fOffset[i];
|
|
886
|
+
let b = fOffset[i + 1u];
|
|
887
|
+
for (var k = a; k < b; k = k + 1u) {
|
|
888
|
+
q = q + fDelta[k].xyz * weights[row + fChan[k]];
|
|
889
|
+
}
|
|
890
|
+
rest[tbl.y + v] = vec4f(q, 1.0);
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
// PASS B — normals over the face subset only, area-weighted exactly like the body's PASS 3. A
|
|
894
|
+
// SEPARATE dispatch, so every position PASS A moved is visible before any normal reads it.
|
|
895
|
+
@compute @workgroup_size(64) fn faceNormals(@builtin(global_invocation_id) g: vec3u) {
|
|
896
|
+
if (g.x >= p.faceCount * p.vertCount) { return; }
|
|
897
|
+
let f = g.x / p.vertCount;
|
|
898
|
+
let i = g.x % p.vertCount;
|
|
899
|
+
let v = fVerts[i];
|
|
900
|
+
let s = faceTable[f].y;
|
|
901
|
+
var n = vec3f(0.0, 0.0, 0.0);
|
|
902
|
+
let a = adjOffset[v];
|
|
903
|
+
let b = adjOffset[v + 1u];
|
|
904
|
+
for (var k = a; k < b; k = k + 1u) {
|
|
905
|
+
let t = adjTri[k] * 3u;
|
|
906
|
+
let p0 = rest[s + idx[t]].xyz;
|
|
907
|
+
let p1 = rest[s + idx[t + 1u]].xyz;
|
|
908
|
+
let p2 = rest[s + idx[t + 2u]].xyz;
|
|
909
|
+
n = n + cross(p1 - p0, p2 - p0);
|
|
910
|
+
}
|
|
911
|
+
let len = length(n);
|
|
912
|
+
restN[s + v] = vec4f(select(vec3f(0.0, 1.0, 0.0), n / len, len > 1e-12), 0.0);
|
|
913
|
+
}`;
|
|
708
914
|
const MORPH = /* wgsl */ `
|
|
709
915
|
struct P { slot: u32, vertCount: u32, entryCount: u32, pad: u32 };
|
|
710
916
|
@group(0) @binding(0) var<storage,read> basePos: array<vec4f>;
|
|
@@ -1264,6 +1470,30 @@ export class GpuCrowdRenderer {
|
|
|
1264
1470
|
*/
|
|
1265
1471
|
morphBGL;
|
|
1266
1472
|
morphLayout;
|
|
1473
|
+
// ── the per-frame face rig ───────────────────────────────────────────────────────────────────
|
|
1474
|
+
facePipe;
|
|
1475
|
+
faceNormalPipe;
|
|
1476
|
+
faceParamBuf;
|
|
1477
|
+
/**
|
|
1478
|
+
* The coarsest rung that still gets a face rig. **LOD0 is the finest**, so a character is rigged
|
|
1479
|
+
* when `rung <= faceRigLod`; the default 1 rigs LOD0 and LOD1 and skips LOD2/LOD3.
|
|
1480
|
+
*
|
|
1481
|
+
* ⚠️ A CALLER SHOULD DRIVE ITS AUDIO THRESHOLD FROM THE SAME SIGNAL. A face moving silently, and a
|
|
1482
|
+
* voice arriving from a frozen face, are both worse than either being off.
|
|
1483
|
+
*/
|
|
1484
|
+
faceRigLod = 1;
|
|
1485
|
+
/** Per-rung CSR of the face channels, built once in `setAssets`. Null where a rung has none. */
|
|
1486
|
+
faceCsr = [];
|
|
1487
|
+
/** Per-rung GPU copies of that CSR, plus the two bind groups (one per face pipeline). */
|
|
1488
|
+
faceLod = [];
|
|
1489
|
+
/** slot → its weight row, rung and destination slot. Only rigged characters appear. */
|
|
1490
|
+
rigged = new Map();
|
|
1491
|
+
/** Rows handed out so far; a freed row is reused rather than compacted (see `disableFaceRig`). */
|
|
1492
|
+
faceRows = 0;
|
|
1493
|
+
wCpu = new Float32Array(0);
|
|
1494
|
+
wBuf = null;
|
|
1495
|
+
faceTableBuf = null;
|
|
1496
|
+
faceTableCpu = new Uint32Array(0);
|
|
1267
1497
|
/** The per-frame skin pass. One dispatch per BUCKET, selected by a dynamic uniform offset. */
|
|
1268
1498
|
skinPipe;
|
|
1269
1499
|
skinBGL;
|
|
@@ -1355,6 +1585,26 @@ export class GpuCrowdRenderer {
|
|
|
1355
1585
|
this.morphPipe = pipe('base');
|
|
1356
1586
|
this.addPipe = pipe('addMorph');
|
|
1357
1587
|
this.normalPipe = pipe('normals');
|
|
1588
|
+
// ⚠️ `layout: 'auto'` HERE, and it is the opposite choice from the three pipelines above — for a
|
|
1589
|
+
// reason that only applies to this shader. An explicit shared layout must declare EVERY binding
|
|
1590
|
+
// the module has, and the per-stage storage-buffer limit applies to the LAYOUT, not to what an
|
|
1591
|
+
// entry point uses: all 11 declared storage buffers would count, against a default limit of 8.
|
|
1592
|
+
// An auto layout is derived per pipeline from what that entry point statically references — 7
|
|
1593
|
+
// each — so each stays inside the limit. The cost is one bind group per pipeline instead of one
|
|
1594
|
+
// shared, which `faceBg` below carries.
|
|
1595
|
+
const fsm = device.createShaderModule({ code: FACE_WGSL });
|
|
1596
|
+
this.facePipe = device.createComputePipeline({
|
|
1597
|
+
layout: 'auto',
|
|
1598
|
+
compute: { module: fsm, entryPoint: 'faceMorph' },
|
|
1599
|
+
});
|
|
1600
|
+
this.faceNormalPipe = device.createComputePipeline({
|
|
1601
|
+
layout: 'auto',
|
|
1602
|
+
compute: { module: fsm, entryPoint: 'faceNormals' },
|
|
1603
|
+
});
|
|
1604
|
+
this.faceParamBuf = device.createBuffer({
|
|
1605
|
+
size: 16,
|
|
1606
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
1607
|
+
});
|
|
1358
1608
|
this.paramBuf = device.createBuffer({
|
|
1359
1609
|
size: 16,
|
|
1360
1610
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
@@ -1482,8 +1732,12 @@ export class GpuCrowdRenderer {
|
|
|
1482
1732
|
usage: U.STORAGE | U.COPY_SRC | extra,
|
|
1483
1733
|
});
|
|
1484
1734
|
this.pools = {
|
|
1485
|
-
rest:
|
|
1486
|
-
|
|
1735
|
+
// ⚠️ COPY_DST on rest/restN is what `enableFaceRig` needs: seeding an animated slot is a
|
|
1736
|
+
// buffer copy INTO the pool, and a pool created write-only-by-shader refuses it outright
|
|
1737
|
+
// ("usage doesn't include BufferUsage::CopyDst"). def/defN are pure skin-pass output and
|
|
1738
|
+
// stay as they were.
|
|
1739
|
+
rest: pool(U.COPY_DST),
|
|
1740
|
+
restN: pool(U.COPY_DST),
|
|
1487
1741
|
def: pool(U.VERTEX),
|
|
1488
1742
|
defN: pool(U.VERTEX),
|
|
1489
1743
|
};
|
|
@@ -1558,8 +1812,47 @@ export class GpuCrowdRenderer {
|
|
|
1558
1812
|
this.skinArrayView = a.skin.createView({ dimension: '2d-array' });
|
|
1559
1813
|
this.buildMatBG(this.dummySceneView);
|
|
1560
1814
|
this.rebuildLodBindGroups();
|
|
1815
|
+
this.buildFaceLods(a);
|
|
1561
1816
|
this.frameBGDirty = true;
|
|
1562
1817
|
}
|
|
1818
|
+
/**
|
|
1819
|
+
* Transpose this kit's face channels once per rung and upload them.
|
|
1820
|
+
*
|
|
1821
|
+
* ⚠️ ONCE, AT LOAD. The transpose is the thing that makes the per-frame pass race-free, and it
|
|
1822
|
+
* depends only on the geometry — not on any weight — so paying for it per frame would be paying
|
|
1823
|
+
* repeatedly for an answer that never changes.
|
|
1824
|
+
*/
|
|
1825
|
+
buildFaceLods(a) {
|
|
1826
|
+
const dev = this.device, U = GPUBufferUsage;
|
|
1827
|
+
if (a.faceFirst === undefined) {
|
|
1828
|
+
this.faceCsr = [];
|
|
1829
|
+
this.faceLod = [];
|
|
1830
|
+
return;
|
|
1831
|
+
}
|
|
1832
|
+
const { face } = splitFaceTargets(a.targets, a.faceFirst);
|
|
1833
|
+
this.faceCsr = a.lods.map((_, lod) => face.length ? buildFaceCsr(face, lod, FACE_CHANNEL_COUNT) : null);
|
|
1834
|
+
const store = (src) => {
|
|
1835
|
+
const b = dev.createBuffer({
|
|
1836
|
+
size: Math.max(src.byteLength, 4),
|
|
1837
|
+
usage: U.STORAGE | U.COPY_DST,
|
|
1838
|
+
});
|
|
1839
|
+
dev.queue.writeBuffer(b, 0, src);
|
|
1840
|
+
return b;
|
|
1841
|
+
};
|
|
1842
|
+
this.faceLod = this.faceCsr.map((csr) => csr?.verts.length
|
|
1843
|
+
? {
|
|
1844
|
+
verts: store(csr.verts),
|
|
1845
|
+
offset: store(csr.offset),
|
|
1846
|
+
chan: store(csr.chan),
|
|
1847
|
+
delta: store(csr.delta),
|
|
1848
|
+
vertCount: csr.verts.length,
|
|
1849
|
+
morphBg: null,
|
|
1850
|
+
normalBg: null,
|
|
1851
|
+
}
|
|
1852
|
+
: null);
|
|
1853
|
+
// A first allocation so the bind groups have real buffers to point at; it grows on demand.
|
|
1854
|
+
this.allocFaceBuffers(Math.max(1, this.wCpu.length / FACE_ROW_FLOATS));
|
|
1855
|
+
}
|
|
1563
1856
|
/**
|
|
1564
1857
|
* Group 2 — the two atlases and the scene copy.
|
|
1565
1858
|
*
|
|
@@ -1687,6 +1980,25 @@ export class GpuCrowdRenderer {
|
|
|
1687
1980
|
l.adjTri.destroy();
|
|
1688
1981
|
}
|
|
1689
1982
|
this.lodGpu = [];
|
|
1983
|
+
// The face rig belongs to the KIT: its CSR is that kit's geometry and its rows name that kit's
|
|
1984
|
+
// slots. Both are meaningless against the next one, so a rig does not survive a swap.
|
|
1985
|
+
for (const f of this.faceLod)
|
|
1986
|
+
if (f) {
|
|
1987
|
+
f.verts.destroy();
|
|
1988
|
+
f.offset.destroy();
|
|
1989
|
+
f.chan.destroy();
|
|
1990
|
+
f.delta.destroy();
|
|
1991
|
+
}
|
|
1992
|
+
this.faceLod = [];
|
|
1993
|
+
this.faceCsr = [];
|
|
1994
|
+
this.rigged.clear();
|
|
1995
|
+
this.faceRows = 0;
|
|
1996
|
+
this.wBuf?.destroy();
|
|
1997
|
+
this.wBuf = null;
|
|
1998
|
+
this.faceTableBuf?.destroy();
|
|
1999
|
+
this.faceTableBuf = null;
|
|
2000
|
+
this.wCpu = new Float32Array(0);
|
|
2001
|
+
this.faceTableCpu = new Uint32Array(0);
|
|
1690
2002
|
if (this.pools) {
|
|
1691
2003
|
this.pools.rest.destroy();
|
|
1692
2004
|
this.pools.restN.destroy();
|
|
@@ -1771,6 +2083,189 @@ export class GpuCrowdRenderer {
|
|
|
1771
2083
|
cp.end();
|
|
1772
2084
|
dev.queue.submit([enc.finish()]);
|
|
1773
2085
|
}
|
|
2086
|
+
// ── the per-frame face rig ───────────────────────────────────────────────────────────────────
|
|
2087
|
+
/** Grow the weight and face-table buffers to hold `rows`, and rebuild the bind groups on them. */
|
|
2088
|
+
allocFaceBuffers(rows) {
|
|
2089
|
+
this.wCpu = new Float32Array(rows * FACE_ROW_FLOATS);
|
|
2090
|
+
this.faceTableCpu = new Uint32Array(rows * 4);
|
|
2091
|
+
this.wBuf?.destroy();
|
|
2092
|
+
this.faceTableBuf?.destroy();
|
|
2093
|
+
this.wBuf = this.device.createBuffer({
|
|
2094
|
+
size: faceBufferBytes(rows),
|
|
2095
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
2096
|
+
});
|
|
2097
|
+
this.faceTableBuf = this.device.createBuffer({
|
|
2098
|
+
size: rows * 16,
|
|
2099
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
2100
|
+
});
|
|
2101
|
+
for (const f of this.faceLod)
|
|
2102
|
+
if (f)
|
|
2103
|
+
this.buildFaceBindGroups(f);
|
|
2104
|
+
}
|
|
2105
|
+
/** One bind group per face pipeline — see the `layout: 'auto'` note in the constructor. */
|
|
2106
|
+
buildFaceBindGroups(f) {
|
|
2107
|
+
const p = this.pools;
|
|
2108
|
+
const l = this.faceLod.indexOf(f);
|
|
2109
|
+
const g = maybe(this.lodGpu, l);
|
|
2110
|
+
if (!p || !g || !this.wBuf || !this.faceTableBuf)
|
|
2111
|
+
return;
|
|
2112
|
+
f.morphBg = this.device.createBindGroup({
|
|
2113
|
+
layout: this.facePipe.getBindGroupLayout(0),
|
|
2114
|
+
entries: [
|
|
2115
|
+
{ binding: 0, resource: { buffer: p.rest } },
|
|
2116
|
+
{ binding: 2, resource: { buffer: f.verts } },
|
|
2117
|
+
{ binding: 3, resource: { buffer: f.offset } },
|
|
2118
|
+
{ binding: 4, resource: { buffer: f.chan } },
|
|
2119
|
+
{ binding: 5, resource: { buffer: f.delta } },
|
|
2120
|
+
{ binding: 6, resource: { buffer: this.wBuf } },
|
|
2121
|
+
{ binding: 7, resource: { buffer: this.faceTableBuf } },
|
|
2122
|
+
{ binding: 11, resource: { buffer: this.faceParamBuf } },
|
|
2123
|
+
],
|
|
2124
|
+
});
|
|
2125
|
+
f.normalBg = this.device.createBindGroup({
|
|
2126
|
+
layout: this.faceNormalPipe.getBindGroupLayout(0),
|
|
2127
|
+
entries: [
|
|
2128
|
+
{ binding: 0, resource: { buffer: p.rest } },
|
|
2129
|
+
{ binding: 1, resource: { buffer: p.restN } },
|
|
2130
|
+
{ binding: 2, resource: { buffer: f.verts } },
|
|
2131
|
+
{ binding: 7, resource: { buffer: this.faceTableBuf } },
|
|
2132
|
+
{ binding: 8, resource: { buffer: g.adjOffset } },
|
|
2133
|
+
{ binding: 9, resource: { buffer: g.adjTri } },
|
|
2134
|
+
{ binding: 10, resource: { buffer: g.idx } },
|
|
2135
|
+
{ binding: 11, resource: { buffer: this.faceParamBuf } },
|
|
2136
|
+
],
|
|
2137
|
+
});
|
|
2138
|
+
}
|
|
2139
|
+
/**
|
|
2140
|
+
* Turn on per-frame face animation for `slot`, drawing into `dstSlot`.
|
|
2141
|
+
*
|
|
2142
|
+
* ⚠️ `dstSlot` STARTS AS A COPY of `slot`, positions AND normals. Only face vertices are rewritten
|
|
2143
|
+
* per frame; every other vertex is correct from this copy and never touched again. Skipping the
|
|
2144
|
+
* copy leaves whatever the pool last held there, which draws as a head animating on a stranger's
|
|
2145
|
+
* body.
|
|
2146
|
+
*
|
|
2147
|
+
* ⚠️ THE CALLER MUST POINT THE INSTANCE RECORD AT `dstSlot`, not at `slot`. `slot` stays pristine
|
|
2148
|
+
* precisely so the pass can recompute from it, so a record still naming `slot` shows a face that
|
|
2149
|
+
* never moves — with every counter healthy.
|
|
2150
|
+
*
|
|
2151
|
+
* Re-entrant: called again with a different rung it re-copies, because the face vertex set differs
|
|
2152
|
+
* per rung. Beyond `faceRigLod` it disables instead, so a caller can drive it straight from the
|
|
2153
|
+
* character's current rung each frame without branching.
|
|
2154
|
+
*/
|
|
2155
|
+
enableFaceRig(slot, lod, dstSlot) {
|
|
2156
|
+
if (lod > this.faceRigLod) {
|
|
2157
|
+
this.disableFaceRig(slot);
|
|
2158
|
+
return;
|
|
2159
|
+
}
|
|
2160
|
+
const csr = maybe(this.faceCsr, lod);
|
|
2161
|
+
const l = maybe(this.lodGpu, lod);
|
|
2162
|
+
const pools = this.pools;
|
|
2163
|
+
if (!csr?.verts.length)
|
|
2164
|
+
throw new Error(`enableFaceRig: LOD ${lod} has no face targets`);
|
|
2165
|
+
if (!l || !pools)
|
|
2166
|
+
throw new Error(`enableFaceRig: LOD ${lod} has no assets`);
|
|
2167
|
+
const prev = this.rigged.get(slot);
|
|
2168
|
+
const row = prev?.row ?? this.faceRows++;
|
|
2169
|
+
this.rigged.set(slot, { row, lod, dst: dstSlot });
|
|
2170
|
+
if ((row + 1) * FACE_ROW_FLOATS > this.wCpu.length)
|
|
2171
|
+
this.allocFaceBuffers(growCapacity(row + 1, this.wCpu.length / FACE_ROW_FLOATS));
|
|
2172
|
+
const bytes = l.plan.slotVerts * 16; // the pools are arrays of vec4
|
|
2173
|
+
// ⚠️ VIA A STAGING BUFFER, because WebGPU REFUSES copyBufferToBuffer when the source and the
|
|
2174
|
+
// destination are the SAME buffer — even for ranges that provably do not overlap. ("Source and
|
|
2175
|
+
// destination are the same buffer.") Both slots live in the one rest pool, so the copy has to
|
|
2176
|
+
// hop out and back. One-time, at enable, so it costs nothing per frame.
|
|
2177
|
+
const staging = this.device.createBuffer({
|
|
2178
|
+
size: bytes,
|
|
2179
|
+
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
|
|
2180
|
+
});
|
|
2181
|
+
const enc = this.device.createCommandEncoder();
|
|
2182
|
+
// Copies are ordered within an encoder and each names two DIFFERENT buffers, so reusing one
|
|
2183
|
+
// staging buffer for both pools is sequencing, not a race.
|
|
2184
|
+
enc.copyBufferToBuffer(pools.rest, slot * 16, staging, 0, bytes);
|
|
2185
|
+
enc.copyBufferToBuffer(staging, 0, pools.rest, dstSlot * 16, bytes);
|
|
2186
|
+
enc.copyBufferToBuffer(pools.restN, slot * 16, staging, 0, bytes);
|
|
2187
|
+
enc.copyBufferToBuffer(staging, 0, pools.restN, dstSlot * 16, bytes);
|
|
2188
|
+
this.device.queue.submit([enc.finish()]);
|
|
2189
|
+
staging.destroy();
|
|
2190
|
+
}
|
|
2191
|
+
/** Stop dispatching for this slot — it costs exactly what it cost before the rig existed. */
|
|
2192
|
+
disableFaceRig(slot) {
|
|
2193
|
+
const r = this.rigged.get(slot);
|
|
2194
|
+
if (!r)
|
|
2195
|
+
return;
|
|
2196
|
+
this.rigged.delete(slot);
|
|
2197
|
+
this.wCpu.fill(0, r.row * FACE_ROW_FLOATS, (r.row + 1) * FACE_ROW_FLOATS);
|
|
2198
|
+
// ⚠️ ROWS ARE NOT COMPACTED. Renumbering would move every live face's row mid-frame, and a
|
|
2199
|
+
// renumbered row is a face wearing someone else's mouth. A freed row is simply left for reuse.
|
|
2200
|
+
}
|
|
2201
|
+
/**
|
|
2202
|
+
* Write one face's 47 channel weights. Cheap enough for every rigged face every frame.
|
|
2203
|
+
*
|
|
2204
|
+
* ⚠️ A console.error PLUS a no-op when the slot has no rig — never silence. A silently dropped
|
|
2205
|
+
* write gives a still face with healthy counters, which reads as a broken driver in the app.
|
|
2206
|
+
*/
|
|
2207
|
+
setFaceWeights(slot, w) {
|
|
2208
|
+
checkFaceWeights(w);
|
|
2209
|
+
const r = this.rigged.get(slot);
|
|
2210
|
+
if (!r) {
|
|
2211
|
+
console.error(`[crowd] setFaceWeights: slot ${slot} has no face rig — call enableFaceRig first`);
|
|
2212
|
+
return;
|
|
2213
|
+
}
|
|
2214
|
+
this.wCpu.set(w, r.row * FACE_ROW_FLOATS);
|
|
2215
|
+
}
|
|
2216
|
+
/** How many faces are currently rigged — for a caller's HUD and for the benchmark. */
|
|
2217
|
+
get faceRigCount() {
|
|
2218
|
+
return this.rigged.size;
|
|
2219
|
+
}
|
|
2220
|
+
/**
|
|
2221
|
+
* Run both face passes for every rigged face. Call once per frame, before `frame()`.
|
|
2222
|
+
*
|
|
2223
|
+
* ⚠️ ONE DISPATCH PAIR PER RUNG, never one per face. Faces at a rung share the same vertex list,
|
|
2224
|
+
* which is the whole reason the CSR is per rung rather than per character; at fifty faces, a
|
|
2225
|
+
* dispatch each would cost more in launches than the work being launched.
|
|
2226
|
+
*/
|
|
2227
|
+
faceFrame() {
|
|
2228
|
+
if (!this.rigged.size || !this.wBuf || !this.faceTableBuf)
|
|
2229
|
+
return;
|
|
2230
|
+
const dev = this.device;
|
|
2231
|
+
dev.queue.writeBuffer(this.wBuf, 0, this.wCpu);
|
|
2232
|
+
const byRung = new Map();
|
|
2233
|
+
for (const [src, r] of this.rigged) {
|
|
2234
|
+
const g = byRung.get(r.lod) ?? [];
|
|
2235
|
+
g.push({ row: r.row, dst: r.dst, src });
|
|
2236
|
+
byRung.set(r.lod, g);
|
|
2237
|
+
}
|
|
2238
|
+
for (const [lod, group] of byRung) {
|
|
2239
|
+
const f = maybe(this.faceLod, lod);
|
|
2240
|
+
if (!f?.morphBg || !f.normalBg)
|
|
2241
|
+
continue;
|
|
2242
|
+
// ⚠️ `f` in the shader indexes THIS TABLE, not the weight buffer — hence the row in .z. The
|
|
2243
|
+
// table is rewritten per rung, so it is packed by position within the group.
|
|
2244
|
+
const tbl = this.faceTableCpu;
|
|
2245
|
+
for (const [i, e] of group.entries()) {
|
|
2246
|
+
tbl[i * 4] = e.src;
|
|
2247
|
+
tbl[i * 4 + 1] = e.dst;
|
|
2248
|
+
tbl[i * 4 + 2] = e.row;
|
|
2249
|
+
tbl[i * 4 + 3] = 0;
|
|
2250
|
+
}
|
|
2251
|
+
dev.queue.writeBuffer(this.faceTableBuf, 0, tbl, 0, group.length * 4);
|
|
2252
|
+
dev.queue.writeBuffer(this.faceParamBuf, 0, faceParams(group.length, f.vertCount, FACE_CHANNEL_COUNT));
|
|
2253
|
+
const threads = group.length * f.vertCount;
|
|
2254
|
+
const enc = dev.createCommandEncoder();
|
|
2255
|
+
const cp = enc.beginComputePass();
|
|
2256
|
+
cp.setPipeline(this.facePipe);
|
|
2257
|
+
cp.setBindGroup(0, f.morphBg);
|
|
2258
|
+
cp.dispatchWorkgroups(Math.ceil(threads / 64));
|
|
2259
|
+
cp.setPipeline(this.faceNormalPipe);
|
|
2260
|
+
cp.setBindGroup(0, f.normalBg);
|
|
2261
|
+
cp.dispatchWorkgroups(Math.ceil(threads / 64));
|
|
2262
|
+
cp.end();
|
|
2263
|
+
// ⚠️ SUBMITTED PER RUNG, because the params uniform and the face table are rewritten between
|
|
2264
|
+
// rungs. Recording both rungs into one encoder would have the second rung's uniform apply to
|
|
2265
|
+
// the first — the classic "one buffer, two dispatches" aliasing bug.
|
|
2266
|
+
dev.queue.submit([enc.finish()]);
|
|
2267
|
+
}
|
|
2268
|
+
}
|
|
1774
2269
|
/**
|
|
1775
2270
|
* Upload this frame's instance records and their per-LOD buckets.
|
|
1776
2271
|
*
|