Merge pull request #28 from wike1701/fix/dynamic-slot-heap-identity
GPU: release dynamic descriptor slots only to the host heap that allocated them
This commit is contained in:
3 files changed
+54
-21
No files matched your search
@@ -847,15 +847,15 @@ pub(crate) fn with_heap<T>(heap: usize, f: impl FnOnce(&Heap) -> T) -> Option<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Gives back dynamic slots of the host heap behind the title heap at
|
/// 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)]
|
#[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());
|
let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
|
||||||
if heaps.iter().any(|&(_, _, h)| h == heap)
|
if heaps.iter().any(|&(_, _, h)| h == heap)
|
||||||
// SAFETY: registered heaps are live (removed when dropped).
|
// SAFETY: registered heaps are live (removed when dropped).
|
||||||
&& let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get()
|
&& let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get()
|
||||||
{
|
{
|
||||||
host.release(first, n);
|
host.release(id, first, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,13 @@ impl Made {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static NEXT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
||||||
|
|
||||||
pub(crate) struct HostHeap {
|
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<crate::draw::BufferSweep>,
|
pub(crate) buffer_sweep: Mutex<crate::draw::BufferSweep>,
|
||||||
pub(crate) buffer_frame: std::sync::atomic::AtomicU64,
|
pub(crate) buffer_frame: std::sync::atomic::AtomicU64,
|
||||||
pub(crate) heap: ID3D12DescriptorHeap,
|
pub(crate) heap: ID3D12DescriptorHeap,
|
||||||
@@ -2041,6 +2047,7 @@ impl HostHeap {
|
|||||||
.flatten();
|
.flatten();
|
||||||
let marks = std::sync::Arc::new(QueueMarks::new(if sampler { 0 } else { count as usize }));
|
let marks = std::sync::Arc::new(QueueMarks::new(if sampler { 0 } else { count as usize }));
|
||||||
let h = HostHeap {
|
let h = HostHeap {
|
||||||
|
id: NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
|
||||||
buffer_sweep: Mutex::default(),
|
buffer_sweep: Mutex::default(),
|
||||||
buffer_frame: std::sync::atomic::AtomicU64::new(u64::MAX),
|
buffer_frame: std::sync::atomic::AtomicU64::new(u64::MAX),
|
||||||
heap,
|
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`]
|
/// Gives back `n` dynamic slots from `first` (an index [`dynamic`]
|
||||||
/// returned).
|
/// returned), only if `id` matches this host heap.
|
||||||
///
|
///
|
||||||
/// [`dynamic`]: HostHeap::dynamic
|
/// [`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 {
|
let Some(at) = first.checked_sub(self.count) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -6733,11 +6748,29 @@ mod tests {
|
|||||||
assert_eq!((a, b), (4, 4 + heap.dynamic - 100));
|
assert_eq!((a, b), (4, 4 + heap.dynamic - 100));
|
||||||
// 40 left at the end: 50 do not fit until something is given back.
|
// 40 left at the end: 50 do not fit until something is given back.
|
||||||
assert_eq!(heap.dynamic(50), None);
|
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(50), Some(4));
|
||||||
assert_eq!(heap.dynamic(40), Some(4 + heap.dynamic - 40));
|
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]
|
#[test]
|
||||||
fn dynamic_search_can_find_a_run_across_the_cursor() {
|
fn dynamic_search_can_find_a_run_across_the_cursor() {
|
||||||
let Some(heap) = HostHeap::new(false, 4) else {
|
let Some(heap) = HostHeap::new(false, 4) else {
|
||||||
@@ -6751,17 +6784,17 @@ mod tests {
|
|||||||
assert_eq!((first, left, right, rest), (4, 5, 7, 9));
|
assert_eq!((first, left, right, rest), (4, 5, 7, 9));
|
||||||
|
|
||||||
// Reallocating the left half puts the cursor back in the middle.
|
// 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));
|
assert_eq!(heap.dynamic(2), Some(left));
|
||||||
heap.release(left, 2);
|
heap.release(heap.id(), left, 2);
|
||||||
heap.release(right, 2);
|
heap.release(heap.id(), right, 2);
|
||||||
assert_eq!(heap.dynamic(4), Some(left));
|
assert_eq!(heap.dynamic(4), Some(left));
|
||||||
assert_eq!(heap.dynamic(1), None);
|
assert_eq!(heap.dynamic(1), None);
|
||||||
|
|
||||||
// A free slot at each physical end does not form a two-slot range.
|
// 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;
|
let last = first + heap.dynamic - 1;
|
||||||
heap.release(last, 1);
|
heap.release(heap.id(), last, 1);
|
||||||
assert_eq!(heap.dynamic(2), None);
|
assert_eq!(heap.dynamic(2), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -543,9 +543,9 @@ pub(crate) struct State {
|
|||||||
/// Tables made afresh in this list: (table, slot count, contents) → first
|
/// Tables made afresh in this list: (table, slot count, contents) → first
|
||||||
/// dynamic slot.
|
/// dynamic slot.
|
||||||
fresh_tables: std::collections::HashMap<(u64, u32, u64), FreshTable, std::hash::BuildHasherDefault<crate::host::AddressHash>>,
|
fresh_tables: std::collections::HashMap<(u64, u32, u64), FreshTable, std::hash::BuildHasherDefault<crate::host::AddressHash>>,
|
||||||
/// The dynamic slots the list holds: title heap, first, count; given
|
/// The dynamic slots the list holds: title heap, host heap id, first,
|
||||||
/// back when the list has run, is reset or released.
|
/// count; given back when the list has run, is reset or released.
|
||||||
dynamic: Vec<(usize, u32, u32)>,
|
dynamic: Vec<(usize, u64, u32, u32)>,
|
||||||
/// Root SRVs bound as one-view tables in this list: (title heap, title
|
/// 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
|
/// 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
|
/// 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 {
|
impl Drop for State {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
for &(heap, first, n) in &self.dynamic {
|
for &(heap, id, first, n) in &self.dynamic {
|
||||||
crate::descriptor::release_dynamic(heap, first, n);
|
crate::descriptor::release_dynamic(heap, id, first, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -586,8 +586,8 @@ impl State {
|
|||||||
static ONCE: std::sync::Once = std::sync::Once::new();
|
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"));
|
ONCE.call_once(|| tracing::warn!("a list ran again after its descriptor tables were given back"));
|
||||||
}
|
}
|
||||||
for (heap, first, n) in self.dynamic.drain(..) {
|
for (heap, id, first, n) in self.dynamic.drain(..) {
|
||||||
crate::descriptor::release_dynamic(heap, first, n);
|
crate::descriptor::release_dynamic(heap, id, first, n);
|
||||||
}
|
}
|
||||||
self.fresh_tables.clear();
|
self.fresh_tables.clear();
|
||||||
self.recording = 0;
|
self.recording = 0;
|
||||||
@@ -2242,7 +2242,7 @@ impl State {
|
|||||||
}) else {
|
}) else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
self.dynamic.push((key.0, index, 1));
|
self.dynamic.push((key.0, host.id(), index, 1));
|
||||||
if let Some((a, s)) = &shadow {
|
if let Some((a, s)) = &shadow {
|
||||||
// SAFETY: a live buffer.
|
// SAFETY: a live buffer.
|
||||||
let base = unsafe { s.buffer.GetGPUVirtualAddress() };
|
let base = unsafe { s.buffer.GetGPUVirtualAddress() };
|
||||||
@@ -2316,7 +2316,7 @@ impl State {
|
|||||||
}) else {
|
}) else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
self.dynamic.push((key.0, index, 1));
|
self.dynamic.push((key.0, host.id(), index, 1));
|
||||||
self.root_srv_slots
|
self.root_srv_slots
|
||||||
.insert(key, (index, shadow.as_ref().map(|(_, s)| s.clone())));
|
.insert(key, (index, shadow.as_ref().map(|(_, s)| s.clone())));
|
||||||
match &shadow {
|
match &shadow {
|
||||||
@@ -4565,7 +4565,7 @@ impl State {
|
|||||||
};
|
};
|
||||||
self.fresh_tables.insert(key, FreshTable { first, epoch: 0, ready: Vec::new() });
|
self.fresh_tables.insert(key, FreshTable { first, epoch: 0, ready: Vec::new() });
|
||||||
self.dynamic
|
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
|
// The title's own slots of this table: what the draw reads of
|
||||||
// them it brings up to date here, in the dynamic slots.
|
// them it brings up to date here, in the dynamic slots.
|
||||||
let title_first = heap.index(table).filter(|first| first + n <= host.count());
|
let title_first = heap.index(table).filter(|first| first + n <= host.count());
|
||||||
|
|||||||
Reference in new issue
Block a user