runtime: cache helper Vulkan device procs, fix uncounted BDA fault and BAR/cleanup guards (RT-VK-HELPER-PROC-CACHE-AND-GUARDS) #1910

Merged
IlyassRS3 merged 1 commit from upstream-pr/1910 into main 2026-11-29 12:44:58 +00:00
IlyassRS3 commented 2026-11-25 15:35:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T15:35:34Z.


Item ID: RT-VK-HELPER-PROC-CACHE-AND-GUARDS (claimed in #1608).

Summary

Hardens and speeds up the four Vulkan replay compute helpers (runtime/src/vulkan_indirect_dispatch_count.h, runtime/src/vulkan_predicate.h, runtime/src/vulkan_predicated_copy.h, runtime/src/quad_index_expand.h):

  1. Fix uncounted count_offset GPU BDA fault + immediate failure cleanup (vulkan_indirect_dispatch_count.h):
    • Previously, when LEONIDA_INDIRECT_GROUP_BUDGET was active and count == VK_NULL_HANDLE with a non-zero count_offset, Batch::prepare() accepted the call and wrote out.count = count_va + count_offset = count_offset != 0; emit() then treated job.count != 0 as has_count = 1 and passed count_offset as a raw GPU device address.
    • Batch::prepare() now rejects !count && count_offset != 0, explicitly sets out.count = count ? count_va + count_offset : 0, prefers pure VRAM ahead of the 256 MiB BAR window on non-ReBAR GPUs (!resizable_bar()), and destroys partial VkBuffer/VkDeviceMemory allocations immediately on failure instead of retaining dead handles until submission completion.
  2. BAR-aware memory selection, heap-size filtering, and OOM fallback (vulkan_predicate.h):
    • Replaces PredicateReducer::batch()'s first-DEVICE_LOCAL loop with memory_type_order() (with memoryHeapCount == 0 mock compatibility), prefers pure VRAM over the 256 MiB BAR window on non-ReBAR GPUs, retries all eligible memory types on VK_ERROR_OUT_OF_DEVICE_MEMORY, and clears failed handles on error.
  3. Immediate pool cleanup on descriptor allocation failure (vulkan_predicated_copy.h):
    • PredicatedBufferCopier::Batch::prepare() destroys the newly created VkDescriptorPool immediately if vkAllocateDescriptorSets fails instead of keeping an empty pool in pools until batch teardown.
  4. Eliminate per-command vkGetDeviceProcAddr string lookups (all four helpers):
    • Resolves and caches all required PFN_vk* function pointers once inside make_pipeline() rather than invoking vkGetDeviceProcAddr through the loader on every prepare(), capture(), emit(), and batch cleanup call.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (macOS host, CPU-only portable Vulkan-Headers mock harness; no game files used).
  • New CTest target vk_helper_proc_cache (runtime/test/vk_helper_proc_cache_test.cpp, Clang 20, C++20, -Wall -Wextra -Werror):
    • ASan + UBSan (default and with LEONIDA_INDIRECT_GROUP_BUDGET=1024): PASS
    • TSan (LEONIDA_INDIRECT_GROUP_BUDGET=1024): PASS
    • -O2 × 3 (LEONIDA_INDIRECT_GROUP_BUDGET=1024): PASS
  • Existing regression suites under ASan + UBSan:
    • runtime/test/indirect_dispatch_count_alloc_test.cpp: PASS
    • runtime/test/replay_quad_list_test.cpp: PASS
  • Mutation test against main's headers with LEONIDA_INDIRECT_GROUP_BUDGET=1024: fails on main (prepare(args, 0, VK_NULL_HANDLE, 16, ...) returned true), passes on this branch.
  • Hygiene: python3 -I scripts/check_no_game_data.py on all 6 touched files and python3 -I tests/test_text_write_line_endings.py passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5494407424 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1910). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T15:35:34Z. --- Item ID: `RT-VK-HELPER-PROC-CACHE-AND-GUARDS` (claimed in #1608). ### Summary Hardens and speeds up the four Vulkan replay compute helpers (`runtime/src/vulkan_indirect_dispatch_count.h`, `runtime/src/vulkan_predicate.h`, `runtime/src/vulkan_predicated_copy.h`, `runtime/src/quad_index_expand.h`): 1. **Fix uncounted `count_offset` GPU BDA fault + immediate failure cleanup (`vulkan_indirect_dispatch_count.h`)**: - Previously, when `LEONIDA_INDIRECT_GROUP_BUDGET` was active and `count == VK_NULL_HANDLE` with a non-zero `count_offset`, `Batch::prepare()` accepted the call and wrote `out.count = count_va + count_offset = count_offset != 0`; `emit()` then treated `job.count != 0` as `has_count = 1` and passed `count_offset` as a raw GPU device address. - `Batch::prepare()` now rejects `!count && count_offset != 0`, explicitly sets `out.count = count ? count_va + count_offset : 0`, prefers pure VRAM ahead of the 256 MiB BAR window on non-ReBAR GPUs (`!resizable_bar()`), and destroys partial `VkBuffer`/`VkDeviceMemory` allocations immediately on failure instead of retaining dead handles until submission completion. 2. **BAR-aware memory selection, heap-size filtering, and OOM fallback (`vulkan_predicate.h`)**: - Replaces `PredicateReducer::batch()`'s first-`DEVICE_LOCAL` loop with `memory_type_order()` (with `memoryHeapCount == 0` mock compatibility), prefers pure VRAM over the 256 MiB BAR window on non-ReBAR GPUs, retries all eligible memory types on `VK_ERROR_OUT_OF_DEVICE_MEMORY`, and clears failed handles on error. 3. **Immediate pool cleanup on descriptor allocation failure (`vulkan_predicated_copy.h`)**: - `PredicatedBufferCopier::Batch::prepare()` destroys the newly created `VkDescriptorPool` immediately if `vkAllocateDescriptorSets` fails instead of keeping an empty pool in `pools` until batch teardown. 4. **Eliminate per-command `vkGetDeviceProcAddr` string lookups (all four helpers)**: - Resolves and caches all required `PFN_vk*` function pointers once inside `make_pipeline()` rather than invoking `vkGetDeviceProcAddr` through the loader on every `prepare()`, `capture()`, `emit()`, and batch cleanup call. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (macOS host, CPU-only portable Vulkan-Headers mock harness; no game files used). - New CTest target `vk_helper_proc_cache` (`runtime/test/vk_helper_proc_cache_test.cpp`, Clang 20, C++20, `-Wall -Wextra -Werror`): - ASan + UBSan (default and with `LEONIDA_INDIRECT_GROUP_BUDGET=1024`): PASS - TSan (`LEONIDA_INDIRECT_GROUP_BUDGET=1024`): PASS - `-O2` × 3 (`LEONIDA_INDIRECT_GROUP_BUDGET=1024`): PASS - Existing regression suites under ASan + UBSan: - `runtime/test/indirect_dispatch_count_alloc_test.cpp`: PASS - `runtime/test/replay_quad_list_test.cpp`: PASS - Mutation test against `main`'s headers with `LEONIDA_INDIRECT_GROUP_BUDGET=1024`: fails on `main` (`prepare(args, 0, VK_NULL_HANDLE, 16, ...)` returned `true`), passes on this branch. - Hygiene: `python3 -I scripts/check_no_game_data.py` on all 6 touched files and `python3 -I tests/test_text_write_line_endings.py` passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 15:35:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T15:35:51Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6060618303 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1910#pullrequestreview-6060618303). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T15:35:51Z. **Review:** COMMENTED --- 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.
zmasi commented 2026-11-28 12:17:41 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:17:41Z.


#1910 | head 86519799ba (merged cleanly with main d48a957bb9) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vk_helper_proc_cache|indirect_dispatch_count_alloc|replay_quad_list|d3d12_indirect_dispatch_count|d3d12_indirect_group_budget|d3d12_predicated_copy)$" --output-on-failure (LEONIDA_VK_VALIDATION=1, plus LEONIDA_INDIRECT_GROUP_BUDGET=1024) | red on main: vk_helper_proc_cache fails (exit 8 at !batch->prepare(args, 0, VK_NULL_HANDLE, 16, 12, 4, out) with LEONIDA_INDIRECT_GROUP_BUDGET=1024) | green on PR+main: 6/6 CTests pass (vk_helper_proc_cache both default and LEONIDA_INDIRECT_GROUP_BUDGET=1024, indirect_dispatch_count_alloc, replay_quad_list, d3d12_indirect_dispatch_count, d3d12_indirect_group_budget, and d3d12_predicated_copy) on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations (coordination note: #2491 also touches !count && count_offset != 0 in vulkan_indirect_dispatch_count.h, choosing to ignore count_offset when !count per D3D12 ExecuteIndirect semantics rather than refusing prepare()) | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239127324 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1910#issuecomment-7239127324). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:17:41Z. --- #1910 | head 86519799babe8962efb5bed31a89915313b490a2 (merged cleanly with main d48a957bb9988afce424552a3c0c8d01ef12320f) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vk_helper_proc_cache|indirect_dispatch_count_alloc|replay_quad_list|d3d12_indirect_dispatch_count|d3d12_indirect_group_budget|d3d12_predicated_copy)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`, plus `LEONIDA_INDIRECT_GROUP_BUDGET=1024`) | red on main: `vk_helper_proc_cache` fails (exit 8 at `!batch->prepare(args, 0, VK_NULL_HANDLE, 16, 12, 4, out)` with `LEONIDA_INDIRECT_GROUP_BUDGET=1024`) | green on PR+main: 6/6 CTests pass (`vk_helper_proc_cache` both default and `LEONIDA_INDIRECT_GROUP_BUDGET=1024`, `indirect_dispatch_count_alloc`, `replay_quad_list`, `d3d12_indirect_dispatch_count`, `d3d12_indirect_group_budget`, and `d3d12_predicated_copy`) on RTX 3090 with `0` Khronos validation errors; `check_no_game_data.py` `0` violations (coordination note: `#2491` also touches `!count && count_offset != 0` in `vulkan_indirect_dispatch_count.h`, choosing to ignore `count_offset` when `!count` per D3D12 `ExecuteIndirect` semantics rather than refusing `prepare()`) | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
enlight3d commented 2026-11-29 12:03:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-29T12:03:31Z.


Run of this pull request on a second machine.

Heads: PR head 86519799b, main 8fb974878. Merged: git merge --no-ff of the PR into main, no conflicts.

Commands (RelWithDebInfo, Ninja, -DLEONIDA_REQUIRE_VULKAN=ON):

git checkout --detach origin/main && git merge --no-edit --no-ff pr-1910
cmake --build runtime/build --target vk_helper_proc_cache_test d3d12_predicated_copy_test d3d12_indirect_dispatch_count_test d3d12_quad_index_test predicated_copy_test replay_quad_list_test indirect_dispatch_count_alloc_test replay_query_predication_test replay_write_immediate_predication_test gta6rt -j 8
ctest -R "^(vk_helper_proc_cache|d3d12_predicated_copy|d3d12_indirect_dispatch_count|d3d12_quad_index|predicated_copy|replay_quad_list|indirect_dispatch_count_alloc|replay_query_predication|replay_write_immediate_predication)$" --output-on-failure -j1

Green: 9/9 pass. vk_helper_proc_cache is a mock-device test (no GPU). The other eight are existing tests that exercise the four touched headers (the GPU ones, d3d12_predicated_copy, d3d12_indirect_dispatch_count and d3d12_quad_index, ran on the RTX 3090).

Red (behavioural: the test compiles against the old headers): the four changed headers (quad_index_expand.h, vulkan_indirect_dispatch_count.h, vulkan_predicate.h, vulkan_predicated_copy.h) restored to main, test and CMake entry kept. vk_helper_proc_cache then fails at vk_helper_proc_cache_test.cpp:229, the immediate-cleanup check after a failed bind (create_buffer_calls == 1 && destroy_buffer_calls == 1 && free_memory_calls == 1). The test stops at its first failing check, so later cases were not enumerated.
One observation: the first case of the test (an uncounted call with a non-zero count_offset must be rejected) passes on main too. The test does not set LEONIDA_INDIRECT_GROUP_BUDGET, and main already rejects an uncounted call when no budget is set, so the budget-active path described in the PR (the one that passed count_offset as a device address) is not exercised by the test.

Link: gta6rt.exe of the merged build vs gta6rt.exe of main 8fb974878 (dumpbin /imports, DLL!symbol, sorted, 694 entries): no new imports vs main.

Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44, Vulkan SDK 1.4.363.1.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7255535561 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1910#issuecomment-7255535561). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-11-29T12:03:31Z. --- Run of this pull request on a second machine. Heads: PR head 86519799b, main 8fb974878. Merged: git merge --no-ff of the PR into main, no conflicts. Commands (RelWithDebInfo, Ninja, -DLEONIDA_REQUIRE_VULKAN=ON): ``` git checkout --detach origin/main && git merge --no-edit --no-ff pr-1910 cmake --build runtime/build --target vk_helper_proc_cache_test d3d12_predicated_copy_test d3d12_indirect_dispatch_count_test d3d12_quad_index_test predicated_copy_test replay_quad_list_test indirect_dispatch_count_alloc_test replay_query_predication_test replay_write_immediate_predication_test gta6rt -j 8 ctest -R "^(vk_helper_proc_cache|d3d12_predicated_copy|d3d12_indirect_dispatch_count|d3d12_quad_index|predicated_copy|replay_quad_list|indirect_dispatch_count_alloc|replay_query_predication|replay_write_immediate_predication)$" --output-on-failure -j1 ``` Green: 9/9 pass. vk_helper_proc_cache is a mock-device test (no GPU). The other eight are existing tests that exercise the four touched headers (the GPU ones, d3d12_predicated_copy, d3d12_indirect_dispatch_count and d3d12_quad_index, ran on the RTX 3090). Red (behavioural: the test compiles against the old headers): the four changed headers (quad_index_expand.h, vulkan_indirect_dispatch_count.h, vulkan_predicate.h, vulkan_predicated_copy.h) restored to main, test and CMake entry kept. vk_helper_proc_cache then fails at vk_helper_proc_cache_test.cpp:229, the immediate-cleanup check after a failed bind (create_buffer_calls == 1 && destroy_buffer_calls == 1 && free_memory_calls == 1). The test stops at its first failing check, so later cases were not enumerated. One observation: the first case of the test (an uncounted call with a non-zero count_offset must be rejected) passes on main too. The test does not set LEONIDA_INDIRECT_GROUP_BUDGET, and main already rejects an uncounted call when no budget is set, so the budget-active path described in the PR (the one that passed count_offset as a device address) is not exercised by the test. Link: gta6rt.exe of the merged build vs gta6rt.exe of main 8fb974878 (dumpbin /imports, DLL!symbol, sorted, 694 entries): no new imports vs main. Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44, Vulkan SDK 1.4.363.1.
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1910
No description provided.