From 277f01375aefaef695f97fc96866903f89c7ff72 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 11:01:44 -0500 Subject: [PATCH] Keep an overwritten texture that is still used. A texture another target was drawn over is marked overwritten, dropped 600 presents later, and offered to the image reuse first. Neither asked whether anything still reads it. In a player's recording the entry of a second descriptor at the scene target's address was dropped that way and made again in the same frame from a released entry there, whose bits are the scene in another format: one frame nearly black with its colours exchanged, brightness 17 against 102 around it. An overwritten entry a draw, a read or the pool's marks used within those 600 presents now stays, and the image reuse passes over any released entry used within the last 16; tex_overwritten_used = 0 drops them as before. Co-Authored-By: Claude Fable 5.2 --- runtime/cfg.c | 5 +++-- runtime/vk_texture.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index a1057dd9..5afba0e2 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -54,7 +54,7 @@ static const char *const known[] = { "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_HUE_CLIP", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_TONE_SLOPE", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", - "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "BUILD_FREE_MB", "BUILD_RESERVE_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "LINE_WIDTH", "POOL_SHOWN_FIRST", "TEX_REUSE_NAMED", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "GATE_WAIT_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MEM_NEED_MB", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", + "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "BUILD_FREE_MB", "BUILD_RESERVE_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "LINE_WIDTH", "POOL_SHOWN_FIRST", "TEX_REUSE_NAMED", "TEX_OVERWRITTEN_USED", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "GATE_WAIT_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MEM_NEED_MB", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOUSE_TRACE", "MOVIE_HOST", "MOVIE_PACE", "MOVIE_CLOCK", "TEST_HITCH", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", @@ -132,7 +132,7 @@ static const struct { const char *key, *section; } sections[] = { {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, {"PIPE_FRAME_WAIT_MS", "performance"}, {"PIPE_SKIP_STEADY", "performance"}, {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, - {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"POOL_SHOWN_FIRST", "performance"}, {"TEX_REUSE_NAMED", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"}, + {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"POOL_SHOWN_FIRST", "performance"}, {"TEX_REUSE_NAMED", "performance"}, {"TEX_OVERWRITTEN_USED", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"}, {"COMPUTE_KEEP", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_SPLIT_FILL", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"WATER_PROJECTIVE", "performance"}, @@ -274,6 +274,7 @@ static const CfgMeta metas[] = { {"LINE_WIDTH", "1", "0|1", RESTART}, // vk_pipeline.c key_lines {"POOL_SHOWN_FIRST", "1", "0|1", RESTART}, // vk_texture.c shown_take {"TEX_REUSE_NAMED", "0", "0|1", RESTART}, // vk_texture.c slot_names + {"TEX_OVERWRITTEN_USED", "1", "0|1", RESTART}, // vk_texture.c drop_overwritten {"GPL_FEEDBACK", "1", "0|1", RESTART}, // vk_pipeline.c gpl_feedback {"PIPE_FAST_WORKERS", "1", "0|1", RESTART}, // vk_pipeline.c lib_worker, fast_take {"FRAME_HOLD_MS", "120", "0..500", RESTART}, // vk_backend.c frame_hold diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index a033c995..190f5d2b 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -92,6 +92,7 @@ typedef struct Tex { uint64_t scan_frame; // one more than the frame vk_texture_gpu_written last looked for overwritten entries uint64_t ow_frame; // gfx_counters.frames when overwritten was set uint8_t overwritten; // released, and a texture at another address was drawn over its storage (overwrite_others) + uint64_t kept_frame; // drop_overwritten: the present (plus one) in which it was last left because it is used uint8_t in_gwin; // in gwin[] int pidx; // 1 + its index in pent[] (vk_texture_pool_note_locked); 0: not listed int ph; // 1 + its index in pref[] (pool_ref); 0: none @@ -674,6 +675,10 @@ static int tex_reuse_on(void) { } static void destroy_tex(Tex *e); +static int used_lately(const struct Tex *e, uint64_t now, uint64_t within); +// The presents a released entry must have gone unused before its image may go to another texture (create_tex): the +// pool's marks reach an entry a few presents after the draw that sampled it. +#define REUSE_UNUSED 16 // NX2NAVI_TEX_REUSE_NAMED (default 0; 1: as before, any released entry's image may go): a registered slot that is not // finalized names e's descriptor, so a shader can still sample e through the pool. The game finalizes a texture object @@ -1274,6 +1279,7 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { int n = 0; for (Tex *q = nvn_hm_get(&reuse, &e->ik); q && n < 128; q = q->ru_next, n++) { if (q->rel_epoch == submit_epoch || now - q->rel_frame < 2 || q->key.addr == k->addr) continue; + if (used_lately(q, now, REUSE_UNUSED)) continue; // still drawn with or sampled: its content is asked for uint64_t dist = q->key.addr > k->addr ? q->key.addr - k->addr : k->addr - q->key.addr; // not one a current slot still names (slot_names), asked only of a candidate that would be the best if (q->overwritten) { @@ -2350,14 +2356,41 @@ static int drop_stale(uint64_t keep, uint64_t *count, int why) { // returns th return dropped; } +// A draw, a read or the pool's marks used e within `within` presents of now (touch, vk_pool_marks_read). +static int used_lately(const Tex *e, uint64_t now, uint64_t within) { + return now - e->use_frame < within || now - e->samp_frame < within; +} + // Drops up to PRESSURE_STEP entries that were marked overwritten OVERWRITTEN_KEEP presents ago or earlier // (overwrite_others): the image reuse found no texture to give their images to in that time. +// Not one that was used since (NX2NAVI_TEX_OVERWRITTEN_USED=0: those too, as before): the mark says another target was +// drawn over the entry's storage, not that nothing reads the entry. In a player's recording the entry of a second +// descriptor at the scene target's address (format 0x40 beside the target's 0x3f, both 2400x1350) was dropped here +// and made again in the same frame, its content copied from a released entry at that address, whose bits are the +// scene in the other format. That frame's picture was nearly black with its colours exchanged (brightness 17 against +// 102 in the frames around it); the frames before and after were as usual. Whatever looks such an entry up after its +// drop makes it again that way, so an entry something still uses stays; so does one the image reuse would take +// (create_tex, REUSE_UNUSED). static void drop_overwritten(void) { + static int used_too = -1; + static uint64_t n_kept; + if (used_too < 0) used_too = !cfg_bool("NX2NAVI_TEX_OVERWRITTEN_USED"); uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); int dropped = 0; for (Tex *e = rel_head, *next; e && now - e->rel_frame >= OVERWRITTEN_KEEP && dropped < PRESSURE_STEP; e = next) { next = e->rel_next; if (!e->overwritten || now - e->ow_frame < OVERWRITTEN_KEEP) continue; + if (!used_too && used_lately(e, now, OVERWRITTEN_KEEP)) { + if (e->kept_frame != now + 1 && (!n_kept++ || !(n_kept & (n_kept - 1)))) // the first, then at powers of two + rt_log("vk: an overwritten texture stays, it is still used: %#llx fmt %#x %ux%u, last used %llu presents " + "ago (%llu so far; NX2NAVI_TEX_OVERWRITTEN_USED=0 drops such a texture)", + (unsigned long long)e->key.addr, e->t.d.format, e->t.d.width, e->t.d.height, + (unsigned long long)(now - (e->use_frame > e->samp_frame ? e->use_frame : e->samp_frame)), + (unsigned long long)n_kept); + e->kept_frame = now + 1; + e->ow_frame = now; // looked at again OVERWRITTEN_KEEP presents from now + continue; + } drop_tex(e, DROP_OVERWRITTEN); dropped++, n_over_drops++; }