Skip to content

D3D12.X: a sparse readback its wait gave up on is not copied into title memory - #3063

Closed
KOLESNiii wants to merge 3 commits into
xcl-dev:mainfrom
KOLESNiii:port/c16-readback-stale
Closed

KOLESNiii wants to merge 3 commits into
xcl-dev:mainfrom
KOLESNiii:port/c16-readback-stale

Conversation

@KOLESNiii

@KOLESNiii KOLESNiii commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #3062; only the top commit (89bcf0d8) belongs here.

readback_ranges submits the copies from the sparse windows into the readback buffer, waits for them (at most 5 s), then copies the buffer into the CPU view and records the sent hashes. When the wait gave up, the GPU copies have not run, and the buffer still holds an earlier readback, of other addresses. Those bytes were written into the title's memory as if they were its own, and hashed as sent. Retail runs log this wait timing out (#675, #505, #846, #646). Part of #3011 (C16).

Now that batch's copy and hashes are left out, so the CPU view keeps its own bytes. A rate-limited warning counts these readbacks.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs:
    • flush_and_copy in readback_ranges leaves the copy out when wait_for says the fence was not reached.
    • Test a_readback_the_wait_gave_up_on_leaves_the_cpu_view_as_it_was. It fills the readback buffer with an earlier readback's bytes, holds the queue on an unsignalled fence, reads back with a 20 ms bound, and checks that the CPU view is unchanged. Once the queue is released, the same readback must bring the bytes the GPU holds (0x3C, uploaded first) into the CPU view. That retry is the check pickles976 asked for; it failed before the stale-wake fix in D3D12.X: a sparse list still in flight after its bounded wait is held, not reset #3062.

Tested

  • NVIDIA GeForce RTX 2070 SUPER 8 GB, driver 621.40; AMD Ryzen 5 3600XT; 32 GB; Windows 11 Home 26300; rustc 1.100.0 MSVC; tests in debug, smoke in release.
  • cargo build --workspace then cargo test --workspace --no-fail-fast -- --test-threads=4 at 89bcf0d8: 1468 passed, 0 failed, 47 ignored; 25 SKIPPED (no wave64 on this adapter, and one "no heap windows").
  • cargo test -p xcl --lib sparse::: 20 passed, 0 failed, 1 ignored (early_commit_timing, ignored on main too). The new test alone: passed 3 of 3.
  • Without the fixes, the new test fails. Without the skipped copy: "bytes of an earlier readback were copied into the CPU view". With D3D12.X: a sparse list still in flight after its bounded wait is held, not reset #3062's old single wait: "a readback after the queue was released did not copy the GPU's bytes", 3 of 3.

Regression run
Both 60 s, XCL_PREPARE=0, XCL_PRESS_AT=25:A, a new empty XCL_DXIL_CACHE for each run, the same retail game.

main:

xcl-smoke v1
commit: 6089122a1c96dff16e93b6fdf04c8ed20cfb6102
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1287 (PresentX 1287)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16057 MB

This PR:

xcl-smoke v1
commit: 89bcf0d8dea0ed999033e8a7948d7cb32e8e0580
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1497 (PresentX 1497)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15865 MB

Not tested

  • The timeout on a real title (see D3D12.X: a sparse list still in flight after its bounded wait is held, not reset #3062): this machine does not reach the world on main.
  • What the title does with the bytes it keeps instead. They are the CPU view's own, which is what it had before the readback. A later readback of the same range brings the GPU's bytes (tested), but only if the caller asks again: a timeout still returns () through readback_ranges / readback_now / readback_after_execute, so callers get no failure receipt.
  • AMD, Intel, and wave64 GPUs.

AI use

🤖 Generated with Claude Code

…, not reset

The sparse queue's wait (wait_for) gives up after 5 s and goes on.
begin_slot and the readback path then Reset the allocator and list of
the submission the wait gave up on, while the GPU may still execute it,
which D3D12 does not allow. Retail runs on other machines log the
timeout ("sparse: the queue has not reached fence value ... after 5 s")
five or more times a run (xcl-dev#675, xcl-dev#505, xcl-dev#846, xcl-dev#646).

wait_for now says whether the fence was reached. When it was not, the
allocator and list go to held_lists with the fence value of their last
submission, a new pair takes their place, and reclaim_ring drops held
pairs once the queue has passed that value. If no new pair can be made,
the wait goes on without a bound instead of resetting.

The mechanism follows Vice-Tay b06fdd5 (GPL-3.0-or-later): a submission
whose bounded wait timed out stays pending until its fence has passed.
No code is taken from it (THIRD_PARTY.md). C16 of xcl-dev#3011.

This makes the lists' lifetime safe, not the readback bytes: after a
timed-out wait, flush_and_copy still copies the readback buffer into
title memory (a separate change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pickles976

Copy link
Copy Markdown

Verified stacked production head 1409b38 (#3062 parent 5a9c5ed) on Wine 11 / Linux 6.17 / real NVIDIA RTX 4060 Laptop, driver 590.48.1. Test-only strengthening selects NVIDIA explicitly and uses panic-safe CPU fence release before State destruction. Diagnostic-relaxed vkd3d-proton 2576c0b4adfae4a+, not stock certification.

The allocator-retention test passes, but extending readback to a successful retry exposes the shared-event wait bug.

The original timeout checks pass: real GPU bytes 0x3C are seeded, CPU is changed to 0xAA, stale readback buffer is poisoned 0xBB, and a CPU-held queue gate forces the 20ms timeout. CPU bytes and sent hashes stay unchanged. After releasing the gate and waiting for actual completion, retrying the same 4KiB range fails to publish the GPU bytes. Log says fence4 timed out “after 5 s (at 3)”, yet the complete two-test run took only1.32s. Fence4 had completed by the later assertion, but the CPU copy had already been skipped. This run:1pass/1fail, fresh26.13s compilation.

A separate, GPU-work-free fence fixture isolates the event behavior:

  1. fence.SetEventOnCompletion(1, state.event) then CPU fence.Signal(1); leave the old event wake pending.
  2. Another CPU thread signals2 after20ms.
  3. Call production state.wait_for(2) with200ms test budget.

Observed: reached=false, observed=1, final=2, elapsed=33.8us, budget=200ms;0/1fail. The old event wake is consumed by the new wait. WaitForSingleObject's result is ignored, and the helper makes only one completion-value check. Logging the configured budget as elapsed time masks the premature return. This isolates the behavior in this head; not a matched-parent regression claim or a GPU hang.

Causal control: a temporary local helper-only probe rechecked the requested fence after every wake and waited against the same absolute deadline (including stale wakes), without resetting the event, forcing completion, raising the production5s budget, or changing the readback algorithm. All3 fixtures then passed, including the actual GPU retry and allocator retirement. Isolated wait: reached=true, observed=2, final=2, elapsed=20.3044ms. Fresh26.12s build,1.84s tests. The temporary production probe was restored; only test changes remain. This is diagnostic evidence, not an owner-head pass or runtime promotion.

Local evidence directories:

  • verify-xcl3063-timeout-retention-retry-20261127T185842Z/
  • verify-xcl3063-stale-event-wait-20261127T190146Z/
  • verify-xcl3063-deadline-wait-probe-20261127T190403Z/

Suggested owner correction: deadline-based fence rechecking, explicit event/failed-wait/device-removal handling, and keep the successful retry assertion alongside the timeout assertion. A simple ResetEvent before waiting does not establish protection against a late old registration. True timeout still returns () through readback_ranges/readback_now/readback_after_execute; callers have no pending/failure receipt, so this alone does not prove CPU consumers cannot continue with stale data. State teardown/pending-buffer lifetime and allocation-failure fallback remain untested. No title launch, shader dispatch, memory-saving claim, or attribution to the separate Game-Init cpu_writes fence0/wanted1 watchdog. All external resource guards stayed unchanged.

The sparse queue's fence waits share one auto-reset event. When a wait
gives up and its value is reached later, the event stays signalled, and
the next wait woke at once, checked the fence once and reported that the
queue had not got there, without waiting. With xcl-dev#3062 and xcl-dev#3063 that
answer holds a list back or skips a readback copy that would have been
fine 20 ms later; before them the caller went on early.

wait_for now checks the fence after every wake and waits again against
the same deadline until its value is reached or the bound is spent. The
timeout warning gives the time actually waited, not the bound.

Found by pickles976 in a review of xcl-dev#3063 (stale wake consumed by the
next wait). The new test leaves such a wake on the event and fails
without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…le memory

readback_ranges submits the copies from the sparse windows into the
readback buffer, waits for them (at most 5 s), then copies the buffer
into the CPU view and records the sent hashes. When the wait gave up,
the GPU copies have not run: the buffer still holds an earlier
readback, of other addresses, and those bytes were written into the
title's memory as if they were its own, and hashed as sent.

Now the copy and the hashes are left out for that batch: the CPU view
keeps its bytes. A rate-limited warning counts these readbacks.

Stacked on the change that holds lists still in flight (C16 of xcl-dev#3011).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KOLESNiii
KOLESNiii force-pushed the port/c16-readback-stale branch from 1409b38 to 89bcf0d Compare November 28, 2026 01:10
@KOLESNiii

Copy link
Copy Markdown
Contributor Author

@pickles976 thanks, confirmed against the source: wait_for (sparse.rs) made one WaitForSingleObject on the shared auto-reset event and one GetCompletedValue check, and ignored the wait's result. So a wake left behind by a wait that had given up ended the next wait at once. The bug is older than these PRs (main has it too, where the caller just went on early), but here a false holds a list back or skips the copy, so it matters more.

Fixed in a new commit on #3062, e9e84e87 ("D3D12.X: a sparse fence wait rechecks the fence after every wake"). #3063 is rebased onto it (now 89bcf0d8).

  • wait_for checks the fence after every wake and waits again against the same deadline, until its value is reached or the bound is spent. A failed or timed-out wait ends it. The event is not reset (as you said, that does not protect against a late old registration). The warning now gives the time actually waited, not the bound.
  • New test a_stale_wake_does_not_end_the_next_wait: it registers a value and reaches it from the CPU, leaving the wake on the event, then waits for the next value, which another thread signals 20 ms later. The wait must return true after at least 10 ms.
  • a_readback_the_wait_gave_up_on_leaves_the_cpu_view_as_it_was now seeds the GPU page with 0x3C. After the queue is released, the same readback must bring those bytes in (your retry assertion).

Tested (RTX 2070 SUPER, driver 621.40, Windows 11 26300, debug tests): full suite --no-fail-fast --test-threads=4: 1467 passed / 0 failed / 47 ignored / 25 SKIPPED at e9e84e87, and 1468 / 0 / 47 / 25 at 89bcf0d8. The stale-wake test passed 3 of 3 on both heads, and the readback test 3 of 3 on 89bcf0d8. With the old single wait put back, both failed 3 of 3: "the wait ended on a stale wake after 111–192 µs", and "a readback after the queue was released did not copy the GPU's bytes" (your failure). Smoke, 60 s on a retail game: 1490 frames at e9e84e87 and 1497 at 89bcf0d8, 0 removals, 0 crashes (both are in the PR descriptions).

Not changed, as you noted: a real timeout still returns () through readback_ranges / readback_now / readback_after_execute, so callers get no failure receipt. Teardown of a pending buffer and the allocation-failure fallback remain untested. Both are outside this PR.

AI use: Claude Code (Claude Opus 5.5) checked the finding, wrote the fix, the tests and this reply at the account owner's request.

@andrewDoing

Copy link
Copy Markdown
Contributor

I checked the device-removed fence sentinel on head 89bcf0d8 with a deterministic CPU-only harness using the exact State::wait_for method and fake fence/event responses. ID3D12Fence::GetCompletedValue returns UINT64_MAX on device removal (Microsoft documentation).

For a nonzero target, injecting that value at entry or after a wake returned true in both cases. Controls behaved as expected: pending timeout returned false; exact, past, and pending-then-completed values returned true.

The comparison treats the removal sentinel as completion. In readback_ranges, a true result permits copying staging contents and updating sent hashes even though this return value does not establish GPU completion. This harness tested decision logic only; it did not induce a device removal or observe corrupted pixels.

A simple global change to return false on the sentinel needs care: the allocation-failure path retries wait_for in a loop. A distinct terminal removal result or a readback-specific guard could avoid confusing removal with an ordinary timeout.

AI use: OpenAI Codex with GPT-6.2 Astra built and ran the harness; Codex reviewed and prepared this report.

@KOLESNiii

Copy link
Copy Markdown
Contributor Author

@andrewDoing thanks. Confirmed by reading 89bcf0d8: wait_for (sparse.rs:847) treats GetCompletedValue() >= target as reached, so after a device removal (UINT64_MAX) it returns true, and readback_ranges (sparse.rs:1111) copies the readback buffer into the title's memory.

I'm leaving it as it is in this PR:

  • It is not new. On main, wait_for returns nothing, and readback_ranges copies after every wait, whether the queue got there, the wait timed out or the device is gone. This PR stops the copy only after a timeout; removal behaves as on main.
  • After a removal no later GPU work of the title runs, and the device-removed path takes over. A readback of stale bytes at that point does not change the outcome.
  • As you say, wait_for returning true on the sentinel is also what lets while !self.wait_for(fv) {} (sparse.rs:898, when no new command list can be made) end on a removed device. Returning false there would turn it into a hang.

If a removal needs its own handling here, I'd add it as a separate change: a check of the sentinel in readback_ranges before the copy, rather than a change to wait_for. A device removal during a readback was not induced on hardware for this PR.

AI use: Claude Code (Claude Opus 5.5) checked the finding against the code and wrote this reply.

@andrewDoing

Copy link
Copy Markdown
Contributor

I tested the suggested readback-local approach on top of 89bcf0d8, reusing the existing gpu::fence::completed_value removal check before staging bytes and sent hashes are published. The shared wait_for behavior remains unchanged, so the allocator retry path still exits on removal.

In a test-only extension of the existing native sparse readback fixture, the real queue and device remained healthy. After a real completed GPU copy, an auxiliary D3D12 fence initialized to UINT64_MAX was substituted only for the final readback observation. With the guard, CPU bytes and sent hashes stayed unchanged; a subsequent normal observation copied the expected bytes. Removing only the guard failed the same fixture at the stale-publication assertion. Sparse and shared-fence tests on the candidate: 25 passed, 0 failed, 1 ignored timing benchmark, no explicit skips.

This is a local candidate, not an actual device-removal test or a complete lifetime fix. A removal occurring after the final observation remains outside the check, and no retail behavior or performance was measured.

AI use: OpenAI Codex with GPT-6.2 Astra built and ran the test; Codex reviewed and prepared this report.

@KOLESNiii KOLESNiii closed this Nov 28, 2026
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.

3 participants