runtime: cache ScratchBuffers device addresses and use best-fit largest-4 spare reuse (RT-VK-SCRATCH-BEST-FIT-AND-CACHE) #1898

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1898 into main
IlyassRS3 commented 2026-11-25 15:07:27 +00:00 (Migrated from github.com)

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


Item ID: RT-VK-SCRATCH-BEST-FIT-AND-CACHE (claimed in #1608).

Summary

Optimizes leonida::ScratchBuffers (runtime/src/vulkan_scratch.h) for continuous per-frame indirect clamping, quad index expansion, and staged transfer workloads:

  1. Cache VkDeviceAddress once per chunk (make_chunk / claim):
    • Chunk now caches VkDeviceAddress address once when the chunk is created (when addressable_ is true), instead of calling vkGetBufferDeviceAddress through the Vulkan driver on every single get(..., address) suballocation.
    • Non-addressable pools still never invoke vkGetBufferDeviceAddress.
  2. Best-fit spare selection (claim):
    • Picks the smallest spare chunk with size >= bytes instead of first-fit, so small suballocations do not consume a 16+ MiB spare chunk and trigger fresh large VkDeviceMemory allocations later in the same frame.
  3. Retain the 4 largest reusable chunks (collect):
    • When spare_ is already at the 4-chunk cap, a completed chunk larger than the smallest spare replaces and destroys that smallest spare instead of being destroyed itself, preventing repeated large buffer/VRAM allocation churn across frames while keeping the cap strictly at 4.
  4. Clean failure state (make_chunk):
    • Resets out = {} on entry and when vkCreateBuffer fails so a non-conforming driver/mock writing a handle on error cannot leave stale state.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (macOS host, CPU-only portable Vulkan-Headers mock harness; no game files used).
  • runtime/test/vulkan_scratch_test.cpp (extended with cached-address, best-fit, largest-4 eviction, and failing vkCreateBuffer tests; Clang 20, C++20, -Wall -Wextra -Werror):
    • ASan + UBSan: PASS
    • TSan: PASS
    • -O2 × 3: PASS
  • Downstream header syntax check (vulkan_indirect_clamp.h + vulkan_scratch.h): PASS.
  • Mutation test against main's vulkan_scratch.h: fails on main (vk.get_device_address_calls == 1), passes on this branch.
  • Hygiene: python3 -I scripts/check_no_game_data.py and python3 -I tests/test_text_write_line_endings.py passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5493938127 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1898). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T15:07:27Z. --- Item ID: `RT-VK-SCRATCH-BEST-FIT-AND-CACHE` (claimed in #1608). ### Summary Optimizes `leonida::ScratchBuffers` (`runtime/src/vulkan_scratch.h`) for continuous per-frame indirect clamping, quad index expansion, and staged transfer workloads: 1. **Cache `VkDeviceAddress` once per chunk (`make_chunk` / `claim`)**: - `Chunk` now caches `VkDeviceAddress address` once when the chunk is created (when `addressable_` is true), instead of calling `vkGetBufferDeviceAddress` through the Vulkan driver on every single `get(..., address)` suballocation. - Non-addressable pools still never invoke `vkGetBufferDeviceAddress`. 2. **Best-fit spare selection (`claim`)**: - Picks the smallest spare chunk with `size >= bytes` instead of first-fit, so small suballocations do not consume a 16+ MiB spare chunk and trigger fresh large `VkDeviceMemory` allocations later in the same frame. 3. **Retain the 4 largest reusable chunks (`collect`)**: - When `spare_` is already at the 4-chunk cap, a completed chunk larger than the smallest spare replaces and destroys that smallest spare instead of being destroyed itself, preventing repeated large buffer/VRAM allocation churn across frames while keeping the cap strictly at 4. 4. **Clean failure state (`make_chunk`)**: - Resets `out = {}` on entry and when `vkCreateBuffer` fails so a non-conforming driver/mock writing a handle on error cannot leave stale state. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (macOS host, CPU-only portable Vulkan-Headers mock harness; no game files used). - `runtime/test/vulkan_scratch_test.cpp` (extended with cached-address, best-fit, largest-4 eviction, and failing `vkCreateBuffer` tests; Clang 20, C++20, `-Wall -Wextra -Werror`): - ASan + UBSan: PASS - TSan: PASS - `-O2` × 3: PASS - Downstream header syntax check (`vulkan_indirect_clamp.h` + `vulkan_scratch.h`): PASS. - Mutation test against `main`'s `vulkan_scratch.h`: fails on `main` (`vk.get_device_address_calls == 1`), passes on this branch. - Hygiene: `python3 -I scripts/check_no_game_data.py` and `python3 -I tests/test_text_write_line_endings.py` passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 15:07:39 +00:00 (Migrated from github.com)

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

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:6060162690 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1898#pullrequestreview-6060162690). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T15:07:39Z. **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 11:58:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T11:58:05Z.


#1898 | head a5fa6c090c (merged cleanly with main 4150b1a1cd) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vulkan_scratch|d3d12_predicated_copy)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: vulkan_scratch fails (exit 8 at line 391: vk.get_device_address_calls == 1) | green on PR+main: 2/2 CTests pass (vulkan_scratch + d3d12_predicated_copy 180/180 Vulkan + WARP + byte-oracle cases) on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238900951 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1898#issuecomment-7238900951). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T11:58:05Z. --- #1898 | head a5fa6c090c3c4225a78d66488a83d30fcec065e8 (merged cleanly with main 4150b1a1cd9de753749e0417ff71a450b6ce92cc) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vulkan_scratch|d3d12_predicated_copy)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: `vulkan_scratch` fails (exit 8 at `line 391: vk.get_device_address_calls == 1`) | green on PR+main: 2/2 CTests pass (`vulkan_scratch` + `d3d12_predicated_copy` 180/180 Vulkan + WARP + byte-oracle cases) on RTX 3090 with `0` Khronos validation errors; `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
fiestorra commented 2026-11-30 08:10:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-11-30T08:10:04Z.


Independent verification on this machine (Windows 11, MSVC 19.44.35226): this pull request does not compile, and the error is in the test file it changes.

1. It applies. merge-tree clean; the commit cherry-picks onto main bbd45f5cc without a conflict.

2. Building its test target:

cmake --build runtime/build-ninja --target vulkan_scratch_test
vulkan_scratch_test.cpp(414): error C2062: type 'char' unexpected
vulkan_scratch_test.cpp(418): error C2062: type 'char' unexpected

C2062 with type 'char' unexpected is what MSVC says when a character literal or escape swallows the closing quote - a stray ' or a malformed \ sequence in the lines around 414 and 418. It is a compile failure, not a test failure, so no test in this pull request can run until it is fixed.

What I did not do: read those lines and guess the intended text. The fix is the author's - one character in a literal - and a stranger editing it would be guessing at which one.

How it was found: a sweep of all 115 open pull requests from 11-25 that touch a C++ test, applying each to main, building the test target and running it through ctest -R. 39 built and passed; this is one of the two that do not build.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7272563947 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1898#issuecomment-7272563947). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-11-30T08:10:04Z. --- Independent verification on this machine (Windows 11, MSVC 19.44.35226): **this pull request does not compile**, and the error is in the test file it changes. **1. It applies.** `merge-tree` clean; the commit cherry-picks onto `main` `bbd45f5cc` without a conflict. **2. Building its test target:** ``` cmake --build runtime/build-ninja --target vulkan_scratch_test vulkan_scratch_test.cpp(414): error C2062: type 'char' unexpected vulkan_scratch_test.cpp(418): error C2062: type 'char' unexpected ``` **`C2062` with `type 'char' unexpected`** is what MSVC says when a character literal or escape swallows the closing quote - a stray `'` or a malformed `\` sequence in the lines around 414 and 418. It is a **compile failure, not a test failure**, so no test in this pull request can run until it is fixed. **What I did not do:** read those lines and guess the intended text. The fix is the author's - one character in a literal - and a stranger editing it would be guessing at which one. **How it was found:** a sweep of all 115 open pull requests from 11-25 that touch a C++ test, applying each to `main`, building the test target and running it through `ctest -R`. **39 built and passed; this is one of the two that do not build.** Written with an AI assistant.
aslan2142 commented 2026-12-01 16:38:45 +00:00 (Migrated from github.com)

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


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:7317954463 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1898#issuecomment-7317954463). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:38:45Z. --- 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!1898
No description provided.