Skip to content

Pulling vertex shader variants in the translation chain (<key>_bp) - #123

Merged
seedds merged 1 commit into
seedds:mainfrom
v0lumehi:pso-b-pull
Oct 26, 2026
Merged

seedds merged 1 commit into
seedds:mainfrom
v0lumehi:pso-b-pull

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 #122 (part 2 of the pipeline series): this PR contains its commits; only the last commit is new here.

Update: #122 landed on main as 66abbc3, and this branch now builds on main: it holds only its own commit.

What

A side library for every vertex shader with stage inputs, as <key>_bv is. Its main0 / main0_z have no [[stage_in]] and read their inputs from a per-draw layout block at buffer 16. The block holds, per stream, the base address, stride and divisor, and per NVN attribute its state word; the base instance comes from [[base_instance]]. dm_fetch (dmsh_pull.metal) decodes the NVN format at run time into the register words Maxwell's fetch gives, so the shader body runs unchanged. A pipeline made with it has no vertex descriptor and serves every vertex layout a pair is drawn with.

Why

A draw can then use the pair's pipeline while the pipeline for its own vertex layout is still being built (the hybrid pipelines in the next PR).

Correctness

The words are bit-identical to Metal's vertex-descriptor fetch for every format vfmt knows, and to the host's CPU conversion for the integer-read-as-float formats:

  • unorm / snorm go through the unpack_* builtins; a plain division under fast math is one ULP off for up to half the values;
  • R11G11B10F NaNs come out canonical, as from Metal's fetch;
  • divisors follow Metal's rule, base + (instance - base) / divisor.

Positions are [[invariant]] and built with preserved invariance. Transform feedback, geometry capture and layer entries get no variant.

Cost

The run-time decode makes a vertex-bound draw with 8 attributes 65 % slower on an M2 Max, about 1 ms per million vertices, so the variant only bridges frames.

The iOS app does not use the variants yet, so build_ipa.sh leaves the _bp libraries out of the IPA unless IOS_PULL=1 (pack_ipa.py --skip-suffix).

Numbers

Breath of the Wild's full Mac build took 25 min (a full retranslation: the chain fingerprint changed):

  • 19,592 shaders built, and 8,888 pulling libraries (275 MB), none failed;
  • every existing library is byte-identical to the previous build.

Checked

  • t_pull.m (304 checks): 76 formats; 73 comparisons with Metal's fetch at aligned, 2-byte and stride-1 placements; 24 comparisons with the CPU conversion; stride 0 and divisors.
  • t_pull_inv.m: 2,000 random triangles, a depth prepass with one pipeline and depth-equal with the other, both ways round.
  • test_dmsh_pull.py: compiles for macOS and iOS.
  • pack_ipa.py --skip-suffix by hand.
  • The other graphics tests and the host unit tests give main's results.

Not checked

  • Other profiles' shader builds.
  • An IPA built with IOS_PULL=1.

@v0lumehi
v0lumehi force-pushed the pso-b-pull branch 3 times, most recently from 494a2f7 to dbb31de Compare October 24, 2026 17:28
@v0lumehi
v0lumehi force-pushed the pso-b-pull branch 2 times, most recently from bfc55d9 to 86b21c4 Compare October 25, 2026 12:42
v0lumehi added a commit to v0lumehi/nxnative that referenced this pull request Oct 25, 2026
… change)

nvn_mtl.m (9,473 lines) keeps its includes, the residency set, deferred release, mtl_init and the memory pools (557 lines with the include lines); the rest moved, in contiguous blocks and in the original order, into 14 files nvn_mtl_<topic>.inc beside it that nvn_mtl.m #includes at the place they were cut from: submit, ubuf, tex, pass, shader, state, pso, args, lookahead, merge, indirect, draw, shot, present. One translation unit, every static still shared; the Makefiles, tests (MTL_T_SRC) and the iOS project compile the same single file. The .inc extension (as nvn_geometry.inc and rt_range_check.inc) keeps the parts out of the Makefiles' nvn/*.m wildcard; dm/host/Makefile's GFX_H already lists nvn/*.inc on main, so make rebuilds when a part changes. Each part opens with a comment saying what it holds; dm/gfx/README.md lists the parts.

Cut again on main with the pipeline series (seedds#123-seedds#125): the parts start at the same top-level blocks as before.

Checked, against the same tree unsplit:
- The parts with their header comments removed, put back in place of the include lines, give the unsplit nvn_mtl.m byte for byte.
- clang -E -P of nvn_mtl.m with the host's flags (Mac) and with iOS flags (arm64-apple-ios18.0, apple-a18): identical text apart from the line number argument of res_add_at (35 sites, __LINE__ now counts from the part's top; the sites stay distinct).
- Object code (otool -tvV of nvn_mtl.m compiled with the Makefile's flags): the same instructions at the same addresses on both platforms; the only differences are those line immediates. Same compiler warnings.
@v0lumehi
v0lumehi force-pushed the pso-b-pull branch 3 times, most recently from 3b378b5 to dd7cd3f Compare October 25, 2026 23:41
…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.
@seedds
seedds merged commit e6b800a into seedds:main Oct 26, 2026
4 checks passed
@v0lumehi
v0lumehi deleted the pso-b-pull 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