metal: GPU time per pass behind VICTORY_PASS_TIMING=1 (#460) #484

Closed
marcus-4 wants to merge 1 commit from upstream-pr/484 into main
marcus-4 commented 2026-11-27 22:06:09 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by marcus-4 on 2026-11-27T22:06:09Z.


What this changes

Refs #460 (step 1 only; record and replay is not part of this)

VICTORY_PASS_TIMING=1, off by default: GPU time per pass on the native Metal path. Today the host times whole command buffers only (GPUEndTime - GPUStartTime), so a slow pass in a world frame can't be told from the others.

  • Each compute, render and blit pass of a submission gets two timestamp samples through its pass descriptor (sampleBufferAttachments): its encoder's start and end, and for render the start of the vertex stage to the end of the fragment stage. Apple GPUs sample at stage boundaries only, so a pass is the finest grain. There is one MTLCounterSampleBuffer per command buffer, resolved in its completed handler.

  • Passes are keyed by kind, their dispatch, draw or op count, and their pipelines. A pipeline shows by its name when name_pipeline gave one, otherwise by its address; a key lists up to four pipelines, each with its count. name_pipeline now records names while the switch is on, as it already did with VICTORY_TRACE_GPU_FAILS.

  • About every 10 s, one pass timing line gives:

    • the GPU time of all timed passes, how many passes and in how many command buffers;
    • the share of the command buffers' own GPU time the passes cover (the rest is the gaps between passes: waits, fences, barriers);
    • the 12 costliest keys.

    Ticks are converted with the device's CPU/GPU clock correlation (sampleTimestamps:gpuTimestamp:).

  • Compute and blit encoders are made with …EncoderWithDescriptor only while the switch is on. With it off, nothing changes.

  • Not timed: a submission split into pieces (VICTORY_PASSES_PER_BUFFER), colour-clear rectangle passes, the gate pass a render pass encodes before itself (its time is in the gaps), and command buffers the GPU failed. A GPU without stage-boundary timestamps logs that once and times nothing.

New module crates/victory-metal/src/pass_timing.rs, wired into gfx.rs (submit_passes, encode_compute, encode_render, encode_blit) and metal.rs. Documented in docs/native-host.md next to VICTORY_TRACE_GPU_FAILS.

How it was tested

M1 Pro, macOS 27.2.1, Xcode 27.2.

  • Metal: gfx::owed_tests::pass_timing_times_each_pass_and_ranks_the_long_one_first. It submits three blit passes, one of them filling 64 MiB. Each pass is timed once under its own key, the 64 MiB pass is the longest, and the passes still ran. It ran on the GPU here, not skipped.
  • Unit tests in pass_timing.rs: the line ranks by time and shares the command buffers' time, keys name each pipeline once with its count, ticks convert by the two clocks.
  • cargo test -p victory-metal -p victory-gpu -p victory-backend: 488 passed, 0 failed
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: clean (and native arm64 clippy on the new module: clean). cargo xtask policy: ok
  • Ran the game: not done (no game run on this machine). A tester's world run with VICTORY_PASS_TIMING=1 would show the top passes of a world frame.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none: host-side timing)
  • New diagnostics are behind a VICTORY_* variable and documented (VICTORY_PASS_TIMING)
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5533704457 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/484). Originally posted by [marcus-4](https://github.com/marcus-4) on 2026-11-27T22:06:09Z. --- ## What this changes Refs #460 (step 1 only; record and replay is not part of this) `VICTORY_PASS_TIMING=1`, off by default: GPU time per pass on the native Metal path. Today the host times whole command buffers only (`GPUEndTime - GPUStartTime`), so a slow pass in a world frame can't be told from the others. - Each compute, render and blit pass of a submission gets two timestamp samples through its pass descriptor (`sampleBufferAttachments`): its encoder's start and end, and for render the start of the vertex stage to the end of the fragment stage. Apple GPUs sample at stage boundaries only, so a pass is the finest grain. There is one `MTLCounterSampleBuffer` per command buffer, resolved in its completed handler. - Passes are keyed by kind, their dispatch, draw or op count, and their pipelines. A pipeline shows by its name when `name_pipeline` gave one, otherwise by its address; a key lists up to four pipelines, each with its count. `name_pipeline` now records names while the switch is on, as it already did with `VICTORY_TRACE_GPU_FAILS`. - About every 10 s, one `pass timing` line gives: - the GPU time of all timed passes, how many passes and in how many command buffers; - the share of the command buffers' own GPU time the passes cover (the rest is the gaps between passes: waits, fences, barriers); - the 12 costliest keys. Ticks are converted with the device's CPU/GPU clock correlation (`sampleTimestamps:gpuTimestamp:`). - Compute and blit encoders are made with `…EncoderWithDescriptor` only while the switch is on. With it off, nothing changes. - Not timed: a submission split into pieces (`VICTORY_PASSES_PER_BUFFER`), colour-clear rectangle passes, the gate pass a render pass encodes before itself (its time is in the gaps), and command buffers the GPU failed. A GPU without stage-boundary timestamps logs that once and times nothing. New module `crates/victory-metal/src/pass_timing.rs`, wired into `gfx.rs` (`submit_passes`, `encode_compute`, `encode_render`, `encode_blit`) and `metal.rs`. Documented in `docs/native-host.md` next to `VICTORY_TRACE_GPU_FAILS`. ## How it was tested M1 Pro, macOS 27.2.1, Xcode 27.2. - [x] Metal: `gfx::owed_tests::pass_timing_times_each_pass_and_ranks_the_long_one_first`. It submits three blit passes, one of them filling 64 MiB. Each pass is timed once under its own key, the 64 MiB pass is the longest, and the passes still ran. It ran on the GPU here, not skipped. - [x] Unit tests in `pass_timing.rs`: the line ranks by time and shares the command buffers' time, keys name each pipeline once with its count, ticks convert by the two clocks. - [x] `cargo test -p victory-metal -p victory-gpu -p victory-backend`: 488 passed, 0 failed - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`: clean (and native arm64 clippy on the new module: clean). `cargo xtask policy`: ok - [ ] Ran the game: not done (no game run on this machine). A tester's world run with `VICTORY_PASS_TIMING=1` would show the top passes of a world frame. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none: host-side timing) - [x] New diagnostics are behind a `VICTORY_*` variable and documented (`VICTORY_PASS_TIMING`)
marcus-4 commented 2026-11-28 08:13:36 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by marcus-4 on 2026-11-28T08:13:36Z.


Closing: this change is already in.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7236445783 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/484#issuecomment-7236445783). Originally posted by [marcus-4](https://github.com/marcus-4) on 2026-11-28T08:13:36Z. --- Closing: this change is already in.

Pull request closed

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!484
No description provided.