runtime: BAR-aware memory fallback and bounded KHR/XDXR stage/query pools (RT-VK-MEMORY-FALLBACK-AND-STAGE-POOL-CAPS) #1874

Open
github-bot wants to merge 1 commit from github-bot/gh-1874 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 13:49 UTC · original
IlyassRS3:fix/vk-memory-fallback-and-stage-pool-caps → main · +1151 −56 · 4 files · 1 commits · open

Item: RT-VK-MEMORY-FALLBACK-AND-STAGE-POOL-CAPS
Claim: #1608

Summary

Fixes three ad-hoc single-type vkAllocateMemory helpers and unbounded per-queue acceleration-structure staging/query pools on the multi-frame world rendering path:

  1. BAR-aware memory fallback and heap-size validation (runtime/src/vulkan_backend.h, runtime/src/vulkan_present_compose.h):

    • Added detail::device_memory_order and detail::host_stage_memory_order over leonida::memory_type_order + resizable_bar() (with a fallback for synthetic zero-heap mocks such as replay_query_edges_test.cpp where memoryHeapCount == 0).
    • PresentComposer::ensure_target: guards zero width/height before vkCreateImage (VUID-VkImageCreateInfo-extent-00944), avoids placing never-mapped RGBA16F compose targets in a 256 MiB non-ReBAR window when pure VRAM is available, skips undersized heaps (VUID-vkAllocateMemory-pAllocateInfo-01713), and retries remaining compatible types (including system RAM and host-only devices) on vkAllocateMemory failure while clearing failed handles before retry.
    • OcclusionResolve::initialize: applies the same BAR-aware ordering, heap-size filtering, OOM retry loop, and cleanup on vkBindBufferMemory failure.
    • VulkanQueue::acquire_stage (KHR TLAS ARRAY_OF_POINTERS and XDXR BLAS-address table staging): prefers DEVICE_LOCAL on Resizable BAR GPUs and system RAM (!DEVICE_LOCAL) on non-ReBAR GPUs so per-frame staging does not exhaust the 256 MiB BAR window, skips undersized heaps, and retries remaining host-visible coherent types on OOM.
  2. Bounded pools, pre-prepare reclamation, and best-fit reuse (runtime/src/vulkan_backend.h):

    • Added VulkanQueue::recycle_completed_khr_pools() so acquire_stage() and allocate_khr_as_queries() reclaim KHR/XDXR stages and postbuild VkQueryPools from already-completed command slots before prepare() allocates fresh ones (previously recycle_finished() ran in acquire() after prepare(), causing steady frame-to-frame traffic to double-allocate).
    • Switched acquire_stage() and allocate_khr_as_queries() to best-fit selection (smallest sufficient pooled entry).
    • Capped khr_stage_pool_ and khr_as_query_pool_ at 8 entries (kMaxPooledKhrStages / kMaxPooledKhrAsQueries), keeping the largest stages/pools and destroying evicted allocations immediately so monotonically growing title_va_aliases tables or TLAS bursts do not accumulate leaked VkDeviceMemory/VkQueryPool handles over sustained streaming.
  3. Regression test (runtime/test/vk_memory_fallback_and_pools_test.cpp, runtime/CMakeLists.txt):

    • Portable CPU/mock Vulkan test covering zero-extent rejection, non-ReBAR BAR avoidance, VRAM -> BAR -> RAM OOM fallback, ReBAR preference/fallback, undersized-heap skipping, host-only devices, synthetic zero-heap mock compatibility, failed-handle clearing, 32-step growing XDXR table & 16-build burst pool caps, 24-step growing KHR postbuild query pool caps, and best-fit + pre-prepare reuse.

Verification

  • AI-assisted: yes
  • GPUs tested: none (CPU/mock Vulkan verification on macOS arm64 with pinned Khronos Vulkan-Headers 6802bb4733b63ed5efd3adb308a6c885ef180ea1)
  • vk_memory_fallback_and_pools_test.cpp:
    • clang++ -std=c++20 -Wall -Wextra -Werror ... -fsanitize=address,undefined -> vk_memory_fallback_and_pools: ok
    • clang++ -std=c++20 -Wall -Wextra -Werror ... -fsanitize=thread -> vk_memory_fallback_and_pools: ok
    • clang++ -std=c++20 -Wall -Wextra -Werror ... -O2 (3 runs) -> vk_memory_fallback_and_pools: ok
  • Existing tests under ASan+UBSan:
    • vk_host_at_aliasing_test.cpp -> vk_host_at_aliasing: ok
    • replay_query_edges_test.cpp -> passed
    • replay_khr_as_copy_postbuild_test.cpp -> passed
  • Mutation test (running the new test against unmodified main vulkan_backend.h and vulkan_present_compose.h): 25 failures reproduced cleanly, exit code 1.
  • Hygiene:
    • python3 -I scripts/check_no_game_data.py runtime/src/vulkan_backend.h runtime/src/vulkan_present_compose.h runtime/test/vk_memory_fallback_and_pools_test.cpp runtime/CMakeLists.txt -> OK
    • python3 -I tests/test_text_write_line_endings.py -> 3 tests OK
