GPU: let go of a placed texture's host copy as soon as its memory is unmapped #432

Open
github-bot wants to merge 2 commits from github-bot/gh-432 into main AGit
Owner

@⁠HailLucifer666 on GitHub · 2026-11-25 00:47 UTC · original
HailLucifer666:perf/evict-unmapped-placed-textures → main · +245 −3 · 2 files · 2 commits · open

Update, after @⁠cafeoh's measurement on an RTX 2080 Ti

The first commit walked every texture inside title_memory_remapped, on the thread doing the
unmapping: ~5,000 walks a second, 975 ms of every second of the load, letting go of 59 host
copies in the whole load. The load stalled and the save never loaded.

8f738674 moves the walk out of the unmap path. That function now only records the range
(a lock and a merge over at most 64 ranges, no read of the texture table). let_go_idle, which
already walks every texture once a frame on the submitting thread, asks there whether a placed
texture's source was among the unmapped ranges.

What may be released is unchanged: only the resource may hold the host copy -- no list not yet
reset, no descriptor slot viewing it. A source no longer existing only adds a reason to let a
copy go, never a reason to keep it, so this cannot release something the per-unmap version kept.

What this changes

A placed texture's host copy used to wait for the idle sweep — five seconds and four frames of nothing asking for it (resource::let_go_idle). While the title streams its next set of textures in, nothing is asking, so a texture the title had already finished with kept its card memory through the whole stream, and the run paged.

Unmapping the title memory is how the title says it is done with a texture, and that was already a call with the range in hand (watch::title_memory_remapped). This acts on it there.

  • resource::evict_placed_in(base, len) walks the placed textures the title fills whose bytes meet [base, base + len) and lets a host copy go when only its title resource holds it — the same two conditions let_go_idle requires (Arc::strong_count(host) == 1 and host::references(&host.texture) == 1), under the same made lock, followed by the same texture::unstage, host::retire, host.gone marking and KINDS[2]/KINDS[3] counters.
  • title_memory_remapped calls it after the views and placements there are forgotten.
  • No fence is waited on, so no submission is held up. A copy a list not yet reset or a descriptor slot still names stays until they let it go; the next use makes it anew from the memory the runtime has just mapped.
  • resource::ranges_overlap(a_start, a_len, b_start, b_len): half-open, zero-length ranges share nothing, ends saturate so a range cannot wrap into a false hit.
  • VICTORY_TEXTURE_SWEEP=0 still lets nothing go; the switch moved into one sweeping() so the two paths cannot disagree. The path is traced through host::trail like the idle sweep's.

Why it is safe

Nothing new is trusted. The two retention conditions are the ones the idle sweep already relied on, and the state it touches (made, staging, KINDS) is touched under the same locks in the same order. The difference is only when they are checked: on the range the runtime just unmapped, rather than after a timer.

Tests

ranges_overlap_handles_boundaries_zero_len_and_overflow (in resource::tests) pins adjacency in both orders, a single shared byte on either side, containment both ways, empty ranges, and the top of the address space. It is the part of this that can be tested without a card.

Mutation-probed: < → <= in the first comparison makes the adjacent case report an overlap, and the test fails on exactly that assertion.

cargo test -p victory-gpu resource:: --locked -- --test-threads=1
  test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured; 104 filtered out

cargo test -p victory-gpu --locked -- --test-threads=1
  test result: ok. 113 passed; 0 failed; 1 ignored

cargo clippy --workspace --all-targets --locked -- -D warnings                       (exit 0)
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings        (exit 0)
cargo test -p xtask --locked                                                          (4 passed)

Scope

crates/victory-gpu/src/resource.rs and the one call in crates/victory-gpu/src/watch.rs, which is where title_memory_remapped is defined (lib.rs only re-exports it).

No test drives evict_placed_in itself: it needs the host device. What is new and testable is the range test; the retention conditions it reuses are let_go_idle's.

