gpu: fence-safe LRU texture eviction and budget-aware mip clamping #1286
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1289"
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 fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-26T11:01:50Z.
Summary
Addresses VRAM exhaustion ("no room on the card" -> refused texture uploads and black surfaces, plus streaming frame-time spikes) on 8–12 GB VRAM GPUs without overlapping #45 (
draw.rsis untouched):Fence-safe LRU placed/committed texture eviction under DXGI budget pressure (
VICTORY_TEXTURE_LRU_EVICT, default on;=0disables):last_used_frameandlast_fence_valueonHostTexture, stamped beforeExecuteCommandListsinHost::submitalongsideHost::last_signalled.evict_lru_textures_with) over title-filled (fills && !host_wrote) textures when hitting the VRAM budget cap or OOM, requiringlast_fence_value <= completed_fence,Arc::strong_count == 1, COMreferences == 1(by default never pinned in any live shader-visibleHostHeap::keptslot), and a 1,000 ms / 2-frame idle window. All staging and COM references are released outside locks, andHOST_TEXTURES/KINDS/MIP_CLAMP_STATSupdate synchronously even underVICTORY_REAPER=1.VICTORY_TEXTURE_LRU_HEAP_EVICT=1, default off so bindless textures bound once into shader-visibleHostHeapslots and read continuously by shaders are never evicted by default): underHost::submittedwhen no GPU submissions are in flight (evict_if_pressured), safely unmakes cold shader-visible title heap slots and untaken dynamic descriptor-table slots (descriptor::evict_kept_resources/HostHeap::evict_kept), preserving a 240-frame anti-thrash cooldown acrosstexture_slots_roomand transparently re-creating + re-uploading from host console memory if drawn again later.Budget-aware mip-tail clamping (
VICTORY_TEXTURE_AUTO_MIP_CLAMP, default off — unchanged unless enabled):VICTORY_TEXTURE_AUTO_MIP_CLAMP=1(auto): clamps eligible large mip-chained read-only 2D textures to<= 2048on8 GiB < budget <= 12 GiB(< 85%pressure) and<= 1024onbudget <= 8 GiBor>= 85%effective budget pressure (leaving> 12 GiBcards with headroom unclamped). Passing an explicit dimension (>= 4, e.g.1024or2048) enforces that cap directly.ResourceDesc,footprint(), and console layouts; clamps only the host D3D12 texture (mip_levels > 1,depth_or_array_size == 1,sample_count <= 1, single-plane non-XOR layout,base <= first_tail_mip, and BC4x4block alignment).Staging::detilebybase_mip, validates cachedStagingshape across upgrades, adjusts explicitTexture2D/Texture2DArraySRVMostDetailedMip/MipLevels/ResourceMinLODClamp, and upgrades cleanly viaensure_unclamped(with deferred SRV rebinding inHost::submit) if touched by an explicit GPU copy or resolve.Verification
cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated(default &--features diagnostics): 0 warningscargo test -p victory-gpu --lib -- --test-threads=1: 225 passed, 0 failedcargo test -p victory-gpu --features diagnostics --lib -- --test-threads=1: 244 passed, 0 failedVICTORY_D3D12_DEBUG=1+VICTORY_D3D12_WARP=1+VICTORY_REAPER=1unit & GPU tests covering:VICTORY_TEXTURE_LRU_HEAP_EVICToff (allow_heap_unmake: false): older fence-completed bindless textures held in a live shader-visibleHostHeap(references >= 2) are never unmade or evicted under VRAM pressure (evict_if_pressured), while unreferenced (references == 1) fence-completed idle textures evict cleanlyVICTORY_TEXTURE_LRU_HEAP_EVICT=1(allow_heap_unmake: true) unmakes coldHostHeapand untaken dynamic table slots under budget cap, respects the 240-frame cooldown acrosstexture_slots_room, does not resurrect inmake_waiting(), and transparently detiles/re-uploads updated host bytes on reuse>= 19 MiBmeasuredGetResourceAllocationInfosavings per texture), upload the exact clamped console mips, and upgrade + rebind SRVs cleanly onensure_unclampedcargo run -p xtask -- policy --staged: passedImported from fxceless/victory-fork. Originally posted by Garu667 on 2026-11-26T12:10:21Z.
A/B on the RTX 3080 (same build as our #1279 test + #1287/#1002/#1279, boat mission, ~3 min standing): the room problem goes away (
passes that made none for lack of room5,912 → 100), fps 9.6 → 9.4, VRAM peak 9.3 → 9.4 GB. But the picture regresses: a character's black printed jacket comes out plain grey (its texture replaced), and white/green speckles appear along the hull and the outboard motors; distinct colours per capture ~120k → ~97k. So the eviction (or the mip clamp) seems to drop or lower textures that are still being drawn. No device loss. Happy to rerun withVICTORY_TEXTURE_LRU_EVICT=0to tell the two parts apart.Imported from fxceless/victory-fork. Originally posted by Garu667 on 2026-11-26T12:19:31Z.
Follow-up, same build and checkpoint with
VICTORY_TEXTURE_LRU_EVICT=0(mip clamp still on): the picture is clean again (the black printed jacket has its print, no speckles on the hull or motors), andpasses that made none for lack of roomgoes back to 5,364 (5,912 without the PR). fps 9.4. So the regression comes from the LRU eviction, not the mip clamp: it seems to evict textures that are still drawn. No device loss either way.Imported from fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-26T14:27:39Z.
Thanks a ton @Garu667 for running both A/B captures and isolating
VICTORY_TEXTURE_LRU_EVICT=0+VICTORY_TEXTURE_AUTO_MIP_CLAMP— that pinpointed the exact root cause right away.Root Cause
HostList::stamp_textures()only stamps textures tracked inHostList::states(explicit resource barriers), not bindless textures bound once into shader-visibleHostHeap::keptand sampled continuously by shaders across later frames. Becauseevict_if_pressured()hadallow_heap_unmake: trueenabled by default, VRAM pressure could unmake live shader-visibleHostHeapSRV slots onto the 240-frame cooldown even while draws were still reading them (causing the grey jacket surface and hull/motor speckles).Fix (
16a6f8e, rebased cleanly onto58de9bf)lru_heap_evict_enabled()gated strictly on opt-inVICTORY_TEXTURE_LRU_HEAP_EVICT=1(off by default), and wiredallow_heap_unmake: lru_heap_evict_enabled()inevict_if_pressured()(withNEED_HEAP_EVICTreads/writes gated on it as well, plushosted()'s proactive check gated onlru_evict_enabled()).VICTORY_TEXTURE_LRU_EVICT=1), LRU eviction now only reclaims fence-completed idle textures whose COM reference count isreferences(&host.texture) == 1(never pinned in any live shader-visibleHostHeapslot), while keepingVICTORY_TEXTURE_AUTO_MIP_CLAMPandflush_unclamped_rebinds()intact.default_lru_eviction_preserves_live_host_heap_textures_until_opt_inverifying under D3D12 debug +VICTORY_REAPER=1(hardware & WARP) that liveHostHeap-pinned textures (references >= 2) are never unmade or evicted under pressure by default whilereferences == 1idle textures still evict cleanly.Imported from fxceless/victory-fork. Originally posted by Garu667 on 2026-11-26T15:03:11Z.
Retest of
16a6f8eon the RTX 3080 (58de9bf+ this PR, same boat checkpoint, ~3 min standing, defaults of the PR): the picture is clean now (printed jacket intact, no speckles on hull or motors), no device loss. But the room problem is back too:passes that made none for lack of room4,540, about what the same build shows without the PR (4,547), VRAM peak 9.3 GB. So with live heap textures left alone, eviction finds little else to free in this scene. The earlier 5,912 → 100 came from evicting textures that were still bound. Maybe the textures worth evicting here are bound ones the title has not drawn for a while (last draw frame rather than refcount)? Happy to test the next iteration, and if it's easier to iterate, I'mgaru667on Discord.Imported from fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-26T17:22:17Z.
Updated
perf/texture-vram-lru-mipcapto42fdddc(rebased ontomaine4245f3, mergeable) with a two-tier fix for@Garu667's findings:1. Tier 2 (default ON, zero-corruption superseded-view yield)
When
keep_views()retains an older placed texture copy in a descriptor slot while a sharper view waits,HostHeapnow records that slot inkeeping(clearing it onkeep(),unmake(), or pressure). Under VRAM pressure (!room()or pendingwant > 0),evict_if_pressured()callsdescriptor::host::yield_kept_views()before/alongsiderefs == 1LRU eviction (VICTORY_KEPT_VIEWS_YIELD=0kill-switch, default1). That releases stale superseded copies down torefs == 1so VRAM can reclaim them while leaving every non-superseded liveHostHeap-pinned texture untouched (references >= 2).2. Tier 3 (opt-in
VICTORY_TEXTURE_LRU_HEAP_EVICT=1,last_used_framerecency guard)Directly matching your suggestion to track draw recency instead of only COM refcounts:
HostHeapnow tracks per-slotlast_used_frame, stamped inHostHeap::mirror(..., for_draw = true)andkeep().VICTORY_TEXTURE_LRU_HEAP_EVICT=1(remains OFF by default),evict_lru_textures_withdrains deadrefs == 1textures first, then unmakes coldHostHeapslots ordered by oldestlast_used_frame(skipping taken dynamic slots, non-heap COM pins whererefs > 1 + kept_refs, and any slot touched withinVICTORY_TEXTURE_LRU_HEAP_IDLE_FRAMES, default120frames).30-frame cooldown instead of240frames so re-drawn textures recover quickly without multi-second stalls.Verification (
42fdddc,VICTORY_D3D12_DEBUG=1 VICTORY_REAPER=1)clippy(default &diagnostics): 0 warningsdescriptor::host::(35/35),resource::(19/19)victory-gpu --lib: default236/236(6 ignored),diagnostics255/255(7 ignored)@Garu667 If you have a moment to retest on the 3080, baseline defaults now exercise Tier 2 (
yield_kept_views), and addingVICTORY_TEXTURE_LRU_HEAP_EVICT=1exercises Tier 3 recency-guarded heap eviction as well.Imported from fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-27T07:15:58Z.
Rebased
perf/texture-vram-lru-mipcapontomain(9a9091d, headec3d8a7), guardingVICTORY_GPU_DETILEonbase_mip == 0so mip-clamped host textures fall back cleanly to CPU detiling while preservingalias_ofHeldpinning andview_address.Verified with
VICTORY_D3D12_DEBUG=1:clippyclean (default &diagnostics),texture::3/3,resource::19/19 (default) / 21/21 (diagnostics),descriptor::45/45,draw::3/3, andgpudetile::1/1.Imported from fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-27T20:57:32Z.
Closing as superseded by
2eade68c(VICTORY_POOL_GATE/ texture budget line acrosspool.rs,resource.rs, andshadow.rs, which keeps card allocation cleanly below budget). If a standalone opt-in LRU texture eviction pass on top of2eade68cwould still help low-VRAM cards, I can prepare a compact separate PR on the new base.Pull request closed