Skip to content

D3D12.X: sparse readbacks don't wait for a queue held by a later signal - #433

Open
Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/sparse-readback-without-cpu-wait
Open

Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/sparse-readback-without-cpu-wait

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

After a title's ExecuteCommandLists whose lists write sparse windows on the GPU (Write32/64BitValue…X, FillMemoryWith32BitValueX, CopyMemoryToMemoryX), sparse::readback_after_execute copies those bytes back to the CPU's view. It waited on the CPU for the title's queue to get there, inside the title's call and while holding the sparse lock. A queue may GPU-wait for a fence that a later submit signals; async compute does this. The wait then never ends, the later submit never comes, and every other submit stops at the sparse lock. Part of #262, which covers the other half of the sparse windows' cross-queue problem: the CPU-write uploads.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs:
    • The copy back is recorded on the title's queue after its lists: a list of that queue's type, copying into a readback buffer of its own. That queue then signals a fence of its own.
    • The copy is still waited for in the call, as on main, so the bytes are in the CPU's view when the title's own fence after that work completes. Two exceptions hand it to a worker thread, which lands it once the GPU gets there, in submission order:
      • the queue has a wait that hasn't completed, so it may be held by a later submit;
      • an earlier copy back of that queue hasn't landed yet.
    • The stale-read window this opens exists only where main hangs.
    • The rb_fence that all title queues signalled is gone. Its values could complete out of order across queues.
    • A copy back's buffer and list stay alive until its fence has reached the value. Its fence exists before the copy is submitted, and if the Signal fails they are never released (from review).
  • crates/xcl/src/hle/d3d12x/real.rs: the queue's Wait is noted, both from the PC method (a new override) and from the console's queue slots. XCL's own waits, made inside its own uploads, are not noted.
  • Comparison with cpu_writes.rs: it does bottom-of-pipe writes outside unified memory always on a worker, after the GPU. So there a CPU read right after the title's fence wait can always see the old value, and the worker writes anyway after 10 s. This PR keeps that window to the case where main hangs.
  • Tests in crates/xcl/tests/, each in its own process, with the device adopted as the title's:
    • sparse_readback_waits.rs: queue A waits for F, runs a list writing 8 bytes of a sparse window, and is read back; queue B then submits and signals F. A must finish within 5 s, and the bytes must land.
    • sparse_readback_lands.rs: 20 times, a list writes 8 bytes, the title waits for its own fence and reads them at once.

Tested

  • Windows 11 Pro 10.0.26200, RTX 5070 Ti, NVIDIA 610.74, debug build. Each GPU run went through our GPU queue on its own.
  • Branch 7184866 on main 9ab7d76; it adds the review fix to 7709697. The runs by @nullmarsh and @TheHoracio below were on 7709697.
3 runs each main 9ab7d76 every copy back on the worker (first version) this PR (7184866)
cargo test -p xcl --test sparse_readback_waits hangs 3/3 — passes 3/3
cargo test -p xcl --test sparse_readback_lands passes 3/3 fails 3/3 (old bytes in round 0) passes 3/3

Regression run

main: Pending: no title on our machine yet; our own run follows when we have one.

PR: Pending: no title on our machine yet; our own run follows when we have one.

Not tested

  • A retail run, and the scripts/smoke.ps1 regression run.
  • A queue whose earlier wait hasn't completed still gets the asynchronous copy, as does any queue with an unlanded copy, even when nothing would have hung. A CPU read right after the title's fence wait can then see the old bytes, until the worker lands them.
  • One worker lands copies in submission order, so a long-held queue delays the landing for other queues. cpu_writes has the same property.
  • A readback buffer and a command list are made per readback submit; there is no pooling.

Size: 426 changed lines (+392 / -34), 189 of them tests.

Overlap:

AI use

  • Claude Code, Claude Opus 5.5.

🤖 Generated with Claude Code

@nullmarsh

Copy link
Copy Markdown

AMD run, since Not tested lists AMD: 7709697 merged onto main a6f5f7a (clean merge): pass on RDNA4, and the hang test fails without the real.rs / sparse.rs changes.

Tested

  • AMD Radeon RX 9060 XT (RDNA4), driver 32.0.32029.2010; Ryzen 7 5800X3D; Windows 11 Pro 26200; Rust 1.100.0; DXC 1.9.2602.17; debug build.
  • .\scripts\test.ps1: passes, 0 failed; no SKIPPED GPU tests.
  • cargo test -p xcl --test sparse_readback_lands --test sparse_readback_waits: both pass. XCL_REQUIRE_GPU=wave64,mesh cargo test -p xcl --lib sparse: 13 passed, 1 ignored (early_commit_timing).
  • Negative control: the merged tree with real.rs and sparse.rs back on main, new test files kept. a_readback_behind_a_wait_for_a_later_signal_does_not_hang fails after 5.4 s with queue A did not finish in 5 s: the readback waits on the CPU for A, which waits for B's later submit. gpu_writes_are_there_when_the_titles_fence_completes passes either way.

Not tested

  • A retail title (smoke.ps1); NVIDIA.

AI use

  • Claude Code (Claude Opus 5.5) ran the commands and wrote this report from the logs; I checked it before posting.

@TheHoracio

