GPU: split and flush command list segments after texture read-backs #2608

Open
github-bot wants to merge 1 commit from github-bot/gh-2608 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 16:45 UTC · original
fuzeofficlal:fix/gpu-readback-same-list-copy → main · +534 −28 · 5 files · 1 commits · open

Summary

list::read_back copies texture rows from host readback memory into the title's buffer inside HostList::defer_after, which runs in Host::execute after the host command lists finish on the GPU.

However, a subsequent CopyTextureRegion (buffer-to-texture via defer_fill), CopyBufferRegion / CopyResource (defer_copy), CopyMemoryToMemoryX, title_written texture upload, or draw/dispatch (Shadow::refresh / bindless::uploads) prepares its title-memory reads inside HostList::run_deferred before GPU execution. This causes two related hazards:

  1. Same command list: copying Texture A -> Buffer B (read_back), transitioning B, and copying Buffer B -> Texture C (or Buffer C) in one command list reads B's stale pre-readback bytes.
  2. Same ExecuteCommandLists batch: hold::Held::pump batches consecutive ready segments on the same queue into a single piece passed to one Host::execute(&lists) call, so a second command list reading B still runs run_deferred before the first list's defer_after writes B.

Changes

  • crates/victory-gpu/src/host.rs: track pending read-back guest ranges (read_backs: Vec<(usize, usize)>) in Deferred (cleared automatically on HostList::reset()), with note_read_back, has_read_backs, and reads_after_read_back.
  • crates/victory-gpu/src/hold.rs & crates/victory-gpu/src/queue.rs: add World::flush_after (&|s| s.has_read_backs() in queue::pump) so Held::pump flushes piece immediately after any segment with pending read-backs, letting its defer_after writes land before the next segment prepares its copies.
  • crates/victory-gpu/src/list.rs: record read-back row ranges in read_back, extract shared cut_segment, and cut onto a fresh HostList segment only when a subsequent copy overlaps pending read-back ranges or a subsequent draw/dispatch/title_written upload follows in the same list (consecutive independent read_backs do not split).
  • crates/victory-gpu/src/resource.rs & crates/victory-gpu/src/hold.rs: add unit tests covering same-list read_back -> copy_texture_region, same-list read_back -> copy_buffer_region, single-batch ExecuteCommandLists(2, [list_a, list_b]), and Held::pump flush_after piece splitting.

Verification

  • Negative control (before fix, hardware & WARP): both same-list read_back -> copy_texture_region and read_back -> copy_buffer_region fail reading sentinel 0x5A bytes instead of [1..=16] / [5..=20].
  • Hardware (RTX 5090, --test-threads=1): read_back (4/4), hold::tests (11/11), list::tests (4/4), draw::tests (11/11), resource::tests (20/20) pass.
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics, --test-threads=1): full victory-gpu --lib suite passes (202 passed; 0 failed; 1 ignored).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings and --features diagnostics — pass (0 warnings).
  • cargo xtask policy — pass (184 files ok).
