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

Open
github-bot wants to merge 2 commits from github-bot/gh-23 into beta AGit
Owner

@⁠spy0eye-beep on GitHub · 2026-11-25 17:03 UTC · original
spy0eye-beep:klbf-staging-ram → beta · +208 −16 · 2 files · 2 commits · open

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

**@⁠spy0eye-beep** on GitHub · 2026-11-25 17:03 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/23) `spy0eye-beep:klbf-staging-ram` → `beta` · +208 −16 · 2 files · 2 commits · open ## 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)
A staging buffer is system memory the size of its texture. It was kept for
every texture sampled in the last idle_ms, so every texture in view cost its
size twice: on the card and in the PC's memory. Its contents are only needed
between a detiling and the upload that copies it; whether a texture changed
is told by the hash and the write watch in `Last`, not by these bytes.

Now let_go_idle also drops the buffer (not the record) of a texture that no
list holds, was uploaded, and has not been detiled for 2 s and 30 frames.
The record (hash, uploaded and dirty flags) stays, so the texture is neither
uploaded nor detiled again until its memory changes; then a buffer is made
anew (Staging::mapped). A texture the title rewrites every few frames keeps
its buffer. VICTORY_STAGING_KEEP=1 keeps every buffer, as before. The memory
line reports how many buffers were let go this way and how many were made
again.

Not measured in the title yet: this is for machines with 16 GB of RAM, where
a run commits about 20 GB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
From HandshakeHackin's WARP fixture on PastiiJay/klbf-lab#23: a texture
with a level waiting for tiles had its buffer let go, but the retry in
upload() re-uploads from those bytes and an unchanged texture is not
detiled again. Such a texture now keeps its buffer (waiting == 0 in the
condition). The decision is a pure function, buffer_may_go, with tests
for a settled texture and for each reason that keeps the buffer.

The memory line's staging count was of records, which outlive a buffer
let go: it now also reports the buffers that exist and their bytes
(RESIDENT), so a run shows what the sweep reclaimed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:52:39 +00:00
Author
Owner

@⁠HandshakeHackin on GitHub · 2026-11-25 17:38 UTC · original

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.

**@⁠HandshakeHackin** on GitHub · 2026-11-25 17:38 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7169793584) 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).
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 18:38 UTC · original

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

**@⁠PastiiJay** on GitHub · 2026-11-25 18:38 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7171180309) Thanks a lot @⁠spy0eye-beep! Noted. It goes into tonight's RX 9070 XT test queue and I'll report the result here.
Author
Owner

@⁠spy0eye-beep on GitHub · 2026-11-25 21:05 UTC · original

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.

**@⁠spy0eye-beep** on GitHub · 2026-11-25 21:05 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7174605078) 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.
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 21:08 UTC · original

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.

**@⁠PastiiJay** on GitHub · 2026-11-25 21:08 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7174656348) 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.
Author
Owner

@⁠HandshakeHackin on GitHub · 2026-11-26 03:22 UTC · original

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

**@⁠HandshakeHackin** on GitHub · 2026-11-26 03:22 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7181661969) 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)
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-26 10:42 UTC · original

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

**@⁠PastiiJay** on GitHub · 2026-11-26 10:42 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/23#issuecomment-7189728389) Thanks for rerunning the eviction fixture. We'll see how it behaves on a 16 GB machine in game.
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/23/head:github-bot/gh-23'
git switch 'github-bot/gh-23'

Merge

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