Skip to content

Metal: the GPU may run one frame behind the game (#67) - #241

Closed
peiikai wants to merge 7 commits into
beta_5from
mac-match-overlap
Closed

peiikai wants to merge 7 commits into
beta_5from
mac-match-overlap

Conversation

@peiikai

@peiikai peiikai commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes the Match-resolution 30 fps lock on Apple GPUs (#67).

Cause

The game waits for its frame's fence right after presenting it, so frame N+1's CPU work never overlapped frame N's GPU work: a frame cost CPU + GPU. Once that passed 16.7 ms the game fell to 30 fps with the GPU idle about a third of the time, and macOS read the idle GPU as one to clock down (DVFS), which kept it there: a lower clock made the GPU part longer, so the frame never fit 60 again.

Fix

nvnSyncWait now waits for the GPU work of the fence waited on before the newest one, so the GPU may run one frame behind the game. The newest list is still translated and submitted first (the game's command memory is free), readbacks finish during translation and uniform data is copied at translation.

Follow-ups from review (second commit):

  • With the lag on, the game's timing counters keep the host clock (as LARECOMP_MTL_COUNTER=cpu). The game reads them right after a wait that only guarantees the frame before, so they got the GPU's time only some frames: noisy input to the game's 30 fps decision. Now pace.c alone picks the 30 fps mode from frame times, as it already does on CPU-bound machines (GPU fence waits count as busy there).
  • LARECOMP_GPU_LAG_CHECK=1 also covers the storage buffers bound to shaders that use gmem (compute and graphics), counted separately.

Small buffers read through gmem (third commit): with the lag, the game rewrites some small buffers for frame N+2 while frame N may still be on the GPU (found by the lag check: a ping-pong pair of 8-byte storage buffers a compute shader reads, changed in flight 17% of the time with the lag and never without). Now a buffer of at most 256 bytes bound to a shader stage's driver table, that the shader only reads, is copied at translation after the driver constants (c0), and its table address points past the heap, where the shader's gmem loads read that copy (shader.h GSNAP). It's general, not by address:

  • The translator traces every gmem store to the table row its address comes from (a dataflow over the code; BRX jump tables are read from the compiler constants). A shader with a store it can't trace takes no snapshots.
  • Buffers that GPU work still in flight may store to stay in place (256-byte granules, marked per generation of submissions and cleared once those complete), so GPU-to-GPU data is never read stale.
  • Metal only, and only with the lag; LARECOMP_GSNAP=0 turns it off. The cost is a copy of a few bytes per draw / dispatch that has such buffers, plus the store tracing once per shader at translation. The Vulkan path is unchanged (its lag is off by default); the same table rewrite would fit its GLSL gmem path too.

Review follow-up (fourth and fifth commits):

  • LARECOMP_GPU_LAG_CHECK=1 now hashes the guest memory of snapshotted buffers too, and LARECOMP_GSNAP=2 takes snapshots without the lag. With the lag off, a snapshotted buffer that changes in flight is either the game rewriting it or a GPU store the tracing missed. 39-room sweep: 235 of ~240,000 snapshotted ranges changed; none of them overlapped a GPU store still in flight (one was stored by a later dispatch in the same command buffer, after the read). No missed GPU store found.
  • A per-shader-row diff of the in-place buffers over 256 bytes, lag on vs off, found a second ping-pong set: the same compute shader's four 3104-byte storage buffers, changed in flight in ~4% of its dispatches with the lag, never without. Compute snapshots now go up to 4 KB (draws stay at 256 bytes). After that, the only in-place buffers no GPU store reaches that change in flight are a 4608-byte and a 416-byte buffer that change as often without the lag.

Play-test: with f3a2ba3 (before the 3104-byte fix) the user saw brief full-screen black flickers walking near Field_09H at 5120x1440 (1x, MetalFX); LARECOMP_GPU_LAG=0 stopped them. With b42c04b they didn't show in a short watched session. A per-frame check of the presented images in scripted runs (hidden and one visible) never caught a black frame on either build, so the link to the 3104-byte race is from the play-test, not a reproduction.

Merged with current beta_5 (merge commit; translate.c keeps both beta_5's BRX jump-table labels for GLSL and this branch's store tracing for MSL).

Numbers

M3 Max, Match at 5120x2880: 30 -> 60 fps, GPU clock 425 -> 665-860 MHz. 3840x2160 30 -> 60, 7680x4320 23.5 -> 49; 1x and Dynamic unchanged at 60. Play-tested in a combined build: Match holds a steady 60.

With the snapshots (M3 Max, 130 s runs, one copy at a time with the GPU to itself): Match 5120x2880 60.0 fps (GPU ~640-1060 MHz), 1x 2560x1440 60.0; the render thread's CPU time per frame 2.5-3.0 ms vs 2.2-2.7 ms with LARECOMP_GSNAP=0. Some later runs had multi-second stalls in both modes (snapshots on and off alike, including at the title screen), from something else on the machine at the time.

Safety

A separate review found the lag safe for a test build. What the GPU reads in place from guest memory while a frame is in flight is vertex / index memory and storage / uniform buffers through gmem; LARECOMP_GPU_LAG_CHECK=1 hashes those ranges at translation and again when their command buffer completes (ranges read through snapshots are counted apart). 39-room warp sweep (tools/perf_sweep.py, Match), lag on vs LARECOMP_GPU_LAG=0:

lag on (with snapshots) lag off
vertex / index ranges changed in flight 0.30% 0.24%
buffers through gmem changed in flight (of all bound) 20.3% 22.6%
the 8-byte pair: read in place / changed 0 (all from snapshots) 13,298 / 0%

Vertex / index memory is at the known baseline (~0.3%). The buffer changes left are buffers the GPU writes itself (they change without the lag too). Before the snapshots, the 8-byte pair changed in flight 17% of the time with the lag. Screenshots with snapshots on and off match (Field and a dungeon room, apart from moving objects).

Switch

Default on with Metal; LARECOMP_GPU_LAG=0 restores the old wait (each fence wait waits for its own frame). Vulkan is unchanged (off unless LARECOMP_GPU_LAG=1).

🤖 Generated with Claude Code

peiikai and others added 6 commits October 20, 2026 10:00
The game waits for its frame's fence right after presenting it, so the
next frame's CPU work never overlapped that frame's GPU work: a frame
cost CPU + GPU. Past 16.7 ms it fell to 30 fps with the GPU idle a third
of the time, and macOS clocked the idle GPU down, which kept it there
(M3 Max, Match at 5120x2880: 30 fps at ~420 MHz; the M1 report: Match at
2880x1800 30-40 fps with the GPU ~45% busy).

nvnSyncWait now waits for the GPU work of the fence waited on before the
newest one. The newest list is still translated and submitted first, so
the game's command memory is free; readbacks finish during translation
and uniform data is copied at translation. LARECOMP_GPU_LAG_CHECK=1 (a
debug check of the vertex / index memory the GPU reads in place) shows
the same in-flight change rate with and without the lag.

M3 Max, Match: 5120x2880 27.6 -> 60 fps (clock 426 -> 860 MHz),
3840x2160 30 -> 60, 7680x4320 23.5 -> 49; 1x and Dynamic unchanged at 60.
Default on with Metal; LARECOMP_GPU_LAG=0 restores the old wait.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ck covers storage buffers

With the GPU a frame behind, the game reads its frame's timing counters right
after a fence wait that only guarantees the frame before, so a counter got
the GPU's time only some frames and the host clock the rest: noisy input to
the game's own 30 fps decision. With the lag on they keep the host clock
(as LARECOMP_MTL_COUNTER=cpu), and pace.c alone picks the 30 fps mode from
frame times, as it already does on CPU-bound machines. nvn_gpu_lag() holds
the LARECOMP_GPU_LAG switch for both.

LARECOMP_GPU_LAG_CHECK=1 now also hashes the storage buffers bound to
shaders that use gmem (compute and graphics), counted separately.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the GPU runs a frame behind (#67)

With the lag, the game rewrites some small buffers for frame N + 2 while
frame N may still be on the GPU: a ping-pong pair of 8-byte storage buffers
a compute shader reads (two animating floats) changed in flight 17% of the
time with the lag, never without (LARECOMP_GPU_LAG_CHECK).

A buffer of at most 256 bytes bound to a stage's driver table, that the
shader only reads, is now copied at translation after the driver constants
(c0), and its table address points past the heap, where the shader's gmem
loads read the copy (shader.h GSNAP). The translator traces every gmem
store to the table row its address comes from (a dataflow over the code,
BRX jump tables read from the compiler constants); a shader with a store it
can't trace takes no snapshots. Buffers that GPU work still in flight may
store to are left in place (256-byte granules, marked per generation of
submissions, cleared once those complete). Metal only, with the lag;
LARECOMP_GSNAP=0 turns it off. The lag check now covers the compute
uniform buffers read through gmem too and counts snapshots apart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… too (#67)

A per-shader-row diff of the lag check, lag on vs off, found a second
ping-pong set the game rewrites for frame N + 2 while frame N may still be
on the GPU: the same compute shader's four 3104-byte storage buffers,
changed in flight in ~4% of its dispatches with the lag, never without.
Snapshots now cover read-only buffers up to 4 KB (up to ~60 KB copied a
frame in the sweep); with that, no buffer that no GPU store reaches
changes in flight more with the lag than without.

LARECOMP_GPU_LAG_CHECK=1 now also hashes the guest memory of snapshotted
buffers, and LARECOMP_GSNAP=2 takes snapshots without the lag. With the lag
off, a snapshotted buffer changing in flight is either the game rewriting
it or a GPU store the store tracing missed; in a 39-room sweep none of
them overlapped a GPU store still in flight.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both ping-pong sets the lag check found are compute inputs. Draws keep the
256-byte limit: fragment shaders reading kilobytes of constant data with
per-pixel offsets are a cost not worth taking without a race found there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
translate.c: both new functions kept (beta_5's BRX jump-table labels for the
GLSL path, this branch's gmem store tracing for the MSL path).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reference translator (REF=HEAD) now defines the thread-local
maxwell_cb1_size as well, so linking both side by side failed with a
multiple definition on Windows CI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@peiikai
peiikai marked this pull request as draft October 20, 2026 22:34
@peiikai

peiikai commented Oct 20, 2026

Copy link
Copy Markdown
Owner Author

Shelved until Beta 6. The second race fix (b42c04b) cut the black flickers down but a longer play-test at 5120x1440 still showed them, so there is at least one more GPU/CPU race with the frame lag on. Not merging for Beta 5; Dynamic resolution covers the M1 Match case for now. To be investigated more deeply for Beta 6.

@peiikai

peiikai commented Oct 23, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #333 (the Metal CPU/GPU overlap with the race fixes), so closing this one.

@peiikai peiikai closed this Oct 23, 2026
@peiikai
peiikai deleted the mac-match-overlap branch October 23, 2026 18:54
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