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

Open
HailLucifer666 wants to merge 2 commits from upstream-pr/432 into main
HailLucifer666 commented 2026-11-25 00:47:03 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-25T00:47:03Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:issue:5482926374 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/432). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-25T00:47:03Z. --- > ### 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.
cafeoh commented 2026-11-25 13:06:05 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by cafeoh on 2026-11-25T12:58:58Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7163124401 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7163124401). Originally posted by [cafeoh](https://github.com/cafeoh) on 2026-11-25T12:58:58Z. --- 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.
HailLucifer666 commented 2026-11-25 14:10:49 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-25T13:31:33Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7163902747 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7163902747). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-25T13:31:33Z. --- 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 commented 2026-11-25 14:10:50 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-25T13:33:12Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:comment:7163942711 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7163942711). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-25T13:33:12Z. --- @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.
cafeoh commented 2026-11-25 14:10:50 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by cafeoh on 2026-11-25T13:38:33Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7164076081 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7164076081). Originally posted by [cafeoh](https://github.com/cafeoh) on 2026-11-25T13:38:33Z. --- 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.
HailLucifer666 commented 2026-11-27 09:39:06 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-27T09:39:06Z.


@cafeoh — thank you for the retest. The save loading is the question this PR existed to answer, so a clean load is the result I wanted, even from one run.

The part still open is the memory side, which the prologue may simply not exercise. Could you take one run that reaches the open world or drives, and report the trail line 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 is how many appear while driving, because that is where streaming happens.

One run is enough to tell whether the saving exists at all; five are only needed if it does not. If the build writes VICTORY_FRAME_LOG=1 too, attaching that log costs nothing and helps.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7214931824 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/432#issuecomment-7214931824). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-27T09:39:06Z. --- @cafeoh — thank you for the retest. The save loading is the question this PR existed to answer, so a clean load is the result I wanted, even from one run. The part still open is the memory side, which the prologue may simply not exercise. Could you take one run that reaches the open world or drives, and report the trail line `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 is how many appear while driving, because that is where streaming happens. One run is enough to tell whether the saving exists at all; five are only needed if it does not. If the build writes `VICTORY_FRAME_LOG=1` too, attaching that log costs nothing and helps.
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/432:upstream-pr/432'
git switch 'upstream-pr/432'

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/432'
git switch 'upstream-pr/432'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/432'
git switch 'upstream-pr/432'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/432'
git switch 'main'
git merge --squash 'upstream-pr/432'
git switch 'main'
git merge --ff-only 'upstream-pr/432'
git switch 'main'
git merge 'upstream-pr/432'
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
fxceless/victory-fork!432
No description provided.