From d4c6d3455fddb5aeaf19bc28f97d35be82d71724 Mon Sep 17 00:00:00 2001 From: imnicho Date: Tue, 24 Nov 2026 20:56:21 +0100 Subject: [PATCH] kernelx: Sleep does not count PC calls that keep the title's threads A title's Sleep does not count the time a bind waits for a pipeline still being built (pso_async::title_held). A title's thread can also be kept inside calls that take no time on the console: a submission, where XCL uploads and maps tiles before the lists and a driver can compile in them, a texture's placement on reserved memory, or a pipeline built in the title's call because it got no stand-in. On an AMD RX 9070 XT a retail title's stall check still ended it after 30 s without a present, with its submission thread inside the driver and no bind waiting (reported on #1288) - and again with 16 384 texture placements of 1 - 50 ms each in 29 s, none of them long by itself (reported on #1782). pso_async::pc_call() is a guard for such a call. While it lasts the title is held once the PC calls since the last frame shown, this one included, have taken 250 ms: one long call, or many short ones in a row. The title's own time between the calls still counts, and a frame shown starts the sum again, so a title that shows its frames is not held by its submissions. The guard is taken for the whole of q_execute_command_lists, in create_placed_resource_x, and around the driver's pipeline creation when that runs in the title's own call (a worker's build holds nobody). Tests: a_long_pc_call_holds_the_title, short_pc_calls_between_two_frames_hold_the_title. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/hle/d3d12x/device_x.rs | 3 + crates/xcl/src/hle/d3d12x/pso_async.rs | 125 ++++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/real.rs | 7 ++ 3 files changed, 132 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 8dec7b1c2..44e91bda2 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -895,6 +895,9 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, if desc.is_null() || ppv.is_null() || !crate::gpu::shader::readable(desc as usize, 0x38) { return E_INVALIDARG; } + // the PC's reserved resource and tile mappings for it can take long where the console's + // placement takes no time (`pso_async::title_held`) + let _in_pc = super::pso_async::pc_call(); let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned(); let d = pc_resource_desc(&x); let buffer = u32::from_le_bytes(d[0..4].try_into().unwrap()) == D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32; diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 3bc4ad2d6..8645667e4 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -140,10 +140,92 @@ static FAILED: AtomicUsize = AtomicUsize::new(0); /// binds waiting right now for a pipeline still being built (`title_held`) static HELD: AtomicUsize = AtomicUsize::new(0); -/// Whether a bind is waiting for a pipeline still being built: one of the title's threads is -/// held where a console title never waits (`kernelx::Sleep` does not count that time). +/// The PC calls of the title's threads (`pc_call`): the ones going on, and how long the ones +/// that ended took since the last frame was shown. +struct PcCalls { + /// (id, when it began) + now: Vec<(u64, std::time::Instant)>, + /// the frames shown when `spent` began to count + frame: u64, + /// the time of the PC calls that ended since then + spent: std::time::Duration, +} + +impl PcCalls { + const fn new() -> Self { + Self { now: Vec::new(), frame: 0, spent: std::time::Duration::ZERO } + } + + /// `frame` frames have been shown: the calls before the last of them are not this wait's. + fn at(&mut self, frame: u64) { + if frame != self.frame { + self.frame = frame; + self.spent = std::time::Duration::ZERO; + } + } + + fn begin(&mut self, id: u64, now: std::time::Instant) { + self.now.push((id, now)); + } + + fn end(&mut self, id: u64, frame: u64, now: std::time::Instant) { + self.at(frame); + if let Some(i) = self.now.iter().position(|c| c.0 == id) { + let (_, since) = self.now.swap_remove(i); + self.spent += now.saturating_duration_since(since); + } + } + + /// Whether a call going on holds the title: the PC calls since the last frame, it included, + /// have taken `PC_CALL_HOLDS_AFTER`. + fn holds(&mut self, frame: u64, now: std::time::Instant) -> bool { + self.at(frame); + let spent = self.spent; + self.now.iter().any(|(_, since)| spent + now.saturating_duration_since(*since) >= PC_CALL_HOLDS_AFTER) + } +} + +static PC_CALLS: Mutex = Mutex::new(PcCalls::new()); +/// From this much time in PC calls since the last frame on, a PC call holds the title: the +/// console's calls return at once. +const PC_CALL_HOLDS_AFTER: std::time::Duration = std::time::Duration::from_millis(250); + +/// Whether one of the title's threads is held where a console title never waits: a bind is +/// waiting for a pipeline still being built, or a PC call is going on after such calls have +/// kept the title's threads for long (`pc_call`). `kernelx::Sleep` does not count that time. pub fn title_held() -> bool { - HELD.load(Ordering::Acquire) != 0 + HELD.load(Ordering::Acquire) != 0 || held_by_pc_call() +} + +fn held_by_pc_call() -> bool { + PC_CALLS.lock().unwrap().holds(super::present::presents(), std::time::Instant::now()) +} + +/// A call of the title into D3D12 that XCL or the PC's driver can keep for long where the +/// console's takes no time: a submission (XCL's uploads and tile mappings before the lists, the +/// driver's compiling in them), a texture's placement, a pipeline built in the call. While it +/// lasts the title is held (`title_held`), once the PC calls since the last frame shown, this +/// one included, have taken `PC_CALL_HOLDS_AFTER`: one long call, or many short ones in a row +/// (a retail title placed 16 384 textures in 29 s without a frame, 1 - 50 ms each). A title +/// that shows its frames is not held by its submissions. +pub struct PcCall(u64); + +pub fn pc_call() -> PcCall { + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let id = NEXT.fetch_add(1, Ordering::Relaxed); + PC_CALLS.lock().unwrap().begin(id, std::time::Instant::now()); + PcCall(id) +} + +impl Drop for PcCall { + fn drop(&mut self) { + PC_CALLS.lock().unwrap().end(self.0, super::present::presents(), std::time::Instant::now()); + } +} + +/// Whether this thread is building a pipeline for `build` (a worker, not one of the title's). +pub(super) fn building_here() -> bool { + BUILDING.with(|b| b.borrow().is_some()) } /// A bind's wait for a pipeline still being built (`title_held`). @@ -386,6 +468,43 @@ fn preload() -> Option<&'static Preload> { mod used_tests { use super::*; + /// A PC call that has kept one of the title's threads for long holds the title, a short one + /// does not, and the hold ends with the call. + #[test] + fn a_long_pc_call_holds_the_title() { + let mut calls = PcCalls::new(); + let began = std::time::Instant::now(); + calls.begin(1, began); + calls.begin(2, began + 2 * PC_CALL_HOLDS_AFTER); + assert!(!calls.holds(0, began), "a call that has just begun holds the title"); + let later = began + 2 * PC_CALL_HOLDS_AFTER; + assert!(calls.holds(0, later), "a PC call has lasted {:?}, but the title is not held", 2 * PC_CALL_HOLDS_AFTER); + calls.end(1, 0, later); + calls.end(2, 0, later); + assert!(!calls.holds(0, later), "the calls are over, the title is still held"); + } + + /// Short PC calls add up between two frames: none of them is long, but after many in a row + /// the one going on holds the title. The title's own time between them is not held, and a + /// frame shown ends it. + #[test] + fn short_pc_calls_between_two_frames_hold_the_title() { + let mut calls = PcCalls::new(); + let step = PC_CALL_HOLDS_AFTER / 10; + let mut now = std::time::Instant::now(); + for id in 0..20 { + calls.begin(id, now); + assert!(id >= 9 || !calls.holds(7, now + step), "{id} short calls hold the title already"); + now += step; + calls.end(id, 7, now); + } + assert!(!calls.holds(7, now), "no PC call is going on, the title is held"); + calls.begin(20, now); + assert!(calls.holds(7, now), "twenty short PC calls since the last frame and one going on, but the title is not held"); + assert!(!calls.holds(8, now), "a frame was shown, the calls before it still hold the title"); + calls.end(20, 8, now); + } + /// While a bind waits for a pipeline still being built, the title is held (`title_held`): /// `kernelx::Sleep` does not count that time. #[test] diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 49c0cd711..e63c00a00 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3340,6 +3340,9 @@ fn note_fallback(what: &str, hr: HRESULT, ok: bool) { type PsoFn = unsafe extern "system" fn(*mut c_void, *const c_void, *const GUID, *mut *mut c_void) -> HRESULT; unsafe fn finish_pso(this: *mut c_void, f: PsoFn, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + // built in the call on one of the title's threads (no stand-in): the driver's time is not + // the title's (`pso_async::title_held`); a worker's build holds nobody + let _in_pc = (!super::pso_async::building_here()).then(super::pso_async::pc_call); let mut hr = driver_pso(f, this, desc, riid, ppv); if hr == E_NOINTERFACE { let iid = pc_iid(Kind::PipelineState); @@ -5957,6 +5960,10 @@ unsafe extern "system" fn q_execute_command_lists(this: *mut c_void, n: u32, lis stop_if_device_removed(); return; } + // from here to the end: XCL's uploads and tile mappings before the lists, then the driver's + // own work in the submission. Where that takes long, it is not the title's time + // (`pso_async::title_held`) + let _in_pc = super::pso_async::pc_call(); // textures in title memory the CPU wrote since: uploaded again first super::title_textures::refresh(this); // console-shaped lists run as the PC lists they were translated into (split title lists expand