Skip to content

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

Open
Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-xdxr-build-index16-and-group-limit
Open

Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-xdxr-build-index16-and-group-limit

Conversation

@Tyrannys

@Tyrannys Tyrannys commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-XDXR-BUILD-INDEX16-AND-GROUP-LIMIT (#1608 (comment))

What and why

Two XDXR BVH builder bugs from @kevincommits' findings in #1608: item 2 of #1608 (comment) and item 4 of #1608 (comment). 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 43a9d56. 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

R16 indices: D3D12 asks only 2-byte alignment of an index buffer, but xdxr_build.comp loaded each
pair of indices as a 4-byte word at the index buffer's address plus 4 * (i / 2). That is a misaligned
load for a buffer at 2 mod 4. It also reads 2 bytes past an odd index count. The shader now loads
the aligned word that holds index i, by the index's own address. resolve_xdxr_build checks the R16
range rounded out to whole words, so that word is inside the checked range.

Workgroup limit: XdxrBuilder::dispatch issued one vkCmdDispatch of ceil(threads / 64) groups. A copy
or a refit into another buffer moves one word a thread, so a structure over 16 MiB needed more than
the 65535 groups Vulkan guarantees along X; so did the per-element steps of a build of over 4M
elements. Larger steps are now split into consecutive vkCmdDispatchBase ranges of at most 65535
groups. The base offsets gl_WorkGroupID and gl_GlobalInvocationID, so the shader is unchanged for
them. The pipeline is created with VK_PIPELINE_CREATE_DISPATCH_BASE_BIT.

xdxr_build.inc is regenerated with glslc from shaderc v2026.1, so it differs throughout.

From kevincommits' findings in ProjectLeonida#1608 (item 2 of 7225958148, item 4 of 7226076494).
xdxr_builder_validation_test checks the split on the CPU. vk_xdxr_build_test gains an R16 case with
an odd triangle count at an address 2 mod 4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…exactly

XdxrTriangles::indices now says that R16 indices are read as the aligned 4-byte words that hold them, so the index range rounded out to whole words must be readable, and that the layer checks that range and refuses a build without it. vk_xdxr_build_test allocates each index buffer at exactly that size instead of 4 bytes an index, and adds an odd-count R16 case at 0 mod 4 whose last index is in the low half of the buffer's last word. From kevincommits' GPU-AV measurement in ProjectLeonida#1608.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant