GPU: share readback chunks between a list's texture read backs #2816

Open
Therecalloftheonion wants to merge 1 commit from upstream-pr/2818 into main
Therecalloftheonion commented 2026-11-25 21:26:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Therecalloftheonion on 2026-11-25T21:26:06Z.


What

HostList::readback made a mapped readback buffer for every texture-to-buffer copy and kept it until the list was reset, so every read back paid CreateCommittedResource and Map on the CPU.

readback_space now carves 512-byte-aligned space from per-list readback chunks (64 KiB to 4 MiB, grown on demand). The copy's placed footprint names that offset, and the chunks are reused when the list resets. A read back larger than a chunk still gets a buffer of its own. The 512-byte alignment is what D3D12 requires for a placed texture footprint.

This is the readback half only. Upload pooling is already covered by open PRs #1382 and #2017, so those paths are untouched.

Benchmark

2000 x 4 KiB readback spaces, release build, RX 6700 XT:

time
one readback buffer per copy 335.4 ms
shared readback chunks 1.57 ms

cargo test --release -p victory-gpu benchmark_readback_spaces -- --ignored --nocapture.

This measures the driver allocation/Map cost removed, not game FPS. Readback memory is system memory, so this is CPU/driver overhead, not VRAM.

Tests

  • readback_spaces_share_chunks_and_are_reused: two read backs share one chunk at 512-byte-aligned offsets; the same space comes back after reset.
  • texture_readback_through_shared_chunks_round_trips_texels: a real texture-to-buffer copy through the chunk, every texel checked.
  • cargo test -p victory-gpu --lib: 195 passed, 2 ignored. Two unrelated upstream tests on this machine are filtered because they fail on main too: the 2^21 view-heap assertion and the copy-pair placement test.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6614074462 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2818). Originally posted by [Therecalloftheonion](https://github.com/Therecalloftheonion) on 2026-11-25T21:26:06Z. --- ## What `HostList::readback` made a mapped readback buffer for every texture-to-buffer copy and kept it until the list was reset, so every read back paid `CreateCommittedResource` and `Map` on the CPU. `readback_space` now carves **512-byte-aligned** space from per-list readback chunks (64 KiB to 4 MiB, grown on demand). The copy's placed footprint names that offset, and the chunks are reused when the list resets. A read back larger than a chunk still gets a buffer of its own. The 512-byte alignment is what D3D12 requires for a placed texture footprint. This is the readback half only. Upload pooling is already covered by open PRs #1382 and #2017, so those paths are untouched. ## Benchmark 2000 x 4 KiB readback spaces, release build, RX 6700 XT: | | time | |---|---| | one readback buffer per copy | 335.4 ms | | shared readback chunks | **1.57 ms** | `cargo test --release -p victory-gpu benchmark_readback_spaces -- --ignored --nocapture`. This measures the driver allocation/Map cost removed, not game FPS. Readback memory is system memory, so this is CPU/driver overhead, not VRAM. ## Tests - `readback_spaces_share_chunks_and_are_reused`: two read backs share one chunk at 512-byte-aligned offsets; the same space comes back after reset. - `texture_readback_through_shared_chunks_round_trips_texels`: a real texture-to-buffer copy through the chunk, every texel checked. - `cargo test -p victory-gpu --lib`: 195 passed, 2 ignored. Two unrelated upstream tests on this machine are filtered because they fail on `main` too: the `2^21` view-heap assertion and the copy-pair placement test. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean.
kotekompotek commented 2026-11-25 22:04:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T21:58:49Z.

Review: COMMENTED


Sound: copies recorded into one list can't execute concurrently, so sequential bump-allocation into shared mapped chunks is safe; 512-alignment keeps placed-footprint copies legal, doubling chunk growth bounds allocation count, and the reuse-after-reset path is covered by the round-trip test. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065560738 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2818#pullrequestreview-6065560738). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T21:58:49Z. **Review:** COMMENTED --- Sound: copies recorded into one list can't execute concurrently, so sequential bump-allocation into shared mapped chunks is safe; 512-alignment keeps placed-footprint copies legal, doubling chunk growth bounds allocation count, and the reuse-after-reset path is covered by the round-trip test. LGTM.
fuzeofficlal commented 2026-11-25 22:04:31 +00:00 (Migrated from github.com)

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


Verified e453bcaa on Windows 11 (RTX 5090 + WARP, --features diagnostics):

  • cargo xtask policy: 184 files ok; cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default & --features diagnostics): 0 warnings.
  • --release benchmark (benchmark_readback_spaces): 2000 x 4096 B readback spaces: one buffer each 503.022ms, shared chunks 1.624ms (~310× speedup).
  • Functional tests:
    • RTX 5090 (--features diagnostics --test-threads=1): both new tests (readback_spaces_share_chunks_and_are_reused and texture_readback_through_shared_chunks_round_trips_texels) plus draw::tests (11/11) passed; host/descriptor::host/pipeline::host 27/28 passed (only pre-existing >1M heap-limit dynamic_slots_are_held_until_released).
    • WARP (VICTORY_D3D12_WARP=1, --features diagnostics --test-threads=1): 200 passed, 0 failed, 2 ignored.
  • Code review: 512-byte placement alignment (len.max(1)), > SCRATCH_CHUNK fallback retaining buffer lifetime in deferred().uploads, single offset accounting (footprint.offset = offset with data = chunk.mapped + at), and reset reuse on HostList::reset() all check out. Clean git merge-tree against origin/main 13f91ca and #2608.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7175402310 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2818#issuecomment-7175402310). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T21:40:58Z. --- Verified `e453bcaa` on Windows 11 (RTX 5090 + WARP, `--features diagnostics`): - `cargo xtask policy`: 184 files ok; `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (default & `--features diagnostics`): 0 warnings. - `--release` benchmark (`benchmark_readback_spaces`): `2000 x 4096 B readback spaces: one buffer each 503.022ms, shared chunks 1.624ms` (**~310×** speedup). - Functional tests: - RTX 5090 (`--features diagnostics --test-threads=1`): both new tests (`readback_spaces_share_chunks_and_are_reused` and `texture_readback_through_shared_chunks_round_trips_texels`) plus `draw::tests` (11/11) passed; `host`/`descriptor::host`/`pipeline::host` 27/28 passed (only pre-existing >1M heap-limit `dynamic_slots_are_held_until_released`). - WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics --test-threads=1`): 200 passed, 0 failed, 2 ignored. - Code review: 512-byte placement alignment (`len.max(1)`), `> SCRATCH_CHUNK` fallback retaining buffer lifetime in `deferred().uploads`, single offset accounting (`footprint.offset = offset` with `data = chunk.mapped + at`), and reset reuse on `HostList::reset()` all check out. Clean `git merge-tree` against `origin/main` `13f91ca` and #2608.
AwsAli05 commented 2026-11-26 00:02:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AwsAli05 on 2026-11-25T23:08:04Z.

Review: COMMENTED


The texel round-trip currently uses the first reservation in the list, so rat is zero. This leaves the new nonzero base-offset path untested. If a reservation comes before the copy, the test's rdata.add(rat + ...) would also count the offset twice, because readback_space already returns a CPU pointer to that reservation.

Please reserve an initial region (or record a first texture copy), assert the next reservation has a nonzero offset, and read its texels with rdata.add(y * pitch + x). That would exercise exactly the difference between the D3D12 footprint offset and the mapped CPU pointer that this optimization introduces.

AI-assisted static review.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066217045 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2818#pullrequestreview-6066217045). Originally posted by [AwsAli05](https://github.com/AwsAli05) on 2026-11-25T23:08:04Z. **Review:** COMMENTED --- The texel round-trip currently uses the first reservation in the list, so `rat` is zero. This leaves the new nonzero base-offset path untested. If a reservation comes before the copy, the test's `rdata.add(rat + ...)` would also count the offset twice, because `readback_space` already returns a CPU pointer to that reservation. Please reserve an initial region (or record a first texture copy), assert the next reservation has a nonzero offset, and read its texels with `rdata.add(y * pitch + x)`. That would exercise exactly the difference between the D3D12 footprint offset and the mapped CPU pointer that this optimization introduces. AI-assisted static review.
Dukestar69 commented 2026-11-26 03:04:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T02:47:06Z.

Review: APPROVED


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

  • cargo test -p victory-gpu --lib readback: 2 passed, 0 failed, 1 ignored (readback_spaces_share_chunks_and_are_reused and texture_readback_through_shared_chunks_round_trips_texels).
  • 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:6067878362 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2818#pullrequestreview-6067878362). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T02:47:06Z. **Review:** APPROVED --- Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090): - `cargo test -p victory-gpu --lib readback`: **2 passed, 0 failed, 1 ignored** (`readback_spaces_share_chunks_and_are_reused` and `texture_readback_through_shared_chunks_round_trips_texels`). - `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).
Marvisak commented 2026-11-26 03:05:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T02:21:04Z.


Automated triage for the maintainers.

HostList::readback_space (host.rs) gives texture-to-buffer read backs 512-byte-aligned space in per-list readback chunks, reused after the list resets, instead of a committed buffer per copy. Copies larger than a chunk still get their own buffer. list.rs::read_back passes the offset into the placed footprint. The memory lives exactly as long as before (until reset). 512 bytes is D3D12's placement alignment for texture footprints. The tests include a real texture round trip through the chunk.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7180719658 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2818#issuecomment-7180719658). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T02:21:04Z. --- <!-- victory-triage v1 head=e453bcaa0c2e6f51a7f49d5fa634a8cf767017b5 verdict=merge-candidate at=2026-11-26T02:20:58Z --> Automated triage for the maintainers. `HostList::readback_space` (`host.rs`) gives texture-to-buffer read backs 512-byte-aligned space in per-list readback chunks, reused after the list resets, instead of a committed buffer per copy. Copies larger than a chunk still get their own buffer. `list.rs::read_back` passes the offset into the placed footprint. The memory lives exactly as long as before (until reset). 512 bytes is D3D12's placement alignment for texture footprints. The tests include a real texture round trip through the chunk. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-26 12:05:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T11:56:29Z.

Review: APPROVED


Reviewed and verified at head e453bcaa against origin/main (63bcca43) on Windows 11 (RTX 5090 + WARP, D3D12 Debug Layer):

  • Design & correctness:
    • Readbacks::allocate (crates/victory-gpu/src/host.rs:1093-1134) and HostList::readback_space (host.rs:2348-2367) enforce D3D12's 512-byte placed-footprint alignment via len.max(1).checked_next_multiple_of(512)?, cap pooled chunks at SCRATCH_CHUNK (4 MiB) with doubling growth from 64 KiB, and fall back to a dedicated mapped_buffer retained in self.deferred().uploads for larger copies.
    • Persistent Map(0, None, ...) readback chunks live for the list's lifetime and rewind cleanly in HostList::reset() (host.rs:1964-1967) once GPU execution has completed.
    • list.rs::read_back (crates/victory-gpu/src/list.rs:856-868, 907-914) applies the chunk offset once on the GPU (to.footprint.offset = offset) and once on the CPU (data = chunk.mapped + at, plus relative row offsets in defer_after), while keeping HostList::is_empty() accurate via ensure_state and defer_after.
  • Non-blocking test nit: in texture_readback_through_shared_chunks_round_trips_texels (crates/victory-gpu/src/host.rs:747), *rdata.add(rat + y * pitch + x) adds rat twice because rdata already points to chunk.mapped + rat. It currently passes only because it is the first allocation (rat == 0), leaving the nonzero-offset GPU readback path uncovered in that test. Reserving an initial slice first, asserting rat > 0, and indexing *rdata.add(y * pitch + x) would harden the test cleanly.
  • Verification:
    • Clean git merge-tree against origin/main (63bcca43); cargo xtask policy (184 files ok); cargo clippy -p victory-gpu --all-targets -- -D warnings clean with default features and --all-features.
    • RTX 5090 (default): readback 2/2 passed (1 ignored benchmark), resource::tests::a_texture_read_back_reaches_the_title_buffer 1/1 passed, draw::tests 11/11 passed.
    • RTX 5090 + VICTORY_D3D12_DEBUG=1 (--features diagnostics) and WARP (VICTORY_D3D12_WARP=1) + VICTORY_D3D12_DEBUG=1: readback 2/2 and a_texture_read_back_reaches_the_title_buffer 1/1 passed on both with zero debug-layer errors.
    • Release benchmark on RTX 5090 (benchmark_readback_spaces): 2000 x 4096 B readback spaces: one buffer each 526.4619ms, shared chunks 1.371ms (384.0x speedup).

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6073720547 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2818#pullrequestreview-6073720547). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T11:56:29Z. **Review:** APPROVED --- Reviewed and verified at head `e453bcaa` against `origin/main` (`63bcca43`) on Windows 11 (RTX 5090 + WARP, D3D12 Debug Layer): - **Design & correctness**: - `Readbacks::allocate` (`crates/victory-gpu/src/host.rs:1093-1134`) and `HostList::readback_space` (`host.rs:2348-2367`) enforce D3D12's 512-byte placed-footprint alignment via `len.max(1).checked_next_multiple_of(512)?`, cap pooled chunks at `SCRATCH_CHUNK` (4 MiB) with doubling growth from 64 KiB, and fall back to a dedicated `mapped_buffer` retained in `self.deferred().uploads` for larger copies. - Persistent `Map(0, None, ...)` readback chunks live for the list's lifetime and rewind cleanly in `HostList::reset()` (`host.rs:1964-1967`) once GPU execution has completed. - `list.rs::read_back` (`crates/victory-gpu/src/list.rs:856-868, 907-914`) applies the chunk offset once on the GPU (`to.footprint.offset = offset`) and once on the CPU (`data = chunk.mapped + at`, plus relative row offsets in `defer_after`), while keeping `HostList::is_empty()` accurate via `ensure_state` and `defer_after`. - **Non-blocking test nit**: in `texture_readback_through_shared_chunks_round_trips_texels` (`crates/victory-gpu/src/host.rs:747`), `*rdata.add(rat + y * pitch + x)` adds `rat` twice because `rdata` already points to `chunk.mapped + rat`. It currently passes only because it is the first allocation (`rat == 0`), leaving the nonzero-offset GPU readback path uncovered in that test. Reserving an initial slice first, asserting `rat > 0`, and indexing `*rdata.add(y * pitch + x)` would harden the test cleanly. - **Verification**: - Clean `git merge-tree` against `origin/main` (`63bcca43`); `cargo xtask policy` (`184 files ok`); `cargo clippy -p victory-gpu --all-targets -- -D warnings` clean with default features and `--all-features`. - RTX 5090 (default): `readback` 2/2 passed (1 ignored benchmark), `resource::tests::a_texture_read_back_reaches_the_title_buffer` 1/1 passed, `draw::tests` 11/11 passed. - RTX 5090 + `VICTORY_D3D12_DEBUG=1` (`--features diagnostics`) and WARP (`VICTORY_D3D12_WARP=1`) + `VICTORY_D3D12_DEBUG=1`: `readback` 2/2 and `a_texture_read_back_reaches_the_title_buffer` 1/1 passed on both with zero debug-layer errors. - Release benchmark on RTX 5090 (`benchmark_readback_spaces`): `2000 x 4096 B readback spaces: one buffer each 526.4619ms, shared chunks 1.371ms` (**384.0x** speedup). LGTM.
pablogonz12 commented 2026-11-27 17:20:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pablogonz12 on 2026-11-27T17:20:01Z.


