From f242ea8285368db7aba158f0966a0ece9376cc6c Mon Sep 17 00:00:00 2001 From: ShortSide <83917259+shortside@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:05:41 +0100 Subject: [PATCH] D3D12.X: timestamps are the CPU's clock on a host without GPU timestamps Apple's D3DMetal (macOS under CrossOver) has no GPU timestamps: a queue's GetTimestampFrequency gives 60 and every D3D12_QUERY_TYPE_TIMESTAMP resolves to 0 (measured with a D3D12 program without XCL: three queries, fence reached, all three 0). A title's Write64BitValue{Top,Bottom}OfPipeX timestamp therefore stayed 0 for ever: timestamps_outside_unified_memory_land_after_the_queue fails there with "never landed (top=0, bottom=0)", and the test title's "no GPU timestamp". A title queue is asked its timestamp frequency when it is created; below 1000 ticks a second that is no GPU clock, and XCL's timestamps become the CPU's clock (QueryPerformanceCounter). A timestamp query that resolved to 0 on a queue that got there turns it on as well. From then on a timestamp is the clock's value when it was recorded, written through the path every other Write64BitValue takes (so it lands in the GPU's order), and the queue's GetTimestampFrequency / GetClockCalibration answer with the same clock. A host with GPU timestamps is asked one frequency per queue and nothing else changes. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/hle/d3d12x/cpu_writes.rs | 69 ++++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/device_x.rs | 9 ++++ crates/xcl/src/hle/d3d12x/list_x.rs | 11 ++-- crates/xcl/src/hle/d3d12x/real.rs | 16 ++++++ 4 files changed, 100 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/cpu_writes.rs b/crates/xcl/src/hle/d3d12x/cpu_writes.rs index eeb524893..1c791ef01 100644 --- a/crates/xcl/src/hle/d3d12x/cpu_writes.rs +++ b/crates/xcl/src/hle/d3d12x/cpu_writes.rs @@ -10,7 +10,7 @@ use std::collections::HashMap; use std::ffi::c_void; -use std::sync::atomic::{AtomicU32, AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::sync::{mpsc, Mutex, OnceLock}; use windows::core::Interface; @@ -30,6 +30,52 @@ pub(super) enum Write { ReadbackU64 { va: usize, src: usize, _buf: ID3D12Resource }, } +/// The host has no GPU timestamps, so XCL's timestamps are the CPU's clock +/// (`QueryPerformanceCounter`, with its frequency as the queue's timestamp frequency). +/// Apple's D3DMetal (macOS under CrossOver) is such a host: `GetTimestampFrequency` gives 60 +/// and every `D3D12_QUERY_TYPE_TIMESTAMP` resolves to 0, so a title that waits for a GPU +/// timestamp, or times its frames with them, read 0 for ever. +static CPU_CLOCK: AtomicBool = AtomicBool::new(false); + +/// Below this many ticks a second a queue's timestamp frequency is not a GPU clock's (PC GPUs: +/// MHz and up). +const SLOWEST_GPU_CLOCK: u64 = 1000; + +/// Whether timestamps are the CPU's clock on this host. +pub(super) fn cpu_clock() -> bool { + CPU_CLOCK.load(Ordering::Acquire) +} + +/// Whether a queue that answers `GetTimestampFrequency` with `frequency` has no GPU clock. +fn no_gpu_clock(frequency: u64) -> bool { + frequency < SLOWEST_GPU_CLOCK +} + +fn use_cpu_clock(why: std::fmt::Arguments) { + if !CPU_CLOCK.swap(true, Ordering::AcqRel) { + crate::xlog_info!("D3D12.X: no GPU timestamps on this host ({why}) – a timestamp is the CPU's clock when it was recorded, written when the GPU gets there"); + } +} + +/// A title queue's own `GetTimestampFrequency` answer, asked when the queue was created. +pub(super) fn note_host_timestamp_frequency(frequency: u64) { + if no_gpu_clock(frequency) { + use_cpu_clock(format_args!("the queue's timestamp frequency is {frequency}")); + } +} + +/// The CPU clock's ticks now. +pub(super) fn clock_now() -> u64 { + super::frame::qpc() as u64 +} + +/// The CPU clock's ticks a second. +pub(super) fn clock_frequency() -> u64 { + let mut f = 0i64; + unsafe { windows_sys::Win32::System::Performance::QueryPerformanceFrequency(&mut f) }; + f as u64 +} + struct Job { fence: ID3D12Fence, value: u64, @@ -74,7 +120,13 @@ fn worker() -> &'static Mutex> { match w { Write::Immediate(va, words) => write_now(*va, words), Write::ReadbackU64 { va, src, .. } => { - let value = (*(*src as *const AtomicU64)).load(Ordering::Acquire); + let mut value = (*(*src as *const AtomicU64)).load(Ordering::Acquire); + if value == 0 && job.fence.GetCompletedValue() >= job.value { + // the queue is done and the query resolved to nothing (a queue + // that never got there says nothing about the host) + use_cpu_clock(format_args!("a timestamp query resolved to 0")); + value = clock_now(); + } write_u64_now(*va, value); } } @@ -192,6 +244,19 @@ mod tests { } } + /// Only an answer no GPU clock gives turns the CPU's clock on. + #[test] + fn a_60_hz_timestamp_frequency_is_no_gpu_clock() { + assert!(no_gpu_clock(0)); + assert!(no_gpu_clock(60)); // Apple's D3DMetal + assert!(!no_gpu_clock(1_000_000)); + assert!(!no_gpu_clock(25_000_000)); + assert!(!no_gpu_clock(1_000_000_000)); + assert!(!no_gpu_clock(clock_frequency())); + let a = clock_now(); + assert!(a != 0 && a <= clock_now()); + } + /// A ReadbackU64 write copies the 64-bit value from the mapped readback buffer after the queue finishes. #[test] fn readback_u64_lands_after_the_queue() { diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 2af03a02d..a8b9f5d7c 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -79,6 +79,15 @@ pub(super) unsafe extern "system" fn create_command_queue_x(this: *mut c_void, d let hr = real::dev_create_command_queue(this, d.as_ptr() as usize, riid, ppv); if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { super::list_x::on_queue_create(*ppv as usize, this as usize, D3D12_COMMAND_LIST_TYPE(d[0] as i32)); + // a queue that takes timestamps says here whether the host has a GPU clock + if d[0] as i32 == D3D12_COMMAND_LIST_TYPE_DIRECT.0 || d[0] as i32 == D3D12_COMMAND_LIST_TYPE_COMPUTE.0 { + const PC_TIMESTAMP_FREQUENCY: usize = 16; + let f: unsafe extern "system" fn(*mut c_void, *mut u64) -> HRESULT = std::mem::transmute(real::orig_fn(*ppv, PC_TIMESTAMP_FREQUENCY)); + let mut frequency = 0u64; + if f(*ppv, &mut frequency) >= 0 { + super::cpu_writes::note_host_timestamp_frequency(frequency); + } + } } hr } diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 2995f05b0..e65a23c30 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2137,7 +2137,7 @@ unsafe fn ensure_timestamp_readback(dev: &ID3D12Device, pool: &mut TimestampPool } /// The GPU timestamp (the PC queue's ticks) written at `va` when the work before it is done. -unsafe fn timestamp(list: *mut c_void, va: u64, what: &str) { +unsafe fn timestamp(list: *mut c_void, va: u64, mode: D3D12_WRITEBUFFERIMMEDIATE_MODE, what: &str) { static N: AtomicU64 = AtomicU64::new(0); let ty = list_type(list); if ty != D3D12_COMMAND_LIST_TYPE_DIRECT && ty != D3D12_COMMAND_LIST_TYPE_COMPUTE { @@ -2150,6 +2150,11 @@ unsafe fn timestamp(list: *mut c_void, va: u64, what: &str) { if uma_dst.is_none() && !super::cpu_writes::writable(va as usize, 8) { return warn(&N, what, format_args!("{va:#x} is not in unified memory")); } + if super::cpu_writes::cpu_clock() { + // no GPU timestamps on this host: the CPU's clock now, written as any other value is + let now = super::cpu_writes::clock_now(); + return write(list, va, &[now as u32, (now >> 32) as u32], mode, what); + } let Some(dev) = device_of(list) else { return }; let (heap, readback) = { let mut m = TIMESTAMPS.lock().unwrap(); @@ -2210,7 +2215,7 @@ pub(super) unsafe extern "system" fn write64_bottom_x(this: *mut c_void, va: u64 const W: &str = "Write64BitValueBottomOfPipeX"; record_write_trace(this, va, value, flags, "W64B"); if flags & BOTTOM_TIMESTAMPS != 0 { - return timestamp(this, va, W); + return timestamp(this, va, D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, W); } note_write64_recorded(this, va, value); write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, W); @@ -2482,7 +2487,7 @@ pub(super) unsafe extern "system" fn write64_top_x(this: *mut c_void, va: u64, v const W: &str = "Write64BitValueTopOfPipeX"; record_write_trace(this, va, value, flags, "W64T"); if flags & TOP_TIMESTAMPS != 0 { - return timestamp(this, va, W); + return timestamp(this, va, D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_IN, W); } note_write64_recorded(this, va, value); write(this, va, &[value as u32, (value >> 32) as u32], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_IN, W); diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 5b01e8569..279e5108b 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4979,6 +4979,22 @@ unsafe fn queue_dispatch(slot: usize, this: *mut c_void, a1: usize, a2: usize, a } hr as u32 as usize } + // a host without GPU timestamps: the clock XCL's timestamps are read from + 16 if super::cpu_writes::cpu_clock() => { + if a1 != 0 { + (a1 as *mut u64).write_unaligned(super::cpu_writes::clock_frequency()); + } + 0 + } + 17 if super::cpu_writes::cpu_clock() => { + let now = super::cpu_writes::clock_now(); + for p in [a1, a2] { + if p != 0 { + (p as *mut u64).write_unaligned(now); + } + } + 0 + } 16 => { let f: unsafe extern "system" fn(*mut c_void, usize) -> i32 = std::mem::transmute(pc(PC_TIMESTAMP_FREQUENCY)); f(this, a1) as u32 as usize