Skip to content

A covered Mac window's presents follow the host's clock instead of its once-a-second display link - #1302

Closed
lunks wants to merge 1 commit into
seedds:mainfrom
lunks:pr/covered-pacing
Closed

lunks wants to merge 1 commit into
seedds:mainfrom
lunks:pr/covered-pacing

Conversation

@lunks

@lunks lunks commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

What and why

Who needs it: anyone who leaves a game running while typing in another app, or whose window is minimized or on another Space. Without it a Mac game window that is covered runs at a few frames a second. The notes already work around it with DM_PRESENT_LINK=0 or DM_WINDOW_TOP=1: ANIMAL WELL 59.7 to 27.5 fps covered, Breath of the Wild 20 fps, Pokémon Violet about 12 fps, Fire Emblem Fortune's Weave 80-180 ms frames, Hyrule Warriors: Age of Imprisonment's held DM_CTL screenshots. We saw it on Star Fox (merged today as 67f1d00): 333 ms frames while typing in another app. No issue reports it.

Cause: macOS fires a covered window's display link about once a second, so each acquire waits out link_wait's timeout and then mtl_present_wait's stall fallback, and every rare tick ends the stall so the next frames wait again. The silent-link rescue does not apply, since the game is usually out of focus then.

The Mac front end now keeps the window's occlusion state (front_window_covered, from NSWindowDidChangeOcclusionStateNotification which it already logs, and read once a second after the window opens, for a window that opens covered). While it is covered, link_sync runs mode 0 as a rescue does: the link is invalidated, the present thread takes its own drawables and the acquire waits for the host's clock at the present interval. Visible again, the requested mode returns with a fresh stall clock; the link's silence while covered is no stall and triggers no rescue. The frame step, targets and on-screen pacing are unchanged; focus (DM_FOCUS_KEEP) is untouched. Reference entry: A covered window's presents follow the host's clock.

Checked

  • Tests (M5 Pro, macOS 27.0, on this branch over main 7550be1): make -C dm/gfx/tests GAME=040074A01BF12000 run passes; t_present_rescue 91 checks, 0 failed, with the new covered cases (pacing leaves the link at once, the link is invalidated before the layer goes to the present thread, no rescue while covered, the link and a fresh stall clock in view again, DM_PRESENT_LINK=0 unchanged). make -C dm/host/tests GAME=040074A01BF12000 run passes. test_docs.py passes; docs_symptoms.py --check passes.
  • Games run: none with this change. The 333 ms frames are from Star Fox on our fork before the change. The frame rate and game speed while covered, and the return to the link, are not verified in a game.
  • regress.py check: not run (no game runs here). regress.py sets DM_WINDOW_TOP=1, so its routes keep a visible window and should not reach this path.
  • Lint: no Python in the change; lint_c.py not run (no clang-tidy of the LLVM version lint-c.yml pins here).

Docs

  • The finding is in its docs/reference/frame-rate.md entry, with a line in Star Fox's note, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • A chain tool changed: not applicable.
  • No game files, keys or data extracted from a game in the change or the attachments

@github-actions github-actions Bot added host The Horizon host: SVCs, services, threads, memory (dm/host) nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) mac The Mac front end and app (dm/mac, front_mac.m) docs Documentation game: 0400E88025B1A000 A game profile labels Nov 5, 2026
fpedrolucas95 pushed a commit to fpedrolucas95/nxnative that referenced this pull request Nov 6, 2026
…follow the host's clock instead of its once-a-second display link
A covered Mac window (other windows over it, minimized, another Space) dropped
to about 3 fps: Star Fox had 333 ms frames while the player typed in another
app. macOS fires a covered window's display link about once a second, so each
acquire waited out link_wait's timeout and then mtl_present_wait's stall
fallback, and every rare tick ended the stall so the next frames waited again.
The silent-link rescue did not apply, since the game is usually out of focus
then. DM_PRESENT_LINK=0 avoided it.

The Mac front end now keeps the window's occlusion state
(front_window_covered, from the occlusion notification and read once a second
after the window opens). While it is covered, link_sync runs mode 0 as a rescue
does: the link is invalidated, the present thread takes its own drawables and
the acquire waits for the host's clock at the present interval. Visible again,
the requested mode returns with a fresh stall clock; the link's silence while
covered is no stall and triggers no rescue. The frame step, targets and
on-screen pacing are unchanged; focus (DM_FOCUS_KEEP) is untouched.

Checked: t_present_rescue's new covered cases (91 checks), make -C dm/gfx/tests
and dm/host/tests GAME=040074A01BF12000 run. No game run with the change yet.
@lunks
lunks force-pushed the pr/covered-pacing branch from e1b2daf to b11cbb8 Compare November 8, 2026 16:57
AmateurWizrd added a commit to AmateurWizrd/nxnative that referenced this pull request Nov 9, 2026
khalilgharbaoui pushed a commit to khalilgharbaoui/nxnative-ultimate that referenced this pull request Nov 14, 2026
…it in variable-refresh fullscreen

