Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Present: pacing leaves the shifted display link at 30 fps too
Donkey Kong Bananza in fullscreen on a variable-refresh screen: the screen's 1/240 s shift came while the
game had set interval 2, on the 30 Hz link. The switch that leaves the link took only 60 fps games, so frames
came 33 and 58 ms apart, 26-27 fps for 33 s, until the game went back to interval 1 and pacing left the link
at 60.

- link_late_note asks to leave the link at any frame rate: mode 0 presents each frame for its period, 30
  and 60 alike.
- link_sync no longer takes pacing back to the link when the rate changes after the move; only a screen or
  window that no longer fits does.
- t_present_rescue runs the handoff at 60 and at 30 fps and checks that a change of rate keeps mode 0
  (6 checks fail without the change).
  • Loading branch information
moretti committed Nov 14, 2026
commit 0ba60e2e91bfd2d7e76f66eb74ae18b66675e520
8 changes: 4 additions & 4 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Metal execution layer of the NVN null device, see nvn_mtl.h.
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
Expand Down Expand Up @@ -16713,7 +16713,7 @@
// before their deadline, and the link thread sits idle 20.7 ms before each late one (run 9). That costs 56 fps on the 60 Hz link and 58.6 on the 120 Hz link, at frame latency
// 2 and 1 alike, and a link made again keeps the offset. Without the link (mode 0: the present thread takes drawables and presents each frame for its period) the game held
// 60.03 fps for 15 minutes (run 10). So once LINK_SHIFT_N presents in a row were shown LINK_SHIFT_MS or more after their targets (three quarters of them; a frame a whole refresh
// late is a miss of its own; half of them, as when run 6's offset flipped each second at 60 fps, is not the steady shift), a 60 fps game leaves the link for mode 0, where it
// late is a miss of its own; half of them, as when run 6's offset flipped each second at 60 fps, is not the steady shift), the game leaves the link for mode 0 (at 60 or 30 fps), where it
// fits (link_leave_ok): fullscreen on a variable-refresh screen, which shows a frame when it is presented, at any rate. Fixed-rate screens (frame latency 1, where the shift was
// not seen) and windows keep the link; leaving those conditions takes pacing back to it. What the system changes at the shift is open (docs/reference/frame-rate.md)
#define LINK_SHIFT_N 120
Expand Down Expand Up @@ -16769,13 +16769,13 @@
}
// Mode 1 only: 1 is the default; 2 (120 Hz link) is a DM_PRESENT_LINK choice, which keeps the link though it also loses frames after the shift (58.6 fps)
if (!link_resync_on() || g_link_moved || g_link_leave || g_dm_present_link != 1 || !link_shift_judge(&g_link_shift, late_ms)) return;
if (link_leave_ok(fs, vrr) && mtl_fps_target() == 60) {
if (link_leave_ok(fs, vrr)) { // at 60 and 30 fps alike: mode 0 presents each frame for its period (Donkey Kong Bananza on the 30 Hz link after the shift: 33 and 58 ms frames, 26-27 fps)
// Only a request: link_sync, on the game's present thread, sets the mode and invalidates the link in one step. Setting the mode here would stop the callback taking frames
// while the link still owns the layer, and a game waiting in mtl_present_wait for an untaken frame would wait forever (the ticking link keeps the stall drop from firing)
g_link_leave = 1;
MLOG("display link: %d presents shown %.2f ms after their targets: pacing leaves the link at the next present, the present thread presents each frame (DM_PRESENT_LINK=0)", LINK_SHIFT_N, g_link_offset.level);
} else {
static int said; if (!said++) MLOG("display link: %d presents shown %.2f ms after their targets; pacing stays on the link (screen %d Hz %s, %s: leaving it needs fullscreen on a variable-refresh screen and a 60 fps game)",
static int said; if (!said++) MLOG("display link: %d presents shown %.2f ms after their targets; pacing stays on the link (screen %d Hz %s, %s: leaving it needs fullscreen on a variable-refresh screen)",
LINK_SHIFT_N, late_ms, hz, vrr ? "variable refresh" : "fixed refresh", fs ? "fullscreen" : "window");
}
}
Expand Down Expand Up @@ -17668,7 +17668,7 @@
}
if (g_link_leave) { g_link_leave = 0; if (want == 1 && !g_rescue_mode) { g_dm_present_link = want = 0; g_link_moved = 1; } } // asked by link_late_note; a mode set meanwhile wins
else if (g_link_moved && want) { g_link_moved = 0; g_link_shift = (link_shift_t){0}; } // a runtime setting (DM_AB, the settings) put pacing back on a link: it wins, and the shift is judged afresh
if (g_link_moved && (!link_leave_ok(front_fullscreen(), front_screen_variable_refresh()) || mtl_fps_target() != 60)) { // left for the shift (link_late_note), and the screen, window or rate no longer fits it
if (g_link_moved && !link_leave_ok(front_fullscreen(), front_screen_variable_refresh())) { // left for the shift (link_late_note), and the screen or window no longer fits it (a change of rate does not: mode 0 paces 30 and 60)
g_link_moved = 0; g_dm_present_link = want = 1; g_link_shift = (link_shift_t){0};
MLOG("display link: pacing back on the link (screen %d Hz %s, %s)", front_screen_max_fps(), front_screen_variable_refresh() ? "variable refresh" : "fixed refresh", front_fullscreen() ? "fullscreen" : "window");
}
Expand Down
14 changes: 8 additions & 6 deletions dm/gfx/tests/t_present_rescue.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,20 @@ static void thirty(void) { // A 30 fps target runs the link at 30 Hz, except i
g_dm_present_link = 2; link_sync(); CHECK("DM_PRESENT_LINK=2 keeps the 120 Hz link in a window", g_link_mode == 2 && test_match_fps == 120);
g_dm_present_link = 1; test_screen_hz = 0; test_variable_refresh = 0; link_sync(); g_link = nil; g_link_started = 0;
}
static void handoff(void) { // The shift judged while a frame waits untaken (link_late_note in the presented handler): the link keeps taking frames until link_sync, on the game's
static void handoff(int fps) { // The shift judged while a frame waits untaken (link_late_note in the presented handler): the link keeps taking frames until link_sync, on the game's
// present thread, sets mode 0 and invalidates it. Setting the mode in the handler stopped the callback while the link still owned the layer, and the waiting game hung
reset(); test_fullscreen = 1; test_variable_refresh = 1;
reset(); test_fullscreen = 1; test_variable_refresh = 1; test_game_fps = fps; // at 30 fps too: Donkey Kong Bananza's interval-2 scenes on the 30 Hz link after the shift ran 26-27 fps
SilentDisplayLink *link = [SilentDisplayLink new];
g_link = link; g_link_started = 1; g_link_rl = CFRunLoopGetCurrent(); g_link_moved = g_link_leave = 0; g_link_shift = (link_shift_t){0}; g_link_offset = (link_offset_t){0};
link_sync();
g_pq_in = 2; g_pq_read = 1; g_pq_done = 0; // frame 1 on its way to the screen, frame 2 queued and not yet taken: what the game waits for at its next acquire
int fired = 0;
for (uint64_t pn = 1000; pn < 1000 + 4 * LINK_SHIFT_N && !g_link_leave; pn++) {
double target = pn / 60.0;
double target = pn / (double)fps;
g_link_ring[pn % PRING] = (link_ent){.n = pn, .target = target};
pthread_mutex_lock(&g_pq_mu); link_late_note(pn, target + 1.0 / 240); fired = g_link_leave; pthread_mutex_unlock(&g_pq_mu);
}
CHECK("the steady 1/240 s shift asks to leave the link", fired);
CHECK(fps == 30 ? "at 30 fps the steady 1/240 s shift asks to leave the link" : "the steady 1/240 s shift asks to leave the link", fired);
pthread_mutex_lock(&g_pq_mu); int takes = link_takes(); pthread_mutex_unlock(&g_pq_mu);
CHECK("after the request the link still takes the waiting frame", takes && g_dm_present_link == 1 && g_link_owns_layer);
link_sync();
Expand All @@ -194,9 +194,11 @@ static void handoff(void) { // The shift judged while a frame waits untaken (l
CHECK("the link stops taking frames only once mode 0 is applied", !takes);
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, false);
CHECK("its run loop invalidates it and gives the layer to the present thread", link.invalidated && !g_link_owns_layer);
test_game_fps = fps == 30 ? 60 : 30; link_sync();
CHECK("the game changing between 30 and 60 fps keeps pacing off the link", g_dm_present_link == 0 && g_link_moved);
g_dm_present_link = 1; link_sync();
CHECK("a runtime setting back on the link wins over the move", !g_link_moved && g_dm_present_link == 1);
test_fullscreen = test_variable_refresh = 0; g_link = nil; g_link_started = 0; g_link_owns_layer = 0;
test_fullscreen = test_variable_refresh = 0; test_game_fps = 60; g_link = nil; g_link_started = 0; g_link_owns_layer = 0;
}
static void metal_lifecycle(void) {
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
Expand All @@ -216,7 +218,7 @@ static void metal_lifecycle(void) {
int main(int argc, char **argv) { @autoreleasepool {
alarm(10); setbuf(stdout, NULL);
if (argc > 2 || (argc == 2 && strcmp(argv[1], "--metal"))) return 2;
thresholds(); backoff(); settings(); lifecycle(); latency(); thirty(); vrr120(); handoff();
thresholds(); backoff(); settings(); lifecycle(); latency(); thirty(); vrr120(); handoff(60); handoff(30);
if (argc == 2) metal_lifecycle();
printf("present rescue: %d checks, %d failed\n", checks, failed); return failed != 0;
} }
6 changes: 3 additions & 3 deletions docs/reference/frame-rate.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,9 @@ Frame pacing: the host's frame step, engine frame limiters, present intervals an

- **Symptom:** a Mac game in fullscreen on a ProMotion screen runs at 60 fps and then, from one moment to the rest of the session, at 56 fps whatever the scene: GPU and CPU far inside the frame, `@sync_ms` 0, no step up. In `DM_NVN_TSV` `@pres_at_ms − @link_target_ms` goes from 0.0 to 4.17 ms on every frame, `@link_lead_ms` falls 32.9 → 28.7 ms, and about every 12th frame has `@link_gap_ms` near 24.6 ms with `@presented_ms` 25 then 20.8 ms. Moving the mouse makes it worse.
- **Cause (the trigger is open):** at one frame the system starts showing every frame exactly 4.17 ms (1/240 s) after the display link's target and keeps that offset. Nothing on the host side changes at that frame: in run 7 frame 20813 took 16.3 ms of work, its GPU work ended 58 ms before its target, and its callback ran 33 ms ahead as before. The moment differs per session (87, 251, 342 and 355 s), so it is no timer of ours; declaring the controller's input as user activity (`IOPMAssertionDeclareUserActivity`; macOS counts only keyboard and mouse) did not prevent it in a run. The offset alone costs nothing: frames 20814–20823 were all shifted and still 16.67 ms apart, and in run 6 the offset flipped 0 ↔ 4.15 ms about once a second for four minutes at 60 fps. Once it holds, the link calls back a tick late about every 11–12 frames: on the 60 Hz link at frame latency 2 the callback, which had started 0.43 ms after the frame two back reached the screen (p10–p90 0.37–0.47 ms over 18,800 frames, also the link's own schedule), now follows that flip 4.17 ms behind its schedule and every 12th tick comes 8 ms late (12.5 ms lost in every 200 ms, 56.5 fps); on the 120 Hz link one callback in 11 comes 4.17 ms late (58.6 fps), at frame latency 2 and 1 alike. Our side is idle then: the callback timing rows of 200 such frames show our callbacks running 0.16 ms with no wait, each present 8.1 ms before its deadline, and the link thread idle 20.7 ms before each late callback where it waits 8.33 ms otherwise. A display link made again keeps the offset (three sessions). What the system changes at the shift, and its rule for the late callbacks, are open.
- **Fix (a workaround):** `nvn_mtl.m` `link_late_note` checks each frame's presentation time against its link target. It counts presents in windows of 120 that follow each other (not a sliding window); a present more than 20 ms late (a whole refresh, a miss of its own) or dropped is not counted, so a window can span more presents and detection can take up to about 240. When three quarters or more of a window were 2 ms or more late (half late, as in run 6's flipping, is not the steady shift and held 60 fps), a 60 fps game leaves the display link for mode 0 (the present thread takes the drawables and presents each frame for its period), logged as `pacing leaves the link` with the settled offset. The presented handler only asks for it (`g_link_leave`); `link_sync`, on the game's present thread, sets `g_dm_present_link = 0` and invalidates the link in one step, as the runtime settings do, so the link keeps taking frames until then and a game waiting in `mtl_present_wait` for one it has not taken is served. Only the default mode 1 leaves the link: `DM_PRESENT_LINK=2` (the 120 Hz link), a manual setting, keeps it although it also loses frames after the shift (58.6 fps). A runtime setting that puts pacing back on a link (`DM_AB`, the settings) wins, and the shift is judged afresh. Only where it fits (`link_leave_ok`): fullscreen on a variable-refresh screen, which shows a frame when it is presented, at any rate. Fixed-rate screens (latency 1, where the shift was not seen) and windows keep the link, logged once as `pacing stays on the link`; leaving those conditions takes pacing back to it (`pacing back on the link`). `DM_PRESENT_RESYNC=0` turns the switch off. Every move of the offset is logged with the first present at the new offset, how long after the flip of the frame two back the callback ran, the screen's rate and refresh kind, fullscreen, latency and pacing mode (`frames now reach the screen … ms after the link's targets`; the first 8 in full, then a count a minute).
- **Found on:** [The Legend of Zelda: Breath of the Wild](../../dm/games/01007EF00011E000/README.md#gpu-work) (M5 Pro, fullscreen, controller only), runs 4–11. Run 7: 60 → 56 at the shift (355 s), then 58.6 fps on the 120 Hz link; run 8 on the 120 Hz link at latency 1 the same (58.63 fps after the shift at 340 s); run 9 the callback timing rows; run 10 in mode 0 from the start: 60.03 fps in every 30 s window for 15 minutes, game time 1.000 of real time. Mode 0 has no link targets, so the shift itself cannot be dated in run 10; every earlier session had it by 87–518 s. Run 11, with no settings: the shift at 210 s, `pacing leaves the link` about 2 s later and the move to mode 0 at frame 12729, then 60.0 fps in every 30 s window for the remaining 23 minutes, game time 1.000 of real time (but for one window in which the game lost focus).
- **Check:** `t_link_ticks` (the judge: no move for frames on their targets or for the offset flipping each second, a move for run 7's pattern; `link_leave_ok` for fixed and variable refresh, window and fullscreen; the offset tracker dates run 7's shift to its first present despite one on-target frame in twelve, and moves back once), `t_present_rescue --metal`, `check_t_present_snap`. A replay of the judge and the tracker over runs 4–7's `DM_NVN_TSV` dated the shift to the frame found by hand in runs 4, 5 and 7, and the judge fired where 56 fps began in run 6 (518 s). `t_present_rescue` also checks the handoff: with the shift judged while a frame waits untaken, the link still takes it, and the next present's `link_sync` moves to mode 0 and gives the layer to the present thread. **Not checked:** external screens (60 Hz, 144/165 Hz, adaptive sync) in a run.
- **Fix (a workaround):** `nvn_mtl.m` `link_late_note` checks each frame's presentation time against its link target. It counts presents in windows of 120 that follow each other (not a sliding window); a present more than 20 ms late (a whole refresh, a miss of its own) or dropped is not counted, so a window can span more presents and detection can take up to about 240. When three quarters or more of a window were 2 ms or more late (half late, as in run 6's flipping, is not the steady shift and held 60 fps), the game leaves the display link for mode 0, at 60 fps and at 30 fps (the 30 Hz link) alike, and a later change of rate keeps it there (the present thread takes the drawables and presents each frame for its period), logged as `pacing leaves the link` with the settled offset. The presented handler only asks for it (`g_link_leave`); `link_sync`, on the game's present thread, sets `g_dm_present_link = 0` and invalidates the link in one step, as the runtime settings do, so the link keeps taking frames until then and a game waiting in `mtl_present_wait` for one it has not taken is served. Only the default mode 1 leaves the link: `DM_PRESENT_LINK=2` (the 120 Hz link), a manual setting, keeps it although it also loses frames after the shift (58.6 fps). A runtime setting that puts pacing back on a link (`DM_AB`, the settings) wins, and the shift is judged afresh. Only where it fits (`link_leave_ok`): fullscreen on a variable-refresh screen, which shows a frame when it is presented, at any rate. Fixed-rate screens (latency 1, where the shift was not seen) and windows keep the link, logged once as `pacing stays on the link`; leaving those conditions takes pacing back to it (`pacing back on the link`). `DM_PRESENT_RESYNC=0` turns the switch off. Every move of the offset is logged with the first present at the new offset, how long after the flip of the frame two back the callback ran, the screen's rate and refresh kind, fullscreen, latency and pacing mode (`frames now reach the screen … ms after the link's targets`; the first 8 in full, then a count a minute).
- **Found on:** [The Legend of Zelda: Breath of the Wild](../../dm/games/01007EF00011E000/README.md#gpu-work) (M5 Pro, fullscreen, controller only), runs 4–11. Run 7: 60 → 56 at the shift (355 s), then 58.6 fps on the 120 Hz link; run 8 on the 120 Hz link at latency 1 the same (58.63 fps after the shift at 340 s); run 9 the callback timing rows; run 10 in mode 0 from the start: 60.03 fps in every 30 s window for 15 minutes, game time 1.000 of real time. Mode 0 has no link targets, so the shift itself cannot be dated in run 10; every earlier session had it by 87–518 s. Run 11, with no settings: the shift at 210 s, `pacing leaves the link` about 2 s later and the move to mode 0 at frame 12729, then 60.0 fps in every 30 s window for the remaining 23 minutes, game time 1.000 of real time (but for one window in which the game lost focus). [Donkey Kong Bananza](../../dm/games/040074A01BF12000/README.md) (fullscreen, variable refresh, 120 Hz): the shift came while the game had set interval 2 on the 30 Hz link, which the switch did not leave then (it took only 60 fps games): frames 33 and 58 ms apart, 26-27 fps for 33 s, until the game went back to interval 1 and the switch left the link at 60.
- **Check:** `t_link_ticks` (the judge: no move for frames on their targets or for the offset flipping each second, a move for run 7's pattern; `link_leave_ok` for fixed and variable refresh, window and fullscreen; the offset tracker dates run 7's shift to its first present despite one on-target frame in twelve, and moves back once), `t_present_rescue --metal` (the handoff at 60 and at 30 fps, and a change of rate after the move keeps mode 0), `check_t_present_snap`. A replay of the judge and the tracker over runs 4–7's `DM_NVN_TSV` dated the shift to the frame found by hand in runs 4, 5 and 7, and the judge fired where 56 fps began in run 6 (518 s). `t_present_rescue` also checks the handoff: with the shift judged while a frame waits untaken, the link still takes it, and the next present's `link_sync` moves to mode 0 and gives the layer to the present thread. **Not checked:** external screens (60 Hz, 144/165 Hz, adaptive sync) in a run.

## CPU cost and pacing

Expand Down
Loading