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

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

@⁠Therecalloftheonion on GitHub · 2026-11-25 21:26 UTC · original
Therecalloftheonion:pr/gpu-copy-arenas → main · +230 −10 · 2 files · 1 commits · open

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.
**@⁠Therecalloftheonion** on GitHub · 2026-11-25 21:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/2818) `Therecalloftheonion:pr/gpu-copy-arenas` → `main` · +230 −10 · 2 files · 1 commits · open ## 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.
HostList::readback made a mapped readback buffer for every texture-to-buffer copy and kept it until the list was reset, so each 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 placed footprint's offset names that space, and the chunks are reused when the list resets. Copies larger than a chunk still get a buffer of their own.

The 512-byte alignment is what D3D12 requires for a placed texture footprint; read backs of one list share the chunks instead of allocating one buffer per copy.

Benchmark (2000 x 4 KiB, release, RX 6700 XT): a readback buffer per copy 335.4 ms, shared chunks 1.57 ms.

Tests: readback_spaces_share_chunks_and_are_reused, texture_readback_through_shared_chunks_round_trips_texels (a real texture-to-buffer copy through the chunk). The rest of the GPU suite passes; two unrelated upstream tests on this machine are filtered (the 2^21 view-heap assertion and the copy-pair test).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:15:00 +00:00
Author
Owner

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

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-25 21:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/2818#issuecomment-7175402310) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 02:21 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-26 02:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2818#issuecomment-7180719658) <!-- 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)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 21:58 UTC · original · reviewed

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.

**@⁠kotekompotek** on GitHub · 2026-11-25 21:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/2818#pullrequestreview-6065560738) · **reviewed** 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.
Author
Owner

@⁠AwsAli05 on GitHub · 2026-11-25 23:08 UTC · original · reviewed

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.

**@⁠AwsAli05** on GitHub · 2026-11-25 23:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2818#pullrequestreview-6066217045) · **reviewed** 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.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 02:47 UTC · original · approved these changes

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

**@⁠Dukestar69** on GitHub · 2026-11-26 02:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/2818#pullrequestreview-6067878362) · **approved these changes** 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).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 11:56 UTC · original · approved these changes

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/2818#pullrequestreview-6073720547) · **approved these changes** 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.
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/2818/head:github-bot/gh-2818'
git switch 'github-bot/gh-2818'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2818'
git switch 'github-bot/gh-2818'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2818'
git switch 'github-bot/gh-2818'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2818'
git switch 'main'
git merge --squash 'github-bot/gh-2818'
git switch 'main'
git merge --ff-only 'github-bot/gh-2818'
git switch 'main'
git merge 'github-bot/gh-2818'
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!2818
No description provided.