Skip to content

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

Open
Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-indirect-dispatch-count-null-offset
Open

Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-indirect-dispatch-count-null-offset

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-INDIRECT-DISPATCH-COUNT-NULL-OFFSET (#1608 (comment))

What and why

Item 2 of @kevincommits' finding in #1608 (#1608 (comment)). 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 43a9d56.

  • 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

With LEONIDA_INDIRECT_GROUP_BUDGET, IndirectDispatchCounter::Batch::prepare accepts a dispatch batch
that has no count buffer. It still applied the count offset: an unaligned offset was refused, and an
aligned one became the job's count address (0x40 for offset 64 on a null buffer). emit() then pushed
that address with has_count 1, so the shader read memory at a raw GPU address.

prepare now zeroes the offset when there is no count buffer, as IndirectArgumentClamp::prepare
ignores it. The replay passes a null count buffer and offset 0 when the command has no count buffer,
as it already does for prepare_indirect_clamp.

From kevincommits' finding in ProjectLeonida#1608 (item 2). indirect_dispatch_count_alloc_test gains a case that
follows the repro.

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

@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.

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

Copy link
Copy Markdown
Contributor

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2491 | head de25d7c (merged cleanly with main d48a957) | 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 timbot103 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 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.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 29, 2026
@TheGeogeo

Copy link
Copy Markdown
Contributor

Tested de25d7c merged (no commit) with main 8fb9748: 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: runtime: cache helper Vulkan device procs, fix uncounted BDA fault and BAR/cleanup guards (RT-VK-HELPER-PROC-CACHE-AND-GUARDS) #1910 has landed since this test, and the PR now conflicts with main 3535eb5 in runtime/src/vulkan_indirect_dispatch_count.h. The results above are on main 8fb9748.
  • No game data check passes.

AI-assisted (Claude Code).

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