[PR #1397] [MERGED] Metal: the GPU frame timer measures the frame again (read about 0.01 ms) #3496

Closed
opened 2026-11-03 04:09:57 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/1397
Author: @ludovic111
Created: 10/23/2026
Status: ✅ Merged
Merged: 10/23/2026
Merged by: @ludovic111

Base: main ← Head: fix/metal-gpu-timer


📝 Commits (1)

  • d6f8dda Metal: the GPU frame timer measures the frame again (read about 0.01 ms)

📊 Changes

2 files changed (+185 additions, -53 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+6 -2)
📝 crates/horizon-client/src/gfx/timing.rs (+179 -51)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

Part of perf-rd32-1 (FEATURES.md 4zd, second half: "the game's GPU frame timer reads about 0.01 ms on Metal").

What

On Metal the GPU frame timer (F3's GPU time, the frame graph, horizon-ctl fly ... raw's gpu_ms, the benchmark reports) read about 0.002–0.01 ms. It now measures the frame.

Why it was wrong

Apple GPUs only take timestamps at pass boundaries, so the frame was bracketed by two one-thread compute passes. Two problems:

  1. Metal resolves counter samples without waiting for the passes that write them. The resolve ran in the frame's own command buffer, so the end stamp it read was mostly the one an earlier frame had left in the shared query. A debug print of the raw stamps showed the end before the start in 78% of frames, by 1 to 7 ms (one to four frames).
  2. A compute pass depends on nothing, so the end marker ran alongside the frame's render passes rather than after them.

How

  • Each readback slot has its own pair of queries and its own resolve range (8 slots).
  • With markers (Metal), a slot is resolved in a later frame, once Queue::on_submitted_work_done reports that the GPU finished the frame it timed. Then it is mapped as before. Vulkan and DirectX 12 write timestamps between commands and still resolve in the frame.
  • The end marker is a render pass that loads the frame's target and shades one pixel (1×1 scissor) with every colour write masked. A pass on the target runs after every pass that drew into it. Metal leaves the timestamps of a pass that does nothing at zero, which is why it shades one pixel.
  • The start stays a compute pass. A render-pass start, which loads and stores the whole target, gave the same numbers. Twenty extra marker passes cost about 0.18 ms at 720p, so one less pass is worth keeping.
  • A frame whose end comes before its start is left untimed. That happened in 45 of 128k frames, a start marker that ran late.

Numbers (M4 Pro, Metal, RD 32, offscreen 1280×720, perf_bench flights; the Mac mini was shared, load 10–60)

scene frame p50 CPU p50 GPU p50 before GPU p50 after
village 1.95 ms 1.39 ms 0.107 ms 1.95 ms
mountain 4.51 ms 2.01 ms 0.002 ms 4.46 ms

At RD 32 the M4 Pro is GPU-bound on the mountain flight: GPU time ≈ frame time, well above the CPU time. That is the case occlusion culling on Metal (4zd, first half) would help.

Pictures

The marker writes nothing (masked writes, 1×1 scissor). An offscreen screenshot of the same frozen view on main and on this branch differs in 0.08% of pixels, max 15/255. Two main runs of the same view differ in 28% (the sky and clouds).

Self-check

  • cargo fmt --all, cargo clippy --workspace --all-targets -- -D warnings: clean.
  • No harness covers the GPU timer; the change is in gfx/timing.rs and its three call sites.

Needs Ludovic

Nothing. Label skip-release: it only changes a debug readout.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/1397 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/23/2026 **Status:** ✅ Merged **Merged:** 10/23/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/metal-gpu-timer` --- ### 📝 Commits (1) - [`d6f8dda`](https://github.com/horizon-rust/horizon/commit/d6f8dda823921845fed39d5b1722d33afc4e4dc9) Metal: the GPU frame timer measures the frame again (read about 0.01 ms) ### 📊 Changes **2 files changed** (+185 additions, -53 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+6 -2) 📝 `crates/horizon-client/src/gfx/timing.rs` (+179 -51) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Part of perf-rd32-1 (FEATURES.md 4zd, second half: "the game's GPU frame timer reads about 0.01 ms on Metal"). ## What On Metal the GPU frame timer (F3's GPU time, the frame graph, `horizon-ctl fly ... raw`'s `gpu_ms`, the benchmark reports) read about 0.002–0.01 ms. It now measures the frame. ## Why it was wrong Apple GPUs only take timestamps at pass boundaries, so the frame was bracketed by two one-thread compute passes. Two problems: 1. **Metal resolves counter samples without waiting for the passes that write them.** The resolve ran in the frame's own command buffer, so the end stamp it read was mostly the one an earlier frame had left in the shared query. A debug print of the raw stamps showed the end before the start in 78% of frames, by 1 to 7 ms (one to four frames). 2. **A compute pass depends on nothing**, so the end marker ran alongside the frame's render passes rather than after them. ## How - Each readback slot has its own pair of queries and its own resolve range (8 slots). - With markers (Metal), a slot is resolved in a later frame, once `Queue::on_submitted_work_done` reports that the GPU finished the frame it timed. Then it is mapped as before. Vulkan and DirectX 12 write timestamps between commands and still resolve in the frame. - The end marker is a render pass that loads the frame's target and shades one pixel (1×1 scissor) with every colour write masked. A pass on the target runs after every pass that drew into it. Metal leaves the timestamps of a pass that does nothing at zero, which is why it shades one pixel. - The start stays a compute pass. A render-pass start, which loads and stores the whole target, gave the same numbers. Twenty extra marker passes cost about 0.18 ms at 720p, so one less pass is worth keeping. - A frame whose end comes before its start is left untimed. That happened in 45 of 128k frames, a start marker that ran late. ## Numbers (M4 Pro, Metal, RD 32, offscreen 1280×720, `perf_bench` flights; the Mac mini was shared, load 10–60) | scene | frame p50 | CPU p50 | GPU p50 before | GPU p50 after | |---|---|---|---|---| | village | 1.95 ms | 1.39 ms | 0.107 ms | **1.95 ms** | | mountain | 4.51 ms | 2.01 ms | 0.002 ms | **4.46 ms** | At RD 32 the M4 Pro is GPU-bound on the mountain flight: GPU time ≈ frame time, well above the CPU time. That is the case occlusion culling on Metal (4zd, first half) would help. ## Pictures The marker writes nothing (masked writes, 1×1 scissor). An offscreen screenshot of the same frozen view on main and on this branch differs in 0.08% of pixels, max 15/255. Two main runs of the same view differ in 28% (the sky and clouds). ## Self-check - `cargo fmt --all`, `cargo clippy --workspace --all-targets -- -D warnings`: clean. - No harness covers the GPU timer; the change is in `gfx/timing.rs` and its three call sites. ## Needs Ludovic Nothing. Label `skip-release`: it only changes a debug readout. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 04:09:57 +07:00
Sign in to join this conversation.
No milestone
No project
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.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#3496
No description provided.