d3d12/vulkan: add 4 GB low-VRAM budget steering, priority/pageable memory, and LEONIDA_RT_AS=off (VK-LOW-VRAM-BUDGET) #443

Open
github-bot wants to merge 2 commits from github-bot/gh-443 into main AGit
Owner

@⁠jujumoneylol on GitHub · 2026-11-24 05:25 UTC · original
jujumoneylol:pr/vulkan-low-vram → main · +738 −34 · 7 files · 2 commits · open

Claimed in the coordination issue as: VK-LOW-VRAM-BUDGET (#14)

What and why

Adds an automatic low-VRAM survival layer in the Vulkan D3D12 shim for discrete GPUs with <= 4.5 GiB dedicated VRAM (e.g., 4 GB GTX 1650 / RTX 3050 4 GB) while leaving larger GPUs (>= 6 GB) and UMA iGPUs untouched by default.

  • Vulkan memory management extensions: opportunistically enables VK_EXT_memory_budget, VK_EXT_memory_priority, and VK_EXT_pageable_device_local_memory when advertised by the driver, chaining VkMemoryPriorityAllocateInfoEXT (1.0 for critical render targets / depth-stencil / arena allocations vs 0.25 for spillable textures/buffers).
  • Proactive VRAM budget steering (runtime/src/vulkan_memory.h, runtime/shims/d3d12_vulkan_device.cpp):
    • Auto-enables on discrete GPUs with <= 4.5 GiB DEVICE_LOCAL VRAM (overridable via LEONIDA_LOW_VRAM=on|off|auto, LEONIDA_VRAM_RESERVE_MB, LEONIDA_VRAM_CAP_MB, LEONIDA_VRAM_LOG=1).
    • Preserves a default 512 MiB DEVICE_LOCAL headroom reserve for critical RT/DS targets; once live VK_EXT_memory_budget headroom drops below the reserve, spillable allocations are proactively routed to system RAM (HOST_VISIBLE) instead of evicting render targets or causing VK_ERROR_OUT_OF_DEVICE_MEMORY.
    • Allows sparse tile heaps (Heap::tiles) to fall back to system memory when VRAM is exhausted instead of returning E_OUTOFMEMORY.
    • Leaves pipeline cache persistence untouched so this does not overlap #218.
  • Ray-tracing opt-out (LEONIDA_RT_AS=off): cleanly disables RT acceleration structures on non-RT / low-VRAM GPUs and answers GetRaytracingAccelerationStructurePrebuildInfo with minimal 256-byte aligned sizes so callers do not allocate or divide by zero.
  • Tests: adds unit tests for the budget/placement policy in runtime/test/d3d12_state_map_test.cpp and a live GPU stress test (runtime/test/d3d12_low_vram_test.cpp) that oversubscribes VRAM past 100% and verifies critical RT/DS pinning + spillable steering + LEONIDA_RT_AS=off.

Tested

  • Built d3d12_state_map_test, d3d12_low_vram_test, and d3d12_vulkan_device_test with MSVC 19.44 (vcvars64) + Ninja on top of main 8f481cf16.
  • d3d12_state_map_test: PASSED (including new low-VRAM policy & placement unit tests).
  • d3d12_low_vram_test: ALL CHECKS PASSED on GTX 1650 (3,935 MiB VRAM, budget=1 priority=1 pageable=1, 512 MiB reserve):
    • Oversubscribed 277 × 16 MiB (4,432 MiB) spillable textures + 6 critical RT/DS targets: 173 textures in VRAM, 104 (1,664 MiB) proactively steered to system RAM, 0 forced spills, 6/6 critical RT/DS pinned in VRAM.
    • Verified LEONIDA_RT_AS=off disables RT and returns 256-byte prebuild sizes.
  • d3d12_vulkan_device_test: PASSED (60/60 resolves matched, 20/20 ID3D12Heap::GetDesc matched, host-image/staging copies, castable formats, and Xbox-ABI proxy checks matched).
  • py -3.13 -I scripts/check_no_game_data.py runtime/CMakeLists.txt runtime/shims/d3d12_com.h runtime/shims/d3d12_vulkan_device.cpp runtime/src/vulkan_memory.h runtime/test/d3d12_low_vram_test.cpp runtime/test/d3d12_state_map_test.cpp: clean.

GPUs tested: NVIDIA GeForce GTX 1650 Mobile 4 GB (driver 592.00, Vulkan 1.4.325, Windows 11 26200)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — Vulkan D3D12 runtime only)
**@⁠jujumoneylol** on GitHub · 2026-11-24 05:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/443) `jujumoneylol:pr/vulkan-low-vram` → `main` · +738 −34 · 7 files · 2 commits · open Claimed in the coordination issue as: VK-LOW-VRAM-BUDGET (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Adds an automatic low-VRAM survival layer in the Vulkan D3D12 shim for discrete GPUs with <= 4.5 GiB dedicated VRAM (e.g., 4 GB GTX 1650 / RTX 3050 4 GB) while leaving larger GPUs (>= 6 GB) and UMA iGPUs untouched by default. - **Vulkan memory management extensions**: opportunistically enables `VK_EXT_memory_budget`, `VK_EXT_memory_priority`, and `VK_EXT_pageable_device_local_memory` when advertised by the driver, chaining `VkMemoryPriorityAllocateInfoEXT` (`1.0` for critical render targets / depth-stencil / arena allocations vs `0.25` for spillable textures/buffers). - **Proactive VRAM budget steering** (`runtime/src/vulkan_memory.h`, `runtime/shims/d3d12_vulkan_device.cpp`): - Auto-enables on discrete GPUs with <= 4.5 GiB `DEVICE_LOCAL` VRAM (overridable via `LEONIDA_LOW_VRAM=on|off|auto`, `LEONIDA_VRAM_RESERVE_MB`, `LEONIDA_VRAM_CAP_MB`, `LEONIDA_VRAM_LOG=1`). - Preserves a default 512 MiB `DEVICE_LOCAL` headroom reserve for critical RT/DS targets; once live `VK_EXT_memory_budget` headroom drops below the reserve, spillable allocations are proactively routed to system RAM (`HOST_VISIBLE`) instead of evicting render targets or causing `VK_ERROR_OUT_OF_DEVICE_MEMORY`. - Allows sparse tile heaps (`Heap::tiles`) to fall back to system memory when VRAM is exhausted instead of returning `E_OUTOFMEMORY`. - Leaves pipeline cache persistence untouched so this does not overlap #218. - **Ray-tracing opt-out (`LEONIDA_RT_AS=off`)**: cleanly disables RT acceleration structures on non-RT / low-VRAM GPUs and answers `GetRaytracingAccelerationStructurePrebuildInfo` with minimal 256-byte aligned sizes so callers do not allocate or divide by zero. - **Tests**: adds unit tests for the budget/placement policy in `runtime/test/d3d12_state_map_test.cpp` and a live GPU stress test (`runtime/test/d3d12_low_vram_test.cpp`) that oversubscribes VRAM past 100% and verifies critical RT/DS pinning + spillable steering + `LEONIDA_RT_AS=off`. ### Tested - Built `d3d12_state_map_test`, `d3d12_low_vram_test`, and `d3d12_vulkan_device_test` with MSVC 19.44 (`vcvars64`) + Ninja on top of `main` `8f481cf16`. - `d3d12_state_map_test`: PASSED (including new low-VRAM policy & placement unit tests). - `d3d12_low_vram_test`: ALL CHECKS PASSED on GTX 1650 (3,935 MiB VRAM, `budget=1 priority=1 pageable=1`, 512 MiB reserve): - Oversubscribed 277 × 16 MiB (4,432 MiB) spillable textures + 6 critical RT/DS targets: 173 textures in VRAM, 104 (1,664 MiB) proactively steered to system RAM, 0 forced spills, 6/6 critical RT/DS pinned in VRAM. - Verified `LEONIDA_RT_AS=off` disables RT and returns 256-byte prebuild sizes. - `d3d12_vulkan_device_test`: PASSED (60/60 resolves matched, 20/20 `ID3D12Heap::GetDesc` matched, host-image/staging copies, castable formats, and Xbox-ABI proxy checks matched). - `py -3.13 -I scripts/check_no_game_data.py runtime/CMakeLists.txt runtime/shims/d3d12_com.h runtime/shims/d3d12_vulkan_device.cpp runtime/src/vulkan_memory.h runtime/test/d3d12_low_vram_test.cpp runtime/test/d3d12_state_map_test.cpp`: clean. GPUs tested: NVIDIA GeForce GTX 1650 Mobile 4 GB (driver 592.00, Vulkan 1.4.325, Windows 11 26200) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — Vulkan D3D12 runtime only)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:00:28 +00:00
Author
Owner

@⁠Sangonapou on GitHub · 2026-11-24 19:00 UTC · original

Tested ccffa86b on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, 16 GB), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: d3d12_state_map passes, d3d12_low_vram fails on this 16 GB card

