metal, gpu: support non-monotonic and rewound fences over MTLSharedEvent #483

Merged
Sheffy007 merged 1 commit from upstream-pr/483 into main 2026-11-28 00:38:42 +00:00
Sheffy007 commented 2026-11-27 22:03:59 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T22:03:59Z.


What this changes

Closes #472
Refs #456, #258

MTLSharedEvent ignores any CPU setSignaledValue or GPU encodeSignalEvent at or below its current signaledValue, whereas D3D12 allows rewinding or repeating ID3D12Fence values via ID3D12Fence::Signal and ID3D12CommandQueue::Signal (for example binary 0 -> 1 -> 0 -> 1 fences).

Previously, once a fence reached a higher value, Signal(0) left its MTLSharedEvent unchanged:

  • background refreshes (glue::refresh / fence_poll) raised the title's in-memory fence word back up to the old high-water value,
  • SetEventOnCompletion and GPU ID3D12CommandQueue::Wait stopped waiting on the next cycle, and
  • repeated GPU encodeSignalEvent calls at or below signaledValue became no-ops.

Changes:

  • crates/victory-metal/src/metal.rs & crates/victory-metal/src/gfx.rs: Track MTLSharedEvent epochs on Fence. Strictly increasing signals use the active shared event in place with zero extra allocations, while CPU or GPU rewinds and repeated GPU signals start a fresh tracked MTLSharedEvent epoch (using raw value + 1 on GPU-rewound epochs so completed stays at the prior value until the rewinding command buffer finishes on the GPU) and carry unsatisfied waiters forward across epochs.
  • crates/victory-gpu/src/fence.rs, crates/victory-gpu/src/native/glue.rs, crates/victory-gpu/src/native/title.rs: Replace monotonic-only raise with sync_host (now != value), serialise fence_signal and fence_poll / refresh updates under the TITLE lock so CPU rewinds cannot race with stale polls and GPU rewinds propagate back to title memory, and remove .max(initial) in Title::fence_value.
  • Add Metal GPU tests (rewound_and_repeated_fence_signals_order_work_across_cycles) and end-to-end title fence tests (rewound_title_fences_stay_low_until_the_new_gpu_signal_completes).

How it was tested

  • cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy (399 files ok)

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

cc @Olima34

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5533683908 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/483). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T22:03:59Z. --- ## What this changes Closes #472 Refs #456, #258 `MTLSharedEvent` ignores any CPU `setSignaledValue` or GPU `encodeSignalEvent` at or below its current `signaledValue`, whereas D3D12 allows rewinding or repeating `ID3D12Fence` values via `ID3D12Fence::Signal` and `ID3D12CommandQueue::Signal` (for example binary `0 -> 1 -> 0 -> 1` fences). Previously, once a fence reached a higher value, `Signal(0)` left its `MTLSharedEvent` unchanged: - background refreshes (`glue::refresh` / `fence_poll`) raised the title's in-memory fence word back up to the old high-water value, - `SetEventOnCompletion` and GPU `ID3D12CommandQueue::Wait` stopped waiting on the next cycle, and - repeated GPU `encodeSignalEvent` calls at or below `signaledValue` became no-ops. Changes: - **`crates/victory-metal/src/metal.rs` & `crates/victory-metal/src/gfx.rs`**: Track `MTLSharedEvent` epochs on `Fence`. Strictly increasing signals use the active shared event in place with zero extra allocations, while CPU or GPU rewinds and repeated GPU signals start a fresh tracked `MTLSharedEvent` epoch (using raw `value + 1` on GPU-rewound epochs so `completed` stays at the prior value until the rewinding command buffer finishes on the GPU) and carry unsatisfied waiters forward across epochs. - **`crates/victory-gpu/src/fence.rs`, `crates/victory-gpu/src/native/glue.rs`, `crates/victory-gpu/src/native/title.rs`**: Replace monotonic-only `raise` with `sync_host` (`now != value`), serialise `fence_signal` and `fence_poll` / `refresh` updates under the `TITLE` lock so CPU rewinds cannot race with stale polls and GPU rewinds propagate back to title memory, and remove `.max(initial)` in `Title::fence_value`. - Add Metal GPU tests (`rewound_and_repeated_fence_signals_order_work_across_cycles`) and end-to-end title fence tests (`rewound_title_fences_stay_low_until_the_new_gpu_signal_completes`). ## How it was tested - [x] `cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask` - [x] `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - [x] `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - [x] `cargo xtask policy` (399 files ok) ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in `docs/native-host.md` - [x] New diagnostics are behind a `VICTORY_*` variable and documented cc @Olima34
Olima34 commented 2026-11-27 22:08:43 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T22:08:43Z.


Thanks @Sheffy007, this looks directly relevant to the stalls we've been chasing. In our world runs on the preview branch, the frame lines often show work held for a wait that does not hold, with fence signals queued seconds before the GPU reaches them. A fence that the title rewinds (0 → 1 → 0 → 1) and Metal ignores would give exactly that kind of wait that never completes. The epoch approach, rather than a single MTLSharedEvent, reads right to me.

Two notes from our side:

  • It merges cleanly with #459 (our indirect-draw gate, which also touches gfx.rs), so the two can land in either order.
  • It doesn't apply to preview/graphics-2026-11-28 as is (a conflict there), so to test it in game on the preview branch we'll port it by hand.

We'll add it to our next game run on the preview branch with #459, and post here whether the "wait that does not hold" lines and the time-outs go down.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229066636 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/483#issuecomment-7229066636). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T22:08:43Z. --- Thanks @Sheffy007, this looks directly relevant to the stalls we've been chasing. In our world runs on the preview branch, the frame lines often show `work held for a wait that does not hold`, with fence signals queued seconds before the GPU reaches them. A fence that the title rewinds (0 → 1 → 0 → 1) and Metal ignores would give exactly that kind of wait that never completes. The epoch approach, rather than a single `MTLSharedEvent`, reads right to me. Two notes from our side: - It merges cleanly with #459 (our indirect-draw gate, which also touches `gfx.rs`), so the two can land in either order. - It doesn't apply to `preview/graphics-2026-11-28` as is (a conflict there), so to test it in game on the preview branch we'll port it by hand. We'll add it to our next game run on the preview branch with #459, and post here whether the "wait that does not hold" lines and the time-outs go down.
Olima34 commented 2026-11-28 01:20:23 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T01:20:23Z.


@Sheffy007 a follow-up from our test runs, now that this is merged. GTA VI never signals a fence at or below its last value, at least in this scenario: 0 of more than 4,096 signals (CPU ID3D12Fence::Signal and queue Signal together) in a 300 s windowed world run, M5 Pro, Series S. So there's nothing for the epoch path to catch in this title yet. It's harmless here: 5 runs with it, 0 GPU errors, 0 time-outs, clean ends. The few "wait that does not hold" lines we see are late advances of a GPU about 2 s behind ("signal of 4 queued 2.1s ago, GPU at 3"), 0 or 1 a run with or without #483. Still a good guard to have for titles that do rewind.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7231594495 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/483#issuecomment-7231594495). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T01:20:23Z. --- @Sheffy007 a follow-up from our test runs, now that this is merged. GTA VI never signals a fence at or below its last value, at least in this scenario: 0 of more than 4,096 signals (CPU `ID3D12Fence::Signal` and queue `Signal` together) in a 300 s windowed world run, M5 Pro, Series S. So there's nothing for the epoch path to catch in this title yet. It's harmless here: 5 runs with it, 0 GPU errors, 0 time-outs, clean ends. The few "wait that does not hold" lines we see are late advances of a GPU about 2 s behind ("signal of 4 queued 2.1s ago, GPU at 3"), 0 or 1 a run with or without #483. Still a good guard to have for titles that do rewind.
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!483
No description provided.