[PR #1832] [MERGED] runtime: share indirect clamp buffers across queue families and fix safety bugs #1845

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1832
Author: @IlyassRS3
Created: 11/25/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: fix/rt-indirect-clamp-sharing-and-safety


📝 Commits (1)

  • 9428f57 runtime: share indirect clamp buffers across queue families and fix safety bugs

📊 Changes

6 files changed (+478 additions, -21 deletions)

View changed files

📝 runtime/CMakeLists.txt (+7 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+2 -1)
📝 runtime/src/vulkan_backend.h (+3 -2)
📝 runtime/src/vulkan_indirect_clamp.h (+46 -16)
📝 runtime/src/vulkan_scratch.h (+5 -2)
➕ runtime/test/indirect_clamp_safety_test.cpp (+415 -0)

📄 Description

Item ID: RT-INDIRECT-CLAMP-SHARING-AND-SAFETY
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162686562

Summary

Fixes queue-family sharing and four safety/correctness issues in IndirectArgumentClamp (runtime/src/vulkan_indirect_clamp.h) and its ScratchBuffers overload (runtime/src/vulkan_scratch.h) when indirect clamping runs across graphics and async-compute queues during world rendering:

  1. Multi-queue-family sharing (VK_SHARING_MODE_CONCURRENT) on both scratch chunks and HostArea counter buffers
    • enable_indirect_clamp() previously did not receive state_.families (unlike enable_copy_scratch() 10 lines earlier in runtime/shims/d3d12_vulkan_device.cpp).
    • Added an optional std::vector<std::uint32_t> families = {} parameter to the explicit-usage ScratchBuffers constructor (runtime/src/vulkan_scratch.h), IndirectArgumentClamp, and VulkanContext::enable_indirect_clamp(), and forwarded state_.families from runtime/shims/d3d12_vulkan_device.cpp.
    • IndirectArgumentClamp::make_area() now calls share_across_queue_families(info, families_) on the host-visible counter buffer as well, because direct and async-compute queues copy counter slots into the same HostArea::buffer.
  2. Prevent VkDeviceMemory leak on vkBindBufferMemory retry + strong exception safety in make_area() / Batch::prepare()
    • make_area() looped over candidate host-visible memory types and allocated a new VkDeviceMemory each iteration without freeing the failed allocation when vkBindBufferMemory failed, leaking device memory. Now frees area.memory before retrying, and cleans up area.buffer/area.memory if areas_.push_back or free_slots_.reserve throws.
    • Batch::prepare() now releases the claimed slot if slots_.push_back(slot) throws.
  3. Zero released HostArea counter slots to avoid double-counting on aborted batches
    • IndirectArgumentClamp::release() read the 4 counter words from area.host and pushed the slot onto LIFO free_slots_ without zeroing them. If a later batch reused that slot and was destroyed without submitting to the GPU (e.g., failed replay or failed vkQueueSubmit), ~Batch() called release(slots_, true) and re-added the prior submission's counters.
    • Now clears the 4 counter words (std::memset(words, 0, 4 * sizeof(std::uint32_t))) right after reading them.
  4. Reject non-4-byte-aligned argument_offset and count_offset
    • indirect_clamp.comp reads uint words via BDA (arguments_va + argument_offset, count_va + count_offset). prepare() checked stride % 4, but not argument_offset % 4 or count_offset % 4. Now rejects misaligned offsets cleanly via fallback(...).
  5. Guard areas_[slot.area].buffer read under mutex_
    • Moved out.stats_buffer = ... areas_[slot.area].buffer into the existing std::lock_guard lock(mutex_) block 2 lines below in prepare(), removing the unlocked read of areas_ without adding any extra lock acquisition.

Tests

Added pure-CPU mock-Vulkan test runtime/test/indirect_clamp_safety_test.cpp (registered as CTest indirect_clamp_safety under if(TARGET Vulkan::Headers) in runtime/CMakeLists.txt).

Verified locally on macOS arm64 (Apple Clang) using the pinned Khronos Vulkan-Headers (6802bb4733b63ed5efd3adb308a6c885ef180ea1):

  • indirect_clamp_safety_test: PASS under ASan+UBSan, PASS under TSan (0 races), PASS under -O2 (3 consecutive runs), -Wall -Wextra -Werror -Wno-missing-field-initializers.
  • Existing vulkan_scratch_test: PASS under ASan+UBSan.
  • Compile-checked replay_pipeline_compiles_test.cpp and depth24_convert_test.cpp against the updated headers.
  • Mutation check against main's vulkan_indirect_clamp.h + vulkan_scratch.h: fails 11 assertions across sharing mode, vkBindBufferMemory retry leak, stale counter double-count, and misaligned offset acceptance.
  • Hygiene: python3 -I scripts/check_no_game_data.py ... and python3 -I tests/test_text_write_line_endings.py pass.

Disclosure

  • AI-assisted: yes
  • GPUs tested: none (macOS arm64 host; CPU mock-Vulkan tests verified under ASan/UBSan/TSan; Windows/MSVC + real GPU runtime validation relies on CI / reviewers)

🔄 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/1832 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix/rt-indirect-clamp-sharing-and-safety` --- ### 📝 Commits (1) - [`9428f57`](https://github.com/ubiquitarianism/leonida/commit/9428f57da22a4693885a5671c61789ddd8bd664d) runtime: share indirect clamp buffers across queue families and fix safety bugs ### 📊 Changes **6 files changed** (+478 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+7 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+2 -1) 📝 `runtime/src/vulkan_backend.h` (+3 -2) 📝 `runtime/src/vulkan_indirect_clamp.h` (+46 -16) 📝 `runtime/src/vulkan_scratch.h` (+5 -2) ➕ `runtime/test/indirect_clamp_safety_test.cpp` (+415 -0) </details> ### 📄 Description Item ID: `RT-INDIRECT-CLAMP-SHARING-AND-SAFETY` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162686562 ## Summary Fixes queue-family sharing and four safety/correctness issues in `IndirectArgumentClamp` (`runtime/src/vulkan_indirect_clamp.h`) and its `ScratchBuffers` overload (`runtime/src/vulkan_scratch.h`) when indirect clamping runs across graphics and async-compute queues during world rendering: 1. **Multi-queue-family sharing (`VK_SHARING_MODE_CONCURRENT`) on both scratch chunks and `HostArea` counter buffers** - `enable_indirect_clamp()` previously did not receive `state_.families` (unlike `enable_copy_scratch()` 10 lines earlier in `runtime/shims/d3d12_vulkan_device.cpp`). - Added an optional `std::vector<std::uint32_t> families = {}` parameter to the explicit-usage `ScratchBuffers` constructor (`runtime/src/vulkan_scratch.h`), `IndirectArgumentClamp`, and `VulkanContext::enable_indirect_clamp()`, and forwarded `state_.families` from `runtime/shims/d3d12_vulkan_device.cpp`. - `IndirectArgumentClamp::make_area()` now calls `share_across_queue_families(info, families_)` on the host-visible counter buffer as well, because direct and async-compute queues copy counter slots into the same `HostArea::buffer`. 2. **Prevent `VkDeviceMemory` leak on `vkBindBufferMemory` retry + strong exception safety in `make_area()` / `Batch::prepare()`** - `make_area()` looped over candidate host-visible memory types and allocated a new `VkDeviceMemory` each iteration without freeing the failed allocation when `vkBindBufferMemory` failed, leaking device memory. Now frees `area.memory` before retrying, and cleans up `area.buffer`/`area.memory` if `areas_.push_back` or `free_slots_.reserve` throws. - `Batch::prepare()` now releases the claimed slot if `slots_.push_back(slot)` throws. 3. **Zero released `HostArea` counter slots to avoid double-counting on aborted batches** - `IndirectArgumentClamp::release()` read the 4 counter words from `area.host` and pushed the slot onto LIFO `free_slots_` without zeroing them. If a later batch reused that slot and was destroyed without submitting to the GPU (e.g., failed replay or failed `vkQueueSubmit`), `~Batch()` called `release(slots_, true)` and re-added the prior submission's counters. - Now clears the 4 counter words (`std::memset(words, 0, 4 * sizeof(std::uint32_t))`) right after reading them. 4. **Reject non-4-byte-aligned `argument_offset` and `count_offset`** - `indirect_clamp.comp` reads `uint` words via BDA (`arguments_va + argument_offset`, `count_va + count_offset`). `prepare()` checked `stride % 4`, but not `argument_offset % 4` or `count_offset % 4`. Now rejects misaligned offsets cleanly via `fallback(...)`. 5. **Guard `areas_[slot.area].buffer` read under `mutex_`** - Moved `out.stats_buffer = ... areas_[slot.area].buffer` into the existing `std::lock_guard lock(mutex_)` block 2 lines below in `prepare()`, removing the unlocked read of `areas_` without adding any extra lock acquisition. ## Tests Added pure-CPU mock-Vulkan test `runtime/test/indirect_clamp_safety_test.cpp` (registered as CTest `indirect_clamp_safety` under `if(TARGET Vulkan::Headers)` in `runtime/CMakeLists.txt`). Verified locally on macOS arm64 (Apple Clang) using the pinned Khronos `Vulkan-Headers` (`6802bb4733b63ed5efd3adb308a6c885ef180ea1`): - `indirect_clamp_safety_test`: PASS under ASan+UBSan, PASS under TSan (0 races), PASS under `-O2` (3 consecutive runs), `-Wall -Wextra -Werror -Wno-missing-field-initializers`. - Existing `vulkan_scratch_test`: PASS under ASan+UBSan. - Compile-checked `replay_pipeline_compiles_test.cpp` and `depth24_convert_test.cpp` against the updated headers. - Mutation check against `main`'s `vulkan_indirect_clamp.h` + `vulkan_scratch.h`: fails 11 assertions across sharing mode, `vkBindBufferMemory` retry leak, stale counter double-count, and misaligned offset acceptance. - Hygiene: `python3 -I scripts/check_no_game_data.py ...` and `python3 -I tests/test_text_write_line_endings.py` pass. ## Disclosure - **AI-assisted**: yes - **GPUs tested**: none (macOS arm64 host; CPU mock-Vulkan tests verified under ASan/UBSan/TSan; Windows/MSVC + real GPU runtime validation relies on CI / reviewers) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:19:09 +01:00
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#1845
No description provided.