diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 3a324390bc..8208677af6 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -1429,6 +1429,21 @@ struct QueueInfo { } static LISTS: Mutex>> = Mutex::new(None); +/// Closed standbys from `reserve_closed_segment` → the private allocator their empty recording is +/// on, kept while the list may be submitted with it. Not in `LISTS`: the allocator's last Release +/// takes `LISTS` (`on_allocator_release`), so it is always dropped with no lock held. +static STANDBY_ALLOCS: Mutex>> = Mutex::new(None); + +/// Forget `list`'s standby allocator (its recording is gone, or so is the list). `in_flight`: the +/// GPU may still run that recording, so the allocator is leaked rather than released. +fn drop_standby_alloc(list: usize) { + let a = STANDBY_ALLOCS.lock().unwrap().as_mut().and_then(|m| m.remove(&list)); + if let Some(a) = a { + if unsafe { super::alloc_pool::allocator_in_flight(a.as_raw() as usize) } { + std::mem::forget(a); + } + } +} static POISONED_EVER: AtomicBool = AtomicBool::new(false); /// `f` with what is kept of every list. @@ -1865,6 +1880,7 @@ pub(super) fn on_release(list: usize) { release_copy_scratch(list); super::real::dispatch_chain::list_reset(list); super::lut3d::list_reset(list); + drop_standby_alloc(list); let old = LISTS.lock().unwrap().as_mut().and_then(|m| m.remove(&list)); if let Some(old) = old { if old.has_sparse() { @@ -4802,8 +4818,16 @@ unsafe fn reserve_closed_segment( let close_is_hooked = false; if !close_is_hooked { if let Some(made) = real::create_closed_pc_command_list(dev.as_raw(), ty) { - note_reserved_closed(made as usize, ty); - return Some(made); + // A list from CreateCommandList1 that was never recorded is not executable: submitting + // it removes the device (DXGI_ERROR_INVALID_CALL). The failed-split route submits this + // standby, so record it empty once on a private allocator first. + if let Some(alloc) = record_empty_closed(wrapper, dev, made, ty) { + note_reserved_closed(made as usize, ty); + let old = STANDBY_ALLOCS.lock().unwrap().get_or_insert_with(HashMap::new).insert(made as usize, alloc); + drop(old); + return Some(made); + } + pc:: u32>(made, 2)(made); } } let temp: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).ok()?; @@ -4825,6 +4849,27 @@ unsafe fn reserve_closed_segment( } } +/// Reset `made` (closed, never recorded) on a private allocator and Close it with nothing recorded, +/// so that it can be submitted. Returns that allocator, which must outlive the recording. +unsafe fn record_empty_closed(wrapper: *mut c_void, dev: &ID3D12Device, made: *mut c_void, ty: D3D12_COMMAND_LIST_TYPE) -> Option { + let Ok(temp) = dev.CreateCommandAllocator::(ty) else { + return None; + }; + if !real::is_adopted(temp.as_raw()) { + real::adopt(temp.as_raw(), real::Kind::CommandAllocator); + } + let ext = *(made as *const usize); + // The standby can be submitted, so its recording must keep the allocator's completion marks. + let reset = super::alloc_pool::reset_list(made as usize, temp.as_raw() as usize, ty.0, |pc_alloc| { + pc:: i32>(made, PC_RESET)(made, pc_alloc, 0) + }); + // (the PC Reset may put the runtime's vtable back: restore ours before the Close) + real::preserve_vtable(made, ext); + let ok = reset >= 0 && segment_close(wrapper, made) >= 0; + real::preserve_vtable(made, ext); + ok.then_some(temp) +} + /// A closed standby must not look recordable if its Reset as the continuation later fails. fn note_reserved_closed(list: usize, ty: D3D12_COMMAND_LIST_TYPE) { clear_segment_state(list); @@ -4841,6 +4886,8 @@ unsafe fn reset_reserved_segment(list: *mut c_void, alloc: usize, ty: D3D12_COMM }); real::preserve_vtable(list, ext); if hr >= 0 { + // (it records on the title's allocator now, not on its standby one) + drop_standby_alloc(list as usize); reset_copy_scratch(list); true } else { @@ -7866,6 +7913,74 @@ mod tests { } } + /// The closed standby the failed-split route submits must be executable: a list straight from + /// CreateCommandList1 was never recorded and removes the device when submitted. + #[test] + fn a_reserved_closed_standby_can_be_submitted() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + } + let dev = dev.unwrap(); + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let Some(standby) = reserve_closed_segment(0x5ba5_d000 as *mut c_void, &dev, ty) else { + crate::gpu::recomp::runner::skip_test("no ID3D12Device4"); + return; + }; + let list = ID3D12CommandList::from_raw_borrowed(&standby).unwrap(); + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + queue.ExecuteCommandLists(&[Some(list.clone())]); + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + queue.Signal(&fence, 1).unwrap(); + let t = std::time::Instant::now(); + while fence.GetCompletedValue() < 1 && t.elapsed().as_secs() < 5 && dev.GetDeviceRemovedReason().is_ok() { + std::thread::yield_now(); + } + assert!(dev.GetDeviceRemovedReason().is_ok(), "submitting the standby removed the device: {:?}", dev.GetDeviceRemovedReason()); + assert!(fence.GetCompletedValue() >= 1); + } + } + + #[test] + fn private_standby_recording_reaches_allocator_tracking() { + if !super::super::alloc_pool::enabled() { + crate::gpu::recomp::runner::skip_test("allocator tracking disabled"); + return; + } + let dev = match crate::gpu::recomp::runner::device() { + Ok(device) => device, + Err(reason) => { crate::gpu::recomp::runner::skip_test(&reason); return; } + }; + if dev.cast::().is_err() { + crate::gpu::recomp::runner::skip_test("no ID3D12Device4"); + return; + } + let mut associations = Vec::new(); + for ty in [D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_LIST_TYPE_COMPUTE, D3D12_COMMAND_LIST_TYPE_COPY] { + unsafe { + let list = reserve_closed_segment(0x5ba5_d000 as *mut c_void, &dev, ty).unwrap(); + let private = STANDBY_ALLOCS.lock().unwrap().as_ref().and_then(|m| { + m.get(&(list as usize)).map(|allocator| allocator.as_raw() as usize) + }).expect("closed standby has a private allocator"); + let tracked = super::super::alloc_pool::recording_backing_for_test(list as usize); + let reservation = super::super::alloc_pool::begin_submit(&[list]).unwrap(); + let list_pinned = super::super::alloc_pool::in_flight(list as usize); + let allocator_pinned = super::super::alloc_pool::allocator_in_flight(private); + drop(reservation); + let allocator_idle = !super::super::alloc_pool::allocator_in_flight(private); + println!("STANDBY_ASSOC queue={} tracked={} list_pinned={list_pinned} allocator_pinned={allocator_pinned} idle_after_cancel={allocator_idle}", + ty.0, tracked == Some(private)); + drop(ID3D12GraphicsCommandList::from_raw(list)); + associations.push(tracked == Some(private) && list_pinned && allocator_pinned && allocator_idle); + } + } + assert_eq!(associations, [true; 3], "every closed standby needs a tracked allocator"); + } + #[test] fn copy_scratch_budget_counts_resources_until_gpu_completion() { use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0;