Skip to content

Fewer render passes: layered passes for cube-map filtering, and compute dispatches run ahead of the pass they would split - #213

Closed
v0lumehi wants to merge 3 commits into
seedds:mainfrom
v0lumehi:gpu-spike
Closed

v0lumehi wants to merge 3 commits into
seedds:mainfrom
v0lumehi:gpu-spike

Conversation

@v0lumehi

Copy link
Copy Markdown
Contributor

Part of #142 / #159. Start at #142 to see how the PRs fit together and the merge order.

Based on main (5b0c11a), two commits. It conflicts with #135 in nvn_mtl.m; whichever lands second needs a small rebase, and I'll do it.

On Apple GPUs a render pass costs about 35–40 µs whatever it draws. Breath of the Wild has two patterns that create many passes. These commits remove them in the host, in a general form.

1. Slice-merged passes (DM_NVN_SLICE_MERGE, on by default)

Every tenth frame, BotW filters its environment cube map in 132 one-draw passes (22 levels × 6 faces, 64×64 down to 8×8). That frame had 284 render passes instead of ~150 and about 5 ms more GPU time. It shows as the regular spike in the Metal HUD's frame-time line.

  • Consecutive passes that each draw into another layer of one array or cube level now run as one layered pass. Only state, uniform updates and draws may lie between them: no game barrier, clear, copy or dispatch.
  • Each draw reaches its layer through a vertex amplification view mapping (count 1, renderTargetArrayIndexOffset).
  • A pipeline with the default maxVertexAmplificationCount of 1 ignores the mapping, so these draws use pipelines built with 2. They compile in the background on first use, and the passes stay separate until then.
  • A draw that samples the attachment, or a clear, ends the merged pass.
  • Microbenchmark (Metal 4, M2 Max): 132 such passes 5.4 ms with the host's barriers, 4.2 ms without, 22 layered passes 0.8 ms.

2. Compute dispatches run ahead of the pass (DM_NVN_CS_HOIST, on by default)

Metal can't run a dispatch inside a render pass, so every dispatch between two draws ends the pass. On a tile-based GPU, each split stores and reloads every attachment and pays a pass's fixed cost. BotW's NintendoWare VFX particles run one dispatch per emitter between the effect draws of the G-buffer and half-resolution passes.

  • When a draw opens a pass, the host scans ahead to the first op that would end the pass anyway.
  • A dispatch in that stretch runs before the pass opens when moving it changes nothing the draws before it read or write:
    • it writes no memory those draws read, samples no colour attachment of the pass and writes no image;
    • the draws write no memory.
  • Depth is exact. A dispatch that samples the pass's depth would see it as it was when the pass began. So the scan stops at such a dispatch once a draw in the stretch writes depth. BotW's particle collision samples the G-buffer depth, but no draw before it writes depth: 0 stops of that kind.
  • An earlier, inexact version was tried on the iPhone in Breath of the Wild performance on iPhone and Mac (60 → 120 fps, then 4K): start here #142 (run 1), at the lowered clock, and showed nothing beyond the scene-to-scene spread. That version could move a depth-sampling dispatch past a depth write; this one can't.

Measured

Mac, M2 Max, Zora's Domain, 900p, fps target 120. Each switch was run back to back off/on; values are means over 300-frame windows. Load averages were 4–10 during these runs, and differences of ~0.3 ms in p50 are within noise there. The two pairs ran at different times, so their baselines differ.

Switch frame p50 frame p95 window max GPU fps passes per frame
Slice merge 0 → 1 10.51 → 10.23 13.16 → 11.11 15.6 → 12.1 p95 13.0 → 8.7 92.6 → 97.5 cube-filter frame 284 → 173
CS hoist 0 → 1 (slice merge on) 9.92 → 8.97 10.70 → 9.98 11.55 → 10.76 p50 7.69 → 6.70 100.8 → 111.4 render 149 → 132, compute encoders 28 → 8

So the merge removes the spike (p95), and the hoist brings most of the p50 gain.

iPhone 17 Pro Max, manual play in the open world (not a fixed route, so only an indication):

  • GPU per frame: p50 ~13.3 → ~12.8 ms, p95 18–19 → 13.5–14.5 ms.
  • Dropped frames per 5 s: 0–25 → mostly 0.

Not measured: 4K, and games other than BotW.

Checked

  • Correctness, on BotW only:
    • The cube-filter frame's 173 render targets, dumped with and without each switch (DM_NVN_TEXDUMP=layers=100).
    • Slice merge: 105 targets identical, the rest within 1 % RMSE (the scene's rain).
    • Hoist: every 3D target within 0.7 % RMSE. Only the 2D overlay of the final picture differed: a loading banner's animation, timed by the wall clock.
    • Screenshots of the same scene, with and without the encode thread.
  • Local CI-equivalent run after the rebase on main: host tests, graphics CPU tests, every graphics test builds, dmhost for three profiles, the iOS compile check and the tool tests pass. t_nvn passes with BotW's profile (152 checks).
  • No unit test covers slice merging or the hoist itself. The checks above are the render-target dumps and screenshots.

Diagnostics and docs

  • The residency-set log line counts merged passes, layer switches, waits for an amplified pipeline, moved dispatches and why scans stopped. DM_NVN_PASSDUMP prints H lines for moved dispatches.
  • DM_NVN_TEXDUMP=layers=<n>:<path> dumps the first frame with at least n draws into single layers of small array or cube levels.
  • Reference entries in docs/reference/rendering.md and the symptom index, the game's note, and both switches in dm/gfx/README.md.

…ayer of one array or cube texture level, with nothing but state, uniform updates and draws between them (no game barrier, clear, copy or dispatch), run as one layered pass (DM_NVN_SLICE_MERGE, on by default)

Breath of the Wild filters its environment cube map every tenth frame in 132 one-draw passes (22 levels of 6 faces, 64x64 to 8x8), and on Apple GPUs a render pass costs about 35-40 us whatever it draws: that frame had 284 render passes instead of about 150 and about 5 ms more GPU time, the regular spike in the Metal HUD's frame-time line. A Metal 4 microbenchmark on the M2 Max: 132 such passes 5.4 ms with the host's barriers, 4.2 ms without, 22 layered passes 0.8 ms.

The draw that opens a pass looks ahead (sm_scan, sm_plan): when its only target is one layer of an array or cube level and the following targets switch to other layers of that level, the pass is opened over those layers (renderTargetArrayLength) and OP_SET_RT switches layers inside it. Each draw reaches its layer through a vertex amplification view mapping (count 1, renderTargetArrayIndexOffset); a pipeline with the default maxVertexAmplificationCount 1 ignores the mapping (every draw landed in layer 0), so these draws use pipelines built with 2 (sm_amp_get, AMP_MARK in the key, maxVertexAmplificationCount in the base key), compiled in the background (utility QoS) on first use while the passes stay apart. A draw that samples the attachment, or a clear, ends the merged pass. The encoder thread records the mapping (RO_R_AMP). The residency set line counts merged passes, layer switches and waits for an amplified pipeline.

Diagnostics: DM_NVN_TEXDUMP=layers=<n>:path dumps the first frame with at least n draws into single layers of small array or cube levels (the same frame with or without the switch).

Measured (Zora's Domain, 900p, FPS 120, M2 Max, back to back, means over 300-frame windows): frame p50 10.51 -> 10.23 ms, p95 13.16 -> 11.11 ms, window max 15.6 -> 12.1 ms, GPU p95 13.0 -> 8.7 ms, frame interval p99 14.3 -> 11.5 ms, 92.6 -> 97.5 fps; the cube filter frame 284 -> 173 render passes. Checked: the cube filter frame's 173 render targets dumped with and without the switch match (105 identical, the rest within 1% RMSE, the scene's rain); screenshots with and without the encoder thread; t_nvn (128 checks) and the tool tests pass. Reference entry in docs/reference/rendering.md, the game's note, the switch in dm/gfx/README.md
…N_CS_HOIST, on by default, 0 turns it off), exact for dispatches that sample the pass's depth: such a dispatch moves only while no draw before it in the pass writes depth

Metal cannot run a compute dispatch inside a render pass, so every dispatch between two draws of one pass ends it, and on a tile-based GPU each split stores and reloads every attachment and pays a pass's fixed cost. When a draw opens a pass, cs_hoist_try scans the ops after it up to the first one that would end the pass anyway; each dispatch met on the way runs right away, before the pass opens, when moving it ahead of the draws before it in that stretch changes nothing they read or write: it writes no memory those draws read (vertex, index, indirect, uniform and storage buffers; its storage buffers count as written), samples no colour attachment of the pass, writes no image, and the draws write no memory (no storage buffer, image or transform feedback). Replay then skips it (csh_take), and the pass stays open across it. A dispatch that samples the pass's depth would see it as the pass began, so the scan stops at such a dispatch once a draw of the stretch (the one opening the pass included) has depth writes on. Breath of the Wild's NintendoWare VFX particles run one dispatch per emitter between the effect draws of the G-buffer and of the half-resolution passes, and their collision samples the G-buffer depth: no draw before them writes depth (0 stops of that kind).

The residency set line counts moved dispatches, those sampling the pass's depth, and why scans stopped; DM_NVN_PASSDUMP prints H lines.

Measured (Zora's Domain, 900p, FPS 120, M2 Max, back to back, means over 300-frame windows, slice merging on in both): compute encoders 28 -> 8 and render passes 149 -> 132 a frame, frame p50 9.92 -> 8.97 ms, p95 10.70 -> 9.98 ms, window max 11.55 -> 10.76 ms, GPU p50 7.69 -> 6.70 ms, frame interval p99 11.1 -> 10.4 ms, 100.8 -> 111.4 fps. Checked: the cube-filter frame's 173 render targets dumped with and without (DM_NVN_TEXDUMP=layers=100) match within 0.7% RMSE for every 3D target (only the 2D overlay of the final picture differed: a loading banner's animation, timed by the wall clock); screenshots of the same scene; t_nvn (128 checks) and the docs test pass. Reference entry in docs/reference/rendering.md, the game's note, the switch in dm/gfx/README.md
seedds added a commit that referenced this pull request Oct 26, 2026
Integrate fast shared-event wakeups, per-segment publication, array-slice
pass merging, and independent compute dispatch hoisting from the PRs.
Serialize CPU completion effects before publishing completed fences and
validate recycled post records. Preserve submission order for early waits.

Guard scheduling against attachment aliases, first-draw and bindless
reads, indirect ranges, stencil writes, and explicit barriers. Add direct
Metal regression coverage and a gated CPU completion race test with TSAN.

Record final BotW and Metroid Dread gameplay validation with optimizations
on and off. BotW retains fewer render passes, but the measured 60 Hz M4
Pro runs do not establish a timing speedup. Keep the original higher-load
and iOS performance claims explicitly unverified.

Co-authored-by: v0lumehi <[email protected]>
@seedds

seedds commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Integrated into main in 6062518, co-authored with @v0lumehi. Layered slice merging and independent compute hoisting are retained, with conservative dependency guards.

The review confirmed missing hazards in the original optimization checks: the first draw could sample an attachment; aliases with different texture offsets and buffer-backed reads could overlap attachment storage; bindless/indirect accesses could have unknown ranges; stencil writes also create depth-stencil dependencies; and explicit barriers must stop hoisting. The fix checks those cases and preserves separate amplification-capable pipeline bases.

The optimizations now have direct tests: t_perf_order exercises real six-layer rendering, independent dispatch movement, and refusal of hazardous ordering. All 37 checks pass with direct encoding, deferred encoding, and all three optimizations disabled; the CPU completion regression's 8 checks also pass under TSAN. The full graphics suite, including current main's bundle/archive and profiling tests, documentation checks, both Mac hosts, and all 133 iOS source compiles pass. Optional shader-fixture checks without local fixtures were skipped.

Fourteen gameplay benchmark runs of the fixed implementation, before rebasing onto current main, cover BotW outdoors and Metroid Dread with switches on/off. BotW's repeated off/on/on/off comparison retains about 7.3% fewer render passes (158.55 -> 146.94), with both optimizations active. It does not demonstrate a timing win: GPU mean 7.051 -> 7.144 ms, submission CPU 3.456 -> 3.653 ms, both about 60 fps. One direct-encoding pair gave 7.331 -> 7.199 ms GPU time, which is insufficient to establish a repeatable gain. Dread remained about 59.9 fps with no obvious rendering regression, but neither pass optimization activated in that room. All runs completed without a red line or crash. Four further on/off execution checks after integrating current main reached frame 5,000 in BotW and 4,400 in Dread, all without errors. Display-link stalls and incomplete screenshots exclude those extra checks from the gameplay/timing evidence.

These M4 Pro / 60 Hz results do not verify the original M2 Max / 120 fps or iPhone gains. Measurements, exact source identities, routes and limits are in the BotW note and Dread note. Closing because the changes and review fixes are now incorporated into main.

@seedds seedds closed this Oct 26, 2026
@v0lumehi
v0lumehi deleted the gpu-spike branch October 26, 2026 09:29
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
Integrate fast shared-event wakeups, per-segment publication, array-slice
pass merging, and independent compute dispatch hoisting from the PRs.
Serialize CPU completion effects before publishing completed fences and
validate recycled post records. Preserve submission order for early waits.

Guard scheduling against attachment aliases, first-draw and bindless
reads, indirect ranges, stencil writes, and explicit barriers. Add direct
Metal regression coverage and a gated CPU completion race test with TSAN.

Record final BotW and Metroid Dread gameplay validation with optimizations
on and off. BotW retains fewer render passes, but the measured 60 Hz M4
Pro runs do not establish a timing speedup. Keep the original higher-load
and iOS performance claims explicitly unverified.

Co-authored-by: v0lumehi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants