native: run ExecuteIndirect with a count buffer, gated on the GPU #116

Manually merged
github merged 1 commit from github-bot/gh-116 into main 2026-11-26 18:09:26 +00:00 AGit
Owner

@⁠Olima34 on GitHub · 2026-11-26 17:15 UTC · original
Olima34:feat/native-indirect-count-buffer → main · +410 −27 · 13 files · 1 commits · merged 2026-11-26 18:09 UTC

Takes "count buffers for ExecuteIndirect" from your free-to-take list, on top of 8765326.

Why. D3D12 runs min(*count, MaxCommandCount) records, with the count read on the GPU when the work runs. A culling pass usually writes it earlier in the same submission. The native host dropped every ExecuteIndirect with a count buffer.

What.

  • Each record still becomes one indirect draw or dispatch, which also names the count's title address and its own index (NativeList::dispatch_indirect_counted, draw_indirect_counted). The count is resolved like the record, so a count at unmapped memory drops only that command (as #102 does).
  • In victory-metal, a one-thread kernel (gfx.rs, GATE) runs before each counted record. It copies the record into a small Metal-tracked buffer, or writes zeros there when the index is not below the count. The indirect command reads that buffer, so a zeroed record dispatches no groups and draws nothing.
  • Dispatches are gated in their own encoder between buffer memory barriers. Draws are gated in a compute pass just before their render pass, ordered by the same fence. Because the gate buffer is tracked, the render pass waits for its write.
  • count_offset is applied on both the macOS and Windows paths.

Why not an indirect command buffer. Encoding the converted shaders' argument-buffer bindings into an ICB is much larger than one gate per record.

Known limit (marked shortcut: in the code). There is one gate dispatch and one small buffer per record. If a large MaxCommandCount shows up in profiles, all records of one ExecuteIndirect can be gated in a single dispatch.

Tests.

  • victory-metal: a_count_buffer_limits_the_indirect_draws_that_run, a_count_buffer_limits_the_indirect_dispatches_that_run.
  • victory-gpu: a_count_written_earlier_on_the_gpu_limits_the_indirect_dispatches (the count is written by a copy earlier in the same list). It fails when the gate ignores the count.

victory-gpu (arm64) and victory-metal tests, Windows clippy and cargo xtask policy pass. Not checked in game.

**@⁠Olima34** on GitHub · 2026-11-26 17:15 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/116) `Olima34:feat/native-indirect-count-buffer` → `main` · +410 −27 · 13 files · 1 commits · merged 2026-11-26 18:09 UTC Takes "count buffers for ExecuteIndirect" from your free-to-take list, on top of 8765326. **Why.** D3D12 runs `min(*count, MaxCommandCount)` records, with the count read on the GPU when the work runs. A culling pass usually writes it earlier in the same submission. The native host dropped every ExecuteIndirect with a count buffer. **What.** - Each record still becomes one indirect draw or dispatch, which also names the count's title address and its own index (`NativeList::dispatch_indirect_counted`, `draw_indirect_counted`). The count is resolved like the record, so a count at unmapped memory drops only that command (as #102 does). - In victory-metal, a one-thread kernel (`gfx.rs`, `GATE`) runs before each counted record. It copies the record into a small Metal-tracked buffer, or writes zeros there when the index is not below the count. The indirect command reads that buffer, so a zeroed record dispatches no groups and draws nothing. - Dispatches are gated in their own encoder between buffer memory barriers. Draws are gated in a compute pass just before their render pass, ordered by the same fence. Because the gate buffer is tracked, the render pass waits for its write. - `count_offset` is applied on both the macOS and Windows paths. **Why not an indirect command buffer.** Encoding the converted shaders' argument-buffer bindings into an ICB is much larger than one gate per record. **Known limit (marked `shortcut:` in the code).** There is one gate dispatch and one small buffer per record. If a large `MaxCommandCount` shows up in profiles, all records of one ExecuteIndirect can be gated in a single dispatch. **Tests.** - victory-metal: `a_count_buffer_limits_the_indirect_draws_that_run`, `a_count_buffer_limits_the_indirect_dispatches_that_run`. - victory-gpu: `a_count_written_earlier_on_the_gpu_limits_the_indirect_dispatches` (the count is written by a copy earlier in the same list). It fails when the gate ignores the count. victory-gpu (arm64) and victory-metal tests, Windows clippy and `cargo xtask policy` pass. Not checked in game.
D3D12 runs min(count, max) records, the count read on the GPU when the
work runs; the native host dropped every ExecuteIndirect with a count
buffer. Each record still becomes one indirect draw or dispatch, which
now also names the count's title address and its own index. Before it
runs, victory-metal encodes a one-thread kernel that copies the record
into a small Metal-tracked buffer, or writes zeros there when the index
is not below the count, and the indirect command reads that buffer: a
zeroed record dispatches no groups and draws nothing. Dispatches are
gated in their own encoder between memory barriers, draws in a compute
pass just before their render pass, ordered by the same fence, so a
count written earlier in the same submission is honoured.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:21:13 +00:00
github-bot force-pushed github-bot/gh-116 from 35b1fa6d67 to c8f2f0722b 2026-11-26 17:32:24 +00:00 Compare
Author
Owner

@⁠Olima34 on GitHub · 2026-11-26 17:31 UTC · original

Rebased on main for ce35cbc (metal.rs: kept both the new failure-trace fields and the gate pipeline). victory-gpu and victory-metal tests, Windows clippy and policy pass.

**@⁠Olima34** on GitHub · 2026-11-26 17:31 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/116#issuecomment-7199169482) Rebased on main for ce35cbc (metal.rs: kept both the new failure-trace fields and the gate pipeline). victory-gpu and victory-metal tests, Windows clippy and policy pass.
github manually merged commit 6894146e32 into main 2026-11-26 18:09:26 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 18:09 UTC · original

Merged, thanks. The per-record gate is a clean fit, and the count tests pass on Metal.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 18:09 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/116#issuecomment-7200125325) Merged, thanks. The per-record gate is a clean fit, and the count tests pass on Metal.
This discussion has been locked. Commenting is limited to contributors.
No description provided.