Built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files.

  • d3d12_state_map: passed (state map, enhanced barriers, subresource decoding, layout tracking, discard ranges, block formats, BAR detection and memory types OK).
  • d3d12_low_vram (ctest, verbose):
[rt] d3d12: low-VRAM survival mode ON (env; 16173 MiB VRAM, 2048 MiB reserved, budget=on, priority=on, pageable=on)
FAIL runtime\test\d3d12_low_vram_test.cpp:157: after.steered_allocations > 0
Device VRAM: 16173 MiB | low_vram_mode=0 (auto=0) | budget=1 priority=1 pageable=1 | reserve=0 MiB
Stress result (320 x 16 MiB = 5120 MiB spillable + 6 critical RT/DS):
  Spillable textures : 320 in VRAM, 0 steered/spilled to system RAM
  Critical RT/DS     : 6 in VRAM, 0 in system RAM
  Stats              : steered=0 (0 MiB), forced_spills=0 (0 MiB)

The 5120 MiB stress fits in 16 GB, so nothing is steered and the > 0 check fails. Note too that the stress run reports low_vram_mode=0 ... reserve=0 MiB, although the line above it turns the mode on with 2048 MiB reserved, so the stress device may not be the one the env enabled. Sizing the stress to the device's budget, or forcing a small budget for the test, would make it pass on large cards. Happy to rerun.

**@⁠Sangonapou** on GitHub · 2026-11-24 19:00 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/443#issuecomment-7141453650) Tested ccffa86b on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, 16 GB), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: `d3d12_state_map` passes, **`d3d12_low_vram` fails** on this 16 GB card Built at this head with `scripts\build_runtime.ps1` (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files. - `d3d12_state_map`: passed (`state map, enhanced barriers, subresource decoding, layout tracking, discard ranges, block formats, BAR detection and memory types OK`). - `d3d12_low_vram` (ctest, verbose): ``` [rt] d3d12: low-VRAM survival mode ON (env; 16173 MiB VRAM, 2048 MiB reserved, budget=on, priority=on, pageable=on) FAIL runtime\test\d3d12_low_vram_test.cpp:157: after.steered_allocations > 0 Device VRAM: 16173 MiB | low_vram_mode=0 (auto=0) | budget=1 priority=1 pageable=1 | reserve=0 MiB Stress result (320 x 16 MiB = 5120 MiB spillable + 6 critical RT/DS): Spillable textures : 320 in VRAM, 0 steered/spilled to system RAM Critical RT/DS : 6 in VRAM, 0 in system RAM Stats : steered=0 (0 MiB), forced_spills=0 (0 MiB) ``` The 5120 MiB stress fits in 16 GB, so nothing is steered and the `> 0` check fails. Note too that the stress run reports `low_vram_mode=0 ... reserve=0 MiB`, although the line above it turns the mode on with 2048 MiB reserved, so the stress device may not be the one the env enabled. Sizing the stress to the device's budget, or forcing a small budget for the test, would make it pass on large cards. Happy to rerun.
Author
Owner

