diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index 66ee7dd6e3..c850d64cdc 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -766,10 +766,19 @@ impl FrameRate { /// While XCL compiles shaders (DXC, 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]". -/// 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. +/// thousands recompile) or the driver builds pipelines, the window's title says so +/// (`compiling_text`). 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. +/// " | compiling shaders: DXC N running, M done | driver N building[ (W waiting for a +/// slot)], D done[, Q pipelines queued][, F failed]": `dxc` (running, done), `driver` builds +/// (running, waiting for a huge-compile slot, done; `real::driver_build_counts`). +fn compiling_text(title: &str, (running, done): (usize, usize), (building, waiting, built): (usize, usize, usize), queued: usize, failed: usize) -> String { + let waiting = if waiting > 0 { format!(" ({waiting} waiting for a slot)") } 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: DXC {running} running, {done} done | driver {building} building{waiting}, {built} done{queued}{failed}") +} + unsafe fn show_compiling(hwnd: HWND) { static SHOWN: Mutex<Option<String>> = Mutex::new(None); static LAST_ACTIVE: Mutex<Option<Instant>> = Mutex::new(None); @@ -782,8 +791,9 @@ unsafe fn show_compiling(hwnd: HWND) { .get_or_insert_with(|| FrameRate::new(now, presented)) .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 dxc = crate::gpu::recomp::dxc::compile_counts(); + let driver = super::real::driver_build_counts(); + let active = dxc.0 > 0 || driver.0 > 0 || driver.1 > 0 || queued > 0; let recent = { let mut last = LAST_ACTIVE.lock().unwrap(); if active { @@ -792,13 +802,7 @@ unsafe fn show_compiling(hwnd: HWND) { last.is_some_and(|t| t.elapsed() < Duration::from_secs(2)) }; let title = format!("XCL – {} | {fps:.1} FPS", window_name()); - let text = if recent { - 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}") - } else { - title - }; + let text = if recent { compiling_text(&title, dxc, driver, queued, failed) } else { title }; let mut shown = SHOWN.lock().unwrap(); if shown.as_deref() != Some(text.as_str()) { let _ = SetWindowTextW(hwnd, &HSTRING::from(text.as_str())); @@ -1922,6 +1926,15 @@ mod tests { drop(token); } + #[test] + fn the_title_counts_dxc_compiles_and_driver_builds() { + assert_eq!( + compiling_text("XCL – t | 30.0 FPS", (0, 12), (2, 1, 40), 5, 0), + "XCL – t | 30.0 FPS | compiling shaders: DXC 0 running, 12 done | driver 2 building (1 waiting for a slot), 40 done, 5 pipelines queued" + ); + assert_eq!(compiling_text("t", (3, 4), (0, 0, 0), 0, 1), "t | compiling shaders: DXC 3 running, 4 done | driver 0 building, 0 done, 1 failed"); + } + #[test] fn present_ring_overlaps_previous_frame_and_drains_cleanly() { let mut ring = PresentRing::new(PRESENT_SLOTS); diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 1ef79d69b7..2fba785b2e 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3930,9 +3930,79 @@ pub fn switch_form_retries() -> u64 { SWITCH_FORM_RETRIES.load(Ordering::Relaxed) } +/// Driver pipeline builds for the window title: every `Create*PipelineState` call XCL makes +/// (`driver_pso`, on a worker or on a title thread) while it runs and once done, and the huge ones +/// waiting for their slot (`driver_compile_identified`). A call given a driver blob of an earlier +/// build (`BLOB_SUPPLIED`) is a load, not a build, and is not counted. +struct DriverBuilds { + building: AtomicUsize, + waiting: AtomicUsize, + done: AtomicUsize, +} + +static DRIVER_BUILDS: DriverBuilds = DriverBuilds::new(); + +thread_local! { + /// this thread's driver call is given a driver blob (`driver_compile_identified`) + static BLOB_SUPPLIED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; +} + +struct BlobSupplied(bool); + +impl BlobSupplied { + fn set(supplied: bool) -> Self { + Self(BLOB_SUPPLIED.with(|b| b.replace(supplied))) + } +} + +impl Drop for BlobSupplied { + fn drop(&mut self) { + BLOB_SUPPLIED.with(|b| b.set(self.0)); + } +} + +/// One counted build or wait: undone when dropped (a build also counts as done then). +struct Counted<'a>(&'a AtomicUsize, Option<&'a AtomicUsize>); + +impl Drop for Counted<'_> { + fn drop(&mut self) { + self.0.fetch_sub(1, Ordering::Relaxed); + if let Some(done) = self.1 { + done.fetch_add(1, Ordering::Relaxed); + } + } +} + +impl DriverBuilds { + const fn new() -> Self { + Self { building: AtomicUsize::new(0), waiting: AtomicUsize::new(0), done: AtomicUsize::new(0) } + } + + fn building(&self) -> Counted<'_> { + self.building.fetch_add(1, Ordering::Relaxed); + Counted(&self.building, Some(&self.done)) + } + + fn waiting(&self) -> Counted<'_> { + self.waiting.fetch_add(1, Ordering::Relaxed); + Counted(&self.waiting, None) + } + + fn counts(&self) -> (usize, usize, usize) { + (self.building.load(Ordering::Relaxed), self.waiting.load(Ordering::Relaxed), self.done.load(Ordering::Relaxed)) + } +} + +/// Driver pipeline builds (running now, waiting for a huge-compile slot, done so far), for the +/// window title. +pub fn driver_build_counts() -> (usize, usize, usize) { + DRIVER_BUILDS.counts() +} + /// A driver pipeline call; E_OUTOFMEMORY while this thread translates with heap windows is /// noted for `with_switch_fallback`. unsafe fn driver_pso(f: PsoFn, this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + let _build = (!BLOB_SUPPLIED.with(|b| b.get())).then(|| DRIVER_BUILDS.building()); let heap = crate::gpu::recomp::pso::mem_windows() & crate::gpu::recomp::HEAP_WINDOWS != 0; let injected = heap && INJECTED_OOM.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |n| n.checked_sub(1)).is_ok(); let hr = if injected { @@ -4201,7 +4271,7 @@ fn compile_slots(bytes: usize) -> &'static Slots { /// for). Logs its time, size and whether it was gated. #[cfg(test)] fn driver_compile<R>(kind: &str, bytes: usize, gate: bool, build: impl FnOnce() -> R) -> R { - driver_compile_identified(kind, bytes, gate, None, build).0 + driver_compile_identified(kind, bytes, gate, None, false, build).0 } /// `XCL_PSO_TRACE=1`: every driver compile is logged, not only huge ones. @@ -4213,27 +4283,41 @@ fn pso_trace() -> bool { /// A timed driver compile with the console record's hash (`fnv64` of the shader container, as in /// `[SHADER]` lines and `xcl shaders --measure-pso`) and whether a driver blob was supplied. /// Also returns the time spent in the driver alone (not waiting for a slot). -fn driver_compile_identified<R>(kind: &str, bytes: usize, gate: bool, record: Option<(u64, bool)>, build: impl FnOnce() -> R) -> (R, std::time::Duration) { +fn driver_compile_identified<R>(kind: &str, bytes: usize, gate: bool, record: Option<u64>, cached: bool, build: impl FnOnce() -> R) -> (R, std::time::Duration) { let huge = gate && bytes >= huge_pso_bytes() && std::thread::current().name().is_some_and(|n| n.starts_with("xcl-pso")); let waited = std::time::Instant::now(); let gate_stall = huge.then(|| crate::stall::span(crate::stall::StallKind::DriverCompileGate)).flatten(); - let _slot = huge.then(|| big_compile_turn(compile_slots(bytes), || big_compile_commit_free(big_compile_headroom(bytes)), super::pso_async::build_is_wanted)); + let _slot = huge.then(|| { + let _waiting = DRIVER_BUILDS.waiting(); + big_compile_turn(compile_slots(bytes), || big_compile_commit_free(big_compile_headroom(bytes)), super::pso_async::build_is_wanted) + }); drop(gate_stall); let waited = waited.elapsed(); static SEQUENCE: AtomicU64 = AtomicU64::new(0); let sequence = SEQUENCE.fetch_add(1, Ordering::Relaxed) + 1; let log = pso_trace() || bytes >= huge_pso_bytes(); if log { - let record = record.map_or_else(String::new, |(hash, cached)| { - format!(", record {hash:016x}, driver blob {}", if cached { "supplied" } else { "absent" }) - }); + let record = record.map_or_else( + || if cached { ", driver blob supplied".to_string() } else { String::new() }, + |hash| format!(", record {hash:016x}, driver blob {}", if cached { "supplied" } else { "absent" }), + ); crate::xlog_info!("D3D12.X: driver compile start #{sequence}: {kind} PSO, {bytes} B DXIL{record}"); } let started = std::time::Instant::now(); + let _blob = BlobSupplied::set(cached); let r = build(); let took = started.elapsed(); if log { crate::xlog_info!("D3D12.X: driver compile finish #{sequence}: {kind} PSO, {bytes} B DXIL, {} ms{}", took.as_millis(), gate_note(bytes >= huge_pso_bytes(), huge, waited)); + } else if took >= std::time::Duration::from_secs(1) { + // a slow one below the huge size, logged without XCL_PSO_TRACE too (a title thread's + // graphics pipelines took 2 - 5 s each, unlogged) + let record = record.map_or_else( + || if cached { ", driver blob supplied".to_string() } else { String::new() }, + |hash| format!(", record {hash:016x}, driver blob {}", if cached { "supplied" } else { "absent" }), + ); + let thread = std::thread::current().name().unwrap_or("title").to_string(); + crate::xlog_info!("D3D12.X: slow driver compile #{sequence}: {kind} PSO, {bytes} B DXIL{record}, {} ms on {thread}", took.as_millis()); } (r, took) } @@ -4399,6 +4483,42 @@ mod big_compile_tests { assert!(gate_note(true, false, ms).contains("ungated here")); } + #[test] + fn driver_builds_count_while_running_and_once_done() { + let b = DriverBuilds::new(); + { + let _one = b.building(); + let _two = b.building(); + let _wait = b.waiting(); + assert_eq!(b.counts(), (2, 1, 0)); + } + assert_eq!(b.counts(), (0, 0, 2), "a wait is not a build"); + } + + unsafe extern "system" fn counting_driver(_: *mut c_void, _: *const c_void, _: *const GUID, _: *mut *mut c_void) -> HRESULT { + // the global counter: other tests' builds can run too, so at least this one + if DRIVER_BUILDS.counts().0 >= 1 { S_OK } else { S_FALSE } + } + + unsafe extern "system" fn blob_driver(_: *mut c_void, _: *const c_void, _: *const GUID, _: *mut *mut c_void) -> HRESULT { + if BLOB_SUPPLIED.with(|b| b.get()) { S_OK } else { S_FALSE } + } + + #[test] + fn every_driver_call_is_counted_and_a_blob_load_is_not() { + let done = DRIVER_BUILDS.counts().2; + let call = |f: PsoFn| unsafe { driver_pso(f, std::ptr::null_mut(), std::ptr::null(), std::ptr::null(), std::ptr::null_mut()) }; + assert_eq!(call(counting_driver), S_OK, "counted while in the driver"); + assert!(DRIVER_BUILDS.counts().2 > done); + // a compile given a driver blob reaches the driver as a load; a cold one does not + assert_eq!(driver_compile_identified("test", 1, false, Some(1), true, || call(blob_driver)).0, S_OK); + assert_eq!(driver_compile_identified("test", 1, false, Some(1), false, || call(blob_driver)).0, S_FALSE); + assert_eq!(driver_compile_identified("two-pass test", 1, false, None, true, || call(blob_driver)).0, S_OK, + "a cache hit without a console record is still a driver-blob load"); + let _ = std::panic::catch_unwind(|| driver_compile_identified("panic test", 1, false, None, true, || panic!("driver panic"))); + assert!(!BLOB_SUPPLIED.with(|b| b.get()), "reset after the call"); + } + #[test] fn giant_driver_compiles_have_their_own_lane() { assert!(std::ptr::eq(compile_slots(GIANT_DRIVER_COMPILE - 1), &BIG_COMPILES)); @@ -4698,7 +4818,8 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL "compute", dxil.len(), gate_compute_driver_compile(cached.is_some()), - Some((record, cached.is_some())), + Some(record), + cached.is_some(), || finish_pso(this, f, © as *const _ as *const c_void, riid, ppv), ); let mut persist_took = in_driver; @@ -4716,7 +4837,8 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL "compute retry", dxil.len(), gate_compute_driver_compile(false), - Some((record, false)), + Some(record), + false, || finish_pso(this, f, © as *const _ as *const c_void, riid, ppv), ); hr = retried; @@ -6086,7 +6208,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const driver_cache::note(false, loaded.take()); } }, - |label, has_cached, f| driver_compile_identified(label, bytes, gate_compute_driver_compile(has_cached), Some((ps_hash, has_cached)), f), + |label, has_cached, f| driver_compile_identified(label, bytes, gate_compute_driver_compile(has_cached), Some(ps_hash), has_cached, f), |c| if depth_bounds { let stream = depth_bounds_stream(c); let desc = D3D12_PIPELINE_STATE_STREAM_DESC { SizeInBytes: stream.0.len(), pPipelineStateSubobjectStream: stream.0.as_ptr() as _ }; @@ -7178,7 +7300,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: }; let desc = [stream.0.len(), stream.0.as_ptr() as usize]; let mut raw = std::ptr::null_mut(); - let (hr, took) = driver_compile_identified(label, mesh_bytes, false, None, || { + let (hr, took) = driver_compile_identified(label, mesh_bytes, false, None, blob.is_some(), || { finish_pso(this, fs, desc.as_ptr().cast(), riid, &mut raw) }); let pso = (!raw.is_null()).then(|| ID3D12PipelineState::from_raw(raw)); @@ -7254,9 +7376,9 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: let mut compute: *mut c_void = library_compute.map_or(std::ptr::null_mut(), |pso| pso.into_raw()); let (mut hc, mut cin_driver) = if compute_from_library { (S_OK, std::time::Duration::ZERO) - } else { driver_compile_identified("compute pass", tp.cs.len(), false, None, || { + } else { driver_compile_identified("compute pass", tp.cs.len(), false, None, ccached.is_some(), || { driver_pso(fc, this, &cdesc as *const _ as *const c_void, &iid, &mut compute) - }); + }) }; let mut cpersist_took = cin_driver; if hc < 0 && ccached.is_some() { forget_compute_blob(ckey); @@ -7266,7 +7388,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: take_cold_slot(); cdesc.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default(); compute = std::ptr::null_mut(); - let (retried, took) = driver_compile_identified("compute pass retry", tp.cs.len(), false, None, || { + let (retried, took) = driver_compile_identified("compute pass retry", tp.cs.len(), false, None, false, || { driver_pso(fc, this, &cdesc as *const _ as *const c_void, &iid, &mut compute) }); hc = retried; @@ -8257,7 +8379,7 @@ unsafe fn translated_graphics_stream_once(this: *mut c_void, desc: *const c_void } let submit = |label: &str, has_cached: bool, out: &StreamOut| { let d = [out.0.len(), out.0.as_ptr() as usize]; - driver_compile_identified(label, out.shader_bytes(), gate_compute_driver_compile(has_cached), Some((ps_hash, has_cached)), + driver_compile_identified(label, out.shader_bytes(), gate_compute_driver_compile(has_cached), Some(ps_hash), has_cached, || finish_pso(this, fs, d.as_ptr() as *const c_void, riid, ppv)) }; let rejected_generation = cached.clone();