GPU: let go of a placed texture's host copy as soon as its memory is unmapped #432
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/fxceless--victory-fork!432
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-432"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@HailLucifer666 on GitHub · 2026-11-25 00:47 UTC · original
HailLucifer666:perf/evict-unmapped-placed-textures→main· +245 −3 · 2 files · 2 commits · openWhat 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 conditionslet_go_idlerequires (Arc::strong_count(host) == 1andhost::references(&host.texture) == 1), under the samemadelock, followed by the sametexture::unstage,host::retire,host.gonemarking andKINDS[2]/KINDS[3]counters.title_memory_remappedcalls it after the views and placements there are forgotten.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=0still lets nothing go; the switch moved into onesweeping()so the two paths cannot disagree. The path is traced throughhost::traillike 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(inresource::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.Scope
crates/victory-gpu/src/resource.rsand the one call incrates/victory-gpu/src/watch.rs, which is wheretitle_memory_remappedis defined (lib.rsonly re-exports it).No test drives
evict_placed_initself: it needs the host device. What is new and testable is the range test; the retention conditions it reuses arelet_go_idle's.@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_incall intitle_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 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_remappedcalledevict_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_remappedno 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_idlealready 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 waitingIDLE_MSandIDLE_FRAMESas 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:33 UTC · original
@cafeoh — the fix for what you measured is pushed as
8f738674on 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_remappedno longer walks the texture table at all. It records the unmapped range — one lock and a merge over at most 64 ranges — andlet_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:
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.title_memory_remappedrather than justevict_placed_in. That call also doesinplace::views_gone,inplace::title_memory_freeingand 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=0still 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 on GitHub · 2026-11-25 13:38 UTC · original
Tested
8f73867on the same machine (RTX 2080 Ti, Windows 10, fork maina253706+ 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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.