diff --git a/crates/victory-gpu/src/copystat.rs b/crates/victory-gpu/src/copystat.rs index 792d942953b..61636c87067 100644 --- a/crates/victory-gpu/src/copystat.rs +++ b/crates/victory-gpu/src/copystat.rs @@ -104,8 +104,12 @@ pub(crate) enum C { /// Title threads writing a view into a shader-visible heap: time /// waiting for the heaps' lock (held by heap syncs and other writers). MirrorWaitUs, + /// Change checks: bytes handed out from pages taken whole, and from + /// pages only partly inside the range (copied, left changed). + TakenWholeBytes, + TakenPartBytes, } -const COUNTERS: usize = 61; +const COUNTERS: usize = 63; static COUNT: [AtomicU64; COUNTERS] = [const { AtomicU64::new(0) }; COUNTERS]; pub(crate) fn add(c: C, n: u64) { @@ -234,7 +238,7 @@ pub(crate) fn log(frames: f64) { asking for other mappings, {:.1} ms waiting for the guard lock; preparing: {:.1} ms in \ refresh batches, steps (each with the batch it flushes): refresh {:.1} ms, copy {:.1} ms, \ work {:.1} ms, GPU writes {:.1} ms, textures {:.1} ms, heaps {:.1} ms, title memory {:.1} ms; \ - heap texture uploads {:.1} ms, state preludes {:.1} ms, heap slots the title copied {:.1} ms ({:.0} slots), title threads waiting to write views {:.1} ms; copying back compared {:.1} MB in {:.1} ms; marked pages: {:.0} compared, {:.1} MB written back in {:.1} ms, {:.0} kept the title's, {:.0} saved; working set {:.0} MB, \ + heap texture uploads {:.1} ms, state preludes {:.1} ms, heap slots the title copied {:.1} ms ({:.0} slots), title threads waiting to write views {:.1} ms; copying back compared {:.1} MB in {:.1} ms; marked pages: {:.0} compared, {:.1} MB written back in {:.1} ms, {:.0} kept the title's, {:.0} saved; taken {:.1} MB whole, {:.1} MB in part; working set {:.0} MB, \ {:.0} hard faults", c[C::Submissions as usize], c[C::Copies as usize], @@ -298,6 +302,8 @@ pub(crate) fn log(frames: f64) { ms(c[C::MarkedUs as usize]), c[C::MarkedKept as usize], c[C::SavedPages as usize], + mb(c[C::TakenWholeBytes as usize]), + mb(c[C::TakenPartBytes as usize]), working_set() as f64 / 1e6, { static HARD: AtomicU64 = AtomicU64::new(0); diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index a455a965ea1..840b742b214 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -90,6 +90,10 @@ pub(crate) struct Shadow { /// goes. base: u64, slice: Option, + /// Ranges (title addresses) where the copy holds what shaders wrote + /// and title memory does not ([`Shadow::keep_writes`]): a refresh + /// widened to whole pages stops short of them ([`Shadow::widen`]). + kept: Mutex>, } /// Snapshot segments (offset, bytes) and the pieces compared. @@ -264,6 +268,7 @@ impl Shadow { marks, base: slice.map_or(0, |s| s.offset), slice, + kept: Mutex::default(), }); if let Some(base) = marks { self::marks().owners.insert(base, Arc::downgrade(&shadow)); @@ -402,9 +407,49 @@ impl Shadow { /// in the copy and does not go into title memory (they computed it /// from memory with no copy, `crate::memory`). pub(crate) fn keep_writes(&self, address: usize, len: usize) { + if len > 0 { + self.kept + .lock() + .unwrap_or_else(|e| e.into_inner()) + .insert((address, address + len)); + } self.copy_back_or_not(address, len, false); } + /// `[from, to)` (inside the buffer) grown to the 64 KiB pages it + /// touches, cut to the buffer and short of ranges holding shaders' + /// kept writes. A page only partly inside a refreshed range is copied + /// in part and stays changed ([`crate::watch::Tracked::take`]), so it + /// was copied again at every later refresh reaching it: in the world, + /// about 80% of the bytes copied per frame (400-550 MB of 550-690 MB on + /// an RTX 4060 Ti). Taken whole, it is + /// guarded and copied again only once the title writes it. + fn widen(&self, from: usize, to: usize) -> (usize, usize) { + if self.tracked.is_none() { + return (from, to); + } + let (start, end) = (self.start as usize, (self.start + self.len) as usize); + let mut low = (from / PAGE * PAGE).max(start); + let mut high = to.div_ceil(PAGE).saturating_mul(PAGE).min(end); + let kept = self.kept.lock().unwrap_or_else(|e| e.into_inner()); + for &(a, e) in kept.iter() { + if e <= low || a >= high { + continue; + } + if a < from { + low = low.max(e.min(from)); + } + if e > to { + high = high.min(a.max(to)); + } + if a < to && e > from { + // Kept writes inside the range itself: as asked, no more. + return (from, to); + } + } + (low.min(from), high.max(to)) + } + fn copy_back_or_not(&self, address: usize, len: usize, back: bool) -> usize { let mut snapshot = self.before.lock().unwrap_or_else(|e| e.into_inner()); let (segments, compared) = &mut *snapshot; @@ -556,6 +601,7 @@ impl Shadow { if from >= to { return 0; } + let (from, to) = self.widen(from, to); let mut busy: Vec<(usize, usize)> = self .pending .lock() @@ -1440,6 +1486,38 @@ pub(crate) mod tests { assert_eq!((arena.chunks.len(), d.chunk), (2, 1)); } + /// Small refreshes inside a page the title wrote take the whole page: + /// the next refreshes there copy nothing until the title writes again, + /// and bytes shaders' kept writes hold are not copied over. + #[test] + fn a_partial_refresh_takes_the_whole_page() { + let p = crate::watch::tests::pool(2); + let len = 2 * PAGE; + register(p, p as u64, len as u64); + let Some(copy) = find(p as u64, len as u64) else { + unregister(p, p as u64); + return; // no host GPU here + }; + copy.update(p, len); + // SAFETY: (all title writes below) bytes of our pool. + unsafe { ((p + 100) as *mut u8).write_volatile(7) }; + assert_eq!(copy.update(p + 64, 16), PAGE, "the written page, whole"); + assert_eq!(copy.update(p + 4096, 16), 0, "taken: nothing to copy"); + assert_eq!(copy.update(p + 64, 16), 0, "nor again"); + assert_eq!(copy.read_host((p + 100) as u64 & !3, 1)[0].to_le_bytes()[0], 7); + // SAFETY: a byte of our pool. + unsafe { ((p + 100) as *mut u8).write_volatile(8) }; + assert_eq!(copy.update(p + 64, 16), PAGE, "written again: copied again"); + // Shaders' kept writes at the page's end: the refresh stops short. + copy.write_host((p + PAGE - 8) as u64, &[0xee]); + copy.keep_writes(p + PAGE - 16, 16); + // SAFETY: a byte of our pool. + unsafe { ((p + 100) as *mut u8).write_volatile(9) }; + assert_eq!(copy.update(p + 64, 16), PAGE - 16, "up to the kept writes"); + assert_eq!(copy.read_host((p + PAGE - 8) as u64, 1)[0].to_le_bytes()[0], 0xee, "kept"); + unregister(p, p as u64); + } + /// Ranges shaders write far apart are kept apart (not the copy between /// them); what the GPU wrote goes back once, whichever overlapping /// ranges declared it, and nothing else does. diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index a59f211d372..03374eb33b1 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -523,6 +523,11 @@ impl Tracked { for p in pages { let (a, b) = ((p * PAGE).max(from), ((p + 1) * PAGE).min(to)); let runs = if unmapped.contains(&p) { &mut zero } else { &mut copy }; + #[cfg(feature = "diagnostics")] + crate::copystat::add( + if whole.contains(&p) { crate::copystat::C::TakenWholeBytes } else { crate::copystat::C::TakenPartBytes }, + (b - a) as u64, + ); match runs.last_mut() { Some((_, end)) if *end == a => *end = b, _ => runs.push((a, b)),