From 53ae6c192ec6a9505c394cc7d64b5c2fe94cb4fb Mon Sep 17 00:00:00 2001 From: DeepDaddyTTV <32502468+DeepDaddyTTV@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:27:38 -0500 Subject: [PATCH 01/16] Stop scanning shader instructions after descriptor declarations have been read. --- runtime/vk_pipeline.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index dcd39267..3888b7c8 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -1125,7 +1125,7 @@ static uint32_t *spirv_rebind(const uint32_t *w, uint64_t n, uint32_t off) { memcpy(o, w, n * 4); for (uint64_t i = 5; i < n;) { uint32_t op = o[i] & 0xffff, len = o[i] >> 16; - if (!len || i + len > n) break; + if (!len || i + len > n || op == SPV_OP_FUNCTION) break; if (op == SPV_OP_DECORATE && len >= 4 && o[i + 2] == SPV_DEC_BINDING && o[i + 3] >= 1) o[i + 3] += off; i += len; } @@ -1139,7 +1139,7 @@ static uint32_t *spirv_set(const uint32_t *w, uint64_t n, uint32_t set) { memcpy(o, w, n * 4); for (uint64_t i = 5; i < n;) { uint32_t op = o[i] & 0xffff, len = o[i] >> 16; - if (!len || i + len > n) break; + if (!len || i + len > n || op == SPV_OP_FUNCTION) break; if (op == SPV_OP_DECORATE && len >= 4 && o[i + 2] == SPV_DEC_DESCRIPTOR_SET) o[i + 3] = set; i += len; } @@ -1546,10 +1546,11 @@ static uint32_t *spirv_pool_rebind(const uint32_t *w, uint64_t n) { uint32_t *cval = calloc(bound, sizeof *cval); if (!kind || !cval) return free(kind), free(cval), NULL; memset(kind, 0xff, bound * sizeof *kind); + // Pool variables and their binding decorations are module declarations, all before the first function. int any = 0; for (uint64_t i = 5; i < n;) { uint32_t op = w[i] & 0xffff, len = w[i] >> 16; - if (!len || i + len > n) break; + if (!len || i + len > n || op == SPV_OP_FUNCTION) break; if (op == OP_TYPE_IMAGE && len >= 9 && w[i + 1] < bound && w[i + 7] == 1) { // sampled image uint32_t dim = w[i + 3], depth = w[i + 4], arrayed = w[i + 5], ms = w[i + 6]; if (dim <= 3 && !ms) kind[w[i + 1]] = (int32_t)((depth == 1 ? 8 : 0) + dim * 2 + (arrayed ? 1 : 0)); @@ -1575,7 +1576,7 @@ static uint32_t *spirv_pool_rebind(const uint32_t *w, uint64_t n) { uint32_t used = 0; for (uint64_t i = 5; i < n;) { uint32_t op = o[i] & 0xffff, len = o[i] >> 16; - if (!len || i + len > n) break; + if (!len || i + len > n || op == SPV_OP_FUNCTION) break; if (op == SPV_OP_DECORATE && len >= 4 && o[i + 1] < bound && kind[o[i + 1]] >= 2000) { int32_t k = kind[o[i + 1]] - 2000; if (o[i + 2] == SPV_DEC_DESCRIPTOR_SET) o[i + 3] = 2; From c7d8ea489ecf3df4a310bbc4514b399a2b244b13 Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Mon, 9 Nov 2026 20:06:31 -0600 Subject: [PATCH 02/16] Find NVIDIA's NGX core beside the driver's Vulkan driver when the registry's NGXCore key has no FullPath, so DLSS and DLAA start there. Co-Authored-By: Claude Fable 5.2 <[EMAIL]> --- runtime/vk_dlaa.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index cb93576f..e084b18a 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -147,6 +147,35 @@ int vk_dlaa_needs_restart(void) { return strcmp(cfg_value("NX2NAVI_TAA"), "fsr3") ? !N.ngx_up && !N.tried : N.ngx_up; } +#ifdef _WIN32 +// The NGX core's folder when the driver has not written NGXCore\FullPath (seen with driver 617.42, whose key held +// only DLSS override values): the core sits in the driver's own DriverStore folder, the one its Vulkan driver is in. +// That is the display adapter's VulkanDriverName, where the Vulkan loader itself finds the driver. +static int ngx_dir_from_adapter(char *dir, DWORD size) { + HKEY cls; + if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}", + 0, KEY_READ, &cls) != ERROR_SUCCESS) + return 0; + int found = 0; + for (DWORD i = 0; !found; i++) { + char sub[64], val[MAX_PATH + 1] = {0}, probe[MAX_PATH + 32]; + DWORD sn = sizeof sub, vn = MAX_PATH; + if (RegEnumKeyExA(cls, i, sub, &sn, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) break; + if (RegGetValueA(cls, sub, "VulkanDriverName", RRF_RT_REG_SZ | RRF_RT_REG_MULTI_SZ, NULL, val, &vn) != ERROR_SUCCESS) + continue; + char *slash = strrchr(val, '\\'); + if (!slash) continue; + *slash = 0; + snprintf(probe, sizeof probe, "%s\\_nvngx.dll", val); + if (GetFileAttributesA(probe) == INVALID_FILE_ATTRIBUTES) continue; + snprintf(dir, size, "%s", val); + found = 1; + } + RegCloseKey(cls); + return found; +} +#endif + static int core_load(void) { if (N.tried) return N.loaded; N.tried = 1; @@ -154,8 +183,8 @@ static int core_load(void) { char dir[MAX_PATH] = {0}, path[MAX_PATH + 32]; DWORD n = sizeof dir; if (RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\NVIDIA Corporation\\Global\\NGXCore", "FullPath", RRF_RT_REG_SZ, - NULL, dir, &n) != ERROR_SUCCESS) { - rt_log("dlaa: the driver has no NGX core (registry NGXCore missing): DLAA off"); + NULL, dir, &n) != ERROR_SUCCESS && !ngx_dir_from_adapter(dir, sizeof dir)) { + rt_log("dlaa: the driver has no NGX core (registry NGXCore missing, none beside its Vulkan driver): DLAA off"); return 0; } snprintf(path, sizeof path, "%s\\_nvngx.dll", dir); From e1276ee80dd85f2a7103a4cd718696064f14b4bc Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Mon, 9 Nov 2026 20:05:36 -0600 Subject: [PATCH 03/16] Drop a movie frame, or have the game retry a sample, when memory runs out, instead of stopping the game. Co-Authored-By: Claude Fable 5.2 <[EMAIL]> --- runtime/ov_movie2.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/runtime/ov_movie2.c b/runtime/ov_movie2.c index 9c66d559..13cebb3e 100644 --- a/runtime/ov_movie2.c +++ b/runtime/ov_movie2.c @@ -300,6 +300,14 @@ static void run_stripes(Stripe base) { if (th[i]) plat_thread_join(th[i]); } +// Out of memory for a decoded frame: the frame is dropped (the game shows the previous one a little longer) and +// the first drop of a run is logged. The game used to stop here; on a PC whose commit charge reached its limit +// for a moment, that ended a run that the next second would have had memory for. +static void movie_oom_log(int w, int h) { + static volatile int32_t drops; + if (plat_xchg32(&drops, 1) == 0) rt_log("movie: out of memory for a %dx%d frame: dropped (later drops not logged)", w, h); +} + // One dav1d picture (8- or 10-bit 4:2:0) -> NV12 with 8-bit samples, and its RGBA; decoder thread static int make_picture(const Dav1dPicture *pic, Picture *p) { int w = pic->p.w, h = pic->p.h, hbd = pic->p.bpc > 8, sh = pic->p.bpc - 8; @@ -322,7 +330,11 @@ static int make_picture(const Dav1dPicture *pic, Picture *p) { overlay = e && strcmp(e, "0"); } uint8_t *o = malloc((size_t)w * h * 3 / 2), *rgba = overlay ? malloc((size_t)w * h * 4) : NULL; - if (!o || (overlay && !rgba)) rt_fatal("movie: out of memory for a %dx%d frame", w, h); + if (!o || (overlay && !rgba)) { // a frame the PC has no memory for is dropped, not the game + free(o), free(rgba); + movie_oom_log(w, h); + return 0; + } const ptrdiff_t ys = pic->stride[0], cs = pic->stride[1]; static int deband_on = -1; // NX2NAVI_MOVIE_DEBAND=0 keeps the decoded planes if (deband_on < 0) { @@ -358,7 +370,11 @@ static int make_picture(const Dav1dPicture *pic, Picture *p) { // debanded planes (NX2NAVI_MOVIE_DEBAND=0 keeps the decoded ones), then the overlay's RGBA or the dithered NV12 { uint16_t *Y16 = malloc((size_t)w * h * 2), *U16 = malloc((size_t)w * h / 2), *V16 = malloc((size_t)w * h / 2); - if (!Y16 || !U16 || !V16) rt_fatal("movie: out of memory for a %dx%d frame", w, h); + if (!Y16 || !U16 || !V16) { + free(Y16), free(U16), free(V16), free(o), free(rgba); + movie_oom_log(w, h); + return 0; + } Stripe base = {0, 0, 0, w, h, o, Y16, U16, V16, rgba}; if (deband_on) { run_stripes(base); @@ -707,7 +723,15 @@ static void ov_decode(Cpu *c) { Sample s = {NULL, 0, ts, eos}; if (data && size) { s.data = malloc((size_t)size); - if (!s.data) rt_fatal("movie: out of memory for a %llu-byte sample", (unsigned long long)size); + if (!s.data) { // no memory for the sample now: the game is told to try again, as with a full queue + static volatile int32_t logged; + if (plat_xchg32(&logged, 1) == 0) + rt_log("movie: out of memory for a %llu-byte sample: retried (later ones not logged)", (unsigned long long)size); + update_events(v); + plat_mutex_unlock(&v->lock); + ret_x(c, R_BUSY); + return; + } memcpy(s.data, data, (size_t)size); s.size = (size_t)size; } From 62f6a7595bf8f91a7d51665f96021f05ff01510a Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Mon, 9 Nov 2026 21:16:28 -0600 Subject: [PATCH 04/16] Destroy the objects the GPU is done with outside the command lock. vk_mem_collect destroyed dead images, views and buffers while it held cmd_lock, and the replay's uploads wait for that lock. Destroying an image and its memory takes up to milliseconds. The collect now moves the objects out under the lock and destroys them after releasing it. Co-Authored-By: Claude Fable 5.2 <[EMAIL]> --- runtime/vk_memory.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 38cfaf51..537540f8 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -2835,7 +2835,18 @@ static void retire(VkCtx *c, int wait_all) { ninflight -= k; } -static void collect_locked(VkCtx *c) { +static void destroy_dead(VkCtx *c, const Dead *d) { + switch (d->kind) { + case DEAD_IMAGE: vkDestroyImage(c->device, (VkImage)(uintptr_t)d->handle, NULL); break; + case DEAD_IMAGE_VMA: vmaDestroyImage(c->vma, (VkImage)(uintptr_t)d->handle, d->alloc); break; + case DEAD_VIEW: vkDestroyImageView(c->device, (VkImageView)(uintptr_t)d->handle, NULL); break; + case DEAD_BUFFER_VMA: vmaDestroyBuffer(c->vma, (VkBuffer)(uintptr_t)d->handle, d->alloc); break; + } +} + +// The objects the GPU is done with: destroyed here (out != NULL: moved to *out, for the caller to destroy without +// cmd_lock; *nout of them). +static void collect_locked_out(VkCtx *c, Dead **out, int *nout, int *cout) { retire(c, 0); uint64_t tl = 0; if (c->timeline) vkGetSemaphoreCounterValue(c->device, c->timeline, &tl); @@ -2849,16 +2860,15 @@ static void collect_locked(VkCtx *c) { } rec_check_handle(d->kind == DEAD_VIEW ? REC_VIEW : d->kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, d->handle, 1, d->tl, tl); - switch (d->kind) { - case DEAD_IMAGE: vkDestroyImage(c->device, (VkImage)(uintptr_t)d->handle, NULL); break; - case DEAD_IMAGE_VMA: vmaDestroyImage(c->vma, (VkImage)(uintptr_t)d->handle, d->alloc); break; - case DEAD_VIEW: vkDestroyImageView(c->device, (VkImageView)(uintptr_t)d->handle, NULL); break; - case DEAD_BUFFER_VMA: vmaDestroyBuffer(c->vma, (VkBuffer)(uintptr_t)d->handle, d->alloc); break; - } + if (!out) { destroy_dead(c, d); continue; } + if (*nout == *cout) grow((void **)out, cout, sizeof **out); + (*out)[(*nout)++] = *d; } ndead = k; } +static void collect_locked(VkCtx *c) { collect_locked_out(c, NULL, NULL, NULL); } + VkCommandBuffer vk_upload_begin(void) { plat_once(&cmd_once, cmd_init, NULL); plat_rmutex_lock(&cmd_lock); @@ -3092,13 +3102,20 @@ void vk_mem_note_submit(uint64_t timeline_value) { plat_rmutex_unlock(&cmd_lock); } +// The objects go once cmd_lock is released: destroying an image and its memory takes up to milliseconds (the +// collect's 0.3 slow ones a frame took a median 0.26-0.43 ms, up to 1.9 ms), and the replay's uploads +// (vk_upload_begin) wait for cmd_lock. void vk_mem_collect(void) { VkCtx *c = vk_ctx(); if (!c) return; plat_once(&cmd_once, cmd_init, NULL); + Dead *out = NULL; + int nout = 0, cout = 0; plat_rmutex_lock(&cmd_lock); - collect_locked(c); + collect_locked_out(c, &out, &nout, &cout); plat_rmutex_unlock(&cmd_lock); + for (int i = 0; i < nout; i++) destroy_dead(c, &out[i]); + free(out); } void vk_mem_stats(VkMemStats *s) { From 6e0dc0b2a6398327eed30e26220999cfb4923b41 Mon Sep 17 00:00:00 2001 From: DeepDaddyTTV <32502468+DeepDaddyTTV@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:41:37 -0500 Subject: [PATCH 05/16] Document the automatic BC3 memory thresholds for GPU texture decoding. --- nx2navi.ini.example | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 9da59324..3799b373 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -123,8 +123,11 @@ # ASTC texture decoding: native (the GPU's own ASTC support), gpu (a compute shader), cpu, or auto (native when the GPU # has it, gpu otherwise). # astc = auto -# With astc = gpu: re-encode the decoded ASTC textures to BC3, a quarter of the video memory, textures slightly blockier, -# 0 or 1. Default: on when the GPU has less than 5 GB of video memory, off otherwise. +# With GPU ASTC decoding, 1 requests BC3 compression. Slight blocking is possible. 0 keeps RGBA8, which uses about +# four times as much memory for these textures and can exhaust memory while loading. +# Leave this setting commented for automatic selection. The renderer requests BC3 when the largest device-local GPU +# heap is under 20 GiB or the system reports less than 47 GiB of RAM (allowing for a nominal 48 GiB PC). If the RAM +# size is unknown, only the GPU heap decides. Look for "NX2NAVI_ASTC_BC unset" in the game log to see the decision. # astc_bc = 1 # Game memory shared with the GPU: auto (mirror on a discrete GPU, import on an integrated one), import (host memory # imported into Vulkan), mirror (pools used as buffers get a copy in video memory) or verify (mirror plus a check of From ab34c302d026ca8f1383e86814bcb096e258b498 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:24:38 -0600 Subject: [PATCH 06/16] Make the texture sharpness bias follow a DLSS mode picked while the game runs. A DLSS or FSR 3.1 mode picked in the menu changes the scene size at once (widescreen.c wide_present), but the texture LOD bias that goes with it stayed the one of the mode the game started with. vk_dlaa.c dlss_mode asks res_dlss_lod_bias again at every mode change, and res_dlss_lod_bias took the share from R.dlss_k, which decide() sets once at start; res_dlss_mode would move it, but returns first while res_init has not switched the output and scene patch sets on, which is always the case for Ocarina's build. Switching from Performance to Quality kept the bias of -1.00 where -0.58 belongs, and switching to DLAA kept a negative bias with the scene at full size. R.dlss_k is also 0 when the game started with DLAA off or without the DLSS model file beside the executable (the driver's NGX supplies the model then), so FSR 3.1 turned on later, or DLSS on such a machine, had no bias in any mode. Without those patch sets res_dlss_lod_bias now takes the share from widescreen.c's wide_dlss_share, the share the scene is drawn at, which follows the mode and the upscaler in use; builds with the sets keep R.dlss_k, which res_dlss_mode moves where their scene can change. vk_sampler_dlss_bias logs each change of the bias, so a mode change can be followed in the log. Co-Authored-By: Claude Opus 5.5 --- runtime/resolution.c | 10 ++++++++-- runtime/vk_texture.c | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/runtime/resolution.c b/runtime/resolution.c index 1e3584e5..205335ff 100644 --- a/runtime/resolution.c +++ b/runtime/resolution.c @@ -378,11 +378,17 @@ void res_init(void) { scene_on ? "" : " (the game's own 5/6 of the output)", (unsigned long long)res_heap_extra_kib()); } +double wide_dlss_share(void); // widescreen.c: the mode's share of the picture (1 DLAA), 0 off + // DLSS upscaling: textures are sampled for the output's resolution, not the scene's (NVIDIA's DLSS guide: a mip bias -// of log2(render / output)), so their detail survives the scaling. 0 without a DLSS mode. +// of log2(render / output)), so their detail survives the scaling. 0 without a DLSS mode. Without the output and scene +// patch sets (Ocarina) the scene's share is widescreen.c's, which follows the mode and the upscaler while the game runs; +// R.dlss_k stays the mode at start there (res_dlss_mode moves it only once res_init has switched the sets on). vk_dlaa.c +// dlss_mode asks again at every mode change. float res_dlss_lod_bias(void) { decide(); - return R.dlss_k ? (float)log2(R.dlss_k) : 0.0f; + double k = res_adjustable() ? R.dlss_k : wide_dlss_share(); + return k > 0 && k < 1 ? (float)log2(k) : 0.0f; } // 16:9 within 1% (1366x768 counts): the shape the game's own layouts and camera are made for. diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index e931b411..0a462775 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -3255,6 +3255,8 @@ static int dlss_biased(const samp_par *k) { void vk_sampler_dlss_bias(float b) { lock(); if (b != dlss_bias) { + rt_log("vk: texture LOD bias %.2f on the scene's tiled textures (the upscaler's render scale; was %.2f)", b, + dlss_bias); dlss_bias = b; for (int i = 0; i < nsslots; i++) sslots[i].s = sslots[i].sp = sslots[i].sc = VK_NULL_HANDLE; // looked up again (the key moves) samp_gen++; From 2a8fd84c5043add854a2092e6fe084982ff962c6 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:11:28 -0600 Subject: [PATCH 07/16] Let the menu test hook put the cursor on a setting's row. The menu test hook's shot of the Screen tab shows only its first screenful of rows, so rows further down (Upscaling, Frame generation) could not be pictured without input, and a hidden test window takes none. NX2NAVI_MENU_TEST_SET now accepts FOCUS=, which calls opt_focus_key for that setting's row: the cursor goes to it and the page scrolls it into view when it is next drawn. It does not switch tabs. Co-Authored-By: Claude Sonnet 5.5 --- runtime/ui/menu.cpp | 15 ++++++++++++--- runtime/ui/menu.h | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 20f15acd..ff4ab868 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -502,7 +502,9 @@ extern "C" void menu_vk_release(void) { // working directory) as menu-test--.png; after the last tab the menu closes. // NX2NAVI_MENU_TEST_SET=:=[,=...][;:...]: cfg_set of each key (NX2NAVI_ prefix // optional) at that presented frame; changes live settings with no input, menu open or not. nx2navi.ini is written only by -// the pseudo-key SAVE=1 (cfg_save, as a row change does; a failed write shows the menu's save note). +// the pseudo-key SAVE=1 (cfg_save, as a row change does; a failed write shows the menu's save note). FOCUS= (the +// NX2NAVI_ prefix optional) puts the cursor on that setting's row and scrolls it into view when the row is next drawn +// (opt_focus_key), so a page's shot shows a row below the first screenful; it does not switch tabs. enum { TAB_SCREEN, TAB_GAME, TAB_SOUND, TAB_CONTROLS, TAB_COUNT }; // Ocarina of Time: the options this port has enum { TEST_PAGE_FRAMES = 30, TEST_SETS = 16 }; struct TestSet { uint64_t frame; char key[64], value[64]; }; @@ -555,8 +557,15 @@ static void test_apply_sets(uint64_t frame) { for (; g_test.next_set < g_test.n_sets && g_test.sets[g_test.next_set].frame <= frame; g_test.next_set++) { const TestSet &t = g_test.sets[g_test.next_set]; rt_log("menu: test hook: frame %llu: %s=%s", (unsigned long long)frame, t.key, t.value); - if (!strcmp(t.key, "NX2NAVI_SAVE")) rt_log("menu: test hook: cfg_save %s", cfg_save() ? "wrote" : "failed"); - else cfg_set(t.key, t.value); + if (!strcmp(t.key, "NX2NAVI_SAVE")) { + rt_log("menu: test hook: cfg_save %s", cfg_save() ? "wrote" : "failed"); + } else if (!strcmp(t.key, "NX2NAVI_FOCUS")) { + char row[80]; + snprintf(row, sizeof row, "%s%s", strncmp(t.value, "NX2NAVI_", strlen("NX2NAVI_")) ? "NX2NAVI_" : "", t.value); + opt_focus_key(row); + } else { + cfg_set(t.key, t.value); + } } } diff --git a/runtime/ui/menu.h b/runtime/ui/menu.h index 82d6b1c3..bcf2fd89 100644 --- a/runtime/ui/menu.h +++ b/runtime/ui/menu.h @@ -10,7 +10,7 @@ // "Continue" and "Quit game". // Test hooks (ui/menu.cpp): NX2NAVI_MENU_TEST=[:] opens the menu at that presented frame and saves // one swapchain shot per tab into NX2NAVI_MENU_TEST_DIR; NX2NAVI_MENU_TEST_SET=:=[,...][;...] sets settings -// at given frames without input (SAVE=1 writes nx2navi.ini). +// at given frames without input (SAVE=1 writes nx2navi.ini, FOCUS= puts the cursor on that setting's row). // // Threads: the event functions are called by the window thread, menu_toggle by whichever thread polls the pad, the // overlay functions by the present path under its lock. ImGui itself runs on the present path only. From f19ed09f1ce8ca7079b5f7d25af8340973dbfea2 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:09:48 -0600 Subject: [PATCH 08/16] Find the player on game 1.1.1 so the world clock starts there. The world clock (frame_step_world_us) starts at the first present that finds the player actor away from the origin, and --seconds, --shot, NX2NAVI_SHOTS=w..., timed "@ms" script lines and recordings all run on it. player_pos read the 1.2.1 scene slot for every build that is not 1.1.0 or 1.4.3, so on 1.1.1 it never found a player: a --seconds run never ended, no picture was taken and timed lines never fired. On 1.1.1 player_pos now reads the Player actor from the slot [main+0x4e371d0] + 0x480, checks that its name pointer (+0x20) reads "Player" and takes the position at +0x31c. The slot is empty on the title screen and holds the actor from the first present after the save has loaded. The other builds keep their scene walk, whose name compare is shared. Co-Authored-By: Claude Sonnet 5.5 --- runtime/frame_rate.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 2f7228c7..839909d2 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -33,9 +33,9 @@ // and in range whichever store it sees. // // The world clock (frame_step_world_us) sums the steps from the first present that finds the player actor away from -// the origin, where the title screen keeps it: the end of the first save load. Recorded and scripted input -// (input.c) runs on it, so a replay presses at the same game time at any frame rate and after a first load of any -// length; a later load (fast travel) still shifts what follows by its length. +// the origin, where the title screen keeps it (1.1.1 has no player actor until the save has loaded): the end of the +// first save load. Recorded and scripted input (input.c) runs on it, so a replay presses at the same game time at any +// frame rate and after a first load of any length; a later load (fast travel) still shifts what follows by its length. // // NX2NAVI_TRACE=perf adds a line per 120 presents: fps, the game seconds stepped against wall seconds, and with // NX2NAVI_SPEED_PROBE=1 the player's position and ground speed (an independent check: Link runs at the same speed at @@ -309,12 +309,24 @@ static uint64_t read_ptr(uint64_t a) { uint64_t p = 0; return read_guest(a, &p, 8) ? p : 0; } -// 1.2.1: [main+0x4728538] (1.1.0: [main+0x4630a68], 1.4.3: [main+0x3add238]) +0x1e8 scene, its component 13 lists the -// resident actors; the one named "Player" has its position at +0x2b4. +// 1.1.1: [[main+0x4e371d0] + 0x480] is the Player actor, its name pointer at +0x20 reads "Player" and its position, in +// metres, is the float triplet at +0x31c. The slot is empty until the first save has loaded and then holds the actor. +// The other builds: [main+0x4728538] (1.1.0: [main+0x4630a68], 1.4.3: [main+0x3add238]) +0x1e8 scene, its component 13 +// lists the resident actors; the one named "Player" has its position at +0x2b4. // The 1.1.0 slot was measured from its own dereference chain: 181 scene accesses in that build // read it, and one of them reproduces this walk (the 14-component check, component 13, the 0x70 stride and the name // compare) instruction for instruction. +static int player_name(uint64_t name) { + char text[7]; + return name && read_guest(name, text, 7) && !memcmp(text, "Player", 7); +} + static int player_pos(float xyz[3]) { + if (RT_V111) { + uint64_t manager = read_ptr(MOD_MAIN_BASE + 0x4e371d0), actor = manager ? read_ptr(manager + 0x480) : 0; + return actor && player_name(read_ptr(actor + 0x20)) && read_guest(actor + 0x31c, xyz, 12) && isfinite(xyz[0]) && + isfinite(xyz[2]); + } uint64_t module = read_ptr(MOD_MAIN_BASE + (RT_V143 ? 0x3add238 : RT_V110 ? 0x4630a68 : 0x4728538)), scene = module ? read_ptr(module + 0x1e8) : 0; int32_t count = 0; @@ -324,9 +336,7 @@ static int player_pos(float xyz[3]) { uint64_t links = read_ptr(resident + 0x28); for (int i = 0; links && i < count; i++) { uint64_t data = read_ptr(links + (uint64_t)i * 0x70 + 8), actor = data ? read_ptr(data + 0x40) : 0; - uint64_t name = actor ? read_ptr(actor + 0x218) : 0; - char text[7]; - if (name && read_guest(name, text, 7) && !memcmp(text, "Player", 7)) + if (actor && player_name(read_ptr(actor + 0x218))) return read_guest(actor + 0x2b4, xyz, 12) && isfinite(xyz[0]) && isfinite(xyz[2]); } return 0; From da303e830d67871616f174c7cf31af81c616fc06 Mon Sep 17 00:00:00 2001 From: DeepDaddyTTV <32502468+DeepDaddyTTV@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:28:11 -0500 Subject: [PATCH 09/16] Retry launcher downloads when the response ends early. --- launcher/net.py | 18 ++++++++++++++---- tests/launcher_test.py | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/launcher/net.py b/launcher/net.py index fb1a71ed..064f6a50 100644 --- a/launcher/net.py +++ b/launcher/net.py @@ -26,6 +26,7 @@ import urllib.error import urllib.parse import urllib.request from datetime import timezone +from http.client import IncompleteRead from pathlib import Path from . import NAME @@ -194,6 +195,13 @@ def certificate_problem(url, refused, version='dev'): 'may be replacing the certificate: let NX2navi through it or turn that scanning off, then try again.') +def _check_eof(response): + # HTTPResponse.read(n) accepts an early EOF on a Content-Length body and leaves the missing byte count in length. + remaining = getattr(response, 'length', None) + if remaining: + raise ConnectionError(f'connection closed with {remaining} response bytes still expected') + + def get_bytes(url, headers=None, timeout=30, version='dev', limit=64 << 20, retries=3): """The body of a GET of url, at most limit bytes. A cut connection, a timeout or a server error (5xx, 429) is tried again after 1, then 2 seconds, as download's are: one connection reset during Gather Build Tools (the package @@ -203,19 +211,20 @@ def get_bytes(url, headers=None, timeout=30, version='dev', limit=64 << 20, retr try: with request(url, headers, timeout, version) as r: data = r.read(limit + 1) + if len(data) > limit: + raise DownloadError(f'{url}: more than {limit >> 20} MB') + _check_eof(r) break except urllib.error.HTTPError as e: if e.code != 429 and e.code < 500: raise last_error = e - except (urllib.error.URLError, OSError, TimeoutError) as e: # not DownloadError or CertificateError + except (urllib.error.URLError, OSError, IncompleteRead) as e: # not DownloadError or CertificateError last_error = e if attempt + 1 < retries: time.sleep(min(2 ** attempt, 8)) else: raise last_error - if len(data) > limit: - raise DownloadError(f'{url}: more than {limit >> 20} MB') return data @@ -261,6 +270,7 @@ def download(url, dest, sha256=None, size=None, progress=None, cancel=None, head raise Cancelled() b = r.read(CHUNK) if not b: + _check_eof(r) break f.write(b) done += len(b) @@ -282,7 +292,7 @@ def download(url, dest, sha256=None, size=None, progress=None, cancel=None, head if e.code in (401, 403, 404, 410) or 300 <= e.code < 400: # 3xx: a redirect _Redirect refused raise DownloadError(f'{url}: HTTP {e.code}') from e last_error = e - except (urllib.error.URLError, OSError, TimeoutError) as e: + except (urllib.error.URLError, OSError, IncompleteRead) as e: last_error = e time.sleep(min(2 ** attempt, 16)) else: diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 89af1da8..679588d3 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -85,7 +85,8 @@ class Server: self.send_response(status) for k, v in headers.items(): self.send_header(k, v) - self.send_header('Content-Length', str(len(body))) + if 'Content-Length' not in headers: + self.send_header('Content-Length', str(len(body))) self.end_headers() self.wfile.write(body) return @@ -521,6 +522,22 @@ class NetTest(Tmp): with self.assertRaises(ConnectionResetError): # every try failed: the last error goes up net.get_bytes('https://example.invalid/down') + def test_early_eof_retries_metadata_and_resumes_file(self): + data = b'{"build":123}' + (self.tmp / 'asset').write_bytes(data) + cut = (200, {'Content-Length': str(len(data))}, data[:7]) + routes = {'/asset': cut} + srv = Server(self.tmp, routes) + self.addCleanup(srv.close) + url = srv.url + 'asset' + with mock.patch.object(net.time, 'sleep', side_effect=lambda _: routes.clear()): + self.assertEqual(net.get_json(url), {'build': 123}) + routes['/asset'] = cut + dest = self.tmp / 'download' + net.download(url, dest, sha256=sha(data), size=len(data)) + self.assertEqual(dest.read_bytes(), data) + self.assertEqual([h.get('Range') for _, h in srv.hits], [None, None, None, 'bytes=7-']) + def test_download_resume_and_checks(self): data = os.urandom(3 * net.CHUNK + 123) (self.tmp / 'f.bin').write_bytes(data) From 4fc8bde5404c3401dd66e22d48b308adf156b56a Mon Sep 17 00:00:00 2001 From: DeepDaddyTTV <32502468+DeepDaddyTTV@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:29:07 -0500 Subject: [PATCH 10/16] Release unused audio voices by their slot. --- runtime/audren.c | 2 ++ runtime/ipc.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/runtime/audren.c b/runtime/audren.c index 407c21f1..f5d9b490 100644 --- a/runtime/audren.c +++ b/runtime/audren.c @@ -307,6 +307,8 @@ static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_s } } else memcpy(&v, voices + i * voice_stride, sizeof v); + // An unused slot can have a zero id. Release its own voice and leave voice zero playing. + if (!v.in_use) v.id = (uint32_t)i; update_voice(r, &v); ServerVoice *server = &r->voices[v.id]; server->use_float_biquads = (uint8_t)float_voices; diff --git a/runtime/ipc.c b/runtime/ipc.c index ba6dd934..1cc4f113 100644 --- a/runtime/ipc.c +++ b/runtime/ipc.c @@ -695,7 +695,7 @@ static void update_build(Update *u, int first) { memcpy(w, &h, sizeof h); w += sizeof h + h.behavior + h.memory_pools + h.voice_resources; for (int i = 0; i < p->voice_count; i++) { - AudrenVoiceIn v = {.id = (uint32_t)i}; + AudrenVoiceIn v = {0}; // the client leaves the id of an unused slot zero if (i == 0) { // one PCM16 voice with a 100 ms buffer: consumed after 20 frames v = (AudrenVoiceIn){.id = 0, .is_new = (uint8_t)first, .in_use = 1, .play_state = PLAY_STARTED, .sample_format = FMT_PCM16, .sample_rate = 48000, .channel_count = 1, .pitch = 1, From b4a898d47b285b50f5c2894c0fd996499bcfc008 Mon Sep 17 00:00:00 2001 From: DeepDaddyTTV <32502468+DeepDaddyTTV@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:28:32 -0500 Subject: [PATCH 11/16] =?UTF-8?q?Bound=20the=20RomFS=20packer=E2=80=99s=20?= =?UTF-8?q?parallel=20reads=20by=20file=20size.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/romfs_packer_test.py | 72 ++++++++++++++++++++++++++++++++++++++ tools/make_game.py | 41 +++++++++++++++------- 2 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 tests/romfs_packer_test.py diff --git a/tests/romfs_packer_test.py b/tests/romfs_packer_test.py new file mode 100644 index 00000000..88ce654d --- /dev/null +++ b/tests/romfs_packer_test.py @@ -0,0 +1,72 @@ +"""RomFS read-ahead and packing with synthetic files. Run: python tests/romfs_packer_test.py""" +import hashlib +import json +from pathlib import Path +import sys +import tempfile +import threading +import unittest +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools')) +import make_game + + +class RomfsPackerTest(unittest.TestCase): + def test_read_ahead_limits_total_bytes_and_file_count(self): + limit = make_game.ROMFS_READ_AHEAD + sizes = [limit - 1, 1, limit + 1, 1, limit] + [0] * 512 + files = [(str(i), i, size) for i, size in enumerate(sizes)] + groups = list(make_game.romfs_read_groups(files)) + self.assertEqual([entry for group in groups for entry in group], files) + for group in groups: + self.assertLessEqual(sum(size for _, _, size in group if size <= limit), limit) + self.assertLessEqual(len(group), 256) + + def test_changed_file_cannot_replace_the_image(self): + source = mock.Mock(romfs=[('changed.bin', 0)]) + source.romfs_size.return_value = 4 + with tempfile.TemporaryDirectory() as tmp: + dst = Path(tmp) / 'romfs' + dst.mkdir() + (dst / 'romfs.bin').write_bytes(b'previous image') + (dst / 'index.json').write_bytes(b'previous index') + for limit, chunks in [(8, [b'123']), (2, [b'123', b'456'])]: + with self.subTest(limit=limit), mock.patch.object(make_game, 'ROMFS_READ_AHEAD', limit): + source.romfs_chunks.return_value = chunks + with self.assertRaises(SystemExit): + make_game.step_romfs(source, dst.parent, 1) + self.assertEqual((dst / 'romfs.bin').read_bytes(), b'previous image') + self.assertEqual((dst / 'index.json').read_bytes(), b'previous index') + + def test_streamed_and_buffered_files_keep_the_same_layout(self): + # Lower the read-ahead limit so these tiny files span several worker groups. + large_file_readers = [] + + class Source: + romfs = [('e', b'123456789'), ('a', b'12345'), ('b', b'678'), ('c', b''), ('d', b'!')] + + def romfs_size(self, ref): + return len(ref) + + def romfs_chunks(self, ref): + if len(ref) > 8: + large_file_readers.append(threading.current_thread()) + yield ref[:3] + yield ref[3:] + + with tempfile.TemporaryDirectory() as tmp, mock.patch.object(make_game, 'ROMFS_READ_AHEAD', 8): + out = Path(tmp) + make_game.step_romfs(Source(), out, 2) + image = (out / 'romfs' / 'romfs.bin').read_bytes() + index = json.loads((out / 'romfs' / 'index.json').read_text()) + self.assertEqual(large_file_readers, [threading.main_thread()]) + self.assertEqual(image, b'12345' + bytes(11) + b'678' + bytes(13) + b'!' + bytes(15) + b'123456789') + self.assertEqual(index['romfs_sha256'], hashlib.sha256(image).hexdigest()) + self.assertEqual(index['romfs_size'], len(image)) + self.assertEqual([(e['path'], e['offset'], e['size']) for e in index['files']], + [('a', 0, 5), ('b', 16, 3), ('c', 32, 0), ('d', 32, 1), ('e', 48, 9)]) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/make_game.py b/tools/make_game.py index 3319ce7e..01e410e3 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -365,11 +365,28 @@ def exefs_digest(out): # ---------------------------------------------------------------- romfs +ROMFS_READ_AHEAD = 64 << 20 # total file bytes requested from workers in one group + + +def romfs_read_groups(files): + group, buffered = [], 0 + for entry in files: + size = entry[2] + ahead = size if size <= ROMFS_READ_AHEAD else 0 # the writer streams larger files + if buffered + ahead > ROMFS_READ_AHEAD or len(group) == 256: + yield group + group, buffered = [], 0 + group.append(entry) + buffered += ahead + if group: + yield group + + def step_romfs(src, out, jobs): dst = out / 'romfs' dst.mkdir(parents=True, exist_ok=True) - files = sorted(src.romfs, key=lambda e: e[0].encode()) - total = sum(src.romfs_size(ref) for _, ref in files) + files = sorted(((rel, ref, src.romfs_size(ref)) for rel, ref in src.romfs), key=lambda e: e[0].encode()) + total = sum(size for _, _, size in files) free = shutil.disk_usage(dst).free if free < total + (1 << 30): fail(f'{dst} needs {total / 1e9:.1f} GB free for the RomFS image, it has {free / 1e9:.1f} GB') @@ -379,28 +396,28 @@ def step_romfs(src, out, jobs): emit('progress', step='romfs', done=0, total=total, unit='bytes') def load(e): # small files are read whole on a worker; big ones are streamed by the writer - rel, ref = e - return None if src.romfs_size(ref) > (64 << 20) else b''.join(src.romfs_chunks(ref)) + _, ref, size = e + return None if size > ROMFS_READ_AHEAD else b''.join(src.romfs_chunks(ref)) with open(part, 'wb', buffering=16 << 20) as img, concurrent.futures.ThreadPoolExecutor(jobs) as ex: - batch = 256 - for start in range(0, len(files), batch): - group = files[start:start + batch] - for (rel, ref), data in zip(group, ex.map(load, group)): + for batch, group in enumerate(romfs_read_groups(files)): + for (rel, ref, size), data in zip(group, ex.map(load, group)): pad = -pos % ALIGN if pad: img.write(b'\0' * pad) h.update(b'\0' * pad) pos += pad - size = src.romfs_size(ref) - entries.append({'path': rel, 'offset': pos, 'size': size}) + offset = pos for chunk in ((data,) if data is not None else src.romfs_chunks(ref)): img.write(chunk) h.update(chunk) - pos += size + pos += len(chunk) + if pos - offset != size: + fail(f'{rel} changed size while packing the RomFS: expected {size} bytes, read {pos - offset}') + entries.append({'path': rel, 'offset': offset, 'size': size}) done += size emit('progress', step='romfs', done=done, total=total, unit='bytes') - if start // batch % 40 == 0: + if batch % 40 == 0: say(f' RomFS {done / 1e9:5.1f} of {total / 1e9:.1f} GB, {time.time() - t0:.0f} s') part.replace(dst / 'romfs.bin') index = {'status': 'pass', 'romfs_size': pos, 'romfs_sha256': h.hexdigest(), 'files': entries} From 834d9e2d20269c60c904eb5409ad3e5dfaa5efbf Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Mon, 9 Nov 2026 21:16:25 -0600 Subject: [PATCH 12/16] Take a submission's write-watch snapshots ahead for the pools the submission at its place in the last frame synced. The pre-snap of a submission took the pools the submission before it had synced. In Ocarina those are another submission's pools: the frame's first and fourth submissions use no pool and queried the pools of the ones before them, and the submissions after them then queried their own pools one at a time at first use. The pools are now remembered by the submission's place in its frame. NX2NAVI_PRESNAP_PLACE=0 gives the old choice back and =ab switches it in the B blocks. Co-Authored-By: Claude Fable 5.2 <[EMAIL]> --- runtime/cfg.c | 1 + runtime/vk.h | 1 + runtime/vk_backend.c | 10 +++++++++- runtime/vk_memory.c | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 8bcb63f5..f490535a 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -54,6 +54,7 @@ static const char *const known[] = { "PREWARM_FULL", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_REPLAY", "PROFILE_SLOW", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "REFLEX", "RENDER_SCALE", "REPLAY_ASYNC", "RTSS", "RTSS_ENUM", "SCENE_SHARE", + "PRESNAP_PLACE", "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", "SHADER_CACHE_FLUSH", "SHADER_CARRY", "SHADER_MEMO", "SHADOW_COMPARE", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", diff --git a/runtime/vk.h b/runtime/vk.h index a124ee7f..81ae6766 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -588,6 +588,7 @@ int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t // batch) without a signal, and returns a new begun one for the rest of the submission. The submission's timeline value // is signalled by its last part, whose signal covers every earlier part in submission order. VkCommandBuffer vk_sync_split(void); +extern int vk_sub_in_frame; // replay: the submission being recorded is the frame's n-th (0 the first; vk_backend.c) // Runs fn(arg) on the completion thread once fence is done on the GPU (event writes, counter reports, sync state). // Callbacks run in the order they were added, after those of earlier values; a value already done runs at once there. void vk_completion_add(uint64_t fence, void (*fn)(void *), void *arg); diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index a2e715c8..953f8842 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -272,8 +272,11 @@ static uint64_t submit_sync(const uint64_t *handles, int n) { return f; } +int vk_sub_in_frame; + static void replay_main(void *arg) { (void)arg; + int sub_idx = 0; // submissions since the last present, in queue order perf_replay_thread(); plat_mutex_lock(&rl); for (;;) { @@ -285,6 +288,7 @@ static void replay_main(void *arg) { plat_cond_broadcast(&rcv); plat_mutex_unlock(&rl); if (it.kind == RI_SUBMIT) { + vk_sub_in_frame = sub_idx++; uint64_t f = submit_sync(it.h, it.n); if (f != it.value) rt_fatal("vk: replayed submission signalled %llu, queued as %llu", (unsigned long long)f, @@ -298,6 +302,7 @@ static void replay_main(void *arg) { uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED); vk_dlssg_submitted(frame); // the frame's submissions were replayed before its present present_image(it.tex, it.has_crop ? it.crop : NULL, it.at, it.queued, frame, it.value); + sub_idx = 0; } plat_mutex_lock(&rl); if (it.kind == RI_SUBMIT) r_submit_done = it.submit_no; @@ -480,7 +485,10 @@ static void vk_be_program_release(const prog_t *p) { replay_drain(DR_PROG_RELEAS // Asynchronous: the handles (nvn_submit_prepare_async ran under the front end's submit lock) are copied and queued; // the value returned is the one the replay will signal. static uint64_t vk_be_submit(const uint64_t *handles, int n) { - if (!gfx_replay_async()) return submit_sync(handles, n); + if (!gfx_replay_async()) { + vk_sub_in_frame = nvn_submits_in_frame; // the front end counts it up after the submit returns + return submit_sync(handles, n); + } vk_sync_start(); ReplayItem it = {.kind = RI_SUBMIT, .n = n, .submit_no = nvn_submit_count()}; if (n > 0) { diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 537540f8..284c004e 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -63,6 +63,9 @@ typedef struct PoolRec { VkBuffer mbuf; // MS_ON: device-local copy of [import_lo, import_hi), same offsets as buf VmaAllocation malloc_; uint32_t acc_sub; // replay: the last submission that synced the mirror (mirror_sync) + uint32_t pm_frame; // replay: the frame (pframe) of pm_cur + uint8_t pm_cur, pm_prev; // replay: the places in their frame (vk_sub_in_frame, 7 the rest) of the submissions + // that synced it, in frame pm_frame and in the one before (NX2NAVI_PRESNAP_PLACE) uint32_t wb_sub; // replay: the last submission that wrote mirror bytes back to guest memory uint32_t retry_sub; // replay: a mirror refused for the VRAM budget is asked again from here, 0 never uint8_t asked_room; // replay: its refusal already asked the textures for room (once until it gets one) @@ -1328,6 +1331,34 @@ enum { MR_BUDGET, MR_UNTRACKED, MR_OVERLAP, MR_ALLOC, MR_N }; int vk_mirror_on; static int mirror_verify; static int mirror_gap = 2; // pre-snap: hot pools at most this many 2 MiB regions apart share one query +static uint32_t pframe; // replay: frames begun (vk_mem_mirror_begin: a place not after the last one's) +static int pplace = 99; // replay: the place of the last submission vk_mem_mirror_begin saw + +// NX2NAVI_PRESNAP_PLACE=1 (default), 0, ab: a submission's pre-snap (vk_mem_mirror_begin) takes the write-watch +// snapshots of the pools the submission at its place in the last frame synced, not those the submission before it +// synced. In Kokiri Forest the frame's first submission (#0) and its fourth (#3) use no pool, and their pre-snaps +// covered the pools of the submission before them, #5's and #2's: 36.8 queries over 282 MiB a frame, 0.23-0.31 ms; then +// #2 and #5, whose submissions before them use no pool either, queried their pools one at a time at their first use, +// 63.5 queries over 254 MiB. +static int presnap_place(void) { + static int mode = -1; + if (mode < 0) mode = rt_knob("PRESNAP_PLACE", "1"); + return RT_KNOB_ON(mode); +} + +static int sub_place(void) { return vk_sub_in_frame >= 0 && vk_sub_in_frame < 7 ? vk_sub_in_frame : 7; } + +static void pm_note(PoolRec *r) { // replay: r synced in the submission being recorded + if (r->pm_frame != pframe) { + r->pm_prev = r->pm_frame + 1 == pframe ? r->pm_cur : 0; + r->pm_cur = 0, r->pm_frame = pframe; + } + r->pm_cur |= (uint8_t)(1u << sub_place()); +} + +static uint8_t pm_last(const PoolRec *r) { // the places that synced r in the last frame + return r->pm_frame == pframe ? r->pm_prev : r->pm_frame + 1 == pframe ? r->pm_cur : 0; +} static PoolRec **mlist; // pool_lock: mirrored pools by import_lo static int nm, mcap; static uint32_t msub; // replay: number of the submission being recorded @@ -1709,6 +1740,7 @@ static void mirror_retire(void *arg) { static void mirror_sync(PoolRec *r) { uint64_t t0 = qpc(); r->acc_sub = msub; + pm_note(r); uint64_t lo = r->import_lo, hi = r->import_hi; uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; int excl = 0; @@ -2191,9 +2223,12 @@ void vk_mem_mirror_begin(void) { plat_mutex_lock(&gen_lock); ww_ep++; uint64_t b0 = ww_bytes; + int pl = sub_place(), pp = presnap_place(); + if (pl <= pplace) pframe++; // a new frame, though its first submissions may have had nothing to record + pplace = pl; for (int i = 0; i < nm; i++) { const PoolRec *r = mlist[i]; - if (r->acc_sub != prev) continue; + if (pp ? !(pm_last(r) >> pl & 1) : r->acc_sub != prev) continue; uint64_t a = r->import_lo >> SNAP_SHIFT, b = (r->import_hi - 1) >> SNAP_SHIFT; if (have && a <= run1 + 1 + (uint64_t)mirror_gap) { if (b > run1) run1 = b; From 03cb703e8fb72a0875356a84b856c9ad792b7f98 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:27:39 -0600 Subject: [PATCH 13/16] Apply anisotropic filtering while the game runs, and add it to the Screen tab. NX2NAVI_ANISO was read once, when the first sampler was made (vk_texture.c sampler_features), and make_sampler raised the level of the surface textures' samplers as it created them. Samplers are looked up by the game's parameters and live until shutdown, so a new level could only reach them after a restart, and the setting had no row in the menu. The decision which samplers get the level moves from make_sampler into aniso_raised, and sampler_for puts the raised level into the sampler key, as it does with the DLSS texture bias. vk_sampler_aniso_live reads the setting again after a settings change, at each present beside vk_dlaa_frame's bias; on a new level it clears the slots' samplers and bumps the sampler generation, as vk_sampler_dlss_bias does, so the slots and the descriptor caches take samplers of the new level from the next draw. The old level's samplers stay until shutdown like every sampler. The setting is marked live in cfg.c, and the Screen tab gets a Texture filtering row (Game, 2x, 4x, 8x, 16x) with the description the catalog already had. Co-Authored-By: Claude Opus 5.5 --- docs/README-BuildKit.md | 2 +- nx2navi.ini.example | 3 +- runtime/cfg.c | 2 +- runtime/ui/menu.cpp | 4 +++ runtime/vk.h | 1 + runtime/vk_backend.c | 1 + runtime/vk_texture.c | 70 +++++++++++++++++++++++++++++++---------- 7 files changed, 63 insertions(+), 20 deletions(-) diff --git a/docs/README-BuildKit.md b/docs/README-BuildKit.md index b34e3146..c21e1a02 100644 --- a/docs/README-BuildKit.md +++ b/docs/README-BuildKit.md @@ -108,7 +108,7 @@ beside the game. "Preparing shaders" once while the driver rebuilds them. With a driver that has Vulkan's graphics pipeline library it lasts at most about 20 seconds once the shaders are translated, and the remaining pipelines are built in the background while you play. -- **Screen** in the menu has Upscaling (DLSS on NVIDIA RTX cards, FSR 3.1 on any card), Frame generation (NVIDIA DLSS +- **Screen** in the menu has Texture filtering (anisotropic, up to 16x, at once), Upscaling (DLSS on NVIDIA RTX cards, FSR 3.1 on any card), Frame generation (NVIDIA DLSS on RTX 40 and 50 cards, with a DLSS multiplier of 2x, or 3x and 4x on RTX 50 cards; FSR 3.1 on any card; it needs DLSS or FSR 3.1 upscaling on, and the frame limit then counts the frames on screen; with no limit chosen it is the display's refresh rate), HDR (the game's picture tone mapped to your display, with its own grading rows, or Native, the game's own HDR; turn HDR on in Windows first) and the diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 3799b373..13450974 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -281,7 +281,8 @@ # aspect = fit # Anisotropic filtering of surface textures: game (the level the game asks for, as on the Switch) or 2, 4, 8, 16. Keeps # ground, roads and walls seen at a shallow angle sharp into the distance. Only filtered, mipmapped textures that tile -# are raised; shadow maps, data maps and screen-sized buffers keep the game's setting. Read at start. +# are raised; shadow maps, data maps and screen-sized buffers keep the game's setting. Changes while the game runs take +# effect at once. # aniso = game # Sun shadow map size: game (the game's own, which changes with the view and drops to about 400 near the ground, so # leaf shadows look blocky and jump) or a fixed 256, 512, 768, 1024, 1536 or 2048. Above 960 the texture heap grows with diff --git a/runtime/cfg.c b/runtime/cfg.c index f490535a..761f485b 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -145,7 +145,7 @@ static const CfgMeta metas[] = { {"GAME_ASPECT", "16:9", "16:9|window|21:9|32:9", RESTART}, // widescreen.c {"GAME_RES", "1440", "1080|1440|1800|2160|2880|window", RESTART}, // widescreen.c: the picture's height {"ASPECT", "fit", "fit|stretch", LIVE}, {"FOV", "100", "70..140", LIVE}, - {"ANISO", "game", "game|2|4|8|16", RESTART}, // vk_texture.c sampler_features + {"ANISO", "game", "game|2|4|8|16", LIVE}, // vk_texture.c sampler_features, vk_sampler_aniso_live // colour adjustment (vk_color.c); the preset is the menu's, the four values are what the present uses {"COLOR_PRESET", "default", "default|vivid|clarity|custom", LIVE}, {"COLOR_BRIGHTNESS", "0", "-30..30", LIVE}, {"COLOR_CONTRAST", "100", "70..150", LIVE}, {"COLOR_SATURATION", "100", "50..200", LIVE}, diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index ff4ab868..6b3df312 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -988,6 +988,10 @@ static void tab_screen(float scale) { int dl = cfg_bool("NX2NAVI_DLAA"), fsr3 = dl && !strcmp(cfg_value("NX2NAVI_TAA"), "fsr3"); if (!fsr3 && (strcmp(cfg_value("NX2NAVI_UPSCALER"), "fsr") || dl)) opt_disable_next(UI_MENU_SHARPNESS_NEEDS_FSR); opt_slider("NX2NAVI_SHARPNESS", UI_MENU_SHARPNESS, 0, 100, UI_MENU_SHARPNESS_DESC, 5, "%.0f %%"); + // Anisotropic filtering of the surface textures that tile (vk_texture.c aniso_raised), at once (vk_sampler_aniso_live) + static const char *const aniso[] = {"game", "2", "4", "8", "16"}; + const char *aniso_names[] = {menu_text(UI_MENU_ANISO_GAME), "2x", "4x", "8x", "16x"}; + opt_choice_list("NX2NAVI_ANISO", UI_MENU_ANISO, aniso, aniso_names, 5, UI_MENU_ANISO_DESC); opt_end(); } diff --git a/runtime/vk.h b/runtime/vk.h index 81ae6766..ec16d27c 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -526,6 +526,7 @@ uint32_t vk_texture_entry_gen(void); // vk_texture.c: bumped by int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void *arg); uint64_t vk_texture_dirty_pos_locked(void); 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 void vk_texture_gpu_written(const tex_t *t); // rendered or copied into by the GPU: no re-upload from storage // The live image of the given format and img's size at img's storage address, NULL when there is none. Texture lock held. diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 953f8842..4533f0c4 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -105,6 +105,7 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin vk_exec_frame_log(t, frame); dynres_frame(); // before vk_dlaa_frame reads the DLSS mode it may override vk_dlaa_frame(); + vk_sampler_aniso_live(); // NX2NAVI_ANISO changed in the menu: the surface textures' samplers take the new level vk_mvsurvey_frame(); vk_mvprev_frame(); vk_mem_census(frame); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 0a462775..816ad79d 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -3128,6 +3128,17 @@ static VkCompareOp compare_op(uint32_t f) { static struct { int init, mirror_clamp_edge, minmax, border_no_format; float aniso; } sf; // aniso: NX2NAVI_ANISO, 0 = the game's +static float aniso_setting(void) { + const char *an = cfg_value("NX2NAVI_ANISO"); + return strcmp(an, "game") ? (float)atoi(an) : 0; +} + +static void aniso_log(void) { + if (sf.aniso > 1) rt_log("vk: anisotropic filtering: %gx", sf.aniso); + else rt_log("vk: anisotropic filtering: the game's"); +} + +// Caller holds the lock. static void sampler_features(VkCtx *c) { if (sf.init) return; VkPhysicalDeviceCustomBorderColorFeaturesEXT cbf = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT}; @@ -3138,12 +3149,25 @@ static void sampler_features(VkCtx *c) { sf.mirror_clamp_edge = f12.samplerMirrorClampToEdge; sf.minmax = f12.samplerFilterMinmax; sf.border_no_format = c->has_custom_border_color && cbf.customBorderColors && cbf.customBorderColorWithoutFormat; - const char *an = cfg_value("NX2NAVI_ANISO"); - sf.aniso = strcmp(an, "game") ? (float)atoi(an) : 0; - rt_log("vk: anisotropic filtering: %s", sf.aniso > 1 ? an : "the game's"); + sf.aniso = aniso_setting(); + aniso_log(); sf.init = 1; } +// NX2NAVI_ANISO raises the level for filtered, mipmapped colour samplers that repeat on U and V: the surface textures, +// which tile, so ground and walls seen at a shallow angle stay sharp into the distance. Depth-compare (shadow) +// samplers, nearest or unmipmapped ones (data maps, LUTs) and clamped ones keep the game's level: clamped samplers +// read screen-sized buffers, whose coordinates can stretch steeply at an object's edge, where anisotropic taps reach +// across it and leave a speckled fringe (the inventory's character preview). The game's own wrap modes decide: a +// mirror-clamp one maps to MIRRORED_REPEAT where the device lacks mirror-clamp-to-edge (make_sampler). Caller holds the +// lock, after sampler_features. +static int aniso_raised(const samp_par *d) { + int tiled = (d->ws == NVN_WRAP_MODE_REPEAT || d->ws == NVN_WRAP_MODE_MIRRORED_REPEAT) && + (d->wt == NVN_WRAP_MODE_REPEAT || d->wt == NVN_WRAP_MODE_MIRRORED_REPEAT); + return sf.aniso > d->aniso && tiled && !d->cmp_mode && d->magf == NVN_MAG_FILTER_LINEAR && + (d->minf == NVN_MIN_FILTER_LINEAR_MIPMAP_NEAREST || d->minf == NVN_MIN_FILTER_LINEAR_MIPMAP_LINEAR); +} + static VkSampler make_sampler(VkCtx *c, const samp_par *d) { sampler_features(c); VkSamplerCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO}; @@ -3175,21 +3199,10 @@ static VkSampler make_sampler(VkCtx *c, const samp_par *d) { if (si.maxLod < si.minLod) si.maxLod = si.minLod; float lim = c->props.limits.maxSamplerLodBias; si.mipLodBias = fmaxf(-lim, fminf(lim, d->lod_bias)); - // NX2NAVI_ANISO raises the level for filtered, mipmapped colour samplers that repeat on U and V: the surface textures, - // which tile, so ground and walls seen at a shallow angle stay sharp into the distance. Depth-compare (shadow) - // samplers, nearest or unmipmapped ones (data maps, LUTs) and clamped ones keep the game's level: clamped samplers - // read screen-sized buffers, whose coordinates can stretch steeply at an object's edge, where anisotropic taps reach - // across it and leave a speckled fringe (the inventory's character preview). - float aniso = d->aniso; - // The game's own wrap modes: a mirror-clamp one maps to MIRRORED_REPEAT where the device lacks mirror-clamp-to-edge. - int tiled = (d->ws == NVN_WRAP_MODE_REPEAT || d->ws == NVN_WRAP_MODE_MIRRORED_REPEAT) && - (d->wt == NVN_WRAP_MODE_REPEAT || d->wt == NVN_WRAP_MODE_MIRRORED_REPEAT); - if (sf.aniso > aniso && tiled && !d->cmp_mode && si.magFilter == VK_FILTER_LINEAR && si.minFilter == VK_FILTER_LINEAR && - (mf == NVN_MIN_FILTER_LINEAR_MIPMAP_NEAREST || mf == NVN_MIN_FILTER_LINEAR_MIPMAP_LINEAR)) - aniso = sf.aniso; - if (aniso > 1) { + // d->aniso holds NX2NAVI_ANISO's level where it applies (sampler_for, aniso_raised) + if (d->aniso > 1) { si.anisotropyEnable = VK_TRUE; - si.maxAnisotropy = fminf(aniso, c->props.limits.maxSamplerAnisotropy); + si.maxAnisotropy = fminf(d->aniso, c->props.limits.maxSamplerAnisotropy); } if (d->cmp_mode) { si.compareEnable = VK_TRUE; @@ -3264,12 +3277,35 @@ void vk_sampler_dlss_bias(float b) { unlock(); } +// NX2NAVI_ANISO, read again at each present after a settings change (vk_backend.c present_image, beside the DLSS bias of +// vk_dlaa_frame): the raised level is part of the sampler key (sampler_for), so on a change the slots look their +// samplers up again and the descriptor caches follow samp_gen, as for vk_sampler_dlss_bias. The old level's samplers +// stay until shutdown like every sampler: one set per level at most. Before the first sampler there is nothing to do: +// sampler_features reads the setting then. +void vk_sampler_aniso_live(void) { + static unsigned seen = ~0u; + unsigned v = cfg_version(); + if (v == seen) return; + seen = v; + lock(); + float a = aniso_setting(); + if (sf.init && a != sf.aniso) { + sf.aniso = a; + aniso_log(); + for (int i = 0; i < nsslots; i++) sslots[i].s = sslots[i].sp = sslots[i].sc = VK_NULL_HANDLE; // looked up again (the key moves) + samp_gen++; + } + unlock(); +} + // Caller holds the lock. static VkSampler sampler_for(VkCtx *c, const samp_par *s) { + sampler_features(c); samp_par k; memset(&k, 0, sizeof k); k = *s; if (dlss_bias != 0 && dlss_biased(&k)) k.lod_bias += dlss_bias; // part of the key: samplers live until shutdown + if (aniso_raised(&k)) k.aniso = sf.aniso; // likewise VkSampler v = (VkSampler)nvn_hm_get(&samplers, &k); if (!v) { v = make_sampler(c, &k); From c9942578e159cb49516b6a3371c0871aee46d095 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:40:22 -0600 Subject: [PATCH 14/16] Keep NVIDIA's shader disk cache beside the exe with a limit that holds the prepared set. NVIDIA's Linux driver compiles into a disk cache in ~/.cache/nvidia, shared with every program, and a process adds at most 1 GiB to it (__GL_SHADER_DISK_CACHE_SIZE); a start that finds more than that in several files deletes the later ones. nx2navi.exe --prepare-shaders builds the 42,142 variants of game 1.1.1 in one process, about 1.5 GB: on the default limit it stored about 30,700 of them, and the first start after the preparation compiled the rest (and the pipelines built from them) again. Unless the environment sets them, the game now points __GL_SHADER_DISK_CACHE_PATH at nvidia-cache beside the exe (made here, the driver only makes GLCache inside it) and __GL_SHADER_DISK_CACHE_SIZE at 8 GiB before its first Vulkan call, as it does for Mesa's cache. The docs name both. Co-Authored-By: Claude Sonnet 5.5 --- docs/README-BuildKit.md | 5 +++++ docs/building.md | 8 ++++++++ docs/installer.md | 4 +++- runtime/vk_device.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/README-BuildKit.md b/docs/README-BuildKit.md index c21e1a02..cb48cdb9 100644 --- a/docs/README-BuildKit.md +++ b/docs/README-BuildKit.md @@ -96,6 +96,11 @@ processor can fill memory and swap until the kernel stops processes of the deskt On NVIDIA, start the game with `NX2NAVI_SHADOW=0` if the 3D world stays black while the HUD draws (issue #72). The profile, logs and settings are in the `--out` folder as on Windows. +On NVIDIA the driver keeps the shaders it compiled in `nvidia-cache` beside `ocarina`, not in `~/.cache/nvidia`, where +one run adds at most 1 GiB while the preparation at the end of the build makes about 1.5 GB. The game sets the folder +and a limit of 8 GiB itself (`__GL_SHADER_DISK_CACHE_PATH` and `__GL_SHADER_DISK_CACHE_SIZE` in the environment take +precedence). Deleting the folder costs one slow start; `./ocarina --warm-shaders` fills it again. + `tools/make_appimage.py` packs this kit into one `NX2navi-Linux--x86_64.AppImage` with its own Python, for players without a source checkout: it checks for the packages above, installs zbic with pip on its first run, picks `--jobs` from the memory, builds into `~/Games/Ocarina` (or the folder given after the dump) and writes `play.sh` diff --git a/docs/building.md b/docs/building.md index 7126091c..154512c7 100644 --- a/docs/building.md +++ b/docs/building.md @@ -209,6 +209,14 @@ The probes open a window, so from an SSH session they need the desktop session's skipped: no graphics+compute+present queue family`: on Wayland `XDG_RUNTIME_DIR=/run/user/$(id -u) WAYLAND_DISPLAY=wayland-1 SDL_VIDEODRIVER=wayland` (the socket name is in `$XDG_RUNTIME_DIR`), on X11 `DISPLAY=:0`. +NVIDIA's Linux driver compiles into a disk cache of its own. A process adds at most `__GL_SHADER_DISK_CACHE_SIZE` to it +(1 GiB by default; at a start the files after the first are deleted while the folder is over that size), and the default +folder, `~/.cache/nvidia`, is shared with every program. `--prepare-shaders` of game 1.1.1 builds 42,142 variants, about +1.5 GB: on the default limit about 30,700 of them were stored and the rest compiled again at their first draw in play. +`runtime/vk_device.c` therefore points `__GL_SHADER_DISK_CACHE_PATH` at `nvidia-cache` beside `ocarina` and the size at +8 GiB before the first Vulkan call, each unless the environment sets it, as it does `MESA_SHADER_CACHE_DIR` for RADV. The +folder can be deleted; `ocarina --warm-shaders` fills it again. + Memory: on Windows the GPU imports guest memory and `GetWriteWatch` tracks the CPU's writes to it. On Linux, re-protecting imported memory invalidates the driver's userptr buffer on every submit (about 1.3 ms per 16 MiB on amdgpu), and importing pins the pages, which drops their write protection. Each CPU-visible pool therefore gets a diff --git a/docs/installer.md b/docs/installer.md index f8634574..ae1d5cc3 100644 --- a/docs/installer.md +++ b/docs/installer.md @@ -202,7 +202,9 @@ bin/ nx2navi_xlate, hactool, ninja, libSDL3.so.0 - The setup's temp folder is `.setup-temp` inside the install folder (`--temp` changes it): the AppImage is mounted read-only, and a Steam Deck's `/home` is often too full for the generated C while the game goes to the SD card. - The game keeps Mesa's shader cache in `mesa-cache` beside `nx2navi` unless `MESA_SHADER_CACHE_DIR` is set, for the - same reason: RADV fills its default folder in `~/.cache` up to 1 GB. + same reason: RADV fills its default folder in `~/.cache` up to 1 GB. NVIDIA's driver cache goes to `nvidia-cache` + beside it with a limit of 8 GiB unless `__GL_SHADER_DISK_CACHE_PATH` or `__GL_SHADER_DISK_CACHE_SIZE` is set: the + default limit of 1 GiB is smaller than what the shader preparation builds. - The window follows `LANG` for English or Russian, cancels through SIGTERM to the CLI's process group (the CLI handles it as Ctrl+Break on Windows) and opens the finished folder with `xdg-open`. - Requirements beyond Windows': Linux 6.7 or later (the write watch, `docs/building.md`) and glibc 2.34. diff --git a/runtime/vk_device.c b/runtime/vk_device.c index d3482d3f..75ab1463 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -72,9 +72,37 @@ static void mesa_cache_beside_exe(void) { plat_setenv("MESA_SHADER_CACHE_DIR", dir); } +#ifdef __linux__ +// NVIDIA's Linux driver keeps the pipelines it compiles in a disk cache of its own, by default in ~/.cache/nvidia, +// shared with every program. One process adds at most __GL_SHADER_DISK_CACHE_SIZE to it (1 GiB unless set), and a start +// that finds more than that in several files deletes the later ones. nx2navi.exe --prepare-shaders builds the 42,142 +// variants of game 1.1.1 into it in one process: with the default limit it stored about 30,700 of them and the rest +// were compiled again by the first start after it. So unless the environment sets them, the cache goes to nvidia-cache +// beside the exe, which nothing else writes, with a limit of NV_CACHE_BYTES. The driver makes GLCache inside that +// folder but not the folder, so it is made here. +#define NV_CACHE_BYTES "8589934592" // 8 GiB +static void nvidia_cache_beside_exe(void) { + char dir[PLAT_PATH_MAX * 2], size[32]; + if (!plat_getenv("__GL_SHADER_DISK_CACHE_PATH", dir, sizeof dir)) { + int len = plat_exe_path(dir, sizeof dir - 16); + char *cut = len ? plat_last_sep(dir) : NULL; + if (cut) { + strcpy(cut + 1, "nvidia-cache"); + plat_fs_mkdir(dir); // already there is fine + plat_setenv("__GL_SHADER_DISK_CACHE_PATH", dir); + } + } + if (!plat_getenv("__GL_SHADER_DISK_CACHE_SIZE", size, sizeof size)) + plat_setenv("__GL_SHADER_DISK_CACHE_SIZE", NV_CACHE_BYTES); +} +#endif + static int create_instance(void) { vk_dlssg_early(); // DLSS Frame Generation: Streamline before any Vulkan call of the process (its slInit asks so) mesa_cache_beside_exe(); +#ifdef __linux__ + nvidia_cache_beside_exe(); +#endif const char *ext[12]; uint32_t n_ext = (uint32_t)gfx_window_vk_extensions(ext, 7); const char *layers[] = {"VK_LAYER_KHRONOS_validation"}; From 662a6e88ee9dbb3a668ada01ce15c8eecdcb332f Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Mon, 9 Nov 2026 20:08:17 -0600 Subject: [PATCH 15/16] Run the game on the die with the larger L3 cache on processors whose dies differ (AMD's two-die X3D processors). Windows places a game on the V-Cache die only when it recognizes the exe as a game, and it does not know this one, so the threads were spread over both dies by frequency preference. cache_die = 1 (the default) narrows the process to the logical processors that share the largest L3; it never widens the set the process was given, and does nothing where every L3 has the same size. The recorder's reserved core is then chosen inside that set. Co-Authored-By: Claude Fable 5.2 <[EMAIL]> --- nx2navi.ini.example | 5 ++++ runtime/cfg.c | 4 ++- runtime/host.h | 1 + runtime/main.c | 1 + runtime/perf.c | 51 +++++++++++++++++++++++++++---------- runtime/platform/linux.c | 34 +++++++++++++++++++++++++ runtime/platform/platform.h | 4 +++ runtime/platform/win32.c | 26 +++++++++++++++++++ 8 files changed, 112 insertions(+), 14 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 13450974..98ebd431 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -66,6 +66,11 @@ # dyn_res = 0 # dyn_res_target = 16.7 # dyn_res_step = 45 +# Processors. On a processor whose dies have L3 caches of different sizes (AMD's two-die X3D processors, with 3D +# V-Cache on one die) the game runs on the die with the larger cache: 1 = on, 0 = Windows places the threads. +# cache_die = 1 +# One logical processor of each physical core, so no two busy threads share a core: 1 = on, 0 = all of them. +# one_per_core = 0 # Shaders and pipelines. # Draws whose pipeline is still building: default waits on a reserved worker; draw is the previous scheduling. diff --git a/runtime/cfg.c b/runtime/cfg.c index 761f485b..725d3a0d 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -38,7 +38,7 @@ static const char *const known[] = { "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", "BIND_SHAKE", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", - "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", + "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CACHE_DIE", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CRASH_FILTER_GUARD", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", @@ -111,6 +111,7 @@ static const struct { const char *key, *section; } sections[] = { {"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"POOL_CREATE_BUDGET", "performance"}, {"POOL_CREATE_MS", "performance"}, {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, + {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"TEX_DISUSE", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, @@ -204,6 +205,7 @@ static const CfgMeta metas[] = { {"RECORDER_CORE", "auto", "auto|0|1", RESTART}, {"RECORDER_PRIO", "1", "0|1", RESTART}, {"REPLAY_TIME", "0", "0|1", RESTART}, {"LOCK_TIME", "0", "0|1", RESTART}, {"POOL_CREATE_BUDGET", "16", "1..4096", RESTART}, {"POOL_CREATE_MS", "6", "0..100", RESTART}, // held submissions handed to the queue together (vk_sync.c, submit batching) {"SUBMIT_BATCH", "4", "1..16", RESTART}, + {"CACHE_DIE", "1", "0|1|ab", RESTART}, {"ONE_PER_CORE", "0", "0|1|ab", RESTART}, // perf.c cpu_placement // shaders and pipelines {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART}, {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, diff --git a/runtime/host.h b/runtime/host.h index f6c1efee..67ab7022 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -59,6 +59,7 @@ int perf_lock_holds_on(void); // NX2NAVI_LOCK_TIME void perf_lock_hold(void *site, uint64_t ns); // a lock held ns from site (vk_texture.c, holds over 1 ms) // NX2NAVI_LOCK_TIME: this thread's lock waits go into the frame-time summary extern void (*perf_rec_waits)(uint64_t *full_ns, uint64_t *drain_ns); // set by the back end (vk_rec.c totals) void perf_present(uint64_t frame); +void perf_cpu_start(void); // the process's logical processors (NX2NAVI_CACHE_DIE, NX2NAVI_ONE_PER_CORE), at start void perf_summary_flush(void); // prints the frame-time summary of the running period (exit); the 60 s lines come from perf_present void gfx_exit_log(void); // back end's lines for the exit summary (weak no-op in prof_win.c) void perf_gpu_wait(uint64_t t0); // a wait for a GPU fence ended (any thread; the render thread's also counts as PERF_FENCE) diff --git a/runtime/main.c b/runtime/main.c index dc27fc34..d59f6a15 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -547,6 +547,7 @@ int main(int argc, char **argv) { // Timed guest waits (kernel.c block_inner) sleep in whole milliseconds; at the default 15.6 ms tick a 1 ms wait // costs a full tick. NX2NAVI_TIMER_1MS=0 keeps the default for comparison. if (cfg_bool("NX2NAVI_TIMER_1MS")) plat_timer_1ms(); + perf_cpu_start(); // before any thread pool counts the usable processors { // NX2NAVI_POWER_FULL=0 leaves Windows' power throttling of an unfocused window in place (A/B runs) const char *pf = cfg_get("NX2NAVI_POWER_FULL"); if (!(pf && !strcmp(pf, "0"))) { diff --git a/runtime/perf.c b/runtime/perf.c index f0fd0f34..01390fa0 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -482,24 +482,49 @@ int rt_knob(const char *name, const char *def) { // render thread, the guest cores, the job workers) never share a core's execution units with another; "ab" switches // it with the A/B blocks. Off by default until measured here; the same rule // is reported to give +4-10% on an 8-core SMT CPU. +// +// NX2NAVI_CACHE_DIE=1, the default: on a processor whose dies have L3 caches of different sizes (AMD's two-die +// X3D processors, 3D V-Cache on one die) the process runs on the die with the larger cache, so the guest cores, the +// render thread and the recorder share one L3 and never cross dies. Windows' own placement of games on that die +// (game mode with AMD's driver) depends on recognizing the exe as a game. 0 leaves the placement to the OS; "ab" +// switches it with the A/B blocks. No change on one-die and Intel processors. +// +// The placement is set at start (perf_cpu_start, before the thread pools count the usable processors) and checked at +// every present for the A/B switches. The rules narrow the processors the process was given (start /affinity, a +// launcher's or a benchmark's pin), never widen them; with both off nothing is set. +static int one_mode = -1, die_mode; +static uint64_t cpu_all, cpu_one, cpu_die, cpu_applied; // cpu_applied: the mask set last, 0 none + static void cpu_placement(void) { - static int mode = -1, applied = -1; - static uint64_t all, one; - if (mode < 0) { - mode = rt_knob("ONE_PER_CORE", "0"); - int cores = plat_cpu_cores(&all, &one); - if (mode && one && one != all) - rt_log("cpu: %d cores, %d logical processors: one per core%s", cores, __builtin_popcountll(all), - mode == 2 ? " in the B blocks" : ""); + if (one_mode < 0) { + one_mode = rt_knob("ONE_PER_CORE", "0"); + die_mode = rt_knob("CACHE_DIE", "1"); + int cores = plat_cpu_cores(&cpu_all, &cpu_one); + uint32_t kb = 0; + cpu_die = plat_cpu_cache_die(&kb); + if (one_mode && cpu_one && cpu_one != cpu_all) + rt_log("cpu: %d cores, %d logical processors: one per core%s", cores, __builtin_popcountll(cpu_all), + one_mode == 2 ? " in the B blocks" : ""); else - mode = 0; + one_mode = 0; + if (cpu_die && cpu_die != cpu_all) + rt_log("cpu: logical processors %#llx of %#llx share the largest L3 cache (%u MiB)%s", + (unsigned long long)cpu_die, (unsigned long long)cpu_all, kb >> 10, + !die_mode ? "; cache_die = 0: not used" : die_mode == 2 ? ": the process runs there in the B blocks" + : ": the process runs there"); + if (!cpu_die || cpu_die == cpu_all) die_mode = 0; } - int on = RT_KNOB_ON(mode); - if (on == applied) return; - applied = on; - plat_process_affinity(on ? one : all); + uint64_t want = cpu_all; + if (RT_KNOB_ON(die_mode)) want &= cpu_die; + if (RT_KNOB_ON(one_mode)) want &= cpu_one; + if (!want) want = cpu_all; + if (want == (cpu_applied ? cpu_applied : cpu_all)) return; // nothing new; never restricted: as it was given + cpu_applied = want; + if (!plat_process_affinity(want)) rt_log("cpu: could not move the process to logical processors %#llx", (unsigned long long)want); } +void perf_cpu_start(void) { cpu_placement(); } + // NX2NAVI_CRASH_FILTER_GUARD (default 1; 0 off): once a second, the crash report's last-chance exception filter is put // back if a DLL loaded after the start (a GPU driver, an overlay) set its own in its place. Without it, a fault in such // a DLL ends the process with no FATAL line in the log. The first eight replacements are logged with the module. diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 8c1ed45a..60cf2c6d 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -155,6 +155,40 @@ int plat_cpu_cores(uint64_t *all, uint64_t *first) { return cores; } +// The L3 cache of each logical processor: the cache/index of /sys/devices/system/cpu/cpu whose level is 3, its +// size in KiB ("98304K"). +uint64_t plat_cpu_cache_die(uint32_t *l3_kb) { + cpu_set_t set; + *l3_kb = 0; + if (sched_getaffinity(0, sizeof set, &set)) return 0; + uint64_t die = 0, in_set = 0; + uint32_t big = 0, small = ~0u; + for (int i = 0; i < 64; i++) { + uint32_t kb = 0; + for (int k = 0; k < 8 && !kb; k++) { + char path[96], text[32] = ""; + uint64_t level = 0; + snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/cache/index%d/level", i, k); + if (!read_u64_file(path, &level)) break; + if (level != 3) continue; + snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/cache/index%d/size", i, k); + FILE *f = fopen(path, "r"); + if (f) { + if (fgets(text, sizeof text, f)) kb = (uint32_t)strtoul(text, NULL, 10) * (strchr(text, 'M') ? 1024 : 1); + fclose(f); + } + } + if (!kb) continue; + if (kb > big) big = kb, die = 0; + if (kb == big) die |= 1ull << i; + if (kb < small) small = kb; + if (CPU_ISSET(i, &set)) in_set |= 1ull << i; + } + if (!die || big == small) return 0; + *l3_kb = big; + return die & in_set; +} + // Linux has no process-wide affinity: each thread of /proc/self/task gets the mask, and threads started later inherit // it from the thread that starts them. int plat_process_affinity(uint64_t mask) { diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 8ad23a8e..09e9dad3 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -41,6 +41,10 @@ uint64_t plat_thread_cpu_ns(uint32_t tid); // CPU time of the thread with id ti // The logical processors the process may use (bit n = processor n, the first 64), into *all, and the lowest of each // physical core among them into *first; returns the number of physical cores, 0 when unknown. int plat_cpu_cores(uint64_t *all, uint64_t *first); +// The logical processors of the process's set (the first 64) that share the largest L3 cache, when the L3 caches +// are not all the same size: the die with AMD's 3D V-Cache on a two-die X3D processor. 0 when they are (one die, or +// equal dies) or the topology is unknown. *l3_kb: that cache's size. +uint64_t plat_cpu_cache_die(uint32_t *l3_kb); int plat_process_affinity(uint64_t mask); // every thread of the process to the processors in mask; 1 on success // Interrupting a thread (kernel.c's asynchronous preemption). A thread makes its own handle; another thread stops it diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 340fbec7..26f23de1 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -95,6 +95,32 @@ int plat_cpu_cores(uint64_t *all, uint64_t *first) { return cores; } +uint64_t plat_cpu_cache_die(uint32_t *l3_kb) { + DWORD_PTR proc, sys; + *l3_kb = 0; + if (!GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys)) return 0; + DWORD len = 0; + GetLogicalProcessorInformationEx(RelationCache, NULL, &len); + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buf = len ? malloc(len) : NULL; + uint64_t die = 0; + DWORD big = 0, small = ~0u; + if (buf && GetLogicalProcessorInformationEx(RelationCache, buf, &len)) + for (DWORD off = 0; off < len;) { + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *e = (void *)((char *)buf + off); + const CACHE_RELATIONSHIP *c = &e->Cache; + if (c->Level == 3 && c->GroupMask.Group == 0) { // every L3, also those outside the process's set + if (c->CacheSize > big) big = c->CacheSize, die = 0; + if (c->CacheSize == big) die |= c->GroupMask.Mask; + if (c->CacheSize < small) small = c->CacheSize; + } + off += e->Size; + } + free(buf); + if (!die || big == small) return 0; + *l3_kb = big >> 10; + return die & proc; +} + int plat_process_affinity(uint64_t mask) { return SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)mask) != 0; } uint64_t plat_thread_cpu_ns(uint32_t tid) { From 0cebadcfac2682c36aa654290d1c2750970d11b7 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 02:02:11 -0500 Subject: [PATCH 16/16] Leave the shader preparation modes on every processor the process was given. The large-cache die's placement is for the game's own threads. --prepare-shaders and --warm-shaders translate and build on as many processors as they find, so on a two-die processor the placement would have halved them. Co-Authored-By: Claude Fable 5.2 --- runtime/main.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/runtime/main.c b/runtime/main.c index d59f6a15..36cb77fb 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -547,7 +547,13 @@ int main(int argc, char **argv) { // Timed guest waits (kernel.c block_inner) sleep in whole milliseconds; at the default 15.6 ms tick a 1 ms wait // costs a full tick. NX2NAVI_TIMER_1MS=0 keeps the default for comparison. if (cfg_bool("NX2NAVI_TIMER_1MS")) plat_timer_1ms(); - perf_cpu_start(); // before any thread pool counts the usable processors + // Before any thread pool counts the usable processors. Not in the shader preparation modes: they translate and + // build on every processor the process was given, and the placement (perf.c cpu_placement) is for the game's own + // threads, so narrowing them to one die would only make the preparation take longer. + int prepare = 0; + for (int i = 1; i < argc; i++) + if (!strcmp(argv[i], "--prepare-shaders") || !strcmp(argv[i], "--warm-shaders")) prepare = 1; + if (!prepare) perf_cpu_start(); { // NX2NAVI_POWER_FULL=0 leaves Windows' power throttling of an unfocused window in place (A/B runs) const char *pf = cfg_get("NX2NAVI_POWER_FULL"); if (!(pf && !strcmp(pf, "0"))) {