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

Closed
opened 2026-11-27 21:43:25 +00:00 by github-bot · 2 comments
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 21:42 UTC · original

Refs #460, #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** on GitHub · 2026-11-27 21:42 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/472) Refs #460, #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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 21:43:25 +00:00
Author
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 21:42 UTC · original

/claim

**@⁠Sheffy007** on GitHub · 2026-11-27 21:42 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/472#issuecomment-7228689333) /claim
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-27 21:43 UTC · original

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

**@⁠github-actions[bot]** on GitHub · 2026-11-27 21:43 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/472#issuecomment-7228691668) Claimed by @⁠Sheffy007. Open a draft pull request with `Refs #472` in its description within 24 hours, or the claim expires.
This discussion has been locked. Commenting is limited to contributors.
No description provided.