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

Open
opened 2026-11-26 21:19:18 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1874
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/vk-memory-fallback-and-stage-pool-caps


📝 Commits (1)

  • 6fbb655 runtime: BAR-aware memory fallback and bounded KHR/XDXR stage/query pools

📊 Changes

4 files changed (+1151 additions, -56 deletions)

View changed files

📝 runtime/CMakeLists.txt (+12 -0)
📝 runtime/src/vulkan_backend.h (+171 -46)
📝 runtime/src/vulkan_present_compose.h (+9 -10)
➕ runtime/test/vk_memory_fallback_and_pools_test.cpp (+959 -0)

📄 Description

Item: RT-VK-MEMORY-FALLBACK-AND-STAGE-POOL-CAPS
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164142288

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

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1874 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/vk-memory-fallback-and-stage-pool-caps` --- ### 📝 Commits (1) - [`6fbb655`](https://github.com/ubiquitarianism/leonida/commit/6fbb6552efae6f8c091777899318d72e111ddcee) runtime: BAR-aware memory fallback and bounded KHR/XDXR stage/query pools ### 📊 Changes **4 files changed** (+1151 additions, -56 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+12 -0) 📝 `runtime/src/vulkan_backend.h` (+171 -46) 📝 `runtime/src/vulkan_present_compose.h` (+9 -10) ➕ `runtime/test/vk_memory_fallback_and_pools_test.cpp` (+959 -0) </details> ### 📄 Description Item: `RT-VK-MEMORY-FALLBACK-AND-STAGE-POOL-CAPS` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164142288 ### 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
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
starred/leonida#1888
No description provided.