Skip to content

Sparse windows: CPU-write uploads run beside the title queue's writes (debug layer 1047); waiting for that queue would deadlock #262

Backlog

Description

@Kalmarv

Findings

  • HANDOFF's "sparse simultaneous-access errors (1047)" come at least partly from XCL. A sparse window is one 4 GiB reserved buffer. sparse::before_execute copies the CPU's writes into it on XCL's upload queue at once, while the submitting title queue may still have writes into the same buffer in flight. D3D12 allows a buffer many reading queues but one writing queue at a time. The debug layer reports OBJECT_ACCESSED_WHILE_STILL_IN_USE (1047): "Simultaneous-access or Buffer Resource … is still referenced by write GPU operations in-flight on another Command Queue".

  • Evidence: a synthetic test under the debug layer, in its own process like tests/texture_states.rs. A title queue copies 200 × 32 MiB into one half of a sparse allocation while the CPU's write to the other half is uploaded. It gets one 1047 per run, 3/3 runs on 7a033c8 (RTX 5070 Ti, NVIDIA 610.74). In the same process, a texture written on two queues and a plain buffer written on two queues give the same 1047.

  • The obvious fix deadlocks. I tried making XCL's upload queue wait for the submitting queue's earlier work, with a fence per title queue. That removes the 1047 (3/3), but the upload queue is one serial queue that every title queue waits on:

    • queue A GPU-waits for a fence F that queue B signals only in a later submit (legal; async compute does it);
    • A's next submit makes the upload queue wait for A;
    • B's upload sits behind that, and B waits for its upload, so B never signals F.

    A test of exactly this, with a 5 s timeout, passes on main and hangs with that change. It's a good regression test for any fix.

A plan that avoids both

  1. Record the CPU-write uploads and tile clears in a list of the submitting queue's type and run it on that queue just before the title's lists. Queue order then puts them after its earlier writes, with no cross-queue wait. They are all CopyBufferRegion, which direct, compute and copy queues accept.
  2. Track ring and slot reuse by each queue's own fence. The ring reclaims oldest first.
  3. Keep tile mappings on XCL's queue. It then never waits for a title queue, so title queues can still wait for it.
  4. Copies back of GPU writes (readback_after_execute) also go on the title queue, landed by a worker thread. This part is a separate small PR.
  5. Tests: the 1047 test, the A/B wait test, and a test that B sees a CPU write while A is blocked.

Question: today every title queue waits for the upload queue, so queue B sees CPU writes uploaded at A's earlier submit. With uploads on A's queue, B's work can run before A's upload and read the old bytes. Which do you prefer?

  • (a) B uploads again any chunk whose upload on A hasn't completed. That means two writers of the same bytes, the same data, and it may overwrite a GPU write made in between.
  • (b) Accept that window.
  • (c) Keep a cross-queue wait for B, with the deadlock above.

Not covered: writes in flight on other title queues into the same window still conflict under any design, because a window is one buffer. No retail run, no AMD.

🤖 Claude Opus 5.5

