Skip to content

test(oot): PoC — mesh stage at 64/128 threads per threadgroup (bench + translator flag + runtime mode 3) - #2391

Open
yannouuuu wants to merge 16 commits into
ly05010419:mainfrom
yannouuuu:oot-mesh-simd-poc
Open

yannouuuu wants to merge 16 commits into
ly05010419:mainfrom
yannouuuu:oot-mesh-simd-poc

Conversation

@yannouuuu

Copy link
Copy Markdown

What — PoC for the mesh-stage thread scaling (the biggest measured lever toward constant 60 FPS on weak Macs)

Branch oot-mesh-simd-poc, fully documented in games/oot/docs/mesh-threads-poc.md. Everything here is off by default; the default path is unchanged and verified (probe-gpu-mesh 7,920 checks pass, unittest 2398 OK, oot + totk runtimes build).

  1. Committed microbenchmark (games/oot/tests/mesh_simd_split_bench.{m,metal}, make -C games/oot WORK=… mesh-simd-split-bench): fed302d7's real structure (1117 threadgroups, 128 vertices × 10 float4 per group, ~13 KB device scratch slot with atomics, 80 raw reads per logical lane through the 8-word range cache). Median of 16 submits on an Apple M2: 32 threads/group 49.9 ms, 64 → 26.8 (−46 %), 128 → 14.4 ms (−71 %); splitting one thread's read chain across SIMD groups via threadgroup memory adds nothing over the thread count.
  2. Translator flag TOTK_MESH_THREADS_SCALE (tools/totk_xlate/MeshStage.cs): scales the mesh stage's local size and every wrapper stride. Fidelity proven: with the flag unset, a JIT build of the translator reproduces the packaged seed's MSL for fed302d7 byte-for-byte (148,784 bytes identical); with scale 4 the MSL compiles and declares 128 threads.
  3. Runtime mode OOT_MESH_TRANSLATION=3 (kind "mesh_threads" external candidates in nvn_mesh_translation.inc / nvn_mesh_tasks.inc): loads a candidate whose reflection may differ only in threadgroup_size/mesh_stage.threads, and uses its thread count for the mesh pipeline and the draw.

Key finding (why this is a PoC and not merged as-is)

In-game on the M2, fed302d7 re-translated at 64 and at 128 threads and loaded via mode 3 crashes with SIGSEGV at the same frame (660), when the scaled pipeline first draws: signal=0xb fault=0x1 esr=0x92000006, faulting PC in libsystem called from guest main+0x1c4280. The guest mesh code assumes a 32-lane workgroup, so the local size cannot simply be raised. A real fix must restructure the per-threadgroup work in the translator (several instances per threadgroup, or extra physical lanes only prefetching the read chain) and pass probe-gpu-mesh plus in-game screenshot equality against the 32-thread output — the crash above is the acceptance test.

Testing

  • make mesh-simd-split-bench reproduces the numbers above on any Mac.
  • Translator: seed-mode run on a one-shader corpus reproduces the packaged MSL exactly (hash-identical), and the scaled variant compiles under metal.
  • python -m unittest discover -s tests: 2398 OK. probe-gpu-mesh: 7,920 checks pass. iOS untouched (!TARGET_OS_IPHONE guards / excluded file). No game content in the diff (the bench is synthetic).

Off by default (the Mac pins the render scale, the picture never changes
under load); when on, patch_oot_fixed_res is left unpatched (as with
OOT_FIXED_RES=0) and the game lowers the 3D scale itself under load, as on
the console. Saved in graphics-drs.txt next to controls.txt, read when the
guest starts; OOT_FIXED_RES in the environment still wins. From the next
start.

Measured on an Apple M2 (10-core GPU, fanless), macOS 27.2.1, app from
3b0eae4, Kokiri Forest standing still, 720p canvas, frame lock off, 40 s
windows, two runs: fixed 34.0-34.2 fps (p50 29.2 ms) against 39.0-40.1 fps
(p50 25.3 ms) with the game's dynamic resolution (the game dropped the 3D
to about 683x384 at the heaviest); in lighter scenes the game keeps the
full scale, so those hold their frame rate. The 720p-from-480p canvas row
measures the same 39 fps. GPU-bound either way (metalperftrace: ~27.9 ms
on-GPU per 29 ms frame); see games/oot/docs/m2-performance.md.

