diff --git a/d3d12/crates/leonida-gpu/src/texture.rs b/d3d12/crates/leonida-gpu/src/texture.rs index 32ce3a4911..04ab5df1c1 100644 --- a/d3d12/crates/leonida-gpu/src/texture.rs +++ b/d3d12/crates/leonida-gpu/src/texture.rs @@ -119,6 +119,25 @@ 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. 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; + } + 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 +2400,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 +6011,25 @@ 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"); + 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 /// its texels landed is read again the frame after the guards see the