Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Fix the iOS save-load freeze: publish the replay block before waiting on uploads

LNPANDY569a415b28a1
diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m
index 7d72888d..14d0c249 100644
--- a/dm/gfx/nvn/nvn_mtl.m
+++ b/dm/gfx/nvn/nvn_mtl.m
@@ -1261,6 +1261,7 @@ int mtl_wait(uint64_t fence, uint64_t timeout_ns) {
static void mtl_finish_gpu(void) {
if (g_fence) mtl_wait(g_fence, UINT64_MAX);
+ if (on_sub_thread()) rec_publish(); // Pending upload commits sit in the open replay block: hand it over before waiting on g_up_pending, or a loader's QueueFinish mid-submission (Three Houses-style per-resource waits) sleeps forever. Other threads (suspend, diagnostics) cannot publish the submit thread's block; their waits resolve once the submission ends
up_wait_zero(&g_up_pending);
}
void mtl_present_flush(void);
@@ -1581,6 +1582,7 @@ static void up_begin(void) {
up_owner_check("up_begin");
if (g_up_cb) return;
unsigned slot = g_up_seq++ % 3;
+ if (atomic_load(&g_up_alloc_busy[slot])) rec_publish(); // The commit of the batch that still holds this slot sits in the open replay block (rec_commit_upload). Hand the block to the encode thread before sleeping on the slot: a submit thread that opens a 4th batch within one submission would otherwise wait forever — nothing else hands the block over mid-submission, and the submission cannot end while up_begin blocks (the iOS save-load freeze of NX2Apple issue #4)
up_wait_zero(&g_up_alloc_busy[slot]);
atomic_store(&g_up_alloc_busy[slot], 1); g_up_cur_slot = slot;
[g_up_alloc4[slot] reset];
diff --git a/scripts/sync_upstream.py b/scripts/sync_upstream.py
index 8810f69d..3c96b58f 100644
--- a/scripts/sync_upstream.py
+++ b/scripts/sync_upstream.py
@@ -35,7 +35,7 @@ OURS = {'dm/host/romfs_arc.c', 'dm/host/romfs_arc.h', 'dm/tools/romfs_arc.py',
# Vendored files that carry fork patches: taken fresh from upstream, then patched below.
PATCHED = ('dm/host/hle_fs.c', 'dm/host/Makefile', 'dm/ios/tools/build_ipa.sh',
'scripts/build_ipa_from_nsp.py', 'dm/gfx/tools/shader_split.py',
- 'dm/ios/Sources/main.m')
+ 'dm/ios/Sources/main.m', 'dm/gfx/nvn/nvn_mtl.m')
def git(*a):
r = subprocess.run(['git', *a], cwd=REPO, capture_output=True, text=True)
@@ -370,6 +370,35 @@ def patch_shader_split(t):
ue_inline_shaders(d, '/{}#{}'.format(rel, _sub), out)'''
return done('shader_split.py 3.9', t.replace(old, new, 1))
+NVM = 'dm/gfx/nvn/nvn_mtl.m'
+def patch_nvn_mtl(t):
+ # The fork's fix for the iOS save-load freeze (NX2Apple issue #4): an upload batch's commit can sit
+ # recorded in the open replay block (rec_commit_upload) while the submit thread sleeps on a slot or
+ # on g_up_pending — nothing else hands the block over mid-submission, so the submission can never
+ # end and the wait never resolves. Both waits publish the block first.
+ P = [
+ ('up_begin publish', 'the iOS save-load freeze of NX2Apple issue #4', [(
+ ''' unsigned slot = g_up_seq++ % 3;
+ up_wait_zero(&g_up_alloc_busy[slot]);''',
+ ''' unsigned slot = g_up_seq++ % 3;
+ if (atomic_load(&g_up_alloc_busy[slot])) rec_publish(); // The commit of the batch that still holds this slot sits in the open replay block (rec_commit_upload). Hand the block to the encode thread before sleeping on the slot: a submit thread that opens a 4th batch within one submission would otherwise wait forever — nothing else hands the block over mid-submission, and the submission cannot end while up_begin blocks (the iOS save-load freeze of NX2Apple issue #4)
+ up_wait_zero(&g_up_alloc_busy[slot]);''')]),
+ ('finish publish', 'hand it over before waiting on g_up_pending', [(
+ ''' if (g_fence) mtl_wait(g_fence, UINT64_MAX);
+ up_wait_zero(&g_up_pending);''',
+ ''' if (g_fence) mtl_wait(g_fence, UINT64_MAX);
+ if (on_sub_thread()) rec_publish(); // Pending upload commits sit in the open replay block: hand it over before waiting on g_up_pending, or a loader's QueueFinish mid-submission (Three Houses-style per-resource waits) sleeps forever. Other threads (suspend, diagnostics) cannot publish the submit thread's block; their waits resolve once the submission ends
+ up_wait_zero(&g_up_pending);''')]),
+ ]
+ for name, marker, pairs in P:
+ if marker in t:
+ skip(f'nvn_mtl.m {name} (already patched)'); continue
+ for old, new in pairs:
+ if old not in t: fail(f'{NVM}: patch "{name}" anchor missing: {old[:70]!r}')
+ t = t.replace(old, new, 1)
+ done(f'nvn_mtl.m {name}')
+ return t
+
def patch_main_m(t):
if 'romfs.idx' in t: return skip('main.m romfs.idx (already patched)')
old = (' for (NSString *d in @[[doc stringByAppendingPathComponent:@"romfs"], [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"romfs"]])\n'
@@ -405,6 +434,7 @@ MARKERS = {
'scripts/build_ipa_from_nsp.py': '--work-dir',
'dm/gfx/tools/shader_split.py': 'a backslash cannot sit inside an f-string expression',
'dm/ios/Sources/main.m': 'romfs.idx',
+ 'dm/gfx/nvn/nvn_mtl.m': 'the iOS save-load freeze of NX2Apple issue #4',
'dm/games/0100A00019DE0000/game.tsv': 'build v1 1.0.137848',
}
PATCHERS = {
@@ -414,6 +444,7 @@ PATCHERS = {
'scripts/build_ipa_from_nsp.py': patch_nsp,
'dm/gfx/tools/shader_split.py': patch_shader_split,
'dm/ios/Sources/main.m': patch_main_m,
+ 'dm/gfx/nvn/nvn_mtl.m': patch_nvn_mtl,
'dm/games/0100A00019DE0000/game.tsv': patch_hades2,
}