From 62f6d4737b28a5fc7e9808350724c0c1a4a0f168 Mon Sep 17 00:00:00 2001 From: Decodere <98278826+Decodere@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:01:24 +0300 Subject: [PATCH 1/4] D3D12.X: switch KickoffX onto an idle spare PC list when the submitted list is in flight Follow-up to #2163 (Part of #1452). `list_x::kickoff_x()` previously reset and resumed recording on the same PC command list it had just submitted (or on `primary` after a mid-list split), even when that list's submission was still executing on the GPU. A subsequent `KickoffX` or final `Close` + `ExecuteCommandLists` then resubmitted an in-flight PC command list, triggering D3D12 debug layer ID 553 (`EXECUTECOMMANDLISTS_COMMANDLIST_STILL_IN_FLIGHT`) and device removal on strict drivers. - Add `title_list::primary_of`, `title_list::switch_active_for_kickoff`, and `title_list::mark_kickoff_unreset` to switch `F_BACKING` / `WRAPPER_OF` / `book.first` / `book.spares` after `Reset` succeeds while keeping `F_PRIMARY` out of `book.first` and `book.spares`, and suppress `push_segments` (with empty-batch early return in `execute_with_waits_on`) if `KickoffX` cannot obtain/reset an idle target until the title's next `Reset`. - Extract shared `list_x::create_closed_spare` (used by both `reset_stand_in` and `kickoff_x`) so a spare whose initial `Close` fails is released instead of pooled, and `list_x::acquire_kickoff_target` so `kickoff_x` never falls back to resetting `current_target` while it is still in flight (trying idle `primary`, all idle pooled spares, then a fresh spare, and leaving `current_target` closed + unreset on failure). - Update `list_x::kickoff_x` to preserve `bind` / `ListState` / DRED pipeline state onto the new active list, clear retired segment state, and guard `init_pso` and predication restore for `COPY` command lists. - Add unit tests covering `switch_active_for_kickoff` and `kickoff_unreset` invariants, back-to-back in-flight `KickoffX` + return to `primary` after drain, `KickoffX` after a mid-list `Write64 -> Wait64` split, and gated-queue no-spare / spare-`Reset` failure paths. Assisted-by: Google Antigravity (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/list_x.rs | 497 +++++++++++++++++++++--- crates/xcl/src/hle/d3d12x/title_list.rs | 114 ++++++ 2 files changed, 552 insertions(+), 59 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..13609f95c6 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2094,7 +2094,10 @@ pub(super) unsafe fn cpu_writes_after(queue: *mut c_void, lists: &[*mut c_void]) /// When `queue` is non-null, monotonic 64-bit waits backed by a recorded `Write64BitValue*` are /// bridged to GPU-side `queue.Wait` without blocking the submitter thread on the CPU. pub(super) unsafe fn execute_with_waits_on(queue: *mut c_void, lists: &[*mut c_void], mut submit: impl FnMut(&[*mut c_void])) { - if lists.is_empty() || PENDING_WAIT_LISTS.load(Ordering::Acquire) == 0 { + if lists.is_empty() { + return; + } + if PENDING_WAIT_LISTS.load(Ordering::Acquire) == 0 { submit(lists); return; } @@ -2586,6 +2589,24 @@ pub(super) unsafe extern "system" fn write64_bottom_x(this: *mut c_void, va: u64 write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, W); } +/// Create a new adopted PC command list matching `template`'s device and type, closed and ready +/// for `Reset`. Releases and forgets the list instead of pooling it if `Close` fails. +unsafe fn create_closed_spare(template: *mut c_void, ty: D3D12_COMMAND_LIST_TYPE, alloc: usize) -> Option<*mut c_void> { + let pc_alloc = super::alloc_pool::current(alloc); + let made = real::create_raw_pc_command_list(device_of(template)?.as_raw(), ty, pc_alloc, 0)?; + let ext = *(made as *const usize); + let hr = pc:: i32>(made, PC_CLOSE)(made); + real::preserve_vtable(made, ext); + if hr < 0 { + super::bind::forget(made); + let rel: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(made as *const *const usize)).add(2)); + rel(made); + return None; + } + on_create(made, pc_alloc, 0, ty); + Some(made) +} + /// The title Resets its list (`title_list` `wrapper`) while the GPU has not finished the last /// submit of the PC list `primary` behind it: a console lets the title submit the new recording /// at once, the PC does not let `primary` be submitted again yet (#1452, debug layer id 553; @@ -2598,20 +2619,7 @@ pub(super) unsafe fn reset_stand_in(wrapper: *mut c_void, primary: *mut c_void, } let spare = match super::title_list::pop_spare(wrapper) { Some(p) => p, - None => { - let pc_alloc = super::alloc_pool::current(alloc); - let made = real::create_raw_pc_command_list(device_of(primary)?.as_raw(), ty, pc_alloc, 0)?; - on_create(made, pc_alloc, 0, ty); - // (closed: it is reset below like a spare) - let ext = *(made as *const usize); - let hr = pc:: i32>(made, PC_CLOSE)(made); - real::preserve_vtable(made, ext); - if hr < 0 { - super::title_list::put_spare(wrapper, made); - return None; - } - made - } + None => create_closed_spare(primary, ty, alloc)?, }; if reset_pc(spare, alloc, pso) < 0 { super::title_list::put_spare(wrapper, spare); @@ -3245,6 +3253,96 @@ pub unsafe extern "system" fn set_predication_buffer_x(this: *mut c_void, desc: apply_predication_copy(this, uma_buf, off, pb, D3D12_PREDICATION_OP(op_raw as i32)); } +/// Select and `Reset` the next active PC command list after `KickoffX` has submitted +/// `current_target`. Preserves the idle-list invariant (#1452, D3D12 debug layer id 553): never +/// calls `reset_one(current_target)` while `in_flight(current_target)` is true. On failure, leaves +/// `current_target` closed and marks `wrapper` unreset so neither a subsequent `KickoffX` nor +/// `ExecuteCommandLists` can resubmit the already-submitted in-flight recording. +unsafe fn acquire_kickoff_target( + wrapper: Option<*mut c_void>, + current_target: *mut c_void, + mut in_flight: impl FnMut(*mut c_void) -> bool, + mut create_spare: impl FnMut() -> Option<*mut c_void>, + mut reset_one: impl FnMut(*mut c_void) -> i32, +) -> Result<*mut c_void, i32> { + const E_OUTOFMEMORY: i32 = 0x8007_000Eu32 as i32; + let Some(w) = wrapper else { + let hr = reset_one(current_target); + return if hr >= 0 { Ok(current_target) } else { Err(hr) }; + }; + let primary = super::title_list::primary_of(w); + let mut last_err: Option = None; + + if current_target != primary && !in_flight(primary) { + let hr = reset_one(primary); + if hr >= 0 { + super::title_list::switch_active_for_kickoff(w, current_target, primary); + return Ok(primary); + } + last_err = Some(hr); + } + + if !in_flight(current_target) { + let hr = reset_one(current_target); + if hr >= 0 { + return Ok(current_target); + } + super::title_list::mark_kickoff_unreset(w); + return Err(hr); + } + + let mut failed_spares = Vec::new(); + let mut switched = None; + while let Some(cand) = super::title_list::pop_spare(w) { + if cand == current_target || in_flight(cand) { + failed_spares.push(cand); + continue; + } + let hr = reset_one(cand); + if hr >= 0 { + switched = Some(cand); + break; + } + last_err = Some(hr); + failed_spares.push(cand); + } + if switched.is_none() { + if let Some(cand) = create_spare() { + if cand != current_target && !in_flight(cand) { + let hr = reset_one(cand); + if hr >= 0 { + switched = Some(cand); + } else { + last_err = Some(hr); + failed_spares.push(cand); + } + } else { + failed_spares.push(cand); + } + } + } + for sp in failed_spares { + if sp != primary && sp != current_target { + super::title_list::put_spare(w, sp); + } + } + if let Some(cand) = switched { + super::title_list::switch_active_for_kickoff(w, current_target, cand); + return Ok(cand); + } + + if !in_flight(current_target) { + let hr = reset_one(current_target); + if hr >= 0 { + return Ok(current_target); + } + super::title_list::mark_kickoff_unreset(w); + return Err(hr); + } + super::title_list::mark_kickoff_unreset(w); + Err(last_err.unwrap_or(E_OUTOFMEMORY)) +} + /// command list #93 KickoffX(): flush commands recorded so far by closing the list, submitting /// it to a compatible live command queue, and resetting it on the same command allocator so /// recording can continue seamlessly until the title's normal `Close` + `ExecuteCommandLists`. @@ -3263,7 +3361,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { return; } let dev_raw = device_of(this).map_or(0, |d| d.as_raw() as usize); - let (alloc, queue, bound_pso, depth_bias, index_buffer, depth_bounds, predication, pred_buf, pred_in_state, active_query, waits, sparse_writes) = { + let (alloc, queue, last_queue, bound_pso, depth_bias, index_buffer, depth_bounds, predication, pred_buf, pred_in_state, active_query, waits, sparse_writes) = { let mut g = LISTS.lock().unwrap(); let Some(s) = g.as_mut().and_then(|m| m.get_mut(&(this as usize))) else { if once(&NO_QUEUE, 4) { @@ -3296,7 +3394,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let cpu_writes = std::mem::take(&mut s.cpu_writes); let pred_in_state = std::mem::replace(&mut s.pred_in_state, false); let pred_buf = s.pred_buf.as_ref().map(|r| r.as_raw() as usize); - (s.allocator, q, s.bound_pso, s.depth_bias, s.index_buffer, s.depth_bounds, s.predication, pred_buf, pred_in_state, active_query, waits, (sparse_writes, write64_signals, cpu_writes)) + (s.allocator, q, s.last_queue, s.bound_pso, s.depth_bias, s.index_buffer, s.depth_bounds, s.predication, pred_buf, pred_in_state, active_query, waits, (sparse_writes, write64_signals, cpu_writes)) }; let (sparse_writes, write64_signals, cpu_writes) = sparse_writes; @@ -3325,8 +3423,9 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void); let exec: ExecFn = transmute_copy(&real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS)); - // If this list was previously split mid-recording around a `Wait64`, flush all earlier closed - // segments in exact order before submitting `this`, then resume recording on `primary`. + // Capture wrapper before `reclaim_for_kickoff` updates `WRAPPER_OF`. If this list was split + // mid-recording around a `Wait64`, flush earlier closed segments in order before `this`. + let wrapper = super::title_list::wrapper_of(this); let kickoff_split = super::title_list::reclaim_for_kickoff(this); if let Some((_, ref prior, _)) = kickoff_split { if !prior.is_empty() { @@ -3364,7 +3463,40 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { } super::cpu_writes::after(q_raw, cpu_writes); - let target = if let Some((primary, _, reclaimed)) = kickoff_split { + let (current_target, prior_retired, reclaimed) = match kickoff_split { + Some((primary, prior, reclaimed)) => (primary, prior, reclaimed), + None => (this, Vec::new(), Vec::new()), + }; + let init_pso = if ty == D3D12_COMMAND_LIST_TYPE_COPY { 0 } else { super::pso_async::resolve_for_bind(bound_pso) }; + let reset_one = |pc_list: *mut c_void| -> i32 { + let ext_closed = *(pc_list as *const usize); + let hr = super::alloc_pool::reset_list(pc_list as usize, alloc, list_type(pc_list).0, |pc_alloc| { + pc:: i32>(pc_list, PC_RESET)(pc_list, pc_alloc, init_pso) + }); + real::preserve_vtable(pc_list, ext_closed); + hr + }; + // Avoid resuming on a PC command list whose submit is still in flight on the GPU (#1452, + // debug layer id 553): pick `primary` once idle, else an idle spare, else a fresh closed spare. + // If no idle candidate can be created or reset while `current_target` is in flight, leave it + // closed and suppress `push_segments` until the title's next `Reset`. + let target = match acquire_kickoff_target( + wrapper, + current_target, + |p| super::alloc_pool::in_flight(p as usize), + || create_closed_spare(current_target, ty, alloc), + reset_one, + ) { + Ok(t) => t, + Err(reset_hr) => { + return warn(&ERR, "KickoffX", format_args!("Reset after kickoff failed ({reset_hr:#x})")); + } + }; + super::dred::reset(target); + if init_pso != 0 { + super::dred::mark_pipeline(target, bound_pso, init_pso); + } + if target != this { let (ended_early, pred_buf_obj, bindings_copy) = { let mut g = LISTS.lock().unwrap(); if let Some(s) = g.as_mut().and_then(|m| m.get_mut(&(this as usize))) { @@ -3373,41 +3505,32 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { (Vec::new(), None, Bindings::default()) } }; - super::bind::inherit_for_split(this, primary, alloc); - for seg in reclaimed { - super::bind::reset(seg as *mut c_void, 0, 0); - clear_segment_state(seg); + super::bind::inherit_for_split(this, target, alloc); + for seg in reclaimed.into_iter().chain(prior_retired.into_iter().map(|p| p as usize)).chain([this as usize]) { + if seg != target as usize { + super::bind::reset(seg as *mut c_void, 0, 0); + clear_segment_state(seg); + } } - if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(primary as usize))) { - s.allocator = alloc; - s.bound_pso = bound_pso; - s.depth_bias = depth_bias; - s.index_buffer = index_buffer; - s.depth_bounds = depth_bounds; - s.predication = predication; - s.pred_buf = pred_buf_obj; - s.pred_in_state = false; - s.occlusion = Occlusion { active: None, ended_early }; - s.written_ranges.clear(); - s.clear_sparse_writes(); - s.clear_write64_signals(); - s.clear_waits(); - s.cpu_writes.clear(); - s.bindings = bindings_copy; - } - primary - } else { - this - }; - - let ext_closed = *(target as *const usize); - let init_pso = super::pso_async::resolve_for_bind(bound_pso); - let reset_hr = super::alloc_pool::reset_list(target as usize, alloc, list_type(target).0, |pc_alloc| { - pc:: i32>(target, PC_RESET)(target, pc_alloc, init_pso) - }); - real::preserve_vtable(target, ext_closed); - if reset_hr < 0 { - return warn(&ERR, "KickoffX", format_args!("Reset after kickoff failed ({reset_hr:#x})")); + let mut g = LISTS.lock().unwrap(); + let s = g.get_or_insert_with(HashMap::new).entry(target as usize).or_default(); + s.allocator = alloc; + s.list_type = Some(ty); + s.last_queue = last_queue.or(Some(queue)); + s.bound_pso = bound_pso; + s.depth_bias = depth_bias; + s.index_buffer = index_buffer; + s.depth_bounds = depth_bounds; + s.predication = predication; + s.pred_buf = pred_buf_obj; + s.pred_in_state = false; + s.occlusion = Occlusion { active: None, ended_early }; + s.written_ranges.clear(); + s.clear_sparse_writes(); + s.clear_write64_signals(); + s.clear_waits(); + s.cpu_writes.clear(); + s.bindings = bindings_copy; } reset_copy_scratch(target); let bindings = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(target as usize))).map(|s| s.bindings.clone()); @@ -3457,11 +3580,13 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { pc::(target, PC_OM_SET_DEPTH_BOUNDS)(target, min, max); } } - if let (Some(op), Some(pb)) = (predication, pred_buf) { - transition(target, pb, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_PREDICATION); - pc::(target, PC_SET_PREDICATION)(target, pb, 0, op); - if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(target as usize))) { - s.pred_in_state = true; + if ty != D3D12_COMMAND_LIST_TYPE_COPY { + if let (Some(op), Some(pb)) = (predication, pred_buf) { + transition(target, pb, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_PREDICATION); + pc::(target, PC_SET_PREDICATION)(target, pb, 0, op); + if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(target as usize))) { + s.pred_in_state = true; + } } } } @@ -7432,4 +7557,258 @@ mod tests { assert!(got.iter().all(|&v| v == 0xDEAD_BEEF), "the UAV was not cleared: {:x?}", &got[..4]); } } + + fn test_device_or_warp() -> Option { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + if let Ok(dev) = crate::gpu::agility::create_device(None, D3D_FEATURE_LEVEL_11_0) { + return Some(dev); + } + unsafe { + let factory: IDXGIFactory4 = CreateDXGIFactory1().ok()?; + let warp: IDXGIAdapter = factory.EnumWarpAdapter().ok()?; + let mut dev: Option = None; + D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).ok()?; + dev + } + } + + unsafe fn kickoff_test_setup(dev: &ID3D12Device) -> (ID3D12CommandQueue, ID3D12CommandAllocator, *mut c_void, *mut c_void, ID3D12Fence, ID3D12Fence) { + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + let q_raw = q.as_raw(); + real::adopt(q_raw, real::Kind::CommandQueue); + on_queue_create(q_raw as usize, dev.as_raw() as usize, ty); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let a = alloc.as_raw() as usize; + let primary = dev.CreateCommandList::<_, _, ID3D12GraphicsCommandList>(0, ty, &alloc, None).unwrap().into_raw(); + real::adopt(primary, real::Kind::CommandList); + on_create(primary, a, 0, ty); + super::super::bind::reset(primary, a, 0); + if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(primary as usize))) { + s.last_queue = Some(q_raw as usize); + } + let w = super::super::title_list::wrap(primary); + let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + (q, alloc, primary, w, gate, done) + } + + static KICKOFF_GPU_TEST_LOCK: Mutex<()> = Mutex::new(()); + + /// Back-to-back `KickoffX` and final `ExecuteCommandLists` while the queue is gated on an + /// unsignalled fence must switch to idle spare PC lists (`note_resubmits == 0`), preserve + /// bindings across each kickoff, and return to `primary` once the GPU finishes (#1452). + #[test] + fn kickoff_x_while_in_flight_switches_to_idle_spare_and_preserves_state() { + let _guard = KICKOFF_GPU_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let Some(dev) = test_device_or_warp() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; + unsafe { + let (q, _alloc, primary, w, gate, done) = kickoff_test_setup(&dev); + let q_raw = q.as_raw(); + if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(primary as usize))) { + (s.bindings.stencil, s.bindings.topology) = (Some(0x5A), Some(4)); + } + let close: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(w as *const *const usize)).add(9)); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS)); + let segs = || { let mut v = Vec::new(); assert!(super::super::title_list::push_segments(w, &mut v)); v }; + + q.Wait(&gate, 1).unwrap(); + assert_eq!(segs(), vec![primary]); + // KickoffX #1 & #2 while gated: must switch primary -> s1 -> s2 with 0 resubmits and keep bindings. + kickoff_x(primary); + let s1 = segs(); + assert_eq!(s1.len(), 1); + let resub1 = super::super::alloc_pool::note_resubmits(q_raw, &s1); + let state1 = { + let g = LISTS.lock().unwrap(); + let s = g.as_ref().unwrap().get(&(s1[0] as usize)).unwrap(); + (s.bindings.stencil, s.bindings.topology, s.last_queue) + }; + kickoff_x(s1[0]); + let s2 = segs(); + assert_eq!(s2.len(), 1); + let resub2 = super::super::alloc_pool::note_resubmits(q_raw, &s2); + let state2 = { + let g = LISTS.lock().unwrap(); + let s = g.as_ref().unwrap().get(&(s2[0] as usize)).unwrap(); + (s.bindings.stencil, s.bindings.topology) + }; + // Unblock GPU so primary & s1 drain; KickoffX #3 on s2 must switch directly back to primary. + gate.Signal(1).unwrap(); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("queue drains"); + kickoff_x(s2[0]); + let after_drain = segs(); + assert_eq!(close(w), 0); + let resub3 = super::super::alloc_pool::note_resubmits(q_raw, &after_drain); + exec(q_raw, after_drain.len() as u32, after_drain.as_ptr()); + super::super::alloc_pool::executed(q_raw, &after_drain); + q.Signal(&done, 2).unwrap(); + crate::gpu::fence::wait(&done, 2, &dev, None, 5000).expect("queue drains 2"); + let removed = dev.GetDeviceRemovedReason(); + assert_eq!(release(w), 0); + drop(q); + + assert_ne!(s1[0], primary, "KickoffX #1 must not keep recording on in-flight primary"); + assert_eq!((resub1, state1), (0, (Some(0x5A), Some(4), Some(q_raw as usize)))); + assert!(s2[0] != primary && s2[0] != s1[0], "KickoffX #2 must use a third distinct PC list"); + assert_eq!((resub2, state2), (0, (Some(0x5A), Some(4)))); + assert_eq!((after_drain, resub3), (vec![primary], 0)); + assert!(removed.is_ok()); + } + } + + /// `KickoffX` on a list split around a mid-list `Write64 -> Wait64` submits both `primary` + /// and the split segment; while in flight it must resume on an idle spare (`note_resubmits == 0`). + #[test] + fn kickoff_x_after_midlist_split_switches_to_idle_spare() { + let _guard = KICKOFF_GPU_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let Some(dev) = test_device_or_warp() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; + unsafe { + let (q, _alloc, primary, w, gate, done) = kickoff_test_setup(&dev); + let q_raw = q.as_raw(); + let close: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(w as *const *const usize)).add(9)); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS)); + + let cell = Box::new(AtomicU64::new(0)); + let va = &*cell as *const AtomicU64 as u64; + note_write64_recorded(primary, va, 1); + wait64_x(primary, va, D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32, 1, 0, 0); + let mut split_segs = Vec::new(); + assert!(super::super::title_list::push_segments(w, &mut split_segs)); + assert_eq!(split_segs.len(), 2); + let seg1 = split_segs[1]; + assert!(split_segs[0] == primary && seg1 != primary); + + q.Wait(&gate, 1).unwrap(); + kickoff_x(seg1); + let mut after = Vec::new(); + assert!(super::super::title_list::push_segments(w, &mut after)); + assert_eq!((after.len(), close(w)), (1, 0)); + let resub = super::super::alloc_pool::note_resubmits(q_raw, &after); + exec(q_raw, after.len() as u32, after.as_ptr()); + super::super::alloc_pool::executed(q_raw, &after); + + gate.Signal(1).unwrap(); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("split kickoff drains"); + let removed = dev.GetDeviceRemovedReason(); + assert_eq!(release(w), 0); + drop(q); + if let Some(gr) = SYNC_GRAPH.lock().unwrap().as_mut() { + gr.cells.remove(&va); + } + assert!(after[0] != primary && after[0] != seg1, "split KickoffX must resume on an idle spare"); + assert_eq!(resub, 0); + assert!(removed.is_ok()); + } + } + + /// Failure paths while `primary` is gated in flight (no spare / create failure, and spare + /// `Reset` failure) must never fall back to resetting `primary` even when `Reset` on `primary` + /// would return `S_OK`, must keep `push_segments` empty so neither a follow-up `KickoffX` nor + /// `ExecuteCommandLists` resubmits it, and must recover cleanly on a second idle spare or + /// post-drain `Reset`. + #[test] + fn kickoff_x_failure_paths_never_reset_in_flight_target() { + const E_OUTOFMEMORY: i32 = 0x8007_000Eu32 as i32; + let _guard = KICKOFF_GPU_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let Some(dev) = test_device_or_warp() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; + unsafe { + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let (q, alloc, primary, w, gate, done) = kickoff_test_setup(&dev); + let (q_raw, a) = (q.as_raw(), alloc.as_raw() as usize); + let close: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(w as *const *const usize)).add(9)); + let reset_w: unsafe extern "system" fn(*mut c_void, usize, usize) -> i32 = std::mem::transmute(*(*(w as *const *const usize)).add(10)); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS)); + let segs = || { let mut v = Vec::new(); assert!(super::super::title_list::push_segments(w, &mut v)); v }; + + // Gate the queue, close and submit `primary` so it is genuinely closed and in flight. + q.Wait(&gate, 1).unwrap(); + assert_eq!(close(w), 0); + let one = [primary]; + exec(q_raw, 1, one.as_ptr()); + super::super::alloc_pool::executed(q_raw, &one); + assert!(super::super::alloc_pool::in_flight(primary as usize)); + + // 1. No pooled spare and `create_spare` fails (`None`): must never call `reset_one(primary)` + // even though `reset_one` would return `S_OK`, and must suppress subsequent submits. + let mut reset_calls = Vec::new(); + let res_no_spare = acquire_kickoff_target( + Some(w), + primary, + |p| super::super::alloc_pool::in_flight(p as usize), + || None, + |p| { reset_calls.push(p); 0 }, + ); + assert_eq!(res_no_spare, Err(E_OUTOFMEMORY)); + assert!(reset_calls.is_empty(), "in-flight primary must never be reset when spare creation fails"); + let empty = segs(); + assert!(empty.is_empty(), "unreset kickoff wrapper must not expose in-flight primary to ExecuteCommandLists"); + assert!(close(w) < 0, "in-flight primary must remain closed"); + kickoff_x(primary); + assert!(segs().is_empty()); + let mut exec_batches = 0usize; + execute_with_waits_on(q_raw, &empty, |_| exec_batches += 1); + assert_eq!(exec_batches, 0); + + // 2. Pooled idle spare `s_fail` fails `Reset`: must never fall back to in-flight `primary`, + // must return `s_fail` to the spare pool, and must keep `push_segments` empty. + let s_fail = create_closed_spare(primary, ty, a).expect("spare 1"); + super::super::title_list::put_spare(w, s_fail); + reset_calls.clear(); + let res_spare_err = acquire_kickoff_target( + Some(w), + primary, + |p| super::super::alloc_pool::in_flight(p as usize), + || None, + |p| { reset_calls.push(p); if p == s_fail { E_OUTOFMEMORY } else { 0 } }, + ); + assert_eq!(res_spare_err, Err(E_OUTOFMEMORY)); + assert_eq!(reset_calls, vec![s_fail], "spare Reset failure must not fall back to in-flight primary"); + assert!(segs().is_empty()); + + // 3. With a second idle spare `s_good` behind `s_fail`, `acquire_kickoff_target` skips + // `s_fail`, switches to `s_good`, and keeps `s_fail` pooled with 0 resubmits. + assert_eq!(super::super::title_list::pop_spare(w), Some(s_fail)); + let s_good = create_closed_spare(primary, ty, a).expect("spare 2"); + super::super::title_list::put_spare(w, s_good); + super::super::title_list::put_spare(w, s_fail); + reset_calls.clear(); + let res_recovered = acquire_kickoff_target( + Some(w), + primary, + |p| super::super::alloc_pool::in_flight(p as usize), + || None, + |p| { reset_calls.push(p); if p == s_fail { E_OUTOFMEMORY } else { reset_pc(p, a, 0) } }, + ); + assert_eq!(res_recovered, Ok(s_good)); + assert_eq!(reset_calls, vec![s_fail, s_good]); + let segs_good = segs(); + assert_eq!(segs_good, vec![s_good]); + assert_eq!(super::super::alloc_pool::note_resubmits(q_raw, &segs_good), 0); + assert_eq!(close(w), 0); + + // 4. Mark unreset, drain the GPU, and verify normal `Reset` recovers `primary` cleanly. + super::super::title_list::mark_kickoff_unreset(w); + assert!(segs().is_empty()); + gate.Signal(1).unwrap(); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("failure-path queue drains"); + assert_eq!(reset_w(w, a, 0), 0); + assert_eq!(segs(), vec![primary]); + assert_eq!(close(w), 0); + let removed = dev.GetDeviceRemovedReason(); + assert_eq!(release(w), 0); + drop(q); + assert!(removed.is_ok()); + } + } } diff --git a/crates/xcl/src/hle/d3d12x/title_list.rs b/crates/xcl/src/hle/d3d12x/title_list.rs index a41e5643fc..18651d78dd 100644 --- a/crates/xcl/src/hle/d3d12x/title_list.rs +++ b/crates/xcl/src/hle/d3d12x/title_list.rs @@ -50,6 +50,9 @@ struct SegmentBook { /// finished primary's last submit, and the PC does not let a list be submitted again before /// that (#1452, debug layer id 553). A console does. first: Option, + /// `KickoffX` already submitted the recording and could not reset an idle target; suppress + /// `push_segments` until the title's next `Reset` starts a new recording. + kickoff_unreset: bool, } /// Wrapper address -> split segment book (populated lazily only for lists that split). @@ -175,6 +178,7 @@ pub(super) unsafe extern "system" fn reset(this: *mut c_void, alloc: usize, pso: let m = sg.get_or_insert_with(HashMap::new); if stand_in.is_some() || m.contains_key(&(this as usize)) { let b = m.entry(this as usize).or_default(); + b.kickoff_unreset = false; let mut old = std::mem::take(&mut b.extra_active); old.extend(std::mem::replace(&mut b.first, stand_in)); b.spares.extend(old.iter().copied()); @@ -252,6 +256,9 @@ pub fn push_segments(list: *mut c_void, out: &mut Vec<*mut c_void>) -> bool { }; let sg = SEGMENTS.lock().unwrap(); let book = sg.as_ref().and_then(|m| m.get(&(list as usize))); + if book.is_some_and(|b| b.kickoff_unreset) { + return true; + } out.push(book.and_then(|b| b.first).map_or(primary, |f| f as *mut c_void)); if let Some(book) = book { for &seg in &book.extra_active { @@ -269,6 +276,48 @@ pub(super) fn wrapper_of(pc: *mut c_void) -> Option<*mut c_void> { WRAPPER_OF.lock().unwrap().as_ref()?.get(&(pc as usize)).map(|&w| w as *mut c_void) } +/// The primary PC list behind `wrapper` (`F_PRIMARY`). +pub(super) unsafe fn primary_of(wrapper: *mut c_void) -> *mut c_void { + *((wrapper as usize + F_PRIMARY) as *const *mut c_void) +} + +/// Commit a `KickoffX` active-list switch on `wrapper` after `new_active` has been reset. +/// Keeps `F_PRIMARY` out of `book.first` / `book.spares`, moves any retired non-primary +/// active list into `book.spares`, and updates `F_BACKING` + `WRAPPER_OF`. +pub(super) unsafe fn switch_active_for_kickoff(wrapper: *mut c_void, old_active: *mut c_void, new_active: *mut c_void) { + if old_active == new_active { + return; + } + let primary = primary_of(wrapper) as usize; + let (old, new) = (old_active as usize, new_active as usize); + { + let mut sg = SEGMENTS.lock().unwrap(); + let b = sg.get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default(); + b.kickoff_unreset = false; + if old != primary { + if b.first == Some(old) { + b.first = None; + } + if !b.spares.contains(&old) { + b.spares.push(old); + } + } + b.first = (new != primary).then_some(new); + } + *((wrapper as usize + F_BACKING) as *mut usize) = new; + let mut wg = WRAPPER_OF.lock().unwrap(); + let m = wg.get_or_insert_with(HashMap::new); + m.remove(&old); + m.insert(new, wrapper as usize); +} + +/// Mark `wrapper` as having no open recording after `KickoffX` submitted its active PC list and +/// failed to obtain/reset an idle target; `push_segments` emits nothing until `Reset` clears it. +pub(super) fn mark_kickoff_unreset(wrapper: *mut c_void) { + let mut sg = SEGMENTS.lock().unwrap(); + sg.get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().kickoff_unreset = true; +} + /// Whether `wrapper` may allocate/activate `n` more split segments for this recording. pub(super) fn can_split(wrapper: *mut c_void, n: usize) -> bool { let sg = SEGMENTS.lock().unwrap(); @@ -641,4 +690,69 @@ mod tests { drop(Box::from_raw(seg1)); } } + + #[test] + fn switch_active_for_kickoff_keeps_primary_out_of_spares() { + let mut vt = vec![0usize; LIST_SLOTS]; + vt[2] = fake_release as usize; + let primary = Box::into_raw(Box::new(Fake { vt: vt.as_ptr(), refs: 1 })); + let spare1 = Box::into_raw(Box::new(Fake { vt: vt.as_ptr(), refs: 1 })); + let spare2 = Box::into_raw(Box::new(Fake { vt: vt.as_ptr(), refs: 1 })); + let (p, s1, s2) = (primary as *mut c_void, spare1 as *mut c_void, spare2 as *mut c_void); + unsafe { + let w = wrap(p); + assert_eq!(primary_of(w), p); + switch_active_for_kickoff(w, p, s1); + assert_eq!((wrapper_of(p), wrapper_of(s1)), (None, Some(w))); + let mut segs = Vec::new(); + assert!(push_segments(w, &mut segs) && segs == vec![s1]); + switch_active_for_kickoff(w, s1, s2); + assert_eq!((wrapper_of(s1), wrapper_of(s2)), (None, Some(w))); + segs.clear(); + assert!(push_segments(w, &mut segs) && segs == vec![s2]); + switch_active_for_kickoff(w, s2, p); + assert_eq!((wrapper_of(s2), wrapper_of(p)), (None, Some(w))); + segs.clear(); + assert!(push_segments(w, &mut segs) && segs == vec![p]); + { + let sg = SEGMENTS.lock().unwrap(); + let b = sg.as_ref().unwrap().get(&(w as usize)).unwrap(); + assert_eq!(b.first, None); + assert!(!b.spares.contains(&(p as usize))); + assert_eq!(b.spares, vec![s1 as usize, s2 as usize]); + } + let rel: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + assert_eq!(rel(w), 0); + assert_eq!(((*primary).refs, (*spare1).refs, (*spare2).refs), (0, 0, 0)); + drop(Box::from_raw(primary)); + drop(Box::from_raw(spare1)); + drop(Box::from_raw(spare2)); + } + } + + #[test] + fn kickoff_unreset_suppresses_push_segments_until_reset() { + unsafe extern "system" fn fake_reset_ok(_this: *mut c_void, _alloc: usize, _pso: usize) -> HRESULT { + 0 + } + let mut vt = vec![0usize; LIST_SLOTS]; + vt[2] = fake_release as usize; + vt[10] = fake_reset_ok as usize; + let primary = Box::into_raw(Box::new(Fake { vt: vt.as_ptr(), refs: 1 })); + let p = primary as *mut c_void; + unsafe { + let w = wrap(p); + let mut segs = Vec::new(); + assert!(push_segments(w, &mut segs) && segs == vec![p]); + mark_kickoff_unreset(w); + segs.clear(); + assert!(push_segments(w, &mut segs) && segs.is_empty()); + assert_eq!(reset(w, 0, 0), 0); + assert!(push_segments(w, &mut segs) && segs == vec![p]); + let rel: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + assert_eq!(rel(w), 0); + assert_eq!((*primary).refs, 0); + drop(Box::from_raw(primary)); + } + } } From 7f4c1fc6ea2156317bfcda0a1dfb4cb6ad16f60e Mon Sep 17 00:00:00 2001 From: Decodere <98278826+Decodere@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:00:30 +0300 Subject: [PATCH 2/4] D3D12.X: add XCL_KICKOFF_SWAP=0 opt-out switch for KickoffX list switching Address @throny's pre-merge review on #2320: - Add `kickoff_swap_enabled_val` / `kickoff_swap_enabled` (`XCL_KICKOFF_SWAP=0`, default ON, matching `alloc_pool::enabled()`'s `XCL_ALLOC_SWAP=0` idiom) in `crates/xcl/src/hle/d3d12x/list_x.rs`. - When `XCL_KICKOFF_SWAP=0`, `kickoff_x` skips `title_list::wrapper_of` and `acquire_kickoff_target` bypasses `in_flight` / spare-pool scanning / `switch_active_for_kickoff` / `mark_kickoff_unreset` (and the `target != this` state-inheritance / segment-reset overhead when unsplit), resetting `current_target` directly while leaving `XCL_ALLOC_SWAP` unaffected. - Add unit tests `kickoff_swap_enabled_val_defaults_on_and_disables_on_zero` and `kickoff_swap_disabled_keeps_current_target_without_spares`. Follow-up to #2163 Part of #1452 Assisted-by: Google Antigravity (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/list_x.rs | 119 ++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 13609f95c6..e9d1a0a616 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -3253,20 +3253,34 @@ pub unsafe extern "system" fn set_predication_buffer_x(this: *mut c_void, desc: apply_predication_copy(this, uma_buf, off, pb, D3D12_PREDICATION_OP(op_raw as i32)); } +fn kickoff_swap_enabled_val(val: Option<&str>) -> bool { + !val.is_some_and(|v| v.trim() == "0") +} + +/// `XCL_KICKOFF_SWAP=0` disables switching `KickoffX` onto an idle spare PC list, falling back to +/// resetting `current_target` directly without affecting `XCL_ALLOC_SWAP`. +fn kickoff_swap_enabled() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| kickoff_swap_enabled_val(std::env::var("XCL_KICKOFF_SWAP").ok().as_deref())) +} + /// Select and `Reset` the next active PC command list after `KickoffX` has submitted -/// `current_target`. Preserves the idle-list invariant (#1452, D3D12 debug layer id 553): never -/// calls `reset_one(current_target)` while `in_flight(current_target)` is true. On failure, leaves -/// `current_target` closed and marks `wrapper` unreset so neither a subsequent `KickoffX` nor -/// `ExecuteCommandLists` can resubmit the already-submitted in-flight recording. +/// `current_target`. When `swap_enabled` is false (`XCL_KICKOFF_SWAP=0`), resets `current_target` +/// directly without touching spares or switching active backing lists. Otherwise preserves the +/// idle-list invariant (#1452, D3D12 debug layer id 553): never calls `reset_one(current_target)` +/// while `in_flight(current_target)` is true. On failure, leaves `current_target` closed and marks +/// `wrapper` unreset so neither a subsequent `KickoffX` nor `ExecuteCommandLists` can resubmit +/// the already-submitted in-flight recording. unsafe fn acquire_kickoff_target( wrapper: Option<*mut c_void>, current_target: *mut c_void, + swap_enabled: bool, mut in_flight: impl FnMut(*mut c_void) -> bool, mut create_spare: impl FnMut() -> Option<*mut c_void>, mut reset_one: impl FnMut(*mut c_void) -> i32, ) -> Result<*mut c_void, i32> { const E_OUTOFMEMORY: i32 = 0x8007_000Eu32 as i32; - let Some(w) = wrapper else { + let Some(w) = wrapper.filter(|_| swap_enabled) else { let hr = reset_one(current_target); return if hr >= 0 { Ok(current_target) } else { Err(hr) }; }; @@ -3423,9 +3437,11 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void); let exec: ExecFn = transmute_copy(&real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS)); - // Capture wrapper before `reclaim_for_kickoff` updates `WRAPPER_OF`. If this list was split - // mid-recording around a `Wait64`, flush earlier closed segments in order before `this`. - let wrapper = super::title_list::wrapper_of(this); + // Capture wrapper before `reclaim_for_kickoff` updates `WRAPPER_OF` (skipped when + // `XCL_KICKOFF_SWAP=0`). If this list was split mid-recording around a `Wait64`, flush + // earlier closed segments in order before `this`. + let swap_enabled = kickoff_swap_enabled(); + let wrapper = swap_enabled.then(|| super::title_list::wrapper_of(this)).flatten(); let kickoff_split = super::title_list::reclaim_for_kickoff(this); if let Some((_, ref prior, _)) = kickoff_split { if !prior.is_empty() { @@ -3483,6 +3499,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let target = match acquire_kickoff_target( wrapper, current_target, + swap_enabled, |p| super::alloc_pool::in_flight(p as usize), || create_closed_spare(current_target, ty, alloc), reset_one, @@ -7744,6 +7761,7 @@ mod tests { let res_no_spare = acquire_kickoff_target( Some(w), primary, + true, |p| super::super::alloc_pool::in_flight(p as usize), || None, |p| { reset_calls.push(p); 0 }, @@ -7767,6 +7785,7 @@ mod tests { let res_spare_err = acquire_kickoff_target( Some(w), primary, + true, |p| super::super::alloc_pool::in_flight(p as usize), || None, |p| { reset_calls.push(p); if p == s_fail { E_OUTOFMEMORY } else { 0 } }, @@ -7785,6 +7804,7 @@ mod tests { let res_recovered = acquire_kickoff_target( Some(w), primary, + true, |p| super::super::alloc_pool::in_flight(p as usize), || None, |p| { reset_calls.push(p); if p == s_fail { E_OUTOFMEMORY } else { reset_pc(p, a, 0) } }, @@ -7811,4 +7831,87 @@ mod tests { assert!(removed.is_ok()); } } + + #[test] + fn kickoff_swap_enabled_val_defaults_on_and_disables_on_zero() { + assert!(kickoff_swap_enabled_val(None)); + assert!(kickoff_swap_enabled_val(Some(""))); + assert!(kickoff_swap_enabled_val(Some("1"))); + assert!(kickoff_swap_enabled_val(Some("true"))); + assert!(!kickoff_swap_enabled_val(Some("0"))); + assert!(!kickoff_swap_enabled_val(Some(" 0 "))); + } + + /// With `XCL_KICKOFF_SWAP=0` (`swap_enabled == false`), `acquire_kickoff_target` keeps + /// `current_target` directly without checking `in_flight`, popping/creating spares, switching + /// the active backing list, or marking the wrapper unreset — even while `current_target` is + /// still in flight on the GPU. + #[test] + fn kickoff_swap_disabled_keeps_current_target_without_spares() { + const E_OUTOFMEMORY: i32 = 0x8007_000Eu32 as i32; + let _guard = KICKOFF_GPU_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let Some(dev) = test_device_or_warp() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; + unsafe { + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let (q, alloc, primary, w, gate, done) = kickoff_test_setup(&dev); + let (q_raw, a) = (q.as_raw(), alloc.as_raw() as usize); + let close: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(w as *const *const usize)).add(9)); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS)); + let segs = || { let mut v = Vec::new(); assert!(super::super::title_list::push_segments(w, &mut v)); v }; + + q.Wait(&gate, 1).unwrap(); + assert_eq!(close(w), 0); + let one = [primary]; + exec(q_raw, 1, one.as_ptr()); + super::super::alloc_pool::executed(q_raw, &one); + assert!(super::super::alloc_pool::in_flight(primary as usize)); + + let pooled_spare = create_closed_spare(primary, ty, a).expect("pooled spare"); + super::super::title_list::put_spare(w, pooled_spare); + + let mut in_flight_calls = 0usize; + let mut create_calls = 0usize; + let mut reset_calls = Vec::new(); + let res = acquire_kickoff_target( + Some(w), + primary, + false, + |p| { in_flight_calls += 1; super::super::alloc_pool::in_flight(p as usize) }, + || { create_calls += 1; create_closed_spare(primary, ty, a) }, + |p| { reset_calls.push(p); 0 }, + ); + assert_eq!(res, Ok(primary)); + assert_eq!(reset_calls, vec![primary]); + assert_eq!(in_flight_calls, 0, "opt-out must bypass in-flight spare selection"); + assert_eq!(create_calls, 0, "opt-out must not create spares"); + assert_eq!(segs(), vec![primary], "wrapper active target must stay primary"); + assert_eq!(super::super::title_list::pop_spare(w), Some(pooled_spare), "opt-out must not pop pooled spares"); + + // Failed Reset while opted out returns Err without marking kickoff_unreset. + super::super::title_list::put_spare(w, pooled_spare); + let err = acquire_kickoff_target( + Some(w), + primary, + false, + |_| { in_flight_calls += 1; true }, + || { create_calls += 1; None }, + |_| E_OUTOFMEMORY, + ); + assert_eq!(err, Err(E_OUTOFMEMORY)); + assert_eq!((in_flight_calls, create_calls), (0, 0)); + assert_eq!(segs(), vec![primary]); + assert_eq!(super::super::title_list::pop_spare(w), Some(pooled_spare)); + super::super::title_list::put_spare(w, pooled_spare); + + gate.Signal(1).unwrap(); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("opt-out queue drains"); + let removed = dev.GetDeviceRemovedReason(); + assert_eq!(release(w), 0); + drop(q); + assert!(removed.is_ok()); + } + } } From 9d9e09fc8f3ac44a7f8bfd9726109c74a163913d Mon Sep 17 00:00:00 2001 From: Decodere <98278826+Decodere@users.noreply.github.com> Date: Fri, 27 Nov 2026 21:15:24 +0300 Subject: [PATCH 3/4] D3D12.X: gate KickoffX list swap on XCL_KICKOFF_SWAP=1 with bounded spares and fence-wait fallback Addresses pre-merge re-review on #2320: - Flip XCL_KICKOFF_SWAP to opt-in (XCL_KICKOFF_SWAP=1, default OFF) so default KickoffX behavior matches main until a KickoffX-using title's world-run frame times are measured. - Add alloc_pool::wait_idle (cloning fence/value and releasing STATE before gpu::fence::wait) and fall back to a bounded 5000 ms fence wait before resetting current_target when no idle spare is available, preserving subsequent commands instead of dropping them via mark_kickoff_unreset (which now triggers only if the bounded wait or Reset itself fails). - Cap per-wrapper KickoffX spares at MAX_KICKOFF_SPARES = 8 via title_list::spare_count, falling through to the bounded fence wait once the cap is reached. - Tighten diff scope against merge-base 6d12f58a by restoring reset_stand_in verbatim and dropping unrelated COPY-guard and DRED hunks from kickoff_x (keeping only the empty-batch early return in execute_with_waits_on required by mark_kickoff_unreset). - Update KickoffX tests for default-OFF / XCL_KICKOFF_SWAP=1 opt-in and add live D3D12 regression coverage for the 8-spare cap and fence-wait fallback. Assisted-by: Google Antigravity (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/alloc_pool.rs | 14 ++ crates/xcl/src/hle/d3d12x/list_x.rs | 320 ++++++++++++++++++------ crates/xcl/src/hle/d3d12x/title_list.rs | 16 ++ 3 files changed, 269 insertions(+), 81 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index 8b81b85913..5d0d18a70a 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -185,6 +185,20 @@ pub fn in_flight(list: usize) -> bool { g.as_ref().and_then(|s| s.last_submit.get(&list)).is_some_and(|l| unsafe { l.fence.GetCompletedValue() } < l.value) } +/// Wait up to `timeout_ms` for the GPU to finish the last submit of PC command list `list`. +/// Releases `STATE` before blocking on the fence. Returns `true` when `list` is no longer in +/// flight (or had no recorded submit), and `false` on timeout or fence/device error. +pub unsafe fn wait_idle(list: usize, dev: &ID3D12Device, timeout_ms: u32) -> bool { + let mark = { + let g = STATE.lock().unwrap(); + g.as_ref().and_then(|s| s.last_submit.get(&list)).map(|l| (l.fence.clone(), l.value)) + }; + let Some((fence, value)) = mark else { + return true; + }; + crate::gpu::fence::wait(&fence, value, dev, None, timeout_ms).is_ok() +} + /// `lists` were just submitted to `queue`. pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) { if !enabled() || queue.is_null() { diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index e9d1a0a616..11c339c02b 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2589,24 +2589,6 @@ pub(super) unsafe extern "system" fn write64_bottom_x(this: *mut c_void, va: u64 write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, W); } -/// Create a new adopted PC command list matching `template`'s device and type, closed and ready -/// for `Reset`. Releases and forgets the list instead of pooling it if `Close` fails. -unsafe fn create_closed_spare(template: *mut c_void, ty: D3D12_COMMAND_LIST_TYPE, alloc: usize) -> Option<*mut c_void> { - let pc_alloc = super::alloc_pool::current(alloc); - let made = real::create_raw_pc_command_list(device_of(template)?.as_raw(), ty, pc_alloc, 0)?; - let ext = *(made as *const usize); - let hr = pc:: i32>(made, PC_CLOSE)(made); - real::preserve_vtable(made, ext); - if hr < 0 { - super::bind::forget(made); - let rel: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(made as *const *const usize)).add(2)); - rel(made); - return None; - } - on_create(made, pc_alloc, 0, ty); - Some(made) -} - /// The title Resets its list (`title_list` `wrapper`) while the GPU has not finished the last /// submit of the PC list `primary` behind it: a console lets the title submit the new recording /// at once, the PC does not let `primary` be submitted again yet (#1452, debug layer id 553; @@ -2619,7 +2601,20 @@ pub(super) unsafe fn reset_stand_in(wrapper: *mut c_void, primary: *mut c_void, } let spare = match super::title_list::pop_spare(wrapper) { Some(p) => p, - None => create_closed_spare(primary, ty, alloc)?, + None => { + let pc_alloc = super::alloc_pool::current(alloc); + let made = real::create_raw_pc_command_list(device_of(primary)?.as_raw(), ty, pc_alloc, 0)?; + on_create(made, pc_alloc, 0, ty); + // (closed: it is reset below like a spare) + let ext = *(made as *const usize); + let hr = pc:: i32>(made, PC_CLOSE)(made); + real::preserve_vtable(made, ext); + if hr < 0 { + super::title_list::put_spare(wrapper, made); + return None; + } + made + } }; if reset_pc(spare, alloc, pso) < 0 { super::title_list::put_spare(wrapper, spare); @@ -3253,30 +3248,58 @@ pub unsafe extern "system" fn set_predication_buffer_x(this: *mut c_void, desc: apply_predication_copy(this, uma_buf, off, pb, D3D12_PREDICATION_OP(op_raw as i32)); } +/// Maximum non-primary spares per wrapped command list for `KickoffX` list switching before +/// falling back to a bounded fence wait on `current_target`. +const MAX_KICKOFF_SPARES: usize = 8; +/// Bounded wait on `current_target`'s last submit fence when no idle spare is available. +const KICKOFF_IDLE_WAIT_MS: u32 = 5_000; + fn kickoff_swap_enabled_val(val: Option<&str>) -> bool { - !val.is_some_and(|v| v.trim() == "0") + val.is_some_and(|v| v.trim() == "1") } -/// `XCL_KICKOFF_SWAP=0` disables switching `KickoffX` onto an idle spare PC list, falling back to -/// resetting `current_target` directly without affecting `XCL_ALLOC_SWAP`. +/// `XCL_KICKOFF_SWAP=1` enables switching `KickoffX` onto an idle spare PC list while the +/// submitted list is still in flight (off by default, leaving `XCL_ALLOC_SWAP` unaffected). fn kickoff_swap_enabled() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); *ON.get_or_init(|| kickoff_swap_enabled_val(std::env::var("XCL_KICKOFF_SWAP").ok().as_deref())) } +/// Create a new adopted PC command list for `KickoffX` matching `template`'s device and type, +/// closed and ready for `Reset`. Releases and forgets the list instead of pooling it if `Close` fails. +unsafe fn create_kickoff_spare(template: *mut c_void, ty: D3D12_COMMAND_LIST_TYPE, alloc: usize) -> Option<*mut c_void> { + let pc_alloc = super::alloc_pool::current(alloc); + let made = real::create_raw_pc_command_list(device_of(template)?.as_raw(), ty, pc_alloc, 0)?; + let ext = *(made as *const usize); + let hr = pc:: i32>(made, PC_CLOSE)(made); + real::preserve_vtable(made, ext); + if hr < 0 { + super::bind::forget(made); + let rel: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(made as *const *const usize)).add(2)); + rel(made); + return None; + } + on_create(made, pc_alloc, 0, ty); + Some(made) +} + /// Select and `Reset` the next active PC command list after `KickoffX` has submitted -/// `current_target`. When `swap_enabled` is false (`XCL_KICKOFF_SWAP=0`), resets `current_target` -/// directly without touching spares or switching active backing lists. Otherwise preserves the -/// idle-list invariant (#1452, D3D12 debug layer id 553): never calls `reset_one(current_target)` -/// while `in_flight(current_target)` is true. On failure, leaves `current_target` closed and marks -/// `wrapper` unreset so neither a subsequent `KickoffX` nor `ExecuteCommandLists` can resubmit -/// the already-submitted in-flight recording. +/// `current_target`. When `swap_enabled` is false (the default unless `XCL_KICKOFF_SWAP=1`), resets +/// `current_target` directly without touching spares or switching active backing lists. Otherwise +/// preserves the idle-list invariant (#1452, D3D12 debug layer id 553): never calls +/// `reset_one(current_target)` while `in_flight(current_target)` is true. Caps per-wrapper spares +/// at `max_spares`, and when no idle spare is available falls back to `wait_idle(current_target)` +/// before resetting `current_target` so subsequent commands are not dropped. Only marks `wrapper` +/// unreset if that bounded wait or `Reset` itself fails. +#[allow(clippy::too_many_arguments)] unsafe fn acquire_kickoff_target( wrapper: Option<*mut c_void>, current_target: *mut c_void, swap_enabled: bool, + max_spares: usize, mut in_flight: impl FnMut(*mut c_void) -> bool, mut create_spare: impl FnMut() -> Option<*mut c_void>, + mut wait_idle: impl FnMut(*mut c_void) -> bool, mut reset_one: impl FnMut(*mut c_void) -> i32, ) -> Result<*mut c_void, i32> { const E_OUTOFMEMORY: i32 = 0x8007_000Eu32 as i32; @@ -3320,7 +3343,7 @@ unsafe fn acquire_kickoff_target( last_err = Some(hr); failed_spares.push(cand); } - if switched.is_none() { + if switched.is_none() && super::title_list::spare_count(w) + failed_spares.len() < max_spares { if let Some(cand) = create_spare() { if cand != current_target && !in_flight(cand) { let hr = reset_one(cand); @@ -3345,9 +3368,10 @@ unsafe fn acquire_kickoff_target( return Ok(cand); } - if !in_flight(current_target) { + if !in_flight(current_target) || wait_idle(current_target) { let hr = reset_one(current_target); if hr >= 0 { + super::title_list::clear_kickoff_unreset(w); return Ok(current_target); } super::title_list::mark_kickoff_unreset(w); @@ -3368,6 +3392,10 @@ unsafe fn acquire_kickoff_target( /// # Safety /// `this` must be a valid adopted `ID3D12GraphicsCommandList` pointer. pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { + kickoff_x_with(this, kickoff_swap_enabled()); +} + +unsafe fn kickoff_x_with(this: *mut c_void, swap_enabled: bool) { static NO_QUEUE: AtomicU64 = AtomicU64::new(0); static ERR: AtomicU64 = AtomicU64::new(0); let ty = list_type(this); @@ -3437,10 +3465,9 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void); let exec: ExecFn = transmute_copy(&real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS)); - // Capture wrapper before `reclaim_for_kickoff` updates `WRAPPER_OF` (skipped when - // `XCL_KICKOFF_SWAP=0`). If this list was split mid-recording around a `Wait64`, flush + // Capture wrapper before `reclaim_for_kickoff` updates `WRAPPER_OF` (only when + // `XCL_KICKOFF_SWAP=1`). If this list was split mid-recording around a `Wait64`, flush // earlier closed segments in order before `this`. - let swap_enabled = kickoff_swap_enabled(); let wrapper = swap_enabled.then(|| super::title_list::wrapper_of(this)).flatten(); let kickoff_split = super::title_list::reclaim_for_kickoff(this); if let Some((_, ref prior, _)) = kickoff_split { @@ -3483,7 +3510,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { Some((primary, prior, reclaimed)) => (primary, prior, reclaimed), None => (this, Vec::new(), Vec::new()), }; - let init_pso = if ty == D3D12_COMMAND_LIST_TYPE_COPY { 0 } else { super::pso_async::resolve_for_bind(bound_pso) }; + let init_pso = super::pso_async::resolve_for_bind(bound_pso); let reset_one = |pc_list: *mut c_void| -> i32 { let ext_closed = *(pc_list as *const usize); let hr = super::alloc_pool::reset_list(pc_list as usize, alloc, list_type(pc_list).0, |pc_alloc| { @@ -3492,16 +3519,18 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { real::preserve_vtable(pc_list, ext_closed); hr }; - // Avoid resuming on a PC command list whose submit is still in flight on the GPU (#1452, - // debug layer id 553): pick `primary` once idle, else an idle spare, else a fresh closed spare. - // If no idle candidate can be created or reset while `current_target` is in flight, leave it - // closed and suppress `push_segments` until the title's next `Reset`. + // When `XCL_KICKOFF_SWAP=1`, avoid resuming on a PC command list whose submit is still in + // flight on the GPU (#1452, debug layer id 553): pick `primary` once idle, else an idle spare, + // else a fresh closed spare up to `MAX_KICKOFF_SPARES`, else wait on `current_target`'s last + // submit fence before resetting it. let target = match acquire_kickoff_target( wrapper, current_target, swap_enabled, + MAX_KICKOFF_SPARES, |p| super::alloc_pool::in_flight(p as usize), - || create_closed_spare(current_target, ty, alloc), + || create_kickoff_spare(current_target, ty, alloc), + |p| device_of(p).is_some_and(|dev| super::alloc_pool::wait_idle(p as usize, &dev, KICKOFF_IDLE_WAIT_MS)), reset_one, ) { Ok(t) => t, @@ -3509,10 +3538,6 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { return warn(&ERR, "KickoffX", format_args!("Reset after kickoff failed ({reset_hr:#x})")); } }; - super::dred::reset(target); - if init_pso != 0 { - super::dred::mark_pipeline(target, bound_pso, init_pso); - } if target != this { let (ended_early, pred_buf_obj, bindings_copy) = { let mut g = LISTS.lock().unwrap(); @@ -3597,13 +3622,11 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { pc::(target, PC_OM_SET_DEPTH_BOUNDS)(target, min, max); } } - if ty != D3D12_COMMAND_LIST_TYPE_COPY { - if let (Some(op), Some(pb)) = (predication, pred_buf) { - transition(target, pb, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_PREDICATION); - pc::(target, PC_SET_PREDICATION)(target, pb, 0, op); - if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(target as usize))) { - s.pred_in_state = true; - } + if let (Some(op), Some(pb)) = (predication, pred_buf) { + transition(target, pb, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_PREDICATION); + pc::(target, PC_SET_PREDICATION)(target, pb, 0, op); + if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(target as usize))) { + s.pred_in_state = true; } } } @@ -7634,8 +7657,8 @@ mod tests { q.Wait(&gate, 1).unwrap(); assert_eq!(segs(), vec![primary]); - // KickoffX #1 & #2 while gated: must switch primary -> s1 -> s2 with 0 resubmits and keep bindings. - kickoff_x(primary); + // KickoffX #1 & #2 while gated (with XCL_KICKOFF_SWAP=1): must switch primary -> s1 -> s2 with 0 resubmits and keep bindings. + kickoff_x_with(primary, true); let s1 = segs(); assert_eq!(s1.len(), 1); let resub1 = super::super::alloc_pool::note_resubmits(q_raw, &s1); @@ -7644,7 +7667,7 @@ mod tests { let s = g.as_ref().unwrap().get(&(s1[0] as usize)).unwrap(); (s.bindings.stencil, s.bindings.topology, s.last_queue) }; - kickoff_x(s1[0]); + kickoff_x_with(s1[0], true); let s2 = segs(); assert_eq!(s2.len(), 1); let resub2 = super::super::alloc_pool::note_resubmits(q_raw, &s2); @@ -7657,7 +7680,7 @@ mod tests { gate.Signal(1).unwrap(); q.Signal(&done, 1).unwrap(); crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("queue drains"); - kickoff_x(s2[0]); + kickoff_x_with(s2[0], true); let after_drain = segs(); assert_eq!(close(w), 0); let resub3 = super::super::alloc_pool::note_resubmits(q_raw, &after_drain); @@ -7703,7 +7726,7 @@ mod tests { assert!(split_segs[0] == primary && seg1 != primary); q.Wait(&gate, 1).unwrap(); - kickoff_x(seg1); + kickoff_x_with(seg1, true); let mut after = Vec::new(); assert!(super::super::title_list::push_segments(w, &mut after)); assert_eq!((after.len(), close(w)), (1, 0)); @@ -7755,39 +7778,44 @@ mod tests { super::super::alloc_pool::executed(q_raw, &one); assert!(super::super::alloc_pool::in_flight(primary as usize)); - // 1. No pooled spare and `create_spare` fails (`None`): must never call `reset_one(primary)` - // even though `reset_one` would return `S_OK`, and must suppress subsequent submits. + // 1. No pooled spare, `create_spare` fails (`None`), and bounded fence wait fails: + // must never call `reset_one(primary)` even though `reset_one` would return `S_OK`, + // and must suppress subsequent submits. let mut reset_calls = Vec::new(); let res_no_spare = acquire_kickoff_target( Some(w), primary, true, + MAX_KICKOFF_SPARES, |p| super::super::alloc_pool::in_flight(p as usize), || None, + |_| false, |p| { reset_calls.push(p); 0 }, ); assert_eq!(res_no_spare, Err(E_OUTOFMEMORY)); - assert!(reset_calls.is_empty(), "in-flight primary must never be reset when spare creation fails"); + assert!(reset_calls.is_empty(), "in-flight primary must never be reset when spare creation and wait fail"); let empty = segs(); assert!(empty.is_empty(), "unreset kickoff wrapper must not expose in-flight primary to ExecuteCommandLists"); assert!(close(w) < 0, "in-flight primary must remain closed"); - kickoff_x(primary); + kickoff_x_with(primary, true); assert!(segs().is_empty()); let mut exec_batches = 0usize; execute_with_waits_on(q_raw, &empty, |_| exec_batches += 1); assert_eq!(exec_batches, 0); - // 2. Pooled idle spare `s_fail` fails `Reset`: must never fall back to in-flight `primary`, - // must return `s_fail` to the spare pool, and must keep `push_segments` empty. - let s_fail = create_closed_spare(primary, ty, a).expect("spare 1"); + // 2. Pooled idle spare `s_fail` fails `Reset` and wait fails: must never fall back to + // in-flight `primary`, must return `s_fail` to the spare pool, and keep `push_segments` empty. + let s_fail = create_kickoff_spare(primary, ty, a).expect("spare 1"); super::super::title_list::put_spare(w, s_fail); reset_calls.clear(); let res_spare_err = acquire_kickoff_target( Some(w), primary, true, + MAX_KICKOFF_SPARES, |p| super::super::alloc_pool::in_flight(p as usize), || None, + |_| false, |p| { reset_calls.push(p); if p == s_fail { E_OUTOFMEMORY } else { 0 } }, ); assert_eq!(res_spare_err, Err(E_OUTOFMEMORY)); @@ -7797,7 +7825,7 @@ mod tests { // 3. With a second idle spare `s_good` behind `s_fail`, `acquire_kickoff_target` skips // `s_fail`, switches to `s_good`, and keeps `s_fail` pooled with 0 resubmits. assert_eq!(super::super::title_list::pop_spare(w), Some(s_fail)); - let s_good = create_closed_spare(primary, ty, a).expect("spare 2"); + let s_good = create_kickoff_spare(primary, ty, a).expect("spare 2"); super::super::title_list::put_spare(w, s_good); super::super::title_list::put_spare(w, s_fail); reset_calls.clear(); @@ -7805,8 +7833,10 @@ mod tests { Some(w), primary, true, + MAX_KICKOFF_SPARES, |p| super::super::alloc_pool::in_flight(p as usize), || None, + |_| false, |p| { reset_calls.push(p); if p == s_fail { E_OUTOFMEMORY } else { reset_pc(p, a, 0) } }, ); assert_eq!(res_recovered, Ok(s_good)); @@ -7832,20 +7862,142 @@ mod tests { } } + /// Per-wrapper `KickoffX` spares must be capped at `MAX_KICKOFF_SPARES`, and when no idle + /// spare is available (cap reached or spare creation fails), `acquire_kickoff_target` must + /// wait on `current_target`'s last submit fence (releasing `alloc_pool` locks while waiting) + /// and reset `current_target` in place so subsequent commands are not dropped. #[test] - fn kickoff_swap_enabled_val_defaults_on_and_disables_on_zero() { - assert!(kickoff_swap_enabled_val(None)); - assert!(kickoff_swap_enabled_val(Some(""))); - assert!(kickoff_swap_enabled_val(Some("1"))); - assert!(kickoff_swap_enabled_val(Some("true"))); + fn kickoff_x_spare_cap_and_fence_wait_fallback_recovers_without_dropping_commands() { + struct SendFence(ID3D12Fence); + unsafe impl Send for SendFence {} + + let _guard = KICKOFF_GPU_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let Some(dev) = test_device_or_warp() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; + unsafe { + let (q, alloc, primary, w, gate, done) = kickoff_test_setup(&dev); + let (q_raw, a) = (q.as_raw(), alloc.as_raw() as usize); + let close: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(w as *const *const usize)).add(9)); + let reset_w: unsafe extern "system" fn(*mut c_void, usize, usize) -> i32 = std::mem::transmute(*(*(w as *const *const usize)).add(10)); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS)); + let segs = || { let mut v = Vec::new(); assert!(super::super::title_list::push_segments(w, &mut v)); v }; + + // 1. With no spares pooled and `create_spare` failing (`None`) while `primary` is in + // flight, fence-wait fallback waits for `primary` (releasing `alloc_pool::STATE` + // while waiting) and resets it in place without dropping commands. + q.Wait(&gate, 1).unwrap(); + assert_eq!(close(w), 0); + let one_p = [primary]; + exec(q_raw, 1, one_p.as_ptr()); + super::super::alloc_pool::executed(q_raw, &one_p); + assert!(super::super::alloc_pool::in_flight(primary as usize)); + + let gate_t1 = SendFence(gate.clone()); + let primary_addr = primary as usize; + let unblocker1 = std::thread::spawn(move || { + let gate_t1 = gate_t1; + std::thread::sleep(std::time::Duration::from_millis(25)); + assert!(super::super::alloc_pool::in_flight(primary_addr)); + gate_t1.0.Signal(1).unwrap(); + }); + let res_oom_wait = acquire_kickoff_target( + Some(w), + primary, + true, + MAX_KICKOFF_SPARES, + |p| super::super::alloc_pool::in_flight(p as usize), + || None, + |p| super::super::alloc_pool::wait_idle(p as usize, &dev, KICKOFF_IDLE_WAIT_MS), + |p| reset_pc(p, a, 0), + ); + unblocker1.join().unwrap(); + assert_eq!(res_oom_wait, Ok(primary), "fence-wait fallback must recover primary on spare creation failure"); + assert_eq!(segs(), vec![primary]); + assert_eq!(super::super::alloc_pool::note_resubmits(q_raw, &[primary]), 0); + assert_eq!(close(w), 0); + exec(q_raw, 1, one_p.as_ptr()); + super::super::alloc_pool::executed(q_raw, &one_p); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("oom wait fallback drains"); + assert_eq!(reset_w(w, a, 0), 0); + assert_eq!(segs(), vec![primary]); + + // 2. Fill all `MAX_KICKOFF_SPARES` slots while `gate` holds every submit in flight. + q.Wait(&gate, 2).unwrap(); + let mut seen = std::collections::HashSet::new(); + seen.insert(primary as usize); + let mut cur = primary; + for i in 0..MAX_KICKOFF_SPARES { + kickoff_x_with(cur, true); + let next = segs(); + assert_eq!(next.len(), 1); + assert_eq!(super::super::alloc_pool::note_resubmits(q_raw, &next), 0); + assert!(seen.insert(next[0] as usize), "spare #{i} must be distinct"); + assert_eq!(super::super::title_list::spare_count(w), i + 1); + cur = next[0]; + } + // Submit `cur` (spare #8) so `primary` and all 8 spares are simultaneously in flight. + assert_eq!(close(w), 0); + let one = [cur]; + exec(q_raw, 1, one.as_ptr()); + super::super::alloc_pool::executed(q_raw, &one); + assert!(super::super::alloc_pool::in_flight(cur as usize)); + + let gate_t2 = SendFence(gate.clone()); + let cur_addr = cur as usize; + let unblocker2 = std::thread::spawn(move || { + let gate_t2 = gate_t2; + std::thread::sleep(std::time::Duration::from_millis(25)); + assert!(super::super::alloc_pool::in_flight(cur_addr)); + gate_t2.0.Signal(2).unwrap(); + }); + let mut create_calls = 0usize; + let res_capped = acquire_kickoff_target( + Some(w), + cur, + true, + MAX_KICKOFF_SPARES, + |p| super::super::alloc_pool::in_flight(p as usize), + || { create_calls += 1; None }, + |p| super::super::alloc_pool::wait_idle(p as usize, &dev, KICKOFF_IDLE_WAIT_MS), + |p| reset_pc(p, a, 0), + ); + unblocker2.join().unwrap(); + assert_eq!(create_calls, 0, "cap must prevent allocating past MAX_KICKOFF_SPARES"); + assert_eq!(res_capped, Ok(cur), "bounded fence wait must recover current_target in place"); + assert_eq!(super::super::title_list::spare_count(w), MAX_KICKOFF_SPARES); + let after_cap = segs(); + assert_eq!(after_cap, vec![cur]); + assert_eq!(super::super::alloc_pool::note_resubmits(q_raw, &after_cap), 0); + assert_eq!(close(w), 0); + exec(q_raw, 1, after_cap.as_ptr()); + super::super::alloc_pool::executed(q_raw, &after_cap); + q.Signal(&done, 2).unwrap(); + crate::gpu::fence::wait(&done, 2, &dev, None, 5000).expect("capped fallback drains"); + + let removed = dev.GetDeviceRemovedReason(); + assert_eq!(release(w), 0); + drop(q); + assert!(removed.is_ok()); + } + } + + #[test] + fn kickoff_swap_enabled_val_defaults_off_and_enables_on_one() { + assert!(!kickoff_swap_enabled_val(None)); + assert!(!kickoff_swap_enabled_val(Some(""))); assert!(!kickoff_swap_enabled_val(Some("0"))); - assert!(!kickoff_swap_enabled_val(Some(" 0 "))); + assert!(!kickoff_swap_enabled_val(Some("false"))); + assert!(!kickoff_swap_enabled_val(Some("true"))); + assert!(kickoff_swap_enabled_val(Some("1"))); + assert!(kickoff_swap_enabled_val(Some(" 1 "))); } - /// With `XCL_KICKOFF_SWAP=0` (`swap_enabled == false`), `acquire_kickoff_target` keeps - /// `current_target` directly without checking `in_flight`, popping/creating spares, switching - /// the active backing list, or marking the wrapper unreset — even while `current_target` is - /// still in flight on the GPU. + /// With `XCL_KICKOFF_SWAP` unset/off (`swap_enabled == false`, the default), + /// `acquire_kickoff_target` keeps `current_target` directly without checking `in_flight`, + /// popping/creating spares, waiting on fences, switching the active backing list, or marking + /// the wrapper unreset — even while `current_target` is still in flight on the GPU. #[test] fn kickoff_swap_disabled_keeps_current_target_without_spares() { const E_OUTOFMEMORY: i32 = 0x8007_000Eu32 as i32; @@ -7868,46 +8020,52 @@ mod tests { super::super::alloc_pool::executed(q_raw, &one); assert!(super::super::alloc_pool::in_flight(primary as usize)); - let pooled_spare = create_closed_spare(primary, ty, a).expect("pooled spare"); + let pooled_spare = create_kickoff_spare(primary, ty, a).expect("pooled spare"); super::super::title_list::put_spare(w, pooled_spare); let mut in_flight_calls = 0usize; let mut create_calls = 0usize; + let mut wait_calls = 0usize; let mut reset_calls = Vec::new(); let res = acquire_kickoff_target( Some(w), primary, false, + MAX_KICKOFF_SPARES, |p| { in_flight_calls += 1; super::super::alloc_pool::in_flight(p as usize) }, - || { create_calls += 1; create_closed_spare(primary, ty, a) }, + || { create_calls += 1; create_kickoff_spare(primary, ty, a) }, + |_| { wait_calls += 1; true }, |p| { reset_calls.push(p); 0 }, ); assert_eq!(res, Ok(primary)); assert_eq!(reset_calls, vec![primary]); - assert_eq!(in_flight_calls, 0, "opt-out must bypass in-flight spare selection"); - assert_eq!(create_calls, 0, "opt-out must not create spares"); + assert_eq!(in_flight_calls, 0, "default-off must bypass in-flight spare selection"); + assert_eq!(create_calls, 0, "default-off must not create spares"); + assert_eq!(wait_calls, 0, "default-off must not wait on fences"); assert_eq!(segs(), vec![primary], "wrapper active target must stay primary"); - assert_eq!(super::super::title_list::pop_spare(w), Some(pooled_spare), "opt-out must not pop pooled spares"); + assert_eq!(super::super::title_list::pop_spare(w), Some(pooled_spare), "default-off must not pop pooled spares"); - // Failed Reset while opted out returns Err without marking kickoff_unreset. + // Failed Reset while disabled returns Err without marking kickoff_unreset. super::super::title_list::put_spare(w, pooled_spare); let err = acquire_kickoff_target( Some(w), primary, false, + MAX_KICKOFF_SPARES, |_| { in_flight_calls += 1; true }, || { create_calls += 1; None }, + |_| { wait_calls += 1; false }, |_| E_OUTOFMEMORY, ); assert_eq!(err, Err(E_OUTOFMEMORY)); - assert_eq!((in_flight_calls, create_calls), (0, 0)); + assert_eq!((in_flight_calls, create_calls, wait_calls), (0, 0, 0)); assert_eq!(segs(), vec![primary]); assert_eq!(super::super::title_list::pop_spare(w), Some(pooled_spare)); super::super::title_list::put_spare(w, pooled_spare); gate.Signal(1).unwrap(); q.Signal(&done, 1).unwrap(); - crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("opt-out queue drains"); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("default-off queue drains"); let removed = dev.GetDeviceRemovedReason(); assert_eq!(release(w), 0); drop(q); diff --git a/crates/xcl/src/hle/d3d12x/title_list.rs b/crates/xcl/src/hle/d3d12x/title_list.rs index 18651d78dd..eef1037a9b 100644 --- a/crates/xcl/src/hle/d3d12x/title_list.rs +++ b/crates/xcl/src/hle/d3d12x/title_list.rs @@ -318,6 +318,22 @@ pub(super) fn mark_kickoff_unreset(wrapper: *mut c_void) { sg.get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().kickoff_unreset = true; } +/// Clear `kickoff_unreset` after `KickoffX` resets `current_target` in place. +pub(super) fn clear_kickoff_unreset(wrapper: *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.kickoff_unreset = false; + } +} + +/// Number of non-primary spare PC lists currently owned by `wrapper` (pooled or active stand-in). +pub(super) fn spare_count(wrapper: *mut c_void) -> usize { + let sg = SEGMENTS.lock().unwrap(); + sg.as_ref() + .and_then(|m| m.get(&(wrapper as usize))) + .map_or(0, |b| b.spares.len() + usize::from(b.first.is_some()) + b.extra_active.len()) +} + /// Whether `wrapper` may allocate/activate `n` more split segments for this recording. pub(super) fn can_split(wrapper: *mut c_void, n: usize) -> bool { let sg = SEGMENTS.lock().unwrap(); From bc3c0423f0dc4f14223aeb9472a418f54a5571eb Mon Sep 17 00:00:00 2001 From: Decodere <98278826+Decodere@users.noreply.github.com> Date: Fri, 27 Nov 2026 21:48:43 +0300 Subject: [PATCH 4/4] D3D12.X: preserve exact main KickoffX default-off path, gate empty-batch skip, and log wait timeout Addresses final pre-merge re-check items on #2320: - Branch cleanly on !swap_enabled in kickoff_x_with so the default-OFF path executes main's exact hand-over-then-reset order without overwriting list_type or last_queue on primary. - Gate execute_with_waits_on's empty-batch early return behind kickoff_swap_enabled() (via execute_with_waits_on_with) so default-OFF behavior is unchanged for empty batches. - Return HRESULT_FROM_WIN32(ERROR_WAIT_TIMEOUT) (0x80070102) instead of E_OUTOFMEMORY when the bounded 5000 ms fence wait expires. - Add live split-list default-OFF test kickoff_x_default_off_split_list_matches_main_handover and verify both opt-in and default-OFF empty-batch handling. Assisted-by: Google Antigravity (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/list_x.rs | 256 ++++++++++++++++++++-------- 1 file changed, 187 insertions(+), 69 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 11c339c02b..829f520cd5 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2093,11 +2093,20 @@ pub(super) unsafe fn cpu_writes_after(queue: *mut c_void, lists: &[*mut c_void]) /// condition is not yet satisfied so earlier lists in the same batch can run on the GPU first. /// When `queue` is non-null, monotonic 64-bit waits backed by a recorded `Write64BitValue*` are /// bridged to GPU-side `queue.Wait` without blocking the submitter thread on the CPU. -pub(super) unsafe fn execute_with_waits_on(queue: *mut c_void, lists: &[*mut c_void], mut submit: impl FnMut(&[*mut c_void])) { - if lists.is_empty() { +pub(super) unsafe fn execute_with_waits_on(queue: *mut c_void, lists: &[*mut c_void], submit: impl FnMut(&[*mut c_void])) { + execute_with_waits_on_with(queue, lists, kickoff_swap_enabled(), submit); +} + +unsafe fn execute_with_waits_on_with( + queue: *mut c_void, + lists: &[*mut c_void], + skip_empty: bool, + mut submit: impl FnMut(&[*mut c_void]), +) { + if skip_empty && lists.is_empty() { return; } - if PENDING_WAIT_LISTS.load(Ordering::Acquire) == 0 { + if lists.is_empty() || PENDING_WAIT_LISTS.load(Ordering::Acquire) == 0 { submit(lists); return; } @@ -3302,13 +3311,12 @@ unsafe fn acquire_kickoff_target( mut wait_idle: impl FnMut(*mut c_void) -> bool, mut reset_one: impl FnMut(*mut c_void) -> i32, ) -> Result<*mut c_void, i32> { - const E_OUTOFMEMORY: i32 = 0x8007_000Eu32 as i32; + const HRESULT_WAIT_TIMEOUT: i32 = 0x8007_0102u32 as i32; let Some(w) = wrapper.filter(|_| swap_enabled) else { let hr = reset_one(current_target); return if hr >= 0 { Ok(current_target) } else { Err(hr) }; }; let primary = super::title_list::primary_of(w); - let mut last_err: Option = None; if current_target != primary && !in_flight(primary) { let hr = reset_one(primary); @@ -3316,7 +3324,6 @@ unsafe fn acquire_kickoff_target( super::title_list::switch_active_for_kickoff(w, current_target, primary); return Ok(primary); } - last_err = Some(hr); } if !in_flight(current_target) { @@ -3340,7 +3347,6 @@ unsafe fn acquire_kickoff_target( switched = Some(cand); break; } - last_err = Some(hr); failed_spares.push(cand); } if switched.is_none() && super::title_list::spare_count(w) + failed_spares.len() < max_spares { @@ -3350,7 +3356,6 @@ unsafe fn acquire_kickoff_target( if hr >= 0 { switched = Some(cand); } else { - last_err = Some(hr); failed_spares.push(cand); } } else { @@ -3378,7 +3383,7 @@ unsafe fn acquire_kickoff_target( return Err(hr); } super::title_list::mark_kickoff_unreset(w); - Err(last_err.unwrap_or(E_OUTOFMEMORY)) + Err(HRESULT_WAIT_TIMEOUT) } /// command list #93 KickoffX(): flush commands recorded so far by closing the list, submitting @@ -3506,10 +3511,6 @@ unsafe fn kickoff_x_with(this: *mut c_void, swap_enabled: bool) { } super::cpu_writes::after(q_raw, cpu_writes); - let (current_target, prior_retired, reclaimed) = match kickoff_split { - Some((primary, prior, reclaimed)) => (primary, prior, reclaimed), - None => (this, Vec::new(), Vec::new()), - }; let init_pso = super::pso_async::resolve_for_bind(bound_pso); let reset_one = |pc_list: *mut c_void| -> i32 { let ext_closed = *(pc_list as *const usize); @@ -3519,61 +3520,110 @@ unsafe fn kickoff_x_with(this: *mut c_void, swap_enabled: bool) { real::preserve_vtable(pc_list, ext_closed); hr }; - // When `XCL_KICKOFF_SWAP=1`, avoid resuming on a PC command list whose submit is still in - // flight on the GPU (#1452, debug layer id 553): pick `primary` once idle, else an idle spare, - // else a fresh closed spare up to `MAX_KICKOFF_SPARES`, else wait on `current_target`'s last - // submit fence before resetting it. - let target = match acquire_kickoff_target( - wrapper, - current_target, - swap_enabled, - MAX_KICKOFF_SPARES, - |p| super::alloc_pool::in_flight(p as usize), - || create_kickoff_spare(current_target, ty, alloc), - |p| device_of(p).is_some_and(|dev| super::alloc_pool::wait_idle(p as usize, &dev, KICKOFF_IDLE_WAIT_MS)), - reset_one, - ) { - Ok(t) => t, - Err(reset_hr) => { + let target = if !swap_enabled { + // Default OFF: exact `main` hand-over-then-reset order without spare switching. + let target = if let Some((primary, _, reclaimed)) = kickoff_split { + let (ended_early, pred_buf_obj, bindings_copy) = { + let mut g = LISTS.lock().unwrap(); + if let Some(s) = g.as_mut().and_then(|m| m.get_mut(&(this as usize))) { + (std::mem::take(&mut s.occlusion.ended_early), s.pred_buf.clone(), s.bindings.clone()) + } else { + (Vec::new(), None, Bindings::default()) + } + }; + super::bind::inherit_for_split(this, primary, alloc); + for seg in reclaimed { + super::bind::reset(seg as *mut c_void, 0, 0); + clear_segment_state(seg); + } + if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(primary as usize))) { + s.allocator = alloc; + s.bound_pso = bound_pso; + s.depth_bias = depth_bias; + s.index_buffer = index_buffer; + s.depth_bounds = depth_bounds; + s.predication = predication; + s.pred_buf = pred_buf_obj; + s.pred_in_state = false; + s.occlusion = Occlusion { active: None, ended_early }; + s.written_ranges.clear(); + s.clear_sparse_writes(); + s.clear_write64_signals(); + s.clear_waits(); + s.cpu_writes.clear(); + s.bindings = bindings_copy; + } + primary + } else { + this + }; + let reset_hr = reset_one(target); + if reset_hr < 0 { return warn(&ERR, "KickoffX", format_args!("Reset after kickoff failed ({reset_hr:#x})")); } - }; - if target != this { - let (ended_early, pred_buf_obj, bindings_copy) = { - let mut g = LISTS.lock().unwrap(); - if let Some(s) = g.as_mut().and_then(|m| m.get_mut(&(this as usize))) { - (std::mem::take(&mut s.occlusion.ended_early), s.pred_buf.clone(), s.bindings.clone()) - } else { - (Vec::new(), None, Bindings::default()) + target + } else { + let (current_target, prior_retired, reclaimed) = match kickoff_split { + Some((primary, prior, reclaimed)) => (primary, prior, reclaimed), + None => (this, Vec::new(), Vec::new()), + }; + // When `XCL_KICKOFF_SWAP=1`, avoid resuming on a PC command list whose submit is still in + // flight on the GPU (#1452, debug layer id 553): pick `primary` once idle, else an idle spare, + // else a fresh closed spare up to `MAX_KICKOFF_SPARES`, else wait on `current_target`'s last + // submit fence before resetting it. + let target = match acquire_kickoff_target( + wrapper, + current_target, + true, + MAX_KICKOFF_SPARES, + |p| super::alloc_pool::in_flight(p as usize), + || create_kickoff_spare(current_target, ty, alloc), + |p| device_of(p).is_some_and(|dev| super::alloc_pool::wait_idle(p as usize, &dev, KICKOFF_IDLE_WAIT_MS)), + reset_one, + ) { + Ok(t) => t, + Err(reset_hr) => { + return warn(&ERR, "KickoffX", format_args!("Reset after kickoff failed ({reset_hr:#x})")); } }; - super::bind::inherit_for_split(this, target, alloc); - for seg in reclaimed.into_iter().chain(prior_retired.into_iter().map(|p| p as usize)).chain([this as usize]) { - if seg != target as usize { - super::bind::reset(seg as *mut c_void, 0, 0); - clear_segment_state(seg); + if target != this { + let (ended_early, pred_buf_obj, bindings_copy) = { + let mut g = LISTS.lock().unwrap(); + if let Some(s) = g.as_mut().and_then(|m| m.get_mut(&(this as usize))) { + (std::mem::take(&mut s.occlusion.ended_early), s.pred_buf.clone(), s.bindings.clone()) + } else { + (Vec::new(), None, Bindings::default()) + } + }; + super::bind::inherit_for_split(this, target, alloc); + for seg in reclaimed.into_iter().chain(prior_retired.into_iter().map(|p| p as usize)).chain([this as usize]) { + if seg != target as usize { + super::bind::reset(seg as *mut c_void, 0, 0); + clear_segment_state(seg); + } } - } - let mut g = LISTS.lock().unwrap(); - let s = g.get_or_insert_with(HashMap::new).entry(target as usize).or_default(); - s.allocator = alloc; - s.list_type = Some(ty); - s.last_queue = last_queue.or(Some(queue)); - s.bound_pso = bound_pso; - s.depth_bias = depth_bias; - s.index_buffer = index_buffer; - s.depth_bounds = depth_bounds; - s.predication = predication; - s.pred_buf = pred_buf_obj; - s.pred_in_state = false; - s.occlusion = Occlusion { active: None, ended_early }; - s.written_ranges.clear(); - s.clear_sparse_writes(); - s.clear_write64_signals(); - s.clear_waits(); - s.cpu_writes.clear(); - s.bindings = bindings_copy; - } + let mut g = LISTS.lock().unwrap(); + let s = g.get_or_insert_with(HashMap::new).entry(target as usize).or_default(); + s.allocator = alloc; + s.list_type = Some(ty); + s.last_queue = last_queue.or(Some(queue)); + s.bound_pso = bound_pso; + s.depth_bias = depth_bias; + s.index_buffer = index_buffer; + s.depth_bounds = depth_bounds; + s.predication = predication; + s.pred_buf = pred_buf_obj; + s.pred_in_state = false; + s.occlusion = Occlusion { active: None, ended_early }; + s.written_ranges.clear(); + s.clear_sparse_writes(); + s.clear_write64_signals(); + s.clear_waits(); + s.cpu_writes.clear(); + s.bindings = bindings_copy; + } + target + }; reset_copy_scratch(target); let bindings = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(target as usize))).map(|s| s.bindings.clone()); if let Some(b) = bindings { @@ -7757,6 +7807,7 @@ mod tests { #[test] fn kickoff_x_failure_paths_never_reset_in_flight_target() { const E_OUTOFMEMORY: i32 = 0x8007_000Eu32 as i32; + const HRESULT_WAIT_TIMEOUT: i32 = 0x8007_0102u32 as i32; let _guard = KICKOFF_GPU_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); let Some(dev) = test_device_or_warp() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; unsafe { @@ -7780,7 +7831,7 @@ mod tests { // 1. No pooled spare, `create_spare` fails (`None`), and bounded fence wait fails: // must never call `reset_one(primary)` even though `reset_one` would return `S_OK`, - // and must suppress subsequent submits. + // must return HRESULT_WAIT_TIMEOUT, and must suppress subsequent submits. let mut reset_calls = Vec::new(); let res_no_spare = acquire_kickoff_target( Some(w), @@ -7792,16 +7843,19 @@ mod tests { |_| false, |p| { reset_calls.push(p); 0 }, ); - assert_eq!(res_no_spare, Err(E_OUTOFMEMORY)); + assert_eq!(res_no_spare, Err(HRESULT_WAIT_TIMEOUT)); assert!(reset_calls.is_empty(), "in-flight primary must never be reset when spare creation and wait fail"); let empty = segs(); assert!(empty.is_empty(), "unreset kickoff wrapper must not expose in-flight primary to ExecuteCommandLists"); assert!(close(w) < 0, "in-flight primary must remain closed"); kickoff_x_with(primary, true); assert!(segs().is_empty()); - let mut exec_batches = 0usize; - execute_with_waits_on(q_raw, &empty, |_| exec_batches += 1); - assert_eq!(exec_batches, 0); + let mut exec_batches_opt_in = 0usize; + execute_with_waits_on_with(q_raw, &empty, true, |_| exec_batches_opt_in += 1); + assert_eq!(exec_batches_opt_in, 0, "opt-in empty batch from kickoff_unreset must be skipped"); + let mut exec_batches_default = 0usize; + execute_with_waits_on_with(q_raw, &empty, false, |_| exec_batches_default += 1); + assert_eq!(exec_batches_default, 1, "default-off execute_with_waits_on must preserve main empty-batch call"); // 2. Pooled idle spare `s_fail` fails `Reset` and wait fails: must never fall back to // in-flight `primary`, must return `s_fail` to the spare pool, and keep `push_segments` empty. @@ -7818,7 +7872,7 @@ mod tests { |_| false, |p| { reset_calls.push(p); if p == s_fail { E_OUTOFMEMORY } else { 0 } }, ); - assert_eq!(res_spare_err, Err(E_OUTOFMEMORY)); + assert_eq!(res_spare_err, Err(HRESULT_WAIT_TIMEOUT)); assert_eq!(reset_calls, vec![s_fail], "spare Reset failure must not fall back to in-flight primary"); assert!(segs().is_empty()); @@ -8072,4 +8126,68 @@ mod tests { assert!(removed.is_ok()); } } + + /// With `XCL_KICKOFF_SWAP` unset/off (`swap_enabled == false`), `kickoff_x_with(seg1, false)` + /// on a mid-list split resumes on `primary` using `main`'s exact hand-over-then-reset path: + /// no new spare is created, bindings hand over to `primary`, `seg1`'s segment state is cleared, + /// and `primary`'s `list_type` / `last_queue` are not overwritten. + #[test] + fn kickoff_x_default_off_split_list_matches_main_handover() { + let _guard = KICKOFF_GPU_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let Some(dev) = test_device_or_warp() else { return crate::gpu::recomp::runner::skip_test("no GPU or WARP"); }; + unsafe { + let (q, _alloc, primary, w, gate, done) = kickoff_test_setup(&dev); + let q_raw = q.as_raw(); + let close: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(w as *const *const usize)).add(9)); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + + let cell = Box::new(AtomicU64::new(0)); + let va = &*cell as *const AtomicU64 as u64; + note_write64_recorded(primary, va, 1); + wait64_x(primary, va, D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32, 1, 0, 0); + let mut split_segs = Vec::new(); + assert!(super::super::title_list::push_segments(w, &mut split_segs)); + assert_eq!(split_segs.len(), 2); + let seg1 = split_segs[1]; + assert!(split_segs[0] == primary && seg1 != primary); + + { + let mut g = LISTS.lock().unwrap(); + let m = g.as_mut().unwrap(); + let sp = m.get_mut(&(primary as usize)).unwrap(); + sp.list_type = None; + sp.last_queue = Some(0xDEAD); + let s1 = m.get_mut(&(seg1 as usize)).unwrap(); + s1.last_queue = Some(q_raw as usize); + s1.bindings.stencil = Some(0x77); + s1.bindings.topology = Some(4); + } + + q.Wait(&gate, 1).unwrap(); + kickoff_x_with(seg1, false); + + let mut after = Vec::new(); + assert!(super::super::title_list::push_segments(w, &mut after)); + assert_eq!(after, vec![primary], "default-off split KickoffX must resume directly on primary"); + assert_eq!(super::super::title_list::spare_count(w), 1, "default-off must not allocate a new spare"); + let primary_state = { + let g = LISTS.lock().unwrap(); + let s = g.as_ref().unwrap().get(&(primary as usize)).unwrap(); + (s.bindings.stencil, s.bindings.topology, s.list_type, s.last_queue) + }; + assert_eq!(primary_state, (Some(0x77), Some(4), None, Some(0xDEAD))); + assert_eq!(close(w), 0); + + gate.Signal(1).unwrap(); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("default-off split drains"); + let removed = dev.GetDeviceRemovedReason(); + assert_eq!(release(w), 0); + drop(q); + if let Some(gr) = SYNC_GRAPH.lock().unwrap().as_mut() { + gr.cells.remove(&va); + } + assert!(removed.is_ok()); + } + } }