From 20e38b05db33a5ce078535696ec063250f790a06 Mon Sep 17 00:00:00 2001 From: Prime <215761102+Prime1997@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:14:29 -0800 Subject: [PATCH] D3D12.X: an indirect dispatch does not build a split program's whole form in the call An indirect dispatch of a split compute program runs its whole form, which was built at its first use on the title's thread (Chain::whole, under build_whole). On a retail title's first world frame a render thread stood in bind::execute_indirect -> dispatch_chain::whole behind that lock (thread dumps at 240, 300, 360 and 400 s of a run on an RTX 3060 Ti) while the driver compiled 1.1 MB of DXIL: no present and no submit from 224 s to the end of the run at 420 s. Where compute dispatches go on without a pipeline still being built (XCL_CS_PSO_WAIT_MS), the whole form is now built once by a thread of its own, and until it is there the program's indirect dispatches are left out and counted as skipped, as those of any late compute pipeline. Unset, as before. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/hle/d3d12x/bind.rs | 15 +++++- crates/xcl/src/hle/d3d12x/pso_async.rs | 7 +++ .../xcl/src/hle/d3d12x/real/dispatch_chain.rs | 50 ++++++++++++++++++- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index b8206dea5c..f2a771ee06 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -2344,8 +2344,19 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us match bind_abi(this, list, pso, gfx, None, 0) { Ok(b) => { let whole = if compute { - match super::real::dispatch_chain::whole(pso) { - Ok(w) => w, + match super::real::dispatch_chain::whole_ready(pso) { + Ok(super::real::dispatch_chain::Whole::Plain) => None, + Ok(super::real::dispatch_chain::Whole::Ready(w)) => Some(w), + Ok(super::real::dispatch_chain::Whole::Late) => { + static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + let n = N.fetch_add(1, Ordering::Relaxed) + 1; + if n <= 4 || n.is_power_of_two() { + crate::xlog_warn!("D3D12.X: ExecuteIndirect of a split compute program whose whole form is still being built skipped ({n} so far)"); + } + ei_work(this, sig, super::targets::SKIPPED); + count(DISPATCHES_SKIPPED); + return; + } Err(e) => { crate::xlog_warn!("D3D12.X: ExecuteIndirect whole compute PSO: {e}"); ei_work(this, sig, super::targets::SKIPPED); diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 51e0453f17..e2648efcc2 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -2626,6 +2626,13 @@ fn gfx_wait_budget() -> Option { /// (`indirect_guard` zeroes only counts out of range). A retail title's thread spent 57 s in the /// driver's compiler on one program no earlier run had used, and another of its threads waited /// for it all that time: no frame for 57 s. +/// A compute pipeline's dispatches go on without it while it is built (`XCL_CS_PSO_WAIT_MS`): +/// what builds one on a title's thread hands it to a thread of its own instead +/// (`real::dispatch_chain::whole_ready`). +pub(super) fn cs_goes_on() -> bool { + cs_wait_budget().is_some() +} + fn cs_wait_budget() -> Option { static B: OnceLock> = OnceLock::new(); *B.get_or_init(|| budget_of(std::env::var("XCL_CS_PSO_WAIT_MS").ok().as_deref())) diff --git a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs index 4ca7de5431..57428482ae 100644 --- a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs +++ b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs @@ -71,8 +71,14 @@ struct Chain { whole_code: Vec, whole: OnceLock, build_whole: Mutex<()>, + /// a thread of its own builds the whole form (`whole_ready`) + whole_sent: std::sync::atomic::AtomicBool, } +/// A chain for the thread that builds its whole form (D3D12 objects are free-threaded). +struct ChainRef(Arc); +unsafe impl Send for ChainRef {} + impl Chain { unsafe fn whole(&self) -> Result { if let Some(p) = self.whole.get() { return Ok(p.clone()); } @@ -110,7 +116,7 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT let mut pieces = built.into_iter().collect::>>()?.into_iter(); let first = pieces.next()?; let chain = Arc::new(Chain { builder, tail: pieces.collect(), words: plan.dwords_per_group, - whole_code: whole.to_vec(), whole: OnceLock::new(), build_whole: Mutex::new(()) }); + whole_code: whole.to_vec(), whole: OnceLock::new(), build_whole: Mutex::new(()), whole_sent: Default::default() }); PROGRAMS.fetch_add(1, Ordering::Relaxed); PIECES.fetch_add(plan.pieces.len() as u64, Ordering::Relaxed); *result = first.into_raw(); @@ -128,6 +134,48 @@ pub(in crate::hle::d3d12x) unsafe fn whole(pso: usize) -> Result Result { + let chain = CHAINS.lock().unwrap().get(&pso).cloned(); + let Some(chain) = chain else { return Ok(Whole::Plain) }; + if let Some(p) = chain.whole.get() { + return Ok(Whole::Ready(p.clone())); + } + if !super::super::pso_async::cs_goes_on() { + return chain.whole().map(Whole::Ready); + } + if !chain.whole_sent.swap(true, Ordering::AcqRel) { + let job = ChainRef(chain.clone()); + // (named as the pipeline workers: a huge compile takes its turn at the gate) + let spawned = std::thread::Builder::new().name("xcl-pso-whole".into()).stack_size(64 << 20).spawn(move || { + let job = job; + if let Err(e) = unsafe { job.0.whole() } { + crate::xlog_warn!("D3D12.X: the whole form of a split compute program was not built ({e}): its indirect dispatches stay left out"); + } + }); + if spawned.is_err() { + chain.whole_sent.store(false, Ordering::Release); + return chain.whole().map(Whole::Ready); + } + } + Ok(Whole::Late) +} + struct Scratch { buffer: ID3D12Resource, bytes: u64,