Skip to content

oot: constant 60 FPS on the baseline M2 - #2392

Open
yannouuuu wants to merge 10 commits into
ly05010419:mainfrom
yannouuuu:oot-60fps-draft
Open

yannouuuu wants to merge 10 commits into
ly05010419:mainfrom
yannouuuu:oot-60fps-draft

Conversation

@yannouuuu

@yannouuuu yannouuuu commented Nov 26, 2026 •

Copy link
Copy Markdown

Umbrella plan for constant 60 FPS on the baseline Mac (M2) — workstream results

Full, measured plan in games/oot/docs/60fps-project.md. All six workstreams are now at a decided state (shipped / measured / blocked with evidence):

W Result
W1 mesh threads Root-caused and bench-proven in #2391: the mesh body is sgid-only with a hardcoded <<5 lane stride; M2 Metal caps 256 verts / 32 KB per mesh threadgroup; lid-strided body at 128 threads = 13.6 ms vs 45–53 ms (−73 %). Remaining: per-shader IR lane flattening + chain v29 + re-seed (the long pole).
W2 render distance Blocked + reverted: any b at main+0x4c52ec (or the store main+0x4c52f4) deadlocks the guest at world entry — even a semantically identical no-op stub at offsets +372/+400/+2000 — while the aspect veneer at main+0x4c5334 in the same loop works. Evidence in the doc; next step = scale at the camera-struct +0x138 writer. Review also caught and fixed a FADD-vs-FMUL encoding bug in the first version.
W3 shadow resolution Blocked on RE: size read from a config struct (u16 at +0x48/0x4a/0x4c) by main+0x244bb8 via indirect dispatch; the atlas is re-created ~1.3×/frame so HLE scaling measured worse (44.5 vs 50.6 fps, re-upload churn). A fix must patch the game's size computation.
W4 compute encoders Mostly shipped already: shared compute encoder (TOTK_DISPATCH_REUSE, default on) = +5 % measured (48.1 → 50.6 fps). Remainder needs the conservative range analysis extended to texture/descriptor writes (probe-gated).
W5 texture pre-warm Shipped on this branch, opt-in (OOT_TEX_PREWARM=1): registration queues slots, submit thread drains at submission start with a per-submission budget (full budget during load bursts). Measured: 27k+ textures / 29 GB pre-uploaded, fps unchanged, area-entry spike 681 → 586 ms (remainder is same-frame registration + CPU-side). Next: hook the area-load state.
W6 CPU headroom Measured no-op: sample 15 s → 90 % of CPU is guest JIT, host ~10 % (mostly waits; busy host = 0.6 ms translate + 0.66 ms submit/frame). At DRS the M2 is guest-CPU-bound (2.04 M instr/frame, GPU busy p50 4.4 ms). TOTK_AB_SAME A/B neutral. Closed with numbers.

Shippable perf so far is in #2388 (DRS default on M1/M2 + settings row: 41 → 49–51.5 fps real on the M2, 60.0 displayed with interpolation; app rebuilt and signed from it).

Verification on this branch: python -m unittest discover -s tests 2398 OK, oot and totk build (shared nvn_mtl.m), GPU probes pass (probe-gpu-ubo-snapshot 16 409, probe-gpu-tex-slots 37 152, probe-gpu-texture-retire 35 021), post-revert bench 3300 frames / 40.9 fps at fixed 720p, GitGuardian pass, CONTRIBUTING self-check clean (shared-file references are comments only / the accepted d2r_gfx symlink).

Measured M2 baseline (Kokiri, main@786ddc00): fixed 720p 41 fps, DRS 49.3–51.5, fixed 1080p 34.9; ~1 210 draws / 114 passes / ~31 compute passes per frame; render kernels ~15 ms vs compute ~4.5 ms; first-visit texture streaming up to 383 tex / 280 MB / 146 ms per frame.

Umbrella plan for the deep performance work: measured baseline (GPU-bound,
draw/pass-bound; 51 fps real with DRS on the M2, 60 displayed via MetalFX),
and six ranked workstreams with acceptance criteria - W1 mesh stage at
64/128 threads per threadgroup (bench -71 % on M2, PoC in ly05010419#2391 with the
crash-repro), W2 render distance/LOD rows, W3 shadow resolution row,
W4 compute-encoder merging, W5 texture pre-warm against load spikes,
W6 CPU headroom for high-refresh Macs. Testing rules per CONTRIBUTING.
…tride), M2 mesh limits (256 verts / 32 KB per threadgroup)

