diff --git a/dm/games/0400E88025B1A000/README.md b/dm/games/0400E88025B1A000/README.md index 3ab9c521e8..d4768ef55c 100644 --- a/dm/games/0400E88025B1A000/README.md +++ b/dm/games/0400E88025B1A000/README.md @@ -46,6 +46,7 @@ Every 3D target is 768x432 from the first frame of the title to the tutorial: Me - [Ampere BMSK and the read of a covered pixel](../../../docs/reference/shaders.md#ampere-bmsk-and-the-read-of-a-covered-pixel): the last 3 shaders, `58046f286de779e1`, `46040940ce0fd49b` and `826168691af2e211`, none asked for in play so far. - [Compute pipelines compiled at first use](../../../docs/reference/shaders.md#compute-pipelines-compiled-at-first-use): 233 compute pipelines in the app's archive, none built at the title. - [Compute pipelines outside the bundled archive built at every start](../../../docs/reference/shaders.md#compute-pipelines-outside-the-bundled-archive-built-at-every-start): the second headless start took its 8 from the device's archive. +- [A covered window's presents follow the host's clock](../../../docs/reference/frame-rate.md#a-covered-windows-presents-follow-the-hosts-clock): 333 ms frames while the player typed in another app. ## Open diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index 3cd9260079..23c3bb1b17 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -14995,7 +14995,14 @@ - (void)metalDisplayLink:(CAMetalDisplayLink *)link needsUpdate:(CAMetalDisplayL static CAMetalDisplayLink *g_link; static CFRunLoopRef g_link_rl; static int g_link_started; static int g_link_owns_layer, g_classic_busy; // Under g_pq_mu: nextDrawable throws while the layer has the display link object attached, so the present thread that takes its own drawable and the display link object are mutually exclusive -static int link_on(void) { return g_dm_present_link && !atomic_load(&g_rescue_mode) && g_link != nil; } +// A covered Mac window (no part on screen): macOS fires its display link about once a second, so a game the link paces drops to a few frames a second (Star Fox: +// 333 ms frames while the player typed in another app; Breath of the Wild stepped up to 3 vsyncs behind another window and stayed there). While the front end says +// so, link_sync runs mode 0, as a rescue does: the present thread takes drawables itself and the acquire waits for the host's own clock at the present interval +// (nvnWindowAcquireTexture in hle_nvn.c), the same frame step and targets as with the link. Visible again, the requested mode returns with a fresh stall clock. +// Focus is not involved: the window may be covered while the game keeps focus (DM_FOCUS_KEEP) and in front while another app is active +__attribute__((weak)) int front_window_covered(void) { return 0; } // front_mac.m; graphics unit tests and iOS have none +static _Atomic int g_covered; // link_sync's sample of front_window_covered; also read by link_on without g_pq_mu +static int link_on(void) { return g_dm_present_link && !atomic_load(&g_rescue_mode) && !atomic_load(&g_covered) && g_link != nil; } // Screens whose refresh rate is not a multiple of the link's (144 or 165 Hz against 60 or 30): asked for 60 Hz, the system fires the link on every third refresh of a 165 Hz screen, // 55 Hz, and a game paced by the link runs at 55 fps (27.5 at 30). There the link runs at the screen's rate and only the refreshes nearest a steady clock of the wanted rate count as // ticks (g_link_vperiod, NvMetalDisplayLinkDelegate): 60 a second on average, each frame shown on the refreshes nearest its 1/60 s slot. DM_LINK_VTICK=0 keeps the system's choice. @@ -15057,7 +15064,13 @@ static void link_sync(void) { // Called every frame by the game's present thre g_rescue_mode = 0; g_stall_t0 = 0; __atomic_store_n(&g_link_tick_ns, now_ns(), __ATOMIC_RELAXED); MLOG("display link: rescue ended, trying pacing mode %d (frame %llu)", want, (unsigned long long)atomic_load(&g_cur_frame)); } - if (g_rescue_mode) want = 0; + int cov = front_window_covered() != 0; + if (cov != atomic_load(&g_covered)) { + atomic_store(&g_covered, cov); + if (!cov) { g_stall_t0 = 0; __atomic_store_n(&g_link_tick_ns, now_ns(), __ATOMIC_RELAXED); } // the link's silence while covered is not a stall + if (want) MLOG("display link: window %s (frame %llu)", cov ? "covered, presents follow the host's clock" : "visible, back to the display link", (unsigned long long)atomic_load(&g_cur_frame)); + } + if (g_rescue_mode || cov) want = 0; pthread_mutex_unlock(&g_pq_mu); if (want && mtl_fps_target() == 30 && link_30hz()) want = 3; // Power-saving mode: display link at 30 Hz if (want) front_screen_match(want == 3 ? 30 : want == 2 ? 120 : 60); @@ -15308,7 +15321,8 @@ void mtl_present_wait(uint64_t tk) { pthread_cond_broadcast(&g_pq_cv); if (!g_stall_t0) { g_stall_t0 = last ? last : now; g_stall_drop = 0; g_stall_red = 0; g_stall_logged = 0; g_stall_n++; } g_stall_drop += n; - int active = dm_focus_now() == 1; uint64_t since = dm_focus_in_since(), from = g_stall_t0 > since ? g_stall_t0 : since; + // A covered window's link is silent until link_sync lets it go (front_window_covered): no stall of an active app, no rescue + int active = dm_focus_now() == 1 && !atomic_load(&g_covered); uint64_t since = dm_focus_in_since(), from = g_stall_t0 > since ? g_stall_t0 : since; uint64_t elapsed = now >= from ? now - from : 0; // Focus can return after the earlier now sample, including while waiting for an in-flight present if (active && !g_stall_logged && elapsed >= STALL_NS) { g_stall_logged = 1; MLOG("display link silent for %.0f ms (active): dropping %llu queued frames not yet displayed, the game continues (stall #%llu; back to normal when the link returns)", (now - from) / 1e6, (unsigned long long)n, (unsigned long long)g_stall_n); } diff --git a/dm/gfx/tests/t_present_rescue.m b/dm/gfx/tests/t_present_rescue.m index 2960d73503..3428c2be64 100644 --- a/dm/gfx/tests/t_present_rescue.m +++ b/dm/gfx/tests/t_present_rescue.m @@ -30,6 +30,7 @@ - (void)invalidate { self.invalidated = YES; } static uint64_t test_now; int test_focus = 1; int test_fullscreen; +int test_covered; int test_variable_refresh; int test_game_fps = 60; uint64_t test_focus_since; @@ -41,7 +42,7 @@ void svc_thread_dump(void) {} static int checks, failed; #define CHECK(name, ok) do { int pass = !!(ok); checks++; failed += !pass; printf("%s: %s\n", name, pass ? "PASS" : "FAIL"); } while (0) static void reset(void) { - test_now = 10000000000ull; test_focus = 1; test_focus_since = 0; + test_now = 10000000000ull; test_focus = 1; test_focus_since = 0; test_covered = 0; g_covered = 0; g_dm_present_link = 1; g_rescue_mode = 0; g_rescue_back_ns = 3000000000ull; g_rescue_n = 0; g_link_tick_ns = test_now - 1000000000ull; g_stall_t0 = g_link_tick_ns; g_stall_logged = g_stall_red = 0; g_stall_drop = 0; @@ -99,6 +100,30 @@ static void lifecycle(void) { CHECK("rescue invalidates the link before releasing the layer", link.invalidated && !g_link && !g_link_owns_layer); g_link_started = 0; } +static void covered(void) { // A covered window: the host's clock paces (mode 0) until it is visible, then the requested link again; the link's silence meanwhile is no stall + reset(); SilentDisplayLink *link = [SilentDisplayLink new]; + g_link = link; g_link_started = 1; g_link_rl = CFRunLoopGetCurrent(); + link_sync(); CHECK("a visible window keeps the display link", mtl_link_active() && g_link_mode == 1); + test_covered = 1; link_sync(); + CHECK("a covered window stops display-link pacing at once", !mtl_link_active() && !mtl_link_wait_tick(1, 1000)); + CHECK("covered keeps the requested mode and is no rescue", g_dm_present_link == 1 && !g_rescue_mode && g_link_mode == 0); + g_stall_t0 = g_link_tick_ns = test_now - 1000000000ull; wait_frame(); + CHECK("a covered window's silent link does not trigger rescue", !g_rescue_mode); + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, false); + CHECK("covered invalidates the link and releases the layer to the present thread", link.invalidated && !g_link && !g_link_owns_layer); + g_link_started = 0; + for (int frame = 0; frame < 3; frame++) link_sync(); + CHECK("no link while the window stays covered", g_link_mode == 0 && !g_link_started && !mtl_link_active()); + g_stall_t0 = test_now - 2000000000ull; g_link_tick_ns = test_now - 2000000000ull; test_now += 1000; + test_covered = 0; link_sync(); + CHECK("visible again wants the requested link", g_link_mode == 1 && g_dm_present_link == 1); + CHECK("visible again starts a fresh stall clock", !g_stall_t0 && g_link_tick_ns == test_now); + g_link = [SilentDisplayLink new]; g_link_owns_layer = 1; // as link_main does once the new link exists + CHECK("the new link paces again", mtl_link_active()); + g_link = nil; g_link_started = 0; + reset(); g_dm_present_link = 0; test_covered = 1; link_sync(); test_covered = 0; link_sync(); + CHECK("DM_PRESENT_LINK=0 stays without the link either way", !mtl_link_active() && !g_link_started); +} static void latency(void) { // Only variable-refresh fullscreen needs the latency-2 workaround; a new link takes the current policy as link_main does. reset(); test_fullscreen = 0; test_variable_refresh = 1; g_present_latency = 0; SilentDisplayLink *link = [SilentDisplayLink new]; link.preferredFrameLatency = link_latency(); @@ -161,7 +186,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(); + thresholds(); backoff(); settings(); lifecycle(); covered(); latency(); thirty(); if (argc == 2) metal_lifecycle(); printf("present rescue: %d checks, %d failed\n", checks, failed); return failed != 0; } } diff --git a/dm/gfx/tests/t_present_rescue_focus.c b/dm/gfx/tests/t_present_rescue_focus.c index 8413e9a4fe..33fe0caed8 100644 --- a/dm/gfx/tests/t_present_rescue_focus.c +++ b/dm/gfx/tests/t_present_rescue_focus.c @@ -1,4 +1,4 @@ -// Strong focus, fullscreen and frame rate hooks override the graphics layer's weak, always-active test defaults. +// Strong focus, fullscreen, frame rate and window-covered hooks override the graphics layer's weak, always-active test defaults. #include extern int test_focus; extern uint64_t test_focus_since; @@ -10,3 +10,5 @@ extern int test_variable_refresh; int front_screen_variable_refresh(void) { return test_variable_refresh; } extern int test_game_fps; int test_game_fps_now(void) { return test_game_fps; } +extern int test_covered; +int front_window_covered(void) { return test_covered; } diff --git a/dm/host/front_mac.m b/dm/host/front_mac.m index 7cd59bb10e..342a22a3e6 100644 --- a/dm/host/front_mac.m +++ b/dm/host/front_mac.m @@ -59,6 +59,8 @@ static void screen_refresh(NSScreen *screen) { // main thread: refresh capabil // the game, which keeps its focus, and ShowKeyboard's applet sends its own out-of-focus / in-focus pair (DM_APPLET). Monster Hunter Rise dropped the hunter's name after the round trip and asked again. Main thread only static int g_text_box; int front_fullscreen(void) { return g_fullscreen; } // nvn_mtl.m link_latency: frame latency 2 in variable-refresh fullscreen +static _Atomic int g_window_covered; // no part of the window is on screen (other windows over it, minimized, another Space): from its occlusion state, on the main thread +int front_window_covered(void) { return g_window_covered; } // nvn_mtl.m link_sync: presents follow the host's clock while it is // The screen's refresh rate matched to the game's frame rate in fullscreen (front_screen_match, from PR #79; DM_SCREEN_MATCH=0 turns it off). 60 fps on a 144 Hz screen cannot be paced // evenly: the display link counts a tick on the refresh nearest each 1/60 s slot (nvn_mtl.m link_range), so frames stay up 2 and 3 refreshes in turn (13.9 / 20.8 ms) and Apple's HUD shows // the frame interval jumping between them. In fullscreen, a screen whose rate is not a multiple of the game's switches to the mode of the same size with the highest rate that is (144 → 120 Hz @@ -1463,9 +1465,13 @@ void front_run(void (*guest)(void)) { // another app's window macOS fires its display link about once a second: Breath of the Wild stepped up to 3 vsyncs at boot and never came back (20 fps runs). // DM_WINDOW_TOP=1 orders it in front without activation and keeps it above other apps' windows; the log names every change of the window's visibility either way if (getenv("DM_WINDOW_TOP") && atoi(getenv("DM_WINDOW_TOP"))) { win.level = NSFloatingWindowLevel; [win orderFrontRegardless]; LOG("front end: window kept in front (DM_WINDOW_TOP)"); } - [[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidChangeOcclusionStateNotification object:win queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { - (void)n; static int vis = -1; int v = (win.occlusionState & NSWindowOcclusionStateVisible) != 0; if (v == vis) return; vis = v; - if (v) LOG("front end: window visible"); else LOG("front end: window covered (its display link fires about once a second: frame times measure nothing)"); }]; + // Covered, the display link would pace the game at that rate (Star Fox: 333 ms frames while the player typed in another app), so the present thread paces presents by the + // host's clock until the window is visible again (nvn_mtl.m link_sync). The state is read once more a second after opening: a window that opens behind another and stays + // there may send no change (a second, not at once, to leave the window server time to place it) + void (^occl)(void) = ^{ static int vis = -1; int v = (win.occlusionState & NSWindowOcclusionStateVisible) != 0; if (v == vis) return; vis = v; g_window_covered = !v; + if (v) LOG("front end: window visible"); else LOG("front end: window covered (its display link fires about once a second: presents follow the host's clock until it is visible)"); }; + [[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidChangeOcclusionStateNotification object:win queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { (void)n; occl(); }]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), occl); // Main menu: AppKit runs ⌘ shortcuts only through menu items, so ⌘Q quits and ⌃⌘F toggles fullscreen from here (AppKit shows the item as Globe-F and keeps ⌃⌘F as a hidden twin) NSMenu *bar = [NSMenu new], *app = [NSMenu new], *wm = [[NSMenu alloc] initWithTitle:@"Window"]; [[bar addItemWithTitle:@"" action:nil keyEquivalent:@""] setSubmenu:app]; diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 4da0f679d0..ec7d86ffbe 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -475,6 +475,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - none seen in a game. When the presented texture changes size or format while snapshots are on, a display command buffer still queued could sample the ring slot's old texture outside the residency set, which faults the queue (`kIOGPUCommandBufferCallbackErrorPageFault`, then every later command buffer ignored), as in A present snapshot's copy needs the same residency commit. → [A replaced snapshot ring texture stays resident until the slot's next use](../reference/frame-rate.md#a-replaced-snapshot-ring-texture-stays-resident-until-the-slots-next-use) - a steady game whose frame-rate counter wanders between 50 and 60 on the iPhone only; the `display link up to tick` lines show about as many empty ticks as frames dropped by merging: frames arrive two in one tick and none in the next. → [Vulkan acquire and vsync event follow the display link](../reference/frame-rate.md#vulkan-acquire-and-vsync-event-follow-the-display-link) - a `regress.py` run of an NVN game stays at 20 fps from the start (`frame N not reached`, a NEW-FAIL) in some runs only: `frame step: hold 1 → 2` and `2 → 3 vsyncs` in the first 15 presents, then `display link recovered: stalled 1017 ms` about once a second to the end, and no trial back to a shorter hold. → [A game window another process starts opens behind other windows](../reference/frame-rate.md#a-game-window-another-process-starts-opens-behind-other-windows) +- a Mac game window covered by other windows (or minimized, or on another Space) drops to about 3 fps: 333 ms frames while the player types in another app, back to normal once it is in view again. `DM_PRESENT_LINK=0` avoided it. → [A covered window's presents follow the host's clock](../reference/frame-rate.md#a-covered-windows-presents-follow-the-hosts-clock) - asked for 60 Hz, the system fires the display link on every third refresh of a 165 Hz screen, 55 Hz, so games paced by it ran at 55 fps (27.5 at 30). → [Virtual 60 Hz ticks on screens that are not a multiple of 60](../reference/frame-rate.md#virtual-60-hz-ticks-on-screens-that-are-not-a-multiple-of-60) - virtual ticks keep the average, but a frame still stays up two or three refreshes in turn (13.9 / 20.8 ms at 144 Hz), and Apple's HUD showed the frame interval jumping between them. → [Fullscreen switches the screen to a rate the frame rate divides](../reference/frame-rate.md#fullscreen-switches-the-screen-to-a-rate-the-frame-rate-divides) - a Mac game in fullscreen holds 60 fps for 45 s to 3 minutes, then the same work takes about twice the GPU time (Tears of the Kingdom 10.5 → 19.7 ms) and the frame step falls to 30 fps. `powermetrics` shows the GPU at its lowest clock (338 MHz on an M4 Pro) while it is 72–98% busy, with thermal pressure nominal. Probes back to 60 fail, because the clock does not rise within a probe. `gamepolicyd` logs `Game mode enabled` and `Set DPS to 2`. → [macOS Game Mode holds the GPU at its lowest clock](../reference/frame-rate.md#macos-game-mode-holds-the-gpu-at-its-lowest-clock) diff --git a/docs/reference/frame-rate.md b/docs/reference/frame-rate.md index 74a2c6af69..bb165e3603 100644 --- a/docs/reference/frame-rate.md +++ b/docs/reference/frame-rate.md @@ -319,6 +319,14 @@ Frame pacing: the host's frame step, engine frame limiters, present intervals an - **Fix:** `DM_WINDOW_TOP=1` orders the window in front without activation and keeps it above other apps' windows (`front_mac.m`). `regress.py` sets it for its runs and captures. The host logs every change of the window's visibility (`front end: window covered`, `window visible`), and `regress.py` runs a route once more when its window was covered (`, window covered` in the report when the second run was covered too). A revision before this change ignores the setting, so its runs can still be covered. - **Found on:** [Breath of the Wild](../../dm/games/01007EF00011E000/README.md) (2026-10-28 and 29: every one of its 20 fps runs had 21 to 115 stalls, the runs at 60 fps 0 to 7). +### A covered window's presents follow the host's clock + +- **Symptom:** a Mac game window covered by other windows (or minimized, or on another Space) drops to about 3 fps: 333 ms frames while the player types in another app, back to normal once it is in view again. `DM_PRESENT_LINK=0` avoided it. +- **Cause:** macOS fires a covered window's display link about once a second. With the link pacing presents, every acquire waited for a tick that did not come (`link_wait` timed out after two periods) and then for its texture's present, which only the link takes, until the stall fallback dropped it (`mtl_present_wait`, `STALL_NS`); each tick that did come ended the stall, so the next frames waited again. The rescue for a silent link did not apply, since the game is usually out of focus then, and its 500 ms grace and retries would still have paced it in bursts ([Present thread rescues a silent display link](#present-thread-rescues-a-silent-display-link)). +- **Fix:** the Mac front end keeps the window's occlusion state (`front_window_covered` in `front_mac.m`, from `NSWindowDidChangeOcclusionStateNotification`, and read once a second after the window opens, for a window that opens covered). While it is covered, `link_sync` (`nvn_mtl.m`) 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 own clock at the present interval (`o->avail` in `nvnWindowAcquireTexture`). The frame step, the game's targets and the pacing in view do not change. In view again, the requested mode returns with a fresh stall clock; the link's silence while covered counts as no stall and triggers no rescue. The log says `display link: window covered, presents follow the host's clock` and `window visible, back to the display link`. Focus is a separate matter (`DM_FOCUS_KEEP`, [A regress run stops when another app takes the key window](#a-regress-run-stops-when-another-app-takes-the-key-window)): a window can be covered in focus, or in view out of focus. +- **Found on:** [Star Fox](../../dm/games/0400E88025B1A000/README.md#found-on-this-game) on the Mac (333 ms frames, seen on our fork before this change). The same once-a-second link slowed [ANIMAL WELL](../../dm/games/010020D01AD24000/README.md) (59.7 to 27.5 fps), [Breath of the Wild](../../dm/games/01007EF00011E000/README.md) (20 fps) and [Pokémon Violet](../../dm/games/01008F6008C5E000/README.md) (about 12 fps) in covered windows. Mode 0 was already known to keep presenting in a covered window ([Streets of Rage 4](../../dm/games/0100EC9010258000/README.md)'s note: `DM_PRESENT_LINK=0` keeps it presenting with the window behind others). +- **Check:** `make -C dm/gfx/tests GAME= check_t_present_rescue` (`covered`: 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). Open: no game run with the change yet (Star Fox typed over in another app, the frame rate and game speed while covered, the link back in view). + ### Virtual 60 Hz ticks on screens that are not a multiple of 60 - **Symptom:** asked for 60 Hz, the system fires the display link on every third refresh of a 165 Hz screen, 55 Hz, so games paced by it ran at 55 fps (27.5 at 30).