Skip to content

GPU: transition shadow buffers between UAV and vertex/index/CBV states - #588

Closed
LAKSHYA-cmd-tech wants to merge 1 commit into
Marvisak:mainfrom
LAKSHYA-cmd-tech:fix/buffer-uav-vertex-state-transition
Closed

LAKSHYA-cmd-tech wants to merge 1 commit into
Marvisak:mainfrom
LAKSHYA-cmd-tech:fix/buffer-uav-vertex-state-transition

Conversation

@LAKSHYA-cmd-tech

Copy link
Copy Markdown

Summary

Resolves #548 (D3D12 debug layer ERROR id=538 and device hung on RTX 30-series GPUs during world execution).

Problem

In #548, the D3D12 debug layer repeatedly reported:
\`nDrawIndexedInstanced / IASetVertexBuffers on 'title buffer 0x159f0000+0xae00000' (182,452,224 bytes): state UNORDERED_ACCESS, expected VERTEX_AND_CONSTANT_BUFFER.
\`nFollowed by GPU device hung (\DXGI_ERROR_DEVICE_HUNG, \

@iurysf

iurysf commented Nov 24, 2026

Copy link
Copy Markdown

Tested on an RTX 4070 (driver 32.0.16.2101) on top of #493 + #483, the pair that stops the device removal on this GPU. Run with VICTORY_D3D12_DEBUG=1 + VICTORY_D3D12_GBV=1 + DRED=1, one run each.

D3D12 message base (#493 + #483) base + this PR
id 538, resource state invalid for use as a vertex buffer 0 17,152
id 527, barrier before-state doesn't match the preceding barrier 0 4
  • Example id 538: IASetVertexBuffers ... Resource state (0x2: INDEX_BUFFER) of resource 'title memory 0xd04800000+0x400000' is invalid for use as a vertex buffer; Expected 0x1 VERTEX_AND_CONSTANT_BUFFER.
  • Example id 527: Before state (0xC0 NON_PIXEL|PIXEL_SHADER_RESOURCE) of 'title buffer 0x3d60000+0xae00000' ... does not match the state (0x8 UNORDERED_ACCESS) specified in preceding ResourceBarrier.
  • My guess, not verified in the code: the same shadow buffer is bound as both index and vertex buffer for one draw, and only one of the two states is set. A combined VERTEX_AND_CONSTANT_BUFFER | INDEX_BUFFER read state may be what's needed there.
  • Without the validation layers, a 300 s run didn't hit a device removal, the same as the base. Both validated runs ended on the title's own exception before the world (base 135 s, this PR 156 s), so these are counts up to that point.

AI-assisted.

@LAKSHYA-cmd-tech
LAKSHYA-cmd-tech force-pushed the fix/buffer-uav-vertex-state-transition branch from 91ebf66 to cc8e78d Compare November 24, 2026 08:14
- Add Shadow::ensure_state to track and transition buffer states on host command lists.
- In prepare_for, ensure vertex and index buffers transition from UNORDERED_ACCESS to VERTEX_AND_CONSTANT_BUFFER and INDEX_BUFFER respectively.
- In readable, ensure buffers accessed as UAVs transition to UNORDERED_ACCESS.
- In root_view, transition buffers used as root CBV or SRV to their respective states.
- Add unit test shadow_buffer_transitions_state_when_requested verifying transitions.
- Resolves Marvisak#548 (D3D12 debug layer ERROR id=538 and device hung from buffer state mismatch).
@dpalacios69

Copy link
Copy Markdown
Contributor

Thanks for working on #548! After reviewing the diff and cross-checking with @iurysf's D3D12 debug-layer test results above, there are a few D3D12 state-tracking issues in this approach that explain both the new id=538 and id=527 errors:

  1. Exclusive single-bit read states clobber simultaneous bindings in the same draw (causes the 17,152 id=538 errors)
    In draw.rs, prepare_for calls self.bind (ensure_state(0xC0) / 0x1), then the vertex-buffer loop (ensure_state(0x1)), and then the index-buffer bind (ensure_state(0x2)). Because title heaps and 4 MiB loose windows (Shadow buffers) commonly back multiple bindings in the same draw (VB + IB + SRV + CBV), transitioning the same Shadow::buffer to INDEX_BUFFER (0x2) immediately invalidates its VERTEX_AND_CONSTANT_BUFFER (0x1) state right before DrawIndexedInstanced. Read-only buffer states need to be combined (e.g. D3D12_RESOURCE_STATE_GENERIC_READ) rather than replacing each other one bit at a time.

  2. indirect::bound and indirect::done transition Shadow::buffer directly without updating Shadow::state (causes id=527)
    In execute_indirect (draw.rs:1015–1055), indirect::bound assumes args.buffer is in UNORDERED_ACCESS and transitions it to INDIRECT_ARGUMENT, and indirect::done transitions it back to UNORDERED_ACCESS (indirect.rs:174–193) without updating Shadow::state. That leaves Shadow::state out of sync with the actual D3D12 state (matching the Before state 0xC0 ... does not match 0x8 UNORDERED_ACCESS id=527 error reported above), and prepare/bind between bound and ExecuteIndirect can also clobber INDIRECT_ARGUMENT on the same shared heap.

  3. Global Shadow::state: AtomicU32 is unsound across command lists and submissions

    • Command lists are recorded on multiple threads and executed in submission order (host.rs:290–296). Mutating a single global AtomicU32 at recording time (Ordering::Relaxed) means barriers are emitted against recording order rather than execution order (which is why textures use per-list HostList::states + HostList::prelude).
    • In D3D12, all buffer resources implicitly decay back to D3D12_RESOURCE_STATE_COMMON after ExecuteCommandLists completes. Because Shadow::state is never reset after Host::execute, subsequent submissions emit barriers with a stale StateBefore instead of COMMON.
  4. Missing UAV barrier and before == 0 skip on dropped draws

    • Back-to-back UNORDERED_ACCESS -> UNORDERED_ACCESS uses on the same buffer need a D3D12_RESOURCE_BARRIER_TYPE_UAV barrier, whereas ensure_state is a no-op when before == wanted.
    • Skipping the explicit transition when before == 0 relies on implicit promotion from COMMON, which breaks if the first ensure_state happens on a draw/dispatch that is later dropped (table_failed, missing index shadow, or rectangles() failure): no GPU command promotes the buffer, yet Shadow::state is already non-zero.
  5. Unit test coverage
    shadow_buffer_transitions_state_when_requested doesn't close/execute the command list or check D3D12 debug-layer messages, so it only verifies the AtomicU32::swap value rather than actual D3D12 barrier validity.

Note that #334 already takes the per-list BufferStates (GENERIC_READ vs UNORDERED_ACCESS) + prelude/close + indirect.rs integration approach along with a debug-layer execution test — it may be worth coordinating with or building on that design instead of tracking a global AtomicU32 on Shadow.

Reviewed against the diff and the code on main; not run on hardware. AI-assisted.

@Headache3318

Copy link
Copy Markdown

Second-read endorsement plus one nit not in @dpalacios69's list. Our independent read of this diff converged on the same prescription he names at the end: main already ships the per-list tool — list::ensure_state at list.rs:391, used for textures/render-targets/depth at draw.rs:403/406/1128/1145 — so extending that to shadow buffers beats hand-rolling a second global tracker; and the #548 id=538 class is not AMD-only, it reproduces on NVIDIA where the DRED report stops at op 42 (DispatchMesh). The nit: root_view sets let wanted = if srv { 0x40 | 0x80 }, which are COPY_DEST | COPY_SOURCE bits, not a shader-read state — so the SRV read path asks for the wrong state whatever tracker design lands, though it becomes moot by default once #493 c48b240 merges and bypasses root_view for SRVs (only VICTORY_ROOT_SRV_DIRECT=1 reaches it). AI-assisted diff-read at head 54c7c89e on base ffe310eb, Arch Linux; nothing built or run from this box.

@dpalacios69

Copy link
Copy Markdown
Contributor

@Headache3318 thanks for the second read, and agreed that extending list::ensure_state beats a second global tracker.

One correction on the nit: in D3D12_RESOURCE_STATES, 0x40 is NON_PIXEL_SHADER_RESOURCE and 0x80 is PIXEL_SHADER_RESOURCE (COPY_DEST is 0x400 and COPY_SOURCE 0x800). So 0x40 | 0x80 in root_view is a shader-read state, the same one @iurysf's debug-layer output prints above as 0xC0 NON_PIXEL|PIXEL_SHADER_RESOURCE. The problem with that line is the one in my list: ensure_state sets a single exclusive state, so a buffer that is also bound as a vertex or index buffer in the same draw ends up in the wrong one.

AI-assisted.

@Headache3318

Copy link
Copy Markdown

windows-gnu cross-target gate on this head merged onto ffe310e (AI-assisted, Arch Linux kernel 7.3.5, rustc 1.98.1, cargo test --no-run --locked --profile diag --features diagnostics --target x86_64-pc-windows-gnu, exit-code judged — a target upstream CI does not compile): all six device-removed-cluster heads pass in 38-41 s each with 0 warnings — #568 3a37896e, #501 934a8361, #242 75f7f205, #1236 6d06de4e, #427 033e906f, #588 merged 1074aaab.
#588's raw head alone fails E0061 'this function takes 2 arguments but 1 argument was supplied' at emit/lower.rs:6963 — NOT windows-gnu-only: it reproduces rc 101 on Linux AND MSVC scoped controls (-p victory-shader); it is staleness inherited from its recorded base 58fed370 (lowers(&words) fixed in main by 8e79c80), #588's own commit touches lower.rs zero lines, and the real 3-way merge compiles clean (rc 0, 40 s, 0 warnings).
API fact for the merge decision: #588's head 54c7c89e has zero CI check-runs — GitHub Actions (introduced in 8e79c80) has never executed on this PR, so there is no CI signal on either side of the merge.

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because by our reading the change is incorrect or would break something that works today.

Tracks one D3D12 state per buffer copy in a global atomic and emits transitions from it, but lists are recorded in parallel and in another order than they run, and buffers decay to the common state after every submission: the 'before' state of these barriers is wrong in general. Also carries unrelated reformatting. The promotion problem it reports (#548) deserves a per-list fix.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
geiyiliyabumo pushed a commit to geiyiliyabumo/VIctory that referenced this pull request Nov 25, 2026
Shaders: Read signed and unsigned bytes and halfwords from raw buffers
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.

RTX 3070 Ti: DXGI_ERROR_DEVICE_HUNG in the world; debug layer reports a title buffer in UAV state used as vertex buffer

5 participants