Skip to content

D3D12.X: wait for title queues before unmapping texture pages - #594

Open
Abdellah-Khanani wants to merge 1 commit into
xcl-dev:mainfrom
Abdellah-Khanani:codex/vram-unmap-after-use
Open

Abdellah-Khanani wants to merge 1 commit into
xcl-dev:mainfrom
Abdellah-Khanani:codex/vram-unmap-after-use

Conversation

@Abdellah-Khanani

@Abdellah-Khanani Abdellah-Khanani commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes #475. Order unmapping of a superseded title texture after work already submitted on the title's command queues, so its pages are not removed while GPU work may still sample it.

What is in it

  • crates/xcl/src/hle/d3d12x/vram.rs: signal reusable per-queue fences and make the mapping queue wait on them before unmapping. If a fence cannot be established, keep the resource mapped.
  • Add a GPU regression test that holds a submitted resource barrier behind a fence and verifies the unmap cannot complete early.

Tested

  • AMD Radeon RX 5700 XT, driver 32.0.21045.29003; Windows 11 build 22631; Rust 1.100.0 MSVC; debug.
  • cargo test -p xcl --lib superseded_texture_unmap_waits_for_title_queue_work -- --nocapture: 1 passed.
  • The regression test fails when the queue wait is removed.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 640 passed, 1 failed, 21 ignored. The failure is gpu_nonfused_mad_and_mac_prevent_fma_contraction (Recompiler: v_fma_f32 is not fused on AMD's RDNA1 driver (mad() left unfused) #472), also reproduced on main.
  • SKIPPED: gpu_geometry_mode_es_gs_strip and gpu_mesh_mode_two_passes (no mesh shaders).

Regression run

Main:

xcl-smoke v1
commit: 508a0c74d7c0625d6d2de8d24494e69dc2e1926f
gpu: AMD Radeon RX 5700 XT
title: d06b10c6
seconds: 120
exit: 0x0
frames shown: 703 (PresentX 704)
presents per 2 s (last 3): 5 4 5
device removals: 0
crashes: 0
peak commit: 16372 MB

This PR:

xcl-smoke v1
commit: 3bb3c2fea4ee6a2e74b20e308c328a108ff548ac
gpu: AMD Radeon RX 5700 XT
title: d06b10c6
seconds: 120
exit: 0x0
frames shown: 1053 (PresentX 1053)
presents per 2 s (last 3): 19 20 19
device removals: 0
crashes: 0
peak commit: 15823 MB

Not tested

  • Other GPU vendors.

Reviewed by

  • Abdellah-Khanani reviewed the change.

AI use

  • OpenAI Codex (GPT-6) wrote the implementation, regression test and description. Abdellah-Khanani reviewed the change.

A mapping queue unmap could overtake draws already submitted on the title queues. Give each title queue a reusable fence and order the unmap after a marker on every known queue; keep the mapping when a marker cannot be established. Add an RX 5700 XT regression test that holds a submitted resource barrier behind a fence.

Co-Authored-By: GPT-6 <noreply@openai.com>
@CryingLaughing

Copy link
Copy Markdown

Tested

  • Clean head 3bb3c2fea4ee6a2e74b20e308c328a108ff548ac on an RTX 4080 with NVIDIA driver 621.01, Core i5-13600K, Windows 11 25H2 build 26200.9974, Rust 1.100.0 MSVC x64, DXC 1.8.2502.11, debug. The reserved-texture fixture enumerates DXGI adapter 0 directly. The scratch run below confirmed that slot was the RTX 4080. XCL_ADAPTER=4080, XCL_REQUIRE_GPU=1, all XCL_SAMPLE_* variables unset.
  • cargo test -p xcl --lib --locked -j 4 hle::d3d12x::vram::tests::superseded_texture_unmap_waits_for_title_queue_work -- --show-output --test-threads=1 passed 1/1. The unmap fence for the synthetic reserved texture stayed pending while the title queue was gated and completed after the gate opened.
  • In a scratch checkout of the same head with self.queue.Wait(&fence, value) removed, the test failed 0/1 as expected. The mapping fence already reached the unmap value 2 while the title queue was still gated. The scratch fixture recorded that value, opened the gate and drained the title queue before asserting the ordering failure.
  • cargo build --workspace --locked -j 4 passed. cargo test --workspace --locked -j 4 -- --test-threads=4 --show-output reported 681 passed, 0 failed, 41 ignored. gpu_nonfused_mad_and_mac_prevent_fma_contraction passed on this setup. The count includes opt-in game-data tests that returned early and four explicit capability skips. gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware require native wave64. test_title_draws_with_a_root_signature_under_the_debug_layer and uploads_start_from_each_subresources_state require the D3D12 debug layer.

Not tested

  • Retail titles, release builds, or other adapters.

AI use

  • OpenAI Codex (GPT-6 family, exact variant unavailable) prepared the scratch control, ran the tests and drafted this report.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…-watch merge

Integrates upstream PRs ivall#594, ivall#640, and ivall#551:
- Order reserved-texture tile unmaps behind known title queues with GPU
  fences before releasing tiles back to pools (ivall#594).
- Add opt-in XCL_PAGE_BUDGET_MB to evict LRU CPU-filled title textures
  when texture page pools hit the cap, remapping and re-uploading on use
  (ivall#640), preventing unbounded VRAM growth during world streaming.
- Merge write-watch scan gaps up to 64 MiB and clip regions with binary
  search to cut GetWriteWatch overhead during streaming (ivall#551).
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…-watch merge

Integrates upstream PRs xcl-dev#594, xcl-dev#640, and xcl-dev#551:
- Order reserved-texture tile unmaps behind known title queues with GPU
  fences before releasing tiles back to pools (xcl-dev#594).
- Add opt-in XCL_PAGE_BUDGET_MB to evict LRU CPU-filled title textures
  when texture page pools hit the cap, remapping and re-uploading on use
  (xcl-dev#640), preventing unbounded VRAM growth during world streaming.
- Merge write-watch scan gaps up to 64 MiB and clip regions with binary
  search to cut GetWriteWatch overhead during streaming (xcl-dev#551).
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

A possible GPU deadlock in 3bb3c2f, found by reading the code (not run):

unmap now makes the mapping queue wait for a marker signalled on every title queue (vram.rs:322-346), and then raises pages.value. before_execute (:600-612) makes the next work on every title queue wait for pages.value. Together they form a cycle when a title queue is held by a signal that a later submission on another queue provides, which is the pattern #433 deals with:

  1. The title submits Q1.Wait(F, n), where F = n comes from work it will submit on Q2 later.
  2. A texture is superseded, and unmap queues Q1.Signal(M1, k) behind that wait, mapQ.Wait(M1, k), the NULL mapping, and mapQ.Signal(pages.fence, v + 1).
  3. The title submits work plus Signal(F, n) on Q2. before_execute(Q2) first inserts Q2.Wait(pages.fence, v + 1).

Now Q2 waits for the mapping queue, the mapping queue waits for Q1's marker, and Q1 waits for F, which only Q2 can signal. That hangs both queues (TDR / device removed). Before this PR the mapping queue never waited on title queues, so there was no cycle.

One way out: don't wait on the GPU. Record each title queue's marker value at supersede time, then do the NULL mapping later from the CPU, once every marker's GetCompletedValue has passed (polled from the next before_execute, or by the existing worker). The pages stay mapped slightly longer, but no queue ever waits for an unmap. Separately, a title queue doesn't need to wait for an unmap at all, only for maps, so unmaps could stop bumping the value before_execute waits on. That alone doesn't remove the cycle if a later map is queued behind the unmap.

The liveness of known_queues() is fine: QUEUES holds a reference, as for drain_gpu.

AI-assisted (Claude Code), read-only review.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…-watch merge

Integrates upstream PRs xcl-dev#594, xcl-dev#640, and xcl-dev#551:
- Order reserved-texture tile unmaps behind known title queues with GPU
  fences before releasing tiles back to pools (xcl-dev#594).
- Add opt-in XCL_PAGE_BUDGET_MB to evict LRU CPU-filled title textures
  when texture page pools hit the cap, remapping and re-uploading on use
  (xcl-dev#640), preventing unbounded VRAM growth during world streaming.
- Merge write-watch scan gaps up to 64 MiB and clip regions with binary
  search to cut GetWriteWatch overhead during streaming (xcl-dev#551).
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…-watch merge

Integrates upstream PRs ivall#594, ivall#640, and ivall#551:
- Order reserved-texture tile unmaps behind known title queues with GPU
  fences before releasing tiles back to pools (ivall#594).
- Add opt-in XCL_PAGE_BUDGET_MB to evict LRU CPU-filled title textures
  when texture page pools hit the cap, remapping and re-uploading on use
  (ivall#640), preventing unbounded VRAM growth during world streaming.
- Merge write-watch scan gaps up to 64 MiB and clip regions with binary
  search to cut GetWriteWatch overhead during streaming (ivall#551).
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…-watch merge

Integrates upstream PRs ivall#594, ivall#640, and ivall#551:
- Order reserved-texture tile unmaps behind known title queues with GPU
  fences before releasing tiles back to pools (ivall#594).
- Add opt-in XCL_PAGE_BUDGET_MB to evict LRU CPU-filled title textures
  when texture page pools hit the cap, remapping and re-uploading on use
  (ivall#640), preventing unbounded VRAM growth during world streaming.
- Merge write-watch scan gaps up to 64 MiB and clip regions with binary
  search to cut GetWriteWatch overhead during streaming (ivall#551).
@Abdellah-Khanani
Abdellah-Khanani marked this pull request as ready for review November 24, 2026 09:43
@Abdellah-Khanani

Copy link
Copy Markdown
Author

Run data that fits the cycle @upipibi describes. I don't have a trace that proves this cycle is the cause.

Tested

  • AMD Radeon RX 5700 XT (RDNA1), driver 32.0.21045.29003; Ryzen 5 2600; Windows 11 Pro 22631; Rust 1.98.1; debug; cargo test --workspace --no-fail-fast -- --test-threads=4.
  • 3bb3c2f rebased onto main dd2ae9a, with only the new test changed so that a failure can't abort the binary:
    • 3 full-suite runs; 2 of them hung the GPU;
    • in one run, UMA import check: ... GPU fence still pending after 2000 ms came first, then D3D12 device removed: 0x887a0007;
    • in both hung runs, everything after the reset failed with 0x887A0005 (82 and 80 FAILED lines).
  • main dd2ae9a without the PR, the same command right after: 649 passed, 1 failed, 21 ignored in the lib (the failure is Recompiler: v_fma_f32 is not fused on AMD's RDNA1 driver (mad() left unfused) #472, which also fails on main on RDNA1), and no hang.
  • Earlier, 3bb3c2f on its own base 508a0c7 passed the whole suite without a hang. So the hang either needs the newer main, or it is timing-dependent and those runs were lucky.

Not tested

  • Any title.
  • Whether the CPU-side unmap @upipibi suggests removes the hang. I can run it on this GPU once there is a commit.

The new test itself does what it should here: it passes with the wait, and it fails without it.

Claude Code (Claude Opus 5.5) ran the tests and wrote this comment; I checked it.

@Ali-AbdulHadii

Copy link
Copy Markdown

Run data and thread dumps confirming the cycle @upipibi described, plus a variant that avoids it.

Setup: Linux, RTX 5080 (NVIDIA 620.73.04), proton-cachyos-slr Wine, vkd3d-proton master 30b0eb9 + the repo patch. XCL: a local branch of main with this PR merged (plus #640 and others), retail title 1.0.2.1 in its world.

What happened with this PR merged: in the world, one frame every ~6 s. The fence bridge's 5 s watchdog released a Wait64BitValueX GPU wait every ~6 s, and every present came ~0.3 s after a release (29–36 releases per run).

Thread dumps (XCL_THREAD_DUMP_AT=160,190), the same in both: the title's submitting thread is in real::q_execute_command_lists → title_textures::refresh → texture::upload_batch → gpu::fence::wait. So the cycle is:

  1. a title compute queue GPU-waits (Wait64BitValueX) for a counter that a list on the title's graphics queue writes;
  2. this PR's unmap makes the mapping queue wait for a marker on every title queue, so it also waits for that compute queue;
  3. vram::before_execute makes the upload queue wait for the mapping queue's fence;
  4. the submitting thread waits on the CPU for the upload batch, so the graphics list that writes the counter is never submitted.

A variant without the cycle: keep the markers, but make the CPU hold the unmap instead of the mapping queue. unmap signals the markers on the title queues, records (resource, total, markers) in a list holding a reference to the resource, and returns. The held unmaps whose markers have all completed are made at the next before_execute, and mapping the resource again drops its held unmap. XCL's queues then never wait for a title queue. Your GPU test still holds: the unmap is not made before the gated queue passes its marker. I added a check that the mapping queue stays free meanwhile.

After: 0 watchdog releases at the same point of the next run, and frames no longer stall in step with them.

Not tested: Windows, AMD.

AI use: Claude Code (Claude Opus 5.5) diagnosed this, wrote the local change and this text; the user approved posting it.

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.

D3D12.X: a title texture's pages can be unmapped while submitted draws still sample it (#288)

4 participants