**@⁠fuzeofficlal** on GitHub · 2026-11-25 16:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/2608) `fuzeofficlal:fix/gpu-readback-same-list-copy` → `main` · +534 −28 · 5 files · 1 commits · open ## Summary `list::read_back` copies texture rows from host readback memory into the title's buffer inside `HostList::defer_after`, which runs in `Host::execute` **after** the host command lists finish on the GPU. However, a subsequent `CopyTextureRegion` (buffer-to-texture via `defer_fill`), `CopyBufferRegion` / `CopyResource` (`defer_copy`), `CopyMemoryToMemoryX`, `title_written` texture upload, or draw/dispatch (`Shadow::refresh` / `bindless::uploads`) prepares its title-memory reads inside `HostList::run_deferred` **before** GPU execution. This causes two related hazards: 1. **Same command list**: copying Texture A -> Buffer B (`read_back`), transitioning B, and copying Buffer B -> Texture C (or Buffer C) in one command list reads B's stale pre-readback bytes. 2. **Same `ExecuteCommandLists` batch**: `hold::Held::pump` batches consecutive ready segments on the same queue into a single `piece` passed to one `Host::execute(&lists)` call, so a second command list reading B still runs `run_deferred` before the first list's `defer_after` writes B. ## Changes - `crates/victory-gpu/src/host.rs`: track pending read-back guest ranges (`read_backs: Vec<(usize, usize)>`) in `Deferred` (cleared automatically on `HostList::reset()`), with `note_read_back`, `has_read_backs`, and `reads_after_read_back`. - `crates/victory-gpu/src/hold.rs` & `crates/victory-gpu/src/queue.rs`: add `World::flush_after` (`&|s| s.has_read_backs()` in `queue::pump`) so `Held::pump` flushes `piece` immediately after any segment with pending read-backs, letting its `defer_after` writes land before the next segment prepares its copies. - `crates/victory-gpu/src/list.rs`: record read-back row ranges in `read_back`, extract shared `cut_segment`, and cut onto a fresh `HostList` segment only when a subsequent copy overlaps pending read-back ranges or a subsequent draw/dispatch/`title_written` upload follows in the same list (consecutive independent `read_back`s do not split). - `crates/victory-gpu/src/resource.rs` & `crates/victory-gpu/src/hold.rs`: add unit tests covering same-list `read_back -> copy_texture_region`, same-list `read_back -> copy_buffer_region`, single-batch `ExecuteCommandLists(2, [list_a, list_b])`, and `Held::pump` `flush_after` piece splitting. ## Verification - Negative control (before fix, hardware & WARP): both same-list `read_back -> copy_texture_region` and `read_back -> copy_buffer_region` fail reading sentinel `0x5A` bytes instead of `[1..=16]` / `[5..=20]`. - Hardware (RTX 5090, `--test-threads=1`): `read_back` (4/4), `hold::tests` (11/11), `list::tests` (4/4), `draw::tests` (11/11), `resource::tests` (20/20) pass. - WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics`, `--test-threads=1`): full `victory-gpu --lib` suite passes (`202 passed; 0 failed; 1 ignored`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings` and `--features diagnostics` — pass (0 warnings). - `cargo xtask policy` — pass (`184 files ok`).
A texture-to-buffer CopyTextureRegion (read_back) copies rows from host readback memory into title memory in HostList::defer_after once the host submission has finished. A later CopyTextureRegion (buffer-to-texture via defer_fill), CopyBufferRegion (defer_copy), CopyMemoryToMemoryX, title-written texture upload, or draw/dispatch in the same command list or same ExecuteCommandLists batch prepares its reads in run_deferred before GPU execution, reading stale title bytes from before the read-back.

Track pending read-back guest byte ranges on HostList, cut the recording title list onto a fresh HostList segment when a subsequent read/write overlaps those ranges (or a draw/dispatch/title-written texture upload follows in the same list), and flush the queue piece in hold::Held::pump right after any segment with pending read-backs so its defer_after writes land before the next segment prepares its copies.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:10:14 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 17:15 UTC · original

WARP verification of 5bf6464 merged cleanly onto origin/main 13f91ca (detached checkout).

Environment: Windows 11. No physical GPU device was created: VICTORY_D3D12_WARP=1, VICTORY_WARP=1, cargo -j 4, separate CARGO_TARGET_DIR.

Commands and results:

  • cargo clippy -j 4 -p victory-gpu --features diagnostics --all-targets: clean, no warnings.
  • cargo test -j 4 -p victory-gpu --features diagnostics --lib: 202 passed, 0 failed, 1 ignored (origin/main: 198 passed). The 4 new tests pass: hold::a_segment_with_flush_after_runs_before_the_next_segment and three resource::a_*read_back* tests.
  • The ignored test (benchmark_private_data_release) run with -- --ignored: 1 passed.
  • Negative control: reverting only list.rs to origin/main makes the three resource read-back tests fail (3 failed, 1 passed under the read_back filter).

Review notes:

  • Overlap with #2611 (hold.rs pump): textually clean and complementary. #2611 flushes the buffered piece before evaluating a segment's waits, and this PR flushes after a segment with read-backs. #2608 + #2611 together: 204 passed, 0 failed, 1 ignored.
  • Overlap with #2605: textually clean, but not compile-clean. This PR adds a required field World::flush_after, and #2605's new test an_unreadable_wait_holds_its_queue_until_readable builds World { read, fence, run, signal } without it. With both merged, cargo test fails with E0063. Whichever lands second needs flush_after: &|_| false there.
  • Edge I read but did not exercise: when next_segment returns None, cut_segment returns None, so the deferred copy or draw at that call site is skipped. Before, it was recorded on the current list. This only applies when a read-back is pending and no further segment is available.

Not tested: physical GPU or driver behaviour, or a retail title. WARP only.

Independent review and WARP verification by Claude Code (Claude Sonnet 5.5), an AI. No game files, dumps or retail data were used.

