diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..8b80def542 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2835,7 +2835,10 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ let old_next = { let mut g = LISTS.lock().unwrap(); let m = g.get_or_insert_with(HashMap::new); - let old = m.remove(&(next_pc as usize)); + let mut old = m.remove(&(next_pc as usize)); + // (a spare that was a first segment keeps the allocator its deferred segments recorded on while the GPU may still + // run them; once it is done the allocator goes with the old state, and its split dispatches' scratch with it) + let own_alloc = old.as_mut().and_then(|o| o.own_alloc.take_if(|a| super::alloc_pool::allocator_in_flight(a.as_raw() as usize))); if let Some(ref o) = old { if !o.pending_waits.is_empty() { PENDING_WAIT_LISTS.fetch_sub(1, Ordering::AcqRel); @@ -2866,6 +2869,7 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ pred_in_state: false, predication, bindings: bindings.clone(), + own_alloc, ..Default::default() }, ); @@ -7079,6 +7083,129 @@ mod tests { } } + /// A recording's first segment stood in for its primary list and holds the allocator its + /// deferred dispatches recorded on. At the next Reset it goes to the spares; a split of a later + /// recording reuses it while those dispatches still run on the GPU (a block after a Wait64). + /// The allocator must outlive them. + #[test] + fn a_spare_reused_by_a_split_keeps_an_own_allocator_still_on_the_gpu() { + use super::super::{alloc_pool, bind, title_list}; + + let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + unsafe { + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let a = alloc.as_raw() as usize; + let new_list = |on: &ID3D12CommandAllocator| { + let l = dev.CreateCommandList::<_, _, ID3D12GraphicsCommandList>(0, ty, on, None).unwrap().into_raw(); + real::adopt(l, real::Kind::CommandList); + on_create(l, on.as_raw() as usize, 0, ty); + l + }; + // the old first segment and the allocator of its deferred segments + let own: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let spare = new_list(&alloc); + assert_eq!(pc:: i32>(spare, PC_CLOSE)(spare), 0); + let primary = new_list(&alloc); + bind::reset(primary, a, 0); + let w = title_list::wrap(primary); + LISTS.lock().unwrap().as_mut().unwrap().get_mut(&(spare as usize)).unwrap().own_alloc = Some(own.clone()); + title_list::put_spare(w, spare); + + // a deferred segment recorded on it, held on the GPU behind a queue Wait + let deferred = new_list(&own); + assert_eq!(pc:: i32>(deferred, PC_CLOSE)(deferred), 0); + let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(*(*(q.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS)); + q.Wait(&gate, 1).unwrap(); + exec(q.as_raw(), 1, &deferred); + alloc_pool::executed(q.as_raw(), &[deferred]); + assert!(alloc_pool::allocator_in_flight(own.as_raw() as usize)); + + // a later recording splits and gets the spare (its own submit is done) + let next = split(primary, None); + assert_eq!(next, Some(spare), "the split reuses the spare"); + // (this test holds one reference; the spare's state held the other) + let refs = (Interface::vtable(&own).base__.base__.base__.base__.AddRef)(own.as_raw()); + (Interface::vtable(&own).base__.base__.base__.base__.Release)(own.as_raw()); + + gate.Signal(1).unwrap(); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("the queue finishes"); + let removed = dev.GetDeviceRemovedReason(); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + assert_eq!(release(w), 0); + drop(ID3D12GraphicsCommandList::from_raw(deferred)); + + assert_eq!(refs, 3, "the allocator of work still on the GPU stays held"); + assert!(removed.is_ok()); + } + } + + /// As above, with the GPU done with the deferred segment before the split: the reused spare + /// keeps no allocator nothing records on any more (its split dispatches' scratch is freed with it). + #[test] + fn a_spare_reused_by_a_split_drops_an_own_allocator_the_gpu_is_done_with() { + use super::super::{alloc_pool, bind, title_list}; + + let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + unsafe { + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let a = alloc.as_raw() as usize; + let new_list = |on: &ID3D12CommandAllocator| { + let l = dev.CreateCommandList::<_, _, ID3D12GraphicsCommandList>(0, ty, on, None).unwrap().into_raw(); + real::adopt(l, real::Kind::CommandList); + on_create(l, on.as_raw() as usize, 0, ty); + l + }; + let own: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let spare = new_list(&alloc); + assert_eq!(pc:: i32>(spare, PC_CLOSE)(spare), 0); + let primary = new_list(&alloc); + bind::reset(primary, a, 0); + let w = title_list::wrap(primary); + LISTS.lock().unwrap().as_mut().unwrap().get_mut(&(spare as usize)).unwrap().own_alloc = Some(own.clone()); + title_list::put_spare(w, spare); + + // the deferred segment recorded on it has run + let deferred = new_list(&own); + assert_eq!(pc:: i32>(deferred, PC_CLOSE)(deferred), 0); + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(*(*(q.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS)); + exec(q.as_raw(), 1, &deferred); + alloc_pool::executed(q.as_raw(), &[deferred]); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("the queue finishes"); + assert!(!alloc_pool::allocator_in_flight(own.as_raw() as usize)); + + let next = split(primary, None); + assert_eq!(next, Some(spare), "the split reuses the spare"); + // (only this test holds it now) + let refs = (Interface::vtable(&own).base__.base__.base__.base__.AddRef)(own.as_raw()); + (Interface::vtable(&own).base__.base__.base__.base__.Release)(own.as_raw()); + let held = LISTS.lock().unwrap().as_ref().unwrap().get(&(spare as usize)).is_some_and(|s| s.own_alloc.is_some()); + + let removed = dev.GetDeviceRemovedReason(); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + assert_eq!(release(w), 0); + drop(ID3D12GraphicsCommandList::from_raw(deferred)); + + assert!(!held, "the reused spare's state keeps no allocator the GPU is done with"); + assert_eq!(refs, 2, "the allocator the GPU is done with is released"); + assert!(removed.is_ok()); + } + } + /// Reproduces the exact intra-list fork-join pattern seen on retail compute queues: /// q_compute list O_c: Write64(cell_A, frame) -> Wait64(cell_B >= frame) -> Write64(cell_C, frame) /// q_direct list O_d: Wait64(cell_A >= frame) -> Write64(cell_B, frame)