Trace in the browser
gia-uh/manifoldx#178 · follow-up to #175 and #177 · 2026-11-08
Goal
Run manifoldx.trace's WGSL kernels on the visitor's GPU through WebGPU. A page shows its raster view while the camera moves; when the camera stops, it shows a progressive, denoised trace of the same glb. Python and the page dispatch the same .wgsl files, so they compute the same picture.
Decisions
| Question | Choice | Why |
|---|---|---|
| Fit 8 storage buffers per stage | Four read-only scene buffers: nodes, tris, shade and one aux array of vec4 that holds materials, the texture table, texels, lights, emissive CDF and the environment tables, at offsets passed in Params. The two AOV buffers become one (two vec4 per pixel). | path: 4 + 2 written = 6; à-trous: 4 + 3 = 7; denoise prep: 4 + 4 = 8. The hot traversal buffers stay apart. |
| Denoiser prep | prep.wgsl: demodulation, variance and its 3×3 blur, the normalised guide. Python's denoised() uses it too. | No CPU pass in the page; one code path. |
| Scene transport | The page reads the .glb itself (glb.js, a port of gltf_read.py) and builds the BVH in JS (bvh.js, binned SAH, recursive over typed arrays). No packed file. | A site's overrides and hide globs change materials and drop nodes per view, so the scene has to be assembled in the page anyway; a build-time packed file would freeze one view. The glb is already served. |
| Sky | sky.js: Environment.sky and its sampling tables ported. | A site passes sun_direction, turbidity, ground. |
| Output | Auto exposure by a one-workgroup reduction, ACES and sRGB in a fragment shader that draws a full-screen triangle onto a canvas. | Works with any canvas format; no readback. |
| Shipping | JS in src/manifoldx/trace/web/, loading the WGSL by URL; manifoldx.trace.web.copy_to(dest) writes JS + WGSL into one folder a site serves. | The WGSL files stay single-sourced. |
API in the page
import { createTracer, loadGlb } from './trace/trace.js';
const scene = await loadGlb(url, { overrides, hide }); // what Scene.from_glb and a node filter take in Python
const tracer = await createTracer(scene, { width, height, environment, canvas });
tracer.setCamera({ position, target, up, fov }); // resets the accumulation if it moved
tracer.frame(); // a few samples within a time budget, then draw
Tasks
1. Pack the buffers. WGSL bindings and
Params offsets; Python _upload; one AOV buffer. The existing tests/trace stay green.2. Prep in WGSL.
prep.wgsl; denoised() through it; a test against the old numpy prep.3. JS scene.
glb.js, bvh.js, sky.js; overrides and hide globs as a Python service applies them.4. trace.js. Device, pipelines, step loop with a frame budget, reset on camera change, denoise, display. First image of a furnished house in headless Chromium.
5. Tests. Playwright on SwiftShader WebGPU: the page's radiance at a fixed seed against Python's on the reference scenes; skipped without an adapter; run in the CI browser job.
6. Docs and hand-over. Know-how section, CHANGELOG, a demo page; samples per second recorded on the issue as devices report them.
Departures
- Parity is statistical, not "within float tolerance": page and Python run on different devices whose
sin,cosandexpdiffer in the last bits, so primary hits match exactly and grazing paths later diverge. The tests compare 4×4 block averages against Python's own spread between two seeds, and the means within 3%. - The emissive CDF lists triangles in the scene's order (Python too), so a BVH of another shape samples the same lamp for the same random number.
- The BVH build runs in a module worker: a 777k-triangle building takes about 4 s to build.
- The fragment stage binds no writable storage: the exposure from the reduction is copied into the draw's uniform.
- Headless Chromium does not composite a WebGPU canvas;
image()renders the same pass offscreen and is what the tests read.
Risks
- SwiftShader is slow: tests use 48×48 scenes and a few samples.
- Some integrated GPUs time out long dispatches (TDR): the loop dispatches one sample per submit and measures the time with
onSubmittedWorkDone. - The JS BVH differs in shape from numpy's; closest hits are the same, so images match up to float order.