Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions crates/victory-gpu/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ fn mirror(handle: u64) {
}
}

/// Gives back dynamic slots of the host heap behind the title heap at
/// `heap`, if that heap still exists.
pub(crate) fn release_dynamic(heap: usize, first: u32, n: u32) {
/// Gives back slots only to the host heap that originally reserved them.
/// A new title heap can be registered at the same address before a list resets.
pub(crate) fn release_dynamic(heap: usize, id: u64, first: u32, n: u32) {
let heaps = SHADER_HEAPS.lock().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.id() == id
{
host.release(first, n);
}
Expand Down Expand Up @@ -657,6 +658,39 @@ mod tests {
create_heap(std::ptr::null(), &extended.0, &HEAP_IIDS[0], out)
}

pub(crate) fn shader_visible_view_heap(count: u32) -> This {
let mut heap = std::ptr::null();
let desc = HeapDesc { kind: VIEWS_KIND, count, flags: SHADER_VISIBLE, nodes: 0 };
assert_eq!(create(desc, Placement { address: 0, size: 0 }, &mut heap), S_OK);
heap
}

#[test]
fn a_stale_list_cannot_release_slots_owned_by_a_replacement_heap() {
let retired = shader_visible_view_heap(4);
let Some(host) = heap(retired).host() else {
eprintln!("no host descriptor heap; skipped");
release(retired);
return;
};
let retired_id = host.id();
let retired_slot = host.dynamic(1).unwrap();
release(retired);

let replacement = shader_visible_view_heap(4);
let host = heap(replacement).host().unwrap();
// SAFETY: the replacement heap is live for this test.
let capacity = unsafe { host.heap.GetDesc().NumDescriptors } - host.count();
assert_eq!(host.dynamic(capacity), Some(retired_slot));
// A reused title address makes registry lookup find the replacement.
release_dynamic(replacement as usize, retired_id, retired_slot, 1);
assert_eq!(host.dynamic(1), None);

release_dynamic(replacement as usize, host.id(), retired_slot, 1);
assert_eq!(host.dynamic(1), Some(retired_slot));
release(replacement);
}

#[test]
fn heaps_hold_count_descriptors_from_their_start() {
let null = std::ptr::null();
Expand Down
9 changes: 9 additions & 0 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! reads it through a table and finds the slot changed.

use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};

use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::*;
Expand All @@ -15,6 +16,7 @@ use crate::host::host;

pub(crate) struct HostHeap {
pub(crate) heap: ID3D12DescriptorHeap,
id: u64,
cpu: usize,
step: u32,
/// Slots mirroring the title's; view heaps have [`DYNAMIC`] more.
Expand All @@ -32,6 +34,8 @@ pub(crate) struct HostHeap {
next: Mutex<(u32, Vec<bool>)>,
}

static NEXT_HEAP_ID: AtomicU64 = AtomicU64::new(1);

/// Host heaps' GPU ranges (start, bytes, bytes per slot, sampler), for
/// device-removed reports: a shader indexing a heap past its end faults.
static GPU_RANGES: Mutex<Vec<(u64, u64, u64, bool)>> = Mutex::new(Vec::new());
Expand Down Expand Up @@ -116,6 +120,7 @@ impl HostHeap {
));
let h = HostHeap {
heap,
id: NEXT_HEAP_ID.fetch_add(1, Ordering::Relaxed),
cpu,
step,
count,
Expand Down Expand Up @@ -240,6 +245,10 @@ impl HostHeap {
self.count
}

pub(crate) fn id(&self) -> u64 {
self.id
}

/// Gives back `n` dynamic slots from `first` (an index [`dynamic`]
/// returned).
///
Expand Down
10 changes: 5 additions & 5 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ pub(crate) struct State {
/// Tables made afresh in this list: (table, what it holds) → first
/// dynamic slot.
fresh_tables: std::collections::HashMap<(u64, u64), u32>,
/// The dynamic slots the list holds: title heap, first, count; given
/// The dynamic slots the list holds: title heap, host ID, first, count; given
/// back when the list is reset or released.
dynamic: Vec<(usize, u32, u32)>,
dynamic: Vec<(usize, u64, u32, u32)>,
}

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);
}
}
}
Expand Down Expand Up @@ -718,7 +718,7 @@ impl State {
};
self.fresh_tables.insert(key, first);
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 whole range: a shader may index past the entries its
// used-resource list names (descriptor arrays).
for k in 0..n {
Expand Down