xdxr: R16 indices at 2 mod 4, and builder steps past 65535 workgroups (RT-XDXR-BUILD-INDEX16-AND-GROUP-LIMIT) #2495

Closed
leonida/mandy
Tyrannys wants to merge 0 commits from rt-xdxr-build-index16-and-group-limit into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

Tyrannys commented 2026-11-27 19:24:40 +00:00

Claimed in the coordination issue as: RT-XDXR-BUILD-INDEX16-AND-GROUP-LIMIT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7226163020)

What and why

Two XDXR BVH builder bugs from @kevincommits' findings in #1608: item 2 of https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225958148 and item 4 of https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7226076494. Thanks for both.

R16 indices (xdxr_build.comp index_of, d3d12_vulkan_device.cpp resolve_xdxr_build).

D3D12 asks only 2-byte alignment of an R16 index buffer, and a mesh with an odd triangle count packed into a shared buffer starts the next one at 2 mod 4. The shader loaded index i as the word at the buffer address plus 4 * (i / 2), through Words (buffer_reference_align = 4). That causes two problems:

  • a misaligned 4-byte load whenever the buffer is at 2 mod 4;
  • with an odd index count, the last index's word reaches 2 bytes past the range the layer checked.

Changes:

  • The shader takes the index's own address (src1 + 2i), loads the aligned word that holds it, and picks the half by bit 1 of that address.
  • The layer checks the R16 range rounded out to whole words (start down, end up to 4). It refuses the build when that range is not mapped, as for any other input range. A title whose R16 index range ends exactly where its memory ends, in the middle of a word, now has its build refused instead of reading 2 bytes past the end.
  • XdxrTriangles::indices documents this contract. With 4-byte loads, the last index of a range that ends mid-word cannot be read without the 2 bytes after it. So a direct caller (only tests; the layer is the one production caller) must leave the rounded range readable. @kevincommits' case A (an exact 6-byte VkBuffer) breaks that contract. Reading R16 indices through 16-bit loads where storageBuffer16BitAccess is enabled would remove the requirement. That needs the feature enabled on the layer's device and a second shader module, so it is left for a follow-up.

Workgroup limit (xdxr_build.h XdxrBuilder::dispatch).

Every step was one vkCmdDispatch of ceil(threads / 64) groups. Vulkan guarantees only 65535 along X, and MoltenVK allows no more. These steps went past it:

  • a copy (record_copy) and the refit copy into another buffer (refit_begin), which move one word a thread, for any structure over 16 MiB;
  • the per-element steps of a build of over 4M elements (morton, karras, nodes and the rest).

A step that needs more than 65535 groups (XdxrBuilder::max_groups) is now split into consecutive vkCmdDispatchBase ranges after one push. The base offsets gl_WorkGroupID and gl_GlobalInvocationID, so every mode, the per-tile ones included, sees the ids one dispatch would give it, with no shader change. The pipeline is created with VK_PIPELINE_CREATE_DISPATCH_BASE_BIT (Vulkan 1.1 core; the layer's device is 1.3). Steps of 65535 groups or fewer are recorded as before.

xdxr_build.inc is regenerated with the documented command (glslc --target-env=vulkan1.2 -O -mfmt=c) using glslc from shaderc v2026.1 (Vulkan SDK 1.4.341). It differs throughout because the compiler is newer: main's .comp with this glslc does not reproduce main's .inc either.

Overlap: #2002 (RT-XDXR-BUILD-LAYOUT-EMPTY-AND-DISPATCH-GUARDS, open since 2026-11-25) also edits XdxrBuilder::dispatch and xdxr_builder_validation_test.cpp, so the two conflict there. The changes combine: #2002 skips zero-group dispatches, and this PR splits large ones. Whichever lands second keeps both. No other open PR conflicts with this branch where it merges cleanly with main itself.

Tested

Windows 11, MSVC (VS 18), RelWithDebInfo, on main 43a9d562d. Short GPU runs only.

  • xdxr_builder_validation_test (CPU, mock driver). The mock records vkCmdDispatch and vkCmdDispatchBase with their bases. Checks:

    • the pipeline has the dispatch-base flag;
    • no dispatch has more than 65535 groups, and each step's dispatches are consecutive from 0;
    • a record_copy of 4 GiB is 16,777,216 groups after one push, as 257 dispatches;
    • 65535 groups is still one vkCmdDispatch, and 65536 is two;
    • a 5M-triangle build, and its refit into another buffer, stay within the limit, and the refit's copy covers the whole structure.

    Old check 7 (one 16,777,216-group dispatch) is replaced. With main's xdxr_build.h, given only the max_groups constant so the test compiles, it fails at the flag check; without that check, at the 257-dispatch check.

  • vk_xdxr_build_test allocates each index buffer at exactly the indices' range rounded out to whole words (it used to allocate 4 bytes an index, which hid case A). It gains two R16 cases with an odd triangle count (97): blas_grid_u16_odd_offset2 (at an address 2 mod 4) and blas_grid_u16_odd_offset0 (at 0 mod 4, its last index in the low half of the buffer's last word). xdxr_build_check validates them and traces rays through them like every other case. With main's shader the submission of the offset-2 build fails on the RTX 4070 SUPER. With the fix it builds and passes the check.

  • ctest -R "^(xdxr_builder_validation|xdxr_build|xdxr_build_check|d3d12_xdxr_rt|d3d12_xdxr_rt_check|vk_xdxr_postbuild_readback)$": 6/6 pass. The GPU parts take seconds; the two Python checks take about 2 minutes on the CPU.

  • scripts/check_no_game_data.py: clean.

