Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions dm/games/0400E88025B1A000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 17 additions & 3 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -3965,7 +3965,7 @@
cpu_unc_take(t);
}
static uint64_t n[2]; if (n[unc]++ < 4) MLOG("texture written by the CPU before its first use ser=%llu %ux%ux%u format %#x%s pool flags %#x: uploaded from its storage, re-uploaded when it changes (%llu so far)",
(unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, unc ? " (uncompressed, in storage the GPU has not written)" : "", t->pool->flags, (unsigned long long)n[unc]);

Check failure on line 3968 in dm/gfx/nvn/nvn_mtl.m

View workflow job for this annotation

GitHub Actions / clang-tidy (ratchet)

Access to field 'flags' results in a dereference of a null pointer (loaded from field 'pool') [clang-analyzer-core.NullDereference] (10 in this file, the baseline has 9)
return 1;
}
// nvn_ft_poll (hle_nvn.c, tex_dirty_upload): the CPU wrote the pages of a registered texture after its first use. It is taken from its storage by the rule tex_cpu_content uses at first
Expand Down Expand Up @@ -14995,7 +14995,14 @@

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.
Expand Down Expand Up @@ -15057,7 +15064,13 @@
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);
Expand Down Expand Up @@ -15308,7 +15321,8 @@
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); }
Expand Down
29 changes: 27 additions & 2 deletions dm/gfx/tests/t_present_rescue.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
} }
4 changes: 3 additions & 1 deletion dm/gfx/tests/t_present_rescue_focus.c
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
extern int test_focus;
extern uint64_t test_focus_since;
Expand All @@ -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; }
12 changes: 9 additions & 3 deletions dm/host/front_mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading
Loading