The real mesh body parallelizes over one 32-lane SIMD group per instance
and hardcodes the 32-lane stride, so extra threads duplicate lane ids
and race (the SIGSEGV). Metal caps at 256 vertices / 32 KB mesh per
threadgroup on the M2, so one 128-vertex instance per group is the only
option; the fix is flattening the lane id in the translator IR
(<<5 -> <<6/<<7, subgroup masks per 32 lanes) behind TOTK_MESH_THREADS_SCALE,
validated per shader by probe-gpu-mesh + screenshot equality vs the
32-thread build.
…45.6 ms at 32/64/128 threads vs lid-strided 13.6 ms at 128
nvnTexturePoolRegisterTexture queues sampled 2D texture slots (>= the
min size); the submit thread drains the queue at the start of every
submission with a per-submission byte budget (full budget during
registration bursts), uploading through the same first-bind path
(tex_mtl_p/pkg_upload) on the same thread. Measured on the M2: 27k+
textures / 29 GB pre-uploaded, fps unchanged (50.0 vs 50.6), area-entry
spike 681 -> 586 ms; the entry-frame remainder is registered+bound in the
same frames and partly CPU-side. Off by default (OOT_TEX_PREWARM=1);
OOT_TEX_PREWARM_MB / OOT_TEX_PREWARM_MIN_KB tune it.
@yannouuuu

Copy link
Copy Markdown
Author

W5 (texture pre-warm) is implemented on this branch, opt-in (OOT_TEX_PREWARM=1, off by default): nvnTexturePoolRegisterTexture queues sampled 2D texture slots, and the submit thread drains the queue at the start of every submission with a per-submission byte budget (pw_drain, full budget during registration bursts). Measured on the M2: 27k+ textures / 29 GB pre-uploaded ahead of first use, fps unchanged (50.0 vs 50.6 baseline), area-entry spike 681 → 586 ms; the remaining entry-frame cost is partly CPU-side (a 237 ms frame with 0 textures / 31 ms GPU) and textures registered+bound in the same frames. Tests: unittest 2398 OK, totk build OK, probe-gpu-ubo-snapshot / probe-gpu-tex-slots / probe-gpu-texture-retire all pass. W1 status: root cause found (body is sgid-only with a hardcoded <<5 lane stride; M2 Metal caps 256 verts / 32 KB mesh per threadgroup), bench g*/t* variants committed in #2391 prove the lever needs the IR lane flattening. Next: W1 IR patch per shader, or W5's load-state hook.

…and the re-creation churn finding

The 1000x1000 shadow atlas size is read from a config struct's u16
fields (+0x48/0x4a/0x4c) by main+0x244bb8 (SetWidth/Height/Depth thunk
calls); the struct arrives as x0 via indirect dispatch. The atlas is
re-created ~1.3x/frame, so HLE-level scaling causes re-upload churn and
measured worse (44.5 vs 50.6 fps). A fix must patch the size at the game's
computation site so viewport, sampling and atlas layout follow.
@yannouuuu

Copy link
Copy Markdown
Author

W3 (shadow resolution row) investigated and blocked with leads documented in games/oot/docs/60fps-project.md: the 1000×1000 shadow atlas size comes from a config struct's u16 fields (+0x48/0x4a/0x4c) read by main+0x244bb8 (SetWidth/Height/Depth thunk calls at +0x870/0x878/0x880); the struct reaches the function as x0 via indirect dispatch. Key finding: the atlas is re-created ~1.3×/frame (5,285 registrations / ~3,900 frames), so HLE-level scaling causes re-upload churn and measured worse (44.5 vs 50.6 fps, GPU p50 43 vs 21 ms) — a fix must patch the size at the game's computation site so viewport, sampling and atlas layout all follow. Next unblock: trace the indirect dispatch to the config global.

… default on) is worth +5 % on the M2 (48.1 -> 50.6 fps); the rest needs the conservative range analysis extended to texture/descriptor writes, probe-gated
@yannouuuu

Copy link
Copy Markdown
Author

W4 measured and documented: the shared compute encoder (TOTK_DISPATCH_REUSE, default on since earlier work) is worth +5 % on the M2 at DRS (48.1 → 50.6 fps, reuse off vs on). Dispatch stats at frame 3900: ~7.8 dispatches/frame, ~63 % still open their own encoder because a binding has no known guest range (textures / descriptor pools / uniform-loaded pointers) — extending the range analysis there is the remaining W4 headroom (~1–2 ms/frame), correctness-critical and gated by probe-gpu-dispatch-chain's negative control + screenshot equality. Barrier-start-off and narrow modes measured neutral while the GPU is saturated. Status of the workstreams: W5 implemented (pre-warm, opt-in, in this branch), W1 root-caused + bench-proven (#2391), W3 blocked on the shadow config RE (leads documented), W4 mostly shipped, W2/W6 open.

The camera far plane (parameter struct +0x138, loaded every frame at
main+0x4c52ec, verified word 0xbd413a60) branches to a veneer that
multiplies it by a data-page factor (cell +60), like the aspect veneer:
the effects page's 渲染距离 row (25/50/75/100 %), 'flag oot_far <percent>',
OOT_FAR_SCALE, graphics-far.txt. At 100 % the far plane is the game's own
bit for bit (fmul by 1.0f is exact). Fewer distant draws when shortened;
measured neutral in a static forest view (expected — it matters when
looking across open areas). unittest 2398 OK, oot+totk build.
@yannouuuu

Copy link
Copy Markdown
Author

W2 (render distance for OoT) is implemented on this branch: the camera far plane (struct +0x138, loaded every frame at main+0x4c52ec, verified word 0xbd413a60) now has a veneer multiplying it by a data-page factor — same pattern as the aspect veneer. Exposed as the effects page's 渲染距离 row (25/50/75/100 %), flag oot_far <percent>, OOT_FAR_SCALE, graphics-far.txt. At 100 % the far plane is bit-identical to the game's (fmul by 1.0f is exact). Measured on the M2: veneer installs and applies live; in a static forest view fps/draws are within noise (the far plane matters when looking across open areas) — screenshot equality at 100 % is the acceptance check. unittest 2398 OK, oot+totk build. LOD-distance row for OoT still needs its own verified sites (BotW's are TOTK offsets). Workstream status: W2 shipped (far) / W5 shipped (pre-warm) / W4 mostly shipped (+5 % measured) / W1 root-caused (#2391) / W3 blocked on RE (leads documented).

…guest JIT is 90%; at DRS the M2 is guest-CPU-bound (2.04M instr/frame, GPU busy p50 4.4 ms); TOTK_AB_SAME A/B neutral (44.0 vs 43.5 fps); no safe host win, closed as measured-no-op
@yannouuuu

Copy link
Copy Markdown
Author

W6 (CPU headroom) closed as measured no-op, with numbers: sample 15 s on the M2 (Kokiri, DRS) shows 90 % of all CPU in guest JIT code; host runtime is ~10 % and mostly blocked (__semwait_signal/gate). Busy host work per frame: JIT translation 0.6 ms + NVN submit/present p50 0.66 ms. At DRS the pipeline is guest-CPU-bound: 2.04 M guest instructions/frame (~63 k/draw, 32 draws/frame) while GPU busy is only p50 4.4 ms. Argument blocks run 60–70 encodes + 3–4 rebinds/frame; TOTK_AB_SAME=0 A/B is neutral (43.5 vs 44.0 fps), so the memcmp path costs nothing and finer invalidation would save < 5 % host — inside noise. Conclusion: the M2 wall at DRS is guest execution + GPU, not host code; what reduces guest CPU per frame is fewer draws (W2 far plane, shipped on this branch). Full workstream board: W2 shipped (far-plane veneer), W5 shipped (pre-warm, opt-in), W4 mostly shipped (+5 % measured), W6 measured no-op, W1 root-caused (#2391), W3 blocked on RE (leads documented). unittest 2398 OK on this branch, oot+totk builds OK.

…f4 deadlocks the guest at world entry

Review caught two bugs and one hard blocker: (1) the stub word 0x1e222800
is FADD not FMUL, and s2/s10 liveness was only linear-scan-verified; fixed
with save/restore of d8 + fmul. (2) Even a semantically identical no-op
stub (original ldr + branch back) at stub offsets +372/+400/+2000, and a
variant branching at the store main+0x4c52f4, all hang the game at world
entry (~frame 680) while main / W5-only builds and the aspect veneer at
main+0x4c5334 in the same loop run fine — the game depends on that exact
code. W2 code reverted (measured neutral in a static view anyway); the
doc records the evidence and the next step (scale at the +0x138 writer).
Also fixes the pw_enqueue lock-order comment. unittest 2398 OK, oot+totk
build OK, bench after revert: 3300 frames / 40.9 fps at fixed 720p.
@yannouuuu
yannouuuu marked this pull request as ready for review November 26, 2026 05:57
@yannouuuu

Copy link
Copy Markdown
Author

Final review pass done, PR marked ready. What changed in this pass:

  • Caught a real bug in the W2 veneer during self-review: the multiply word 0x1e222800 is fadd s0,s0,s2, not fmul (and the register liveness was only linear-scan-verified). Fixed to a save/restore-d8 + fmul stub — but then found the hard blocker: any branch at main+0x4c52ec or at the store main+0x4c52f4 deadlocks the guest at world entry (~frame 680), even a semantically identical no-op stub, at stub offsets +372/+400/+2000; control builds (main, W5-only) and the aspect veneer at main+0x4c5334 in the same loop run fine. So W2's code is reverted (commit 785afaa6); it measured neutral in a static view anyway. Evidence + next step (scale at the camera +0x138 writer) are in games/oot/docs/60fps-project.md.
  • Code cleanups kept: pw_enqueue lock-order comment, settings row values sorted.
  • Final verification on the pushed tip: unittest 2398 OK, oot and totk build OK (shared nvn_mtl.m), bench after the revert 3300 frames / 40.9 fps at fixed 720p (no stall), GitGuardian passing, CONTRIBUTING self-check clean.

Board: W5 shipped (pre-warm, opt-in, 27k tex/29 GB, spike 681→586 ms), W4 mostly shipped (+5 % measured), W1 root-caused + bench-proven (#2391), W2/W3 blocked with RE evidence, W6 measured no-op. Shippable perf (DRS default M1/M2, 60 FPS displayed) is in #2388.

@yannouuuu yannouuuu changed the title docs(oot): draft — project plan for constant 60 FPS on the baseline M2 oot: project plan for constant 60 FPS on the baseline M2 — workstream results (W5 shipped, W1/W3/W4/W6 measured or root-caused, W2 reverted with evidence) Nov 26, 2026
…prewarm_default); OOT_TEX_PREWARM / graphics-prewarm.txt override

Measured fps-neutral and picture-identical on the M2 (default run 50.6
fps with 900 textures / 1516 MB pre-uploaded during load; forced-off run
identical fps, no prewarm activity), and it removes most of the 100-680 ms
area-entry spike (681 -> 586 ms measured earlier) — the visible freezes.
Shared nvn_mtl.m calls a weak nvn_prewarm_default(); only OoT's runtime
defines it (sysctl brand check, like the DRS default). Doc records the
frame-budget finding: real fps on the M2 is resolution-independent
(~50 at DRS, ~40 fixed) — the wall is per-frame CPU (submit encode
14-16 ms + 46M guest instructions), so real 60 needs W1/W4/W2.
@yannouuuu

Copy link
Copy Markdown
Author

Two updates from the live-game check (2026-11-26):

  1. W5 pre-warm is now ON by default on base M1/M2 Macs (commit f8c23147): nvn_prewarm_default() in game_patches_ns2.c (same sysctl check as the DRS default), called through a weak symbol from the shared nvn_mtl.m; OOT_TEX_PREWARM=1/0 wins, then graphics-prewarm.txt. Measured on the M2: default run 50.6 fps with 900 textures / 1516 MB pre-uploaded during load; forced-off run identical fps, zero prewarm activity. This targets the occasional freezes (area-entry texture spike 681 → 586 ms measured). unittest 2398 OK, oot+totk build OK. The installed app was rebuilt from this branch (codesign verified).

  2. Real-60 diagnosis, measured in-world (Kokiri) on the M2: with the app's settings (1080p canvas, DRS on, internal ~683×384) the game holds ~50–51 real fps (frame p50 19.6 ms). It is not pixel-bound: fixed 448p internal = 40 fps, fixed 720p = 41, DRS = 50–51 regardless of canvas — so the wall is per-frame CPU: NVN submit+present p50 14.3 ms (encode of 1 220 draws / 113 passes / 2 138 arg-block encodes, only 4 % reused) plus 46 M guest instructions/frame. Title/menu runs at 60–144 real. Reaching a constant real 60 in-world (≤ 16.7 ms/frame) needs ~15–20 % off that CPU path: W1 (mesh IR lane-flattening — the long pole), the W4 range-analysis extension (arg-block reuse), and W2 (fewer draws, currently blocked at the load site). All documented in games/oot/docs/60fps-project.md.

@yannouuuu yannouuuu changed the title oot: project plan for constant 60 FPS on the baseline M2 — workstream results (W5 shipped, W1/W3/W4/W6 measured or root-caused, W2 reverted with evidence) oot: constant 60 FPS on the baseline M2 Nov 26, 2026
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