From 1cadd633872c072d2aae490476447958eb72f69e Mon Sep 17 00:00:00 2001 From: chexit Date: Sat, 21 Nov 2026 09:33:31 +0300 Subject: [PATCH] Order the DLSS frame so consumers read the current resolved frame (#137) The HD film/tonemap/final-frame passes were recorded into the first command buffer and read `taa.target` before NGX wrote the DLSS result into it: the submit was `once(world).chain([NGX, delinearize])`, so the first frame read the target's initial contents and later frames read the previous result. Successful DLSS init in the log did not mean the current frame was shown. Finish the world encoder (world + display->linear) right after `Hd::taa` records the DLSS input, then record film, bloom, the tone curve, the hand, the HUD, capture and present into a fresh encoder. One `queue.submit` of [world+linearize, NGX, delinearize, rest] keeps that GPU order with no CPU sync; `submission_order` builds the list and stays the single-buffer path when DLSS is off. Staging-belt recall, GPU/HD pass timing and the capture readback all stay within the one submission, so they keep working across the split (HD pass timestamps resolve in a later frame, as before). Track native and NGX history readiness separately (`HistoryReady`): a successful DLSS frame no longer marks the native history usable, so the first fallback frame after DLSS starts over instead of blending a stale or empty native texture; a native frame resets NGX in turn. A cut/teleport, resize or TAA off clears both. `HdFrame.taa.w` now reflects native history usability, so film motion blur sits out DLSS frames rather than reproject a history it does not own. Narrow tests: the submission order, and the native/DLSS history lifecycle including the first fallback frame and reset. The DLSS-gated tests and the real-RTX acceptance (docs/DLSS-TESTING.md) remain pending on Windows/Linux with an RTX; no RTX is available here, so this is verified by the ordering and history unit tests plus the native suite, not a DLSS capture. Co-Authored-By: Kimi Code --- crates/horizon-client/src/gfx.rs | 71 +++++++++++-- crates/horizon-client/src/gfx/hd/film.rs | 4 +- crates/horizon-client/src/gfx/hd/frame.rs | 4 +- crates/horizon-client/src/gfx/hd/mod.rs | 4 +- crates/horizon-client/src/gfx/hd/taa.rs | 121 ++++++++++++++++++++-- 5 files changed, 181 insertions(+), 23 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 62d0cdfb9..c9db2b0e2 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -177,6 +177,22 @@ fn next_block_entity_revision() -> u64 { NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) } +/// The frame's command buffers in the order the GPU must run them. Without DLSS the whole +/// frame is one buffer (`rest`). With DLSS the world and the display→linear conversion are +/// finished into `before`, NGX evaluates in its own buffer and the linear→display conversion +/// follows it (`temporal`, in that order); everything that reads the resolved frame — film, +/// bloom, the tone curve, the hand, the HUD, capture and present — is recorded into a fresh +/// encoder finished into `rest`. One `queue.submit` of `before`, `temporal`, `rest` keeps that +/// order on the GPU with no CPU wait (#137): a consumer placed in `before` would read +/// `taa.target` before NGX writes it. +fn submission_order(before: Option, temporal: Vec, rest: T) -> Vec { + before + .into_iter() + .chain(temporal) + .chain(std::iter::once(rest)) + .collect() +} + #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct Globals { @@ -4323,6 +4339,9 @@ impl Renderer { }) }); let mut temporal_commands = Vec::new(); + // The world (and DLSS's linearize) finished into its own buffer when DLSS evaluates; + // see `submission_order`. + let mut dlss_before: Option = None; if self.animated_textures { self.animate(&mut encoder, frame.ticks); self.hud @@ -6331,7 +6350,7 @@ impl Renderer { &mut encoder, (&self.lightmap_view, &self.lightmap_sampler), ); - match &mut self.mod_post { + let source = match &mut self.mod_post { Some(post) if mod_post => { let size = self.world_post.size().unwrap_or([1, 1]); post.write( @@ -6342,16 +6361,22 @@ impl Renderer { self.mod_params.1, ); post.run(&self.device, &mut encoder, (hd.lit_view(), depth), &hd.fx); - let source = hd.taa(&self.device, &self.adapter, &mut encoder, &mut temporal_commands, hd::HdSource::Fx, depth); - let source = hd.film(&self.device, &mut encoder, source, depth); - hd.finish(&self.device, &mut encoder, source, &view_target); - } - _ => { - let source = hd.taa(&self.device, &self.adapter, &mut encoder, &mut temporal_commands, hd::HdSource::Lit, depth); - let source = hd.film(&self.device, &mut encoder, source, depth); - hd.finish(&self.device, &mut encoder, source, &view_target); + hd.taa(&self.device, &self.adapter, &mut encoder, &mut temporal_commands, hd::HdSource::Fx, depth) } + _ => hd.taa(&self.device, &self.adapter, &mut encoder, &mut temporal_commands, hd::HdSource::Lit, depth), + }; + // DLSS evaluates in its own command buffer between the world and every pass that + // reads the resolved frame. Finish the world (and the linearize) now, then record + // film, bloom, the tone curve, the hand, the HUD, capture and present into a fresh + // encoder submitted after NGX and the delinearize — one submit, no CPU wait (#137). + if !temporal_commands.is_empty() { + dlss_before = Some(encoder.finish()); + encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("Horizon frame after DLSS"), + }); } + let source = hd.film(&self.device, &mut encoder, source, depth); + hd.finish(&self.device, &mut encoder, source, &view_target); } else if let Some((color, depth)) = &scaled && mod_post && let Some(post) = &mut self.mod_post @@ -6535,7 +6560,7 @@ impl Renderer { self.first_frame = FirstFrame::Drawn; } let submission = crate::spans::time("submit", || { - self.queue.submit(std::iter::once(commands).chain(temporal_commands)) + self.queue.submit(submission_order(dlss_before, temporal_commands, commands)) }); if let Some(cull) = &mut self.cull { cull.submitted(); @@ -6905,6 +6930,32 @@ fn frustum(m: Mat4) -> [Vec4; 6] { #[cfg(test)] mod tests { + /// #137: the GPU must run the world (and DLSS's linearize) before NGX, and every consumer of + /// the resolved frame after the delinearize. Without DLSS the frame stays a single buffer. + #[test] + fn dlss_submission_orders_consumers_after_ngx() { + use super::submission_order; + assert_eq!(submission_order(None, vec![], "frame"), vec!["frame"]); + let order = submission_order( + Some("world+linearize"), + vec!["ngx", "delinearize"], + "film/tonemap/ui/capture/present", + ); + assert_eq!( + order, + vec![ + "world+linearize", + "ngx", + "delinearize", + "film/tonemap/ui/capture/present" + ] + ); + let at = |name: &str| order.iter().position(|s| *s == name).unwrap(); + assert!(at("world+linearize") < at("ngx")); + assert!(at("ngx") < at("delinearize")); + assert!(at("delinearize") < at("film/tonemap/ui/capture/present")); + } + #[test] fn surface_is_8_bit_and_not_srgb() { use super::surface_format; diff --git a/crates/horizon-client/src/gfx/hd/film.rs b/crates/horizon-client/src/gfx/hd/film.rs index 89413af41..15b428046 100644 --- a/crates/horizon-client/src/gfx/hd/film.rs +++ b/crates/horizon-client/src/gfx/hd/film.rs @@ -6,7 +6,9 @@ //! size) and the world depth (reversed-Z; world size, equal to the output's in film quality), //! plus `HdFrame`: `film` (x: motion blur on; y: depth of field on; z: focus distance in blocks, //! 0 = automatic; w: depth of field strength), `inverse_view_proj`, `prev_view_proj` and `taa.xyz` -//! (the camera's move), `taa.w` (the previous frame is usable: not after a cut or a teleport). +//! (the camera's move), `taa.w` (the native history is usable: not after a cut or a teleport, +//! and 0 while DLSS resolves — NGX does not fill the native history, so motion blur sits out +//! those frames rather than reproject a history it does not own, #137). //! - Writes `motion` (`fs_motion`: a blur along the camera's motion on screen, rebuilt from depth; //! things that move by themselves are not blurred) and then `dof` (`fs_dof`: a disc gather whose //! radius follows the circle of confusion of each pixel's distance from the focus). Either pass diff --git a/crates/horizon-client/src/gfx/hd/frame.rs b/crates/horizon-client/src/gfx/hd/frame.rs index d61cbfc4f..c48779898 100644 --- a/crates/horizon-client/src/gfx/hd/frame.rs +++ b/crates/horizon-client/src/gfx/hd/frame.rs @@ -80,7 +80,9 @@ pub(super) struct HdFrame { pub(super) shadow: [f32; 4], /// The previous frame's camera-relative view-projection without jitter (for TAA). pub(super) prev_view_proj: [[f32; 4]; 4], - /// xyz: how far the camera moved since that frame; w: 1 when the history is usable. + /// xyz: how far the camera moved since that frame; w: 1 when the *native* TAA history is + /// usable (0 after a cut/teleport/resize, and while DLSS resolves — NGX keeps its own history + /// and does not fill the native textures, #137). pub(super) taa: [f32; 4], /// x: the history's weight; y: the sharpening (0 without TAA); zw: the jitter in world pixels. pub(super) taa_params: [f32; 4], diff --git a/crates/horizon-client/src/gfx/hd/mod.rs b/crates/horizon-client/src/gfx/hd/mod.rs index 6657d5784..37215f17b 100644 --- a/crates/horizon-client/src/gfx/hd/mod.rs +++ b/crates/horizon-client/src/gfx/hd/mod.rs @@ -587,7 +587,7 @@ impl Hd { } // A long jump of the camera (a teleport) starts the history over. if !self.taa.active || scene.camera_delta.length() > 64.0 { - self.taa.history_ready = false; + self.taa.history_ready.reset(); } let sun_height = scene.sun.normalize_or(glam::Vec3::Y).y; self.sky.stale = (sun_height - self.sky.drawn_for).abs() > 0.001; @@ -614,7 +614,7 @@ impl Hd { scene.camera_delta.x, scene.camera_delta.y, scene.camera_delta.z, - if self.taa.history_ready { 1.0 } else { 0.0 }, + if self.taa.history_ready.native_usable() { 1.0 } else { 0.0 }, ], view_proj: scene.view_proj.to_cols_array_2d(), water: [ diff --git a/crates/horizon-client/src/gfx/hd/taa.rs b/crates/horizon-client/src/gfx/hd/taa.rs index cf042df78..50a21eb18 100644 --- a/crates/horizon-client/src/gfx/hd/taa.rs +++ b/crates/horizon-client/src/gfx/hd/taa.rs @@ -8,8 +8,8 @@ //! motion/depth. Matching opaque pixels use explicit current-to-previous render-pixel motion; //! other pixels keep camera/depth reprojection. `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 history usable), `taa_params` -//! (x history weight, y sharpening, zw jitter in world pixels). +//! 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). //! - Writes `target` (the resolved frame, output size) and the next history together (two //! targets); history alpha holds the camera distance for disocclusion. The histories ping-pong //! (`flip`); a teleport > 64 blocks, `resize` or TAA off starts them over. @@ -17,7 +17,10 @@ //! 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. -//! `submissions` is ordered world+linearize, NGX evaluate, delinearize; native TAA is the fallback. +//! `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 +//! native history unusable (`HistoryReady`) so the first fallback frame does not read it stale. use super::{ DEPTH, FLOAT, HDR, Hd, HdSource, SAMPLER, UNFILTERED, UNIFORM, entries, module, stamp, }; @@ -114,11 +117,62 @@ pub fn jitter(frame: u32, size: [u32; 2], count: u32) -> [f32; 2] { ] } +/// Which temporal history is ready to read next frame. The native TAA pass writes its two +/// history textures; DLSS (NGX) keeps its own history inside the SDK and never fills them. The +/// readiness of the two is tracked separately (#137): a successful DLSS frame must not leave the +/// *native* history looking usable, or the first fallback frame blends a stale or empty texture; +/// and a native frame leaves NGX's history stale, so DLSS resets when it resumes. A cut, a +/// teleport, a resize or TAA off clears both. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(super) struct HistoryReady { + native: bool, + #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] + dlss: bool, +} + +impl HistoryReady { + /// Neither history is usable: the start, a cut/teleport, a resize, or TAA off. + pub(super) fn reset(&mut self) { + self.native = false; + #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] + { + self.dlss = false; + } + } + + /// The native pass wrote its history textures; NGX's history is now stale. + pub(super) fn after_native(&mut self) { + self.native = true; + #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] + { + self.dlss = false; + } + } + + /// Whether the native history textures may be blended (drives `HdFrame.taa.w`). + pub(super) fn native_usable(&self) -> bool { + self.native + } + + /// NGX resolved the frame into its own history; the native textures were not written. + #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] + pub(super) fn after_dlss(&mut self) { + self.dlss = true; + self.native = false; + } + + /// Whether NGX must reset its history this frame (the first, or after native frames/a cut). + #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] + pub(super) fn dlss_reset(&self) -> bool { + !self.dlss + } +} + pub(super) struct Taa { /// The resolved frame (output size), and the two histories (one read, one written, alternating). pub(super) target: wgpu::TextureView, history: [wgpu::TextureView; 2], - pub(super) history_ready: bool, + pub(super) history_ready: HistoryReady, history_flip: usize, /// This frame is anti-aliased (set by `write`). pub(super) active: bool, @@ -186,7 +240,7 @@ impl Taa { super::target(device, "HD TAA history", [1, 1], HDR).view, super::target(device, "HD TAA history", [1, 1], HDR).view, ], - history_ready: false, + history_ready: HistoryReady::default(), history_flip: 0, active: false, #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] @@ -213,7 +267,7 @@ impl Taa { super::target(device, "HD TAA history", out, HDR).view, super::target(device, "HD TAA history", out, HDR).view, ]; - self.history_ready = false; + self.history_ready.reset(); self.groups = None; #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] if let Some(dlss) = &mut self.dlss { @@ -250,7 +304,7 @@ impl Hd { let mut after = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("HD DLSS output conversion"), }); - let reset = !self.taa.history_ready; + let reset = self.taa.history_ready.dlss_reset(); let result = self.taa.dlss.as_mut().unwrap().render( device, adapter, @@ -268,7 +322,7 @@ impl Hd { Ok(commands) => { submissions.push(commands); submissions.push(after.finish()); - self.taa.history_ready = true; + self.taa.history_ready.after_dlss(); return HdSource::Taa; } Err(e) => { @@ -330,7 +384,7 @@ impl Hd { pass.draw(0..3, 0..1); drop(pass); self.taa.history_flip = write; - self.taa.history_ready = true; + self.taa.history_ready.after_native(); HdSource::Taa } } @@ -399,4 +453,53 @@ mod tests { .collect(); assert_eq!(seen.len(), count as usize); } + + /// The native history starts unusable, becomes usable once the native pass writes it, and a + /// cut/teleport/resize/TAA-off (`reset`) starts it over. This is what drives `HdFrame.taa.w`. + #[test] + fn native_history_readiness_lifecycle() { + let mut h = HistoryReady::default(); + assert!(!h.native_usable(), "the first frame has no history to blend"); + h.after_native(); + assert!(h.native_usable()); + h.after_native(); + assert!(h.native_usable(), "consecutive native frames keep the history"); + h.reset(); + assert!(!h.native_usable(), "a cut/resize starts the history over"); + } + + /// #137: a successful DLSS frame keeps its history in NGX, not in the native textures, so it + /// must leave the native history unusable — the first fallback frame then starts over instead + /// of blending a stale or empty texture. A native frame leaves NGX's history stale in turn. + #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] + #[test] + fn dlss_frames_do_not_make_the_native_history_usable() { + let mut h = HistoryReady::default(); + assert!(h.dlss_reset(), "the first DLSS frame resets NGX"); + h.after_dlss(); + assert!(!h.native_usable(), "NGX did not fill the native history textures"); + assert!(!h.dlss_reset(), "consecutive DLSS frames keep NGX's history"); + h.after_dlss(); + assert!(!h.native_usable()); + // The first fallback frame: the native history is still unusable, so it does not blend. + assert!(!h.native_usable()); + h.after_native(); + assert!(h.native_usable()); + assert!(h.dlss_reset(), "NGX's history is stale after native frames"); + } + + /// #137: a cut/teleport, a resize or TAA off clears both histories, whichever backend ran. + #[cfg(all(feature = "dlss", any(windows, target_os = "linux")))] + #[test] + fn reset_clears_both_histories() { + let mut h = HistoryReady::default(); + h.after_dlss(); + h.reset(); + assert!(!h.native_usable()); + assert!(h.dlss_reset()); + h.after_native(); + h.reset(); + assert!(!h.native_usable()); + assert!(h.dlss_reset()); + } }