gpu(native): avoid unsignalled Metal waits and preserve presentation order #60

Merged
shabouwcaw merged 4 commits from upstream-pr/60 into main 2026-11-26 13:26:55 +00:00
shabouwcaw commented 2026-11-26 11:42:45 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by shabouwcaw on 2026-11-26T11:42:45Z.


A queue wait whose CPU signal arrives more than about five seconds later can time out on Metal, dropping subsequent work and fence signals. Hold waits on the CPU unless a signal reaching the requested value is already submitted to the GPU; already-completed waits need no GPU command.

Track submitted signal values per fence and release CPU-held work through the existing pump. Preserve presentation ordering by putting deferred presents in the same FIFO as fence signals, retaining their queue, surface, and texture until submission. This also prevents presents from overtaking memory-held rendering. Deferred present errors are logged.

Validation:

  • Native arm64 tests for victory-shader, victory-hle, victory, victory-gpu and xtask: 505 passed, 4 previously ignored.
  • The six-second CPU fence wait regression and cross-queue signal test pass with Metal Shader Converter 3.0.
  • Extended the offscreen presentation test: Wait → render → Present → Signal stays blocked until the CPU signals, then displays the expected red frame. This failed before the presentation fix and passes after it.
  • Both Windows-target workspace Clippy gates pass with warnings denied, including all features.
  • Asset policy: 365 files pass; macOS target-directory script regressions pass.
  • Native GPU Clippy completes; warnings in touched files are pre-existing.

Refs #2. This fixes a demonstrated timeout mechanism, not a confirmed cause of the reported 53-second stall. A previously submitted signal can still be delayed behind long-running GPU work; that timeout case remains. CPU-held waits release through the approximately 1 ms pump.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6625906680 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/60). Originally posted by [shabouwcaw](https://github.com/shabouwcaw) on 2026-11-26T11:42:45Z. --- A queue wait whose CPU signal arrives more than about five seconds later can time out on Metal, dropping subsequent work and fence signals. Hold waits on the CPU unless a signal reaching the requested value is already submitted to the GPU; already-completed waits need no GPU command. Track submitted signal values per fence and release CPU-held work through the existing pump. Preserve presentation ordering by putting deferred presents in the same FIFO as fence signals, retaining their queue, surface, and texture until submission. This also prevents presents from overtaking memory-held rendering. Deferred present errors are logged. Validation: - Native arm64 tests for victory-shader, victory-hle, victory, victory-gpu and xtask: 505 passed, 4 previously ignored. - The six-second CPU fence wait regression and cross-queue signal test pass with Metal Shader Converter 3.0. - Extended the offscreen presentation test: Wait → render → Present → Signal stays blocked until the CPU signals, then displays the expected red frame. This failed before the presentation fix and passes after it. - Both Windows-target workspace Clippy gates pass with warnings denied, including all features. - Asset policy: 365 files pass; macOS target-directory script regressions pass. - Native GPU Clippy completes; warnings in touched files are pre-existing. Refs #2. This fixes a demonstrated timeout mechanism, not a confirmed cause of the reported 53-second stall. A previously submitted signal can still be delayed behind long-running GPU work; that timeout case remains. CPU-held waits release through the approximately 1 ms pump.
LakshanSivakumar commented 2026-11-26 12:01:50 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:01:50Z.

Review: CHANGES_REQUESTED


