gpu(native), metal: refused fills land in the GPU's order, not on the… #445

Manually merged
github merged 1 commit from github-bot/gh-441 into main 2026-11-28 00:42:30 +00:00 AGit
Owner

@⁠ShivamCodesBS on GitHub · 2026-11-27 18:48 UTC · original
ShivamCodesBS:fix/cpu-fills-wait-for-earlier-passes → main · +227 −40 · 4 files · 1 commits · merged 2026-11-28 00:38 UTC

… CPU at submit

A pass submit_passes cannot encode was dropped and the host paid its fills on the CPU at submit time. That let a queue the fence releases, or a value wait the CPU polls, go on before this command buffer's earlier GPU passes had run (#80): on the console the refused pass's value write ran in the GPU's order, after the earlier passes and before the fence signals.

Now the backend re-encodes a committed submission's refused fills as a blit pass of the same command buffer (a blit fill always encodes: no attachments, no pipeline), before the fence signals; copies are not re-encoded (a copy may have partly run, its source may have changed). When a piece boundary stops later passes from being encoded (next_piece failed), their fills are attached to the last committed piece's Owed and paid from its completion handler, after that piece's GPU passes have run, failed or not. The host pays fills on the CPU only when nothing of the submission committed (no GPU work in flight).

Tests: owed_tests::fills_attached_after_a_piece_commit_are_paid_by_the_completion; vertex_tests::a_committed_failure_leaves_the_fills_to_the_backend replaces only_the_fills_of_refused_passes_are_paid_on_the_cpu, and an_uncommitted_failure_pays_every_fill_on_the_cpu covers the uncommitted path.

What this changes

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings
  • Ran the game (say how far it got), or not needed

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md
  • New diagnostics are behind a VICTORY_* variable and documented
**@⁠ShivamCodesBS** on GitHub · 2026-11-27 18:48 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/441) `ShivamCodesBS:fix/cpu-fills-wait-for-earlier-passes` → `main` · +227 −40 · 4 files · 1 commits · merged 2026-11-28 00:38 UTC … CPU at submit A pass submit_passes cannot encode was dropped and the host paid its fills on the CPU at submit time. That let a queue the fence releases, or a value wait the CPU polls, go on before this command buffer's earlier GPU passes had run (#80): on the console the refused pass's value write ran in the GPU's order, after the earlier passes and before the fence signals. Now the backend re-encodes a committed submission's refused fills as a blit pass of the same command buffer (a blit fill always encodes: no attachments, no pipeline), before the fence signals; copies are not re-encoded (a copy may have partly run, its source may have changed). When a piece boundary stops later passes from being encoded (next_piece failed), their fills are attached to the last committed piece's Owed and paid from its completion handler, after that piece's GPU passes have run, failed or not. The host pays fills on the CPU only when nothing of the submission committed (no GPU work in flight). Tests: owed_tests::fills_attached_after_a_piece_commit_are_paid_by_the_completion; vertex_tests::a_committed_failure_leaves_the_fills_to_the_backend replaces only_the_fills_of_refused_passes_are_paid_on_the_cpu, and an_uncommitted_failure_pays_every_fill_on_the_cpu covers the uncommitted path. ## What this changes <!-- One change per pull request. Name the issue you claimed for it: Refs #123 (or Closes #123). --> ## How it was tested - [ ] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory` - [ ] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` - [ ] Ran the game (say how far it got), or not needed ## Checklist - [ ] No game files, game code, disassembly listings or SDK files are included - [ ] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md - [ ] New diagnostics are behind a `VICTORY_*` variable and documented
A pass submit_passes cannot encode was dropped and the host paid its fills on
the CPU at submit time. That let a queue the fence releases, or a value wait
the CPU polls, go on before this command buffer's earlier GPU passes had run
(#80): on the console the refused pass's value write ran in the GPU's order,
after the earlier passes and before the fence signals.

Now the backend re-encodes a committed submission's refused fills as a blit
pass of the same command buffer (a blit fill always encodes: no attachments,
no pipeline), before the fence signals; copies are not re-encoded (a copy may
have partly run, its source may have changed). When a piece boundary stops
later passes from being encoded (next_piece failed), their fills are attached
to the last committed piece's Owed and paid from its completion handler,
after that piece's GPU passes have run, failed or not. The host pays fills on
the CPU only when nothing of the submission committed (no GPU work in flight).

Tests: owed_tests::fills_attached_after_a_piece_commit_are_paid_by_the_completion;
vertex_tests::a_committed_failure_leaves_the_fills_to_the_backend replaces
only_the_fills_of_refused_passes_are_paid_on_the_cpu, and
an_uncommitted_failure_pays_every_fill_on_the_cpu covers the uncommitted path.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 18:49:26 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-27 21:00 UTC · original

A bug in this PR as it stands (head e8399a9d), in crates/victory-metal/src/gfx.rs. The completion handler is now registered with cb.addCompletedHandler(&failed as *const _ as *mut _), where it was &*failed before. failed is a block2::RcBlock, so &failed passes the address of the RcBlock wrapper rather than of the block itself. Metal would then call into memory that isn't a block. It should stay &*failed.

**@⁠Olima34** on GitHub · 2026-11-27 21:00 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/441#issuecomment-7228052277) A bug in this PR as it stands (head e8399a9d), in `crates/victory-metal/src/gfx.rs`. The completion handler is now registered with `cb.addCompletedHandler(&failed as *const _ as *mut _)`, where it was `&*failed` before. `failed` is a `block2::RcBlock`, so `&failed` passes the address of the `RcBlock` wrapper rather than of the block itself. Metal would then call into memory that isn't a block. It should stay `&*failed`.
github manually merged commit 8a0bda129a into main 2026-11-28 00:42:30 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-28 00:37 UTC · original · requested changes

Thanks for this, the idea is right: a refused pass's fill belongs in the GPU's order, not on the CPU at submit time (#80). We read the whole diff (head e8399a9d) while helping the maintainer clear the queue, and two things need fixing before it can merge:

  1. crates/victory-metal/src/gfx.rs:1650 (from our earlier comment, still there): cb.addCompletedHandler(&failed as *const _ as *mut _) passes the address of the block2::RcBlock wrapper, not of the block. Metal would call into memory that isn't a block on every completed command buffer. It must stay &*failed, as on main (and as line 1509 still does). The new test fills_attached_after_a_piece_commit_are_paid_by_the_completion calls pay_refund by hand, so it doesn't reach the real handler; a test that commits a real piece and waits for its completion would have caught this.
  2. Owed::pay_refund can pay twice. It reads refund under the lock but leaves it in place. If the completion handler runs between attach_refund's store and its completed load, the handler pays, then attach_refund sees completed and pays the same fills again, possibly over a newer value the title wrote in between. Taking the vector out under the lock (std::mem::take) in pay_refund makes each fill paid exactly once; a test for "attach after completion, then a second pay_refund writes nothing" would pin it.

Also, the PR description is empty: please fill in the template (what it fixes, how you checked it). Once these are in and CI is green on the new head, we'll look again.

**@⁠Olima34** on GitHub · 2026-11-28 00:37 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/441#pullrequestreview-6094673032) · **requested changes** Thanks for this, the idea is right: a refused pass's fill belongs in the GPU's order, not on the CPU at submit time (#80). We read the whole diff (head e8399a9d) while helping the maintainer clear the queue, and two things need fixing before it can merge: 1. **`crates/victory-metal/src/gfx.rs:1650`** (from our earlier comment, still there): `cb.addCompletedHandler(&failed as *const _ as *mut _)` passes the address of the `block2::RcBlock` wrapper, not of the block. Metal would call into memory that isn't a block on every completed command buffer. It must stay `&*failed`, as on main (and as line 1509 still does). The new test `fills_attached_after_a_piece_commit_are_paid_by_the_completion` calls `pay_refund` by hand, so it doesn't reach the real handler; a test that commits a real piece and waits for its completion would have caught this. 2. **`Owed::pay_refund` can pay twice.** It reads `refund` under the lock but leaves it in place. If the completion handler runs between `attach_refund`'s store and its `completed` load, the handler pays, then `attach_refund` sees `completed` and pays the same fills again, possibly over a newer value the title wrote in between. Taking the vector out under the lock (`std::mem::take`) in `pay_refund` makes each fill paid exactly once; a test for "attach after completion, then a second `pay_refund` writes nothing" would pin it. Also, the PR description is empty: please fill in the template (what it fixes, how you checked it). Once these are in and CI is green on the new head, we'll look again.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-28 00:39 UTC · original

Thanks, this is merged. One small fix went in on top (45d90fe9): in commit_piece the block that builds the completion handler ended in a semicolon, so the handler was dropped at once and Metal was given a dangling pointer. It only showed with VICTORY_PASSES_PER_BUFFER set. The block now returns the handler and it is passed the same way submit does.

**@⁠LakshanSivakumar** on GitHub · 2026-11-28 00:39 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/441#issuecomment-7231083106) Thanks, this is merged. One small fix went in on top (45d90fe9): in commit_piece the block that builds the completion handler ended in a semicolon, so the handler was dropped at once and Metal was given a dangling pointer. It only showed with VICTORY_PASSES_PER_BUFFER set. The block now returns the handler and it is passed the same way submit does.
Author
Owner

@⁠Olima34 on GitHub · 2026-11-28 00:40 UTC · original

My change request above crossed with the maintainer merging this PR. He fixed point 1, the completion handler, himself in 45d90fe9. Point 2 may still be worth a follow-up PR against main: Owed::pay_refund leaves refund in place, so an attach_refund that races the completion handler can pay the same fills twice. Taking the vector out under the lock (std::mem::take) would make each fill paid once.

**@⁠Olima34** on GitHub · 2026-11-28 00:40 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/441#issuecomment-7231105868) My change request above crossed with the maintainer merging this PR. He fixed point 1, the completion handler, himself in 45d90fe9. Point 2 may still be worth a follow-up PR against main: `Owed::pay_refund` leaves `refund` in place, so an `attach_refund` that races the completion handler can pay the same fills twice. Taking the vector out under the lock (`std::mem::take`) would make each fill paid once.
This discussion has been locked. Commenting is limited to contributors.
No description provided.