Textures: staging buffers come from a pool instead of being made per texture #781

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

@⁠marquezinii on GitHub · 2026-11-25 07:27 UTC · original
marquezinii:perf/staging-pool → main · +224 −1 · 2 files · 1 commits · open

The pass over the heap's textures (draw.rs heap_textures) runs on the thread that records the frame, with a budget of 250 ms and 192 MB a pass, three times that while 1,024 or more slots are due. On a Ryzen 5 5600X + RTX 5070 the queue held about 5,000 due slots for minutes, so frames sat at that budget (0.7-1.4 s) whenever it ran full.

I split the pass with local counters. In the 111 heaviest lines of a run, staging was 257 ms of the median 392 ms of the texture part. Most of it was staging() making an upload buffer for each texture (Host::upload_buffer, a committed resource and a map) and releasing it when the texture was let go a frame later. A small benchmark of 1,000 buffers / 350 MB on the 5070:

ms per MB
create + map 1.9
release 1.17
create + map with D3D12_HEAP_FLAG_CREATE_NOT_ZEROED 1.9 (no change)
write a buffer that already exists 0.36

This change keeps those buffers in a pool:

  • A Staging that is dropped gives its buffer back. The lists that recorded a copy from it hold the Arc until they are reset, so the card is done with it by then.
  • Buffers are kept by size class: whole 64 KiB up to 1 MiB, then eighths of a power of two (at most an eighth unused). A texture takes the buffer of its class given back last.
  • With the pool full, the oldest buffer of the class holding the most bytes is released. Most staging buffers live a frame, so the newest are the ones asked for again. A first version that refused new buffers when full reused only 5 %; this one reused 77 % in the same scenes.
  • A buffer taken from the pool holds another texture's texels. detile writes every subresource (including the zeros for levels on demand), and if the first refresh does not detile at all (memory all zero, or not there yet) the buffer is zeroed, as a new one would be.
  • VICTORY_STAGING_POOL_MB (512) caps it; 0 makes and releases every buffer as before. The diagnostics memory line counts buffers made, taken, given back and released.

Measured on 310c6eb plus #95 and #665 (the remap and fetch waits hid this otherwise), diag build, Series X, 480 s runs with the same scripted input, one exe with the switch on and off:

VICTORY_STAGING_POOL_MB=0 pool on
staging time per slot looked at 0.112 ms 0.036 ms
lines with the texture part over 150 ms 46 23
buffers made / taken from the pool 56,021 / 0 14,781 / 50,093

The queue drained a few minutes into the world in the runs with the pool. After that the pass costs 15-40 ms a frame. Frames in the steady door scene of the prologue are bound by the card either way (about 2 fps there with these two runs), so this shows up while textures stream in, not in the steady frame. I compared the captured frames (VICTORY_CAPTURE) of both runs at the same offsets and saw no texture with stale texels.

Tests: two new unit tests (size classes; a buffer given back is taken again for its class and not for another). cargo test --release -p victory-gpu passes, except inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset. That test also fails on a26859f without this change when the whole suite runs (3 of 3 runs here) and passes alone. Clippy shows no warnings in the changed files, in both CI configurations. Rebased on a26859f; levels on demand were not enabled in the measured runs.

