Textures: let go of the staging buffer of a texture that stays unchanged (16 GB machines) #23

Open
spy0eye-beep wants to merge 78 commits from upstream-pr/23 into main
spy0eye-beep commented 2026-11-25 17:03:14 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by spy0eye-beep on 2026-11-25T17:03:14Z.


What

Aimed at 16 GB machines (the guide says a run commits about 20 GB).

A texture's staging buffer is system memory the size of the texture (an upload heap). It is kept for every texture sampled in the last idle_ms, so every texture in view costs its size twice: once on the card, once in the PC's memory. A tester's memory map on fxceless/victory-fork#54 shows 7 GB of write-combined memory at 19.7 GB private, 146 × 32 MB of it.

The buffer's contents are only needed between a detiling and the upload that copies it. Whether a texture changed is decided by the hash and the write watch in Last, not by those bytes.

Change

let_go_idle also drops the buffer (not the record) of a texture that:

  • no list holds (Arc::strong_count == 1),
  • was uploaded and is not dirty,
  • has not been detiled for 2 s and 30 frames (so a texture the title rewrites every few frames keeps its buffer).

The record (hash, uploaded, dirty) stays, so the texture is neither uploaded nor detiled again until its memory changes. When it does, Staging::mapped makes a buffer anew, under the same lock the detiling writes under. A list recording an upload gets the buffer through Staging::buffer(), which makes one if needed.

  • VICTORY_STAGING_KEEP=1 keeps every buffer, as before.
  • The memory line now also reports staging buffers of unchanged textures ... (N made again ...), so a run shows how much this frees and what it costs.

This complements the existing let_go_idle (which drops the whole record of textures no longer sampled, and re-uploads them later). This one only drops the bytes of textures still sampled but unchanged.

Testing

  • cargo test --workspace --all-features on beta (c71c7d6) with this change and the discrete-GPU PR together, on the RTX 4060 Laptop (587 passed, 2 failed: the rect tests, failing on beta too) and on the Intel UHD (the same 9 failures as beta). This branch alone was built and checked, not run separately.
  • cargo check / cargo build --release -p victory: OK. Clippy: same two pre-existing errors as beta.
  • Not measured in the title. I have no run to show the memory it saves or whether re-made buffers cost frame time; the new memory-line numbers are there for that. Please treat it as something to try, not a measured improvement.

AI-assisted (Claude).

🤖 Generated with Claude Code

