Proposal: GPU time per pass on the native Metal path (and later, record and replay of one world frame) #460

Open
opened 2026-11-27 21:03:44 +00:00 by Hayyan612 · 9 comments
Hayyan612 commented 2026-11-27 21:03:44 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Hayyan612 on 2026-11-27T21:03:44Z.


The native host times whole command buffers (GPUEndTime - GPUStartTime in victory-metal/src/gfx.rs), not passes, so a slow pass in a world frame cannot be told from the others. On the Wine route, #47 found its frame recorder and replayer, with per-pass GPU times and leave-one-pass-out costs, the tool that helped most. victory-fork and cool_fox use the same (VICTORY_RECORD, docs/replay.md there); victory-fork found, for one, that two water-lighting programs cost about 15 ms a frame.

Step 1, small: VICTORY_PASS_TIMING=1 (off by default). An MTLCounterSampleBuffer sampled at each encoder's start and end (stage-boundary sampling, which Apple GPUs support), resolved when the buffer completes. Log the N most expensive passes of a world frame, by pipeline hash and draw count.

Step 2, later: record one world frame's submissions and the title memory they read to a folder outside the repo, and replay it on its own with per-pass times. That makes a shader or pipeline change measurable on a real frame without playing to it again.

Not claiming. I can write step 1 with a test on a synthetic two-pass frame, if it is wanted.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6654441567 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460). Originally posted by [Hayyan612](https://github.com/Hayyan612) on 2026-11-27T21:03:44Z. --- The native host times whole command buffers (`GPUEndTime - GPUStartTime` in `victory-metal/src/gfx.rs`), not passes, so a slow pass in a world frame cannot be told from the others. On the Wine route, #47 found its frame recorder and replayer, with per-pass GPU times and leave-one-pass-out costs, the tool that helped most. victory-fork and cool_fox use the same (`VICTORY_RECORD`, `docs/replay.md` there); victory-fork found, for one, that two water-lighting programs cost about 15 ms a frame. **Step 1, small:** `VICTORY_PASS_TIMING=1` (off by default). An `MTLCounterSampleBuffer` sampled at each encoder's start and end (stage-boundary sampling, which Apple GPUs support), resolved when the buffer completes. Log the N most expensive passes of a world frame, by pipeline hash and draw count. **Step 2, later:** record one world frame's submissions and the title memory they read to a folder outside the repo, and replay it on its own with per-pass times. That makes a shader or pipeline change measurable on a real frame without playing to it again. Not claiming. I can write step 1 with a test on a synthetic two-pass frame, if it is wanted.
marcus-4 commented 2026-11-27 21:53:20 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by marcus-4 on 2026-11-27T21:53:20Z.


/claim

I'm taking step 1 only (VICTORY_PASS_TIMING=1), not the record and replay. Thanks for writing it up, @Hayyan612. If you'd rather do it yourself, say so and I'll release it.

Plan

  • Off by default. When the device supports stage-boundary sampling (supportsCounterSampling(.atStageBoundary), true on Apple GPUs), each compute, render and blit pass the native host encodes gets a pair of timestamp samples. One MTLCounterSampleBuffer holds the slots, allocated per command buffer, and passes are attached through their pass descriptors (sampleBufferAttachments). Compute passes are made with computeCommandEncoderWithDescriptor only while the switch is on, so nothing changes with it off.
  • In the command buffer's completed handler, resolve the samples, convert GPU ticks to nanoseconds with sampleTimestamps, and add each pass's time to its key: the pipeline hash (or blit), plus the draw or dispatch count.
  • Every N presents, one log line with the top passes of the window by GPU time (pass timing), plus the total and the share of command-buffer GPU time the passes cover. A device without the counters logs that once and times nothing.
  • Test (victory-metal, Metal): a synthetic frame of a short and a long compute pass, plus a blit, where the long pass must rank first, with nonzero times that add up to no more than the command buffer's own GPU time.

Draft PR within the day.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7228843221 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7228843221). Originally posted by [marcus-4](https://github.com/marcus-4) on 2026-11-27T21:53:20Z. --- /claim I'm taking step 1 only (`VICTORY_PASS_TIMING=1`), not the record and replay. Thanks for writing it up, @Hayyan612. If you'd rather do it yourself, say so and I'll release it. **Plan** - Off by default. When the device supports stage-boundary sampling (`supportsCounterSampling(.atStageBoundary)`, true on Apple GPUs), each compute, render and blit pass the native host encodes gets a pair of timestamp samples. One `MTLCounterSampleBuffer` holds the slots, allocated per command buffer, and passes are attached through their pass descriptors (`sampleBufferAttachments`). Compute passes are made with `computeCommandEncoderWithDescriptor` only while the switch is on, so nothing changes with it off. - In the command buffer's completed handler, resolve the samples, convert GPU ticks to nanoseconds with `sampleTimestamps`, and add each pass's time to its key: the pipeline hash (or `blit`), plus the draw or dispatch count. - Every N presents, one log line with the top passes of the window by GPU time (`pass timing`), plus the total and the share of command-buffer GPU time the passes cover. A device without the counters logs that once and times nothing. - Test (`victory-metal`, Metal): a synthetic frame of a short and a long compute pass, plus a blit, where the long pass must rank first, with nonzero times that add up to no more than the command buffer's own GPU time. Draft PR within the day.
github-actions[bot] commented 2026-11-27 21:53:30 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T21:53:30Z.


Claimed by @marcus-4. Open a draft pull request with Refs #460 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7228845623 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7228845623). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T21:53:30Z. --- Claimed by @marcus-4. Open a draft pull request with `Refs #460` in its description within 24 hours, or the claim expires.
marcus-4 commented 2026-11-28 08:19:17 +00:00 (Migrated from github.com)
Author
Contributor

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


/release

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7236506722 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7236506722). Originally posted by [marcus-4](https://github.com/marcus-4) on 2026-11-28T08:19:17Z. --- /release
marcus-4 commented 2026-11-28 08:24:43 +00:00 (Migrated from github.com)
Author
Contributor

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


/release

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7236565398 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7236565398). Originally posted by [marcus-4](https://github.com/marcus-4) on 2026-11-28T08:24:43Z. --- /release
github-actions[bot] commented 2026-11-28 08:24:53 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-28T08:24:53Z.


Released. This issue is free to claim.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7236567225 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7236567225). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-28T08:24:53Z. --- Released. This issue is free to claim.
AnastasisKon commented 2026-11-28 10:40:07 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T10:40:07Z.


/claim

Taking step 1 only: (off by default) — an sampled at each encoder's start/end, resolved on completion, logging the most expensive passes of a world frame by pipeline hash and draw count — plus a unit test on a synthetic two-pass command buffer. Step 2 (record/replay) stays open. Draft PR from my fork's vibe/ branch to follow.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7238043915 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7238043915). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T10:40:07Z. --- /claim Taking step 1 only: (off by default) — an sampled at each encoder's start/end, resolved on completion, logging the most expensive passes of a world frame by pipeline hash and draw count — plus a unit test on a synthetic two-pass command buffer. Step 2 (record/replay) stays open. Draft PR from my fork's vibe/ branch to follow.
github-actions[bot] commented 2026-11-28 10:40:18 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-28T10:40:18Z.


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

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7238045834 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7238045834). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-28T10:40:18Z. --- Claimed by @AnastasisKon. Open a draft pull request with `Refs #460` in its description within 24 hours, or the claim expires.
AnastasisKon commented 2026-11-28 12:47:52 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T12:47:52Z.


/release - done, first two steps: PR #594 adds VICTORY_PASS_TIMING (GPU time per pass at each encoder boundary) and PR #602 adds VICTORY_PASS_TIMING=draw (per draw and per dispatch), both in my fork's main, with #597 making run-summary.sh summarise the lines. The parenthetical part of the proposal, record and replay of one world frame, is still open and is not covered by these; freeing the claim on the timing part so it can be picked up separately.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7239473162 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7239473162). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T12:47:52Z. --- /release - done, first two steps: PR #594 adds `VICTORY_PASS_TIMING` (GPU time per pass at each encoder boundary) and PR #602 adds `VICTORY_PASS_TIMING=draw` (per draw and per dispatch), both in my fork's `main`, with #597 making run-summary.sh summarise the lines. The parenthetical part of the proposal, record and replay of one world frame, is still open and is not covered by these; freeing the claim on the timing part so it can be picked up separately.
github-actions[bot] commented 2026-11-28 12:48:02 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-28T12:48:02Z.


Released. This issue is free to claim.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7239475284 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/460#issuecomment-7239475284). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-28T12:48:02Z. --- Released. This issue is free to claim.
Sign in to join this conversation.
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#460
No description provided.