metal: VICTORY_PASS_TIMING — per-pass GPU timing at each encoder boundary (#460 step 1) #594

Open
AnastasisKon wants to merge 1 commit from upstream-pr/594 into main
AnastasisKon commented 2026-11-28 11:04:08 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T11:04:08Z.


What this changes

Refs #460 (step 1 only; step 2 record/replay stays open). Adds VICTORY_PASS_TIMING=1 (or =<n>): per-pass GPU time on the native Metal path. An MTLCounterSampleBuffer of the timestamp counter set is sampled at each pass encoder's start and end, resolved when the command buffer completes, and at each present the frame's n most expensive passes (10 for =1) are logged most-expensive-first by pipeline (shader hash) and work (draws / thread groups / blit ops), then the frame total (pass time frame=<f> ... lines).

Implementation notes:

  • New module crates/victory-metal/src/pass_timing.rs; sampling hooks in gfx.rs's submit path (one sample buffer per command buffer, one per piece of a split submission) and in both completion handlers; the frame boundary is each present in present.rs.
  • Only encoder kinds the device reports as sampling at their own boundary are sampled (AtDispatchBoundary / AtDrawBoundary / AtBlitBoundary): Metal asserts (SIGABRT) on sampleCountersInBuffer for an unsupported kind, and support differs per GPU family — an AGXG15X (this Mac) samples dispatch and draw boundaries but not blit. Passes of an unsampled kind get no label, keeping sample indices aligned with labels.
  • A render pass with indirect draws starts its sample pair on its gate compute encoder when compute boundaries are sampled, so the gate's time is inside the pass's.
  • Pipeline names are now kept without VICTORY_TRACE_GPU_FAILS so the log can name passes by shader hash.
  • Off by default; documented in docs/native-host.md under "Bring-up diagnostics".

How it was tested

  • cargo test -p victory-hle -p victory -p victory-gpu -p xtask (host arm64, run with -- --test-threads=1 per #548): 708 passed, 0 failed
  • cargo test -p victory-metal -- --test-threads=1: 85 passed, 0 failed, including the new pass_timing::tests::each_pass_of_a_command_buffer_is_sampled_and_a_frame_reports_them — a synthetic two-pass command buffer (blit fills, or a zero-draw render pass when the device samples no blit boundary, as here) yields one resolved sample per pass, with sane durations
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: clean
  • cargo xtask policy: clean
  • Ran the game: not needed for this change — the switch is off by default and touches no game data; the unit test exercises the sampling/resolve path on a real device

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented behaviour are marked: per-GPU-family sampling support is noted in the module doc and docs/native-host.md (G15X: no blit boundary)
  • The new diagnostic is behind VICTORY_PASS_TIMING, off by default, and documented in docs/native-host.md
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5539450174 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/594). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T11:04:08Z. --- ## What this changes Refs #460 (step 1 only; step 2 record/replay stays open). Adds `VICTORY_PASS_TIMING=1` (or `=<n>`): per-pass GPU time on the native Metal path. An `MTLCounterSampleBuffer` of the timestamp counter set is sampled at each pass encoder's start and end, resolved when the command buffer completes, and at each present the frame's `n` most expensive passes (10 for `=1`) are logged most-expensive-first by pipeline (shader hash) and work (draws / thread groups / blit ops), then the frame total (`pass time frame=<f> ...` lines). Implementation notes: - New module `crates/victory-metal/src/pass_timing.rs`; sampling hooks in `gfx.rs`'s submit path (one sample buffer per command buffer, one per piece of a split submission) and in both completion handlers; the frame boundary is each present in `present.rs`. - Only encoder kinds the device reports as sampling at their own boundary are sampled (`AtDispatchBoundary` / `AtDrawBoundary` / `AtBlitBoundary`): Metal asserts (SIGABRT) on `sampleCountersInBuffer` for an unsupported kind, and support differs per GPU family — an AGXG15X (this Mac) samples dispatch and draw boundaries but not blit. Passes of an unsampled kind get no label, keeping sample indices aligned with labels. - A render pass with indirect draws starts its sample pair on its gate compute encoder when compute boundaries are sampled, so the gate's time is inside the pass's. - Pipeline names are now kept without `VICTORY_TRACE_GPU_FAILS` so the log can name passes by shader hash. - Off by default; documented in `docs/native-host.md` under "Bring-up diagnostics". ## How it was tested - [x] `cargo test -p victory-hle -p victory -p victory-gpu -p xtask` (host arm64, run with `-- --test-threads=1` per #548): 708 passed, 0 failed - [x] `cargo test -p victory-metal -- --test-threads=1`: 85 passed, 0 failed, including the new `pass_timing::tests::each_pass_of_a_command_buffer_is_sampled_and_a_frame_reports_them` — a synthetic two-pass command buffer (blit fills, or a zero-draw render pass when the device samples no blit boundary, as here) yields one resolved sample per pass, with sane durations - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`: clean - [x] `cargo xtask policy`: clean - [ ] Ran the game: not needed for this change — the switch is off by default and touches no game data; the unit test exercises the sampling/resolve path on a real device ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented behaviour are marked: per-GPU-family sampling support is noted in the module doc and `docs/native-host.md` (G15X: no blit boundary) - [x] The new diagnostic is behind `VICTORY_PASS_TIMING`, off by default, and documented in `docs/native-host.md`
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/594:upstream-pr/594'
git switch 'upstream-pr/594'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/594'
git switch 'upstream-pr/594'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/594'
git switch 'upstream-pr/594'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/594'
git switch 'main'
git merge --squash 'upstream-pr/594'
git switch 'main'
git merge --ff-only 'upstream-pr/594'
git switch 'main'
git merge 'upstream-pr/594'
git push origin 'main'
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!594
No description provided.