native: a byte budget on host texture copies (evict unused ones under memory pressure) #294

Closed
opened 2026-11-27 09:10:19 +00:00 by m0rff · 6 comments
m0rff commented 2026-11-27 09:10:19 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T09:10:19Z.


Piece of #8 (16–24 GB Macs run out of GPU memory), proposed in #110.

What is wrong. On the native host a host texture copy is made when a view first names a texture (Title::realize, called from glue::mirror for every device-written view, staging heaps included) and kept until the title releases the resource, or, for a placed texture, until another texture takes its address or its pool is released. Nothing bounds their total. On electrifie's M4 16 GB run in #8 the host made ~13,000 new copies during the first load while the footprint grew ~7.6 GB, and Metal then failed ordinary command buffers with kIOGPUCommandBufferCallbackErrorOutOfMemory. #252 (hot-texture expiry) and 2d4cd0a1 (sync off the lock) change probing and sync cost, not how many copies live.

Plan.

  • A byte budget for sampled host texture copies: Metal's recommendedMaxWorkingSetSize minus the title's memory (both logged at startup since #149), with a floor; VICTORY_METAL_TEXTURE_BUDGET_MB overrides it.
  • Over budget, give up the least recently used copies that are safe to give up: not a render target, depth texture or GPU-written texture; shown by no shader-visible heap entry (shaders index heaps themselves, so a shown copy counts as used, see #79); and held by no recorded or in-flight work. An evicted texture is made again and re-uploaded the next time a view names it, including a record the title copies into a heap with plain stores.
  • Goes through the existing release path, so it does not fight VICTORY_METAL_RESIDENCY_SETS=heap (the last reference leaving takes it out of the set).
  • Counters on the texture sync trace: live copy bytes, bytes shown by heap entries, evictable bytes, evicted bytes. That is the per-second count electrifie asked for. It also tells whether this cap frees much, or whether the levers named in #79 (one copy instead of two, dropping top mips) are the real ones.
  • Unit tests for the eviction order and for never evicting a copy in use.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6643416948 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/294). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T09:10:19Z. --- Piece of #8 (16–24 GB Macs run out of GPU memory), proposed in #110. **What is wrong.** On the native host a host texture copy is made when a view first names a texture (`Title::realize`, called from `glue::mirror` for every device-written view, staging heaps included) and kept until the title releases the resource, or, for a placed texture, until another texture takes its address or its pool is released. Nothing bounds their total. On electrifie's M4 16 GB run in #8 the host made ~13,000 new copies during the first load while the footprint grew ~7.6 GB, and Metal then failed ordinary command buffers with `kIOGPUCommandBufferCallbackErrorOutOfMemory`. #252 (hot-texture expiry) and 2d4cd0a1 (sync off the lock) change probing and sync cost, not how many copies live. **Plan.** - A byte budget for sampled host texture copies: Metal's `recommendedMaxWorkingSetSize` minus the title's memory (both logged at startup since #149), with a floor; `VICTORY_METAL_TEXTURE_BUDGET_MB` overrides it. - Over budget, give up the least recently used copies that are safe to give up: not a render target, depth texture or GPU-written texture; shown by no shader-visible heap entry (shaders index heaps themselves, so a shown copy counts as used, see #79); and held by no recorded or in-flight work. An evicted texture is made again and re-uploaded the next time a view names it, including a record the title copies into a heap with plain stores. - Goes through the existing release path, so it does not fight `VICTORY_METAL_RESIDENCY_SETS=heap` (the last reference leaving takes it out of the set). - Counters on the texture sync trace: live copy bytes, bytes shown by heap entries, evictable bytes, evicted bytes. That is the per-second count electrifie asked for. It also tells whether this cap frees much, or whether the levers named in #79 (one copy instead of two, dropping top mips) are the real ones. - Unit tests for the eviction order and for never evicting a copy in use.
m0rff commented 2026-11-27 09:10:24 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T09:10:24Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7214364820 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/294#issuecomment-7214364820). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T09:10:24Z. --- /claim
github-actions[bot] commented 2026-11-27 09:10:36 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T09:10:36Z.


Claimed by @m0rff. Open a draft pull request with Refs #294 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7214368487 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/294#issuecomment-7214368487). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T09:10:36Z. --- Claimed by @m0rff. Open a draft pull request with `Refs #294` in its description within 24 hours, or the claim expires.
Hayyan612 commented 2026-11-27 15:37:19 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Hayyan612 on 2026-11-27T15:37:19Z.


@m0rff, an idea that might fit alongside the byte budget, before anyone builds it. Not claiming anything.

Every host texture copy can be rebuilt from title memory, since that's what texture sync does on a change. So instead of only evicting copies over budget, idle copies could be marked purgeable (setPurgeableState(.volatile)): macOS/iOS then reclaims them under real memory pressure, rather than the process hitting OutOfMemory or being killed, and leaves them alone when there's room. On next use, setPurgeableState(.nonVolatile) returns the previous state; if it was .empty, the copy is detiled again from title memory, as after a change.

How it might combine with #301: the budget decides when a copy is idle enough; purgeable decides whether the OS needs the memory back, so an idle copy that fits stays warm instead of being re-detiled.

Open questions:

  1. Are host copies standalone textures or placed in an MTLHeap? Purgeable state is per heap for placed resources, so it would only suit standalone copies.
  2. Volatile copies would have to leave the residency sets (#79) while volatile.
  3. The cost of a re-detile when the OS did purge.

Happy to build and test it (state transitions, and the rebuild path with a simulated purge) if it fits your design, or leave it to you.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7222280760 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/294#issuecomment-7222280760). Originally posted by [Hayyan612](https://github.com/Hayyan612) on 2026-11-27T15:37:19Z. --- @m0rff, an idea that might fit alongside the byte budget, before anyone builds it. Not claiming anything. Every host texture copy can be rebuilt from title memory, since that's what texture sync does on a change. So instead of only evicting copies over budget, idle copies could be marked **purgeable** (`setPurgeableState(.volatile)`): macOS/iOS then reclaims them under real memory pressure, rather than the process hitting `OutOfMemory` or being killed, and leaves them alone when there's room. On next use, `setPurgeableState(.nonVolatile)` returns the previous state; if it was `.empty`, the copy is detiled again from title memory, as after a change. How it might combine with #301: the budget decides **when** a copy is idle enough; purgeable decides **whether the OS needs the memory back**, so an idle copy that fits stays warm instead of being re-detiled. Open questions: 1. Are host copies standalone textures or placed in an `MTLHeap`? Purgeable state is per heap for placed resources, so it would only suit standalone copies. 2. Volatile copies would have to leave the residency sets (#79) while volatile. 3. The cost of a re-detile when the OS did purge. Happy to build and test it (state transitions, and the rebuild path with a simulated purge) if it fits your design, or leave it to you.
m0rff commented 2026-11-27 15:40:43 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T15:40:43Z.


@Hayyan612 thanks. One limit to check before building: shaders index heaps themselves (#79), so the host can't tell when a copy that a heap entry shows is read, and it could not safely be volatile. That leaves the copies no entry shows and no list holds, the same set #301 may give up, and testers' 24 GB runs found little in it (about 310 MiB over a run). The larger cost there is copies overlapping one range (#356, draft #359). I won't build purgeable copies in #301. If you want to try it, open your own issue and measure how much would qualify first.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7222349256 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/294#issuecomment-7222349256). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T15:40:43Z. --- @Hayyan612 thanks. One limit to check before building: shaders index heaps themselves (#79), so the host can't tell when a copy that a heap entry shows is read, and it could not safely be volatile. That leaves the copies no entry shows and no list holds, the same set #301 may give up, and testers' 24 GB runs found little in it (about 310 MiB over a run). The larger cost there is copies overlapping one range (#356, draft #359). I won't build purgeable copies in #301. If you want to try it, open your own issue and measure how much would qualify first.
Hayyan612 commented 2026-11-27 15:44:28 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Hayyan612 on 2026-11-27T15:44:28Z.


Thanks @m0rff, that settles it: if heap-indexed copies can't be volatile, ~310 MiB isn't worth the complexity, and #356/#359 is the bigger win. I'll leave it unless a measurement shows otherwise.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7222424001 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/294#issuecomment-7222424001). Originally posted by [Hayyan612](https://github.com/Hayyan612) on 2026-11-27T15:44:28Z. --- Thanks @m0rff, that settles it: if heap-indexed copies can't be volatile, ~310 MiB isn't worth the complexity, and #356/#359 is the bigger win. I'll leave it unless a measurement shows otherwise.
m0rff commented 2026-11-27 22:46:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T22:46:09Z.


Done on main through #359 (582670f1), with the budget off by default. The overlap work continues on #356.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229602227 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/294#issuecomment-7229602227). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T22:46:09Z. --- Done on main through #359 (582670f1), with the budget off by default. The overlap work continues on #356.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS#294
No description provided.