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

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

@⁠Sheffy007 on GitHub · 2026-11-27 22:03 UTC · original
Sheffy007:fix/non-monotonic-metal-fences → main · +411 −30 · 6 files · 1 commits · merged 2026-11-28 00:38 UTC

What this changes

Closes #476
Refs #460, #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

**@⁠Sheffy007** on GitHub · 2026-11-27 22:03 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/483) `Sheffy007:fix/non-monotonic-metal-fences` → `main` · +411 −30 · 6 files · 1 commits · merged 2026-11-28 00:38 UTC ## What this changes Closes #476 Refs #460, #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
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, rewinding a fence to 0 left its MTLSharedEvent at the old
high-water value, so background refreshes raised the title's fence word
back up, SetEventOnCompletion and GPU Queue::Wait stopped waiting, and
repeated GPU signals became no-ops.

Track MTLSharedEvent epochs per Fence so monotonic advances use the
existing shared event in place while CPU or GPU rewinds and repeated GPU
signals start a fresh tracked epoch, carrying unsatisfied waiters across
epochs. Serialise CPU fence_signal and host fence_poll/refresh updates
under the title lock and propagate lowered host values via sync_host.

Closes #472
Refs #456, #258
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 22:05:18 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-27 22:08 UTC · original

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 #463 (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 #463, and post here whether the "wait that does not hold" lines and the time-outs go down.

**@⁠Olima34** on GitHub · 2026-11-27 22:08 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/483#issuecomment-7229066636) 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 #463 (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 #463, and post here whether the "wait that does not hold" lines and the time-outs go down.
github-bot referenced this pull request from a commit 2026-11-28 00:42:29 +00:00
github manually merged commit 972f2934d0 into main 2026-11-28 00:42:32 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-28 01:20 UTC · original

@⁠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 #487. Still a good guard to have for titles that do rewind.

**@⁠Olima34** on GitHub · 2026-11-28 01:20 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/483#issuecomment-7231594495) @⁠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 #487. Still a good guard to have for titles that do rewind.
This discussion has been locked. Commenting is limited to contributors.
No description provided.