fix(nvn): preserve shared pool textures and fix argument buffer reuse across shaders

- Remove alias_trim: idle timer eviction of shared textures destroyed CPU-uploaded terrain normal/blend maps and static model textures (such as Link's house door) after 180 idle frames
- Track g_last_sh per stage and require g_last_sh[stage] == sh for argument buffer reuse/patching, preventing stale argument buffers when switching between different shaders
- Revert idle trimming assertion in t_alias_takeover and clean up DM_NVN_ALIAS_IDLE docs
This commit is contained in:
Ruslan Kyrylyuk committed 2026-11-10 18:57:37 +01:00
1 parent 7bdd185630
commit 56e7ee16b6
3 files changed
+7 -55

No files matched your search

+7 -44
View File
@@ -1389,13 +1389,11 @@ void mtl_prepare(void) {
}
static void sub_cut_begin(void); static void ts_sub_begin(void), ts_boundary(void); static int ts_on(void);
static void gmem_commit_patch(void); // the raw pool table words of the arguments encoded so far follow the pools (gmem_slot_note)
static void alias_trim(void);
void mtl_sub_begin(void) {
mtl_init(); g_sub_serial++; g_seg = 0; seg_clk_put(g_sclk); g_sclk = NULL;
sub_cut_begin();
defer_drain();
rh_trim();
alias_trim();
defer_reset();
mtl_prepare(); // done by then in the submit entry; the replay tool (nvn_replay) reaches it here
warm_claim();
@@ -2686,45 +2684,6 @@ static void alias_take_over(const alias_key *nk) {
}
if (n) { ser_note(0); g_alias_taken += n; }
}
enum { ALIAS_TRIM_IDLE_DEFAULT = 180 };
static uint64_t g_alias_trimmed;
static uint32_t g_alias_trim_cursor;
static void alias_trim(void) {
if (!g_alias.n) return;
static int idle = -1;
if (idle < 0) {
const char *e = getenv("DM_NVN_ALIAS_IDLE");
idle = e ? atoi(e) : ALIAS_TRIM_IDLE_DEFAULT;
if (idle < 16) idle = 16;
}
uint32_t n = 0; alias_key ks[32];
pthread_mutex_lock(&g_smu);
uint32_t cap = g_alias.cap;
if (cap) {
uint32_t start = g_alias_trim_cursor % cap;
for (uint32_t step = 0; step < cap && n < 32; step++) {
uint32_t i = (start + step) % cap;
if (!g_alias.vals[i]) continue;
void *obj = g_alias.vals[i];
if (entry_holds(obj) || tex_object_pinned((__bridge id)obj)) continue;
alias_key k; memcpy(&k, g_alias.keys + (size_t)i * g_alias.klen, sizeof k);
uint64_t seen = (uint64_t)(uintptr_t)nvn_hm_get(&g_alias_seen, &k);
if (seen && g_fence + 1 - seen < (uint64_t)idle) continue;
ks[n++] = k;
}
g_alias_trim_cursor = (start + 256) % cap;
}
for (uint32_t j = 0; j < n; j++) {
void *old = nvn_hm_del(&g_alias, &ks[j]);
if (!old) continue;
nvn_hm_del(&g_alias_seen, &ks[j]);
id o = CFBridgingRelease(old);
if (g_alias_set) CFSetRemoveValue(g_alias_set, (__bridge const void *)o);
defer_obj_release(o);
}
if (n) { ser_note(0); g_alias_trimmed += n; }
pthread_mutex_unlock(&g_smu);
}
static __unsafe_unretained id serial_get(uint64_t k) { pthread_mutex_lock(&g_smu); __unsafe_unretained id o = hm_get_id(&g_by_serial, &k); pthread_mutex_unlock(&g_smu); return o; }
static void serial_put(uint64_t k, id o) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); if (alias_has(old)) defer_obj_unref(old); else defer_obj_release(old); } hm_put_id(&g_by_serial, &k, o); held_add((__bridge void *)o, 1); ser_note(k); pthread_mutex_unlock(&g_smu); }
static void serial_del(uint64_t k) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); if (alias_has(old)) defer_obj_unref(old); else defer_obj_release(old); hm_del_id(&g_by_serial, &k); ser_note(k); } pthread_mutex_unlock(&g_smu); } // nothing to drop: the texture ID caches stay valid
@@ -4579,7 +4538,7 @@ void mtl_mem_stat(unsigned long long frame, unsigned long long rss_mb) {
#if TARGET_OS_IPHONE
snprintf(avail, sizeof avail, "%llu MB", (unsigned long long)(os_proc_available_memory() >> 20));
#endif
MLOG("memory up to frame %llu: resident %llu MB, GPU allocated %llu MB, deferred-release queue %lu, residency set %lu, serial objects %u, memory pools %d (resident %d; peak %d / tracking table capacity %d; freed but alive %lu, %llu MB), shared textures %u (%u of them unreferenced; evicted with pools %llu, on storage takeover %llu, trimmed %llu in total), texture re-uploads %llu in total, per-frame arena blocks created %llu in total, finalized textures still referenced %u (small table, %llu put in total), retired but held by a serial %u, recycled render-target heaps made %llu, reused %llu, freed %llu, waiting %u (%llu MB); headroom to the system memory limit %s", frame, rss_mb, gpu, nd, nr, ns, live, pres, peak, cap, dead_n, dead_mb, ashared, aorph, (unsigned long long)g_alias_evicted, (unsigned long long)g_alias_taken, (unsigned long long)g_alias_trimmed, (unsigned long long)g_pkg_reuploads, (unsigned long long)g_arena_made, nghost, (unsigned long long)ghost_puts, nwait,
MLOG("memory up to frame %llu: resident %llu MB, GPU allocated %llu MB, deferred-release queue %lu, residency set %lu, serial objects %u, memory pools %d (resident %d; peak %d / tracking table capacity %d; freed but alive %lu, %llu MB), shared textures %u (%u of them unreferenced; evicted with pools %llu, on storage takeover %llu in total), texture re-uploads %llu in total, per-frame arena blocks created %llu in total, finalized textures still referenced %u (small table, %llu put in total), retired but held by a serial %u, recycled render-target heaps made %llu, reused %llu, freed %llu, waiting %u (%llu MB); headroom to the system memory limit %s", frame, rss_mb, gpu, nd, nr, ns, live, pres, peak, cap, dead_n, dead_mb, ashared, aorph, (unsigned long long)g_alias_evicted, (unsigned long long)g_alias_taken, (unsigned long long)g_pkg_reuploads, (unsigned long long)g_arena_made, nghost, (unsigned long long)ghost_puts, nwait,
(unsigned long long)g_rh_made, (unsigned long long)g_rh_reused, (unsigned long long)g_rh_trimmed, rh_n, (unsigned long long)(rh_bytes >> 20), avail);
{ void mtl_vc_stats(unsigned *, uint64_t *, uint64_t *), mtl_tb2_stats(uint64_t *, uint64_t *, uint64_t *, uint64_t *, uint64_t *); uint64_t mtl_tex_mis_count(void); unsigned vn; uint64_t vh, vm, tb, tm, tf, tc, tcb;
mtl_vc_stats(&vn, &vh, &vm); mtl_tb2_stats(&tb, &tm, &tf, &tc, &tcb);
@@ -10319,7 +10278,8 @@ static int fb_image_view(int tid, const nvn_rbind *b, id<MTLTexture> t) {
}
static uint8_t g_args_dirty[6] = {1, 1, 1, 1, 1, 1};
static uint64_t g_args_reg_seen, g_args_dirty_seen, g_args_ser_seen;
static inline void args_dirty_all(void) { memset(g_args_dirty, 1, sizeof g_args_dirty); }
static __unsafe_unretained NvShader *g_last_sh[6];
static inline void args_dirty_all(void) { memset(g_args_dirty, 1, sizeof g_args_dirty); memset(g_last_sh, 0, sizeof g_last_sh); }
// A reused block holds resource IDs: it is stale after a registration change, a guest write, or a serial's Metal object replaced (g_ser_gen: an upgraded or re-uploaded packaged texture,
// an alias taken or evicted) with the same program and bindings (MacNativeGames e49aa55)
static inline void args_sync_dirty(void) {
@@ -10721,9 +10681,10 @@ static int encode_args_(id<MTLCommandEncoder> en, NvShader *sh, int nvn_stage, i
if (nvn_stage == 1 && !metal_vertex && sh->has_logic && lop_on()) lo_bind(en);
if (!sh->ae) return 1;
args_sync_dirty();
int reusable = !passdump() && !g_args_dirty[nvn_stage] && sh->ab_buf && sh->ab_pass == g_pass.serial && nvn_stage != 5 && !(sh->rf.drv_read[0] && sh->ab_binst != g_draw_binst) && !((sh->rf.pool || sh->rf.htab) && sh->ab_pgen != g_psnap_gen);
int reusable = !passdump() && (g_last_sh[nvn_stage] == sh) && !g_args_dirty[nvn_stage] && sh->ab_buf && sh->ab_pass == g_pass.serial && nvn_stage != 5 && !(sh->rf.drv_read[0] && sh->ab_binst != g_draw_binst) && !((sh->rf.pool || sh->rf.htab) && sh->ab_pgen != g_psnap_gen);
if (reusable && !g_args_ubo[nvn_stage]) {
bind_args(en, nvn_stage, metal_vertex, sh->ab_buf, sh->ab_gpu, sh->ab_off, sh->rf.abi);
g_last_sh[nvn_stage] = sh;
g_stat_args_reuse++;
return 1;
}
@@ -10737,6 +10698,7 @@ static int encode_args_(id<MTLCommandEncoder> en, NvShader *sh, int nvn_stage, i
else if (b->kind == NVN_RB_CBSIZE) p[b->aid] = cbsize_table(nvn_stage, &use); }
bind_args(en, nvn_stage, metal_vertex, ab, g_cur_arena->gpu + off, off, sh->rf.abi);
sh->ab_buf = ab; sh->ab_gpu = g_cur_arena->gpu + off; sh->ab_off = off; g_args_ubo[nvn_stage] = 0; // pass, base instance and pool generation are the ones the copied block was encoded for
g_last_sh[nvn_stage] = sh;
g_stat_args_patch++;
return 1;
}
@@ -11010,6 +10972,7 @@ static int encode_args_(id<MTLCommandEncoder> en, NvShader *sh, int nvn_stage, i
if (gmem_aid >= 0) gmem_slot_note((uint64_t *)(void *)(g_cur_arena->cpu + off) + gmem_aid, gml_taken ? (__bridge void *)g_gml_last : NULL); // the arena arena_alloc took it from (no contents message: see ubo_read)
bind_args(en, nvn_stage, metal_vertex, ab, g_cur_arena->gpu + off, off, sh->rf.abi);
sh->ab_buf = ab; sh->ab_gpu = g_cur_arena->gpu + off; sh->ab_off = off; sh->ab_pass = g_pass.serial; sh->ab_binst = g_draw_binst; sh->ab_pgen = g_psnap_gen; g_args_dirty[nvn_stage] = 0; g_args_ubo[nvn_stage] = 0;
g_last_sh[nvn_stage] = sh;
g_stat_args_enc++;
return 1;
}
-10
View File
@@ -73,15 +73,5 @@ int main(void) { @autoreleasepool {
mtl_tex_release(&b); mtl_tex_release(&c);
next_submission(); defer_drain();
// Idle eviction of unreferenced shared textures with no takeover
tex_t d = tex(91004, NVN_FORMAT_RGBA8, 64);
mtl_tex_init(&d);
id<MTLTexture> td = tex_mtl(&d);
CHECK(td && shared(td) && held(td), "d is shared and held");
mtl_tex_release(&d);
CHECK(!held(td) && shared(td), "d is no longer held, but still shared before idle timeout");
for (int i = 0; i <= 20; i++) next_submission();
CHECK(!shared(td), "unreferenced shared texture with no overlapping storage is trimmed after idle timeout");
printf("t_alias_takeover: %d checks, %d failed\n", checks, failed); return failed != 0;
} }
-1
View File
@@ -115,7 +115,6 @@ DM_NICK switch The account nickname the game sees (default `Player`)
DM_NRO switch `0` turns off loading of the NRO modules the game's NRR files register (on by default): every `nn::ro::LoadModule` then fails
DM_NRO_CAPTURE diagnostic folder: a `nn::ro::LoadModule` with no rewritten copy writes the image to `<folder>/<SHA-256>.nro` and ends the run (the NSP script's capture of an NRO the game decodes at run time)
DM_NRO_DIR path folder of NROs the shader split reads like `main`, and in which it finds a CRA archive's file table (the NSP script sets it for a captured NRO)
DM_NVN_ALIAS_IDLE switch `=<frames>`: frames without reference before an idle alias texture is released (default 180, minimum 16)
DM_NVN_ALLOCPROF diagnostic `=1` charges Metal allocation growth to each replayed op type and function, and logs command allocator sizes and each new texture (serial, size, format, storage); `=2` also gives each main submission a fresh command allocator and reports command memory by operation, by encoder, by submission size and the indirect-count multi-draws encoded
DM_NVN_API switch `=<major>.<minor>` overrides the NVN API version `nvnDeviceGetInteger` reports (by default decided by the SDK version or the profile's `device_api`: 55.15 for SDK 11 and later, 53.313 before)
DM_NVN_BARRIER switch How draw passes are ordered: `full` (default) waits for all earlier GPU work, `game` only for the previous pass's fragment stage unless the game set a barrier, `ab` alternates with the `DM_AB` phases
Can't render this file because it contains an unexpected character in line 20 and column 104.