runtime: BAR-aware memory fallback and heap-size filtering in BindlessSet (RT-BINDLESS-MEMORY-FALLBACK-AND-BAR) #1879

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1879 into main
IlyassRS3 commented 2026-11-25 14:00:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T14:00:49Z.


Item: RT-BINDLESS-MEMORY-FALLBACK-AND-BAR
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164522386

Summary

Fixes memory-type selection and OOM fallback in BindlessSet (runtime/src/vulkan_bindless.h) using vulkan_memory.h's memory_type_order() and resizable_bar() policy:

  1. Fixes non-ReBAR local == fallback OOM trap in BindlessSet::create():
    • Previously, memory_type(bits, DEVICE_LOCAL) and memory_type(bits, HOST_VISIBLE | HOST_COHERENT) each returned only the first matching memory type with no heap-size check and no retry loop.
    • When a small (<= 256 MiB) DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT BAR type appears at index 0 before main DEVICE_LOCAL VRAM and system RAM on a non-ReBAR GPU, local == fallback == 0, so if (fallback != local) evaluated false on BAR OOM — skipping both main VRAM and system RAM and degrading or failing bindless descriptor table flushes (up to 64 MiB as the table grows).
    • Now table (device_local == true) tries non-small-BAR DEVICE_LOCAL types first, then remaining DEVICE_LOCAL types, then falls back to HOST_VISIBLE | HOST_COHERENT types (tracking tried_mask to avoid duplicate attempts and incrementing table_host_fallbacks_ when a DEVICE_LOCAL type existed and host fallback succeeded).
  2. Keeps 64 MiB staging buffers off the 256 MiB BAR window on non-ReBAR GPUs + adds OOM retry:
    • staging (device_local == false) prefers system RAM over small BAR on non-ReBAR GPUs (while preserving ReBAR staging when resizable_bar() is true) and retries secondary HOST_VISIBLE | HOST_COHERENT types on OOM.
  3. Heap-size enforcement (VUID-vkAllocateMemory-pAllocateInfo-01713):
    • Filters out memory types whose heap is smaller than requirements.size when memoryHeapCount > 0 (preserving compatibility with synthetic zero-heap mocks where memoryHeapCount == 0).
  4. create_fallbacks() multi-type OOM retry & failed-handle cleanup:
    • Prefers non-small-BAR DEVICE_LOCAL types, retries all eligible fitting types on OOM, records failure via note_failure("fallback image", result), and clears failed image.image / image.memory / fallback_sampler_ handles so ~BindlessSet() never frees an invalid handle.

No overlap with open PRs #425 or #1092.

