Skip to content

Hybrid pipelines: draw with the pair's pulling pipeline while the draw's own pipeline is built, on #75's demand-first builds - #124

Merged
seedds merged 3 commits into
seedds:mainfrom
v0lumehi:pso-c-hybrid
Oct 26, 2026
Merged

seedds merged 3 commits into
seedds:mainfrom
v0lumehi:pso-c-hybrid

Conversation

@v0lumehi

@v0lumehi v0lumehi commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Part of #142: Breath of the Wild performance on iPhone and Mac (60 → 120 fps, then 4K). Start there to see how the PRs fit together and the merge order.

Builds on #123 (part 3 of the pipeline series): this PR contains its commits; the last two commits (the hybrid and a docs diagram) are new here.

What

On a pipeline miss the draw first takes its pipeline without compiling: its base made already or in the archive, then a specialization. Failing that, it draws with the pair's pulling pipeline (<key>_bp, pull_bind), specialized with the draw's colour state, while its own pipeline is built in the background; a later draw takes that one. Both compute the same positions, so a draw that switches neither flickers nor z-fights. Only a pulling base already made or archived is used; otherwise the draw waits, as before, and the base is built for the pair's next miss. Merged passes, readable-tile and diagnostic fragments, and layer-writing or transform feedback vertex shaders have no stand-in. DM_PSO_HYBRID=0 turns it off. It needs the Metal 4 path, so it is on by default on the Mac only.

Miss path

flowchart TD
    D[Draw needs a pipeline] --> C{In the cache?}
    C -- yes --> R[Draw with it]
    C -- no --> B{Base made already<br/>or in the archive?}
    B -- yes --> S[Specialize, a few µs] --> R
    B -- no --> P{Pulling stand-in<br/>for the pair?}
    P -- yes --> H[Draw with the pulling pipeline now]
    H --> Q[Queue the draw's own pipeline first<br/>pso_pend_demand, low priority]
    Q -. later draws .-> R
    P -- no --> W[Build at once and wait<br/>as on main after #75]
    W --> R
    W --> K[Then predict: at most 2 speculative builds,<br/>only for draws without a stand-in]
Loading

How it builds on #75 (demand-first)

  • pso_pend_kick stays the speculative admission as NVN: prioritize demanded PSOs and bound speculative precompile #75 made it: at most two running, at most two per prediction, and the oldest finished entry reclaimed when the table is full. t_pso_pending passes unchanged.
  • A draw with a stand-in queues its own pipeline before the prediction runs (pso_pend_demand). Demanded builds are outside the speculative limit and run on serial queues at utility QoS, one per two performance cores (pp_bg_queue). A demanded build never waits behind speculation, and speculation never waits behind it.
  • The prediction spends its budget only on draws that will have no stand-in, since only those would wait.
  • A draw that would wait for a build still queued makes the pipeline itself at once (pso_pend_take). Breath of the Wild's cold start had waited 0.5–0.9 s for one pipeline behind a burst.
  • The first miss of a submission loads, in parallel, the shader libraries the rest of the submission binds (shader_prefetch_rest). This helps the waiting path too: 248 libraries took about 210 ms, where loading them one by one on the submit thread took about 1 s.

Diagnostics

  • A pipelines up to frame line every 300 frames: submit-thread pipeline, library and prediction time; archive lookups; specializations; base compiles; hybrid draws; background builds; the draws that waited, and why.
  • DM_PSO_WAITLOG=1 logs each wait over 20 ms with its pair.
  • DM_PSO_PULL_ALL=1 makes every draw that has a pulling pipeline draw with it.

Numbers

Breath of the Wild, Mac (M2 Max), Zora's Domain. Steady-state GPU p50 over frames 2,400–5,100: 8.98 ms with the hybrid, 8.98 ms without it, 9.35 ms through the MTLDevice. The cold-start numbers need the next PR's prebuild.

Checked

  • t_pull (346 checks, now also the layout block the host fills), t_pull_inv, t_pso_pending (48) and t_pso_archive (32).
  • The other graphics tests and the host unit tests give main's results.
  • BotW screenshot series from cold starts show no flicker, and the final picture is correct.

Not checked

  • Other games in play.
  • Every combination of merged passes with stand-ins (they get none by design).

@v0lumehi

Copy link
Copy Markdown
Contributor Author

A question about the docs: this PR adds a Mermaid diagram of the miss path to docs/development.md (in the "Hybrid pipelines" section; GitHub renders it). The docs have no diagrams so far.

  • Are diagrams welcome in the docs, or would you rather keep them as text only? If you'd rather not, I'll drop the commit.
  • If they are welcome: would an architecture overview be useful, say docs/architecture.md with a few diagrams? It would cover how a game gets from the NSP to running (extraction, rewriter, shader chain, packing), the host's layers (loader, HLE services, NVN / Vulkan / GL on Metal, present path), and a frame's way through the graphics layer. It would be a separate PR.

…sh_pull.py with its decoder dmsh_pull.metal)

A side library of every vertex shader with stage inputs, as <key>_bv is, whose main0 / main0_z have no [[stage_in]] and read their inputs from a per-draw layout block at buffer 16: per stream the base address, stride and divisor, per NVN attribute its state word, the base instance from [[base_instance]] (so indirect draws are right too). dm_fetch decodes the NVN format at run time into the register words Maxwell's fetch gives the shader, and the input takes them with as_type, so the body runs unchanged. A pipeline built with it has no vertex descriptor and serves every vertex layout the pair is drawn with; the hybrid pipelines (next change) draw with it while a draw's own pipeline is built.

The words are bit-identical to Metal's vertex-descriptor fetch for every format nvn_mtl.m vfmt knows and to the host's CPU conversion for the integer-read-as-float formats: unorm / snorm through the unpack builtins (a plain division under fast math is a unit in the last place off for up to half the values), R11G11B10F NaNs canonical as from Metal, divisors with Metal's rule base + (instance - base) / divisor. Positions are [[invariant]] and built with preserved invariance, so a pulling pipeline and the vertex-descriptor one compute the same positions. Transform feedback, geometry capture and layer entries get no variant; shaders without stage inputs need none. The run-time decode costs GPU time: +65 % of a vertex-bound draw with 8 attributes on an M2 Max, about 1 ms per million vertices, so it is meant only to bridge the frames until the draw's own pipeline is built.

The iOS app does not use the variants yet (the Metal 4 path is off on iOS by default), so build_ipa.sh leaves the _bp libraries out of the IPA unless IOS_PULL=1 (pack_ipa.py --skip-suffix).

Chain version 8393d85a4eae (on main's 20fe3d62b9f6). Breath of the Wild's full Mac build on chain 97e2f301eb73 (25 min, a full retranslation since the code fingerprint changed): 19,592 shaders ok, 8,888 pulling libraries generated (275 MB), none failed, 43 vertex shaders without stage inputs; every existing library is byte-identical to the previous build (19,592 _b.metallib and _b.metal, 6,254 _bf, 1,070 _bv). Other profiles' shaders were not rebuilt.

Tests: t_pull.m (304 checks: the 76 formats vfmt knows, 73 comparisons with Metal's fetch at aligned, 2-byte and stride-1 placements, 24 with the CPU conversion, stride 0 and divisors 1-3 with a base instance), t_pull_inv.m (2,000 random triangles: a depth prepass with one pipeline and depth-equal with the other passes every covered pixel, both ways round) and test_dmsh_pull.py (the generator: entries, ids it adds or keeps, entries left out, shaders declined; compiles for macOS and iOS); pack_ipa.py's --skip-suffix checked by hand. The other graphics tests and the host unit tests give the results they give on main. docs/reference/rendering.md ("Pulling vertex inputs") describes the variant and its cost; AGENTS.md lists the new chain files.
…ts shader pair's pulling pipeline meanwhile, on main's demand-first pipeline builds

On a miss the draw first takes its pipeline without compiling (its base made or in the archive, then a specialization). Failing that it draws with the pair's pulling pipeline (<key>_bp: no vertex descriptor, the streams read through a per-draw layout block at buffer 16, pull_bind), specialized with the draw's colour state, while its own pipeline is built in the background; a later draw takes that one. Both compute the same positions (t_pull_inv), so a draw that switches does not flicker or z-fight. Only a pulling base already made or archived is used; otherwise it is built in the background for the pair's next miss and the draw waits. Merged passes, readable-tile and diagnostic fragments, and layer-writing or transform feedback vertex shaders have no stand-in. DM_PSO_HYBRID=0 turns it off; it needs the Metal 4 path, so it is on by default on the Mac only.

On main's demand-first pipelines (seedds#75): pso_pend_kick stays the speculative admission (at most two running, at most two per prediction, the oldest finished entry reclaimed when the table is full). A draw with a stand-in queues its own pipeline before the prediction runs (pso_pend_demand): outside the speculative limit, on serial queues at utility QoS, one per two performance cores (pp_bg_queue), so a demanded build never waits behind speculation and speculation never waits behind it. The prediction spends its budget only on draws that will have no stand-in, since only those would wait. A draw that would wait for a build still queued makes the pipeline itself at once (pso_pend_take; Breath of the Wild's cold start waited 0.5-0.9 s for one pipeline behind a burst before). The first miss of a submission loads the shader libraries the rest of it binds in parallel (shader_prefetch_rest): 248 libraries in about 210 ms where one by one on the submit thread took about 1 s, in the hybrid and the waiting path alike.

Diagnostics: a "pipelines up to frame" line every 300 frames (submit-thread pipeline, library and prediction time, archive lookups, specializations, base compiles, hybrid draws, background builds, the draws that waited and why), DM_PSO_WAITLOG=1 (each wait over 20 ms with its pair), DM_PSO_PULL_ALL=1 (every draw with a pulling pipeline draws with it), and the longframe line's count of hybrid draws.

Breath of the Wild, Mac (M2 Max), steady state in Zora's Domain: mean GPU p50 over frames 2,400-5,100 was 8.98 ms with the hybrid, 8.98 ms on the Metal 4 path without it, 9.35 ms through the MTLDevice. Cold-start numbers are with the prebuild of the next change.

Tests: t_pull (346 checks, now also the layout block the host fills), t_pull_inv, t_pso_pending (48) and t_pso_archive (32) pass; the other graphics tests and the host unit tests give the results they give on main. docs/development.md ("Hybrid pipelines") and dm/gfx/README.md describe it.
…nd specialization, pulling stand-in with the draw's own pipeline queued first, or a build at once with the bounded prediction)
@seedds
seedds merged commit 8593d85 into seedds:main Oct 26, 2026
4 checks passed
@v0lumehi
v0lumehi deleted the pso-c-hybrid branch October 26, 2026 09:29
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