Skip to content

D3D12.X: per-queue worker dispatch and atomic 64-bit immediate writes (#1488) - #1643

Open
slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/cpu-writes-queue-independence
Open

slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/cpu-writes-queue-independence

Conversation

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

Summary

Implements dedicated per-queue worker dispatch and atomic 64-bit immediate writes in crates/xcl/src/hle/d3d12x/cpu_writes.rs and list_x.rs (Issue #1488, finding XCL-BT-003):

  1. Per-queue worker dispatch (HOL-blocking & deadlock prevention):
    • Previously, a single global xcl-cpu-writes worker thread handled writes across all command queues.
    • When independent command queues waited on cross-queue fences, a queue waiting on an unsignalled fence blocked the shared thread, starving unblocked queues (head-of-line blocking) and causing deadlocks.
    • Replaced the single global worker with dedicated per-queue worker threads and channels (spawn_queue_worker), ensuring independent command queues make forward progress without blocking each other while preserving strict in-queue FIFO ordering.
  2. Atomic 64-bit immediate writes:
    • Added Write::ImmediateU64(usize, u64) and updated write_now to commit 8-byte aligned word pairs as a single atomic u64 via AtomicU64::store(..., Ordering::Release).
    • In list_x.rs, records Write::ImmediateU64 directly for 64-bit bottom-of-pipe writes to title memory, preventing 64-bit tearing under concurrent title polling.
  3. Unit tests:
    • atomic_64bit_write_ordering: Validates atomic u64 store without tearing under high-frequency concurrent polling reader.
    • independent_queues_do_not_head_of_line_block: Validates that an unblocked command queue executes immediately (< 3s) without being head-of-line blocked behind a stalled command queue waiting on an unsignalled fence.

Changes

  • crates/xcl/src/hle/d3d12x/cpu_writes.rs: per-queue worker dispatch channels, atomic 64-bit stores, and regression tests.
  • crates/xcl/src/hle/d3d12x/list_x.rs: emit Write::ImmediateU64 for 64-bit immediate writes.

Tested

  • Windows 11 x64, Rust MSVC stable.
  • cargo test -p xcl -- cpu_writes: 5 passed, 0 failed.
  • cargo test -p xcl -- list_x: 27 passed, 0 failed.
  • Clean diff: zero trailing whitespace, clean rebase on latest upstream/main (c160d86), GPL-2.0-only compliant, zero retail game data or keys.

Not Tested

  • Extreme high-queue-count multi-GPU concurrent execution.

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

- Replace the single global xcl-cpu-writes FIFO thread with dedicated
  per-queue worker threads and channels in cpu_writes.rs, preventing
  cross-queue head-of-line blocking and deadlocks when independent
  command queues await inter-queue fences (finding XCL-BT-003 from xcl-dev#1488).
- Ensure 64-bit immediate writes are stored atomically as a single u64
  via AtomicU64::store(..., Ordering::Release) on 8-byte aligned addresses
  (and via Write::ImmediateU64 in list_x.rs), preventing torn reads.
- Add unit test atomic_64bit_write_ordering verifying atomic 64-bit store
  without tearing under concurrent multi-threaded polling.
- Add unit test independent_queues_do_not_head_of_line_block verifying
  that an unblocked queue executes immediately even when another queue
  remains stalled on an unsignalled fence.

@gut0to gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted review; I traced the fence-value and channel-enqueue ordering.

static FIRST: std::sync::Once = std::sync::Once::new();
FIRST.call_once(|| crate::xlog_info!("D3D12.X: bottom-of-pipe writes outside unified memory are done by the CPU after the GPU ({} in the first submission)", job.writes.len()));
let _ = worker().lock().unwrap().send(job);
let _ = tx.send(job);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fence value and Signal are serialized under QUEUES, but the job is sent after that lock is released. Two concurrent after() calls can therefore signal values 1 then 2 yet enqueue job 2 before job 1. Once the fence reaches 2, the worker can apply the newer payload and then overwrite it with the stale value-1 payload, violating the per-queue FIFO guarantee. Please keep enqueue order serialized with fence-value assignment (or use another ordering mechanism). AI-assisted review; I verified the race from this call sequence.

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

Integrates 10 open upstream PRs on top of upstream/main (c160d86):
- xcl-dev#1643: per-queue cpu_writes worker threads (eliminating cross-queue
  head-of-line stalls) and atomic 64-bit immediate writes
- xcl-dev#1618: skip DXC validator where the runtime accepts the BYPASS hash
- xcl-dev#1535, xcl-dev#1536: PresentX COPY_SOURCE barrier and sparse upload COMMON
  state transitions
- xcl-dev#1516: route stream graphics PSO creation through tracked path
- xcl-dev#1478: avoid taking sparse state lock on resource final Release
- xcl-dev#1473: implement S_QUADMASK_B32 and S_QUADMASK_B64
- xcl-dev#1621, xcl-dev#1625: frame clock revalidation and short uncompressed
  DirectStorage request zero-padding
- xcl-dev#1557: accept no buffer context before Start in XMA2 voice test under
  Wine

Verified under GE-Proton11-14 + vkd3d-proton ec20719452e916b:
- Release build (x86_64-pc-windows-msvc): ok (32.14s)
- Proton unit tests (heap_window_tests, dxil::core_tests, cpu_writes,
  frame, sparse, present, dstorage, xma2_source_voice,
  gpu_sparse_vram_descriptors_buffers_and_xmem, gpu_valu_float_and_int):
  71 passed, 0 failed, 2 ignored, 0 skipped.
@Anon64

Anon64 commented Nov 24, 2026

Copy link
Copy Markdown

Regression run for this PR: head 7711f4c, based on main c160d86. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s, in one batch with the main run.

main c160d86:

xcl-smoke v1
commit: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1531 (PresentX 1531)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 100443 MB

this PR:

xcl-smoke v1
commit: 7711f4c55672e0fe155943e39c529d8a822af937
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1528 (PresentX 1528)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 99200 MB

No device removals or crashes; 99 % of main's frames, 98 % of its peak commit.

Peak commit on this machine is mostly the pipeline preload's DXC compiles (#1035) and varies from run to run, so it says little about this PR.

Not tested: the whole suite; whether the title reaches the changed code in these 60 s.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #562 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.

5 participants