Skip to content

D3D12.X: unmap sparse tiles after the work the title's queues were given - #496

Closed
notdxbi wants to merge 1 commit into
xcl-dev:mainfrom
notdxbi:pr-unmap-after-title-work
Closed

notdxbi wants to merge 1 commit into
xcl-dev:mainfrom
notdxbi:pr-unmap-after-title-work

Conversation

@notdxbi

@notdxbi notdxbi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

When the title unmaps physical pages in a sparse window, sparse::decommitted unmapped their tiles (UpdateTileMappings to NULL) on the sparse queue at once, ahead of work still in flight on the title's queues that reads or writes those tiles. While a retail game streamed its world (Linux, RTX 5060), NVIDIA's MMU faulted in a copy engine (NVRM Xid 31 … ENGINE CE0 … FAULT_PTE, a GPU VA with nothing mapped) and the device was lost. Now each known title queue signals a fence of its own and the sparse queue waits for it before unmapping; nothing waits on the CPU. Part of #175 / #248.

What is in it

  • hle/d3d12x/sparse.rs: after_work_on(state, queues), called by decommitted with present::known_queues() before unmap_tiles; one fence per title queue (a fence shared by several queues could go backwards).
  • Test unmapping_waits_for_work_on_title_queues (GPU): with a title queue held on a gate fence, the sparse queue's next signal does not complete until the gate opens. Without the change it fails ("the unmap queue went ahead of the title's work in flight").

Tested

Regression run

  • None: scripts/smoke.ps1 needs Windows (see Not tested).

Not tested

  • Windows: no Windows machine here, so no scripts/smoke.ps1 blocks and no MSVC build; this PR needs someone to run the regression on Windows before it can be merged.
  • AMD.

AI use

  • Claude Code with Claude Opus 5.5: found the cause, wrote the change, the test and this description. The runs and numbers are real, on the hardware above; the PR is opened by me after checking them.

🤖 Generated with Claude Code

When the title unmaps physical pages in a sparse window,
sparse::decommitted unmapped their tiles (UpdateTileMappings to NULL) on
the sparse queue at once, ahead of work still in flight on the title's
queues that reads or writes those tiles. While a retail game streamed
its world under Wine on an RTX 5060, NVIDIA's MMU faulted in a copy
engine (NVRM Xid 31, a GPU VA with nothing mapped) and the device was
lost. Now each known title queue signals a fence of its own and the
sparse queue waits for it before unmapping; nothing waits on the CPU.
In the same scenario the fault did not happen again.

Test unmapping_waits_for_work_on_title_queues: with a title queue held
on a gate fence, the sparse queue's next signal does not complete until
the gate opens. It fails without the change.

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

ghost commented Nov 24, 2026

Copy link
Copy Markdown

Windows / MSVC run, since Not tested asks for one: 409f40f merged onto main 508a0c7 (clean merge) builds and passes.

Tested

  • AMD Radeon RX 9060 XT (RDNA4), driver 32.0.32029.2010; Ryzen 7 5800X3D; Windows 11 Pro 26200; Rust 1.100.0 x86_64-pc-windows-msvc; DXC 1.9.2602.17; debug build.
  • .\scripts\test.ps1: 681 passed, 0 failed, 41 ignored; no SKIPPED GPU tests.
  • cargo test -p xcl --lib unmapping_waits_for_work: unmapping_waits_for_work_on_title_queues passes on the hardware adapter.

Not tested

  • smoke.ps1 (no retail title here yet).

AI use

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

@vibryxcx

Copy link
Copy Markdown

Windows regression run with a retail title, which Not tested asks for (the comment above ran the suite only).

