From 7d9f21f86274814124016c36b877a69708a2b0aa Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Wed, 25 Nov 2026 23:56:36 -0800 Subject: [PATCH] D3D12.X: promote deferred compute before list preparation Correctness-critical deferred dispatches marked their pipeline wanted only after command-list split allocation and reset. Publish demand first so foreground worker promotion and compiler admission overlap that native preparation. Bounded waits and the immediate wait after a failed split retain their existing behavior. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/bind.rs | 8 +++---- crates/xcl/src/hle/d3d12x/list_x.rs | 34 +++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index 02c43da04e..3414e2e27b 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -295,8 +295,8 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us let pc: F = std::mem::transmute(super::real::orig_fn(this, 59)); let raw_pso = with(this, |s| s.pso); let unbuilt_compute = signature_dispatches(sig) && super::pso_async::is_pending(raw_pso) && super::real::is_console_compute(raw_pso); - if unbuilt_compute && super::list_x::defer(this, super::list_x::Deferred::Indirect(sig, max, args, args_off, count_buf, count_off)) { - return super::pso_async::need_by_submit(raw_pso); + if unbuilt_compute && super::list_x::defer(this, raw_pso, super::list_x::Deferred::Indirect(sig, max, args, args_off, count_buf, count_off)) { + return; } let (pso, bound_unbuilt) = super::pso_async::resolve_wait_bound(raw_pso); let (gfx, compute) = (super::real::is_translated_graphics(pso), super::real::is_translated_compute(pso)); @@ -938,8 +938,8 @@ fn dispatch_groups(pso: usize, dims: [u32; 3], flags: u32) -> Option<([u32; 3], /// `Dispatch` / `DispatchX(x, y, z, flags)`. pub(super) unsafe fn dispatch_flags(this: *mut c_void, x: u32, y: u32, z: u32, flags: u32) { let pso = with(this, |s| s.pso); - if super::pso_async::is_pending(pso) && super::list_x::defer(this, super::list_x::Deferred::Dispatch(x, y, z, flags)) { - return super::pso_async::need_by_submit(pso); + if super::pso_async::is_pending(pso) && super::list_x::defer(this, pso, super::list_x::Deferred::Dispatch(x, y, z, flags)) { + return; } let pso = super::pso_async::resolve_wait(pso); let Some(list) = ID3D12GraphicsCommandList::from_raw_borrowed(&this) else { return }; diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 84deadf7be..4918e553c3 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -3202,12 +3202,14 @@ fn wait_target_after_split(list: *mut c_void, placement: WaitPlacement, continua } /// Gives a dispatch whose pipeline is still being built a segment of its own, recorded at the submit. -pub(super) unsafe fn defer(list: *mut c_void, what: Deferred) -> bool { +pub(super) unsafe fn defer(list: *mut c_void, pso: usize, what: Deferred) -> bool { // A bounded bind records now and waits for the same bound. Deferring it only adds a segment; // thousands of these exhausted command-list memory in a retail title with a 1 ms budget. - if !defer_pipeline_wait(super::pso_async::waits_bounded(), || { - split_recording(list, SegmentReason::DeferredDispatch(what)).is_some() - }) { + if !defer_pipeline_wait( + super::pso_async::waits_bounded(), + || super::pso_async::need_by_submit(pso), + || split_recording(list, SegmentReason::DeferredDispatch(what)).is_some(), + ) { return false; } static N: AtomicU64 = AtomicU64::new(0); @@ -3218,8 +3220,15 @@ pub(super) unsafe fn defer(list: *mut c_void, what: Deferred) -> bool { true } -fn defer_pipeline_wait(waits_bounded: bool, split: impl FnOnce() -> bool) -> bool { - !waits_bounded && split() +fn defer_pipeline_wait(waits_bounded: bool, demand: impl FnOnce(), split: impl FnOnce() -> bool) -> bool { + if waits_bounded { + return false; + } + // The split can allocate and reset native objects. Let the worker and its compiler/driver + // admission run concurrently with that preparation; a failed split waits for the same build + // immediately in the caller. + demand(); + split() } /// Records the deferred dispatches of `wrapper`'s recording, waiting for their pipelines as a bind did. @@ -6052,11 +6061,18 @@ mod tests { } #[test] - fn bounded_pipeline_wait_does_not_allocate_a_deferred_segment() { + fn bounded_wait_skips_defer_and_unbounded_demand_precedes_split() { + let demanded = std::cell::Cell::new(0); let mut splits = 0; - assert!(!defer_pipeline_wait(true, || { splits += 1; true })); + assert!(!defer_pipeline_wait(true, || demanded.set(demanded.get() + 1), || { splits += 1; true })); + assert_eq!(demanded.get(), 0); assert_eq!(splits, 0); - assert!(defer_pipeline_wait(false, || { splits += 1; true })); + assert!(defer_pipeline_wait(false, || demanded.set(demanded.get() + 1), || { + assert_eq!(demanded.get(), 1, "pipeline demand must be visible before split preparation"); + splits += 1; + true + })); + assert_eq!(demanded.get(), 1); assert_eq!(splits, 1, "the default still defers to the submit"); }