**@⁠HailLucifer666** on GitHub · 2026-11-25 00:47 UTC · [original](https://github.com/fxceless/victory-fork/pull/432) `HailLucifer666:perf/evict-unmapped-placed-textures` → `main` · +245 −3 · 2 files · 2 commits · open > ### Update, after @⁠cafeoh's measurement on an RTX 2080 Ti > > The first commit walked every texture inside `title_memory_remapped`, on the thread doing the > unmapping: ~5,000 walks a second, 975 ms of every second of the load, letting go of 59 host > copies in the whole load. The load stalled and the save never loaded. > > **`8f738674` moves the walk out of the unmap path.** That function now only records the range > (a lock and a merge over at most 64 ranges, no read of the texture table). `let_go_idle`, which > already walks every texture once a frame on the submitting thread, asks there whether a placed > texture's source was among the unmapped ranges. > > What may be released is unchanged: only the resource may hold the host copy -- no list not yet > reset, no descriptor slot viewing it. A source no longer existing only adds a reason to let a > copy go, never a reason to keep it, so this cannot release something the per-unmap version kept. ## What this changes A placed texture's host copy used to wait for the idle sweep — five seconds and four frames of nothing asking for it (`resource::let_go_idle`). While the title streams its next set of textures in, nothing is asking, so a texture the title had already finished with kept its card memory through the whole stream, and the run paged. Unmapping the title memory is how the title says it is done with a texture, and that was already a call with the range in hand (`watch::title_memory_remapped`). This acts on it there. - `resource::evict_placed_in(base, len)` walks the **placed** textures the title **fills** whose bytes meet `[base, base + len)` and lets a host copy go when only its title resource holds it — the same two conditions `let_go_idle` requires (`Arc::strong_count(host) == 1` and `host::references(&host.texture) == 1`), under the same `made` lock, followed by the same `texture::unstage`, `host::retire`, `host.gone` marking and `KINDS[2]`/`KINDS[3]` counters. - `title_memory_remapped` calls it after the views and placements there are forgotten. - No fence is waited on, so no submission is held up. A copy a list not yet reset or a descriptor slot still names stays until they let it go; the next use makes it anew from the memory the runtime has just mapped. - `resource::ranges_overlap(a_start, a_len, b_start, b_len)`: half-open, zero-length ranges share nothing, ends saturate so a range cannot wrap into a false hit. - `VICTORY_TEXTURE_SWEEP=0` still lets nothing go; the switch moved into one `sweeping()` so the two paths cannot disagree. The path is traced through `host::trail` like the idle sweep's. ## Why it is safe Nothing new is trusted. The two retention conditions are the ones the idle sweep already relied on, and the state it touches (`made`, staging, `KINDS`) is touched under the same locks in the same order. The difference is only *when* they are checked: on the range the runtime just unmapped, rather than after a timer. ## Tests `ranges_overlap_handles_boundaries_zero_len_and_overflow` (in `resource::tests`) pins adjacency in both orders, a single shared byte on either side, containment both ways, empty ranges, and the top of the address space. It is the part of this that can be tested without a card. Mutation-probed: `<` → `<=` in the first comparison makes the adjacent case report an overlap, and the test fails on exactly that assertion. ``` cargo test -p victory-gpu resource:: --locked -- --test-threads=1 test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured; 104 filtered out cargo test -p victory-gpu --locked -- --test-threads=1 test result: ok. 113 passed; 0 failed; 1 ignored cargo clippy --workspace --all-targets --locked -- -D warnings (exit 0) cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (exit 0) cargo test -p xtask --locked (4 passed) ``` ## Scope `crates/victory-gpu/src/resource.rs` and the one call in `crates/victory-gpu/src/watch.rs`, which is where `title_memory_remapped` is defined (`lib.rs` only re-exports it). No test drives `evict_placed_in` itself: it needs the host device. What is new and testable is the range test; the retention conditions it reuses are `let_go_idle`'s.
A placed texture's host copy waited for the idle sweep: five seconds and four
frames of nothing asking for it (`resource::let_go_idle`). Nothing is asking
while the title streams its next set of textures in, so a texture the title
had already finished with kept its card memory through the whole stream -- and
the run paged. Unmapping the title memory is how the title says it is done
with a texture; that is the moment to act on, and it was already a call
(`watch::title_memory_remapped`) with the range in hand.

WHAT GOES, AND WHEN.
`resource::evict_placed_in(base, len)` walks the placed textures the title
fills whose bytes meet `[base, base + len)`, and lets a host copy go when only
its title resource holds it: `Arc::strong_count(host) == 1` and
`host::references(&host.texture) == 1`, the same two conditions `let_go_idle`
requires, taken under the same `made` lock and followed by the same
`texture::unstage`, `host::retire`, `host.gone` marking and `KINDS[2]`/`KINDS[3]`
counters. `title_memory_remapped` calls it after the views and placements there
are forgotten. Nothing waits on a fence, so no submission is held up, and no
list or slot that still names the copy is touched: it stays, as before, until
they let it go. The next use makes the host copy anew from the memory the
runtime has just mapped, which is exactly what the idle sweep relied on.

Overlap is answered by a new `resource::ranges_overlap(a_start, a_len, b_start,
b_len)`: half-open, zero-length ranges share nothing, and the ends saturate, so
a range cannot wrap into a false hit. It is the one piece of this that can be
tested without a card, so it is:
`ranges_overlap_handles_boundaries_zero_len_and_overflow` pins adjacency in
both orders, a single shared byte on either side, containment both ways, empty
ranges, and the top of the address space. Mutation-probed: `<` to `<=` in the
first comparison makes the adjacent case report an overlap and the test fails
on it.

`VICTORY_TEXTURE_SWEEP=0` still lets nothing go. The switch moved into one
`sweeping()` so the two paths cannot disagree about it. The path is traced
through `host::trail` like the idle sweep's, naming how many went and their
KiB.

SCOPE.
resource.rs and the call in watch.rs, which is where `title_memory_remapped`
is defined (`lib.rs` re-exports it). No test drives `evict_placed_in` itself:
it needs the host device, and the two conditions it acts on are the ones
`let_go_idle` already had. What is new and testable is the range test.
cafeoh measured this on an RTX 2080 Ti: loading a save ran `evict_placed_in`
~5,000 times a second and it took ~975 ms of every second, on the same
thread that was doing the unmapping -- so the load stalled and the save
never loaded. In the whole load it let go of 59 host copies, so nearly
every one of those walks found nothing, and skipping just that call made
the load work.

The walk was the cost, and it was in the wrong place. `title_memory_remapped`
now only records the range: a lock, a merge over at most KEPT_RANGES ranges,
no read of the texture table, whatever the table's size. `let_go_idle` --
which already walks every texture once a frame, on the thread that submits
-- asks whether a placed texture's source was among those ranges and lets
its host copy go without waiting out the idle time.

What a host copy needs to go is unchanged: nothing else may hold it (no
list not yet reset, no descriptor slot), which is why this cannot make a
texture be read from the wrong memory; the source no longer existing only
adds a reason to let it go, never a reason to keep it. The idle path is
untouched, so a texture nothing unmapped still waits its IDLE_MS and
IDLE_FRAMES.

Ranges are merged as they arrive -- overlapping, touching, and bridging
ranges become one -- so five thousand unmaps a second do not become five
thousand ranges. Past KEPT_RANGES the sweep is told to treat every placed
texture as due, which is sound for the same reason.

Tests: merge in every arrival order, the cap and the "same range grown is
not a new range" case, and which textures a set of ranges makes due.
Probe: store ranges without merging -> both range tests red with the
unsorted, unmerged list -> restored byte-identical (sha256 531418328c0f49c7).
`cargo test -p victory-gpu --lib -- --test-threads=1`: 148 passed, 0 failed,
3 ignored. Clippy: only the two pre-existing (#841).

Not tested: in game, on any card. This is cafeoh's report explained, not
their report re-run.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:10 +00:00
Author
Owner

@⁠cafeoh on GitHub · 2026-11-25 12:58 UTC · original

Tested in game on an RTX 2080 Ti (Turing), Windows 10, on top of fork main a253706: with this PR, loading a save (or starting a new game) fails — the title says it can't load, then the screen stays black. Without it, the same save loads every time (5 runs).

I timed the new evict_placed_in call in title_memory_remapped. During the load it runs ~5,000 times a second and takes ~975 ms of every second, so the unmapping thread does almost nothing else; it let go of 0 host copies in almost every second (59 in total). Skipping only that call (everything else in the PR kept) makes the load work again.

The per-unmap walk over every texture looks like the cost: maybe an index by address, or batching the ranges and checking once a frame, would avoid it.

Not tested: whether the memory saving shows up once in the world; other cards.

AI-assisted (Claude Code); runs and observations are mine.

**@⁠cafeoh** on GitHub · 2026-11-25 12:58 UTC · [original](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7163124401) Tested in game on an RTX 2080 Ti (Turing), Windows 10, on top of fork main a253706: with this PR, loading a save (or starting a new game) fails — the title says it can't load, then the screen stays black. Without it, the same save loads every time (5 runs). I timed the new `evict_placed_in` call in `title_memory_remapped`. During the load it runs ~5,000 times a second and takes ~975 ms of every second, so the unmapping thread does almost nothing else; it let go of 0 host copies in almost every second (59 in total). Skipping only that call (everything else in the PR kept) makes the load work again. The per-unmap walk over every texture looks like the cost: maybe an index by address, or batching the ranges and checking once a frame, would avoid it. Not tested: whether the memory saving shows up once in the world; other cards. AI-assisted (Claude Code); runs and observations are mine.
Author
Owner

@⁠HailLucifer666 on GitHub · 2026-11-25 13:31 UTC · original

Thank you @⁠cafeoh — this is the report that closes it, and your diagnosis is exactly right: the per-unmap walk over every texture was the cost.

What was happening. title_memory_remapped called evict_placed_in, which took a snapshot of the texture table and visited every entry — 18,639 of them in a mature run — for each unmap. All of that ran on the thread doing the unmapping, so at your measured ~5,000 unmaps a second the loader spent ~975 ms of every second inside a search that found nothing: 59 host copies in the entire load. The thread got no time to actually unmap, the load missed its deadlines, and the title gave up on the save. The black screen is the title after it has given up, not a graphics fault.

The fix (8f738674, on this branch). title_memory_remapped no longer walks anything. It records the range — one lock and a merge over at most 64 ranges, no read of the texture table, whatever its size. let_go_idle already walks every texture once a frame on the submitting thread, so the question "was this placed texture's source among the unmapped ranges?" is now asked there, at the cost of one bounds check per texture per frame, which is roughly 5,000× fewer visits than before. A placed texture whose source is gone goes without waiting out the idle time; everything else keeps waiting IDLE_MS and IDLE_FRAMES as it did.

Why this cannot read the wrong memory. The condition for letting a host copy go is unchanged and is still the strict one: only the resource may hold it — no list not yet reset, no descriptor slot viewing it. A source having been unmapped only ever adds a reason to let a copy go, never a reason to keep it, so the batched version cannot release something the per-unmap version would have kept.

Measured here: cargo test -p victory-gpu --lib -- --test-threads=1 → 148 passed, 0 failed, 3 ignored. Tests cover the merge in every arrival order (before, after, overlapping, touching, bridging), the 64-range cap, that growing one range never counts as a new one, and which textures a set of ranges makes due. Mutation probe: storing ranges without merging turns both range tests red; restored byte-identical.

Not tested: in game, on any card. I have no GPU that runs the title. Your 2080 Ti is the only place this can be confirmed, and the two things worth watching are exactly the two you did not get to: whether the save loads, and whether the memory saving appears once you are in the world. If the load still fails with this commit, the next suspect is a different call in the same function and I would want the same timing breakdown again.

**@⁠HailLucifer666** on GitHub · 2026-11-25 13:31 UTC · [original](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7163902747) Thank you @⁠cafeoh — this is the report that closes it, and your diagnosis is exactly right: the per-unmap walk over every texture was the cost. **What was happening.** `title_memory_remapped` called `evict_placed_in`, which took a snapshot of the texture table and visited every entry — 18,639 of them in a mature run — for *each* unmap. All of that ran on the thread doing the unmapping, so at your measured ~5,000 unmaps a second the loader spent ~975 ms of every second inside a search that found nothing: 59 host copies in the entire load. The thread got no time to actually unmap, the load missed its deadlines, and the title gave up on the save. The black screen is the title after it has given up, not a graphics fault. **The fix (`8f738674`, on this branch).** `title_memory_remapped` no longer walks anything. It records the range — one lock and a merge over at most 64 ranges, no read of the texture table, whatever its size. `let_go_idle` already walks every texture once a frame on the submitting thread, so the question "was this placed texture's source among the unmapped ranges?" is now asked there, at the cost of one bounds check per texture per frame, which is roughly 5,000× fewer visits than before. A placed texture whose source is gone goes without waiting out the idle time; everything else keeps waiting `IDLE_MS` and `IDLE_FRAMES` as it did. **Why this cannot read the wrong memory.** The condition for letting a host copy go is unchanged and is still the strict one: only the resource may hold it — no list not yet reset, no descriptor slot viewing it. A source having been unmapped only ever *adds* a reason to let a copy go, never a reason to keep it, so the batched version cannot release something the per-unmap version would have kept. **Measured here:** `cargo test -p victory-gpu --lib -- --test-threads=1` → 148 passed, 0 failed, 3 ignored. Tests cover the merge in every arrival order (before, after, overlapping, touching, bridging), the 64-range cap, that growing one range never counts as a new one, and which textures a set of ranges makes due. Mutation probe: storing ranges without merging turns both range tests red; restored byte-identical. **Not tested:** in game, on any card. I have no GPU that runs the title. Your 2080 Ti is the only place this can be confirmed, and the two things worth watching are exactly the two you did not get to: whether the save loads, and whether the memory saving appears once you are in the world. If the load still fails with this commit, the next suspect is a different call in the same function and I would want the same timing breakdown again.
Author
Owner

@⁠HailLucifer666 on GitHub · 2026-11-25 13:33 UTC · original

@⁠cafeoh — the fix for what you measured is pushed as 8f738674 on this branch, and I would be grateful if you could run it once more. My machine cannot run the title, so yours is the only place this can be settled.

What changed. title_memory_remapped no longer walks the texture table at all. It records the unmapped range — one lock and a merge over at most 64 ranges — and let_go_idle, which already walks every texture once a frame on the submitting thread, asks there whether a placed texture's source was among those ranges. The 5,000 walks a second become one walk a frame, and the thread doing the unmapping gets its time back.

What is worth watching, in the order you would notice it:

  1. Does the save load? That is the whole question. Five runs, as before, is plenty.
  2. If it loads: the memory numbers once you are in the world. You said you had not reached that. The trail line to look for is unmapped: N host copies of placed textures let go (M KiB), each held by its title texture alone. Under the old code that line appeared for 59 copies across a whole load; the interesting number now is how many appear once you are driving around, because that is where streaming actually happens.
  3. If it still fails to load: the same timing breakdown you gave me, but for the whole of title_memory_remapped rather than just evict_placed_in. That call also does inplace::views_gone, inplace::title_memory_freeing and the guard range work, and if the load is still being starved, one of those is next in line. I am not going to guess which — your numbers named the last one exactly.

VICTORY_TEXTURE_SWEEP=0 still turns all of this off if you want the load path with none of it.

Two things this commit explicitly does not fix: whatever caused the black screen after the failed load is the title's own fallback, not something this code draws; and the original question of whether a placed texture really is finished when the title unmaps its memory is still unproven — this only makes acting on that guess cheap enough to be harmless.

**@⁠HailLucifer666** on GitHub · 2026-11-25 13:33 UTC · [original](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7163942711) @⁠cafeoh — the fix for what you measured is pushed as `8f738674` on this branch, and I would be grateful if you could run it once more. My machine cannot run the title, so yours is the only place this can be settled. **What changed.** `title_memory_remapped` no longer walks the texture table at all. It records the unmapped range — one lock and a merge over at most 64 ranges — and `let_go_idle`, which already walks every texture once a frame on the submitting thread, asks there whether a placed texture's source was among those ranges. The 5,000 walks a second become one walk a frame, and the thread doing the unmapping gets its time back. **What is worth watching, in the order you would notice it:** 1. **Does the save load?** That is the whole question. Five runs, as before, is plenty. 2. **If it loads: the memory numbers once you are in the world.** You said you had not reached that. The trail line to look for is `unmapped: N host copies of placed textures let go (M KiB), each held by its title texture alone`. Under the old code that line appeared for 59 copies across a whole load; the interesting number now is how many appear once you are driving around, because that is where streaming actually happens. 3. **If it still fails to load:** the same timing breakdown you gave me, but for the whole of `title_memory_remapped` rather than just `evict_placed_in`. That call also does `inplace::views_gone`, `inplace::title_memory_freeing` and the guard range work, and if the load is still being starved, one of those is next in line. I am not going to guess which — your numbers named the last one exactly. `VICTORY_TEXTURE_SWEEP=0` still turns all of this off if you want the load path with none of it. Two things this commit explicitly does **not** fix: whatever caused the black screen after the failed load is the title's own fallback, not something this code draws; and the original question of whether a placed texture really is finished when the title unmaps its memory is still unproven — this only makes acting on that guess cheap enough to be harmless.
Author
Owner

@⁠cafeoh on GitHub · 2026-11-25 13:38 UTC · original

Tested 8f73867 on the same machine (RTX 2080 Ti, Windows 10, fork main a253706 + a few PRs): the save loads and the run reaches the prologue's cell scene with no device loss. One run so far, not five.

Memory: over the prologue (no driving), the sweep let go of about as many host copies as without the PR (32,877 vs 33,663 in a comparable run), so no saving visible here yet; this scene may simply not stream enough to show it.

Not tested: driving or the open world; other cards; more runs.

AI-assisted (Claude Code); runs and observations are mine.

**@⁠cafeoh** on GitHub · 2026-11-25 13:38 UTC · [original](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7164076081) Tested `8f73867` on the same machine (RTX 2080 Ti, Windows 10, fork main a253706 + a few PRs): the save loads and the run reaches the prologue's cell scene with no device loss. One run so far, not five. Memory: over the prologue (no driving), the sweep let go of about as many host copies as without the PR (32,877 vs 33,663 in a comparable run), so no saving visible here yet; this scene may simply not stream enough to show it. Not tested: driving or the open world; other cards; more runs. AI-assisted (Claude Code); runs and observations are mine.
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/432/head:github-bot/gh-432'
git switch 'github-bot/gh-432'

Merge

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