Skip to content

perf(host): batch Apple Metal glass captures, blur into slots, end adaptive settle early - #575

Merged
CommaBot merged 1 commit into
mainfrom
perf/apple-metal-batched-glass-settle
Dec 6, 2026
Merged

CommaBot merged 1 commit into
mainfrom
perf/apple-metal-batched-glass-settle

Conversation

@CommaBot

@CommaBot CommaBot commented Dec 6, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Apple Metal (apple_render.m) glass and present-pipeline optimization: every tested benchmark and fixture frame is byte-identical to main (with one intentional bugfix in item 3: switching back to a previously cached backdrop blur now updates adaptive glass instead of staying stuck on the prior backdrop's stale state).

  1. blur_into — blur straight into the slot. A glass blur's last pass (dual-scale, progressive, contained, Gaussian, halved) is written directly into its cache slot's texture, and a no-pass grab is blitted from the target straight into it. The slot texture is sized exactly as the level the blur used to be left in (the level's size where the old code swapped textures, else the 64-px-aligned compact size), so the glass samples a texture of the same size and the normalized coordinates round the same way. Removes the post-blur slot_fill copy, and the level reallocation in the swap path.
  2. Lookahead captures. When a top-level capture misses and breaks the frame's pass, the glass groups after it whose rectangle nothing drawn since reaches (region_untouched, merged liquid over its run's bounds) are captured in the same break. Their adaptive updates go into one ad_run_updates_batch and their bare words' into one veil pass (veil_run_updates_items), and each is drawn on as kept when reached. Kept blurs of groups in between are pinned, and their words' state is eased in the same pass. The lookahead stops at the first group that cannot be captured yet (something drawn reaches it, a lens or blur layer between, a picture not loaded) instead of skipping past it. That keeps blurs in frame order, so ltmp, the slot LRU and eviction are exactly as before. Skipping past a group reordered blurs and produced 1-LSB differences.
  3. Adaptive settle convergence. Once a slot's backdrop has held still 60 ms, its update's 16-bit state and latch are blitted into a 3-buffer MTLStorageModeShared ring and compared in the command buffer's completion handler, with no CPU/GPU wait. A slot whose state no longer changes ends its 0.42 s settling (as gles_render.h does). Each adaptive slot records which blur it last sampled (cap_gen, a 64-bit slot generation). Sampling a different blur, even a kept one, counts as a backdrop change. This also fixes an existing bug: when the backdrop changed back to one whose blur was still kept (A→B→C→A), adaptive glass was not updated and could stay stuck on the previous backdrop's state. A settle step's frame whose glass has all settled is not drawn at all (only with a valid canvas and no GPU failure or missed drawable), and the readback is taken only if the canvas command buffer also completed. Late handlers are epoch-guarded against ad_forget. A dt under 4 ms is never taken as converged, because an easing step that small can round to no change.
  4. MBFrame.is_settle. A settle step's frame taken over by the app's frame before it is drawn is no longer counted in mb_host_frames_dropped(). An app frame taken over by another app frame still is. A settle step never replaces an app frame that is waiting to be drawn: it joins its parts to that frame and keeps the frame as is, including its latency mark, ground and size.
  5. macOS settle cap. Idle adaptive settle steps in mac_app.m are capped at 60 Hz (lastSettleMs, 15 ms gate), as on iOS and Android.

Not done: removing the lv[0] copy. It would need offset sampling in fpost, a shader change that has to be mirrored on all four backends. It also samples a texture of another size, which this work showed flips LSBs (item 1), so it cannot be byte-exact.

Verification

  • New packages/host/tests/apple_render_batch_test.m, wired into scripts/check-ios-host.sh:
    • blur_into against the previous level path: 362 grabs at 1x/2x/3x, all 9 blur modes, 0 texels differ.
    • Lookahead on vs off at 390x844@3x and 1280x800@2x: 0 frames differ. Render encoders go 264 → 226/224 and blits 59/60 → 44. Behind a badge drawn since, or under glass drawn since, nothing is captured ahead and the frames are the same.
    • Settle frame taken over by an app frame: not counted as dropped. Two app frames: counted once.
    • Backdrop coming back through a kept blur (A, B ×79, C ×19, A again at 1x), both a short one and one after a long rest drawn the way the film path draws (settle steps only while settling): readback on vs off 0 frames differ (160 and 220 frames), and the glass is settling again when A returns and eases back to A's state.
    • A settle step while an app frame is pending: the same frame object is kept, with its lat, clear and is_settle unchanged and the settle parts joined in.
    • A glass run of 96 instances: lookahead on vs off 0 frames differ. With one instance reaching the next group, nothing is captured ahead.
    • Convergence: with a light backdrop moving under the glass, settling ends 9 frames after the last change instead of 51. An instant dark change and a 30-frame ramp still ease (settling every frame they change). 0/100 frames differ from time-only settling in all three cases.
  • bash scripts/check-ios-host.sh --with-native-tests: all Apple native tests pass. mac_app.m compiles with -Wall -Werror with 0 warnings. apple_render.m has only the same macOS 27 SDK deprecation diagnostics as main.
  • Renderer-level A/B fixture against main: 1,180 frames (a 60-case sweep of blur, tint and shape at 1x/2x/3x; scroll and settle animations at 390x844@3x, 1280x800@2x and 400x300@1x in light and dark, with partial-damage frames; the present path). 0 frames differ. Render encoders -14%, blits -41%.

Real app scroll A/B (offscreen Metal, Apple M5 Max, 5 interleaved pairs, medians)

The same app scene before and after, a 40-step wheel scroll then 0.9 s idle, rendered frame by frame through the film path (mb_render_read / mb_render_read_step). Each frame was hashed. With consecutive duplicate frames collapsed, the hash sequences are identical: every frame the change does not draw is an exact repeat of the frame before it. Final PNGs are byte-identical (390x844@3x 392,634 B / 375,714 B; 1280x800@2x 593,432 B / 580,410 B).

scene frames drawn settle frames blurs render encoders blits GPU ms total per scroll frame: enc / blit / GPU ms
390x844@3x light 51 → 31 (-39.2%) 46 → 25 (-45.7%) 122 → 122 (0%) 1201 → 1134 (-5.6%) 293 → 205 (-30.0%) 98.16 → 73.43 (-25.2%) 52.09 → 50.95 (-2.2%) / 12.14 → 9.09 (-25.1%) / 3.18 → 3.00 (-5.5%)
390x844@3x dark 51 → 31 (-39.2%) 46 → 25 (-45.7%) 126 → 126 (0%) 1233 → 1166 (-5.4%) 305 → 213 (-30.2%) 123.80 → 82.12 (-33.7%) 53.55 → 52.41 (-2.1%) / 12.68 → 9.45 (-25.4%) / 3.79 → 3.59 (-5.2%)
1280x800@2x light 119 → 102 (-14.3%) 108 → 87 (-19.4%) 255 → 255 (0%) 2534 → 2425 (-4.3%) 622 → 483 (-22.3%) 252.18 → 222.81 (-11.6%) 46.33 → 44.98 (-2.9%) / 10.85 → 8.58 (-20.9%) / 3.40 → 3.24 (-4.7%)
1280x800@2x dark 119 → 102 (-14.3%) 108 → 87 (-19.4%) 255 → 255 (0%) 2534 → 2425 (-4.3%) 622 → 483 (-22.3%) 331.85 → 287.33 (-13.4%) 46.33 → 44.98 (-2.9%) / 10.85 → 8.58 (-20.9%) / 3.95 → 4.01 (+1.5%)

In this scene, lookahead finds little to batch. The bar's glass groups each read the glass drawn just before them (a pill's capture margin covers its neighbour, and the scroll edge sits under the pills), so they form a real dependency chain. Batching across such a chain is not possible without changing what each capture sees.

🤖 Generated with Claude Code

@CommaBot
CommaBot force-pushed the perf/apple-metal-batched-glass-settle branch 2 times, most recently from f6c641a to 285475b Compare December 6, 2026 01:06
…aptive settle early

Zero visual change: every frame is byte-identical to before.

- blur_into: a glass blur's last pass is written straight into its cache
  slot's texture (sized exactly as the level it used to be left in, so the
  glass samples a texture of the same size), removing the post-blur
  slot_fill copy (or the level texture swap and reallocation).
- Lookahead captures: when a top-level capture misses and breaks the
  frame's pass, the glass groups after it whose rectangle nothing drawn
  since reaches (region_untouched) are captured in the same break, with
  their adaptive updates in one ad_run_updates_batch and their bare words'
  in one veil pass. The lookahead stops at the first group that cannot be
  captured yet (something drawn reaches it, a lens or blur layer between,
  a picture not loaded), so blurs are made in frame order and every scratch
  texture and slot is as it would be: byte-identical, unlike skipping it.
- Adaptive settle convergence: once a slot's backdrop has held still 60 ms,
  its update's 16-bit state and latch are blitted into a 3-buffer shared
  ring and compared in the command buffer's completion handler (no CPU/GPU
  wait); a slot whose state no longer changes ends its 0.42 s settling at
  its next update, and a settle step's frame whose glass has all settled is
  not drawn. Epoch-guarded against ad_forget; dt below 4 ms is never taken
  as converged.
- A settle step's frame taken over by the app's before it is drawn is no
  longer counted in mb_host_frames_dropped.
- macOS: idle adaptive settle steps capped at 60 Hz, as iOS and Android.

Tests: apple_render_batch_test (run by check-ios-host.sh) checks blur_into
against the previous level path for every blur mode, lookahead on/off
byte parity with fewer encoders and copies (and no capture ahead behind a
badge or under glass drawn since), settle frames not counted as dropped,
and early convergence with byte-identical frames.

Co-authored-by: zuozijian <26162189+zuozijian@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CommaBot
CommaBot force-pushed the perf/apple-metal-batched-glass-settle branch from 285475b to d7b05b5 Compare December 6, 2026 01:16
@CommaBot
CommaBot merged commit b1251f4 into main Dec 6, 2026
3 checks passed
@CommaBot
CommaBot deleted the perf/apple-metal-batched-glass-settle branch December 6, 2026 01:23
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