How to measure the WebGPU ray-marcher honestly, and the rationale behind the
empty-space-skipping design in render/scene-renderer.ts.
renderToView() returns as soon as the work is submitted; WebGPU executes
asynchronously. The ms/frame shown in the demo status bars is therefore JS +
submit time, not GPU cost. It cannot find a shader bottleneck.
Use SceneRenderer.timePass(w, h, iters) instead — it wraps the render pass in a
timestamp-query and returns the median GPU pass duration in ms. device.ts
requests the feature when the adapter has it, so this is free when unavailable
(timePass returns NaN).
Two caveats learned the hard way:
- Profile headless (Deno). Chrome quantizes timestamps unless the page is cross-origin isolated; Deno gives full resolution.
- Keep passes well under the GPU watchdog. Passes in the hundreds of ms
(macOS/Metal) corrupt subsequent timestamp reads — we saw negative deltas
appear only after a 633 ms pass.
timePassdiscards non-positive samples, andprofile-fiducials.tsprofiles at 448² so no pass gets near the danger zone. Relative scaling is resolution-independent; only absolutes shrink.
render/test/profile-fiducials.ts diffs GPU time across configurations so each
delta attributes cost to exactly one variable:
- volume only → baseline
- + N empty fields (n=0) → isolates per-step call/dispatch overhead
- + N full fields → adds the loop body
- sweep field count / spheres-per-field / step size → which one does cost track?
This is what identified the Volume+Fiducials bottleneck. Adding fields was free;
the cost tracked total sphere count × march steps: ~100 length() tests at every
one of ~570 sub-millimetre steps, virtually all rejected.
Note this beats a GPU capture tool for our purposes: the fragment shader is generated, so we control every variable. (RenderDoc has no Metal backend and cannot capture Chrome on macOS at all; Nsight/RenderDoc on a Windows/Linux NVIDIA/AMD box are worth reaching for only for instruction-level detail — occupancy, divergence — after the algorithmic cost is gone.)
A field opts in via providesSkip + skipWGSL(slot), emitting
skip_<kind><slot>(wp) -> f32: a conservative lower bound on the distance from
wp within which that field contributes nothing. 0 means "no information" and
forces the normal fine step, so the default is safe by construction — a field
that says nothing can never cause geometry to be skipped.
Non-obvious decisions, and why:
- The horizon is cached and coasted — that is the optimization. Computing the
fiducial bound costs the same O(N) loop as sampling it. A design that recomputes a
global
min(skip_i)every step would pay the full O(N) per step and save nothing. The renderer stores aresume_<field>per field and re-evaluates the bound only when the ray reaches it. - A global leap needs every field to defer. A dense
ImageFieldnever yields a bound, so inside the volume only per-field coasting fires; the global jump is what makes sparse-only scenes (markups) fast. Both paths are exercised in the profiler (sections A–D and E respectively). - No variable-step opacity correction is needed. Leaps only cross space every
field proved empty, so no sampled segment ever changes length and the fixed-step
pow(1-a, step/unit)integration stays exact. (This is why leaping through participating media would be wrong — don't "optimize" that later.) - One step of slack.
wpis the jittered sample position (±0.5 step offt), so the bound has a full step subtracted before becoming a horizon. - Transforms disable skipping. A nonlinear
TransformFieldwarp invalidates a distance measured in un-warped space (you would need the warp's Lipschitz bound). The renderer excludes any field carrying atransform.
Spheres are an exact SDF. The conservative form used is nearest-centre distance
minus the field's largest radius: since min_j(d_j) <= d_k and max_r >= r_k for
every k, it never exceeds the true min_k(d_k - r_k), so it cannot skip a sphere —
and it needs only dot products in the loop plus one sqrt.
| before | after | |
|---|---|---|
| scene | 236 ms | 26.9 ms |
| fiducial cost | 214 ms | 11.2 ms |
| fiducials only | (broken) | 10.0 ms |
Output is byte-identical to the pre-optimization render — the correctness bar for a conservative bound. Any change here must keep that property (or explain precisely why sample phase changed, as the global leap does in sparse-only scenes).
The scene is now volume-bound (~16 ms ImageField). Lowering that floor needs a
per-field occupancy grid implementing the same skip_i interface — which also
generalizes to SegmentField and fiber strands, where an analytic distance bound is
impractical. Each field owning a grid over its own extent/resolution gives the
"small high-res structure inside a big sparse volume" case for free, without a true
nested hierarchy.
Tried and rejected — don't re-derive it. The idea: give every field that lacks its
own bound a default skip = distance to its own world AABB, so rays could skip the parts
of the scene box outside a given volume. It looks obviously right for Multi-Volume, where
Panoramix sits +200 mm R of CTACardio and each ray spends much of its span outside one
volume.
Measured with render/test/profile-boxskip.ts (A/B in one process, warmed up, 448²,
M-series):
| scene | boxSkip off | on | delta |
|---|---|---|---|
| MultiVolume | 23.45 ms | 25.50 ms | +8.7% |
| Volume+Fiducials | 26.72 ms | 28.68 ms | +7.3% |
| Segmentation | 6.80 ms | 13.35 ms | +96.5% |
| SingleVolume | 18.66 ms | 15.77 ms | −15.5% |
Why the theory fails: ImageField's out-of-box sample was already nearly free — it
early-returns on the texture-bounds test — so there was no per-step cost to eliminate.
Meanwhile every field now pays a box distance plus horizon bookkeeping at every step it is
inside its box, which is most of the march (the scene box is the union of the field
boxes). Fields that already have a cheap early-out are hurt worst: SegmentField's
v<=0.02||v>=0.98 test means its sample was ~1 texture fetch, so the added bookkeeping
nearly doubles it. The global all-defer leap almost never fires, because most rays are
inside at least one volume for their whole traversal.
The lone SingleVolume win survives warm-up but has no algorithmic explanation — there
the field box is the scene box, so the bound is 0 at every sample and no skip can occur.
It is almost certainly a shader-compiler/occupancy artifact of restructuring the loop, and
not something to build on.
Kept behind SceneRenderer.boxSkip (default false) rather than deleted, so the result
stays reproducible and can be re-measured on other GPUs — the balance may differ on
NVIDIA/AMD. The generalizable lesson: a skip bound only pays when it removes work that
was actually expensive. providesSkip is opt-in for exactly this reason; a field should
supply a bound only when it is genuinely informative (like FiducialField's O(N) loop),
not merely when one is computable. For dense volumes the real win is an occupancy grid over
air inside the box, not the box itself.
layout: "auto" derives the bind group layout from what the shader actually
references. In a scene of purely procedural fields (fiducials/markups only) nothing
samples a texture, so binding 2 is absent from the derived layout while the bind
group still supplied it → invalid pipeline → the view silently renders nothing.
The sampler declaration and its bind entry are now emitted under the same
usesSampler() condition so they cannot drift. Watch for this whenever adding a
field kind with bindingCount = 0.