Breath of the Wild in fullscreen on a ProMotion Mac (M5 Pro): at one frame of a session (87, 251, 342
and 355 s in four of them) 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. From then
on the link calls back a tick late about every 11-12 frames: 56 fps on the 60 Hz link, 58.6 on the
120 Hz link at frame latency 2 and 1 alike. A display link made again keeps the offset. Without the
link (mode 0, the present thread presenting each frame for its period) the game held 60.03 fps for 15
minutes at game time 1.000.

- link_late_note checks each frame's presentation time against its link target. When 120 presents in
  a row were three quarters or more 2 ms or more late, a 60 fps game leaves the link for mode 0, where
  it fits (link_leave_ok): fullscreen on a variable-refresh screen, at any rate. Fixed-rate screens and
  windows keep the link; leaving those conditions takes pacing back to it. DM_PRESENT_RESYNC=0 turns the
  switch off.
- Each move of the frames' offset against the link's targets is logged with its first present, the
  callback's time after the flip two frames back, the screen's rate and refresh kind, fullscreen,
  latency and pacing mode (the first 8, then a count a minute).
- DM_NVN_TSV gains where the link callback that took each frame spent its time: @link_slack_ms
  (update.targetTimestamp minus the present call), @link_wait_ms (snapshot and external-producer
  waits), @link_alloc_ms (present_now's allocator wait), @link_run_ms (entry to exit), @link_idle_ms
  (since the previous callback exited), @link_held_ms (the longest callback that took no frame since
  the previous present), @link_lock_ms (the wait for g_pq_mu). On 200 late frames on the 120 Hz link
  our callbacks ran 0.16 ms (max 0.5) with no wait, each present came 8.1 ms before its deadline, and
  the link thread sat idle 20.7 ms before each late callback where it waits 8.33 ms otherwise: the
  system called back late, not the host.
- The link callback's hold counts the link's own ticks (link_hold_s): a frame stays on screen for its
  whole period when that covers two or more ticks. The fixed "period > 25 ms" let every tick of the
  120 Hz link (DM_PRESENT_LINK=2) take a 60 fps frame, and with present snapshots the game ran at 87 fps
  and 1.16 of real time. The 60 and 30 Hz links hold exactly as before.

t_link_ticks 48 checks (the hold on 120, 60 and 30 Hz and virtual ticks; the shift judge;
link_leave_ok; the offset tracker); t_present_rescue --metal; check_t_present_snap. A replay of the
judge and the tracker over four sessions' frame rows dated the shift to the frame found by hand and
fired where 56 fps began. Not checked: the switch itself in a run; external screens.

Hand merge (nvn_mtl.m, t_present_rescue.m, env_switches.tsv): the presented handler and the hand-over to the system are the fork's now (DM_PRESENT_SHOWN, the present after the GPU with its show block): link_late_note goes into that handler and g_pn_present_at is set where show hands the frame over. In link_sync the fork's covered-window pacing (PR seedds#1302) comes first, and pacing taken back to the link for a screen that no longer fits leaves a covered window off it. t_present_rescue runs covered() and handoff() both.

(cherry picked from commit e5ef9ba)
khalilgharbaoui pushed a commit to khalilgharbaoui/nxnative-ultimate that referenced this pull request Nov 14, 2026
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).

Hand merge: link_sync keeps a covered window off the link (the fork's PR seedds#1302) when pacing goes back to it; t_present_rescue runs covered() beside handoff(60) and handoff(30).

(cherry picked from commit 0ba60e2)
khalilgharbaoui pushed a commit to khalilgharbaoui/nxnative-ultimate that referenced this pull request Nov 14, 2026
…it in variable-refresh fullscreen

Breath of the Wild in fullscreen on a ProMotion Mac (M5 Pro): at one frame of a session (87, 251, 342
and 355 s in four of them) 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. From then
on the link calls back a tick late about every 11-12 frames: 56 fps on the 60 Hz link, 58.6 on the
120 Hz link at frame latency 2 and 1 alike. A display link made again keeps the offset. Without the
link (mode 0, the present thread presenting each frame for its period) the game held 60.03 fps for 15
minutes at game time 1.000.

- link_late_note checks each frame's presentation time against its link target. When 120 presents in
  a row were three quarters or more 2 ms or more late, a 60 fps game leaves the link for mode 0, where
  it fits (link_leave_ok): fullscreen on a variable-refresh screen, at any rate. Fixed-rate screens and
  windows keep the link; leaving those conditions takes pacing back to it. DM_PRESENT_RESYNC=0 turns the
  switch off.
- Each move of the frames' offset against the link's targets is logged with its first present, the
  callback's time after the flip two frames back, the screen's rate and refresh kind, fullscreen,
  latency and pacing mode (the first 8, then a count a minute).
- DM_NVN_TSV gains where the link callback that took each frame spent its time: @link_slack_ms
  (update.targetTimestamp minus the present call), @link_wait_ms (snapshot and external-producer
  waits), @link_alloc_ms (present_now's allocator wait), @link_run_ms (entry to exit), @link_idle_ms
  (since the previous callback exited), @link_held_ms (the longest callback that took no frame since
  the previous present), @link_lock_ms (the wait for g_pq_mu). On 200 late frames on the 120 Hz link
  our callbacks ran 0.16 ms (max 0.5) with no wait, each present came 8.1 ms before its deadline, and
  the link thread sat idle 20.7 ms before each late callback where it waits 8.33 ms otherwise: the
  system called back late, not the host.
- The link callback's hold counts the link's own ticks (link_hold_s): a frame stays on screen for its
  whole period when that covers two or more ticks. The fixed "period > 25 ms" let every tick of the
  120 Hz link (DM_PRESENT_LINK=2) take a 60 fps frame, and with present snapshots the game ran at 87 fps
  and 1.16 of real time. The 60 and 30 Hz links hold exactly as before.

t_link_ticks 48 checks (the hold on 120, 60 and 30 Hz and virtual ticks; the shift judge;
link_leave_ok; the offset tracker); t_present_rescue --metal; check_t_present_snap. A replay of the
judge and the tracker over four sessions' frame rows dated the shift to the frame found by hand and
fired where 56 fps began. Not checked: the switch itself in a run; external screens.

Hand merge (nvn_mtl.m, t_present_rescue.m, env_switches.tsv): the presented handler and the hand-over to the system are the fork's now (DM_PRESENT_SHOWN, the present after the GPU with its show block): link_late_note goes into that handler and g_pn_present_at is set where show hands the frame over. In link_sync the fork's covered-window pacing (PR seedds#1302) comes first, and pacing taken back to the link for a screen that no longer fits leaves a covered window off it. t_present_rescue runs covered() and handoff() both.

(cherry picked from commit e5ef9ba)
khalilgharbaoui pushed a commit to khalilgharbaoui/nxnative-ultimate that referenced this pull request Nov 14, 2026
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).

Hand merge: link_sync keeps a covered window off the link (the fork's PR seedds#1302) when pacing goes back to it; t_present_rescue runs covered() beside handoff(60) and handoff(30).

(cherry picked from commit 0ba60e2)
seedds added a commit that referenced this pull request Nov 25, 2026
macOS fires an occluded window's CAMetalDisplayLink about once a second, stalling acquire and presentation on covered Mac windows (Star Fox 333 ms frames, ANIMAL WELL 59.7 -> 27.5 fps, Hades 60.0 -> 21.9 fps, and DM_CTL screenshots delayed until uncover). Out-of-focus games did not trigger silent-link rescue.

Export front_window_covered() from front_mac.m (NSWindowDidChangeOcclusionStateNotification plus a 1 s post-open check, preserving dm_fstep_settle() on uncover). While covered, link_sync switches to mode 0 (invalidating the link, including during startup) so the present thread takes drawables and acquires follow the host clock, returning to the requested link with a fresh stall clock when visible. Coordinate with NvMetalDisplayLinkDelegate, active/probation watchdog checks, timed rescue expiry, and DM_PRESENT_SCREEN.

Checked: t_present_rescue --metal (193 checks, 0 failed), t_present_snap, t_link_ticks (91 checks), lint_c.py tidy (LLVM 23.1.4, 0 new findings), test_docs.py, live cover/uncover A/B on Hades (59.7 fps covered, 0.17 s DM_CTL shot, clean return to 59.9 fps on display link), and regress.py check (full) on Hades (PASS).

Co-authored-by: Pedro Nascimento <91118+lunks@users.noreply.github.com>
@seedds

seedds commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @lunks for the diagnosis and patch!

Landed on main as 9594077 (with co-author credit). While rebasing onto current main, we preserved the frame-step settle hook on uncover and wired the covered-window state into the newer display-link startup invalidation, probation/rescue checks, stray callback guard, and screen-wait path so covered windows never stall or falsely trip rescue/probation.

Checked:

  • Metal-validated presentation unit tests (193 checks in t_present_rescue --metal, plus t_present_snap, t_link_ticks, clang-tidy, and doc tests).
  • Live cover/uncover A/B test on Hades (Mac, M6, macOS 27.2.1): baseline dropped from 60.0 to ~22 fps with repeated display-link stalls and a 9.8 s screenshot delay while covered; with this fix it stayed at 59.7 fps with zero stalls, took screenshots in 0.17 s while covered, and returned smoothly to 59.9 fps on the display link once uncovered.
  • Full regression smoke run on Hades passed (59.9 → 60.0 fps).

Still open: live cover/uncover on an NVN title (including Star Fox) was not run here because Xcode's offline Metal toolchain was unavailable on this machine; those NVN-specific paths are covered by the unit tests.

@seedds seedds closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation game: 0400E88025B1A000 A game profile host The Horizon host: SVCs, services, threads, memory (dm/host) mac The Mac front end and app (dm/mac, front_mac.m) nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants