diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 3c9dd506b8..203e40314f 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -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>> = Mutex::new(None); +pub(super) static RES: Mutex>> = Mutex::new(None); /// The view in slot `s` names `res` (0: none). pub fn note_resource(s: &Slot, res: usize) { @@ -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) }; @@ -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); + } } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index bd9cd731cf..7da3ebedcd 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -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 @@ -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"); }