runtime: guard XdxrBuilder empty dispatches, layout failure reset, and pipeline cleanup (RT-XDXR-BUILD-LAYOUT-EMPTY-AND-DISPATCH-GUARDS) #2002

Open
github-bot wants to merge 1 commit from github-bot/gh-2002 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 17:55 UTC · original
IlyassRS3:fix/rt-xdxr-build-layout-empty-and-dispatch-guards → main · +169 −34 · 2 files · 1 commits · open

Claim: RT-XDXR-BUILD-LAYOUT-EMPTY-AND-DISPATCH-GUARDS (claim comment #7170142282 on #1608).

Problem

In runtime/src/xdxr_build.h:

  1. Half-populated layout on overflow: XdxrLayout::compute() populated tlas, box16, node_size, elements, element_size, and nodes before checking the 32-bit offset / 29-bit unit limit and returning false, leaving a failed layout partially initialized.
  2. 32-bit wrap in tiles() and kept_tiles(): (sort_size + radix_tile - 1) / radix_tile and (inner() + radix_tile - 1) / radix_tile wrap uint32_t near UINT32_MAX (returning 0 instead of 2^24).
  3. Zero-workgroup dispatch + wasted barrier on empty BLAS/TLAS: finish() unconditionally dispatched mode_morton with count = l.sort_size, emitting vkCmdDispatch(cb, 0, 1, 1) and a compute-to-compute vkCmdPipelineBarrier whenever sort_size == 0.
  4. Overflow-prone 64 * tiles round-trip: finish() multiplied tile group counts by 64 (64 * l.tiles(), 64 * l.kept_tiles()) before dispatch() divided back by 64, which can overflow uint32_t for large tile counts.
  5. Null VkCommandBuffer and partial make_pipeline() cleanup: record_blas / record_tlas / record_copy / record_postbuild did not guard !cb, and make_pipeline() left layout_ alive on vkCreateShaderModule or vkCreateComputePipelines failure until ~XdxrBuilder().

Fix

  • Reset *this = XdxrLayout{}; before returning false on overflow in XdxrLayout::compute().
  • Make tiles() and kept_tiles() overflow-free (n ? 1u + (n - 1u) / radix_tile : 0u).
  • Add dispatch_groups() (skipping groups == 0), guard !threads in dispatch(), skip mode_morton when l.sort_size == 0, and dispatch exact tile/workgroup counts directly in finish().
  • Guard !cb in record_blas, record_tlas, record_copy, and record_postbuild.
  • Build into local VkPipelineLayout / VkPipeline handles in make_pipeline(), destroying layout immediately on shader-module or pipeline failure so a failed XdxrBuilder keeps layout_ and pipeline_ null.
  • Expand runtime/test/xdxr_builder_validation_test.cpp (0 CMakeLists.txt edits) to cover all 5 fixes.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran runtime/test/xdxr_builder_validation_test.cpp cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror -Wno-missing-field-initializers
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror -Wno-missing-field-initializers
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror -Wno-missing-field-initializers
  • Ran python3 -I scripts/check_no_game_data.py runtime/src/xdxr_build.h runtime/test/xdxr_builder_validation_test.cpp and python3 -I tests/test_text_write_line_endings.py: all passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2002) `IlyassRS3:fix/rt-xdxr-build-layout-empty-and-dispatch-guards` → `main` · +169 −34 · 2 files · 1 commits · open Claim: `RT-XDXR-BUILD-LAYOUT-EMPTY-AND-DISPATCH-GUARDS` (claim comment `#7170142282` on #1608). ### Problem In `runtime/src/xdxr_build.h`: 1. **Half-populated layout on overflow**: `XdxrLayout::compute()` populated `tlas`, `box16`, `node_size`, `elements`, `element_size`, and `nodes` before checking the 32-bit offset / 29-bit unit limit and returning `false`, leaving a failed layout partially initialized. 2. **32-bit wrap in `tiles()` and `kept_tiles()`**: `(sort_size + radix_tile - 1) / radix_tile` and `(inner() + radix_tile - 1) / radix_tile` wrap `uint32_t` near `UINT32_MAX` (returning `0` instead of `2^24`). 3. **Zero-workgroup dispatch + wasted barrier on empty BLAS/TLAS**: `finish()` unconditionally dispatched `mode_morton` with `count = l.sort_size`, emitting `vkCmdDispatch(cb, 0, 1, 1)` and a compute-to-compute `vkCmdPipelineBarrier` whenever `sort_size == 0`. 4. **Overflow-prone `64 * tiles` round-trip**: `finish()` multiplied tile group counts by 64 (`64 * l.tiles()`, `64 * l.kept_tiles()`) before `dispatch()` divided back by 64, which can overflow `uint32_t` for large tile counts. 5. **Null `VkCommandBuffer` and partial `make_pipeline()` cleanup**: `record_blas` / `record_tlas` / `record_copy` / `record_postbuild` did not guard `!cb`, and `make_pipeline()` left `layout_` alive on `vkCreateShaderModule` or `vkCreateComputePipelines` failure until `~XdxrBuilder()`. ### Fix - Reset `*this = XdxrLayout{};` before returning `false` on overflow in `XdxrLayout::compute()`. - Make `tiles()` and `kept_tiles()` overflow-free (`n ? 1u + (n - 1u) / radix_tile : 0u`). - Add `dispatch_groups()` (skipping `groups == 0`), guard `!threads` in `dispatch()`, skip `mode_morton` when `l.sort_size == 0`, and dispatch exact tile/workgroup counts directly in `finish()`. - Guard `!cb` in `record_blas`, `record_tlas`, `record_copy`, and `record_postbuild`. - Build into local `VkPipelineLayout` / `VkPipeline` handles in `make_pipeline()`, destroying `layout` immediately on shader-module or pipeline failure so a failed `XdxrBuilder` keeps `layout_` and `pipeline_` null. - Expand `runtime/test/xdxr_builder_validation_test.cpp` (0 `CMakeLists.txt` edits) to cover all 5 fixes. ### Verification - **AI-assisted**: yes (Antigravity agent by Google DeepMind). - **GPUs tested**: none (host is macOS arm64; no retail files). - Ran `runtime/test/xdxr_builder_validation_test.cpp` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror -Wno-missing-field-initializers` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror -Wno-missing-field-initializers` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror -Wno-missing-field-initializers` - Ran `python3 -I scripts/check_no_game_data.py runtime/src/xdxr_build.h runtime/test/xdxr_builder_validation_test.cpp` and `python3 -I tests/test_text_write_line_endings.py`: all passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:25 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 17:55 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 17:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2002#pullrequestreview-6062449492) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2002/head:github-bot/gh-2002'
git switch 'github-bot/gh-2002'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2002'
git switch 'github-bot/gh-2002'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2002'
git switch 'github-bot/gh-2002'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2002'
git switch 'main'
git merge --squash 'github-bot/gh-2002'
git switch 'main'
git merge --ff-only 'github-bot/gh-2002'
git switch 'main'
git merge 'github-bot/gh-2002'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.