Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Kernel: one ray query in the hardware trace (#99)

Yujia4412f81320f3
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index bb861cf..14ad125 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -2483,6 +2483,16 @@ sky's light sample became a function). Next for the cache: its training as a pas
(training that does not thin out with the resolution, deeper paths, a smaller kernel), and with it the table's
size; a pass for the few pixels that go on, which is what the segment test waits for.
+**One ray query in the hardware trace (2026-11-16, after it).** The two cutout shortcuts made Zero Day's and
+Kabukichō's trace slower, not faster (0.4 to 0.8 ms), and nothing explained it. The cause: `trace` is inlined at
+each of its calls, and it held three ray queries, one for rays that take cutouts as opaque (the shortcuts'), one
+for a shadow ray's first hit, one in the loop of ordered steps; each one written costs every ray, taken or not.
+They are one query in one loop now (a shadow ray's first step takes any hit; a scene without cutouts keeps its
+plain query), with the same hits: render-identity 0 changed of 229, bench-window's shots as main's. The trace:
+Zero Day 9.41 -> 8.41 ms (-10.6 %), Bistro 8.57 -> 8.05 (-6.1 %) and 4.58 -> 4.36 along its camera path, the Bistro
+slice 9.00 -> 8.29, a glass scene 4.97 -> 4.60, Kabukichō 16.23 -> 14.61 guarded (-10 %); scenes without cutouts
+(lamp-rooms, the fire scene, Tokyo) as they were. The shortcuts now save Zero Day 0.3 ms where they cost it 0.4.
+
## 2. Temporal fixes
Targets (2026-11-08): bench-temporal, bench-ghosting and their UE MRQ references move to Bistro night (§ 0 step 7); the
diff --git a/docs/RUNTIME.md b/docs/RUNTIME.md
index 3862b87..77bc747 100644
--- a/docs/RUNTIME.md
+++ b/docs/RUNTIME.md
@@ -1355,12 +1355,12 @@ view, the way to hold a live picture to the offline one).
|---|---|---|---|
| 1 `A_GLASS_SHADOWS` | shadow rays to the analytic lights and the sky pass glass in full, untinted | light under tinted glass 4.3 x (`shadow/glass-tint`) | a glass scene +34 % (khronos-transmission 5.20 -> 6.97 ms) |
| 2 `A_MEDIA_SHADOWS` | only homogeneous media dim shadow rays: smoke and fire cast no shadow | light behind optical depth 1: e x (`shadow/medium`) | the Bistro slice +0.84 ms (9 %), the fire scene +0.10 |
-| 4 `A_CUTOUT_PATHS` | cutouts are opaque to rays past a camera hit's own, off mirror paths | a camera hit's own light is whole (`sky/cutout-clear-live`: its samples take the share those rays cannot bring, `light_mis`); what a cutout lights sends nothing on (`sky/cutout-bounce`: black) | Bistro +1.48 ms of 9.06 |
-| 8 `A_CUTOUT_EMITTERS` | cutouts are opaque to emitters' shadow rays | an emitter behind a clear card: black (`shadow/cutout-emitter`) | Bistro +0.62 ms |
+| 4 `A_CUTOUT_PATHS` | cutouts are opaque to rays past a camera hit's own, off mirror paths | a camera hit's own light is whole (`sky/cutout-clear-live`: its samples take the share those rays cannot bring, `light_mis`); what a cutout lights sends nothing on (`sky/cutout-bounce`: black) | Bistro +1.63 ms of 8.54 |
+| 8 `A_CUTOUT_EMITTERS` | cutouts are opaque to emitters' shadow rays | an emitter behind a clear card: black (`shadow/cutout-emitter`) | Bistro +1.35 ms |
| 16 `A_FAR_EMITTERS` | the light tree proposes no emitter beyond 120 m; without a tree, the emitter cull (60 m, 120 m in view) | panels 150 m up: 3 % of their light (`light/emitter-far`); Kabukichō's and Tokyo's shots unchanged | Tokyo from the air 4.7 -> 9.2 ms; the 3 x 3 district +0.15 |
-Measured 2026-11-15 with the loop guard on. Bistro held still, ΔE against the offline render: 1.82 as shipped, 1.66
-without the two cutout bits, which cost Kabukichō and Zero Day time instead (0.75 and 0.80 ms less without them).
+Measured 2026-11-15 and 16 with the loop guard on. Bistro held still, ΔE against the offline render: 1.82 as
+shipped, 1.66 without the two cutout bits (+3.5 ms; Zero Day, with few cutouts, +0.3).
Two more were removed that day: a single-query shadow ray past cutouts (it saved nothing: Bistro 10.13 ms with it,
10.09 without, Tokyo +0.05) and the switch for the sampler below.
diff --git a/renderer/shaders/trace_hw.wgsl b/renderer/shaders/trace_hw.wgsl
index 8776dbb..98a90d7 100644
--- a/renderer/shaders/trace_hw.wgsl
+++ b/renderer/shaders/trace_hw.wgsl
@@ -8,14 +8,13 @@
// Ray tracing off (pathtrace.wgsl `rays_off`): past the camera ray, every ray misses.
if (rays_off) { return Hit(tmax_in, 0xffffffffu, 0.0, 0.0, NO_INST); }
let mask = select(0xffu, 1u, mode == TRACE_SHADOW);
- if (!has(F_MASKED)) {
- let h = trace_opaque(o, d, tmax_in, mask, mode != TRACE_CLOSEST);
- return h;
- }
- // A_CUTOUT_PATHS, A_CUTOUT_EMITTERS (TRACE_ANY_OPAQUE): cutouts opaque (pathtrace.wgsl `cutouts_opaque`,
- // `emitter_tr`).
- @if(!cutout_lite) if (cutouts_opaque() || mode == TRACE_ANY_OPAQUE) { return trace_opaque(o, d, tmax_in, mask, mode != TRACE_CLOSEST); }
- @if(cutout_lite) if (cutouts_opaque() || mode != TRACE_CLOSEST) { return trace_opaque(o, d, tmax_in, mask, mode != TRACE_CLOSEST); }
+ let any = mode != TRACE_CLOSEST;
+ // (A scene without cutouts: one query. A feature bit: the kernel has this or what follows, not both.)
+ if (!has(F_MASKED)) { return trace_opaque(o, d, tmax_in, mask, any); }
+ // Whether a cutout stops this ray as any triangle does: A_CUTOUT_PATHS, A_CUTOUT_EMITTERS (TRACE_ANY_OPAQUE)
+ // (pathtrace.wgsl `cutouts_opaque`, `emitter_tr`).
+ @if(!cutout_lite) let solid = cutouts_opaque() || mode == TRACE_ANY_OPAQUE;
+ @if(cutout_lite) let solid = cutouts_opaque() || any;
// Alpha-cutout triangles: every query forces opacity, and a cutout hit that fails its alpha test is stepped
// through. The ray query's own candidate loop (non-opaque traversal) lost hits on Metal: ~0.1 % of rays near
// cutout foliage missed even the opaque ground behind it or found it unlit (black pixels on the ground; the
@@ -24,29 +23,31 @@
// the cutout instances alone; or one candidate loop on them) came out within ±0.4 ms on Bistro: the leaves' alpha
// tests and the traversal past them cost it, however the queries are arranged. Culling the cutout geometry by its
// non-opaque flag (RAY_FLAG_CULL_NO_OPAQUE) is slow on Metal (Bistro's trace +1.7 ms).
- if (mode != TRACE_CLOSEST) {
- // Any hit blocks a shadow ray, except a cutout one that its alpha lets through.
- let h = trace_opaque(o, d, tmax_in, mask, true);
- if (h.tri == 0xffffffffu || is_proc(h) || !cutout(h.tri) || alpha_pass(h.tri, h.inst, h.u, h.v, h.t)) { return h; }
- }
+ //
// In order, each query starting past the last cutout hit its alpha let through. Shadow rays take up to 64 of them
// and count as blocked past that: with 16, as closest hits take, the rest counted as unblocked (light through dense
// foliage: Bistro's offline render up to 2.3 % bright in a region against the software BVH).
- @if(!cutout_lite2) let steps = select(16u, 64u, mode != TRACE_CLOSEST);
+ @if(!cutout_lite2) let steps = select(16u, 64u, any);
@if(cutout_lite2) let steps = 4u;
+ // One ray query in the source for every kind of ray. `trace` is inlined where it is called, and each query written
+ // in it costs every ray, taken or not: with a query of its own for opaque cutouts and one for a shadow ray's first
+ // hit beside the ordered ones, Zero Day's trace was 9.40 ms for 8.39 and Bistro's 8.56 for 8.05. A shadow ray's
+ // first query takes any hit; if that is a cutout its alpha lets through, the ordered ones start from the ray's
+ // origin.
+ var first = any;
var t0 = 0.0;
- for (var k = 0u; k < steps; k++) {
- let h = trace_opaque(o + d * t0, d, tmax_in - t0, mask, false);
+ for (var k = 0u; k < steps + u32(any); k++) {
+ let h = trace_opaque(o + d * t0, d, tmax_in - t0, mask, first);
if (h.tri == 0xffffffffu) {
- // (A shadow ray comes here only past a cutout that let it through: `shadow_hole`.)
- if (mode != TRACE_CLOSEST) { shadow_hole = true; }
+ // (A shadow ray comes here in order only past a cutout that let it through: `shadow_hole`.)
+ if (any && !first) { shadow_hole = true; }
return Hit(tmax_in, 0xffffffffu, 0.0, 0.0, NO_INST);
}
let t = t0 + h.t;
- if (is_proc(h) || !cutout(h.tri) || alpha_pass(h.tri, h.inst, h.u, h.v, t)) { return Hit(t, h.tri, h.u, h.v, h.inst); }
- t0 = t + 1e-4 * max(1.0, t);
+ if (solid || is_proc(h) || !cutout(h.tri) || alpha_pass(h.tri, h.inst, h.u, h.v, t)) { return Hit(t, h.tri, h.u, h.v, h.inst); }
+ if (first) { first = false; } else { t0 = t + 1e-4 * max(1.0, t); }
}
- if (mode != TRACE_CLOSEST) { return Hit(t0, 0u, 0.0, 0.0, NO_INST); }
+ if (any) { return Hit(t0, 0u, 0.0, 0.0, NO_INST); }
return Hit(tmax_in, 0xffffffffu, 0.0, 0.0, NO_INST);
}