Skip to content

XDXR builder: split dispatches at maxComputeWorkGroupCount[0] - #7773

Open
ABilenduke wants to merge 2 commits into
ItsNotRexed:mainfrom
ABilenduke:fix/xdxr-dispatch-group-limit
Open

ABilenduke wants to merge 2 commits into
ItsNotRexed:mainfrom
ABilenduke:fix/xdxr-dispatch-group-limit

Conversation

@ABilenduke

@ABilenduke ABilenduke commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: #7333 (comment) (found in a code audit; no item ID yet)

Depends on: #7771 (stacked: this branch contains that PR's commit; review only the last commit, "XDXR builder: ..."), and #3944 (touches xdxr_build.comp)

What and why

XdxrBuilder::dispatch recorded every step as one vkCmdDispatch(cb, (threads + 63) / 64, 1, 1) and never looked at the device limit. Vulkan guarantees only 65,535 groups in X (lavapipe and ANV report exactly that), and past it the behaviour is undefined (VUID-vkCmdDispatch-groupCountX-00386). With the current layout:

  • record_copy (runtime/src/xdxr_build.h:192) runs one thread per word of the destination's room. A CLONE/COMPACT into a 64 MiB pool records 262,144 groups, and a 16 MiB room already passes the limit.
  • The MODE_COPY_AS of a refit into another buffer passes it from 98,689 triangles.
  • MODE_INIT passes it from 1,677,697 triangles.
  • The steps of one thread per element pass it from 4,194,241 elements.

The fix:

  • dispatch() (runtime/src/xdxr_build.h:353-358) now records a step as dispatches of at most max_groups_ workgroups. Each carries the index of its first thread in a new push constant, pc.base (runtime/src/xdxr_build.comp:116). Push grows to 108 bytes, and the static_assert is updated.
  • The shader adds pc.base to its thread index, and pc.base / 64 to the tile index of the per-tile modes: radix count and scatter, kept and kept index (xdxr_build.comp:772-773). The single-workgroup modes (scans, small sort, header) are never split.
  • No barrier separates the dispatches of one step. Their invocations write different words, or meet only through device-scope atomics, as the invocations of one dispatch already do.
  • MODE_PROPAGATE, the one step where invocations pass data, still holds for three reasons:
    • its plain loads read only what earlier steps wrote;
    • each slot's plain store has one writer;
    • the arrival counter's acquire-release atomicAdd works across dispatches as within one.
      No invocation waits for another, so the dispatches may run in any order or overlap. The header comment in xdxr_build.comp (lines 50-58) and docs/bvh.md (GPU builder, items 2 and 5) now say "step" where a step can be several dispatches, and they explain this.
  • VulkanContext::configure_xdxr_dispatch(limits) (runtime/src/vulkan_backend.h:1110) passes maxComputeWorkGroupCount[0]. The D3D12 shim calls it next to configure_counted_dispatch (runtime/shims/d3d12_vulkan_device.cpp:4890), where it already queries the limits. Until then, and for a 0 limit, the builder uses 65,535.

xdxr_build.inc was regenerated as xdxr_build_spv.h documents. The paragraph below the GPU builder list in docs/bvh.md (line 1000) documents the split and its thresholds.

Tests:

  • New xdxr_dispatch_limit_test (CPU only, fake vkGetDeviceProcAddr) records the group counts and push constants. It checks that no dispatch exceeds the limit and that the chunks of each step are contiguous and cover its threads. Cases: a 64 MiB copy, a 150k-triangle refit into another buffer, a 4.5M-triangle build, and a 300k-triangle build with a limit of 1,000, which also splits the per-tile modes.
  • New GPU cases in vk_xdxr_build_test (blas_chunked_*) build, and refit into another buffer, with one workgroup per dispatch. They require the same bytes as the same work recorded whole.

Not changed: a CLONE/COMPACT copy still runs one thread per word of the room, so its cost grows with the pool rather than with the structure. It is now within the limit. Making it proportional to the structure (a fixed grid with a grid-stride loop) is a separate follow-up.

Tested

Windows 11, NVIDIA GeForce RTX 5070 Ti, driver 32.0.16.2124, Vulkan SDK 1.4.363.0, MSVC (VS 18) + Ninja, RelWithDebInfo. Rebased on main 18559d5 plus the R4-1 commit.

Before the fix (the new unit test on unfixed code, default limit 65,535):

ctest -R ^xdxr_dispatch_limit$ --output-on-failure
line 101: d.groups <= limit
  dispatch 0 (mode 12): 262144 groups, limit 65535
  dispatch 0 (mode 12): 99610 groups, limit 65535
  dispatch 0 (mode 0): 175782 groups, limit 65535
  dispatch 1 (mode 1): 70313 groups, limit 65535
  ...
xdxr_dispatch_limit_test: 12 failures

After the fix:

ctest -R xdxr --output-on-failure
xdxr_build_output   Passed
xdxr_descs          Passed
xdxr_build          Passed    1.45 s
xdxr_build_check    Passed  167.57 s
d3d12_xdxr_rt       Passed    1.47 s
d3d12_xdxr_rt_check Passed   32.41 s
xdxr_dispatch_limit Passed    0.11 s
100% tests passed (7/7)

LEONIDA_VK_VALIDATION=1 ctest -R xdxr --output-on-failure
100% tests passed (7/7)   (validation on; no Validation Error / VUID messages)
blas_chunked:       0/50 rays differ
blas_chunked_refit: 0/1000 rays differ

Mutation check: with the shader temporarily ignoring pc.base (and the .inc regenerated), xdxr_build fails on the blas_chunked build. Restored, the full set passed again. glslc reproduces the committed xdxr_build.inc byte for byte from the branch's xdxr_build.comp.

Not posted to the coordination issue yet.

AI-assisted: yes (Claude Code found, fixed and tested this on the GPU above; an independent AI review was addressed before opening)

Checklist

  • 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. (Not applicable: no translator change.)

🤖 Generated with Claude Code

ABilenduke and others added 2 commits November 23, 2026 12:17
index_of loaded the u16 index i as the dword at src1 + 4 * (i >> 1). D3D12 aligns
an R16_UINT index buffer to 2 bytes only, so at an address that is 2 mod 4 every
one of those loads was misaligned (the Words loads are Aligned 4). On an RTX 5070 Ti
it silently read the wrong halves: 578 of 1000 rays missed or hit the wrong triangle.
Now the index's own address is computed, rounded down to its word, and the half
picked by bit 1. vk_xdxr_build_test gains blas_grid_u16_offset2, the R16 grid with
its indices 2 bytes into the buffer, which must match blas_grid_u16_stride16 byte
for byte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every step was one vkCmdDispatch((threads + 63) / 64, 1, 1) with no limit. Vulkan
guarantees only 65,535 groups in X, and a CLONE/COMPACT copy into a 64 MiB pool
recorded 262,144; a refit into another buffer passes the limit from 98,689
triangles and a full build from 1,677,697 (MODE_INIT).

dispatch() now records a step as dispatches of at most max_groups workgroups, each
with its first thread in a new push constant (pc.base; Push grows to 108 bytes).
The shader adds it to its thread index, and pc.base / 64 to the tile index of the
per-tile radix and kept modes. The backend passes the device limit
(VulkanContext::configure_xdxr_dispatch, from the shim's limits query); until then,
and for a 0 limit, the builder uses 65,535.

No barrier separates the dispatches of one step. Their invocations write different
words or meet only through device-scope atomics, as within one dispatch; in
MODE_PROPAGATE no invocation waits for another and the plain loads read only what
earlier steps wrote, so the dispatches may run in any order. The comments in
xdxr_build.comp and docs/bvh.md (GPU builder, steps 2 and 5) now say "step" where
a step can be several dispatches.

xdxr_dispatch_limit_test records the dispatches through a fake vkGetDeviceProcAddr;
vk_xdxr_build_test builds and refits with one workgroup per dispatch and compares
the bytes with the same builds recorded whole.

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

Copy link
Copy Markdown

Tested 6a6f99643 on NVIDIA GeForce RTX 3050 (driver 621.14, Vulkan 1.4.357), Windows 11 Pro (10.0.26100 x64), MSVC 19.44.35230.0 (Ninja, Release), Vulkan SDK 1.4.363.1: pass

What was run

  • cmake --build runtime/build --target xdxr_dispatch_limit_test vk_xdxr_build_test (0 compiler warnings)
  • runtime/build/xdxr_dispatch_limit_test.exe (both normal and with LEONIDA_VK_VALIDATION=1): xdxr_dispatch_limit_test: ok (0 validation errors)
  • runtime/build/vk_xdxr_build_test.exe <out_dir> (both normal and with LEONIDA_VK_VALIDATION=1): vk_xdxr_build_test: built every case (30 cases including blas_grid_u16_offset2, 0 validation errors)
  • python tools/xdxr_bvh.py check-build <out_dir> --rays 200: all cases match (0 validation errors, 0/200 rays differ across all 30 cases)
  • python scripts/check_no_game_data.py & git diff --check: pass

Code review

  • XdxrBuilder::dispatch cleanly splits 1D compute dispatches at max_groups_ (std::max(max_groups, min_max_groups) with min_max_groups = 65535) and advances push.base_group per chunk without adding inter-chunk barriers (gl_WorkGroupID.x + pc.base_group keeps workgroup indices disjoint across chunks of the same stage).
  • Note: this branch also carries the index_of unaligned R16_UINT fix and blas_grid_u16_offset2 test from XDXR builder: read R16 indices from the aligned word that holds them #7771 (Depends on: #7771).

AI-assisted.

@Joaco9020

Copy link
Copy Markdown

Tested 6a6f99643 (with #7771's dcbb61ccd below it) on NVIDIA GeForce RTX 3070 (Ampere), driver 621.01, Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.1: pass

  • xdxr_dispatch_limit_test (new target, configured and built with MSVC): xdxr_dispatch_limit_test: ok.
  • vk_xdxr_build_test <dir>: "built every case" (37 cases written, including blas_grid_u16_offset2).
  • tools/xdxr_bvh.py check-build <dir> --rays 1000: all cases match, 0 validation errors, 0/1000 rays differ in all 30 checked structures.
  • d3d12_xdxr_rt_test <dir>: exit 0 (BLAS 72,704 bytes, same as on main).

Note: this GPU's maxComputeWorkGroupCount[0] is far above 65,535, so these GPU runs show no regression on a large-limit device; the split itself is what the new mock test covers. Synthetic fixtures only, no game files. AI-assisted (Claude).

@rojasruiz

Copy link
Copy Markdown

Tested 6a6f996 merged onto main dde82f2 (clean merge) on NVIDIA GeForce RTX 3080, driver 615.78.08 (maxComputeWorkGroupCount[0] = 2^31-1, so only the forced-limit cases split here), Linux 7.2.15, GCC/CMake native build of the portable runtime subset: pass, and the builder's output is byte-identical to main.

  • ctest -R xdxr: 5/5 passed (xdxr_build_output, xdxr_descs, xdxr_build, xdxr_build_check 100 s, and the new xdxr_dispatch_limit). main passes the same 4 that exist there.
  • vk_xdxr_build_test <out> on the GPU: "built every case", including the four new ones. In the same run:
    • blas_chunked.bin == blas_chunked_reference.bin;
    • blas_chunked_refit.bin == blas_chunked_refit_reference.bin;
    • blas_chunked_refit_source.bin == blas_chunked_refit_reference_source.bin;
    • blas_chunked.json matches its reference except for "address".
  • Against main's vk_xdxr_build_test on the same GPU: all 30 .bin outputs the two builds share are byte-identical, and the 30 shared .json files differ only in the "address" field (the buffer's device address). So with the device limit the 3080 reports, nothing splits and nothing changes.
  • scripts/run_gpu_tests.py: 192/241, 48 skipped, identical to main (the one failure is the known ms_wave_info_s3).
  • check_no_game_data.py on the nine files: clean.

Not run: the D3D12 shim path (configure_xdxr_dispatch from d3d12_vulkan_device.cpp), which is Windows-only.

AI-assisted (Claude Code).

@kamilolejnik

Copy link
Copy Markdown

Tested 6a6f996 (with #7771's dcbb61c under it) on Intel Iris Xe Graphics (TGL GT2, Xe-LP), Mesa ANV 26.3.1, Vulkan 1.4.363, Linux 6.12 (Arch container on Debian 13), GCC, CMake + Ninja RelWithDebInfo: pass. First non-NVIDIA run here.

ctest -R xdxr --output-on-failure: 5/5 passed: xdxr_build_output, xdxr_descs, xdxr_build (1.3 s), xdxr_build_check (109 s) and the new xdxr_dispatch_limit. With #7771 alone the first four pass the same way, so splitting the dispatches changes nothing that the build check sees on this driver.

Synthetic geometry only, no game files. AI-assisted (Claude Code).

@lohant27-netizen

Copy link
Copy Markdown

Tested 6a6f99643 (this head, which includes #7771's dcbb61ccd) on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release: pass.

ctest -R "xdxr|bvh": 7/7 passed, plus bvh_builder skipped (it needs the game's XboxBvhBuilder.dll): xdxr_build_output, xdxr_descs, xdxr_build, xdxr_build_check, d3d12_xdxr_rt, d3d12_xdxr_rt_check, xdxr_dispatch_limit.

  • The split path runs on the GPU even though NVIDIA's maxComputeWorkGroupCount[0] is far above 65,535: vk_xdxr_build_test forces a 1-group limit, and the split build and refit match the whole ones byte for byte.
  • xdxr_dispatch_limit (fake device) is ok.

@Sangonapou

Copy link
Copy Markdown

Tested 6a6f996 (the dispatch-split commit on top of #7771) on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass

Built with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0).

  • ctest -R "xdxr|dispatch_limit": 7/7 passed, including xdxr_dispatch_limit and xdxr_build_check (96 s). The chunked cases: blas_chunked 0/50 rays differ, blas_chunked_refit and its _source / _reference variants 0/1000.
  • Same with LEONIDA_VK_VALIDATION=1: 7/7 passed, no Validation Error or VUID messages.

NVIDIA reports a far larger maxComputeWorkGroupCount[0] than 65,535, so the split only runs here because the builder defaults to the 65,535 minimum when not handed the device limit, which is what the test uses.

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.

7 participants