Repository navigation
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
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.compindex_of,d3d12_vulkan_device.cppresolve_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
ias the word at the buffer address plus4 * (i / 2), throughWords(buffer_reference_align = 4). That causes two problems:Changes:
src1 + 2i), loads the aligned word that holds it, and picks the half by bit 1 of that address.XdxrTriangles::indicesdocuments 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-byteVkBuffer) breaks that contract. Reading R16 indices through 16-bit loads wherestorageBuffer16BitAccessis 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.hXdxrBuilder::dispatch).Every step was one
vkCmdDispatchofceil(threads / 64)groups. Vulkan guarantees only 65535 along X, and MoltenVK allows no more. These steps went past it:record_copy) and the refit copy into another buffer (refit_begin), which move one word a thread, for any structure over 16 MiB;A step that needs more than 65535 groups (
XdxrBuilder::max_groups) is now split into consecutivevkCmdDispatchBaseranges after one push. The base offsetsgl_WorkGroupIDandgl_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 withVK_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.incis 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.compwith this glslc does not reproducemain's.inceither.Overlap: #2002 (
RT-XDXR-BUILD-LAYOUT-EMPTY-AND-DISPATCH-GUARDS, open since 2026-11-25) also editsXdxrBuilder::dispatchandxdxr_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 withmainitself.Tested
Windows 11, MSVC (VS 18), RelWithDebInfo, on
main43a9d56. Short GPU runs only.xdxr_builder_validation_test(CPU, mock driver). The mock recordsvkCmdDispatchandvkCmdDispatchBasewith their bases. Checks:record_copyof 4 GiB is 16,777,216 groups after one push, as 257 dispatches;vkCmdDispatch, and 65536 is two;Old check 7 (one 16,777,216-group dispatch) is replaced. With
main'sxdxr_build.h, given only themax_groupsconstant so the test compiles, it fails at the flag check; without that check, at the 257-dispatch check.vk_xdxr_build_testallocates 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) andblas_grid_u16_odd_offset0(at 0 mod 4, its last index in the low half of the buffer's last word).xdxr_build_checkvalidates them and traces rays through them like every other case. Withmain'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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code