Skip to content

GPU: one tracker for the states of title buffer copies - #367

Closed
MrEvilGamer wants to merge 1 commit into
Marvisak:mainfrom
MrEvilGamer:pr-buffer-state
Closed

MrEvilGamer wants to merge 1 commit into
Marvisak:mainfrom
MrEvilGamer:pr-buffer-state

Conversation

@MrEvilGamer

Copy link
Copy Markdown

What

A title buffer's host copy that a shader writes is promoted to the unordered access state, and that state does not decay back to the common state. Reading the copy later as vertex, index or constant data without a barrier is invalid. The debug layer reports it, and NVIDIA GPUs hang on it (see #199 and #264, which name IASetVertexBuffers with a buffer in UNORDERED_ACCESS). Separately, the indirect passes (indirect::bound and indirect::done) moved the same buffers with hard-coded "before" states that did not match what the list had left them in.

Every change of a copy's state now goes through one function, HostList::resource_state, which remembers the state the list left each buffer in, so a barrier always names it:

  • Shadow::refresh asks for the read state (BUFFER_READ: vertex and constant, index, both shader-resource stages, indirect argument), and Shadow::gpu_writes asks for unordered access.
  • indirect::bound and indirect::done use the same call instead of fixed transitions.
  • A list ends with the buffers it moved in the common state again (buffers_to_common, run by close), so the next list starts from a known state.

Refs #199, #264. It does not close them (see "Not fixed").

Tested

Windows 11, NVIDIA GeForce RTX 4060 Ti, diagnostics build, VICTORY_D3D12_DEBUG=1 (D3D12 debug layer), VICTORY_INDIRECT=1, a retail 1.0.2.1 copy. Each run goes through the menus (scripted Enter presses) into the world.

Debug layer errors in the world, one run each, on d2a4bae and on the same commit with this change:

id message d2a4bae with this change
527 ResourceBarrier: before state does not match the state in the preceding barrier 33 0
538 DrawIndexedInstanced: vertex buffer in the wrong state 23 0

The other errors are unchanged by it: 622 (wave64 compute shaders on a 32-lane device, #189 and #192 cover that), 599 (two) and 645 (the descriptor heap cap). A first run with the change did not get past the menus, so I did not count it; the second reached the world 20 s in.

I also ran the same change on a larger local branch with #24 and #282 applied, where the counts were far higher before it (4,832 of id 527 and 9 of id 538) and zero after.

  • cargo test -p victory-gpu: 43 passed, 1 ignored.
  • cargo clippy -p victory-gpu --all-targets, with and without diagnostics: no warning names this code.
  • cargo xtask policy passes on a clean checkout of the commit.
  • cargo fmt --check already fails on d2a4bae in files this change does not touch. The lines it adds are formatted.

Not fixed

The device is still removed (DXGI_ERROR_DEVICE_HUNG) about 100 to 140 s into the world, with and without this change. With the state errors gone, the debug layer reports nothing else before the removal, so the hang has another cause. The world is still black.

I did not measure the cost. Each buffer a list uses adds one barrier at its first use and one when the list closes. Frame times in the world were 0.7 to 1.1 s both before and after, but that is not a benchmark.

Checklist

  • cargo fmt, cargo clippy --workspace --all-targets and cargo test --workspace pass. (Only -p victory-gpu was run; fmt --check fails on main already.)
  • cargo xtask policy passes. The change adds no game files or data taken from them.
  • An AI tool wrote part of this change: Claude Code (Claude Sonnet 5.5) helped write the change and the description, and a person ran and checked it on hardware.

🤖 Generated with Claude Code

A title buffer's host copy that a shader writes is promoted to the
unordered access state, which does not decay back to the common state.
Reading it later as vertex, index or constant data without a barrier is
invalid (the debug layer reports it, and NVIDIA GPUs hang), and the
indirect passes moved the same buffers with hard-coded before states
that did not match what the list had left them in.

Every change of state now goes through HostList::resource_state, which
remembers the state the list left each buffer in, so a barrier always
names it. Shadow::refresh and Shadow::gpu_writes request the read or the
write state, the argument clamp and its `done` use the same call, and a
list ends with the buffers it moved in the common state again.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@MrEvilGamer

Copy link
Copy Markdown
Author

Closing this as a duplicate of #334, which was opened first and finds the same cause: the host copies of title buffers are promoted to unordered access, never come back to the common state, and the indirect passes use fixed barrier states, so the debug layer reports a vertex buffer in the wrong state (ids 527 and 538) on NVIDIA.

What I measured agrees with the results already reported on #334: with the tracker the state errors in the world went from 33 and 23 to 0, and the device is still removed (DXGI_ERROR_DEVICE_HUNG) about 100 to 140 s into the world, so the hang has another cause. Please review #334 rather than this one.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Review note. Built on main (d2a4bae), cargo test -p victory-gpu passes on an RTX 5070 (no debug layer on this machine, so state errors would not show here). This does the same job as #334 and the two conflict in host.rs and indirect.rs, so only one can go in.

The same case I noted on #334 applies here: within one command a write can lose to a read. bind handles tables before root views; for a dispatch that writes a host copy through a table UAV and reads the same copy through a root SRV, gpu_writes moves it to UNORDERED_ACCESS and the root view's refresh then moves it back to a read state, so the dispatch writes it while it is in a read state and no barrier separates those writes from the next vertex or index read.

One difference from #334: here refresh always transitions to read, so every UAV use becomes READ then UAV, one extra barrier per use. With thousands of draws per frame that is worth measuring.

From synthetic tests and reading only; I have no copy of the title.

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.

2 participants