The per-queue FIFO fixes the render → present → signal submission order. Before landing:

  1. Keep unsatisfied waits off Metal when the producer may be late or fail (native/title.rs ~353/364). A high-water mark recorded at commit survives an asynchronous failure, so later waits can take the same path.
  2. Retain the backend fence while a CPU wait is queued (title.rs ~367/272/397). Right now releasing the fence makes the lookup return u64::MAX and satisfies the wait.
  3. Keep drawable acquisition from blocking the shared pump: nextDrawable can block while the global lock is held (victory-metal/src/present.rs ~179). Please add a regression that drains several deferred presents through the worker.
    Heads-up: this conflicts with #48 in texture_tests.rs.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073789453 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/60#pullrequestreview-6073789453). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:01:50Z. **Review:** CHANGES_REQUESTED --- The per-queue FIFO fixes the render → present → signal submission order. Before landing: 1. Keep unsatisfied waits off Metal when the producer may be late or fail (`native/title.rs` ~353/364). A high-water mark recorded at commit survives an asynchronous failure, so later waits can take the same path. 2. Retain the backend fence while a CPU wait is queued (`title.rs` ~367/272/397). Right now releasing the fence makes the lookup return `u64::MAX` and satisfies the wait. 3. Keep drawable acquisition from blocking the shared pump: `nextDrawable` can block while the global lock is held (`victory-metal/src/present.rs` ~179). Please add a regression that drains several deferred presents through the worker. Heads-up: this conflicts with #48 in `texture_tests.rs`.
shabouwcaw commented 2026-11-26 12:09:47 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by shabouwcaw on 2026-11-26T12:09:47Z.


@LakshanSivakumar Addressed in 0a6dfc1:

  1. Unsatisfied waits stay off Metal. Removed the committed-signal high-water mark. Queue waits now remain in the CPU FIFO until the backend reports the requested value as completed, including when a producer has already committed. A delayed or asynchronously failed producer therefore cannot cause a later unsatisfied wait to be encoded on Metal. Added a regression with a committed, stalled producer that verifies the consumer remains CPU-held until completion; this test exercises the delayed-producer case, rather than injecting an asynchronous GPU failure.

  2. Queued waits retain the backend fence. Each wait gets a unique token owning an Rc to its specific backend fence, independent of the title-address lookup. Releasing the title fence or reusing its address cannot satisfy the old wait. The reference is released when the wait is consumed. Deferred signals also retain their queue and fence. Added a release/address-reuse regression that checks both continued blocking and eventual release of the retained fence.

  3. Drawable acquisition no longer blocks the shared pump. A per-surface worker calls nextDrawable; the pump polls its result without blocking. A pending present stays at its FIFO head, preserving render → present → signal order while other queues can progress. Per-frame autorelease pools release temporary drawable references. The presentation regression now queues six deferred presents, signals the gate directly on the backend, and verifies that the timer worker drains all six and produces the expected pixels. A separate FIFO regression checks that a pending action blocks only its own queue.

For the overlap with #48, the refactored presentation helper now uses its proposed eight-page pool for both immediate and deferred cases. #48 is still open; I have not merged that branch or claimed the textual merge conflict is resolved.

Validation passed locally on native arm64 macOS:

  • cargo test -j6 -p victory-hle -p victory -p victory-gpu -p victory-metal -p xtask
  • Final GPU/Metal rerun after the overlapping pool-size adjustment: cargo test -j6 -p victory-gpu -p victory-metal (GPU: 150 passed, 1 ignored; Metal unit/integration suites: 15 passed).
  • cargo clippy -j6 --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy (365 files checked)
  • git diff --check

The native build still emits existing warnings. These are local results; CI for the pushed commit is separate.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7191646251 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/60#issuecomment-7191646251). Originally posted by [shabouwcaw](https://github.com/shabouwcaw) on 2026-11-26T12:09:47Z. --- @LakshanSivakumar Addressed in 0a6dfc1: 1. **Unsatisfied waits stay off Metal.** Removed the committed-signal high-water mark. Queue waits now remain in the CPU FIFO until the backend reports the requested value as completed, including when a producer has already committed. A delayed or asynchronously failed producer therefore cannot cause a later unsatisfied wait to be encoded on Metal. Added a regression with a committed, stalled producer that verifies the consumer remains CPU-held until completion; this test exercises the delayed-producer case, rather than injecting an asynchronous GPU failure. 2. **Queued waits retain the backend fence.** Each wait gets a unique token owning an `Rc` to its specific backend fence, independent of the title-address lookup. Releasing the title fence or reusing its address cannot satisfy the old wait. The reference is released when the wait is consumed. Deferred signals also retain their queue and fence. Added a release/address-reuse regression that checks both continued blocking and eventual release of the retained fence. 3. **Drawable acquisition no longer blocks the shared pump.** A per-surface worker calls `nextDrawable`; the pump polls its result without blocking. A pending present stays at its FIFO head, preserving render → present → signal order while other queues can progress. Per-frame autorelease pools release temporary drawable references. The presentation regression now queues six deferred presents, signals the gate directly on the backend, and verifies that the timer worker drains all six and produces the expected pixels. A separate FIFO regression checks that a pending action blocks only its own queue. For the overlap with #48, the refactored presentation helper now uses its proposed eight-page pool for both immediate and deferred cases. #48 is still open; I have not merged that branch or claimed the textual merge conflict is resolved. Validation passed locally on native arm64 macOS: - `cargo test -j6 -p victory-hle -p victory -p victory-gpu -p victory-metal -p xtask` - Final GPU/Metal rerun after the overlapping pool-size adjustment: `cargo test -j6 -p victory-gpu -p victory-metal` (GPU: 150 passed, 1 ignored; Metal unit/integration suites: 15 passed). - `cargo clippy -j6 --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy` (365 files checked) - `git diff --check` The native build still emits existing warnings. These are local results; CI for the pushed commit is separate.
LakshanSivakumar commented 2026-11-26 12:48:25 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:48:25Z.

Review: CHANGES_REQUESTED


Thanks, all three earlier points are addressed (no GPU-encoded unsatisfied waits, the fence retained while a wait is queued, nextDrawable off the shared pump with the six-present regression). Small things before merge:

  1. Every unsatisfied fence wait is now CPU-held until the ~1 ms timer sees completion, even behind an already-committed producer. Please share a before/after headless frame figure and add a docs line on that tradeoff.
  2. The ROADMAP text says only waits with no queued signal are held; the code holds all unsatisfied ones. Please reword.
  3. The blocking Metal::present busy-loops if no drawable arrives. It has no title caller now, but please bound it or add a shortcut: comment.
    This also overlaps #48 in texture_tests.rs, so whichever lands second needs a rebase.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6074388266 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/60#pullrequestreview-6074388266). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:48:25Z. **Review:** CHANGES_REQUESTED --- Thanks, all three earlier points are addressed (no GPU-encoded unsatisfied waits, the fence retained while a wait is queued, nextDrawable off the shared pump with the six-present regression). Small things before merge: 1. Every unsatisfied fence wait is now CPU-held until the ~1 ms timer sees completion, even behind an already-committed producer. Please share a before/after headless frame figure and add a docs line on that tradeoff. 2. The ROADMAP text says only waits with no queued signal are held; the code holds all unsatisfied ones. Please reword. 3. The blocking `Metal::present` busy-loops if no drawable arrives. It has no title caller now, but please bound it or add a `shortcut:` comment. This also overlaps #48 in `texture_tests.rs`, so whichever lands second needs a rebase.
shabouwcaw commented 2026-11-26 12:51:38 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by shabouwcaw on 2026-11-26T12:51:38Z.


Follow-up in 12bd1b9:

  1. Documented the scheduling tradeoff in docs/native-host.md: every unsatisfied fence wait stays CPU-held, so completion between polls can add roughly one nominal 1 ms timer interval before dependent work is submitted, potentially longer under host scheduling or lock contention. Title calls also pump. The before/after headless frame figure is still outstanding: there is no game installation available on this development machine, so I cannot provide a valid measured figure or claim unchanged performance. The docs explicitly distinguish the passing functional tests from an unmeasured frame-rate impact. A tester with a local installation will need to compare 7288e18 (before) against this revision with the same run duration, inputs, settings, and saved state.

  2. Reworded docs/ROADMAP.md to say all unsatisfied queue waits are CPU-held until observed backend completion, including waits behind already-committed signals whose producers can still be delayed or fail.

  3. Bounded the standalone blocking Metal::present helper to a two-second drawable wait, returning timed out waiting for a drawable. It sleeps between polls; the title path continues to use only nonblocking try_present. Added a regression with a connected acquisition channel that never supplies a drawable; it verifies the timeout error and no presented frame. Also removed two redundant casts in this file flagged by the native Metal lint check.

