indirect clamp: bound dispatch records by the device's maxComputeWorkGroupCount, not 65,535 (RT-INDIRECT-DISPATCH-GROUP-LIMIT) #2230

Manually merged
github merged 1 commit from github-bot/gh-2230 into main 2026-11-27 00:35:15 +00:00 AGit
Owner

@⁠Tyrannys on GitHub · 2026-11-26 08:50 UTC · original
Tyrannys:rt-indirect-dispatch-group-limit → main · +539 −305 · 6 files · 2 commits · merged 2026-11-26 21:43 UTC

Claimed in the coordination issue as: RT-INDIRECT-DISPATCH-GROUP-LIMIT (#1608)

What and why

The ExecuteIndirect argument clamp (runtime/src/indirect_clamp.comp, driven by vulkan_indirect_clamp.h) bounded every dispatch record at a fixed 65,535 groups per dimension, which is Vulkan's guaranteed minimum for maxComputeWorkGroupCount. Real devices report far more in x (this RTX 4070 SUPER: 2,147,483,647). So a one-dimensional indirect dispatch of more than 65,535 groups ran only its first 65,535 groups. The rest never wrote their output, which leaves stale blocks on screen.

Now:

  • The clamp gets the device's maxComputeWorkGroupCount per axis in its push constants (the block grows from 72 to 88 bytes: three limit words and a reserved word) and bounds dispatch records by those limits (indirect_clamp_dispatch_limit).
  • The 2^20 groups-per-dispatch cap, the 2^27 list budget and LEONIDA_INDIRECT_GROUP_BUDGET are unchanged, so the TDR protection stays the same. Since x alone can now be above 65,535, the product check divides instead of multiplying.
  • Mesh dispatches keep 65,535 in x. Their limit is a separate one (maxMeshWorkGroupCount), so they are left for another PR.
  • No CPU readback of arguments. The pass is the same single tiny dispatch as before.

Credit: the idea is from codec-d/cool_fox docs/findings.md ("Blocky patches: indirect dispatches cut at 65,535 groups"). I used the idea only and took no code.

Not covered here (follow-up, if needed): a dispatch above the device's own limit. That means x > 65,535 on a device whose x limit is 65,535, or y/z > 65,535 anywhere. It is still cut at that limit. Running it whole would mean splitting it with a group-base offset that the translated shader adds to WorkgroupId. The translator reads WorkgroupId with no base today, and vkCmdDispatchBase would need VK_PIPELINE_CREATE_DISPATCH_BASE_BIT on every compute pipeline, which would invalidate existing pipeline caches. Direct Dispatch (not indirect) still passes its counts through unchanged. D3D12 already forbids counts above 65,535 there.

Tested

  • New case 10 in indirect_clamp_test sends real vkCmdDispatchIndirect calls through the clamped scratch. A marker shader (runtime/test/glsl/cs_group_marker.comp) does one atomic per workgroup, and the test reads the marks back. No game files.
    • 70000 x 1 x 1: on main, 65,535 groups ran once and 4,465 never ran (the test fails, 3 CHECKs). With this change, all 70,000 groups run exactly once and none run past the end.
    • 65535 x 1 x 1 and 1 x 65535 x 1 (exactly at the old limit): every group runs exactly once, both on main and with this change.
    • A size above the device's own limit is skipped, with a message.
  • Updated case 6b (the cut now follows the device's y limit) and added case 6c (70,000 in x comes back unclamped where the device allows it).
  • LEONIDA_VK_VALIDATION=1 indirect_clamp_test: passes, 0 validation errors.
  • ctest -R "indirect|clamp": 25/25 passed. Full runtime ctest (-j 2, while a title boot was running on the same GPU): 473/477 passed. Two were timeouts (d3d12_queue_wait_timeout, d3d12_queue_list_type) that pass when rerun alone. Two fail without involving the clamp: d3d12_set_descriptor_heaps dispatches with no PSO bound, which is fixed by the open #2153, and xbox_present fails present with VkResult -11 while the boot holds the display.
  • tests/test_indirect_clamp_telemetry.py, tests/test_ctest_registration.py: pass.
  • indirect_clamp_spv.h was regenerated with the Vulkan SDK 1.4.341 glslangValidator (-V --target-env vulkan1.2 -Os --spirv-val), which emits different ids than the earlier header.

GPUs tested: NVIDIA GeForce RTX 4070 SUPER, 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

**@⁠Tyrannys** on GitHub · 2026-11-26 08:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2230) `Tyrannys:rt-indirect-dispatch-group-limit` → `main` · +539 −305 · 6 files · 2 commits · merged 2026-11-26 21:43 UTC Claimed in the coordination issue as: RT-INDIRECT-DISPATCH-GROUP-LIMIT (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why The ExecuteIndirect argument clamp (`runtime/src/indirect_clamp.comp`, driven by `vulkan_indirect_clamp.h`) bounded every dispatch record at a fixed 65,535 groups per dimension, which is Vulkan's guaranteed minimum for `maxComputeWorkGroupCount`. Real devices report far more in x (this RTX 4070 SUPER: 2,147,483,647). So a one-dimensional indirect dispatch of more than 65,535 groups ran only its first 65,535 groups. The rest never wrote their output, which leaves stale blocks on screen. Now: - The clamp gets the device's `maxComputeWorkGroupCount` per axis in its push constants (the block grows from 72 to 88 bytes: three limit words and a reserved word) and bounds dispatch records by those limits (`indirect_clamp_dispatch_limit`). - The 2^20 groups-per-dispatch cap, the 2^27 list budget and `LEONIDA_INDIRECT_GROUP_BUDGET` are unchanged, so the TDR protection stays the same. Since x alone can now be above 65,535, the product check divides instead of multiplying. - Mesh dispatches keep 65,535 in x. Their limit is a separate one (`maxMeshWorkGroupCount`), so they are left for another PR. - No CPU readback of arguments. The pass is the same single tiny dispatch as before. Credit: the idea is from codec-d/cool_fox `docs/findings.md` ("Blocky patches: indirect dispatches cut at 65,535 groups"). I used the idea only and took no code. Not covered here (follow-up, if needed): a dispatch above the device's own limit. That means x > 65,535 on a device whose x limit is 65,535, or y/z > 65,535 anywhere. It is still cut at that limit. Running it whole would mean splitting it with a group-base offset that the translated shader adds to `WorkgroupId`. The translator reads `WorkgroupId` with no base today, and `vkCmdDispatchBase` would need `VK_PIPELINE_CREATE_DISPATCH_BASE_BIT` on every compute pipeline, which would invalidate existing pipeline caches. Direct `Dispatch` (not indirect) still passes its counts through unchanged. D3D12 already forbids counts above 65,535 there. ### Tested - New case 10 in `indirect_clamp_test` sends real `vkCmdDispatchIndirect` calls through the clamped scratch. A marker shader (`runtime/test/glsl/cs_group_marker.comp`) does one atomic per workgroup, and the test reads the marks back. No game files. - `70000 x 1 x 1`: on `main`, 65,535 groups ran once and **4,465 never ran** (the test fails, 3 CHECKs). With this change, all 70,000 groups run exactly once and none run past the end. - `65535 x 1 x 1` and `1 x 65535 x 1` (exactly at the old limit): every group runs exactly once, both on `main` and with this change. - A size above the device's own limit is skipped, with a message. - Updated case 6b (the cut now follows the device's y limit) and added case 6c (70,000 in x comes back unclamped where the device allows it). - `LEONIDA_VK_VALIDATION=1 indirect_clamp_test`: passes, 0 validation errors. - `ctest -R "indirect|clamp"`: 25/25 passed. Full runtime `ctest` (`-j 2`, while a title boot was running on the same GPU): 473/477 passed. Two were timeouts (`d3d12_queue_wait_timeout`, `d3d12_queue_list_type`) that pass when rerun alone. Two fail without involving the clamp: `d3d12_set_descriptor_heaps` dispatches with no PSO bound, which is fixed by the open #2153, and `xbox_present` fails `present` with VkResult -11 while the boot holds the display. - `tests/test_indirect_clamp_telemetry.py`, `tests/test_ctest_registration.py`: pass. - `indirect_clamp_spv.h` was regenerated with the Vulkan SDK 1.4.341 glslangValidator (`-V --target-env vulkan1.2 -Os --spirv-val`), which emits different ids than the earlier header. GPUs tested: NVIDIA GeForce RTX 4070 SUPER, 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. - [x] 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)
The ExecuteIndirect argument clamp (indirect_clamp.comp) cut every dispatch
record at a fixed 65,535 groups per dimension, Vulkan's guaranteed minimum.
Devices report far more in x (NVIDIA: 2^31 - 1), so a one-dimensional
indirect dispatch of more than 65,535 groups ran only its first 65,535
groups; the rest never wrote their output.

The clamp now takes the device's maxComputeWorkGroupCount per axis in its
push constants (88 bytes, was 72) and bounds dispatch records by that. The
2^20 groups-per-dispatch cap, the list budget and LEONIDA_INDIRECT_GROUP_BUDGET
are unchanged; the product check now divides instead of multiplying, since x
alone may exceed 65,535. Mesh dispatches keep 65,535 in x.

indirect_clamp_test runs real indirect dispatches through the clamped
scratch with a marker shader (one atomic per group): 70,000 x 1 x 1 ran
65,535 groups and left 4,465 unrun on main, and runs all 70,000 exactly once
now; 65,535 x 1 x 1 and 1 x 65,535 x 1 (at the old limit) run exactly once
on both. A size past the device's own limit is skipped.

Idea from codec-d/cool_fox docs/findings.md ("Blocky patches: indirect
dispatches cut at 65,535 groups"); idea only, no code taken.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:23:55 +00:00
Author
Owner

@⁠real-rusher on GitHub · 2026-11-26 10:06 UTC · original

Verified on main a73a57e1 with Mesa llvmpipe (Mesa 25.2.8, LLVM 20.1.2, 256 bits, Ubuntu 24.04 x86-64, no GPU), GCC 13, Ninja. Merges cleanly.

  • The generated headers match their sources: glslangValidator 15.1.0 with the flags in the header comments rebuilds runtime/src/indirect_clamp_spv.h (2578 words) and runtime/test/cs_group_marker_spv.h (286 words) word for word, apart from the generator word.
  • ctest -R '^indirect_clamp$' passes with this PR: "every kind clamped, in-range records bit-identical, the budget saturating", validation errors 0.
  • The PR's indirect_clamp_test.cpp does not build against main (leonida::indirect_clamp_dispatch_limit is missing there).
  • Limit: llvmpipe reports maxComputeWorkGroupCount 65535, so the 70000 x 1 x 1 case is skipped on it ("past this device's maxComputeWorkGroupCount; skipped"). The new above-65535 path still needs a device with a higher limit.

AI-assisted: yes (Claude Code)

**@⁠real-rusher** on GitHub · 2026-11-26 10:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2230#issuecomment-7188921463) Verified on main a73a57e1 with Mesa llvmpipe (Mesa 25.2.8, LLVM 20.1.2, 256 bits, Ubuntu 24.04 x86-64, no GPU), GCC 13, Ninja. Merges cleanly. - The generated headers match their sources: glslangValidator 15.1.0 with the flags in the header comments rebuilds runtime/src/indirect_clamp_spv.h (2578 words) and runtime/test/cs_group_marker_spv.h (286 words) word for word, apart from the generator word. - ctest -R '^indirect_clamp$' passes with this PR: "every kind clamped, in-range records bit-identical, the budget saturating", validation errors 0. - The PR's indirect_clamp_test.cpp does not build against main (leonida::indirect_clamp_dispatch_limit is missing there). - Limit: llvmpipe reports maxComputeWorkGroupCount 65535, so the 70000 x 1 x 1 case is skipped on it ("past this device's maxComputeWorkGroupCount; skipped"). The new above-65535 path still needs a device with a higher limit. AI-assisted: yes (Claude Code)
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 10:37 UTC · original · reviewed

Independent native Windows check on GeForce RTX 5070, NVIDIA 610.88: indirect_clamp, indirect_clamp_safety, and d3d12_indirect_dispatch_count passed. The GPU mark-and-readback cases ran without skips, including the 70,000-group X dispatch and the 65,535-group controls. The counted Vulkan dispatches also matched their expected results. Synthetic inputs only. AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 10:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2230#pullrequestreview-6072570510) · **reviewed** Independent native Windows check on GeForce RTX 5070, NVIDIA 610.88: `indirect_clamp`, `indirect_clamp_safety`, and `d3d12_indirect_dispatch_count` passed. The GPU mark-and-readback cases ran without skips, including the 70,000-group X dispatch and the 65,535-group controls. The counted Vulkan dispatches also matched their expected results. Synthetic inputs only. AI-assisted.
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-26 13:03 UTC · original · approved these changes

Reviewed by Claude Code (AI-assisted) on my behalf. Looks right to me, approving.

  • The new overflow check x > max_groups || y > max_groups / x || z > max_groups / x / y is safe now that x can be
    far above 65,535. The old x * y could overflow 32 bits once x is no longer capped there. The new
    cost = x*y*z*64 stays under 2^26.
  • The push block is 6 addresses plus 10 words, 88 bytes, matching the shader. Test 6b only passes with the
    regenerated indirect_clamp_spv.h, so the header and the .comp agree.
  • Mesh dispatches stay at 65,535 in x. That matches what is seen there: a mesh record past it is a slot the title
    never filled.

Tested on Linux with Mesa lavapipe at 7a761696: indirect_clamp_test, indirect_clamp_safety_test and
indirect_topology_and_stats_test pass. lavapipe reports 65,535 in x, so the 70,000 x 1 x 1 case is skipped there.
It still needs a run on an NVIDIA or AMD device, whose x limit is far higher.

Non-blocking: on a device whose x limit is 65,535, a wider dispatch is still cut. A rate-limited log line when a
dispatch record asks past the device limit would make that case visible. The stats already count clamped records,
but they don't say why.


Generated by Claude Code

**@⁠kawaii-shakal** on GitHub · 2026-11-26 13:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2230#pullrequestreview-6074596448) · **approved these changes** Reviewed by Claude Code (AI-assisted) on my behalf. Looks right to me, approving. - The new overflow check `x > max_groups || y > max_groups / x || z > max_groups / x / y` is safe now that x can be far above 65,535. The old `x * y` could overflow 32 bits once x is no longer capped there. The new `cost = x*y*z*64` stays under 2^26. - The push block is 6 addresses plus 10 words, 88 bytes, matching the shader. Test 6b only passes with the regenerated `indirect_clamp_spv.h`, so the header and the `.comp` agree. - Mesh dispatches stay at 65,535 in x. That matches what is seen there: a mesh record past it is a slot the title never filled. Tested on Linux with Mesa lavapipe at 7a761696: `indirect_clamp_test`, `indirect_clamp_safety_test` and `indirect_topology_and_stats_test` pass. lavapipe reports 65,535 in x, so the 70,000 x 1 x 1 case is skipped there. It still needs a run on an NVIDIA or AMD device, whose x limit is far higher. Non-blocking: on a device whose x limit is 65,535, a wider dispatch is still cut. A rate-limited log line when a dispatch record asks past the device limit would make that case visible. The stats already count clamped records, but they don't say why. --- _Generated by [Claude Code](https://claude.ai/code)_
github manually merged commit 595b1325d9 into main 2026-11-27 00:35:15 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-26 21:43 UTC · original

Thanks for the fix. main had moved past this branch, so I merged it in as a maintainer (a merge commit, no rebase). Your clamp change itself is untouched.

  • What the merge resolved: main (fac8b4bb9) contains #2378, which changed indirect_clamp.comp next to your dispatch bound: a draw record of 2^23 vertices or more now runs empty and charges nothing. indirect_clamp.comp, vulkan_indirect_clamp.h and indirect_clamp_test.cpp merged without conflicts and keep both changes.
  • Generated header: the only conflict was runtime/src/indirect_clamp_spv.h. It is regenerated from the merged .comp with the command in its header comment (glslangValidator 16.6.0, -V --target-env vulkan1.2 -Os --spirv-val; spirv-val passes).
  • AMD check: on RADV, Mesa 26.3.1 (Ryzen 7950X3D iGPU and RX 7900 XTX, both maxComputeWorkGroupCount[0] = 4294967295), the 70000 × 1 × 1 case ran instead of being skipped. indirect_clamp_test passed with the validation layer on, and the kernel log stayed clean. On llvmpipe it is skipped as before.
  • Extra local check: a 67,440 × 1 × 1 record run through vkCmdDispatchIndirect and a group counter ran all 67,440 groups on both RADV GPUs.
  • Other tests: Linux ctest on llvmpipe passed 114 of 114. The Windows suite under Wine on the iGPU: 488 tests, none changed against main fac8b4bb9.
  • The review's non-blocking point (a log line when a record is cut at the device's limit) will come as a small follow-up PR on top of this one: dispatch counters and an indirect clamp dispatch: line.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

**@⁠aslan2142** on GitHub · 2026-11-26 21:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2230#issuecomment-7203994171) Thanks for the fix. `main` had moved past this branch, so I merged it in as a maintainer (a merge commit, no rebase). Your clamp change itself is untouched. - **What the merge resolved:** `main` (fac8b4bb9) contains #2378, which changed `indirect_clamp.comp` next to your dispatch bound: a draw record of 2^23 vertices or more now runs empty and charges nothing. `indirect_clamp.comp`, `vulkan_indirect_clamp.h` and `indirect_clamp_test.cpp` merged without conflicts and keep both changes. - **Generated header:** the only conflict was `runtime/src/indirect_clamp_spv.h`. It is regenerated from the merged `.comp` with the command in its header comment (glslangValidator 16.6.0, `-V --target-env vulkan1.2 -Os --spirv-val`; `spirv-val` passes). - **AMD check:** on RADV, Mesa 26.3.1 (Ryzen 7950X3D iGPU and RX 7900 XTX, both `maxComputeWorkGroupCount[0]` = 4294967295), the 70000 × 1 × 1 case ran instead of being skipped. `indirect_clamp_test` passed with the validation layer on, and the kernel log stayed clean. On llvmpipe it is skipped as before. - **Extra local check:** a 67,440 × 1 × 1 record run through `vkCmdDispatchIndirect` and a group counter ran all 67,440 groups on both RADV GPUs. - **Other tests:** Linux `ctest` on llvmpipe passed 114 of 114. The Windows suite under Wine on the iGPU: 488 tests, none changed against `main` fac8b4bb9. - **The review's non-blocking point** (a log line when a record is cut at the device's limit) will come as a small follow-up PR on top of this one: dispatch counters and an `indirect clamp dispatch:` line. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-26 21:43 UTC · original

Merging.

  • Why: a dispatch record's bound follows the device's maxComputeWorkGroupCount instead of a fixed 65,535, so a wide dispatch is no longer cut on a device that can run it.
  • Maintainer commit 595b1325d: a merge of main fac8b4bb9, described in the comment above. It regenerates indirect_clamp_spv.h from the merged shader and changes nothing else of the PR.
  • Tested on that merge, Mesa 26.3.1:
    • indirect_clamp_test with the validation layer on llvmpipe, the integrated RDNA2 GPU and the RX 7900 XTX (RADV), with a clean kernel log. A 67,440 × 1 × 1 record runs all its groups on both RADV devices.
    • Linux runtime ctest: 114/114. Windows suite under Wine: 488 tests, none changed against main.
    • Registration, docs, line-ending, telemetry and game-data checks pass.
  • World run (RX 7900 XTX, RADV, Wine; a local build with this merge and the counters that follow as their own PR): 22,494 dispatch records in about four minutes, the widest 28,800 groups in x, none above 65,535 and none cut. So in our runs the old bound was not being hit; the change matters for titles and scenes that issue wider dispatches.

Thanks @⁠Tyrannys, and the reviewers on this PR.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

**@⁠aslan2142** on GitHub · 2026-11-26 21:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2230#issuecomment-7203994477) Merging. - **Why:** a dispatch record's bound follows the device's `maxComputeWorkGroupCount` instead of a fixed 65,535, so a wide dispatch is no longer cut on a device that can run it. - **Maintainer commit `595b1325d`:** a merge of `main` `fac8b4bb9`, described in the comment above. It regenerates `indirect_clamp_spv.h` from the merged shader and changes nothing else of the PR. - **Tested on that merge**, Mesa 26.3.1: - `indirect_clamp_test` with the validation layer on llvmpipe, the integrated RDNA2 GPU and the RX 7900 XTX (RADV), with a clean kernel log. A 67,440 × 1 × 1 record runs all its groups on both RADV devices. - Linux runtime `ctest`: 114/114. Windows suite under Wine: 488 tests, none changed against `main`. - Registration, docs, line-ending, telemetry and game-data checks pass. - **World run** (RX 7900 XTX, RADV, Wine; a local build with this merge and the counters that follow as their own PR): 22,494 dispatch records in about four minutes, the widest 28,800 groups in x, none above 65,535 and none cut. So in our runs the old bound was not being hit; the change matters for titles and scenes that issue wider dispatches. Thanks @⁠Tyrannys, and the reviewers on this PR. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)
This discussion has been locked. Commenting is limited to contributors.
No description provided.