@⁠jujumoneylol on GitHub · 2026-11-24 21:54 UTC · original

Thanks for testing on a 16 GB RTX 3080 Ti and catching both issues, @⁠Sangonapou!

Fixed in 08e18bdfc:

  1. Large-VRAM GPU test failure (d3d12_low_vram_test):

    • Previously, the >4.5 GiB re-create path only set LEONIDA_LOW_VRAM=1 and LEONIDA_VRAM_RESERVE_MB=2048 without a cap, leaving ~14 GiB of headroom on a 16 GB card so the 5,120 MiB stress loop never crossed the reserve.
    • It now sets LEONIDA_VRAM_CAP_MB=2048 + LEONIDA_VRAM_RESERVE_MB=512 and sizes the stress allocation to effective_budget + 512 MiB (2,560 MiB on capped large GPUs), so spillover is deterministically exercised on 8/16/24/32 GB cards while keeping all 6 critical RT/DS targets pinned in DEVICE_LOCAL VRAM.
    • Also flushes stdout and logs the re-created stress device's stats separately from the initial probe so low_vram_mode=0 and the stderr banner no longer interleave misleadingly.
  2. Environment-variable docs check (tests/test_docs_env_vars.py):

    • Documented LEONIDA_LOW_VRAM, LEONIDA_VRAM_RESERVE_MB, LEONIDA_VRAM_CAP_MB, LEONIDA_VRAM_LOG, and LEONIDA_RT_AS=off in docs/d3d12_vulkan.md (§4.8 and §4.10).

Re-verified on Windows 11 26200 / NVIDIA GTX 1650 (both uncapped 4 GB auto mode and LEONIDA_VRAM_CAP_MB=2048 mode), d3d12_state_map_test, tests/test_docs_env_vars.py (4/4), and scripts/check_no_game_data.py. Ready for a rerun on your 16 GB card!

AI-assisted fix and verification.

**@⁠jujumoneylol** on GitHub · 2026-11-24 21:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/443#issuecomment-7145552400) Thanks for testing on a 16 GB RTX 3080 Ti and catching both issues, @⁠Sangonapou! Fixed in `08e18bdfc`: 1. **Large-VRAM GPU test failure (`d3d12_low_vram_test`):** - Previously, the >4.5 GiB re-create path only set `LEONIDA_LOW_VRAM=1` and `LEONIDA_VRAM_RESERVE_MB=2048` without a cap, leaving ~14 GiB of headroom on a 16 GB card so the 5,120 MiB stress loop never crossed the reserve. - It now sets `LEONIDA_VRAM_CAP_MB=2048` + `LEONIDA_VRAM_RESERVE_MB=512` and sizes the stress allocation to `effective_budget + 512 MiB` (2,560 MiB on capped large GPUs), so spillover is deterministically exercised on 8/16/24/32 GB cards while keeping all 6 critical RT/DS targets pinned in `DEVICE_LOCAL` VRAM. - Also flushes stdout and logs the re-created stress device's stats separately from the initial probe so `low_vram_mode=0` and the `stderr` banner no longer interleave misleadingly. 2. **Environment-variable docs check (`tests/test_docs_env_vars.py`):** - Documented `LEONIDA_LOW_VRAM`, `LEONIDA_VRAM_RESERVE_MB`, `LEONIDA_VRAM_CAP_MB`, `LEONIDA_VRAM_LOG`, and `LEONIDA_RT_AS=off` in `docs/d3d12_vulkan.md` (§4.8 and §4.10). Re-verified on Windows 11 26200 / NVIDIA GTX 1650 (both uncapped 4 GB auto mode and `LEONIDA_VRAM_CAP_MB=2048` mode), `d3d12_state_map_test`, `tests/test_docs_env_vars.py` (4/4), and `scripts/check_no_game_data.py`. Ready for a rerun on your 16 GB card! AI-assisted fix and verification.
Author
Owner

