diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..91d47708ae 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2711,12 +2711,45 @@ pub(super) unsafe fn record_deferred(wrapper: *mut c_void) { Deferred::Indirect(sig, max, args, args_off, count_buf, count_off) => super::bind::execute_indirect(seg, sig, max, args, args_off, count_buf, count_off), } let ext = *(seg as *const usize); - pc:: i32>(seg, PC_CLOSE)(seg); + let hr = segment_close(wrapper, seg); real::preserve_vtable(seg, ext); + if hr < 0 { + // a recording the runtime refused is not submitted: the segment leaves the recording + // (a list whose Close failed is still open and cannot be reset), its call is dropped, + // and the next one does not follow it + static CLOSE_ERR: AtomicU64 = AtomicU64::new(0); + warn(&CLOSE_ERR, "deferred call", format_args!("Close failed ({hr:#x}), the call is dropped")); + super::bind::dispatch_dropped(); + super::title_list::drop_segment(wrapper, seg); + continue; + } last = Some(seg); } } +/// (tests) What the next native Close of a put-off segment of this wrapper does instead +/// (`segment_close`; tests run in parallel, so it is the given wrapper's only). +#[cfg(test)] +type CloseHook = Box i32 + Send>; +#[cfg(test)] +static CLOSE_HOOK: Mutex> = Mutex::new(Vec::new()); + +/// The native Close of `wrapper`'s put-off segment `p`: its HRESULT. +unsafe fn segment_close(wrapper: *mut c_void, p: *mut c_void) -> i32 { + #[cfg(test)] + { + let hook = { + let mut g = CLOSE_HOOK.lock().unwrap(); + g.iter().position(|h| h.0 == wrapper as usize).map(|i| g.remove(i).1) + }; + if let Some(hook) = hook { + return hook(p); + } + } + let _ = wrapper; + pc:: i32>(p, PC_CLOSE)(p) +} + /// The allocator `list`'s put-off calls recorded on is reset once the GPU is done with them, so /// their split dispatches' scratch does not outlive them. unsafe fn reset_own_alloc(list: *mut c_void) { @@ -2969,8 +3002,17 @@ unsafe fn deferred_segment(wrapper: *mut c_void, list: *mut c_void, ty: D3D12_CO None => real::create_raw_pc_command_list(dev.as_raw(), ty, super::alloc_pool::current(own), 0)?, }; let ext = *(p as *const usize); - pc:: i32>(p, PC_CLOSE)(p); + let hr = segment_close(wrapper, p); real::preserve_vtable(p, ext); + if hr < 0 { + // a list that did not close is no segment: the call waits in its list instead + static CLOSE_ERR: AtomicU64 = AtomicU64::new(0); + warn(&CLOSE_ERR, "put-off segment", format_args!("Close failed ({hr:#x})")); + real::dispatch_chain::list_reset(p as usize); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(p as *const *const usize)).add(2)); + release(p); + return None; + } super::title_list::push_segment(wrapper, p); Some(p) } @@ -7079,6 +7121,156 @@ mod tests { } } + /// A direct queue, its allocator and a title list wrapped as XCL wraps one, bound to `standin` + /// (None: no adapter). + unsafe fn put_off_rig(standin: usize) -> Option<(ID3D12Device, ID3D12CommandQueue, ID3D12CommandAllocator, *mut c_void, *mut c_void)> { + use super::super::{bind, title_list}; + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let warp = CreateDXGIFactory1::().and_then(|f| f.EnumWarpAdapter::()); + if warp.and_then(|w| D3D12CreateDevice(&w, D3D_FEATURE_LEVEL_11_0, &mut dev)).is_err() { + return None; + } + } + let dev = dev.unwrap(); + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let raw: ID3D12GraphicsCommandList = dev.CreateCommandList(0, ty, &alloc, None).unwrap(); + let pc0 = raw.into_raw(); + real::adopt(pc0, real::Kind::CommandList); + on_create(pc0, alloc.as_raw() as usize, 0, ty); + bind::reset(pc0, alloc.as_raw() as usize, standin); + let wrap = title_list::wrap(pc0); + Some((dev, queue, alloc, pc0, wrap)) + } + + /// A put-off call whose recording the driver refuses at the submit (its segment's Close + /// fails) is not submitted as it is: the segment goes out empty, the call is dropped, and the + /// rest of the submission runs (its fence is signalled, the device stays). + #[test] + fn a_put_off_call_whose_close_fails_is_not_submitted() { + use super::super::{bind, pso_async, title_list}; + use std::time::{Duration, Instant}; + unsafe { + let standin = 0x7E57_C105usize; + static BUILD: AtomicBool = AtomicBool::new(false); + BUILD.store(false, Ordering::Release); + pso_async::submit(standin, true, Box::new(|| { + let t = Instant::now(); + while !BUILD.load(Ordering::Acquire) && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(5)); + } + 0 + })); + let Some((dev, queue, _alloc, pc0, wrap)) = put_off_rig(standin) else { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + bind::dispatch(pc0, 1, 1, 1); + let mut segs = Vec::new(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 3, "the dispatch was put off"); + let refused = segs[1]; + let add_ref: unsafe extern "system" fn(*mut c_void) -> u32 = + std::mem::transmute(*(*(refused as *const *const usize)).add(1)); + add_ref(refused); // Keep a test reference so final Release can reveal a lost owner. + + // the replay's recording fails in the runtime: a query begun and never ended + let mut heap: Option = None; + dev.CreateQueryHeap(&D3D12_QUERY_HEAP_DESC { Type: D3D12_QUERY_HEAP_TYPE_OCCLUSION, Count: 1, NodeMask: 0 }, &mut heap).unwrap(); + let heap = heap.unwrap(); + let h = heap.as_raw() as usize; + static REFUSED: AtomicU32 = AtomicU32::new(0); + CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(move |p| { + pc::(p, PC_BEGIN_QUERY)(p, h, D3D12_QUERY_TYPE_OCCLUSION, 0); + let hr = pc:: i32>(p, PC_CLOSE)(p); + REFUSED.store(hr as u32, Ordering::Release); + hr + }))); + let close_fn: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(9)); + assert_eq!(close_fn(wrap), 0); + BUILD.store(true, Ordering::Release); + record_deferred(wrap); + assert!(!CLOSE_HOOK.lock().unwrap().iter().any(|h| h.0 == wrap as usize), "the replay closed its segment"); + let release = |list: *mut c_void| { + let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(list as *const *const usize)).add(2)); + f(list) + }; + if (REFUSED.load(Ordering::Acquire) as i32) >= 0 { + // Some runtimes accept Close with an open query, so this run never hits the failure path. + release(wrap); + release(refused); + let _ = pso_async::released(standin); + crate::gpu::recomp::runner::skip_test("the runtime closed a list with a query left open"); + return; + } + // what the submit collects after the replay (`q_execute_command_lists`) + segs.clear(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 2, "the refused segment is not submitted"); + + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void); + let exec: ExecFn = std::mem::transmute(*(*(queue.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS)); + exec(queue.as_raw(), segs.len() as u32, segs.as_ptr()); + queue.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 2000).expect("the submission runs"); + assert!(dev.GetDeviceRemovedReason().is_ok(), "the device stays"); + + // The failed-Close list cannot Reset. Its next spare reuse must not lose the + // wrapper's owning reference when a new segment is made instead. + assert!(deferred_segment(wrap, pc0, D3D12_COMMAND_LIST_TYPE_DIRECT).is_some()); + release(wrap); + assert_eq!(release(refused), 0, "the refused segment retained an untracked owner"); + let _ = pso_async::released(standin); + } + } + + /// A put-off segment that does not close when it is made is not used: the call waits in its + /// list instead of being put off. + #[test] + fn a_put_off_segment_that_does_not_close_is_not_used() { + use super::super::{bind, pso_async, title_list}; + unsafe { + let standin = 0x7E57_C106usize; + static BUILD: AtomicBool = AtomicBool::new(false); + BUILD.store(false, Ordering::Release); + pso_async::submit(standin, true, Box::new(|| { + let t = std::time::Instant::now(); + while !BUILD.load(Ordering::Acquire) && t.elapsed() < std::time::Duration::from_secs(10) { + std::thread::sleep(std::time::Duration::from_millis(5)); + } + 0 + })); + let Some((_dev, _queue, _alloc, pc0, wrap)) = put_off_rig(standin) else { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(|p| { + pc:: i32>(p, PC_CLOSE)(p); + windows::Win32::Foundation::E_FAIL.0 + }))); + // (the call that is not put off waits for the build in the call; started here, after + // the device is made, so the build is still going when the call comes) + let ends = std::thread::spawn(|| { + std::thread::sleep(std::time::Duration::from_millis(300)); + BUILD.store(true, Ordering::Release); + }); + bind::dispatch(pc0, 1, 1, 1); + ends.join().unwrap(); + assert!(!CLOSE_HOOK.lock().unwrap().iter().any(|h| h.0 == wrap as usize), "the put-off segment was closed"); + let mut segs = Vec::new(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 1, "nothing was put off"); + let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2)); + rel_fn(wrap); + let _ = pso_async::released(standin); + } + } + /// 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) diff --git a/crates/xcl/src/hle/d3d12x/title_list.rs b/crates/xcl/src/hle/d3d12x/title_list.rs index a41e5643fc..882139e80f 100644 --- a/crates/xcl/src/hle/d3d12x/title_list.rs +++ b/crates/xcl/src/hle/d3d12x/title_list.rs @@ -292,6 +292,19 @@ pub(super) fn put_spare(wrapper: *mut c_void, pc: *mut c_void) { sg.get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().spares.push(pc as usize); } +/// Take a refused segment out of `wrapper`'s recording and release it. A list whose Close failed +/// cannot be reset, so it cannot be kept as a reusable spare. +pub(super) unsafe fn drop_segment(wrapper: *mut c_void, pc: *mut c_void) { + let mut sg = SEGMENTS.lock().unwrap(); + if let Some(b) = sg.as_mut().and_then(|m| m.get_mut(&(wrapper as usize))) { + b.extra_active.retain(|&p| p != pc as usize); + } + drop(sg); + super::real::dispatch_chain::list_reset(pc as usize); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(pc_slot(pc, 2)); + release(pc); +} + /// Add the closed segment `pc` to `wrapper`'s recording, after the ones it has. pub(super) fn push_segment(wrapper: *mut c_void, pc: *mut c_void) { SEGMENTS.lock().unwrap().get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().extra_active.push(pc as usize);