From 26ee82398dee6cdb5c7a657eba8f5cd2edcbe688 Mon Sep 17 00:00:00 2001 From: chexit Date: Sat, 21 Nov 2026 16:50:34 +0300 Subject: [PATCH] Native TAA: a reactive mask for water, and the wind-motion contract test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Water animates in place — the waves tilt the normals, so the refraction, the SSR/sky reflection and the sun's glint move with no geometric motion to reproject by — and its history conflicts with the frame instead of steadying it: the shimmer most visible on water. The TAA passes now read the water's own depth (binding 7) and trust the history less where water animates fastest: near the camera (the fine ripples and sparkles change every frame up close; far water keeps only the slow waves, the fine ones fade out by footprint, so it keeps the full weight). The cap is 0.8 for fs_taa and 0.7 for fs_taau, faded out to 96 blocks; no water this frame means no change, and the GPU A/B smoke (no water in its scene) resolves bit-identically. Also pins the wind motion contract numerically: a wind-blown plant's previous position is the same vertex at the previous frame's wind time and strength, and the camera-only decode misses by the wind's move. Co-Authored-By: Kimi Code --- crates/horizon-client/src/gfx/hd/taa.rs | 93 +++++++++++++++++++++-- crates/horizon-client/src/gfx/hd/taa.wgsl | 20 ++++- 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/gfx/hd/taa.rs b/crates/horizon-client/src/gfx/hd/taa.rs index 2c76a0a6e..46d6eb8b4 100644 --- a/crates/horizon-client/src/gfx/hd/taa.rs +++ b/crates/horizon-client/src/gfx/hd/taa.rs @@ -4,12 +4,13 @@ //! Contract: //! - Reads: the colour source (`HdSource::Lit`/`Fx`, the lit world with water, or the mod's post //! output; display space, world size), the *world* depth (reversed-Z, the pass's own depth from the -//! world pass, not the G-buffer's), the history (output size, `Rgba16Float`), and the G-buffer's -//! motion/depth. The motion buffer is current→previous in *jittered* render pixels (a still +//! world pass, not the G-buffer's), the history (output size, `Rgba16Float`), the G-buffer's +//! motion/depth, and the water's own depth (the reactive mask). The motion buffer is current→previous in *jittered* render pixels (a still //! surface carries the two frames' jitter difference); matching opaque pixels decode it for //! their history grid — `fs_taa` as written (its history is on the jittered grid), `fs_taau` //! minus the jitter difference (its history is on the stable output grid) — and other pixels -//! keep camera/depth reprojection. `HdFrame`: +//! keep camera/depth reprojection. Water pixels are reactive (the water depth marks them): +//! their colour animates without geometric motion, so near water keeps less history. `HdFrame`: //! `prev_view_proj` (last frame's matrix; **with** jitter for `fs_taa`, **without** for `fs_taau`, //! chosen in `Hd::write`), `taa` (xyz the camera's move since, w the *native* history usable), //! `taa_params` (x history weight, y sharpening, zw jitter in world pixels), `prev_jitter` @@ -21,6 +22,9 @@ //! draws; the public matrix stays unjittered. The hand and the HUD are drawn after tonemap. //! - A history belongs to one camera and one output size: do not feed one history from two cameras. //! - Optional DLSS: the same inputs go through display→linear conversion, NGX, then linear→display. +//! The motion buffer goes as written (jittered render pixels, current→previous) with the +//! `MVJittered` create flag and the current jitter in render pixels per evaluate, so NGX undoes +//! the jitter difference itself (DLSS Programming Guide §3.6.2/§3.7.3; `dlss.rs`). //! `submissions` is ordered world+linearize, NGX evaluate, delinearize; the renderer finishes the //! world encoder before NGX and records every consumer of the resolved frame after the //! delinearize, all in one submit (#137). Native TAA is the fallback; a DLSS frame leaves the @@ -37,9 +41,10 @@ use super::dlss; use std::sync::{Arc, Mutex}; pub(super) const TAA: &str = include_str!("taa.wgsl"); -/// Group 0 of `fs_taa` / `fs_taau`: frame, colour, world depth, history, sampler, motion and G-buffer depth. -pub(super) const TAA_BINDINGS: [wgpu::BindingType; 7] = - [UNIFORM, UNFILTERED, DEPTH, FLOAT, SAMPLER, UNFILTERED, DEPTH]; +/// Group 0 of `fs_taa` / `fs_taau`: frame, colour, world depth, history, sampler, motion, G-buffer +/// depth and water depth (the reactive mask: water animates without geometric motion). +pub(super) const TAA_BINDINGS: [wgpu::BindingType; 8] = + [UNIFORM, UNFILTERED, DEPTH, FLOAT, SAMPLER, UNFILTERED, DEPTH, DEPTH]; /// TAA is on unless `HORIZON_HD_TAA=0`. pub fn taa_enabled() -> bool { @@ -353,6 +358,7 @@ impl Hd { wgpu::BindingResource::Sampler(&self.sampler), wgpu::BindingResource::TextureView(&self.gbuffer.motion), wgpu::BindingResource::TextureView(&self.gbuffer.depth), + wgpu::BindingResource::TextureView(self.water.depth_view()), ], ) }); @@ -834,6 +840,68 @@ mod tests { } } + /// terrain.wgsl `wind_sway`: the sway at world xz and wind time t (whole cycles over 256 + /// blocks and 600 s). + fn wind_sway(w: [f32; 2], t: f32) -> Vec3 { + let tau = std::f32::consts::TAU; + let a1 = tau * (5.0 * w[0] + 3.0 * w[1]) / 256.0 + tau * 120.0 / 600.0 * t; + let a2 = tau * (-2.0 * w[0] + 7.0 * w[1]) / 256.0 + tau * 200.0 / 600.0 * t; + let a3 = tau * (23.0 * w[0] - 17.0 * w[1]) / 256.0 + tau * 350.0 / 600.0 * t; + let push = 0.45 + 0.35 * a1.sin() + 0.2 * a2.sin(); + let side = 0.25 * (a2 + 1.3).sin(); + Vec3::new( + 0.8 * push - 0.6 * side, + a3.sin(), + 0.6 * push + 0.8 * side, + ) + } + + /// A wind-blown plant carries the wind's own motion: its previous position is the same vertex + /// at the previous frame's wind time and strength (`Globals.hd_motion.zw`, terrain.wgsl's + /// `wind_offset_at`), so the vector lands on where the plant was drawn. The camera-only decode + /// (the previous position taken as the current one) misses by the wind's screen move. + #[test] + fn wind_motion_lands_on_the_previous_drawn_position() { + let low = [1280u32, 720]; + let out = [1920u32, 1080]; + let count = jitter_count(out[0] as f32 / low[0] as f32); + let unjittered = projection(); + // A plant's top (h = 1): the sway times 0.14 * strength, as wind_offset_at draws it. + let plant = |base: Vec3, t: f32, k: f32| { + let s = wind_sway([base.x, base.z], t); + let a = 0.14 * k; + base + Vec3::new(s.x * a, -(s.x * a + s.z * a).abs() * 0.25, s.z * a) + }; + let (t_prev, t_cur, k) = (10.0, 10.0 + 1.0 / 60.0, 1.0); + let mut moved = 0.0f32; + for frame in 1..=count * 2 { + let cur = drawn(unjittered, frame, low, count); + let prev = drawn(unjittered, frame - 1, low, count); + let jc = jitter_px(frame, low, count); + let jp = jitter_px(frame - 1, low, count); + for base in probe_points() { + let p = plant(base, t_cur, k); + let prev_pos = plant(base, t_prev, k); + moved = moved.max(px_err(uv_of(cur, p), uv_of(cur, prev_pos), out)); + let motion = motion_pixels(cur, prev, low, p, prev_pos); + let uv = taau_lookup(uv_of(unjittered, p), motion, low, jc, jp); + let expected = uv_of(unjittered, prev_pos); + assert!( + px_err(uv, expected, out) < 0.02, + "frame {frame} point {base:?}: wind lookup {uv:?} vs {expected:?}" + ); + // The old camera-only previous position misses by exactly the wind's own move. + let legacy_motion = motion_pixels(cur, prev, low, p, p); + let legacy = taau_lookup(uv_of(unjittered, p), legacy_motion, low, jc, jp); + let shift = px_err(uv_of(unjittered, p), uv_of(unjittered, prev_pos), out); + assert!((px_err(legacy, expected, out) - shift).abs() < 1e-3); + assert!(shift > 0.001, "frame {frame} point {base:?}: no wind move"); + } + } + // The fixture must actually move the plants on screen (near ones most). + assert!(moved > 0.05, "the wind barely moved the probes: {moved}"); + } + /// #143 A/B smoke on the real `fs_taau`: a still, finely patterned surface at a fractional /// render scale (1280x720 -> 1920x1080) must resolve to the same stable output pixel on every /// jitter phase. A is the old decode (the buffer as written), B the corrected one; a third @@ -947,6 +1015,12 @@ mod tests { wgpu::TextureFormat::Depth32Float, wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT, ); + let water_depth = tex( + "TAAU smoke water depth", + low, + wgpu::TextureFormat::Depth32Float, + wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT, + ); let motion = tex( "TAAU smoke motion", low, @@ -974,6 +1048,7 @@ mod tests { let source_view = source.create_view(&Default::default()); let depth_view = depth.create_view(&Default::default()); let gbuffer_view = gbuffer_depth.create_view(&Default::default()); + let water_view = water_depth.create_view(&Default::default()); let motion_view = motion.create_view(&Default::default()); let target_view = target.create_view(&Default::default()); // Depth textures take no buffer copies: fill them with a constant frag_depth pass. @@ -1054,6 +1129,8 @@ override DEPTH_VALUE: f32 = 1.0; queue.submit([encoder.finish()]); }; fill_depth(&depth_view, &depth_plane); + // No water in the smoke scene: the reactive mask stays off and the decode is judged alone. + fill_depth(&water_view, &depth_zero); // The pass's layout and production fs_taau pipeline (its own targets go unused). let taa = Taa::new(&device); @@ -1293,6 +1370,10 @@ override DEPTH_VALUE: f32 = 1.0; binding: 6, resource: wgpu::BindingResource::TextureView(&gbuffer_view), }, + wgpu::BindGroupEntry { + binding: 7, + resource: wgpu::BindingResource::TextureView(&water_view), + }, ], }); let mut encoder = device.create_command_encoder(&Default::default()); diff --git a/crates/horizon-client/src/gfx/hd/taa.wgsl b/crates/horizon-client/src/gfx/hd/taa.wgsl index a16e4e635..abe578245 100644 --- a/crates/horizon-client/src/gfx/hd/taa.wgsl +++ b/crates/horizon-client/src/gfx/hd/taa.wgsl @@ -10,6 +10,7 @@ struct TaaOut { @group(0) @binding(4) var linear: sampler; @group(0) @binding(5) var motion_tex: texture_2d; @group(0) @binding(6) var gbuffer_depth: texture_depth_2d; +@group(0) @binding(7) var water_depth: texture_depth_2d; // Opaque G-buffer geometry has an explicit current-to-previous vector, in jittered render pixels // (both frames' jitter included, so a still surface carries their difference). Each caller decodes @@ -23,6 +24,19 @@ fn history_motion(px: vec2, world_depth: f32) -> vec3 { return vec3(0.0); } +// Water animates in place — the waves tilt the normals, so the refraction, the reflection and the +// sun's glint move without any geometric motion to reproject by — and its history conflicts with +// the frame instead of steadying it (the shimmer). Trust the history less there, most near the +// camera: the fine ripples and sparkles change every frame up close, while far water keeps only +// the slow waves (the fine ones fade out by footprint), so it keeps the full weight. 0 where +// there is no water this frame; `dist` is the pixel's world-depth distance. +fn water_reactive(px: vec2, dist: f32) -> f32 { + if frame.water.x <= 0.5 || textureLoad(water_depth, px, 0) <= 0.0 { + return 0.0; + } + return clamp(1.0 - dist / 96.0, 0.0, 1.0); +} + override DEBUG_MOTION: bool = false; fn debug_motion(motion: vec3) -> vec4 { return vec4(abs(motion.xy) * 0.25, motion.z, 1.0); @@ -141,7 +155,8 @@ fn fs_taa(in: FullOut) -> TaaOut { let past = clip_to_box(box_lo, box_hi, to_ycocg(squash(history_color(uv)))); // Fast motion trusts the history less. let moved = length((uv - in.uv) * vec2(size)); - let keep = mix(frame.taa_params.x, frame.taa_params.x * 0.75, clamp(moved / 4.0, 0.0, 1.0)); + var keep = mix(frame.taa_params.x, frame.taa_params.x * 0.75, clamp(moved / 4.0, 0.0, 1.0)); + keep = mix(keep, min(keep, 0.8), water_reactive(px, dist)); resolved = mix(centre, past, keep); } } @@ -254,7 +269,8 @@ fn fs_taau(in: FullOut) -> TaaOut { let near = clamp(length(t - floor(t + 0.5)) / 0.7071, 0.0, 1.0); let fresh = (1.0 - frame.taa_params.x) * mix(1.8, 0.5, near); let moved = length((uv - in.uv) * high); - let keep = (1.0 - fresh) * mix(1.0, 0.8, clamp(moved / 4.0, 0.0, 1.0)); + var keep = (1.0 - fresh) * mix(1.0, 0.8, clamp(moved / 4.0, 0.0, 1.0)); + keep = mix(keep, min(keep, 0.7), water_reactive(nearest, dist)); resolved = mix(current, past, clamp(keep, 0.0, 0.98)); } }