diff --git a/dm/games/0400C3F00006E000/README.md b/dm/games/0400C3F00006E000/README.md index 330be04071..e88182d7c7 100644 --- a/dm/games/0400C3F00006E000/README.md +++ b/dm/games/0400C3F00006E000/README.md @@ -94,6 +94,7 @@ Update 1.8.0 (v917504, SDK 21.4.0) is not on this Mac. Pull request #1116 ported ## Found on this game +- [GPU timestamps per submission segment](../../../docs/reference/frame-rate.md#gpu-timestamps-per-submission-segment): with `DM_NVN_GPU_TS=segment` (the profile has no `gpu_timestamps` key), distant mountains and bridges flickered in free roam and on the title screen while the GPU's idle gaps between segments counted as GPU time; without them, no flicker (M5) - [A NUP with complete standalone updates stops at "no base game"](../../../docs/reference/packing.md#a-nup-with-complete-standalone-updates-stops-at-no-base-game) (packing reproduced with the base dump; 1.8.0 gameplay remains untested) - [Large sparse textures as Metal placement sparse textures](../../../docs/reference/nvn-objects.md#large-sparse-textures-as-metal-placement-sparse-textures) - [Physical pages mapped at several places of a placement sparse texture](../../../docs/reference/nvn-objects.md#physical-pages-mapped-at-several-places-of-a-placement-sparse-texture) diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index f73da6dac7..927ba929f8 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -1506,7 +1506,8 @@ static void post(post_op o) { // so a game that waits for the fence reads one submission's reports together; a segment whose feedback has not arrived by then takes the last segment's times #define SEG_CLK_N 1024 struct seg_clk { _Atomic int refs; _Atomic uint64_t t[SEG_CLK_N][2]; }; // per submission (g_sclk, allocated at its first cut): guest ns of each cut segment's GPU start / end (0 = no feedback yet) -static int seg_ts(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_GPU_TS"); v = e && *e ? !strcmp(e, "segment") : DM_GAME_GPU_TS_SEGMENT; MLOG("counter timestamps: %s (profile key gpu_timestamps; DM_NVN_GPU_TS=segment / submission)", v ? "GPU start / end time of their segment" : "one time for the whole submission"); } return v; } +static int g_seg_gaps; // DM_NVN_GPU_TS=segment_gaps: segment times keep the GPU's idle gaps between segments (see seg_gap_before) +static int seg_ts(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_GPU_TS"); g_seg_gaps = e && !strcmp(e, "segment_gaps"); v = e && *e ? !strcmp(e, "segment") || g_seg_gaps : DM_GAME_GPU_TS_SEGMENT; MLOG("counter timestamps: %s (profile key gpu_timestamps; DM_NVN_GPU_TS=segment / submission)", v ? "GPU start / end time of their segment" : "one time for the whole submission"); } return v; } static void seg_clk_put(seg_clk *c) { if (c && atomic_fetch_sub(&c->refs, 1) == 1) free(c); } static uint64_t gpu_s_guest_ns(double s) { // a feedback GPU time (seconds on the mach absolute time base) on the guest clock; 0 when unknown static mach_timebase_info_data_t tb; if (!tb.denom) mach_timebase_info(&tb); @@ -1514,10 +1515,40 @@ static void post(post_op o) { if (s <= 0 || !g || at > now || now - at > g) return 0; return g - (now - at); } +// The GPU idles between segments while the host translates and commits the next one; a Switch GPU runs a submission's commands without such gaps. A game that times its GPU work as +// the difference of two reports (agl's dynamic resolution: Mario Kart World, Donkey Kong Bananza, Splatoon Raiders) read the gaps as GPU work, saw its budget exceeded at times and stepped its +// resolution down and up again from frame to frame: thin distant geometry flickered (Mario Kart World's mountains and bridges, on the title screen already; GitHub issue #1290 for Splatoon Raiders). +// So each segment's times move back by the idle gaps before it in the submission (a segment with no feedback yet ends the sum); DM_NVN_GPU_TS=segment_gaps keeps them +static uint64_t seg_gap_before(seg_clk *sc, uint32_t seg, uint32_t last, uint64_t c0) { + uint64_t gap = 0, prev_end = 0; + for (uint32_t i = 0; i <= seg && i <= last && i < SEG_CLK_N; i++) { + uint64_t st = i < last ? atomic_load(&sc->t[i][0]) : c0, en = i < last ? atomic_load(&sc->t[i][1]) : 0; + if (!st) break; + if (prev_end && st > prev_end) gap += st - prev_end; + prev_end = en; + } + return gap; +} +// DM_NVN_GPU_TS_PAD= (1.0 to 2.0, default 1): GPU headroom for a game's dynamic resolution. A submission's report times are stretched by the factor from its first segment's +// start, so the game measures its GPU work that much longer and lowers its resolution until the padded time fits its budget: the real GPU time then stays under the frame by about +// 1 - 1/factor (1.10: about 9 %), room for the host's own GPU work and for spikes. Splatoon Raiders, once the gap-free timestamps let it see the real GPU time, filled the frame +// (GPU median 18.7 ms against 16.7) and dropped frames. A tuning switch, off by default +static double ts_pad(void) { + static double v = -1; + if (v < 0) { const char *e = getenv("DM_NVN_GPU_TS_PAD"); double f = e && *e ? atof(e) : 1.0; v = f >= 1.0 && f <= 2.0 ? f : 1.0; + if (v != 1.0) MLOG("counter timestamps: GPU times reported %.2fx (DM_NVN_GPU_TS_PAD): headroom for the game's dynamic resolution", v); } + return v; +} static uint64_t rep_ticks(const post_op *o, seg_clk *sc, uint32_t last, uint64_t c0, uint64_t c1) { // the time a counter report records (614.4 MHz); c0 / c1: the last segment's GPU start / end on the guest clock int top = o->type == 0x10; uint64_t ns = 0; if (sc && o->seg < last && o->seg < SEG_CLK_N) ns = atomic_load(&sc->t[o->seg][!top]); if (!ns) ns = top ? c0 : c1; + if (ns && sc && !g_seg_gaps) { uint64_t gap = seg_gap_before(sc, o->seg < last ? o->seg : last, last, c0); if (gap < ns) ns -= gap; } + double pad = ts_pad(); + if (ns && pad != 1.0) { // stretched from the submission's start: its first segment's GPU start (gaps come only after it), or the last segment's when it has one + uint64_t base = sc && last ? atomic_load(&sc->t[0][0]) : c0; + if (base && ns > base) ns = base + (uint64_t)((double)(ns - base) * pad); + } return ns ? nvn_timestamp_from_ns(ns) : gpu_ticks(); } diff --git a/docs/env_switches.tsv b/docs/env_switches.tsv index 598f89cc20..e1b8e57c2d 100644 --- a/docs/env_switches.tsv +++ b/docs/env_switches.tsv @@ -152,7 +152,8 @@ DM_NVN_GEO_LEAN switch `0` draws passthrough geometry shaders through their full DM_NVN_GEO_MESH switch `0` draws passthrough geometry pairs that have a mesh library (`geo_mesh.tsv`) through the compute path; by default they are mesh draws in the render pass (`geo_mesh_draw`) DM_NVN_GEO_MESH_RUN switch `0` dispatches every mesh geometry draw on its own; by default consecutive mesh draws of one pair whose result does not depend on their order are drawn as one dispatch (`geo_mesh_take`) DM_NVN_GPUPROF diagnostic `=`, `-` or `every:`: logs the GPU start and duration of every encoder of those frames (`[gpuprof]` lines from Metal 4 counter heap timestamps) -DM_NVN_GPU_TS switch `segment` or `submission`: what timestamp reports record (GPU start / end of the segment holding each report, or one time for the whole submission), instead of the profile's `gpu_timestamps` +DM_NVN_GPU_TS switch `segment`, `segment_gaps` or `submission`: what timestamp reports record (GPU start / end of the segment holding each report, without the GPU's idle time between segments; the same with that idle time; or one time for the whole submission), instead of the profile's `gpu_timestamps` +DM_NVN_GPU_TS_PAD switch `=` (1.0 to 2.0, default 1) stretches each submission's reported GPU times by the factor, GPU headroom for a game's dynamic resolution (`nvn_mtl.m` `ts_pad`) DM_NVN_HILITE diagnostic `=,…`: those draws use a solid colour instead (magenta, green, cyan, yellow, blue, white in order), to tell wrong geometry from wrong shading DM_NVN_INDLOG diagnostic `=`: the first n indirect draws (and those of pass-dumped frames) log their arguments at encode and as the GPU left them once the submission completed, with the first indices DM_NVN_IXCLOG diagnostic `=` logs the first n indirect draw and dispatch conversions (record to arguments) once the GPU has run them diff --git a/docs/reference/frame-rate.md b/docs/reference/frame-rate.md index 1e605836bb..6a8cb610cf 100644 --- a/docs/reference/frame-rate.md +++ b/docs/reference/frame-rate.md @@ -107,7 +107,8 @@ Frame pacing: the host's frame step, engine frame limiters, present intervals an - **Symptom:** slow motion when the GPU is the bottleneck: a game's interval controller that times the GPU never steps up (GitHub issue #9). - **Cause:** a game that times its GPU work as the difference of two timestamp reports in one submission (`TIMESTAMP_TOP` at the start of the frame's command buffer, `TIMESTAMP` at its end) read 0–6 ticks, because the host wrote all the reports of a submission with one time in its completion handler. - **Fix:** the profile key `gpu_timestamps segment` makes each report take the GPU start (`TIMESTAMP_TOP`) or end (other types) of the `sub_cut` segment ([Submissions handed to the GPU in segments](#submissions-handed-to-the-gpu-in-segments)) it was encoded in (`rep_ticks` in `nvn_mtl.m`: MTL4CommitFeedback times converted to the guest clock, written in the last segment's handler as before). `DM_NVN_GPU_TS=submission` gives the old timestamps. -- **Found on:** [The Legend of Zelda: Breath of the Wild](../../dm/games/01007EF00011E000/README.md) (legacy frame-step method): BotW's frame has 5 reports in one submission (`main+0xe94584`, `main+0xe94664`); with two more cuts the controller always takes the GPU path, and the GPU time now reads the frame's GPU span, gaps where the GPU waits for the host's translation included. Measured on the Mac with two BotW runs sharing the GPU under a heavy CPU load (load average about 75, a shader rebuild running): GPU time p50 16–23 ms; the fixed run stayed at interval 2 (3 while the GPU time passed 1.9 vsyncs, 1 for under 2% of the frames) at 0.93–0.98 of real time, while a run with `DM_NVN_GPU_TS=submission` spent 10–70% of its frames at interval 1 and ran at 0.78–0.95. The same GPU time drives the game's dynamic resolution. +- **Found on:** [The Legend of Zelda: Breath of the Wild](../../dm/games/01007EF00011E000/README.md) (legacy frame-step method): BotW's frame has 5 reports in one submission (`main+0xe94584`, `main+0xe94664`); with two more cuts the controller always takes the GPU path, and the GPU time then read the frame's GPU span, gaps where the GPU waits for the host's translation included (taken out since: below). Measured on the Mac with two BotW runs sharing the GPU under a heavy CPU load (load average about 75, a shader rebuild running): GPU time p50 16–23 ms; the fixed run stayed at interval 2 (3 while the GPU time passed 1.9 vsyncs, 1 for under 2% of the frames) at 0.93–0.98 of real time, while a run with `DM_NVN_GPU_TS=submission` spent 10–70% of its frames at interval 1 and ran at 0.78–0.95. The same GPU time drives the game's dynamic resolution. +- **Gaps between segments:** a span of reports read the GPU's idle time between segments too, while the host translated the next one: about 5–6 ms of a Mario Kart World frame on a MacBook Air M5. agl's dynamic resolution (Donkey Kong Bananza, Mario Kart World, Splatoon Raiders: [issue #1290](https://github.com/seedds/nxnative/issues/1290)) then read the GPU over budget when it was not, stepped down, read it under budget and stepped back up: the render size swung from frame to frame, and fine distant detail (Mario Kart World's mountains and bridges in free roam, and its title screen) flickered. A report now leaves out the GPU's idle time before its segment: `seg_gap_before` sums the gaps between the end of each earlier segment and the start of the next, and `rep_ticks` subtracts them from the segment's time, so a span of reports reads the GPU's work alone. `DM_NVN_GPU_TS=segment_gaps` keeps the gaps. Checked on the Mac (M5, macOS 27.0.1) by play: Mario Kart World with `DM_NVN_GPU_TS=segment` flickered on its title screen and in free roam before, and no longer; Donkey Kong Bananza (profile `gpu_timestamps segment`) showed no flicker or slowdown. Not checked: Breath of the Wild's interval controller, which read the gaps as GPU time: it may step to interval 1 more often. ### Raw NVN timestamp units