GPU: release dynamic descriptor slots only to the host heap that allocated them

Port of fxceless/victory-fork#1324 (commit 52ac4ec, GPL-3.0).

HostHeap::release previously matched only the title heap's pointer address. When a title releases a shader-visible heap before lists referencing it are reset, and a new heap is allocated at the same address, resetting the older list freed dynamic slots belonging to the new heap. On NVIDIA (where VICTORY_DYNAMIC_SLOTS=524288 is active), two live command lists could then overwrite each other's descriptors.

Each HostHeap now gets a monotonic identity (NEXT_ID) recorded alongside dynamic slot ranges in draw::State::dynamic and checked in HostHeap::release.

Tested with VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib --features diagnostics descriptor:: (50 passed, 0 failed), including dynamic_slots_are_given_back_only_to_the_heap_that_took_them.
This commit is contained in:
wike1701 committed 2026-11-27 10:04:21 +01:00
1 parent 18a8dc2640
commit 47747d6236
3 files changed
+54 -21

No files matched your search

+41 -8
View File
@@ -111,7 +111,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<crate::draw::BufferSweep>,
pub(crate) buffer_frame: std::sync::atomic::AtomicU64,
pub(crate) heap: ID3D12DescriptorHeap,
@@ -1951,6 +1957,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,
@@ -3320,11 +3327,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;
};
@@ -6526,11 +6541,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 {
@@ -6544,17 +6577,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);
}