Skip to content

D3D12.X: run sparse::readback_after_execute asynchronously per queue - #1732

Open
gustavsi wants to merge 4 commits into
xcl-dev:mainfrom
gustavsi:wine/async-sparse-readback
Open

gustavsi wants to merge 4 commits into
xcl-dev:mainfrom
gustavsi:wine/async-sparse-readback

Conversation

@gustavsi

@gustavsi gustavsi commented Nov 24, 2026 •

Copy link
Copy Markdown

Move sparse::readback_after_execute off sparse::State::queue and out from under STATE.lock() into per-queue FIFO background workers, completing asynchronously when the queue has an unsatisfied GPU queue.Wait (avoiding CPU/GPU deadlocks on wait-before-signal Wait64BitValueX) and synchronously when no unsatisfied queue.Wait is active (preserving immediate CPU visibility after a GPU fence wait). Part of #1452.

Stacked on #877; only the top commit (e4d8229) belongs here.

Previously, sparse::readback_after_execute locked STATE, called s.queue.Wait(&s.rb_fence, v), and synchronously executed s.readback_ranges(&merged) (s.wait_for(s.value) with a 5,000 ms timeout) inside ExecuteCommandLists. When an async compute queue carried both a sparse memory write and a wait-before-signal Wait64BitValueX whose producer direct queue had not yet been submitted on the same thread, this deadlocked both the submitter thread (preventing submission of the producer queue) and sparse::State::queue for 5,000 ms per submit, triggering repeated xcl-sync-watchdog timeouts and causing a retail title's stall watchdog to abort after 5–6 consecutive submits.

With per-queue FIFO workers waiting on a raw per-queue fence outside STATE.lock() before invoking readback_ranges, sparse::State::queue is never stalled behind a blocked title queue (sparse readback timeouts dropped to 0 across >500 s gameplay runs). Active unsatisfied queue.Wait calls are tracked via sparse::note_queue_wait (hooked in q_wait_pc and the Wait64BitValueX GPU bridge, cleaned up in q_release via sparse::forget_queue): when queue has an unsatisfied wait, readback_after_execute returns immediately; when queue is unstalled, readback_after_execute waits on the worker's completion condvar outside STATE.lock() so CPU reads immediately after a GPU fence wait (such as Write32BitValueBottomOfPipeX in testtitle) always see the read-back bytes.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs:
    • Add per-queue RB_QUEUES map (RbQueueState holding a raw ID3D12Fence via super::real::create_raw_fence, monotonic fence value, and channel sender to a dedicated xcl-sparse-rb-<queue> FIFO worker thread) and QUEUE_WAITS tracking (note_queue_wait, queue_has_unsatisfied_wait, forget_queue).
    • Have readback_after_execute filter/merge sparse ranges using uma::get(), signal the queue's fence, and enqueue an RbJob without acquiring STATE or calling s.queue.Wait — returning immediately if queue_has_unsatisfied_wait is true, or waiting on the job's completion condvar outside STATE.lock() otherwise.
    • Have each worker wait on its fence outside STATE.lock() (5,000 ms timeout with power-of-two warning logging on late queues) before locking STATE and running s.readback_ranges(&job.ranges).
    • Add unit test readback_after_execute_does_not_block_on_stalled_queue verifying on a real mapped sparse tile that readback_after_execute returns in < 1 s and leaves STATE unlocked while a compute queue is blocked on an unsignaled fence, and completes synchronously once the gate fence is signaled.
  • crates/xcl/src/hle/d3d12x/real.rs & crates/xcl/src/hle/d3d12x/list_x.rs:
    • Record active unsatisfied queue.Wait calls via sparse::note_queue_wait in q_wait_pc and try_gpu_wait, and clean up per-queue state in q_release via sparse::forget_queue.

