diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 1c2bbdf..cf0d5e2 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -847,15 +847,15 @@ pub(crate) fn with_heap(heap: usize, f: impl FnOnce(&Heap) -> T) -> Option } /// Gives back dynamic slots of the host heap behind the title heap at -/// `heap`, if that heap still exists. +/// `heap`, if that host heap instance (`id`) still exists. #[cfg(windows)] -pub(crate) fn release_dynamic(heap: usize, first: u32, n: u32) { +pub(crate) fn release_dynamic(heap: usize, id: u64, first: u32, n: u32) { let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner()); if heaps.iter().any(|&(_, _, h)| h == heap) // SAFETY: registered heaps are live (removed when dropped). && let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get() { - host.release(first, n); + host.release(id, first, n); } } diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 847da51..f2aad97 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -121,7 +121,13 @@ impl Made { } } +static NEXT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); + pub(crate) struct HostHeap { + /// Identity of this host heap instance, checked by [`release`] so a stale + /// list resetting after a title heap was replaced at the same address does + /// not free slots belonging to the new heap. + id: u64, pub(crate) buffer_sweep: Mutex, pub(crate) buffer_frame: std::sync::atomic::AtomicU64, pub(crate) heap: ID3D12DescriptorHeap, @@ -2041,6 +2047,7 @@ impl HostHeap { .flatten(); let marks = std::sync::Arc::new(QueueMarks::new(if sampler { 0 } else { count as usize })); let h = HostHeap { + id: NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed), buffer_sweep: Mutex::default(), buffer_frame: std::sync::atomic::AtomicU64::new(u64::MAX), heap, @@ -3410,11 +3417,19 @@ impl HostHeap { } } + /// Identity of this host heap instance (see [`release`]). + pub(crate) fn id(&self) -> u64 { + self.id + } + /// Gives back `n` dynamic slots from `first` (an index [`dynamic`] - /// returned). + /// returned), only if `id` matches this host heap. /// /// [`dynamic`]: HostHeap::dynamic - pub(crate) fn release(&self, first: u32, n: u32) { + pub(crate) fn release(&self, id: u64, first: u32, n: u32) { + if id != self.id { + return; + } let Some(at) = first.checked_sub(self.count) else { return; }; @@ -6733,11 +6748,29 @@ mod tests { assert_eq!((a, b), (4, 4 + heap.dynamic - 100)); // 40 left at the end: 50 do not fit until something is given back. assert_eq!(heap.dynamic(50), None); - heap.release(a, 50); + heap.release(heap.id(), a, 50); assert_eq!(heap.dynamic(50), Some(4)); assert_eq!(heap.dynamic(40), Some(4 + heap.dynamic - 40)); } + #[test] + fn dynamic_slots_are_given_back_only_to_the_heap_that_took_them() { + let (Some(a), Some(b)) = (HostHeap::new(false, 4), HostHeap::new(false, 4)) else { + return; // no host GPU + }; + assert_ne!(a.id(), b.id()); + let first = a.dynamic(2).unwrap(); + let after_alloc = a.free.load(std::sync::atomic::Ordering::Relaxed); + a.release(b.id(), first, 2); + assert_eq!( + a.free.load(std::sync::atomic::Ordering::Relaxed), + after_alloc, + "releasing with another heap's identity must not free slots" + ); + a.release(a.id(), first, 2); + assert_eq!(a.free.load(std::sync::atomic::Ordering::Relaxed), after_alloc + 2); + } + #[test] fn dynamic_search_can_find_a_run_across_the_cursor() { let Some(heap) = HostHeap::new(false, 4) else { @@ -6751,17 +6784,17 @@ mod tests { assert_eq!((first, left, right, rest), (4, 5, 7, 9)); // Reallocating the left half puts the cursor back in the middle. - heap.release(left, 2); + heap.release(heap.id(), left, 2); assert_eq!(heap.dynamic(2), Some(left)); - heap.release(left, 2); - heap.release(right, 2); + heap.release(heap.id(), left, 2); + heap.release(heap.id(), right, 2); assert_eq!(heap.dynamic(4), Some(left)); assert_eq!(heap.dynamic(1), None); // A free slot at each physical end does not form a two-slot range. - heap.release(first, 1); + heap.release(heap.id(), first, 1); let last = first + heap.dynamic - 1; - heap.release(last, 1); + heap.release(heap.id(), last, 1); assert_eq!(heap.dynamic(2), None); } diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index c09e693..0d7ea53 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -543,9 +543,9 @@ pub(crate) struct State { /// Tables made afresh in this list: (table, slot count, contents) → first /// dynamic slot. fresh_tables: std::collections::HashMap<(u64, u32, u64), FreshTable, std::hash::BuildHasherDefault>, - /// The dynamic slots the list holds: title heap, first, count; given - /// back when the list has run, is reset or released. - dynamic: Vec<(usize, u32, u32)>, + /// The dynamic slots the list holds: title heap, host heap id, first, + /// count; given back when the list has run, is reset or released. + dynamic: Vec<(usize, u64, u32, u32)>, /// Root SRVs bound as one-view tables in this list: (title heap, title /// address; 0 for none) → dynamic slot, and the host copy the slot /// shows: later draws of the list bring that one up to date, whatever @@ -564,8 +564,8 @@ pub(crate) struct State { impl Drop for State { fn drop(&mut self) { - for &(heap, first, n) in &self.dynamic { - crate::descriptor::release_dynamic(heap, first, n); + for &(heap, id, first, n) in &self.dynamic { + crate::descriptor::release_dynamic(heap, id, first, n); } } } @@ -586,8 +586,8 @@ impl State { static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| tracing::warn!("a list ran again after its descriptor tables were given back")); } - for (heap, first, n) in self.dynamic.drain(..) { - crate::descriptor::release_dynamic(heap, first, n); + for (heap, id, first, n) in self.dynamic.drain(..) { + crate::descriptor::release_dynamic(heap, id, first, n); } self.fresh_tables.clear(); self.recording = 0; @@ -2242,7 +2242,7 @@ impl State { }) else { return false; }; - self.dynamic.push((key.0, index, 1)); + self.dynamic.push((key.0, host.id(), index, 1)); if let Some((a, s)) = &shadow { // SAFETY: a live buffer. let base = unsafe { s.buffer.GetGPUVirtualAddress() }; @@ -2316,7 +2316,7 @@ impl State { }) else { return false; }; - self.dynamic.push((key.0, index, 1)); + self.dynamic.push((key.0, host.id(), index, 1)); self.root_srv_slots .insert(key, (index, shadow.as_ref().map(|(_, s)| s.clone()))); match &shadow { @@ -4565,7 +4565,7 @@ impl State { }; self.fresh_tables.insert(key, FreshTable { first, epoch: 0, ready: Vec::new() }); self.dynamic - .push((std::ptr::from_ref(heap) as usize, first, n)); + .push((std::ptr::from_ref(heap) as usize, host.id(), first, n)); // The title's own slots of this table: what the draw reads of // them it brings up to date here, in the dynamic slots. let title_first = heap.index(table).filter(|first| first + n <= host.count());