**@⁠oni-swr** on GitHub · 2026-11-25 17:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7169244866) **WARP verification** of `5bf6464` merged cleanly onto `origin/main` `13f91ca` (detached checkout). Environment: Windows 11. No physical GPU device was created: `VICTORY_D3D12_WARP=1`, `VICTORY_WARP=1`, `cargo -j 4`, separate `CARGO_TARGET_DIR`. Commands and results: - `cargo clippy -j 4 -p victory-gpu --features diagnostics --all-targets`: clean, no warnings. - `cargo test -j 4 -p victory-gpu --features diagnostics --lib`: **202 passed, 0 failed, 1 ignored** (`origin/main`: 198 passed). The 4 new tests pass: `hold::a_segment_with_flush_after_runs_before_the_next_segment` and three `resource::a_*read_back*` tests. - The ignored test (`benchmark_private_data_release`) run with `-- --ignored`: 1 passed. - Negative control: reverting only `list.rs` to `origin/main` makes the three `resource` read-back tests fail (3 failed, 1 passed under the `read_back` filter). Review notes: - Overlap with #2611 (`hold.rs` pump): textually clean and complementary. #2611 flushes the buffered piece before evaluating a segment's waits, and this PR flushes after a segment with read-backs. #2608 + #2611 together: 204 passed, 0 failed, 1 ignored. - Overlap with #2605: textually clean, but **not compile-clean**. This PR adds a required field `World::flush_after`, and #2605's new test `an_unreadable_wait_holds_its_queue_until_readable` builds `World { read, fence, run, signal }` without it. With both merged, `cargo test` fails with E0063. Whichever lands second needs `flush_after: &|_| false` there. - Edge I read but did not exercise: when `next_segment` returns `None`, `cut_segment` returns `None`, so the deferred copy or draw at that call site is skipped. Before, it was recorded on the current list. This only applies when a read-back is pending and no further segment is available. Not tested: physical GPU or driver behaviour, or a retail title. WARP only. _Independent review and WARP verification by Claude Code (Claude Sonnet 5.5), an AI. No game files, dumps or retail data were used._
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 17:50 UTC · original

Reviewed: flush_after ends the piece after a segment whose title-memory writes land after submission (read-backs) — the only way a following segment's wait can observe them. Complementary to #2611 (which flushes the buffered piece before evaluating waits); different failure, both needed. Test repro is clean. CI green.

**@⁠kotekompotek** on GitHub · 2026-11-25 17:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7170084830) Reviewed: `flush_after` ends the piece after a segment whose title-memory writes land *after* submission (read-backs) — the only way a following segment's wait can observe them. Complementary to #2611 (which flushes the buffered piece *before* evaluating waits); different failure, both needed. Test repro is clean. CI green.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 21:01 UTC · original

I reproduced #2781 by recording a real D3D12 GPU write and then invoking the guest CopyBufferRegion vtable entry. The GPU and title copies of the source both contain 0x55667788 after submission. The destination still reads the old 0x11223344 with main 13f91ca and with #2608 at 5bf6464 when both commands share a list or a submission. The separate-submission control reads the new value.

34fc326 is one commit on your exact 5bf646428c48f4e2cd31a4063597e1c87d3a3d12. It reuses the segmentation path for a deferred buffer copy whose source overlaps a prior declared GPU write. The queue submits the current piece before preparing a later list with the same dependency. The same fixture now reads 0x55667788 in all three layouts. The disjoint-range and copy-before-write controls retain their original values and stay in one segment. Surrounding words stay intact.

Six paired runs reproduced these results in both debug settings. The full serial GPU suite passed 203 tests with one ignored in each setting. Strict Clippy in both configurations and asset policy passed. If you prefer to keep this as a separate follow-up, the fork branch retains your exact PR head.

Please retain the Git author on cherry-pick or rebase. If you fold the fix or test into another commit, please add the following trailer.

Co-authored-by: andrewpozdnakov7-jpg [email protected]

AI-assisted with Codex. Synthetic GPU commands on WARP.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 21:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7174511045) I reproduced #2781 by recording a real D3D12 GPU write and then invoking the guest CopyBufferRegion vtable entry. The GPU and title copies of the source both contain 0x55667788 after submission. The destination still reads the old 0x11223344 with main 13f91ca and with #2608 at 5bf6464 when both commands share a list or a submission. The separate-submission control reads the new value. [34fc326](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/34fc326cdf19c7c286fc0413050640ff186c54f8) is one commit on your exact 5bf646428c48f4e2cd31a4063597e1c87d3a3d12. It reuses the segmentation path for a deferred buffer copy whose source overlaps a prior declared GPU write. The queue submits the current piece before preparing a later list with the same dependency. The same fixture now reads 0x55667788 in all three layouts. The disjoint-range and copy-before-write controls retain their original values and stay in one segment. Surrounding words stay intact. Six paired runs reproduced these results in both debug settings. The full serial GPU suite passed 203 tests with one ignored in each setting. Strict Clippy in both configurations and asset policy passed. If you prefer to keep this as a separate follow-up, the fork branch retains your exact PR head. Please retain the Git author on cherry-pick or rebase. If you fold the fix or test into another commit, please add the following trailer. Co-authored-by: andrewpozdnakov7-jpg <[email protected]> AI-assisted with Codex. Synthetic GPU commands on WARP.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 21:21 UTC · original