I tested e453bcaa merged onto 63bcca43 on an RX 9070 XT (RDNA4), Mesa RADV 26.3.1, GE-Proton11-14 and vkd3d-proton cae8353e.

  • The two readback tests passed with a hardware D3D12 device required (2 passed; the benchmark was ignored in that test run).
  • Release microbenchmark, 2,000 allocations of 4 KiB, one run: main's per-buffer control took 1,485.1 ms; the PR benchmark measured 1,225.2 ms for per-buffer allocation and 3.285 ms for shared chunks.

The benchmark releases each standalone buffer immediately, while chunks remain on the list; it measures this allocation loop rather than end-to-end submission performance. Unit tests and benchmark only, not a game run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7224288496 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2818#issuecomment-7224288496). Originally posted by [pablogonz12](https://github.com/pablogonz12) on 2026-11-27T17:20:01Z. --- I tested `e453bcaa` merged onto `63bcca43` on an RX 9070 XT (RDNA4), Mesa RADV 26.3.1, GE-Proton11-14 and vkd3d-proton `cae8353e`. - The two readback tests passed with a hardware D3D12 device required (2 passed; the benchmark was ignored in that test run). - Release microbenchmark, 2,000 allocations of 4 KiB, one run: main's per-buffer control took 1,485.1 ms; the PR benchmark measured 1,225.2 ms for per-buffer allocation and 3.285 ms for shared chunks. The benchmark releases each standalone buffer immediately, while chunks remain on the list; it measures this allocation loop rather than end-to-end submission performance. Unit tests and benchmark only, not a game run.
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/2818:upstream-pr/2818'
git switch 'upstream-pr/2818'

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/2818'
git switch 'upstream-pr/2818'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2818'
git switch 'upstream-pr/2818'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2818'
git switch 'main'
git merge --squash 'upstream-pr/2818'
git switch 'main'
git merge --ff-only 'upstream-pr/2818'
git switch 'main'
git merge 'upstream-pr/2818'
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!2816
No description provided.