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
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3183
Loading…
Reference in a new issue
No description provided.
Delete branch "neotoxic/Longshot:perf/bc-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
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.hkey #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.texture_ram_cachein 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, takesauto,1024or0.keep_give_back). It goes first, before the textures in use: it only saves decodes.vk: Texture RAM cache Auto: 1024 MB at most for evicted BC images (32133 MB of RAM, 17390 MB available).reuse_upload), so it is never kept.keep_memory,create_cached_poolinvk_vma.cpp); until it is there an evicted image is not kept. Before, each copy was acreate_buffer_cachedon the replay thread, and a new VMA block there cost up to 9 ms (desktop) and 21 ms (laptop) for a single image.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=1keeps none over a quarter of the cap, as #2273 does, andRX_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).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), thenkeep_cap,keep_memory,keep_store,keep_take,keep_frameandkeep_give_backingpu/vk/vk_texture.cpp, afterreuse_upload, its two calls under memory pressure intexture_frame,create_cached_pool/destroy_cached_poolingpu/vk/vk_vma.cpp, and the row ingame/mods/graphics/mod.toml.Testing
Tested:
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.--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.tests/test_bc_keep.pycovers 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 withRX_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 stubskeep_store, the eviction's new call.run_all.py --changed origin/mainon Windows: 124 passed, 2 failed, 11 skipped; test_image_view_retry and test_tex_desc_memo fail the same way onmainon this PC.3a0f7d8fwith these steps:dev.py rtthendev.py smokePASS on Windows;run_all.py --changed origin/main164 passed, 1 failed (test_dlssg, which fails on main too).281e41aaand652c92d7, on mainde6fb7b5):dev.py rtthendev.py smokePASS 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.cppcovers 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.pychecks 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_tableandtest_switches_removedpass;schema-gen --checkpasses.test_i18n_completeandtest_settings_area_keysfail on this branch exactly as onmainatde6fb7b5("Fill title screen" untranslated,mouse_aim_sensitivity); the new row's five texts are in all 13 languages.Still to test:
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), notmain. The switch is the only difference, so the comparison isolates it.Setup:
dev.py abwith the tour above (TOUR_DESTSstepping 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):
ca63a459, RX_VK_BC_KEEP_MB=0ca63a459, RX_VK_BC_KEEP_MB=1024RTX 5080, Windows 11,
RX_VK_VRAM_MB=3584:ca63a459, RX_VK_BC_KEEP_MB=0ca63a459, RX_VK_BC_KEEP_MB=1024Veix'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.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.
keep_storeCPU time a runThe 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 atRX_VK_BC_KEEP_MB=128(32 MB an image). A: step 3.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 main3a0f7d8f.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:
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):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, on281e41aa, the same tour. Auto gives the desktop (32 GB of RAM, 17 GB free) 1024 MB; the laptop reports 15832 MB of RAM, which281e41aacounted as 15 GB, so 448 MB (652c92d7rounds up: 512 MB, as the curve says for 16 GB).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 mainde6fb7b5; 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 in652c92d7.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
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis 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_lifterran past its 600 s limit on that laptop; I did not rerun it onmain. Windows--changed: see above.main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
RX_VK_BC_KEEP_MBis itsSETTINGline ingpu/vk/switches.def, the three other switches stay off with their rows.tests/that reads changed source is updated, not weakened.docs/dev/reference/switches.md.Assisted-by:/Co-Authored-By:trailers.🤖 Generated with Claude Code
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.
Possibly related, for context:
b77c2f4b) into perf/bc-ram-cache cda61d8ee2e345b3cab) into perf/bc-ram-cacheBrought 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.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:
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.
#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 :)
18edd5097) into perf/bc-ram-cacheBrought 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 onmainat18edd509too, on this PC, run there with the same toolchain.test_pass_barrier.pyhas failed onmainsince #3161; #3196 fixes it.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
autotoday 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.
3a0f7d8f7) into perf/bc-ram-cache d17951acedVeix'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".
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.
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)
@HumanBean: done as you asked,
281e41aaand652c92d7. The description is updated; Measurements step 6 has the numbers.RX_VK_BC_KEEP_MBstays its switch for runs, withauto,1024or0.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).
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)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.