diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 2aa4178..bbb733b 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -2184,6 +2184,10 @@ struct Deferred { /// Buffer ranges refreshed since the last title copy (host copy, /// address, bytes): refreshing them again would find the same bytes. refreshed: std::collections::HashSet<(usize, usize, usize), std::hash::BuildHasherDefault>, + /// Ranges declared written by shaders since the list last wrote title + /// memory (host copy, address, bytes): declaring them again would + /// change nothing. + gpu_written: std::collections::HashSet<(usize, usize, usize), std::hash::BuildHasherDefault>, /// Textures whose contents the list brings up to date. textures: std::collections::HashSet, /// Bottom-of-pipe writes, done once the list has run: address, what, @@ -2218,6 +2222,13 @@ impl Deferred { self.upload_bytes += size; self.uploads.push(buffer); } + + /// The steps recorded last write title memory: what was brought up to + /// date before them is not any more. + fn title_written(&mut self) { + self.refreshed.clear(); + self.gpu_written.clear(); + } } impl Drop for Deferred { @@ -3685,6 +3696,12 @@ fn continues(last: (usize, usize, usize), next: (usize, usize, usize)) -> bool { && (d.checked_add(all).is_some_and(|end| end <= s) || s.checked_add(all).is_some_and(|end| end <= d)) } +/// See [`HostList::defer_gpu_write`]. +fn declare_once() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var("VICTORY_DECLARE_ONCE").map_or(true, |v| v != "0")) +} + /// The ranges the shaders of `runs` may write, per host copy, as declared. /// One list's lock at a time: a submission may name a list twice. fn declared_writes(runs: &[&HostList]) -> Vec { @@ -6630,7 +6647,7 @@ impl HostList { deferred .copies .push(Step::Copy(dst as usize, src as usize, len)); - deferred.refreshed.clear(); + deferred.title_written(); self.changed_something(); } @@ -6677,15 +6694,20 @@ impl HostList { /// Shaders of the list write `[address, address + len)` of a buffer's /// host copy: copied back into title memory once the list has run. + /// Declared once until the list next writes title memory (but with + /// `VICTORY_DECLARE_ONCE=0`): the dispatches of a frame declared the + /// same ranges thousands of times, each a step of its own. pub(crate) fn defer_gpu_write( &self, shadow: &std::sync::Arc, address: usize, len: usize, ) { - self.deferred() - .copies - .push(Step::GpuWrite(shadow.clone(), address, len)); + let mut deferred = self.deferred(); + let key = (std::sync::Arc::as_ptr(shadow) as usize, address, len); + if deferred.gpu_written.insert(key) || !declare_once() { + deferred.copies.push(Step::GpuWrite(shadow.clone(), address, len)); + } } /// A read back: once the list has run, `rows` rows of `row` bytes go @@ -7012,8 +7034,10 @@ impl HostList { /// Runs `work` when the list is submitted, in order with its copies. /// `work` may write title memory (refreshes after it copy again). pub(crate) fn defer(&self, work: impl Fn() + Send + 'static) { - self.deferred().copies.push(Step::Run(Box::new(work))); - self.deferred().refreshed.clear(); + let mut deferred = self.deferred(); + deferred.copies.push(Step::Run(Box::new(work))); + deferred.title_written(); + drop(deferred); self.changed_something(); } diff --git a/crates/victory-gpu/src/host/place.rs b/crates/victory-gpu/src/host/place.rs index cde0149..9e815d4 100644 --- a/crates/victory-gpu/src/host/place.rs +++ b/crates/victory-gpu/src/host/place.rs @@ -255,7 +255,7 @@ impl HostList { shadow: to, from: scratch.map(|s| s.2 as usize), })); - deferred.refreshed.clear(); + deferred.title_written(); self.changed_something(); true }