|
| 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