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.

The fast-history clamp: the diffuse history follows a lighting edit in colour (#55)

Yujia52e36f9959f0
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index f455394..b9c6794 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -1953,17 +1953,46 @@ converged frame of the state after, beside the frame k frames after a history re
| sign off, the floor beside it | 2.94 / 2.53 / 1.66 / 1.12 / 1.05 | 12.7 / 1.56 | 0.73 / 0.46 |
| sign on, the floor beside it | 0.35 / 0.48 / 0.76 / 0.95 / 0.97 | 7.28 / 0.84 | 0.80 / 0.43 |
-- **Open, the live view's largest fault found so far:** a lamp switched off leaves its pool on the wall for seconds,
- 11 times too bright after 8 frames, 3.3 times after half a second, still 17 % after 4 s, in blotches. The excess
- falls as 1 / k. The diffuse history is cut once, to 2 frames, when its fast mean leaves the slow one; half the old
- light is still in it, the slow mean is set to the fast one, the anti-lag has nothing more to find, and the history
- accumulates again from there (and after 45 frames the long history is back). The upscaler's 16 samples follow that.
- ReBLUR's form is another: the slow history clamped every frame into the range of a fast one (about 6 frames) around
- the pixel, so it is dragged along until it has followed. Not done here.
+- **The live view's largest fault found so far:** a lamp switched off leaves its pool on the wall for seconds, 11
+ times too bright after 8 frames, 3.3 times after half a second, still 17 % after 4 s, in blotches. The excess
+ falls as 1 / k: the diffuse history is cut once, on luminance, when its fast mean leaves the slow one, and what the
+ cut leaves still holds the old light. Closed below ("The fast-history clamp").
- The question #39 left: the radiance cache's 4 frames while the lighting is live. At 32 frames the bounce follows
later (lamp off: 2.24 for 1.66 at 32 frames, 1.38 for 1.12 at 128; lamp on: 0.50 for 0.72, 0.79 for 0.93). The 4
frames buy what they were said to and stay; the 0.16 ΔE they cost a scene at rest is the price.
+**The fast-history clamp (2026-11-13, after it).** ReBLUR's way of following a lighting change, in `dtemporal`: every
+frame, while the scene's lighting is live, the slow means around the pixel (3 × 3 on its surface) are held, a channel
+each, to the band of the fast means there (a quarter of each new sample; their mean ± 3 standard errors, from the fast
+luminances' spread), and the pixel's history is changed by the same ratios and counts for as many frames less. The
+fast mean in colour is kept in the buffer that was the `judge` pass's unread ping-pong partner: no new memory. The
+cut by luminance stays beside it. `bench-temporal --shot edit`, ΔE against the converged frame of the state after:
+
+| edit, region | 8 frames: before → now | 32 frames | 128 frames | a view with no memory, 32 |
+|---|---|---|---|---|
+| lamp off, its pool | 37.2 → 24.0 | 15.2 → 3.05 | 3.48 → 0.72 | 0.50 |
+| lamp off, its bounce | 6.77 → 6.07 | 3.38 → 1.32 | 0.73 → 0.40 | 0.45 |
+| lamp on, its pool | 5.68 → 5.12 | 1.92 → 1.10 | 0.62 → 0.51 | 0.43 |
+| lamp on, its bounce | 6.65 → 6.63 | 3.00 → 2.73 | 0.81 → 0.77 | 1.15 |
+| sign off, the floor beside it | 23.4 → 15.6 | 12.7 → 4.72 | 3.07 → 1.16 | 0.94 |
+| sign on, the floor beside it | 18.3 → 12.3 | 7.28 → 3.77 | 1.52 → 0.93 | 0.80 |
+
+- Elsewhere: the Bistro slice half a second after its explosion 6.02 → 5.76, at rest 2.82 → 2.83; bench-ghosting
+ (both scenes), Bistro and Thames held still and the fire shot as before (where the lighting is not live the clamp
+ does not run). test-render 187 pass. `dtemporal` 0.22 → 0.25 ms on the slice.
+- What was tried and left out, each measured (`out/liveq/gpu-runs-std.md`, A0–A10, B1–B8):
+ - what the code seemed to say (the slow mean set to the fast one at a cut; the test waiting for 6 frames): taking
+ both away changed little (the pool 2.7 times too bright at half a second for 3.3);
+ - the clamp on luminance alone, a pixel's or the neighbourhood's: the luminance follows and the colour does not (a
+ pink floor after the sign: ΔE 9.6 at 32 frames);
+ - a band of 2 standard errors: the edits a little closer (the pool 2.78 for 3.05) and the slice at rest 0.10
+ worse (2.92: it finds noise);
+ - the clamp alone, without the cut by luminance: 0.23 better again at the explosion, 0.11 worse at rest.
+- Left: the first 8 frames (the fast mean itself takes a few frames); the bounce, which is the radiance cache's
+ memory (the raw samples' own luminance 8 frames after the lamp goes: 1.31 on the floor); 3 to 6 times the ΔE of a
+ view with no memory at half a second. The long history on a settled scene is untouched: nothing measured here
+ speaks against it.
+
Not done here: filters sized by hit distance, which is what would let a short history stand without noise; the
Playground and the page still open a 1280 × 720 view; the live buffers are sized by the output (about 1.7 GB at
2560 × 1440), though the traced grid rarely fills it.
diff --git a/renderer/shaders/live.wgsl b/renderer/shaders/live.wgsl
index 3b5b80e..917a4be 100644
--- a/renderer/shaders/live.wgsl
+++ b/renderer/shaders/live.wgsl
@@ -59,9 +59,10 @@ struct Pixel { color: vec4<f32>, albedo: vec4<f32>, normal: vec4<f32>, emit: vec
@group(0) @binding(14) var<storage, read_write> ae_state: array<vec4<f32>, 2>; // exposure, target, mean log2 lum, valid
@group(0) @binding(15) var<storage, read_write> lumq: array<f32>; // quarter res log2 luminance (unexposed)
@group(0) @binding(16) var<storage, read_write> lumb: array<f32>; // edge-aware blur of lumq
-// Per traced sample, what `judge` finds of the output grid's colour history (`Judged`, f16). (`ae_in`: its ping-pong
-// partner, not read.)
-@group(0) @binding(17) var<storage, read> ae_in: array<vec4<u32>>;
+// Per traced sample, what `judge` finds of the output grid's colour history (`Judged`, f16), in `ae_out`. Until then
+// `ae_out` holds what the previous frame's `dtemporal` wrote to `ae_in`, its ping-pong partner: the diffuse fast mean
+// in colour (`fast_load`).
+@group(0) @binding(17) var<storage, read_write> ae_in: array<vec4<u32>>;
@group(0) @binding(18) var<storage, read_write> ae_out: array<vec4<u32>>;
// Specular history on the traced grid (rgb + history length), diffuse luminance moments (mean, mean square, fast mean, -).
@group(0) @binding(19) var<storage, read> shist_in: array<vec4<f32>>;
@@ -295,6 +296,14 @@ fn medium_share(q: u32) -> f32 {
return floor(sample_px[q].normal.w) / 255.0;
}
+// The diffuse irradiance's fast mean in colour (f16 rgb; a quarter of each new sample, as its luminance in the
+// moments), per traced sample: `dtemporal` reads the previous frame's from `ae_out` (before `judge` writes its own
+// there) and writes this frame's to `ae_in`, the next frame's `ae_out`.
+fn fast_load(j: u32) -> vec3<f32> {
+ let v = ae_out[j];
+ return vec3<f32>(unpack2x16float(v.x), unpack2x16float(v.y).x);
+}
+
fn sample_color(q: u32) -> vec3<f32> {
let s = sample_parts(q);
return (s[0] + s[1]) * medium_tr(q) + s[2];
@@ -348,6 +357,9 @@ const HIST_CENTRE: f32 = 16.0;
// noise, at which anti-lag leaves it two frames (`dtemporal`).
const HIST_MOVING: f32 = 48.0;
const ANTILAG: f32 = 0.15;
+// The fast-history clamp's band, in standard errors of the fast means' mean around the pixel (`dtemporal`). At 2 the
+// Bistro slice at rest read dE 2.92 for 2.82 (it found noise); at 3, 2.83.
+const FAST_BAND: f32 = 3.0;
// Diffuse history length from which the firefly cut stops (dtemporal).
const FIREFLY_FRAMES: f32 = 16.0;
@@ -374,7 +386,12 @@ fn dtemporal(@builtin(global_invocation_id) gid: vec3<u32>) {
var l = lum(irr);
var hd = vec4<f32>(0.0);
var hm = vec4<f32>(0.0);
+ var hf = vec3<f32>(0.0); // the fast mean in colour, reprojected
+ var slow_c = vec3<f32>(0.0); // the slow and the fast means in colour around the point
+ var fast_c = vec3<f32>(0.0);
var around = vec2<f32>(-1.0);
+ var fast_sd = 0.0; // of the fast luminances around the point (their spread in space), and how many
+ var fast_n = 1.0;
var speed = 0.0; // output pixels this frame
let ps = vec2<i32>(L.isz.zw);
if (L.size.w == 0u && ps.x > 0) {
@@ -408,16 +425,18 @@ fn dtemporal(@builtin(global_invocation_id) gid: vec3<u32>) {
let j = u32(ty * ps.x + tx);
hd += hist_in[j] * bw;
hm += mom_in[j] * bw;
+ hf += fast_load(j) * bw;
wsum += bw;
}
if (wsum > 1e-3) {
hd /= wsum;
hm /= wsum;
+ hf /= wsum;
} else {
hd = vec4<f32>(0.0);
hm = vec4<f32>(0.0);
}
- if (non_finite(hd.rgb) || non_finite(hm.xyz)) {
+ if (non_finite(hd.rgb) || non_finite(hm.xyz) || non_finite(hf)) {
hd = vec4<f32>(0.0);
hm = vec4<f32>(0.0);
}
@@ -425,17 +444,28 @@ fn dtemporal(@builtin(global_invocation_id) gid: vec3<u32>) {
// Slow / fast means over the 3x3 around it on the same surface (anti-lag compares them).
if (t > 0.0 && L.mode.z != 0u) {
var acc = vec2<f32>(0.0);
+ var acc2 = 0.0;
var na = 0.0;
for (var k = 0; k < 9; k++) {
let tx = i32(round(tc.x)) + k % 3 - 1;
let ty = i32(round(tc.y)) + k / 3 - 1;
if (tx < 0 || ty < 0 || tx >= ps.x || ty >= ps.y || !same_surface(tx, ty, pp.z, n, t)) { continue; }
- let m = mom_in[u32(ty * ps.x + tx)];
+ let j = u32(ty * ps.x + tx);
+ let m = mom_in[j];
if (m.w <= 0.0 || non_finite(m.xyz)) { continue; }
+ slow_c += hist_in[j].rgb;
+ fast_c += fast_load(j);
acc += vec2<f32>(m.x, m.z);
+ acc2 += m.z * m.z;
na += 1.0;
}
- if (na > 0.0) { around = acc / na; }
+ if (na > 0.0) {
+ around = acc / na;
+ slow_c /= na;
+ fast_c /= na;
+ fast_sd = sqrt(max(acc2 / na - around.y * around.y, 0.0));
+ fast_n = na;
+ }
}
}
}
@@ -457,6 +487,39 @@ fn dtemporal(@builtin(global_invocation_id) gid: vec3<u32>) {
}
let fast = select(l, mix(hm.z, l, 0.25), hd.w > 0.0);
var count = min(hd.w + 1.0, cap);
+ // The fast-history clamp (ReBLUR's), before the cut below: the cut acts once, on luminance, and the history it
+ // leaves still holds the old light (a lamp switched off left its pool on the wall 3.3 times too bright after half
+ // a second, 17 % after 4 s, falling as 1 / frames; a sign's, in its colour). So every frame the slow means around
+ // the point are held, a channel each, to the band of the fast means there: their mean +- FAST_BAND of its standard
+ // error (the fast luminances' spread over the root of their number; both means are of the same pixels, so what
+ // differs between neighbours cancels), and this pixel's history is changed as they are: by the same ratios, or
+ // by the difference itself in a channel the history has next to none of. Its luminance mean follows it (once:
+ // the cut then moves it toward the fast mean, it does not scale it again). What is left of the history counts
+ // for as many frames less, by the channel changed most among those that matter (a twentieth of the largest): the
+ // pixel goes back to the spatial filter. While the lighting is live, as the cut: on a settled scene there is
+ // nothing to find.
+ var hist = hd.rgb;
+ if (L.mode.z != 0u && around.x >= 0.0 && hd.w > 0.0 && !non_finite(fast_c) && !non_finite(slow_c)) {
+ let rel = FAST_BAND * fast_sd / sqrt(fast_n) / max(around.y, 1e-6);
+ let to = clamp(slow_c, fast_c * max(1.0 - rel, 0.0), fast_c * (1.0 + rel) + 1e-4);
+ if (any(to != slow_c)) {
+ let has = slow_c > vec3<f32>(1e-5);
+ let r = select(vec3<f32>(1.0), to / slow_c, has);
+ hist = max(hist * r + select(to - slow_c, vec3<f32>(0.0), has), vec3<f32>(0.0));
+ let was = lum(slow_c);
+ if (was > 1e-5) {
+ let rl = lum(to) / was;
+ m1 *= rl;
+ m2 = m1 * m1 + sd * sd * rl * rl;
+ } else {
+ m1 = max(m1 + lum(to) - was, 0.0);
+ m2 = m1 * m1 + sd * sd;
+ }
+ let big = max(slow_c, to);
+ let left = select(vec3<f32>(1.0), min(slow_c, to) / max(big, vec3<f32>(1e-6)), big > vec3<f32>(0.05 * max(big.x, max(big.y, big.z))));
+ count = max(min(count, hd.w * min(left.x, min(left.y, left.z)) + 1.0), 2.0);
+ }
+ }
// Anti-lag (as ReBLUR's): around this point the fast average left the slow one by more than its
// (neighbourhood-averaged) noise allows: the lighting changed here (a sign switched off, a door opened, a wall
// gone, a spark passing). The history is cut by how far, down to two frames at ANTILAG of the mean: a slow change
@@ -476,7 +539,9 @@ fn dtemporal(@builtin(global_invocation_id) gid: vec3<u32>) {
}
let a = 1.0 / max(count, 1.0);
let mom = vec4<f32>(mix(m1, l, a), mix(m2, l * l, a), fast, 1.0);
- let d = mix(hd.rgb, irr, a);
+ let d = mix(hist, irr, a);
+ let fc = select(irr, mix(hf, irr, 0.25), hd.w > 0.0);
+ ae_in[q] = vec4<u32>(pack2x16float(min(fc.rg, vec2<f32>(6.0e4))), pack2x16float(vec2<f32>(min(fc.b, 6.0e4), 0.0)), 0u, 0u);
hist_out[q] = vec4<f32>(d, count);
mom_out[q] = mom;
// Denoiser input; w = variance of the accumulated mean luminance (its noise estimate).
diff --git a/renderer/src/live.rs b/renderer/src/live.rs
index 3b8b30b..1272f29 100644
--- a/renderer/src/live.rs
+++ b/renderer/src/live.rs
@@ -246,7 +246,7 @@ impl Passes {
count: None,
});
entries.push(wgpu::BindGroupLayoutEntry { binding: 12, visibility: wgpu::ShaderStages::COMPUTE, ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), count: None });
- entries.extend([buf(13, false), buf(14, false), buf(15, false), buf(16, false), buf(17, true), buf(18, false)]);
+ entries.extend([buf(13, false), buf(14, false), buf(15, false), buf(16, false), buf(17, false), buf(18, false)]);
entries.extend([buf(19, true), buf(20, false), buf(21, true), buf(22, false), buf(23, false), buf(24, false), buf(25, true), buf(26, false), buf(27, true), buf(28, true), buf(29, true), buf(30, false)]);
// The merged layout: the buffers that stay, the slabs, and the offsets' uniform (merged.rs).
if g.merged {
@@ -275,7 +275,8 @@ pub struct Targets {
hist: [Buf; 2],
/// The traced grid's g-buffer (ping-pong: this frame's, the previous frame's).
gb: [Buf; 2],
- /// What `judge` finds of the colour history per traced sample (live.wgsl `Judged`); the second is not read.
+ /// Ping-pong, per traced sample: what `judge` finds of the colour history (live.wgsl `Judged`) in the one a frame
+ /// writes, the diffuse fast mean in colour (`fast_load`) in the other, which the next frame reads before `judge`.
alb: [Buf; 2],
/// Specular history, diffuse luminance moments (ping-pong, traced grid), the specular filter's two slots, and the
/// scratch `dtemporal` and the upscaled image share.
diff --git a/renderer/src/merged.rs b/renderer/src/merged.rs
index 29935d9..e5caaa9 100644
--- a/renderer/src/merged.rs
+++ b/renderer/src/merged.rs
@@ -159,7 +159,7 @@ pub const LIVE_BUFS: [LiveBuf; 25] = [
LiveBuf { name: "ae_state", binding: 14, slab: SLAB_V4, decl: "@group(0) @binding(14) var<storage, read_write> ae_state: array<vec4<f32>, 2>;" },
LiveBuf { name: "lumq", binding: 15, slab: SLAB_F1, decl: "@group(0) @binding(15) var<storage, read_write> lumq: array<f32>;" },
LiveBuf { name: "lumb", binding: 16, slab: SLAB_F1, decl: "@group(0) @binding(16) var<storage, read_write> lumb: array<f32>;" },
- LiveBuf { name: "ae_in", binding: 17, slab: SLAB_U4, decl: "@group(0) @binding(17) var<storage, read> ae_in: array<vec4<u32>>;" },
+ LiveBuf { name: "ae_in", binding: 17, slab: SLAB_U4, decl: "@group(0) @binding(17) var<storage, read_write> ae_in: array<vec4<u32>>;" },
LiveBuf { name: "ae_out", binding: 18, slab: SLAB_U4, decl: "@group(0) @binding(18) var<storage, read_write> ae_out: array<vec4<u32>>;" },
LiveBuf { name: "shist_in", binding: 19, slab: SLAB_V4, decl: "@group(0) @binding(19) var<storage, read> shist_in: array<vec4<f32>>;" },
LiveBuf { name: "shist_out", binding: 20, slab: SLAB_V4, decl: "@group(0) @binding(20) var<storage, read_write> shist_out: array<vec4<f32>>;" },