WIP: perf(vk): keep an evicted BC-kept ASTC texture's image in system memory, taken back instead of a decode (Texture RAM cache, Auto by default) #3183

Draft
neotoxic wants to merge 15 commits from neotoxic/Longshot:perf/bc-ram-cache into main
Contributor

Summary

Draft. An in-game setting, Auto by default, as HumanBean asked: Graphics > GPU, "Texture RAM cache", Auto / Max / Off. It is for players on smaller cards (4 GB, or 6-8 GB on a slow PCIe link); on a card whose budget never evicts it does nothing.

On a card whose texture budget evicts, an area's ASTC textures kept as BC3/BC7 go when the budget is met, and coming back to that area decodes and re-encodes each of them again. The finalized-texture reuse (#1222) cannot serve them: the image it would copy from is the one the budget just destroyed.

With the setting on, an evicted texture of that kind first has its BC image copied into a buffer in system memory, keyed by its blocks' hash and shape (the same astc_reuse.h key #1222 uses). A texture made again later with the same blocks and shape takes that copy back by one GPU copy, instead of a decode and a BC encode. Copies leave least recently used first. Nothing is kept before an eviction, so a card that never evicts pays nothing, Auto included.

  • The row is texture_ram_cache in the graphics system mod (game/mods/graphics/mod.toml), applied at restart; the renderer reads it from the start-up snapshot. Auto follows HumanBean's curve: nothing under 12 GB of RAM or on an integrated GPU (whose VRAM is that same memory), 256 MB at 12 GB rising to 1 GB at 24 GB and more (16 GB: 512 MB), and never more than a quarter of the memory free when the cap is first read; under 64 MB it keeps nothing. Max is 1 GB, Off nothing. Its switch, RX_VK_BC_KEEP_MB, takes auto, 1024 or 0.
  • Memory pressure gives it all back: when the system runs short (on Windows the commit headroom under its reserve, on Linux the same shortfall that already gives back the capture arena), every kept copy is dropped, the pool is freed once the GPU is done with it, and nothing is kept again until the game restarts (keep_give_back). It goes first, before the textures in use: it only saves decodes.
  • The cap is logged once: vk: Texture RAM cache Auto: 1024 MB at most for evicted BC images (32133 MB of RAM, 17390 MB available).
  • Only images that are exactly the decoded blocks qualify: ASTC kept as BC3 or BC7, hashed at a whole first upload, never drawn by the GPU, plain 2D. A texture uploaded again after its first upload has no hash (reuse_upload), so it is never kept.
  • The copies' memory is one pool of cached host memory (64 MB blocks, one more than the cap needs), made all at once by a thread of its own at the first eviction (keep_memory, create_cached_pool in vk_vma.cpp); until it is there an evicted image is not kept. Before, each copy was a create_buffer_cached on the replay thread, and a new VMA block there cost up to 9 ms (desktop) and 21 ms (laptop) for a single image.
  • Three more switches, all off, for testers with very little to spare (Veix's steps, Measurements): RX_VK_BC_KEEP_PRESENT_MB=<MB> caps the bytes one present copies (an evicted image past it is not kept), RX_VK_BC_KEEP_QUARTER=1 keeps none over a quarter of the cap, as #2273 does, and RX_VK_BC_KEEP_AHEAD_MB=<MB> copies about that much a pass of the eviction of the BC images unused as long as an eviction asks, while they are still in VRAM, so that their eviction copies nothing (Veix's plan A).
  • Once something was kept, two lines join the ASTC lines every 600 presents:
vk: BC images kept in system memory: 1752 evicted ones kept (1199 MB), 1504 taken back (1027 MB), 1478 not found, 1 without memory; 1502 held (1020 of 1024 MB)
vk: BC images kept, the copies: 51 presents copied (188 MB at most in one), 0 left over the present's budget, 11 before their memory was made; keeping took 11 ms of CPU (904 us at most once)

The idea of keeping transcoded BC blocks in RAM comes from HeyListen (Sacrappy). No code was taken; the code is Longshot's own.

Read first: gpu/vk/bc_keep.h (the cap and Auto's curve, the sizes, the kept copies, the per-present budget), then keep_cap, keep_memory, keep_store, keep_take, keep_frame and keep_give_back in gpu/vk/vk_texture.cpp, after reuse_upload, its two calls under memory pressure in texture_frame, create_cached_pool / destroy_cached_pool in gpu/vk/vk_vma.cpp, and the row in game/mods/graphics/mod.toml.

Testing

Tested:

  • CachyOS, RTX 3060 Laptop 6 GB (texture budget 3584 MB), PCIe 3.0 x8, display wired to the dGPU: dev.py ab --a RX_VK_BC_KEEP_MB=0 --b RX_VK_BC_KEEP_MB=1024 --runs 3, 150 s a run spent teleporting every 12 s through Market, Kakariko, Kokiri Forest, Lost Woods and Hyrule Field (child). Each B run kept 1,718 to 1,816 evicted images and took 1,314 to 1,453 back (1.0 to 1.1 GB); no texture looked wrong on screen, no FATAL line.
  • Windows 11, RTX 5080, the same tour with --set RX_VK_VRAM_MB=3584 (the laptop's budget): 998 to 1,131 taken back a run (1.0 to 1.3 GB), no FATAL line.
  • Checks: tests/test_bc_keep.py covers the cap (unset, a number, auto, integrated GPU), a BC level's bytes, and the kept copies (found by blocks and shape, least recently used out first, never one over the cap, a quarter of it with RX_VK_BC_KEEP_QUARTER, room made before a copy), and the per-present budget (each present from nothing, exact fit, one image over the budget never kept). It also checks the source: the copy before the evicted texture's end, the take-back at a first whole upload, the frame's clean-up. tests/test_readback_timeout_reuse.py's fixture stubs keep_store, the eviction's new call. run_all.py --changed origin/main on Windows: 124 passed, 2 failed, 11 skipped; test_image_view_retry and test_tex_desc_memo fail the same way on main on this PC.
  • On main 3a0f7d8f with these steps: dev.py rt then dev.py smoke PASS on Windows; run_all.py --changed origin/main 164 passed, 1 failed (test_dlssg, which fails on main too).
  • The setting (281e41aa and 652c92d7, on main de6fb7b5): dev.py rt then dev.py smoke PASS on Windows (in play from kokiri-start, 900 frames, the menu opened); the A/B in Measurements, step 6, on both machines (281e41aa). tests/test_bc_keep.cpp covers Auto's curve (8 and 11 GB, 12, a 16 GB PC reporting a little under and the laptop's 15832 MB, 24 and 64 GB, RAM unknown, integrated GPU, the quarter of the free memory, under 64 MB) and Max / Off; tests/test_bc_keep.py checks the source: the cap read from the snapshot, the give-back on both systems' pressure, the pool freed after it. test_bc_keep, test_menu_ini_keys (the new key in FROZEN), test_readback_timeout_reuse (its stubs brought up to the eviction's calls), test_switch_table and test_switches_removed pass; schema-gen --check passes. test_i18n_complete and test_settings_area_keys fail on this branch exactly as on main at de6fb7b5 ("Fill title screen" untranslated, mouse_aim_sensitivity); the new row's five texts are in all 13 languages.

