Skip to content

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

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-indirect-dispatch-group-limit
Nov 26, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-indirect-dispatch-group-limit

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

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

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 test: bind a PSO before direct heap dispatch #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

…GroupCount

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 <noreply@anthropic.com>
@real-rusher

Copy link
Copy Markdown
Contributor

Verified on main a73a57e 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)

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kawaii-shakal kawaii-shakal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 7a76169: 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

…dispatch limit and ProjectLeonida#2378's unfilled draw records

main's ProjectLeonida#2378 (a draw record of 2^23 vertices or more runs empty and charges nothing) changed indirect_clamp.comp
next to this branch's dispatch bound. The .comp, vulkan_indirect_clamp.h and indirect_clamp_test.cpp merged without
conflicts and keep both changes. The only conflict was the generated indirect_clamp_spv.h: it is regenerated from the
merged indirect_clamp.comp as its header comment says (glslangValidator 16.6.0, -V --target-env vulkan1.2 -Os
--spirv-val; spirv-val passes).

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

Copy link
Copy Markdown
Collaborator

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 (fac8b4b) contains indirect clamp: a draw record of 2^23 vertices or more runs empty and free (RT-INDIRECT-DRAW-UNFILLED-COUNT) #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 fac8b4b.
  • 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

Copy link
Copy Markdown
Collaborator

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
aslan2142 merged commit 55e2bd9 into ProjectLeonida:main Nov 26, 2026
2 of 5 checks passed
aslan2142 added a commit that referenced this pull request Nov 26, 2026
…RECT-DISPATCH-COUNTERS)

#2230 bounds a dispatch record by the device's maxComputeWorkGroupCount instead of 65 535. Nothing said how wide
the title's dispatches are, nor when a device's own limit still cuts one (llvmpipe allows 65 535 in x; RADV 2^32 - 1),
which its review asked for. The title's tile passes ask for 67 440 groups in x at 2560x1440 (found in the
VIctory-Belobug-AMD fork of VIctory, crates/victory-gpu/src/indirect.rs; no code of it is used).

- indirect_clamp.comp: stats words 12-15 count dispatch records that run wider than 65 535 groups in x, the widest
  x that runs, those cut at the device's limits (and still running), and all of them.
- vulkan_indirect_clamp.h: the counters, read with the others (16 words a slot, words 8-15 zeroed before the pass),
  and a line every 10 s while dispatch records are clamped: `indirect clamp dispatch: N records, W wider than 65535
  groups in x, widest x X, C cut at the device's limits (x x y x z)`.
- indirect_clamp_spv.h regenerated from the .comp as its header says (glslangValidator 16.6.0).
- docs/d3d12_vulkan.md section 4.7: the device's bound per dimension (it still said 65 535), the line, and the test.

Tests: indirect_clamp_test's cases 6b and 6c check the counters (70 000 x 1 x 1: wider than 65 535 where the device
runs it, cut at its limit and counted where not). tests/test_indirect_clamp_telemetry.py checks the new atomics.

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

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants