perf(geometry): workers encode streamed meshes' GPU vertex records; the main thread uploads them instead of merging

Closed
LO
18d ago
+1181-126

What

Streamed meshes are encoded for the GPU in the geometry workers instead of on the main thread.

  • Once the renderer has drawn a load's first fragment, its quantization frame is published to every geometry worker.
  • From then on, each worker attaches the mesh's 12-byte quantized vertex records (gpuRecords) and transfers them.
  • The main thread builds a streamed fragment by concatenating those records, instead of merging, quantizing and collapse-checking on the main thread.
  • Anything that does not carry matching records takes today's main-thread path unchanged: a different frame or lattice origin, changed positions or colour, or a fragment that does not provably quantize.

Rendered batches are unchanged; see the changeset gpu-records-per-mesh for the API.

How the frame reaches the workers

  • The frame goes through a small SharedArrayBuffer cell that the pool hands every worker at stream start:
    • the frame data is written first, then an Int32 ready word via Atomics.store;
    • workers read the ready word, then the data, for every mesh.
  • So a worker picks the frame up in the middle of a slice; it does not need its event loop to deliver a message.
  • The old record-frame message stays as the fallback when there is no cell. A test shows it gives byte-identical records.
  • A mesh collected before the frame is ready keeps the old path.
  • On cross-origin isolation:
    • The parallel worker path already requires SharedArrayBuffer: processAdaptive only picks processParallel when it exists, and processParallel shares the source through one.
    • The viewer and embed deploys send COOP same-origin + COEP credentialless.
    • So the cell adds no new requirement.

Evidence

Main-thread work (untimed CPU traces, Holter cold, 3 traces per arm, median, ms; streamComplete → drain end):

itembaseM2
drain window1,5481,016
main thread busy1,038720
flushPending (fragment build, incl.)435236
quantizeInterleaved / latticeCollapses / mergeGeometry90 / 65 / 530 / 0 / 0

Records coverage (untimed, 4021111b8d = this head, one cold load each; meshes that carried worker records):

fixturerecordsmissing (old path)
Holter97.3% (55,337 / 56,874)1,537
BWK93.6% (36,787 / 39,290)2,503
skolebygg92.3% (11,463 / 12,419)956
advanced_model76.8% (4,859 / 6,323)1,464

Why advanced_model is lower:

  • The frame is the origin of the first fragment the main thread builds. Every mesh a worker collects before that fragment exists takes the old path; each worker reports exactly one not-ready read.
  • advanced_model streams fast enough that about 23% of its meshes come before its first frame (about 640 ms).
  • That only means less gain there. A load with both kinds of batch is the normal case, and it is covered below.
  • Fixing the frame before streaming starts (for example from the prepass bounds) would lift it. That is a follow-up, proposed only if the timed result shows advanced_model matters.

Identity:

  • Corpus identity test (streamedBatches.identity.real-ifc) passes 5/0. The final batches equal the full rebuild, and finalize batch, assembled and rebuilt counts equal the pre-M2 baseline on all 5 models.
  • That includes advanced_model with both kinds of batch: 2.64 M vertices from worker records and 1.23 M merged on the main thread.
  • Unit tests:
    • the ready word is written last;
    • a worker picks the frame up mid-slice with no message;
    • the message-only fallback gives byte-identical records;
    • batches that mix records and merged meshes finalize the same as the merged load and as the full rebuild.
  • Untimed identity proof on this head (4021111b8d), before the timed slot:
    • Browser per-element fingerprint against main@65083544b0, 2 cold loads per side: IDENTICAL on advanced_model (the mixed record/no-record case), Holter, BWK and skolebygg. Example: advanced_model 6,650 elements, sig 4b97a7ea9472bc1e on both sides.
    • Renderer suite 2,795 passed, 0 failed.
    • Corpus identity 5/0, with finalize batch, assembled and rebuilt counts equal to the pre-M2 baseline on all 5 models.

Tests run (focused; the merge queue runs the full suites):

  • Renderer suite: 2,745 passed, 0 failed.
  • Geometry tests: 30/30.
  • Renderer, geometry and viewer tsc: clean on this head (fleet).

Timed verdict: pending. One capped exclusive A/B against main, with an A/A of main in the same admission: browser-cold on Holter, BWK and advanced_model (advanced_model as the no-regression check). The table will be added here.

šŸ¤– Generated with Claude Code

Summary by CodeRabbit

  • Performance
    • Eligible geometry can be prepared for faster GPU batch assembly, helping streamed model loads display more efficiently.
    • When the optimized path is unavailable or unsuitable, models continue to use the existing processing path.
  • Reliability
    • GPU-built batches preserve geometry and match full rebuilds across varied mesh arrival orders.
    • Models that cannot be safely quantized continue to use the standard batch-building path.