GPU: track direct buffer states in HostList and restore COPY_DEST to COMMON in place_copy (#350) #424

Open
github-bot wants to merge 2 commits from github-bot/gh-424 into a-publier AGit
Owner

@⁠BE29ZZ on GitHub · 2026-11-27 00:53 UTC · original
BE29ZZ:fix/issue-350-buffer-state → a-publier · +837 −27 · 5 files · 2 commits · open

Fixes #350.

Root cause

Card-side title buffers (Shadow::buffer) start in D3D12_RESOURCE_STATE_COMMON and are expected to stay in COMMON (or read-promoted) between operations and lists of a submission:

  1. HostList::place_copy (crates/victory-gpu/src/host/place.rs) records CopyBufferRegion(&to.buffer, ...) (implicitly promoting to.buffer from COMMON to COPY_DEST) and follows it only with wait_for_all, leaving to.buffer in COPY_DEST. It also calls to.gpu_writes(...), so card_before appends plan.check (an unnamed list from make_prelude) to the same ExecuteCommandLists batch, where plan.check binds to.buffer with SetComputeRootShaderResourceView (host.rs) while it is still promoted to COPY_DEST — producing D3D12 Debug Layer error ID 538 (Severity 1). Any later root view or indirect clamp in the same list/batch hits the same violation.
  2. In place_copy's same-buffer branch (from.buffer == to.buffer), copying from -> between promotes the buffer to COPY_SOURCE, and copying between -> to then wrote to the same buffer without transitioning COPY_SOURCE -> COPY_DEST.
  3. Simply adding an unconditional COPY_DEST -> COMMON after place_copy would break whenever the buffer had already been promoted to a read (or write) state earlier in the same HostList or an earlier HostList of the same ExecuteCommandLists batch.

Changes

  • crates/victory-gpu/src/host/buffers.rs (new module): per-HostList D3D12 direct buffer state tracker (Track, LiveState, HostList::buffer_read / buffer_write / buffer_*_barrier / prelude_buffer_barriers, and check_barriers):
    • Tracks read-only implicit promotions (combining read bits without barriers), exclusive writes (COPY_DEST / UNORDERED_ACCESS), and explicit -> COMMON resets.
    • Pure read-only lists carry zero extra barriers; cross-list reads in the same ExecuteCommandLists merge without prelude transitions.
    • When a later list in the same ExecuteCommandLists batch writes a buffer (or emits an internal barrier whose StateBefore assumed entry in COMMON), HostList::prelude bridges prev -> COMMON first; live_buffers resets cleanly at ExecuteCommandLists boundaries (parts cuts and VICTORY_GPU_TIMING).
    • Before plan.check dispatches, check_barriers transitions any compared shadow buffer left in an incompatible non-read state (e.g. a direct root UAV) to NON_PIXEL_SHADER_RESOURCE.
  • crates/victory-gpu/src/host/place.rs:
    • place_copy allocates between before mutating state, pre-transitions from/to when already in an incompatible state, transitions from == to through COPY_SOURCE -> COPY_DEST -> COMMON, and restores both to (COPY_DEST -> COMMON) and from (-> COMMON) after copying.
    • place_source tracks COPY_SOURCE and restores from.buffer to COMMON alongside between's transition in one ResourceBarrier call.
  • crates/victory-gpu/src/host.rs: wires buffer_states into HostList (make_list, reset, untouched, prelude) and live_buffers into Host::submit and Host::card_before.
  • crates/victory-gpu/src/indirect.rs & crates/victory-gpu/src/draw.rs: records direct buffer promotions (indirect::clamp_into, indirect::open_batch, IASetVertexBuffers, IASetIndexBuffer, direct root CBV/SRV/UAVs) on HostList only when the draw/dispatch actually runs, so place_copy and cross-list preludes have the exact StateBefore mask.

Verification (Windows 11 x64, RTX 3060, Rust 1.98.1, MSVC)

  • cargo clippy --workspace --all-targets --locked -- -D warnings — passed (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — passed (0 warnings)
  • cargo test -p victory-gpu --lib --locked -- --test-threads=1 — 402 passed, 0 failed (34 ignored), including 5 new host::buffers::tests state-machine tests and the new end-to-end GPU test host::place::tests::place_copy_restores_buffer_to_common_and_runs_cleanly
  • cargo test -p victory-gpu --lib --all-features --locked host::buffers & host::place — 6 passed, 0 failed
  • cargo test -p xtask --locked — 9 passed, 0 failed (including repository_passes_policy)

(Note: this machine does not have Windows Graphics Tools / d3d12SDKLayers.dll installed, so live VICTORY_D3D12_DEBUG=1 verification against the title is still welcome from a machine with the debug layer enabled; place_copy_restores_buffer_to_common_and_runs_cleanly also asserts zero host.log_messages() when VICTORY_D3D12_DEBUG=1 is active.)

**@⁠BE29ZZ** on GitHub · 2026-11-27 00:53 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/424) `BE29ZZ:fix/issue-350-buffer-state` → `a-publier` · +837 −27 · 5 files · 2 commits · open Fixes #350. ### Root cause Card-side title buffers (`Shadow::buffer`) start in `D3D12_RESOURCE_STATE_COMMON` and are expected to stay in `COMMON` (or read-promoted) between operations and lists of a submission: 1. `HostList::place_copy` (`crates/victory-gpu/src/host/place.rs`) records `CopyBufferRegion(&to.buffer, ...)` (implicitly promoting `to.buffer` from `COMMON` to `COPY_DEST`) and follows it only with `wait_for_all`, leaving `to.buffer` in `COPY_DEST`. It also calls `to.gpu_writes(...)`, so `card_before` appends `plan.check` (an unnamed list from `make_prelude`) to the **same** `ExecuteCommandLists` batch, where `plan.check` binds `to.buffer` with `SetComputeRootShaderResourceView` (`host.rs`) while it is still promoted to `COPY_DEST` — producing D3D12 Debug Layer error ID 538 (Severity 1). Any later root view or indirect clamp in the same list/batch hits the same violation. 2. In `place_copy`'s same-buffer branch (`from.buffer == to.buffer`), copying `from -> between` promotes the buffer to `COPY_SOURCE`, and copying `between -> to` then wrote to the same buffer without transitioning `COPY_SOURCE -> COPY_DEST`. 3. Simply adding an unconditional `COPY_DEST -> COMMON` after `place_copy` would break whenever the buffer had already been promoted to a read (or write) state earlier in the same `HostList` or an earlier `HostList` of the same `ExecuteCommandLists` batch. ### Changes - **`crates/victory-gpu/src/host/buffers.rs`** (new module): per-`HostList` D3D12 direct buffer state tracker (`Track`, `LiveState`, `HostList::buffer_read` / `buffer_write` / `buffer_*_barrier` / `prelude_buffer_barriers`, and `check_barriers`): - Tracks read-only implicit promotions (combining read bits without barriers), exclusive writes (`COPY_DEST` / `UNORDERED_ACCESS`), and explicit `-> COMMON` resets. - Pure read-only lists carry zero extra barriers; cross-list reads in the same `ExecuteCommandLists` merge without prelude transitions. - When a later list in the same `ExecuteCommandLists` batch writes a buffer (or emits an internal barrier whose `StateBefore` assumed entry in `COMMON`), `HostList::prelude` bridges `prev -> COMMON` first; `live_buffers` resets cleanly at `ExecuteCommandLists` boundaries (`parts` cuts and `VICTORY_GPU_TIMING`). - Before `plan.check` dispatches, `check_barriers` transitions any compared shadow buffer left in an incompatible non-read state (e.g. a direct root UAV) to `NON_PIXEL_SHADER_RESOURCE`. - **`crates/victory-gpu/src/host/place.rs`**: - `place_copy` allocates `between` before mutating state, pre-transitions `from`/`to` when already in an incompatible state, transitions `from == to` through `COPY_SOURCE -> COPY_DEST -> COMMON`, and restores both `to` (`COPY_DEST -> COMMON`) and `from` (`-> COMMON`) after copying. - `place_source` tracks `COPY_SOURCE` and restores `from.buffer` to `COMMON` alongside `between`'s transition in one `ResourceBarrier` call. - **`crates/victory-gpu/src/host.rs`**: wires `buffer_states` into `HostList` (`make_list`, `reset`, `untouched`, `prelude`) and `live_buffers` into `Host::submit` and `Host::card_before`. - **`crates/victory-gpu/src/indirect.rs`** & **`crates/victory-gpu/src/draw.rs`**: records direct buffer promotions (`indirect::clamp_into`, `indirect::open_batch`, `IASetVertexBuffers`, `IASetIndexBuffer`, direct root CBV/SRV/UAVs) on `HostList` only when the draw/dispatch actually runs, so `place_copy` and cross-list preludes have the exact `StateBefore` mask. ### Verification (Windows 11 x64, RTX 3060, Rust `1.98.1`, MSVC) - `cargo clippy --workspace --all-targets --locked -- -D warnings` — passed (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` — passed (0 warnings) - `cargo test -p victory-gpu --lib --locked -- --test-threads=1` — **402 passed, 0 failed** (34 ignored), including 5 new `host::buffers::tests` state-machine tests and the new end-to-end GPU test `host::place::tests::place_copy_restores_buffer_to_common_and_runs_cleanly` - `cargo test -p victory-gpu --lib --all-features --locked host::buffers` & `host::place` — 6 passed, 0 failed - `cargo test -p xtask --locked` — 9 passed, 0 failed (including `repository_passes_policy`) *(Note: this machine does not have Windows Graphics Tools / `d3d12SDKLayers.dll` installed, so live `VICTORY_D3D12_DEBUG=1` verification against the title is still welcome from a machine with the debug layer enabled; `place_copy_restores_buffer_to_common_and_runs_cleanly` also asserts zero `host.log_messages()` when `VICTORY_D3D12_DEBUG=1` is active.)*
- Add per-HostList buffer state tracking (host/buffers.rs) covering direct buffer reads/writes in place_copy, place_source, indirect clamps, and draw bindings
- Transition place_copy destination buffers (COPY_DEST -> COMMON) and source buffers (including same-buffer COPY_SOURCE -> COPY_DEST -> COMMON) with exact pre-transition StateBefore when already promoted earlier in the list
- Bridge cross-list non-COMMON buffer states in HostList::prelude and before plan.check within the same ExecuteCommandLists batch (resetting on ExecuteCommandLists cut/timing boundaries)
- Add unit tests for the state machine and an end-to-end GPU test in host/place.rs
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:59:46 +00:00
Incorporates MamaLeVrai's ignored debug-layer test (copies_in_place_leave_their_buffers_to_root_views from #425) using ID3D12InfoQueue1::RegisterMessageCallback across PC-memory (256 KiB) and card-memory (8 MiB) buffers, and adds a 5th case covering an earlier root SRV read before place_copy in the same list.

Co-authored-by: MamaLeVrai <[email protected]>
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-27 01:37 UTC · original

Independent check on an RDNA 2 card (RX 6950 XT, driver 32.0.21045.29003, Windows 11 build 26300, release), at 91192d2. I had opened #425 for the same issue a few minutes after you, with only the COMMON reset. Yours goes further (the per-list buffer state tracking covers the earlier-read case mine left open), so I'll close mine in favor of this one.

My #425 had a debug-layer test you may want. host::place::tests::copies_in_place_leave_their_buffers_to_root_views is ignored, and wants --features diagnostics and VICTORY_D3D12_DEBUG=1. It listens with ID3D12InfoQueue1::RegisterMessageCallback, because the host's submission empties the message queue. It runs 4 cases (copy then root SRV, copy then the card compare, the same-buffer copy, ...) for a 256 KiB host copy in PC memory and an 8 MiB one on the card. Results:

  • on a-publier 4eb6cfd: 7 of 8 cases get layer messages (id 538 among them);
  • on this PR, with the test dropped into host/place.rs's tests: 0 messages in all 8 cases, and the compare ran in each card case.

cargo test --release -p victory-gpu --lib on this PR, twice: 402 passed, 0 failed, 34 ignored.

The test is at https://github.com/MamaLeVrai/VIctory-Belobug-AMD/blob/copy-dest-root-srv/crates/victory-gpu/src/host/place.rs (the tests module: title_of, listen, messages, out_gpu and the test). Feel free to take it.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-27 01:37 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/424#issuecomment-7207444688) Independent check on an RDNA 2 card (RX 6950 XT, driver 32.0.21045.29003, Windows 11 build 26300, release), at `91192d2`. I had opened #425 for the same issue a few minutes after you, with only the COMMON reset. Yours goes further (the per-list buffer state tracking covers the earlier-read case mine left open), so I'll close mine in favor of this one. My #425 had a debug-layer test you may want. `host::place::tests::copies_in_place_leave_their_buffers_to_root_views` is ignored, and wants `--features diagnostics` and `VICTORY_D3D12_DEBUG=1`. It listens with `ID3D12InfoQueue1::RegisterMessageCallback`, because the host's submission empties the message queue. It runs 4 cases (copy then root SRV, copy then the card compare, the same-buffer copy, ...) for a 256 KiB host copy in PC memory and an 8 MiB one on the card. Results: - on `a-publier` `4eb6cfd`: 7 of 8 cases get layer messages (id 538 among them); - on this PR, with the test dropped into `host/place.rs`'s tests: **0 messages in all 8 cases**, and the compare ran in each card case. `cargo test --release -p victory-gpu --lib` on this PR, twice: 402 passed, 0 failed, 34 ignored. The test is at https://github.com/MamaLeVrai/VIctory-Belobug-AMD/blob/copy-dest-root-srv/crates/victory-gpu/src/host/place.rs (the `tests` module: `title_of`, `listen`, `messages`, `out_gpu` and the test). Feel free to take it. AI-assisted (Claude Code); no game data.
Author
Owner

@⁠BE29ZZ on GitHub · 2026-11-27 01:52 UTC · original

Thank you @⁠MamaLeVrai for the independent verification on your RX 6950 XT with the D3D12 debug layer (0 messages across all 8 cases!) and for sharing the test!

I've incorporated copies_in_place_leave_their_buffers_to_root_views into crates/victory-gpu/src/host/place.rs (with Co-authored-by credit) and added a 5th case (case 4) covering an earlier root SRV read of the destination buffer before place_copy in the same list. Re-verified locally with both cargo clippy passes (--all-targets and --all-features -- -D warnings), host::buffers, host::place, and xtask policy tests — all green.

**@⁠BE29ZZ** on GitHub · 2026-11-27 01:52 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/424#issuecomment-7207648173) Thank you @⁠MamaLeVrai for the independent verification on your RX 6950 XT with the D3D12 debug layer (0 messages across all 8 cases!) and for sharing the test! I've incorporated `copies_in_place_leave_their_buffers_to_root_views` into `crates/victory-gpu/src/host/place.rs` (with `Co-authored-by` credit) and added a 5th case (`case 4`) covering an earlier root SRV read of the destination buffer before `place_copy` in the same list. Re-verified locally with both `cargo clippy` passes (`--all-targets` and `--all-features -- -D warnings`), `host::buffers`, `host::place`, and `xtask` policy tests — all green.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-27 06:49 UTC · original

Tested on RX 7900 XT (RDNA 3), driver 32.0.32029.2010, Windows 11 26300, Rust 1.98.1. Head 7f42a52 vs base a-publier 4eb6cfd (0 behind). Lib tests built with the dev test profile and with --profile diag --features diagnostics. No debug layer on this machine.

  • cargo test -p victory-gpu --lib, dev, 2 runs each side: base 396 passed / 34 ignored, head 402 passed / 35 ignored. 0 failed on both sides.
  • Diag profile, 1 run each side: base 444 passed / 1 failed, head 449 passed / 2 failed.
    • timeline::report::tests::real_waits_are_recorded_with_who_ended_them fails on base too.
    • watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view failed once on head in the suite. It passed 3/3 alone on each side. It is the known flaky watch:: test.
  • New tests on head, run alone:
    • host::buffers::tests: 5/5 passed, 3 runs.
    • place_copy_restores_buffer_to_common_and_runs_cleanly: passed 3/3 on dev, 3/3 on diag, and 1/1 with VICTORY_CARD_ALL=1. The destination is a real copy here, not in place, so the test does not return early.
  • Fail-without-fix: on 4eb6cfd I ran the new GPU test without the parts that need the PR's code (the tracker state asserts, buffer_read and prelude_buffer_barriers). The same place_copy calls and byte checks stayed. It passes 4/4 there, VICTORY_CARD_ALL=1 included. So on this card the copied bytes are right without the fix. Without the debug layer, this test catches a regression only through its tracker asserts.

Not tested: the debug-layer test (no d3d12SDKLayers here), release profile, the game.

**@⁠adrianmjim** on GitHub · 2026-11-27 06:49 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/424#issuecomment-7211881990) Tested on RX 7900 XT (RDNA 3), driver 32.0.32029.2010, Windows 11 26300, Rust 1.98.1. Head `7f42a52` vs base `a-publier` `4eb6cfd` (0 behind). Lib tests built with the dev test profile and with `--profile diag --features diagnostics`. No debug layer on this machine. - `cargo test -p victory-gpu --lib`, dev, 2 runs each side: base 396 passed / 34 ignored, head 402 passed / 35 ignored. 0 failed on both sides. - Diag profile, 1 run each side: base 444 passed / 1 failed, head 449 passed / 2 failed. - `timeline::report::tests::real_waits_are_recorded_with_who_ended_them` fails on base too. - `watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view` failed once on head in the suite. It passed 3/3 alone on each side. It is the known flaky `watch::` test. - New tests on head, run alone: - `host::buffers::tests`: 5/5 passed, 3 runs. - `place_copy_restores_buffer_to_common_and_runs_cleanly`: passed 3/3 on dev, 3/3 on diag, and 1/1 with `VICTORY_CARD_ALL=1`. The destination is a real copy here, not in place, so the test does not return early. - Fail-without-fix: on `4eb6cfd` I ran the new GPU test without the parts that need the PR's code (the tracker state asserts, `buffer_read` and `prelude_buffer_barriers`). The same `place_copy` calls and byte checks stayed. It passes 4/4 there, `VICTORY_CARD_ALL=1` included. So on this card the copied bytes are right without the fix. Without the debug layer, this test catches a regression only through its tracker asserts. Not tested: the debug-layer test (no d3d12SDKLayers here), release profile, the game.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/424/head:github-bot/gh-424'
git switch 'github-bot/gh-424'

Merge

Merge the changes and update on Forgejo.
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-424'
git switch 'github-bot/gh-424'
git rebase 'a-publier'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-424'
git switch 'github-bot/gh-424'
git rebase 'a-publier'
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-424'
git switch 'a-publier'
git merge --squash 'github-bot/gh-424'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-424'
git switch 'a-publier'
git merge 'github-bot/gh-424'
git push origin 'a-publier'
This discussion has been locked. Commenting is limited to contributors.
No description provided.