Tested

  • AMD Radeon RX 6800 (RDNA2, driver 32.0.21045.5002), Intel Core i5-10600KF, Windows 11 build 26200, Rust 1.100.0 MSVC, release build. cargo clean --release -p xcl before each build.
  • This PR's head 409f40f as it is, against its base, main 2997c9a. No uncommitted changes.
  • Standard run: scripts/smoke.ps1 -Game <folder> -Seconds 60, no input. Blocks below.
  • Menu run: XCL_PRESS_A_AT=14,17,21,40,60,66,72, -Seconds 180 (the presses Memory: decommitted sparse tiles stay mapped until the next submit #532 uses):
2997c9a 409f40f
frames shown in 180 s 537 538
DirectStorage requests (sum of the activity lines) 18,901 18,901
longest stretch with 0 presents after 10 s 2 s 4 s
crashes / device removals 0 / 0 0 / 0
peak commit 15,555 MB 15,525 MB
sparse pools at the end 960 MB (951 MB mapped) 960 MB (951 MB mapped)

Regression run

main 2997c9a:

xcl-smoke v1
commit: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21
gpu: AMD Radeon RX 6800
title: GTA6
seconds: 60
exit: 0x0
frames shown: 599 (PresentX 599)
presents per 2 s (last 3): 35 36 34
device removals: 0
crashes: 0
peak commit: 15171 MB

This PR 409f40f:

xcl-smoke v1
commit: 409f40f85227fc3be5cdf43da7ea006ca89913fc
gpu: AMD Radeon RX 6800
title: GTA6
seconds: 60
exit: 0x0
frames shown: 679 (PresentX 679)
presents per 2 s (last 3): 36 36 36
device removals: 0
crashes: 0
peak commit: 15148 MB

Not tested

  • The changed path itself. In neither run does the title get to its world load within 180 s (both end in the menus at 2 – 3 presents per 2 s, and the sparse pools end the same in both), and nothing in the log shows a decommit. So this is a regression check only, not a check of the fix.
  • The suite on this machine (RDNA4 is covered above); NVIDIA on Windows.
  • One run of each.

AI use

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

@xstraok

xstraok commented Nov 24, 2026

Copy link
Copy Markdown

Tested main a6f5f7a + #257 + #458 + this PR (gh pr diff 496 applied) on RX 9060 XT (RDNA4, RADV Mesa
26.2.6), Linux, Proton Experimental Wine. The retail title still lost the device 67 s into world loading,
but with an AMD SQC (scalar data) page fault at an address no BO ever covered – past the end of the shared view
heap, not a sparse tile (RADV_DEBUG=bo_history; details in <issue from draft 1>). So on AMD the device loss
comes from a different cause, and these runs neither confirm nor rule out this PR's fix. No regression seen from
it (2 runs to 129 s and 145 s).
AI-assisted report (Claude Code, Claude Opus 5.5); runs made by me on the hardware above.

@notdxbi

notdxbi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @nullmarsh and @vibryxcx for the runs.

A note for review: #532 changes the same function (sparse::decommitted). It parks a fully decommitted tile until the next submit instead of unmapping it at once, which is about the driver cost of the calls. This PR is about ordering: the NULL mapping goes on the sparse queue only after the work already given to the title's queues (each queue signals a fence of its own; the sparse queue waits on the GPU). They should compose. When a parked tile is finally unmapped, it still needs to be ordered after work in flight that may use it. If #532 goes in first, I'll rebase this onto it and put the wait where the parked tiles are actually unmapped.

@Kalmarv

Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Tested 409f40f (this PR's head) and its base 2997c9a with one added integration test, on an RTX 5070 Ti (NVIDIA 610.74), Ryzen 9 9950X, Windows 11 Pro 25H2 (26200.9711), rustc 1.99.0, debug.

What the test does. A title's queue may GPU-wait for a fence that another of its queues signals only in a later submit; async compute does this.

  • Queues A (direct) and B (compute) have each submitted once through sparse::before_execute and present::note_execute, as the ExecuteCommandLists hook does, so both are known queues.
  • A then waits for fence F, and signals done after it.
  • The title frees a sparse block (sparse::free → decommitted).
  • B submits through before_execute and only then signals F.
  • Each submit has its own empty list, so that no list is submitted again before its first run has finished (with one list submitted twice, the D3D12 debug layer reports message 553 and drops the second submit).
  • If done hasn't completed after 5 s, the test signals F from the CPU so that everything can finish, then fails.

Results (sparse_decommit_waits, 3 runs each, one at a time):

  • base 2997c9a: passes 3 of 3;
  • 409f40f: fails 3 of 3 with "queue A did not finish in 5 s".

Why, by reading 409f40f. This part is not measured beyond the result above.

  • decommitted calls after_work_on, so the sparse queue waits for a fence that every known queue signals, A included. A has that signal queued behind its wait for F.
  • s.signal() follows the unmap.
  • B's before_execute then waits for s.fence at that value, so B's Signal(F) never runs, and A never gets past its wait.
  • On a title, that would be every title queue stopped behind the sparse queue while the title streams.
  • The PR's own test unmapping_waits_for_work_on_title_queues holds the title queue on a gate the CPU opens, so it doesn't reach this case.

Not tested: AMD, Linux/vkd3d-proton (your setup), and any retail title. I haven't tried a fix. The test takes about 5 s even when it passes: its watchdog sleeps the full 5 s before checking.

The test, as a patch on 409f40f (git apply; built and run there and on 2997c9a):

crates/xcl/tests/sparse_decommit_waits.rs
diff --git a/crates/xcl/tests/sparse_decommit_waits.rs b/crates/xcl/tests/sparse_decommit_waits.rs
new file mode 100644
index 0000000..d695c98
--- /dev/null
+++ b/crates/xcl/tests/sparse_decommit_waits.rs
@@ -0,0 +1,91 @@
+//! A title's queue may GPU-wait for a fence another of its queues signals only in a later
+//! submit (async compute does). Unmapping the tiles of a block the title gives back
+//! (`sparse::decommitted`, here through `sparse::free`) must not make that wait for itself.
+
+use std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::Arc;
+use std::time::Duration;
+
+use windows::core::Interface;
+use windows::Win32::Foundation::{CloseHandle, WAIT_OBJECT_0};
+use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0;
+use windows::Win32::Graphics::Direct3D12::*;
+use windows::Win32::System::Threading::{CreateEventW, WaitForSingleObject};
+
+use xcl::hle::d3d12x::{present, sparse};
+
+/// Queue A waits for fence F; the title frees a sparse block; queue B submits and only then
+/// signals F. Both queues finish.
+#[test]
+fn a_decommit_while_a_queue_waits_for_a_later_signal_does_not_hang() {
+    unsafe {
+        std::env::set_var("XCL_UMA_MB", "64");
+        std::env::set_var("XCL_COHERENT_MB", "64");
+        let Ok(dev) = xcl::gpu::agility::create_device(None, D3D_FEATURE_LEVEL_11_0) else {
+            xcl::gpu::recomp::runner::skip_test("no GPU");
+            return;
+        };
+        let windows = sparse::init(&dev);
+        let Some(&(k, _, _)) = windows.first() else {
+            xcl::gpu::recomp::runner::skip_test("no sparse windows");
+            return;
+        };
+        let (block, _) = sparse::alloc_in(k, 0x1_0000, 0x1_0000).expect("sparse allocation");
+        let qa: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC::default()).unwrap();
+        let qb: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_COMPUTE, ..Default::default() }).unwrap();
+        let empty = |ty| {
+            let al: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap();
+            let l: ID3D12GraphicsCommandList = dev.CreateCommandList(0, ty, &al, None).unwrap();
+            l.Close().unwrap();
+            (al, l.cast::<ID3D12CommandList>().unwrap())
+        };
+        let (_aa, la) = empty(D3D12_COMMAND_LIST_TYPE_DIRECT);
+        let (_ab, lb) = empty(D3D12_COMMAND_LIST_TYPE_COMPUTE);
+        // a list is not submitted again before its first run has finished
+        let (_ab2, lb2) = empty(D3D12_COMMAND_LIST_TYPE_COMPUTE);
+        let f: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
+        let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
+        // a submit as the title's ExecuteCommandLists hook makes it
+        let submit = |q: &ID3D12CommandQueue, l: &ID3D12CommandList| {
+            sparse::before_execute(q, &[], 0x1000);
+            present::note_execute(q.as_raw(), &[l.as_raw()]);
+            q.ExecuteCommandLists(&[Some(l.clone())]);
+        };
+
+        // both queues have submitted before; then A waits for what B signals later
+        submit(&qa, &la);
+        submit(&qb, &lb);
+        qa.Wait(&f, 1).unwrap();
+        qa.Signal(&done, 1).unwrap();
+
+        // if nothing finishes in 5 s, F is signalled from the CPU so that the queues (and a
+        // CPU wait inside XCL, if any) can go on and the process can end
+        let fired = Arc::new(AtomicBool::new(false));
+        let watchdog = {
+            let fired = fired.clone();
+            let (f, done) = (f.as_raw() as usize, done.as_raw() as usize);
+            std::thread::spawn(move || {
+                std::thread::sleep(Duration::from_secs(5));
+                let (done, f) = (done as *mut std::ffi::c_void, f as *mut std::ffi::c_void);
+                let done = ID3D12Fence::from_raw_borrowed(&done).unwrap();
+                if done.GetCompletedValue() < 1 {
+                    fired.store(true, Ordering::SeqCst);
+                    let f = ID3D12Fence::from_raw_borrowed(&f).unwrap();
+                    f.Signal(1).unwrap();
+                }
+            })
+        };
+
+        // the title gives a block back; then B submits and signals F
+        assert!(sparse::free(block), "the block is a sparse allocation");
+        submit(&qb, &lb2);
+        qb.Signal(&f, 1).unwrap();
+
+        let ev = CreateEventW(None, false, false, None).unwrap();
+        done.SetEventOnCompletion(1, ev).unwrap();
+        let _ = WaitForSingleObject(ev, 60_000) == WAIT_OBJECT_0;
+        let _ = CloseHandle(ev);
+        watchdog.join().unwrap();
+        assert!(!fired.load(Ordering::SeqCst), "queue A did not finish in 5 s: the unmap waits for A, which waits for B, which waits for the unmap");
+    }
+}