Not tested: a real device with a 65535 limit (MoltenVK) running a split dispatch on the GPU, and GPU-assisted validation of the aligned loads.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Claimed in the coordination issue as: RT-XDXR-BUILD-INDEX16-AND-GROUP-LIMIT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7226163020) ### What and why Two XDXR BVH builder bugs from @kevincommits' findings in #1608: item 2 of https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225958148 and item 4 of https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7226076494. Thanks for both. **R16 indices (`xdxr_build.comp` `index_of`, `d3d12_vulkan_device.cpp` `resolve_xdxr_build`).** D3D12 asks only 2-byte alignment of an R16 index buffer, and a mesh with an odd triangle count packed into a shared buffer starts the next one at 2 mod 4. The shader loaded index `i` as the word at the buffer address plus `4 * (i / 2)`, through `Words` (`buffer_reference_align = 4`). That causes two problems: - a misaligned 4-byte load whenever the buffer is at 2 mod 4; - with an odd index count, the last index's word reaches 2 bytes past the range the layer checked. Changes: - The shader takes the index's own address (`src1 + 2i`), loads the aligned word that holds it, and picks the half by bit 1 of that address. - The layer checks the R16 range rounded out to whole words (start down, end up to 4). It refuses the build when that range is not mapped, as for any other input range. A title whose R16 index range ends exactly where its memory ends, in the middle of a word, now has its build refused instead of reading 2 bytes past the end. - `XdxrTriangles::indices` documents this contract. With 4-byte loads, the last index of a range that ends mid-word cannot be read without the 2 bytes after it. So a direct caller (only tests; the layer is the one production caller) must leave the rounded range readable. @kevincommits' case A (an exact 6-byte `VkBuffer`) breaks that contract. Reading R16 indices through 16-bit loads where `storageBuffer16BitAccess` is enabled would remove the requirement. That needs the feature enabled on the layer's device and a second shader module, so it is left for a follow-up. **Workgroup limit (`xdxr_build.h` `XdxrBuilder::dispatch`).** Every step was one `vkCmdDispatch` of `ceil(threads / 64)` groups. Vulkan guarantees only 65535 along X, and MoltenVK allows no more. These steps went past it: - a copy (`record_copy`) and the refit copy into another buffer (`refit_begin`), which move one word a thread, for any structure over 16 MiB; - the per-element steps of a build of over 4M elements (morton, karras, nodes and the rest). A step that needs more than 65535 groups (`XdxrBuilder::max_groups`) is now split into consecutive `vkCmdDispatchBase` ranges after one push. The base offsets `gl_WorkGroupID` and `gl_GlobalInvocationID`, so every mode, the per-tile ones included, sees the ids one dispatch would give it, with no shader change. The pipeline is created with `VK_PIPELINE_CREATE_DISPATCH_BASE_BIT` (Vulkan 1.1 core; the layer's device is 1.3). Steps of 65535 groups or fewer are recorded as before. `xdxr_build.inc` is regenerated with the documented command (`glslc --target-env=vulkan1.2 -O -mfmt=c`) using glslc from shaderc v2026.1 (Vulkan SDK 1.4.341). It differs throughout because the compiler is newer: `main`'s `.comp` with this glslc does not reproduce `main`'s `.inc` either. Overlap: #2002 (`RT-XDXR-BUILD-LAYOUT-EMPTY-AND-DISPATCH-GUARDS`, open since 2026-11-25) also edits `XdxrBuilder::dispatch` and `xdxr_builder_validation_test.cpp`, so the two conflict there. The changes combine: #2002 skips zero-group dispatches, and this PR splits large ones. Whichever lands second keeps both. No other open PR conflicts with this branch where it merges cleanly with `main` itself. ### Tested Windows 11, MSVC (VS 18), RelWithDebInfo, on `main` 43a9d562d. Short GPU runs only. - `xdxr_builder_validation_test` (CPU, mock driver). The mock records `vkCmdDispatch` and `vkCmdDispatchBase` with their bases. Checks: - the pipeline has the dispatch-base flag; - no dispatch has more than 65535 groups, and each step's dispatches are consecutive from 0; - a `record_copy` of 4 GiB is 16,777,216 groups after one push, as 257 dispatches; - 65535 groups is still one `vkCmdDispatch`, and 65536 is two; - a 5M-triangle build, and its refit into another buffer, stay within the limit, and the refit's copy covers the whole structure. Old check 7 (one 16,777,216-group dispatch) is replaced. With `main`'s `xdxr_build.h`, given only the `max_groups` constant so the test compiles, it fails at the flag check; without that check, at the 257-dispatch check. - `vk_xdxr_build_test` allocates each index buffer at exactly the indices' range rounded out to whole words (it used to allocate 4 bytes an index, which hid case A). It gains two R16 cases with an odd triangle count (97): `blas_grid_u16_odd_offset2` (at an address 2 mod 4) and `blas_grid_u16_odd_offset0` (at 0 mod 4, its last index in the low half of the buffer's last word). `xdxr_build_check` validates them and traces rays through them like every other case. With `main`'s shader the submission of the offset-2 build fails on the RTX 4070 SUPER. With the fix it builds and passes the check. - `ctest -R "^(xdxr_builder_validation|xdxr_build|xdxr_build_check|d3d12_xdxr_rt|d3d12_xdxr_rt_check|vk_xdxr_postbuild_readback)$"`: 6/6 pass. The GPU parts take seconds; the two Python checks take about 2 minutes on the CPU. - `scripts/check_no_game_data.py`: clean. Not tested: a real device with a 65535 limit (MoltenVK) running a split dispatch on the GPU, and GPU-assisted validation of the aligned loads. GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
lixonik approved these changes 2026-11-27 21:41:20 +00:00
lixonik left a comment