Activity

  1. iliassgm commented on Nov 23, 2026

    @iliassgm
    Contributor

    Taking this on (items 1–3 & 5 of the plan, keeping readback_after_execute separate as suggested):

    1. Record sparse tile clears and dirty CPU-write CopyBufferRegion uploads into per-queue command lists matching the submitting queue's type (DIRECT / COMPUTE / COPY) and submit them directly on that queue (bypassing title-queue hooks via own_work), while keeping UpdateTileMappings on XCL's own queue (queue.Wait(&s.fence) runs before the upload list).
    2. Track staging-ring reclamation and lazy per-queue command slots by each queue's own fence (cleaning up released queues safely via q_release), falling back to a temporary upload buffer if a different blocked queue holds the front of the 64 MiB ring so cross-queue waits cannot deadlock even under ring pressure.
    3. Add real D3D12 GPU tests covering: (a) concurrent writes + CPU upload without simultaneous-access conflict (and debug layer 1047 check when installed), (b) cross-queue A-waits-on-B deadlock regression (5 s timeout), (c) Queue B seeing CPU writes while Queue A is blocked, and (d) COMPUTE/COPY queue upload types.
  2. Kalmarv commented on Nov 23, 2026

    @Kalmarv
    ContributorAuthor

    @iliassgm thanks, good. Item 4 (readback_after_execute on the title queue, landed by a worker) is ours, a separate PR touching sparse.rs and real.rs (Wait noting). We'll keep it apart from your lines and rebase onto yours if yours lands first. When your PR is up we'll run it against the synthetic tests from the issue (the 1047 under the debug layer and the A/B wait with a 5 s timeout) and post the results there.

    Both tests are on a tests-only branch on main 4017f8f, if you want them in your PR: test/sparse-1047-cross-queue (615c267, only crates/xcl/tests/sparse_queues.rs and sparse_queue_waits.rs; GPL-2.0 like the rest). On main the 1047 test fails (3/3 runs, one 1047 each, RTX 5070 Ti) and the A/B wait test passes; the A/B test hangs on an upload fix that waits for the submitting queue. Not checked yet on CI without a GPU: they skip when the device or the debug layer cannot be created, and may run on WARP.

    🤖 Claude Opus 5.5

  3. Cattn commented on Nov 24, 2026

    @Cattn
    Contributor

    A related case on the title-texture side, in case it affects your per-queue plan:

    Setup: RTX 4070 12 GB, Ryzen 7 7800X3D, 32 GB RAM, Windows 11, a retail title, XCL_NETWORK=0.

    What blocks: while the title streams its world, thread dumps every 30 s show its submit thread blocked in XCL in title_textures::refresh → texture::upload_batch → texture::wait → gpu::fence::wait.

    • Each upload batch goes to XCL's own queue, and the title's thread waits on the CPU until the GPU has run it.
    • With video memory past the card, those waits are long, and presents drop to 0–8 per 20 s until the title's watchdog ends it.

    What changed it: a local change submits title-texture uploads on the submitting queue itself, when it is a DIRECT queue on the same device.

    • The upload list runs ahead of the title's lists, so the GPU orders the copies, with no cross-queue wait.
    • A fence on that queue retires the staging buffers. There is no CPU wait unless more than 512 MB is in flight.
    • Other queue types keep the current path.

    Result: the title got past its loading screen for the first time on this machine, twice: main 3516080 + this change, and the same with #335. Afterwards the world draws black and the watchdog ends it, which is a separate problem.

    It's the same direction as your item 1 for sparse uploads. To avoid touching the same lines, I'm planning a separate PR limited to title_textures.rs / gpu/texture.rs, unless you'd rather fold title textures into yours.

    (Analysis and change by Claude Code, Claude Opus 5.5; I ran the builds and game runs on the hardware above.)

  4. Kalmarv commented on Nov 24, 2026

    @Kalmarv
    ContributorAuthor

    @iliassgm one data point for your fix. We first tried having XCL's upload queue wait on the GPU for a fence that the submitting queue signals (local commit bfd37fc, not pushed as a PR). It removes the 1047, but the A/B wait test fails 3 of 3: queue A does not finish in 5 s. On main the test passes 3 of 3 (RTX 5070 Ti, NVIDIA 610.74). That test is now its own tests-only PR, #603 (crates/xcl/tests/sparse_queue_waits.rs); the 1047 test stays with the fix.

    🤖 Claude Opus 5.5

  5. wondercrash commented on Nov 24, 2026

    @wondercrash
    Contributor

    A retail run for the issue's Not covered ("No retail run"). The retail title produces the 1047 in the issue's shape, in the reverse direction, and also from UpdateTileMappings.

    Setup: RTX 5090 32 GB, NVIDIA 621.14, Ryzen 7 9800X3D, 62 GB RAM, Windows 11 23H2 (10.0.22631), release build. Retail title (GTA VI 1.0.2.1, Series X package), XCL_NETWORK=0, D3D12 debug layer on, XCL_DRED=1. Run 33 is on 5dd057d and run 34 on 72d2386 (run 34 also has 3 GB of VRAM ballast). Both are from our integration branch wondercrash/xcl: upstream main plus other open upstream PRs plus our own changes, not plain main. Its sparse.rs splits the sparse state's lock (our change) but keeps main's queue structure: title queues wait for the sparse queue's fence in before_execute (main 689f2db sparse.rs:1540), the sparse queue waits for a title queue only for readbacks (readback_after_execute, sparse.rs:1234), and UpdateTileMappings runs on the sparse queue (sparse.rs:706, 744). XCL logs only the first 8 messages of each debug-layer id, so these counts are a floor.

    Run 33: all 8 logged id-1047 messages come by 50.6 s. They involve one title queue and three buffers.

    • run33.log:1228, 4.49 s, the issue's case: ExecuteCommandLists on 'xcl sparse queue' while the buffer "is still referenced by read|write GPU operations in-flight on another Command Queue" (the title queue).
    • run33.log:22961-22971, 43.0-43.6 s, both directions. The title queue starts writes while the sparse queue has write or write|tilemapping in flight (22961, 22962, 22969). The sparse queue starts writes while the title queue has read|write or write in flight (22970, 22971).
    • run33.log:23034-23035, 50.55 s: UpdateTileMappings on 'xcl sparse queue' while the title queue has write in flight on the buffer: "not safe to start tilemapping GPU operations now".

    Run 34: the first 1047 is at 4.54 s (run34.log:1214, the upload shape), and the 8th is logged at 30.75 s (run34.log:12421).

    @iliassgm, on keeping tile mappings on XCL's queue (plan item 3): the last shape above is a mapping change on XCL's queue while the title queue's write to the same buffer is still in flight. Moving uploads to the submitting queue does not change that ordering. Item 3 keeps XCL's queue from ever waiting for a title queue, so we expect this 1047 to remain after the fix [inference]. It may be worth listing next to the issue's Not covered item. Our runs say nothing about the (a)/(b)/(c) question.

    Device removal: both runs ended in a device removal with no DRED page fault (run33.log:23411, 81.26 s; run34.log:23510, 87.67 s). When the device is removed, XCL logs the last 64 debug-layer messages. In run 34, 56 of those 64 are 1047 (48 from UpdateTileMappings), 9-68 ms before the removal. The 1047s start about 77 s before either removal and keep coming in between, so we don't read them as the cause. These two runs don't rule it out either.

    Not tested

    • Any fix: we have not tried per-queue uploads. We found no open PR for plan items 1-3 to run.
    • Plain main in a title run.
    • Whether the race corrupts anything visible.
    • AMD.

    AI use: omp with Claude Opus 5.5 read the run logs and the code, and Claude Opus 5.5 wrote this; the runs are ours on the machine above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions