From 96e68a46b2b2b5bca88a24bb4b60ba9ea18406c3 Mon Sep 17 00:00:00 2001 From: dhruv manral Date: Tue, 24 Nov 2026 15:14:49 +0530 Subject: [PATCH] D3D12.X: bound driver PSO compile concurrency and surface stage progress Part of #156. Bounds background driver pipeline compilation concurrency (1 big >= XCL_BIG_PSO_KB behind free-commit wait capped at 30 s, then 2 on wave32-only / 4 on wave64 via XCL_DRIVER_COMPILE_THREADS) across compute, graphics, MS, and GS paths while keeping title threads and xcl-pso-urgent exempt. Re-enters the guard on cold retry if a cached compute blob is rejected, fixes pso_async::counts() to exclude dormant lazy stand-ins, translates GS/fallback stream formats and topologies, and surfaces driver compile progress in the window title. Assisted-by: T3 Code / Antigravity (gemini-pro-orchestrator-neon) --- crates/xcl/src/hle/d3d12x/present.rs | 14 +- crates/xcl/src/hle/d3d12x/pso_async.rs | 111 ++++++++-- crates/xcl/src/hle/d3d12x/real.rs | 293 +++++++++++++++++++++++-- 3 files changed, 379 insertions(+), 39 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index 01b3c92a4..d57d5edbc 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -371,10 +371,10 @@ impl FrameRate { } } -/// While XCL compiles shaders (DXC, programs the disk cache did not have – the title draws +/// While XCL compiles shaders (DXC or the GPU driver's pipeline compiler, programs the disk cache did not have – the title draws /// nothing with those pipelines meanwhile: after an XCL update the screen stays black while /// thousands recompile), the window's title says so: -/// "XCL – | N FPS | compiling shaders: N running, M done[, Q pipelines queued][, F failed]". +/// "XCL – | N FPS | compiling shaders: N dxc, M done[, D driver (C done)][, Q pipelines queued][, F failed]". /// It stays 2 s after the last compile (compiles in the title's own calls come one by one), /// then only the game name and presented frame rate remain. unsafe fn show_compiling(hwnd: HWND) { @@ -390,7 +390,8 @@ unsafe fn show_compiling(hwnd: HWND) { .sample(now, presented); let (_, failed, queued) = super::pso_async::counts(); let (running, done) = crate::gpu::recomp::dxc::compile_counts(); - let active = running > 0 || queued > 0; + let (driver_running, driver_done) = super::real::driver_compile_counts(); + let active = running > 0 || driver_running > 0 || queued > 0; let recent = { let mut last = LAST_ACTIVE.lock().unwrap(); if active { @@ -400,9 +401,14 @@ unsafe fn show_compiling(hwnd: HWND) { }; let title = format!("XCL – {} | {fps:.1} FPS", window_name()); let text = if recent { + let driver = if driver_running > 0 || driver_done > 0 { + format!(", {driver_running} driver ({driver_done} done)") + } else { + String::new() + }; let queued = if queued > 0 { format!(", {queued} pipelines queued") } else { String::new() }; let failed = if failed > 0 { format!(", {failed} failed") } else { String::new() }; - format!("{title} | compiling shaders: {running} running, {done} done{queued}{failed}") + format!("{title} | compiling shaders: {running} dxc, {done} done{driver}{queued}{failed}") } else { title }; diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 33a85cf12..8283515d6 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -16,6 +16,8 @@ struct Pending { job: Mutex>, /// the build went to the workers (a lazy one goes on its first `resolve`) sent: std::sync::atomic::AtomicBool, + /// included in `QUEUED` (flipped back to false once in `complete`) + counted: std::sync::atomic::AtomicBool, /// the real pipeline state (raw pointer), 0 while building or when building failed real: AtomicUsize, done: Mutex, @@ -42,6 +44,20 @@ impl Pending { /// stand-in handed to the title → its real pipeline static PENDING: Mutex>>> = Mutex::new(None); +fn finish_pending(p: &Pending, real: usize, queued: &AtomicUsize) -> bool { + let mut done = p.done.lock().unwrap(); + let orphan = p.orphan.load(Ordering::Acquire); + if !orphan { + p.real.store(real, Ordering::Release); + } + *done = true; + if p.counted.swap(false, Ordering::AcqRel) { + queued.fetch_sub(1, Ordering::Relaxed); + } + p.cv.notify_all(); + orphan +} + /// The build is done (`real`: its pipeline, 0 = failed). fn complete(p: &Pending, real: usize) { if real == 0 { @@ -49,23 +65,16 @@ fn complete(p: &Pending, real: usize) { } else { BUILT.fetch_add(1, Ordering::Relaxed); } - let orphan = { - let mut done = p.done.lock().unwrap(); - let orphan = p.orphan.load(Ordering::Acquire); - if !orphan { - p.real.store(real, Ordering::Release); - } - *done = true; - p.cv.notify_all(); - orphan - }; - // outside `done`: the Release override takes PENDING (`counts` takes PENDING, then `done`) + let orphan = finish_pending(p, real, &QUEUED); + // outside `done`: the Release override takes PENDING if orphan { release(real); } } static BUILT: AtomicUsize = AtomicUsize::new(0); static FAILED: AtomicUsize = AtomicUsize::new(0); +/// Pipelines currently queued for or building on the background workers (excludes dormant lazy stand-ins). +static QUEUED: AtomicUsize = AtomicUsize::new(0); pub fn enabled() -> bool { std::env::var("XCL_ASYNC_PSO").map_or(true, |v| v != "0") @@ -128,7 +137,8 @@ pub fn submit_lazy(standin: usize, compute: bool, job: Job, key: u64) { fn submit_as(standin: usize, compute: bool, job: Job, send: bool, key: u64) { let p = Arc::new(Pending { job: Mutex::new(Some(job)), - sent: std::sync::atomic::AtomicBool::new(send), + sent: std::sync::atomic::AtomicBool::new(false), + counted: std::sync::atomic::AtomicBool::new(false), real: AtomicUsize::new(0), done: Mutex::new(false), cv: Condvar::new(), @@ -144,7 +154,7 @@ fn submit_as(standin: usize, compute: bool, job: Job, send: bool, key: u64) { crate::xlog_info!("D3D12.X: pipelines whose shaders are not compiled yet get a stand-in and are built in the background"); }); if send { - let _ = queue().lock().unwrap().send(p); + enqueue(p); } else if used::known(key) { if let Some(q) = preload() { q.list.lock().unwrap().push_back(p); @@ -153,6 +163,27 @@ fn submit_as(standin: usize, compute: bool, job: Job, send: bool, key: u64) { } } +/// Mark `p` sent and increment `queued` once; returns `true` iff `p` should be sent to workers +/// (returns `false` if already sent or already completed inline by `resolve_wait`). +fn mark_enqueued(p: &Pending, queued: &AtomicUsize) -> bool { + if !p.sent.swap(true, Ordering::AcqRel) { + let done = p.done.lock().unwrap(); + if !*done { + p.counted.store(true, Ordering::Release); + queued.fetch_add(1, Ordering::Relaxed); + return true; + } + } + false +} + +/// Send a pipeline to the workers once (skipped if already completed inline by `resolve_wait`). +fn enqueue(p: Arc) { + if mark_enqueued(&p, &QUEUED) { + let _ = queue().lock().unwrap().send(p); + } +} + /// The pipelines a title used, by a hash of their shaders, kept between runs /// (`used-pipelines.txt` beside the DXIL cache, a key a line). /// @@ -357,9 +388,7 @@ pub fn resolve(pso: usize) -> usize { match p.real.load(Ordering::Acquire) { 0 => { // a lazy one is wanted now: to the workers - if !p.sent.swap(true, Ordering::AcqRel) { - let _ = queue().lock().unwrap().send(p); - } + enqueue(p); pso } r => r, @@ -462,8 +491,52 @@ pub fn is_pending(pso: usize) -> bool { entry(pso).is_some_and(|p| !*p.done.lock().unwrap()) } -/// (built in the background, failed, still building) +/// (built in the background, failed, queued or building on workers) pub fn counts() -> (usize, usize, usize) { - let pending = PENDING.lock().unwrap().as_ref().map_or(0, |m| m.values().filter(|p| !*p.done.lock().unwrap()).count()); - (BUILT.load(Ordering::Relaxed), FAILED.load(Ordering::Relaxed), pending) + (BUILT.load(Ordering::Relaxed), FAILED.load(Ordering::Relaxed), QUEUED.load(Ordering::Relaxed)) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn new_pending(compute: bool) -> Pending { + Pending { + job: Mutex::new(Some(Box::new(|| 0))), + sent: std::sync::atomic::AtomicBool::new(false), + counted: std::sync::atomic::AtomicBool::new(false), + real: AtomicUsize::new(0), + done: Mutex::new(false), + cv: Condvar::new(), + compute, + key: 0, + used: Default::default(), + orphan: Default::default(), + late: Default::default(), + } + } + + #[test] + fn dormant_lazy_pipelines_are_not_counted_as_queued() { + // Isolated local counter: immune to parallel tests touching the global `QUEUED`. + let queued = AtomicUsize::new(0); + + let dormant = new_pending(false); + assert_eq!(queued.load(Ordering::Relaxed), 0, "unrequested lazy pipeline must not inflate queued count"); + + // Enqueuing once increments queued; a duplicate enqueue is a no-op; completion drains it. + assert!(mark_enqueued(&dormant, &queued)); + assert_eq!(queued.load(Ordering::Relaxed), 1); + assert!(!mark_enqueued(&dormant, &queued)); + assert_eq!(queued.load(Ordering::Relaxed), 1); + assert!(!finish_pending(&dormant, 0, &queued)); + assert_eq!(queued.load(Ordering::Relaxed), 0); + + // Inline completion (resolve_wait) followed by resolve/enqueue must never increment or leak queued. + let inline = new_pending(true); + assert!(!finish_pending(&inline, 0, &queued)); + assert_eq!(queued.load(Ordering::Relaxed), 0); + assert!(!mark_enqueued(&inline, &queued)); + assert_eq!(queued.load(Ordering::Relaxed), 0); + } } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 5b01e8569..b29517bcc 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3333,10 +3333,40 @@ unsafe fn abi_rs_raw(this: *mut c_void) -> Option<*mut c_void> { /// A compute pipeline with a console CS: `None` = not a console shader, `Some(Err)` = console /// shader the recompiler could not take, `Some(Ok(hr))` = translated pipeline created (or the /// driver's verdict on it). -/// DXIL size from which a compute pipeline's driver compile goes to the background -const BIG_DRIVER_COMPILE: usize = 256 << 10; +/// DXIL size from which a pipeline's driver compile is serialized (`BIG_COMPILES`) and +/// deferred to the background (`XCL_BIG_PSO_KB`, default 256 KiB). +fn big_driver_compile_bytes() -> usize { + static N: OnceLock = OnceLock::new(); + *N.get_or_init(|| { + std::env::var("XCL_BIG_PSO_KB") + .ok() + .and_then(|v| v.parse::().ok()) + .unwrap_or(256) + .saturating_mul(1024) + }) +} + +/// Maximum concurrent translated driver `Create*PipelineState` calls +/// (`XCL_DRIVER_COMPILE_THREADS`, default 2 on 32-lane-only GPUs such as NVIDIA to avoid +/// `nvgpucomp64.dll` process-heap lock convoys and commit exhaustion, 4 on 64-lane GPUs). +fn driver_compile_threads(wave32_only: bool) -> usize { + static N: OnceLock = OnceLock::new(); + *N.get_or_init(|| { + let default = if wave32_only { 2 } else { 4 }; + let n = std::env::var("XCL_DRIVER_COMPILE_THREADS") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(default) + .clamp(1, 64); + crate::xlog_info!( + "D3D12.X: driver PSO compile concurrency = {n} (big >= {} KiB serialized to 1)", + big_driver_compile_bytes() >> 10 + ); + n + }) +} -/// A counting semaphore: background driver compiles of big programs at once. +/// A counting semaphore bounding concurrent driver pipeline compiles. struct Slots { n: Mutex, cv: std::sync::Condvar, @@ -3363,22 +3393,144 @@ impl Drop for SlotGuard { } } +/// At most one big (`>= big_driver_compile_bytes()`) translated pipeline in the driver compiler +/// at a time. Always acquired BEFORE `driver_compiles()` so waiting big shaders do not occupy +/// slots needed by small shaders. static BIG_COMPILES: Slots = Slots { n: Mutex::new(0), cv: std::sync::Condvar::new(), most: 1 }; -/// Before a big driver compile in the background: until 3 GB of commit are free (each takes -/// ~1.5 GB; the title loading next to it must not run out – no minute's grace as for DXC). +fn driver_compiles(wave32_only: bool) -> &'static Slots { + static S: OnceLock = OnceLock::new(); + S.get_or_init(|| Slots { n: Mutex::new(0), cv: std::sync::Condvar::new(), most: driver_compile_threads(wave32_only) }) +} + +/// Translated driver `Create*PipelineState` compiles: [running now, finished]. +static DRIVER_COMPILING: [AtomicUsize; 2] = [const { AtomicUsize::new(0) }; 2]; + +/// (driver PSO compiles running now, finished so far) – surfaced in the XCL window title. +pub fn driver_compile_counts() -> (usize, usize) { + (DRIVER_COMPILING[0].load(Ordering::Relaxed), DRIVER_COMPILING[1].load(Ordering::Relaxed)) +} + +fn trace_pso_time() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var("XCL_TRACE_PSO_TIME").is_ok_and(|v| v != "0")) +} + +/// Before a big driver compile in the background: wait up to 30 s for 3 GB of free commit +/// (each big compile can take ~1.5 GB in the driver). Bounded so a 32 GB machine running +/// with 1-2 GB free does not livelock while holding `BIG_COMPILES`. fn wait_for_commit() { use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; + let start = std::time::Instant::now(); loop { let mut m: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; m.dwLength = std::mem::size_of::() as u32; if unsafe { GlobalMemoryStatusEx(&mut m) } == 0 || m.ullAvailPageFile >= 3 << 30 { return; } + if start.elapsed().as_secs() >= 30 { + static WARNED: AtomicU64 = AtomicU64::new(0); + if WARNED.fetch_add(1, Ordering::Relaxed) < 4 { + crate::xlog_warn!( + "D3D12.X: low commit ({} MB free) before big driver PSO compile – waited 30s, proceeding", + m.ullAvailPageFile >> 20 + ); + } + return; + } std::thread::sleep(std::time::Duration::from_millis(200)); } } +/// Whether the current thread is a background `xcl-pso-N` (or `xcl-pso-pre-N`) worker, +/// excluding title threads and `xcl-pso-urgent`. +fn is_background_pso_worker_name(name: Option<&str>) -> bool { + name.is_some_and(|n| n.starts_with("xcl-pso-") && n != "xcl-pso-urgent") +} + +/// Bounds background (`xcl-pso-*` other than `xcl-pso-urgent`) driver PSO compilation concurrency, +/// leaves title / urgent threads exempt so blocking binds never queue behind background work, +/// tracks in-flight count for the window title, and logs slow compiles or admission waits (>= 2 s, +/// or all when `XCL_TRACE_PSO_TIME=1`). +struct DriverCompileGuard { + // drop order: release `driver` slot before `big` slot + _driver: Option, + _big: Option, + waited: std::time::Duration, + started: std::time::Instant, + dxil_bytes: usize, + kind: &'static str, + background: bool, + hr: Option, + discarded: bool, +} + +impl DriverCompileGuard { + fn enter(device: *mut c_void, dxil_bytes: usize, kind: &'static str) -> Self { + let current = std::thread::current(); + let background = is_background_pso_worker_name(current.name()); + let w32 = wave32_only(device); + let big = dxil_bytes >= big_driver_compile_bytes(); + let wait_started = std::time::Instant::now(); + // Only background `xcl-pso-N` / `xcl-pso-pre-N` workers contend on the background + // semaphores and `wait_for_commit()`. Title threads and `xcl-pso-urgent` bypass both so a + // blocking bind never queues behind unrelated background compiles or a 30 s commit wait. + // Lock order for background workers: BIG_COMPILES -> wait_for_commit -> driver_compiles. + // Small background shaders never acquire BIG_COMPILES, so they keep a free lane while + // a big shader compiles whenever driver_compile_threads >= 2. + let (_big, _driver) = if background { + let big_slot = big.then(|| { + let slot = BIG_COMPILES.acquire(); + wait_for_commit(); + slot + }); + let driver_slot = driver_compiles(w32).acquire(); + (big_slot, Some(driver_slot)) + } else { + (None, None) + }; + let waited = wait_started.elapsed(); + DRIVER_COMPILING[0].fetch_add(1, Ordering::Relaxed); + Self { _driver, _big, waited, started: std::time::Instant::now(), dxil_bytes, kind, background, hr: None, discarded: false } + } + + fn finish(mut self, hr: HRESULT) { + self.hr = Some(hr); + } + + /// Drop this guard before retrying without a rejected cached blob so the retry re-enters with + /// full DXIL sizing and does not double-count `DRIVER_COMPILING[1]`. + fn discard(mut self) { + self.discarded = true; + } +} + +impl Drop for DriverCompileGuard { + fn drop(&mut self) { + let in_flight = DRIVER_COMPILING[0].fetch_sub(1, Ordering::Relaxed); + if self.discarded { + return; + } + DRIVER_COMPILING[1].fetch_add(1, Ordering::Relaxed); + let elapsed = self.started.elapsed(); + let slow = elapsed >= std::time::Duration::from_secs(2) || self.waited >= std::time::Duration::from_secs(2); + if slow || trace_pso_time() { + static SLOW_LOGS: AtomicU64 = AtomicU64::new(0); + if trace_pso_time() || SLOW_LOGS.fetch_add(1, Ordering::Relaxed) < 64 { + let hr = self.hr.unwrap_or(0); + let lane = if self.background { "bg" } else { "urgent" }; + crate::xlog_info!( + "D3D12.X: driver PSO compile ({}, {} KB DXIL, {lane}): {:.2}s (wait {:.2}s, hr={hr:#x}, concurrent={in_flight})", + self.kind, + self.dxil_bytes >> 10, + elapsed.as_secs_f64(), + self.waited.as_secs_f64() + ); + } + } + } +} + unsafe fn translated_compute_pso(this: *mut c_void, f: PsoFn, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> Option> { use crate::gpu::recomp::pso::{console_to_dxil_for, is_console, Stage}; if !crate::gpu::shader::readable(desc as usize, std::mem::size_of::()) { @@ -3404,7 +3556,7 @@ unsafe fn translated_compute_pso(this: *mut c_void, f: PsoFn, desc: *const c_voi // a big program: the driver's compile takes it seconds to a minute (a retail game's 1 MB // ray-tracing resolve programs ~30 s each, ~600 of them): built in the background like one // whose DXIL is missing – the title's loading does not wait for it, its first bind does - let big = dxil.len() >= BIG_DRIVER_COMPILE; + let big = dxil.len() >= big_driver_compile_bytes(); if big && crate::gpu::recomp::dxc::defer() { return Some(Err(())); } @@ -3428,22 +3580,22 @@ unsafe fn translated_compute_pso(this: *mut c_void, f: PsoFn, desc: *const c_voi copy.CachedPSO = D3D12_CACHED_PIPELINE_STATE { pCachedBlob: b.as_ptr() as *const c_void, CachedBlobSizeInBytes: b.len() }; } let started = std::time::Instant::now(); - // in the background, one at a time and within the commit left (each ~1.5 GB in the driver) - let _slot = (big && cached.is_none() && std::thread::current().name().is_some_and(|n| n.starts_with("xcl-pso"))).then(|| { - let slot = BIG_COMPILES.acquire(); - wait_for_commit(); - slot - }); + // Background workers take BIG_COMPILES (1 big at a time within free commit) -> driver_compiles; + // a pipeline with the driver's cached blob enters with size 0 so it is not serialized as big. + let mut guard = DriverCompileGuard::enter(this, if cached.is_some() { 0 } else { dxil.len() }, "CS"); let mut hr = finish_pso(this, f, © as *const _ as *const c_void, riid, ppv); if hr < 0 && cached.is_some() { + guard.discard(); // a blob the driver does not take (another root signature at the same address…): compiled forget_compute_blob(key); if from_disk { driver_cache::note(false, disk.as_deref()); } copy.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default(); + guard = DriverCompileGuard::enter(this, dxil.len(), "CS"); hr = finish_pso(this, f, © as *const _ as *const c_void, riid, ppv); } + guard.finish(hr); let reused = hr >= 0 && cached.is_some() && copy.CachedPSO.CachedBlobSizeInBytes != 0; if hr >= 0 && !reused { keep_compute_blob(key, *ppv, dxil.len()); @@ -3731,7 +3883,10 @@ unsafe fn translated_graphics_pso(this: *mut c_void, f: PsoFn, desc: *const c_vo if debug_pso_switch("XCL_DEBUG_NO_CULL", ps_hash) { copy.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; } + let dxil_bytes = vs_dxil.len() + ps_dxil.as_deref().map_or(0, <[u8]>::len) + rect_gs.as_deref().map_or(0, <[u8]>::len); + let guard = DriverCompileGuard::enter(this, dxil_bytes, "VS+PS"); let hr = retry_without_rect_gs(&mut copy, rect_gs.is_some(), |c| finish_pso(this, f, c as *const _ as *const c_void, riid, ppv)); + guard.finish(hr); if hr < 0 { note_translation_error(format!( "the driver rejected the PSO with the translated shaders ({hr:#x}; topology type {}, {} render targets, DSV format {}, HS {}, GS {})", @@ -4233,11 +4388,24 @@ fn two_pass_ps(ps: Option<&[u8]>, split_wave64: bool) -> Result<(Option> /// Create the mesh pipeline (`stream`, RS and shaders already in it) and the compute pass; /// the title gets the mesh pipeline state. #[allow(clippy::too_many_arguments)] -unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu::recomp::pso::TwoPass, mesh_rs: *mut c_void, kind: &'static str, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { +unsafe fn finish_two_pass( + this: *mut c_void, + stream: &StreamOut, + tp: crate::gpu::recomp::pso::TwoPass, + ps_dxil_bytes: usize, + mesh_rs: *mut c_void, + kind: &'static str, + riid: *const GUID, + ppv: *mut *mut c_void, +) -> HRESULT { + let mesh_dxil = tp.ms.len() + ps_dxil_bytes; let Some(rs) = abi_rs_for(this, &tp.samplers) else { return E_INVALIDARG }; let fs: PsoFn = std::mem::transmute(orig_fn(this, slot_of(this, "CreatePipelineState"))); let desc: [usize; 2] = [stream.0.len(), stream.0.as_ptr() as usize]; + let mesh_kind = if kind == "MS" { "MS-mesh" } else { "GS-mesh" }; + let guard = DriverCompileGuard::enter(this, mesh_dxil, mesh_kind); let hr = finish_pso(this, fs, desc.as_ptr() as *const c_void, riid, ppv); + guard.finish(hr); if hr < 0 { note_translation_error(format!("the driver rejected the mesh pipeline from the console {kind} ({hr:#x})")); return E_INVALIDARG; @@ -4248,7 +4416,10 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: cdesc.CS = bytecode(&tp.cs); let iid = pc_iid(Kind::PipelineState); let mut compute: *mut c_void = std::ptr::null_mut(); + let cs_kind = if kind == "MS" { "MS-cs" } else { "GS-cs" }; + let guard = DriverCompileGuard::enter(this, tp.cs.len(), cs_kind); let hc = fc(this, &cdesc as *const _ as *const c_void, &iid, &mut compute); + guard.finish(hc); if hc < 0 || compute.is_null() { note_translation_error(format!("the driver rejected the compute pass of the console {kind} ({hc:#x})")); let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(*ppv as *const *const usize)).add(2)); @@ -4314,6 +4485,7 @@ unsafe fn translated_mesh_stream(this: *mut c_void, desc: *const c_void, riid: * return Some(E_INVALIDARG); } }; + let ps_bytes = ps.as_deref().map_or(0, <[u8]>::len); let Some(rs) = abi_rs_for(this, &ps_samplers) else { return Some(E_INVALIDARG) }; // the title's state (blend, rasteriser, depth, formats …) with XCL's RS and shaders let mut out = StreamOut::default(); @@ -4339,7 +4511,7 @@ unsafe fn translated_mesh_stream(this: *mut c_void, desc: *const c_void, riid: * out.raw(*ty, payload, palign == 8); } } - Some(finish_two_pass(this, &out, tp, rs, "MS", riid, ppv)) + Some(finish_two_pass(this, &out, tp, ps_bytes, rs, "MS", riid, ppv)) } /// A graphics PSO with a console VS + GS: the GS pipeline in two passes, as a mesh pipeline @@ -4390,6 +4562,7 @@ unsafe fn translated_gs_pso(this: *mut c_void, d: &D3D12_GRAPHICS_PIPELINE_STATE return E_INVALIDARG; } }; + let ps_bytes = ps.as_deref().map_or(0, <[u8]>::len); let Some(rs) = abi_rs_for(this, &ps_samplers) else { return E_INVALIDARG }; let mut out = StreamOut::default(); out.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE, &rs); @@ -4403,14 +4576,18 @@ unsafe fn translated_gs_pso(this: *mut c_void, d: &D3D12_GRAPHICS_PIPELINE_STATE out.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL, &d.DepthStencilState); let mut rt = D3D12_RT_FORMAT_ARRAY { NumRenderTargets: d.NumRenderTargets, ..Default::default() }; rt.RTFormats = d.RTVFormats; + for f in &mut rt.RTFormats { + *f = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_target_format(f.0 as u32) as i32); + } + let dsv = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_view_format(d.DSVFormat.0 as u32) as i32); out.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS, &rt); - out.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, &d.DSVFormat); + out.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, &dsv); out.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC, &d.SampleDesc); out.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_NODE_MASK, &d.NodeMask); if d.Flags.0 != 0 { out.put(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_FLAGS, &d.Flags); } - finish_two_pass(this, &out, tp, rs, "GS", riid, ppv) + finish_two_pass(this, &out, tp, ps_bytes, rs, "GS", riid, ppv) } /// Rewrite a pipeline state stream with stand-in shaders. None if not possible. @@ -4445,6 +4622,25 @@ unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec, [usize; 2])> { 25 => put(&mut buf, &dm?.0), 12 | 19 => buf[at..at + 16].fill(0), // input layout, cached PSO 7 => buf[at..at + 32].fill(0), // stream output + // console-only topology (e.g. rect list = 19) and target / depth formats must be + // translated for stand-ins and fallback streams too, else D3D12 rejects with E_INVALIDARG + 14 if psize >= 4 && at + 4 <= buf.len() => { + let topo = u32::from_le_bytes(buf[at..at + 4].try_into().unwrap()); + if !(1..=4).contains(&topo) { + let tri = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE.0 as u32; + buf[at..at + 4].copy_from_slice(&tri.to_le_bytes()); + } + } + 15 if psize >= 32 && at + 32 <= buf.len() => { + for k in 0..8 { + let f = super::device_x::pc_target_format(u32::from_le_bytes(buf[at + 4 * k..at + 4 * k + 4].try_into().unwrap())); + buf[at + 4 * k..at + 4 * k + 4].copy_from_slice(&f.to_le_bytes()); + } + } + 16 if psize >= 4 && at + 4 <= buf.len() => { + let f = super::device_x::pc_target_format(u32::from_le_bytes(buf[at..at + 4].try_into().unwrap())); + buf[at..at + 4].copy_from_slice(&f.to_le_bytes()); + } _ => {} } off += (poff + psize + 7) & !7; @@ -5625,6 +5821,71 @@ mod queue_tests { } QUEUE_OLDER.store(false, Ordering::Release); } + + #[test] + fn big_compile_serialization_leaves_fast_lane_for_small_compiles() { + static BIG: Slots = Slots { n: Mutex::new(0), cv: std::sync::Condvar::new(), most: 1 }; + static TOTAL: Slots = Slots { n: Mutex::new(0), cv: std::sync::Condvar::new(), most: 2 }; + + // First big compile acquires BIG -> TOTAL. + let b1 = BIG.acquire(); + let t1 = TOTAL.acquire(); + + // A second big compile blocks on BIG without taking TOTAL, so a small compile can still + // acquire TOTAL and finish while b1 is running. + let b2_entered = std::sync::Arc::new(AtomicBool::new(false)); + let b2_flag = b2_entered.clone(); + let h = std::thread::spawn(move || { + let _b2 = BIG.acquire(); + b2_flag.store(true, Ordering::Release); + let _t2 = TOTAL.acquire(); + }); + + let small = TOTAL.acquire(); + assert!(!b2_entered.load(Ordering::Acquire), "second big compile must wait on BIG"); + drop(small); + drop(t1); + drop(b1); + h.join().unwrap(); + assert!(b2_entered.load(Ordering::Acquire)); + } + + #[test] + fn urgent_and_title_threads_are_exempt_from_background_slots() { + assert!(is_background_pso_worker_name(Some("xcl-pso-0"))); + assert!(is_background_pso_worker_name(Some("xcl-pso-14"))); + assert!(is_background_pso_worker_name(Some("xcl-pso-pre-0"))); + assert!(!is_background_pso_worker_name(Some("xcl-pso-urgent"))); + assert!(!is_background_pso_worker_name(Some("main"))); + assert!(!is_background_pso_worker_name(None)); + + // Saturate both background slots and verify an exempt (title / urgent) compile enters + // immediately without blocking on either semaphore. + let b = BIG_COMPILES.acquire(); + let w32 = wave32_only(std::ptr::null_mut()); + let cap = driver_compile_threads(w32); + let held: Vec = (0..cap).map(|_| driver_compiles(w32).acquire()).collect(); + + let guard = DriverCompileGuard::enter(std::ptr::null_mut(), big_driver_compile_bytes(), "CS"); + assert!(!guard.background, "title/test thread is not an xcl-pso-* worker"); + assert!(guard._big.is_none() && guard._driver.is_none(), "title compile must bypass background slots"); + guard.finish(S_OK); + + // Also verify a thread explicitly named `xcl-pso-urgent` bypasses saturated background slots. + std::thread::Builder::new() + .name("xcl-pso-urgent".into()) + .spawn(|| { + let g = DriverCompileGuard::enter(std::ptr::null_mut(), big_driver_compile_bytes(), "CS"); + assert!(!g.background && g._big.is_none() && g._driver.is_none()); + g.finish(S_OK); + }) + .unwrap() + .join() + .unwrap(); + + drop(held); + drop(b); + } } #[cfg(test)]