vulkan: a counted dispatch with no count buffer ignores the count offset (RT-INDIRECT-DISPATCH-COUNT-NULL-OFFSET) #2491

Closed
leonida/mandy
Tyrannys wants to merge 0 commits from rt-indirect-dispatch-count-null-offset into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

Tyrannys commented 2026-11-27 18:43:47 +00:00

Claimed in the coordination issue as: RT-INDIRECT-DISPATCH-COUNT-NULL-OFFSET (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225617414)

What and why

Item 2 of @kevincommits' finding in #1608 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225370093). Thanks for the audit and the repro.

With LEONIDA_INDIRECT_GROUP_BUDGET set, IndirectDispatchCounter::Batch::prepare (vulkan_indirect_dispatch_count.h) accepts a dispatch batch with no count buffer. It still applied the count offset:

  • an unaligned offset was refused;
  • an aligned one became the job's count address. For example, offset 64 on a null buffer gave 0x40.

emit() then pushed that address with has_count = 1, so indirect_dispatch_count.comp read a raw GPU address. The replay (vulkan_replay.h) passed p.source.buffer_offset + ind->count_offset even for a command with no count buffer.

The fix ignores the offset without a count buffer, as IndirectArgumentClamp::prepare does:

  • prepare sets count_offset to 0 when count is null. The alignment and overflow checks then pass, and the count address stays 0, so emit() pushes has_count = 0.
  • The replay passes a null count buffer and offset 0 when ind->count_buffer is null, as prepare_indirect_clamp just above it already does.

With a count buffer nothing changes: the offset is still checked and added.

Overlap: #1863 also edits vulkan_replay.h. It already conflicts with main (labeled needs rebase); its rebase would meet this two-line change too. No conflict with #2469 or #2489.

Tested

Windows 11, MSVC (VS 18), RelWithDebInfo, on main 43a9d562d.

  • indirect_dispatch_count_alloc_test (CPU, mock driver) gains case 6, which follows the repro. It sets the budget to 4 before anything reads it.

    • With no count buffer, offsets 2, 64 and ~3 all prepare, with a count address of 0. emit() pushes count 0 and has_count 0.
    • With a count buffer, offset 2 is still refused. Offset 64 gives the buffer's address plus 64, and emit() pushes that address with has_count 1.

    With main's vulkan_indirect_dispatch_count.h the case fails on its first check (offset 2 refused). With the fix the whole test passes.

  • ctest -R "^(indirect_dispatch_count_alloc|d3d12_indirect_dispatch_count|d3d12_indirect_group_budget|d3d12_indirect_zero|indirect_clamp)$": 5/5 pass. The GPU ones run in seconds; d3d12_indirect_group_budget covers the replay path with the budget set.

  • scripts/check_no_game_data.py: clean.

The replay change has no test of its own: no CPU test drives prepare_counted_dispatch through the replay.

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

Claimed in the coordination issue as: RT-INDIRECT-DISPATCH-COUNT-NULL-OFFSET (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225617414) ### What and why Item 2 of @kevincommits' finding in #1608 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225370093). Thanks for the audit and the repro. With `LEONIDA_INDIRECT_GROUP_BUDGET` set, `IndirectDispatchCounter::Batch::prepare` (`vulkan_indirect_dispatch_count.h`) accepts a dispatch batch with no count buffer. It still applied the count offset: - an unaligned offset was refused; - an aligned one became the job's count address. For example, offset 64 on a null buffer gave `0x40`. `emit()` then pushed that address with `has_count = 1`, so `indirect_dispatch_count.comp` read a raw GPU address. The replay (`vulkan_replay.h`) passed `p.source.buffer_offset + ind->count_offset` even for a command with no count buffer. The fix ignores the offset without a count buffer, as `IndirectArgumentClamp::prepare` does: - `prepare` sets `count_offset` to 0 when `count` is null. The alignment and overflow checks then pass, and the count address stays 0, so `emit()` pushes `has_count = 0`. - The replay passes a null count buffer and offset 0 when `ind->count_buffer` is null, as `prepare_indirect_clamp` just above it already does. With a count buffer nothing changes: the offset is still checked and added. Overlap: #1863 also edits `vulkan_replay.h`. It already conflicts with `main` (labeled `needs rebase`); its rebase would meet this two-line change too. No conflict with #2469 or #2489. ### Tested Windows 11, MSVC (VS 18), RelWithDebInfo, on `main` 43a9d562d. - `indirect_dispatch_count_alloc_test` (CPU, mock driver) gains case 6, which follows the repro. It sets the budget to 4 before anything reads it. - With no count buffer, offsets 2, 64 and `~3` all prepare, with a count address of 0. `emit()` pushes count 0 and `has_count` 0. - With a count buffer, offset 2 is still refused. Offset 64 gives the buffer's address plus 64, and `emit()` pushes that address with `has_count` 1. With `main`'s `vulkan_indirect_dispatch_count.h` the case fails on its first check (offset 2 refused). With the fix the whole test passes. - `ctest -R "^(indirect_dispatch_count_alloc|d3d12_indirect_dispatch_count|d3d12_indirect_group_budget|d3d12_indirect_zero|indirect_clamp)$"`: 5/5 pass. The GPU ones run in seconds; `d3d12_indirect_group_budget` covers the replay path with the budget set. - `scripts/check_no_game_data.py`: clean. The replay change has no test of its own: no CPU test drives `prepare_counted_dispatch` through the replay. GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, 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. - [ ] 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)
lixonik approved these changes 2026-11-27 21:10:08 +00:00
lixonik left a comment

Reviewed de25d7cb9 (runtime/src/vulkan_indirect_dispatch_count.h, runtime/src/vulkan_replay.h, runtime/test/indirect_dispatch_count_alloc_test.cpp) and tested:

  • Zeroing count_offset when !count in IndirectDispatchCounter::Batch::prepare (matching IndirectArgumentClamp::prepare) and gating p.source.buffer / count_offset on ind->count_buffer in vulkan_replay.h:4148 cleanly prevents non-null count_offset from failing alignment or turning into a raw count GPU address with has_count = 1.
  • Tested on Windows 11 (MSVC 19.44): indirect_dispatch_count_alloc passed (0.03 s).

Looks good to merge.

AI-assisted (Antigravity, Gemini).

Reviewed `de25d7cb9` (`runtime/src/vulkan_indirect_dispatch_count.h`, `runtime/src/vulkan_replay.h`, `runtime/test/indirect_dispatch_count_alloc_test.cpp`) and tested: - Zeroing `count_offset` when `!count` in `IndirectDispatchCounter::Batch::prepare` (matching `IndirectArgumentClamp::prepare`) and gating `p.source.buffer` / `count_offset` on `ind->count_buffer` in `vulkan_replay.h:4148` cleanly prevents non-null `count_offset` from failing alignment or turning into a raw count GPU address with `has_count = 1`. - Tested on Windows 11 (MSVC 19.44): `indirect_dispatch_count_alloc` passed (0.03 s). Looks good to merge. AI-assisted (Antigravity, Gemini).
timbot103 commented 2026-11-28 02:01:55 +00:00

Reviewed at head de25d7cb9 on Windows 11 (MSVC 2022 19.44).

Verification

  • Built indirect_dispatch_count_alloc_test.
  • indirect_dispatch_count_alloc_test: passed.

Notes

  • Normalizing count_offset to 0 when count_buffer == VK_NULL_HANDLE before alignment/address arithmetic (vulkan_indirect_dispatch_count.h:78-88, 120-164; vulkan_replay.h:4104-4152) cleanly avoids bogus alignment rejections and passes has_count = 0 to indirect_dispatch_count.comp, while keeping full validation intact for real count buffers.

Approve.

AI-assisted review + local MSVC verification.

Reviewed at head `de25d7cb9` on Windows 11 (MSVC 2022 `19.44`). ### Verification - Built `indirect_dispatch_count_alloc_test`. - `indirect_dispatch_count_alloc_test`: **passed**. ### Notes - Normalizing `count_offset` to `0` when `count_buffer == VK_NULL_HANDLE` before alignment/address arithmetic (`vulkan_indirect_dispatch_count.h:78-88`, `120-164`; `vulkan_replay.h:4104-4152`) cleanly avoids bogus alignment rejections and passes `has_count = 0` to `indirect_dispatch_count.comp`, while keeping full validation intact for real count buffers. Approve. AI-assisted review + local MSVC verification.
zmasi commented 2026-11-28 13:12:09 +00:00

#2491 | head de25d7cb91432f753fdc378834a7679e1cc1d5cb (merged cleanly with main d48a957bb9988afce424552a3c0c8d01ef12320f) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(indirect_dispatch_count_alloc|d3d12_indirect_dispatch_count|d3d12_indirect_group_budget|d3d12_indirect_zero|indirect_clamp)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: indirect_dispatch_count_alloc fails at line 316: batch->prepare(args_buf, 0, VK_NULL_HANDLE, 2, 12, 4u, out) (rc=8) | green on PR+main: 5/5 CTests pass (indirect_dispatch_count_alloc, d3d12_indirect_dispatch_count, d3d12_indirect_group_budget 6/6 cases, d3d12_indirect_zero, indirect_clamp 0 validation errors) on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2491 | head de25d7cb91432f753fdc378834a7679e1cc1d5cb (merged cleanly with main d48a957bb9988afce424552a3c0c8d01ef12320f) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(indirect_dispatch_count_alloc|d3d12_indirect_dispatch_count|d3d12_indirect_group_budget|d3d12_indirect_zero|indirect_clamp)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: `indirect_dispatch_count_alloc` fails at `line 316: batch->prepare(args_buf, 0, VK_NULL_HANDLE, 2, 12, 4u, out)` (`rc=8`) | green on PR+main: 5/5 CTests pass (`indirect_dispatch_count_alloc`, `d3d12_indirect_dispatch_count`, `d3d12_indirect_group_budget` 6/6 cases, `d3d12_indirect_zero`, `indirect_clamp` `0` validation errors) on RTX 3090 with `0` Khronos validation errors; `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
timbot103 reviewed 2026-11-29 01:08:15 +00:00
timbot103 left a comment
No description provided.
Reviewed `de25d7cb9` and found no issues. The counted-dispatch fallback now passes a null handle and zero offset for a fixed-count dispatch. The helper also normalizes the offset before alignment and device-address arithmetic, so it emits a zero count address with `has_count = 0` and the shader skips the count load. A real count buffer still goes through replay bounds and alignment checks and the helper’s device-address overflow check. See `runtime/src/vulkan_replay.h:4104`, `runtime/src/vulkan_replay.h:4147`, `runtime/src/vulkan_indirect_dispatch_count.h:85`, and `runtime/src/indirect_dispatch_count.comp:26`. The D3D12 entry point, the legacy command-list recorder, and the shared stateful draw, indexed-draw, and mesh replay validator reject nonzero offsets paired with an absent count buffer. Existing validation tests assert this behavior. The clamp helper and draw and mesh emission guard count reads by buffer presence. See `runtime/shims/d3d12_com.cpp:1003`, `runtime/src/command_list.h:953`, `runtime/test/d3d12_execute_indirect_validation_test.cpp:198`, `runtime/test/d3d12_state_map_test.cpp:797`, `runtime/src/vulkan_replay.h:2724`, `runtime/src/vulkan_replay.h:5661`, and `runtime/src/vulkan_indirect_clamp.h:597`. The new mock test exercises unaligned, aligned, and near-limit offsets with an absent count buffer and checks the emitted count address and flag for both buffer states. Its environment setup uses `_putenv_s` on Windows and `setenv` on Linux before the first budget read. The existing GPU budget test exercises the default clamp path with a zero offset in its fixed-count cases, so the mock test provides direct coverage of the helper regression. See `runtime/test/indirect_dispatch_count_alloc_test.cpp:177`, `runtime/test/indirect_dispatch_count_alloc_test.cpp:300`, `runtime/CMakeLists.txt:3206`, and `runtime/test/d3d12_indirect_dispatch_count_test.cpp:120`. Static audit only. No builds, tests, or GPU runs performed for this review. AI-assisted.
TheGeogeo commented 2026-11-29 13:11:57 +00:00

Tested de25d7cb9 merged (no commit) with main 8fb974878: MSVC 19.52, Windows 11 10.0.26300.
GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40.

  • Green: indirect_dispatch_count_alloc passes. The 296 relinked CTests pass, except xbox_present, which fails on main too (its resize case).
  • Red: with runtime/src/vulkan_indirect_dispatch_count.h and runtime/src/vulkan_replay.h back to main, indirect_dispatch_count_alloc fails at "line 316: batch->prepare(args_buf, 0, VK_NULL_HANDLE, 2, 12, 4u, out)".
  • gta6rt links with nothing new over main: no new unresolved symbol, same imports.
  • Needs a rebase: #1910 has landed since this test, and the PR now conflicts with main 3535eb5c4 in runtime/src/vulkan_indirect_dispatch_count.h. The results above are on main 8fb974878.
  • No game data check passes.

AI-assisted (Claude Code).

Tested de25d7cb9 merged (no commit) with main 8fb974878: MSVC 19.52, Windows 11 10.0.26300. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40. - Green: `indirect_dispatch_count_alloc` passes. The 296 relinked CTests pass, except `xbox_present`, which fails on main too (its resize case). - Red: with `runtime/src/vulkan_indirect_dispatch_count.h` and `runtime/src/vulkan_replay.h` back to main, `indirect_dispatch_count_alloc` fails at "line 316: batch->prepare(args_buf, 0, VK_NULL_HANDLE, 2, 12, 4u, out)". - `gta6rt` links with nothing new over main: no new unresolved symbol, same imports. - Needs a rebase: #1910 has landed since this test, and the PR now conflicts with main 3535eb5c4 in `runtime/src/vulkan_indirect_dispatch_count.h`. The results above are on main 8fb974878. - No game data check passes. AI-assisted (Claude Code).
aslan2142 commented 2026-12-01 16:45:28 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.