Skip to content

D3D12.X: timestamps are the CPU's clock on a host without GPU timestamps - #996

Open
shortside wants to merge 1 commit into
xcl-dev:mainfrom
shortside:d3d12x/host-without-gpu-timestamps
Open

shortside wants to merge 1 commit into
xcl-dev:mainfrom
shortside:d3d12x/host-without-gpu-timestamps

Conversation

@shortside

@shortside shortside commented Nov 24, 2026 •

Copy link
Copy Markdown

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. A title's Write64BitValueBottomOfPipeX / Write64BitValueTopOfPipeX timestamp therefore stays 0 for ever: timestamps_outside_unified_memory_land_after_the_queue fails there ("never landed (top=0, bottom=0)"), and so does the test title's "no GPU timestamp" check. Part of #258.

On such a host XCL's timestamps become the CPU's clock (QueryPerformanceCounter). A host with GPU timestamps is asked one GetTimestampFrequency per title queue and nothing else changes for it.

Related: #317 (draft) touches the same functions for a different thing, the console's OS-clock timestamp flags. This PR does not change which flags mean what.

What is in it (one commit, +100 / -5)

  • hle/d3d12x/cpu_writes.rs: cpu_clock() and what turns it on: a title queue whose timestamp frequency is below 1000 ticks a second (note_host_timestamp_frequency), or a timestamp query that resolved to 0 on a queue that reached its fence (that write gets the clock's value too). Unit test a_60_hz_timestamp_frequency_is_no_gpu_clock.
  • hle/d3d12x/device_x.rs: create_command_queue_x asks a direct / compute queue its timestamp frequency.
  • hle/d3d12x/list_x.rs: with cpu_clock(), timestamp writes the clock's value through write, as any other Write64BitValue*. A target in unified memory gets it from the GPU, in the list's order (WriteBufferImmediate, marker in for top of pipe, marker out for bottom). A target outside unified memory gets it from the cpu_writes worker after the queue's fence, as every other value written there does.
  • hle/d3d12x/real.rs: with cpu_clock(), queue GetTimestampFrequency answers the clock's frequency and GetClockCalibration its value for both.

What it is not: the value is the clock when the timestamp was recorded, not when the GPU got there, so GPU times measured between two timestamps come out too short (top and bottom of one submission differ only by the CPU time between the two calls). It also changes the value, not the ordering: outside unified memory the write still comes from the cpu_writes worker, so the gap @Lucasyudi described in #258 (a title's own Signal and wait can overtake the worker's write) is as open as before, for timestamps as for any other value. An earlier version wrote every clock value from that worker; the test title, whose timestamp is in unified memory, read 0 right after its own fence, which is why those go through the list.

Tested

  • MacBook Pro M5 Pro, macOS 27.2.1, Rosetta 2; CrossOver 26.3 (Wine 11.0), Windows 10 bottle, D3DMetal; DXC 1.9.2609. Cross-built for x86_64-pc-windows-msvc with clang-cl / lld-link 23.1.6 and an xwin SDK 10.0.26100, Rust 1.100.0, debug. Test binaries run under Wine. main is 9aec53d, the head is f242ea8.
  • Without XCL, a D3D12 program on this host (EndQuery + ResolveQueryData of three timestamps into a readback buffer, fence reached): GetTimestampFrequency S_OK, 60; all three slots 0.
  • xcl-<hash>.exe cpu_writes:: timestamps_outside_unified_memory --test-threads=1, 3 runs each: main 2 passed, 1 failed (timestamps_outside_unified_memory_land_after_the_queue) every run; head 4 passed, 0 failed every run (the log shows "no GPU timestamps on this host (a timestamp query resolved to 0)").
  • hle:: --skip ::gpu_ --test-threads=1, skipping the 3 tests that crash this host on main too (create_list_reset_and_clear_state_track_initial_pso_on_d3d12, placed_title_textures_share_vram_and_preserve_pixels, d32s8_targets_take_their_own_size_of_video_memory): main 288 passed, 4 failed, 2 ignored; head 290 passed, 3 failed, 2 ignored. The only test that changed is the timestamp one; the other 3 failures are the same on both (write_buffer_immediate_on_open_existing_heap_visible_to_cpu, mapped_frame_dump_distinguishes_dark_bright_and_undecoded_content, xma2_source_voice_decodes_and_manages_lifetime, the last one is XAudio2: GetState names the oldest queued buffer when the host gives none #890).
  • The test title (tests/e2e.rs): main dies on this host before its timestamp check (the fallback PSO, D3D12.X: a stage the PC cannot run goes to the fallback without the driver #750). On a local stack of mac-on-main-wip (db065d8) + D3D12.X: a stage the PC cannot run goes to the fallback without the driver #750's diff + this change (ported by hand, the branch's cpu_writes.rs differs): "D3D12.X: console command list memory methods (writes, timestamp, fill, copy, KickoffX, Wait32/64)" passes in 3 of 3 runs, 47 PASS, and the log shows "no GPU timestamps on this host (the queue's timestamp frequency is 60)". Without this change that stack fails the check with "no GPU timestamp" (D3D12.X: a stage the PC cannot run goes to the fallback without the driver #750's description has the same).

Tested by others

Regression run

  • From my Mac: MacBook Pro M5 Pro (24 GB), macOS 27.2.1, CrossOver 26.3 (Wine 11.0), D3DMetal. scripts/smoke.ps1 -Seconds 60 under PowerShell 7 on macOS, the retail title, release builds cross-built for x86_64-pc-windows-msvc, each from a clean tree of the commit, each on a fresh data root, one after the other. The script starts xcl.exe through a small wrapper that runs it under Wine and rewrites only the Log: / Report: lines to macOS paths; XCL_DXC_THREADS=4 for both runs. The blocks are as printed.
  • What they show and what they do not: on this host main does not draw (OpenExistingHeapFromAddress … Not implemented, macOS (Apple Silicon, CrossOver + D3DMetal): the newer retail game reaches its first-run menus at 30 fps #258), so both runs sit at 0 frames for the 60 s and end cleanly; the device is made, but XCL's log shows no command queue and no timestamp before that point, so the change is not reached. It is a check that nothing is worse (no crash, no device removal, the same frames), not a run of the change. gpu: is D3DMetal's adapter name, and peak commit reads 0 MB under Wine, so the commit limit cannot be compared here. A Windows pair would say more.

main:

xcl-smoke v1
commit: b9b6ea62080cc62fc19e212033372d01c6214e5c
gpu: AMD Compatibility Mode
title: Retail game
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

this PR:

xcl-smoke v1
commit: f242ea8285368db7aba158f0966a0ece9376cc6c
gpu: AMD Compatibility Mode
title: Retail game
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

Not tested

  • Windows: I have no Windows machine. Two testers ran the suite there, linked under Tested by others: the new path stays off on an AMD card and the suite passes on an NVIDIA one.
  • Whether a title's own wait can overtake a timestamp written outside unified memory (the cpu_writes ordering above): timestamps_outside_unified_memory_land_after_the_queue polls until the value is there, so it cannot see that.
  • A title that makes its own EndQuery / ResolveQueryData timestamp queries on the PC queue is not intercepted and still reads 0 on such a host (noted by @mikebuckets171).
  • The GPU tests and the whole suite (cargo test --workspace does not finish on this host, see macOS (Apple Silicon, CrossOver + D3DMetal): the newer retail game reaches its first-run menus at 30 fps #258).
  • Any retail title; a title's GetTimestampFrequency / GetClockCalibration calls (the test title makes neither).
  • In 4 of 5 runs of the local stack the title's "files: delete hides the file without touching the original" check failed; I have not checked whether that comes from running repeatedly on one data folder.

AI use

  • Claude Code with Claude Fable 5.2: found the cause, wrote the probe, the change, the test and this description, and ran the tests above on my machine.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@dcastell1

Copy link
Copy Markdown

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on main@02bfd1d (baseline 727 passed, 0 failed, 41 ignored on this box):

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
728 passed; 0 failed; 41 ignored   (exit 0)

One more passing test than the baseline: the CPU-clock timestamp path on a host without GPU timestamps is covered and green.

Tree restored afterwards (full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.

@DTsordas

Copy link
Copy Markdown

Test report for #996 at f242ea8: Windows, AMD RDNA4. The CPU-clock path stays off on a host with GPU timestamps.

This covers the first point under Not tested: whether the new path is taken on Windows.

Tested

  • AMD Radeon RX 9060 XT (driver 32.0.32029.2010), Ryzen 5 5600X, Windows 11 Pro 26300, Rust 1.100.0 MSVC, debug; DXC 1.9.2602.17 (SDK 10.0.26100.0).
  • cargo test --workspace --no-fail-fast -- --test-threads=4 at f242ea8: 725 passed, 0 failed, 41 ignored, 0 SKIPPED. Among them: a_60_hz_timestamp_frequency_is_no_gpu_clock, readback_u64_lands_after_the_queue and timestamps_outside_unified_memory_land_after_the_queue.
  • The cpu_writes / list_x timestamp tests and --test e2e, with --nocapture: "no GPU timestamps on this host" appears 0 times in their output. It also appears 0 times in the test title's own log (target\tmp\e2e\data\XCL.TestTitle\logs\xcl.log, backend "AMD Radeon RX 9060 XT"). The title's "D3D12.X: console command list memory methods (writes, timestamp, fill, copy, KickoffX, Wait32/64)" check passes with the GPU's own timestamps.

Not tested

  • A retail title, and its GetTimestampFrequency / GetClockCalibration calls, on Windows; no xcl-smoke blocks from here.
  • NVIDIA / Intel on Windows for the off-path check: only this AMD card.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

@shortside

Copy link
Copy Markdown
Author

@Lucasyudi I only saw your timestamp plan in #258 (04:50 and 04:59) after opening this. I had searched issues and PRs, not the comments of that thread. You said it first, so this is your call.

This PR ends up at the same detection as yours (a 60 Hz queue) and the same answers for GetTimestampFrequency / GetClockCalibration. It differs in how the value gets written. I hit the race you describe too: my first version wrote the clock from the cpu_writes worker, and the test title read 0 at +0x10 right after its own fence. Here the timestamp instead goes through write, like any other Write64BitValue*: the clock value is taken when the timestamp is recorded and the GPU writes it in list order, so nothing waits on the worker. The cost is that it is the record time, not the time the GPU got there. It is not stacked on #317.

On mac-on-main-wip (db065d8) + #750 + this, the title's "console command list memory methods" check passes 3 of 3 here (M5 Pro, CrossOver 26.3).

Whichever you prefer: take this approach into yours and I close this one, or keep this one and I add whatever your version has that this lacks (your three unit tests, the 1 MHz threshold instead of my 1 kHz). I won't take it out of draft before you answer.

@mikebuckets171

Copy link
Copy Markdown

Passes on a second Apple silicon host, on the gnullvm route - and here the trigger that fires is the second one you built in.

Host: MacBook Pro M5 Pro (48 GB), macOS 27.2.1, CrossOver 26 with D3DMetal, Rosetta 2, Rust 1.100.0, x86_64-pc-windows-gnullvm with LLVM-MinGW 20261117 (clang 23.1.6), --release --locked, test binaries under Wine, one test per process (--exact <full test path> --nocapture --test-threads=1).

hle::d3d12x::list_x::tests::timestamps_outside_unified_memory_land_after_the_queue:

  • base 02bfd1d: FAILED - panicked at crates/xcl/src/hle/d3d12x/list_x.rs:4981: non-UMA GPU timestamp never landed (top=0, bottom=0), exit 0x65, after 5.34 s of polling. Same symptom the description reports.

  • head f242ea8: ok. 1 passed; 0 failed in 0.44 s, exit 0x0, with

    xcl: D3D12.X: no GPU timestamps on this host (a timestamp query resolved to 0) - a timestamp is the CPU's clock when it was recorded, written when the GPU gets there
    

Which trigger fired, and why that matters. It is the "resolved to 0 on a queue that reached its fence" one, not the frequency gate. note_host_timestamp_frequency is wired into create_command_queue_x, and a lib test that builds its own device never goes through that wrapper - so on this host the fallback is not a rare second line of defence, it is the one that actually carries the library tests. Both paths exist for a title; for these tests only the fallback can fire, and it does, reliably, on the first timestamp write.

Correcting one thing I posted earlier on #258, because it changes what your fix has to cover. I wrote that this host "has no readback heap at all" and that XCL's D3D12_HEAP_TYPE_READBACK buffer cannot be created here. That was wrong: the heap type is refused only in the initial states I first tried (GENERIC_READ, RESOLVE_DEST). In COPY_DEST it is created fine, at 64 bytes and at 32768 bytes (TIMESTAMP_SLOTS * 8, the size list_x.rs:2118 asks for), Map with no range and Map with a read range both S_OK - which is exactly the shape runner.rs:1442 and list_x.rs:2127 ask for. So on this host the readback plumbing of the timestamp path works, and what fails is the content: EndQuery / ClearRenderTargetView / EndQuery, ResolveQueryData into that READBACK + COPY_DEST buffer, fence signalled, Map(0, 0..16) S_OK → stamps 0 and 0. That makes your change the right one rather than a workaround for a missing buffer type: the query heap and the readback buffer are both there, they just never get a number in them.

One number that argues for your real.rs half specifically. A plain D3D12 program on this host (no XCL) gets GetTimestampFrequency S_OK = 60, and GetClockCalibration S_OK with its GPU value equal to the CPU value (or 41-42 ticks below it) and advancing with wall time at ~10^6-10^7 ticks per run - i.e. the "GPU" clock it reports is already the host QPC at ~1 GHz. A title that reads both is given 60 ticks/second and a ~1 GHz-scaled value at the same time, so the error before this change is a factor of ~16.7 million, not just a zero. Making both answer from the clock is what removes that.

What this change does not reach, as far as I can read the diff (not tested against a title). timestamp() returns early with the clock when cpu_clock() is set, and real.rs answers GetTimestampFrequency / GetClockCalibration from the clock, so XCL's own Write64BitValue*OfPipeX timestamps and the port's frequency answers are covered. A title that calls BeginQuery / EndQuery / ResolveQueryData / Map itself through the real queue is not intercepted by this change, and on this host such a query resolves to 0 - the test title makes neither call, so nothing here shows whether a real title does that. Flagging it because it is the remaining route to a 0, not because the PR should necessarily cover it.

Host: macOS 27.2.1 (Apple silicon, M5 Pro), CrossOver 26, D3DMetal; local inference unloaded and the bottle's wineserver stopped for each run; the plain-D3D12 probe is a ~250-line PE with no XCL in it and reads no files. AI use: GPT-6 Luna built both trees, ran the test and the probe, and drafted this comment; the codes and timings are from those runs.

@shortside

Copy link
Copy Markdown
Author

@mikebuckets171 your reading is right, thanks. In unified memory the value is written by the GPU in list order; outside it, it goes through the cpu_writes worker like every other value there, so the ordering gap @Lucasyudi described is untouched by this PR. My note above ("nothing waits on the worker") and the description said that too broadly. I have corrected the description and added both of your points under Not tested (the ordering outside unified memory, and a title's own timestamp queries).

@Lucasyudi so the picture for your decision: this PR supplies the value and the frequency / calibration answers; your q.Wait idea for cpu_writes::after is a separate fix that both approaches would need.

@DavidM1911

Copy link
Copy Markdown
Contributor

Independent verification, merged onto current main 384f7f7 locally (clean merge).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib timestamp: 2 passed, 0 failed — a_60_hz_timestamp_frequency_is_no_gpu_clock covers the new CPU-clock fallback path, and timestamps_outside_unified_memory_land_after_the_queue still passes with the list_x changes.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

@shortside
shortside marked this pull request as ready for review November 24, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants