metal: tables residency per encoder by default (a set kept all of title memory resident) #76

Manually merged
github merged 3 commits from github-bot/gh-76 into main 2026-11-26 17:09:34 +00:00 AGit
Owner

@⁠AeroUp on GitHub · 2026-11-26 13:42 UTC · original
AeroUp:fix/metal-residency-sets → main · +21 −10 · 3 files · 3 commits · merged 2026-11-26 17:08 UTC

What

Heap residency currently goes through one MTLResidencySet per heap (addAllocation on every write_*, useResidencySet on every command buffer). A set never forgets an allocation, and the title names thousands of textures and buffers in its descriptor heaps, so the set held all of title memory plus every host texture copy resident for the whole process. On my Mac that is what turned into kIOGPUCommandBufferCallbackErrorTimeout after the world load (and, with the host's texture copies, kIOGPUCommandBufferCallbackErrorOutOfMemory), followed by the known 0xc0000005 at image+0x55e56f8.

This flips the default to per-encoder useResource (the existing fallback path) and keeps the set path behind VICTORY_METAL_RESIDENCY_SETS=1.

Measured (M3 Pro, 18 GB, macOS 27.2.1, Series S profile, 1920x1080 layer)

The process is x86_64 because it runs the game's x86_64 code in-process; Rosetta translates the CPU side only and the Metal backend is native (no D3D12 or Wine translation). The DXIL converter is D3DMetal's libmetalirconverter because Apple's Metal Shader Converter is not installed on this machine.

sets on (before) sets off (after)
menus ~24 fps, GPU 3 ms 30 fps (the title's own cap), GPU 2.5 ms
story loading art — 8–17 fps, 37–55 ms GPU, 4K
frames timeout ~frame 1950, then 0xc0000005 past 4200+, boot logo and loading art correct
Metal memory 12+ GB and OOM ~9.4 GB

The 4K world's first frames still stall (~20 s) and the title's own watchdog then ends the process; that is a separate problem and this change does not claim to fix it.

Testing

  • cargo build --release --target x86_64-apple-darwin (Metal backend, the host build) clean.
  • The heap tests in crates/victory-metal/tests run in both modes (common::backends()) and pass.
  • Ran the game with the change: boot logo, story loading art (Jason/Lucia) at 1920x1080, 30 fps menus.

Notes

If you would rather keep sets on for speed on bigger machines, an alternative is to prune the set (remove replaced entries and targets) — that is the "not done" in docs/metal-backend.md. Until that exists, this default avoids a failure mode that looks like a hung GPU.

**@⁠AeroUp** on GitHub · 2026-11-26 13:42 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/76) `AeroUp:fix/metal-residency-sets` → `main` · +21 −10 · 3 files · 3 commits · merged 2026-11-26 17:08 UTC ## What Heap residency currently goes through one `MTLResidencySet` per heap (`addAllocation` on every `write_*`, `useResidencySet` on every command buffer). A set never forgets an allocation, and the title names thousands of textures and buffers in its descriptor heaps, so the set held **all of title memory plus every host texture copy resident for the whole process**. On my Mac that is what turned into `kIOGPUCommandBufferCallbackErrorTimeout` after the world load (and, with the host's texture copies, `kIOGPUCommandBufferCallbackErrorOutOfMemory`), followed by the known `0xc0000005 at image+0x55e56f8`. This flips the default to per-encoder `useResource` (the existing fallback path) and keeps the set path behind `VICTORY_METAL_RESIDENCY_SETS=1`. ## Measured (M3 Pro, 18 GB, macOS 27.2.1, Series S profile, 1920x1080 layer) The process is x86_64 because it runs the game's x86_64 code in-process; Rosetta translates the CPU side only and the Metal backend is native (no D3D12 or Wine translation). The DXIL converter is D3DMetal's `libmetalirconverter` because Apple's Metal Shader Converter is not installed on this machine. | | sets on (before) | sets off (after) | |---|---|---| | menus | ~24 fps, GPU 3 ms | **30 fps** (the title's own cap), GPU 2.5 ms | | story loading art | — | **8–17 fps**, 37–55 ms GPU, 4K | | frames | timeout ~frame 1950, then 0xc0000005 | past **4200+**, boot logo and loading art correct | | Metal memory | 12+ GB and OOM | ~9.4 GB | The 4K world's first frames still stall (~20 s) and the title's own watchdog then ends the process; that is a separate problem and this change does not claim to fix it. ## Testing - `cargo build --release --target x86_64-apple-darwin` (Metal backend, the host build) clean. - The heap tests in `crates/victory-metal/tests` run in both modes (`common::backends()`) and pass. - Ran the game with the change: boot logo, story loading art (Jason/Lucia) at 1920x1080, 30 fps menus. ## Notes If you would rather keep sets on for speed on bigger machines, an alternative is to prune the set (remove replaced entries and targets) — that is the "not done" in `docs/metal-backend.md`. Until that exists, this default avoids a failure mode that looks like a hung GPU.
The title names thousands of textures and buffers in its descriptor heaps.
A residency set never forgets an allocation, so the heap set held every one
of them (all of title memory, about 6.7 GB on a Series S profile, plus the
host's texture copies) resident for the whole process. After the world load,
command buffers started to fail with kIOGPUCommandBufferCallbackErrorTimeout,
and with 5 GB of host texture copies also kIOGPUCommandBufferCallbackError-
OutOfMemory; the title then hit an access violation at image+0x55e56f8.

With useResource per encoder, residency follows the frame instead. Measured
on an M3 Pro / 18 GB: sets on, a timeout at about frame 1950 after the load
and the crash that follows; sets off, the boot logo and the story loading art
render at 8-30 fps (Metal about 9.4 GB) and frames pass 4000. The 4K world's
first frames still stall, which is being tracked separately.

VICTORY_METAL_RESIDENCY_SETS=1 gives the old path back; the heap tests run in
both modes.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:45:21 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 14:54 UTC · original · requested changes

Thanks. Since this flips the default to per-encoder residency, please update docs/native-host.md (~448), which still names VICTORY_METAL_NO_RESIDENCY_SETS, and document the new VICTORY_METAL_RESIDENCY_SETS switch there.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 14:54 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/76#pullrequestreview-6076252753) · **requested changes** Thanks. Since this flips the default to per-encoder residency, please update docs/native-host.md (~448), which still names VICTORY_METAL_NO_RESIDENCY_SETS, and document the new VICTORY_METAL_RESIDENCY_SETS switch there.
The ruled-out note still named the old opt-out switch, and the article on residency
lived only in docs/metal-backend.md.
Author
Owner

@⁠AeroUp on GitHub · 2026-11-26 16:12 UTC · original

Done in 563de87: docs/native-host.md no longer names VICTORY_METAL_NO_RESIDENCY_SETS; the ruled-out note now names VICTORY_METAL_RESIDENCY_SETS=1, and a short paragraph there says which path is the default and why (a set never forgets an allocation, so it holds all of title memory plus the host's texture copies, which ends in kIOGPUCommandBufferCallbackErrorTimeout). Pruning the set is listed as not done, as in docs/metal-backend.md.

**@⁠AeroUp** on GitHub · 2026-11-26 16:12 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/76#issuecomment-7197335407) Done in 563de87: `docs/native-host.md` no longer names `VICTORY_METAL_NO_RESIDENCY_SETS`; the ruled-out note now names `VICTORY_METAL_RESIDENCY_SETS=1`, and a short paragraph there says which path is the default and why (a set never forgets an allocation, so it holds all of title memory plus the host's texture copies, which ends in `kIOGPUCommandBufferCallbackErrorTimeout`). Pruning the set is listed as not done, as in `docs/metal-backend.md`.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 16:37 UTC · original · review (dismissed)

Docs now name VICTORY_METAL_RESIDENCY_SETS and the new default. Merged onto main, victory-metal and the native tests pass, clippy and policy are clean, CI is green.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 16:37 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/76#pullrequestreview-6077667645) · **review (dismissed)** Docs now name VICTORY_METAL_RESIDENCY_SETS and the new default. Merged onto main, victory-metal and the native tests pass, clippy and policy are clean, CI is green.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 16:38 UTC · original

Approved, but main has moved and this now conflicts (docs/native-host.md). Please merge main into the branch and I will merge it right after.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 16:38 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/76#issuecomment-7197917474) Approved, but main has moved and this now conflicts (docs/native-host.md). Please merge main into the branch and I will merge it right after.
Author
Owner

@⁠AeroUp on GitHub · 2026-11-26 16:48 UTC · original

Merged upstream/main into the branch. One conflict in docs/native-host.md, resolved by keeping both sides: upstream's new "views of memory in no pool" bullet and this PR's residency paragraph using VICTORY_METAL_RESIDENCY_SETS=1. Release build of victory-metal for x86_64-apple-darwin is clean.

**@⁠AeroUp** on GitHub · 2026-11-26 16:48 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/76#issuecomment-7198112432) Merged `upstream/main` into the branch. One conflict in `docs/native-host.md`, resolved by keeping both sides: upstream's new "views of memory in no pool" bullet and this PR's residency paragraph using `VICTORY_METAL_RESIDENCY_SETS=1`. Release build of `victory-metal` for `x86_64-apple-darwin` is clean.
github manually merged commit 677f6543c5 into main 2026-11-26 17:09:34 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 17:08 UTC · original

Merged in 8c47ddd. The docs now name VICTORY_METAL_RESIDENCY_SETS, and your merge of main resolved the conflict. Thanks!

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 17:08 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/76#issuecomment-7198562134) Merged in 8c47ddd. The docs now name `VICTORY_METAL_RESIDENCY_SETS`, and your merge of main resolved the conflict. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.