Skip to content

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

Open
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-readback-same-list-copy
Open

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-readback-same-list-copy

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

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:

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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 5bf6464. 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 andrewpozdnakov7-jpg@users.noreply.github.com

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

@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

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 Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

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.
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

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
fuzeofficlal force-pushed the fix/gpu-readback-same-list-copy branch from 5bf6464 to 40d72b9 Compare November 25, 2026 23:28
Steenax6 added a commit to Steenax6/VIctory-NVID that referenced this pull request Nov 26, 2026
…er it; a step that reads what an earlier read-back of its list writes is said

A list notes each texture the first time a draw samples it, and records
its upload (or the check of it at submission) only then. A title memory
copy later in the same list, or other work that may write title memory,
can rewrite the texture's memory, and the draws after it read the texture
as the first check left it. A copy or such work now clears the note, so
the next draw that samples the texture records its check again, behind the
copy (Marvisak/VIctory pull request Marvisak#2297, its test with it).

A texture to buffer read-back lands in title memory once its list has run,
but the list's later copies, fills and buffer refreshes read title memory
at submission, before: they see the bytes from before the read-back
(Marvisak pull request Marvisak#2608 cuts the list into segments there, +534
lines in list.rs, queue.rs and a hold module we do not have). Not ported
until a run shows the title doing it: such a step is now counted and said
in the log (at the 1st, 2nd, 4th... time), which costs nothing for the
lists without read-backs.

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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants