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

Merged
Olima34 merged 1 commit from upstream-pr/116 into main 2026-11-26 18:09:23 +00:00
Olima34 commented 2026-11-26 17:15:43 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T17:15:43Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6631755890 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/116). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T17:15:43Z. --- 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 commented 2026-11-26 17:31:39 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T17:31:39Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7199169482 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/116#issuecomment-7199169482). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T17:31:39Z. --- 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.
LakshanSivakumar commented 2026-11-26 18:09:47 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T18:09:47Z.


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

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7200125325 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/116#issuecomment-7200125325). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T18:09:47Z. --- Merged, thanks. The per-record gate is a clean fit, and the count tests pass on Metal.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!116
No description provided.