(Test and report by Claude Code, Claude Opus 5.5; the runs are ours, on the hardware above.)

Title smoke run

  • Windows 11 Pro 25H2 (build 26200), AMD Ryzen 9 9950X, 32 GB RAM, NVIDIA GeForce RTX 5070 Ti (driver 610.74), Rust 1.99.0, release builds (cargo build --release -p xcl) from clean checkouts
  • main: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21 (this PR's base); PR head: 409f40f85227fc3be5cdf43da7ea006ca89913fc, no uncommitted changes
  • scripts/smoke.ps1 -Game <title folder> -Seconds 60 on a retail title, main first, after one 60 s warm-up run of main; both sides start from the same learned coherent-window size
  • PR 1423 frames vs main 1510 (94 %), peak commit 14532 vs 14549 MB (100 %); 0 crashes and 0 device removals on both sides, both exited 0x0; main 2997c9a's four runs in this batch (warm-up included) showed 1446-1527 frames

main:

xcl-smoke v1
commit: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21
gpu: NVIDIA GeForce RTX 5070 Ti
title: GTA VI
seconds: 60
exit: 0x0
frames shown: 1510 (PresentX 1510)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14549 MB

PR:

xcl-smoke v1
commit: 409f40f85227fc3be5cdf43da7ea006ca89913fc
gpu: NVIDIA GeForce RTX 5070 Ti
title: GTA VI
seconds: 60
exit: 0x0
frames shown: 1423 (PresentX 1423)
presents per 2 s (last 3): 59 61 60
device removals: 0
crashes: 0
peak commit: 14532 MB

Not tested: whether the title unmapped sparse tiles in these 60 s (the log has no count of unmaps), so the run does not show the fault this PR fixes; it ran on native Windows on an RTX 5070 Ti, not under Wine on an RTX 5060 as in the PR; AMD GPUs.

🤖 Claude Opus 5.5

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

@Kalmarv's sparse_decommit_waits test reproduces on AMD. My earlier pass here was the whole suite on the head merged onto main; the suite has no case like this one.

Tested

  • AMD Radeon RX 9060 XT (RDNA4; DXGI adapter 0, which the test's create_device(None, …) uses), driver 32.0.32029.2010; Ryzen 7 5800X3D; Windows 11 Pro 26200; Rust 1.100.0 MSVC; debug.
  • The test from the comment above applied with git apply to the PR head 409f40f as it is and to its parent 2997c9a, not merged onto main; cargo test -p xcl --test sparse_decommit_waits -- --test-threads=1, 3 runs each:
    • 409f40f: fails 3 of 3, "queue A did not finish in 5 s: the unmap waits for A, which waits for B, which waits for the unmap";
    • 2997c9a: passes 3 of 3.

Not tested

  • A fix; a retail title; Linux/vkd3d-proton; the head merged onto current main (it conflicts).

AI use

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

@raylitebull

Copy link
Copy Markdown
Member

Closing as superseded by #532, which has already been merged into main.

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