Repository navigation
Metal: bound the memory kept for off-screen geometry (iPhone morph crash) - #924
Merged
Merged
Conversation
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>
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.
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):
morph_jobsestimates.Each state's cartoon, once drawn, left three copies behind for good:
RepCartoon::stdRendererMetal::_vboCacheRendererMetal::_rtGeomCacheFix
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._vboCacheentries remember the frame that last drew them.trimGPUCacheruns inbeginFrame(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 throughcachedVBOBuffer.CGOShaderBytes).RepCartoonTrimShaderCGOs, called fromSceneRenderMetaljust beforeFreeAllVBOs, 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)
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
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.UnitTests_macOSlocally: 1859 tests, 0 failures (4 skipped).Not in this PR (follow-ups)
morph_jobsiOS limits: the refuse limit ismax(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.🤖 Generated with Claude Code