Verification

  • AI-assisted: yes
  • GPUs tested: none (CPU-only macOS arm64 host with Khronos Vulkan-Headers)
  • New unit test runtime/test/vk_bindless_memory_fallback_test.cpp (CTest vk_bindless_memory_fallback):
    • clang++ -std=c++20 -Wall -Wextra -Wno-missing-field-initializers -Werror -O1 -g -fsanitize=address,undefined: passed (0 leaks, 0 invalid frees, 0 undersized alloc attempts)
    • clang++ -std=c++20 -O1 -g -fsanitize=thread: passed
    • clang++ -std=c++20 -O2 (3 runs): passed
  • Existing bindless_sizing_test: passed; vk_bindless_grow_test.cpp and vk_bindless_test.cpp: syntax-checked cleanly
  • Mutation check against main: compiling vk_bindless_memory_fallback_test.cpp against main's runtime/src/vulkan_bindless.h fails immediately (FAIL line 346: !mock.table_types_bound.empty() && mock.table_types_bound.back() == 1)
  • python3 -I scripts/check_no_game_data.py on all changed files: passed
  • python3 -I tests/test_text_write_line_endings.py: passed (3 tests)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5492807214 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1879). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T14:00:49Z. --- Item: `RT-BINDLESS-MEMORY-FALLBACK-AND-BAR` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164522386 ### Summary Fixes memory-type selection and OOM fallback in `BindlessSet` (`runtime/src/vulkan_bindless.h`) using `vulkan_memory.h`'s `memory_type_order()` and `resizable_bar()` policy: 1. **Fixes non-ReBAR `local == fallback` OOM trap in `BindlessSet::create()`**: - Previously, `memory_type(bits, DEVICE_LOCAL)` and `memory_type(bits, HOST_VISIBLE | HOST_COHERENT)` each returned only the first matching memory type with no heap-size check and no retry loop. - When a small (<= 256 MiB) `DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT` BAR type appears at index 0 before main `DEVICE_LOCAL` VRAM and system RAM on a non-ReBAR GPU, `local == fallback == 0`, so `if (fallback != local)` evaluated false on BAR OOM — skipping both main VRAM and system RAM and degrading or failing bindless descriptor table flushes (up to 64 MiB as the table grows). - Now `table` (`device_local == true`) tries non-small-BAR `DEVICE_LOCAL` types first, then remaining `DEVICE_LOCAL` types, then falls back to `HOST_VISIBLE | HOST_COHERENT` types (tracking `tried_mask` to avoid duplicate attempts and incrementing `table_host_fallbacks_` when a `DEVICE_LOCAL` type existed and host fallback succeeded). 2. **Keeps 64 MiB staging buffers off the 256 MiB BAR window on non-ReBAR GPUs + adds OOM retry**: - `staging` (`device_local == false`) prefers system RAM over small BAR on non-ReBAR GPUs (while preserving ReBAR staging when `resizable_bar()` is true) and retries secondary `HOST_VISIBLE | HOST_COHERENT` types on OOM. 3. **Heap-size enforcement (`VUID-vkAllocateMemory-pAllocateInfo-01713`)**: - Filters out memory types whose heap is smaller than `requirements.size` when `memoryHeapCount > 0` (preserving compatibility with synthetic zero-heap mocks where `memoryHeapCount == 0`). 4. **`create_fallbacks()` multi-type OOM retry & failed-handle cleanup**: - Prefers non-small-BAR `DEVICE_LOCAL` types, retries all eligible fitting types on OOM, records failure via `note_failure("fallback image", result)`, and clears failed `image.image` / `image.memory` / `fallback_sampler_` handles so `~BindlessSet()` never frees an invalid handle. No overlap with open PRs `#425` or `#1092`. ### Verification - **AI-assisted**: yes - **GPUs tested**: none (CPU-only macOS arm64 host with Khronos Vulkan-Headers) - New unit test `runtime/test/vk_bindless_memory_fallback_test.cpp` (CTest `vk_bindless_memory_fallback`): - `clang++ -std=c++20 -Wall -Wextra -Wno-missing-field-initializers -Werror -O1 -g -fsanitize=address,undefined`: passed (0 leaks, 0 invalid frees, 0 undersized alloc attempts) - `clang++ -std=c++20 -O1 -g -fsanitize=thread`: passed - `clang++ -std=c++20 -O2` (3 runs): passed - Existing `bindless_sizing_test`: passed; `vk_bindless_grow_test.cpp` and `vk_bindless_test.cpp`: syntax-checked cleanly - **Mutation check against `main`**: compiling `vk_bindless_memory_fallback_test.cpp` against `main`'s `runtime/src/vulkan_bindless.h` fails immediately (`FAIL line 346: !mock.table_types_bound.empty() && mock.table_types_bound.back() == 1`) - `python3 -I scripts/check_no_game_data.py` on all changed files: passed - `python3 -I tests/test_text_write_line_endings.py`: passed (3 tests)
copilot-pull-request-reviewer[bot] commented 2026-11-25 14:01:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T14:01:05Z.

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:6059001190 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1879#pullrequestreview-6059001190). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T14:01:05Z. **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.
Timon360SB commented 2026-11-25 15:07:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Timon360SB on 2026-11-25T15:07:12Z.