Thanks for the thorough reproduction and verification of #2781! Keeping #2608 scoped to texture read_back segmentation and tracking #2781 (deferred buffer copies after shader writes) as a separate follow-up (alongside #2810, which tackles #2781 in host.rs without splitting GPU submissions) keeps both changes clean and easy for maintainers to compare and review.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 21:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7174969406) Thanks for the thorough reproduction and verification of `#2781`! Keeping `#2608` scoped to texture `read_back` segmentation and tracking `#2781` (deferred buffer copies after shader writes) as a separate follow-up (alongside `#2810`, which tackles `#2781` in `host.rs` without splitting GPU submissions) keeps both changes clean and easy for maintainers to compare and review.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:20 UTC · original · edited 2026-11-26 00:24 UTC

Automated triage for the maintainers.

Recommended for a maintainer's look. read_back in crates/victory-gpu/src/list.rs writes title memory in defer_after, after the host lists run, while later copies, draws and dispatches in the same list or the same Held::pump piece read title memory before submission. The PR records read-back ranges in Deferred (note_read_back / reads_after_read_back in host.rs), cuts the segment where a later command could observe them, and ends the piece after such a segment via World::flush_after (hold.rs, set in queue::pump). New tests cover same-list texture and buffer copies, two lists in one submission, and the piece split; it merges cleanly and CI check is passing.

On your reply: confirmed. host_list_after_read_back, host_list_after_buffers and list_for_texture_copy now use cut_segment(this, current).unwrap_or(current), so a failed next_segment records the command on the current list instead of dropping it. Note for whoever merges #2605 later: its hold test builds World without flush_after and will need flush_after: &|_| false.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 23:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7177434194) · edited 2026-11-26 00:24 UTC <!-- victory-triage v1 head=40d72b9bd411a5c486f95ec981aed3e0abf6189c verdict=merge-candidate at=2026-11-26T00:24:19Z --> Automated triage for the maintainers. Recommended for a maintainer's look. `read_back` in `crates/victory-gpu/src/list.rs` writes title memory in `defer_after`, after the host lists run, while later copies, draws and dispatches in the same list or the same `Held::pump` piece read title memory before submission. The PR records read-back ranges in `Deferred` (`note_read_back` / `reads_after_read_back` in `host.rs`), cuts the segment where a later command could observe them, and ends the piece after such a segment via `World::flush_after` (`hold.rs`, set in `queue::pump`). New tests cover same-list texture and buffer copies, two lists in one submission, and the piece split; it merges cleanly and CI `check` is passing. On your reply: confirmed. `host_list_after_read_back`, `host_list_after_buffers` and `list_for_texture_copy` now use `cut_segment(this, current).unwrap_or(current)`, so a failed `next_segment` records the command on the current list instead of dropping it. Note for whoever merges #2605 later: its hold test builds `World` without `flush_after` and will need `flush_after: &|_| false`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 23:28 UTC · original

Rebased onto origin/main (842b0cb) and updated in 40d72b9b per triage feedback:

  • host_list_after_read_back, host_list_after_buffers, and list_for_texture_copy in crates/victory-gpu/src/list.rs now fall back to the current host list (cut_segment(this, current).unwrap_or(current)) when next_segment fails instead of dropping the subsequent copy/draw/dispatch command.
  • Noted on #2605's World literal (flush_after: &|_| false) if #2605 lands first.

Verified after rebase on Windows 11 (RTX 5090 + WARP): hold::tests (11/11), list::tests (4/4 on RTX 5090 and 4/4 on WARP), cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics), and cargo xtask policy (191 files ok).

**@⁠fuzeofficlal** on GitHub · 2026-11-25 23:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7177611220) Rebased onto `origin/main` (`842b0cb`) and updated in `40d72b9b` per triage feedback: - `host_list_after_read_back`, `host_list_after_buffers`, and `list_for_texture_copy` in `crates/victory-gpu/src/list.rs` now fall back to the current host list (`cut_segment(this, current).unwrap_or(current)`) when `next_segment` fails instead of dropping the subsequent copy/draw/dispatch command. - Noted on `#2605`'s `World` literal (`flush_after: &|_| false`) if `#2605` lands first. Verified after rebase on Windows 11 (RTX 5090 + WARP): `hold::tests` (11/11), `list::tests` (4/4 on RTX 5090 and 4/4 on WARP), `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`), and `cargo xtask policy` (`191 files ok`).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/2608/head:github-bot/gh-2608'
git switch 'github-bot/gh-2608'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2608'
git switch 'github-bot/gh-2608'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2608'
git switch 'github-bot/gh-2608'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2608'
git switch 'main'
git merge --squash 'github-bot/gh-2608'
git switch 'main'
git merge --ff-only 'github-bot/gh-2608'
git switch 'main'
git merge 'github-bot/gh-2608'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2608
No description provided.