Skip to content

Commit e221694

Browse files
Record the pinned Air Mario Kart texture upload backport
1 parent d24d7c2 commit e221694

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎dm/ios/Engines/NxNative/README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Patches for pinned iOS engine lineages
2+
3+
The signed iPhone and iPad engines can use different base sources and shader
4+
chains. Apply these patches to the source identified here. Keep the engine's
5+
existing generated shader resources; a launcher update should not change their
6+
chain or replace the game's saved data.
7+
8+
## Mario Kart World: file-backed texture uploads (Air 60.59)
9+
10+
The [mkw-neo upload gate](https://github.com/MenAtCafe/mkw-neo/commit/00d76d51560d)
11+
avoids wiring a large file-backed guest pool when only a packaged texture in
12+
that pool needs an upload. The Air variant checks the live guest reservation
13+
and stages only uploads that would otherwise wire at least 64 MiB and eight
14+
times the texture's size. Wrapped pools, small pools, non-packaged textures
15+
and other titles retain the original path. This retains the 60.56 compute
16+
archives for both Mario Kart World and Pokémon Legends: Z-A.
17+
18+
Apply `mkw-air-file-backed-texture-uploads.patch` to the Air 60.56 renderer
19+
with its chunked guest-file host (865cc35-derived), not to the shared engine
20+
whose host lacks file-backed guest memory. Source SHA-256 before patching:
21+
22+
- `dm/host/hle_svc.c`: `51f35785f230a7c3727f9f4ab7e4b839cd5633ff6c368e9a1b80a2e6dca50044`
23+
- `dm/gfx/nvn/nvn_mtl.m`: `2e311373518793777787f96d233c748a8fbb0fca8a17d3bc8d68e172d02ae40c`
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--- a/dm/host/hle_svc.c
2+
+++ b/dm/host/hle_svc.c
3+
@@ -75,6 +75,13 @@
4+
static int g_gf_tab[GF_MAX]; // the files, open for reading and writing
5+
static int g_gf_n; // how many; 0 while the guest is anonymous. Set by mem_init_once, whose pthread_once orders it before every reader
6+
static uint64_t g_gf_min_free; // DM_GUEST_FILE_MIN_FREE in bytes: the free space on the files' disk under which the game is stopped; 0 = never
7+
+// Report what was actually mapped, including anonymous fallback after a
8+
+// filesystem failure. This flag and reservation are immutable after boot.
9+
+int dm_guest_file_span(uint64_t a, uint64_t n) {
10+
+ uint64_t b = (uint64_t)(uintptr_t)g_as;
11+
+ return g_gf_n && a >= b && n <= g_reserve && a - b <= g_reserve - n;
12+
+}
13+
+
14+
15+
// Every second the free space on the files' disk (they all lie on one volume): under DM_GUEST_FILE_MIN_FREE the game is stopped (exit code 87; the memory watchdog's is 86, hle_nvn.c), before a full
16+
// disk leaves the kernel no room to write the guest's pages out, nor a phone's system any room. Every 10th second one line, [RED] under 2 GB free: what the disk cost so far and the footprint's parts
17+
--- a/dm/gfx/nvn/nvn_mtl.m
18+
+++ b/dm/gfx/nvn/nvn_mtl.m
19+
@@ -820,6 +820,15 @@
20+
for (int i = 0; i < g_npool; i++) if (addr >= g_pools[i].lo && addr + len <= g_pools[i].hi) return g_plast = i;
21+
return -1;
22+
}
23+
+extern int dm_guest_file_span(uint64_t addr, uint64_t len);
24+
+static uint64_t mtl_unwired_pool_bytes(uint64_t addr, uint64_t len) {
25+
+ if (len > UINT64_MAX - addr) return 0;
26+
+ pthread_mutex_lock(&g_mu);
27+
+ int i = pool_find(addr, len);
28+
+ uint64_t bytes = i >= 0 && !g_pools[i].buf ? g_pools[i].hi - g_pools[i].base : 0;
29+
+ pthread_mutex_unlock(&g_mu);
30+
+ return bytes;
31+
+}
32+
int mtl_addr_known(uint64_t addr, uint64_t len) {
33+
if (len > UINT64_MAX - addr) return 0;
34+
pthread_mutex_lock(&g_mu);
35+
@@ -2598,7 +2607,17 @@
36+
if (!d) return nil;
37+
d.usage |= keep & (MTLTextureUsageRenderTarget | MTLTextureUsagePixelFormatView);
38+
int rb_shift = rb_packed_shift(t->d.format); // the RGB565 family is repacked into Metal's order on the CPU (rb_packed_shift)
39+
- int direct = !rb_shift && stamp && direct_upload_on() && g_nvn_deswz_gpu && !(t->pool && nvn_sparse_pool(t->pool)) && nvn_deswz_gpu_direct_supported(&t->d);
40+
+ // mkw-neo 00d76d51560d: upload packaged CPU texture bytes without
41+
+ // wiring an otherwise unused, file-backed guest pool just to deswizzle.
42+
+ // Keep small or already wired pools on the GPU; keep pressure bursts
43+
+ // from pulling a much larger file range into residency for one texture.
44+
+ uint64_t unwired_bytes = 0;
45+
+ if (t->has_layout && !rb_shift && g_nvn_deswz_gpu && t->L.size &&
46+
+ !(t->pool && nvn_sparse_pool(t->pool)) && !strcasecmp(DM_GAME_ID, "0400C3F00006E000") &&
47+
+ dm_guest_file_span((uint64_t)t->mem, t->L.size))
48+
+ unwired_bytes = mtl_unwired_pool_bytes((uint64_t)t->mem, t->L.size);
49+
+ int keep_cpu = unwired_bytes >= (64ull << 20) && unwired_bytes / 8 >= t->L.size;
50+
+ int direct = !keep_cpu && !rb_shift && stamp && direct_upload_on() && g_nvn_deswz_gpu && !(t->pool && nvn_sparse_pool(t->pool)) && nvn_deswz_gpu_direct_supported(&t->d);
51+
if (direct) d.usage |= MTLTextureUsageShaderWrite | MTLTextureUsagePixelFormatView;
52+
id<MTLTexture> mt = reuse && reuse.pixelFormat == d.pixelFormat && reuse.width == d.width && reuse.height == d.height && reuse.mipmapLevelCount == d.mipmapLevelCount && reuse.textureType == d.textureType
53+
&& (reuse.usage & d.usage) == d.usage
54+
@@ -2619,8 +2638,13 @@
55+
}
56+
g_bp_up_bytes += total;
57+
void *mb; uint64_t mo;
58+
- int use_gpu = !rb_shift && !sparse && g_nvn_deswz_gpu && mtl_addr((uint64_t)t->mem, t->L.size, &mb, &mo); // a sparse pool's texture is gathered through its page table on the CPU
59+
- if (g_nvn_deswz_gpu && !use_gpu && !sparse && !rb_shift) RED_ONCE(40, "swizzled texture storage is not in a memory pool; falling back to the CPU");
60+
+ int use_gpu = !keep_cpu && !rb_shift && !sparse && g_nvn_deswz_gpu && mtl_addr((uint64_t)t->mem, t->L.size, &mb, &mo); // a sparse pool's texture is gathered through its page table on the CPU
61+
+ if (keep_cpu) {
62+
+ static unsigned guarded;
63+
+ if (guarded++ < 8) MLOG("MKW packaged texture: staging %llu bytes to avoid wiring a %llu MiB file-backed pool",
64+
+ (unsigned long long)t->L.size, (unsigned long long)(unwired_bytes >> 20));
65+
+ }
66+
+ if (!keep_cpu && g_nvn_deswz_gpu && !use_gpu && !sparse && !rb_shift) RED_ONCE(40, "swizzled texture storage is not in a memory pool; falling back to the CPU");
67+
if (use_gpu) {
68+
// The GPU reads the memory pool at execution time, which is closer to a real Switch than a snapshot at CPU upload time; the dirty-page stamp is still taken before encoding, and later writes still trigger a re-upload.
69+
up_begin();

0 commit comments

Comments
 (0)