perf(geometry): workers encode streamed meshes' GPU vertex records; the main thread uploads them instead of merging
Closed
LO
18d ago+1181+1181+0123456789012345678901234567890123456789-126-126-012345678901234567890123456789
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
SharedArrayBuffercell 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.
- the frame data is written first, then an Int32 ready word via
- 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-framemessage 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:processAdaptiveonly picksprocessParallelwhen it exists, andprocessParallelshares the source through one. - The viewer and embed deploys send COOP same-origin + COEP credentialless.
- So the cell adds no new requirement.
- The parallel worker path already requires
Evidence
Main-thread work (untimed CPU traces, Holter cold, 3 traces per arm, median, ms; streamComplete ā drain end):
| item | base | M2 |
|---|---|---|
| drain window | 1,548 | 1,016 |
| main thread busy | 1,038 | 720 |
| flushPending (fragment build, incl.) | 435 | 236 |
| quantizeInterleaved / latticeCollapses / mergeGeometry | 90 / 65 / 53 | 0 / 0 / 0 |
Records coverage (untimed, 4021111b8d = this head, one cold load each; meshes that carried worker records):
| fixture | records | missing (old path) |
|---|---|---|
| Holter | 97.3% (55,337 / 56,874) | 1,537 |
| BWK | 93.6% (36,787 / 39,290) | 2,503 |
| skolebygg | 92.3% (11,463 / 12,419) | 956 |
| advanced_model | 76.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.