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

Closed
github-bot wants to merge 1 commit from github-bot/gh-484 into main AGit
Owner

@⁠marcus-4 on GitHub · 2026-11-27 22:06 UTC · original
marcus-4:feat/pass-timing → main · +434 −4 · 5 files · 1 commits · closed without merging

What this changes

Refs #464 (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)
**@⁠marcus-4** on GitHub · 2026-11-27 22:06 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/484) `marcus-4:feat/pass-timing` → `main` · +434 −4 · 5 files · 1 commits · closed without merging ## What this changes Refs #464 (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`)
The host timed whole command buffers only, so a slow pass in a world
frame could not be told from the others. With the switch, each compute,
render and blit pass of a submission gets two timestamp samples through
its pass descriptor (Apple GPUs sample at stage boundaries only), one
MTLCounterSampleBuffer per command buffer, resolved when it completes.
Passes are keyed by kind, count and pipelines (names, else addresses).
About every 10 s a `pass timing` line gives the total, the share of the
command buffers' GPU time the passes cover, and the 12 costliest keys.

Off by default; compute and blit encoders take a descriptor only while it
is on. Not timed: submissions split into pieces, colour-clear passes, the
gate pass before a render pass, failed command buffers.

Test (Metal): three blit passes, one of 64 MiB, each timed once under its
own key, the big one longest.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 22:11:24 +00:00
github-bot force-pushed github-bot/gh-484 from c76b0900ff to f280c8e1da 2026-11-27 23:55:11 +00:00 Compare
github-bot force-pushed github-bot/gh-484 from f280c8e1da to a42a3c8e21 2026-11-28 01:02:57 +00:00 Compare
github-bot force-pushed github-bot/gh-484 from a42a3c8e21 to c5095b2c06 2026-11-28 02:05:05 +00:00 Compare
github-bot closed this pull request 2026-11-28 08:20:40 +00:00
Author
Owner

@⁠marcus-4 on GitHub · 2026-11-28 08:13 UTC · original

Closing: this change is already in.

**@⁠marcus-4** on GitHub · 2026-11-28 08:13 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/484#issuecomment-7236445783) Closing: this change is already in.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.