From 65dacfa7c7ee4a4b7e104097ac547e89d5357dec Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:23:10 -0500 Subject: [PATCH 1/3] Ask the write watch once per run of regions in the texture pool's check, and pass released entries by. At every submission that binds the texture pools the replay checks each listed texture for CPU writes, and takes the write-watch snapshot of the 2 MiB regions that have none yet as the list comes to them: one query a region, since the list is in no address order. Standing still that is 5 to 10 queries and the check takes 0.11 ms. While the player runs through Kakariko or Castle Town the game writes about 32 textures into new storage every four or five frames, a region the CPU wrote is not frozen again for 1,800 submissions, and the check asked for 100 to 500 regions one by one, 2.7 times a frame: 0.5 to 1.5 ms a check, about 3 us a query, in five sessions of one player. The regions are now collected for the whole list first and asked in one query per run of adjacent regions. On this machine 400 queries of 2 MiB take 727 us and one query over the same 800 MiB takes 360 us. The same regions are queried and the same pages stamped, all of them before the list is looked at, as each entry's were before its own look; a run across the edge of a watched reservation is left to the queries by range. pool_presnap_runs = 0 asks region by region as before. A released entry stays in the list until it is destroyed, the check does nothing for it, and the stamp it compares stays as it was at the release, so one whose region took a write later missed the region check at every submission from then on and was fetched only to be passed by: 1,000 to 6,300 entries a check, with up to 8,192 released entries kept. The list now carries the released state, set at the release and cleared at the take-back, and the check passes such an entry by without touching it or asking for its regions. The pool log's freshness line counts the released entries passed by, and its snapshot queries count the queries either way. --probe-vkmem has a case for the queries by run. Co-Authored-By: Claude Fable 5.2 --- runtime/cfg.c | 2 +- runtime/host.h | 2 ++ runtime/memory.c | 2 ++ runtime/tex_probe.c | 47 ++++++++++++++++++++++++++++++++++++++- runtime/vk.h | 5 +++++ runtime/vk_memory.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ runtime/vk_texture.c | 41 ++++++++++++++++++++++++++++------ 7 files changed, 143 insertions(+), 9 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index a1057dd9..3c6ffad9 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -60,7 +60,7 @@ static const char *const known[] = { "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_FREECAM", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPELINE_CACHE_PLAY", "PIPELINE_CACHE_PLAY_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SEED", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "TEX_FLAT3D", "TEX_KEEP_TAKEN", "TEX_STALE_KEPT", "PRESENT_HELD", "HELD_SYNC", "PREPARE_PIPELINES", "PREWARM_FULL", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_REPLAY", "PROFILE_SLOW", - "POOL_FAST", "POOL_GROUP", "POOL_GROUP_CHECK", "POOL_PRESNAP", + "POOL_FAST", "POOL_GROUP", "POOL_GROUP_CHECK", "POOL_PRESNAP", "POOL_PRESNAP_RUNS", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "REFLEX", "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_PIPE", "REPLAY_RUNS", "REPLAY_WORKERS", "RTSS", "RTSS_ENUM", "SCENE_SHARE", "PRESNAP_PLACE", diff --git a/runtime/host.h b/runtime/host.h index 3b5928db..63fa2b9a 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -220,6 +220,8 @@ uint64_t mem_written_pages(uint64_t addr, uint64_t size, uint64_t *pages, uint64 uint64_t mem_written_peek(uint64_t addr, uint64_t size, uint64_t *pages, uint64_t max); // the same without the reset: the bits stay for the next // mem_written_pages (verify checks only) +int mem_watched(uint64_t addr, uint64_t size); // [addr, addr+size) lies inside one of those two ranges, so + // one mem_written_pages call can answer for all of it int mem_freeze(uint64_t a); // makes the 2 MiB region at a (2 MiB aligned, watched, // committed PAGE_READWRITE) read-only until a write or // mem_thaw thaws it; 1 frozen (or already), 0 refused diff --git a/runtime/memory.c b/runtime/memory.c index be2e0839..fa010e77 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -1203,6 +1203,8 @@ uint64_t mem_written_peek(uint64_t addr, uint64_t size, uint64_t *pages, uint64_ return written_pages(addr, size, pages, max, 0); } +int mem_watched(uint64_t addr, uint64_t size) { return size && watched(addr, size); } + static uint64_t image_bytes(void) { uint64_t n = 0; for (int i = 0; i < ldr_count(); i++) n += ldr_module(i)->size; diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index 3905c530..224ab2cb 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -411,6 +411,51 @@ static int test_import(pool_t *pool) { return 0; } +// The write-watch snapshot of many ranges at once (vk_memory.c vk_mem_snap_note_locked, vk_mem_snap_noted_locked), as +// the texture pool's freshness check asks for it: ranges in three adjacent 2 MiB regions of the heap and in one apart, +// noted in no address order, take one query per run of adjacent regions; afterwards every region has the epoch's +// snapshot, the regions with a CPU write since `since` say so, the one without does not, and the pages are exact. +// The bytes written are the ones already there, so the tests after this one find their memory as it was. +static int test_snap_runs(uint8_t *h) { + static const uint32_t off[4] = {0xe10000, 0xa80000, 0x603000, 0x9f0000}; // regions at 14, 10, 6 and 8 MiB + uint64_t base = (uintptr_t)h; + GCHECK(!(base & 0x1fffff), "the heap at %#llx does not start on a 2 MiB region", (unsigned long long)base); + int left = -1; + uint32_t g0, now; + vk_texture_new_submit(); + uint32_t ep = vk_texture_epoch(); + vk_mem_gen_lock(); + for (int i = 0; i < 4; i++) vk_mem_snap_note_locked(base + off[i], 0x2000, ep); + vk_mem_snap_noted_locked(ep, &left); // what was written before this test + vk_mem_gen_unlock(); + GCHECK(!left, "a run inside the heap was left to the queries by range"); + vk_mem_changed(base + 0x200000, 0x1000, 0, &g0); + volatile uint8_t *w[3] = {h + 0xe10010, h + 0xa80800, h + 0x604000}; + for (int i = 0; i < 3; i++) *w[i] = *w[i]; + vk_texture_new_submit(); + ep = vk_texture_epoch(); + vk_mem_gen_lock(); + for (int i = 0; i < 4; i++) vk_mem_snap_note_locked(base + off[i], 0x2000, ep); + for (int i = 0; i < 4; i++) vk_mem_snap_note_locked(base + off[i], 0x2000, ep); // a region noted twice is asked once + uint64_t q = vk_mem_snap_noted_locked(ep, &left), again = 0; + int why[4]; + for (int i = 0; i < 4; i++) { + why[i] = vk_mem_unchanged_why_locked(base + off[i], 0x2000, g0, ep); + again += vk_mem_snap_range_locked(base + off[i], 0x2000, ep); + } + vk_mem_gen_unlock(); + GCHECK(q == 2 && !left, "four regions in two runs took %llu queries (left %d)", (unsigned long long)q, left); + GCHECK(!again, "%llu regions had no snapshot after the queries by run", (unsigned long long)again); + GCHECK(why[0] == 3 && why[1] == 3 && why[2] == 3 && why[3] == 0, "regions written, written, written, not written " + "answer %d %d %d %d", why[0], why[1], why[2], why[3]); + GCHECK(vk_mem_changed_snap(base + 0x604000, 0x1000, g0, ep, &now) == 1 && + vk_mem_changed_snap(base + 0x603000, 0x1000, g0, ep, &now) == 0 && + vk_mem_changed_snap(base + 0x9f0000, 0x2000, g0, ep, &now) == 0, "the pages written are not reported exactly"); + printf("probe-vkmem: write-watch snapshot by runs: 4 regions in 2 runs took 2 queries; written regions and pages exact\n"); + g_gpu_checks += 5; + return 0; +} + // A GPU write is noted on a pool, and the pool is finalized and made again at its address before the write is // reported: the report must leave alone what the CPU has written to the new pool since. With shadow storage the new // pool's shadow is a copy of guest memory from before those writes and holds no byte of the old write @@ -1184,7 +1229,7 @@ static int test_gpu(void) { .size = 0x40000, .game_mem = h + 0x800000, .span = 0x40000}; pool_t virt = {.magic = NVN_POOL_MAGIC, .flags = 0x2a1, .mem = (uint8_t *)(uintptr_t)(RT_GPU_VA_BASE + 0x200000000ull), .size = 0x80000, .span = 0x80000}; - if (test_import(&pool) || test_readfile(pool.mem + 0x38000) || test_virtual(&phys, &virt)) return 1; + if (test_import(&pool) || test_readfile(pool.mem + 0x38000) || test_snap_runs(h) || test_virtual(&phys, &virt)) return 1; GCHECK(vk_pool_init(&tpool), "vk_pool_init of the texture pool"); if (test_textures(&tpool, &phys)) return 1; pool_t dpool = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = h + 0x500000, .size = 0x100000, .game_mem = h + 0x500000}; diff --git a/runtime/vk.h b/runtime/vk.h index 5c858e9f..001493dd 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -670,6 +670,11 @@ int vk_mem_unchanged_why_locked(uint64_t addr, uint64_t size, uint32_t since, ui void vk_mem_gen_lock(void); void vk_mem_gen_unlock(void); uint64_t vk_mem_snap_range_locked(uint64_t addr, uint64_t size, uint32_t epoch); +// The same snapshot for many ranges, with the lock held exclusively: note each range, then one call queries the noted +// regions that have no snapshot in epoch, one query per run of adjacent regions (the number of queries). *left: 1 +// when it left regions without one, for vk_mem_snap_range_locked. +void vk_mem_snap_note_locked(uint64_t addr, uint64_t size, uint32_t epoch); +uint64_t vk_mem_snap_noted_locked(uint32_t epoch, int *left); void vk_sampler_dlss_bias(float bias); // texture LOD bias added for DLSS upscaling (material samplers) void vk_sampler_aniso_live(void); // each present: NX2NAVI_ANISO after a settings change (tiled samplers) uint32_t vk_sampler_gen(void); // changes whenever a sampler slot is registered diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 1571b8b8..05b50924 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -1058,6 +1058,59 @@ uint64_t vk_mem_snap_range_locked(uint64_t addr, uint64_t size, uint32_t epoch) return 0; } +// The snapshot of many ranges at once, for the texture pool's freshness check (vk_texture.c +// vk_texture_pool_fresh_locked): vk_mem_snap_note_locked notes the regions of a range that have no snapshot in epoch +// yet, and vk_mem_snap_noted_locked queries what was noted, one query per run of adjacent regions. +// Asked range by range (vk_mem_snap_range_locked), every such region got a query of its own, because a texture rarely +// spans two regions and the list is in no address order. While the player runs through a town the game writes about 32 +// textures into new storage every four or five frames, a region the CPU wrote is not frozen again for freeze_idle +// submissions, and so 100 to 500 regions of the texture pools were queried one by one at every check, 2.7 checks a +// frame: the check took 0.5 to 1.5 ms against 0.11 ms standing still (5 to 10 queries), about 3 us a query (five +// sessions of one player, RTX 5090, 2026-11-12). About half of a 2 MiB query is the call itself: in a process of its +// own, 400 of them took 727 us and one query over the same 800 MiB 360 us, with 0, 8 or 64 pages written. +// The regions queried and the pages stamped are the same, and every one is stamped before any entry of the list is +// looked at, as before: each entry's regions were queried ahead of its own look. A run is asked in one query only where +// it lies inside one watched reservation (mem_watched): a query across the edge of one fails as a whole, so the regions +// of such a run are left to the queries by range, and *left says that there were some. +static uint32_t snap_noted[SNAP_REGIONS]; // gen_lock: snap_pass when the region is in snap_want +static uint32_t snap_pass = 1; +static uint32_t *snap_want; // gen_lock: the regions noted since the last vk_mem_snap_noted_locked +static int nsnap_want, csnap_want; + +void vk_mem_snap_note_locked(uint64_t addr, uint64_t size, uint32_t epoch) { + if (!size || !epoch) return; + uint64_t lo = addr & ~0xfffull, hi = (addr + size + 0xfff) & ~0xfffull; + uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; + if (r1 >= SNAP_REGIONS) return; + for (uint64_t r = r0; r <= r1; r++) { + if (snap_at[r] == epoch || snap_noted[r] == snap_pass) continue; + snap_noted[r] = snap_pass; + if (nsnap_want == csnap_want) grow((void **)&snap_want, &csnap_want, sizeof *snap_want); + snap_want[nsnap_want++] = (uint32_t)r; + } +} + +static int snap_want_cmp(const void *a, const void *b) { + uint32_t x = *(const uint32_t *)a, y = *(const uint32_t *)b; + return x < y ? -1 : x > y; +} + +uint64_t vk_mem_snap_noted_locked(uint32_t epoch, int *left) { + uint64_t calls = 0; + *left = 0; + if (nsnap_want > 1) qsort(snap_want, (size_t)nsnap_want, sizeof *snap_want, snap_want_cmp); + for (int i = 0, k; i < nsnap_want; i = k + 1) { + for (k = i; k + 1 < nsnap_want && snap_want[k + 1] == snap_want[k] + 1;) k++; + uint64_t r0 = snap_want[i], r1 = snap_want[k]; + if (r1 > r0 && !mem_watched(r0 << SNAP_SHIFT, (r1 - r0 + 1) << SNAP_SHIFT)) *left = 1; + else calls += snap_regions(r0, r1, epoch); + } + nsnap_want = 0; + if (!++snap_pass) memset(snap_noted, 0, sizeof snap_noted), snap_pass = 1; + n_snap_calls += calls; + return calls; +} + void vk_mem_snap_stats(uint64_t *checks, uint64_t *calls, uint64_t *untracked) { *checks = __atomic_exchange_n(&n_snap_checks, 0, __ATOMIC_RELAXED); *calls = __atomic_exchange_n(&n_snap_calls, 0, __ATOMIC_RELAXED); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index a033c995..0554dae7 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1440,10 +1440,15 @@ static void rel_unlink(Tex *e) { // that the check of an entry whose 2 MiB regions took no write since (vk_mem_unchanged_locked, one shared lock for the // whole list) touches neither the entry nor the lock: 5,600 entries took 0.77 ms a submission one by one. Texture lock // held. -typedef struct { Tex *e; uint64_t addr, size; uint32_t since; int ok; } PEnt; // ok: uploaded as of since +// rel is the entry's released state (mark_released, entry_of's take-back): the check does nothing for a released +// entry, and with the state here it does not touch the entry to find that out. On a card with room the sweep keeps up +// to 8,192 released entries, they stay listed, and since stays as it was at their release, so each one whose regions +// took a write later missed the region check at every submission from then on: 1,000 to 6,300 entries a check in +// Kakariko and Castle Town, a third to a half of the list. +typedef struct { Tex *e; uint64_t addr, size; uint32_t since; uint8_t ok, rel; } PEnt; // ok: uploaded as of since static PEnt *pent; static int npent, cpent, *pq, cpq; -static uint64_t n_pool_checks, n_pool_skips; +static uint64_t n_pool_checks, n_pool_skips, n_pool_rel; // The region check's misses (vk_mem_unchanged_why_locked): not uploaded, a region without this epoch's snapshot yet, one // untracked, one written since, nothing to check; and the snapshot queries its own pass made (NX2NAVI_POOL_PRESNAP). static uint64_t n_pool_miss[5], n_presnap_q; @@ -1534,7 +1539,7 @@ int vk_texture_pool_note_locked(int id, uint32_t *ent) { if (!p || !q) rt_fatal("vk: out of memory for the texture pool's entry list"); pent = p, pq = q, cpent = cpq = n; } - pent[npent++] = (PEnt){e, e->pub.addr, e->t.L.size, 0, 0}; + pent[npent++] = (PEnt){e, e->pub.addr, e->t.L.size, 0, 0, (uint8_t)e->released}; e->pidx = npent; } *ent = pool_ref(e); @@ -1553,6 +1558,14 @@ static int pool_presnap(void) { if (mode < 0) mode = rt_knob("POOL_PRESNAP", "1"); return RT_KNOB_ON(mode); } +// NX2NAVI_POOL_PRESNAP_RUNS=1 (default), 0, ab: those snapshots are taken for the whole list first, one write-watch +// query per run of adjacent regions (vk_memory.c vk_mem_snap_noted_locked), instead of one per region as the list comes +// to it. The log's "snapshot queries" of the pool freshness check count either. +static int pool_presnap_runs(void) { + static int mode = -1; + if (mode < 0) mode = rt_knob("POOL_PRESNAP_RUNS", "1"); + return RT_KNOB_ON(mode); +} static void pool_made(uint64_t t0); // below, with the pool's creation budget // The submission being recorded checked the pool's entries by this list: for the idle clock it walked the pool @@ -1561,13 +1574,24 @@ static int pool_by_list; int vk_texture_pool_fresh_locked(void) { int nq = 0, ps = pool_presnap(); + uint64_t rel0 = n_pool_rel; pool_by_list = 1; if (ps) vk_mem_gen_lock(); else vk_mem_gen_lock_shared(); + int by_range = ps; // each entry's regions are asked for as the list comes to it + if (ps && pool_presnap_runs()) { + for (int i = 0; i < npent; i++) + if (pent[i].ok && !pent[i].rel) vk_mem_snap_note_locked(pent[i].addr, pent[i].size, submit_epoch); + n_presnap_q += vk_mem_snap_noted_locked(submit_epoch, &by_range); + } for (int i = 0; i < npent; i++) { const PEnt *p = &pent[i]; + if (p->rel) { // released: nothing to check (the loop below would pass it by) + n_pool_rel++; + continue; + } if (p->ok) { - if (ps) n_presnap_q += vk_mem_snap_range_locked(p->addr, p->size, submit_epoch); + if (by_range) n_presnap_q += vk_mem_snap_range_locked(p->addr, p->size, submit_epoch); int why = vk_mem_unchanged_why_locked(p->addr, p->size, p->since, submit_epoch); if (!why) continue; n_pool_miss[why]++; @@ -1579,6 +1603,7 @@ int vk_texture_pool_fresh_locked(void) { if (ps) vk_mem_gen_unlock(); else vk_mem_gen_unlock_shared(); n_pool_skips += (uint64_t)(npent - nq); + n_pool_skips -= n_pool_rel - rel0; for (int k = nq; k-- > 0;) { // from the end: an entry leaving swaps the last one into its place int i = pq[k]; PEnt *p = &pent[i]; @@ -3909,6 +3934,7 @@ static Tex *entry_of(const tex_t *t) { uint64_t gap = e->samp_frame - e->rel_frame; if (gap > e->back_gap) e->back_gap = gap > UINT32_MAX ? UINT32_MAX : (uint32_t)gap; e->released = 0, nreleased--, rel_unlink(e), n_site[2]++, tex_gen_flip(e, e->superseded || e->overwritten); + if (e->pidx) pent[e->pidx - 1].rel = 0; // the pool's check looks at it again e->overwritten = 0; e->og_backs++, e->og_back_frame = e->samp_frame; if (e->superseded) { // its image may still be in use: the first barrier waits for all earlier work @@ -3971,6 +3997,7 @@ static int wb_wanted(const Tex *e) { return e->t.L.size <= wb_limit || e->run < static void mark_released(Tex *e) { // tex_lock held; e is not released flight_ev(e, FLIGHT_TEX_RELEASE, 0, 0); e->released = 1; + if (e->pidx) pent[e->pidx - 1].rel = 1; // the pool's check passes it by (vk_texture_pool_fresh_locked) e->rel_epoch = submit_epoch; e->rel_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); nreleased++; @@ -4365,11 +4392,11 @@ void vk_texture_bucket_stats(char *out, size_t n) { if (used < n) snprintf(out + used, n - used, "; pool freshness check: %llu entries checked, %llu passed by their regions (missed: " "%llu not uploaded, %llu a region without a snapshot yet, %llu untracked, %llu written since, %llu " - "nothing to check; %llu snapshot queries of NX2NAVI_POOL_PRESNAP)", + "nothing to check; %llu snapshot queries of NX2NAVI_POOL_PRESNAP; %llu released entries passed by)", (unsigned long long)n_pool_checks, (unsigned long long)n_pool_skips, (unsigned long long)n_pool_miss[0], (unsigned long long)n_pool_miss[1], (unsigned long long)n_pool_miss[2], (unsigned long long)n_pool_miss[3], - (unsigned long long)n_pool_miss[4], (unsigned long long)n_presnap_q); + (unsigned long long)n_pool_miss[4], (unsigned long long)n_presnap_q, (unsigned long long)n_pool_rel); used = strlen(out); if (used < n) snprintf(out + used, n - used, "; %d slot groups holding %llu slots", ngrp_live, (unsigned long long)n_grouped); @@ -4380,7 +4407,7 @@ void vk_texture_bucket_stats(char *out, size_t n) { if (used < n) snprintf(out + used, n - used, "; pool slots hold %d entries, %d of them GPU-owned (stamped in each submission " "checked by list)", held, gown); - n_bumps = n_undone = n_pool_checks = n_pool_skips = n_presnap_q = 0; + n_bumps = n_undone = n_pool_checks = n_pool_skips = n_presnap_q = n_pool_rel = 0; memset(n_pool_miss, 0, sizeof n_pool_miss); memset(n_site, 0, sizeof n_site); memset(bump_count, 0, sizeof bump_count); From a389f2450f98037597f5c5027e6e1cc966121b47 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:27:46 -0500 Subject: [PATCH 2/3] Add a switch that compares textures over 64 KiB by content before uploading them again, and hash eight chains at once. While the player runs through a town the game moves about 32 textures to new storage every four or five frames, back to back in one block, and a frame after their first upload every page of the block shows a write again. A texture up to 64 KiB keeps a hash of its storage and is uploaded again only when its bytes changed: of those, 0 to 4 in a hundred are (9 of 440 in one recording of 600 frames). A larger one has no hash and 75 to 94 in a hundred are uploaded again, whatever the size and format: 3,034 second uploads in those 600 frames beside 4,233 first ones, the ASTC ones decoded and encoded on the GPU a second time. The frames of the second uploads take 0.9 to 2.5 ms longer than frames without uploads, and the frames after them wait 0.3 to 1.3 ms longer for the GPU (six recordings of one player). tex_same_big = 1 has textures up to 8 MiB keep the hash too. It is off by default: the bytes are read once more at the first upload and once at the comparison, 15 us each for 300 KiB, which together is about what the second upload takes on the submitting thread (27 us), so what it gains is the GPU's work and whatever else a second upload costs its frame, and that is not measured in play. A texture whose comparisons found other bytes twice in a row takes no hash any more, and an area load takes none. The hash runs eight chains side by side and folds them, 20 GB/s against 7 for the one chain on a Ryzen 7 9850X3D, which the small textures' comparisons get as well. Each step is still a bijection of its chain and of the word it takes. The pool log's buckets line counts the hashed uploads and the comparisons when there are any. --probe-vkmem has a case that passes with the switch off and on: the same bytes written again, other bytes, and the same bytes after two rewrites in a row. Co-Authored-By: Claude Fable 5.2 --- runtime/cfg.c | 2 +- runtime/tex_probe.c | 33 ++++++++++++++++++ runtime/vk_exec_draw.c | 2 +- runtime/vk_texture.c | 76 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 105 insertions(+), 8 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 3c6ffad9..e77a1891 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -58,7 +58,7 @@ static const char *const known[] = { "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", - "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_FREECAM", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPELINE_CACHE_PLAY", "PIPELINE_CACHE_PLAY_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SEED", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "TEX_FLAT3D", "TEX_KEEP_TAKEN", "TEX_STALE_KEPT", "PRESENT_HELD", "HELD_SYNC", + "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_FREECAM", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPELINE_CACHE_PLAY", "PIPELINE_CACHE_PLAY_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SEED", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "TEX_SAME_BIG", "TEX_FLAT3D", "TEX_KEEP_TAKEN", "TEX_STALE_KEPT", "PRESENT_HELD", "HELD_SYNC", "PREPARE_PIPELINES", "PREWARM_FULL", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_REPLAY", "PROFILE_SLOW", "POOL_FAST", "POOL_GROUP", "POOL_GROUP_CHECK", "POOL_PRESNAP", "POOL_PRESNAP_RUNS", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index 224ab2cb..5fce16aa 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -973,6 +973,39 @@ static int test_textures(pool_t *tpool, pool_t *phys) { vk_texture_stats(NULL, NULL, &re1); #ifdef _WIN32 // elsewhere an import's pinning can mark pages written again without a CPU write: the count is not exact GCHECK(re1 == re0 + 1, "storage rewritten: %llu re-uploads (want 1)", (unsigned long long)(re1 - re0)); + // The same bytes written again, so that every page of the storage shows a write. The texture is over 64 KiB: with + // NX2NAVI_TEX_SAME_BIG it is compared by content and left as it is, without the switch it is uploaded again. Other + // bytes upload either way, and the image holds them. After two comparisons in a row that found other bytes its + // uploads take no hash any more, so the same bytes written again then upload with the switch too. + { + int big = rt_knob("TEX_SAME_BIG", "0") == 1; + volatile uint8_t *sm = (volatile uint8_t *)t.mem; + uint64_t re2, re3, re4, re5; + GCHECK(t.L.size > (64u << 10), "the RGBA8 texture's storage is %llu bytes, not over 64 KiB", (unsigned long long)t.L.size); + for (uint64_t o = 0; o < t.L.size; o += 0x1000) sm[o] = sm[o]; + if (read_back(&t, 0, lin, 1, "RGBA8 written again with its own bytes")) return 1; + vk_texture_stats(NULL, NULL, &re2); + GCHECK(re2 == re1 + (big ? 0 : 1), "the same bytes written again: %llu re-uploads (want %d)", + (unsigned long long)(re2 - re1), big ? 0 : 1); + if (fill_storage(&t, lin)) return 1; + for (unsigned lv = 0; lv < 3; lv++) + if (read_back(&t, lv, lin, 1, "RGBA8 rewritten after the comparison")) return 1; + vk_texture_stats(NULL, NULL, &re3); + GCHECK(re3 == re2 + 1, "other bytes after the comparison: %llu re-uploads (want 1)", (unsigned long long)(re3 - re2)); + if (fill_storage(&t, lin)) return 1; + if (read_back(&t, 0, lin, 1, "RGBA8 rewritten a second time in a row")) return 1; + vk_texture_stats(NULL, NULL, &re4); + GCHECK(re4 == re3 + 1, "other bytes a second time: %llu re-uploads (want 1)", (unsigned long long)(re4 - re3)); + for (uint64_t o = 0; o < t.L.size; o += 0x1000) sm[o] = sm[o]; + if (read_back(&t, 2, lin, 1, "RGBA8 written again with its own bytes after two rewrites")) return 1; + vk_texture_stats(NULL, NULL, &re5); + GCHECK(re5 == re4 + 1, "the same bytes after two rewrites in a row: %llu re-uploads (want 1: no hash is taken any " + "more)", (unsigned long long)(re5 - re4)); + printf("probe-vkmem: a texture over 64 KiB written again with its own bytes: %s (NX2NAVI_TEX_SAME_BIG=%d); other " + "bytes upload, and after two rewrites in a row the same bytes do too\n", + big ? "left as it is" : "uploaded again", big); + g_gpu_checks += 5; + } #endif if (read_back(&t, 1, lin, 0, "RGBA8 ReadTexels from storage")) return 1; diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index c2924a6d..99597a70 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2313,7 +2313,7 @@ static void pool_catch_up(uint64_t from, uint64_t to) { pool_flush(); PL.switches++; if (PL.switches % 3600 == 0) { // diagnosis: what invalidates the slots (vk_texture.c vk_texture_bucket_stats) - char bs[768]; + char bs[1024]; vk_texture_bucket_stats(bs, sizeof bs); rt_log("vk: texture pool buckets over 3600 switches: %s", bs); } diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 0554dae7..da5cedec 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -77,8 +77,9 @@ typedef struct Tex { uint8_t listed; // in owned[] uint8_t *snap; // a texture that fills no page: its storage bytes when the GPU write was recorded uint32_t wt_n; // WriteTexels into the storage since the last upload - uint64_t chash; // content_hash of the storage at its last upload (small textures, CHASH_MAX) + uint64_t chash; // content_hash of the storage at its last upload (chash_wanted) uint8_t chash_ok; // chash holds it + uint8_t chash_miss; // over CHASH_MAX: comparisons in a row that found other bytes (CHASH_MISSES) struct Tex *rel_prev, *rel_next; // while released: the list of released entries, oldest first ImgKey ik; // the image's creation parameters uint8_t reusable; // its image may go to a new texture once it is released (own memory, GPU-only content) @@ -152,6 +153,9 @@ static uint64_t n_uploads, n_reuploads; // Re-uploads since the last perf line (tex_lock): all, and those of textures a WriteTexels wrote since their last upload. static uint64_t ru_n, ru_bytes, ru_ns, ru_wt_n, ru_wt_bytes, ru_wt_ns; static uint64_t ru_same_n, ru_same_bytes; // re-uploads skipped: the pages were written, the texture's bytes were not +// The same for textures over CHASH_MAX (NX2NAVI_TEX_SAME_BIG), since the last pool log line: comparisons that found the +// bytes unchanged and their bytes, comparisons that found others, and uploads that took a hash with their bytes. +static uint64_t big_same_n, big_same_bytes, big_diff_n, big_hash_n, big_hash_bytes; // NX2NAVI_TEX_ALIAS (default on; =0: a new image over live storage starts empty): a new descriptor over the storage of a // live image that the GPU wrote gets that image's content (alias_copy). Counts since the last log line. static int tex_alias = -1; @@ -3043,10 +3047,47 @@ static void astc_check(const Tex *e, const uint8_t *raw, const VkAstcRegion *ar, // with data the CPU writes, or written with the same bytes), about 420 re-uploads a frame of 1.5 KiB each, and one // costs a staging buffer, a copy and a barrier, several microseconds, where hashing 1.5 KiB takes a fraction of one. // A CPU write during the hash sets the pages' write bits again, so the next check sees it. -enum { CHASH_MAX = 64 << 10 }; +// +// NX2NAVI_TEX_SAME_BIG=1 (default 0; ab): textures up to CHASH_BIG_MAX keep the hash too. While the player runs through +// a town the game moves about 32 textures to new storage every four or five frames, back to back in one block, and a +// frame after their first upload every page of the block shows a write again. Of the ones the hash covers, 0 to 4 in a +// hundred are uploaded again (128x128, 171x171, 256x256 BC1: 9 of 440 in one recording of 600 frames); of the others, +// whatever their size and format, 75 to 94 in a hundred (256x256 BC3, 341x341 BC1, everything from 512x512 up): 3,034 +// uploads in those 600 frames beside 4,233 first ones, their ASTC ones decoded and encoded on the GPU a second time. +// The frames of those second uploads take 0.9 to 2.5 ms longer than the frames without uploads and the frames after +// them wait 0.3 to 1.3 ms longer for the GPU (six recordings of one player, RTX 5090, 2026-11-12). +// What the hash costs is why the switch is off: the bytes are read once more at the first upload and once at the +// comparison, 15 to 18 us each for 300 KiB (17 to 20 GB/s, below), together about what the second upload takes on +// the submitting thread (27 us for 300 KiB). So the gain is the GPU's work and whatever else a second upload costs a +// frame, and that has not been measured in play. A texture the CPU really rewrites would pay the hash for nothing each +// time: after CHASH_MISSES comparisons in a row that found other bytes its uploads take no hash any more. An area load +// takes none either (vk_exec_pool_loading): it uploads gigabytes in a few frames. +// The pool log's buckets line counts the comparisons and the hashed bytes. +enum { CHASH_MAX = 64 << 10, CHASH_BIG_MAX = 8 << 20, CHASH_MISSES = 2 }; static uint64_t content_hash(const void *p, uint64_t n) { const uint8_t *b = p; uint64_t h = 0x9e3779b97f4a7c15ull ^ n, i = 0; + // Eight chains side by side over 64 bytes a step, folded into one: a chain's multiply waits for the one before it, + // so the chains, which wait for nothing of each other, read eight times what one reads in the same time. A step is + // a bijection of its chain for the word it takes and of the word for the chain, as the single chain's is, and so + // is each fold. Measured on textures read from memory (Ryzen 7 9850X3D, clang -O2): one chain 7 GB/s, four 10, + // eight 17 to 20, sixteen no more; a copy of the same bytes runs at 70. + if (n >= 512) { + uint64_t c[8]; + for (int k = 0; k < 8; k++) c[k] = h ^ (0xc2b2ae3d27d4eb4full * (uint64_t)(k + 1)); + for (; i + 64 <= n; i += 64) { + uint64_t w[8]; + memcpy(w, b + i, 64); + for (int k = 0; k < 8; k++) { + c[k] = (c[k] ^ w[k]) * 0xbf58476d1ce4e5b9ull; + c[k] ^= c[k] >> 29; + } + } + for (int k = 0; k < 8; k++) { + h = (h ^ c[k]) * 0x94d049bb133111ebull; + h ^= h >> 29; + } + } for (; i + 8 <= n; i += 8) { uint64_t w; memcpy(&w, b + i, 8); @@ -3189,6 +3230,16 @@ static int astc_copy_opaque(const Tex *e, uint8_t *dst, const uint8_t *src, uint return ok; } +// Whether an upload of e takes the hash of its storage: always up to CHASH_MAX; above it, up to CHASH_BIG_MAX, with +// NX2NAVI_TEX_SAME_BIG, outside an area load, and while the comparisons have not found other bytes CHASH_MISSES times +// in a row ("NX2NAVI_TEX_SAME_BIG" above content_hash). +static int chash_wanted(const Tex *e) { + static int mode = -1; + if (e->t.L.size <= CHASH_MAX) return 1; + if (mode < 0) mode = rt_knob("TEX_SAME_BIG", "0"); + return RT_KNOB_ON(mode) && e->t.L.size <= CHASH_BIG_MAX && e->chash_miss < CHASH_MISSES && !vk_exec_pool_loading(); +} + static void upload(Tex *e) { static volatile int32_t n_bad, n_undecoded, n_split; int no_mem = 0; // a part's ASTC decode had no memory: the texture is uploaded again later @@ -3205,12 +3256,13 @@ static void upload(Tex *e) { e->uploaded = 1; return; } - e->chash_ok = e->t.L.size <= CHASH_MAX; // the hash of what this upload copies (make_fresh_snap compares it) + e->chash_ok = (uint8_t)chash_wanted(e); // the hash of what this upload copies (make_fresh_snap compares it) if (e->chash_ok) { // read with the storage held mapped, as the copies below: the game unmaps a texture's memory on its own thread if (mem_read_hold(e->pub.addr, e->t.L.size)) { e->chash = content_hash((const void *)(uintptr_t)e->pub.addr, e->t.L.size); mem_read_release(); + if (e->t.L.size > CHASH_MAX) big_hash_n++, big_hash_bytes += e->t.L.size; } else { e->chash_ok = 0; } @@ -3481,15 +3533,20 @@ static int storage_rewritten(const Tex *e, uint32_t since, uint32_t *now) { // The pages of an uploaded texture show a write (or are not tracked): whether its own bytes are still those it was // uploaded with (upload, content_hash). WriteTexels since the upload always counts as a change. -static int same_content(const Tex *e) { +static int same_content(Tex *e) { static int on = -1; // NX2NAVI_TEX_SAME=0: every write to the pages uploads again (diagnosis) if (on < 0) { const char *v = cfg_get("NX2NAVI_TEX_SAME"); on = !(v && !strcmp(v, "0")); } - if (!on || !e->chash_ok || e->wt_n || e->t.L.size > CHASH_MAX) return 0; + if (!on || !e->chash_ok || e->wt_n) return 0; // chash_ok: the upload took the hash (chash_wanted) if (!mem_read_hold(e->pub.addr, e->t.L.size)) return 0; // unmapped since: not the same; the upload deals with it uint64_t h = content_hash((const void *)(uintptr_t)e->pub.addr, e->t.L.size); mem_read_release(); - if (h != e->chash) return 0; + int big = e->t.L.size > CHASH_MAX; + if (h != e->chash) { + if (big) big_diff_n++, e->chash_miss += e->chash_miss < 255; + return 0; + } ru_same_n++, ru_same_bytes += e->t.L.size; + if (big) big_same_n++, big_same_bytes += e->t.L.size, e->chash_miss = 0; return 1; } @@ -4398,6 +4455,13 @@ void vk_texture_bucket_stats(char *out, size_t n) { (unsigned long long)n_pool_miss[2], (unsigned long long)n_pool_miss[3], (unsigned long long)n_pool_miss[4], (unsigned long long)n_presnap_q, (unsigned long long)n_pool_rel); used = strlen(out); + if (used < n && big_hash_n + big_same_n + big_diff_n) // NX2NAVI_TEX_SAME_BIG + snprintf(out + used, n - used, "; textures over 64 KiB: %llu uploads hashed (%.1f MiB), after a write to their " + "pages %llu left as they were (%.1f MiB), %llu uploaded again", + (unsigned long long)big_hash_n, big_hash_bytes / 1048576.0, (unsigned long long)big_same_n, + big_same_bytes / 1048576.0, (unsigned long long)big_diff_n); + big_hash_n = big_hash_bytes = big_same_n = big_same_bytes = big_diff_n = 0; + used = strlen(out); if (used < n) snprintf(out + used, n - used, "; %d slot groups holding %llu slots", ngrp_live, (unsigned long long)n_grouped); int held = 0, gown = 0; From dee111f6be4be843e64c89ab2a050ba0a918b701 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:46:01 -0500 Subject: [PATCH 3/3] Bring --probe-vkmem's texture cases up to the rules the runtime has now. The probe ended in FAIL on an unchanged tree, so none of its cases after the texture slots ran. Three of its expectations were older than the rule they test; the runtime does what its comments say in each. A finalized slot of a CPU-written texture shows its own image while that image still holds the texture (stale_kept). The probe expected such a slot to resolve to nothing after its texture's release although another texture object kept the image current. It now expects the image with tex_stale_kept on and nothing with it off, and a case of its own covers the rule: after the finalize the slot shows the same image, bytes written to the storage afterwards do not reach it and nothing is uploaded; registered again, in a later submission, it holds the storage's bytes of then. A slot follows another descriptor's live texture at its storage only when that texture has the slot's size (follow_size_ok). The probe put a 32x32 texture under a 64x64 slot and expected the slot to follow it. It now checks both: a 64x64 texture of another format is followed, and under a 32x32 one the slot gets an image of its own descriptor. The texture detail cases and the case of an ASTC decode without scratch memory returned without a check whenever the image was neither RGBA8 nor BC3, which was meant for a device that samples ASTC itself. Since opaque ASTC textures are made as BC1 that held for every image of those cases: the probe printed their line and checked nothing. They read BC1 images now. The probe passes with the default settings (188 GPU checks; 164 when it last passed), with tex_stale_kept = 0, tex_same_big = 1, pool_presnap_runs = 0, shadow storage, ASTC images as BC3 and as RGBA8, and texture detail reduced and low. Co-Authored-By: Claude Fable 5.2 --- runtime/tex_probe.c | 110 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 12 deletions(-) diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index 5fce16aa..2f2d3110 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -809,11 +809,19 @@ static int detail_fill(const tex_t *t, unsigned layers, unsigned round) { return 0; } -// Whether level lv, layer ly of image (RGBA8 or BC3, w x h at that level) is `want` in every texel. +// The image formats of an ASTC texture whose levels detail_level_is can read: the RGBA8 decode, its BC3 encoding, and +// the BC1 one an opaque texture gets (vk_texture.c astc_reformat; these cases' blocks are opaque). An image in the ASTC +// format itself, where the device samples it, is not read here. +static int detail_readable(VkFormat f) { + return f == VK_FORMAT_R8G8B8A8_UNORM || f == VK_FORMAT_BC3_UNORM_BLOCK || f == VK_FORMAT_BC1_RGB_UNORM_BLOCK; +} + +// Whether level lv, layer ly of image (RGBA8, BC3 or BC1, w x h at that level) is `want` in every texel. static int detail_level_is(const VkTexImage *img, uint32_t lv, uint32_t ly, uint32_t w, uint32_t h, const uint8_t want[4]) { VkCtx *c = vk_ctx(); int bc = img->format == VK_FORMAT_BC3_UNORM_BLOCK || img->format == VK_FORMAT_BC3_SRGB_BLOCK; - size_t n = bc ? (size_t)((w + 3) / 4) * ((h + 3) / 4) * 16 : (size_t)w * h * 4; + int bc1 = img->format == VK_FORMAT_BC1_RGB_UNORM_BLOCK || img->format == VK_FORMAT_BC1_RGB_SRGB_BLOCK; + size_t n = bc || bc1 ? (size_t)((w + 3) / 4) * ((h + 3) / 4) * (bc ? 16 : 8) : (size_t)w * h * 4; VkBuffer rb; VmaAllocation ra; uint8_t *r = vk_staging(n, &rb, &ra); @@ -840,6 +848,15 @@ static int detail_level_is(const VkTexImage *img, uint32_t lv, uint32_t ly, uint memcpy(&e0, r + i + 8, 2), memcpy(&e1, r + i + 10, 2); same &= e0 == e && e1 == e; } + } else if (bc1) { // BC1: the endpoints at bytes 0 to 3, then 2 bits a texel. With equal endpoints the selectors 0 + // to 2 all give the colour; 3 is black in this format, so none may be 3. + uint16_t e = (uint16_t)((want[0] >> 3) << 11 | (want[1] >> 2) << 5 | want[2] >> 3); + for (size_t i = 0; i < n; i += 8) { + uint16_t e0, e1; + uint32_t sel; + memcpy(&e0, r + i, 2), memcpy(&e1, r + i + 2, 2), memcpy(&sel, r + i + 4, 4); + same &= e0 == e && e1 == e && !(sel & sel >> 1 & 0x55555555u); + } } else { for (size_t i = 0; i < n; i += 4) same &= !memcmp(r + i, want, 4); } @@ -862,7 +879,7 @@ static int detail_case(pool_t *pool, uint64_t off, uint64_t room, uint32_t size, if (detail_fill(&t, layers, round)) return 1; const VkTexImage *img = vk_texture_get(&t); GCHECK(img && img->mirrored, "ASTC %ux%u texture image", size, size); - if (img->format != VK_FORMAT_R8G8B8A8_UNORM && img->format != VK_FORMAT_BC3_UNORM_BLOCK) return 0; // native ASTC + if (!detail_readable(img->format)) return 0; // native ASTC GCHECK(img->skip == want_skip && img->levels == levels - want_skip && img->width == size >> want_skip && img->height == size >> want_skip && img->layers == layers, "ASTC %ux%u, %u levels, %u layers: image %ux%u with %u levels, %u layers, made without %u (want %u)", size, size, @@ -900,7 +917,7 @@ static int detail_retry(pool_t *pool, uint64_t off, uint64_t room) { const VkTexImage *img = vk_texture_get(&t); vk_astc_scratch_fail(0); GCHECK(img && img->mirrored, "the retry case's texture image"); - if (img->format != VK_FORMAT_R8G8B8A8_UNORM && img->format != VK_FORMAT_BC3_UNORM_BLOCK) return 0; // native ASTC + if (!detail_readable(img->format)) return 0; // native ASTC vk_texture_debug(img, &ups, &ch, &pw); GCHECK(vk_texture_upload_retries() == r0 + 1 && ups == 1, "an ASTC upload whose decode had no memory was not left for another try (%llu tries noted, %u uploads)", @@ -1046,8 +1063,15 @@ static int test_textures(pool_t *tpool, pool_t *phys) { vk_texture_register(6, &bn, NULL); vk_texture_release(&b); const VkTexImage *kept = vk_texture_by_id(6, NULL, NULL); - GCHECK(kept && kept->width == 64 && kept->height == 48 && !vk_texture_by_id(5, NULL, NULL), - "a release finalized slot 6 of another live texture, or left its own slot 5 current"); + GCHECK(kept && kept->width == 64 && kept->height == 48, "a release finalized slot 6 of another live texture"); + // Its own slot 5 is finalized. The texture's image still holds the texture (slot 6's texture keeps it current), so + // with the kept-stale rule the slot shows that image (test_stale_kept, vk_texture.c stale_kept); without the rule + // it resolves to nothing. + const VkTexImage *own5 = vk_texture_by_id(5, NULL, NULL); + if (rt_knob("TEX_STALE_KEPT", "1") == 1) + GCHECK(own5 && own5->image == kept->image, "finalized slot 5 does not show its texture's image, which is still current"); + else + GCHECK(!own5, "a release left its own slot 5 current"); vk_texture_release(&bn); // Once the live texture at its storage holds GPU content the storage bytes lack, the finalized CPU slot follows it. // The live texture is cleared on the GPU and noted as rendered into, as a draw into it would be; the slot's image @@ -1065,14 +1089,26 @@ static int test_textures(pool_t *tpool, pool_t *phys) { const VkTexImage *followed = vk_texture_by_id(5, NULL, NULL); GCHECK(followed && followed->image == live->image, "finalized CPU slot 5 does not follow the rendered texture at its storage"); GCHECK(image_is_color(followed->image, 16, 12, cleared), "the image of slot 5 does not hold the clear written into the live texture"); - // GPU-owned slots do follow the live image at their storage, even when its descriptor has changed. + // GPU-owned slots do follow the live image at their storage, even when its descriptor has changed, but only one of + // their own size (vk_texture.c follow_size_ok): the game's render targets share storage, and the live one at an + // address is often a target of another size that a frame laid out there. The 64x64 slot gets the image of a 64x64 + // texture of another format; once that one is released and a 32x32 one is live there, the slot gets an image of + // its own descriptor. (In the other order the slot's own entry is live again after the second step, and a slot + // whose own entry is live keeps it.) vk_texture_register(5, &g, NULL); vk_texture_release(&g); - tex_t g2 = make_tex(phys, 0, NVN_FORMAT_R32UI, 32, 32, 1, 5); + tex_t g2 = make_tex(phys, 0, NVN_FORMAT_R32UI, 64, 64, 1, 5); live = vk_texture_get(&g2); VkImage live_img = live ? live->image : VK_NULL_HANDLE; const VkTexImage *slot = vk_texture_by_id(5, NULL, NULL); - GCHECK(live && slot && slot->image == live_img, "slot 5 does not follow the live texture at its storage"); + GCHECK(live && slot && slot->image == live_img, "slot 5 does not follow the live texture of its size at its storage"); + vk_texture_release(&g2); + tex_t g3 = make_tex(phys, 0, NVN_FORMAT_R32UI, 32, 32, 1, 8); + live = vk_texture_get(&g3); + slot = vk_texture_by_id(5, NULL, NULL); + GCHECK(live && slot && slot->image != live->image && slot->width == 64 && slot->height == 64 && + slot->format == VK_FORMAT_R8G8B8A8_UNORM, "64x64 slot 5 follows the 32x32 texture at its storage, or has no " + "image of its own descriptor"); samp_par sp; memset(&sp, 0, sizeof sp); sp.minf = NVN_MIN_FILTER_LINEAR_MIPMAP_LINEAR, sp.magf = NVN_MAG_FILTER_LINEAR, sp.ws = sp.wt = sp.wr = NVN_WRAP_MODE_REPEAT; @@ -1080,8 +1116,8 @@ static int test_textures(pool_t *tpool, pool_t *phys) { vk_sampler_register(0, &sp); VkSampler s0 = vk_sampler_by_id(0), s1 = vk_sampler_for(&sp); GCHECK(s0 && s0 == s1 && !vk_sampler_by_id(1), "sampler slots"); - g_gpu_checks += 12; - vk_texture_release(&t), vk_texture_release(&b2), vk_texture_release(&g2); + g_gpu_checks += 14; + vk_texture_release(&t), vk_texture_release(&b2), vk_texture_release(&g2), vk_texture_release(&g3); for (int i = 0; i < 4; i++) free(lin[i]), free(bl[i]); free(gl[0]); uint64_t images, ups, reups; @@ -1119,6 +1155,56 @@ static void image_bytes(VkImage image, uint32_t w, uint32_t h, uint8_t *out) { vmaDestroyBuffer(c->vma, rb, ra); } +// The kept-stale rule (vk_texture.c stale_kept, slot_resolve; NX2NAVI_TEX_STALE_KEPT, default on). The slot of a +// texture the CPU writes, finalized with its texture, goes on showing that texture's own image while the image still +// holds the texture: the game finalizes ahead of the replay of the frames it recorded before. The image is sampled as +// it is, so bytes the storage gets after the finalize do not reach it and nothing is uploaded. With the switch off the +// slot resolves to nothing, which a draw shows as the stand-in. Either way a registration makes the descriptor current +// again, with the storage's bytes as they are then. That another descriptor made at the storage ends the rule is the +// "stale CPU slot" case of test_textures. +static int test_stale_kept(pool_t *tpool) { + int on = rt_knob("TEX_STALE_KEPT", "1") == 1; + uint8_t *ref[1] = {0}, *ref2[1] = {0}, got[16 * 16 * 4]; + tex_t k = make_tex(tpool, 0xc0000, 0x25, 16, 16, 1, 60); + if (fill_storage(&k, ref)) return 1; + vk_texture_register(9, &k, NULL); + const VkTexImage *own = vk_texture_by_id(9, NULL, NULL); + GCHECK(own && own->image && own->width == 16 && own->height == 16, "slot 9 does not resolve its registered texture"); + VkImage own_img = own->image; + image_bytes(own_img, 16, 16, got); + GCHECK(!memcmp(got, ref[0], sizeof got), "the image of slot 9 does not hold the storage's bytes"); + uint64_t up0, re0, up1, re1; + vk_texture_stats(NULL, &up0, &re0); + vk_texture_release(&k); + if (fill_storage(&k, ref2)) return 1; // what the game puts into the storage next + const VkTexImage *after = vk_texture_by_id(9, NULL, NULL); + vk_texture_stats(NULL, &up1, &re1); + if (on) { + GCHECK(after && after->image == own_img, "a finalized CPU slot does not show its texture's own image"); + image_bytes(own_img, 16, 16, got); + GCHECK(!memcmp(got, ref[0], sizeof got) && up1 == up0 && re1 == re0, "the kept image of a finalized slot took the " + "storage's later bytes (%llu uploads and %llu re-uploads since the finalize)", + (unsigned long long)(up1 - up0), (unsigned long long)(re1 - re0)); + } else { + GCHECK(!after && up1 == up0 && re1 == re0, "a finalized CPU slot resolves with NX2NAVI_TEX_STALE_KEPT=0"); + } + // In a later submission: an entry's storage is checked for writes once per submission (vk_texture_by_id_locked), + // and this one's was at the first lookup above. + vk_texture_new_submit(); + vk_texture_register(9, &k, NULL); + const VkTexImage *again = vk_texture_by_id(9, NULL, NULL); + GCHECK(again && again->image && again->width == 16 && again->height == 16, "slot 9 registered again does not resolve"); + image_bytes(again->image, 16, 16, got); + GCHECK(!memcmp(got, ref2[0], sizeof got), "slot 9 registered again does not hold the bytes the storage has now"); + vk_texture_release(&k); + free(ref[0]), free(ref2[0]); + printf("probe-vkmem: a finalized slot of a CPU-written texture: %s (NX2NAVI_TEX_STALE_KEPT=%d); registered again it " + "holds the storage's bytes of then\n", + on ? "its own image as it was, whatever the storage gets afterwards" : "nothing", on); + g_gpu_checks += on ? 6 : 5; + return 0; +} + // ---- two descriptors on one CPU-written storage (vk_texture.c "split fill", start_at) // D1 is a texture the CPU writes. D2 names the same storage with a texture flag for which the cache does not take the // content from the CPU (COMPRESSIBLE here), so a slot of D2 shows D1's image for as long as D1 has a live entry. @@ -1264,7 +1350,7 @@ static int test_gpu(void) { .size = 0x80000, .span = 0x80000}; if (test_import(&pool) || test_readfile(pool.mem + 0x38000) || test_snap_runs(h) || test_virtual(&phys, &virt)) return 1; GCHECK(vk_pool_init(&tpool), "vk_pool_init of the texture pool"); - if (test_textures(&tpool, &phys)) return 1; + if (test_textures(&tpool, &phys) || test_stale_kept(&tpool)) return 1; pool_t dpool = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = h + 0x500000, .size = 0x100000, .game_mem = h + 0x500000}; GCHECK(vk_pool_init(&dpool), "vk_pool_init of the texture detail pool"); int detail_bad = test_detail(&dpool);