metal, gpu: support non-monotonic and rewound fences over MTLSharedEvent #483
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/483"
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 Sheffy007 on 2026-11-27T22:03:59Z.
What this changes
Closes #472
Refs #456, #258
MTLSharedEventignores any CPUsetSignaledValueor GPUencodeSignalEventat or below its currentsignaledValue, whereas D3D12 allows rewinding or repeatingID3D12Fencevalues viaID3D12Fence::SignalandID3D12CommandQueue::Signal(for example binary0 -> 1 -> 0 -> 1fences).Previously, once a fence reached a higher value,
Signal(0)left itsMTLSharedEventunchanged:glue::refresh/fence_poll) raised the title's in-memory fence word back up to the old high-water value,SetEventOnCompletionand GPUID3D12CommandQueue::Waitstopped waiting on the next cycle, andencodeSignalEventcalls at or belowsignaledValuebecame no-ops.Changes:
crates/victory-metal/src/metal.rs&crates/victory-metal/src/gfx.rs: TrackMTLSharedEventepochs onFence. 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 trackedMTLSharedEventepoch (using rawvalue + 1on GPU-rewound epochs socompletedstays 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-onlyraisewithsync_host(now != value), serialisefence_signalandfence_poll/refreshupdates under theTITLElock so CPU rewinds cannot race with stale polls and GPU rewinds propagate back to title memory, and remove.max(initial)inTitle::fence_value.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 xtaskcargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warningscargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warningscargo xtask policy(399 files ok)Checklist
docs/native-host.mdVICTORY_*variable and documentedcc @Olima34
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 singleMTLSharedEvent, reads right to me.Two notes from our side:
gfx.rs), so the two can land in either order.preview/graphics-2026-11-28as 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.
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::Signaland queueSignaltogether) 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.