Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
make GAME="$GAME" -j3 tsan-dirty-init tsan-dirty-reuse 2>&1 | tee ../../../logs/t_dirty-tsan.log
make GAME="$GAME" -j3 present-history-tsan 2>&1 | tee ../../../logs/t_present_history-tsan.log
# Each name is a test target in dm/gfx/tests/Makefile that runs without a GPU, listed by hand: a test removed there is removed here too
tests="t_gfx_suspend t_capture t_dirty_slots t_dirty_race t_dirty_retire_race t_dirty_init t_dirty_reuse t_tex_layout t_pace t_fstep t_link_drain t_link_latch t_link_ticks t_present_history t_cpu_tex t_pkg_share t_hmap t_pso_skey t_refl t_present_rescue t_gpuprof t_gpu_timing t_sparse_layout t_timestamp t_counter_clock t_ub t_pso_pending t_vk_desc t_rec_draws t_ubc_wait t_thread_rt t_rt_comp t_rt_stall t_pend_index t_bind_stages t_prog_bind_identity t_missing_keys t_yaz0"
tests="t_gfx_suspend t_capture t_dirty_slots t_dirty_race t_dirty_retire_race t_dirty_init t_dirty_reuse t_tex_layout t_pace t_fstep t_link_drain t_link_latch t_gpu_keep t_link_ticks t_present_history t_cpu_tex t_pkg_share t_hmap t_pso_skey t_refl t_present_rescue t_gpuprof t_gpu_timing t_sparse_layout t_timestamp t_counter_clock t_ub t_pso_pending t_vk_desc t_rec_draws t_ubc_wait t_thread_rt t_rt_comp t_rt_stall t_pend_index t_bind_stages t_prog_bind_identity t_missing_keys t_yaz0"
make GAME="$GAME" -j3 $tests 2>&1 | tee ../../../logs/gfx-build.log
failed=0
for t in $tests; do
Expand Down
3 changes: 2 additions & 1 deletion dm/games/040074A01BF12000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Each step below stopped the boot before its fix:
- `oe::GetCurrentGraphicsScale`: which tier the console reports is not measured; the host answers 0 in both modes ([The graphics scale does not follow the operation mode](../../../docs/reference/services.md#the-graphics-scale-does-not-follow-the-operation-mode)). The game takes it as its display manager's scale index: its frame buffer is the display's size times {1.0, 0.8, 2/3}[tier] (`main+0x30f840`), and a renderer parameter of 0, 9 or 13 that its dynamic resolution adds to its GPU time (`hle_oe.c`). Measured docked in the mining tunnel (M6, 16 GB, a window, fresh save, a host of chain `cfcc3a82c78b` with only the answer changed): with tier 1 the frame buffer was 2304x1296 and the main passes 1676x943, GPU p50 6.8-7.2 ms; with tier 0 2880x1620 and 2095x1179, GPU p50 8.3-8.9 ms; 60 fps with both (frame p50 16.6-16.7 ms). Not measured: the busy views below, where the larger picture leaves less room.
- The 9 GB pool.
- The terrain's stores through loaded pointers keep reaching memory pools the host learns only afterwards (12 in a 17,700-frame session); each such miss is checked against the uniform shadows that relied on it, and none so far covered a written page ([Global memory at an address loaded from memory](../../../docs/reference/shaders.md#global-memory-at-an-address-loaded-from-memory), Open).
- **Frame rate in busy scenes:** the game picks its own present interval: 3 (20 fps) in its cutscenes, and 1 ↔ 2 (60 ↔ 30 fps) by its frame-rate controller (`main+0x204a18`; settings at `0x703f72b200`: interval 1 to 3, margin -0.2, 2 late frames to go up, 6 to come back). It measures each frame from its start (`+0xf0`) to the later of its submission and the GPU's end (`+0x128`, ticks) and wants interval 2 from 1.2 vsyncs (20 ms). On the Mac (M4 Pro) that measure sits at 17 ms a frame (p90 24): the host's translation (CPU, 5-9 ms) before the GPU's 10-13 ms. The upscaler's HMMA through `simdgroup_matrix` ([Ampere HMMA through simdgroup_matrix](../../../docs/reference/shaders.md#ampere-hmma-through-simdgroup_matrix)) cut the switches by about a fifth; the geometry runs' dependency check, quadratic in the run's length ([Geometry dependency checks skip read-only pairs](../../../docs/reference/shaders.md#geometry-dependency-checks-skip-read-only-pairs)), was about 8 % of the submitting thread, and making it linear cut them from 30 to 12 on the same scripted route. Entering a level stalled for 1.3 s on GPU drains, one or two per geometry draw ([CPU reads after a drain wait only for later writes](../../../docs/reference/rendering.md#cpu-reads-after-a-drain-wait-only-for-later-writes)); one CPU-bound build frame of 195-280 ms remains. Measured without `sample` attached: it suspends the process and makes the submitting thread look stalled. Its geometry draws are mesh draws ([Mesh geometry groups sized to the vertex output](../../../docs/reference/shaders.md#mesh-geometry-groups-sized-to-the-vertex-output)): translation 9.9 → 7.2 ms a frame, GPU p50 11.8 → 9.0 ms on the scripted route. Open: the rest of the translation time in the busiest views (argument encoding, render pass setup), where translation and the GPU together still miss a vsync now and then.
- **Frame rate in busy scenes:** the game picks its own present interval: 3 (20 fps) in its cutscenes, and 1 ↔ 2 (60 ↔ 30 fps) by its frame-rate controller (`main+0x204a18`; settings at `0x703f72b200`: interval 1 to 3, margin -0.2, 2 late frames to go up, 6 to come back). It measures each frame from its start (`+0xf0`) to the later of its submission and the GPU's end (`+0x128`, ticks) and wants interval 2 from 1.2 vsyncs (20 ms). On the Mac (M4 Pro) that measure sits at 17 ms a frame (p90 24): the host's translation (CPU, 5-9 ms) before the GPU's 10-13 ms. The upscaler's HMMA through `simdgroup_matrix` ([Ampere HMMA through simdgroup_matrix](../../../docs/reference/shaders.md#ampere-hmma-through-simdgroup_matrix)) cut the switches by about a fifth; the geometry runs' dependency check, quadratic in the run's length ([Geometry dependency checks skip read-only pairs](../../../docs/reference/shaders.md#geometry-dependency-checks-skip-read-only-pairs)), was about 8 % of the submitting thread, and making it linear cut them from 30 to 12 on the same scripted route. Entering a level stalled for 1.3 s on GPU drains, one or two per geometry draw ([CPU reads after a drain wait only for later writes](../../../docs/reference/rendering.md#cpu-reads-after-a-drain-wait-only-for-later-writes)); one CPU-bound build frame of 195-280 ms remains. Measured without `sample` attached: it suspends the process and makes the submitting thread look stalled. Its geometry draws are mesh draws ([Mesh geometry groups sized to the vertex output](../../../docs/reference/shaders.md#mesh-geometry-groups-sized-to-the-vertex-output)): translation 9.9 → 7.2 ms a frame, GPU p50 11.8 → 9.0 ms on the scripted route. Open: the rest of the translation time in the busiest views (argument encoding, render pass setup), where translation and the GPU together still miss a vsync now and then. Once at interval 2 the game stayed there for tens of seconds on main (2026-11-30, `e693bb89`), because macOS lowers the GPU's clock at 30 fps: about 5,000 draws took 12 ms of GPU at 60 fps and 20.4-22.4 ms at 30, over the controller's 20 ms, until a lighter view let it back to 1 ([A game's own 30 fps fallback held by the GPU clock its longer hold lowered](../../../docs/reference/frame-rate.md#a-games-own-30-fps-fallback-held-by-the-gpu-clock-its-longer-hold-lowered); the GPU clock keeper: in 4 sessions with it on an M3 Max MacBook Pro, 23 probes, all won within 0.3-0.7 s, no window at 30 fps; without it, two windows at 30 fps with GPU p50 24.6-25.3 ms).
- **Submission-copy qualification ([PR #2677](https://github.com/seedds/nxnative/pull/2677), 2026-11-17, open):** against main `d6585a2d`, on the M4 Pro (14 CPU cores, 20 GPU cores, 48 GB, macOS 27.2.1), the mining-entrance overlook stays capped: 59.6 fps, frame p99 16.88 ms, copy/replay CPU 7.75 ms a frame and GPU p50 7.59 ms, with no pipeline or library creation in the 100-130 s window. Headless, handheld mode and MetalFX, the packed v458752 build's derived shader profile, identical save and archive copies, no movement or stack sampling. This is not a CPU-limited route. The planned main/submission-copy-only ABBA comparison was stopped when the first candidate process created four pipeline objects in its window; a single repeat from the combined warmed archive, at 150-180 s, stopped on main creating one. Those comparisons are excluded: no valid submission-copy performance result was obtained for this main revision, and the change should remain held. The separate sampler was not applied. Baseline limitation: before the window, main reports 16 zero-GPU-address diagnostics for geometry shader `5d537f7630ec3671_bl3`, stage 2 argument block slots 2/3/4/7; the scene renders. Those four unused mesh scratch bindings are now explained by [the mesh address diagnostic correction](../../../docs/reference/rendering.md#mesh-geometry-reports-unused-compute-scratch-as-zero-gpu-addresses); the submission-copy performance result remains open.
- **Frame drops in a 63-minute Mac session (M5 Pro, 2026-11-08):** 40 drops under 54 fps, 126 s in all, every one in scenes of more than 12,000 draws, with the GPU at its top clock, no thermal or droop limit and the hold at interval 1 (the controller cut of [PR #1765](https://github.com/seedds/nxnative/pull/1765)).
- The worst (14-23 fps for 26 s, and around 1,400 s): 1,700-2,800 geometry mesh draws a frame, none in runs, refused because their fragment shader `7f7781d3621f0ce6` (`SilverStarMaterial`: two uniform buffers, the support buffer, a discard) has resources. Runs now take fragment shaders that write no memory ([Mesh geometry draws gathered into one dispatch](../../../docs/reference/shaders.md#mesh-geometry-draws-gathered-into-one-dispatch)). With it and the `b64d15d642f9d2d6` pair below, a 113,000-frame session held 59.4-60 fps in every 1,500-frame window (M5 Pro, 2026-11-09, [PR #2641](https://github.com/seedds/nxnative/pull/2641)), and panning the map of Sublayer 900 went from 16-26 to 43-48 fps (submission 30-45 → 11-12 ms) with the map's pair. Not measured here: on the M4 Pro (2026-11-15) the opening, the mining tunnel and the area after it draw no geometry draw whose fragment shader has resources (main's rule refused none), so both rules give the same frames there (submission p50 6.2-6.4 ms, GPU 11.8-12.2 ms, every mesh draw in one run a frame). Open: a session in those areas with the `geometry mesh draws` line (draws in runs, runs ended by `a fragment binding change`), to see that the draws join runs, and whether a pass with colour attached keeps some single.
Expand All @@ -111,6 +111,7 @@ Each step below stopped the boot before its fix:

## Found on this game

- [A game's own 30 fps fallback held by the GPU clock its longer hold lowered](../../../docs/reference/frame-rate.md#a-games-own-30-fps-fallback-held-by-the-gpu-clock-its-longer-hold-lowered): its controller's interval 2 kept for tens of seconds, GPU p50 12 ms at 60 fps against 20.4-22.4 ms at 30 for the same draws; with the keeper every drop went back to 60 within 0.7 s (23 of 23, M3 Max MacBook Pro).
- [The prebuild dropped a finished period behind one slow compile](../../../docs/reference/rendering.md#the-prebuild-dropped-a-finished-period-behind-one-slow-compile): resolved, a prebuild lost a finished period behind a slow compute compile (and with the timer fix in [PR #3491](https://github.com/seedds/nxnative/pull/3491), first-start prepare/first-frame dropped from 123.66 → 1.04 s / 145.8 → 5.4 s on M3 Max against `25c5566a`).
- [A replay without the live run's Switch 2 state](../../../docs/reference/nvn-objects.md#a-replay-without-the-live-runs-switch-2-state) and [A texture the GPU's copies filled that the capture did not read](../../../docs/reference/nvn-objects.md#a-texture-the-gpus-copies-filled-that-the-capture-did-not-read): Switch 2 capture state (`sdk_ounce`, `nvn_timestamp_ns`) and ASTC readback ([PR #1236](https://github.com/seedds/nxnative/pull/1236)).
- [HDR colors more saturated than Off, and Off after HDR](../../../docs/reference/rendering.md#hdr-colors-more-saturated-than-off-and-off-after-hdr): its mode menu's PQ window has colors outside BT.709 (77 % of its lit pixels moved by a ΔE_ITP above 10 when clipped at 0), which Game's HDR clipped. Resolved.
Expand Down
71 changes: 71 additions & 0 deletions dm/gfx/nvn/nvn_gpu_keep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// The GPU clock keeper's decisions (nvn_mtl.m gpu_keep_main runs them; t_gpu_keep tests them). Pure: no Metal, the caller passes the clock.
//
// macOS sets the GPU's clock from how busy the GPU was. When a game holds its frames for two vsyncs instead of one, the GPU idles half of each frame and the clock falls, so the
// same frame takes longer on the GPU: Donkey Kong Bananza at about 5,000 draws took 12 ms of GPU a frame at 60 fps and 20.4-22.4 ms at 30 fps (M-series Mac, main at e693bb89).
// A frame-rate controller that steps back to the shorter hold only once a frame fits it (Bananza's wants its frame under 1.2 vsyncs) then never does: at the longer hold the
// frames stay slow because the hold is longer. The Switch's GPU clock does not follow the load, so the game's rule is right there and only wrong here.
//
// The keeper breaks that loop with a probe: when the frames are held longer than the shortest hold seen and a frame's GPU time is a large part of that shortest period, it keeps
// the GPU busy for up to GPU_KEEP_PROBE_NS (the caller spins a small kernel while the game's queue is idle), so the clock rises and the game can see its frames fit. It stops as
// soon as the hold shortens (won), and gives up when it does not (lost): the scene really needs the longer hold, or something other than the GPU holds it. Each loss in a row
// doubles the wait before the next try, and after GPU_KEEP_TRIES losses the keeper waits until the hold shortens by itself. At the shortest hold it never runs, so a game at its
// full frame rate, a game whose only hold is the longer one, and a game whose GPU is light at the longer hold cost nothing. The caller also stops it (allowed = 0) when the window
// is not focused or is covered, while nothing is presented, while presents are discarded for a suspend, at a serious thermal state and in Low Power Mode.
#pragma once
#include <stdint.h>

#define GPU_KEEP_LONGER 1.5 // a present period above this many times the shortest one seen is a longer hold (2 vsyncs against 1, 3 against 2)
#define GPU_KEEP_FIT 0.6 // a frame's GPU time above this share of the shortest period: the GPU's clock may be what keeps the longer hold
#define GPU_KEEP_PROBE_NS 4000000000ull // macOS raises the clock over seconds; Bananza's controller then needs 6 frames that fit
#define GPU_KEEP_COOL_NS 8000000000ull // the wait after the first loss, doubled after each further one
#define GPU_KEEP_TRIES 4 // losses in a row before waiting for the hold to shorten by itself

enum { GPU_KEEP_IDLE, GPU_KEEP_PROBE, GPU_KEEP_COOL, GPU_KEEP_DONE };
enum { GPU_KEEP_EV_NONE, GPU_KEEP_EV_START, GPU_KEEP_EV_WON, GPU_KEEP_EV_LOST, GPU_KEEP_EV_STOPPED };

typedef struct {
double p_min; // the shortest present period seen, in seconds (0: none yet)
double period; // the latest present's period, in seconds
int state; // GPU_KEEP_*
int losses; // probes lost in a row in this stretch of longer holds
uint64_t t_state; // when state was entered (the caller's clock, ns)
uint64_t cool_ns; // the current wait in GPU_KEEP_COOL
uint32_t probes, won, lost;
} nvn_gpu_keep;

// Each present's period (the minimum time it is shown; 0 = unlimited, not a hold)
static inline void nvn_gpu_keep_period(nvn_gpu_keep *k, double period) {
if (!(period > 0)) return;
k->period = period;
if (!(k->p_min > 0) || period < k->p_min) k->p_min = period;
}
static inline int nvn_gpu_keep_longer(const nvn_gpu_keep *k) { return k->p_min > 0 && k->period > k->p_min * GPU_KEEP_LONGER; }
static inline int nvn_gpu_keep_spin(const nvn_gpu_keep *k) { return k->state == GPU_KEEP_PROBE; }

// One step: now (ns), gpu_ms a frame's recent GPU time (< 0: not known), allowed (see above). Returns the event, GPU_KEEP_EV_*.
static inline int nvn_gpu_keep_step(nvn_gpu_keep *k, uint64_t now, double gpu_ms, int allowed) {
int longer = nvn_gpu_keep_longer(k);
if (!longer) { // back at the shortest hold: a probe won, and the next stretch of longer holds starts with no losses
int ev = k->state == GPU_KEEP_PROBE ? GPU_KEEP_EV_WON : GPU_KEEP_EV_NONE;
if (ev) k->won++;
k->state = GPU_KEEP_IDLE; k->losses = 0; k->cool_ns = 0;
return ev;
}
switch (k->state) {
case GPU_KEEP_IDLE:
if (allowed && gpu_ms > GPU_KEEP_FIT * k->p_min * 1e3) { k->state = GPU_KEEP_PROBE; k->t_state = now; k->probes++; return GPU_KEEP_EV_START; }
return GPU_KEEP_EV_NONE;
case GPU_KEEP_PROBE:
if (!allowed) { k->state = GPU_KEEP_IDLE; return GPU_KEEP_EV_STOPPED; } // not a loss: the probe did not get its time
if (now - k->t_state < GPU_KEEP_PROBE_NS) return GPU_KEEP_EV_NONE;
k->lost++; k->losses++; k->t_state = now;
k->cool_ns = k->cool_ns ? k->cool_ns * 2 : GPU_KEEP_COOL_NS;
k->state = k->losses >= GPU_KEEP_TRIES ? GPU_KEEP_DONE : GPU_KEEP_COOL;
return GPU_KEEP_EV_LOST;
case GPU_KEEP_COOL:
if (now - k->t_state >= k->cool_ns) k->state = GPU_KEEP_IDLE;
return GPU_KEEP_EV_NONE;
default: // GPU_KEEP_DONE: until the hold shortens
return GPU_KEEP_EV_NONE;
}
}
Loading
Loading