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

QuestionChoiceWhy
Fit 8 storage buffers per stageFour 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 prepprep.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 transportThe 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.
Skysky.js: Environment.sky and its sampling tables ported.A site passes sun_direction, turbidity, ground.
OutputAuto 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.
ShippingJS 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

Risks