Skip to content

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

Open
Therecalloftheonion wants to merge 1 commit into
Marvisak:mainfrom
Therecalloftheonion:pr/gpu-copy-arenas
Open

Therecalloftheonion wants to merge 1 commit into
Marvisak:mainfrom
Therecalloftheonion:pr/gpu-copy-arenas

Conversation

@Therecalloftheonion

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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 GPU: split and flush command list segments after texture read-backs #2608.

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

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.

@AwsAli05 AwsAli05 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Owner

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

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

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
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