Still to test:

  • A 4 GB card, NVIDIA or AMD, where the budget evicts hardest (@Beanstalk ab --a RX_VK_BC_KEEP_MB=0 --b RX_VK_BC_KEEP_MB=auto --runs 3 on a 4 GB runner, with the vk: BC images kept line)
  • An 8 GB card on PCIe x4, the case HeyListen measured a gain on (@Beanstalk ab --a RX_VK_BC_KEEP_MB=0 --b RX_VK_BC_KEEP_MB=auto --runs 3 on a x4 runner)
  • A small card with little RAM to spare (4 GB card, 8 GB of RAM, where Auto keeps nothing): Max against Off, then the light switches on top of Max (@Beanstalk ab --a RX_VK_BC_KEEP_MB=0 --b RX_VK_BC_KEEP_MB=1024 --runs 3, with both vk: BC images kept lines; then --a RX_VK_BC_KEEP_MB=1024 --b RX_VK_BC_KEEP_MB=1024 --b RX_VK_BC_KEEP_PRESENT_MB=8 --b RX_VK_BC_KEEP_QUARTER=1 --runs 3)
  • A tester on a 4-6 GB card with 16 GB of RAM or more: play a while at Auto, going back and forth between areas, then at Off, and say whether returning to an area hitches less (by hand, a tester)

Measurements

This PR adds the Texture RAM cache setting, which ships at Auto, as HumanBean asked (step 6).

Verdict: it works as meant (a gigabyte of images a run taken back instead of decoded and re-encoded), but brings no gain on either machine here, and no loss since the copies' memory is made out of the frame (step 3): Auto against Off is within run-to-run spread on both (step 6). The gain, if any, is on smaller cards, where decoding and encoding cost more: "Still to test" has the farm runs. The first tables below are from before the setting, with the switch.

