Texture compaction: empty mostly empty pool blocks when the card or the PC is short of memory #4581
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4581"
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?
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mcozcan on 2026-11-30T23:12:05Z.
What it changes
A 64 MiB texture block costs the card (and, on NVIDIA, the process's commit, see #4288) in full however little of it is taken. In a long session the blocks fill with holes: 1.0 to 1.5 GB free inside texture blocks all through a 50-minute run on a 12 GB card, while the card was over its budget. Letting textures go by age leaves those holes, so no memory comes back, and #4288's texture-line cut rightly stands still while the blocks hold that much free space.
What gives memory back is a block emptied whole. This adds a compaction step to
resource::let_go_idle:VICTORY_COMPACT_COMMIT_FLOOR_MB(4096 by default; the same floor as #4288'sVICTORY_TEXTURE_COMMIT_FLOOR_MB, kept apart so either can land first) or the card's use is over its budget, and only with at least 512 MiB free in texture blocks; at most one look every 5 s. The commit is counted as #4288 counts it, without what the page file may still grow by (#3443).pool::sparse_texture_blocks, and lets go of the idle textures in them (idle for 3 s, held by their title texture alone: no list, no slot), exactly as the sweep lets an idle texture go (unstage,wanted,used,retire). Emptied blocks then go back through the pool's usual spare policy.pool::block_ofnames the block a resource was cut from through itsReclaimprivate data (GetPrivateDataadds a reference, released at once; the resource keeps its own).VICTORY_COMPACT_SPARSE=0turns it off.Whole blocks (
VICTORY_COMPACT_WHOLE=1, off by default;9fd549f8ande6cac2f6)@CuteBooty's runs (below) showed the per-texture rule letting a lot go and giving little back: when one member of a sparse block cannot go (a slot or list holds it, it was read lately, the title wrote it), the block stays, and the members let go beside it are made and uploaded again at their next use. As @Jesper-Andersson suggested, with this switch a block's members go only when every one of them can:
HostTexture::piece), so a round makes no D3D12 call for them.usedread again under the lock, not written by the title, references one, same block), and the block is taken only if the checked bytes are exactly what it holds now; otherwise it is given up and the next one is tried.pool::mark_draining):Pool::cuttakes it after every other block with room (still before making a new block), so it is not filled again while its last pieces are retired and rest. Without marks the order is as before.pool::room), so what it empties goes back to Windows also below 85 % of the card's budget, where they would keep up to 64.VICTORY_COMPACT_SPARES=0leaves the spares as they are, to measure the whole-block rule apart from this.texture compaction, whole blocks) gives per round: looks and time, textures skipped because their lock was busy, candidate blocks, blocks with every member free to go, emptied and given up, and the MB held back in candidate blocks by reason (held by a slot or list / read lately / written by the title). Both compaction lines now also give the textures compaction let go that were made again since the start (COMPACT_COMEBACK), to compare the two rules' re-upload cost.The decisions are pure functions in a new module,
drain.rs(not Windows-only), with unit tests.How it was tested
VICTORY_SAMPLED_EVICT=1):VICTORY_COMPACT_COMMIT_FLOOR_MB=99999; card at 92-96 % of its budget): 5,514 idle textures (654 MB) let go; 12 texture blocks (768 MB) released to Windows, all in the first ~12 minutes (an upper bound: the counter includes blocks the sweep emptied); none in the last 22 minutes although ~330 MB more was let go; the texture blocks never shrank (43-46 blocks), and 6 to 17 blocks under a quarter full stayed all session. No cost seen in frame times.9fd549f8,e6cac2f6): not run in the game yet. Two arms on an 8 GB card would tell whether it helps:VICTORY_COMPACT_WHOLE=0and=1, same route, 30 minutes each,VICTORY_POOL_STATS=1,VICTORY_LOG=info; compare blocks released, the texture block total over time, textures made again, and what holds the candidate blocks.cargo test -p victory-gpu --libpassed for the first two commits, with a new test (block_of_names_the_block_and_keeps_the_count: the block and pool named, the reference count unchanged after 1,000 calls, the piece back in the pool once the resource goes,Nonefor a committed resource). The two failureslist::thin::tests::bindings_record_no_workandshader_cache::tests::every_switch_of_the_translator_is_in_the_keyfail onmaintoo (#4296).e6cac2f6(this branch's head; the same tree CI ran on before its commit messages were reworded), this repository's CI workflow passed onwindows-latest: both clippy steps andcargo test --workspace --locked(run, run in my own repository because the runs here wait for approval).drain's 7 unit tests also pass on Linux.Checked for overlap
let_go_idlenear the card rule), #1562 (block-aware idle eviction, older and much larger), #4454 (first-upload admission scans), #4219 (index non-full pool blocks; touchesPool::cut), #4558 (the sweep on a thread of its own; this runs insidelet_go_idle, so it would move with it).Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by tyr0nsur on 2026-11-30T23:21:34Z.
There is a second trace that meets both gates for a useful test of this draft. The matched RTX 4050 Laptop 6 GiB run in #4309 has 723–799 MiB free in texture blocks while local use is 5,665–5,697 MiB against a 5,114–5,148 MiB budget. In its last 10 samples the dynamic sweep cap remains above its configured soft cap, so #4339 would leave the limit unchanged there. That makes this route a useful compaction-only comparison if the reporter can test a build.
One detail that would make the comparison easier to interpret:
compact_sparselogssparse.len()as the number of blocks the evictions came from. That is the size of the candidate set, including blocks the bounded scan never visited and blocks with no eligible texture. Could the line distinguish candidate blocks, blocks actually touched, and backing MiB returned once retirement drains? The last quantity is the result we care about here, and #3766 already instruments blocks released from both texture pools.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mcozcan on 2026-11-30T23:48:12Z.
Thanks, good catch. The line counted the candidate set, not what was touched or returned. Updated in
506c283e: it now reports candidate blocks, blocks a texture actually went from (and whether the scan stopped at its bound), and the texture blocks released to Windows since compaction first acted, in blocks and MiB, from a new texture-pools-only counter inpool.rs(TEXTURES_LET_GO; both texture pools share theSPARESbudget, so staging releases are left out). Releases land after the textures' retirement drains, so a later line shows what an earlier look gave back; it counts every release since the first look, sweep included, so it is an upper bound on compaction's share. The #4309 trace sounds like the right comparison; I can't run it on that laptop, but if the reporter can test a build I'm happy to help read the log.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Jesper-Andersson on 2026-12-01T01:33:15Z.
A data point from an 8 GB card, and a question, as promised in #4462.
Setup: RTX 2070 SUPER 8 GB, Series S, DLSS Performance, FSR 3 frame generation, no texture cap. Our fork's build, with this PR ported at
e7ebbcfb(before506c283e), so without the newTEXTURES_LET_GOline. Driver 621.40, ReBAR off, Ryzen 7 5800X, 32 GB RAM, Windows 11. One 35-minute session, not repeated; other cards and Series X not tested. The card was over its budget for almost all of it, so the 50 % line applied.What we saw:
Since our port has no release counter, "no block came back" is read from the flat block total, not counted directly.
Reading the code (not measured), the candidates for what keeps a sparse block alive:
We're adding a per-block count of what holds each block (slot, list, host-written, young, idle) to our build, and will post it here. Would a rule that lets a block's residents go only when all of them can go, sorted emptiest first, fit your design? If it helps, I can offer it as a small follow-up behind its own switch, after your counters show what our census shows. It's your design, so your call.
Drafted with AI assistance (Claude Code).
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by CuteBooty on 2026-12-01T03:11:13Z.
A short run of this PR on its own trigger, no forcing: card over budget. RTX 3070 8 GB, both commits cherry-picked onto v2026.11.30.01, Series X profile at 1080p, DLSS Performance, DLSS frame generation x2, upstream's play settings plus
VICTORY_SAMPLED_EVICT=1. Six minutes in the world. Closed normally, no driver events.For comparison, the 34-minute forced session on the Series S profile is in #4462: 12 blocks (768 MB) released in the first 12 minutes, then none, and the texture blocks never shrank. Here, on the real trigger, the pace looks similar at the start. Six minutes is too short to say whether it keeps going.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mcozcan on 2026-12-01T06:51:24Z.
@Jesper-Andersson yes, that fits, and I'd welcome it as a follow-up behind its own switch. @CuteBooty's runs make the case: in the 34-minute session compaction let go 654 MB, but blocks came back only in the first ~12 minutes (12 at most, 768 MB), and the last ~330 MB it let go returned nothing. As written, compact_sparse lets go of every idle texture in a sparse block whether or not the block can empty, so when a slot or list still holds one member, the rest are made again at their next use for no gain. Letting a block's members go only when all of them can, emptiest block first, should stop that.
On your candidates: 1 (slot-held) is skipped by design here; 4 (spare) goes through the pool's usual spare policy, so one emptied block can stay; 2 (placement refilling a sparse block) I haven't measured, and your per-block census would settle it.
@CuteBooty thanks for both runs, and for the counter check. I'll fold the numbers into the PR description; it still says "ran once, 30 MB" from my 12 GB run.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mcozcan on 2026-12-01T09:37:45Z.
@Jesper-Andersson I put the whole-block rule into this PR instead of leaving it for a follow-up, so there is no need for yours. It's in
9fd549f8(ande6cac2f6, a clippy fix), behindVICTORY_COMPACT_WHOLE=1(off by default; off, the per-texture rule runs as before):VICTORY_COMPACT_SPARES=0turns that part off).Not run in the game yet. The CI workflow passed at
e6cac2f6(clippy, both builds, andcargo test --workspace), run in my own repository since the runs here wait for approval. The description has the details and @CuteBooty's numbers.@CuteBooty if you have time for it: two runs on the same route, 30 minutes each, with
VICTORY_POOL_STATS=1andVICTORY_LOG=info, one withVICTORY_COMPACT_WHOLE=0and one with=1. Your forced setting (VICTORY_COMPACT_COMMIT_FLOOR_MB=99999) works for both. The lines to compare are blocks released to Windows, the texture block total over time, "let go by compaction and made again", and in the=1run the "kept in candidate blocks" split.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by tyr0nsur on 2026-12-01T13:13:16Z.
Code review location:
crates/victory-gpu/src/resource.rs; line 4474The last successful drain can be invisible in the play log. The pool increments
TEXTURES_LET_GOafter retirement and the 2.5 s piece rest. If that release restores headroom,compact_planstops scheduling rounds and this line never prints the new total. The throttle can also skip the successful round itself (e.g. round 13).On
e6cac2f6, a local synthetic D3D12 test on RTX 4060 confirmed the counter timing: with one texture still in the regular block, its count stayed flat. After the last texture went and the pool reclaimed, each of the regular and small texture blocks added 64 MiB. A separate 64 MiB staging release only increased the all-pools counter. The unchanged branch's pool and drain test selection also passed (38 passed, 3 ignored).Could the existing periodic pool or memory line emit the texture-only cumulative count when it changes, even after compaction stops? This would let the #4309 retest tell whether the last drain returned a block. The count measures the pool dropping its block reference. The DXGI samples still measure the adapter's reported usage. The periodic reporting in #3766 is related.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mcozcan on 2026-12-01T13:17:08Z.
Code review location:
crates/victory-gpu/src/resource.rs; line 4474Thanks, and for checking the counter's timing on the card. Done in
b697bcba:texture compaction: texture blocks released to Windows since compaction first acted: N (MB ...)whenever the count changes, once compaction has acted at least once. It is printed whether or not compaction still asks to look, so a release that lands after the last round, and gives the room back that stops further rounds, still shows. It sits next to the slot eviction line (resource::compaction_released_line, called from the same place asslot_evict_line).COMPACT_BASE), so all three lines count from the same first look.As you say, it counts both texture pools and the sweep's releases. The line says it is an upper bound on compaction's share, and it measures the pool letting its block go, not what DXGI reports. Not built on Windows or run in the game yet. For the #4309 retest, this line, the
memory now:lines and the DXGI samples over the same minutes should show whether the last drain returned a block.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by vi-tester-8gb on 2026-12-01T15:33:49Z.
Field report from an 8 GB laptop card, played by hand: one 79-minute session with the whole-block rule on, ending in a crash I haven't seen in any other run.
PC: RTX 5060 Laptop GPU 8 GB (driver 621.24), i7-13620H, 32 GB RAM, Windows 11.
Build:
v2026.11.30.03+ #4206 + #4294, with this PR atcef63b9cand #4880 on top, local--releasebuild. Series S profile, DLSS Performance, DLSS FG 2x,--fps 16,VICTORY_SAMPLED_EVICT=1,VICTORY_WAIT_RESIDENT=textures,VICTORY_VRAM_BUDGET_MB=6400(so "over budget" means over 6400 MB, about 600 MB under the real ~7000 MB),VICTORY_COMPACT_WHOLE=1. Driving and walking around the city.What compaction did in 79 minutes (from its own log lines):
since the start 2 emptied, 30 given up, 4 textures (6 MB) let go.texture blocks released to Windows since compaction first acted: 1 (64 MB).The end: that one block release was logged at
15:23:00. At15:23:32the process stopped with:No
DEVICE_HUNG, no nvlddmkm event, and the card had ~470 MB of real headroom. I searched all 22 session logs I have (VIctory forks, t30/t303 builds among them): this error appears only in this one. That isn't proof. It's one crash, and I don't have a debug-layer run. But the timing (32 s after the only block this PR ever handed back) makes me suspect a list still referencing something in the released block. If it helps, I can run a diagnostics build withVICTORY_D3D12_DEBUG=1.For comparison, without the cap and without this PR (otherwise the same settings), two long sessions crept up to the real budget at roughly 70-90 min, and one ended in
DEVICE_HUNG. Next we'll play with the cap alone (no compaction) to see whether the cap is enough on its own.Written up with help from Claude (AI); numbers are from our own logs.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mcozcan on 2026-12-01T16:01:34Z.
Thanks, this is exactly the kind of run the rule needed. Two things stand out.
0 of N blocks with every member free matches what Remorce saw in #4773 and your breakdown says why: the candidates are kept by "held by a slot or list" and "read lately", not by the rule itself. So on its own the whole-block rule mostly gives up, as designed; what could change that is #4580 (sampled eviction) freeing the index-picked textures first. I'm running three forced arms tonight on an RTX 5070 12 GB (per-texture, whole, whole with
VICTORY_SAMPLED_EVICT=0) to separate the two, and will post the numbers here.The crash 32 s after the only block release is the part I take most seriously. One crash isn't proof, but a released block followed by an invalid command is the failure the rule must never cause. Yes please: a diagnostics build with
VICTORY_D3D12_DEBUG=1(andVICTORY_D3D12_DRED=lightif you can afford it) withVICTORY_COMPACT_WHOLE=1would tell us whether the command names a resource in that block. In the meantime I'll go through the release path again (texture retirement vs the block's heap going back to Windows, and anything that can still name a view into it) and report what I find.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mcozcan on 2026-12-01T16:12:36Z.
@vi-tester-8gb as promised, I went through the release path on
cef63b9c(code reading only, nothing run). I found no way for it to leave a host list naming a texture that is gone. Here is the path:compact_keptpasses it (resource.rs:4333-4347): idle, not written by the title, noArcheld by a list, and a D3D12 reference count of 1. It checks this again for every member under that member's lock just before letting go (resource.rs:4446-4461). Then it hands the copies tocrate::host::retire(resource.rs:4487-4489), the same call the sweep uses.retire(host.rs:990-1003) drops an object only after every submission started or queued at that moment has finished, plusbury_lag(host.rs:1028, 64 submissions by default). That lag is there for lists that are recorded but not yet sent.give_back_to,pool.rs:1329). It then rests forREST= 2.5 s (pool.rs:171).reclaimlets a block go only when it is wholly free (pool.rs:508-540), so only after all of the above has happened for every piece in it.The one thing
VICTORY_COMPACT_WHOLE=1changes on that path is the spares cap (pool.rs:59). While compaction is asking to act, the texture pools keep at most 2 empty blocks instead of up to 64. So blocks the sweep emptied also go back to Windows, and new blocks are made more often. Your "1 (64 MB)" counts those releases as well (the counter is an upper bound on compaction's share). And in your run compaction itself let only 4 textures (6 MB) go in 79 minutes.What the error says:
0x80070057fromClose(host.rs:11206) means the runtime refused the arguments of a command recorded into that list. It is not a page fault or a device removal. The cases of it this code has met before were copies into a level that a remade host copy doesn't hold (list.rs:1889-1895, the test atresource.rs:9791-9795). Neither of those is on the release path. That fits the crash not coming from the block release, but it doesn't rule it out. One crash and no debug layer can't settle it either way, so theVICTORY_D3D12_DEBUG=1run is still the way to find out, and the command it names will say whether a resource from a released block is involved. If it is, that's a bug here, and I'll fix it in this PR.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by CuteBooty on 2026-12-01T16:23:37Z.
@mcozcan thanks for folding the numbers in and for the whole-block rule. I can't do supervised route runs today, so the two 30-minute runs will have to wait for another day. I'll post them here if I get to them.
Standing still wouldn't answer your question: on my runs, all the blocks compaction released came out in the first ~12 minutes of driving, and a stationary session loads almost nothing new after the first minute.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Jesper-Andersson on 2026-12-01T16:35:44Z.
A short one from an 8 GB card at
cef63b9c: compaction never acted, so this run says nothing about the whole-block rule yet. It also ended in a crash. I can't tell whether it relates to this PR; no block was released before it, unlike vi-tester-8gb's run.Setup: our fork's diagnostics build with this PR (to
cef63b9c) and #4580 (to732ee452) cherry-picked onto it, not your branch on main.VICTORY_COMPACT_SPARSE=1,VICTORY_COMPACT_WHOLE=1,VICTORY_SAMPLED_EVICT=pressure,victory play's memory defaults otherwise. RTX 2070 SUPER 8 GB (driver 621.40, ReBAR off), Ryzen 7 5800X, 32 GB RAM, Windows 11 Pro Insider 10.0.26320. Series S, DLSS Performance, FSR 3 frame generation. One free-play session, 24 minutes in the world before the crash. GPU clocks locked at 1890 MHz.Compaction: no compaction line in the whole log, and the frame-rate lines never printed "released to Windows since compaction first acted". The card never went over its budget (closest -341 MB of 7,048 MB; #4580 was on, numbers there), so the card trigger never fired, and no line shows the commit trigger firing either. No log line shows either the whole-block rule or the spares cap acting (spare empty blocks: at most 2 in this run and in the census run without it).
The crash: at world + 24.2 min the device hung (
0x887A0006, DEVICE_HUNG), and Windows then stopped with bugcheck 0x10E (VIDEO_MEMORY_MANAGEMENT_INTERNAL, parameter 1 = 0x2e); no dump was written. Our own line at the loss says the submitting thread was between submissions, nothing of ours on the card, for 38,023 ms before the device went. The cause isn't isolated:0x80070057, with no device hang);No storahci or disk events around the crash.
If 8 GB is still an open question after your 12 GB arms, we'll rerun with compaction forced (
VICTORY_COMPACT_COMMIT_FLOOR_MB=99999) on a debug-layer build (VICTORY_D3D12_DEBUG=1), with GPU clocks unlocked.Spec dependence:
Not tested: compaction acting (per-texture or whole blocks);
VICTORY_COMPACT_SPARES=0; release build; Series X; other cards; DLSS frame generation; a second session.Drafted with AI assistance (Claude Code).
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mcozcan on 2026-12-01T17:03:14Z.
@Jesper-Andersson thanks. I agree it says nothing about the whole-block rule, and the code confirms that more strongly than "it never logged". On
cef63b9c,compact_planreturns early unless the card is over its budget or the commit is under the floor (resource.rs:4106), and it setsCOMPACT_ASKED_ATonly after that (:4118). The spares cap (pool.rs:59) depends oncompacting_whole()(resource.rs:4270-4277), which needsCOMPACT_ASKED_AT.compact_whole_blocksruns only from a plan. So in your run,VICTORY_COMPACT_SPARSE=1/VICTORY_COMPACT_WHOLE=1did nothing beyond that check. The spares you saw (at most 2) are the pool's usual policy, and the hang can't come from a block this PR released or a cap it set.Before the forced run on a debug-layer build, one thing to know. A local review found an issue in whole-block mode.
compact_whole_blockstook the texture pool locks with a blocking lock and ran its check phase without a time bound while holding the texture table. With the sweep on its own low-priority thread (VICTORY_SWEEP_OFF_THREAD=1, #4558), that could make a title thread wait. The fix usespool::lock_now(try-lock) intexture_block_state/mark_draining/texture_block_statesand bounds the check loop byCOMPACT_MOST_US. It's in testing here and I'll push it to this PR after that. If you're not in a hurry, the forced run is most useful on that head. I'll say here when it's up.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:36:55Z.
Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.
Pull request closed