<!-- greyfinch-sync:PastiiJay/klbf-lab:issue:5495652513 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/23). Originally posted by [spy0eye-beep](https://github.com/spy0eye-beep) on 2026-11-25T17:03:14Z. --- ## What Aimed at 16 GB machines (the guide says a run commits about 20 GB). A texture's staging buffer is system memory the size of the texture (an upload heap). It is kept for every texture sampled in the last `idle_ms`, so every texture in view costs its size twice: once on the card, once in the PC's memory. A tester's memory map on fxceless/victory-fork#54 shows 7 GB of write-combined memory at 19.7 GB private, 146 × 32 MB of it. The buffer's **contents** are only needed between a detiling and the upload that copies it. Whether a texture changed is decided by the hash and the write watch in `Last`, not by those bytes. ## Change `let_go_idle` also drops the **buffer** (not the record) of a texture that: - no list holds (`Arc::strong_count == 1`), - was uploaded and is not dirty, - has not been detiled for 2 s and 30 frames (so a texture the title rewrites every few frames keeps its buffer). The record (hash, `uploaded`, `dirty`) stays, so the texture is neither uploaded nor detiled again until its memory changes. When it does, `Staging::mapped` makes a buffer anew, under the same lock the detiling writes under. A list recording an upload gets the buffer through `Staging::buffer()`, which makes one if needed. - `VICTORY_STAGING_KEEP=1` keeps every buffer, as before. - The memory line now also reports `staging buffers of unchanged textures ... (N made again ...)`, so a run shows how much this frees and what it costs. This complements the existing `let_go_idle` (which drops the whole record of textures no longer sampled, and re-uploads them later). This one only drops the bytes of textures still sampled but unchanged. ## Testing - `cargo test --workspace --all-features` on `beta` (`c71c7d6`) with this change **and** the discrete-GPU PR together, on the RTX 4060 Laptop (587 passed, 2 failed: the `rect` tests, failing on `beta` too) and on the Intel UHD (the same 9 failures as `beta`). This branch alone was built and checked, not run separately. - `cargo check` / `cargo build --release -p victory`: OK. Clippy: same two pre-existing errors as `beta`. - **Not measured in the title.** I have no run to show the memory it saves or whether re-made buffers cost frame time; the new memory-line numbers are there for that. Please treat it as something to try, not a measured improvement. AI-assisted (Claude). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
HandshakeHackin commented 2026-11-25 17:38:33 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by HandshakeHackin on 2026-11-25T17:38:33Z.


I tested the sweep on Windows WARP at fc0480b with two real 256-byte staging allocations. Both textures were recently sampled, unchanged long enough, previously uploaded and clean. One still had a mip waiting for tiles.

After the sweep Settled texture Texture waiting for tiles
Current PR Released Released
With s.waiting.load(Relaxed) == 0 in the eviction condition Released Retained

The retry in upload() sets dirty when the card has room again. It relies on the previous detiled bytes, and an unchanged texture can pass its refresh without detiling again. Keeping the pending buffer preserves those bytes while still reclaiming the settled texture. The fixture checked buffer residency. Rendering after a retry on hardware is still unverified.

One related diagnostic: the “staging of textures … alive” line counted 512 bytes before and after even when both allocations had been released. Counting resident buffers separately from staging records would let a 16 GB tester see what the sweep reclaimed. Windows comparison and exact fixture.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7169793584 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7169793584). Originally posted by [HandshakeHackin](https://github.com/HandshakeHackin) on 2026-11-25T17:38:33Z. --- I tested the sweep on Windows WARP at `fc0480b` with two real 256-byte staging allocations. Both textures were recently sampled, unchanged long enough, previously uploaded and clean. One still had a mip waiting for tiles. | After the sweep | Settled texture | Texture waiting for tiles | | --- | --- | --- | | Current PR | Released | Released | | With `s.waiting.load(Relaxed) == 0` in the eviction condition | Released | Retained | The retry in `upload()` sets `dirty` when the card has room again. It relies on the previous detiled bytes, and an unchanged texture can pass its refresh without detiling again. Keeping the pending buffer preserves those bytes while still reclaiming the settled texture. The fixture checked buffer residency. Rendering after a retry on hardware is still unverified. One related diagnostic: the “staging of textures … alive” line counted **512 bytes before and after** even when both allocations had been released. Counting resident buffers separately from staging records would let a 16 GB tester see what the sweep reclaimed. [Windows comparison and exact fixture](https://github.com/HandshakeHackin/klbf-lab/actions/runs/45666204704/job/139388903225).
PastiiJay commented 2026-11-25 18:38:05 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-25T18:38:05Z.


Thanks a lot @spy0eye-beep! Noted. It goes into tonight's RX 9070 XT test queue and I'll report the result here.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7171180309 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7171180309). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-25T18:38:05Z. --- Thanks a lot @spy0eye-beep! Noted. It goes into tonight's RX 9070 XT test queue and I'll report the result here.
spy0eye-beep commented 2026-11-25 21:05:45 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by spy0eye-beep on 2026-11-25T21:05:45Z.


Thanks @HandshakeHackin, that fixture caught a real gap. Both points are in cbeed1e:

  • A level waiting for tiles keeps its buffer: the condition now includes waiting == 0, as you proposed. The decision is a small pure function (buffer_may_go), with tests for a settled texture (released) and for each reason that keeps the buffer, the waiting case among them.
  • The memory line: the "staging of textures … alive" figure counts records, which outlive a released buffer. It now also reports … with a buffer N MB from a separate resident counter (up on creation and re-creation, down on release and on dropping a record that still has one), so a 16 GB run shows what the sweep actually reclaimed.

