Path tracer v1: manifoldx.trace
A progressive path tracer for any manifoldx scene, written as hand-written WGSL compute kernels that wgpu-py dispatches, over a BVH built in numpy. It reads a .glb, accumulates samples per pixel, denoises with an edge-aware à-trous filter and writes an sRGB PNG.
Scene.from_glb(path, overrides), Tracer(scene, w, h), tracer.step(n), tracer.image(denoise=True), and manifoldx trace scene.glb --camera ... --spp 256 -o out.png.
architectureFour modules under src/manifoldx/trace/: gltf_read.py (glb to flat triangle arrays in world space), bvh.py (binned SAH, built level by level with vectorised numpy), tracer.py (buffers, pipelines, accumulation, tonemap), and WGSL kernels in trace/wgsl/ loaded as package data: common (structs, RNG, BVH walk), primary, path, atrous.
techPython 3.13, numpy 2, wgpu-py 0.31 compute pipelines, Pillow (already the viz extra) to decode textures and write PNG. No new dependency.
specThe consumer's high-quality-mode spec, sections 03, 05 and 07 (delivery steps 1 to 4). This plan covers the manifoldx side only.
issue#175
Decisions this plan takes
| question | choice | why |
|---|---|---|
| DSL or WGSL | Hand-written WGSL, dispatched straight through wgpu, not manifoldx.compute. | The DSL compiles per-entity kernels over ECS columns: no structs, fixed arrays, loops with a stack, or storage buffers of triangles. Answers the spec's first open question. |
| Megakernel | One path per pixel per dispatch, all bounces in one kernel. | Simplest, and a 1024×640 dispatch stays far under a GPU watchdog. Wavefront only if the measure says so. |
| BVH layout | 32-byte nodes (min, left-or-first; max, count), children adjacent, leaves of up to 4 triangles reordered contiguous; triangles stored as v0, e1, e2 for Möller–Trumbore; shading data in a second buffer. | Two vec4 reads per node, one cache line per triangle test. |
| Textures | Base-colour textures packed as RGBA8 into one storage buffer with a table of offsets, sampled bilinearly in the kernel; vertex colours multiply. | The scenes this serves have textured floors; a texture array would force one size. |
| Glass | glTF rules: KHR_materials_transmission without KHR_materials_volume is thin-walled (straight through, Fresnel reflect); with a volume it refracts. alphaMode BLEND becomes thin transmission of 1 − alpha. Shadow rays pass thin glass, attenuated. | Window panes are single surfaces: refraction would trap rays. Sunlight through a window must reach next-event estimation. |
| Sun and sky | An Environment: an equirect HDR map (from ibl.EnvironmentMap, a sky gradient or a file) sampled by a luminance CDF, plus an optional sun disk sampled in its cone. MIS with BSDF sampling for both and for emissive triangles. | The sun is what lights an interior; sampling it directly cuts noise most. |
| Denoiser | 5 à-trous passes on demodulated radiance (radiance / albedo), weights from normal, depth and luminance; albedo multiplied back. | Keeps texture detail the filter would blur. |
Tasks
1glb reader into a trace Scenesrc/manifoldx/trace/gltf_read.py, scene.py · tests/trace/test_scene.pypending
- Tests: a glb written by
manifoldx.gltf.build_glbwith a translated, rotated box and a camera reads back to the same world-space triangles, normals, colours, UVs, material factors and camera pose. - Read node trees (TRS or matrix), accessors with stride and normalised integers, indices of any width, PBR factors, emission and its strength, transmission, IOR, volume, alpha mode, base-colour textures, cameras and
KHR_lights_punctual.overridesmaps a node or material name to material fields.
uv run pytest tests/trace/test_scene.py passes; the factory glb and a house glb read without error.
2Binned SAH BVH in numpysrc/manifoldx/trace/bvh.py · tests/trace/test_bvh.pypending
- Tests: every triangle sits in exactly one leaf; every node's box holds its children; closest hits of 2,000 random rays found by walking the BVH in numpy equal brute force.
- Build all nodes of one depth at once: centroid bounds by
reduceat, 16 bins per axis, SAH sweep by cumulative sums, partition by a stable sort on (node, side). Leaf at 4 triangles or when no split beats the leaf.
uv run pytest tests/trace/test_bvh.py; 240k triangles build in a few seconds.
3GPU upload and primary rays: albedo, normal, depthsrc/manifoldx/trace/tracer.py, wgsl/common.wgsl, wgsl/primary.wgslpending
- Device from the high-performance adapter with its storage-buffer limits; buffers for nodes, triangles, shading data, materials, textures.
Tracer.aovs()returns albedo, normal and depth arrays. A script renders the factory glb next toEngine.render_frameof the same triangles and camera. Milestone 1: send both images.
A test at 64×64 checks the depth of a plane against its analytic value; the side-by-side matches in silhouette.
4Path tracing kernelwgsl/path.wgsl, trace/environment.py, trace/scenes.pypending
- Lambert plus GGX (VNDF sampling), thin and solid dielectrics, emission; environment CDF, sun cone, punctual lights, emissive triangles by an area CDF; MIS by the power heuristic; up to 6 bounces, Russian roulette from the third.
- Accumulation buffer;
step(n)dispatches n passes;image()applies exposure, ACES fit and sRGB.scenes.cornell_box()builds the box in code. Milestone 2: the first noisy image.
Furnace test: a white Lambert sphere in a uniform environment of radiance 1 renders 1 ± 2 %. A Lambert plane under a sun matches albedo · E · cosθ / π.
5Reference scenestests/trace/test_references.py, tests/trace/refs/*.pngpending
- Cornell box, glass sphere, room with a window at 64×64, fixed seed and sample count, compared by mean error with stored references rendered at high sample count on a GPU. Skipped without an adapter.
Breaking a kernel line (the Fresnel term) makes the glass test fail.
6À-trous denoiserwgsl/atrous.wgsl, tracer.pypending
- First-hit albedo, normal and depth averaged across samples; 5 passes at step 1, 2, 4, 8, 16. Milestone 3: denoised image next to the noisy one.
On the Cornell box at 16 spp, denoised error against the 4,096 spp reference is lower than the raw error.
7The command and the benchmarksrc/manifoldx/cli.py, benchmarks/trace.pypending
manifoldx trace scene.glb [--camera px,py,pz:tx,ty,tz[:fov]] [--size WxH] [--spp N] [--sun ...] [--no-denoise] -o out.png, printing samples per second.- A
tracesuite inbenchmarks/: samples per second on the Cornell box and a large mesh, by size.
uv run python -m benchmarks trace prints a table; the CLI writes a PNG.
8Measure an interior on an RTX 3060PR descriptionpending
- A house glb at 1024×640: samples per second, and seconds until the denoised image stops changing visibly. Over 3 minutes: stop and rethink (wavefront, smaller leaves, half resolution plus upsampling).
The numbers go in the PR under ## Timing with the GPU line.
Where this plan departs from the spec
- The spec says no textures in v1. The scenes it serves texture their floors, so base-colour textures are sampled; normal and roughness maps stay out.
- Glass in shadow rays: rays pass every dielectric, thin or solid, tinted by (1 − F) and the base colour, so a glass sphere casts a light shadow instead of a black one. Caustics are still out (
src/manifoldx/trace/wgsl/path.wgsl,transmittance). - Reference tests compare 6×6 block averages, not pixels: at 64 spp the per-pixel noise hid a Fresnel term set to zero (
tests/trace/test_tracer.py,error). - The denoiser test runs on the window room only. At 48×48 a 16-pixel step spans a third of the Cornell box and every filtering adds error there; at 1024×640 the filter cuts the Cornell box's error 3.5 times.
- Directly seen emitters are marked as misses in the guide buffer so the filter leaves them alone; a lamp otherwise bleeds into its neighbours.
Global constraints
- No hardware ray tracing and no new native dependency.
- Tests that need a GPU skip without an adapter and stay at 64×64 or smaller, so CI's software Vulkan runs them in seconds.
- Material colours are sRGB in manifoldx and linear in glTF: the reader keeps glTF's linear values.