diff --git a/dm/games/0400486024DEC000/README.md b/dm/games/0400486024DEC000/README.md index 90cfcadbbe..14b770814d 100644 --- a/dm/games/0400486024DEC000/README.md +++ b/dm/games/0400486024DEC000/README.md @@ -105,6 +105,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F - [Shaders report which textures they read through a pool](../../../docs/reference/memory.md#shaders-report-which-textures-they-read-through-a-pool) - [Repeated GPU out-of-memory failures end the game](../../../docs/reference/memory.md#repeated-gpu-out-of-memory-failures-end-the-game) - [A GPU whose access macOS revoked stops the game](../../../docs/reference/rendering.md#a-gpu-whose-access-macos-revoked-stops-the-game): after a GPU hang or page faults (issue #1326), nothing was drawn for about 70 s before the game aborted, and in full screen WindowServer's watchdog killed it. +- [Render targets dead by overwrite (`DM_NVN_RT_RECLAIM`)](../../../docs/reference/rendering.md#render-targets-dead-by-overwrite-dm_nvn_rt_reclaim): the Kokiri Forest load's 3.2 GB of render targets over 74 MB of pool storage, 1.1-1.2 GB with it on. - [Texture IDs of one storage share one packaged texture](../../../docs/reference/nvn-objects.md#texture-ids-of-one-storage-share-one-packaged-texture) - [Finalizing a copy of a texture object](../../../docs/reference/nvn-objects.md#finalizing-a-copy-of-a-texture-object) - [Dynamic resolution that never moves, and draw passes that wait for each other](../../../docs/reference/frame-rate.md#dynamic-resolution-that-never-moves-and-draw-passes-that-wait-for-each-other) diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index 74c8068fb7..af590c10a3 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -1934,6 +1934,7 @@ uint64_t mtl_sub_commit(void) { g_cb = nil; g_commit_blocks = nil; objc_autoreleasePoolPop(g_sub_pool); g_sub_pool = NULL; + { void rc_tick_(void); rc_tick_(); } // DM_NVN_RT_RECLAIM return fence; } @@ -2219,6 +2220,156 @@ static alias_key tex_alias_key(const tex_t *t) { k.d.flags &= ~NVN_TEXTURE_FLAGS_IMAGE; return k; } +// ---------- DM_NVN_RT_RECLAIM (scratch: (c) of the render-target spike) ---------- +// A shared texture (an entry of g_alias) is dead by overwrite once other textures' render-pass writes have covered its whole storage range in its pool since its own last write: +// on the console its bytes now hold their pixels. Ocarina of Time's Kokiri load, f1,200: 716 of the 828 live render targets, 2,697 MB, 781 of them over 74 MB of one virtual pool. +// A dead entry leaves the table with its serials' mappings; its Metal texture goes to a free list and is given to the next texture of the same Metal descriptor, after the fence of +// its last use, zero-filled as a new one is; the list keeps a texture 15 frames. Writes this does not see (shader image stores, copies, clears outside a pass, uploads) exclude +// their textures instead: IMAGE textures, copy / clear destinations, readback sources, pool copies over their range, sparse pools and textures filled from pool bytes never go +int nvn_sparse_pool(const void *pool); +@interface RcFree : NSObject { @public id t; uint64_t fence, frame; } @end +@implementation RcFree @end +typedef struct { alias_key k; uintptr_t lo, hi; uint32_t ncov; int32_t bpos; uint8_t dead, excl, over, used; uintptr_t cov[8][2]; uint64_t dreads, dframes, dlast, dnext; } rc_ent; // dreads: ID reads while dead; dframes: frames with one; dnext: deaths followed by a read before the next write +static rc_ent *g_rc; static int g_rc_n, g_rc_cap; static nvn_hmap g_rc_idx = {.klen = sizeof(alias_key)}; // key → index + 1 +static int *g_rcb; static int g_rcb_n, g_rcb_cap; // indices of the entries the overlap scan visits (live, not reclaimed) +static nvn_hmap g_rc_gone = {.klen = 8}; // serial → 1 while its texture was reclaimed and it has not been written again +static NSMutableArray *g_rc_free; static pthread_mutex_t g_rc_mu = PTHREAD_MUTEX_INITIALIZER; static int g_rc_on_ = -1; +static uint64_t g_rc_deaths, g_rc_reclaims, g_rc_reuses, g_rc_trims, g_rc_ghost, g_rc_again, g_rc_sampled, g_rc_excl_n, g_rc_free_bytes; +static int rc_on(void) { if (g_rc_on_ < 0) { g_rc_on_ = getenv("DM_NVN_RT_RECLAIM") != NULL; if (g_rc_on_) MLOG("DM_NVN_RT_RECLAIM: render targets dead by overwrite are reclaimed (free list by descriptor, 15 frames)"); } return g_rc_on_; } +static void rcb_add(int i) { if (g_rc[i].bpos >= 0) return; if (g_rcb_n == g_rcb_cap) { g_rcb_cap = g_rcb_cap ? g_rcb_cap * 2 : 1024; g_rcb = realloc_or_die(g_rcb, g_rcb_cap * sizeof *g_rcb, "rc"); } g_rc[i].bpos = g_rcb_n; g_rcb[g_rcb_n++] = i; } +static void rcb_del(int i) { int p = g_rc[i].bpos; if (p < 0) return; int last = g_rcb[--g_rcb_n]; g_rcb[p] = last; g_rc[last].bpos = p; g_rc[i].bpos = -1; } +static int rc_get(const tex_t *t) { // caller holds g_rc_mu + alias_key k = tex_alias_key(t); uintptr_t ix = (uintptr_t)nvn_hm_get(&g_rc_idx, &k); + if (ix) return (int)ix - 1; + if (g_rc_n == g_rc_cap) { g_rc_cap = g_rc_cap ? g_rc_cap * 2 : 4096; g_rc = realloc_or_die(g_rc, g_rc_cap * sizeof *g_rc, "rc"); } + rc_ent *e = &g_rc[g_rc_n]; memset(e, 0, sizeof *e); e->k = k; e->bpos = -1; e->lo = (uintptr_t)t->mem; e->hi = e->lo + t->L.size; + e->excl = (t->d.flags & NVN_TEXTURE_FLAGS_IMAGE) || !t->mem || !t->L.size || (t->pool && nvn_sparse_pool(t->pool)); + nvn_hm_put(&g_rc_idx, &k, (void *)(uintptr_t)(g_rc_n + 1)); + return g_rc_n++; +} +static int rc_cover(rc_ent *u, uintptr_t a, uintptr_t b) { // adds [a, b) to u's coverage; 1 when its whole range is covered + if (a < u->lo) a = u->lo; if (b > u->hi) b = u->hi; if (a >= b || u->over) return 0; + uintptr_t out[9][2]; uint32_t n = 0; + for (uint32_t i = 0; i < u->ncov; i++) { uintptr_t x = u->cov[i][0], y = u->cov[i][1]; if (y < a || x > b) { out[n][0] = x; out[n][1] = y; n++; } else { if (x < a) a = x; if (y > b) b = y; } } + out[n][0] = a; out[n][1] = b; n++; + if (n > 8) { u->over = 1; return 0; } // too fragmented to follow: never counted dead until its next write + for (uint32_t i = 1; i < n; i++) for (uint32_t j = i; j > 0 && out[j][0] < out[j - 1][0]; j--) { uintptr_t s0 = out[j][0], s1 = out[j][1]; out[j][0] = out[j - 1][0]; out[j][1] = out[j - 1][1]; out[j - 1][0] = s0; out[j - 1][1] = s1; } + memcpy(u->cov, out, n * sizeof out[0]); u->ncov = n; + return n == 1 && u->cov[0][0] <= u->lo && u->cov[0][1] >= u->hi; +} +static void rc_reclaim(int i) { // caller holds g_rc_mu, not g_smu + rc_ent *e = &g_rc[i]; uint64_t keys[64]; int nk = 0; + pthread_mutex_lock(&g_smu); + __unsafe_unretained id m = hm_get_id(&g_alias, &e->k); + if (!m) { pthread_mutex_unlock(&g_smu); rcb_del(i); return; } + if (serial_holds(&g_ghost, m)) { g_rc_ghost++; pthread_mutex_unlock(&g_smu); return; } // a finalized serial a record may still replay: kept + for (uint32_t j = 0; j < g_by_serial.cap && nk < 64; j++) if (g_by_serial.vals[j] == (__bridge void *)m) memcpy(&keys[nk++], g_by_serial.keys + (size_t)j * g_by_serial.klen, 8); + pthread_mutex_unlock(&g_smu); + if (nk == 64) return; // more holders than followed: kept + for (int j = 0; j < nk; j++) { serial_del(keys[j]); nvn_hm_put(&g_rc_gone, &keys[j], (void *)(uintptr_t)(i + 1)); } + pthread_mutex_lock(&g_smu); + void *old = nvn_hm_del(&g_alias, &e->k); nvn_hm_del(&g_alias_seen, &e->k); + id o = old ? CFBridgingRelease(old) : nil; if (o && g_alias_set) CFSetRemoveValue(g_alias_set, (__bridge const void *)o); + pthread_mutex_unlock(&g_smu); + if (o && [o conformsToProtocol:@protocol(MTLTexture)]) { RcFree *f = [RcFree new]; f->t = o; f->fence = g_fence + 1; f->frame = atomic_load(&g_cur_frame); + if (!g_rc_free) g_rc_free = [NSMutableArray new]; [g_rc_free addObject:f]; g_rc_free_bytes += ((id)o).allocatedSize; g_rc_reclaims++; } + rcb_del(i); +} +// A render pass attaches t (OP_SET_RT): its own coverage restarts at once (written again: never dead, its IDs come back). It covers the entries it overlaps in its pool only when the +// pass ends (the next OP_SET_RT or the submission's end), and only if the pass's draws and clears covered the whole attachment (viewport ∩ scissor): a partial pass (dynamic resolution +// into a larger target, atlas tiles) leaves the bytes outside its area as they were, which block-linear layouts make non-contiguous, so it covers nothing +enum { RC_PEND = 9 }; +static tex_t g_rcp_t[RC_PEND]; static int g_rcp_n; static float g_rc_area[4]; static int g_rc_area_any; // area: x0, y0, x1, y1 of what the pass drew or cleared +static uint64_t g_rc_partial, g_rc_full; +static void rc_cover_others(const tex_t *t) { // caller holds g_rc_mu + int me = rc_get(t); rc_ent *e = &g_rc[me]; + uintptr_t lo = e->lo, hi = e->hi; uint64_t pool = e->k.pool; + for (int b = 0; b < g_rcb_n; b++) { int i = g_rcb[b]; rc_ent *u = &g_rc[i]; if (i == me || u->dead || u->k.pool != pool || u->hi <= lo || u->lo >= hi) continue; + if (rc_cover(u, lo, hi)) { u->dead = 1; g_rc_deaths++; if (!u->excl) { rc_reclaim(i); b--; } } } // rcb_del swapped another entry into slot b +} +static void rc_pass_end(void) { + if (!g_rcp_n) return; + pthread_mutex_lock(&g_rc_mu); + for (int j = 0; j < g_rcp_n; j++) { const tex_t *t = &g_rcp_t[j]; + int full = g_rc_area_any && g_rc_area[0] <= 0.5f && g_rc_area[1] <= 0.5f && g_rc_area[2] >= (float)t->d.width - 0.5f && g_rc_area[3] >= (float)t->d.height - 0.5f; + if (full) { g_rc_full++; rc_cover_others(t); } else g_rc_partial++; } + g_rcp_n = 0; g_rc_area_any = 0; + pthread_mutex_unlock(&g_rc_mu); +} +static void rc_area_add(float x0, float y0, float x1, float y1) { // a draw's viewport ∩ scissor, or a clear's area, in the pass's pixels + if (!g_rcp_n) return; + if (x0 > x1) { float x = x0; x0 = x1; x1 = x; } if (y0 > y1) { float y = y0; y0 = y1; y1 = y; } + if (!g_rc_area_any) { g_rc_area[0] = x0; g_rc_area[1] = y0; g_rc_area[2] = x1; g_rc_area[3] = y1; g_rc_area_any = 1; return; } + if (x0 < g_rc_area[0]) g_rc_area[0] = x0; if (y0 < g_rc_area[1]) g_rc_area[1] = y0; if (x1 > g_rc_area[2]) g_rc_area[2] = x1; if (y1 > g_rc_area[3]) g_rc_area[3] = y1; // the bounding box: separate rectangles that leave a hole count as covering it (rare for full-target passes) +} +static void rc_write(const tex_t *t) { + if (!t || !t->serial || !t->mem || !t->L.size || !rc_on()) return; + pthread_mutex_lock(&g_rc_mu); + int was_gone = nvn_hm_del(&g_rc_gone, &t->serial) != NULL; + int me = rc_get(t); rc_ent *e = &g_rc[me]; e->ncov = 0; e->dead = 0; e->over = 0; e->used = 1; e->dlast = 0; rcb_add(me); + if (g_rcp_n < RC_PEND) g_rcp_t[g_rcp_n++] = *t; + pthread_mutex_unlock(&g_rc_mu); + if (was_gone) ser_note(t->serial); // its IDs are taken again (pool tables, tex_by_id), now with a texture +} +static uint64_t g_rc_dead_reads, g_rc_hist_n, g_rc_hist_b; +int rc_dead_read(uint64_t serial) { + pthread_mutex_lock(&g_rc_mu); uintptr_t ix = (uintptr_t)nvn_hm_get(&g_rc_gone, &serial); int r = ix != 0; + if (r) { g_rc_dead_reads++; if (ix <= (uintptr_t)g_rc_n) { rc_ent *e = &g_rc[ix - 1]; uint64_t f = atomic_load(&g_cur_frame); e->dreads++; if (e->dlast != f + 1) { if (!e->dlast) e->dnext++; e->dframes++; e->dlast = f + 1; } + if (e->dnext > 4 && !e->excl) { e->excl = 1; g_rc_hist_n++; g_rc_hist_b += e->hi - e->lo; } } } // read before its next write after more than 4 deaths: history-like (luminance, bloom chains), kept from now on + pthread_mutex_unlock(&g_rc_mu); return r; +} +static void rc_dead_dump(void) { + enum { N = 12 }; int top[N]; int nt = 0; + pthread_mutex_lock(&g_rc_mu); + for (int i = 0; i < g_rc_n; i++) { if (!g_rc[i].dreads) continue; int j = nt < N ? nt++ : N - 1; if (j == N - 1 && nt == N && g_rc[top[j]].dreads >= g_rc[i].dreads) continue; top[j] = i; + for (; j > 0 && g_rc[top[j]].dreads > g_rc[top[j - 1]].dreads; j--) { int x = top[j]; top[j] = top[j - 1]; top[j - 1] = x; } } + char line[2400]; int L = 0; + for (int j = 0; j < nt && L < (int)sizeof line - 120; j++) { rc_ent *e = &g_rc[top[j]]; + L += snprintf(line + L, sizeof line - L, " [%ux%u f%d t%d off %#llx: %llu reads in %llu frames, %llu deaths read before a write]", e->k.d.width, e->k.d.height, e->k.d.format, e->k.d.target, (unsigned long long)e->k.off, + (unsigned long long)e->dreads, (unsigned long long)e->dframes, (unsigned long long)e->dnext); } + pthread_mutex_unlock(&g_rc_mu); + MLOG("DM_NVN_RT_RECLAIM: most read while dead:%s", line); +} +static void rc_excl(const tex_t *t) { // a copy, clear or readback touched t: never reclaimed + if (!t || !t->serial || !rc_on()) return; + pthread_mutex_lock(&g_rc_mu); int i = rc_get(t); if (!g_rc[i].excl) { g_rc[i].excl = 1; g_rc_excl_n++; } pthread_mutex_unlock(&g_rc_mu); +} +static void rc_excl_range(uint64_t a, uint64_t n) { // a pool copy over [a, a + n) + if (!n || g_rc_on_ != 1) return; + pthread_mutex_lock(&g_rc_mu); + for (int b = 0; b < g_rcb_n; b++) { rc_ent *u = &g_rc[g_rcb[b]]; if (!u->excl && u->lo < a + n && u->hi > a) { u->excl = 1; g_rc_excl_n++; } } + pthread_mutex_unlock(&g_rc_mu); +} +// The creation of a shared texture: one of the free list's of the same descriptor whose last use the GPU has finished, or nil +static id rc_take(MTLTextureDescriptor *d, const tex_t *t) { + if (g_rc_on_ != 1) return nil; + pthread_mutex_lock(&g_rc_mu); + if (nvn_hm_del(&g_rc_gone, &t->serial)) { g_rc_again++; g_rc_sampled++; } // resolved again before any render pass wrote it: sampled (or copied) first + id r = nil; uint64_t done = atomic_load(&g_done); + for (NSInteger j = (NSInteger)g_rc_free.count - 1; j >= 0 && !r; j--) { RcFree *f = g_rc_free[j]; id x = f->t; + if (f->fence > done || x.width != d.width || x.height != d.height || x.depth != d.depth || x.pixelFormat != d.pixelFormat || x.textureType != d.textureType || x.mipmapLevelCount != d.mipmapLevelCount + || x.sampleCount != d.sampleCount || x.arrayLength != d.arrayLength || x.usage != d.usage || x.storageMode != d.storageMode || x.allowGPUOptimizedContents != d.allowGPUOptimizedContents + || memcmp(&(MTLTextureSwizzleChannels){x.swizzle.red, x.swizzle.green, x.swizzle.blue, x.swizzle.alpha}, &(MTLTextureSwizzleChannels){d.swizzle.red, d.swizzle.green, d.swizzle.blue, d.swizzle.alpha}, sizeof(MTLTextureSwizzleChannels))) continue; + r = x; g_rc_free_bytes -= x.allocatedSize; [g_rc_free removeObjectAtIndex:(NSUInteger)j]; g_rc_reuses++; } + pthread_mutex_unlock(&g_rc_mu); + return r; +} +// After each submission: free-list textures unused for 15 frames go; counters every 150 frames +void rc_tick_(void) { + if (g_rc_on_ != 1) return; + rc_pass_end(); + static uint64_t last, lines; uint64_t f = atomic_load(&g_cur_frame); + if (f == last) return; + last = f; + pthread_mutex_lock(&g_rc_mu); + for (NSInteger j = (NSInteger)g_rc_free.count - 1; j >= 0; j--) { RcFree *x = g_rc_free[j]; if (f - x->frame >= 15) { g_rc_free_bytes -= x->t.allocatedSize; defer_obj_release(x->t); [g_rc_free removeObjectAtIndex:(NSUInteger)j]; g_rc_trims++; } } + unsigned long nfree = (unsigned long)g_rc_free.count; uint64_t fb = g_rc_free_bytes; + pthread_mutex_unlock(&g_rc_mu); + if (f % 600 == 0 && lines < 200) rc_dead_dump(); + if (f % 150 == 0 && lines++ < 200) MLOG("DM_NVN_RT_RECLAIM: frame %llu: deaths %llu, reclaimed %llu, reused %llu, trimmed %llu, kept for a ghost %llu, resolved again %llu (sampled before written %llu), ID reads while dead %llu, excluded %llu (history-like %llu / %llu MB); passes full / partial %llu / %llu; free list %lu / %llu MB; tracked %d, scanned %d", + (unsigned long long)f, (unsigned long long)g_rc_deaths, (unsigned long long)g_rc_reclaims, (unsigned long long)g_rc_reuses, (unsigned long long)g_rc_trims, (unsigned long long)g_rc_ghost, (unsigned long long)g_rc_again, + (unsigned long long)g_rc_sampled, (unsigned long long)g_rc_dead_reads, (unsigned long long)g_rc_excl_n, (unsigned long long)g_rc_hist_n, (unsigned long long)(g_rc_hist_b >> 20), (unsigned long long)g_rc_full, (unsigned long long)g_rc_partial, nfree, (unsigned long long)(fb >> 20), g_rc_n, g_rcb_n); +} static NvPkg *pkg_shared(const tex_t *t) { alias_key k = tex_alias_key(t); NvPkg *pk = nil; pthread_mutex_lock(&g_smu); NvPkgRef *r = hm_get_id(&g_pkg_share, &k); if (r) pk = r->pkg; pthread_mutex_unlock(&g_smu); @@ -3893,8 +4044,9 @@ static void sparse_view(NvPkg *pk, const tex_t *t) { MTLTextureDescriptor *d = tex_desc(t, 0); if (!d) return nil; mt = image_texture(t, d); - if (!mt) { mt = [g_dev newTextureWithDescriptor:d]; - if (mt) { res_add(mt); if (!tex_init_fill(t, mt)) tex_zero_fill(mt); } } + int filled = 0; + if (!mt) { mt = rc_take(d, t); if (!mt) mt = [g_dev newTextureWithDescriptor:d]; // rc_take: DM_NVN_RT_RECLAIM's free list + if (mt) { res_add(mt); if (!(filled = tex_init_fill(t, mt))) tex_zero_fill(mt); else rc_excl(t); } } if (!mt) { RED_ONCE(2, "failed to create MTLTexture %ux%u format %#x target %d", t->d.width, t->d.height, t->d.format, t->d.target); return nil; } } if (mt.allocatedSize >= (256u << 20)) { static int nb; if (nb++ < 20) // the textures that take most of the GPU's memory (a GPU out-of-memory error says nothing of which) @@ -5751,7 +5903,8 @@ static MTLTextureType ttype(int target, int fallback) { static int g_queue_origin_ll; // SET_WINDOW_ORIGIN's raster flip is queue state, independent of later device-origin changes. void mtl_set_queue_origin_lower_left(int on) { g_queue_origin_ll = on != 0; } void mtl_set_tex_pool_n(int n) { g_tpool_n = n; } -static id treg_tex(const treg_t *r) { return r->gone ? tex_made(&r->t) : tex_mtl(&r->t); } // Caller holds g_rmu +int rc_dead_read(uint64_t serial); // DM_NVN_RT_RECLAIM: a reclaimed serial's ID reads as an empty slot until a render pass writes it again (its bytes belong to the texture that overwrote it) +static id treg_tex(const treg_t *r) { return r->gone ? tex_made(&r->t) : (g_rc_on_ == 1 && rc_dead_read(r->t.serial)) ? nil : tex_mtl(&r->t); } // Caller holds g_rmu // The upload stamp of the packaged texture an ID's view was made from (tex_by_id's guest-write shortcut); none for sparse ones (their page table changes too) or another base. Caller holds g_rmu static void treg_pkg_stamp(treg_t *rp, id base) { rp->view_span = NULL; if (!rp->view_pkg) return; @@ -11929,6 +12082,12 @@ static void draw_(const p_draw *dw, const p_draw_idx *di) { g_mdxc.next = NULL; if (!S.prog) { RED_ONCE(18, "no program bound before the draw"); PD("Z %llu draw skipped: no program bound", g_stat_pass + !g_enc); return; } __unsafe_unretained NvProg *pr = g_geo_program ? g_geo_program : prog_get(S.prog); + if (g_rcp_n) { // DM_NVN_RT_RECLAIM: the area this draw can write (viewport 0 ∩ scissor; no viewport set: the whole target) + float x0 = -1e30f, y0 = -1e30f, x1 = 1e30f, y1 = 1e30f; + if (S.has_vp) { x0 = S.vp[0][0]; y0 = S.vp[0][1]; x1 = S.vp[0][0] + S.vp[0][2]; y1 = S.vp[0][1] + S.vp[0][3]; if (x0 > x1) { float x = x0; x0 = x1; x1 = x; } if (y0 > y1) { float y = y0; y0 = y1; y1 = y; } } + if (R.has_sc) { if (R.sc.x > x0) x0 = R.sc.x; if (R.sc.y > y0) y0 = R.sc.y; if (R.sc.x + R.sc.w < x1) x1 = R.sc.x + R.sc.w; if (R.sc.y + R.sc.h < y1) y1 = R.sc.y + R.sc.h; } + if (x1 > x0 && y1 > y0) rc_area_add(x0, y0, x1, y1); + } image_prepare(pr, 0x1f); draw_gs_check(pr); // a geometry stage left with no function (see prog_fold_gs) if (!g_geo_program && S.rast_discard && S.tf_on) { tf_draw(pr, dw, di); return; } @@ -12414,6 +12573,7 @@ static int rb_swap_gpu(uint64_t src, size_t row, size_t img, uint64_t dst, size_ // texture on a pool's buffer, which holds the guest's bits (linear and buffer textures) static int rb_copy_shift(const tex_t *t, id mt) { return mt.buffer ? 0 : rb_packed_shift(t->d.format); } static void copy_t2b(const p_copy_b2t *c, uint32_t size) { + if (g_rc_on_ == 1) rc_excl(c->tex); // DM_NVN_RT_RECLAIM: a copy, clear or readback touched it rp_flush(); // Materialize pending clears before reading, matching copy_t2t. g_wr_epoch++; g_mdx_epoch++; if (c->tex) xv_check(c->tex->serial, c->has_view && c->view.set_levels ? c->view.base_level : 0, c->has_view && c->view.set_layers ? c->view.min_layer : 0, "texture-to-buffer copy"); @@ -12603,6 +12763,7 @@ static void mirror_rb_land(void *p) { // the submission's completion handler free(m); } static void copy_b2t(const p_copy_b2t *c, uint32_t size) { + if (g_rc_on_ == 1) rc_excl(c->tex); // DM_NVN_RT_RECLAIM: a copy, clear or readback touched it if (c->tex) zskip_del(c->tex->serial); id t = tex_mtl(c->tex); sparse_gpu_wrote(c->tex); if (!t) { PD("C ser=%llu (no host texture)", c->tex->serial); return; } @@ -12787,6 +12948,7 @@ static void draw_tex(const p_draw_tex *c) { } // GPU: texture -> texture (previously mislisted as a no-op, 3 times per frame on the character select page). Same size, not mirrored, same format -> blit layer by layer; the other semantics (scaling with filtering, mirroring, format change) cannot be expressed by blit: report a failure, to be implemented static void copy_t2t(const p_copy_t2t *c) { + if (g_rc_on_ == 1) rc_excl(c->dst); // DM_NVN_RT_RECLAIM: a copy, clear or readback touched it if (c->src) xv_check(c->src->serial, c->has_sv && c->sv.set_levels ? c->sv.base_level : 0, c->has_sv && c->sv.set_layers ? c->sv.min_layer : 0, "texture copy source"); rp_flush(); // Land the pending pure-clear render pass first (zero-source candidates only enter the set when the render pass ends); the blit() below would end the render pass anyway if (c->dst) zskip_del(c->dst->serial); @@ -12998,6 +13160,7 @@ static unsigned mtl_pf_chmask(MTLPixelFormat pf) { // View with changed format: clear on that view. Partial colour regions / channel masks use a triangle and preserve everything else. static void clear_tex_tri(id t, unsigned lv, unsigned l0, const p_clear_tex *c); static void clear_tex(const p_clear_tex *c) { + if (g_rc_on_ == 1) rc_excl(c->tex); // DM_NVN_RT_RECLAIM: a copy, clear or readback touched it if (c->tex) zskip_del(c->tex->serial); id t = tex_mtl(c->tex); sparse_gpu_wrote(c->tex); PD("X ser=%llu view format %#x region %d,%d,%d %dx%dx%d mask %#x %s %g,%g,%g,%g", c->tex ? c->tex->serial : 0ull, c->has_view && c->view.set_format ? c->view.format : -1, c->r.xoffset, c->r.yoffset, c->r.zoffset, c->r.width, c->r.height, c->r.depth, c->mask, @@ -13318,13 +13481,14 @@ void mtl_exec(const uint8_t *p) { break; } case OP_CLEAR_BUF: { const p_clear_buf *cb = a; mtl_sub_fill(cb->addr, cb->size, cb->value); break; } case OP_COPY_B2B: { uint64_t s_, d, n; memcpy(&s_, a, 8); memcpy(&d, (const uint8_t *)a + 8, 8); memcpy(&n, (const uint8_t *)a + 16, 8); - PD("Y copy_b2b cmd=%p src=%#llx dst=%#llx bytes=%llu", p, (unsigned long long)s_, (unsigned long long)d, (unsigned long long)n); mtl_sub_copy(s_, d, n); break; } + PD("Y copy_b2b cmd=%p src=%#llx dst=%#llx bytes=%llu", p, (unsigned long long)s_, (unsigned long long)d, (unsigned long long)n); if (g_rc_on_ == 1) { rc_excl_range(s_, n); rc_excl_range(d, n); } mtl_sub_copy(s_, d, n); break; } case OP_UPDATE_UB: { uint64_t d, n; memcpy(&d, a, 8); memcpy(&n, (const uint8_t *)a + 8, 8); if (p == g_mdx_stop) { g_mdx_epoch++; g_mdx_stop = NULL; } // it writes a record or count that masked copies were made of (mdx_run) PD("Y update_ub cmd=%p src=%#llx dst=%#llx bytes=%llu f=%g,%g,%g,%g", p, (unsigned long long)((const uint8_t *)a + 16), (unsigned long long)d, (unsigned long long)n, n >= 4 ? ((const float *)((const uint8_t *)a + 16))[0] : 0.f, n >= 8 ? ((const float *)((const uint8_t *)a + 16))[1] : 0.f, n >= 12 ? ((const float *)((const uint8_t *)a + 16))[2] : 0.f, n >= 16 ? ((const float *)((const uint8_t *)a + 16))[3] : 0.f); mtl_sub_update((const uint8_t *)a + 16, d, n); break; } case OP_SET_RT: { const p_set_rt *r = a; + if (g_rc_on_ == 1) { rc_pass_end(); for (int i = 0; i < r->n && i < 8; i++) rc_write(r->color[i]); rc_write(r->depth); } else if (g_rc_on_) { for (int i = 0; i < r->n && i < 8; i++) rc_write(r->color[i]); rc_write(r->depth); } // DM_NVN_RT_RECLAIM if (defer_begin_q(p)) break; int pend = R.clear_d || R.clear_s; for (int i = 0; i < 8; i++) pend |= R.clear_c[i]; if (g_enc && !pend && R.has_rt && tbdr_on() && rt_range_same(&R.rt,r)) { @@ -13341,6 +13505,7 @@ void mtl_exec(const uint8_t *p) { } case OP_CLEAR_COLOR: { const p_clear_color *c = a; + if (g_rcp_n) { if (R.has_sc) rc_area_add(R.sc.x, R.sc.y, R.sc.x + R.sc.w, R.sc.y + R.sc.h); else rc_area_add(-1e30f, -1e30f, 1e30f, 1e30f); } // DM_NVN_RT_RECLAIM: a clear writes its scissor if (g_rc_skip) { PD("c %llu color clear skipped by conditional rendering slot%d", g_stat_pass, c->index); break; } // A scissored clear to 0 outside a pass with all channels (SMBW clears pyramid levels as sub-rectangles of 480×272 and other targets): copy from the zero buffer into the scissor rectangle in the replay stream if (!g_enc && c->mask == 0xf && !c->rgba[0] && !c->rgba[1] && !c->rgba[2] && !c->rgba[3] && R.has_sc && c->index >= 0 && c->index < R.rt.n && R.rt.color[c->index] @@ -13357,6 +13522,7 @@ void mtl_exec(const uint8_t *p) { break; } case OP_CLEAR_DS: { + if (g_rcp_n) { if (R.has_sc) rc_area_add(R.sc.x, R.sc.y, R.sc.x + R.sc.w, R.sc.y + R.sc.h); else rc_area_add(-1e30f, -1e30f, 1e30f, 1e30f); } // DM_NVN_RT_RECLAIM: a clear writes its scissor const p_clear_ds *c = a; if (g_rc_skip) { PD("c %llu depth-stencil clear skipped by conditional rendering", g_stat_pass); break; } if (g_rt_merged || g_sm_pass) rp_end(); diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index d1d6d34f35..e2b4b6fa70 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -612,6 +612,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - GPU memory grows with every image shown; the memory line's `shared textures` count keeps rising. → [Shared-storage entries taken over by a new texture](../reference/rendering.md#shared-storage-entries-taken-over-by-a-new-texture) - while the game makes many textures (Ocarina of Time's cutscenes), frames of hundreds of milliseconds up to a second, all of it in `submit+present` (the `[longframe]` lines); `sample` of the process names `alias_take_over` the submit thread's hottest function. → [A storage takeover walked the serial table for each entry](../reference/rendering.md#a-storage-takeover-walked-the-serial-table-for-each-entry) - GPU memory grows as resolution changes or rooms load, although almost every retained texture still has a live NVN descriptor; reclaiming unused sharing-table entries cannot recover it. → [Live resolution variants share physical image storage](../reference/rendering.md#live-resolution-variants-share-physical-image-storage) +- while a scene loads, the residency set grows by gigabytes of render targets, and wired memory and swap grow with it. Ocarina of Time's Kokiri Forest load on a 16 GB M2 Pro: the set went from 1.6 to 6.6 GB in about 30 s, 3.2 GB of it render targets (72 → 1,068 textures, 31 → 313 descriptors). Wired memory went from 3.0 to 9.8 GB and swap from 1.1 to 3.2 GB. → [Render targets dead by overwrite (`DM_NVN_RT_RECLAIM`)](../reference/rendering.md#render-targets-dead-by-overwrite-dm_nvn_rt_reclaim) - the whole picture came out darker and more saturated on the Mac and the iPhone: comparing with a Switch screenshot, every channel at every level equalled the Switch value decoded from sRGB once more (0.55 → 0.26, 0.75 → 0.52). → [Render-target views that change the format](../reference/rendering.md#render-target-views-that-change-the-format) - a movie (or another picture the game converts each frame) does not show: the screen keeps the last 3D frame while the decoder delivers every picture. A pass dump shows a pass writing one serial and the composite sampling another serial of the same size, the two Metal formats `RGBA8Unorm` (70) and `RGBA8Unorm_sRGB` (71). → [Textures of an sRGB / linear pair over the same storage](../reference/rendering.md#textures-of-an-srgb--linear-pair-over-the-same-storage) - a clear of, or a format-converting copy into, a texture made without render-target use is declined. → [Render-target use added on demand](../reference/rendering.md#render-target-use-added-on-demand) diff --git a/docs/env_switches.tsv b/docs/env_switches.tsv index 316dbbde07..a9a46a5553 100644 --- a/docs/env_switches.tsv +++ b/docs/env_switches.tsv @@ -176,6 +176,7 @@ DM_NVN_RP_AREA switch `0` makes passes on targets of 16 M pixels or more cover t DM_NVN_RT_CONTINUE switch `0` ends the pass whenever the game sets render targets, as before; by default SetRenderTargets naming the open pass's targets continues the pass DM_NVN_RT_RANGE switch `=1` (experimental, off by default; with `DM_NVN_TBDR=1`) keeps the pass open when the game sets the same colour and depth target again with a view that differs only in its level count DM_NVN_RT_RANGE_CHECK diagnostic With `DM_NVN_RT_RANGE=1` and `DM_NVN_PASSDUMP`, renders a candidate pass both ways in the same frame and logs and writes the comparison (`CHECK_RESULTS.tsv`, `[RED]` on a difference) +DM_NVN_RT_RECLAIM switch `=1` (off by default) reclaims render targets dead by overwrite: their Metal textures go to a free list keyed on the descriptor, and a reclaimed ID reads as an empty slot until a pass writes it again ([reference](reference/rendering.md#render-targets-dead-by-overwrite-dm_nvn_rt_reclaim)) DM_NVN_SHDUMP diagnostic `=` saves programs missing from the shader map as `.ctl` (control section) and `.code`, to find the files that hold them DM_NVN_SHMAP path The shader map (`shader_map.tsv`); `c1_full.tsv` and `pso_manifest.tsv` are taken from the same folder. Default: the game profile's folder on the Mac; the iOS app sets it DM_NVN_SHOT diagnostic `=[,…]:` saves the swapchain texture as an image at those frames (`%d` in the path = frame number) diff --git a/docs/reference/rendering.md b/docs/reference/rendering.md index 0ca09f4979..ffc1543928 100644 --- a/docs/reference/rendering.md +++ b/docs/reference/rendering.md @@ -328,6 +328,23 @@ The replay on Metal (`dm/gfx/nvn/nvn_mtl.m`) and the Vulkan and OpenGL layers. E - **Check:** `make -C dm/gfx/tests GAME=0100DCA0064A6000 check_t_image_alias` runs under Metal validation with direct and deferred encoding: 128 live descriptions retain their individual dimensions while sharing one allocation; small-first fresh storage, literal pixels across resolution changes, descriptor creation after a writer, indirect Metal 4 argument-buffer access, native integer-image atomics, depth/stencil clears and stencil views, pool/offset/format separation, excluded CPU-visible/sparse images and repeated destruction are checked. Integer-image matching restores the `ShaderAtomic` usage that Metal omits from its queried usage flags. - **Found on:** [Luigi's Mansion 3](../../dm/games/0100DCA0064A6000/README.md#texture-memory-2026-10-27), investigated from [#257](https://github.com/seedds/nxnative/issues/257) and [#100](https://github.com/seedds/nxnative/issues/100). The game's note records the live-allocation census and Mac comparison; other games and long iOS sessions remain unvalidated. +### Render targets dead by overwrite (`DM_NVN_RT_RECLAIM`) + +- **Symptom:** while a scene loads, the residency set grows by gigabytes of render targets, and wired memory and swap grow with it. Ocarina of Time's Kokiri Forest load on a 16 GB M2 Pro: the set went from 1.6 to 6.6 GB in about 30 s, 3.2 GB of it render targets (72 → 1,068 textures, 31 → 313 descriptors). Wired memory went from 3.0 to 9.8 GB and swap from 1.1 to 3.2 GB. +- **Cause:** the game keeps its render targets as texture objects over one 305 MB virtual pool and reuses the storage by overwrite. At f1,200 there were 781 live targets (2,788 MB of sizes) over 74 MB of storage. 716 of the 828 live ones (2,697 MB) had their whole storage range written by other targets since their own last write. On the console they cost only the pool's bytes. Here each one kept a private Metal texture, and `treg_tex` made one for every registered ID, dead or not. [Shared-storage entries taken over by a new texture](#shared-storage-entries-taken-over-by-a-new-texture) doesn't apply, because the objects stay live. [Live resolution variants share physical image storage](#live-resolution-variants-share-physical-image-storage) covers `COMPRESSIBLE|IMAGE` families at one offset, not render targets at moving offsets. +- **Fix (`DM_NVN_RT_RECLAIM=1`, off by default):** `nvn_mtl.m` (`rc_*`). A shared texture is dead once other textures' render passes have covered its whole storage range in its pool since its own last write. + - Coverage counts only passes whose draws and clears (viewport ∩ scissor) covered the whole attachment. A partial pass covers nothing: a third of Ocarina's attachment passes are partial. + - A dead texture leaves the sharing table along with its serials' mappings. + - Its Metal texture goes to a free list keyed on the full Metal descriptor. It is handed to the next texture of that descriptor after the fence of its last use, zero-filled as a new one is, and released after 15 idle frames. + - A reclaimed serial's texture ID reads as an empty slot until a render pass writes it again. + - Never reclaimed: IMAGE textures, copy and clear destinations, readback sources, anything a pool copy overlaps, sparse pools, textures filled from pool bytes, and textures read before their next write after more than 4 deaths (luminance and bloom chains: 429 textures, 324 MB in a run to f4,978). + - The descriptor key is deliberate. On a placement heap, a texture reads another's bytes only with the same width, height, bytes per pixel and compression (`dm/gfx/tests/probes/heap_alias.m`), so handing a texture over between other descriptors would need a clear anyway. +- **Check:** Ocarina of Time on the M2 Pro, to f7,500. Render targets in the set stay at 1.1–1.2 GB instead of 3.5 GB through the load, peak wired memory is 7.6–8.8 GB, and swap grows by 0.05–2.4 GB. Shots at f1,500 and f2,500 match a tree without it in scene and brightness (mean 127.4 / 96.3 against 128.9 / 96.1). `dm/gfx/tests/probes/rt_reclaim_sim.py` simulates the same on a logged write / release trace: about 3.5 GB to 1.2 GB. +- **Open:** + - A free list keyed on the allocation size (`heapTextureSizeAndAlign`) instead of the full descriptor would also reuse across dynamic-resolution steps: 855 steps at 2048×1152 give 21 distinct RGBA8 sizes (`dm/gfx/tests/probes/rt_sizes.m`). That needs a placement heap per size class, and a clear on every handover. + - Off by default until other games with render targets shared by storage (Wonder, Breath of the Wild, STEINS;GATE) are run with it. +- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md) (Switch 2, `0400486024DEC000`, v131072). + ### Render-target views that change the format - **Symptom:** the whole picture came out darker and more saturated on the Mac and the iPhone: comparing with a Switch screenshot, every channel at every level equalled the Switch value decoded from sRGB once more (0.55 → 0.26, 0.75 → 0.52).