#246

Metal upload staging: reuse the 32 MB blocks instead of allocating one per flush

Opendormonbearwants to merge intofrom

What changed

The Metal upload path now reuses its 32 MB staging blocks. up_flush puts a flushed block into a ring of 3 with the same fence defer_obj_release would have used, and up_alloc takes one the GPU has finished with instead of creating a new buffer. Oversized blocks and anything beyond the ring are retired as before.

Why

Every upload batch allocated a new 32 MB private buffer and retired it. On an iPhone 15 Pro Max a 47-minute run went through about 6400 of them, and some were never destroyed after retirement. They had left the residency set, so defer_obj_release had done its part, but something else still held a reference. With DM_RES_CENSUS=1 that run ended with 15 such blocks, 448 MB. We also have WatchdogTermination reports from long sessions, and GPU allocated memory climbing over a session was the pattern leading up to them.

I did not find the holder (the 8 blocks listed all had the same retain count). This change does not depend on it: blocks are no longer abandoned, so they cannot pile up.

Measured on the same device, one run each, different routes, both with DM_RES_CENSUS=1, on a fork based on an older main:

At frame 124800BeforeAfter
Retired, not destroyed buffers15 / 448 MB0
GPU allocated2728 MB2432 MB
Resident3145 MB2952 MB

Block allocations: about 6400 before (47 minutes); 6 after, with 3221 reuses, at frame 72000 (about 22 minutes).

Known limits:

  • Resident memory still grows slowly after this (headroom 786 MB to 433 MB over 39 minutes). That part is not GPU memory from these blocks and is not addressed here.
  • Up to 3 blocks stay resident, so the idle cost can be 96 MB rather than 32 MB. Calculated, not measured separately.
  • The "N created" count in the staging block diagnostic line prints only when a block is swapped mid-batch, so it stays at 1 for a whole run. That hid this. Not changed here.

Checks run

No game files needed:

  • python3 dm/i18n/scan_literals.py (reports 2 strings in gen_romfs_overlay.py and drop_light.py; output identical on untouched main, nothing from this change)
  • python3 dm/ios/tools/dm_i18n.py --check (dm_i18n OK 486 条)

Game files and a workspace prepared by ./setup.sh needed:

  • make -C dm/host/tests run (not run, no host code touched)
  • make -C dm/gfx/tests run (stops at a link error in t_defer_chain, missing dm_cam_* symbols; same on untouched main, so no graphics unit test exercised this change. t_nvn also fails to link here; I did not compare it on main.)
  • make -C dm/ios/tests test (not run, no iOS shell code touched)
  • dm/verify/regress.sh (not run, rewriter untouched)
  • cd dm/ios && ./run.sh build (built through dm/ios/tools/shell_build.sh ios instead)
  • dm/ios/tools/smoke_mac.sh 1 (not run)
  • dm/ios/tools/check_hud_gen.sh (not run)

Anything else you ran, and on what:

  • iPhone 15 Pro Max, iOS 27.2: 39 minutes on my fork with this logic plus a reuse counter. No crash; a Metal command buffer error I had seen before this change appeared once.
  • iPhone 15 Pro Max: this commit on main (6a3f1ee), built with my local dmsh_chain files: about 2 minutes in game, ran normally. Too short to show the memory effect on its own.

Visible changes

  • Nothing on screen changes
  • Something on screen changes — screenshots taken after the change are attached below
  • New on-screen text is in dm/i18n/ui_text.tsv, and dm/host/dm_i18n_tab.h was regenerated

No game content

  • This change contains no game files, nothing generated from game files, no keys, and no built package
  • It does not carry the local rewrites ./setup.sh makes to dm/gfx/tools/dmsh_chain.lock, dm/gfx/nvn/dmsh_chain.h and dm/gfx/nvn/c1_full.tsv

Comments

  • New and changed comments are in both Chinese and English