metal: a submission's passes go in command buffers of at most 8 passes #296
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/296"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Tarraga on 2026-11-27T09:16:38Z.
Refs #79 (the command-buffer time-out piece only)
Problem
macOS stops a command buffer that runs too long on the GPU (
kIOGPUCommandBufferCallbackErrorTimeout).submit_passesputs 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.
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.Fix
submit_passes(victory-metal,gfx.rs) commits a submission's passes in command buffers of at mostVICTORY_PASSES_PER_BUFFERpasses (default 8; 0 puts the whole submission in one command buffer, as before).Metal::next_piece. It gets the submission's waits again (values already reached, since the host keeps unsatisfied waits on the CPU), andattached_heapsis emptied so the heaps' residency sets are attached again.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 ifnext_piecedoes not emptyattached_heaps.cargo test -p victory-metal(x86_64, with D3DMetal'slibmetalirconverterasVICTORY_MSC_DYLIB): all pass.cargo test -p victory-gpu --lib(arm64): 315 pass.texture_tests::described_rectangle_list_draw_uses_supplied_trianglesfails on this Mac with or without this change, as on main.cargo xtask policypasses. Windows clippy is not installed here, so I'm leaving it to CI.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:32:34Z.
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.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Tarraga on 2026-11-27T11:14:23Z.
Thanks. I've rebased on main and pushed two changes.
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.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:
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.recover_lostcovers a piece that Metal never reports on. Paying that piece also lets the next one go.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.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 policypasses. I've marked it ready for review. The workflow still needs your approval.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T11:54:32Z.
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.