Skip to content

D3D12.X: read back sparse-window Wait64BitValueX counters during CPU waits - #1044

Open
xroneyaa wants to merge 1 commit into
xcl-dev:mainfrom
xroneyaa:d3d12x-wait64-sparse-readback
Open

xroneyaa wants to merge 1 commit into
xcl-dev:mainfrom
xroneyaa:d3d12x-wait64-sparse-readback

Conversation

@xroneyaa

Copy link
Copy Markdown

When a Wait32/64BitValueX falls back to CPU polling (PendingWait::wait_until_satisfied) for a counter inside a sparse VRAM window, CPU memory only holds the last uploaded/copied-back value while GPU writes live in sparse VRAM. This reads back [va, va + width) from sparse VRAM (sparse::readback_now) before spinning and at 10 ms intervals during the sleep loop (plus a final readback on timeout). Addresses item #2 measured in #505 (and keeps coherent-window / heap addresses lock-free on the fast path via uma::is_sparse). Part of #505.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs:
    • sparse::contains(va): checks gpu::uma::get().is_some_and(|u| u.is_sparse(va)) before taking STATE to verify an active sparse window covers va.
  • crates/xcl/src/hle/d3d12x/list_x.rs:
    • PendingWait::wait_until_satisfied / wait_until_satisfied_with: preserves the zero-overhead fast path when CPU memory already satisfies the wait; for unsatisfied sparse-window addresses, runs sparse::readback_now(va, width) before spinning, throttled at 10 ms intervals while polling, and once on timeout expiry.
    • Unit test pending_wait_sparse_readback_pulls_gpu_writes_and_skips_coherent verifying coherent waits never call readback and sparse waits pull delayed writes cleanly.
    • GPU test pending_wait_reads_back_gpu_write_in_sparse_window verifying a real CopyBufferRegion into a sparse window buffer (invisible to CPU-only polling) is read back and satisfies PendingWait::wait_until_satisfied.

Tested

  • GPU: NVIDIA GeForce RTX 3060 Ti (8024 MiB VRAM, driver 32.0.16.1088, 32-lane waves); CPU: Intel Core i5-12400F; 16 GB RAM; Windows 11 Pro Build 22631; Rust 1.100.0 (stable-x86_64-pc-windows-msvc), debug (scripts/test.ps1) and release (cargo build --release -p xcl).
  • cargo test --workspace -- --test-threads=4 (scripts/test.ps1): 729 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 (no wave64: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware)).
  • cargo test -p xcl --lib pending_wait_ -- --test-threads=4: 3 passed, 0 failed (run 3 consecutive times with 0 flakes). In pending_wait_reads_back_gpu_write_in_sparse_window, CPU-only polling (wait_until_satisfied_with(..., false, ...)) fails on the real GPU sparse-VRAM write before wait_until_satisfied passes with sparse readback enabled.

Regression run
main:

xcl-smoke v1
commit: 6efdabcdaac93ce5cc123a85e85deba5d49cb294
gpu: NVIDIA GeForce RTX 3060 Ti
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1508 (PresentX 1509)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 10406 MB

PR:

xcl-smoke v1
commit: 8fd59213003554c868d6b8729ecdb1fb7d09a77c
gpu: NVIDIA GeForce RTX 3060 Ti
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1554 (PresentX 1554)
presents per 2 s (last 3): 61 59 61
device removals: 0
crashes: 0
peak commit: 10404 MB

Not tested

  • AMD GPUs / wave64 paths and Linux / Wine + vkd3d-proton.
  • A retail title phase where a CPU-fallback Wait64BitValueX counter explicitly lands in a sparse window on main (not yet triggered during our 60 s retail smoke run).

AI use

  • Antigravity (Google DeepMind; exact model identifier not exposed in environment) analyzed #505 and the D3D12.X sync/sparse implementation, wrote the fix and tests, and ran the test suite and retail smoke runs.

@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 + Radeon iGPU; Rust stable MSVC, debug.

  • cargo test -p xcl --lib wait: 15 passed, 0 failed — includes the two new tests pending_wait_sparse_readback_pulls_gpu_writes_and_skips_coherent and pending_wait_reads_back_gpu_write_in_sparse_window (the latter exercises the real UMA/sparse path).

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