Built and played on Mac (the runtime rebuilds in ~25 s, make -C games/oot
WORK=...); python -m unittest discover -s tests: 2393 OK (57 skipped).
game_patches_ns2.c is excluded from the iOS project and the new row is
inside !TARGET_OS_IPHONE, so the iOS build is unchanged (not rebuilt here).
…me goes

720p canvas, Kokiri Forest: GPU-bound (~27.9 ms on-GPU per 29 ms frame,
layer active ~100 % of the time, CPU ~1.5 cores), ~160 passes and ~1,215
draws per frame, ~9.8 ms/frame of compute passes, ~3.3 ms of 1000x1000
shadow passes. The cost is draw/pass-count bound, not pixel bound (halving
the 3D resolution moves the render passes only 16.4 -> 13.0 ms).

Ruled out with A/B windows on the M2: GPU keep-alive off, yield_fast,
async replay, drawable at picture size, present link off (all neutral);
shading-rate images are bound but every palette entry is 1x1, so full-rate
shading is not waste in this scene. The game's own dynamic resolution and
the 720p-from-480p row each measure +14 % (34 -> 39-40 fps).

Lists where the remaining work is: pass merging, indirect-argument batching,
the mesh/culling compute passes (translator-level SIMD split is worth up to
-42 % on the mesh stage in the offline meshbench), shadow passes.
…s outside the table's union bounds

The copy routine (uniform-snapshot helpers, wait=0) called pend_overlay_x ~1,800
times a frame in Ocarina of Time, each scanning all ~16 pending entries and
applying none: 28k loop iterations a frame of pure overhead (measured with
the pov counters). The table now carries its union bounds (min lo / max hi,
maintained on append and recomputed at the submission-start compaction);
a query outside [span_lo, span_hi) overlaps no entry, completed ones
included, so the scan is skipped. pend_hits gets the same check.
TOTK_PEND_SPAN=0 restores the full scan (A/B).

Measured on an Apple M2, OoT 720p canvas, Kokiri Forest: copy-routine scans
15.6 -> 0.0 entries per call (~29k iterations/frame removed), applied
entries identical (0.00), replay scans unchanged (56 calls/frame, 11.5
applied - those overlap the span, as they should); frame rate unchanged
(34.4 vs 34.2 fps - the frame is GPU-bound at ~96 %, this removes CPU work,
not GPU time). probe-gpu-ubo-snapshot passes with the change (16,409 checks:
overlay application, prior-submission writes, arena churn, fault guard).

The file is shared (symlinked) by the games; the change is game-agnostic and
behaviour-preserving (the early-out only fires when no entry can overlap).
…solution + MetalFX interpolation)

Measured on the M2 with metalperftrace (output FPS = drawables/s shown): 720p
native alone shows 34; with interpolation 30+30 it shows 55.5-57; 720p native +
the dynamic-resolution row + interpolation 30+30 shows a steady 60.0 (present
interval p50 16.67 ms, ~50% interpolated frames, game rate ~30 fps adapting its
internal resolution 683x384-1152x648 under load); 480p canvas + 30+30 also shows
60.0. On-GPU ~16-17.5 ms with interpolation on. The interpolation costs GPU time
per generated frame, which is why the DRS row matters alongside it.
…-split fix)

PR ly05010419#2324 cut the render passes in this scene from 159.7 to 114.2 per frame
on M1/M2 and moved the M2 frame rate at 720p native from 34.1 to 41 fps.
With the dynamic-resolution row on: 51 fps at 720p canvas, 50.8 fps at the
480p canvas (p99 22.3 ms, the most stable config). Displayed rate with
MetalFX interpolation 30+30: 54-60 FPS.
…FPS displayed recipe

