WIP: perf(vk): texture RAM cache, a proof of concept with its diagnostics (not for merging as is) #2273
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2273
Loading…
Reference in a new issue
No description provided.
Delete branch "Veix/Longshot:wip/texture-ram-cache"
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?
Summary
A diagnostic and proof-of-concept branch, not for merging as is: a texture RAM cache that removes the GPU re-conversion of evicted textures, behind the drops when the camera turns with a full VRAM budget. Handed over so someone can read it, try it and make a clean pull request of it.
When the VRAM budget evicts a converted texture (ASTC decoded to RGBA8 or re-encoded to BC3, or a texture unswizzled on the CPU) and the game uses it again, it is converted again on the GPU, at about 0.8 ms a MB, in front of the frame's draws. The cache reads the converted result back into RAM (on a thread of its own, without a wait), keyed by the hash of its storage and its shape, and a texture used again is filled by one plain copy from there.
It is one commit: a local test branch of 14 commits (on
main89c3d21e, 2026-11-20) squashed, with currentmainmerged into it. It carries everything the measurements were made with. What belongs to what:The cache (the proof of concept):
RX_VK_TEXRAM_MB=<MB>: the cache's size. Off by default in this branch (0): set it to try it. The measurements used 4096.RX_VK_TEXRAM_RGBA_MB(1.5 by default; the runs used 1000),RX_VK_TEXRAM_RATIO,RX_VK_TEXRAM_STEP_MB(what one read-back takes at most, 8 MB): which textures are worth keeping, and the read-back's pace.vk_texture.cpp: the LRU, the read-back worker and its segment workers, the entries as GPU-read buffers, the whole-upload lookup (a texture uploaded whole again is filled from the cache too).On by default, each worth a pull request of its own (separable from the cache):
RX_VK_HASH_MEMO(=0 off): a texture's hash remembered by its storage and shape; made again over storage none of whose pages was written since, it takes the hash without reading the storage again (a turn in Kokiri hashed about 1.8 ms a frame).RX_VK_DEPTH_ZERO(=0 off): a depth buffer read before the GPU drew it, its storage all zero, is cleared on the GPU instead of unswizzled on the CPU (in Kokiri 15 of 25 such reads).RX_VK_EXACT_STALE(=0 off, the one switch with a row inswitches.def): a resolved texture handle is stale only when its own texture went.Experiments, probably not for a clean version:
RX_VK_TEXCACHE,RX_VK_TEXCACHE_BAKE,RX_VK_TEXCACHE_TAIL,RX_VK_TEXCACHE_TOP_MB(converted textures baked to disk and filled by a copy, small levels first): the idea's first form, before the RAM cache.RX_VK_HOLD_UNUSED,RX_VK_RESERVE_UNUSED,RX_VK_FLOOR_ALL(the hold's ages and the floor on every card),RX_VK_SHADOW_ROUND(_MIN,_MAX: shadow images rounded to fewer sizes),RX_VK_TARGET_BLOCKS_MIN,RX_VK_IMAGE_BLOCK_MB,RX_VK_UPLOAD_BUDGET_MB,RX_VK_UPLOAD_BUDGET_MS,RX_VK_ASTC_BC_FINE_MB,RX_VK_TARGET_DEDICATED_DEPTH(depth targets in memory of their own).HOS_NVN_SYNC_SKIP/HOS_NVN_SYNC_BLOCK(host/nvn_native.c) andHOS_CHAIN/HOS_CHAIN_CAP_M(host/hos_chain.c).Diagnostics only, not for a clean version:
pool_resizedinvk_replay.cpp(rm_*,k_*,cache_*,rt_cpu_ms,job_*,p_*,prof_*,views_*,desc_builds,tg_*): what making textures cost by step and kind, the render thread's own time.rm_create_msand its three neighbours were namedcreate_msand so on before;mainhas columns of those names now, with other values.RX_VK_UPLOAD_TIMING,RX_VK_UPLOAD_REPORT_MS,RX_VK_LOG_UPLOADS(_MIN_KB),RX_VK_LOG_TARGET_MISSES,RX_VK_IMAGE_SPLIT(a render target's image made in three timed steps),HOS_SIZE_TRACE_SQ,RX_VK_SAVE_TEX(_DIR), the render targets' block log invk_vma.cpp, the "target made" log of the first 400 targets.Most of these switches are plain
getenvreads without a row inswitches.def.Testing
Please no automatic fixes on this branch (
@fix, bot pushes): it is a hand-over, and its next form is a clean branch.Tested: Windows 11, RTX 4070 SUPER 12 GB, driver 621.24: the 14 commits on
main89c3d21e, measured on the walking route (below), 2026-11-20. After mergingmain67ba729dit builds (Windows, llvm-mingw); the merge keptmain's new render-target pool path (pool_take_memory,create_image_in_class) and step timers, with this branch's timers and logs around them.Still to test:
main: the walking route at the 60 % VRAM line,RX_VK_TEXRAM_MB=4096 RX_VK_TEXRAM_RGBA_MB=1000against the same build without them (by hand)Measurements
Verdict: on
main89c3d21e, the cache took the second Market stay's frames over 25 ms from 26 to 17 and its late frames from 5.19 to 2.95 %; the castle entry got worse (p99.9 29 to 48 ms).Setup: Windows 11, RTX 4070 SUPER 12 GB, driver 621.24, Ryzen 9 9900X. 3840x2160, the walking route (Castle Town, the castle, Castle Town again), the 60 % VRAM line (
RX_VK_COMPACT_PCT=60, as on a smaller card), 90 fps cap, the frame gate off,HOS_NATIVE_ZSTD=1on both sides. 3 runs against 3, interleaved. "main" is this build with the cache off andRX_VK_HASH_MEMO=0 RX_VK_DEPTH_ZERO=0 RX_VK_EXACT_STALE=0.Not measured on today's
main: about 150 commits came since, several of them on textures and VRAM.Risks
RX_VK_HASH_MEMO,RX_VK_DEPTH_ZERO,RX_VK_EXACT_STALE), and the diagnostics cost time when their switches are set.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.gpu/vk/switches.def.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Possibly related, for context:
Comment only — your handover asks for no bot fixes, and this is the cross-check you asked for against our BC3 caching work.
We built our own implementation of the disk half last week on the Deck LCD bench, before seeing this branch: same content key (the
reuse_uploadXXH64 plus shape), same writer discipline (temp+rename, background thread), but the file is written by one copy-out at the texture's first whole upload rather than a later-present bake, so nothing in the frame path waits. Deck numbers, including post-#2194, are in our comment on #2292 rather than repeated here; short version: after #2194's 5.3x decode speedup the warm disk cache still takes −42% hitch frames and −61% bytes staged on a Vangogh, and 62 minutes of normal play ran at 0.067% frames over 150 ms.Where that leaves your branch, part by part:
The disk variant (
TEXCACHE/_BAKE): you flag it as the idea's first form and it regressed castle entry; our read is that the bake-with-GPU-wait path is what bit you there. Since #2292 already carries the same idea with a stamp, checksum, cost model and size policy, a clean branch of this handover doesn't need to carry the disk half at all. It's being done properly over there.The RAM cache is the part nobody else covers — and our Deck data says nothing about it, honestly: our arms never press the VRAM budget (the texture census sat at 2,096 MB against a 2,560 MB budget, no eviction churn), so the evicted-reconversion path your cache targets never fired on our route. The trace frames your ~0.8 ms/MB re-conversion cost exactly as you describe, so the bug class is real; it just needs a churn scenario to show it, not an entry scenario.
Castle-entry regression (p99.9 29 → 48 ms): reads like read-backs competing with the load's own uploads. Your
_STEP_MBand_TAILknobs point the right way; when a clean branch paces read-backs against the load, the castle arms here are ready.The three separable switches (
RX_VK_HASH_MEMO,RX_VK_DEPTH_ZERO,RX_VK_EXACT_STALE) read as straightforward and Deck-relevant — hashing and CPU unswizzle work sit in the same stall profile we keep measuring on Vangogh, though we didn't isolate either cost separately in our traces.One Deck caution for the RAM tier's sizing: on 16 GB of shared LPDDR5 with the emulator already swapping several GB, a 4 GB
TEXRAMdefault sized from a 12 GB discrete card wants a Deck-sized default, and on Linux a RAM tier double-stores bytes the page cache already holds warm.Offer: when the clean branch (or just those three switches) rebases onto main, we'll run it on a Deck LCD with scripted Castle Town arms, cold and warm, with traces.
Deck LCD test of this handover at pinned
c8430046: it works on the Deck, and the three separable switches are the strongest Deck signal in the branch. Bench claim posted to team updates at 18:45; nothing was pushed to your branch, everything below ran your pinned head plus one bench-local build shim.Build, Linux. One error on Linux: the frame-trace block at
vk_replay.cpp:5046callsQueryThreadCycleTime(GetCurrentThread(), &cyc)with no_WIN32guard (ULONG64,GetCurrentThread: Win32 APIs; the file guards every other such block with#ifdef _WIN32, the PR lists Windows builds only, so Linux was likely never attempted). The bench shim (a local commit over your pin, not a push):CLOCK_THREAD_CPUTIME_IDunder#ifndef _WIN32,ns_per_cycle = 1.0, keeps thert_cputrace column live. A clean branch needs the guard, or the column goes. Renderer-only Deck build is otherwise clean; it runs on our v3.1.1 host unchanged (ABI fine).Setup. Steam Deck LCD (Vangogh, 16 GB shared LPDDR5), Castle Town from the Market save, scripted walk+turn, 20 cycles,
RX_VK_FRAME_LOG, hitch = frame over 60 ms. Churn scenario:RX_VK_COMPACT_PCT=60against the Deck's ~2,560 MB allowance with a ~2,100 MB working set. Note for your notes:GLOBAL_MIN_GRAPHICS_REQis 8,192 MB, so the dynamic-floor pathRX_VK_FLOOR_ALLforces on big cards is already live on the Deck, no test switch needed there.The cache under churn (cache off vs 4096 MB vs 1536 MB, same scenario):
Real but modest on this hardware: −12% hitch frames, −13% slow-frame seconds, ~900 decodes removed. Smaller than your 4K/12 GB scenario because the Deck working set nearly fits the allowance, so churn is gentler than your forced 60% line.
Sizing on shared memory: the 4096 number does not port. With
TEXRAM_MB=4096the Deck's swap grew from 1.1 to 4.5 GB during the arm. The 1536 arm matched 4096's frame effect (136 vs 129 hitches, within noise) at a third of the fills, with fills costing only ~70-100 ms per 20-minute arm either way. On 16 GB UMA, 1024-1536 looks like the right ceiling for a clean branch default.The mechanism fires without churn too. Default budgets,
TEXRAM_MB=4096: fills of 1,099 textures / 366 MB, and ASTC decodes fell 3,209 to 1,233, so the RAM cache is catching re-created textures in-session, not only evicted ones. Frame effect at default budgets was within boot-to-boot variance (paired boots: 219 vs 142 hitches, repeat pair 175 vs 212, honest read: no clear frame win without churn), but the decode-skip itself was consistent across both pairs (roughly −1,850 decodes each). Worth keeping the whole-upload lookup in the clean version.The three separable switches are the best Deck numbers here. Same churn scenario, cache off in both arms,
RX_VK_HASH_MEMO=0 RX_VK_DEPTH_ZERO=0 RX_VK_EXACT_STALE=0against them as shipped in your branch:44% fewer hitch frames and 47% less staged data from the switches alone, and the doubling of both conversion kinds with
EXACT_STALE=0matches your own write-up of what that counter does. One pair, so a strong signal rather than a measurement, but on Deck this is the branch's first PR in waiting:RX_VK_EXACT_STALEabove all, the other two alongside or next.Correctness: screenshots at churn with 4096 MB of fills, before and after the scripted camera turn, clean: no magenta or black rectangles, no mosaic, no smeared textures, no wrong colors.
One squash artifact: the trace's
cache_n/cache_mbcolumns stayed at zero in every arm wherek_cache_n/k_cache_mbfired (0 vs 1,229). One of the two is miswired; a clean branch should keep just one.Happy to re-run the churn pair and the switch arms against a clean branch, on this Deck, same protocol. Traces and score JSON archived on our side (18 files, hash-manifested) if you want any raw CSV.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.