From ecfe39d59474b8649adbb1b99e055315e1b52e77 Mon Sep 17 00:00:00 2001 From: Kal0s-design <356578040+Kal0s-design@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:51:56 +0100 Subject: [PATCH 1/2] d3d12-gpu: a sparse read of only holes keeps the staged texels' digest A partly mapped texture is read through its unmapped pages, which come back as zeros. The partial read path stored the digest of every read before rejecting an all-zero one, so a read that caught nothing mapped replaced the digest of the texels already detiled, and the next read that found those same texels detiled and uploaded them again. Last::accept_partial now rejects an all-zero read before touching the digest. The full read path is unchanged and still takes a texture that really is all zeros. After the fix in TaynanGT/Vice-Tay (26aeded). Co-Authored-By: Claude Opus 5.5 --- d3d12/crates/leonida-gpu/src/texture.rs | 42 ++++++++++++++++++++----- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/texture.rs b/d3d12/crates/leonida-gpu/src/texture.rs index 32ce3a4911..7460e5d89d 100644 --- a/d3d12/crates/leonida-gpu/src/texture.rs +++ b/d3d12/crates/leonida-gpu/src/texture.rs @@ -119,6 +119,24 @@ struct Last { quiet: u32, } +impl Last { + /// Whether a read of a partly mapped texture has texels to detile, its + /// digest then kept. A read that catches only holes (all zeros) changes + /// nothing: the digest of the texels staged stays, so a later read of + /// those same texels does not detile and upload them again. + fn accept_partial(&mut self, bytes: &[u8]) -> bool { + if bytes.iter().all(|&b| b == 0) { + return false; + } + let hash = digest(bytes); + if self.hash == Some(hash) { + return false; + } + self.hash = Some(hash); + true + } +} + /// Frames a guarded texture is hashed at every check from its first check, /// and after the hash found a change the guard did not see. const HOT_FRAMES: u64 = 64; @@ -2381,12 +2399,7 @@ impl Staging { crate::copystat::add(crate::copystat::C::TexHashed, size as u64); let changed = with_read(size, |read| { crate::host::copy_mapped(read.as_mut_ptr(), self.memory, size, false); - let hash = digest(read); - if last.hash == Some(hash) { - return false; - } - last.hash = Some(hash); - if read.iter().all(|&b| b == 0) { + if !last.accept_partial(read) { return false; } self.detile(read); @@ -5997,7 +6010,22 @@ mod backoff_tests { #[cfg(test)] mod partial_tests { - use super::partial_due; + use super::{Last, digest, partial_due}; + + /// A partly mapped texture read once with its texels, then through + /// holes only (zeros), keeps the digest of the texels staged: reading + /// the same texels again detiles nothing. + #[test] + fn a_zero_sparse_read_keeps_the_digest_of_the_staged_texels() { + let mut last = Last::default(); + let (holes, texels) = ([0u8; 128], [0x7au8; 128]); + assert!(!last.accept_partial(&holes)); + assert_eq!(last.hash, None, "nothing staged yet: the next read with texels detiles"); + assert!(last.accept_partial(&texels)); + assert!(!last.accept_partial(&holes)); + assert_eq!(last.hash, Some(digest(&texels)), "the staged texels' digest stays"); + assert!(!last.accept_partial(&texels), "the same texels again: nothing to detile"); + } /// A partly mapped texture read with the bytes its pages held before /// its texels landed is read again the frame after the guards see the From 0729d56dfad6d38388cc80d974c909747531e67b Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:26:25 +0100 Subject: [PATCH 2/2] leonida-gpu: name where accept_partial comes from, and check other texels Co-Authored-By: Claude Opus 5.5 --- d3d12/crates/leonida-gpu/src/texture.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/d3d12/crates/leonida-gpu/src/texture.rs b/d3d12/crates/leonida-gpu/src/texture.rs index 7460e5d89d..04ab5df1c1 100644 --- a/d3d12/crates/leonida-gpu/src/texture.rs +++ b/d3d12/crates/leonida-gpu/src/texture.rs @@ -123,7 +123,8 @@ impl Last { /// Whether a read of a partly mapped texture has texels to detile, its /// digest then kept. A read that catches only holes (all zeros) changes /// nothing: the digest of the texels staged stays, so a later read of - /// those same texels does not detile and upload them again. + /// those same texels does not detile and upload them again. After + /// a fix in TaynanGT/Vice-Tay (26aeded, GPL-3.0). fn accept_partial(&mut self, bytes: &[u8]) -> bool { if bytes.iter().all(|&b| b == 0) { return false; @@ -6025,6 +6026,9 @@ mod partial_tests { assert!(!last.accept_partial(&holes)); assert_eq!(last.hash, Some(digest(&texels)), "the staged texels' digest stays"); assert!(!last.accept_partial(&texels), "the same texels again: nothing to detile"); + let others = [0x3cu8; 128]; + assert!(last.accept_partial(&others), "other texels are detiled"); + assert_eq!(last.hash, Some(digest(&others)), "and their digest is the one kept"); } /// A partly mapped texture read with the bytes its pages held before