Skip to content

Metal: bound the memory kept for off-screen geometry (iPhone morph crash) - #924

Merged
javierbq merged 2 commits into
masterfrom
fix/metal-gpu-cache-cap
Nov 12, 2026
Merged

javierbq merged 2 commits into
masterfrom
fix/metal-gpu-cache-cap

Conversation

@javierbq

Copy link
Copy Markdown
Owner

Problem

Playing a morph killed the iPhone app. Reproduced on an iPhone 15 Pro (TestFlight 1.13.1 build 376) with 2kpo and the Morph sheet's defaults (15 frames per model, loop → 300 states):

  • The build is fine: about 2.1 MB per frame, as morph_jobs estimates.
  • Auto-play then added about 13 MB per newly drawn state until the system ended the app (jetsam SIGKILL, no crash report) at frame 191, at 3.45 GB with 79 MB left.

Each state's cartoon, once drawn, left three copies behind for good:

Copy (2kpo, per state) Size Owner
Shader-ready CGO (CPU vertex buffers) ~4.8 MB RepCartoon::std
Metal buffer uploaded from it ~4 MB RendererMetal::_vboCache
Ray-tracing geometry extracted from it ~5 MB RendererMetal::_rtGeomCache

Fix

A new setting, metal_gpu_cache_mb (0 = the platform default: 256 MB on iOS, 1024 MB on the Mac; below 0 = no limit), bounds each of these separately. What is on screen is always kept, so a static scene draws as before; only geometry no draw used in the last two frames is a candidate, least recently drawn first.

  • RendererMetal: _vboCache entries remember the frame that last drew them. trimGPUCache runs in beginFrame (after the previous frame's draw lists are cleared) and frees uploads plus their RT geometry, never what the built acceleration structures came from. All six upload sites now go through cachedVBOBuffer.
  • RepCartoon: a cartoon notes the frame that drew it and what its shader CGO holds (CGOShaderBytes). RepCartoonTrimShaderCGOs, called from SceneRenderMetal just before FreeAllVBOs, releases the shader CGO and hands the source CGO back as the preshader, so the next draw converts it again. Ray tracing and picking read that same CGO object (ray ? ray : preshader), so neither changes.
  • _cmd.get_metal_gpu_cache_stats(_COb, trim) reports both caches (for tests and diagnostics).

Results (iPhone 15 Pro, default 2kpo morph, looping)

Highest memory Left before the kill limit
Before 3.45 GB → killed at frame 191 79 MB
Renderer cap only ~2.96 GB, flat ~570 MB
This PR ~1.68 GB, flat ~1.86 GB

Re-making a released 110-residue cartoon costs about 2.5 ms per frame on an M3 Pro (3.9 vs 1.4 ms per offscreen frame), well inside a 30 fps budget.

Tests

  • New GpuCacheBudgetTests (macOS, live engine): a 40-state cartoon object. It checks that states not shown are freed past the budget (cache and cartoon bytes drop below a quarter of the uncapped figure), and that a released state renders pixel-identical once re-made.
  • Full UnitTests_macOS locally: 1859 tests, 0 failures (4 skipped).
  • iOS device build ran on the phone for the measurements above. Not run locally: the pip/GL build and the Python suites (CI covers them).

Not in this PR (follow-ups)

  • morph_jobs iOS limits: the refuse limit is max(400 MB, available/2), so the 400 MB floor can exceed free memory. The per-frame estimate also leaves out drawn-state copies, which are now bounded but not zero.
  • Only the cartoon rep releases its shader CGO; sticks, spheres and surfaces keep theirs. Morphs never carry surfaces, and the other reps are small per state.

🤖 Generated with Claude Code

Playing a morph killed the iPhone app. Every state of a multi-state object
keeps its own cartoon, and each state's first draw left three copies of it
behind for good: the cartoon's shader-ready CGO (its CPU vertex buffers),
the Metal buffer uploaded from them, and the ray-tracing geometry
extracted from them. For 2kpo that is about 13 MB a state; the default
300-state morph reached 3.45 GB and the system ended the app (jetsam,
SIGKILL, no crash report) at frame 191 of its first loop.

Both caches are now bounded by a new setting, metal_gpu_cache_mb
(0 = the platform default, 256 MB on iOS and 1024 on the Mac; below 0 no
limit), applied separately to each:

- RendererMetal: _vboCache entries remember the scene frame that last drew
  them. At the start of a frame (after the previous frame's draw lists are
  cleared) trimGPUCache frees, least recently drawn first, uploads no draw
  used in the last two frames, with the RT geometry of the same CPU
  buffers, and never what the built acceleration structures came from.
  All six upload sites now go through cachedVBOBuffer.
- RepCartoon: a cartoon notes the frame that drew it and what its shader
  CGO holds (CGOShaderBytes). RepCartoonTrimShaderCGOs, run by
  SceneRenderMetal before it drains freed buffers, releases the shader CGO
  of cartoons not drawn in the last two frames and hands the source CGO
  back as the preshader, so the next draw converts it again. Ray tracing
  and picking read that same CGO object, so neither changes.

What is on screen is always kept, so a static scene draws as before.
Measured on an iPhone 15 Pro with the default 2kpo morph: playback now
levels off at about 1.68 GB with 1.86 GB to spare, looping indefinitely.
Re-making a released 110-residue cartoon costs about 2.5 ms a frame on an
M3 Pro.

_cmd.get_metal_gpu_cache_stats(_COb, trim) reports both caches; the new
GpuCacheBudgetTests check that undrawn states are freed past the budget
and that a released state renders identically once re-made.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lighting_air_msl.py pins the bodies of RendererMetal::beginFrame and
SceneRenderMetal against master. #924 adds the upload cache's frame clock
and trim to beginFrame, and the metal_gpu_cache_mb budget and the cartoon
shader-CGO trim to SceneRenderMetal, so both digests move; recorded with
a #924 note as earlier changes were.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javierbq
javierbq merged commit f8eb48e into master Nov 12, 2026
7 checks passed
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.

1 participant