Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions crates/xcl/src/hle/d3d12x/cpu_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -74,7 +120,13 @@ fn worker() -> &'static Mutex<mpsc::Sender<Job>> {
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);
}
}
Expand Down Expand Up @@ -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() {
Expand Down
9 changes: 9 additions & 0 deletions crates/xcl/src/hle/d3d12x/device_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 8 additions & 3 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading