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 ubiquitarianism#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>
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.

1 participant