manifoldxplan · path tracer v1 execution: inline, one session#1752026-11-08
0 of 0 done

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.

goalScene.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

questionchoicewhy
DSL or WGSLHand-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.
MegakernelOne 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 layout32-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.
TexturesBase-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.
GlassglTF 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 skyAn 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.
Denoiser5 à-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
  1. Tests: a glb written by manifoldx.gltf.build_glb with a translated, rotated box and a camera reads back to the same world-space triangles, normals, colours, UVs, material factors and camera pose.
  2. 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. overrides maps a node or material name to material fields.
verify

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
  1. 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.
  2. 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.
verify

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
  1. Device from the high-performance adapter with its storage-buffer limits; buffers for nodes, triangles, shading data, materials, textures.
  2. Tracer.aovs() returns albedo, normal and depth arrays. A script renders the factory glb next to Engine.render_frame of the same triangles and camera. Milestone 1: send both images.
verify

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
  1. 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.
  2. 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.
verify

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
  1. 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.
verify

Breaking a kernel line (the Fresnel term) makes the glass test fail.

6À-trous denoiserwgsl/atrous.wgsl, tracer.pypending
  1. 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.
verify

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
  1. 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.
  2. A trace suite in benchmarks/: samples per second on the Cornell box and a large mesh, by size.
verify

uv run python -m benchmarks trace prints a table; the CLI writes a PNG.

8Measure an interior on an RTX 3060PR descriptionpending
  1. 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).
verify

The numbers go in the PR under ## Timing with the GPU line.

Where this plan departs from the spec

Global constraints