Skip to content

Trace caustics from the lights and splat them to the camera (crucible#1715) - #52

Draft
anxuanng wants to merge 4 commits into
mainfrom
1715-light-tracing
Draft

anxuanng wants to merge 4 commits into
mainfrom
1715-light-tracing

Conversation

@anxuanng

@anxuanng anxuanng commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Part of codepawl/crucible#1715.

Opt-in light tracing for caustics: caustics: "light-tracing" (packing option) with causticPhotons (1 to 64 light paths per pixel sample, default 1). Off by default.

What it does

  • Paths start on the emissive triangles (emissive CDF, point uniform in area, cosine direction on a random face), follow only smooth glass and mirror bounces (dispersion with the hero wavelength from Trace spectral dispersion for glass: hero wavelength per path (#46) #49) and, at the first non-singular lobe after at least one solid (not thin-walled) caster, join the pinhole camera with a visibility ray and are splatted into a u32 fixed point buffer (new binding 15, atomicAdd, stochastic rounding). The host adds the splat to the film sums when it reads the film back (add_caustic_splat), rescaled to the photons really traced.
  • Camera paths drop what the light paths carry: a diffuse vertex, then a smooth caster, then an emitter counts nothing. No MIS between the two.
  • caustic_photons takes the first reserved word of KernelParamsV2 (byte 324, still 336 bytes); the flag is kernel_flags bit 8192.
  • Off compiles the feature out (FRAME_HAS_CAUSTIC_LIGHT_TRACING is rewritten to false per frame, as the texture mip flag is).
  • Bounded and validated: photons 1 to 64; the packer refuses orthographic, noiseThreshold, an unknown value or causticPhotons without light tracing, each naming what to pass.

Limits (documented in the README): emissive triangles only (analytic lights and the environment are not light traced), pinhole (no depth of field in the caustic), no adaptive sampling, a caustic seen through glass is not joined, emission direction is not importance sampled toward casters.

Evidence

Why no rainbow appeared

The storyboard's prism lies flat on the floor under a sun that is high in the sky. With a debug counter at each stage of the light path (temporary, removed), 540 x 960, 8 samples x 32 photons: 132.7 M photons emitted, 92.4 M hit a surface, 189 k vertices after a caster, 23.7 k reached a diffuse surface, 23.0 k were inside the image, 29 passed the camera visibility test. The shadow ray of the other 22,989 hits the prism itself, and a map of where the photons land puts 99.9% of them under the prism (x -0.15 to 0.15, z -0.25 to 0.25). The cause is physics, not a culling bug: a ray entering the left face at that angle meets the right face at 53 degrees, past the 41 degree critical angle for glass of 1.517, is totally internally reflected and leaves through the base into the floor. There is no fan to the right of that prism, so the camera-path image has none either. Light tracing on that scene adds 1.0 in total to the picture (the difference image is black: https://raw.githubusercontent.com/codepawl/cook/evidence-52/diff-as-built.png).

The scene tools now take --prism-tilt (turn about the prism axis, resting on its lowest edge) and --prism-lift (a stand). With tilt -30 degrees and lift 0.5 m the entry face is near the angle of minimum deviation and a fan lands on the floor: https://raw.githubusercontent.com/codepawl/cook/evidence-52/diff-s64-p32.png is the difference (light tracing on minus off, same seed, 64 x 32): added energy 3382, 3298, 3092 (R, G, B summed over the image), 15,199 pixels above 10% of the peak, box x 53 to 539, y 434 to 766, centroid (376, 572).

Reference on the fan

Camera paths only (filterGlossy 0, 2048 samples, no fill panel) against light tracing (128 x 8), sun radius 0.3 with the same power, 270 x 480, mean of a 60 x 50 box inside the fan (x 150 to 210, y 270 to 320):

dispersion camera paths only (R, G, B) light tracing (R, G, B) ratio
0 0.2012, 0.1909, 0.1735 0.1696, 0.1609, 0.1462 0.843, 0.843, 0.843
1 0.1979, 0.1931, 0.1766 0.1697, 0.1621, 0.1507 0.857, 0.840, 0.853

The ratio is stable across seeds and photon counts (a second reference seed gave 0.2021, 0.1912, 0.1768; 256 x 16 light tracing gave 0.1691), so it is bias, not noise. It is outside the 10% I aimed for and I do not yet know where the missing 15% is. With a sun of radius 0.8 the ratio was 0.96 (0.148 against 0.143), so it depends on the sun size. The earlier table in this description (ratio 1.0) was a box in the shadow that holds no caustic; the camera-path reference there equals the image without any sun at all.

What the reference showed, and the fix

The camera-path reference at the default filterGlossy found almost none of the caustic (box mean 0.082 with a sun of radius 0.8, the same as with no caustic), and with filterGlossy: 0 it found it (0.148). The blur turns the camera path's smooth glass into a non-singular lobe after a diffuse bounce. With light tracing that also meant the camera path's chain was not dropped, so a blurred copy of the caustic stayed next to the light traced one. The blur now skips smooth glass and mirror lobes while light tracing is on (test: light tracing keeps smooth glass sharp under filterGlossy).

Timings and smoothness

Storyboard scene with tilt -30, lift 0.5 (prism, sun radius 0.09 emission 900, fill panel, floor, dispersion 1) on an RTX 3060 shared with other jobs, so the wall times carry neighbour load (they vary by up to 2x between identical runs). Roughness is the mean absolute difference of neighbouring pixels over the mean, in a 120 x 100 box on the fan (x 300 to 420, y 540 to 640 at 540 x 960; doubled at 1080 x 1920):

size samples x photons roughness wall
540 x 960 16 x 4 1.07 45 s
540 x 960 32 x 16 0.55 49 s
540 x 960 64 x 32 0.29 18 s
540 x 960 128 x 64 0.14 47 s
540 x 960 256 x 64 0.10 70 s
1080 x 1920 64 x 32 0.285 42 s
1080 x 1920 128 x 64 0.142 132 s
1080 x 1920 256 x 64 0.101 262 s

Roughness depends on samples x photons and not on the size; time grows with the pixel count. The fan reads as smooth at 128 x 64 (0.14) and reaches 0.10 at 256 x 64. Stills: https://raw.githubusercontent.com/codepawl/cook/evidence-52/540x960-s16-p4.png, https://raw.githubusercontent.com/codepawl/cook/evidence-52/540x960-s64-p32.png, https://raw.githubusercontent.com/codepawl/cook/evidence-52/540x960-s256-p64.png, https://raw.githubusercontent.com/codepawl/cook/evidence-52/1080x1920-s256-p64.png.

Not done: importance sampling of the emission toward the casters. Only 0.14% of the photons cross a caster here, so it would cut the time a lot (the photon count is the cost), but the fan is smooth without it at the counts above, and the 15% bias should be understood first.

How I checked it

  • node --test: 187 pass, including tests/caustic-light-tracing.test.mjs (option validation, off packs the same bytes, the flag and photon word are the only differences, kernel text checks, a GPU test that light tracing changes no pixel of a scene without a caster, and a GPU test that filterGlossy does not move a light traced image of smooth glass).
  • cargo test (addon crate): passes, including the new specialization test (flag off or zero photons compiles the feature out), the splat scale and kernel text test, and add_caustic_splat unit tests.
  • Reference and timing scripts: tools/caustic-reference.sh, tools/caustic-sweep.sh, tools/caustic-render.mjs, tools/caustic-noise.mjs, tools/caustic-diff.mjs.

…#1715)

Opt-in `caustics: "light-tracing"` (`causticPhotons` 1 to 64 per pixel sample): paths start on the emissive triangles, follow smooth
glass and mirror bounces (dispersion with the hero wavelength included) and, after a solid caster, join the pinhole camera at the
first rough or diffuse hit and are splatted into a u32 fixed point buffer (binding 15). The host adds the splat to the film sums.
Camera paths drop the diffuse, smooth bounce, emitter chain they would find themselves. No MIS between the two. Off compiles the
feature out of the kernel.
…e fan finder tools (crucible#1715)

The dispersion short's prism scene sent its light out through the prism base by total internal reflection, so no fan reached the
floor and light tracing had nothing to add there. With a tilted, lifted prism the fan exists, and a camera-path reference on the
fan showed that filterGlossy blurred the camera paths' glass into a non-singular lobe: the caustic chain was not dropped, and the
blurred copy of the caustic stayed in the image next to the light traced one. The blur now skips smooth glass and mirror lobes
while light tracing is on.

Tools: --prism-tilt, --prism-lift, --no-fill and --filter-glossy for caustic-render, caustic-diff (where light tracing added
light), and caustic-reference takes SCENE_FLAGS, SIZE and BOX so its box can sit on the fan.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant