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.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
The live frame at render resolution, one temporal upscaler, the window at its physical pixels (ROADMAP § 1a)
yujiaxu-ops5c617c0b186f
diff --git a/docs/REPORT.md b/docs/REPORT.mdindex 6b9441a..aed9103 100644--- a/docs/REPORT.md+++ b/docs/REPORT.md@@ -143,9 +143,10 @@ traces 1+ spp with hardware ray queries (primary visibility, GI, soft shadows, rroulette), reprojects and accumulates history validated per pixel by depth/normal (TSR/Lumen-style temporal reuse),denoises with an edge-avoiding à-trous filter (two extra wide passes where history is short, the role Lumen's radiancecache plays; directly seen emission bypasses it), and applies UE's post chain (UE auto / local exposure, six-stage-bloom, cos⁴ vignette, filmic tonemap through a 32³ log LUT). Below 100 % render scale the temporal pass also upsamples-jittered samples to the output (TSR-style). Dynamic resolution (50–100 %) and then samples per frame adapt to the-GPU budget. Scene changes from the game keep history (geometry stays valid) and cap it briefly so+bloom, cos⁴ vignette, filmic tonemap through a 32³ log LUT). The frame is traced, denoised and composed at the render+resolution with one sub-pixel jitter a frame, and one temporal upscaler takes it to the window's physical pixels (the+structure of UE's TSR after Lumen; ROADMAP § 1a). Dynamic resolution (down to half of the window's points a side) and+then samples per frame adapt to the GPU budget. Scene changes from the game keep history (geometry stays valid) and cap it briefly solighting changes converge within a few frames.`node tools/bench-live.ts` (1280×720, M5): frame `k` frames after a full history reset, vs the UE5 references.diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex acc13c6..73fa0f2 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -1729,6 +1729,57 @@ pipeline. The real-time pipeline moves toward Lumen's structure — split signalchecked against UE.- Since then the merge gate reports what pathtrace.wgsl additions cost compiled in but off (docs/WORKFLOW.md).+### 1a. The frame at render resolution, one upscaler (2026-11-12)++Why: every host held the view at 1280 × 720 and stretched it over the window (a Retina window is 2560 × 1440), and+every quality bench compared on the 720p grid, so none saw it. At the window's pixels against our offline render of+that size, a still live frame kept 0.32–0.42 of the detail (the regression of its luma gradients on the offline+render's; an offline 720p frame stretched keeps 0.48–0.57). Making the view the window's size did not pay as the+pipeline stood: upscaling was done per signal on the output grid (nine 80-byte samples gathered a pixel, five+output-size histories, the specular filter's three passes), 2.75 ms of Bistro's frame at 720p and 9.9 ms at 1440p.++Now, the structure of UE's TSR after Lumen and of the vendor upscalers after a denoiser (live.wgsl):++- **One sub-pixel jitter a frame** for every camera ray (Halton 2, 3; 8 phases at full resolution, 8 / scale² below).+- **Everything noisy at the render resolution**: the specular history and its filter moved to the traced grid beside+ the diffuse irradiance (`stemporal`, `sfilt*`); `compose` makes one colour a traced sample, with its velocity, depth+ and whether its shading is animated.+- **One temporal upscaler** (`judge`, `reject`, `upscale`) with one colour history on the output grid:+ - holding still, the history is not clamped into the frame's range (a full-resolution TAA's rule cut the detail+ finer than a traced pixel every frame: Thames kept 0.53–0.67); each sample is held against the range of the+ history around its own point instead, a 3 × 3 mean of what lies outside is the correction, and a disocclusion is+ found by depth (this frame's nearest among nine against the previous frame's), confirmed by colour;+ - in motion, as the pixel's speed nears a pixel a frame, the history goes from 16 samples to 4 and is clamped into+ the nine samples' range (TSR's velocity weight clamping, TAA's clamp). An orbit of Thames at 9 pixels a frame+ against the offline render of the pose (detail / trail px): neither 0.48 / 49, both 0.52 / 31; main stretched+ 0.31 / 45;+ - animated shading holds 4 samples (a reactive mask, as FSR 2 and UE's responsive AA take one);+ - translucent particles are drawn after it (they have no motion vectors), then depth of field and motion blur.+- **The view is the window's physical pixels** (`play`; `live --display-scale k` headless): dynamic resolution starts+ from the points' resolution and goes down to half of it, its steps a point's (`LiveCore::set_display_scale`).++Measured on the M5 Pro (`out/liveq/gpu-runs-std.md`; single runs unless said):++| as the window runs it, 60 fps both | main: 1280 × 720 output, stretched | this: 2560 × 1440 output |+|---|---|---|+| Bistro | 1056 × 594 rays, GPU 15.1 ms, detail 0.32 | 960 × 540 rays, 14.4 ms, 0.55 |+| Thames (a voxel level) | 1280 × 720 rays, 15.3 ms, 0.39 | 1152 × 648 rays, 14.8 ms, 0.88 |+| Kabukichō | 832 × 468 rays, 15.8 ms, 0.42 | 736 × 414 rays, 14.4 ms, 0.72 |++- Bistro's output-grid passes at 1440p: 2.8 ms (the old path 9.9).+- At 1280 × 720 and the 0.5 floor (the perf smoke's drive, builds alternated on a quiet GPU): GPU p50 / p95 7.07 /+ 7.38 ms against main's 8.33–8.36 / 8.63–9.19.+- Layer B, Bistro against the unbiased offline render: ΔE 2.98 / SSIM 0.859 (main 3.62 / 0.812 when its limits were+ set), luminance 0.994.+- bench-ghosting: Bistro passes. Kabukichō fails two of its five limits as they stand, the p95 excess 0.5 s after the+ turn and the strafe return (5.1 for limits of 4.2 and 4.5): the excess is taken over the settled frame, whose own+ p95 fell from 7.9 to 4.2; the shots' raw p95 are 9.3 for main's 10.8 and 11.3. The limits are to be set again.++Not done here: the whole-frame history caps of the traced grid's accumulations (48 frames while the camera moves, 16+after a lighting change, 4 after a geometry edit, 24 with spark lights) and filters sized by hit distance; 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.+## 2. Temporal fixesTargets (2026-11-08): bench-temporal, bench-ghosting and their UE MRQ references move to Bistro night (§ 0 step 7); thediff --git a/playground/host/src/main.rs b/playground/host/src/main.rsindex 7ccc06a..0058b31 100644--- a/playground/host/src/main.rs+++ b/playground/host/src/main.rs@@ -20,7 +20,7 @@ mod ui;mod viewport;use threert_render::gpu;-use threert_render::live::{next_spp, Camera, LiveCore, MIN_SCALE};+use threert_render::live::{next_spp, Camera, LiveCore};use threert_render::script::GameSession;use anyhow::{Context, Result, anyhow};use glam::Vec3;@@ -1009,7 +1009,7 @@ impl State {self.spp = if core.render_scale < 1.0 { 1 } else { next_spp(self.spp, core.gpu_ms, FPS).min(self.spp_cap) };if self.auto_bounces && core.frame % 30 == 1 {let budget = 1000.0 / FPS as f32;- if self.spp == 1 && core.render_scale <= MIN_SCALE && core.gpu_ms > budget * 0.92 && core.bounces > 2 {+ if self.spp == 1 && core.render_scale <= core.min_scale && core.gpu_ms > budget * 0.92 && core.bounces > 2 {core.bounces -= 1;} else if core.gpu_ms < budget * 0.55 && core.bounces < self.max_bounces {core.bounces += 1;diff --git a/renderer/shaders/live.wgsl b/renderer/shaders/live.wgslindex 8e301b2..5a7baf9 100644--- a/renderer/shaders/live.wgsl+++ b/renderer/shaders/live.wgsl@@ -1,11 +1,15 @@-// Real-time viewport passes, run after the 1-spp LIVE path-trace dispatch (UE-editor-style real-time path):-// temporal - reprojects last frame's history with depth/normal validation and accumulates the new sample-// (static camera => progressive convergence to the path-traced reference, like Lumen/TSR history);-// below 100% render scale it also upsamples: samples traced at the internal resolution with their-// sub-pixel jitter are splatted onto the output grid (UE TSR's reconstruction)-// atrous* - edge-avoiding wavelet denoise of demodulated irradiance, strength ~ 1/sqrt(history length)-// bloom_* - UE Standard bloom: six Gaussian stages on a 1/4 .. 1/64 downsample chain, summed from the widest up-// post - exposure, bloom, UE cos^4 vignette, UE filmic tonemap via a log-encoded 3D LUT, BGRA8 output+// Real-time viewport passes, run after the 1-spp LIVE path-trace dispatch. The frame is traced, denoised and composed+// at the render resolution (the traced grid: `isz`), and one temporal upscaler takes it to the output grid (`size`), as+// UE's TSR does after Lumen and the vendor upscalers after a denoiser:+// refl - the specular reflection resolved from neighbours' rays+// dtemporal, atrous* - diffuse irradiance: accumulated over its reprojected history, edge-avoiding wavelet filter+// stemporal, sfilt* - specular: accumulated over its surface / reflection-hit history, filtered by roughness+// compose - albedo x irradiance + specular + what the camera ray saw + deterministic direct light+// judge, reject - what of the output-grid colour history this frame's samples contradict+// upscale - the composed frame, by its sub-pixel jitter, into the output grid's one colour history+// dof*, mblur - depth of field and motion blur on the output grid+// bloom_* - UE Standard bloom: six Gaussian stages on a 1/4 .. 1/64 downsample chain, summed from the widest up+// post - exposure, bloom, UE cos^4 vignette, UE filmic tonemap via a log-encoded 3D LUT, BGRA8 outputstruct Cam { pos: vec4<f32>, fwd: vec4<f32>, right: vec4<f32>, up: vec4<f32> }; // pos.w = tan(fov/2), fwd.w = aspectstruct LiveParams {@@ -26,6 +30,7 @@ struct LiveParams {bt: vec4<f32>, // bloom stage 0..3 tint (BloomN Tint * intensity / 6)bt2: vec4<f32>, // bloom stage 4, 5 tint, -, -dof: vec4<f32>, // depth of field: CoC radius of infinity (output px; 0 = off), focus distance (m), max radius (px), -+ jit: vec4<f32>, // this frame's sub-pixel jitter (every traced sample's: x, y in 0 .. 1 of a traced pixel), -, -};// color = diffuse lighting + first-hit distance, emit = directly seen emission / sky + roughness, spec = specular lighting// + reflection hit distance (see pathtrace.wgsl).@@ -33,11 +38,16 @@ struct Pixel { color: vec4<f32>, albedo: vec4<f32>, normal: vec4<f32>, emit: vec@group(0) @binding(0) var<uniform> L: LiveParams;@group(0) @binding(1) var<storage, read> sample_px: array<Pixel>; // color.w = first-hit distance (0 = sky)-// Diffuse irradiance history (rgb, frames) on the traced grid (`dtemporal`); `mom_in` / `mom_out` hold its moments.+// Diffuse irradiance history (rgb, frames) on the traced grid (`dtemporal`; `mom_in` / `mom_out` hold its moments),+// then the output grid's colour history (rgb, samples; `out_hist`, `upscale`).@group(0) @binding(2) var<storage, read> hist_in: array<vec4<f32>>;@group(0) @binding(3) var<storage, read_write> hist_out: array<vec4<f32>>;-@group(0) @binding(4) var<storage, read> gb_in: array<vec2<u32>>; // octahedral normal, view depth (0 = sky), roughness (`GB`)+// The traced grid's g-buffer (`stemporal`): octahedral normal, view depth (0 = sky), roughness (`GB`).+@group(0) @binding(4) var<storage, read> gb_in: array<vec2<u32>>;@group(0) @binding(5) var<storage, read_write> gb_out: array<vec2<u32>>;+// fa: the irradiance filter's ping-pong on the traced grid, then the image on the output grid (what the post chain+// reads). fb: its other half, then the composed colour of the traced grid (`compose`; w: first-hit distance), then+// depth of field's output when motion blur follows.@group(0) @binding(6) var<storage, read_write> fa: array<vec4<f32>>;@group(0) @binding(7) var<storage, read_write> fb: array<vec4<f32>>;@group(0) @binding(8) var<storage, read_write> bloom_a: array<vec4<f32>>; // bloom pyramid levels, then stage sums@@ -49,20 +59,21 @@ 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-// Accumulated base color (the denoiser's demodulation) + animated flag, and accumulated directly seen emission (kept-// out of the denoiser, added back after it), as f16: (albedo.rg, albedo.b | anim, emission.rg, emission.b | roughness).+// 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>>;@group(0) @binding(18) var<storage, read_write> ae_out: array<vec4<u32>>;-// Specular history (rgb + history length), diffuse luminance moments (mean, mean square, fast mean, -).+// 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>>;@group(0) @binding(20) var<storage, read_write> shist_out: array<vec4<f32>>;@group(0) @binding(21) var<storage, read> mom_in: array<vec4<f32>>;@group(0) @binding(22) var<storage, read_write> mom_out: array<vec4<f32>>;-// The specular denoiser's ping-pong (`temporal` -> `spec0` -> `spec1` -> `spec2`): (rgb, history length) as f16, two-// slots of one entry per output pixel. The three passes are bandwidth-bound: 8 bytes per pixel instead of 16.+// The specular denoiser's ping-pong on the traced grid (`stemporal` -> `sfilt0` -> `sfilt1` -> `sfilt2`): (rgb, history+// length) as f16, two slots (the passes are bandwidth-bound: 8 bytes per pixel instead of 16). The result is slot 1;+// slot 0 then holds `reject`'s verdict.@group(0) @binding(23) var<storage, read_write> spec_pp: array<vec2<u32>>;-// Scratch: the traced surfaces for the diffuse denoiser and upsample (`dtemporal`), then depth of field's output when-// motion blur follows.+// Scratch: the traced surfaces for the diffuse denoiser (`dtemporal`), then the upscaled image when depth of field /+// motion blur follow (they read it; w: the circle of confusion).@group(0) @binding(24) var<storage, read_write> sb: array<vec4<f32>>;// Reflection resolve: the trace kernel's specular-lobe rays, 2 per traced pixel ((radiance back, lobe pdf; 0 = none),// (specular albedo E, octahedral direction)), and the resolved specular reflection per traced pixel.@@ -70,8 +81,9 @@ struct Pixel { color: vec4<f32>, albedo: vec4<f32>, normal: vec4<f32>, emit: vec@group(0) @binding(26) var<storage, read_write> refl_res: array<vec4<f32>>;// Per traced pixel: where its surface point was the previous frame, as a world-space offset (w = 1: a moving object).@group(0) @binding(27) var<storage, read> motion_in: array<vec4<f32>>;-// Directional lights' direct light (the sun: nearly deterministic) at camera hits per traced pixel, and its-// history per output pixel (ping-pong): no denoiser, a short clamped history like emission (UE lights directly).+// Directional lights' direct light (the sun: nearly deterministic) at camera hits per traced pixel: no denoiser+// (`compose` adds it as it is, UE lights directly). `xhist_out`: per traced sample, its screen velocity (output pixels+// this frame), view depth and whether its shading is animated (`compose`). (`xhist_in`: its ping-pong partner, not read.)@group(0) @binding(28) var<storage, read> direct_in: array<vec4<f32>>;@group(0) @binding(29) var<storage, read> xhist_in: array<vec4<f32>>;@group(0) @binding(30) var<storage, read_write> xhist_out: array<vec4<f32>>;@@ -85,14 +97,6 @@ fn spec_store(slot: u32, i: u32, c: vec4<f32>) {spec_pp[slot * L.size.x * L.size.y + i] = vec2<u32>(pack2x16float(v.rg), pack2x16float(v.ba));}-fn hist_albedo(v: vec4<u32>) -> vec3<f32> { return vec3<f32>(unpack2x16float(v.x), unpack2x16float(v.y).x); }-fn hist_emission(v: vec4<u32>) -> vec3<f32> { return vec3<f32>(unpack2x16float(v.z), unpack2x16float(v.w).x); }-// Albedo, the frames of the output-grid histories, emission, roughness (f16).-fn pack_ae(alb: vec3<f32>, frames: f32, em: vec3<f32>, rough: f32) -> vec4<u32> {- let e = min(em, vec3<f32>(6.0e4));- return vec4<u32>(pack2x16float(alb.rg), pack2x16float(vec2<f32>(alb.b, frames)), pack2x16float(e.rg), pack2x16float(vec2<f32>(e.b, rough)));-}-fn ray_dir(c: Cam, x: f32, y: f32) -> vec3<f32> {let w = f32(L.size.x);let h = f32(L.size.y);@@ -221,7 +225,7 @@ fn refl(@builtin(global_invocation_id) gid: vec3<u32>) {let n = normalize(px.normal.xyz + vec3<f32>(1e-6));let rough = max(px.emit.w, 0.02);let a2 = rough * rough * rough * rough;- let sp = sample_pos(i32(gid.x), i32(gid.y), i);+ let sp = sample_pos(i32(gid.x), i32(gid.y));let d = ray_dir(L.cur, sp.x, sp.y);let x = L.cur.pos.xyz + d * t;let v = -d;@@ -253,7 +257,7 @@ fn refl(@builtin(global_invocation_id) gid: vec3<u32>) {let st = pj.spec.w;if (k > 0u && st > 0.0 && st < 1.0e3) {// Re-aim at j's hit from this surface point (contact reflections).- let pjp = sample_pos(q.x, q.y, j);+ let pjp = sample_pos(q.x, q.y);let xj = L.cur.pos.xyz + ray_dir(L.cur, pjp.x, pjp.y) * tj;l = normalize(xj + l * st - x);}@@ -287,229 +291,21 @@ fn sample_color(q: u32) -> vec3<f32> {return s[0] + s[1] + s[2];}-// Traced sample under an output pixel (nearest; raw-sample view mode).+// The traced sample under an output pixel (nearest by the pixels' centres).fn sample_under(x: u32, y: u32) -> u32 {let sx = min(u32((f32(x) + 0.5) * f32(L.isz.x) / f32(L.size.x)), L.isz.x - 1u);let sy = min(u32((f32(y) + 0.5) * f32(L.isz.y) / f32(L.size.y)), L.isz.y - 1u);return sy * L.isz.x + sx;}-// Jittered position of traced sample (qx, qy), in output pixels.-fn sample_pos(qx: i32, qy: i32, q: u32) -> vec2<f32> {- let j = sample_px[q].normal.w;- let jx = floor(j / 4096.0);- let jit = vec2<f32>(jx, j - jx * 4096.0) / 4095.0;- return (vec2<f32>(f32(qx), f32(qy)) + jit) * vec2<f32>(f32(L.size.x) / f32(L.isz.x), f32(L.size.y) / f32(L.isz.y));+// Position of traced sample (qx, qy), in output pixels: every sample of a frame has the frame's jitter.+fn sample_pos(qx: i32, qy: i32) -> vec2<f32> {+ return (vec2<f32>(f32(qx), f32(qy)) + L.jit.xy) * vec2<f32>(f32(L.size.x) / f32(L.isz.x), f32(L.size.y) / f32(L.isz.y));}-// This frame's samples for one output pixel. t / n / anim / rough / st: the nearest sample's surface. tmin / tmax /-// sky: hit distances present among the gathered samples (sky = 1 if any missed): an edge pixel's history may come-// from either side while the jitter flips its surface. smean: mean of the neighbourhood's specular in YCoCg (the-// temporal pass keeps the specular history, surface or reflection hit, that is closer to it).-struct Gathered {- c: vec3<f32>, s: vec3<f32>, e: vec3<f32>, alb: vec3<f32>, t: f32, n: vec3<f32>, anim: f32, rough: f32, st: f32, bq: u32,- conf: f32, tmin: f32, tmax: f32, sky: f32, smean: vec3<f32>,- // Range of the 3x3 samples' emission and albedo (noise-free: their history is clamped into it while moving).- emin: vec3<f32>, emax: vec3<f32>, amin: vec3<f32>, amax: vec3<f32>,- // The nearest sample's motion offset (w = 1: on a moving object).- mv: vec4<f32>,- // Deterministic direct light (splatted like the emission) and the 3x3 samples' range of it.- x: vec3<f32>, xmin: vec3<f32>, xmax: vec3<f32>,-};--fn gather(gid: vec3<u32>) -> Gathered {- var g: Gathered;- let full = L.isz.x == L.size.x && L.isz.y == L.size.y;- let pc = vec2<f32>(f32(gid.x) + 0.5, f32(gid.y) + 0.5);- let ic = vec2<i32>(floor(pc * vec2<f32>(f32(L.isz.x) / f32(L.size.x), f32(L.isz.y) / f32(L.size.y))));- // nearest traced sample- var bq = u32(ic.y) * L.isz.x + u32(ic.x);- var bx = ic;- if (!full) {- var best = 1e9;- for (var k = 0; k < 9; k++) {- let qx = ic.x + k % 3 - 1;- let qy = ic.y + k / 3 - 1;- if (qx < 0 || qy < 0 || qx >= i32(L.isz.x) || qy >= i32(L.isz.y)) { continue; }- let q = u32(qy) * L.isz.x + u32(qx);- let d = sample_pos(qx, qy, q) - pc;- if (dot(d, d) < best) { best = dot(d, d); bq = q; bx = vec2<i32>(qx, qy); }- }- }- let sb = sample_px[bq];- g.bq = bq;- let nb = normalize(sb.normal.xyz + vec3<f32>(1e-6));- g.t = sb.color.w;- g.n = sb.normal.xyz;- g.anim = sb.albedo.w;- g.rough = sb.emit.w;- // Reflection hit distance for the mirror-image reprojection: on near-mirror surfaces (wet street, puddles) the- // resolved one (pass `refl`), else this sample's own. Rougher ones (signs, 0.22) resolve over a wider disk whose- // mean distance mixes what different neighbours reflect (bench-ghosting strafe-mid sign band +0.1 dE).- g.st = select(sb.spec.w, refl_res[bq].w, refl_res[bq].w > 0.0 && sb.emit.w < NEAR_MIRROR);- g.mv = motion_in[bq];- g.tmin = 1e30;- g.emin = vec3<f32>(1e30);- g.xmin = vec3<f32>(1e30);- g.amin = vec3<f32>(1e30);- // Specular neighbourhood mean around the nearest sample (same surface only), in YCoCg.- var m1 = vec3<f32>(0.0);- var mn = 0.0;- var wsum = 0.0;- for (var k = 0; k < 9; k++) {- let qx = bx.x + k % 3 - 1;- let qy = bx.y + k / 3 - 1;- if (qx < 0 || qy < 0 || qx >= i32(L.isz.x) || qy >= i32(L.isz.y)) { continue; }- let q = u32(qy) * L.isz.x + u32(qx);- let s = sample_px[q];- if (s.color.w > 0.0) {- g.tmin = min(g.tmin, s.color.w);- g.tmax = max(g.tmax, s.color.w);- } else {- g.sky = 1.0;- }- // Emission / albedo range over all nine (edges: the history holds a blend of both sides).- let p = sample_parts(q);- g.emin = min(g.emin, p[2]);- let xd = direct_in[q].rgb;- g.xmin = min(g.xmin, xd);- g.xmax = max(g.xmax, xd);- g.emax = max(g.emax, p[2]);- g.amin = min(g.amin, s.albedo.rgb);- g.amax = max(g.amax, s.albedo.rgb);- var ok = false;- if (g.t > 0.0) {- ok = s.color.w > 0.0 && abs(s.color.w - g.t) < 0.05 * g.t + 0.01 && dot(normalize(s.normal.xyz + vec3<f32>(1e-6)), nb) > 0.8;- } else {- ok = s.color.w == 0.0;- }- if (!ok) { continue; }- m1 += ycocg(p[1]);- mn += 1.0;- if (full && q != bq) { continue; }- // At 100% render scale only the pixel's own sample is accumulated; below, the 3x3 samples are splatted with- // a Gaussian on the jittered distance (sigma ~ 0.48 output px).- var wt = select(0.0, 1.0, q == bq);- if (!full) {- let d = sample_pos(qx, qy, q) - pc;- wt = max(exp(-2.2 * dot(d, d)), select(0.0, 1e-4, q == bq));- }- if (wt <= 0.0) { continue; }- g.c += p[0] * wt;- g.s += p[1] * wt;- g.e += p[2] * wt;- g.x += direct_in[q].rgb * wt;- g.alb += s.albedo.rgb * wt;- wsum += wt;- }- if (wsum > 0.0) {- g.c /= wsum;- g.s /= wsum;- g.e /= wsum;- g.x /= wsum;- g.alb /= wsum;- }- g.conf = min(wsum, 1.0);- g.smean = m1 / max(mn, 1.0);- return g;-}--// The output-grid diffuse history follows the traced grid's in hist_in / hist_out (sized for both).+// The output grid's colour history follows the traced grid's diffuse history in hist_in / hist_out (sized for both).fn out_hist(i: u32) -> u32 { return L.size.x * L.size.y + i; }-// Bilinear history fetch at previous-frame pixel position (fx, fy) for a surface at previous-view depth z with normal-// n; `virt` = specular history through a reflection (only the reflecting surface's normal / roughness must match).-// d: the output-grid diffuse irradiance history (rgb, frames); f: frames of the other output-grid histories.-struct Hist { d: vec4<f32>, s: vec4<f32>, f: f32, alb: vec3<f32>, em: vec3<f32>, w: f32 };--fn fetch_history(fx: f32, fy: f32, z: f32, zr: f32, n: vec3<f32>, t: f32, s: Gathered, virt: bool) -> Hist {- var h: Hist;- let w = L.size.x;- let hh = L.size.y;- let upsampled = L.isz.x != L.size.x || L.isz.y != L.size.y;- let x0 = i32(floor(fx));- let y0 = i32(floor(fy));- let ax = fx - f32(x0);- let ay = fy - f32(y0);- var wsum = 0.0;- for (var k = 0; k < 4; k++) {- let qx = x0 + (k & 1);- let qy = y0 + (k >> 1u);- if (qx < 0 || qy < 0 || qx >= i32(w) || qy >= i32(hh)) { continue; }- let q = u32(qy) * w + u32(qx);- let g = gb_unpack(gb_in[q]);- var ok = false;- if (virt) {- ok = g.depth > 0.0 && dot(g.n, n) > 0.95 && abs(g.rough - s.rough) < 0.1;- } else if (t > 0.0) {- ok = g.depth > 0.0 && abs(g.depth - z) < 0.04 * z + 0.002 && dot(g.n, n) > 0.8;- } else {- ok = g.depth == 0.0;- }- // Upsampling: also accept history of any surface among this frame's neighbourhood samples (depths scaled to- // the previous view like this pixel's own), or sky if some sample missed.- if (!ok && upsampled && !virt) {- let in_range = (g.depth > zr * s.tmin * 0.97 - 0.002) && (g.depth < zr * s.tmax * 1.03 + 0.002);- ok = select(s.sky > 0.5, in_range, g.depth > 0.0);- }- if (!ok) { continue; }- let bw = select(1.0 - ax, ax, (k & 1) == 1) * select(1.0 - ay, ay, (k >> 1u) == 1);- h.s += shist_in[q] * bw;- if (!virt) {- h.d += hist_in[out_hist(q)] * bw;- let ae = ae_in[q];- h.f += unpack2x16float(ae.y).y * bw;- h.alb += hist_albedo(ae) * bw;- h.em += hist_emission(ae) * bw;- }- wsum += bw;- }- if (wsum > 1e-3) {- h.d /= wsum;- h.s /= wsum;- h.f /= wsum;- h.alb /= wsum;- h.em /= wsum;- h.w = wsum;- } else {- h = Hist(vec4<f32>(0.0), vec4<f32>(0.0), 0.0, vec3<f32>(0.0), vec3<f32>(0.0), 0.0);- }- if (non_finite(h.d.rgb) || non_finite(h.s.rgb) || non_finite(h.alb) || non_finite(h.em) || non_finite(vec3<f32>(h.d.w, h.s.w, h.f))) {- h = Hist(vec4<f32>(0.0), vec4<f32>(0.0), 0.0, vec3<f32>(0.0), vec3<f32>(0.0), 0.0);- }- return h;-}--// Albedo and emission history at previous-frame position (fx, fy), Catmull-Rom over 4x4 pixels (bilinear resampling-// every frame blurs texture and sign detail while the view moves). No surface test: these signals are noise-free, so-// the caller clamps them into this frame's neighbourhood range instead (TAA), which also removes the filter's ringing.-struct TexHist { alb: vec3<f32>, em: vec3<f32>, x: vec3<f32> };--fn fetch_tex_history(fx: f32, fy: f32) -> TexHist {- let x1 = floor(fx);- let y1 = floor(fy);- let t = vec2<f32>(fx - x1, fy - y1);- let w0 = t * (-0.5 + t * (1.0 - 0.5 * t));- let w1 = 1.0 + t * t * (-2.5 + 1.5 * t);- let w2 = t * (0.5 + t * (2.0 - 1.5 * t));- let w3 = t * t * (-0.5 + 0.5 * t);- let wx = array<f32, 4>(w0.x, w1.x, w2.x, w3.x);- let wy = array<f32, 4>(w0.y, w1.y, w2.y, w3.y);- var h = TexHist(vec3<f32>(0.0), vec3<f32>(0.0), vec3<f32>(0.0));- for (var j = 0; j < 4; j++) {- let qy = u32(clamp(i32(y1) + j - 1, 0, i32(L.size.y) - 1));- for (var i = 0; i < 4; i++) {- let qx = u32(clamp(i32(x1) + i - 1, 0, i32(L.size.x) - 1));- let ae = ae_in[qy * L.size.x + qx];- let wt = wx[i] * wy[j];- h.alb += hist_albedo(ae) * wt;- h.em += hist_emission(ae) * wt;- h.x += xhist_in[qy * L.size.x + qx].rgb * wt;- }- }- return h;-}-// Previous-frame pixel position of world point p (x < 0: behind the previous camera).fn prev_pixel(p: vec3<f32>) -> vec3<f32> {let d = p - L.prev.pos.xyz;@@ -521,16 +317,8 @@ fn prev_pixel(p: vec3<f32>) -> vec3<f32> {}// ------------------------------------------------------------------------------------------------ diffuse, traced grid-// UE denoises at the internal resolution and upsamples after: the diffuse irradiance is accumulated (`dtemporal`) and-// filtered (`atrous*`) on the traced grid, a quarter of the pixels at the 0.5 floor, then upsampled per output pixel-// (`upsample_irradiance`) and remodulated by its sharp albedo. Irradiance is smooth, so little is lost.--// The previous frame's output pixel covering its traced pixel (tx, ty) (its g-buffer checks history on the traced grid).-fn prev_out_index(tx: i32, ty: i32) -> u32 {- let o = (vec2<f32>(f32(tx), f32(ty)) + 0.5) * vec2<f32>(f32(L.size.x), f32(L.size.y)) / vec2<f32>(L.isz.zw);- let oi = clamp(vec2<i32>(o), vec2<i32>(0), vec2<i32>(i32(L.size.x) - 1, i32(L.size.y) - 1));- return u32(oi.y) * L.size.x + u32(oi.x);-}+// The diffuse irradiance (diffuse lighting / albedo) is accumulated (`dtemporal`) and filtered (`atrous*`) on the traced+// grid; `compose` remodulates it by each sample's own albedo.// The previous frame's surface at its traced pixel (tx, ty) is this one (previous-view depth z, normal n; t = 0: sky).fn same_surface(tx: i32, ty: i32, z: f32, n: vec3<f32>, t: f32) -> bool {@@ -539,7 +327,7 @@ fn same_surface(tx: i32, ty: i32, z: f32, n: vec3<f32>, t: f32) -> bool {// same_surface with the normals' least cosine `ncos`.fn same_surface_n(tx: i32, ty: i32, z: f32, n: vec3<f32>, t: f32, ncos: f32) -> bool {- let g = gb_unpack(gb_in[prev_out_index(tx, ty)]);+ let g = gb_unpack(gb_in[u32(ty) * L.isz.z + u32(tx)]);if (t > 0.0) { return g.depth > 0.0 && abs(g.depth - z) < 0.04 * z + 0.002 && dot(g.n, n) > ncos; }return g.depth == 0.0;}@@ -565,7 +353,7 @@ fn dtemporal(@builtin(global_invocation_id) gid: vec3<u32>) {// A compact copy of the sample's surface for the denoiser's and the upsample's edge stops (`sb` is free until// `spec0`; the 80-byte samples cost twice the bandwidth): normal, first-hit distance, negative on animated emitters.sb[q] = vec4<f32>(n, select(t, -t, anim && t > 0.0));- let sp = sample_pos(i32(gid.x), i32(gid.y), q);+ let sp = sample_pos(i32(gid.x), i32(gid.y));let dir = ray_dir(L.cur, sp.x, sp.y);let p = select(L.cur.pos.xyz + dir * 1.0e6, L.cur.pos.xyz + dir * t, t > 0.0);var irr = sample_parts(q)[0] / max(px.albedo.rgb, vec3<f32>(0.02));@@ -676,192 +464,6 @@ fn traced_surface(q: u32) -> vec4<f32> {return vec4<f32>(v.xyz, abs(v.w));}-// The denoised irradiance of the traced grid (fa after the last à-trous stage) at output pixel gid: bilinear over the-// four nearest traced pixels, each only if it shows this pixel's surface (depth, normal; joint bilateral), else the-// nearest sample's. lo / hi: the range of all four (either side of an edge), for the output-grid history's clamp.-struct Upsampled { irr: vec3<f32>, lo: vec3<f32>, hi: vec3<f32> };--fn upsample_irradiance(gid: vec3<u32>, s: Gathered, n: vec3<f32>) -> Upsampled {- let tc = (vec2<f32>(f32(gid.x), f32(gid.y)) + 0.5) * vec2<f32>(L.isz.xy) / vec2<f32>(f32(L.size.x), f32(L.size.y)) - 0.5;- let x0 = i32(floor(tc.x));- let y0 = i32(floor(tc.y));- let f = tc - floor(tc);- var sum = vec3<f32>(0.0);- var wsum = 0.0;- var u: Upsampled;- u.lo = fa[s.bq].rgb;- u.hi = u.lo;- for (var k = 0; k < 4; k++) {- let tx = x0 + (k & 1);- let ty = y0 + (k >> 1u);- if (tx < 0 || ty < 0 || tx >= i32(L.isz.x) || ty >= i32(L.isz.y)) { continue; }- let q = u32(ty) * L.isz.x + u32(tx);- let fq = fa[q].rgb;- u.lo = min(u.lo, fq);- u.hi = max(u.hi, fq);- let pq = traced_surface(q);- var ok = false;- if (s.t > 0.0) {- ok = pq.w > 0.0 && abs(pq.w - s.t) < 0.05 * s.t + 0.01 && dot(pq.xyz, n) > 0.8;- } else {- ok = pq.w == 0.0;- }- if (!ok) { continue; }- let bw = max(select(1.0 - f.x, f.x, (k & 1) == 1) * select(1.0 - f.y, f.y, (k >> 1u) == 1), 1e-4);- sum += fq * bw;- wsum += bw;- }- u.irr = select(fa[s.bq].rgb, sum / wsum, wsum > 0.0);- if (non_finite(u.irr) || non_finite(u.lo) || non_finite(u.hi)) { u = Upsampled(vec3<f32>(0.0), vec3<f32>(0.0), vec3<f32>(0.0)); }- return u;-}--@compute @workgroup_size(8, 8, 1)-fn temporal(@builtin(global_invocation_id) gid: vec3<u32>) {- let w = L.size.x;- let h = L.size.y;- if (gid.x >= w || gid.y >= h) { return; }- let i = gid.y * w + gid.x;- let s = gather(gid);- let t = s.t;- let n = normalize(s.n + vec3<f32>(1e-6));- let dir = ray_dir(L.cur, f32(gid.x) + 0.5, f32(gid.y) + 0.5);- var p = L.cur.pos.xyz + dir * 1.0e6;- var depth = 0.0;- if (t > 0.0) {- p = L.cur.pos.xyz + dir * t;- depth = dot(p - L.cur.pos.xyz, L.cur.fwd.xyz);- }-- var hist = Hist(vec4<f32>(0.0), vec4<f32>(0.0), 0.0, vec3<f32>(0.0), vec3<f32>(0.0), 0.0);- var virt = vec4<f32>(0.0);- var texp = vec2<f32>(-1.0);- if (L.size.w == 0u) {- // Moving objects reproject through where their surface point was (motion vectors), the rest with the camera.- let pp = prev_pixel(p + select(vec3<f32>(0.0), s.mv.xyz, s.mv.w > 0.5 && t > 0.0));- if (pp.z > 0.0) {- texp = pp.xy;- let zr = select(dot(dir, L.prev.fwd.xyz), pp.z / t, t > 0.0);- hist = fetch_history(pp.x, pp.y, pp.z, zr, n, t, s, false);- }- // Reflections: also reproject through the mirror image of what they show (planar-mirror virtual point, UE's- // history "from hit"). Not on moving objects: the camera-only reprojection ignores a moving reflector's own- // motion, so on the crowd's vans it landed on other surfaces (a level street's normal passes the test) and,- // picked as the history closer to a mostly dark neighbourhood mean, replaced the roofs' reflections with darker- // ones. The Bistro crowd in motion (lockstep, 10 s): specular 0.55 -> 0.83 of our offline render, the vans'- // roofs 0.46 -> 0.85. Limited to near mirrors as well (roughness < 0.2), Bistro's glossy surfaces gained a- // little more, but Kabukichō's wet street lost the history that carries it through strafes (bench-ghosting- // --scene kabukicho strafe-mid 3.3 -> 4.3): every static surface keeps it.- if (t > 0.0 && s.st > 0.0 && s.mv.w <= 0.5) {- let pv = prev_pixel(L.cur.pos.xyz + dir * (t + min(s.st, 1.0e3)));- if (pv.z > 0.0) {- virt = fetch_history(pv.x, pv.y, pv.z, 1.0, n, t, s, true).s;- }- }- }-- // History length of this pixel's output-grid histories (specular, albedo, emission, direct): global cap (camera- // motion / resets), animated shading keeps ~4 frames.- let cap = select(L.misc.y, min(L.misc.y, 4.0), s.anim > 1.5);- let count = min(hist.f + s.conf, cap);- let a = clamp(s.conf / max(count, 1e-4), 0.0, 1.0);-- // Specular (UE 5.8 Lumen reflections, LumenReflectionDenoiserTemporal.usf): the surface history and the reflection-hit- // one (reprojected through the mirror image of what it shows); the one closer to this frame's neighbourhood mean- // wins. While the view moves the frame count is capped: 12 frames (mirror-like surfaces at full resolution ~2), and- // 4 for 8 frames after the view last translated (the stop included): neither reprojection is exact for glossy- // reflections moving against their surface, and history that lands wrong smears them (bench-ghosting: strafes,- // sudden stops). A pure rotation reprojects exactly and keeps the longer history; holding still the cap lifts- // (static reflections keep converging, as under UE's TSR).- // Not clamped to the neighbourhood (UE clamps to 4 sigma of its spatially resolved reflections; ours, 4 to 8 sigma- // of nine resolved 1-spp samples until 2026-11-09): around sparse bright reflections and highlights the nine samples- // are mostly all dark, so the box clipped converged history every frame the view moved and the light was lost, not- // delayed. The Bistro crowd in motion (lockstep, 10 s): specular 0.26 -> 0.55 of our offline render. bench-ghosting- // excess turn-mid 3.43 -> 0.76, strafe-mid 5.14 -> 2.19, stop+0.1s 4.41 -> 1.86: each shot is compared with its- // converged frame, and the clipped reflections were most of the error.- let full = L.isz.x == L.size.x && L.isz.y == L.size.y;- let moving = any(L.cur.pos.xyz != L.prev.pos.xyz) || any(L.cur.fwd.xyz != L.prev.fwd.xyz);- let translated = L.mode.y < 8u && !xflag(16384u);- let ys = ycocg(hist.s.rgb);- let yv = ycocg(virt.rgb);- var surf = select(0.0, 0.8, length(s.smean - yv) > length(s.smean - ys) + 0.01 / exposure_scale());- surf = select(surf, 1.0, virt.w <= 0.0);- surf = select(surf, 0.0, hist.s.w <= 0.0 && virt.w > 0.0);- let hy = mix(yv, ys, surf);- var max_s = select(cap, select(12.0, mix(2.0, 12.0, clamp(s.rough / 0.05, 0.0, 1.0)), full), moving);- if (translated) { max_s = min(max_s, 4.0); }- let frames_s = select(virt.w, hist.s.w, surf > 0.5);- var count_s = 0.0;- var s_out = s.s;- if (hist.s.w > 0.0 || virt.w > 0.0) {- count_s = min(min(frames_s + s.conf, max_s), cap);- s_out = mix(max(ycocg_rgb(hy), vec3<f32>(0.0)), s.s, clamp(s.conf / max(count_s, 1e-4), 0.0, 1.0));- } else {- count_s = s.conf;- }-- // The diffuse history holds irradiance (lighting / albedo; smooth, so resampling it every frame costs no detail),- // remodulated by albedo. Albedo and emission are noise-free: their history is resampled sharply (Catmull-Rom),- // clamped into this frame's 3x3 range (TAA) and held to 4 frames while the view moves, 16 when still, so texture- // and sign detail don't blur through resampling (bench-temporal move59 vs converged 5.87 -> 5.07; bilinear at 4- // frames 5.12, at 2 frames 4.94 but shimmering).- var h_alb = hist.alb;- var h_em = hist.em;- var h_x = s.x;- if (texp.x >= 0.0 && hist.w > 0.0) {- let th = fetch_tex_history(texp.x, texp.y);- h_alb = th.alb;- h_em = th.em;- h_x = th.x;- }- // Deterministic direct light, no denoiser. While anything changes (the view, this object, lighting) a short clamped- // history like the emission's, as short on moving objects (a turning surface turns from the sun): crisp moving- // shadows. Otherwise it accumulates like the diffuse history, so soft-shadow penumbrae converge.- let changing = moving || s.mv.w > 0.5 || L.mode.z != 0u;- var x_out = mix(h_x, s.x, a);- if (changing) { x_out = mix(clamp(h_x, s.xmin, s.xmax), s.x, max(a, 0.25)); }- // w: screen-space velocity (output pixels this frame; camera and object motion) for motion blur, f16 x 2.- var vel = vec2<f32>(0.0);- if (L.size.w == 0u) {- let pv = prev_pixel(p + select(vec3<f32>(0.0), s.mv.xyz, s.mv.w > 0.5 && t > 0.0));- if (pv.z > 0.0) { vel = clamp(vec2<f32>(f32(gid.x), f32(gid.y)) - pv.xy, vec2<f32>(-2048.0), vec2<f32>(2048.0)); }- }- let x_fin = select(x_out, vec3<f32>(0.0), non_finite(x_out));- xhist_out[i] = vec4<f32>(x_fin, bitcast<f32>(pack2x16float(vel)));- h_alb = clamp(h_alb, s.amin, s.amax);- // The emission is clamped while something changes (the view, this object, lighting, animated shading), as the- // specular is. Holding still, small emitters (lamp globes, string-light bulbs, sign strokes) that the jitter finds- // in only some frames' 3x3 boxes below full resolution lost their light to the clamp: Bistro at the 0.5 floor had- // 69 % of the offline radiance in the 1-53 nit blocks (89 % now), 94.5 % overall (98 %). Background pixels too: the- // sky through media, 0 or 1 per sample, read high with the clamp (volume/furnace-live 0.9965 with it, 0.993- // without, since the media lookups stopped losing light at the boundary).- if (changing || L.mode.y < 8u || s.anim > 1.5) { h_em = clamp(h_em, s.emin, s.emax); }- let tex_a = max(a, select(1.0 / 16.0, 0.25, moving));- let alb_out = mix(h_alb, s.alb, tex_a);- let em = mix(h_em, s.e, tex_a);- shist_out[i] = vec4<f32>(s_out, count_s);-- // Diffuse: the traced grid's denoised irradiance (`dtemporal`, `atrous*`), upsampled. Each frame that is the- // irradiance of the surface this pixel's nearest jittered sample hit; at an edge that flips between both sides while- // the albedo history holds their blend, so it is accumulated here too, like the albedo (TAA: clamped into the range- // of the traced values around, 16 frames still, 4 moving), else edges dither (Kabukicho at the 0.5 floor, @16 / @256- // SSIM 0.711 / 0.721 -> 0.721 / 0.726).- let up = upsample_irradiance(gid, s, n);- let tc = hist_out[s.bq].w;- // Its frames follow the traced history's (restarted there by anti-lag and disocclusion).- let count_d = min(min(hist.d.w + s.conf, select(16.0, 4.0, moving || s.mv.w > 0.5)), max(tc, 1.0));- let irr = mix(clamp(hist.d.rgb, up.lo, up.hi), up.irr, clamp(s.conf / max(count_d, 1e-4), 0.0, 1.0));- hist_out[out_hist(i)] = vec4<f32>(select(irr, vec3<f32>(0.0), non_finite(irr)), count_d);- // Times this pixel's albedo, plus the directly seen emission and the sun's direct light as the final image shows- // them (the emission as its f16 history holds it), and the view depth: `spec2` only adds the specular, reading one- // vector instead of four (the composition was most of that pass's cost).- let ae = pack_ae(alb_out, count, em, s.rough);- fb[i] = vec4<f32>(max(irr * max(alb_out, vec3<f32>(0.02)), vec3<f32>(0.0)) + hist_emission(ae) + x_fin, depth);- spec_store(0u, i, vec4<f32>(max(s_out, vec3<f32>(0.0)), count_s)); // specular denoiser input- ae_out[i] = ae;- gb_out[i] = gb_pack(n, depth, s.rough);-}-// ------------------------------------------------------------------------------------------------ denoise// Diffuse: edge-avoiding a-trous on demodulated irradiance, luminance edge-stop scaled by the pixel's temporal@@ -963,28 +565,147 @@ fn atrous_gated(gid: vec3<u32>, stage: u32, step: i32, max_noise: f32) {@compute @workgroup_size(8, 8, 1) fn atrous3(@builtin(global_invocation_id) gid: vec3<u32>) { atrous_gated(gid, 3u, 8, 0.05); }@compute @workgroup_size(8, 8, 1) fn atrous4(@builtin(global_invocation_id) gid: vec3<u32>) { atrous_gated(gid, 4u, 16, 0.05); }-// Specular: not demodulated (reflections carry their own image); a small kernel scaled by roughness, so mirror-like-// surfaces keep sharp reflections and glossy ones are smoothed. Three passes over `spec_pp`: slot 0 -> 1 -> 0 -> 1.-fn spec_filter(gid: vec3<u32>, step: i32, from_a: bool) {- let w = L.size.x;- let h = L.size.y;+// ------------------------------------------------------------------------------------------------ render resolution+// Everything noisy is accumulated and filtered on the traced grid (the render resolution), composed there into one+// colour, and one temporal upscaler takes that to the output grid (`upscale`): the structure of UE's TSR after Lumen+// and of the vendor upscalers after a denoiser. `gb_out` is the traced grid's g-buffer.++// Specular history at previous-frame position pp (prev_pixel: output pixels, view depth) on the previous traced grid:+// bilinear over the taps that show this surface; `virt`: through a reflection (the reflecting surface's normal and+// roughness must match, not its depth).+fn spec_history(pp: vec3<f32>, n: vec3<f32>, t: f32, rough: f32, virt: bool) -> vec4<f32> {+ let ps = vec2<i32>(L.isz.zw);+ let tc = (pp.xy + 0.5) * vec2<f32>(ps) / vec2<f32>(f32(L.size.x), f32(L.size.y)) - 0.5;+ let x0 = i32(floor(tc.x));+ let y0 = i32(floor(tc.y));+ let f = tc - floor(tc);+ var h = vec4<f32>(0.0);+ var wsum = 0.0;+ for (var k = 0; k < 4; k++) {+ let tx = x0 + (k & 1);+ let ty = y0 + (k >> 1u);+ if (tx < 0 || ty < 0 || tx >= ps.x || ty >= ps.y) { continue; }+ let j = u32(ty * ps.x + tx);+ let g = gb_unpack(gb_in[j]);+ var ok = false;+ if (virt) {+ ok = g.depth > 0.0 && dot(g.n, n) > 0.95 && abs(g.rough - rough) < 0.1;+ } else if (t > 0.0) {+ ok = g.depth > 0.0 && abs(g.depth - pp.z) < 0.04 * pp.z + 0.002 && dot(g.n, n) > 0.8;+ } else {+ ok = g.depth == 0.0;+ }+ if (!ok) { continue; }+ let bw = select(1.0 - f.x, f.x, (k & 1) == 1) * select(1.0 - f.y, f.y, (k >> 1u) == 1);+ h += shist_in[j] * bw;+ wsum += bw;+ }+ if (wsum <= 1e-3) { return vec4<f32>(0.0); }+ h /= wsum;+ if (non_finite(h.rgb) || non_finite(vec3<f32>(h.w))) { return vec4<f32>(0.0); }+ return h;+}++// Specular temporal accumulation on the traced grid (UE 5.8 Lumen reflections, LumenReflectionDenoiserTemporal.usf): the+// surface history and the reflection-hit one (reprojected through the mirror image of what it shows); the one closer to+// this frame's neighbourhood mean wins. Writes the specular filter's input and the traced grid's g-buffer.+@compute @workgroup_size(8, 8, 1)+fn stemporal(@builtin(global_invocation_id) gid: vec3<u32>) {+ let iw = L.isz.x;+ let ih = L.isz.y;+ if (gid.x >= iw || gid.y >= ih) { return; }+ let q = gid.y * iw + gid.x;+ let px = sample_px[q];+ let t = px.color.w;+ let n = normalize(px.normal.xyz + vec3<f32>(1e-6));+ let rough = px.emit.w;+ // The history is the traced pixel's, whatever sub-pixel point each frame's jitter sampled it at: it is fetched+ // where the pixel's centre was (the sample's surface, through the centre). Fetched where the jittered sample+ // itself was, it was resampled at a new sub-pixel offset every frame, each time a little blurrier: the wet+ // street's reflections converged to blobs (Kabukichō, bench-ghosting's converged frames).+ let sp = (vec2<f32>(f32(gid.x), f32(gid.y)) + 0.5) * vec2<f32>(f32(L.size.x) / f32(iw), f32(L.size.y) / f32(ih));+ let dir = ray_dir(L.cur, sp.x, sp.y);+ var p = L.cur.pos.xyz + dir * 1.0e6;+ var depth = 0.0;+ if (t > 0.0) {+ p = L.cur.pos.xyz + dir * t;+ depth = dot(p - L.cur.pos.xyz, L.cur.fwd.xyz);+ }+ let cur = sample_parts(q)[1];+ // This frame's neighbourhood mean (same surface), in YCoCg.+ var m1 = vec3<f32>(0.0);+ var mn = 0.0;+ for (var k = 0; k < 9; k++) {+ let qx = i32(gid.x) + k % 3 - 1;+ let qy = i32(gid.y) + k / 3 - 1;+ if (qx < 0 || qy < 0 || qx >= i32(iw) || qy >= i32(ih)) { continue; }+ let j = u32(qy) * iw + u32(qx);+ let s = sample_px[j];+ var ok = false;+ if (t > 0.0) {+ ok = s.color.w > 0.0 && abs(s.color.w - t) < 0.05 * t + 0.01 && dot(normalize(s.normal.xyz + vec3<f32>(1e-6)), n) > 0.8;+ } else {+ ok = s.color.w == 0.0;+ }+ if (!ok) { continue; }+ m1 += ycocg(sample_parts(j)[1]);+ mn += 1.0;+ }+ let smean = m1 / max(mn, 1.0);+ // Reflection hit distance for the mirror-image reprojection: on near mirrors the resolved one (pass `refl`).+ let st = select(px.spec.w, refl_res[q].w, refl_res[q].w > 0.0 && rough < NEAR_MIRROR);+ let mv = motion_in[q];+ var hs = vec4<f32>(0.0);+ var hv = vec4<f32>(0.0);+ if (L.size.w == 0u && L.isz.z > 0u) {+ let pp = prev_pixel(p + select(vec3<f32>(0.0), mv.xyz, mv.w > 0.5 && t > 0.0));+ if (pp.z > 0.0) { hs = spec_history(pp, n, t, rough, false); }+ // (Not on moving objects: the camera-only reprojection ignores a moving reflector's own motion.)+ if (t > 0.0 && st > 0.0 && mv.w <= 0.5) {+ let pv = prev_pixel(L.cur.pos.xyz + dir * (t + min(st, 1.0e3)));+ if (pv.z > 0.0) { hv = spec_history(pv, n, t, rough, true); }+ }+ }+ let cap = select(L.misc.y, min(L.misc.y, 4.0), px.albedo.w > 1.5);+ let moving = any(L.cur.pos.xyz != L.prev.pos.xyz) || any(L.cur.fwd.xyz != L.prev.fwd.xyz);+ let translated = L.mode.y < 8u && !xflag(16384u);+ let ys = ycocg(hs.rgb);+ let yv = ycocg(hv.rgb);+ var surf = select(0.0, 0.8, length(smean - yv) > length(smean - ys) + 0.01 / exposure_scale());+ surf = select(surf, 1.0, hv.w <= 0.0);+ surf = select(surf, 0.0, hs.w <= 0.0 && hv.w > 0.0);+ let hy = mix(yv, ys, surf);+ var max_s = select(cap, mix(2.0, 12.0, clamp(rough / 0.05, 0.0, 1.0)), moving);+ if (translated) { max_s = min(max_s, 4.0); }+ let frames_s = select(hv.w, hs.w, surf > 0.5);+ var count_s = 1.0;+ var s_out = cur;+ if (hs.w > 0.0 || hv.w > 0.0) {+ count_s = min(min(frames_s + 1.0, max_s), cap);+ s_out = mix(max(ycocg_rgb(hy), vec3<f32>(0.0)), cur, clamp(1.0 / max(count_s, 1e-4), 0.0, 1.0));+ }+ s_out = select(max(s_out, vec3<f32>(0.0)), vec3<f32>(0.0), non_finite(s_out));+ shist_out[q] = vec4<f32>(s_out, count_s);+ spec_store(0u, q, vec4<f32>(s_out, count_s));+ gb_out[q] = gb_pack(n, depth, rough);+}++// The specular filter (`spec_filter`'s kernel) on the traced grid: three passes over `spec_pp`, slot 0 -> 1 -> 0 -> 1.+fn spec_filter_traced(gid: vec3<u32>, step: i32, from_a: bool) {+ let w = L.isz.x;+ let h = L.isz.y;if (gid.x >= w || gid.y >= h) { return; }let i = gid.y * w + gid.x;let src = select(1u, 0u, from_a);let c = spec_load(src, i);let g = gb_unpack(gb_out[i]);let rough = g.rough;- // Kernel radius x saturate(roughness x 8) (UE LumenReflectionDenoiserSpatial.usf): mirror-like surfaces are not- // filtered spatially; a pass of step s runs where the scaled radius reaches it.- // Fresh history (disocclusions, < 4 frames): every pass and a loose normal test, since 1-spp reflections on rippled- // wet ground are speckle that the normal-map detail would otherwise keep apart (Kabukicho k = 1 SSIM vs UE +0.02).let fresh = c.w < 4.0;let reach = select(4.0 * clamp(rough * 8.0, 0.0, 1.0), 4.0, xflag(2u) || fresh);let npow = select(64.0, 8.0, fresh);let strength = (0.25 + smoothstep(0.08, 0.5, rough)) / sqrt(max(c.w, 1.0)) * 4.0;var outv = c.rgb;if (strength >= 0.02 && f32(step) <= reach) {- // Edge-stop relative to the local mean: a dark 1-spp centre must still accept its neighbours.var li = 0.0;var nl = 0.0;for (var k = 0; k < 9; k++) {@@ -1012,11 +733,6 @@ fn spec_filter(gid: vec3<u32>, step: i32, from_a: bool) {if ((g.depth == 0.0) != (gq.depth == 0.0)) { wt = 0.0; }wt *= exp(-abs(gq.depth - g.depth) / (0.02 * g.depth + 0.01));wt *= exp(-abs(gq.rough - rough) * 10.0);- // Edge stop on luminance: wide (8 x strength x the local mean). Narrow (2 x), it kept sparse bright- // reflections out of their neighbours' means and their own (the centre weighs itself by its distance- // from the mean): their energy was lost, not spread. Static Bistro's specular 0.80 -> 0.84 of our- // offline render (layer B pose, 64 frames). Without it (no luminance stop) 0.85, but Zero Day's- // layer B rose 3.46 -> 3.53 (reflections blurred across sharp edges).wt *= exp(-abs(lum(sq) - li) / (strength * 8.0 * (li + 1e-3)));sum += sq * wt;wsum += wt;@@ -1026,19 +742,357 @@ fn spec_filter(gid: vec3<u32>, step: i32, from_a: bool) {if (non_finite(outv)) { outv = c.rgb; }}spec_store(1u - src, i, vec4<f32>(outv, c.w));- // The last pass also composes the final image: the rest (fb, from `temporal`: diffuse, directly seen emission, the- // sun's direct light; w: view depth) and this specular. Into fa, or back into fb for depth of field / motion blur to- // read (w: the CoC).- if (step == 4) {- let rest = fb[i];- let img = vec4<f32>(rest.rgb + outv, dof_coc(rest.w));- if (L.misc2.z > 0.0 || L.dof.x > 0.0) { fb[i] = img; } else { fa[i] = img; }+}++@compute @workgroup_size(8, 8, 1) fn sfilt0(@builtin(global_invocation_id) gid: vec3<u32>) { spec_filter_traced(gid, 1, true); }+@compute @workgroup_size(8, 8, 1) fn sfilt1(@builtin(global_invocation_id) gid: vec3<u32>) { spec_filter_traced(gid, 2, false); }+@compute @workgroup_size(8, 8, 1) fn sfilt2(@builtin(global_invocation_id) gid: vec3<u32>) { spec_filter_traced(gid, 4, true); }++// The frame at render resolution: each traced sample's colour (its own albedo times the denoised irradiance, the+// denoised specular, what the camera ray saw directly and the deterministic direct light) into fb (w: first-hit+// distance), and its motion into `xhist_out`: screen velocity (output pixels this frame; camera and object), view depth,+// and whether its shading is animated (signs, screens: the upscaler's reactive mask, as FSR 2 and UE's responsive AA+// take one: what changes there has no motion vector).+@compute @workgroup_size(8, 8, 1)+fn compose(@builtin(global_invocation_id) gid: vec3<u32>) {+ let iw = L.isz.x;+ let ih = L.isz.y;+ if (gid.x >= iw || gid.y >= ih) { return; }+ let q = gid.y * iw + gid.x;+ let px = sample_px[q];+ let t = px.color.w;+ let c = fa[q].rgb * max(px.albedo.rgb, vec3<f32>(0.02)) + spec_load(1u, q).rgb + sample_parts(q)[2] + direct_in[q].rgb;+ let sp = sample_pos(i32(gid.x), i32(gid.y));+ let dir = ray_dir(L.cur, sp.x, sp.y);+ var p = L.cur.pos.xyz + dir * 1.0e6;+ var depth = 0.0;+ if (t > 0.0) {+ p = L.cur.pos.xyz + dir * t;+ depth = dot(p - L.cur.pos.xyz, L.cur.fwd.xyz);+ }+ // (A static surface under a camera that holds still has no velocity: exactly none, not the reprojection's rounding,+ // so the upscaler reads its history where it wrote it.)+ var vel = vec2<f32>(0.0);+ let mv = motion_in[q];+ let object_moves = mv.w > 0.5 && t > 0.0;+ let camera_moves = any(L.cur.pos != L.prev.pos) || any(L.cur.fwd != L.prev.fwd) || any(L.cur.right != L.prev.right) || any(L.cur.up != L.prev.up);+ if (L.size.w == 0u && (camera_moves || object_moves)) {+ let pv = prev_pixel(p + select(vec3<f32>(0.0), mv.xyz, object_moves));+ if (pv.z > 0.0) { vel = clamp(sp - 0.5 - pv.xy, vec2<f32>(-2048.0), vec2<f32>(2048.0)); }+ }+ fb[q] = vec4<f32>(finite_or_zero(max(c, vec3<f32>(0.0))), t);+ xhist_out[q] = vec4<f32>(vel, depth, select(0.0, 1.0, px.albedo.w > 1.5));+}++// The upscaler works on linear radiance and its means are plain ones. (Weighted by 1 / (1 + luminance x exposure), so+// that a bright sample does not outweigh its neighbours, the blend with the history lost light around lamps: Bistro+// read 0.974 of offline's luminance for 1.015, with no less flicker and no shorter a trail in motion to show for it.)++// Samples of colour history an output pixel holds at most (a frame adds its nearest traced sample's weight): holding+// still, and moving UP_SPEED output pixels a frame or faster.+const UP_SAMPLES: f32 = 16.0;+const UP_MOVING: f32 = 4.0;+const UP_SPEED: f32 = 1.0;+// The reconstruction kernel exp(-UP_SHARP d^2), d in output pixels from the pixel's centre to a traced sample.+const UP_SHARP: f32 = 3.5;+// A history that this frame's samples lie outside of by this share of their luminance is dropped entirely (`reject`).+const UP_REJECT: f32 = 0.25;+// The same on animated shading (`compose`'s reactive mask): it shows this frame's picture, not a mean of the last ones.+const UP_REACTIVE: f32 = 4.0;+// How far outside the range of the history's pixels around it a point sample may lie, in that range's widths (`judge`).+const UP_POINT: f32 = 0.5;+// A surface counts as gone from in front when what is nearest now lies behind what was nearest by this share of its+// depth (`judge`), and the history as stale there when it lies outside this frame's range by UP_STALE of its luminance.+const UP_GONE: f32 = 0.05;+const UP_STALE: f32 = 0.05;++// History rejection at the render resolution (where UE's TSR judges its history too), in two passes. What a frame+// can say about the history is its low frequencies. Clipping each output pixel's history into the traced colours'+// range (a full-resolution TAA's rule) cut the detail finer than a traced pixel every frame; here the test is turned+// round: each traced sample is held against the range of the history around the very point it was taken at (where+// that point was a frame ago). A still, converged image explains every sample, however fine its detail and whichever+// side of an edge the jitter put the sample on; what the history cannot explain (a disocclusion, a light switched) is+// how far the sample lies outside that range.++// The one case that test cannot see is the rim of something that went away: output pixels that were part object, part+// background, whose colour the background's samples next to them explain. That is a disocclusion, and it is found the+// way every upscaler finds one, by depth: what is nearest the camera among the nine samples now lies behind what was+// nearest among the nine of the previous frame there (both dilated, so a still edge under the jitter mostly agrees+// with itself), confirmed by colour (the history there lies outside this frame's range), so the still edges that the+// jitter does catch keep their history.++// `judge`: per traced sample, into `ae_out` (f16): that distance (linear RGB), the sample's luminance (< 0: no+// history there), the motion its output pixels reproject by: the surface nearest the camera among the nine samples+// around (the sky the farthest), since an edge moves with what is in front of it; and whether its history is of+// something that is gone.+struct Judged { d: vec3<f32>, lum: f32, vel: vec2<f32>, gone: bool };+fn judged_at(q: u32) -> Judged {+ let v = ae_out[q];+ let b = unpack2x16float(v.y);+ return Judged(vec3<f32>(unpack2x16float(v.x), b.x), b.y, unpack2x16float(v.z), v.w != 0u);+}++@compute @workgroup_size(8, 8, 1)+fn judge(@builtin(global_invocation_id) gid: vec3<u32>) {+ let iw = i32(L.isz.x);+ let ih = i32(L.isz.y);+ if (i32(gid.x) >= iw || i32(gid.y) >= ih) { return; }+ let q = gid.y * L.isz.x + gid.x;+ var near = 1.0e30;+ var vel = vec2<f32>(0.0);+ for (var k = 0; k < 9; k++) {+ let qx = i32(gid.x) + k % 3 - 1;+ let qy = i32(gid.y) + k / 3 - 1;+ if (qx < 0 || qy < 0 || qx >= iw || qy >= ih) { continue; }+ let mv = xhist_out[u32(qy * iw + qx)];+ let z = select(1.0e29, mv.z, mv.z > 0.0);+ if (z < near) {+ near = z;+ vel = mv.xy;+ }+ }+ var d = vec3<f32>(0.0);+ var l = -1.0;+ var gone = false;+ if (L.size.w == 0u) {+ let up = vec2<f32>(f32(L.size.x), f32(L.size.y)) / vec2<f32>(f32(iw), f32(ih));+ // Where this sample's point was a frame ago (output pixels; a pixel's centre at + 0.5).+ let was = (vec2<f32>(f32(gid.x), f32(gid.y)) + L.jit.xy) * up - vel;+ let pp = vec2<i32>(floor(was));+ var lo = vec3<f32>(1.0e30);+ var hi = vec3<f32>(-1.0e30);+ var hsum = vec3<f32>(0.0);+ var hn = 0.0;+ for (var k = 0; k < 9; k++) {+ let x = pp.x + k % 3 - 1;+ let y = pp.y + k / 3 - 1;+ if (x < 0 || y < 0 || x >= i32(L.size.x) || y >= i32(L.size.y)) { continue; }+ let hv = hist_in[out_hist(u32(y) * L.size.x + u32(x))];+ if (hv.w <= 0.0 || non_finite(hv.rgb)) { continue; }+ lo = min(lo, hv.rgb);+ hi = max(hi, hv.rgb);+ hsum += hv.rgb;+ hn += 1.0;+ }+ if (hn > 0.0) {+ // The history's pixels are means over a pixel and the sample is a point: of a feature finer than a pixel+ // it can lie outside their range by as much as they differ among themselves (UP_POINT of it allowed).+ let slack = UP_POINT * (hi - lo);+ let c = fb[q].rgb;+ d = clamp(c - clamp(c, lo - slack, hi + slack), vec3<f32>(-6.0e4), vec3<f32>(6.0e4));+ l = min(lum(c), 6.0e4);+ // Gone from in front: the previous frame's nearest depth around where this point was (its traced grid's+ // g-buffer) against this frame's nearest, taken to the previous view by this sample's own depths.+ let ps = vec2<i32>(L.isz.zw);+ if (ps.x > 0) {+ var zr = 1.0;+ let t = fb[q].w;+ let z = xhist_out[q].z;+ if (t > 0.0 && z > 0.0) {+ let sp = sample_pos(i32(gid.x), i32(gid.y));+ let mv = motion_in[q];+ let pv = prev_pixel(L.cur.pos.xyz + ray_dir(L.cur, sp.x, sp.y) * t + select(vec3<f32>(0.0), mv.xyz, mv.w > 0.5));+ if (pv.z > 0.0) { zr = pv.z / z; }+ }+ let pc = vec2<i32>(floor(was * vec2<f32>(ps) / vec2<f32>(f32(L.size.x), f32(L.size.y))));+ var was_near = 1.0e30;+ for (var k = 0; k < 9; k++) {+ let x = pc.x + k % 3 - 1;+ let y = pc.y + k / 3 - 1;+ if (x < 0 || y < 0 || x >= ps.x || y >= ps.y) { continue; }+ let zq = gb_unpack(gb_in[u32(y * ps.x + x)]).depth;+ was_near = min(was_near, select(1.0e29, zq, zq > 0.0));+ }+ let now_near = select(near * zr, 1.0e29, near >= 1.0e29);+ if (was_near < 1.0e29 && now_near > was_near * (1.0 + UP_GONE) + 0.01) {+ var clo = vec3<f32>(1.0e30);+ var chi = vec3<f32>(-1.0e30);+ for (var k = 0; k < 9; k++) {+ let qx = i32(gid.x) + k % 3 - 1;+ let qy = i32(gid.y) + k / 3 - 1;+ if (qx < 0 || qy < 0 || qx >= iw || qy >= ih) { continue; }+ let cq = fb[u32(qy * iw + qx)].rgb;+ clo = min(clo, cq);+ chi = max(chi, cq);+ }+ let hm = hsum / hn;+ gone = lum(abs(hm - clamp(hm, clo, chi))) > UP_STALE * lum(hm) + 0.01 / exposure_scale();+ }+ }+ }}+ ae_out[q] = vec4<u32>(pack2x16float(d.xy), pack2x16float(vec2<f32>(d.z, l)), pack2x16float(vel), u32(gone));}-@compute @workgroup_size(8, 8, 1) fn spec0(@builtin(global_invocation_id) gid: vec3<u32>) { spec_filter(gid, 1, true); }-@compute @workgroup_size(8, 8, 1) fn spec1(@builtin(global_invocation_id) gid: vec3<u32>) { spec_filter(gid, 2, false); }-@compute @workgroup_size(8, 8, 1) fn spec2(@builtin(global_invocation_id) gid: vec3<u32>) { spec_filter(gid, 4, true); }+// `reject`: per traced pixel, into `spec_pp`'s slot 0 (free once the specular filter is done; f16): the correction+// `upscale` adds to every output pixel's history here (linear RGB), and the share of that history to drop. The+// correction is the mean of the nine samples' distances less what their scatter explains (noise, which the blend+// averages); a correction that is a large share of the colour drops the history too, as do samples that found none+// and one whose history is of something gone.+@compute @workgroup_size(8, 8, 1)+fn reject(@builtin(global_invocation_id) gid: vec3<u32>) {+ let iw = i32(L.isz.x);+ let ih = i32(L.isz.y);+ if (i32(gid.x) >= iw || i32(gid.y) >= ih) { return; }+ let q = gid.y * L.isz.x + gid.x;+ let e = exposure_scale();+ var kern = array<f32, 2>(0.5, 0.25);+ var d1 = vec3<f32>(0.0);+ var x1 = 0.0;+ var x2 = 0.0;+ var level = 0.0;+ var wsum = 0.0;+ var wall = 0.0;+ for (var k = 0; k < 9; k++) {+ let dx = k % 3 - 1;+ let dy = k / 3 - 1;+ let qx = i32(gid.x) + dx;+ let qy = i32(gid.y) + dy;+ if (qx < 0 || qy < 0 || qx >= iw || qy >= ih) { continue; }+ let wt = kern[abs(dx)] * kern[abs(dy)];+ wall += wt;+ let j = judged_at(u32(qy * iw + qx));+ if (j.lum < 0.0) { continue; }+ let hw = 1.0 / (1.0 + j.lum * e);+ let x = lum(j.d) * hw;+ d1 += j.d * wt;+ x1 += x * wt;+ x2 += x * x * wt;+ level += j.lum * hw * wt;+ wsum += wt;+ }+ var delta = vec3<f32>(0.0);+ var drop = 1.0;+ if (wsum > 0.0) {+ // The scatter of the distances about their mean (in luminances compressed by the exposure, so a bright+ // sample's noise counts as a dark one's): a mean of nine carries about a third of it.+ let mean = x1 / wsum;+ let sd = sqrt(max(x2 / wsum - mean * mean, 0.0));+ let keep = clamp(1.0 - 0.5 * sd / max(abs(mean), 1e-12), 0.0, 1.0);+ delta = d1 / wsum * keep;+ let found = wsum / wall;+ drop = clamp(abs(mean) * keep / (UP_REJECT * level / wsum + 0.01 / e), 0.0, 1.0) * found + (1.0 - found);+ }+ // (Its history is of something that is gone from in front of this: all of it goes.)+ if (judged_at(q).gone) {+ delta = vec3<f32>(0.0);+ drop = 1.0;+ }+ spec_store(0u, q, vec4<f32>(clamp(delta, vec3<f32>(-6.0e4), vec3<f32>(6.0e4)), drop));+}++// Colour history (linear, w = samples) at previous-frame output position (fx, fy): Catmull-Rom over 4 x 4 pixels+// (bilinear resampling every frame blurs while the view moves), held within the range of the four pixels around the+// position (the filter's negative lobes ring at strong edges).+fn up_history(fx: f32, fy: f32) -> vec4<f32> {+ let x1 = floor(fx);+ let y1 = floor(fy);+ let t = vec2<f32>(fx - x1, fy - y1);+ let w0 = t * (-0.5 + t * (1.0 - 0.5 * t));+ let w1 = 1.0 + t * t * (-2.5 + 1.5 * t);+ let w2 = t * (0.5 + t * (2.0 - 1.5 * t));+ let w3 = t * t * (-0.5 + 0.5 * t);+ let wx = array<f32, 4>(w0.x, w1.x, w2.x, w3.x);+ let wy = array<f32, 4>(w0.y, w1.y, w2.y, w3.y);+ var h = vec4<f32>(0.0);+ var lo = vec4<f32>(1.0e30);+ var hi = vec4<f32>(-1.0e30);+ for (var j = 0; j < 4; j++) {+ let qy = u32(clamp(i32(y1) + j - 1, 0, i32(L.size.y) - 1));+ for (var i = 0; i < 4; i++) {+ let qx = u32(clamp(i32(x1) + i - 1, 0, i32(L.size.x) - 1));+ let v = hist_in[out_hist(qy * L.size.x + qx)];+ h += v * (wx[i] * wy[j]);+ if ((i == 1 || i == 2) && (j == 1 || j == 2)) {+ lo = min(lo, v);+ hi = max(hi, v);+ }+ }+ }+ return clamp(h, lo, hi);+}++// The temporal upscaler: the composed frame of the traced grid (fb, `compose`) to the output grid, one colour history. Each output pixel takes this frame's traced samples around it by their distance (the frame's jitter moves+// them), reprojects its history by the motion `judge` chose there, corrects and discounts that history by `reject`'s+// verdict, and blends by how near a sample came.+@compute @workgroup_size(8, 8, 1)+fn upscale(@builtin(global_invocation_id) gid: vec3<u32>) {+ let w = L.size.x;+ let h = L.size.y;+ if (gid.x >= w || gid.y >= h) { return; }+ let i = gid.y * w + gid.x;+ let iw = i32(L.isz.x);+ let ih = i32(L.isz.y);+ let up = vec2<f32>(f32(w), f32(h)) / vec2<f32>(f32(iw), f32(ih)); // output pixels a traced pixel+ // In traced-sample units: sample (qx, qy) sits at (qx, qy), this pixel's centre at rc.+ let rc = vec2<f32>(f32(gid.x) + 0.5, f32(gid.y) + 0.5) / up - L.jit.xy;+ let bc = clamp(vec2<i32>(round(rc)), vec2<i32>(0), vec2<i32>(iw - 1, ih - 1));+ var sum = vec3<f32>(0.0);+ var wsum = 0.0;+ var xlo = vec3<f32>(1.0e30);+ var xhi = vec3<f32>(-1.0e30);+ for (var k = 0; k < 9; k++) {+ let qx = bc.x + k % 3 - 1;+ let qy = bc.y + k / 3 - 1;+ if (qx < 0 || qy < 0 || qx >= iw || qy >= ih) { continue; }+ let d = (vec2<f32>(f32(qx), f32(qy)) - rc) * up;+ let wt = exp(-UP_SHARP * dot(d, d));+ let c = fb[u32(qy * iw + qx)].rgb;+ sum += c * wt;+ wsum += wt;+ xlo = min(xlo, c);+ xhi = max(xhi, c);+ }+ let cur = select(fb[u32(bc.y * iw + bc.x)].rgb, sum / wsum, wsum > 1e-9);+ let conf = min(wsum, 1.0);+ // `reject`'s verdict, bilinear over the four traced pixels around; the motion `judge` chose at the nearest.+ let f0 = vec2<i32>(floor(rc));+ let ft = rc - floor(rc);+ var verdict = vec4<f32>(0.0);+ var vsum = 0.0;+ for (var k = 0; k < 4; k++) {+ let qx = f0.x + (k & 1);+ let qy = f0.y + (k >> 1u);+ if (qx < 0 || qy < 0 || qx >= iw || qy >= ih) { continue; }+ let bw = select(1.0 - ft.x, ft.x, (k & 1) == 1) * select(1.0 - ft.y, ft.y, (k >> 1u) == 1);+ verdict += spec_load(0u, u32(qy * iw + qx)) * bw;+ vsum += bw;+ }+ verdict = select(vec4<f32>(0.0, 0.0, 0.0, 1.0), verdict / vsum, vsum > 1e-4);+ let vel = judged_at(u32(bc.y * iw + bc.x)).vel;+ var hist = vec4<f32>(0.0);+ if (L.size.w == 0u) {+ let hp = vec2<f32>(f32(gid.x), f32(gid.y)) - vel;+ if (hp.x > -0.5 && hp.y > -0.5 && hp.x < f32(w) - 0.5 && hp.y < f32(h) - 0.5) {+ if (vel.x == 0.0 && vel.y == 0.0) { hist = hist_in[out_hist(i)]; } else { hist = up_history(hp.x, hp.y); }+ }+ if (non_finite(hist.rgb) || non_finite(vec3<f32>(hist.w))) { hist = vec4<f32>(0.0); }+ }+ var outc = cur;+ var count = conf;+ // In motion the history is resampled every frame, each time a little blurrier, and what it holds of another+ // surface (the band behind a moving edge reprojects by the edge's motion) nothing else removes: so, as the pixel's+ // speed nears UP_SPEED, it holds fewer samples (UE TSR's r.TSR.Velocity.WeightClampingSampleCount / PixelSpeed) and+ // is clamped into the range of this frame's nine samples (every temporal AA's rule; FSR 2's rectification). Holding+ // still it keeps both its length and the detail finer than a traced pixel, which the clamp would cut. The orbit of+ // Thames at 9 pixels a frame against offline (detail / trail px): neither 0.48 / 49, the clamp 0.45 / 36, 4 samples+ // 0.57 / 37, both 0.53 / 31.+ let moving = clamp(length(vel) / UP_SPEED, 0.0, 1.0);+ let kept = hist.w * (1.0 - verdict.w);+ if (kept > 0.0) {+ var hc = max(hist.rgb + verdict.xyz, vec3<f32>(0.0));+ hc = mix(hc, clamp(hc, xlo, xhi), moving);+ let most = min(mix(UP_SAMPLES, UP_MOVING, moving), select(UP_SAMPLES, UP_REACTIVE, xhist_out[u32(bc.y * iw + bc.x)].w > 0.5));+ count = min(kept + conf, most);+ let a = clamp(conf / max(count, 1e-4), 0.0, 1.0);+ outc = mix(hc, cur, a);+ }+ let lin = finite_or_zero(max(outc, vec3<f32>(0.0)));+ hist_out[out_hist(i)] = vec4<f32>(lin, count);+ // The image: into fa, or into sb for depth of field / motion blur to read (w: the CoC of the nearest sample's depth).+ let img = vec4<f32>(lin, dof_coc(xhist_out[u32(bc.y * iw + bc.x)].z));+ if (L.misc2.z > 0.0 || L.dof.x > 0.0) { sb[i] = img; } else { fa[i] = img; }+}// ------------------------------------------------------------------------------------------------ exposure@@ -1309,13 +1363,13 @@ fn dof_tiles(@builtin(global_invocation_id) gid: vec3<u32>) {var m = 0.0;for (var y = gid.y * DOF_TILE; y < min((gid.y + 1u) * DOF_TILE, L.size.y); y++) {for (var x = gid.x * DOF_TILE; x < min((gid.x + 1u) * DOF_TILE, L.size.x); x++) {- m = max(m, abs(fb[y * L.size.x + x].w));+ m = max(m, abs(sb[y * L.size.x + x].w));}}lumq[gid.y * tw + gid.x] = m;}-// Depth of field on the composed HDR image (fb -> fa, or sb when motion blur follows), before motion blur as in UE.+// Depth of field on the upscaled HDR image (sb -> fa, or fb when motion blur follows), before motion blur as in UE.// Scatter as gather over the largest CoC around: each pixel spreads its colour over its own CoC disk. Samples in front// of this pixel form a foreground layer over the rest, its coverage from their spread; samples behind count only as// far as this pixel's own CoC reaches, so blurred background doesn't cover a sharp pixel.@@ -1325,7 +1379,7 @@ fn dof(@builtin(global_invocation_id) gid: vec3<u32>) {let h = L.size.y;if (gid.x >= w || gid.y >= h || L.dof.x <= 0.0) { return; }let i = gid.y * w + gid.x;- let center = fb[i];+ let center = sb[i];let ci = abs(center.w);let tw = (w + DOF_TILE - 1u) / DOF_TILE;let th = (h + DOF_TILE - 1u) / DOF_TILE;@@ -1354,7 +1408,7 @@ fn dof(@builtin(global_invocation_id) gid: vec3<u32>) {let q = vec2<i32>(round(pc + r * sqrt((f32(k) + 0.5) / f32(DOF_SAMPLES)) * vec2<f32>(cos(a), sin(a))));if (q.x < 0 || q.y < 0 || q.x >= i32(w) || q.y >= i32(h)) { continue; }let j = u32(q.y) * w + u32(q.x);- let sj = fb[j];+ let sj = sb[j];let cj = abs(sj.w);let d = length(vec2<f32>(q) - pc);let front = clamp((zi - view_depth(j)) / (0.02 * zi + 0.05), 0.0, 1.0);@@ -1370,23 +1424,28 @@ fn dof(@builtin(global_invocation_id) gid: vec3<u32>) {if (fw > 1e-6) { outv = mix(outv, fg / fw, clamp(fw, 0.0, 1.0)); }if (non_finite(outv)) { outv = center.rgb; }}- if (L.misc2.z > 0.0) { sb[i] = vec4<f32>(outv, 0.0); } else { fa[i] = vec4<f32>(outv, 0.0); }+ if (L.misc2.z > 0.0) { fb[i] = vec4<f32>(outv, 0.0); } else { fa[i] = vec4<f32>(outv, 0.0); }}// ------------------------------------------------------------------------------------------------ motion blur// The image motion blur reads: depth of field's output, or the composed image.fn mb_src(q: u32) -> vec3<f32> {- if (L.dof.x > 0.0) { return sb[q].rgb; }- return fb[q].rgb;+ if (L.dof.x > 0.0) { return fb[q].rgb; }+ return sb[q].rgb;+}++// Output pixel q's motion and depth: those of the traced sample under it (`compose`).+fn motion_under(q: u32) -> vec4<f32> {+ return xhist_out[sample_under(q % L.size.x, q / L.size.x)];}fn velocity(q: u32) -> vec2<f32> {- return unpack2x16float(bitcast<u32>(xhist_out[q].w)) * L.misc2.z;+ return motion_under(q).xy * L.misc2.z;}fn view_depth(q: u32) -> f32 {- let z = gb_unpack(gb_out[q]).depth;+ let z = motion_under(q).z;return select(z, 1.0e6, z <= 0.0);}@@ -1396,8 +1455,8 @@ const MB_SAMPLES: i32 = 12;// velocity to the blur length, post.w caps it). A reconstruction filter after McGuire et al. 2012 without tiles:// samples along the pixel's own blur; a sample counts where it is in front and its own blur reaches this pixel, or// behind and this pixel's blur reaches it, so moving objects smear over a static background but a static foreground-// stays sharp. fb (`spec2` composes there while it runs: misc2.z > 0, set only when the view or an object moved), or-// sb after depth of field -> fa.+// stays sharp. sb (`upscale` writes there while it runs: misc2.z > 0, set only when the view or an object moved), or+// fb after depth of field -> fa.@compute @workgroup_size(8, 8, 1)fn mblur(@builtin(global_invocation_id) gid: vec3<u32>) {let w = L.size.x;@@ -1459,10 +1518,10 @@ fn post(@builtin(global_invocation_id) gid: vec3<u32>) {if (mode == 1u || mode == 2u || mode == 5u) {var d = vec3<f32>(0.0);if (mode == 1u) {- let a = clamp(hist_albedo(ae_out[i]), vec3<f32>(0.0), vec3<f32>(1.0));+ let a = clamp(sample_px[sample_under(gid.x, gid.y)].albedo.rgb, vec3<f32>(0.0), vec3<f32>(1.0));d = select(pow((a + 0.055) / 1.055, vec3<f32>(1.0 / 2.4)) * 1.055 - 0.055, a * 12.92, a <= vec3<f32>(0.0031308));} else if (mode == 2u) {- let g = gb_unpack(gb_out[i]);+ let g = gb_unpack(gb_out[sample_under(gid.x, gid.y)]);d = select(g.n * 0.5 + 0.5, vec3<f32>(0.0), g.depth == 0.0);} else {let t = clamp(log2(max(hist_out[sample_under(gid.x, gid.y)].w, 1.0)) / 12.0, 0.0, 1.0); // 1 .. 4096 frames (diffuse)@@ -1473,12 +1532,15 @@ fn post(@builtin(global_invocation_id) gid: vec3<u32>) {return;}var src = fa[i].rgb;- if (mode == 3u) { src = hist_out[sample_under(gid.x, gid.y)].rgb * max(hist_albedo(ae_out[i]), vec3<f32>(0.02)) + shist_out[i].rgb + hist_emission(ae_out[i]) + xhist_out[i].rgb; }- if (mode == 4u) { src = sample_color(sample_under(gid.x, gid.y)); }- // Signals apart, denoised: diffuse (6), specular (7).- if (mode == 6u) { src = max(fa[i].rgb - spec_load(1u, i).rgb - hist_emission(ae_out[i]) - xhist_out[i].rgb, vec3<f32>(0.0)); }- if (mode == 8u) { src = xhist_out[i].rgb; }- if (mode == 7u) { src = spec_load(1u, i).rgb; }+ // The traced sample under the pixel: its accumulations without the spatial filters (3), the raw sample (4), and the+ // denoised signals apart: diffuse (6: the composed colour less the rest), specular (7), deterministic direct light (8).+ // (6 does not hold with depth of field and motion blur both on: the composed colour's buffer is theirs then.)+ let tq = sample_under(gid.x, gid.y);+ if (mode == 3u) { src = hist_out[tq].rgb * max(sample_px[tq].albedo.rgb, vec3<f32>(0.02)) + shist_out[tq].rgb + sample_parts(tq)[2] + direct_in[tq].rgb; }+ if (mode == 4u) { src = sample_color(tq); }+ if (mode == 6u) { src = max(fb[tq].rgb - spec_load(1u, tq).rgb - sample_parts(tq)[2] - direct_in[tq].rgb, vec3<f32>(0.0)); }+ if (mode == 8u) { src = direct_in[tq].rgb; }+ if (mode == 7u) { src = spec_load(1u, tq).rgb; }if (L.post.x > 0.0 && mode == 0u) {// Scene fringe: red sampled radially outward, blue inward (nearest texel).let k = L.post.x * 0.0025;diff --git a/renderer/shaders/pathtrace.wgsl b/renderer/shaders/pathtrace.wgslindex a9dd880..6f7aac6 100644--- a/renderer/shaders/pathtrace.wgsl+++ b/renderer/shaders/pathtrace.wgsl@@ -70,7 +70,7 @@ struct Params {fog_color: vec4<f32>, // ambient inscattering radiance, max opacityvol: vec4<f32>, // volumetric scattering albedo (0 = off), HG anisotropy, extinction scale in range, -vol2: vec4<f32>, // volumetric albedo tint rgb, range (m, 0 = unlimited)- raster: vec4<f32>, // live primary visibility from the raster pass (1 = on), this frame's sub-pixel jitter x, y; ray tracing off (1)+ raster: vec4<f32>, // live: primary visibility from the raster pass (1 = on), this frame's sub-pixel jitter x, y (every camera ray's); ray tracing off (1)cache: vec4<f32>, // live radiance cache (1 = on), history cap in samples, history cap in frames, -motion: vec4<f32>, // live motion vectors: 1 = on, -, -, - (first dynamic triangle: group_first[1].x)prev_pos: vec4<f32>, // live: the previous frame's camera (emitter reservoir reprojection), as cam_*@@ -2919,9 +2919,11 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {// Raster primary: one visibility sample per pixel at the frame's jitter (lens sampling would need its own rays).let raster = LIVE && P.raster.x > 0.5 && P.cam_right.w <= 0.0;+ // Live: one sub-pixel jitter a frame for the whole image (live.rs: Halton 2, 3), which the temporal upscaler+ // resolves (live.wgsl `upscale`) and the raster pass draws at. Offline, every sample draws its own.var jx = rnd();var jy = rnd();- if (raster) {+ if (LIVE) {jx = P.raster.y;jy = P.raster.z;}diff --git a/renderer/src/live.rs b/renderer/src/live.rsindex c86da58..2536f14 100644--- a/renderer/src/live.rs+++ b/renderer/src/live.rs@@ -4,10 +4,12 @@//! (`view`) and the playground client; `live` runs it headless at a fixed frame rate for benchmarks://!//! threert-render live <game bundle.js> [--assets DIR] [--room id] [--width 1280 --height 720 --fps 60 --bounces 4-//! --scale 1|0.6|auto] [--frame-log frames.jsonl]+//! --scale 1|0.6|auto --display-scale 1|2|3] [--frame-log frames.jsonl]//!-//! `--scale` is the render scale (UE screen percentage): samples are traced at that fraction of the output-//! resolution and the temporal pass reconstructs the output (TSR-style upsampling); `auto` = dynamic resolution.+//! `--scale` is the render scale (UE screen percentage): the frame is traced, denoised and composed at that fraction+//! of the output resolution and the temporal upscaler takes it to the output; `auto` = dynamic resolution.+//! `--display-scale`: the output is a display's physical pixels, that many a point (a Retina window: 2), as the+//! window host's is; dynamic resolution then starts from the points' resolution and goes down to half of it.//!//! stdin (JSON lines): {"cmd":"camera","pos":[x,y,z],"target":[x,y,z]} | {"cmd":"event","type":t,"payload":{..}}//! | {"cmd":"click","u":u,"v":v} | {"cmd":"reset"} | {"cmd":"time","t":s} (set the scene time)@@ -54,10 +56,16 @@ pub struct LiveOpts {/// `interval` runs from the end of one frame's work to the next's, so it carries the variation of the work itself;/// `start` runs from one frame's start to the next's, which the pacer sets: the frame rate a player sees.pub frame_log: Option<PathBuf>,+ /// The output's pixels a point of its display (1: the output is in points).+ pub display_scale: f32,}-/// Lowest dynamic-resolution scale (UE r.DynamicRes.MinScreenPercentage default).+/// Lowest dynamic-resolution scale (UE r.DynamicRes.MinScreenPercentage default) of a view whose pixels are the+/// display's points; a host whose view is in the display's physical pixels divides it by the display's scale factor+/// (`LiveCore::min_scale`), so the fewest rays a window traces do not depend on the display's density.pub const MIN_SCALE: f32 = 0.5;+/// The lowest render scale any view takes (a view of 3 pixels a point at `MIN_SCALE`, rounded down).+pub const SCALE_FLOOR: f32 = 0.125;#[repr(C)]#[derive(Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)]@@ -95,6 +103,8 @@ struct LiveParams {bt2: [f32; 4],/// Depth of field: CoC radius of infinity (output px; 0 = off), focus distance (m), max radius (px), -.dof: [f32; 4],+ /// This frame's sub-pixel jitter (every camera ray's: x, y in 0 .. 1 of a traced pixel), -, -.+ jit: [f32; 4],}/// Bloom pyramid level j (0..4: 1/4 .. 1/64 resolution) size; stage k (0..5) runs on level max(k - 1, 0).@@ -245,7 +255,7 @@ impl Passes {}let layout = d.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("live"), entries: &entries });let pl = d.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[Some(&layout)], immediate_size: 0 });- let pipes = ["refl", "dtemporal", "atrous0", "atrous1", "atrous2", "atrous3", "atrous4", "temporal", "spec0", "spec1", "spec2", "dof_tiles", "dof", "mblur", "lum_down", "exposure", "lum_blur", "bloom_down", "bloom_dn1", "bloom_dn2", "bloom_dn3", "bloom_dn4",+ let pipes = ["refl", "dtemporal", "atrous0", "atrous1", "atrous2", "atrous3", "atrous4", "stemporal", "sfilt0", "sfilt1", "sfilt2", "compose", "judge", "reject", "upscale", "dof_tiles", "dof", "mblur", "lum_down", "exposure", "lum_blur", "bloom_down", "bloom_dn1", "bloom_dn2", "bloom_dn3", "bloom_dn4","bloom_h5", "bloom_v5", "bloom_h4", "bloom_v4", "bloom_h3", "bloom_v3", "bloom_h2", "bloom_v2", "bloom_h1", "bloom_v1", "bloom_h0", "bloom_v0", "post"].into_iter().map(|name| {@@ -259,12 +269,14 @@ impl Passes {/// Screen-size targets shared across scene reloads.pub struct Targets {params: wgpu::Buffer,+ /// The diffuse irradiance history of the traced grid, then the output grid's colour history (ping-pong).hist: [Buf; 2],+ /// The traced grid's g-buffer (ping-pong: this frame's, the previous frame's).gb: [Buf; 2],- /// Accumulated base color (the denoiser's demodulation albedo) + directly seen emission (kept out of the- /// denoiser), output resolution, f16.+ /// What `judge` finds of the colour history per traced sample (live.wgsl `Judged`); the second is not read.alb: [Buf; 2],- /// Specular history, diffuse luminance moments (ping-pong), specular denoiser scratch.+ /// Specular history, diffuse luminance moments (ping-pong, traced grid), the specular filter's two slots, and the+ /// scratch `dtemporal` and the upscaled image share.shist: [Buf; 2],mom: [Buf; 2],sa: Buf,@@ -272,7 +284,8 @@ pub struct Targets {/// Reflection resolve: the resolved specular reflection per traced pixel (live.wgsl `refl`), from the trace/// kernel's specular-lobe rays (2 vec4 per traced pixel, pathtrace.wgsl `refl`: `kernel.refl`).refl_res: Buf,- /// The history of the deterministic direct light (`kernel.direct`), ping-pong.+ /// Per traced sample: screen velocity, view depth, animated shading (live.wgsl `compose`); the frame's parity+ /// picks which of the two is written, the other is not read.xhist: [Buf; 2],fa: Buf,fb: Buf,@@ -1167,6 +1180,10 @@ pub struct LiveCore {pub render_scale: f32,/// Adjust `render_scale` to the frame budget (`govern_resolution`).pub dynamic_res: bool,+ /// Dynamic resolution's lowest scale: `MIN_SCALE` over the display's scale factor.+ pub min_scale: f32,+ /// The view's pixels a point of its display (`set_display_scale`): dynamic resolution's steps are a point's.+ display_scale: f32,/// Dynamic resolution: consecutive measured frames well over the frame time, and the first frame at the current/// scale.over_budget: (u32, u32),@@ -1208,6 +1225,8 @@ pub struct LiveCore {lit_changed: u32,/// The scene has light-sampled particles (emitter table entries with a nominal area).spark_lights: bool,+ /// Samples per pixel of the last frame rendered (the kernel's sample buffer holds their sums).+ shown_spp: u32,/// CPU time of the last commit (ms): skinning and re-transforming moving objects (`reskin`, rigid instance poses), and/// uploading them with the dynamic BLAS / TLAS rebuild submitted (frame log `skin`, `upload`).pub cpu_skin_ms: f32,@@ -1452,7 +1471,7 @@ fn dof_params(dof: Option<&crate::desc::Dof>, tan_half_fov: f32, w: u32, h: u32)/// Live passes that run on the traced grid (the rest on the output grid).fn traced_pass(name: &str) -> bool {- name == "refl" || name == "dtemporal" || name.starts_with("atrous")+ name == "refl" || name == "dtemporal" || name.starts_with("atrous") || name == "stemporal" || name.starts_with("sfilt") || name == "compose" || name == "judge" || name == "reject"}/// Per-object motion vectors (THREERT_NO_MV=1: off, moving objects keep a short history instead).@@ -1606,7 +1625,7 @@ impl LiveCore {// brings it, `gpu::loop_guard_of`.)let guard_read = (gpu::loop_guard_of(!g.hwrt).is_some() && targets.kernel.guard.is_some()).then(|| (g.device.create_buffer(&wgpu::BufferDescriptor { label: Some("loop guard read"), size: 4, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false }), Arc::new(AtomicU32::new(0))));- let mut core = LiveCore { particle_spawns: Default::default(), grid_sources: Default::default(), g, passes, targets, sc, presets: presets.to_path_buf(), assets: assets.to_path_buf(), w, h, seq: 0, aspect: w as f32 / h as f32, cam, prev_cam: cam, frame: 0, history_len: 0.0, reset: true, cap_next: None, bloom: ([0.0; 6], [0.0; 6]), vignette: 0.0, post: [0.0; 4], blur: (0.0, 0.0), still: 0, inflight: Default::default(), gpu_ms: 0.0, gpu_frame: 0, gpu_trace_ms: 0.0, edit_ms: 0.0, instanced_ms: 0.0, edit_split: EditSplit::default(), rebuild_ms: 0.0, fallback: None, fallbacks: 0, bounces: 4, rays: true, view_mode: std::env::var("THREERT_VIEW_MODE").ok().and_then(|v| v.parse().ok()).unwrap_or(0), fixed_dt: None, motion_blur_off: false, ghost_fix_off: false, triangles, scene_name, time: 0.0, frame_dt: 1.0 / 60.0, last_frame: Instant::now(), post_key: String::new(), render_scale: 1.0, dynamic_res: false, over_budget: (0, 0), profile, timer, raster, cache, restir, restir_prev: None, kernel_bg, prev_span: None, reposed: false, emitters_moved: false, cold_frames: 0, pose_span: None, pose_reposed: false, inst_moving: vec![], inst_moved: false, prev_isz: (0, 0), lit_changed: 0, spark_lights: false, cpu_skin_ms: 0.0, cpu_upload_ms: 0.0, gpu_blas_ms: 0.0, cpu_wait_ms: 0.0, sky_cover: 0.0, guard_trips: guard_read.as_ref().map(|_| 0), guard_read };+ let mut core = LiveCore { particle_spawns: Default::default(), grid_sources: Default::default(), g, passes, targets, sc, presets: presets.to_path_buf(), assets: assets.to_path_buf(), w, h, seq: 0, aspect: w as f32 / h as f32, cam, prev_cam: cam, frame: 0, history_len: 0.0, reset: true, cap_next: None, bloom: ([0.0; 6], [0.0; 6]), vignette: 0.0, post: [0.0; 4], blur: (0.0, 0.0), still: 0, inflight: Default::default(), gpu_ms: 0.0, gpu_frame: 0, gpu_trace_ms: 0.0, edit_ms: 0.0, instanced_ms: 0.0, edit_split: EditSplit::default(), rebuild_ms: 0.0, fallback: None, fallbacks: 0, bounces: 4, rays: true, view_mode: std::env::var("THREERT_VIEW_MODE").ok().and_then(|v| v.parse().ok()).unwrap_or(0), fixed_dt: None, motion_blur_off: false, ghost_fix_off: false, triangles, scene_name, time: 0.0, frame_dt: 1.0 / 60.0, last_frame: Instant::now(), post_key: String::new(), render_scale: 1.0, dynamic_res: false, min_scale: MIN_SCALE, display_scale: 1.0, over_budget: (0, 0), profile, timer, raster, cache, restir, restir_prev: None, kernel_bg, prev_span: None, reposed: false, emitters_moved: false, cold_frames: 0, pose_span: None, pose_reposed: false, inst_moving: vec![], inst_moved: false, prev_isz: (0, 0), lit_changed: 0, spark_lights: false, shown_spp: 1, cpu_skin_ms: 0.0, cpu_upload_ms: 0.0, gpu_blas_ms: 0.0, cpu_wait_ms: 0.0, sky_cover: 0.0, guard_trips: guard_read.as_ref().map(|_| 0), guard_read };core.spark_lights = core.sc.tables.emitter_tris.iter().any(|e| e.nominal);core.sky_cover = core.sc.tables.cloud_cover;core.apply_post(&pp);@@ -3539,11 +3558,21 @@ impl LiveCore {Ok(())}- /// Fixed render scale (`s` in 0.25 ..= 1), or dynamic resolution (`s` = 0).+ /// The view is a display's physical pixels, `k` a point: dynamic resolution, from the points' resolution (the rays+ /// a view in points starts with) down to `MIN_SCALE` of it. A fixed scale is set after it (`set_scale`).+ pub fn set_display_scale(&mut self, k: f32) {+ let k = k.max(1.0);+ self.display_scale = k;+ self.min_scale = (MIN_SCALE / k).max(SCALE_FLOOR);+ self.render_scale = 1.0 / k;+ self.dynamic_res = true;+ }++ /// Fixed render scale (`s` in SCALE_FLOOR ..= 1), or dynamic resolution (`s` = 0).pub fn set_scale(&mut self, s: f32) {self.dynamic_res = s <= 0.0;if s > 0.0 {- self.render_scale = s.clamp(0.25, 1.0);+ self.render_scale = s.clamp(SCALE_FLOOR, 1.0);}}@@ -3574,12 +3603,12 @@ impl LiveCore {/// Path-trace dispatches are submitted inside (each needs its own uniform contents)./// Traced resolution at the current render scale.pub fn internal_size(&self) -> (u32, u32) {- let s = self.render_scale.clamp(0.25, 1.0);+ let s = self.render_scale.clamp(SCALE_FLOOR, 1.0);(((self.w as f32 * s).round() as u32).clamp(8, self.w), ((self.h as f32 * s).round() as u32).clamp(8, self.h))}/// Dynamic resolution (UE r.DynamicRes): twice a second, move the render scale toward the frame budget (traced- /// pixels ~ GPU time), within [MIN_SCALE, 1], in damped steps with hysteresis so it does not oscillate.+ /// pixels ~ GPU time), within [`min_scale`, 1], in damped steps with hysteresis so it does not oscillate./// Two frames in a row well over the frame time (x 1.25) act at once (UE/// r.DynamicRes.MaxConsecutiveOverbudgetGPUFrameCount = 2): turning from a light view at a raised scale toward a/// heavy one otherwise cost up to 30 frames at 35-48 ms (the 3 x 3 district's drive, its far end). Just over the@@ -3594,7 +3623,7 @@ impl LiveCore {}// Not in the first 2 s: those frames run while caches fill (Kabukichō's first frames at 0.7 take ~47 ms), and// the regular check settles the start as before.- self.over_budget.0 = if self.frame >= 120 && self.gpu_ms > 1250.0 / fps as f32 && self.render_scale > MIN_SCALE { self.over_budget.0 + 1 } else { 0 };+ self.over_budget.0 = if self.frame >= 120 && self.gpu_ms > 1250.0 / fps as f32 && self.render_scale > self.min_scale { self.over_budget.0 + 1 } else { 0 };let urgent = self.over_budget.0 >= 2;if self.frame % 30 != 1 && !urgent {return;@@ -3602,14 +3631,18 @@ impl LiveCore {self.over_budget.0 = 0;let before = self.render_scale;let budget = 1000.0 / fps as f32 * 0.9;- let target = (self.render_scale * (budget / self.gpu_ms).sqrt()).clamp(MIN_SCALE, 1.0);+ let target = (self.render_scale * (budget / self.gpu_ms).sqrt()).clamp(self.min_scale, 1.0);// The hysteresis (0.03) does not hold the scale just short of either end: 0.525 with the floor as the target// kept the district's return drive at 21-22 ms.- let to_end = (target == 1.0 && self.render_scale < 1.0 && self.gpu_ms < budget * 0.8) || (target == MIN_SCALE && self.render_scale > MIN_SCALE);- if (target - self.render_scale).abs() > 0.03 || to_end {+ let to_end = (target == 1.0 && self.render_scale < 1.0 && self.gpu_ms < budget * 0.8) || (target == self.min_scale && self.render_scale > MIN_SCALE);+ // (The hysteresis, the steps and the grid of scales are a view in points'; on a display of k pixels a point the+ // same share of the rays is 1 / k of the scale. Left absolute, a Retina view held 0.325 at 17.3 ms: its target,+ // 0.30, was inside the hysteresis.)+ let k = self.display_scale;+ if (target - self.render_scale).abs() > 0.03 / k || to_end {// Down fast when far over budget (a heavy scene just opened), up slowly; straight to the target when urgent.- let step = if urgent { target - self.render_scale } else { (target - self.render_scale).clamp(if self.gpu_ms > budget * 1.5 { -0.3 } else { -0.15 }, 0.1) };- self.render_scale = ((self.render_scale + step) * 40.0).round() / 40.0;+ let step = if urgent { target - self.render_scale } else { (target - self.render_scale).clamp(if self.gpu_ms > budget * 1.5 { -0.3 } else { -0.15 } / k, 0.1 / k) };+ self.render_scale = ((self.render_scale + step) * 40.0 * k).round() / (40.0 * k);}if self.render_scale != before {// `frame` is already the next frame's index (render advanced it): the first one at the new scale.@@ -3750,17 +3783,19 @@ impl LiveCore {let media = self.sc.up.kernel.features & gpu::F_MEDIA != 0;let media_train = (media && self.cache.is_some()).then(|| g.media_pipeline(&self.sc.up.kernel, "media_train"));let media_composite = media.then(|| g.media_pipeline(&self.sc.up.kernel, "media_composite"));- // Raster primary: this frame's jitter (Halton 2, 3) for the visibility pass and every camera ray.- let jitter = (halton(self.frame % 16 + 1, 2), halton(self.frame % 16 + 1, 3));+ // This frame's sub-pixel jitter (Halton 2, 3), every camera ray's and the raster visibility pass's: 8 phases at+ // full resolution, as many more as an output pixel has fewer traced ones (8 / scale^2: the upscaler needs each+ // output pixel visited), in the 12 bits a side the samples carry it in.+ let phases = ((8.0 * (w as f32 / iw as f32) * (h as f32 / ih as f32)).round() as u32).clamp(8, 512);+ let quant = |v: f32| (v * 4095.0).round() / 4095.0;+ let jitter = (quant(halton(self.frame % phases + 1, 2)), quant(halton(self.frame % phases + 1, 3)));for k in 0..spp.max(1) {let mut p = self.sc.base;// Software traversal's units as edits left them: the top tree's root and how many it holds (trace_units.wgsl).if let Some(u) = &self.sc.up.units {(p.motion[2], p.motion[3]) = (f32::from_bits(u.layout.top_first), f32::from_bits(u.count));}- if self.raster.is_some() && !self.sc.instanced {- p.raster = [1.0, jitter.0, jitter.1, 0.0];- }+ p.raster = [(self.raster.is_some() && !self.sc.instanced) as u32 as f32, jitter.0, jitter.1, 0.0];p.raster[3] = if self.rays { 0.0 } else { 1.0 };if self.restir {let pc = self.prev_cam;@@ -3918,15 +3953,17 @@ impl LiveCore {bt: [t[0], t[1], t[2], t[3]],bt2: [t[4], t[5], 0.0, 0.0],dof: dof_params(self.sc.dof.as_ref(), cam.tan_half_fov, w, h),+ jit: [jitter.0, jitter.1, 0.0, 0.0],};g.queue.write_buffer(&self.targets.params, 0, bytemuck::bytes_of(&lp));let bg = &self.sc.bgs[(self.frame % 2) as usize];- // Translucent particles: drawn over the composed image right after the specular filter composes it (into fb when- // depth of field / motion blur follow, else fa; live.wgsl spec_filter), then their volume voxels trained.+ // Translucent particles: drawn over the upscaled image right after `upscale` (in sb when depth of field / motion+ // blur follow, else fa: they have no motion vectors, so they are no part of the colour history; UE draws its+ // translucency after the upscaler the same way), then their volume voxels trained.// (Nothing to draw while every translucent particle system is hidden in place.)let tl = self.sc.up.translucent.as_ref().filter(|_| g.translucent_particles_live(&self.sc.up)).map(|t| {let pipes = g.translucent_pipelines(&self.sc.up.kernel, true);- let target = if lp.misc2[2] > 0.0 || lp.dof[0] > 0.0 { &self.targets.fb.buffer } else { &self.targets.fa.buffer };+ let target = if lp.misc2[2] > 0.0 || lp.dof[0] > 0.0 { &self.targets.sb.buffer } else { &self.targets.fa.buffer };// Below full resolution the particles are drawn per traced pixel, then laid over the output (`translucent_up`).let low = (iw, ih) != (w, h);let tp = gpu::TranslucentParams { size: [w, h, t.first, 1], view: [iw, ih, gpu::VOLUME_TRAIN_CAP, low as u32], misc: [VOLUME_TRAIN_SHARE, translucent_hidden(self.frame) as u32 as f32, 0.0, 0.0] };@@ -3976,7 +4013,7 @@ impl LiveCore {let (gw, gh) = if *name == "exposure" { (8, 8) } else if *name == "dof_tiles" { (w.div_ceil(16), h.div_ceil(16)) } else if traced_pass(name) { (iw, ih) } else if name.starts_with("bloom") || name.starts_with("lum_") { (w.div_ceil(4), h.div_ceil(4)) } else { (w, h) };pass.dispatch_workgroups(gw.div_ceil(8), gh.div_ceil(8), 1);drop(pass);- if *name == "spec2" && tl.is_some() {+ if *name == "upscale" && tl.is_some() {for (j, train) in [(0u32, false), (1, true)] {let q = (self.passes.pipes.len() as u32 + 4 + j) * 2;let mut pass = e.begin_compute_pass(&wgpu::ComputePassDescriptor { label: Some(if train { "vtrain" } else { "translucent" }), timestamp_writes: Some(wgpu::ComputePassTimestampWrites { query_set: &p.queries, beginning_of_pass_write_index: Some(q), end_of_pass_write_index: Some(q + 1) }) });@@ -3999,7 +4036,7 @@ impl LiveCore {}let (gw, gh) = if *name == "dof_tiles" { (w.div_ceil(16), h.div_ceil(16)) } else if traced_pass(name) { (iw, ih) } else if name.starts_with("bloom") || name.starts_with("lum_") { (w.div_ceil(4), h.div_ceil(4)) } else { (w, h) };pass.dispatch_workgroups(gw.div_ceil(8), gh.div_ceil(8), 1);- if *name == "spec2" {+ if *name == "upscale" {tl_dispatch(&mut pass, false);tl_dispatch(&mut pass, true);}@@ -4075,6 +4112,7 @@ impl LiveCore {}}self.prev_cam = cam;+ self.shown_spp = spp.max(1);self.restir_prev = Some((iw, ih));self.prev_isz = (iw, ih);self.reset = false;@@ -4097,20 +4135,21 @@ impl LiveCore {}#[cfg(not(target_arch = "wasm32"))]- /// The last frame's signals apart, linear RGB f32 per output pixel: diffuse (the composed image less the rest, so+ /// The last frame's signals apart, linear RGB f32 per output pixel: diffuse (the composed colour less the rest, so/// with the sun's direct light, which view mode 6 leaves out), specular (the specular denoiser's output: mode 7) and- /// emission (what the camera saw directly). Without motion blur or depth of field they add up to- /// `read_hdr`. Waits for the GPU.+ /// emission (what the camera ray saw directly). They are the render resolution's (the frame as `compose` hands it to+ /// the upscaler), each output pixel taking the traced sample under it; not with depth of field and motion blur both+ /// on (the composed colour's buffer is theirs then). Waits for the GPU.pub fn read_signals(&self) -> Result<[Vec<f32>; 3]> {let d = &self.g.device;- let n = self.w as u64 * self.h as u64;- // The frame just rendered wrote the ping-pong histories at index frame % 2 (`bind_groups`: frame f writes 1 - f % 2).- let k = (self.frame % 2) as usize;+ let (w, h) = (self.w as usize, self.h as usize);+ let (iw, ih) = (self.prev_isz.0 as usize, self.prev_isz.1 as usize);+ let (n, tn) = ((w * h) as u64, (iw * ih) as u64);let t = &self.targets;- let read = |buf: &Buf, offset: u64, size: u64| -> Result<Vec<u8>> {+ let read = |buffer: &wgpu::Buffer, offset: u64, size: u64| -> Result<Vec<u8>> {let r = d.create_buffer(&wgpu::BufferDescriptor { label: Some("signals read"), size, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false });let mut e = d.create_command_encoder(&Default::default());- e.copy_buffer_to_buffer(&buf.buffer, buf.offset + offset, &r, 0, size);+ e.copy_buffer_to_buffer(buffer, offset, &r, 0, size);self.g.queue.submit([e.finish()]);r.slice(..).map_async(wgpu::MapMode::Read, |_| {});d.poll(wgpu::PollType::wait_indefinitely())?;@@ -4118,18 +4157,23 @@ impl LiveCore {Ok(v)};let half = |b: u32, hi: bool| half::f16::from_bits(if hi { (b >> 16) as u16 } else { b as u16 }).to_f32();- let fa = read(&t.fa, 0, n * 16)?;- let spec = read(&t.sa, n * 8, n * 8)?; // spec_pp slot 1: (rgb, history) as two pairs of f16- let ae = read(&t.alb[k], 0, n * 16)?; // albedo, frames, emission, roughness (f16): emission in z, w.lo- let (fa, spec, ae): (&[[f32; 4]], &[[u32; 2]], &[[u32; 4]]) = (bytemuck::cast_slice(&fa), bytemuck::cast_slice(&spec), bytemuck::cast_slice(&ae));- let (mut dif, mut spe, mut emi) = (Vec::with_capacity(n as usize * 3), Vec::with_capacity(n as usize * 3), Vec::with_capacity(n as usize * 3));- for i in 0..n as usize {- let s = [half(spec[i][0], false), half(spec[i][0], true), half(spec[i][1], false)];- let e = [half(ae[i][2], false), half(ae[i][2], true), half(ae[i][3], false)];- for c in 0..3 {- spe.push(s[c]);- emi.push(e[c]);- dif.push((fa[i][c] - s[c] - e[c]).max(0.0));+ let composed = read(&t.fb.buffer, t.fb.offset, tn * 16)?;+ let spec = read(&t.sa.buffer, t.sa.offset + n * 8, tn * 8)?; // spec_pp slot 1: (rgb, history) as two pairs of f16+ let samples = read(&self.sc.up.accum, 0, tn * 80)?; // the kernel's samples (live.wgsl `Pixel`): emit at 48+ let (composed, spec, samples): (&[[f32; 4]], &[[u32; 2]], &[[f32; 20]]) = (bytemuck::cast_slice(&composed), bytemuck::cast_slice(&spec), bytemuck::cast_slice(&samples));+ let (mut dif, mut spe, mut emi) = (Vec::with_capacity(w * h * 3), Vec::with_capacity(w * h * 3), Vec::with_capacity(w * h * 3));+ let finite = |v: f32| if v.is_finite() { v } else { 0.0 };+ for y in 0..h {+ for x in 0..w {+ // live.wgsl `sample_under`.+ let q = (((y as f32 + 0.5) * ih as f32 / h as f32) as usize).min(ih - 1) * iw + (((x as f32 + 0.5) * iw as f32 / w as f32) as usize).min(iw - 1);+ let s = [half(spec[q][0], false), half(spec[q][0], true), half(spec[q][1], false)];+ for c in 0..3 {+ let e = finite(samples[q][12 + c] / self.shown_spp as f32);+ spe.push(s[c]);+ emi.push(e);+ dif.push((composed[q][c] - s[c] - e).max(0.0));+ }}}Ok([dif, spe, emi])@@ -4306,8 +4350,10 @@ pub fn run(presets: &Path, opts: LiveOpts) -> Result<()> {// Frame-script cost (script + commit), reported every 2 s.let (mut fs_sum, mut fs_max, mut fs_n) = (0.0f32, 0.0f32, 0u32);core.bounces = opts.bounces;- core.render_scale = opts.scale.unwrap_or(1.0);- core.dynamic_res = opts.scale.is_none();+ core.set_display_scale(opts.display_scale);+ if let Some(s) = opts.scale {+ core.set_scale(s);+ }eprintln!("live: load {}", serde_json::Value::Object(core.load_timings().iter().map(|(k, v)| (k.clone(), (*v).into())).collect()));eprintln!("live: {}x{} @{} fps on {} ({})", w, h, opts.fps, core.g.adapter_info.name, if core.g.hwrt { "HW RT" } else { "BVH" });let staging = core.g.device.create_buffer(&wgpu::BufferDescriptor { label: Some("snapshot"), size: (w * h * 4) as u64, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false });@@ -4564,6 +4610,7 @@ pub fn parse_opts(args: &[String]) -> Result<LiveOpts> {assets: PathBuf::from(flag("--assets").unwrap_or_else(|| ".".into())),room: flag("--room").unwrap_or_else(|| "local".into()),frame_log: flag("--frame-log").map(PathBuf::from),+ display_scale: flag("--display-scale").map(|v| v.parse::<f32>()).transpose()?.unwrap_or(1.0),})}diff --git a/renderer/src/view.rs b/renderer/src/view.rsindex d88664e..c69ad9a 100644--- a/renderer/src/view.rs+++ b/renderer/src/view.rs@@ -12,7 +12,7 @@//! stdout JSON lines: {"type":"ready"} | {"type":"click","u":..,"v":..,"cam":{..},"seq":n} | {"type":"key","key":"L"}use crate::gpu;-use crate::live::{next_spp, parse_cmd, parse_scale, save_bgra, Camera, Cmd, LiveCore, MIN_SCALE};+use crate::live::{next_spp, parse_cmd, parse_scale, save_bgra, Camera, Cmd, LiveCore};use crate::script::GameSession;use anyhow::Result;use glam::Vec3;@@ -129,10 +129,15 @@ impl App {color_space: Default::default(),};surface.configure(&g.device, &config);- let (game, mut core) = GameSession::start(&a.bundle, &a.room, &a.assets, g, &a.presets, a.width, a.height)?;+ // The view is the window's physical pixels (`--width` / `--height` are its size in points): the temporal upscaler+ // resolves to the display, and nothing is stretched after it.+ let (vw, vh) = (config.width, config.height);+ let (game, mut core) = GameSession::start(&a.bundle, &a.room, &a.assets, g, &a.presets, vw, vh)?;+ core.set_display_scale(window.scale_factor() as f32);core.bounces = a.bounces;- core.render_scale = a.scale.unwrap_or(1.0);- core.dynamic_res = a.scale.is_none();+ if let Some(s) = a.scale {+ core.set_scale(s);+ }let d = &core.g.device;let module = d.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("blit"), source: wgpu::ShaderSource::Wgsl(BLIT.into()) });let layout = d.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {@@ -149,7 +154,7 @@ impl App {label: Some("blit"),layout: Some(&pl),vertex: wgpu::VertexState { module: &module, entry_point: Some("vs"), buffers: &[], compilation_options: Default::default() },- fragment: Some(wgpu::FragmentState { module: &module, entry_point: Some("fs"), targets: &[Some(format.into())], compilation_options: wgpu::PipelineCompilationOptions { constants: &[("W", a.width as f64), ("H", a.height as f64), ("SW", config.width as f64), ("SH", config.height as f64)], ..Default::default() } }),+ fragment: Some(wgpu::FragmentState { module: &module, entry_point: Some("fs"), targets: &[Some(format.into())], compilation_options: wgpu::PipelineCompilationOptions { constants: &[("W", vw as f64), ("H", vh as f64), ("SW", config.width as f64), ("SH", config.height as f64)], ..Default::default() } }),primitive: Default::default(),depth_stencil: None,multisample: Default::default(),@@ -159,7 +164,7 @@ impl App {let blit_bg = d.create_bind_group(&wgpu::BindGroupDescriptor { label: None, layout: &layout, entries: &[wgpu::BindGroupEntry { binding: 0, resource: core.targets.out.as_entire_binding() }] });let cam = core.scene_camera();let home = Orbit::from_camera(&cam);- eprintln!("view: {}x{} on {} ({}), {:?}", a.width, a.height, core.g.adapter_info.name, if core.g.hwrt { "HW RT" } else { "BVH" }, format);+ eprintln!("view: {}x{} ({}x{} points) on {} ({}), {:?}", vw, vh, a.width, a.height, core.g.adapter_info.name, if core.g.hwrt { "HW RT" } else { "BVH" }, format);emit(serde_json::json!({ "type": "ready", "adapter": core.g.adapter_info.name, "hwrt": core.g.hwrt }));self.state = Some(State {window,@@ -416,7 +421,7 @@ fn frame(s: &mut State, cmds: &mpsc::Receiver<Cmd>, fps: u32) -> Result<()> {s.spp = if s.core.render_scale < 1.0 { 1 } else { next_spp(s.spp, s.core.gpu_ms, fps).min(s.spp_cap) };if s.core.frame % 30 == 1 {let budget = 1000.0 / fps as f32;- let lowest = !s.core.dynamic_res || s.core.render_scale <= MIN_SCALE;+ let lowest = !s.core.dynamic_res || s.core.render_scale <= s.core.min_scale;if s.spp == 1 && lowest && s.core.gpu_ms > budget * 0.92 && s.core.bounces > 2 {s.core.bounces -= 1;} else if s.core.gpu_ms < budget * 0.55 && s.core.bounces < s.max_bounces {@@ -502,7 +507,7 @@ fn texel(x: i32, y: i32) -> vec4<f32> {}@fragment fn fs(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {- // Bilinear upscale from the internal resolution to the (Retina) window.+ // The view's pixels are the window's (1 : 1); bilinear if the window moved to a display of another density.let dims = vec2<f32>(W, H);let uv = p.xy / vec2<f32>(SW, SH);let q = uv * dims - 0.5;