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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/2608 into main
fuzeofficlal commented 2026-11-25 16:45:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T16:45:13Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6609233789 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2608). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T16:45:13Z. --- ## 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`).
oni-swr commented 2026-11-25 18:02:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T17:15:46Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169244866 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7169244866). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T17:15:46Z. --- **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._
kotekompotek commented 2026-11-25 18:02:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T17:50:56Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170084830 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7170084830). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T17:50:56Z. --- 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.
andrewpozdnakov7-jpg commented 2026-11-25 21:03:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T21:01:42Z.


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 5bf646428c. 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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7174511045 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7174511045). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T21:01:42Z. --- 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.
fuzeofficlal commented 2026-11-25 22:04:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T21:21:32Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7174969406 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7174969406). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T21:21:32Z. --- 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.
Marvisak commented 2026-11-26 00:03:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:20:43Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177434194 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7177434194). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T23:20:43Z. --- <!-- 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)_
fuzeofficlal commented 2026-11-26 00:03:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T23:28:33Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177611220 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2608#issuecomment-7177611220). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T23:28:33Z. --- 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`).
Dukestar69 commented 2026-11-26 06:02:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T05:16:10Z.

Review: APPROVED


Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • Diff review: Tracking pending read_back guest-memory ranges on HostList, cutting onto a fresh segment when subsequent copies/draws/dispatches in the same list would read those bytes, and flushing the piece via World::flush_after in Held::pump ensures texture read-backs complete before dependent work in the same list or batch executes.
  • cargo test -p victory-gpu hold::tests: 11 passed, 0 failed (including a_segment_with_flush_after_runs_before_the_next_segment).
  • cargo test -p victory-gpu resource::tests -- --test-threads=1 (RTX 5090): 21 passed, 0 failed (including all 3 new read-back-then-copy tests).
  • VICTORY_WARP=1 cargo test -p victory-gpu resource::tests -- --test-threads=1 (WARP): 21 passed, 0 failed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6068901352 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2608#pullrequestreview-6068901352). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T05:16:10Z. **Review:** APPROVED --- Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP): - Diff review: Tracking pending `read_back` guest-memory ranges on `HostList`, cutting onto a fresh segment when subsequent copies/draws/dispatches in the same list would read those bytes, and flushing the piece via `World::flush_after` in `Held::pump` ensures texture read-backs complete before dependent work in the same list or batch executes. - `cargo test -p victory-gpu hold::tests`: 11 passed, 0 failed (including `a_segment_with_flush_after_runs_before_the_next_segment`). - `cargo test -p victory-gpu resource::tests -- --test-threads=1` (RTX 5090): 21 passed, 0 failed (including all 3 new read-back-then-copy tests). - `VICTORY_WARP=1 cargo test -p victory-gpu resource::tests -- --test-threads=1` (WARP): 21 passed, 0 failed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2608:upstream-pr/2608'
git switch 'upstream-pr/2608'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2608'
git switch 'upstream-pr/2608'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2608'
git switch 'upstream-pr/2608'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2608'
git switch 'main'
git merge --squash 'upstream-pr/2608'
git switch 'main'
git merge --ff-only 'upstream-pr/2608'
git switch 'main'
git merge 'upstream-pr/2608'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2606
No description provided.