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
33 changes: 32 additions & 1 deletion crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ pub unsafe fn publish_range(first: &Slot, n: u32, names: &[(usize, usize)]) {
}

/// view slot address → the resource its view names (`XCL_TRACE_FRAME`, `XCL_DUMP_TARGETS`)
static RES: Mutex<Option<std::collections::HashMap<usize, usize>>> = Mutex::new(None);
pub(super) static RES: Mutex<Option<std::collections::HashMap<usize, usize>>> = Mutex::new(None);

/// The view in slot `s` names `res` (0: none).
pub fn note_resource(s: &Slot, res: usize) {
Expand Down Expand Up @@ -1341,6 +1341,15 @@ pub fn named_by_index(i: u32) -> usize {
NAMES.get().map_or(0, |n| n.name(i))
}

/// The PC resource `res` is destroyed: no view slot names it any more in `RES`, or a trace would
/// call `GetDesc` on the dead pointer. `RES` is filled only by the diagnostics, so without them
/// (map never made) this costs one lock; with them it is a scan of the map per release.
pub fn forget_resource(res: usize) {
if let Some(m) = RES.lock().unwrap().as_mut() {
m.retain(|_, r| *r != res);
}
}

pub fn write(s: &Slot, words: &[u32]) {
let n = words.len().min(if s.sampler { 4 } else { 8 });
unsafe { std::ptr::copy_nonoverlapping(words.as_ptr(), s.at as *mut u32, n) };
Expand Down Expand Up @@ -2848,4 +2857,26 @@ mod tests {
}
assert_eq!((device.refs.load(Ordering::Relaxed), global_heap.refs.load(Ordering::Relaxed), source_heap.refs.load(Ordering::Relaxed)), (0, 0, 1));
}

/// A released resource is gone from the view-slot map: a descriptor table slot that named
/// it must not hand its dead pointer to a trace's `GetDesc`.
#[test]
fn a_released_resource_is_forgotten_by_the_resource_map() {
let (at_a, at_b, at_c) = (0x7ff1_5a1e_0000usize, 0x7ff1_5a1e_0040usize, 0x7ff1_5a1e_0080usize);
let (dead, live) = (0x7ff1_5a1e_d000usize, 0x7ff1_5a1e_a000usize);
{
let mut g = RES.lock().unwrap();
let m = g.get_or_insert_with(Default::default);
m.insert(at_a, dead);
m.insert(at_b, dead);
m.insert(at_c, live);
}
assert_eq!(resource_at(at_a), Some(dead));
forget_resource(dead);
assert_eq!(resource_at(at_a), None);
assert_eq!(resource_at(at_b), None);
assert_eq!(resource_at(at_c), Some(live));
forget_resource(live);
assert_eq!(resource_at(at_c), None);
}
}
12 changes: 12 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,7 @@ unsafe extern "system" fn res_release(this: *mut c_void) -> u32 {
super::vram::park_recyclable(this as usize, info, state.0 as u32);
}
}
super::console_heap::forget_resource(this as usize);
let gone = UMA_BUFFERS.lock().unwrap().as_mut().and_then(|m| m.remove(&(this as usize)));
// a buffer may be over a heap of pages that are freed next; a texture, and a buffer of XCL's
// in video memory (or over a GPU view's tiles), is not
Expand Down Expand Up @@ -11932,11 +11933,22 @@ mod release_tests {
let fake = Box::new(Fake { vt: unsafe { ext.as_ptr().add(1) }, refs: AtomicUsize::new(2) });
let this = &*fake as *const Fake as *mut c_void;
BUFFER_DELTA.lock().unwrap().get_or_insert_with(HashMap::new).insert(this as usize, 0x40);
// a view slot of the console heap's resource map names it (and another names a live one)
let (slot, other_slot, live) = (0x7ff1_5a1e_0000usize, 0x7ff1_5a1e_0040usize, 0x7ff1_5a1e_a000usize);
{
let mut g = super::super::console_heap::RES.lock().unwrap();
let m = g.get_or_insert_with(Default::default);
m.insert(slot, this as usize);
m.insert(other_slot, live);
}
unsafe {
assert_eq!(res_release(this), 1, "not the last");
assert_eq!(buffer_delta(this as usize), 0x40, "kept while another reference is held");
assert_eq!(super::super::console_heap::resource_at(slot), Some(this as usize), "kept while another reference is held");
assert_eq!(res_release(this), 0);
}
assert_eq!(super::super::console_heap::resource_at(slot), None, "the released resource is still in the resource map");
assert_eq!(super::super::console_heap::resource_at(other_slot), Some(live));
assert_eq!(fake.refs.load(Ordering::SeqCst), 0);
assert_eq!(ENTRY_AT_END.load(Ordering::SeqCst), 0, "the entry went before the object");
}
Expand Down
Loading