@⁠jujumoneylol on GitHub · 2026-11-26 07:49 UTC · original

Rebased onto main (a73a57e13) → head 922235029, resolving conflicts with main's VK_EXT_memory_budget HUD reader, dedicated_image / memory_mutex(), and robustness2 / device_fault setup.

Re-verified on Windows 11 / NVIDIA GeForce GTX 1650 (4 GB):

  • ctest: d3d12_low_vram, d3d12_vram_budget, d3d12_dxr_validation, d3d12_state_map all PASS (0 failed; d3d12_dxr_validation_khr skipped on non-RT GPU)
  • d3d12_low_vram_test with LEONIDA_VRAM_CAP_MB=2048 + LEONIDA_VRAM_RESERVE_MB=512: 6/6 critical RT/DS pinned in VRAM, 68 spillable allocations (1,088 MiB) steered to system RAM, 0 forced spills
  • tests/test_docs_env_vars.py (4/4) and scripts/check_no_game_data.py PASS
**@⁠jujumoneylol** on GitHub · 2026-11-26 07:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/443#issuecomment-7185878174) Rebased onto `main` (`a73a57e13`) → head `922235029`, resolving conflicts with `main`'s `VK_EXT_memory_budget` HUD reader, `dedicated_image` / `memory_mutex()`, and `robustness2` / `device_fault` setup. Re-verified on Windows 11 / NVIDIA GeForce GTX 1650 (4 GB): - `ctest`: `d3d12_low_vram`, `d3d12_vram_budget`, `d3d12_dxr_validation`, `d3d12_state_map` all PASS (0 failed; `d3d12_dxr_validation_khr` skipped on non-RT GPU) - `d3d12_low_vram_test` with `LEONIDA_VRAM_CAP_MB=2048` + `LEONIDA_VRAM_RESERVE_MB=512`: 6/6 critical RT/DS pinned in VRAM, 68 spillable allocations (1,088 MiB) steered to system RAM, 0 forced spills - `tests/test_docs_env_vars.py` (4/4) and `scripts/check_no_game_data.py` PASS
Author
Owner

@⁠ammonium231 on GitHub · 2026-11-26 09:24 UTC · original

RTX 3090 / 621.40 (24322 MiB VRAM), validation on, this PR merged onto current main a73a57e13: d3d12_low_vram auto-steering stays off (low_vram_mode=0, auto=0, cap=0). With LEONIDA_LOW_VRAM=1 and a 2048 MiB cap the stress path passes (75 spillable in VRAM, 85 steered; 6/6 critical RT/DS pinned; LEONIDA_RT_AS=off ok). d3d12_state_map and d3d12_vram_budget also pass, 0 VUID.

**@⁠ammonium231** on GitHub · 2026-11-26 09:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/443#issuecomment-7187940387) RTX 3090 / 621.40 (24322 MiB VRAM), validation on, this PR merged onto current main a73a57e13: d3d12_low_vram auto-steering stays off (low_vram_mode=0, auto=0, cap=0). With LEONIDA_LOW_VRAM=1 and a 2048 MiB cap the stress path passes (75 spillable in VRAM, 85 steered; 6/6 critical RT/DS pinned; LEONIDA_RT_AS=off ok). d3d12_state_map and d3d12_vram_budget also pass, 0 VUID.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/443/head:github-bot/gh-443'
git switch 'github-bot/gh-443'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-443'
git switch 'github-bot/gh-443'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-443'
git switch 'github-bot/gh-443'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-443'
git switch 'main'
git merge --squash 'github-bot/gh-443'
git switch 'main'
git merge --ff-only 'github-bot/gh-443'
git switch 'main'
git merge 'github-bot/gh-443'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.