Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
patches: a derived profile without frame step stubs keeps the game's own step (#303)
v0lumehi055c19e3322a
diff --git a/patches/0040-nsp-derived-profile-without-frame-step.patch b/patches/0040-nsp-derived-profile-without-frame-step.patchnew file mode 100644index 00000000..7fc3d64a--- /dev/null+++ b/patches/0040-nsp-derived-profile-without-frame-step.patch@@ -0,0 +1,104 @@+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001+From: v0lumehi <[email protected]>+Date: Sat, 31 Oct 2026 13:59:49 +0100+Subject: [PATCH] build_ipa_from_nsp: a derived profile without frame step+ stubs keeps the game's own step++Tears of the Kingdom 1.4.2 ran at double speed. The profile registers only 1.4.3, so a derived profile packs 1.4.2. The profile's patches.py has FRAME_STEP stubs for the 1.4.3 main only. The 1.4.2 main gets no frame step state page, and derive_profile copied fps_target 60 unchanged. Without the page, fps_present_cap() returns 60, present_itv() presents every vsync, and the game steps 1/30 s per frame.++derive_profile now reads the FRAME_STEP table of the profile's patches.py with ast (the file is never run). When the table has entries and none is for the derived main's build ID, the derived profile gets fps_target 0, with a comment line that says why. Registered builds, shader_only derivations and profiles without FRAME_STEP are unchanged.++Reported in LNPANDY/NX2Apple#303, traced by fredchu.++Upstream-Status: forwarded seedds/nxnative#794+Reason: A derived profile with frame step stubs for other builds only kept fps_target 60, so Tears of the Kingdom 1.4.2 ran at double speed+Issue: LNPANDY/NX2Apple#303++Co-authored-by: Fred Chu <[email protected]>++---+ dm/games/README.md | 2 +-+ dm/tools/tests/test_nsp_incremental.py | 14 +++++++++++++++ scripts/build_ipa_from_nsp.py | 16 +++++++++++++++-+ 3 files changed, 30 insertions(+), 2 deletions(-)++diff --git a/dm/games/README.md b/dm/games/README.md+index 1229b36..25059ea 100644+--- a/dm/games/README.md++++ b/dm/games/README.md+@@ -188,7 +188,7 @@ One row per profile; the notes are each profile's `README.md`.+ | `present_snap` | `on` / `off` (the default): present snapshots by default (`nvn_mtl.m` `mtl_snap_on`, a ring of 2 host textures): each present is copied and the game's swapchain texture is free at once, so the GPU does not idle between frames ([Present snapshots](../../docs/reference/frame-rate.md#present-snapshots-the-games-swapchain-texture-free-at-once)). A setting the game was tested with: the iOS launcher's **Frame buffering** defaults to it, and `DM_PRESENT_SNAP` / `Documents/env.txt` override it. Breath of the Wild sets it on |+ | `pso_mtl4` | `on` / `off` (the default): on iOS, render pipelines through MTL4Compiler by default, with the pipeline archive, the prepare step, the hybrid pipelines and the prebuild (on by default on the Mac; `DM_PSO_MTL4=1` / `0` overrides). A setting the game was tested with on an iPhone. It also gives the app the [pipeline archive made offline](../../docs/development.md#the-pipeline-archive-made-offline) (`build_ipa.sh` builds it for `g18p` unless `DM_PSO_GPUS` says otherwise; without the key, none) and the pulling libraries (`_bp`). Breath of the Wild sets it on |+ | `enc_thread` | `on` / `off` (the default): on the Mac, Metal encoding on its own thread by default, as on iOS for every game (`nvn_rec.h` `rec_on`; `DM_NVN_ENC_THREAD=1` / `0` and the launcher's Encode thread override it). For a game that waits on its fence every frame, whose submit thread would otherwise also encode before the wait: Need for Speed: Hot Pursuit Remastered's 60 fps frame took 18.1 ms with encoding on the submit thread and 16.7 on its own |+-| `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`). `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 |++| `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`). `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 |+ | `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) |+ | `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) |+diff --git a/dm/tools/tests/test_nsp_incremental.py b/dm/tools/tests/test_nsp_incremental.py+index 66db552..4387057 100644+--- a/dm/tools/tests/test_nsp_incremental.py++++ b/dm/tools/tests/test_nsp_incremental.py+@@ -122,6 +122,20 @@ class DeriveProfile(Base):+ nsp.derive_profile(g, self.pdir, 'v65536', self.ex, ['main'], shader_only=True)+ self.assertEqual(derived.table('shader_map.tsv').read_text(), 'generated DLC shader rows\n')+++ def test_derived_main_without_frame_step_stubs_keeps_the_games_step(self):++ source = self.src / 'game.tsv'; source.write_text(source.read_text() + 'fps_target\t60\n')++ (self.src / 'patches.py').write_text(f'def frame_step(ctx): pass\nFRAME_STEP = {{"{BID}": frame_step}}\n') # stubs for the registered main only, never run++ self.derive()++ self.assertEqual(game.Game(self.pdir).get('fps_target'), '0')++ self.assertIn('frame step stubs for other builds only', (self.pdir / 'game.tsv').read_text())++ (self.src / 'patches.py').write_text(f'FRAME_STEP = {{"{BID}": 1, "{BID2}": 2}}\n') # the derived main has stubs too++ self.derive(); self.assertEqual(game.Game(self.pdir).get('fps_target'), '60')++ (self.src / 'patches.py').write_text('CUTS = {}\n'); self.derive() # no frame step in the profile++ self.assertEqual(game.Game(self.pdir).get('fps_target'), '60')++ (self.src / 'patches.py').write_text(f'FRAME_STEP = {{"{BID}": 1}}\n'); self.derive()++ nsp.derive_profile(game.Game(self.src), self.pdir, 'v65536', self.ex, ['main'], shader_only=True) # shader tables only: the settings stay++ self.assertEqual(game.Game(self.pdir).get('fps_target'), '60')+++ def test_missing_corrupt_and_duplicate_override_rows_derive_again(self):+ for change in ('missing', 'corrupt', 'override', 'indented override', 'truncated'):+ with self.subTest(change):+diff --git a/scripts/build_ipa_from_nsp.py b/scripts/build_ipa_from_nsp.py+index 600fd55..53be322 100755+--- a/scripts/build_ipa_from_nsp.py++++ b/scripts/build_ipa_from_nsp.py+@@ -823,6 +823,15 @@ def derived_key(profile_dir):+ return profile_dir.with_name(profile_dir.name + '.derived.key')++++def frame_step_builds(path): # the build IDs of the FRAME_STEP table of a patches.py, read without running the file (empty: no table)++ try:++ for n in ast.parse(path.read_text(encoding='utf-8')).body:++ if isinstance(n, ast.Assign) and any(isinstance(t, ast.Name) and t.id == 'FRAME_STEP' for t in n.targets) and isinstance(n.value, ast.Dict):++ return {k.value for k in n.value.keys if isinstance(k, ast.Constant)}++ except (OSError, UnicodeError, SyntaxError): pass++ return set()+++++ def derive_profile(g, pdir, ver, d, mods, app=None, shader_only=False):+ """The game's profile for a version it was not brought up on (the folder has no update, another update, files of other builds, or a registered build under another title ID: app),+ in pdir (outside the repository, where a provisional profile goes): its keys as they are, with this version's build row, package and shader versions, and app's title ID and+@@ -830,7 +839,8 @@ def derive_profile(g, pdir, ver, d, mods, app=None, shader_only=False):+ left out: the build makes them again from this version's own files, as for a provisional profile. The hand-written tables (the prewarm list, shader_facts_manual.tsv) are copied:+ they name shaders by content key. A folder derived from the same inputs (the files copied and the game.tsv written, kept in <pdir>.derived.key) is kept as it is, with what later+ stages wrote into it (mem_used, the heap check, the split's tables), so their own reuse holds. shader_only preserves all registered game.tsv rows and links its existing cuts:+- only the shader tables change with installed DLC or newly decoded compute kernels. Returns the version's display version"""++ only the shader tables change with installed DLC or newly decoded compute kernels. A profile with frame step stubs (FRAME_STEP) for other builds only gets fps_target 0 for this version:++ its main has no state page, so a cap of 30 or 60 would present every vsync while the game steps 1/30 s per frame (double speed). Returns the version's display version"""+ src = sorted(p for p in g.dir.iterdir() if p.is_file() and (p.name not in DERIVED_SKIP or shader_only and p.name == 'patches.py'))+ T, disp = '\t', nacp.version(d / 'control.nacp') or '?'+ row = T.join(['build', ver, disp] + [f'{m}={build_id(d / "ExeFS" / m)}' for m in mods if (d / 'ExeFS' / m).exists()])+@@ -841,7 +851,11 @@ def derive_profile(g, pdir, ver, d, mods, app=None, shader_only=False):+ if app is not None and app != g.title_id: # the host reports the program ID and answers save data queries for it (hle_svc.c, hle_fs.c)+ ids = {'title_id': f'title_id{T}{app:016X}', 'program_id': f'program_id{T}{(pg.npdm(d / "ExeFS/main.npdm")[1] or app):016X}'}+ out.insert(1, f'# under title ID {app:016X}, not the profile\'s {g.title_id:016X}')++ stubs = frame_step_builds(g.dir / 'patches.py')++ own_step = shader_only or not stubs or not (d / 'ExeFS/main').exists() or build_id(d / 'ExeFS/main') in stubs+ for line in (g.dir / 'game.tsv').read_text(encoding='utf-8').splitlines():++ if not own_step and line.split(T, 1)[0] == 'fps_target' and line.partition(T)[2].strip() != '0':++ out += ['# the profile has frame step stubs for other builds only: this version\'s main has none, so it keeps the game\'s own step', f'fps_target{T}0']; continue+ if line.split(T, 1)[0] in ids: out.append(ids[line.split(T, 1)[0]]); continue+ if not shader_only and line.split(T, 1)[0] in ('build', 'package', 'shader_versions'):+ if not put: out += [row, f'package{T}{ver}', f'shader_versions{T}{ver}']; put = Truediff --git a/patches/series b/patches/seriesindex 9248d27a..99d8d350 100644--- a/patches/series+++ b/patches/series@@ -24,3 +24,4 @@0036-the-layout-editor-long-press-the-layout-button-a.patch0037-totk-1.4.2-frame-step-sites.patch0039-patch-nso-a-zero-extending-and-of-the-index-is-a.patch+0040-nsp-derived-profile-without-frame-step.patch