@AlexandreSchuelter

Copy link
Copy Markdown

Test report on AMD (RX 6900 XT, RDNA2): no new failures, and the sparse-window GPU test runs (not skipped) and passes here.

Tested

  • AMD Radeon RX 6900 XT (Navi 21), driver 32.0.21045.29003; Ryzen 7 5800X3D; Windows NT 10.0.26300
  • Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug; DXC 1.9.2602.17; XCL_NETWORK=0, XCL_REQUIRE_GPU=wave64; no SKIPPED tests
  • cargo build --workspace + cargo test --workspace --no-fail-fast -- --test-threads=4:
Tree Passed Failed Ignored
main 47efa2e 738 3 41
this PR (8fd5921) merged onto 47efa2e 740 3 41

Not tested

  • A retail title / scripts/smoke.ps1 (no game here), release build, repeated runs of the new tests, Linux / vkd3d-proton
  • The PR as-is (only merged onto main)

AI use: Claude Code (Claude Opus 5.5) fetched the branch, ran the suites and wrote this report; the runs are real, on the hardware above.

@pvfreis pvfreis left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 8fd5921 against its merge base 6efdabc, by reading the code; nothing was built or run here. The change is small and the lock side is clean: wait_all_on is reached with no XCL lock held (real.rs:5101, list_x.rs:2805 after the LISTS guard is dropped), sparse::contains and readback_now take only STATE, and nothing under STATE takes LISTS or SYNC_GRAPH, so I found no lock-order deadlock. The problem is in what readback_now does while it holds STATE, which this PR now runs every 10 ms on the waited cell.

  1. CONFIRMED: a poll can overwrite a CPU write to the waited cell with the old VRAM value (crates/xcl/src/hle/d3d12x/sparse.rs:1293-1303, copy at :1032, hash at :1037). readback_now first scans the page and uploads what the CPU changed (:1293, :1301), then submits the copy back, waits for the sparse queue (:1018) and writes the 8 bytes from the staging buffer into the cell (:1032). Nothing stops a title thread from storing to the cell between the scan and the copy; that takes a GPU round trip. Scenario: a Wait64BitValueX falls back to the CPU because no Write64 was recorded for the address (list_x.rs:1803, None => FallbackCpu), and the producer is a title thread that stores the counter on the CPU. If its store lands inside the window, the copy puts the old value back and sync_sent_range records that as sent, so the write is gone for the CPU and never reaches VRAM. On main the same window exists in res_map and readback_after_execute, but only for ranges the title asked to read or the GPU just wrote, once; here it is every 10 ms on the cell a writer is expected to hit. Fix: after the copy back, do not store over a cell whose CPU bytes changed since the scan (compare with the value read before the round trip, or compare-exchange for a 4/8-byte range), or copy into a local and only use it for the comparison.

  2. CONFIRMED (minor): the cost is not measured, and the regression run does not reach the code. Every readback is a synchronous submit + fence wait on the sparse queue under the global STATE lock, plus allocator and list resets; the first one runs before the 512 spins (list_x.rs:239-244), so a sparse-window wait that another thread would have satisfied within microseconds now pays a GPU round trip. The description says under Not tested that no CPU-fallback wait landed in a sparse window in the 60 s smoke run, so the two xcl-smoke blocks show only that the fast path is unchanged. Lists that record Write32/64 into a sparse window are already copied back after execution on main (list_x.rs:2794, :2817, real.rs:5105), so this PR only adds writes XCL does not record (a shader, a copy).

  3. CONFIRMED: the tests cover the first readback on a real sparse window, not the polling. pending_wait_reads_back_gpu_write_in_sparse_window (list_x.rs:4084-4170) does go through sparse::alloc, contains, a real CopyBufferRegion into the window buffer and the real readback_now, and checks that the CPU view is still zero before; that part does what the description says, and two reports on the PR say it ran unskipped (NVIDIA, AMD RDNA2). But the copy has completed before the wait starts (:4150), so only the readback before the spin runs on a GPU. The 10 ms loop and the readback on expiry are covered with a closure only (:4041-4081), and nothing runs a wait while another thread writes the cell (finding 1). The skip reason "no sparse UMA" (:4093) is new; CONTRIBUTING lists the reasons used so far.