Baseline: the same build with RX_VK_BC_KEEP_MB=0 (ca63a459's code with the old default, which was on), not main. The switch is the only difference, so the comparison isolates it.

Setup: dev.py ab with the tour above (TOUR_DESTS stepping through five areas every 12 s), 150 s measured a run, 3 interleaved runs a side. The medians, each run in brackets.

RTX 3060 Laptop 6 GB, CachyOS, PCIe 3.0 x8, budget 3584 MB (its own):

Before: this PR @ ca63a459, RX_VK_BC_KEEP_MB=0 After: this PR @ ca63a459, RX_VK_BC_KEEP_MB=1024
fps 64.0 (65.7 / 62.4 / 64.0) 62.9 (63.0 / 62.9 / 62.0)
1% low fps 14.6 (15.4 / 13.9 / 14.6) 14.4 (13.6 / 14.6 / 14.4)
frames of 25 ms or more 419 (419 / 426 / 390) 408 (408 / 408 / 448)
frames of 50 ms or more 147 (134 / 167 / 147) 155 (161 / 145 / 155)
longest frame 450 ms (450 / 617 / 380) 331 ms (327 / 331 / 470)

RTX 5080, Windows 11, RX_VK_VRAM_MB=3584:

Before: this PR @ ca63a459, RX_VK_BC_KEEP_MB=0 After: this PR @ ca63a459, RX_VK_BC_KEEP_MB=1024
fps 175.0 (174.5 / 176.7 / 175.0) 175.4 (174.5 / 175.4 / 177.0)
1% low fps 98.5 (98.6 / 98.5 / 98.3) 97.5 (97.5 / 96.6 / 99.1)
frames of 25 ms or more 43 (43 / 43 / 41) 48 (48 / 46 / 50)
frames of 50 ms or more 13 (14 / 10 / 13) 16 (16 / 16 / 15)

Veix's steps (copies out of the frame, #2273)

Each step measured against the one before it, same tour, same machines, 3 runs a side, medians (each run in brackets where it matters).

1. A byte budget per present (RX_VK_BC_KEEP_PRESENT_MB), to test whether the copies made at an eviction are the slow frames there. A: 1024 MB, no budget. The copies come in bursts: on the desktop 13 to 18 presents a run make all of them, the biggest 290 to 530 MB; on the laptop 50 to 64 presents, the biggest 165 to 366 MB.

Desktop, 8 MB Desktop, 32 MB Laptop, 8 MB Laptop, 32 MB
frames of 50 ms or more, A → B 14 → 13 15 → 13 149 (150 / 142 / 149) → 129 (140 / 123 / 129) 143 → 137
frames of 25 ms or more, A → B 47 → 42 41 → 41 382 → 357 388 → 376
1% low fps, A → B 98.6 → 97.3 98.0 → 99.1 14.7 → 17.1 15.1 → 15.8
taken back a run, A → B ~1.2 GB → 110 MB ~1.4 GB → 440 MB ~1.05 GB → 350 MB ~1.1 GB → 810 MB

8 MB on the laptop is the only cell that looks like a gain, and it costs two thirds of what is taken back; step 3's runs put the same A at 136, so it is within run-to-run spread. Kept as a switch, off.

3. The copies' memory made out of the frame (done before 2). A: step 1's build, B: the pool; 1024 MB, no budget.

Desktop A → B Laptop A → B
keep_store CPU time a run 10-17 ms → 1 ms 236-277 ms → 10-11 ms
its longest single call 2.4-8.4 ms → 55-74 µs 16-18 ms → 0.14-0.9 ms
the pool made (on its own thread) 43 ms 270 ms
frames of 50 ms or more 15 → 15 136 → 137
frames of 25 ms or more 42 → 43 400 → 372
taken back a run ~1.2 GB → 745 MB ~1.05 GB → ~1.06 GB

The keeping now costs nearly nothing, but the slow frames at area changes stay: the copies are not what makes them. On the desktop 20 to 22 images go unkept while the pool is made, at the first burst, which is where its lower take-back comes from. In this PR as it is.

2. No copy over a quarter of the cap (RX_VK_BC_KEEP_QUARTER). At 1024 MB a quarter is 256 MB and the biggest copies are about 22 MB, so it was measured at RX_VK_BC_KEEP_MB=128 (32 MB an image). A: step 3.

Desktop A → B Laptop A → B
frames of 50 ms or more 15 → 13 148 → 138
frames of 25 ms or more 43 → 40 375 → 404
taken back a run ~90 MB → ~90 MB 94-166 MB → 57-67 MB

No gain, and a third less taken back on the laptop: the images it refuses are the big ones. Kept as a switch, off, for a tester with very little memory to spare. Also seen at 128 MB: the laptop copies ~1.9 GB a run to take back ~0.1 GB, and 53 to 178 images a run find no room while dropped copies wait for the GPU.

4. Plan A: copied ahead of the eviction (RX_VK_BC_KEEP_AHEAD_MB). Each pass of the eviction first copies about that much of the BC images unused as long as an eviction asks (in the eviction's order), while they are still in VRAM; an evicted image already kept is moved to the front of the LRU, not copied again. A: 1024 MB, B: the same with 4 MB a pass, on main 3a0f7d8f.

Desktop A → B Laptop A → B
frames of 50 ms or more 16 → 18 139 → 136
frames of 25 ms or more 46 (55 / 45 / 46) → 58 (60 / 58 / 56) 378 → 356
1% low fps 83.9 → 82.0 15.8 → 16.1
copied ahead a run 790-830 images (560-590 MB) 2,050-2,290 images (1.5-1.7 GB)
copied at the eviction a run 915-923 → 100-135 images 1,800-1,910 → 200-270 images
taken back a run 720-926 MB → ~900 MB 924-1,112 MB → 691-774 MB
no room in the pool a run 0-2 → 421-758 0-1 → 147-327

It does what it says: the bursts at area changes copy almost nothing. But no fewer slow frames on either machine (a few more on the desktop), and on the laptop it copies more than the eviction did and takes back a quarter less: the images copied ahead, many of them never evicted, push out of the LRU the ones that were taken back. Kept as a switch, off, with the others.

(The desktop's A side is at 138 fps here against 177 in the steps above: a stray console window spun one core of the desktop through these runs, on both sides alike, so A against B still holds. Once it was closed the same tour went back to 180-191 fps.)

5. Compressed in system memory? (HumanBean's ask; measured, not written.) The kept copies of one desktop run, written out by a local patch that is not in this PR: 915 images, 638 MB, all BC3. zstd 1.5.7 on one core, each image whole:

zstd level size compress decompress
-5 (lz4's ground) 55.9 % 768 MB/s 1,446 MB/s
-1 42.1 % 594 MB/s 1,495 MB/s
1 36.7 % 488 MB/s 1,065 MB/s
3 38.2 % 323 MB/s 948 MB/s

It packs that well because 98.8 % of the blocks have one constant alpha: 742 of the 915 images (521 of the 638 MB) are wholly opaque, re-encoded as BC3, half of each block saying "alpha 255".

What it would give: at 1024 MB a run fills only ~640 MB, so nothing. For a small setting, what 256 MB packed at ~42 % would hold, 640 MB, against 256 MB unpacked (A: 256, B: 640, on a8eba9bc):

Desktop A → B Laptop A → B
taken back a run 112-132 MB → 534-740 MB 92-142 MB → 401-455 MB
frames of 50 ms or more 14 → 13 153 → 141
frames of 25 ms or more 46 → 38 377 → 390
1% low fps 103.1 → 107.5 14.8 → 15.9

Three to five times as much taken back, and no fewer slow frames on either machine, as in steps 1 to 4: a take-back saves the decode, and the decodes are not what the area changes' slow frames wait on. Its cost would be ~1.4 s of CPU to pack and ~0.5 s to unpack a run, off the render thread (5.4 MB unpacks in 3.6 ms). Not written.

6. The setting, Auto by default (HumanBean's ask). A: Off (RX_VK_BC_KEEP_MB=0), B: Auto, on 281e41aa, the same tour. Auto gives the desktop (32 GB of RAM, 17 GB free) 1024 MB; the laptop reports 15832 MB of RAM, which 281e41aa counted as 15 GB, so 448 MB (652c92d7 rounds up: 512 MB, as the curve says for 16 GB).

Desktop, Off → Auto (1024 MB) Laptop, Off → Auto (448 MB)
fps 157.0 (156.7 / 157.2 / 157.0) → 156.8 (154.4 / 156.8 / 156.9) 59.6 (59.4 / 59.6 / 59.6) → 59.4 (59.2 / 59.4 / 59.4)
1% low fps 56.6 → 56.9 13.7 → 13.4
frames of 25 ms or more 59 (72 / 59 / 56) → 62 (71 / 54 / 62) 400 (394 / 400 / 429) → 405 (405 / 453 / 386)
frames of 50 ms or more 13 (19 / 13 / 12) → 14 (16 / 14 / 11) 148 (148 / 155 / 148) → 151 (161 / 143 / 151)
longest frame 153.5 ms → 139.7 ms 464.1 ms → 430.1 ms
kept / taken back a run 933-934 images (645 MB) / 1,057-1,095 (757-768 MB) 2,519-2,603 (1.7-1.8 GB, 447 of 448 MB held) / 278-323 (190-224 MB)
keeping's CPU a run 1 ms (227 µs at most once) 27-33 ms (3.6 ms at most once)

Within run-to-run spread on both, either way. At 448 MB the laptop's tour churns the cache: it copies 1.7 GB a run to take back 0.2 GB. The desktop ran with the laptop's budget forced (RX_VK_VRAM_MB=3584), on main de6fb7b5; its 157 fps against 175 in the first table is the newer main or the PC that night, both sides alike. One laptop Off run hit Linux memory pressure (9.8 GB free at start); the Off line it logged then is gone in 652c92d7.

Seen on the way: the wholly opaque ASTC textures as BC1 rather than BC3 would take half the VRAM and half the system memory, at no CPU cost. Not in this PR.

Risks

  • On by default (Auto) on a PC with 12 GB of RAM or more and a discrete GPU, but it only acts on a card whose texture budget evicts: elsewhere it runs the same code as before, with no copy and no extra memory (the cap's log line comes at the first eviction).
  • A wrong take-back would show as the wrong texture. The key is the blocks' XXH64 hash and the whole image shape, the same one #1222's reuse has relied on since it merged; only images that are exactly the decoded blocks are kept.
  • The cost is up to the cap in system memory (Auto: 256 MB at 12 GB of RAM, 512 MB at 16 GB, 1 GB from 24 GB, never over a quarter of the free memory), and a GPU copy of each kept image at its eviction.
  • The whole cap plus 64 MB of system memory is taken at the first eviction (the pool), not as copies come. If the system then runs short of memory, it is all given back and not taken again that run.
  • After updating, a player notices nothing. Saves, settings and caches are untouched.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named). Linux full suite on the laptop (CachyOS): 771 passed, 5 failed, none in this change. Four make throwaway commits ([email protected]) that my machine's own commit hook refuses: test_check_third_party, test_ci_source_times, test_dev_vcs, test_run_all_output. test_lifter ran past its 600 s limit on that laptop; I did not rerun it on main. Windows --changed: see above.
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A switch added or changed: the setting ships at Auto, the default HumanBean chose, the measurements showing no loss; RX_VK_BC_KEEP_MB is its SETTING line in gpu/vk/switches.def, the three other switches stay off with their rows.
  • A check under tests/ that reads changed source is updated, not weakened.
  • Docs updated: the new row in docs/dev/reference/switches.md.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. Windows (this PC) and Linux (the laptop, CachyOS) both built and ran it.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

🤖 Generated with Claude Code

## Summary Draft. An **in-game setting, Auto by default**, as HumanBean asked: Graphics > GPU, "Texture RAM cache", Auto / Max / Off. It is for players on smaller cards (4 GB, or 6-8 GB on a slow PCIe link); on a card whose budget never evicts it does nothing. On a card whose texture budget evicts, an area's ASTC textures kept as BC3/BC7 go when the budget is met, and coming back to that area decodes and re-encodes each of them again. The finalized-texture reuse (#1222) cannot serve them: the image it would copy from is the one the budget just destroyed. With the setting on, an evicted texture of that kind first has its BC image copied into a buffer in system memory, keyed by its blocks' hash and shape (the same `astc_reuse.h` key #1222 uses). A texture made again later with the same blocks and shape takes that copy back by one GPU copy, instead of a decode and a BC encode. Copies leave least recently used first. Nothing is kept before an eviction, so a card that never evicts pays nothing, Auto included. - The row is `texture_ram_cache` in the graphics system mod (`game/mods/graphics/mod.toml`), applied at restart; the renderer reads it from the start-up snapshot. **Auto** follows HumanBean's curve: nothing under 12 GB of RAM or on an integrated GPU (whose VRAM is that same memory), 256 MB at 12 GB rising to 1 GB at 24 GB and more (16 GB: 512 MB), and never more than a quarter of the memory free when the cap is first read; under 64 MB it keeps nothing. **Max** is 1 GB, **Off** nothing. Its switch, `RX_VK_BC_KEEP_MB`, takes `auto`, `1024` or `0`. - **Memory pressure** gives it all back: when the system runs short (on Windows the commit headroom under its reserve, on Linux the same shortfall that already gives back the capture arena), every kept copy is dropped, the pool is freed once the GPU is done with it, and nothing is kept again until the game restarts (`keep_give_back`). It goes first, before the textures in use: it only saves decodes. - The cap is logged once: `vk: Texture RAM cache Auto: 1024 MB at most for evicted BC images (32133 MB of RAM, 17390 MB available)`. - Only images that are exactly the decoded blocks qualify: ASTC kept as BC3 or BC7, hashed at a whole first upload, never drawn by the GPU, plain 2D. A texture uploaded again after its first upload has no hash (`reuse_upload`), so it is never kept. - The copies' memory is one pool of cached host memory (64 MB blocks, one more than the cap needs), made all at once by a thread of its own at the first eviction (`keep_memory`, `create_cached_pool` in `vk_vma.cpp`); until it is there an evicted image is not kept. Before, each copy was a `create_buffer_cached` on the replay thread, and a new VMA block there cost up to 9 ms (desktop) and 21 ms (laptop) for a single image. - Three more switches, all off, for testers with very little to spare (Veix's steps, Measurements): `RX_VK_BC_KEEP_PRESENT_MB=<MB>` caps the bytes one present copies (an evicted image past it is not kept), `RX_VK_BC_KEEP_QUARTER=1` keeps none over a quarter of the cap, as #2273 does, and `RX_VK_BC_KEEP_AHEAD_MB=<MB>` copies about that much a pass of the eviction of the BC images unused as long as an eviction asks, while they are still in VRAM, so that their eviction copies nothing (Veix's plan A). - Once something was kept, two lines join the ASTC lines every 600 presents: ``` vk: BC images kept in system memory: 1752 evicted ones kept (1199 MB), 1504 taken back (1027 MB), 1478 not found, 1 without memory; 1502 held (1020 of 1024 MB) vk: BC images kept, the copies: 51 presents copied (188 MB at most in one), 0 left over the present's budget, 11 before their memory was made; keeping took 11 ms of CPU (904 us at most once) ``` The idea of keeping transcoded BC blocks in RAM comes from HeyListen (Sacrappy). No code was taken; the code is Longshot's own. Read first: `gpu/vk/bc_keep.h` (the cap and Auto's curve, the sizes, the kept copies, the per-present budget), then `keep_cap`, `keep_memory`, `keep_store`, `keep_take`, `keep_frame` and `keep_give_back` in `gpu/vk/vk_texture.cpp`, after `reuse_upload`, its two calls under memory pressure in `texture_frame`, `create_cached_pool` / `destroy_cached_pool` in `gpu/vk/vk_vma.cpp`, and the row in `game/mods/graphics/mod.toml`. ## Testing **Tested:** - CachyOS, RTX 3060 Laptop 6 GB (texture budget 3584 MB), PCIe 3.0 x8, display wired to the dGPU: `dev.py ab --a RX_VK_BC_KEEP_MB=0 --b RX_VK_BC_KEEP_MB=1024 --runs 3`, 150 s a run spent teleporting every 12 s through Market, Kakariko, Kokiri Forest, Lost Woods and Hyrule Field (child). Each B run kept 1,718 to 1,816 evicted images and took 1,314 to 1,453 back (1.0 to 1.1 GB); no texture looked wrong on screen, no FATAL line. - Windows 11, RTX 5080, the same tour with `--set RX_VK_VRAM_MB=3584` (the laptop's budget): 998 to 1,131 taken back a run (1.0 to 1.3 GB), no FATAL line. - Checks: `tests/test_bc_keep.py` covers the cap (unset, a number, auto, integrated GPU), a BC level's bytes, and the kept copies (found by blocks and shape, least recently used out first, never one over the cap, a quarter of it with `RX_VK_BC_KEEP_QUARTER`, room made before a copy), and the per-present budget (each present from nothing, exact fit, one image over the budget never kept). It also checks the source: the copy before the evicted texture's end, the take-back at a first whole upload, the frame's clean-up. `tests/test_readback_timeout_reuse.py`'s fixture stubs `keep_store`, the eviction's new call. `run_all.py --changed origin/main` on Windows: 124 passed, 2 failed, 11 skipped; test_image_view_retry and test_tex_desc_memo fail the same way on `main` on this PC. - On main 3a0f7d8f with these steps: `dev.py rt` then `dev.py smoke` PASS on Windows; `run_all.py --changed origin/main` 164 passed, 1 failed (`test_dlssg`, which fails on main too). - The setting (281e41aa and 652c92d7, on main de6fb7b5): `dev.py rt` then `dev.py smoke` PASS on Windows (in play from kokiri-start, 900 frames, the menu opened); the A/B in Measurements, step 6, on both machines (281e41aa). `tests/test_bc_keep.cpp` covers Auto's curve (8 and 11 GB, 12, a 16 GB PC reporting a little under and the laptop's 15832 MB, 24 and 64 GB, RAM unknown, integrated GPU, the quarter of the free memory, under 64 MB) and Max / Off; `tests/test_bc_keep.py` checks the source: the cap read from the snapshot, the give-back on both systems' pressure, the pool freed after it. `test_bc_keep`, `test_menu_ini_keys` (the new key in FROZEN), `test_readback_timeout_reuse` (its stubs brought up to the eviction's calls), `test_switch_table` and `test_switches_removed` pass; `schema-gen --check` passes. `test_i18n_complete` and `test_settings_area_keys` fail on this branch exactly as on `main` at de6fb7b5 ("Fill title screen" untranslated, `mouse_aim_sensitivity`); the new row's five texts are in all 13 languages. **Still to test:** - A 4 GB card, NVIDIA or AMD, where the budget evicts hardest (@Beanstalk ab --a RX_VK_BC_KEEP_MB=0 --b RX_VK_BC_KEEP_MB=auto --runs 3 on a 4 GB runner, with the vk: BC images kept line) - An 8 GB card on PCIe x4, the case HeyListen measured a gain on (@Beanstalk ab --a RX_VK_BC_KEEP_MB=0 --b RX_VK_BC_KEEP_MB=auto --runs 3 on a x4 runner) - A small card with little RAM to spare (4 GB card, 8 GB of RAM, where Auto keeps nothing): Max against Off, then the light switches on top of Max (@Beanstalk ab --a RX_VK_BC_KEEP_MB=0 --b RX_VK_BC_KEEP_MB=1024 --runs 3, with both vk: BC images kept lines; then --a RX_VK_BC_KEEP_MB=1024 --b RX_VK_BC_KEEP_MB=1024 --b RX_VK_BC_KEEP_PRESENT_MB=8 --b RX_VK_BC_KEEP_QUARTER=1 --runs 3) - A tester on a 4-6 GB card with 16 GB of RAM or more: play a while at Auto, going back and forth between areas, then at Off, and say whether returning to an area hitches less (by hand, a tester) ## Measurements This PR adds the Texture RAM cache setting, which ships at **Auto**, as HumanBean asked (step 6). **Verdict:** it works as meant (a gigabyte of images a run taken back instead of decoded and re-encoded), but brings no gain on either machine here, and no loss since the copies' memory is made out of the frame (step 3): Auto against Off is within run-to-run spread on both (step 6). The gain, if any, is on smaller cards, where decoding and encoding cost more: "Still to test" has the farm runs. The first tables below are from before the setting, with the switch. Baseline: the same build with `RX_VK_BC_KEEP_MB=0` (ca63a459's code with the old default, which was on), not `main`. The switch is the only difference, so the comparison isolates it. Setup: `dev.py ab` with the tour above (`TOUR_DESTS` stepping through five areas every 12 s), 150 s measured a run, 3 interleaved runs a side. The medians, each run in brackets. RTX 3060 Laptop 6 GB, CachyOS, PCIe 3.0 x8, budget 3584 MB (its own): | | Before: this PR @ ca63a459, RX_VK_BC_KEEP_MB=0 | After: this PR @ ca63a459, RX_VK_BC_KEEP_MB=1024 | |---|---|---| | fps | 64.0 (65.7 / 62.4 / 64.0) | 62.9 (63.0 / 62.9 / 62.0) | | 1% low fps | 14.6 (15.4 / 13.9 / 14.6) | 14.4 (13.6 / 14.6 / 14.4) | | frames of 25 ms or more | 419 (419 / 426 / 390) | 408 (408 / 408 / 448) | | frames of 50 ms or more | 147 (134 / 167 / 147) | 155 (161 / 145 / 155) | | longest frame | 450 ms (450 / 617 / 380) | 331 ms (327 / 331 / 470) | RTX 5080, Windows 11, `RX_VK_VRAM_MB=3584`: | | Before: this PR @ ca63a459, RX_VK_BC_KEEP_MB=0 | After: this PR @ ca63a459, RX_VK_BC_KEEP_MB=1024 | |---|---|---| | fps | 175.0 (174.5 / 176.7 / 175.0) | 175.4 (174.5 / 175.4 / 177.0) | | 1% low fps | 98.5 (98.6 / 98.5 / 98.3) | 97.5 (97.5 / 96.6 / 99.1) | | frames of 25 ms or more | 43 (43 / 43 / 41) | 48 (48 / 46 / 50) | | frames of 50 ms or more | 13 (14 / 10 / 13) | 16 (16 / 16 / 15) | ### Veix's steps (copies out of the frame, #2273) Each step measured against the one before it, same tour, same machines, 3 runs a side, medians (each run in brackets where it matters). **1. A byte budget per present** (`RX_VK_BC_KEEP_PRESENT_MB`), to test whether the copies made at an eviction are the slow frames there. A: 1024 MB, no budget. The copies come in bursts: on the desktop 13 to 18 presents a run make all of them, the biggest 290 to 530 MB; on the laptop 50 to 64 presents, the biggest 165 to 366 MB. | | Desktop, 8 MB | Desktop, 32 MB | Laptop, 8 MB | Laptop, 32 MB | |---|---|---|---|---| | frames of 50 ms or more, A → B | 14 → 13 | 15 → 13 | 149 (150 / 142 / 149) → 129 (140 / 123 / 129) | 143 → 137 | | frames of 25 ms or more, A → B | 47 → 42 | 41 → 41 | 382 → 357 | 388 → 376 | | 1% low fps, A → B | 98.6 → 97.3 | 98.0 → 99.1 | 14.7 → 17.1 | 15.1 → 15.8 | | taken back a run, A → B | ~1.2 GB → 110 MB | ~1.4 GB → 440 MB | ~1.05 GB → 350 MB | ~1.1 GB → 810 MB | 8 MB on the laptop is the only cell that looks like a gain, and it costs two thirds of what is taken back; step 3's runs put the same A at 136, so it is within run-to-run spread. Kept as a switch, off. **3. The copies' memory made out of the frame** (done before 2). A: step 1's build, B: the pool; 1024 MB, no budget. | | Desktop A → B | Laptop A → B | |---|---|---| | `keep_store` CPU time a run | 10-17 ms → 1 ms | 236-277 ms → 10-11 ms | | its longest single call | 2.4-8.4 ms → 55-74 µs | 16-18 ms → 0.14-0.9 ms | | the pool made (on its own thread) | 43 ms | 270 ms | | frames of 50 ms or more | 15 → 15 | 136 → 137 | | frames of 25 ms or more | 42 → 43 | 400 → 372 | | taken back a run | ~1.2 GB → 745 MB | ~1.05 GB → ~1.06 GB | The keeping now costs nearly nothing, but the slow frames at area changes stay: the copies are not what makes them. On the desktop 20 to 22 images go unkept while the pool is made, at the first burst, which is where its lower take-back comes from. In this PR as it is. **2. No copy over a quarter of the cap** (`RX_VK_BC_KEEP_QUARTER`). At 1024 MB a quarter is 256 MB and the biggest copies are about 22 MB, so it was measured at `RX_VK_BC_KEEP_MB=128` (32 MB an image). A: step 3. | | Desktop A → B | Laptop A → B | |---|---|---| | frames of 50 ms or more | 15 → 13 | 148 → 138 | | frames of 25 ms or more | 43 → 40 | 375 → 404 | | taken back a run | ~90 MB → ~90 MB | 94-166 MB → 57-67 MB | No gain, and a third less taken back on the laptop: the images it refuses are the big ones. Kept as a switch, off, for a tester with very little memory to spare. Also seen at 128 MB: the laptop copies ~1.9 GB a run to take back ~0.1 GB, and 53 to 178 images a run find no room while dropped copies wait for the GPU. **4. Plan A: copied ahead of the eviction** (`RX_VK_BC_KEEP_AHEAD_MB`). Each pass of the eviction first copies about that much of the BC images unused as long as an eviction asks (in the eviction's order), while they are still in VRAM; an evicted image already kept is moved to the front of the LRU, not copied again. A: 1024 MB, B: the same with 4 MB a pass, on main 3a0f7d8f. | | Desktop A → B | Laptop A → B | |---|---|---| | frames of 50 ms or more | 16 → 18 | 139 → 136 | | frames of 25 ms or more | 46 (55 / 45 / 46) → 58 (60 / 58 / 56) | 378 → 356 | | 1% low fps | 83.9 → 82.0 | 15.8 → 16.1 | | copied ahead a run | 790-830 images (560-590 MB) | 2,050-2,290 images (1.5-1.7 GB) | | copied at the eviction a run | 915-923 → 100-135 images | 1,800-1,910 → 200-270 images | | taken back a run | 720-926 MB → ~900 MB | 924-1,112 MB → 691-774 MB | | no room in the pool a run | 0-2 → 421-758 | 0-1 → 147-327 | It does what it says: the bursts at area changes copy almost nothing. But no fewer slow frames on either machine (a few more on the desktop), and on the laptop it copies more than the eviction did and takes back a quarter less: the images copied ahead, many of them never evicted, push out of the LRU the ones that were taken back. Kept as a switch, off, with the others. (The desktop's A side is at 138 fps here against 177 in the steps above: a stray console window spun one core of the desktop through these runs, on both sides alike, so A against B still holds. Once it was closed the same tour went back to 180-191 fps.) **5. Compressed in system memory?** (HumanBean's ask; measured, not written.) The kept copies of one desktop run, written out by a local patch that is not in this PR: 915 images, 638 MB, all BC3. zstd 1.5.7 on one core, each image whole: | zstd level | size | compress | decompress | |---|---|---|---| | -5 (lz4's ground) | 55.9 % | 768 MB/s | 1,446 MB/s | | -1 | 42.1 % | 594 MB/s | 1,495 MB/s | | 1 | 36.7 % | 488 MB/s | 1,065 MB/s | | 3 | 38.2 % | 323 MB/s | 948 MB/s | It packs that well because 98.8 % of the blocks have one constant alpha: 742 of the 915 images (521 of the 638 MB) are wholly opaque, re-encoded as BC3, half of each block saying "alpha 255". What it would give: at 1024 MB a run fills only ~640 MB, so nothing. For a small setting, what 256 MB packed at ~42 % would hold, 640 MB, against 256 MB unpacked (A: 256, B: 640, on a8eba9bc): | | Desktop A → B | Laptop A → B | |---|---|---| | taken back a run | 112-132 MB → 534-740 MB | 92-142 MB → 401-455 MB | | frames of 50 ms or more | 14 → 13 | 153 → 141 | | frames of 25 ms or more | 46 → 38 | 377 → 390 | | 1% low fps | 103.1 → 107.5 | 14.8 → 15.9 | Three to five times as much taken back, and no fewer slow frames on either machine, as in steps 1 to 4: a take-back saves the decode, and the decodes are not what the area changes' slow frames wait on. Its cost would be ~1.4 s of CPU to pack and ~0.5 s to unpack a run, off the render thread (5.4 MB unpacks in 3.6 ms). Not written. **6. The setting, Auto by default** (HumanBean's ask). A: Off (`RX_VK_BC_KEEP_MB=0`), B: Auto, on 281e41aa, the same tour. Auto gives the desktop (32 GB of RAM, 17 GB free) 1024 MB; the laptop reports 15832 MB of RAM, which 281e41aa counted as 15 GB, so 448 MB (652c92d7 rounds up: 512 MB, as the curve says for 16 GB). | | Desktop, Off → Auto (1024 MB) | Laptop, Off → Auto (448 MB) | |---|---|---| | fps | 157.0 (156.7 / 157.2 / 157.0) → 156.8 (154.4 / 156.8 / 156.9) | 59.6 (59.4 / 59.6 / 59.6) → 59.4 (59.2 / 59.4 / 59.4) | | 1% low fps | 56.6 → 56.9 | 13.7 → 13.4 | | frames of 25 ms or more | 59 (72 / 59 / 56) → 62 (71 / 54 / 62) | 400 (394 / 400 / 429) → 405 (405 / 453 / 386) | | frames of 50 ms or more | 13 (19 / 13 / 12) → 14 (16 / 14 / 11) | 148 (148 / 155 / 148) → 151 (161 / 143 / 151) | | longest frame | 153.5 ms → 139.7 ms | 464.1 ms → 430.1 ms | | kept / taken back a run | 933-934 images (645 MB) / 1,057-1,095 (757-768 MB) | 2,519-2,603 (1.7-1.8 GB, 447 of 448 MB held) / 278-323 (190-224 MB) | | keeping's CPU a run | 1 ms (227 µs at most once) | 27-33 ms (3.6 ms at most once) | Within run-to-run spread on both, either way. At 448 MB the laptop's tour churns the cache: it copies 1.7 GB a run to take back 0.2 GB. The desktop ran with the laptop's budget forced (`RX_VK_VRAM_MB=3584`), on main de6fb7b5; its 157 fps against 175 in the first table is the newer main or the PC that night, both sides alike. One laptop Off run hit Linux memory pressure (9.8 GB free at start); the Off line it logged then is gone in 652c92d7. Seen on the way: the wholly opaque ASTC textures as BC1 rather than BC3 would take half the VRAM and half the system memory, at no CPU cost. Not in this PR. ## Risks - On by default (Auto) on a PC with 12 GB of RAM or more and a discrete GPU, but it only acts on a card whose texture budget evicts: elsewhere it runs the same code as before, with no copy and no extra memory (the cap's log line comes at the first eviction). - A wrong take-back would show as the wrong texture. The key is the blocks' XXH64 hash and the whole image shape, the same one #1222's reuse has relied on since it merged; only images that are exactly the decoded blocks are kept. - The cost is up to the cap in system memory (Auto: 256 MB at 12 GB of RAM, 512 MB at 16 GB, 1 GB from 24 GB, never over a quarter of the free memory), and a GPU copy of each kept image at its eviction. - The whole cap plus 64 MB of system memory is taken at the first eviction (the pool), not as copies come. If the system then runs short of memory, it is all given back and not taken again that run. - After updating, a player notices nothing. Saves, settings and caches are untouched. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [x] `mise run test` passes (any failure that also fails on `main` is named). Linux full suite on the laptop (CachyOS): 771 passed, 5 failed, none in this change. Four make throwaway commits (`[email protected]`) that my machine's own commit hook refuses: `test_check_third_party`, `test_ci_source_times`, `test_dev_vcs`, `test_run_all_output`. `test_lifter` ran past its 600 s limit on that laptop; I did not rerun it on `main`. Windows `--changed`: see above. - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A switch added or changed: the setting ships at Auto, the default HumanBean chose, the measurements showing no loss; `RX_VK_BC_KEEP_MB` is its `SETTING` line in `gpu/vk/switches.def`, the three other switches stay off with their rows. - [x] A check under `tests/` that reads changed source is updated, not weakened. - [x] Docs updated: the new row in `docs/dev/reference/switches.md`. - [x] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. Windows (this PC) and Linux (the laptop, CachyOS) both built and ran it. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
perf(vk): keep an evicted BC-kept ASTC texture's image in system memory, taken back instead of a decode (opt-in)
Some checks failed
PR description / Title and description (pull_request_target) Has been cancelled
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
ca63a45925
On a card that evicts (4-8 GB), an area's ASTC textures kept as BC3/BC7 go when the budget is met, and coming back
to that area decodes and re-encodes each again. The finalized-texture reuse (#1222) cannot serve them: the image it
copies from is the one the budget destroyed.

With RX_VK_BC_KEEP_MB set (MB, or auto: a sixteenth of the RAM, 1 GB at most, none on an integrated GPU), an
evicted texture of that kind has its BC image copied into a buffer in system memory first, by its blocks' hash and
shape (astc_reuse.h), and a texture made again with the same blocks and shape takes it back by one GPU copy. Least
recently used out first. Off by default: no gain on an RTX 3060 Laptop (6 GB) nor an RTX 5080 held to the same
budget, and a few more slow frames at the evictions on the latter; for testers on smaller cards.

The readback-timeout fixture stubs keep_store, the eviction's new call.

Idea credited to HeyListen (Sacrappy), rewritten here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

Tested in game: CachyOS RTX 3060 Laptop 6 GB and Windows 11 RTX 5080 (budget held to 3584 MB): a five-area teleport tour, 3 A/B runs a side each; about 1 GB of BC images a run taken back from system memory, no wrong texture, no FATAL line.

Tested in game: CachyOS RTX 3060 Laptop 6 GB and Windows 11 RTX 5080 (budget held to 3584 MB): a five-area teleport tour, 3 A/B runs a side each; about 1 GB of BC images a run taken back from system memory, no wrong texture, no FATAL line.
Member

Possibly related, for context:

  • PR #2273 WIP: perf(vk): texture RAM cache, a proof of concept with its diagnostics (not for merging as is) — Earlier open texture RAM cache targets evicted-texture reconversion; substantially broader proof-of-concept.
Possibly related, for context: - PR #2273 WIP: perf(vk): texture RAM cache, a proof of concept with its diagnostics (not for merging as is) — Earlier open texture RAM cache targets evicted-texture reconversion; substantially broader proof-of-concept.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge main (e345b3cab) into perf/bc-ram-cache
Some checks failed
PR description / Title and description (pull_request_target) Has been cancelled
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
718c24bc67
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

Brought up to current main (e345b3ca) by a merge, nothing rewritten. Windows, run_all.py --changed origin/main: 129 passed, 0 failed. The Linux full suite (laptop) is in the checklist.

Brought up to current `main` (e345b3ca) by a merge, nothing rewritten. Windows, `run_all.py --changed origin/main`: 129 passed, 0 failed. The Linux full suite (laptop) is in the checklist.
Owner

So this can increase performance, at the cost of extra memory, from what I'm understanding.

Maybe we should have a setting in game, 'auto' by default, with option for 'Max' (1GB) and Off.

Auto:

  • Less than 12GB: Disabled
  • 12GB-24GB: scale from 256MB to 1GB
  • More than 24GB: 1GB

This means: 12GB = 256MB, 16GB = 512MB, 24GB = 1GB.

We'd need some testing. It makes sense to add this, but the extra RAM cost needs to be managed. We have to find how much we can optimize this, without increasing memory pressure. Also we need to see what the smallest amount of this is worth it. E.g. Maybe 16GB should only get 256MB if that gives enough. Or maybe we should look at memory pressure.

Also key thing: We should make sure we store the images well compressed when moving to RAM, this would allow us to store more with less RAM, which is key for this.

So this can increase performance, at the cost of extra memory, from what I'm understanding. Maybe we should have a setting in game, 'auto' by default, with option for 'Max' (1GB) and Off. Auto: - Less than 12GB: Disabled - 12GB-24GB: scale from 256MB to 1GB - More than 24GB: 1GB This means: 12GB = 256MB, 16GB = 512MB, 24GB = 1GB. We'd need some testing. It makes sense to add this, but the extra RAM cost needs to be managed. We have to find how much we can optimize this, without increasing memory pressure. Also we need to see what the smallest amount of this is worth it. E.g. Maybe 16GB should only get 256MB if that gives enough. Or maybe we should look at memory pressure. Also key thing: We should make sure we store the images well compressed when moving to RAM, this would allow us to store more with less RAM, which is key for this.
Member

#2273

maybe there is something in here, the copy in background is probably how it could gain you more easily, copying on the hot path in frame will probably cancel most gains. There is also other stuff in there, not all is applicable, but there is more to texture cost then just encoding, also the swizzling, so that branch puts more in memory then just bc3. Obviously just for reference, maybe it helps :)

https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2273 maybe there is something in here, the copy in background is probably how it could gain you more easily, copying on the hot path in frame will probably cancel most gains. There is also other stuff in there, not all is applicable, but there is more to texture cost then just encoding, also the swizzling, so that branch puts more in memory then just bc3. Obviously just for reference, maybe it helps :)
Merge main (18edd5097) into perf/bc-ram-cache
Some checks failed
PR description / Title and description (pull_request_target) Has been cancelled
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
9de202d46a
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

Brought up to current main (18edd509) by a merge, 9de202d4, nothing rewritten. No conflict. Windows, run_all.py --changed origin/main: 130 passed, 1 failed (test_pass_barrier.py): all of them fail on main at 18edd509 too, on this PC, run there with the same toolchain. test_pass_barrier.py has failed on main since #3161; #3196 fixes it.

Brought up to current `main` (18edd509) by a merge, 9de202d4, nothing rewritten. No conflict. Windows, `run_all.py --changed origin/main`: 130 passed, 1 failed (`test_pass_barrier.py`): all of them fail on `main` at 18edd509 too, on this PC, run there with the same toolchain. `test_pass_barrier.py` has failed on `main` since #3161; #3196 fixes it.
Author
Contributor

Thanks, both.

@HumanBean: yes, that is the trade: about a gigabyte of system memory for fewer decodes and re-encodes when going back to an area. The switch's auto today keeps a sixteenth of the RAM, 1 GB at most, and nothing on an integrated GPU. Your curve (nothing under 12 GB, 256 MB to 1 GB from 12 to 24 GB, 1 GB above) is easy to put in its place, behind an in-game Auto / Max / Off row once it shows a gain. On storage: what is kept is already the BC3 or BC7 image, a quarter of the RGBA size. I will measure what a fast lossless pass saves on those blocks before adding one, and which cap is the smallest that still pays (256, 512 and 1024 MB on the laptop's tour), and post the numbers here.

@Veix: agreed. Today the copy to system memory is recorded when the texture is evicted, inside the frame, which fits the few extra 25 ms frames in the desktop runs above. Next I will read #2273's background copy and move this one off the frame, then measure again. Swizzling and the rest of #2273 I will look at too, keeping only what applies here.

Thanks, both. @HumanBean: yes, that is the trade: about a gigabyte of system memory for fewer decodes and re-encodes when going back to an area. The switch's `auto` today keeps a sixteenth of the RAM, 1 GB at most, and nothing on an integrated GPU. Your curve (nothing under 12 GB, 256 MB to 1 GB from 12 to 24 GB, 1 GB above) is easy to put in its place, behind an in-game Auto / Max / Off row once it shows a gain. On storage: what is kept is already the BC3 or BC7 image, a quarter of the RGBA size. I will measure what a fast lossless pass saves on those blocks before adding one, and which cap is the smallest that still pays (256, 512 and 1024 MB on the laptop's tour), and post the numbers here. @Veix: agreed. Today the copy to system memory is recorded when the texture is evicted, inside the frame, which fits the few extra 25 ms frames in the desktop runs above. Next I will read #2273's background copy and move this one off the frame, then measure again. Swizzling and the rest of #2273 I will look at too, keeping only what applies here.
RX_VK_BC_KEEP_PRESENT_MB=<MB> caps what one present copies out of evicted
images into the kept BC copies (RX_VK_BC_KEEP_MB): an evicted image past it
is not kept. A step towards Veix's copy outside the frame (#2273): it tests
whether the copies made at an eviction are what the slow frames there are.

The log's last statistics also say how many presents copied, the most one
copied, how many images the budget left out and the CPU time keep_store took.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each kept copy was a buffer of create_buffer_cached, made on the replay thread
during the present that evicts: a new VMA block there took up to 9 ms on the
PC and 21 ms on an RTX 3060 Laptop for a single image (the keep_store CPU time
the log gives since the previous commit).

The copies now come from a pool of cached host memory whose blocks (64 MB, one
more than RX_VK_BC_KEEP_MB needs) are all made at the first eviction by a
thread of its own; until it is there an evicted image is not kept (counted in
the log). Room is made before a copy is taken: the dropped copies the GPU is
done with free theirs at once, the others once it is.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
As #2273 does: one big image no longer pushes most of the others out of the
kept copies. Only a small RX_VK_BC_KEEP_MB changes anything (the biggest kept
images are about 22 MB).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Measured at RX_VK_BC_KEEP_MB=128 (the only size where it changes anything): no fewer slow frames on the PC or the RTX 3060 Laptop, and on the laptop a third less taken back (94-166 MB down to 57-67 MB): the images it refuses are the big ones worth keeping.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The quarter rule of the previous commit, back as a switch, off: measured at
RX_VK_BC_KEEP_MB=128 it took back a third less on an RTX 3060 Laptop
(94-166 MB down to 57-67 MB) and no fewer slow frames there or on the PC,
but a tester with very little memory to spare may see otherwise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fix(vk): the kept BC images' pool and its buffers carry the rest tier's memory priority
Some checks failed
PR description / Title and description (pull_request_target) Has been cancelled
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
d5f7402eb7
tests/test_memory_priority.py asks every VmaAllocationCreateInfo to set one:
with VK_EXT_memory_priority on, VMA would give them 0, the lowest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

Veix's copy-outside-the-frame idea (#2273), step by step, each measured on the desktop (RTX 5080, 3584 MB budget) and the laptop (RTX 3060 Laptop 6 GB, PCIe 3.0 x8); the tables are in Measurements under "Veix's steps".

  • The copies' memory made out of the frame (in the PR now): one pool, made at the first eviction by a thread of its own. Keeping went from 236-277 ms of CPU a run on the laptop (up to 18 ms for one image) to 10-11 ms (under 1 ms), with the same take-back. The slow frames at area changes did not move, on either machine: the copies are not what makes them.
  • A byte budget per present (RX_VK_BC_KEEP_PRESENT_MB, off) and no copy over a quarter of the cap (RX_VK_BC_KEEP_QUARTER, off, as #2273): no gain beyond run-to-run spread here, and both take back less. Kept as switches for testers with very little memory to spare; "Still to test" has the farm run for a small card.

Next: Veix's plan A, copying the long-unused BC textures in the background while they are still in VRAM, so that an eviction only does the bookkeeping.

Veix's copy-outside-the-frame idea (#2273), step by step, each measured on the desktop (RTX 5080, 3584 MB budget) and the laptop (RTX 3060 Laptop 6 GB, PCIe 3.0 x8); the tables are in Measurements under "Veix's steps". - **The copies' memory made out of the frame** (in the PR now): one pool, made at the first eviction by a thread of its own. Keeping went from 236-277 ms of CPU a run on the laptop (up to 18 ms for one image) to 10-11 ms (under 1 ms), with the same take-back. The slow frames at area changes did not move, on either machine: the copies are not what makes them. - **A byte budget per present** (`RX_VK_BC_KEEP_PRESENT_MB`, off) and **no copy over a quarter of the cap** (`RX_VK_BC_KEEP_QUARTER`, off, as #2273): no gain beyond run-to-run spread here, and both take back less. Kept as switches for testers with very little memory to spare; "Still to test" has the farm run for a small card. Next: Veix's plan A, copying the long-unused BC textures in the background while they are still in VRAM, so that an eviction only does the bookkeeping.
perf(vk): copy the long-unused BC images ahead of their eviction with RX_VK_BC_KEEP_AHEAD_MB
Some checks failed
PR description / Title and description (pull_request_target) Successful in 8s
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
a8eba9bc4c
Veix's plan A in #2273: once the kept images' memory is made, each pass of the
eviction copies about RX_VK_BC_KEEP_AHEAD_MB of the BC images unused as long as
an eviction asks, while they are still in VRAM, so that the burst of an area
change copies nothing for them. An evicted image kept already is refreshed in
the LRU instead of copied again. Off by default; the log's copies line says
how many went ahead and how many evicted ones were kept already.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner

Okay, I think my 12-24GB scale for auto makes sense, you can propose something better though! Let's go ahead with an option and auto by default though, I think it makes sense. Maybe we should also do a memory check, and scale it to that (e.g. check memory pressure)

Okay, I think my 12-24GB scale for auto makes sense, you can propose something better though! Let's go ahead with an option and auto by default though, I think it makes sense. Maybe we should also do a memory check, and scale it to that (e.g. check memory pressure)
# Conflicts:
#	gpu/vk/vk_vma.cpp
feat(vk): a Texture RAM cache row, Auto by default, given back under memory pressure
Some checks failed
PR description / Title and description (pull_request_target) Has been cancelled
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
281e41aa83
HumanBean asked for the BC images kept in system memory to be a setting,
Auto by default, with Max and Off, Auto scaled by the PC's memory.

The row is system/graphics:texture_ram_cache, in Graphics > GPU and
memory. RX_VK_BC_KEEP_MB now sets it for a run (auto, 1024, 0) and the
renderer reads the setting, not the switch. Auto keeps nothing under
12 GB of RAM or on an integrated GPU, 256 MB at 12 GB up to 1 GB at
24 GB and more, and never over a quarter of the memory available when
the first eviction makes the copies' memory. Max is 1 GB on any PC.

Under memory pressure (Windows' commit reserve, Linux's MemAvailable
reserves, the same checks that already cut the textures' budget and
give the capture arena back), every kept copy is dropped and the pool
is freed once the GPU is done with them; nothing is kept again that run.

test_readback_timeout_reuse's fixture stubs what the copies ahead of an
eviction (a8eba9bc) read, which it no longer compiled without.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fix(vk): count a 16 GB PC as 16 GB in Auto, and no give-back line when Off
Some checks are pending
CI / What the change needs (pull_request) Blocked by required conditions
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Blocked by required conditions
CI / Third-party parts and guide screenshots (pull_request) Blocked by required conditions
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Blocked by required conditions
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Blocked by required conditions
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
PR description / Title and description (pull_request_target) Waiting to run
652c92d778
A 16 GB laptop reports 15832 MB of RAM: rounded to the nearest GB that
is 15, and Auto kept 448 MB instead of the 512 MB the curve gives 16 GB.
The RAM is now rounded up.

With the setting at Off, Linux memory pressure still logged "its 0 kept
images dropped and its memory given back": keep_give_back now does
nothing when there is no cap.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

@HumanBean: done as you asked, 281e41aa and 652c92d7. The description is updated; Measurements step 6 has the numbers.

  • The setting: Graphics > GPU, "Texture RAM cache": Auto (default), Max (1 GB), Off. Applied at restart. RX_VK_BC_KEEP_MB stays its switch for runs, with auto, 1024 or 0.
  • Auto is your curve: nothing under 12 GB of RAM or on an integrated GPU, 256 MB at 12 GB up to 1 GB at 24 GB and more (16 GB: 512 MB). The RAM is rounded up, since a 16 GB laptop reports 15832 MB.
  • Memory check: Auto never takes more than a quarter of the memory free when the cache is first set up, and nothing if that leaves under 64 MB. Under memory pressure (on Windows, commit headroom under its reserve; on Linux, the same shortfall that already gives back the capture arena), every kept copy is dropped and the pool freed. It goes before anything else, since it only saves decodes, and nothing is kept again that run.
  • Measured, Off against Auto, 3 runs a side on the five-area tour: within run-to-run spread on both machines, no gain and no loss. Desktop (RTX 5080, 3584 MB budget, 1024 MB cap): fps 157.0 → 156.8, frames of 50 ms or more 13 → 14. Laptop (RTX 3060 Laptop 6 GB, 448 MB cap before the rounding fix): fps 59.6 → 59.4, frames of 50 ms or more 148 → 151.

Any gain would be on smaller cards. "Still to test" has the farm runs for a 4 GB card and an 8 GB card on PCIe x4, and one for a small card with 8 GB of RAM, where Auto keeps nothing (Max against Off).

@HumanBean: done as you asked, 281e41aa and 652c92d7. The description is updated; Measurements step 6 has the numbers. - **The setting:** Graphics > GPU, "Texture RAM cache": Auto (default), Max (1 GB), Off. Applied at restart. `RX_VK_BC_KEEP_MB` stays its switch for runs, with `auto`, `1024` or `0`. - **Auto is your curve:** nothing under 12 GB of RAM or on an integrated GPU, 256 MB at 12 GB up to 1 GB at 24 GB and more (16 GB: 512 MB). The RAM is rounded up, since a 16 GB laptop reports 15832 MB. - **Memory check:** Auto never takes more than a quarter of the memory free when the cache is first set up, and nothing if that leaves under 64 MB. Under memory pressure (on Windows, commit headroom under its reserve; on Linux, the same shortfall that already gives back the capture arena), every kept copy is dropped and the pool freed. It goes before anything else, since it only saves decodes, and nothing is kept again that run. - **Measured, Off against Auto, 3 runs a side on the five-area tour:** within run-to-run spread on both machines, no gain and no loss. Desktop (RTX 5080, 3584 MB budget, 1024 MB cap): fps 157.0 → 156.8, frames of 50 ms or more 13 → 14. Laptop (RTX 3060 Laptop 6 GB, 448 MB cap before the rounding fix): fps 59.6 → 59.4, frames of 50 ms or more 148 → 151. Any gain would be on smaller cards. "Still to test" has the farm runs for a 4 GB card and an 8 GB card on PCIe x4, and one for a small card with 8 GB of RAM, where Auto keeps nothing (Max against Off).
neotoxic changed title from WIP: perf(vk): keep an evicted BC-kept ASTC texture's image in system memory, taken back instead of a decode (opt-in, RX_VK_BC_KEEP_MB) to WIP: perf(vk): keep an evicted BC-kept ASTC texture's image in system memory, taken back instead of a decode (Texture RAM cache, Auto by default) 2026-12-05 20:56:48 +00:00
Some checks are pending
CI / What the change needs (pull_request) Blocked by required conditions
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Blocked by required conditions
CI / Third-party parts and guide screenshots (pull_request) Blocked by required conditions
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Blocked by required conditions
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Blocked by required conditions
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
PR description / Title and description (pull_request_target) Waiting to run
This pull request has changes conflicting with the target branch.
  • gpu/vk/vk_texture.cpp
Some workflows are waiting to be reviewed.
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.
git fetch -u 'perf/bc-ram-cache:neotoxic-perf/bc-ram-cache'
git switch 'neotoxic-perf/bc-ram-cache'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!3183
No description provided.