**@⁠marquezinii** on GitHub · 2026-11-25 07:27 UTC · [original](https://github.com/fxceless/victory-fork/pull/781) `marquezinii:perf/staging-pool` → `main` · +224 −1 · 2 files · 1 commits · open The pass over the heap's textures (`draw.rs` `heap_textures`) runs on the thread that records the frame, with a budget of 250 ms and 192 MB a pass, three times that while 1,024 or more slots are due. On a Ryzen 5 5600X + RTX 5070 the queue held about 5,000 due slots for minutes, so frames sat at that budget (0.7-1.4 s) whenever it ran full. I split the pass with local counters. In the 111 heaviest lines of a run, staging was 257 ms of the median 392 ms of the texture part. Most of it was `staging()` making an upload buffer for each texture (`Host::upload_buffer`, a committed resource and a map) and releasing it when the texture was let go a frame later. A small benchmark of 1,000 buffers / 350 MB on the 5070: | | ms per MB | |---|---:| | create + map | 1.9 | | release | 1.17 | | create + map with `D3D12_HEAP_FLAG_CREATE_NOT_ZEROED` | 1.9 (no change) | | write a buffer that already exists | 0.36 | This change keeps those buffers in a pool: - A `Staging` that is dropped gives its buffer back. The lists that recorded a copy from it hold the `Arc` until they are reset, so the card is done with it by then. - Buffers are kept by size class: whole 64 KiB up to 1 MiB, then eighths of a power of two (at most an eighth unused). A texture takes the buffer of its class given back last. - With the pool full, the oldest buffer of the class holding the most bytes is released. Most staging buffers live a frame, so the newest are the ones asked for again. A first version that refused new buffers when full reused only 5 %; this one reused 77 % in the same scenes. - A buffer taken from the pool holds another texture's texels. `detile` writes every subresource (including the zeros for levels on demand), and if the first refresh does not detile at all (memory all zero, or not there yet) the buffer is zeroed, as a new one would be. - `VICTORY_STAGING_POOL_MB` (512) caps it; 0 makes and releases every buffer as before. The diagnostics memory line counts buffers made, taken, given back and released. **Measured** on `310c6eb` plus #95 and #665 (the remap and fetch waits hid this otherwise), diag build, Series X, 480 s runs with the same scripted input, one exe with the switch on and off: | | `VICTORY_STAGING_POOL_MB=0` | pool on | |---|---:|---:| | staging time per slot looked at | 0.112 ms | 0.036 ms | | lines with the texture part over 150 ms | 46 | 23 | | buffers made / taken from the pool | 56,021 / 0 | 14,781 / 50,093 | The queue drained a few minutes into the world in the runs with the pool. After that the pass costs 15-40 ms a frame. Frames in the steady door scene of the prologue are bound by the card either way (about 2 fps there with these two runs), so this shows up while textures stream in, not in the steady frame. I compared the captured frames (`VICTORY_CAPTURE`) of both runs at the same offsets and saw no texture with stale texels. **Tests:** two new unit tests (size classes; a buffer given back is taken again for its class and not for another). `cargo test --release -p victory-gpu` passes, except `inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset`. That test also fails on `a26859f` without this change when the whole suite runs (3 of 3 runs here) and passes alone. Clippy shows no warnings in the changed files, in both CI configurations. Rebased on `a26859f`; levels on demand were not enabled in the measured runs.
A title texture's staging buffer was an upload buffer made for it and
released with it. On an RTX 5070 making and mapping one costs about 1.9 ms
a megabyte and releasing it 1.2 more, whatever the heap flags, against
0.36 to write one already there. The pass over the heap's textures stages
up to 192 MB a frame (576 MB while it catches up) on the thread that
records the frame, so most of its 250-750 ms went to that.

A staging buffer let go now goes back to a pool by size class (whole
64 KiB up to 1 MiB, then eighths of a power of two), and the next texture
of that class takes the one given back last. With the pool full, the
oldest buffer of the class holding the most bytes is released to make
room: most staging buffers live a frame, so the newest are the likeliest
to be asked for again. A buffer taken from the pool that the first
refresh does not detile into (memory all zero, or not there yet) is
zeroed, as a new one would be.

The pool keeps at most VICTORY_STAGING_POOL_MB (512); 0 makes and
releases every buffer as before. The memory line counts buffers made,
taken from the pool, given back and released.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:51:00 +00:00
Author
Owner

@⁠marquezinii on GitHub · 2026-11-25 07:31 UTC · original

The red check is the Clippy step failing on three lints that main has as well (its run for a26859f fails on the same ones): placed_filler_twins never used (resource.rs:2553), sort_unstable_by_key (draw.rs:2387), depth_written never read (indirect.rs:1335). This PR only changes texture.rs and copystat.rs.

**@⁠marquezinii** on GitHub · 2026-11-25 07:31 UTC · [original](https://github.com/fxceless/victory-fork/pull/781#issuecomment-7155952758) The red check is the Clippy step failing on three lints that `main` has as well (its run for `a26859f` fails on the same ones): `placed_filler_twins` never used (resource.rs:2553), `sort_unstable_by_key` (draw.rs:2387), `depth_written` never read (indirect.rs:1335). This PR only changes texture.rs and copystat.rs.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/texture.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/781/head:github-bot/gh-781'
git switch 'github-bot/gh-781'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-781'
git switch 'github-bot/gh-781'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-781'
git switch 'github-bot/gh-781'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-781'
git switch 'main'
git merge --squash 'github-bot/gh-781'
git switch 'main'
git merge --ff-only 'github-bot/gh-781'
git switch 'main'
git merge 'github-bot/gh-781'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.