Reviewed the diff and tested locally on Windows 11 + NVIDIA GeForce RTX 5070 12 GB (MSVC 19.44, Ninja Release).

  • xdxr_build.comp index_of() aligns the R16 index byte address down to 4 bytes (a.x & ~3u) and selects low/high half via (a.x & 2u), and resolve_xdxr_build() validates the word-rounded range (t.index_va - lead, words) while preserving out + lead.
  • Splitting compute dispatches past XdxrBuilder::max_groups (65535) via vkCmdDispatchBase with VK_PIPELINE_CREATE_DISPATCH_BASE_BIT is sound: xdxr_build.comp uses only gl_GlobalInvocationID.x, gl_WorkGroupID.x, and gl_LocalInvocationID.x (never gl_NumWorkGroups).
  • Built xdxr_builder_validation_test, vk_xdxr_build_test, and d3d12_xdxr_rt_test and ran ctest --output-on-failure:
    • xdxr_builder_validation: passed (0.03 s).
    • xdxr_build: passed (0.99 s, including blas_grid_u16_odd_offset2 and blas_grid_u16_odd_offset0).
    • xdxr_build_check: passed (85.10 s, full 1000-ray BVH check).
    • d3d12_xdxr_rt: passed (1.81 s).
    • d3d12_xdxr_rt_check: passed (16.90 s).

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341; VK_LAYER_KHRONOS_validation not installed locally).
AI-assisted (Antigravity, Gemini).

Reviewed the diff and tested locally on Windows 11 + NVIDIA GeForce RTX 5070 12 GB (MSVC 19.44, Ninja Release). - `xdxr_build.comp` `index_of()` aligns the R16 index byte address down to 4 bytes (`a.x & ~3u`) and selects low/high half via `(a.x & 2u)`, and `resolve_xdxr_build()` validates the word-rounded range `(t.index_va - lead, words)` while preserving `out + lead`. - Splitting compute dispatches past `XdxrBuilder::max_groups` (65535) via `vkCmdDispatchBase` with `VK_PIPELINE_CREATE_DISPATCH_BASE_BIT` is sound: `xdxr_build.comp` uses only `gl_GlobalInvocationID.x`, `gl_WorkGroupID.x`, and `gl_LocalInvocationID.x` (never `gl_NumWorkGroups`). - Built `xdxr_builder_validation_test`, `vk_xdxr_build_test`, and `d3d12_xdxr_rt_test` and ran `ctest --output-on-failure`: - `xdxr_builder_validation`: passed (0.03 s). - `xdxr_build`: passed (0.99 s, including `blas_grid_u16_odd_offset2` and `blas_grid_u16_odd_offset0`). - `xdxr_build_check`: passed (85.10 s, full 1000-ray BVH check). - `d3d12_xdxr_rt`: passed (1.81 s). - `d3d12_xdxr_rt_check`: passed (16.90 s). GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341; `VK_LAYER_KHRONOS_validation` not installed locally). AI-assisted (Antigravity, Gemini).
timbot103 commented 2026-11-28 02:01:50 +00:00

Reviewed at head d85a9ab4f on Windows 11 (MSVC 2022 19.44) with dual-GPU Vulkan validation (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363.0).

Verification

  • Built xdxr_builder_validation_test and vk_xdxr_build_test.
  • xdxr_builder_validation_test: all checks passed.
  • vk_xdxr_build_test (LEONIDA_VK_VALIDATION=1): built every case (0 validation errors) on both NVIDIA GeForce RTX 5080 (616.64) and AMD Radeon(TM) Graphics (25.10.30.02).
  • Checked xdxr_build.inc: SPIR-V 1.5 header keeps the consistent Shaderc generator word (0x000d000b) and passes spirv-val --target-env vulkan1.2; R16 2-mod-4 word-aligned load and half selection (xdxr_build.comp:264-274, d3d12_vulkan_device.cpp:3919-3929) look right.

Finding (Request changes)

  • vkCmdDispatchBase exceeds maxComputeWorkGroupCount[0] on devices at the 65,535 limit (xdxr_build.h:408-424; xdxr_builder_validation_test.cpp:50-55, 203-232):
    Splitting >65,535 workgroups by calling vkCmdDispatchBase(cb, uint32_t(first), 0, 0, min(65535, groups - first), 1, 1) passes baseGroupX = 65535, groupCountX = 1 on the second chunk when groups == 65536. On a Vulkan device whose maxComputeWorkGroupCount[0] == 65535 (the exact spec minimum this split targets), that violates VUID-vkCmdDispatchBase-baseGroupX-00421 (baseGroupX < maxComputeWorkGroupCount[0]) and VUID-vkCmdDispatchBase-groupCountX-00424 (groupCountX <= maxComputeWorkGroupCount[0] - baseGroupX). Desktop GPUs like RTX 5080 and AMD Radeon mask this because their X limit is 2^31 - 1, and the unit mock only checks per-call count rather than baseGroupX + groupCountX <= limit. Passing a logical workgroup offset in push constants with ordinary vkCmdDispatch(cb, chunk, 1, 1) calls stays strictly valid on 65,535-limit hardware.

AI-assisted review + local MSVC/dual-GPU verification.

Reviewed at head `d85a9ab4f` on Windows 11 (MSVC 2022 `19.44`) with dual-GPU Vulkan validation (`LEONIDA_VK_VALIDATION=1`, Vulkan SDK `1.4.363.0`). ### Verification - Built `xdxr_builder_validation_test` and `vk_xdxr_build_test`. - `xdxr_builder_validation_test`: **all checks passed**. - `vk_xdxr_build_test` (`LEONIDA_VK_VALIDATION=1`): **built every case** (0 validation errors) on both **NVIDIA GeForce RTX 5080** (`616.64`) and **AMD Radeon(TM) Graphics** (`25.10.30.02`). - Checked `xdxr_build.inc`: SPIR-V 1.5 header keeps the consistent Shaderc generator word (`0x000d000b`) and passes `spirv-val --target-env vulkan1.2`; R16 2-mod-4 word-aligned load and half selection (`xdxr_build.comp:264-274`, `d3d12_vulkan_device.cpp:3919-3929`) look right. ### Finding (Request changes) - **`vkCmdDispatchBase` exceeds `maxComputeWorkGroupCount[0]` on devices at the 65,535 limit (`xdxr_build.h:408-424`; `xdxr_builder_validation_test.cpp:50-55`, `203-232`):** Splitting >65,535 workgroups by calling `vkCmdDispatchBase(cb, uint32_t(first), 0, 0, min(65535, groups - first), 1, 1)` passes `baseGroupX = 65535, groupCountX = 1` on the second chunk when `groups == 65536`. On a Vulkan device whose `maxComputeWorkGroupCount[0] == 65535` (the exact spec minimum this split targets), that violates **`VUID-vkCmdDispatchBase-baseGroupX-00421`** (`baseGroupX < maxComputeWorkGroupCount[0]`) and **`VUID-vkCmdDispatchBase-groupCountX-00424`** (`groupCountX <= maxComputeWorkGroupCount[0] - baseGroupX`). Desktop GPUs like RTX 5080 and AMD Radeon mask this because their X limit is `2^31 - 1`, and the unit mock only checks per-call count rather than `baseGroupX + groupCountX <= limit`. Passing a logical workgroup offset in push constants with ordinary `vkCmdDispatch(cb, chunk, 1, 1)` calls stays strictly valid on 65,535-limit hardware. AI-assisted review + local MSVC/dual-GPU verification.
aslan2142 commented 2026-12-01 16:45:30 +00:00

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.

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.
Sign in to join this conversation.
No description provided.