Skip to content

Render pipelines through MTL4Compiler, with a per-game pipeline archive - #122

Closed
v0lumehi wants to merge 1 commit into
seedds:mainfrom
v0lumehi:pso-a-archive
Closed

v0lumehi wants to merge 1 commit into
seedds:mainfrom
v0lumehi:pso-a-archive

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.

One commit on main.

What

Render pipelines are built through Metal 4's MTL4Compiler. pso_build turns pso_desc's descriptor into a base whose colour attachments (format, blending, factors, operations, write mask) are unspecialized. It builds that base once and makes every pipeline from it by specialization. The bases go into a per-game archive (pso_archive/ beside pso_user.tsv): one file per flush, written by an MTL4PipelineDataSetSerializer that captures binaries only. Every file is opened at start, and a base found in one is taken from it directly. Once there are more than 8 files, the archive is compacted: the bases the prewarm takes from the old files are captured again through the compiler, written into one file, and the old files are removed. Compiles and flushes are serialized by a read-write lock.

Why

A pipeline whose colour state alone differed took 3–5 ms to build; a specialization takes about 4 µs on an M2 Max. A base taken from the archive costs about 0.06 ms, against 10–80 ms to build one. Breath of the Wild's 4,829 recorded pipeline keys come from 1,818 bases. 2,845 of the keys differed only in the blend factors of targets whose blending is off, which the key no longer holds (pso_key_norm).

On by default on the Mac only. On iOS DM_PSO_MTL4=1 turns it on: the archive's memory is not measured against an iPhone's limit yet, and metal-tt rejects the Metal 4 serializer's pipeline script, so no archive can be made offline for an iPhone. DM_PSO_MTL4=0 keeps the MTLDevice path. So does a device without a Metal 4 compiler (t_pso_pending's fake device), and so do functions with no Metal 4 descriptor (the DM_NVN_HILITE / DM_NVN_MAGSCAN diagnostics).

Checked

  • t_pso_archive, 32 checks with the validation layer on:
    • specialized pipelines draw the same pixels as MTLDevice ones, into a pass with a depth attachment the base does not name;
    • a flushed file gives the base back without compiling;
    • an unregistered function keeps the MTLDevice path;
    • key normalization;
    • nine files compacted into one that gives every base back without compiling.
  • t_pso_pending passes (48 checks).
  • The other graphics tests and the host unit tests give main's results here. t_refl, t_fuse_check, t_reorder_chain, t_defer_chain, t_mip_extent, t_rt_range_ro and t_rt_range fail on main as well.
  • Breath of the Wild on the Mac: steady-state GPU p50 was 8.98 ms against 9.35 ms through the MTLDevice.

Not checked

  • iOS (off there).
  • Compaction in a real run (only in the unit test).
  • Other games' pipelines.

… colour state unspecialized, specialized per colour state, and a per-game pipeline archive of the bases

pso_desc still describes each pipeline. pso_build turns it into an MTL4RenderPipelineDescriptor whose colour attachments (format, blending, factors, operations, write mask) are unspecialized, builds that base once and makes each pipeline from it by specialization: about 4 µs on an M2 Max, where a pipeline whose colour state alone differed took 3-5 ms to build. Metal 4 pipelines name no depth or stencil format, so one base serves every depth / stencil attachment. Breath of the Wild's 4,829 recorded pipeline keys came from 1,818 bases, and 2,845 of the keys differed only in the blend factors of targets whose blending was off, which the key no longer holds (pso_key_norm; the depth / stencil formats stay in the key for pipelines built through the MTLDevice). MTL4Compiler takes function descriptors, so every function made from a library is noted with its library, entry name and constant values as it is made (fn4_note), keyed by identity. A function with no note (the diagnostic DM_NVN_HILITE and DM_NVN_MAGSCAN overrides) keeps the MTLDevice path, and so does a device without a Metal 4 compiler (t_pso_pending's fake device).

The archive: pso_archive/ beside pso_user.tsv, files written by an MTL4PipelineDataSetSerializer that captures binaries only (with the descriptor capture as well it writes no archive and reports no error). Each flush (after the prewarm, and 10 s after a burst of new bases) writes what was captured since the one before; at start every file is opened and a base found in one is taken from it directly, about 0.06 ms against 10-80 ms to build one. Compiles hold a read lock and a flush the write lock (g_arch_rw): a compile that finished during a flush crashed in Metal. With more than 8 files the archive is compacted: the bases the prewarm takes from them are taken again through the compiler with those files as its lookup archives, which captures them, the flush after the prewarm writes them into one file and the old files are removed (Breath of the Wild wrote 5-7 files per start).

On by default on the Mac only. On iOS DM_PSO_MTL4=1 turns it on: the archive's memory is not measured against an iPhone's limit yet, and metal-tt rejects the Metal 4 serializer's pipeline script, so no archive can be made offline for an iPhone. DM_PSO_MTL4=0 builds through the MTLDevice as before; DM_PSO_ARCHIVE=off or =<folder>.

Tests: t_pso_archive (32 checks, validation layer on): pipelines differing only in colour state share one base and draw the same pixels as pipelines built through the MTLDevice, into a pass with a depth attachment the base does not name; a flushed archive file gives the base back without compiling; an unregistered function keeps the MTLDevice path; the key normalization; compaction of nine files into one that gives every base back without compiling. t_pso_pending passes (48). The other graphics tests give the results they give on main here (t_refl, t_fuse_check, t_reorder_chain, t_defer_chain, t_mip_extent, t_rt_range_ro and t_rt_range fail on main as well); the host unit tests pass. docs/development.md and dm/gfx/README.md describe the bases and the archive.
seedds added a commit that referenced this pull request Oct 25, 2026
Adapt PR #122: compile reusable bases with unspecialized colour state, specialize per draw state, and persist bases between launches. Keep iOS opt-in and the MTLDevice fallback.

Preserve rasterizing depth-only pipelines through the legacy path. Share in-progress base builds, including errors, while unrelated bases compile in parallel. Defer timed flushes during compaction and finish flush/cleanup together; synchronize the pending count with serializer capture.

Checked on Apple M4 Pro: t_pso_archive 48 checks with Metal validation, t_pso_pending 48, t_integer_blend 63, t_geometry 95, and t_pso_skey pass. The Breath of the Wild Mac host builds and nvn_mtl.m passes the iOS arm64 syntax check. Real-game memory usage and iPhone runtime behavior were not measured.

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

seedds commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Landed on main in 66abbc3, with co-author credit to @v0lumehi. Thanks for the pipeline-base and archive implementation.

The landed version includes these fixes:

  • Rasterizing pipelines without a fragment function use the MTLDevice path, preserving depth-only writes while respecting Metal 4's descriptor requirements.
  • Concurrent requests for the same base share an in-progress compilation and its result; unrelated bases can still compile in parallel.
  • Timed archive flushes defer during compaction's prewarm, and the final flush and cleanup run together. The pending count is synchronized with serializer capture under the compile/flush lock.

Checked on an Apple M4 Pro: t_pso_archive passes 48 checks with Metal validation enabled, t_pso_pending 48, t_integer_blend 63, t_geometry 95, and the pipeline-key round-trip tests pass. The Breath of the Wild Mac host builds, and nvn_mtl.m passes the iOS arm64 syntax check. Real-game memory usage and iPhone runtime behavior remain unmeasured; iOS stays opt-in.

Closing this PR because its implementation, with the fixes above, is now on main.

@seedds seedds closed this Oct 25, 2026
@v0lumehi
v0lumehi deleted the pso-a-archive 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
Adapt PR seedds#122: compile reusable bases with unspecialized colour state, specialize per draw state, and persist bases between launches. Keep iOS opt-in and the MTLDevice fallback.

Preserve rasterizing depth-only pipelines through the legacy path. Share in-progress base builds, including errors, while unrelated bases compile in parallel. Defer timed flushes during compaction and finish flush/cleanup together; synchronize the pending count with serializer capture.

Checked on Apple M4 Pro: t_pso_archive 48 checks with Metal validation, t_pso_pending 48, t_integer_blend 63, t_geometry 95, and t_pso_skey pass. The Breath of the Wild Mac host builds and nvn_mtl.m passes the iOS arm64 syntax check. Real-game memory usage and iPhone runtime behavior were not measured.

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