Long-frame log while playing: the drops are first-visit texture streaming
(up to 383 textures / 280 MB in one frame -> 100-580 ms frames), rare arena
heap migrations (~90 ms) and one-off pipeline compiles (~17 ms); steady state
p50 ~24 ms at 720p. 60 FPS displayed, measured with the settings files alone
(graphics-canvas.txt=1, graphics-drs.txt=1, graphics-frame.txt=interp-1):
present interval p50/p95/max 16.67 ms, 0% over one refresh, 300/600 presents
interpolated = steady 60.0 on the 60 Hz display, game logic held at 30 fps,
internal resolution adapting through DRS.
…pped interpolation

Measured at canvas 1920x1080 on the rebased tree: fixed scale 34.9 fps;
with the dynamic-resolution row 51.5 fps (p50 19.3 ms, the measured ceiling
without interpolation — a real 60 fps game rate needs <= 16.7 ms/frame on the
GPU and is not reachable in this scene on the M2); with DRS + uncapped MetalFX
interpolation (interp-4): steady 60.0 displayed (present interval p50/p95
16.67 ms, 373-399/600 interpolated) with the game logic at ~40 fps real.
With 30+30 instead: 59.5-60.0 displayed, game held at 30. Applied to the
player's settings: graphics-canvas.txt=3, graphics-drs.txt=1, interp-4.
…hs to it

Measured ceiling 50.6-51.5 fps (frame 19.6 ms, on-GPU 19.3-20.8 ms, layer
~100% active) with the game's dynamic resolution; 41 fps at fixed 720p.
Resolution is not the lever (fixed scales 1.0/0.75/0.6 all ~41 fps — the cost
is draw/pass/compute bound: ~1,210 draws, 114 passes, ~31 compute passes at
10.2 ms/frame). Keep-alive off hurts on the fanless M2 (43.9 vs 50.6) — keep
it on. skip_invisible skips nothing here. Reaching 16.7 ms needs ~15% less
GPU: merge adjacent compute encoders, translator mesh SIMD split, an opt-in
shadow-resolution row, more pass merging — each needs its GPU probes.
…s compute ~4.5 ms

OOT_DISPATCH_GPU_FILE (Precise timestamps) over 30 frames at dynamic
resolution: compute kernels ~4.5 ms/frame, render kernels ~15 ms/frame, so
the 19.3 ms GPU wall is the render side. TOTK_BARRIER_START=0 (racy,
measurement only) is neutral (50.7 vs 50.6 fps): the per-encoder queue
barrier is not the cost while the GPU is saturated. Real 60 fps in this
scene on the M2 therefore needs geometry/mesh-side cuts (translator mesh
SIMD split, pass merging, draw reduction), not encoder/barrier overhead.
…+ translator flag + runtime mode 3)

Microbenchmark with fed302d7's real structure committed as a make target
(mesh-simd-split-bench): on an Apple M2, 32 -> 128 threads per threadgroup
is 49.9 -> 14.4 ms (-71 %); splitting one thread's read chain across SIMD
groups adds nothing over the thread count. Translator: TOTK_MESH_THREADS_SCALE
scales the mesh local size (off unless set; scale unset reproduces the
packaged seed MSL byte-for-byte). Runtime: OOT_MESH_TRANSLATION=3 loads an
external mesh_threads candidate (threadgroup_size may differ) and uses its
thread count for the pipeline and the draw. In-game on the M2 the real
fed302d7 crashes (SIGSEGV, frame 660) at 64 and 128 threads: the guest code
assumes 32 lanes, so a real fix must restructure the work per threadgroup,
validated by probe-gpu-mesh and in-game screenshots. probe-gpu-mesh passes
(7920 checks), unittest 2398 OK, oot+totk build. See
games/oot/docs/mesh-threads-poc.md.
The guest mesh body uses only gl_SubgroupInvocationID (0..31 per SIMD
group) and a hardcoded 32-lane stride (<<5) for its per-lane address; at
64/128 physical threads the SIMD groups duplicate the same lane ids and
race on the stage memory — that is the SIGSEGV. Metal limits on the M2
(bench-verified): max 256 vertices and 32 KB mesh size per threadgroup,
so with 10 float4/vertex one 128-vertex instance per threadgroup is the
only option; multi-instance per group is impossible. The only route to
more threads is flattening the lane id in the translator's IR (lane
stride <<5 -> <<6/<<7, subgroup masks per 32 lanes), validated by
probe-gpu-mesh + screenshot equality vs the 32-thread build.
…gains nothing from more threads

M2, fed302d7 structure: t128 (lid-strided body) 16.6 ms (-69 %) vs
g32/g64/g128 (sgid-only, hardcoded 32-lane stride, like the real mesh
body) 41.7/36.4/42.3 ms. Proves the -71 % lever needs the IR-level lane
flattening, not just a bigger threadgroup. Also documents the Metal
limits found by compile failures: max 256 vertices and 32 KB mesh size
per mesh threadgroup on the M2.
…addressing, block-scaled stage header

MeshStage.cs (behind TOTK_MESH_THREADS_SCALE): the mesh body's subgroup
lane id used for addressing becomes dm_lid (subgroup masks keep the
per-32-lane id), and the wrapper's mem[0] is scaled by threads/32 so one
group covers that many 32-blocks; 32-lane strides stay, stage layout
unchanged. fed302d7 variant: compiles, reflection matches the seed
except threadgroup_size, mode 3 loads it — but 64-thread execution still
faults at main+0x1c4280 (raw-pool address path still 32-lane). Documented
in mesh-threads-poc.md with the next step.
@yannouuuu

Copy link
Copy Markdown
Author

W1 progress (commit ac705474): the translator now has a lane-flattening pass (MeshStage.cs, behind the existing TOTK_MESH_THREADS_SCALE): the mesh body's subgroup lane id used for addressing is replaced by the global thread id dm_lid (subgroup masks keep the per-32-lane id), and the stage header mem[0] is scaled by threads/32 so one threadgroup covers that many 32-vertex blocks; all 32-lane strides stay, so the stage layout and export loops are untouched. On fed302d7 the 64-thread variant now generates, compiles standalone, has a reflection identical to the seed except threadgroup_size/mesh_stage.threads, and loads in-game via mode 3 (loaded mode=3, no boot crash, 50 fps until world entry). Remaining blocker, documented in games/oot/docs/mesh-threads-poc.md: at 64 threads the guest still faults at main+0x1c280-area (fault=0x1) — the raw-pool address path in the body still assumes 32 lanes; next step is to trace that access and flatten it too. Acceptance unchanged: probe-gpu-mesh + pixel-identical foliage vs the 32-thread build, then chain v29 + re-seed.

…ith ANY loaded 64-thread variant (flattened or not), so the blocker is the 64-thread dispatch path, not the lane flattening

mode 3 + no dir (32t fallback): fine, 3900 frames; mode 3 + dir but variant
rejected (hash fallback): fine, 3900 frames; mode 3 + variant loaded
(original unflattened _b AND the new flattened one): SIGSEGV fault=0x1 at
the first mesh draw, guest chain innermost main+0x5703c0 (vtable callee).
Next: map the faulting JIT pc / watch the slot header at the first 64t
draw, fix the dispatch, then re-validate the flattened variant.
@yannouuuu

Copy link
Copy Markdown
Author

Isolation result (commit 1e91a4cc): the world-entry SIGSEGV is not the lane duplication — it happens with any loaded 64-thread variant, flattened or not. Three runs on the PoC build (Kokiri, DRS): mode 3 with no variant dir (32-thread fallback) → fine, 3 900 frames; mode 3 + dir but variant rejected on hash → fine, 3 900 frames; mode 3 + variant loaded (original unflattened _b and the new flattened one) → SIGSEGV fault=0x1 right at the first fed302d7 mesh draw, guest chain innermost main+0x5703c0 (vtable callee at main+0x5703b8). So the blocker is the 64-thread dispatch path itself (object-block launch / grid sizing / slot bookkeeping at 64 threads). Next step, documented in mesh-threads-poc.md: map the faulting JIT pc via the runtime translation log or watch the slot-header words during the first 64-thread draw, fix the dispatch, then re-validate the flattened variant (probe-gpu-mesh + pixel-identical foliage vs 32 threads).

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