Skip to content

D3D12.X: give each command queue its own cpu_writes worker - #562

Open
gridoe wants to merge 1 commit into
xcl-dev:mainfrom
gridoe:cpu-writes-per-queue
Open

gridoe wants to merge 1 commit into
xcl-dev:mainfrom
gridoe:cpu-writes-per-queue

Conversation

@gridoe

@gridoe gridoe commented Nov 24, 2026 •

Copy link
Copy Markdown

Give each D3D12 command queue its own cpu_writes completion worker thread instead of multiplexing every queue through one global FIFO channel.

Previously, cpu_writes::after sent every queue's bottom-of-pipe writes to a single shared xcl-cpu-writes thread. If one queue waited on an unsignaled GPU fence (up to WAIT_MS = 10 s), bottom-of-pipe writes from other queues whose fences had already completed were head-of-line blocked behind it. Storing a dedicated channel per QueueFence lets independent queues drain concurrently, and removing the entry on CommandQueue::Release (forget_queue) lets its worker exit cleanly and close its Win32 event handle.

What is in it

  • crates/xcl/src/hle/d3d12x/cpu_writes.rs: replace the single global worker() channel with a per-queue mpsc::Sender<Job> on QueueFence (preserving the LATE timeout warning from dbe9324), close the event handle when the worker exits, add forget_queue(queue), and add unit test blocked_queue_does_not_stall_another_queues_writes (isolated on a WARP device with a Drop drain guard so its intentional fence wait cannot interfere with sibling GPU tests sharing the default adapter).
  • crates/xcl/src/hle/d3d12x/real.rs: call cpu_writes::forget_queue when a CommandQueue reaches refcount 0 in q_release.

Tested

  • Hardware / OS / toolchain: NVIDIA GeForce RTX 3080 (10053 MiB VRAM, driver 617.42, wave32), AMD Ryzen 7 5700X3D (16 logical CPUs), Windows 11 IoT Enterprise LTSC 2024 26H2 (Build 26300.9974), DXC 1.9.2602.17, rustc 1.99.1 (58cf8f9c3 2026-10-13) (stable-x86_64-pc-windows-msvc), debug (tests) and release (smoke).
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 717 passed, 0 failed, 41 ignored.
  • SKIPPED GPU tests on this adapter: 2 (no wave64 — gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).
  • Parallel flakiness check (cargo test -p xcl --lib cpu_writes, default threads): 20/20 consecutive runs passed (4 passed each, 0 device removals, 0 flakes).

Regression run

main:

xcl-smoke v1
commit: 09940bb87fa11bc394fd9e5de570c2b7cfd16323
gpu: NVIDIA GeForce RTX 3080
title: d2d324b7
seconds: 60
exit: 0x0
frames shown: 1442 (PresentX 1442)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14579 MB

PR:

xcl-smoke v1
commit: 8ec2a46974d337c5c121e2af582524bd18897497
gpu: NVIDIA GeForce RTX 3080
title: d2d324b7
seconds: 60
exit: 0x0
frames shown: 1541 (PresentX 1541)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14546 MB

Not tested

  • AMD / Intel GPUs and wave64-native adapters.
  • Other retail titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant).
  • Sample-data tests requiring XCL_SAMPLE_* env vars.

AI use

  • Antigravity with Gemini 4 Neon: wrote the per-queue worker refactor, WARP-isolated unit test, rebase, and PR description; all builds, tests, stress runs, and smoke runs were executed locally on the hardware listed above.

@brothernog

brothernog commented Nov 24, 2026 •

Copy link
Copy Markdown

Tested 3dc82a8 on an RTX 3050 (driver 610.88), Windows 11 Home 26300, Rust 1.100.0, debug. The new test makes its neighbours flaky here when they run in parallel:

  • cargo test -p xcl --lib cpu_writes (4 tests, default threads): 5 of 20 runs failed, and 1 more of 6 in an earlier batch.
    • 3 runs: title_textures::tests::gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes panics at title_textures.rs:1192, where CreateCommandQueue returns 0x887A0005 (DXGI_ERROR_DEVICE_REMOVED).
    • 2 runs: the new test fails at cpu_writes.rs:293, "q1 write landed before its fence was signaled" (left: 11). That is what a removed device looks like from there, since its fences read as complete.
    • The failing runs take ~7 s, against ~1 s for the passing ones.
  • Same filter, PR code with --skip blocked_queue: 0 of 20 failed.
  • Same filter with cpu_writes.rs and real.rs from main 508a0c7 (the 3 old tests): 0 of 20 failed.
  • The new test alone (--exact): 0 of 20 failed. The filter with --test-threads=1: 0 of 10 failed.

So it only shows up when blocked_queue_does_not_stall_another_queues_writes runs next to another GPU test. My guess, not confirmed: D3D12CreateDevice on the default adapter hands back the same device the other tests use, so the q1.Wait(&blocker, 1) puts a GPU-side wait on the shared device. I didn't query GetDeviceRemovedReason. Since the suite runs with --test-threads=4, it could make the suite flaky on other machines too.

Also run: cargo test -p xcl --lib cpu_writes -- --list shows the new test. I didn't run the full suite.

This review was AI-assisted.

T-Crypt added a commit to T-Crypt/bathyal that referenced this pull request Nov 24, 2026
@WebIdentity1

Copy link
Copy Markdown

Native Windows integration check of this PR (3dc82a89896626374064563d436e3041261f9b74) with #317 (d87aa071287e4a8cd5f4a068d3b791e01c7052c7), both applied to main 9ce2070eccf436d7bd38601c90407da447df34f3. This is a local conflict resolution, not a test of either unmodified PR head.

The tested integration is available at commit de9deed5 for the original authors to inspect/reuse; no competing upstream PR. It keeps this PR's queue-local channels and release cleanup, calls #317's write_now(&Write) from the worker, and converts the new fixture to Write::U32. Raw test queues are removed from the worker map after completion. The fixture unblocks/drains before its assertions so the negative control can fail without leaving a queued GPU wait or dangling write destination; its independence and ordering assertions remain.