Validation:

  • GPU/Metal suite passed after the timeout change: GPU 150 passed / 1 ignored; Metal unit and integration suites 15 passed.
  • New timeout regression passed separately (about 2.14 s including setup).
  • Native cargo clippy -j6 -p victory-metal --all-targets -- -D warnings passed.
  • cargo xtask policy passed (365 files); git diff --check passed.

Acknowledged the overlap with #48: the overlapping helper already uses its eight-page pool adjustment, but whichever PR lands second still needs a rebase. This follow-up does not change texture_tests.rs again.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7192555756 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/60#issuecomment-7192555756). Originally posted by [shabouwcaw](https://github.com/shabouwcaw) on 2026-11-26T12:51:38Z. --- Follow-up in 12bd1b9: 1. Documented the scheduling tradeoff in `docs/native-host.md`: every unsatisfied fence wait stays CPU-held, so completion between polls can add roughly one nominal 1 ms timer interval before dependent work is submitted, potentially longer under host scheduling or lock contention. Title calls also pump. **The before/after headless frame figure is still outstanding:** there is no game installation available on this development machine, so I cannot provide a valid measured figure or claim unchanged performance. The docs explicitly distinguish the passing functional tests from an unmeasured frame-rate impact. A tester with a local installation will need to compare 7288e18 (before) against this revision with the same run duration, inputs, settings, and saved state. 2. Reworded `docs/ROADMAP.md` to say **all unsatisfied queue waits** are CPU-held until observed backend completion, including waits behind already-committed signals whose producers can still be delayed or fail. 3. Bounded the standalone blocking `Metal::present` helper to a two-second drawable wait, returning `timed out waiting for a drawable`. It sleeps between polls; the title path continues to use only nonblocking `try_present`. Added a regression with a connected acquisition channel that never supplies a drawable; it verifies the timeout error and no presented frame. Also removed two redundant casts in this file flagged by the native Metal lint check. Validation: - GPU/Metal suite passed after the timeout change: GPU 150 passed / 1 ignored; Metal unit and integration suites 15 passed. - New timeout regression passed separately (about 2.14 s including setup). - Native `cargo clippy -j6 -p victory-metal --all-targets -- -D warnings` passed. - `cargo xtask policy` passed (365 files); `git diff --check` passed. Acknowledged the overlap with #48: the overlapping helper already uses its eight-page pool adjustment, but whichever PR lands second still needs a rebase. This follow-up does not change `texture_tests.rs` again.
LakshanSivakumar commented 2026-11-26 13:26:52 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T13:26:52Z.

Review: APPROVED


Follow-up items are addressed cleanly: scheduling tradeoff and missing local frame-rate baseline are documented in docs/native-host.md, docs/ROADMAP.md matches the CPU-held wait policy, blocking Metal::present is bounded with a regression test, and the two present.rs clippy cast warnings are gone. CI is green; merging (before/after headless frame comparison remains open for a run with game data).

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6074930621 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/60#pullrequestreview-6074930621). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T13:26:52Z. **Review:** APPROVED --- Follow-up items are addressed cleanly: scheduling tradeoff and missing local frame-rate baseline are documented in `docs/native-host.md`, `docs/ROADMAP.md` matches the CPU-held wait policy, blocking `Metal::present` is bounded with a regression test, and the two `present.rs` clippy cast warnings are gone. CI is green; merging (before/after headless frame comparison remains open for a run with game data).
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!60
No description provided.