metal: a submission's passes go in command buffers of at most 8 passes #296

Manually merged
github merged 2 commits from github-bot/gh-296 into main 2026-11-27 13:38:56 +00:00 AGit
Owner

@⁠Tarraga on GitHub · 2026-11-27 09:16 UTC · original
Tarraga:metal-passes-per-buffer → main · +296 −13 · 3 files · 2 commits · merged 2026-11-27 13:35 UTC

Refs #79 (the command-buffer time-out piece only)

Problem

macOS stops a command buffer that runs too long on the GPU (kIOGPUCommandBufferCallbackErrorTimeout). submit_passes puts a whole submission in one command buffer. With the ray-traced passes running, a world submission has 72 to 80 passes and took 6 to 21 s, so it timed out again and again. No pass alone took more than 1.4 s when each one was timed in a command buffer of its own. The watchdog stops a command buffer, not a pass.

Evidence

All on an M4 Pro, 48 GB, Series S profile, macOS 27, heap residency sets on.

  • One command buffer per submission: 13 time-outs in 600 s of one run.
  • Command buffers of at most 8 passes, residency sets attached to the first piece only: no time-out in three runs of 490 to 800 s. But every run then had a GPU page fault (kIOGPUCommandBufferCallbackErrorPageFault) 90 to 135 s in, in the world's later passes (tiled cascade shadows, final gather). The fault went away with every submission waited for on the CPU, so it was work in flight. A residency set keeps its allocations resident only for the command buffers it is attached to, and pieces 2 and later had none.
  • With the sets attached to every piece (this PR): no page fault in two runs of 307 and 339 s.
  • Not always enough: in the 339 s run, one piece of 8 passes took 6.3 s and timed out (11 time-outs at 85 to 108 s). A run with one pass per command buffer had no GPU error in 337 s. That is why the limit is a switch.

Fix

submit_passes (victory-metal, gfx.rs) commits a submission's passes in command buffers of at most VICTORY_PASSES_PER_BUFFER passes (default 8; 0 puts the whole submission in one command buffer, as before).

  • The earlier pieces are committed as they fill.
  • Each new piece comes from Metal::next_piece. It gets the submission's waits again (values already reached, since the host keeps unsatisfied waits on the CPU), and attached_heaps is emptied so the heaps' residency sets are attached again.
  • Encoders stay in order across pieces through the order fence where it is in play, and through Metal's tracking otherwise.
  • The last piece carries the fence signals and what the submission owes if it fails (Owed). A failed earlier piece is logged the same way as the last one.

Guess: the default of 8 comes from one machine. A slower GPU, or a heavier pass, may need fewer. 1 is the safe value, at the cost of more command buffers. This is noted in docs/native-host.md, "Command buffers of at most 8 passes".

Test

  • gfx::owed_tests::a_long_submission_is_cut_into_command_buffers_of_at_most_n_passes: 21 blit passes with a limit of 8 go in 3 command buffers. Every pass runs, in order (the last one overwrites the first one's bytes), and the last piece signals the fence. With 0 they go in one. Fails without the split.
  • gfx::heap_set_tests::the_next_piece_of_a_split_submission_attaches_the_heaps_again: a heap's set is attached to the second piece too. Fails if next_piece does not empty attached_heaps.
  • cargo test -p victory-metal (x86_64, with D3DMetal's libmetalirconverter as VICTORY_MSC_DYLIB): all pass.
  • cargo test -p victory-gpu --lib (arm64): 315 pass. texture_tests::described_rectangle_list_draw_uses_supplied_triangles fails on this Mac with or without this change, as on main.
  • cargo xtask policy passes. Windows clippy is not installed here, so I'm leaving it to CI.
**@⁠Tarraga** on GitHub · 2026-11-27 09:16 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/296) `Tarraga:metal-passes-per-buffer` → `main` · +296 −13 · 3 files · 2 commits · merged 2026-11-27 13:35 UTC Refs #79 (the command-buffer time-out piece only) ## Problem macOS stops a command buffer that runs too long on the GPU (`kIOGPUCommandBufferCallbackErrorTimeout`). `submit_passes` puts a whole submission in one command buffer. With the ray-traced passes running, a world submission has 72 to 80 passes and took 6 to 21 s, so it timed out again and again. No pass alone took more than 1.4 s when each one was timed in a command buffer of its own. The watchdog stops a command buffer, not a pass. ## Evidence All on an M4 Pro, 48 GB, Series S profile, macOS 27, heap residency sets on. - One command buffer per submission: 13 time-outs in 600 s of one run. - Command buffers of at most 8 passes, residency sets attached to the first piece only: no time-out in three runs of 490 to 800 s. But every run then had a GPU page fault (`kIOGPUCommandBufferCallbackErrorPageFault`) 90 to 135 s in, in the world's later passes (tiled cascade shadows, final gather). The fault went away with every submission waited for on the CPU, so it was work in flight. A residency set keeps its allocations resident only for the command buffers it is attached to, and pieces 2 and later had none. - With the sets attached to every piece (this PR): no page fault in two runs of 307 and 339 s. - Not always enough: in the 339 s run, one piece of 8 passes took 6.3 s and timed out (11 time-outs at 85 to 108 s). A run with one pass per command buffer had no GPU error in 337 s. That is why the limit is a switch. ## Fix `submit_passes` (`victory-metal`, `gfx.rs`) commits a submission's passes in command buffers of at most `VICTORY_PASSES_PER_BUFFER` passes (default 8; 0 puts the whole submission in one command buffer, as before). - The earlier pieces are committed as they fill. - Each new piece comes from `Metal::next_piece`. It gets the submission's waits again (values already reached, since the host keeps unsatisfied waits on the CPU), and `attached_heaps` is emptied so the heaps' residency sets are attached again. - Encoders stay in order across pieces through the order fence where it is in play, and through Metal's tracking otherwise. - The last piece carries the fence signals and what the submission owes if it fails (`Owed`). A failed earlier piece is logged the same way as the last one. Guess: the default of 8 comes from one machine. A slower GPU, or a heavier pass, may need fewer. 1 is the safe value, at the cost of more command buffers. This is noted in `docs/native-host.md`, "Command buffers of at most 8 passes". ## Test - `gfx::owed_tests::a_long_submission_is_cut_into_command_buffers_of_at_most_n_passes`: 21 blit passes with a limit of 8 go in 3 command buffers. Every pass runs, in order (the last one overwrites the first one's bytes), and the last piece signals the fence. With 0 they go in one. Fails without the split. - `gfx::heap_set_tests::the_next_piece_of_a_split_submission_attaches_the_heaps_again`: a heap's set is attached to the second piece too. Fails if `next_piece` does not empty `attached_heaps`. - `cargo test -p victory-metal` (x86_64, with D3DMetal's `libmetalirconverter` as `VICTORY_MSC_DYLIB`): all pass. - `cargo test -p victory-gpu --lib` (arm64): 315 pass. `texture_tests::described_rectangle_list_draw_uses_supplied_triangles` fails on this Mac with or without this change, as on main. - `cargo xtask policy` passes. Windows clippy is not installed here, so I'm leaving it to CI.
macOS stops a command buffer that runs too long on the GPU
(kIOGPUCommandBufferCallbackErrorTimeout). With the ray-traced passes on, a
world submission of 72 to 80 passes ran 6 to 21 s and timed out again and
again, while none of its passes alone took more than 1.4 s. submit_passes
now commits the passes in pieces of VICTORY_PASSES_PER_BUFFER (default 8,
0 for one command buffer as before).

Each piece is a command buffer of its own (Metal::next_piece): it gets the
submission's waits again and attaches the heaps' residency sets again, as a
residency set keeps its allocations resident only for the command buffers
it is given to. The last piece carries the signals and what the submission
owes if it fails; a failed earlier piece is logged.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 10:37:24 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 10:32 UTC · original

This is a draft, so only a first look. The evidence is clear: the time-outs, and the page faults until every piece gets the residency sets again.

Two questions. The default of 8 is on for everyone and comes from one Mac, and your own run has one piece of 8 passes that took 6.3 s. I would keep the default at 0 (off) until two other Macs, not an M4 Pro, have run it. Or please say why 8 is safe on a slower GPU. And when an earlier piece fails, do the later pieces still run, and does the submission still pay what it owes only once? Please add a test for that. I have not run it; your workflow needs my approval when it is ready.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 10:32 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/296#issuecomment-7216009861) This is a draft, so only a first look. The evidence is clear: the time-outs, and the page faults until every piece gets the residency sets again. Two questions. The default of 8 is on for everyone and comes from one Mac, and your own run has one piece of 8 passes that took 6.3 s. I would keep the default at 0 (off) until two other Macs, not an M4 Pro, have run it. Or please say why 8 is safe on a slower GPU. And when an earlier piece fails, do the later pieces still run, and does the submission still pay what it owes only once? Please add a test for that. I have not run it; your workflow needs my approval when it is ready.
github-bot force-pushed github-bot/gh-296 from ea4d35cc9c to 391573267e 2026-11-27 11:17:49 +00:00 Compare
Author
Owner

@⁠Tarraga on GitHub · 2026-11-27 11:14 UTC · original

Thanks. I've rebased on main and pushed two changes.

  1. Default. It is now 0 (off): the whole submission goes in one command buffer, as before. The switch stays (VICTORY_PASSES_PER_BUFFER=<n>). I can't argue that 8 is safe on a slower GPU, so I've stopped claiming it. native-host.md now says the split has run on one Mac only, and gives 1 as the safest value to try. On that same M4 Pro, one run had a piece of 8 passes take 6.3 s and time out (11 time-outs in 339 s). With one pass per command buffer, a run had no GPU error in 337 s.

  2. When an earlier piece fails. It didn't fully hold, so I've fixed the code. The later pieces did still run, since every piece was committed before any completed. But a failed earlier piece was only logged: its value writes were never paid, while the last piece still signalled the fence. Now what a submission owes is split among its pieces:

    • Each piece owes the value writes of its own passes. It ends by raising a per-submission piece event to its number, and the next piece waits for that value.
    • The last piece also owes the fence signals and the order event.
    • A piece the GPU fails pays its own share on the CPU, once (Owed: the writes, then the event). The next piece runs only after those writes. The later pieces' writes and signals come from the GPU, and nothing is paid twice.
    • Each piece is also in flight on its own, so recover_lost covers a piece that Metal never reports on. Paying that piece also lets the next one go.
    • Test: owed_tests::a_failed_earlier_piece_pays_its_own_writes_and_the_later_pieces_still_run. It runs 12 passes in pieces of 4 and drops the first piece's command buffer, as the GPU would fail it. That is a test-only hook: I found no safe way to make the GPU fail on purpose in a test. The test checks that the failed piece's words are written, that the later pieces ran and the fence is reached, and that a word written after the fence is not written over. Before this change, it fails at the first piece's words.
    • One guess, written down in native-host.md: a failed command buffer does not run its last event signal. If it did, the next piece could start before the CPU has written the failed piece's values.

The default is now off, so none of this changes anything unless the switch is set.

Tests (one Mac): victory-metal on x86_64 with D3DMetal's converter, all pass (36 unit, 12 render, 14 resources). victory-gpu on arm64: 317 passed and 2 failed. The two failures (described_rectangle_list_draw_uses_supplied_triangles, an_indirect_dispatch_mesh_x_draws_the_groups_its_record_holds) fail the same way on main here, because this Mac has no arm64 shader converter. cargo xtask policy passes. I've marked it ready for review. The workflow still needs your approval.

**@⁠Tarraga** on GitHub · 2026-11-27 11:14 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/296#issuecomment-7216842688) Thanks. I've rebased on main and pushed two changes. 1. Default. It is now 0 (off): the whole submission goes in one command buffer, as before. The switch stays (`VICTORY_PASSES_PER_BUFFER=<n>`). I can't argue that 8 is safe on a slower GPU, so I've stopped claiming it. native-host.md now says the split has run on one Mac only, and gives 1 as the safest value to try. On that same M4 Pro, one run had a piece of 8 passes take 6.3 s and time out (11 time-outs in 339 s). With one pass per command buffer, a run had no GPU error in 337 s. 2. When an earlier piece fails. It didn't fully hold, so I've fixed the code. The later pieces did still run, since every piece was committed before any completed. But a failed earlier piece was only logged: its value writes were never paid, while the last piece still signalled the fence. Now what a submission owes is split among its pieces: - Each piece owes the value writes of its own passes. It ends by raising a per-submission piece event to its number, and the next piece waits for that value. - The last piece also owes the fence signals and the order event. - A piece the GPU fails pays its own share on the CPU, once (`Owed`: the writes, then the event). The next piece runs only after those writes. The later pieces' writes and signals come from the GPU, and nothing is paid twice. - Each piece is also in flight on its own, so `recover_lost` covers a piece that Metal never reports on. Paying that piece also lets the next one go. - Test: `owed_tests::a_failed_earlier_piece_pays_its_own_writes_and_the_later_pieces_still_run`. It runs 12 passes in pieces of 4 and drops the first piece's command buffer, as the GPU would fail it. That is a test-only hook: I found no safe way to make the GPU fail on purpose in a test. The test checks that the failed piece's words are written, that the later pieces ran and the fence is reached, and that a word written after the fence is not written over. Before this change, it fails at the first piece's words. - One guess, written down in native-host.md: a failed command buffer does not run its last event signal. If it did, the next piece could start before the CPU has written the failed piece's values. The default is now off, so none of this changes anything unless the switch is set. Tests (one Mac): victory-metal on x86_64 with D3DMetal's converter, all pass (36 unit, 12 render, 14 resources). victory-gpu on arm64: 317 passed and 2 failed. The two failures (`described_rectangle_list_draw_uses_supplied_triangles`, `an_indirect_dispatch_mesh_x_draws_the_groups_its_record_holds`) fail the same way on main here, because this Mac has no arm64 shader converter. `cargo xtask policy` passes. I've marked it ready for review. The workflow still needs your approval.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 11:54 UTC · original

Thank you for turning the default off and for fixing the case where an earlier piece fails; the test for it is what I asked for.

I have not run it. I will run it with the switch off and compare against main (it should be the same) and then with VICTORY_PASSES_PER_BUFFER=1. Please tell me how I check that, with the switch off, the code takes exactly the old path (one command buffer, the same fence signals). If someone with 16 GB can try =1, that is the data point we are missing.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 11:54 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/296#issuecomment-7217613963) Thank you for turning the default off and for fixing the case where an earlier piece fails; the test for it is what I asked for. I have not run it. I will run it with the switch off and compare against main (it should be the same) and then with `VICTORY_PASSES_PER_BUFFER=1`. Please tell me how I check that, with the switch off, the code takes exactly the old path (one command buffer, the same fence signals). If someone with 16 GB can try `=1`, that is the data point we are missing.
github manually merged commit 27a7065576 into main 2026-11-27 13:38:56 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.