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

Merged
IlyassRS3 merged 1 commit from upstream-pr/2002 into main 2026-11-28 17:55:05 +00:00
IlyassRS3 commented 2026-11-25 17:55:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T17:55:32Z.


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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496424474 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2002). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T17:55:32Z. --- 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.
copilot-pull-request-reviewer[bot] commented 2026-11-25 17:55:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T17:55:45Z.

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:6062449492 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2002#pullrequestreview-6062449492). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T17:55:45Z. **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 13:12:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T13:12:13Z.


#2002 | head 6c462cc77a (merged cleanly with main d48a957bb9) | ctest --test-dir runtime/build -C RelWithDebInfo -R "xdxr" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: xdxr_builder_validation fails at line 194: g_rec.binds == 1 && g_rec.dispatches == 3 && g_rec.zero_dispatches == 0 && g_rec.barriers == 2 (rc=8) | green on PR+main: 8/8 xdxr CTests pass (xdxr_builder_validation, xdxr_build_output, xdxr_descs, xdxr_build + xdxr_build_check 0 ray mismatches across 30 BLAS/TLAS cases, d3d12_xdxr_rt + d3d12_xdxr_rt_check 0 ray mismatches, and vk_xdxr_postbuild_readback) 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:7239767308 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2002#issuecomment-7239767308). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T13:12:13Z. --- #2002 | head 6c462cc77aee7224aca9df62e3fa6062d4aee46e (merged cleanly with main d48a957bb9988afce424552a3c0c8d01ef12320f) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "xdxr" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: `xdxr_builder_validation` fails at `line 194: g_rec.binds == 1 && g_rec.dispatches == 3 && g_rec.zero_dispatches == 0 && g_rec.barriers == 2` (`rc=8`) | green on PR+main: 8/8 `xdxr` CTests pass (`xdxr_builder_validation`, `xdxr_build_output`, `xdxr_descs`, `xdxr_build` + `xdxr_build_check` 0 ray mismatches across 30 BLAS/TLAS cases, `d3d12_xdxr_rt` + `d3d12_xdxr_rt_check` 0 ray mismatches, and `vk_xdxr_postbuild_readback`) 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
MoonEyes commented 2026-11-28 17:55:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:55:03Z.


Red/green: On main xdxr_builder_validation fails at line 194 (an empty dispatch is not skipped, zero_dispatches != 0), and with the PR ctest -R "^xdxr_builder_validation$" passes.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243387110 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2002#issuecomment-7243387110). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:55:03Z. --- Red/green: On main xdxr_builder_validation fails at line 194 (an empty dispatch is not skipped, zero_dispatches != 0), and with the PR ctest -R "^xdxr_builder_validation$" passes. Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 21:57:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:57:48Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 6c462cc77; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^xdxr_builder_validation$
  • Red on main: 1 failure (line 194: empty dispatch not skipped, zero_dispatches != 0)
  • Green with the PR: OK
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246398301 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2002#issuecomment-7246398301). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:57:48Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `6c462cc77`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^xdxr_builder_validation$` - Red on main: 1 failure (line 194: empty dispatch not skipped, zero_dispatches != 0) - Green with the PR: OK - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
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!2002
No description provided.