cargo test -p victory-gpu --all-features on an RTX 4060 Laptop: 164 passed, 0 failed. Clippy shows only beta's existing two errors; rustfmt shows no new differences. Rendering after a retry is still untested in game, as you note.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7174605078 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7174605078). Originally posted by [spy0eye-beep](https://github.com/spy0eye-beep) on 2026-11-25T21:05:45Z. --- Thanks @HandshakeHackin, that fixture caught a real gap. Both points are in `cbeed1e`: - **A level waiting for tiles keeps its buffer**: the condition now includes `waiting == 0`, as you proposed. The decision is a small pure function (`buffer_may_go`), with tests for a settled texture (released) and for each reason that keeps the buffer, the waiting case among them. - **The memory line**: the "staging of textures … alive" figure counts records, which outlive a released buffer. It now also reports `… with a buffer N MB` from a separate resident counter (up on creation and re-creation, down on release and on dropping a record that still has one), so a 16 GB run shows what the sweep actually reclaimed. `cargo test -p victory-gpu --all-features` on an RTX 4060 Laptop: 164 passed, 0 failed. Clippy shows only `beta`'s existing two errors; rustfmt shows no new differences. Rendering after a retry is still untested in game, as you note.
PastiiJay commented 2026-11-25 21:08:02 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-25T21:08:02Z.


Thanks @spy0eye-beep, and thanks @HandshakeHackin for catching the waiting-tiles case. The buffer_may_go tests and the separate "with a buffer" counter are exactly what we need to see the gain on a 16 GB machine. It goes into our in-game test queue on the RX 9070 XT, with a check that the picture after a retry stays intact.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7174656348 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7174656348). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-25T21:08:02Z. --- Thanks @spy0eye-beep, and thanks @HandshakeHackin for catching the waiting-tiles case. The `buffer_may_go` tests and the separate "with a buffer" counter are exactly what we need to see the gain on a 16 GB machine. It goes into our in-game test queue on the RX 9070 XT, with a check that the picture after a retry stays intact.
HandshakeHackin commented 2026-11-26 03:22:28 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by HandshakeHackin on 2026-11-26T03:22:28Z.


I reran the two-texture fixture against cbeed1e on Windows WARP. The texture waiting for tiles kept all 256 original bytes across two sweeps. With default eviction, resident staging memory went from 512 to 256 bytes after the sweep, returned to 512 when the settled buffer was requested again, and fell to zero when both records dropped. With VICTORY_STAGING_KEEP=1, both allocations stayed resident until drop. A second lookup of the settled buffer counted no extra allocation in either mode.

That verifies the waiting-buffer fix and the resident counter through release, recreation, and drop. Rendering after a tile retry still needs the in-game run. Fixture and both passing runs

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7181661969 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7181661969). Originally posted by [HandshakeHackin](https://github.com/HandshakeHackin) on 2026-11-26T03:22:28Z. --- I reran the two-texture fixture against `cbeed1e` on Windows WARP. The texture waiting for tiles kept all 256 original bytes across two sweeps. With default eviction, resident staging memory went from 512 to 256 bytes after the sweep, returned to 512 when the settled buffer was requested again, and fell to zero when both records dropped. With `VICTORY_STAGING_KEEP=1`, both allocations stayed resident until drop. A second lookup of the settled buffer counted no extra allocation in either mode. That verifies the waiting-buffer fix and the resident counter through release, recreation, and drop. Rendering after a tile retry still needs the in-game run. [Fixture and both passing runs](https://github.com/HandshakeHackin/klbf-lab/actions/runs/45748041751/job/139668785559)
PastiiJay commented 2026-11-26 10:42:35 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-26T10:42:35Z.


Thanks for rerunning the eviction fixture. We'll see how it behaves on a 16 GB machine in game.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7189728389 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7189728389). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-26T10:42:35Z. --- Thanks for rerunning the eviction fixture. We'll see how it behaves on a 16 GB machine in game.
akimfomin commented 2026-11-28 02:04:33 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by akimfomin on 2026-11-28T02:04:33Z.


Continued #2547 demonstrates how live bytes and retained blocks can tell different stories. For your staging release check, use three otherwise equal textures: unchanged with no upload pending, updated once, and still in flight. Record the host staging bytes before and after collecting just the idle one. A resident-memory total alone would hide which class changed.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7232112710 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7232112710). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-28T02:04:33Z. --- [Continued #2547](https://github.com/ProjectLeonida/mandy/pull/2547) demonstrates how live bytes and retained blocks can tell different stories. For your staging release check, use three otherwise equal textures: unchanged with no upload pending, updated once, and still in flight. Record the host staging bytes before and after collecting just the idle one. A resident-memory total alone would hide which class changed.
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/23:upstream-pr/23'
git switch 'upstream-pr/23'

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