TheHoracio commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Test report on AMD RDNA3: RX 7800 XT (16 GB), driver 32.0.32015.7029, Ryzen 7 9800X3D, Windows 11 Pro 10.0.26300, Rust 1.100.0 MSVC, DXC 1.9.2602.17, debug build. No retail data was used.

Tested: the PR head 7709697 as it is (on its base 9ab7d76, not merged onto current main).

  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 653 passed, 0 failed, 40 ignored, no SKIPPED tests.
  • Both new tests pass: a_readback_behind_a_wait_for_a_later_signal_does_not_hang (sparse_readback_waits) and gpu_writes_are_there_when_the_titles_fence_completes (sparse_readback_lands). The suite did not hang.

Not tested: a regression run on a title, a release build, and the head merged onto current main.

AI use: Claude Code ran the commands and drafted this comment; the runs are real runs on the hardware above.

@Cattn

Cattn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

From a static review (no builds or runs) of 7709697:

readback_on submits the copy before creating or signaling its fence. If either operation fails, the early return drops the readback buffer and command allocator while the GPU may still be using them. In land, SetEventOnCompletion can fail and let execution reach Map and copy back while the fence is still pending. These are GPU-ordering and resource-lifetime bugs, and I found no indication of malicious code.

Keep the submitted resources alive until GPU completion is confirmed, and require that confirmation before copying back.

super::title_textures::own_work(|| queue.ExecuteCommandLists(&[Some(l)]));
let q = queue.as_raw() as usize;
if !self.rb_fences.contains_key(&q) {
let f: ID3D12Fence = self.dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).ok()?;
self.rb_fences.insert(q, (f, 0));
}
let (fence, value) = self.rb_fences.get_mut(&q)?;
*value += 1;
queue.Signal(&*fence, *value).ok()?;
Some(Readback { queue: q, fence: fence.clone(), value: *value, buf, parts, _list: (alloc, list) })

unsafe fn land(&self) {
use windows::Win32::System::Threading::{CreateEventW, WaitForSingleObject};
if self.fence.GetCompletedValue() < self.value {
let Ok(event) = CreateEventW(None, false, false, None) else { return };
while self.fence.GetCompletedValue() < self.value && self.fence.SetEventOnCompletion(self.value, event).is_ok() {
WaitForSingleObject(event, 10_000);
}
let _ = CloseHandle(event);
}
let mut p = std::ptr::null_mut();
if self.fence.GetCompletedValue() == u64::MAX || self.buf.Map(0, None, Some(&mut p)).is_err() {
return;
}
if let Some(s) = STATE.lock().unwrap().as_mut() {
s.copy_back(p as *const u8, &self.parts);
}
self.buf.Unmap(0, None);

Review by Claude Code (Claude Opus 5.5) with a gpt-6.2-sol audit subagent; I checked each point against the code.

After a title's ExecuteCommandLists whose lists write sparse windows on the
GPU, readback_after_execute made XCL's queue wait for the title's queue
(through one rb_fence shared by all title queues) and then waited on the CPU
for the copy, inside the title's call and under the sparse lock. A queue may
GPU-wait for a fence that a later submit signals (async compute): the CPU
wait then never ends, and the later submit never comes. Values of the shared
fence signalled from several queues could also complete out of order.

The copy back is now recorded on the title's queue after its lists (a list of
the queue's type, into a readback buffer of its own), followed by a signal of
a fence of that queue's own. It is still waited for in the call, so the bytes
are in the CPU's view when the title's own fence after the work completes -
unless a wait the queue was given has not completed (Wait is noted, from the
PC method and from the console's queue slots; XCL's own waits are not), or an
earlier copy back of that queue is not landed yet. Then a worker thread lands
it, in submission order, once the GPU is there.

The fence exists before anything is submitted, and a copy back is landed
only once its fence has reached the value (looking again every millisecond
when there is no event to wait on), so its buffer and list live until the GPU
is done with them; if the Signal after the copy fails, nothing can tell when
it is done and they are never released (review on ivall#433).

Tests (own processes, the device adopted as the title's):
- sparse_readback_waits: queue A waits for F, runs a list writing 8 bytes of a
  sparse window and is read back; queue B then submits and signals F. Main
  hangs (A not done after 5 s); here A finishes and the bytes land.
- sparse_readback_lands: 20 times a list writes 8 bytes, the title waits for
  its fence and reads them at once. Passes on main and here; fails in round 0
  when every copy back goes to the worker.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Kalmarv
Kalmarv force-pushed the fix/sparse-readback-without-cpu-wait branch from 7709697 to 7184866 Compare November 24, 2026 01:06
@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, both points were right. They're fixed in 7184866, squashed into the PR's commit:

  • readback_on: the fence is now created before anything is submitted. If the Signal after the copy fails, nothing can tell when the copy is done, so the readback buffer and command list are deliberately leaked instead of released.
  • land: it returns only once the fence has reached the value, so the buffer is mapped and copied back only after the GPU is done with it. When SetEventOnCompletion fails, it now polls every millisecond instead of going ahead. CreateEventW failing had the same problem (it returned and dropped the buffer while the copy could still be pending), and it now polls the same way.

No test covers these paths: the failures of CreateFence, Signal, CreateEventW and SetEventOnCompletion can't be forced in a synthetic test. The existing tests and the whole suite were run again on the new head (see the description).

🤖 Claude Opus 5.5

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.

4 participants