**@⁠IlyassRS3** on GitHub · 2026-11-25 13:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1874) `IlyassRS3:fix/vk-memory-fallback-and-stage-pool-caps` → `main` · +1151 −56 · 4 files · 1 commits · open Item: `RT-VK-MEMORY-FALLBACK-AND-STAGE-POOL-CAPS` Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ### Summary Fixes three ad-hoc single-type `vkAllocateMemory` helpers and unbounded per-queue acceleration-structure staging/query pools on the multi-frame world rendering path: 1. **BAR-aware memory fallback and heap-size validation (`runtime/src/vulkan_backend.h`, `runtime/src/vulkan_present_compose.h`)**: - Added `detail::device_memory_order` and `detail::host_stage_memory_order` over `leonida::memory_type_order` + `resizable_bar()` (with a fallback for synthetic zero-heap mocks such as `replay_query_edges_test.cpp` where `memoryHeapCount == 0`). - **`PresentComposer::ensure_target`**: guards zero width/height before `vkCreateImage` (`VUID-VkImageCreateInfo-extent-00944`), avoids placing never-mapped RGBA16F compose targets in a 256 MiB non-ReBAR window when pure VRAM is available, skips undersized heaps (`VUID-vkAllocateMemory-pAllocateInfo-01713`), and retries remaining compatible types (including system RAM and host-only devices) on `vkAllocateMemory` failure while clearing failed handles before retry. - **`OcclusionResolve::initialize`**: applies the same BAR-aware ordering, heap-size filtering, OOM retry loop, and cleanup on `vkBindBufferMemory` failure. - **`VulkanQueue::acquire_stage`** (KHR TLAS `ARRAY_OF_POINTERS` and XDXR BLAS-address table staging): prefers `DEVICE_LOCAL` on Resizable BAR GPUs and system RAM (`!DEVICE_LOCAL`) on non-ReBAR GPUs so per-frame staging does not exhaust the 256 MiB BAR window, skips undersized heaps, and retries remaining host-visible coherent types on OOM. 2. **Bounded pools, pre-prepare reclamation, and best-fit reuse (`runtime/src/vulkan_backend.h`)**: - Added `VulkanQueue::recycle_completed_khr_pools()` so `acquire_stage()` and `allocate_khr_as_queries()` reclaim KHR/XDXR stages and postbuild `VkQueryPool`s from already-completed command slots before `prepare()` allocates fresh ones (previously `recycle_finished()` ran in `acquire()` after `prepare()`, causing steady frame-to-frame traffic to double-allocate). - Switched `acquire_stage()` and `allocate_khr_as_queries()` to best-fit selection (smallest sufficient pooled entry). - Capped `khr_stage_pool_` and `khr_as_query_pool_` at 8 entries (`kMaxPooledKhrStages` / `kMaxPooledKhrAsQueries`), keeping the largest stages/pools and destroying evicted allocations immediately so monotonically growing `title_va_aliases` tables or TLAS bursts do not accumulate leaked `VkDeviceMemory`/`VkQueryPool` handles over sustained streaming. 3. **Regression test (`runtime/test/vk_memory_fallback_and_pools_test.cpp`, `runtime/CMakeLists.txt`)**: - Portable CPU/mock Vulkan test covering zero-extent rejection, non-ReBAR BAR avoidance, VRAM -> BAR -> RAM OOM fallback, ReBAR preference/fallback, undersized-heap skipping, host-only devices, synthetic zero-heap mock compatibility, failed-handle clearing, 32-step growing XDXR table & 16-build burst pool caps, 24-step growing KHR postbuild query pool caps, and best-fit + pre-prepare reuse. ### Verification - **AI-assisted**: yes - **GPUs tested**: none (CPU/mock Vulkan verification on macOS arm64 with pinned Khronos `Vulkan-Headers` `6802bb4733b63ed5efd3adb308a6c885ef180ea1`) - `vk_memory_fallback_and_pools_test.cpp`: - `clang++ -std=c++20 -Wall -Wextra -Werror ... -fsanitize=address,undefined` -> `vk_memory_fallback_and_pools: ok` - `clang++ -std=c++20 -Wall -Wextra -Werror ... -fsanitize=thread` -> `vk_memory_fallback_and_pools: ok` - `clang++ -std=c++20 -Wall -Wextra -Werror ... -O2` (3 runs) -> `vk_memory_fallback_and_pools: ok` - Existing tests under ASan+UBSan: - `vk_host_at_aliasing_test.cpp` -> `vk_host_at_aliasing: ok` - `replay_query_edges_test.cpp` -> passed - `replay_khr_as_copy_postbuild_test.cpp` -> passed - Mutation test (running the new test against unmodified `main` `vulkan_backend.h` and `vulkan_present_compose.h`): **25 failures** reproduced cleanly, exit code 1. - Hygiene: - `python3 -I scripts/check_no_game_data.py runtime/src/vulkan_backend.h runtime/src/vulkan_present_compose.h runtime/test/vk_memory_fallback_and_pools_test.cpp runtime/CMakeLists.txt` -> OK - `python3 -I tests/test_text_write_line_endings.py` -> 3 tests OK
- Replace single-type vkAllocateMemory picks in PresentComposer::ensure_target,
  OcclusionResolve::initialize, and VulkanQueue::acquire_stage with ordered
  memory_type_order + BAR-aware fallback and OOM retry across compatible types,
  enforcing heap-size bounds (VUID-vkAllocateMemory-pAllocateInfo-01713) and
  clearing memory handles on failed allocation attempts.
- Reject zero-extent compose targets in PresentComposer::ensure_target before
  vkCreateImage (VUID-VkImageCreateInfo-extent-00944).
- Reclaim completed slots' KHR/XDXR stages and postbuild query pools before
  prepare() allocates fresh ones, switch pool selection to best-fit, and cap
  khr_stage_pool_ and khr_as_query_pool_ at 8 largest entries so growing world
  streaming tables do not leak VkDeviceMemory/VkQueryPool allocations.

Item: RT-VK-MEMORY-FALLBACK-AND-STAGE-POOL-CAPS
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:31 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 13:49 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 13:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1874#pullrequestreview-6058813492) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠Timon360SB on GitHub · 2026-11-25 15:14 UTC · original

Independent verification — Turing and Blackwell

  • Base: 5e95fa2f46d5df1a8397d5ee871add369031d8b6
  • Head: 6fbb6552efae6f8c091777899318d72e111ddcee

Environment

GPU Windows 11 build
RTX 5070 (Blackwell) 26200
RTX 2060 SUPER (Turing) 28000

NVIDIA 621.14, Vulkan SDK 1.4.363.0,
MSVC 19.44.35230, Ninja RelWithDebInfo.

Three repetitions per GPU, revision and validation mode.
The new mock-Vulkan test was run once per system on HEAD.

Results

Test Base Head
vk_presenter_capture_colour 12/12 12/12
vk_binary_occlusion 12/12 12/12
vk_overlapping_occlusion 12/12 12/12
vk_xdxr_postbuild_readback 12/12 12/12
replay_query_edges 12/12 12/12
replay_khr_as_copy_postbuild 12/12 12/12
vk_memory_fallback_and_pools N/A 2/2
Total 72/72 74/74

146 executions: 146 passed, 0 failed, 0 skipped.

Findings

  • The new memory fallback and pool-cap mock test
    passes under Windows/MSVC on both systems.
  • Binary occlusion: 1026 visible, 3 zero and 3 repeated;
    offsets and sentinels exact.
  • Overlapping occlusion: 261 segments summed correctly.
  • XDXR postbuild readback: reported_size=320,
    expected=320, compute_to_host_barriers=1.
  • No VUID diagnostics in the captured logs.
    Validation-layer feature-adjustment warnings were present.

No regressions observed on either architecture.
Allocation fallback and pool limits were covered by the
new mock test; physical BAR exhaustion and long-running
world-rendering workloads were not exercised.

Synthetic tests only. No game files used.
AI-assisted: yes.

**@⁠Timon360SB** on GitHub · 2026-11-25 15:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1874#issuecomment-7166536649) ## Independent verification — Turing and Blackwell - **Base:** `5e95fa2f46d5df1a8397d5ee871add369031d8b6` - **Head:** `6fbb6552efae6f8c091777899318d72e111ddcee` ### Environment | GPU | Windows 11 build | |---|---| | RTX 5070 (Blackwell) | 26200 | | RTX 2060 SUPER (Turing) | 28000 | NVIDIA 621.14, Vulkan SDK 1.4.363.0, MSVC 19.44.35230, Ninja RelWithDebInfo. Three repetitions per GPU, revision and validation mode. The new mock-Vulkan test was run once per system on HEAD. ### Results | Test | Base | Head | |---|---:|---:| | `vk_presenter_capture_colour` | 12/12 | 12/12 | | `vk_binary_occlusion` | 12/12 | 12/12 | | `vk_overlapping_occlusion` | 12/12 | 12/12 | | `vk_xdxr_postbuild_readback` | 12/12 | 12/12 | | `replay_query_edges` | 12/12 | 12/12 | | `replay_khr_as_copy_postbuild` | 12/12 | 12/12 | | `vk_memory_fallback_and_pools` | N/A | 2/2 | | **Total** | **72/72** | **74/74** | 146 executions: 146 passed, 0 failed, 0 skipped. ### Findings - The new memory fallback and pool-cap mock test passes under Windows/MSVC on both systems. - Binary occlusion: 1026 visible, 3 zero and 3 repeated; offsets and sentinels exact. - Overlapping occlusion: 261 segments summed correctly. - XDXR postbuild readback: reported_size=320, expected=320, compute_to_host_barriers=1. - No VUID diagnostics in the captured logs. Validation-layer feature-adjustment warnings were present. No regressions observed on either architecture. Allocation fallback and pool limits were covered by the new mock test; physical BAR exhaustion and long-running world-rendering workloads were not exercised. Synthetic tests only. No game files used. AI-assisted: yes.
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/1874/head:github-bot/gh-1874'
git switch 'github-bot/gh-1874'

Merge

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