Independent verification — Turing and Blackwell

  • Base: 77baf906275d7b695664adbf0b33f3c2b306a1dc
  • Head: 5a6baea948d46eb4d51437f1f7e55ad03e216272

Environment

GPU Windows 11 build ReBAR
RTX 5070 26200 Enabled
RTX 2060 SUPER 28000 Unavailable

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

Three repetitions per GPU, revision and validation mode. Core, GPU-assisted and synchronization validation requested in validated runs.

Results

Test Base Head
bindless_sizing 12/12 12/12
vk_bindless_grow 12/12 12/12
vk_bindless_ps_tex 12/12 12/12
vk_bindless_ps_imgatomic 12/12 12/12
d3d12_bindless_churn 12/12 12/12
vk_bindless_memory_fallback N/A 2/2
Total 60/60 62/62

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

Findings

  • The new mock-Vulkan memory fallback test passes under Windows/MSVC on both systems.
  • Bindless growth: 400 frames, 3,200 keys added, table growth from 64 to 512 slots, zero allocations or descriptor pools in the second half.
  • Texture readback: 24 draws per execution, zero bad pixels.
  • D3D12 bindless churn: 96 frames, all sampled texels matched.
  • No Vulkan validation errors found in the captured logs.

No regressions observed on either GPU, including the no-ReBAR Turing configuration. Allocation failure handling was covered by the new mock test; physical BAR exhaustion was not forced.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7166332752 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1879#issuecomment-7166332752). Originally posted by [Timon360SB](https://github.com/Timon360SB) on 2026-11-25T15:07:12Z. --- ## Independent verification — Turing and Blackwell - **Base:** `77baf906275d7b695664adbf0b33f3c2b306a1dc` - **Head:** `5a6baea948d46eb4d51437f1f7e55ad03e216272` ### Environment | GPU | Windows 11 build | ReBAR | |---|---|---| | RTX 5070 | 26200 | Enabled | | RTX 2060 SUPER | 28000 | Unavailable | NVIDIA 621.14, Vulkan SDK 1.4.363.0, MSVC 19.44.35230, Ninja RelWithDebInfo. Three repetitions per GPU, revision and validation mode. Core, GPU-assisted and synchronization validation requested in validated runs. ### Results | Test | Base | Head | |---|---:|---:| | `bindless_sizing` | 12/12 | 12/12 | | `vk_bindless_grow` | 12/12 | 12/12 | | `vk_bindless_ps_tex` | 12/12 | 12/12 | | `vk_bindless_ps_imgatomic` | 12/12 | 12/12 | | `d3d12_bindless_churn` | 12/12 | 12/12 | | `vk_bindless_memory_fallback` | N/A | 2/2 | | **Total** | **60/60** | **62/62** | 122 executions: 122 passed, 0 failed, 0 skipped. ### Findings - The new mock-Vulkan memory fallback test passes under Windows/MSVC on both systems. - Bindless growth: 400 frames, 3,200 keys added, table growth from 64 to 512 slots, zero allocations or descriptor pools in the second half. - Texture readback: 24 draws per execution, zero bad pixels. - D3D12 bindless churn: 96 frames, all sampled texels matched. - No Vulkan validation errors found in the captured logs. No regressions observed on either GPU, including the no-ReBAR Turing configuration. Allocation failure handling was covered by the new mock test; physical BAR exhaustion was not forced. Synthetic tests only. No game files used. AI-assisted: yes.
MoonEyes commented 2026-11-28 02:51:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T02:51:10Z.


Hi @IlyassRS3, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/src/vulkan_bindless.h), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232635129 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1879#issuecomment-7232635129). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T02:51:10Z. --- Hi @IlyassRS3, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/src/vulkan_bindless.h`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
enlight3d commented 2026-11-29 12:03:53 +00:00 (Migrated from github.com)

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


Run of this pull request on a second machine (head only: this branch does not merge cleanly into current main).

Heads: PR head 5a6baea94 (1 commit, built on main 77baf9062), current main 8fb974878.
Merge into main: conflict in runtime/src/vulkan_bindless.h. I therefore built and tested the PR head itself (detached), not a merge.

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

git checkout --detach pr-1879
cmake --build runtime/build --target vk_bindless_memory_fallback_test bindless_sizing_test vk_bindless_grow_test vk_null_descriptor_test gta6rt -j 8
ctest -R "^(vk_bindless_memory_fallback|bindless_sizing|vk_bindless_grow|vk_null_descriptor)$" --output-on-failure -j1

Green: 4/4 pass. vk_bindless_memory_fallback is CPU-only (mock device); bindless_sizing, vk_bindless_grow and vk_null_descriptor are the existing tests that include vulkan_bindless.h (the last two use the RTX 3090). The vk_bindless_ps_* tests were not run: they need generated shader inputs this machine does not have.

Red (behavioural: the test compiles and runs): same head with runtime/src/vulkan_bindless.h restored to the PR's parent, test and CMake entry kept. vk_bindless_memory_fallback then fails at vk_bindless_memory_fallback_test.cpp:346 (the non-ReBAR case: the initial table must not be bound to type 0, the 256 MiB BAR window, but to the main VRAM type). The test stops at its first failing check, so later cases were not enumerated.

Link: gta6rt.exe of the PR head vs gta6rt.exe of main 8fb974878 (dumpbin /imports, DLL!symbol, sorted): the only import present in the head and not in main is api-ms-win-crt-stdio-l1-1-0.dll!__stdio_common_vswscanf. It comes from the older base (main has since changed an swscanf call), not from this diff, which adds no scanf call. No other new imports.

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

The branch is behind main and conflicts in the file above; could you merge main into it? Thank you.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7255540111 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1879#issuecomment-7255540111). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-11-29T12:03:53Z. --- Run of this pull request on a second machine (head only: this branch does not merge cleanly into current main). Heads: PR head 5a6baea94 (1 commit, built on main 77baf9062), current main 8fb974878. Merge into main: conflict in runtime/src/vulkan_bindless.h. I therefore built and tested the PR head itself (detached), not a merge. Commands (RelWithDebInfo, Ninja, -DLEONIDA_REQUIRE_VULKAN=ON): ``` git checkout --detach pr-1879 cmake --build runtime/build --target vk_bindless_memory_fallback_test bindless_sizing_test vk_bindless_grow_test vk_null_descriptor_test gta6rt -j 8 ctest -R "^(vk_bindless_memory_fallback|bindless_sizing|vk_bindless_grow|vk_null_descriptor)$" --output-on-failure -j1 ``` Green: 4/4 pass. vk_bindless_memory_fallback is CPU-only (mock device); bindless_sizing, vk_bindless_grow and vk_null_descriptor are the existing tests that include vulkan_bindless.h (the last two use the RTX 3090). The vk_bindless_ps_* tests were not run: they need generated shader inputs this machine does not have. Red (behavioural: the test compiles and runs): same head with runtime/src/vulkan_bindless.h restored to the PR's parent, test and CMake entry kept. vk_bindless_memory_fallback then fails at vk_bindless_memory_fallback_test.cpp:346 (the non-ReBAR case: the initial table must not be bound to type 0, the 256 MiB BAR window, but to the main VRAM type). The test stops at its first failing check, so later cases were not enumerated. Link: gta6rt.exe of the PR head vs gta6rt.exe of main 8fb974878 (dumpbin /imports, DLL!symbol, sorted): the only import present in the head and not in main is api-ms-win-crt-stdio-l1-1-0.dll!__stdio_common_vswscanf. It comes from the older base (main has since changed an swscanf call), not from this diff, which adds no scanf call. No other new imports. Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44, Vulkan SDK 1.4.363.1. The branch is behind main and conflicts in the file above; could you merge main into it? Thank you.
aslan2142 commented 2026-12-01 16:38:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:38:28Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317945047 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1879#issuecomment-7317945047). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:38:28Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

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!1879
No description provided.