Tested

  • Hardware / OS: AMD Radeon RX 9060 XT (16 GiB VRAM, RADV Mesa 26.2.7), Fedora Linux, GE-Proton11-14-x86_64, vkd3d-proton master ec20719452e916b (-Db_ndebug=true), DXC 1.9.2609.5, cross-compiled x86_64-pc-windows-msvc (clang-cl + lld-link).
  • Rebased onto main 689f2db + D3D12.X: preserve native vtables and wrap fences for vkd3d-proton under Wine (#175) #867 (a4c5a82) + Memory: avoid amdgpu userptr stalls under Wine (#175) #871 (b3f2f7c) + Memory: lazily map sparse VRAM tiles for Wine graphics heaps (#175) #877 (b112fe7) and verified under Proton (--test-threads=1, XCL_DXC_DIR set for GPU tests):
    • --exact hle::d3d12x::sparse::tests::readback_after_execute_does_not_block_on_stalled_queue: 1 passed, 0 failed, 0 ignored, 0 skipped
    • hle::d3d12x::sparse::tests: 16 passed, 0 failed, 1 ignored (pre-existing #[ignore] early_commit_timing)
    • hle::d3d12x::list_x::: 27 passed, 0 failed, 0 ignored
    • --exact gpu::recomp::tests::gpu_sparse_vram_descriptors_buffers_and_xmem gpu::recomp::tests::gpu_valu_float_and_int: 2 passed, 0 failed, 0 ignored, 0 skipped
  • Retail title 60 s smoke run under GE-Proton11-14 (XCL_NETWORK=0 XCL_PSO_PRELOAD=0 XCL_RUN_SECONDS=60 XCL_ACTIVITY=1, VKD3D_CONFIG=""): 1079 frames shown (1079 PresentX, 0 0 0 presents/2 s) with 0 crashes and 0 device removals.

Regression run
Produced under Wine (GE-Proton11-14, AMD Radeon RX 9060 XT, vkd3d-proton ec20719452e916b, XCL_NETWORK=0 XCL_PSO_PRELOAD=0 XCL_RUN_SECONDS=60 XCL_ACTIVITY=1, VKD3D_CONFIG="") using the xcl-smoke v1 log extraction format (Linux environment; native Windows scripts/smoke.ps1 could not be run; peak commit: 0 MB is what 689f2db logs under Wine before #1097).

main (689f2db):

xcl-smoke v1
commit: 689f2db477c3c062cf162649781640ef8a977c18
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x3
frames shown: 0 (did not reach end)
presents per 2 s (last 3): 0 0
device removals: 0
crashes: 2
peak commit: 0 MB

This PR (e4d8229, stacked on #877):

xcl-smoke v1
commit: e4d8229650344e003513bd3775eb90502854c96b
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1079 (PresentX 1079)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

Not tested

  • Native Windows (test_title_runs_and_game_folder_is_untouched e2e needs re-check on Windows after e4d8229).
  • NVIDIA Linux GPUs.

AI use

  • Antigravity (Google DeepMind, exact model name not exposed to the agent) assisted with root-cause analysis, implementation, rebase, unit test design, and running the Wine test/smoke verification on this machine.

anoyarou pushed a commit to anoyarou/xcl that referenced this pull request Nov 24, 2026
…cl-dev#1732)

sparse::readback_after_execute previously stalled sparse::State::queue
behind the title queue (s.queue.Wait) and synchronously blocked the
submitter thread inside STATE.lock() for up to 5000 ms (s.wait_for).
When an async compute queue carried both a sparse write and a
wait-before-signal Wait64BitValueX whose producer direct queue had not
yet been submitted on the same thread, this deadlocked both the
submitter thread (blocking submission of the producer queue) and
sparse::State::queue for 5000 ms per submit, triggering xcl-sync-watchdog
timeouts and the title's stall watchdog after 5-6 consecutive submits.

Dispatch sparse readbacks to per-queue background workers that wait on a
raw per-queue fence outside STATE.lock() before invoking readback_ranges,
never stalling sparse::State::queue or blocking ExecuteCommandLists.
@LocranKing

Copy link
Copy Markdown

Independent Windows release GPU check on NVIDIA RTX 3060 Ti (00eec16bbc2b41bc4f56d0f8486dfbcbe8073169, merge-base b7aa9f3bd212088e0d927439284dc5ed325ce3c7): test_title_runs_and_game_folder_is_untouched fails on Write32BitValueBottomOfPipeX: 0x0 (the 60 s retail smoke passes).

Setup

  • GPU: NVIDIA GeForce RTX 3060 Ti (8024 MiB VRAM, wave32), driver 32.0.16.2101
  • CPU / RAM: AMD Ryzen 5 5500 (6C/12T), 31.8 GB RAM
  • OS: Windows 11 Home Single Language, build 26200
  • Toolchain: rustc 1.100.0, DXC 1.9.2602.17, --release

Workspace tests (cargo test --workspace --release --no-fail-fast -- --test-threads=4)

  • Exit 101: 773 passed, 1 failed, 41 ignored across 20 result blocks (SKIPPED: 2 no wave64, 2 no D3D12 debug layer).
  • Failure in crates\xcl\tests\e2e.rs:132:5 (test_title_runs_and_game_folder_is_untouched):
    thread 'test_title_runs_and_game_folder_is_untouched' panicked at crates\xcl\tests\e2e.rs:132:5:
    failed checks in the test title:
    [FAIL] D3D12.X: console command list memory methods (writes, timestamp, fill, copy, KickoffX, Wait32/64): 
    Write32BitValueBottomOfPipeX: 0x0
    
    This looks directly related to moving sparse::readback_after_execute onto an asynchronous per-queue worker: after the title's GPU fence wait completes, the CPU reads 0x0 before the async readback copies the GPU write back to CPU memory.
  • Also note the PR branch currently includes two earlier Wine commits (b66a150, fbf1440) on top of b7aa9f3.

Regression run (scripts/smoke.ps1, 60 s, XCL_NETWORK=0, title folder name redacted)

  • PR head vs base b7aa9f3: 1585 / 1508 frames (105.1%), 15209 / 14730 MB peak commit (103.3%), 0 device removals, 0 crashes.

Base (main b7aa9f3):

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

PR head (00eec16):

xcl-smoke v1
commit: 00eec16bbc2b41bc4f56d0f8486dfbcbe8073169
gpu: NVIDIA GeForce RTX 3060 Ti
title: retail-1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1585 (PresentX 1585)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15209 MB

Not tested: Linux / Wine, repeating e2e to measure flake rate vs deterministic failure, AMD / wave64 hardware.

AI use: Antigravity (Gemini 4 Neon) built and ran the workspace test suite and smoke pair on this hardware and drafted this report from the captured outputs; reviewed before posting. No game files attached.

anoyarou added a commit to anoyarou/xcl that referenced this pull request Nov 24, 2026
…pub(crate)

Fixes compilation errors on MSVC Windows target:

- D3D12.X: restores pub(crate) unsafe fn create_raw_fence in real.rs required
  by sparse::readback_after_execute (xcl-dev#1732). Creates an unproxied raw
  ID3D12Fence bypassing title vtable hooks so internal queue worker threads under
  Wine/vkd3d-proton signal native fences without hitting proxy mismatches.

- KernelX: restores pub(crate) visibility for commit_sparse in kernelx.rs
  (xcl-dev#549, xcl-dev#1298), required by DirectStorage's lazy MEM_RESERVE sparse
  window allocation in dstorage.rs and placeholder mappings.

- Recompiler: adds #[cfg(test)] gate above mod tests in emit.rs to silence unused
  PRELUDE_MEM import warnings in non-test release builds (xcl-dev#1524).

Assisted-by: Antigravity
anoyarou added a commit to anoyarou/xcl that referenced this pull request Nov 24, 2026
…driver gave up callback

Eliminates compiler warnings for unused items:

- D3D12.X: removes rb_fence and rb_value from sparse::State and test fixtures
  (xcl-dev#1732). Readback fences are now managed per-queue in RB_QUEUES
  by async readback workers, making the fields on State obsolete.

- D3D12.X: hooks note_driver_gave_up in real::translated_compute_pso_once when
  driver_compile_identified returns E_OUTOFMEMORY after DRIVER_GAVE_UP_AFTER
  (xcl-dev#1665, xcl-dev#1524), ensuring failed massive compute pipelines are
  persisted in used-pipelines.txt (!<key>) and eliminating dead code warnings.

Assisted-by: Antigravity
@gustavsi
gustavsi force-pushed the wine/async-sparse-readback branch from 00eec16 to 64cb447 Compare November 24, 2026 21:13
@gustavsi

Copy link
Copy Markdown
Author

Thanks @LocranKing for catching that on Windows!

Your diagnosis was spot on: when an unstalled queue (like testtitle's Write32BitValueBottomOfPipeX + wait(&queue, &fence, 1)) completes, the title's own GPU fence completed before the background worker finished readback_ranges.

Updated 64cb447 to track active unsatisfied queue.Wait calls (sparse::note_queue_wait in q_wait_pc and list_x::try_gpu_wait, cleaned up in q_release via sparse::forget_queue):

  • When queue has an unsatisfied queue.Wait (wait-before-signal Wait64BitValueX whose producer is not yet submitted), readback_after_execute returns immediately while the per-queue FIFO worker finishes asynchronously (never deadlocking the submitter thread or stalling sparse::State::queue).
  • When queue has no unsatisfied queue.Wait, readback_after_execute still waits on the queue's fence outside STATE.lock() and off sparse::State::queue, but waits for the FIFO worker's completion condvar before returning so CPU reads right after a GPU fence wait always observe the updated bytes.

(The two earlier commits b66a150 / fbf1440 are there because this PR is stacked on #877 for Wine smoke testing; only the top commit 64cb447 belongs to this PR.)

…er Wine (xcl-dev#175)

vkd3d-proton validates iface->lpVtbl on adopted D3D12 objects passed back into d3d12core.dll (command allocators, lists, heaps, resources, PSOs, and fences). On unmodified assertion-free vkd3d-proton (such as GE-Proton or -Db_ndebug=true builds), XCL's in-place vtable adoption causes CreateCommandList/Reset to fail with E_INVALIDARG and fence waits to reject adopted fences.

Under Wine (gated behind util::under_wine(), native Windows unchanged):
- UnadoptGuard temporarily restores the native vtable on adopted command allocators (including alloc_pool backings and reset_pc_allocator), command lists, heaps (CreatePlacedResource/CreatePlacedResource1), RTV resources, and PSOs around calls into d3d12core.dll.
- FenceProxy wraps title ID3D12Fence1 objects so the underlying PC fence keeps its native lpVtbl, unwrapping in queue Signal/Wait while preserving Fence::xcl-dev#12 completed_value_address and freeing on final Release.
- real::create_raw_fence creates un-proxied native ID3D12Fence objects for XCL's own internal queue tracking (late_release, alloc_pool, list_x, cpu_writes, present, vram, drain_gpu) so un-adopted internal queues like sparse::queue() never receive a FenceProxy.

Note: assert-enabled vkd3d-proton builds (such as stock Proton Experimental) still validate iface->lpVtbl inside an adopted object's own methods, so an assertion-free build (-Db_ndebug=true / GE-Proton) remains required for in-place adoption.

Assisted-by: Antigravity (Google DeepMind)
On Linux with amdgpu, a multi-GiB coherent UMA window imported via OpenExistingHeapFromAddress (VK_EXT_external_memory_host userptr) causes two severe stalls:
1. GetWriteWatch(WRITE_WATCH_FLAG_RESET) on the userptr range triggers amdgpu MMU-notifier evictions (~100-200 ms/submit).
2. A multi-GiB userptr BO stalls every Vulkan GPU submission (~150 ms/frame, dropping PresentX frames).

Under Wine (native Windows unchanged):
- Key the default 512 MiB coherent_hint() cap on AMD GPUs (DXGI VendorId 0x1002) under Wine so NVIDIA Linux keeps a full-size coherent window (unless XCL_COHERENT_MB is set).
- Allocate the coherent window without MEM_WRITE_WATCH and exclude it from watch_regions(), notifying title_textures on DirectStorage writes into it and hashing small/dynamic coherent textures with a 1/16 round-robin sweep for large ones.
- Route overflow PAGE_GRAPHICS_* title GPU heaps into the sparse VRAM windows so shaders still read them directly.

Assisted-by: Antigravity (Google DeepMind)
…#175)

Under Wine, overflow PAGE_GRAPHICS_* title heaps are routed to sparse VRAM
windows (xcl-dev#871). Eagerly mapping 64 KiB VRAM tiles for the full reserved
heap up front consumes ~3.2 GiB of local VRAM for zero-filled or never-
written pages before the title even leaves the menus.

Add sparse::alloc_lazy (used under util::under_wine() only; native Windows
unchanged):
- Commits CPU pages up front so host accesses never fault, while deferring
  64 KiB VRAM tile allocation until the first non-zero CPU write or GPU
  write target registration (placed buffers, buffer UAVs, and command-list
  memory writes via sparse::ensure_lazy_mapped).
- Unit tests cover lazy-range overlap, hole masks, non-zero page detection,
  and coalesced lazy tile allocation.

Assisted-by: Antigravity (Google DeepMind)
…g STATE or sparse::State::queue

sparse::readback_after_execute previously stalled sparse::State::queue
behind the title queue (s.queue.Wait) and synchronously blocked the
submitter thread inside STATE.lock() for up to 5000 ms (s.wait_for).
When an async compute queue carried both a sparse write and a
wait-before-signal Wait64BitValueX whose producer direct queue had not
yet been submitted on the same thread, this deadlocked both the
submitter thread (blocking submission of the producer queue) and
sparse::State::queue for 5000 ms per submit, triggering xcl-sync-watchdog
timeouts and the title's stall watchdog after 5-6 consecutive submits.

Dispatch sparse readbacks through per-queue FIFO background workers that
wait on a raw per-queue fence outside STATE.lock() before invoking
readback_ranges, never stalling sparse::State::queue. Track active
unsatisfied queue.Wait calls via note_queue_wait (in q_wait_pc and the
Wait64BitValueX GPU bridge, cleaned up in q_release via forget_queue):
when the queue has an unsatisfied Wait, readback_after_execute returns
immediately while the worker finishes asynchronously; when no
unsatisfied Wait is active, readback_after_execute waits on the worker's
completion condvar outside STATE.lock() so CPU reads immediately after a
GPU fence wait (such as Write32BitValueBottomOfPipeX in testtitle)
always observe the updated bytes.
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main in crates/xcl/src/hle/d3d12x/vram.rs — needs a rebase before it can be tested.

@gustavsi
gustavsi force-pushed the wine/async-sparse-readback branch from 64cb447 to e4d8229 Compare November 24, 2026 22:44
@gustavsi

Copy link
Copy Markdown
Author

Thanks @DavidM1911! Rebased the stack (#867 -> #871 -> #877 -> #1732) onto main 689f2db (HEAD e4d8229, resolving the vram.rs conflict) and re-verified unit/GPU tests and the 60 s smoke run under Wine — ready for Windows testing.

@JJAutoCompare

Copy link
Copy Markdown

XCL PR #1732 verification on Intel Arc A770

Head e4d8229650344e003513bd3775eb90502854c96b, detached and clean. STOR-WINLAB ran Windows x64 build 26200.9974 on an AMD x64 CPU and Intel Arc A770 (driver 32.0.101.9175). DXC 1.9.2609.5 and rustc 1.100.0 (0ef1bc386, x86_64-pc-windows-msvc). Debug profiles, with debug info and incremental compilation disabled.

Tested

  • cargo build --workspace and cargo test --workspace --no-run succeeded outside the Arc mutex. Cargo listed 19 test binaries.
  • Each focused run used cargo test -p xcl --lib hle::d3d12x::sparse::tests::<name> -- --exact. Each reported 1 passed, 0 failed, 0 ignored and 0 skip markers.
    • readback_after_execute_does_not_block_on_stalled_queue exercised sparse UMA on the A770. It asserted return in under one second with an unsignaled compute-queue fence, availability of STATE, and queue drain after the fence was signaled. It then exercised the unstalled call.
    • ensure_mapped_with_query_holes_skips_uncommitted_pages_on_readback checked that readback across sparse holes did not fault and that a write to a newly committed page was detected. Both D3D12 filters ran under the Arc mutex with XCL_ADAPTER=A770. The holes test selects the default D3D12 device with a WARP fallback internally.
    • lazy_range_helpers_track_tile_overlap_holes_and_nonzero_pages checked overlap, hole masks and nonzero-page detection with XCL_ADAPTER=NONE_CPU_ONLY outside the mutex.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 ran once under Invoke-ArcTest.ps1 with XCL_ADAPTER=A770 and RUST_TEST_NOCAPTURE=1. Exit 101. All 20 result lines (19 binaries and one doctest suite) sum to 954 passed, 25 failed and 45 ignored. All three focused tests also passed in this run.

Workspace failures and skips

24 DXIL/recompiler tests failed (20 compute PSO rejections with 0x80070057 and four output mismatches). The title e2e failed its dynamic depth-bias check (blue pixel, expected green). The log contains 25 exact SKIPPED lines, excluded from coverage.

Not tested

Matched main run, retail-title smoke, sample-data coverage, Wine or other GPUs.

AI use

T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high) ran the build and verification. Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr1732-suite.log and published.

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