Repository navigation
perf(host): batch Apple Metal glass captures, blur into slots, end adaptive settle early - #575
Merged
Merged
Conversation
CommaBot
force-pushed
the
perf/apple-metal-batched-glass-settle
branch
2 times, most recently
from
December 6, 2026 01:06
f6c641a to
285475b
Compare
…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
force-pushed
the
perf/apple-metal-batched-glass-settle
branch
from
December 6, 2026 01:16
285475b to
d7b05b5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Apple Metal (
apple_render.m) glass and present-pipeline optimization: every tested benchmark and fixture frame is byte-identical tomain(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).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-blurslot_fillcopy, and the level reallocation in the swap path.region_untouched, merged liquid over its run's bounds) are captured in the same break. Their adaptive updates go into onead_run_updates_batchand 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, soltmp, the slot LRU and eviction are exactly as before. Skipping past a group reordered blurs and produced 1-LSB differences.MTLStorageModeSharedring 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 (asgles_render.hdoes). 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 againstad_forget. Adtunder 4 ms is never taken as converged, because an easing step that small can round to no change.MBFrame.is_settle. A settle step's frame taken over by the app's frame before it is drawn is no longer counted inmb_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.mac_app.mare 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 infpost, 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
packages/host/tests/apple_render_batch_test.m, wired intoscripts/check-ios-host.sh:blur_intoagainst the previous level path: 362 grabs at 1x/2x/3x, all 9 blur modes, 0 texels differ.lat,clearandis_settleunchanged and the settle parts joined in.bash scripts/check-ios-host.sh --with-native-tests: all Apple native tests pass.mac_app.mcompiles with-Wall -Werrorwith 0 warnings.apple_render.mhas only the same macOS 27 SDK deprecation diagnostics asmain.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).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