From d9f73491099e806295df7d6968e2f30484e5598f Mon Sep 17 00:00:00 2001 From: seedds Date: Thu, 5 Nov 2026 22:20:54 +0100 Subject: [PATCH] Donkey Kong Bananza runs its own VRR mode on a 120 Hz screen: the rate vi::SetLayerExtendedSwapInterval asks for paces presentation, so frames just past a vsync show at 40-58 fps instead of 30 The game has two frame-rate controllers, chosen per frame by what vi::QueryDisplayVrrSupported answered (main+0x2036b0). The fixed-refresh one (main+0x204a18) has only whole present intervals, so a frame a little over 16.7 ms took the game to 30 fps. The VRR one (main+0x204640) asks for 40 to 60 fps in steps through SetLayerExtendedSwapInterval(layer, 1, fps) and leaves VRR for interval 2 below 40. The host answered that the display has no VRR, so only the first ever ran. A profile key, vrr on, answers yes on the Mac when the game's window is on a screen of 120 Hz or more (front_screen_max_fps, which follows the window to another screen; hle_vi.c). Docked too, a deliberate difference from the Switch 2, whose VRR is handheld only: docked is what the Mac shows by default, and the game's VRR controller does not look at the operation mode. iOS never answers VRR, as it is untested there. The rate replaces the present interval the game leaves set (hle_nvn.c nvn_vrr_set): a period of 1 / fps, no 30 Hz link, and the acquire waits the rate's ticks of a 120 Hz display link with the fractional tick carried, as display mode does above 60 Hz. SetDisplayVrrCapability(false) and ClearLayerExtendedSwapInterval clear it; a frame step page or 120 fps ignore it. A key and not a rule for every game: a game's VRR mode must keep its speed between whole vsyncs, and only this one has been played that way. DM_VRR=0 / 1 overrides it on the Mac. Tested on an M1 Pro's built-in ProMotion screen, in a Mac app packed at 975bb4c with this change on top: the game asked for 40 to 60 fps in 2 fps steps and ran 47.6-52.5 fps a 300-frame window in the first two minutes, with no shader failures and no red lines. Played by hand, the picture and the speed looked normal. That app's host was this patch on 975bb4c, the commit its shaders were built for; this commit's own host was not run in the game. Not measured: the speed against interval pacing over the same scripted inputs, the same spots without the key, an external screen. Checked: t_nvn (245 checks, 10 of them new for VRR), the rest of the graphics tests and the host tests, the docs, env switch, game profile, settings, launcher and player env tool tests, the Python lint ratchet (0 new findings), and both changed C files compile for iOS. clang-tidy was not run locally. regress.py check was not run: the change acts only with the key, which only this profile sets, and this game has no smoke route. Co-authored-by: Mikolaj Wojcik --- dm/games/040074A01BF12000/README.md | 6 ++-- dm/games/040074A01BF12000/game.tsv | 4 +++ dm/games/README.md | 1 + dm/games/game.py | 3 ++ dm/gfx/hle_vi.c | 27 +++++++++++----- dm/gfx/nvn/hle_nvn.c | 48 +++++++++++++++++++++++++++-- dm/gfx/tests/t_nvn.c | 22 +++++++++++++ docs/agents/symptoms.md | 1 + docs/env_switches.tsv | 1 + docs/reference/frame-rate.md | 9 ++++++ 10 files changed, 109 insertions(+), 13 deletions(-) diff --git a/dm/games/040074A01BF12000/README.md b/dm/games/040074A01BF12000/README.md index d585bbbadb..94af0b91a0 100644 --- a/dm/games/040074A01BF12000/README.md +++ b/dm/games/040074A01BF12000/README.md @@ -1,6 +1,6 @@ # Donkey Kong Bananza (`040074A01BF12000`) -A Switch 2 game: update v458752 over the base game, SDK 20.5.19 built for the Switch 2 (`sdk` names its spec `Ounce`), NVN2. Nintendo EPD's engine (sead / agl / gsys, Phive), modules `main` + `sdk`. **Plays on the Mac** (M4 Pro): three play sessions of about 5 minutes each and scripted runs of up to 15,900 frames, through the opening, the mining tunnel and the areas after it, with the guide movies in their panel, at 60 fps; in busy scenes the game drops itself to 30 fps for a few seconds ([Open](#open)). The first start with no pipeline archive builds the pipelines for about 4 minutes, once. Nothing past the first level's opening areas is tested; iOS is untested. +A Switch 2 game: update v458752 over the base game, SDK 20.5.19 built for the Switch 2 (`sdk` names its spec `Ounce`), NVN2. Nintendo EPD's engine (sead / agl / gsys, Phive), modules `main` + `sdk`. **Plays on the Mac** (M4 Pro): three play sessions of about 5 minutes each and scripted runs of up to 15,900 frames, through the opening, the mining tunnel and the areas after it, with the guide movies in their panel, at 60 fps; in busy scenes the game drops itself to 30 fps for a few seconds ([Open](#open)), or with `vrr on` on a 120 Hz screen to 40-58 fps in its VRR mode. The first start with no pipeline archive builds the pipelines for about 4 minutes, once. Nothing past the first level's opening areas is tested; iOS is untested. ## What it needs @@ -10,6 +10,7 @@ A Switch 2 game: update v458752 over the base game, SDK 20.5.19 built for the Sw | `app_pool 0x240000000`, `mem_reserve 0x270000000` | `game.tsv` | the Switch 2 leaves games 9 GB of its 12 GB (not measured here); with a Switch 1 pool (3.2 GB) the game's heaps did not fit: `AudioSystemHeap` creation returned null (`main+0x564b48`) | | `romfs_marker System/Resource/ResourceSizeTable.Product.rsizetable.zs` | `game.tsv` | a file every version has | | `render_cond on` | `game.tsv` | agl's render buffer binding wraps its clears in render conditions (`SetRenderEnableConditional` at `main+0x12798c`, before the `ClearColor` at `main+0x1279d4`); drawn unconditionally, a clear-only rebinding of the composite target right after the composite draw wiped it before the upscaler read it, and every gameplay frame was black | +| `vrr on` | `game.tsv` | the game's VRR mode (`main+0x204640`, chosen per frame at `main+0x2036b0` when `vi::QueryDisplayVrrSupported` says yes) asks for 40-60 fps through `vi::SetLayerExtendedSwapInterval(layer, 1, fps)`, and the host paces frames at that rate on the 120 Hz display link; without the key the fixed-refresh controller (`main+0x204a18`) can only pick 60, 30 or 20 fps. Below 40 the game leaves VRR and presents at interval 2. Answered on the Mac when the window's screen runs at 120 Hz or more, docked too (the Switch 2 has VRR handheld only); never on iOS, where it is untested; `DM_VRR=0` keeps the whole intervals | ## Files @@ -60,12 +61,13 @@ Each step below stopped the boot before its fix: - `oe::GetCurrentGraphicsScale`: which tier the console reports in which mode is not measured (the host answers 0 handheld, 1 console). The game uses it as a quality tier, not for correctness: a byte of its graphics settings and a renderer parameter of 0, 9 or 13 (`hle_oe.c`). - 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. With `vrr on` the VRR controller replaces this one on a 120 Hz screen ([A Switch 2 game's VRR rate paces presentation](../../../docs/reference/frame-rate.md#a-switch-2-games-vrr-rate-paces-presentation)): its settings (the 40 fps floor, 2 fps steps) are read from what it asked for, not from its settings object, and its speed at those rates was judged by playing, not measured. - The NVN2 device values the driver reads from the GPU at run time (17 items: scratch scale factors, L2 size, texture layers and sizes, linear stride alignment, pool page size, …) are still the Switch 1 ones; the constant ones are NVN2's ([The Switch 2's device values](../../../docs/reference/nvn-objects.md#the-switch-2s-device-values)). - iOS untested. ## Found on this game +- [A Switch 2 game's VRR rate paces presentation](../../../docs/reference/frame-rate.md#a-switch-2-games-vrr-rate-paces-presentation): 60 → 30 fps on frames just past a vsync; its own VRR mode at 40-60 fps with `vrr on`. - [A prebuild pool sized from the Super cores](../../../docs/reference/shaders.md#a-prebuild-pool-sized-from-the-super-cores): the pipeline prebuild of its 21,225 shader pairs, M5 Pro, 393-452 s on 4 workers against 187-272 s on Metal's 14 (resolved). - [A depth clear over the whole target with a partial stencil mask](../../../docs/reference/rendering.md#a-depth-clear-over-the-whole-target-with-a-partial-stencil-mask): 22.8 MB of depth and stencil loaded a frame for a stencil mask of 1 (resolved). - [No Game language row in an app packed from a NUP or a dump](../../../docs/reference/packing.md#no-game-language-row-in-an-app-packed-from-a-nup-or-a-dump): reported by testers; this folder's dump has no control data. diff --git a/dm/games/040074A01BF12000/game.tsv b/dm/games/040074A01BF12000/game.tsv index 93896a9889..24daef0d4e 100644 --- a/dm/games/040074A01BF12000/game.tsv +++ b/dm/games/040074A01BF12000/game.tsv @@ -44,3 +44,7 @@ render_cond on # Dynamic resolution and the present interval read the GPU time between the frame's timestamp reports (about 26 a frame): with one time for the whole submission they read about 0, # the game kept 2304x1296 at 46 ms of GPU a frame and fell to interval 3 (15 fps before the odd-interval fix of the display link, 20 after). Each report takes its segment's GPU start / end: in the slow areas it steps down to 1152x648 with its upscaler and holds 30 fps gpu_timestamps segment +# The game's VRR mode (main+0x204640, chosen per frame at main+0x2036b0 when vi::QueryDisplayVrrSupported says yes): it asks for 40-60 fps through vi::SetLayerExtendedSwapInterval(layer, 1, fps) +# and below 40 turns VRR off and presents at interval 2. Without the key the host answers no VRR, and the fixed-refresh controller (main+0x204a18) can only pick 60, 30 or 20 fps. +# Answered on the Mac when the window's screen runs at 120 Hz or more (ProMotion), docked too (the Switch 2 has VRR handheld only); never on iOS (untested). DM_VRR=0 turns it off, DM_VRR=1 on for any screen +vrr on diff --git a/dm/games/README.md b/dm/games/README.md index 2d187dc39c..3516b78468 100644 --- a/dm/games/README.md +++ b/dm/games/README.md @@ -224,6 +224,7 @@ One row per profile; the notes are each profile's `README.md`. | `fps_target` | Frame rate cap of the rewriter's frame step, for builds that have one (`FRAME_STEP` in the profile's `patches.py`: Tears of the Kingdom 1.4.3, Breath of the Wild 1.9.0, Hyrule Warriors: Age of Calamity 1.3.0, Paper Mario: The Thousand-Year Door 1.0.1, ASTRAL CHAIN 1.0.1), or the engine's own frame limit the host writes into game memory (`hle_fpslock.c`: Monster Hunter Rise 16.0.2, which also needs `present_interval display`; any Unreal Engine 4.2x / 5 build, its `t.MaxFPS` (or `ue_fps_cvar`) and `rhi.SyncInterval` or `nvn.SyncInterval`, found by name by the rewriter, so the game asks for present interval 1 itself; Luigi's Mansion 3 1.4.0, its frame limiter's milliseconds per frame, with `present_interval display`; Pokémon Brilliant Diamond 1.3.0, Unity's integer `Application.targetFrameRate`, also with `present_interval display`; EA SPORTS FC 27, Frostbite's simulation rate, found by name on any build, and on 1.8d.310c its frame regulator, also with `present_interval display`; Fire Emblem Fortune's Weave, a target frame rate the game steps by, found by its readers on any build, so the game asks for present interval 1 itself). `0` (the default): the game's own step. `30` / `60`: the host decides each frame's step in whole vsyncs and keeps the frame on screen that long (`hle_nvn.c` `fstep_tick`, `nvn_fstep.h`): 1 at a 60 cap, 2 when 60 does not hold or at a 30 cap (and while a prerendered movie plays), plus one vsync while the game is behind real time after a late frame. Presents follow that hold instead of the interval the game sets. `DM_FPS_TARGET` overrides it, also from `Documents/env.txt`; the log line `frame step: game time … of real time` checks the game's speed. A derived profile (`derive_profile`) whose `main` has no `FRAME_STEP` stubs, while the profile has them for other builds, gets `fps_target 0`: without the stubs the host would present every vsync and the game would run at double speed. On a main with neither a frame step nor an engine limit the host writes, the host keeps the game's own present interval whatever `fps_target` or `DM_FPS_TARGET` says (`hle_fpslock.c` `dm_fps_lock_engine`) | | `fps_max` | `60` (the default) or `120`: the highest rate `fps_target` and the player's **Frame rate** take. `120` only for a game with an engine limit the host writes and whose speed was measured at 120 against 60 over the same inputs: the target then follows the frames that fit, 120 or 60 ([120 fps follows the frame step](../../docs/reference/frame-rate.md#120-fps-follows-the-frame-step)). | | `fps_hold` | `on` (the default): with `fps_target`, three late frames in a row raise the hold above the cap's vsyncs (30 or 20 fps), and steady frames that fit try the lower hold again. `off`: frames are never held longer than the cap's vsyncs; each is shown as soon as it is ready. For a game whose updates do not wait for the presents and step by the cap whatever the hold (Age of Calamity): there the hold only paces the screen. `DM_FPS_HOLD=on` / `off` overrides it, also from `Documents/env.txt` Paper Mario: The Thousand-Year Door sets it off too: its frame waits for three render worker threads, and holding frames left them idle, so a frame cost more the longer the hold (the hold stuck at 3 vsyncs, 20 fps, on a loaded Mac; 32–58 fps without holds) | +| `vrr` | `on` / `off` (the default): `vi::QueryDisplayVrrSupported` answers yes on the Mac when the game's window is on a screen of 120 Hz or more, docked too, unlike the Switch 2's handheld-only VRR (`hle_vi.c`; never on iOS, where it is untested), and the rate the game then asks for with `vi::SetLayerExtendedSwapInterval(layer, 1, fps)` paces presentation in place of the present interval, on the 120 Hz display link (`hle_nvn.c` `nvn_vrr_set`). Only for a game whose VRR mode was checked to keep its speed at the rates it picks; without the key the host answers no VRR, as before. `DM_VRR=0` / `1` overrides it on the Mac (`1` also on a slower screen). Donkey Kong Bananza sets it on: [A Switch 2 game's VRR rate paces presentation](../../docs/reference/frame-rate.md#a-switch-2-games-vrr-rate-paces-presentation) | | `ue_fps_cvar` | Unreal Engine games: the console variable the host writes `fps_target` into (`hle_fpslock.c`). `t.MaxFPS` (the default) is the engine's frame limit; a game whose settings apply a variable of its own to `t.MaxFPS` names that one, or the next settings change sets the game's 30 again (SHIN MEGAMI TENSEI V: Vengeance: `Project.FrameRateLimit`). The rewriter finds it by its registered name, so a name set here must be in `dm/tools/ue_cvar.py`'s `NAMES` (a red line otherwise): [Unreal's frame limit and present interval, found by name](../../docs/reference/frame-rate.md#unreals-frame-limit-and-present-interval-found-by-name) | | `ue_texture_pool` | Unreal Engine games: megabytes of the texture streaming pool (`r.Streaming.PoolSize`) the host raises the game's own to, on the Mac (`hle_uecvar.c`; the rewriter finds the variable by its name, `dm/tools/ue_cvar.py`): past its pool the streamer drops the top mips of what is in view, and the Switch's pool is small (DRAGON QUEST VII Reimagined: 200 MB, 1000 on Windows). `0` (the default): the game's own pool. A larger value the game sets, or −1, stays. `DM_UE_TEXTURE_POOL` overrides it; on iOS only that switch turns it on (`Documents/env.txt`). An improvement over the Switch, not a fix: [Unreal's texture streaming pool](../../docs/reference/rendering.md#unreals-texture-streaming-pool) | | `core_preempt` | `on` / `off` (the default): additionally check higher-priority runnable threads pinned to the caller's core after every thread SVC (`hle_thread.c`). `DM_PREEMPT=0` / `1` overrides it. Wake-triggered same-core handoffs apply to every game with either setting and wait until the threads the wake made runnable block, with no wall-clock cap. Metroid Dread no longer needs this key or its movie request-ring gates; see [Same-core wake handoffs](../../docs/reference/threads.md#same-core-wake-handoffs) | diff --git a/dm/games/game.py b/dm/games/game.py index 53e23ee897..d9a38d5f84 100644 --- a/dm/games/game.py +++ b/dm/games/game.py @@ -191,6 +191,8 @@ def header_one(g): if sks and (not m or not 0x30 <= int(m[1], 0) or not 0 < int(m[2], 0) or int(m[1], 0) + int(m[2], 0) > 0x880 or (int(m[1], 0) < 0x760 and int(m[1], 0) + int(m[2], 0) > 0x700)): # not over +0x700..+0x75f, the code area and compute fields the tools read sys.exit(f'{g.id}: shader_key_skip takes + within the first 0x880 bytes of the control section, past its header (+0x30) and clear of +0x700..+0x75f: {sks}') sko, skl = (int(m[1], 0), int(m[2], 0)) if m else (0, 0) + vrr = g.get('vrr', 'off') # vi::QueryDisplayVrrSupported answers yes on a 120 Hz screen, and the extended swap interval's rate paces presentation (hle_vi.c, hle_nvn.c): on / off + if vrr not in ('on', 'off'): sys.exit(f'{g.id}: vrr must be on or off: {vrr}') rcd = g.get('render_cond', 'off') # NVN conditional rendering evaluated at replay, timestamp reports with counter 0 (nvn_mtl.m OP_RENDER_COND): on / off if rcd not in ('on', 'off'): sys.exit(f'{g.id}: render_cond must be on or off: {rcd}') tfu = g.get('tex_flush_upload', 'off') # textures written by the CPU into a pool and published with nvnMemoryPoolFlushMappedRange are uploaded like packaged ones (hle_nvn.c): on / off @@ -259,6 +261,7 @@ def header_one(g): f'#define DM_GAME_TEX_FLUSH_UPLOAD {int(tfu == "on")}', f'#define DM_GAME_SHADER_KEY_SKIP_OFF {sko:#x}', f'#define DM_GAME_SHADER_KEY_SKIP_LEN {skl:#x}', f'#define DM_GAME_RENDER_COND {int(rcd == "on")}', + f'#define DM_GAME_VRR {int(vrr == "on")}', f'#define DM_GAME_TEX_DIRTY_UPLOAD {int(tdu == "on")}', f'#define DM_GAME_TEX_DYN_SLOTS {int(tds == "on")}', f'#define DM_GAME_GPU_TS_SEGMENT {int(gts == "segment")}', diff --git a/dm/gfx/hle_vi.c b/dm/gfx/hle_vi.c index 56e37ee4dd..e60b582268 100644 --- a/dm/gfx/hle_vi.c +++ b/dm/gfx/hle_vi.c @@ -93,10 +93,18 @@ HLE_FORCE("_ZN2nn2vi21QueryDisplayHdrStatusEPbPNS0_15HdrOutputFormatEPtPKNS0_7Di (void)disp; if (on) *on = 0; if (fmt) *fmt = 0; if (peak) *peak = 0; return RES_OK; } HLE_FORCE("_ZN2nn2vi25QueryDisplayHdrPaperWhiteEPtPKNS0_7DisplayE", "display HDR paper white: 203 nits (BT.2408); HDR is off", Result, h_vi_PaperWhite, uint16_t *nits, const void *disp) { (void)disp; if (nits) *nits = 203; return RES_OK; } -HLE_FORCE("_ZN2nn2vi24QueryDisplayVrrSupportedEPbPKNS0_7DisplayE", "display VRR: not supported (fixed refresh rate)", Result, h_vi_VrrSupported, uint8_t *on, const void *disp) { - (void)disp; if (on) *on = 0; return RES_OK; } -HLE_FORCE("_ZN2nn2vi23SetDisplayVrrCapabilityEPNS0_7DisplayEb", "display VRR capability: the host has no VRR", Result, h_vi_SetVrr, void *disp, unsigned on) { - (void)disp; (void)on; return RES_OK; } +// VRR: supported only for a game whose profile has vrr on (or DM_VRR=1), on the Mac, with the window on a screen of 120 Hz or more, docked too (the Switch 2 has it handheld only); +// the rate the game then asks for paces its frames (hle_nvn.c nvn_vrr_available, nvn_vrr_set) +__attribute__((weak)) int nvn_vrr_available(void) { return 0; } // hle_nvn.c; builds without NVN have no VRR +__attribute__((weak)) void nvn_vrr_set(int fps) { (void)fps; } +HLE_FORCE("_ZN2nn2vi24QueryDisplayVrrSupportedEPbPKNS0_7DisplayE", "display VRR: supported for a profile with vrr on, on a Mac screen of 120 Hz or more", Result, h_vi_VrrSupported, uint8_t *on, const void *disp) { + (void)disp; int v = nvn_vrr_available(); + static atomic_int was = -1; if (atomic_exchange(&was, v) != v) LOG("vi::QueryDisplayVrrSupported: %s", v ? "yes (profile vrr on, or DM_VRR=1)" : "no (fixed refresh rate)"); + if (on) *on = (uint8_t)v; + return RES_OK; } +HLE_FORCE("_ZN2nn2vi23SetDisplayVrrCapabilityEPNS0_7DisplayEb", "display VRR capability: off clears the extended swap interval's rate", Result, h_vi_SetVrr, void *disp, unsigned on) { + (void)disp; if (!(on & 0xff)) nvn_vrr_set(0); + return RES_OK; } // The largest display buffer (vi2 through the SDK, sdk+0x5e10c0, which the host does not serve: Result 0x272 and the output left as the game set it). BufferMaxResolution is // 0 1280x720, 1 1920x1080, 2 2560x1440, 3 3840x2160 (Fire Emblem Fortune's Weave's own table, main+0x1abc2c0 / +0x1abc050). Fire Emblem Fortune's Weave sets 0 first, so the failed call // told it 720p: it chose its 1280x720 DLSS network and rendered at 640x360, and the host scaled that to the window. Answered as a Switch 2 would: in handheld mode its own 1920x1080 @@ -113,11 +121,14 @@ HLE_FORCE("_ZN2nn2vi31QueryDisplayBufferMaxResolutionEPNS0_19BufferMaxResolution static atomic_int once; if (!atomic_exchange(&once, 1)) LOG("vi::QueryDisplayBufferMaxResolution: %dx%d (%s, display %dx%d)", k[r][0], k[r][1], dm_opmode() == 1 ? "docked" : "handheld", w, h); if (out) *out = r; return RES_OK; } -// The extended swap interval asks for a presentation rate between whole vsync intervals (VRR); without VRR the swap interval NVN sets stays in effect -HLE_FORCE("_ZN2nn2vi28SetLayerExtendedSwapIntervalEPNS0_5LayerEii", "layer extended swap interval: needs VRR, which the host has not; NVN's swap interval stays", Result, h_vi_ExtSwap, +// The extended swap interval asks for a presentation rate between whole vsync intervals (VRR). Donkey Kong Bananza passes (1, frames a second), 40 to 60 (main+0x2049a8, +// main+0x2038d0). With VRR answered as supported that rate paces presentation (hle_nvn.c); otherwise, or with another first argument, the swap interval NVN sets stays in effect +HLE_FORCE("_ZN2nn2vi28SetLayerExtendedSwapIntervalEPNS0_5LayerEii", "layer extended swap interval: the rate paces presentation where VRR is answered as supported", Result, h_vi_ExtSwap, void *layer, int a, int b) { - (void)layer; static atomic_int once; if (!atomic_exchange(&once, 1)) LOG("vi::SetLayerExtendedSwapInterval %d %d: no VRR on the host, the NVN swap interval stays", a, b); return RES_OK; } -HLE_FORCE("_ZN2nn2vi30ClearLayerExtendedSwapIntervalEPNS0_5LayerE", "layer extended swap interval cleared", Result, h_vi_ExtSwapClear, void *layer) { (void)layer; return RES_OK; } + (void)layer; int use = nvn_vrr_available() && a == 1 && b > 0 && b <= 120; + static atomic_int once; if (!use && !atomic_exchange(&once, 1)) LOG("vi::SetLayerExtendedSwapInterval %d %d: not followed (%s), the NVN swap interval stays", a, b, nvn_vrr_available() ? "an unknown form" : "no VRR on the host"); + nvn_vrr_set(use ? b : 0); return RES_OK; } +HLE_FORCE("_ZN2nn2vi30ClearLayerExtendedSwapIntervalEPNS0_5LayerE", "layer extended swap interval cleared: the NVN swap interval paces again", Result, h_vi_ExtSwapClear, void *layer) { (void)layer; nvn_vrr_set(0); return RES_OK; } HLE_FORCE("_ZN2nn2vi35SetLayerNextHdrContentGamutMetadataEPNS0_5LayerENS0_15HdrContentGamutE", "layer HDR metadata: SDR output, nothing to apply", Result, h_vi_HdrGamut, void *layer, int g) { (void)layer; (void)g; return RES_OK; } HLE_FORCE("_ZN2nn2vi39SetLayerNextHdrToSdrToneMappingMetadataEPNS0_5LayerENS0_14HdrToneMappingE", "layer HDR metadata: SDR output, nothing to apply", Result, h_vi_HdrToneMap, void *layer, int m) { diff --git a/dm/gfx/nvn/hle_nvn.c b/dm/gfx/nvn/hle_nvn.c index 277d7a2edf..99789db39a 100644 --- a/dm/gfx/nvn/hle_nvn.c +++ b/dm/gfx/nvn/hle_nvn.c @@ -1852,13 +1852,50 @@ static void present_cfg(const win_t *o) { LOG("[nvn] present tick %d Hz, interval mode %s (%s; present interval set by the game %d; %s)", g_hz, g_display_mode ? "display (one frame per tick, a cut)" : "game (original semantics)", m ? "DM_PRESENT_INTERVAL" : "game profile present_interval", o->interval, scr > 0 ? "window" : "no window"); } -static int present_itv(const win_t *o) { int c = fps_present_cap(), k = atomic_load(&g_fs_k); return c ? (k && fstep_on() ? k : fstep_hz() / c) : g_display_mode ? 1 : o->interval > 0 ? o->interval : 1; } // vsyncs per frame that presentation follows +// VRR (profile key vrr, DM_VRR=1 / 0): a Switch 2 game told that the display can refresh at a variable rate (vi::QueryDisplayVrrSupported, hle_vi.c) asks for a frame rate between +// whole vsyncs with vi::SetLayerExtendedSwapInterval(layer, 1, fps) and leaves NVN's present interval as it was. Donkey Kong Bananza's VRR controller (main+0x204640) takes the rate +// from its frame time (60 / (vsyncs + margin), clamped and stepped by its settings); at 40 fps or more it sets it through main+0x204934, below 40 it turns VRR off and presents at +// whole intervals again (interval 2, 30 fps). While a rate is set it replaces the present interval: frames are paced at 1 / fps, on the 120 Hz display link where the screen has +// one, so a frame stays up 2 or 3 refreshes of 8.3 ms (40, 48 or 60 fps exactly; 45 or 50 alternate) instead of 1 or 2 vsyncs of 16.7 ms. Answered only on the Mac, on a window +// whose screen runs at 120 Hz or more (ProMotion; front_screen_max_fps, which follows the window to another screen) unless DM_VRR=1; never on iOS, where it is untested; never with +// a frame step or 120 fps, which decide the steps themselves. Docked too, a deliberate difference from the Switch 2, whose VRR is handheld only: the docked picture is the one the +// Mac shows by default, and the game's VRR mode does not depend on the operation mode +static _Atomic int g_vrr_fps; // the rate the game set, 0 = none (cleared, or VRR capability off) +int nvn_vrr_available(void) { // hle_vi.c: what vi::QueryDisplayVrrSupported answers +#if TARGET_OS_OSX + static int env = -2; if (env == -2) { const char *e = getenv("DM_VRR"); env = e && *e ? atoi(e) != 0 : -1; } + if (env == 0 || (env < 0 && !DM_GAME_VRR) || dm_fps_state || fps_high()) return 0; + return env > 0 || front_screen_max_fps() >= 120; +#else + return 0; // iOS: VRR untested there, so the game keeps its whole present intervals +#endif +} +void nvn_vrr_set(int fps) { // hle_vi.c: the extended swap interval's rate, 0 to clear it + if (fps < 0) fps = 0; + if (fps > 120) fps = 120; + int was = atomic_exchange(&g_vrr_fps, fps); + if (was == fps) return; + static _Atomic unsigned changes; unsigned n = atomic_fetch_add(&changes, 1) + 1; + if (n <= 40 || n % 200 == 0) LOG("[nvn] VRR: %d → %d fps (change %u)%s", was, fps, n, fps ? "" : ": the present interval paces again"); +} +static int vrr_fps(void) { int v = atomic_load(&g_vrr_fps); return v > 0 && !dm_fps_state && !fps_high() ? v : 0; } +static int g_vrr_link_env = -1; // DM_PRESENT_LINK chose the display link: VRR keeps it +static void vrr_link_tick(void) { // present thread, once per present: the 120 Hz display link while a VRR rate is set on a screen that has it, the 60 Hz one again after + static int on_was; + if (g_vrr_link_env < 0) g_vrr_link_env = getenv("DM_PRESENT_LINK") != NULL; + int on = !g_vrr_link_env && vrr_fps() && front_screen_max_fps() >= 120; + if (on == on_was) return; + on_was = on; mtl_link_mode_set(on ? 2 : 1); + static _Atomic unsigned n; if (atomic_fetch_add(&n, 1) < 20) LOG("[nvn] VRR: display link at %d Hz", on ? 120 : 60); +} +static int present_itv(const win_t *o) { if (vrr_fps()) return 1; // a VRR frame: at most 25 ms (40 fps), held for no whole number of vsyncs + int c = fps_present_cap(), k = atomic_load(&g_fs_k); return c ? (k && fstep_on() ? k : fstep_hz() / c) : g_display_mode ? 1 : o->interval > 0 ? o->interval : 1; } // vsyncs per frame that presentation follows // The acquire's wait for the display link: the present interval's vsyncs (60 Hz units), or with present_interval display above 60 Hz (DM_PRESENT_HZ=120 on a 120 Hz link, // DM_PRESENT_LINK=2) one tick of the present rate, as many link ticks as that takes. Before, a 120 Hz link waited two ticks per frame there, and display mode never went past 60 fps static int link_wait(const win_t *o, uint64_t timeout_ns) { static int hz_was, rate_was, remainder; // acquire thread only, as with mtl_link_wait_ticks if (fps_high()) { int t = present_itv(o) * mtl_link_rate() / 120; return mtl_link_wait_ticks(t < 1 ? 1 : t, timeout_ns); } // the hold, in ticks of 1/120 s, on a link at 120 or 60 Hz - int hz = g_display_mode && !fps_present_cap() && g_hz > 60 ? g_hz : 0, rate = mtl_link_rate(); + int v = vrr_fps(), hz = v ? v : g_display_mode && !fps_present_cap() && g_hz > 60 ? g_hz : 0, rate = mtl_link_rate(); // VRR: the game's rate, in link ticks as below if (hz != hz_was || rate != rate_was) { remainder = 0; hz_was = hz; rate_was = rate; } if (hz) { int ticks = 1; @@ -1871,7 +1908,11 @@ static int link_wait(const win_t *o, uint64_t timeout_ns) { } static uint64_t present_period(const win_t *o) { present_cfg(o); - int was = atomic_load(&g_game_itv), want = o->interval > 0 ? o->interval : 1; + int v = vrr_fps(), was = atomic_load(&g_game_itv), want = v ? 1 : o->interval > 0 ? o->interval : 1; // VRR: the interval the game left set does not count (no 30 Hz link) + if (v && g_hz > 0) { + if (was != 1) { atomic_store(&g_game_itv, 1); atomic_store(&g_itv2_since, 0); } + return 1000000000ull / (uint64_t)v; + } if (want != was) { atomic_store(&g_game_itv, want); if (want < 2 || want % 2) atomic_store(&g_itv2_since, 0); // an odd interval needs the 60 Hz link: 3 vsyncs on a 30 Hz link were 2 ticks, 15 fps for 20 @@ -2000,6 +2041,7 @@ NVN(nvnQueuePresentTexture, void, NVNqueue *q, NVNwindow *w, int idx) { (void)q; win_t *o = (win_t *)w; o->presents++; dm_ue_cvar_tick(); if (fps_high()) high_cap_tick(); + vrr_link_tick(); if (dm_fps_state) __atomic_store_n((uint32_t *)(dm_fps_state + 4), (uint32_t)dm_movie_playing(), __ATOMIC_RELAXED); // the stubs' copy of the movie state (hle.h) int c = fps_step_cap(); if (dm_fps_state) { static int was; if (c != was) { LOG("[nvn] frame step cap %d → %d fps at present %llu%s", was, c, (unsigned long long)o->presents, c == 30 && was == 60 ? (dm_movie_playing() ? " (movie playing)" : thermal_capped() ? " (thermal state serious)" : "") : ""); was = c; } } diff --git a/dm/gfx/tests/t_nvn.c b/dm/gfx/tests/t_nvn.c index d773f68527..537732e3ea 100644 --- a/dm/gfx/tests/t_nvn.c +++ b/dm/gfx/tests/t_nvn.c @@ -89,6 +89,28 @@ int main(void) { EXPECT("the state page's movie flag is the host's copy, not read for the cap",60,fps_present_cap()); dm_fps_state=NULL;unsetenv("DM_FPS_TARGET");g_present_cap=-1;nvn_present_config(0,0); } + // VRR (profile vrr, DM_VRR): the rate of vi::SetLayerExtendedSwapInterval replaces the present interval the game left set + // (Donkey Kong Bananza stays at interval 2 when it steps from 30 fps up into VRR), and clearing it gives the interval back. + { win_t w={.interval=2};nvn_present_config(60,0);dm_fps_state=NULL;setenv("DM_VRR","1",1);setenv("DM_FPS_TARGET","0",1);g_present_cap=-1; + EXPECT("DM_VRR=1: VRR answered without a 120 Hz screen",1,nvn_vrr_available()); + present_period(&w);g_itv2_since=(now_ns()-2000000000ull)|1; + EXPECT("interval 2 held a second: 30 Hz link",30,nvn_game_fps()); + nvn_vrr_set(45); + EXPECT("VRR 45: period ns",22222222,present_period(&w)); + EXPECT("VRR 45: one vsync, the interval not held",1,present_itv(&w)); + EXPECT("VRR 45: no 30 Hz link",60,nvn_game_fps()); + nvn_vrr_set(60); + EXPECT("VRR 60: period ns",16666666,present_period(&w)); + nvn_vrr_set(500); + EXPECT("VRR above 120 is 120",8333333,present_period(&w)); + nvn_vrr_set(0); + EXPECT("VRR cleared: the game's interval again",2,present_itv(&w)); + EXPECT("VRR cleared: its period",33333333,present_period(&w)); + _Alignas(8) uint8_t state[64]={0};float cap=60;memcpy(state,&cap,4);dm_fps_state=state;nvn_vrr_set(45); + EXPECT("a frame step page: no VRR",0,nvn_vrr_available()); + EXPECT("a frame step page: its own step, not the VRR rate",16666666,present_period(&w)); + nvn_vrr_set(0);dm_fps_state=NULL;unsetenv("DM_VRR");unsetenv("DM_FPS_TARGET");g_present_cap=-1;nvn_present_config(0,0); + } // No frame step page and no engine limiter the host writes (a main the profile's // stubs or limiter rows do not know): nothing makes the game follow the cap, so // presentation keeps the game's own interval (Tears of the Kingdom 1.4.2 at 2x). diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 8850f59a87..7cf0620bba 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -441,6 +441,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - with the display link driving presents, a presented frame comes out black when three presents are on the GPU at once, or two after a dropped frame; on an M6 the same reuse also logs `[RED] display command buffer error` with `kIOGPUCommandBufferCallbackErrorPageFault`. → [A present command allocator is reused only after its own present completes](../reference/frame-rate.md#a-present-command-allocator-is-reused-only-after-its-own-present-completes) - "sometimes the speed increases when jumping" (Mario 1.2.1 on the iPhone): in `display` mode interval-2 frames were shown for 16.7 ms and the game visibly jumped ahead. "When pressing B, Link's move pace is speeding up" (Breath of the Wild on the iPhone); `old frames dropped by merging`. → [Frames of a dynamic present interval held on screen and at the acquire](../reference/frame-rate.md#frames-of-a-dynamic-present-interval-held-on-screen-and-at-the-acquire) - Donkey Kong Bananza ran its heavy areas at 15 fps where its GPU took about 46 ms a frame: the frames came every 66.7 ms, and the presentation thread spent 85% of its time in `nvnWindowAcquireTexture` waiting for display-link ticks. → [Odd present intervals on the 30 Hz display link](../reference/frame-rate.md#odd-present-intervals-on-the-30-hz-display-link) +- Donkey Kong Bananza drops from 60 straight to 30 fps whenever a frame runs past one vsync (or to 20 in its heaviest views), on a ProMotion Mac that could show 40-60 fps; the log has `the game sets present interval 1 → 2 (from main+0x204c20)` and no line for the VRR calls. → [A Switch 2 game's VRR rate paces presentation](../reference/frame-rate.md#a-switch-2-games-vrr-rate-paces-presentation) - with `present_interval display`, `DM_PRESENT_HZ=120` and `DM_PRESENT_LINK=2` (a 120 Hz display link) a game stays at 60 fps although its frame takes 8 ms; the display link logs about two ticks per present. With a requested 90 Hz, truncating the tick conversion instead allows 120 fps (reproduced through the real acquire with simulated link ticks). → [One frame per tick of a 120 Hz display link](../reference/frame-rate.md#one-frame-per-tick-of-a-120-hz-display-link) - frames miss the screen while the GPU stays at its lowest clock (444 MHz on an M2 Max); more work (a higher render scale) only makes the frames slower. → [Present snapshots: the game's swapchain texture free at once](../reference/frame-rate.md#present-snapshots-the-games-swapchain-texture-free-at-once) - 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) diff --git a/docs/env_switches.tsv b/docs/env_switches.tsv index 71a188769d..6f2f4db2a2 100644 --- a/docs/env_switches.tsv +++ b/docs/env_switches.tsv @@ -355,6 +355,7 @@ DM_UE_TEXTURE_POOL switch Megabytes Unreal's texture streaming pool (`r.Streamin DM_UPSCALER player `metalfx` draws the game's own upscaler shaders (agl's NDSR network, found by the shader map) with MetalFX's spatial scaler; `game` (the default) runs them DM_VK_CAPTURE path `=` (Mac) writes every SPIR-V module the game creates to `/.spv`; the NSP script's Vulkan shader capture stage sets it DM_VK_LOG diagnostic `=1` logs Vulkan object creation in the Vulkan-on-Metal layer +DM_VRR switch `0` answers no VRR for a profile with `vrr on`, so the game keeps its whole present intervals (60, 30, 20 fps); `1` answers VRR for it on any screen, also below 120 Hz (by default only when the window is on a screen of 120 Hz or more). Mac only: iOS never answers VRR. A profile without the key is unaffected DM_VSYNC_HZ switch `=`: rate at which a host thread fires the vi vsync event when the display link does not drive it (default 60) DM_WATCH diagnostic `=` dumps all threads after that many seconds (SIGALRM) and turns on the stall watchdog; off unless set DM_WATCH_SAMPLE diagnostic `=` (Mac): when the stall watchdog fires, samples the process for 1 s with `/usr/bin/sample` and writes the thread stacks to that path diff --git a/docs/reference/frame-rate.md b/docs/reference/frame-rate.md index d644ee7a0a..dff1d5780a 100644 --- a/docs/reference/frame-rate.md +++ b/docs/reference/frame-rate.md @@ -228,6 +228,15 @@ Frame pacing: the host's frame step, engine frame limiters, present intervals an - **Found on:** [Donkey Kong Bananza](../../dm/games/040074A01BF12000/README.md): the slow area on an M1, frame time p50 66.7 → 50.2 ms at interval 3 (`DM_NVN_TSV` `@frame_ms`, `@itv`, `@link_gap_ms` 33.3 → 16.7). - **Check:** `t_nvn` (`interval 3: 60 Hz link`, `back to 2 from 3 waits a second again`). +### A Switch 2 game's VRR rate paces presentation + +- **Symptom:** Donkey Kong Bananza drops from 60 straight to 30 fps whenever a frame runs past one vsync (or to 20 in its heaviest views), on a ProMotion Mac that could show 40-60 fps; the log has `the game sets present interval 1 → 2 (from main+0x204c20)` and no line for the VRR calls. +- **Cause:** the game has two frame-rate controllers, chosen per frame at `main+0x2036b0` by what `vi::QueryDisplayVrrSupported` answered. The fixed-refresh one (`main+0x204a18`) turns the frame time into whole vsyncs (60 / interval: 60, 30 or 20 fps). The VRR one (`main+0x204640`) takes 60 / (vsyncs + margin), clamps and steps it (2 fps steps on the Mac), and at 40 fps or more calls `SetDisplayVrrCapability(true)` and `SetLayerExtendedSwapInterval(layer, 1, fps)` (`main+0x204934`); below 40 it turns VRR off and presents at interval 2 again. The host answered that the display has no VRR, so only the first ever ran. +- **Fix:** a profile key, `vrr on`: `QueryDisplayVrrSupported` answers yes on the Mac when the game's window is on a screen of 120 Hz or more (`front_screen_max_fps`, which follows the window to another screen; `hle_vi.c`), `SetLayerExtendedSwapInterval(layer, 1, fps)` hands the rate to `nvn_vrr_set` (`hle_nvn.c`), and `SetDisplayVrrCapability(false)` and `ClearLayerExtendedSwapInterval` clear it. While a rate is set it replaces the present interval, which the game leaves at 2 when it steps up into VRR: `present_period` is 1 / fps, the 30 Hz link is not taken, and the acquire waits the rate's ticks of a 120 Hz display link, with the fractional tick carried as in display mode above 60 Hz (45 fps: 3, 3, 2 ticks of 8.3 ms). A frame step state page or 120 fps turns it off: they decide the steps themselves. A key and not a rule for every game, because a game's VRR mode must keep its speed at rates between whole vsyncs, and no other game here has been checked for that. Docked too, a deliberate difference from the Switch 2, whose VRR is handheld only: the docked picture is what the Mac shows by default, and the game's VRR controller does not look at the operation mode. iOS never answers VRR: it is untested there, and Low Power Mode and the thermal state change the display link under it. `DM_VRR=0` / `1` overrides it on the Mac. +- **Found on:** [Donkey Kong Bananza](../../dm/games/040074A01BF12000/README.md#found-on-this-game) (M1 Pro, built-in ProMotion screen): with the key the game asked for 40 to 60 fps in 2 fps steps and ran 47.6-52.5 fps a 300-frame window in the first two minutes after start, no new red lines; the same spots were not timed without the key. Played by hand, the player saw a normal picture and normal speed. +- **Check:** `t_nvn` (a rate replaces the interval the game left at 2 and the 30 Hz link, its period at 45 and 60, clamped at 120, cleared back to the interval; a frame step page ignores it). +- **Not checked:** game speed measured against interval pacing over the same scripted inputs, the game below 40 fps with the key (it leaves VRR there by its own rule), a screen below 120 Hz with `DM_VRR=1`, an external screen, the iPhone (off there). The on-screen dropped-frame counts still assume a 60 fps target, so a 20 ms VRR frame counts as dropped. + ### One frame per tick of a 120 Hz display link - **Symptom:** with `present_interval display`, `DM_PRESENT_HZ=120` and `DM_PRESENT_LINK=2` (a 120 Hz display link) a game stays at 60 fps although its frame takes 8 ms; the display link logs about two ticks per present. With a requested 90 Hz, truncating the tick conversion instead allows 120 fps (reproduced through the real acquire with simulated link ticks).