metal, gpu: support non-monotonic and rewound ID3D12Fence signals over MTLSharedEvent #472

Closed
opened 2026-11-27 21:42:50 +00:00 by Sheffy007 · 2 comments
Sheffy007 commented 2026-11-27 21:42:50 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T21:42:50Z.


Refs #456, #258

Problem

ID3D12Fence::Signal and ID3D12CommandQueue::Signal allow setting a fence to any u64 value, including lowering/rewinding a fence (such as resetting a reusable binary fence to 0 before signalling 1 again) or repeating a value. On macOS, however, victory-metal maps a fence directly onto a single MTLSharedEvent value:

  1. MTLSharedEvent.signaledValue is strictly monotonic in Metal: setSignaledValue(v) and encodeSignalEvent(_, v) silently ignore any v <= signaledValue.
  2. After a fence reaches 1 once and the title resets it via ID3D12Fence::Signal(0), MTLSharedEvent.signaledValue remains 1. Within 1 ms (glue::refresh -> Title::completed -> fence::raise) or on the next GetCompletedValue() / SetEventOnCompletion(1, ...), the stale 1 overwrites the fence back to 1 and fires completion events while new GPU work is still in flight, and ID3D12CommandQueue::Wait(fence, 1) skips waiting altogether.
  3. Subsequent ID3D12CommandQueue::Signal(fence, 1) calls encode encodeSignalEvent(_, 1) on an event already at 1, becoming GPU no-ops instead of waiting for the command buffer to finish.
  4. Title::fence_value clamps completed(&f).max(initial) (preventing lowering below initial), and fence::completed / set_event_on_completion poll outside the waiters lock before calling raise, allowing a concurrent ID3D12Fence::Signal(0) to be clobbered by a stale poll result or ignoring a GPU-side Queue::Signal(fence, 0).

Proposed Fix

  • Track a strictly increasing MTLSharedEvent high-water ticket alongside the logical D3D12 (epoch, current) value and in-flight (raw_ticket, d3d_value) GPU signals inside victory_metal::metal::Fence so MTLSharedEvent always advances monotonically while completed, signal, wait, encodeWaitForEvent, and encodeSignalEvent observe exact D3D12 values across rewinds and repeated signals.
  • Order crates/victory-gpu/src/fence.rs updates by fence epoch under the waiters lock and remove the .max(initial) clamp in Title::fence_value so neither stale polls nor initial overwrite a CPU or GPU rewind.
  • Add Metal backend and native title tests covering binary/rewound Signal(0) -> Queue::Signal(1) cycles, Queue::Wait across rewinds, SetEventOnCompletion, and GPU-side fence rewinds.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6654901009 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/472). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T21:42:50Z. --- Refs #456, #258 ## Problem `ID3D12Fence::Signal` and `ID3D12CommandQueue::Signal` allow setting a fence to any `u64` value, including lowering/rewinding a fence (such as resetting a reusable binary fence to `0` before signalling `1` again) or repeating a value. On macOS, however, `victory-metal` maps a fence directly onto a single `MTLSharedEvent` value: 1. `MTLSharedEvent.signaledValue` is strictly monotonic in Metal: `setSignaledValue(v)` and `encodeSignalEvent(_, v)` silently ignore any `v <= signaledValue`. 2. After a fence reaches `1` once and the title resets it via `ID3D12Fence::Signal(0)`, `MTLSharedEvent.signaledValue` remains `1`. Within 1 ms (`glue::refresh` -> `Title::completed` -> `fence::raise`) or on the next `GetCompletedValue()` / `SetEventOnCompletion(1, ...)`, the stale `1` overwrites the fence back to `1` and fires completion events while new GPU work is still in flight, and `ID3D12CommandQueue::Wait(fence, 1)` skips waiting altogether. 3. Subsequent `ID3D12CommandQueue::Signal(fence, 1)` calls encode `encodeSignalEvent(_, 1)` on an event already at `1`, becoming GPU no-ops instead of waiting for the command buffer to finish. 4. `Title::fence_value` clamps `completed(&f).max(initial)` (preventing lowering below `initial`), and `fence::completed` / `set_event_on_completion` poll outside the `waiters` lock before calling `raise`, allowing a concurrent `ID3D12Fence::Signal(0)` to be clobbered by a stale poll result or ignoring a GPU-side `Queue::Signal(fence, 0)`. ## Proposed Fix - Track a strictly increasing `MTLSharedEvent` high-water ticket alongside the logical D3D12 `(epoch, current)` value and in-flight `(raw_ticket, d3d_value)` GPU signals inside `victory_metal::metal::Fence` so `MTLSharedEvent` always advances monotonically while `completed`, `signal`, `wait`, `encodeWaitForEvent`, and `encodeSignalEvent` observe exact D3D12 values across rewinds and repeated signals. - Order `crates/victory-gpu/src/fence.rs` updates by fence epoch under the `waiters` lock and remove the `.max(initial)` clamp in `Title::fence_value` so neither stale polls nor `initial` overwrite a CPU or GPU rewind. - Add Metal backend and native title tests covering binary/rewound `Signal(0)` -> `Queue::Signal(1)` cycles, `Queue::Wait` across rewinds, `SetEventOnCompletion`, and GPU-side fence rewinds.
Sheffy007 commented 2026-11-27 21:42:51 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T21:42:51Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7228689333 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/472#issuecomment-7228689333). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T21:42:51Z. --- /claim
github-actions[bot] commented 2026-11-27 21:43:01 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T21:43:01Z.


Claimed by @Sheffy007. Open a draft pull request with Refs #472 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7228691668 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/472#issuecomment-7228691668). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T21:43:01Z. --- Claimed by @Sheffy007. Open a draft pull request with `Refs #472` in its description within 24 hours, or the claim expires.
Sign in to join this conversation.
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#472
No description provided.