perf(vk): use smaller VMA block sizes on < 7 GB GPUs #1073
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1073
Loading…
Reference in a new issue
No description provided.
Delete branch "flix331/Longshot:perf/vk-vram-6gb-blocks"
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?
Type: perf · Risk: low · Tested on: Windows, NVIDIA GeForce RTX 3050 6GB Laptop GPU (driver 610.62) · Needs: Linux / AMD / 8 GB+ GPU verification
Why
On
< 7 GBGPUs (such as an RTX 3050 6 GB Laptop with5,233 MBdriver allowance), VMA's default128 MBblocks for texture, image, and render-target pools (and64 MBgeneral heap blocks) waste substantial headroom per partially filled block and force compactions to move large amounts of texture data before a full128 MBblock can be returned to the driver. In Castle Town Market onmain,1,119 MBsat trapped free inside VMA blocks while the texture cache was squeezed to1,553textures (budget 1,336 MB).Halving those pool block sizes to
64 MB(textures/images) and32 MB(transient render-target pool and general heap) strictly when physical VRAM (DEVICE_LOCALheap) is< 7 GBreduces wasted tail space inside blocks, keeps more textures cached, and lowers longest-submission latency without touching8 GB+cards.What changed
Added
< 7 GBphysical VRAM gating (constrained_vram()) ingpu/vk/vk_vma.cppso only constrained cards use smaller VMA block sizes (8 GB+cards keepmain's128 MB / 64 MBvalues).device_local_heap_bytes()andconstrained_vram()(heap > 0 && heap < 7 GB) ingpu/vk/vk_vma.cpp.preferredLargeHeapBlockSizeto32 MBon< 7 GBcards (64 MBotherwise) invma_init().kImageBlockwithimage_block_bytes()(64 MBon< 7 GB,128 MBotherwise, still overridable viaRX_VK_IMAGE_BLOCK_MB) and cappedtarget_images_pool()at32 MBon< 7 GBcards.Measurements
Verdict: Across a matched 2.5-minute Castle Town Market run (
p=1440..4920,30samples each), free space trapped in VMA blocks dropped by485 MB(634 MBvs1,119 MBonmain),+631more textures stayed resident (2,184vs1,553), longest submission dropped by2.11 ms(16.46 msvs18.57 ms), and minimum FPS improved from14.6to16.6 fpswhile eliminating5.4–5.6 GBovershoot spikes.Hardware: Windows 11, NVIDIA GeForce RTX 3050 6GB Laptop GPU (
6,001 MBphysical VRAM,5,233 MBdriver allowance, driver610.62), 16 GB RAM, 12 threadsScene: Castle Town Market (
p=1440..4920,30samples each),1920x1080, game's own upscaler (dlss=0), no frame rate cap, syncedlongshot_options.iniMethod:
logs/longshot_log.txtparsed across the matchedp=1440..4920window (30samples onmain,30samples onPR (c))How to reproduce: Launch with
VK_ICD_FILENAMESpointing to NVIDIAnv-vk64.json, walk around Castle Town Market for ~2.5 minutes, and inspectvk: VRAM:,GPU time, andvk: textures from a poolinlogs/longshot_log.txt.e090cc4875201832128 MBtex/img/RT,64 MBheap64 MBtex/img,32 MBRT & heap (< 7 GB)30samples)2,3122,19521.29(14.6–26.9)21.05(16.6–27.0)25.61 ms/18.57 ms24.68 ms/16.46 ms12/6(5.2–5.6 GB)12/9(5.1–5.3 GB)5,098 MB used (1,119 MB free in blocks); 1,553 textures (budget 1,336 MB)5,249 MB used (634 MB free in blocks); 2,184 textures (budget 1,605 MB)Flags and settings
None
Testing
Tested:
nx2/make_game.py --update(llvm-mingw) and tested in Castle Town Market (longshot_log.txt).tests/test_vram_compact.py:PASS: the compaction's rules.tests/test_vram_pressure.py:PASS: the shadows go by what is really allocated.Not tested: Linux, AMD, Intel Arc/iGPU,
8 GB+dedicated GPUs, and fulltests/run_all.py.Failed (expected pending maintainer decision on test fixture):
tests/test_blocks_at_start.py: fails compilingpools.cppbecause its source-extraction fixture expectskImageBlockinstead ofimage_block_bytes().Must be tested:
128 MB/64 MBblock sizes and behavior identical tomain, by hand: checkvk: textures from a poolandvk: render targets' imageslines inlongshot_log.txt.Risks
What could break: Twice as many VMA
VkDeviceMemoryblock allocations for textures (64 MBvs128 MB) and render targets (32 MBvs128 MB) on< 7 GBGPUs, still well below typical driver allocation limits (~4,096).8 GB+GPUs keepmainbehavior.Turning it off: Revert commit
75201832(or setRX_VK_IMAGE_BLOCK_MB=128).Compatibility: Nothing changes (saves,
longshot_options.ini, shader/pipeline caches, and vk ABI are unaffected).Issues
See #939
Checklist
Everyone
type(scope): description) of the status line's type.main.python -I -B tests/run_all.pypasses (pending maintainer decision ontests/test_blocks_at_start.pyfixture expectingkImageBlock).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.75201832659e62352fb8@Beanstalk
Beanstalk test farm
Commit
9e62352· tier reviewed · approved by HumanBean · dashboardWhat will be tested · AI plan (
gpt-6.1-terra)The plan compares main and the pull request on a 6 GB GPU and an 8 GB-or-larger GPU, covering both sides of the allocator threshold. A short Market and area-change tour measures frame times, VRAM and load times, and compares screenshots for visual regressions. Crashes, failed pool creation, substantial performance or memory regressions, and screenshot differences fail the plan; each tester’s game runs take about 14 minutes.
Done. Run 2: the farm's reports were cleared after its Windows and Linux fixes: started again. Comment
@Beanstalkto run it again.AMD Custom GPU 0932 (RADV VANGOGH), 26.1.99NVIDIA GeForce RTX 4090, 621.24.0Measurements · vram6gb
Verdict: could not be judged on vram6gb (1/1 report).
Hardware: 1: linux+amd+radv+igpu+handheld
AMD Custom GPU 0932 (RADV VANGOGH), 26.1.99AMD Custom APU 0932, 16 GB RAMScene: market-tour, from a new save (dlss=0, frame_rate_cap=6); each cell's settings and switches as the table names them
Method: the Beanstalk test farm: main and this PR built and run on the same PC in one session, game starts alternating (ABBA); market-tour: 1 game start a side, market 60 s after 10 s warm-up, field-after-tour 30 s after 5 s warm-up; each number the median of the runs, then of 1 report
How to reproduce: the farm's plan above (
@Beanstalkon this PR runs it again)02903b09e62352Flags and settings
None: the plan varied no setting, switch or build option.
Markdown for the PR description
Measurements · vram-ge8gb
Verdict: fail on vram-ge8gb (1/1 report):
market-tour screenshot.max_diff;market-tour market.frametime_p99_ms.max_delta_pct (+25.0%)failed.Hardware: 1: windows+nvidia+nvidia-proprietary+dgpu
NVIDIA GeForce RTX 4090, 621.24.013th Gen Intel(R) Core(TM) i9-13900K, 64 GB RAMScene: market-tour, from a new save (dlss=0, frame_rate_cap=6); each cell's settings and switches as the table names them
Method: the Beanstalk test farm: main and this PR built and run on the same PC in one session, game starts alternating (ABBA); market-tour: 1 game start a side, market 60 s after 10 s warm-up, field-after-tour 30 s after 5 s warm-up; each number the median of the runs, then of 1 report
How to reproduce: the farm's plan above (
@Beanstalkon this PR runs it again)02903b09e62352Flags and settings
None: the plan varied no setting, switch or build option.
Markdown for the PR description
WIP:perf(vk): use smaller VMA block sizes on < 7 GB GPUsto perf(vk): use smaller VMA block sizes on < 7 GB GPUsNicely contained: the change stays in
vk_vma.cpp, and 8 GB+ cards keep main's sizes. Smaller blocks waste less space at the end of each one, so the idea holds. A few things before merge:tests/test_target_image_pool.py:56also looks forpi.blockSize = kImageBlock;. AGENTS.md asks for checks to be updated in the same PR. Please update both, and givetest_blocks_at_start's fixture a stand-inconstrained_vram.device_vram_bytes()in place of the new heap loop. It goes throughRX_VK_TEST_VRAM_MB(#1107, made so this PR can be measured on a big card), and it's the < 7 GB line the DLSS preset already uses.RX_VK_IMAGE_BLOCK_MB=128doesn't bring main's behaviour back. TheRX_VK_IMAGE_PREALLOCrow indocs/development.mdstill says 128 MiB. The block switch's default now depends on the card, so it needs a row of its own.Overlap: #637 makes this same change with other numbers (≤ 8 GB, 16 MB target blocks), so the two conflict. #1057 adds its own
is_constrained_vram(). One shared helper would serve both.This is from reading the code only; nothing was built or run.
🤖 Generated with Claude Code
@ -21,6 +21,18 @@ namespace mkwvk {VmaAllocator vma;static uint64_t device_local_heap_bytes() {Main already has this as
device_vram_bytes()(vk_memory.cpp, declared invk_internal.h). It also appliestest_vram_bytes(), soRX_VK_TEST_VRAM_MBcaps it. As written,RX_VK_TEST_VRAM_MB=6144on a 24 GB card lowers the count inblocks_at_start(which reads throughtest_vram_byteson main) but keeps 128 MB blocks, a mix no real card has. Usingdevice_vram_bytes()here also keeps the gate on the same < 7 GB line as the DLSS preset (upscaler_rows.h).@ -35,2 +47,3 @@ci.pVulkanFunctions = &f;ci.preferredLargeHeapBlockSize = 64ull << 20; // (64 MB blocks: Eglantine found 256 MB ones wasted VRAM)// 64 MB blocks on >= 7 GB cards; 32 MB on < 7 GB cards to halve fragmentation in default heap blocksci.preferredLargeHeapBlockSize = constrained_vram() ? (32ull << 20) : (64ull << 20);Two things this changes that the Risks section doesn't cover. VMA gives an allocation over half the default block its own memory (the comment at the top of the images' pool says the same), so on < 7 GB cards that cut-off drops from 32 MB to 16 MB for everything outside the pools. This also isn't undone by
RX_VK_IMAGE_BLOCK_MB=128, which the description gives as the way to turn the change off.@ -48,1 +61,3 @@const uint64_t kImageBlock = (getenv("RX_VK_IMAGE_BLOCK_MB") ? (uint64_t)std::max(16, atoi(getenv("RX_VK_IMAGE_BLOCK_MB"))) : 128u) << 20;static uint64_t image_block_bytes() {if (const char* e = getenv("RX_VK_IMAGE_BLOCK_MB")) return (uint64_t)std::max(16, atoi(e)) << 20;return (constrained_vram() ? 64ull : 128ull) << 20;The default of
RX_VK_IMAGE_BLOCK_MBnow depends on the card, but the switch has no row indocs/development.md. TheRX_VK_IMAGE_PREALLOCrow there still says "Initial 128 MiB image blocks". Could both go in this PR? Also,constrained_vram()scans the heaps on every call. Astatic constwould make it read once, like the other switches in this file.@ -71,11 +87,12 @@ VmaPool target_images_pool() {ai.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;uint32_t type = 0;if (vmaFindMemoryTypeIndexForImageInfo(vma, &rep, &ai, &type) != VK_SUCCESS) return VK_NULL_HANDLE;const uint64_t block_size = constrained_vram() ? std::min<uint64_t>(image_block_bytes(), 32ull << 20) : image_block_bytes();A custom VMA pool can't hold an allocation larger than its block. So a render target over 32 MB fails here and falls through to
create_image's default path, where (with the 32 MB default block fromvma_init) it gets a dedicatedvkAllocateMemory. That covers 4K 16-bit float and depth targets (~63 MiB) and anything larger at 1440p, which is the per-target churn this pool exists to stop. It also caps the target pool at 32 MB even whenRX_VK_IMAGE_BLOCK_MBis set, so the switch can't restore main. Could you check a 1440p/4K run, or keep this atimage_block_bytes()(64 MB)?Tested on Linux, measuring a 6 GB card's rules on an RTX 3090. This PR is merged with
mainc167ec67, plus #1107'sRX_VK_TEST_VRAM_MB, which is merged now. That switch makes this PR's <7 GB gate take effect on a large card.Hardware: RTX 3090, NVIDIA 620.70.06, CachyOS.
Settings: DLAA 2560x1440 with
RX_VK_TEST_VRAM_MB=6144 RX_VK_TEST_ALLOWED_MB=5233, as on the RTX 3050 6 GB Laptop of the description.Route: Hyrule Field, then the Market (teleports), walking and turning. Two sessions of this PR, interleaved with main's, five main sessions in all.
This PR is inside main's range on every line. Its VRAM peaks sit at the lower end of main's, and GPU time is 0.2 to 1 ms lower, but two sessions on this swapping PC can't separate that from noise. So it isn't worse, and isn't measurably better here. A real 6 GB card, where going over the allowance actually pages, is the test that would show the smaller blocks paying off. On Linux NVIDIA, going over costs next to nothing.
🤖 Generated with Claude Code
Can you make it clear what the gain is from this, if frame time / FPS doesn't change?
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.