Tested

  • Windows 10 Home 19045, Ryzen 7 7800X3D, RTX 3090 driver 591.74 and AMD Radeon integrated graphics driver 32.0.21030.2001; Rust 1.98.1 MSVC, debug, SDK DXC 1.8.2502.11, public Agility 1.620.2.
  • cargo test --locked -p xcl --lib -- cpu_writes::tests:: timestamps_outside_unified_memory_land_after_the_queue write64_os_timestamp_flags_route_to_qpc_instead_of_gpu_timestamps --nocapture --test-threads=1: 6 passed / 0 failed / 0 ignored, no skips. Covers independent queue completion, QPC/PAGE_GUARD, atomic64, retained ReadbackU64, GPU timestamp readback and flag routing.
  • Device identity checked from each fixture's actual device LUID: default device was AMD. Separate test-only adapter-selection overlay asserted RTX 3090 for all five GPU fixtures; again 6/0/0, no skips (sixth test is CPU-only).
  • Negative control: only the worker factory changed to return clones of one shared Sender; all Write variants and fixture cleanup retained. cargo test --locked -p xcl --lib blocked_queue_does_not_stall_another_queues_writes -- --nocapture --test-threads=1: 0 passed / 1 failed / 0 ignored on each GPU. In both: queue2 did not finish independently, queue1 remained unwritten before signaling, both drained after signaling, GetDeviceRemovedReason = Ok(()). With per-queue workers, independence passed and those ordering/drain/device checks also passed.
  • Both cargo build --locked --workspace runs passed. Matched serial workspace command in the interactive desktop session: cargo test --locked --workspace --no-fail-fast -- --test-threads=1 --show-output. Main: 683 passed / 1 failed / 41 ignored. Integration: 686 passed / 1 failed / 41 ignored. Sole failure on both: gpu_clamp_and_omod_per_instruction (Recompiler: complete remaining RDNA2 integer CLAMP=1 saturation (#105) #202). Four explicit skips on both, included in Rust's pass counts: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no native wave64); test_title_draws_with_a_root_signature_under_the_debug_layer, uploads_start_from_each_subresources_state (debug-layer availability). Sample inputs unset; system XGameRuntime comparison unavailable. Test-only device-selection/control overlays were restored before these full runs; all changed-file hashes match the published commit.

Not tested
Retail regression, release builds, or the full four-thread suite on this integration. The reported parallel-test interference is not fixed or disproved by these serial runs. Full-suite recompiler tests select NVIDIA; default-device fixtures may use AMD. Synthetic inputs only; no retail files used.

AI use
Codex (GPT-6) prepared the integration and test-only controls, ran the native Windows checks and wrote this report. Runtime changes originate in #317 and #562.

@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 3dc82a89896626374064563d436e3041261f9b74 on AMD RDNA4. I merged it onto current main dd2ae9a locally (clean merge) and tested that tree.

Tested

I cannot say this PR causes the crash: I have one occurrence and no dump. It is a different symptom from @brothernog's (a removed device next to the new test, on an RTX 3050), but both point at flakiness when the lib tests run in parallel with this change, so it may be worth running cargo test -p xcl --lib cpu_writes and the whole lib a few times on the final head.

Review: per-queue workers remove the head-of-line blocking described: each QueueFence owns its channel, and its worker waits on its own fence. Two things to check: (1) a worker thread is spawned per queue and ends when its sender is dropped, so a title that creates and releases many queues relies on forget_queue running for each; a queue never released keeps its thread. (2) The new test leaves a queue blocked on an unsignalled fence while other tests in the same process use the device; that matches the removed-device failures @brothernog saw, and @WebIdentity1's integration commit drains the blocked queue before asserting, which may be the fix.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; no crash dump was captured.

Run and report by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@anes66

anes66 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

There is a separate completion race in the existing writes_land_after_the_queue test that this branch retains.

write_now release-stores the dwords one at a time. The test waits for the first dword to become 7, then reads both values. That does not establish that the second store has happened.

29c254a is a small test-only fix on main 64b3f35: wait for the last dword, 9, with acquire ordering before checking the array. It preserves the two-dword coverage. #317 changes this test for its new 64-bit write path; this commit does not implement that feature or the per-queue worker change.

Tested

  • A temporary 30 ms pause between the worker's stores reproduced left: [7, 0], right: [7, 9]. The observation used atomic loads during this control.
  • Waiting for the last word passed with the same pause.
  • Both the pause and the temporary observation code were removed. The final diff changes only the wait condition and adds its explanation inside #[cfg(test)]; runtime code is unchanged.
  • Workspace build passed. cargo test --locked --workspace --no-fail-fast -j 4 -- --test-threads=4 --show-output: 701 passed, 0 failed, 41 ignored, exit 0, on that final patch (committed unchanged).
  • Windows 10 Pro 19045, Ryzen 7 9800X3D, RTX 3060 12 GB / 32.0.16.2114, Rust 1.100.0 MSVC, DXC 1.9.2602.17, Agility SDK 1.620.2, debug. Two native-wave64 SKIPPED; samples unset.

Not tested
This is not a validation of #562's head, its queue cleanup, or the device-removal reports above. Those still need separate investigation. No retail title, release, AMD/Intel or native-wave64 run.

AI use
Codex (GPT-6; exact variant unavailable) investigated the test, wrote the minimal correction, ran the controlled comparison and suite, and drafted this report.

@iggyvilla

Copy link
Copy Markdown
Contributor

Tested (PR head 3dc82a8, as fetched from pull/562/head, not merged onto current main; RDNA2/wave64, which the PR lists under Not tested)

  • AMD Radeon RX 6650 XT 8 GB (RDNA2), driver 32.0.21045.29003; AMD Ryzen 7 5700X, 32 GB RAM;
    Windows 11 Home build 26300.9974, rustc 1.100.0 (0ef1bc386 2026-11-09) stable-x86_64-pc-windows-msvc.

  • cargo build --release -p xcl — ok.

  • cargo build --workspace — ok (debug test title built).

  • cargo test --workspace --no-fail-fast -- --test-threads=4 on the PR head, debug:
    678 passed, 3 failed, 41 ignored, 0 skipped.

    • xcl lib: 638 passed, 3 failed, 21 ignored
    • console_dlls_e2e 13; console_shaders 3 passed, 4 ignored; disasm_e2e 1; e2e 3;
      gpu_compute_e2e 2; gpu_pso_e2e 7; heap_probe 4 ignored; pls_quota 1;
      predication_e2e 1; rpf_e2e 2; texture_states 1; xg_oracle 6 passed, 12 ignored.
    • No SKIPPED lines.
  • The three failures are the known failures of main on this card, not new ones:

  • Parallel-test flakiness (the removed-device failures and the one STATUS_ACCESS_VIOLATION crash
    reported in the comments) did not reproduce here:

    • cargo test -p xcl --lib cpu_writes at default threads, 10 runs: 10/10 ok (4 passed each,
      ~0.37 s per run), no removed device, no q1 write landed before its fence was signaled.
    • cargo test -p xcl --lib -- --test-threads=4, 3 runs: 3/3 finished without a crash, each with
      only the three known failures above (638/3/21).
    • The new test alone, 5 runs: 5/5 pass.
      This is a small sample (10 filter runs, 3 whole-lib runs) and the reports above were on NVIDIA
      adapters; it does not disprove the interference, only says it did not show on this card.
  • 60 s smokes with XCL_NETWORK=0, scripts/smoke.ps1 -Game D:\GTA6.Xbox.v1.0.2.1-CYBERLEEK,
    head first and its merge base 508a0c7 immediately after:

    Head 3dc82a8:

xcl-smoke v1
commit: 3dc82a89896626374064563d436e3041261f9b74
gpu: AMD Radeon RX 6650 XT
title: GTA6.Xbox.v1.0.2.1-CYBERLEEK
seconds: 60
exit: 0x0
frames shown: 359 (PresentX 364)
presents per 2 s (last 3): 0 53 1
device removals: 0
crashes: 0
peak commit: 11685 MB

Base 508a0c7:

xcl-smoke v1
commit: 508a0c74d7c0625d6d2de8d24494e69dc2e1926f
gpu: AMD Radeon RX 6650 XT
title: GTA6.Xbox.v1.0.2.1-CYBERLEEK
seconds: 60
exit: 0x0
frames shown: 428 (PresentX 431)
presents per 2 s (last 3): 0 36 56
device removals: 0
crashes: 0
peak commit: 11947 MB

Both smokes show a 0 in the last-3 presents window (likely a load/menu pause in the sampled
interval); the frame counts of the two 60 s runs are reported as measured. One run each, so no A/B
claim is made. Both exit 0x0, 0 device removals, 0 crashes.

Not tested

  • No retail-title run past the 60 s smoke (no measured menus/fps trajectory, no profile).
  • The PR head was not merged onto current main; only the head as fetched was built and run.
  • Tests were the debug profile; the release build was only built (and used for the smoke), not tested.
  • No XCL_SAMPLE_* tests (none set).
  • The flakiness checks are a small sample (10 cpu_writes filter runs, 3 whole-lib runs); the
    interference reported on NVIDIA adapters was not ruled out, only not reproduced.

AI use

opencode with DeepSeek V4.1 Flash (opencode-go/deepseek-v4.1-flash) ran the build, the tests, the
flakiness repeats and the smoke runs and wrote this report.

@milas6669

Copy link
Copy Markdown
Contributor

Regression run of 3dc82a898966 (PR head, built as it is, not merged onto main) against main e50836b, on a retail title.

Tested

  • NVIDIA GeForce RTX 3070 (32-lane waves), Intel i9-10900KF, Windows 11 Pro 10.0.26200, rustc 1.100.0, release builds
  • scripts/smoke.ps1 -Seconds 60, the same title and time for both, one run each, main first:

main (e50836b):

xcl-smoke v1
commit: e50836bdf6f5c026f281d545ed86fb2f07ba6e6a
gpu: NVIDIA GeForce RTX 3070
title: dumped
seconds: 60
exit: 0x0
frames shown: 287 (PresentX 287)
presents per 2 s (last 3): 9 10 10
device removals: 0
crashes: 0
peak commit: 15863 MB

this PR (3dc82a8):

xcl-smoke v1
commit: 3dc82a89896626374064563d436e3041261f9b74
gpu: NVIDIA GeForce RTX 3070
title: dumped
seconds: 60
exit: 0x0
frames shown: 304 (PresentX 304)
presents per 2 s (last 3): 6 6 6
device removals: 0
crashes: 0
peak commit: 15737 MB

What this does and does not say

  • It is a regression check: no crash, no device removal, and the frame count and peak commit are within the limits in CONTRIBUTING.md (at least 80 % of main's frames, at most 125 % of its peak commit).
  • It is not a speed measurement. One 60 s run varies a lot here: four runs of main on this machine gave 424, 359, 370 and 287 frames. A difference of this size between main and the PR is inside that noise.

Not tested

  • cargo test on this head (I did not run the test suite for this report)
  • AMD / wave64 hardware, visual correctness (I did not look at the picture)

AI use

  • Claude Code, Claude Sonnet 5.5, ran the commands and wrote this report.

Previously all command queues sent their bottom-of-pipe CPU writes into a single shared worker channel. If one queue stalled on its GPU fence (up to WAIT_MS = 10 s), writes from other queues whose fences had already completed were head-of-line blocked behind it.

Store a dedicated worker channel per QueueFence entry so independent queues drain concurrently, and remove the entry on CommandQueue Release (forget_queue) so the worker exits cleanly and closes its event handle. Isolate the new blocked-queue unit test on a WARP device with a Drop drain guard so its intentional fence wait cannot interfere with sibling GPU tests sharing the default hardware adapter.

Assisted-by: Antigravity (Gemini)
@gridoe
gridoe force-pushed the cpu-writes-per-queue branch from 3dc82a8 to 8ec2a46 Compare November 24, 2026 08:22
@gridoe

gridoe commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Rebased onto main and updated to 8ec2a46974d3 (8ec2a46): isolated blocked_queue_does_not_stall_another_queues_writes onto a WARP device with a Drop drain guard so its intentional q1.Wait(&blocker, 1) cannot stall sibling GPU tests sharing the default hardware adapter (addressing the parallel test flake reported by @brothernog and @MesshM's drain note).

Tested

  • Hardware / OS / toolchain: NVIDIA GeForce RTX 3080 (10053 MiB VRAM, driver 617.42, wave32), AMD Ryzen 7 5700X3D (16 logical CPUs), Windows 11 IoT Enterprise LTSC 2024 26H2 (Build 26300.9974), DXC 1.9.2602.17, rustc 1.99.1 (58cf8f9c3 2026-10-13) (stable-x86_64-pc-windows-msvc), debug (tests) and release (smoke).
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 717 passed, 0 failed, 41 ignored (2 SKIPPED GPU tests: no wave64 — gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).
  • Parallel flakiness check (cargo test -p xcl --lib cpu_writes, default threads): 20/20 consecutive runs passed (4 passed each, 0 device removals, 0 flakes).
  • 60 s regression run (scripts/smoke.ps1, release) on 8ec2a46 vs main 09940bb:

main:

xcl-smoke v1
commit: 09940bb87fa11bc394fd9e5de570c2b7cfd16323
gpu: NVIDIA GeForce RTX 3080
title: gta6_stage
seconds: 60
exit: 0x0
frames shown: 1442 (PresentX 1442)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14579 MB

PR:

xcl-smoke v1
commit: 8ec2a46974d337c5c121e2af582524bd18897497
gpu: NVIDIA GeForce RTX 3080
title: gta6_stage
seconds: 60
exit: 0x0
frames shown: 1541 (PresentX 1541)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14546 MB

Not tested

  • AMD / Intel GPUs and native wave64 hardware (not re-verified on RTX 3050 / RX 9060 XT).
  • Other retail titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant).
  • Sample-data tests requiring XCL_SAMPLE_* env vars.

AI use

  • Antigravity with Gemini 4 Neon: wrote the WARP-isolation and Drop drain fix, ran the test suite, parallel flakiness repeats, and scripts/smoke.ps1 regression run, and drafted this comment.

wondercrash pushed a commit to wondercrash/xcl that referenced this pull request Nov 24, 2026
Split supported recording segments before waits; open the queue gate only when its progress value is visible. Verify the original stale read and patched result with a two-queue GPU fixture.
Queue-local completion workers adapted from xcl-dev#562. Recorded-order trace and motivation: xcl-dev#539.
Assisted-by: omp (amd-openai/gpt-6.2-sol)
@3L1A5-1

3L1A5-1 commented Nov 24, 2026

Copy link
Copy Markdown

A retail run with this PR, in case it helps with what it covers and what it does not.

Setup: RTX 2060 SUPER 8 GB, GTA VI 1.0.2.1, main 930e8b4, release. Merged locally with 8ec2a46 (clean except for cpu_writes.rs, where I kept this PR's worker loop) and a diagnostic on cpu_writes and the counters the title waits on.

What it fixes: without it, one queue's write waited behind another queue's in the shared FIFO. It was written after newer values from the other queue (fence 17 / wanted 14), and the counter went down. With this PR, every write I logged landed on time (queue fence == wanted).

What it does not fix: the title submits the same closed list again and again, 12 times in 300 s. cpu_writes_after re-applies that list's recorded values each time, so its old counter value still overwrites newer ones, now from the queue's own worker. Details and the trace: #505 (my two comments today).

So on this title the per-queue worker is needed, but it is not enough by itself.

AI use: Claude Code (Claude Opus 5.5) ran this and wrote the note.

@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #1643 solves the same problem in a different way: #1607, group D63. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

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.

9 participants