Question: wait_for gives up after 5 s and readback_ranges then copies the staging buffer whether or not the copy ran (sparse.rs:811-820, :1018, :1032; that part is main's). With the sparse queue held behind a title queue (the case #433 is about), is it intended that this wait's readbacks block on it under STATE and then use those bytes?

Against CONTRIBUTING: Regression run, Not tested and AI use are there and the description check is green. AI use names the tool but says the exact model is not known; CONTRIBUTING asks for the exact model, so that is the maintainer's call. Related: #433 (open) changes readback_after_execute and removes rb_fence; #795 / #797 (merged) and #1045 (open, diagnostics) touch the same wait path in list_x.rs but not these lines.

Verdict: not ready to merge as it is; the idea is right and the locking is sound, but the poll should not store over a concurrent CPU write (1), and that is not tested.

Review by Claude Code (written by Claude Fable 5.2, checked and edited with Claude Opus 5.5); each point checked by a person before posting.

@JJAutoCompare

Copy link
Copy Markdown

Empirical test report (8fd5921)

Commit 8fd59213003554c868d6b8729ecdb1fb7d09a77c (PR #1044).

Hardware / environment

  • Intel Arc A770 selected with XCL_ADAPTER=A770, Windows x64 build 26200, and an AMD Family 23 Model 113 CPU. The fixture initialized three sparse VRAM windows and a 512 MB coherent window. The GPU driver version was not recorded.
  • rustc 1.100.0 (0ef1bc386 2026-11-09), DXC 1.9.2609.5, and Cargo's unoptimized test profile with debug info and incremental compilation disabled. The focused run used RUST_TEST_NOCAPTURE=1.

Commands and results

  • cargo test --workspace --no-run compiled the workspace test binaries outside the Arc lock (exit 0).
  • Inside Invoke-ArcTest.ps1, ran only the two new fixtures using the prebuilt xcl library test executable (exit 0):
xcl-77aca8ccb20e299f.exe --exact hle::d3d12x::list_x::tests::pending_wait_sparse_readback_pulls_gpu_writes_and_skips_coherent hle::d3d12x::list_x::tests::pending_wait_reads_back_gpu_write_in_sparse_window --test-threads=2
  • 2 passed, 0 failed, 0 ignored, 0 skipped, 706 filtered out. Both fixtures exercised their assertions.

Observed behavior

  • The CPU fixture verified that non-sparse and already-satisfied waits bypass readback. It also verified that sparse waits can complete on the first readback or after a later poll.
  • The GPU fixture copied a 64-bit value into a mapped sparse VRAM tile and waited for the copy to complete. The CPU view remained zero and a CPU-only poll timed out. The sparse wait succeeded and the CPU view matched the GPU-written value.

Not tested

The full GPU workspace suite and game smoke runs were omitted.

AI use

Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr1044-focused.log and published.

@luckyteagirl

Copy link
Copy Markdown
Contributor

Independent native Windows AMD GPU check of exact PR head 8fd59213003554c868d6b8729ecdb1fb7d09a77c.

Tested

  • Windows 11 build 26200, Ryzen 9 5980HX, Radeon RX 6800M driver 30.0.13002.13003, Rust 1.99.0 MSVC, DXC/DXIL 1.9.2609.5.
  • XCL_ADAPTER=6800M XCL_REQUIRE_GPU=wave64 cargo test --locked -p xcl --lib -j 2 pending_wait_ -- --test-threads=1 --show-output: 3 passed, 0 failed, 0 ignored in 1.26 s, with no SKIPPED line.
  • The run included pending_wait_reads_back_gpu_write_in_sparse_window. Its log shows three sparse VRAM windows and D3D12.X UMA initialization on the RX 6800M; the GPU fixture completed. The other two wait tests passed as well.

This checks the focused sparse readback regression on the PR head. I did not run a full workspace suite, a current-main merge, or a retail title phase with a sparse counter wait. No game data was used or uploaded.

AI-assisted testing and report drafting with OpenAI Codex (GPT-6); I checked the exact revision and test log before posting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants