From b303c2aa903ee50d203c5ac7cf5367426304de68 Mon Sep 17 00:00:00 2001 From: epicmeme546 <178368684+epicmeme546@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:12:21 -0500 Subject: [PATCH 01/15] Release render targets that were drawn over at another address, and keep released ones the game still samples. Ocarina lays its per-frame render targets out anew every frame in one GPU-only pool. When an early target changes its size, every target after it moves, and each move made a new entry with an image of its own while the old entry stayed live for as long as a pool descriptor named it. After 6,000 frames of walking through Kakariko Village 1,043 such entries held 6.1 GiB of device memory against 233 MiB in the targets still drawn. On a driver that charges device memory to the system commit this kept the commit low, and the idle textures of the area were evicted and uploaded again while walking. A GPU write in the GPU-only window now marks the entries at other addresses whose storage it runs over as overwritten and releases them. Their images go to the next texture made with the same parameters, or away after 600 presents. On the same walk the process's private memory ends at 23.2 GB instead of 29.3 GB and device memory in use at 8.5 GiB. A released render target whose content exists only in its image is no longer dropped after 120 presents under commit pressure: the game went on sampling such targets and took them back empty, which drew the scene black for one frame every two to three seconds. Such an entry now waits 1,800 presents, or eight times the longest it stayed released before the game took it back, and none goes while lookups still reach it. Co-Authored-By: Claude --- runtime/vk_texture.c | 174 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 157 insertions(+), 17 deletions(-) diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index df39a347..880730d9 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -53,6 +53,7 @@ typedef struct Tex { int uploaded, released; uint32_t rel_serial; // reg_serial when it was released: slots registered before then are stale (slot_resolve) uint64_t rel_frame; // gfx_counters.frames when it was released (drop_stale) + uint32_t back_gap; // most presents between a release and its taking back so far (stale_after) uint32_t n_up; // uploads of this entry (vk_texture_debug) int last_ch; // last vk_mem_changed result of make_fresh: 1 written, 0 clean, -1 not tracked struct Tex *next_at; // other entries at the same storage address @@ -78,6 +79,11 @@ typedef struct Tex { uint8_t superseded; // released, and another descriptor's entry was made at its address since (start_at) uint32_t rel_epoch; // submit_epoch when it was released struct Tex *ru_prev, *ru_next; // the reuse list of ik, newest release first + uint64_t gw_frame; // gfx_counters.frames at its creation or newest GPU write (overwrite_others) + uint64_t scan_frame; // one more than the frame vk_texture_gpu_written last looked for overwritten entries + uint64_t ow_frame; // gfx_counters.frames when overwritten was set + uint8_t overwritten; // released, and a texture at another address was drawn over its storage (overwrite_others) + uint8_t in_gwin; // in gwin[] } Tex; // A slot keeps what vk_texture_by_id resolved for it, so a draw binding skips the key lookup and the address chain: the @@ -321,12 +327,75 @@ static int format_compressed(VkFormat f) { // vkCmdClearColorImage takes no bl // them move to new addresses, new descriptors whose images were made again: vkCreateImage, the memory requirements and // the binding of a dedicated allocation cost 8% of the replay in Kokiri Forest. The released entries of each parameter // set are kept in a list, newest release first; create_tex takes the one nearest in address of those released at least -// two frames earlier, never during the current submission's replay, whose later parts may still use it. +// two frames earlier, never during the current submission's replay, whose later parts may still use it, and an +// overwritten one (below) before any other. static nvn_hmap reuse = {.klen = sizeof(ImgKey)}; // ImgKey -> the newest released reusable entry static int tex_reuse = -1; static uint64_t n_reused, n_reuse_new; // since the last perf line: images taken from a released entry, made anew static uint64_t n_restarts; // superseded entries taken back (start_at) +// Moved render targets. Ocarina keeps about a thousand texture objects for its per-frame render targets and lays the +// targets out anew every frame with a linear allocator in one GPU-only pool. A target early in that layout changes its +// size with an effect on screen (811x811, then 778x778 a frame later in Kakariko Village), and every target after it +// then lies about 100 KB further on: eleven targets, 55 MiB of images, at new addresses in each frame the size +// changes, each a new entry with an image of its own, as few of them can lie in the pool memory itself (pool_fits). +// The game finalizes an old texture object only when it needs the object again, 90 to 300 frames later, and a +// descriptor in the texture pool goes on naming it, so the entries were neither released nor idle: after 6,000 frames +// of walking through the village 1,043 of them held 6.1 GiB of device memory, against 233 MiB in the targets still +// drawn, and once released they waited GPU_STALE_FRAMES more. A driver that charges device memory to the system's +// commit kept the commit low from then on, and the idle textures of the area went and came back (commit_evict). +// +// On the console those targets are views of one pool's memory: when another target is drawn over a target's bytes, its +// content is gone. overwrite_others says so here. When the GPU writes a texture in the GPU-only address window +// (nvn_gpu_only), each entry at another address whose storage overlaps it is marked overwritten and released as if +// the game had finalized it. Its image then goes to the next texture made with the same parameters (create_tex), or +// away OVERWRITTEN_KEEP presents later (drop_overwritten), or as early as an intact entry's while memory is short +// (drop_stale). Left alone are an entry at the same address (start_at decides about those), an entry the written +// texture lies inside (a level or a layer of it, which a game draws through a texture of its own) and an entry +// written in this frame or the one before: two layouts that take turns within a frame keep their images. The mark +// goes when the game takes the entry back (entry_of). +// gwin: the entries this applies to (reusable, in the window), sorted by address; gwin_span: the largest storage size +// one of them had, which bounds how far below an address an overlapping entry can start. +#define OVERWRITE_FRAMES 2 +#define OVERWRITTEN_KEEP 600 +static struct Tex **gwin; +static int ngwin, cgwin; +static uint64_t gwin_span; +static uint64_t n_overwritten, n_over_drops; // since the last perf line: entries marked, dropped by drop_overwritten + +static int gwin_lower(uint64_t a) { + int lo = 0, hi = ngwin; + while (lo < hi) { + int m = (lo + hi) / 2; + if (gwin[m]->key.addr < a) lo = m + 1; + else hi = m; + } + return lo; +} + +static void gwin_add(Tex *e) { + if (ngwin == cgwin) { + cgwin = cgwin ? 2 * cgwin : 256; + if (!(gwin = realloc(gwin, sizeof *gwin * (size_t)cgwin))) rt_fatal("vk: out of memory for the render target list"); + } + int i = gwin_lower(e->key.addr); + memmove(gwin + i + 1, gwin + i, sizeof *gwin * (size_t)(ngwin - i)); + gwin[i] = e, ngwin++; + if (e->t.L.size > gwin_span) gwin_span = e->t.L.size; + e->in_gwin = 1; +} + +static void gwin_remove(Tex *e) { + if (!e->in_gwin) return; + for (int i = gwin_lower(e->key.addr); i < ngwin && gwin[i]->key.addr == e->key.addr; i++) + if (gwin[i] == e) { + memmove(gwin + i, gwin + i + 1, sizeof *gwin * (size_t)(ngwin - i - 1)); + ngwin--; + break; + } + e->in_gwin = 0; +} + static int tex_reuse_on(void) { if (tex_reuse < 0) { tex_reuse = cfg_bool("NX2NAVI_TEX_REUSE"); @@ -673,16 +742,20 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { // again every frame, and DLSS's scene target (2560x1440 at a fixed address in Kokiri Forest) lost its image // to a moving target of the same size in between, and DLSS its history (vk_dlaa.c evaluate), several times a // second. Not one at this address either: its own descriptor comes back for it (start_at). Of the others the - // one nearest in address, so a target the allocator moves keeps the image it had. + // one nearest in address, so a target the allocator moves keeps the image it had; an overwritten one first + // (overwrite_others): nothing can ask for its content again. uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); - Tex *a = NULL; - uint64_t best = UINT64_MAX; + Tex *a = NULL, *kept = NULL; + uint64_t best = UINT64_MAX, best_kept = UINT64_MAX; int n = 0; for (Tex *q = nvn_hm_get(&reuse, &e->ik); q && n < 128; q = q->ru_next, n++) { if (q->rel_epoch == submit_epoch || now - q->rel_frame < 2 || q->key.addr == k->addr) continue; uint64_t dist = q->key.addr > k->addr ? q->key.addr - k->addr : k->addr - q->key.addr; - if (dist < best) best = dist, a = q; + if (q->overwritten) { + if (dist < best) best = dist, a = q; + } else if (dist < best_kept) best_kept = dist, kept = q; } + if (!a) a = kept; if (a) { img = a->pub.image, e->alloc = a->alloc; vk_exec_image_gone(img); // the views and records of the texture it was @@ -714,6 +787,8 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { e->bytes = own.size; } e->reusable = e->alloc && gpu_only; + e->gw_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (e->reusable && nvn_gpu_only(k->addr)) gwin_add(e); // A new image's handle can be one a destroyed image had. Views are purged with the image (destroy_tex), so none // should be cached under it: one that is was made from a destroyed texture's record after its purge, and would // reach this image's memory through the dead one's. Dropped here, and counted in the log. @@ -835,6 +910,7 @@ static void rel_unlink(Tex *e) { static void destroy_tex(Tex *e) { own_remove(e); + gwin_remove(e); n_site[0]++; tex_gen_bump(e->key.addr); nvn_hm_del(&by_key, &e->key); @@ -959,11 +1035,28 @@ int vk_texture_commit_low(void) { return __atomic_load_n(&commit_low, __ATOMIC_R // again, empty if it is a render target. Under commit pressure that a system-managed page file never relieves, dropping // young ones destroyed 2160p render targets about every frame for whole sessions. Frame generation also reads a frame's // depth target at the host present (vk_dlssg.c vk_dlssg_frame tags it), after the blit deferred destruction waits for. +// +// A released entry is not dead to the game. A descriptor registered for a render target still resolves to its released +// entry (slot_resolve: a finalized slot with no live texture at its storage gets its own), and the game can sample it +// or take it back long after the release. In Kakariko Village, with the system's commit low, render targets dropped at +// STALE_FRAMES came back empty and the scene was drawn black for one frame every two to three seconds (interface +// intact), for as long as the commit stayed low. So no entry goes while lookups still reach it (idle), and one whose +// content exists only in its image waits GPU_STALE_FRAMES, or GAP_TIMES the longest it stayed released before the game +// took it back when that is longer. An overwritten entry has no content left to wait for (overwrite_others): it goes +// with the intact ones, whether or not a descriptor still names it. #define STALE_FRAMES 120 +#define GPU_STALE_FRAMES 1800 +#define GAP_TIMES 8 static int idle(const Tex *e, uint64_t now) { return look_round - e->use_round >= IDLE_ROUNDS && now - e->use_frame >= STALE_FRAMES; } +static uint64_t stale_after(const Tex *e) { + if (comes_back_intact(e) || e->overwritten) return STALE_FRAMES; + uint64_t gap = (uint64_t)e->back_gap * GAP_TIMES; + return gap > GPU_STALE_FRAMES ? gap : GPU_STALE_FRAMES; +} + static void drop_stale(uint64_t keep, uint64_t *count) { uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); int dropped = 0; @@ -971,12 +1064,26 @@ static void drop_stale(uint64_t keep, uint64_t *count) { for (Tex *e = rel_head, *next; e && now - e->rel_frame >= STALE_FRAMES; e = next) { // in release order if (dropped == PRESSURE_STEP || rel_bytes <= keep) return; next = e->rel_next; - if (!e->bytes || (!any && !comes_back_intact(e))) continue; + if (!e->bytes || (!any && !comes_back_intact(e) && !e->overwritten)) continue; + if (now - e->rel_frame < stale_after(e) || (!e->overwritten && !idle(e, now))) continue; destroy_tex(e); dropped++, (*count)++; } } +// Drops up to PRESSURE_STEP entries that were marked overwritten OVERWRITTEN_KEEP presents ago or earlier +// (overwrite_others): the image reuse found no texture to give their images to in that time. +static void drop_overwritten(void) { + uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + int dropped = 0; + for (Tex *e = rel_head, *next; e && now - e->rel_frame >= OVERWRITTEN_KEEP && dropped < PRESSURE_STEP; e = next) { + next = e->rel_next; + if (!e->overwritten || now - e->ow_frame < OVERWRITTEN_KEEP) continue; + destroy_tex(e); + dropped++, n_over_drops++; + } +} + // NX2NAVI_TEX_EVICT (default on; 0 = only released entries go under VRAM pressure, as before): above the VRAM pressure // threshold, once the released entries that come back intact are gone, live entries go too, least recently looked up // first: those that come back intact and that no lookup reached for IDLE_ROUNDS pool walks and STALE_FRAMES presents @@ -1075,6 +1182,7 @@ static void sweep_pressure(void) { // tex_lock held if (tex_evict && freed < over) evict_idle(over - freed, used, budget); } if (rel_head) { + drop_overwritten(); uint64_t cap = released_cap_bytes(); if (rel_bytes > cap) drop_stale(cap, &n_cap_drops); if (rel_bytes > COMMIT_LOW_KEEP && commit_is_low()) drop_stale(COMMIT_LOW_KEEP, &n_commit_drops); @@ -1755,7 +1863,10 @@ static Tex *entry_of(const tex_t *t) { vk_texstat_time(VK_TS_CREATE, c0); } if (e->released) { + uint64_t gap = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) - 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_bump(k.addr); + e->overwritten = 0; if (e->superseded) { // its image may still be in use: the first barrier waits for all earlier work e->superseded = 0, e->reused = 1; n_restarts++; @@ -1813,6 +1924,34 @@ static void wb_cfg_init(void) { // up to 9 frames in a row while a tile loads. static int wb_wanted(const Tex *e) { return e->t.L.size <= wb_limit || e->run < WB_RUN; } +static void mark_released(Tex *e) { // tex_lock held; e is not released + e->released = 1; + e->rel_epoch = submit_epoch; + e->rel_serial = reg_serial; + e->rel_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + nreleased++; + rel_append(e); + n_site[3]++; + tex_gen_bump(e->key.addr); +} + +// e, in the GPU-only address window, is written by the GPU in frame fr: the entries its storage runs over are +// overwritten (section "Moved render targets", above gwin). +static void overwrite_others(const Tex *e, uint64_t fr) { + uint64_t lo = e->key.addr, hi = lo + e->t.L.size; + int released = 0; + for (int i = gwin_lower(lo > gwin_span ? lo - gwin_span : 0); i < ngwin && gwin[i]->key.addr < hi; i++) { + Tex *q = gwin[i]; + uint64_t qlo = q->key.addr, qhi = qlo + q->t.L.size; + if (q->overwritten || qlo == lo || qhi <= lo || (lo >= qlo && hi <= qhi)) continue; + if (fr - q->gw_frame < OVERWRITE_FRAMES) continue; + q->overwritten = 1, q->ow_frame = fr; + n_overwritten++; + if (!q->released) mark_released(q), released = 1; + } + if (released) sweep_released(); +} + void vk_texture_gpu_written(const tex_t *t) { if (!t || !t->mem) return; vk_exec_frame_written(t); @@ -1820,6 +1959,12 @@ void vk_texture_gpu_written(const tex_t *t) { Tex *e = entry_of(t); if (e) { make_fresh(e); // a partial render keeps what the storage held before + uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + e->gw_frame = now; + if (e->scan_frame != now + 1 && nvn_gpu_only(e->key.addr)) { // once per frame and entry + e->scan_frame = now + 1; + overwrite_others(e, now); + } e->pub.gpu_owned = 1; e->chash_ok = 0; // the image now holds the GPU's content: matching storage bytes no longer mean "unchanged" uint64_t f = vk_exec_recording_fence(); @@ -1853,14 +1998,7 @@ void vk_texture_release(const tex_t *t) { lock(); Tex *e = nvn_hm_get(&by_key, &k); if (e && !e->released) { - e->released = 1; - e->rel_epoch = submit_epoch; - e->rel_serial = reg_serial; - e->rel_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); - nreleased++; - rel_append(e); - n_site[3]++; - tex_gen_bump(k.addr); + mark_released(e); sweep_released(); } unlock(); @@ -2140,13 +2278,15 @@ void vk_texture_new_submit(void) { n_checks = 0; rt_log("perf: frame %llu: released textures kept %d (%.0f MiB of own memory), dropped past the size limit %llu, " "under VRAM pressure %llu, under commit pressure %llu; idle live textures evicted %llu (%.1f MiB); images " - "reused %llu, made %llu, pool bindings not tried %llu, superseded taken back %llu", + "reused %llu, made %llu, pool bindings not tried %llu, superseded taken back %llu; render targets " + "overwritten at another address %llu, dropped unused %llu", (unsigned long long)f * 60, nreleased, rel_bytes / 1048576.0, (unsigned long long)n_cap_drops, (unsigned long long)n_pressure_drops, (unsigned long long)n_commit_drops, (unsigned long long)n_evict, n_evict_bytes / 1048576.0, (unsigned long long)n_reused, (unsigned long long)n_reuse_new, - (unsigned long long)n_bind_skips, (unsigned long long)n_restarts); + (unsigned long long)n_bind_skips, (unsigned long long)n_restarts, (unsigned long long)n_overwritten, + (unsigned long long)n_over_drops); n_cap_drops = n_pressure_drops = n_commit_drops = n_evict = n_evict_bytes = 0; - n_reused = n_reuse_new = n_bind_skips = n_restarts = 0; + n_reused = n_reuse_new = n_bind_skips = n_restarts = n_overwritten = n_over_drops = 0; } unlock(); } From 76c3661aff0cf7e0f7a5bbb1d9f5dc2992399783 Mon Sep 17 00:00:00 2001 From: epicmeme546 <178368684+epicmeme546@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:17:12 -0500 Subject: [PATCH 02/15] Keep the vertex layout in the pipeline on AMD's RDNA 3 and newer driver, and save the pipeline cache while a large compute shader builds. AMD's driver for RDNA 3 and newer compiles a pipeline that has dynamic vertex input again at every boot: neither pipeline-cache.bin nor the driver's own disk cache gives it back. On an RX 7900 XT (26.10.1) a player's key file of 3,979 records used the whole 20 s boot budget at every boot. Dynamic vertex input is now off by default on that driver (vk_caps.amd_llpc); with the vertex layout in the pipeline the third boot of that file built 4,273 pipelines in 0.9 s, all from the file. NX2NAVI_VK_DYN_VERTEX=1 turns it back on. The RDNA 1 and 2 driver branch is not measured and keeps it. The background builder of large compute shaders now compiles into a pipeline cache of its own and merges it into the main one when the build is done. A create into the main cache holds the cache's lock for its whole length and the cache is only saved while nobody holds it, so a build of minutes kept pipeline-cache.bin from being written at all: on the same card one compute shader of 4,874,502 SPIR-V words was still compiling 590 s after the boot, and nothing built in that session reached the file. NX2NAVI_PIPE_FEEDBACK=2 also logs every graphics pipeline build with hashes of its modules and its fixed state, which is how the recompiles were found. Co-Authored-By: Claude --- runtime/vk.h | 3 +- runtime/vk_caps.c | 1 + runtime/vk_pipeline.c | 92 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 82 insertions(+), 14 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 676cb43a..23c8f5e7 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -61,10 +61,11 @@ typedef struct { int gpl; // VK_EXT_graphics_pipeline_library enabled with fast linking (NX2NAVI_GPL=0: off) int amd; // an AMD GPU (vendor 0x1002), any driver int amd_sc; // AMD's Windows driver branch for RDNA 1 and 2 ("AMD proprietary shader compiler") + int amd_llpc; // AMD's own driver for RDNA 3 and newer (its driverInfo names LLPC) int mesa; // a Mesa driver the game runs on: RADV, ANV or NVK } VkCaps; extern VkCaps vk_caps; -void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, mesa +void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, amd_llpc, mesa void vk_caps_features(VkPhysicalDeviceFeatures *f); // before vkCreateDevice (NX2NAVI_VK_NO_FLOAT64 clears shaderFloat64) // Before vkCreateDevice, with the driver's graphics pipeline library feature and properties: sets vk_caps.gpl, // 1 = enable the extensions (VK_KHR_pipeline_library, VK_EXT_graphics_pipeline_library) and the feature. diff --git a/runtime/vk_caps.c b/runtime/vk_caps.c index 771fe840..c91041c3 100644 --- a/runtime/vk_caps.c +++ b/runtime/vk_caps.c @@ -44,6 +44,7 @@ void vk_caps_driver(VkPhysicalDevice p) { // AMD split RDNA 1 and 2 off into a driver branch of their own; its driverInfo reads // "26.10.1 (AMD proprietary shader compiler)" where the RDNA 3 and 4 driver names LLPC. vk_caps.amd_sc = drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY && strstr(drv.driverInfo, "shader compiler"); + vk_caps.amd_llpc = drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY && !vk_caps.amd_sc; vk_caps.mesa = drv.driverID == VK_DRIVER_ID_MESA_RADV || drv.driverID == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA || drv.driverID == VK_DRIVER_ID_MESA_NVK; } diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index f5659f3c..2d12ff92 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -79,7 +79,7 @@ static struct { int dyn_vi, dyn_clamp, dyn_poly, dyn_blend_en, dyn_blend_eq, dyn_wmask, dyn_a2c; int f_logic_op, f_depth_clamp, f_fill_non_solid, f_dual_src, f_full_subgroups; int cache_control; // pipelineCreationCacheControl: a create can ask for the pipeline caches only - int feedback; // NX2NAVI_PIPE_FEEDBACK: 1 count pipeline cache hits, 2 also log each compute build + int feedback; // NX2NAVI_PIPE_FEEDBACK: 1 count pipeline cache hits, 2 also log each compute and graphics build uint64_t cache_loaded; // bytes of pipeline-cache.bin the driver took at boot int pv_last; // provokingVertexLast enabled: NVN's default is the last vertex VkShaderStageFlags sg32_stages; // stages that accept requiredSubgroupSize = 32 (0: not available) @@ -509,11 +509,19 @@ typedef struct { VkStructureType sType; const void *pNext; VkPipelineCreateFlags // copy, reading through a null entry, with compute and library builds running beside the autosave. static PlatMutex cache_rw = PLAT_MUTEX_INIT; +// A pipeline cache of this thread's own that its creates build into in place of P.cache; VK_NULL_HANDLE (every thread +// but the background builder of large compute shaders) for P.cache itself. A create into it does not hold cache_rw, so +// a build of minutes does not keep pipeline-cache.bin from being saved; bg_merge moves its content into P.cache. +static __thread VkPipelineCache own_cache; +static volatile uint32_t cache_merges; // bg_merge's merges into P.cache: new content for autosave_once to save + static VkResult pipe_create_in(int kind, void *ci, VkPipeline *pipe, VkPipelineCache cache) { - if (cache) plat_mutex_lock_shared(&cache_rw); + int shared = cache && !own_cache; + if (cache && own_cache) cache = own_cache; + if (shared) plat_mutex_lock_shared(&cache_rw); VkResult r = kind == 2 ? vkCreateComputePipelines(P.device, cache, 1, ci, NULL, pipe) : vkCreateGraphicsPipelines(P.device, cache, 1, ci, NULL, pipe); - if (cache) plat_mutex_unlock_shared(&cache_rw); + if (shared) plat_mutex_unlock_shared(&cache_rw); return r; } @@ -723,14 +731,30 @@ static int ensure_init(void) { } else { rt_log("vk_pipeline: descriptor update templates unavailable: sets are written with write structs"); } - // NX2NAVI_VK_DYN_VERTEX=0/1 forces dynamic vertex input off or on. Default off on Intel's frozen Windows driver for its - // older integrated GPUs (UHD 630, Ice Lake: Vulkan 1.3.215 and older), which draws nothing with it. + // NX2NAVI_VK_DYN_VERTEX=0/1 forces dynamic vertex input off or on. Default off on two drivers: + // - Intel's frozen Windows driver for its older integrated GPUs (UHD 630, Ice Lake: Vulkan 1.3.215 and older), which + // draws nothing with it. + // - AMD's driver for RDNA 3 and newer (vk_caps.amd_llpc), which compiles a pipeline with dynamic vertex input again + // at every boot: neither pipeline-cache.bin nor the driver's own disk cache gives it back. Measured on an + // RX 7900 XT (26.10.1) with NX2NAVI_PIPE_FEEDBACK=2, booting one save twice from an empty cache: the second boot + // compiled all 622 recorded non-mesh pipelines again at 83 ms each, with the module and state hashes of the first + // boot (the same with NX2NAVI_GPL=0, so it is not the pipeline libraries or their layouts), while the mesh + // pipelines, which have no vertex input, came from the file. With the vertex layout in the pipeline the second + // boot built 787 pipelines in 170 ms and the third 898 of 898 from the file in 233 ms, with no build left for the + // scene. A player's file of 3,979 records used the whole boot budget (PREWARM_PIPE_MS, 20 s) at every boot; with + // the vertex layout in the pipeline its third boot built 4,273 pipelines in 0.9 s. The price is more pipelines, + // one per vertex layout (787 records against 692 for that boot); records written with dynamic vertex input still + // prebuild, each for the layout of the draw that recorded it. The RDNA 1 and 2 branch (vk_caps.amd_sc) is not + // measured and keeps dynamic vertex input. const char *dv = cfg_get("NX2NAVI_VK_DYN_VERTEX"); // no fixed default: it depends on the GPU, see above int intel_legacy = c->props.vendorID == 0x8086 && c->props.apiVersion <= VK_MAKE_API_VERSION(0, 1, 3, 215); - int dv_on = dv && *dv ? strcmp(dv, "0") != 0 : !intel_legacy; + int dv_on = dv && *dv ? strcmp(dv, "0") != 0 : !intel_legacy && !vk_caps.amd_llpc; P.dyn_vi = dv_on && c->has_vertex_input_dynamic_state && PROC(set_vi, "vkCmdSetVertexInputEXT"); if (!dv_on && c->has_vertex_input_dynamic_state) - rt_log("vk_pipeline: dynamic vertex input off (%s)", dv && *dv ? "NX2NAVI_VK_DYN_VERTEX=0" : "Intel's older Windows driver draws nothing with it"); + rt_log("vk_pipeline: dynamic vertex input off (%s)", + dv && *dv ? "NX2NAVI_VK_DYN_VERTEX=0" + : intel_legacy ? "Intel's older Windows driver draws nothing with it" + : "AMD's driver compiles pipelines that have it again at every boot"); if (c->has_eds3) { P.dyn_clamp = e3.extendedDynamicState3DepthClampEnable && P.f_depth_clamp && PROC(set_clamp, "vkCmdSetDepthClampEnableEXT"); P.dyn_poly = e3.extendedDynamicState3PolygonMode && PROC(set_poly, "vkCmdSetPolygonModeEXT"); @@ -1977,7 +2001,8 @@ static VkPipelineCreateFlags noopt_flag(void) { // with the prewarm, the stage library prebuild and every cache save, so a log tells whether a slow boot compiled or // read its pipelines: on NVIDIA the driver's own disk cache is kept per executable, so a renamed or new exe gets nothing // from it, and only pipeline-cache.bin can make its first boot quick. =2 also logs every compute build with a hash of -// its final module, so two runs can be compared for a module that changes between them. +// its final module, and every graphics pipeline build with the hashes of its final modules and of its fixed state, so +// two runs can be compared for a module or a state that changes between them. enum { FB_GRAPHICS, FB_LIBRARY, FB_COMPUTE, FB_KINDS }; static struct { PlatMutex lock; @@ -2125,7 +2150,7 @@ static uint32_t vs_union_missing(const VkPipeState *s, GfxShaderIo fin[32]) { static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Layout *l, uint32_t fs_off) { VkShaderModule vm = VK_NULL_HANDLE, fm = VK_NULL_HANDLE; - uint64_t vw, fw; + uint64_t vw, fw, vm_hash = 0, fm_hash = 0; // the hashes: of the final modules, for the PIPE_FEEDBACK 2 line const uint32_t *vcode = gfx_shader_spirv(s->vs, &vw); uint32_t *vpatched = NULL, *fpatched = NULL; int mesh = is_mesh(s); @@ -2176,9 +2201,9 @@ static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Lay fcode = fpatched = r; } // A bound colour target outside the shader's output map gets write mask 0 (target_wmask) - if (fcode) fm = make_module(fcode, fw); + if (fcode) fm = make_module(fcode, fw), fm_hash = mm_hash; } - vm = make_module(vcode, vw); + vm = make_module(vcode, vw), vm_hash = mm_hash; free(vpatched), free(fpatched); VkPipeline pipe = VK_NULL_HANDLE; if (!vm || (s->fs && !fm) || (mesh && s->ts && !tm)) goto out; @@ -2281,6 +2306,21 @@ static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Lay } else { perf_count(PERF_CNT_PIPE_BUILT); fb_note(FB_GRAPHICS, &fb); + if (P.feedback >= 2) { // the state hash: what the create info takes from the key besides the modules + uint64_t st = fnv(fnv(FNV0, cba, sizeof cba), k->color, sizeof k->color); + if (!P.dyn_vi) // the vertex layout is pipeline state then (zero in a mesh pipeline's key) + st = fnv(fnv(fnv(fnv(st, k->attr_fmt, sizeof k->attr_fmt), k->attr_off, sizeof k->attr_off), + k->attr_bind, sizeof k->attr_bind), k->rate, sizeof k->rate); + uint32_t bits[] = {(uint32_t)mesh, k->topo_class, k->minus_w, k->clamp, k->poly_mode, k->samples, k->a2c, + k->logic_op, k->n_color, k->fbl, k->dsb, l->set1 != 0, fs_off, pi.flags, k->depth, k->stencil}; + rt_log("vk_pipeline: graphics: modules %016llx %016llx, state %016llx (mesh %d, topology class %u, samples %u, " + "colors %u, feedback loop %u, depth format %u, stage-independent layout %d, flags 0x%x), %s, %.1f ms", + (unsigned long long)vm_hash, (unsigned long long)fm_hash, (unsigned long long)fnv(st, bits, sizeof bits), + mesh, (unsigned)k->topo_class, (unsigned)k->samples, (unsigned)k->n_color, (unsigned)k->fbl, + (unsigned)k->depth, l->set1 != 0, (unsigned)pi.flags, + pb_last ? "from pipeline-binaries.bin" : fb.flags & VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT ? "from pipeline-cache.bin" : "compiled", + (double)fb.duration / 1e6); + } } out: if (vm) vkDestroyShaderModule(P.device, vm, NULL); @@ -4144,6 +4184,25 @@ static double bg_growth_cap(void) { return cap; } +// The builder compiles into a pipeline cache of its own (own_cache at pipe_create_in), not into P.cache. A create into +// P.cache holds cache_rw shared from start to end, and the cache is only saved at a moment when nobody holds it: on an +// RX 7900 XT (26.10.1) the largest of these shaders (4,874,502 SPIR-V words) was still compiling 590 s after the boot, +// pipeline-cache.bin was not written once in that time, and the game was closed before the build ended, so the next +// boot found neither that shader nor the pipelines built in play and started the same build again. bg_merge moves the +// result into P.cache when the build is done, at a moment with no build into P.cache running: the lock is tried as +// cache_save tries it, because a waiting exclusive request would hold back every new build. +static void bg_merge(VkPipelineCache own, int built) { + int locked = 0; + while (built && !LB.stop && !(locked = plat_mutex_trylock(&cache_rw))) plat_sleep_ms(2); + if (locked) { + VkResult r = vkMergePipelineCaches(P.device, P.cache, 1, &own); + plat_mutex_unlock(&cache_rw); + if (r == VK_SUCCESS) cache_merges++; + else rt_log("vk_pipeline: background compute build: its cache did not merge into %s (%d)", P.cache_path, (int)r); + } + vkDestroyPipelineCache(P.device, own, NULL); +} + static void bg_main(void *arg) { (void)arg; plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); @@ -4216,8 +4275,13 @@ static void bg_main(void *arg) { else BG.failed++, rt_log("vk_pipeline: background compute build: %s", why ? why : "no layout"); continue; } + VkPipelineCache own = VK_NULL_HANDLE; + VkPipelineCacheCreateInfo oci = {.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO}; + if (P.cache && vkCreatePipelineCache(P.device, &oci, NULL, &own) != VK_SUCCESS) own = VK_NULL_HANDLE; double t0 = now_ms(); + own_cache = own; // without one the build goes into P.cache as before VkPipeline pipe = compute_create(cs, l, 0); + own_cache = VK_NULL_HANDLE; double ms = now_ms() - t0; plat_mutex_lock(&P.lock); if (flight) cs_flight_remove(cs); @@ -4229,6 +4293,7 @@ static void bg_main(void *arg) { vkDestroyPipeline(P.device, pipe, NULL); } plat_mutex_unlock(&P.lock); + if (own) bg_merge(own, pipe != VK_NULL_HANDLE); // after the entry: a dispatch waiting for it does not wait for this if (plat_xchg32(&BG.raised, 0)) plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); if (pipe) BG.built++, BG.ms += ms; else BG.failed++; @@ -4853,14 +4918,15 @@ static void autosave_once(void) { if (now - last_ms < 20000) return; VkPipeStats s; vk_pipeline_stats(&s); - if (s.graphics + s.compute == last_n) return; + uint64_t n = s.graphics + s.compute + cache_merges; // a merge comes after its pipeline was counted (bg_main) + if (n == last_n) return; // Only at a moment with no build running (cache_save): while one runs the save waits for the next tick, 500 ms // on, before it logs or flushes anything, so a busy stretch does not repeat the statistics lines every tick. if (P.cache && P.cache_path[0]) { if (!plat_mutex_trylock(&cache_rw)) return; plat_mutex_unlock(&cache_rw); } - last_n = s.graphics + s.compute, last_ms = now; + last_n = n, last_ms = now; save_with(0); } From e3e2d999c0896a8c8ff0b275040ba469fe9347e1 Mon Sep 17 00:00:00 2001 From: ajcohn716 <105183780+ajcohn716@users.noreply.github.com> Date: Mon, 9 Nov 2026 15:46:41 -0800 Subject: [PATCH 03/15] Recognize RGB9E5 texels when selecting integer storage views --- runtime/vk_exec_draw.c | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 73ec22e9..666ff689 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -578,6 +578,7 @@ static int texel_bytes(VkFormat f) { case VK_FORMAT_R16G16_SINT: case VK_FORMAT_R16G16_SFLOAT: case VK_FORMAT_R16G16_UNORM: case VK_FORMAT_R8G8B8A8_UINT: case VK_FORMAT_R8G8B8A8_SINT: case VK_FORMAT_R8G8B8A8_UNORM: case VK_FORMAT_B8G8R8A8_UNORM: case VK_FORMAT_A2B10G10R10_UINT_PACK32: case VK_FORMAT_A2B10G10R10_UNORM_PACK32: case VK_FORMAT_B10G11R11_UFLOAT_PACK32: + case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32: return 4; case VK_FORMAT_R32G32_UINT: case VK_FORMAT_R32G32_SINT: case VK_FORMAT_R32G32_SFLOAT: case VK_FORMAT_R16G16B16A16_UINT: case VK_FORMAT_R16G16B16A16_SINT: case VK_FORMAT_R16G16B16A16_SFLOAT: case VK_FORMAT_R16G16B16A16_UNORM: return 8; From 4d330dd76e26310fb177dce957d6dedc4d151203 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Tue, 10 Nov 2026 00:38:53 +0100 Subject: [PATCH 04/15] Keep the upscaler a frame started with, so choosing a DLSS mode while FSR 3.1 runs no longer calls NGX before it is loaded. vk_dlaa_frame decides at each present whether DLAA runs, with FSR 3.1 when NX2NAVI_TAA is fsr3 and NGX is not up. evaluate read NX2NAVI_TAA again. Choosing a DLSS mode in the menu sets it to dlss, which only takes effect after a restart when the run started with FSR 3.1; when that happened between the two reads, evaluate took DLSS's path and called N.alloc_params, which is null without NGX (issue #325). The choice is now kept from vk_dlaa_frame (fsr_frame), and evaluate returns when neither upscaler can run, until the next present turns DLAA off. --- runtime/vk_dlaa.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 04f6ac09..31eecb48 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -118,6 +118,7 @@ int vk_dlaa_wanted(void) { // The temporal upscaler behind dlaa: NVIDIA DLSS through NGX, once NGX is up, or else AMD FSR 3.1, which takes the same // inputs. The DLSS mode setting (NX2NAVI_DLSS) is the render scale for both. static int fsr3(void) { return !N.ngx_up && !strcmp(cfg_value("NX2NAVI_TAA"), "fsr3"); } +static int fsr_frame; // fsr3() as vk_dlaa_init and then vk_dlaa_frame read it: the upscaler this frame runs with int vk_dlaa_fsr3(void) { return vk_dlaa_wanted() && fsr3(); } // The upscaler the menu has set cannot start in this run: DLSS when NGX was never tried (a device made without its @@ -242,6 +243,7 @@ static int utf8_to_wide(const char *in, wchar_t *out, size_t cap) { void vk_dlaa_init(void) { if (vk_dlaa_wanted() && fsr3()) { + fsr_frame = 1; N.available = vk_ffx_load(); rt_log("dlaa: the temporal upscaler is AMD FSR 3.1%s", N.available ? "" : ", which is missing: the game's own scaling"); return; @@ -441,6 +443,10 @@ const VkTexImage *vk_dlaa_swap_img; VkImageView vk_dlaa_swap_view; static int dl_active(void) { return N.available && !D.failed && D.on; } +// The upscaler this frame runs with, as vk_dlaa_frame read it. The menu can set NX2NAVI_TAA to dlss between that and +// evaluate; read again there while NGX is not up (DLSS starts only after a restart), it sent evaluate to DLSS's path +// with NGX's functions not loaded, a call to address 0 (issue #325). +static int use_fsr(void) { return !N.ngx_up && fsr_frame; } static void dl_fail(const char *what, NgxResult r) { rt_log("dlaa: %s failed (%08x): DLAA off for this run", what, (unsigned)r); @@ -1296,7 +1302,8 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { struct Size *e = size_for(cb, w, h, ow, oh); if (!e) return dl_fail("work image creation", 0); if (e != D.last) D.reset = 1, D.why[0]++, e->chars_ok = 0; // another size's history is from another moment - if (!fsr3() && !D.params && !NGX_OK(N.alloc_params(&D.params))) return dl_fail("parameter allocation", 0); + if (!use_fsr() && !N.ngx_up) return; // neither runs this frame: vk_dlaa_frame turns DLAA off at the next present + if (!use_fsr() && !D.params && !NGX_OK(N.alloc_params(&D.params))) return dl_fail("parameter allocation", 0); NgxParam *p = D.params; VkImageView zv = vk_exec_target_view(z->img, z->format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT, z->level, z->base_layer, 1, 0); @@ -1320,7 +1327,7 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { uint64_t c0 = timed ? kern_now_ns() : 0; rec_drain(); // NGX records into the command buffer itself: the recorder thread must have made every call so far if (timed) T.cpu_drain += kern_now_ns() - c0; - if (fsr3()) { // FSR 3.1 (vk_ffx.cpp) in DLSS's place: the same images, then the same blit below + if (use_fsr()) { // FSR 3.1 (vk_ffx.cpp) in DLSS's place: the same images, then the same blit below if (t->level || t->base_layer || z->level || z->base_layer) return dl_fail("FSR 3.1 on a target that is not level 0, layer 0", 0); if (D.last_color != t->image) target_changed(t); uint64_t now = plat_now_ns(); @@ -1652,7 +1659,10 @@ void vk_dlaa_frame(void) { vk_dlaa_swap_img = NULL, vk_dlaa_swap_view = VK_NULL_HANDLE; dlss_mode(); preset_check(); - if (!N.ngx_up) N.available = fsr3() && cfg_bool("NX2NAVI_DLAA") && vk_ffx_load(); // FSR 3.1 follows the setting + if (!N.ngx_up) { // FSR 3.1 follows the setting + fsr_frame = fsr3(); + N.available = fsr_frame && cfg_bool("NX2NAVI_DLAA") && vk_ffx_load(); + } int on = N.available && cfg_bool("NX2NAVI_DLAA"); if (on != D.on) { rt_log("dlaa: %s", on ? "on" : "off"); From 3cdb3b810bf59bdc0bcca5726e9d5f4f63b7fee3 Mon Sep 17 00:00:00 2001 From: peigongdsd Date: Tue, 10 Nov 2026 08:47:36 +0800 Subject: [PATCH 05/15] Enable GPU pass timestamp labels without vendor breadcrumbs --- runtime/vk_crumbs.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/runtime/vk_crumbs.c b/runtime/vk_crumbs.c index 1266fafc..a40ecb88 100644 --- a/runtime/vk_crumbs.c +++ b/runtime/vk_crumbs.c @@ -105,8 +105,13 @@ static void pass_marks_init(VkCtx *c) { set_checkpoint = c->has_nv_checkpoints ? (PFN_vkCmdSetCheckpointNV)vk_dpa(c->device, "vkCmdSetCheckpointNV") : NULL; pm_amd = !off && write_marker && words; - vk_pass_marks_on = pm_amd || set_checkpoint; - if (vk_pass_marks_on) + // Timestamp profiling needs pass labels, but no vendor breadcrumb extension. + const char *timing = cfg_get("NX2NAVI_GPU_PASSES"); + int timestamps = timing && *timing && strcmp(timing, "0"); + vk_pass_marks_on = pm_amd || set_checkpoint || timestamps; + if (timestamps && !pm_amd && !set_checkpoint) + rt_log("vk: pass labels on for timestamp profiling; vendor breadcrumbs unavailable"); + else if (vk_pass_marks_on) rt_log("vk: pass marks on (%s%s%s; NX2NAVI_PASS_CRUMBS=0 turns them off%s)", pm_amd ? "VK_AMD_buffer_marker" : "", pm_amd && set_checkpoint ? " and " : "", set_checkpoint ? "VK_NV_device_diagnostic_checkpoints" : "", set_checkpoint ? "" : ", =1 adds NVIDIA checkpoints where the driver has them"); From b88439e2e00cf2dd22f3da635c080a7e1239ffbf Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Mon, 9 Nov 2026 19:47:37 -0500 Subject: [PATCH 06/15] Reflect every location in vertex and fragment interface arrays. --- docs/maxwell.md | 7 +++++-- runtime/gfx_shader.c | 36 +++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/docs/maxwell.md b/docs/maxwell.md index 830770e5..ba9f1814 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -115,8 +115,11 @@ Grep-level look on 2026-10-22, statuses updated 2026-10-26; "saw" is what the gr `provokingVertexLast` and chains `VkPipelineRasterizationProvokingVertexStateCreateInfoEXT{LAST_VERTEX}` in `create_graphics`; without the feature it logs that flat outputs come from the first vertex. **Fixed.** 2. **Unwritten vertex outputs.** `spirv_add_outputs` (`vk_pipeline.c`) adds outputs initialised to (0, 0, 0, 1) for - FS inputs the VS lacks. Our translator never links stages, so its `InitializeVertexOutputs` would initialise - no user output; translator patch 0006 initialises the unlinked outputs. **Fixed.** + FS inputs the VS lacks. `gfx_shader.c` reflects the full declared location span of VS output and FS input arrays, + not just their base location: an FS `vec4[16]` requires all 16 locations even when it only accesses some elements. + The same reflected mask feeds recorded-pipeline prewarm, independent stage-library unions and direct pipeline builds. + Our translator never links stages, so its `InitializeVertexOutputs` would initialise no user output; + translator patch 0006 initialises the unlinked outputs. **Implemented; array-span runtime validation pending.** 3. **Masked clears.** `vk_exec.c` skips colour clears with a partial channel mask (`W_CLEAR_MASK`); `clear_ds` skips stencil clears with a partial mask (`W_STENCIL_MASK`). NX2navi issues such clears. Check the log for `colour clear with channel mask` and `stencil clear with mask`. The hardware recipe (1.3) shows what the replay must do: colour clear = scissor 0 + channel mask, stencil clear = `value & mask` merged under the front write mask; so a draw with `colorWriteMask` / `stencilWriteMask` set to the clear mask is the exact equivalent. **Missing.** diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index a0356a92..4eda40b8 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -952,7 +952,7 @@ static void add_res(GfxShader *s, int kind, const char *o, const char *e) { // SPIR-V opcodes and enumerants used below (SPIR-V 1.6 specification, section 3). enum { OP_ENTRY_POINT = 15, OP_EXECUTION_MODE = 16, OP_TYPE_INT = 21, OP_TYPE_FLOAT = 22, OP_TYPE_VECTOR = 23, - OP_TYPE_ARRAY = 28, OP_TYPE_POINTER = 32, OP_VARIABLE = 59, OP_DECORATE = 71, + OP_TYPE_ARRAY = 28, OP_TYPE_POINTER = 32, OP_CONSTANT = 43, OP_VARIABLE = 59, OP_DECORATE = 71, DEC_BUILTIN = 11, DEC_LOCATION = 30, DEC_COMPONENT = 31, SC_INPUT = 1, SC_OUTPUT = 3, EM_LOCAL_SIZE = 17 }; // Interface Locations and the compute workgroup size from the module. Ids beyond 2^20 are ignored (translated modules stay @@ -963,11 +963,11 @@ static void reflect_spirv(GfxShader *s) { s->local[0] = s->local[1] = s->local[2] = 1; if (n < 5) return; uint32_t bound = w[3] < (1u << 20) ? w[3] : (1u << 20); - // per id: location + 1 (0 = none), component flag, scalar/vector description of types, pointee of pointers + // per id: location + 1 (0 = none), component flag, scalar/vector type and array span, pointer target, constant value int32_t *loc = calloc(bound, sizeof *loc); - uint8_t *packed = calloc(bound, 1), *ttype = calloc(bound, 1), *tcomps = calloc(bound, 1); - uint32_t *pointee = calloc(bound, sizeof *pointee); - if (!loc || !packed || !ttype || !tcomps || !pointee) goto out; + uint8_t *packed = calloc(bound, 1), *ttype = calloc(bound, 1), *tcomps = calloc(bound, 1), *tlocs = calloc(bound, 1); + uint32_t *pointee = calloc(bound, sizeof *pointee), *value = calloc(bound, sizeof *value); + if (!loc || !packed || !ttype || !tcomps || !tlocs || !pointee || !value) goto out; for (uint64_t i = 5; i < n;) { uint32_t op = w[i] & 0xffff, len = w[i] >> 16; if (!len || i + len > n) break; @@ -979,13 +979,17 @@ static void reflect_spirv(GfxShader *s) { if (a[1] == DEC_LOCATION) loc[id] = (int32_t)a[2] + 1; else if (a[1] == DEC_COMPONENT) packed[id] = 1; } else if (op == OP_TYPE_INT && len >= 4 && id < bound) { - ttype[id] = a[2] ? GFX_TYPE_SINT : GFX_TYPE_UINT, tcomps[id] = 1; + ttype[id] = a[2] ? GFX_TYPE_SINT : GFX_TYPE_UINT, tcomps[id] = 1, tlocs[id] = 1; } else if (op == OP_TYPE_FLOAT && len >= 3 && id < bound) { - ttype[id] = GFX_TYPE_FLOAT, tcomps[id] = 1; + ttype[id] = GFX_TYPE_FLOAT, tcomps[id] = 1, tlocs[id] = 1; } else if (op == OP_TYPE_VECTOR && len >= 4 && id < bound && a[1] < bound) { - ttype[id] = ttype[a[1]], tcomps[id] = (uint8_t)a[2]; - } else if (op == OP_TYPE_ARRAY && len >= 4 && id < bound && a[1] < bound) { - ttype[id] = ttype[a[1]], tcomps[id] = tcomps[a[1]]; // element type (arrayed interface blocks) + ttype[id] = ttype[a[1]], tcomps[id] = (uint8_t)a[2], tlocs[id] = 1; + } else if (op == OP_CONSTANT && len == 4 && a[1] < bound) { + value[a[1]] = a[2]; + } else if (op == OP_TYPE_ARRAY && len >= 4 && id < bound && a[1] < bound && a[2] < bound) { + ttype[id] = ttype[a[1]], tcomps[id] = tcomps[a[1]]; + uint64_t slots = (uint64_t)tlocs[a[1]] * value[a[2]]; + tlocs[id] = (uint8_t)(slots < 32 ? slots : 32); // the interface masks cover 32 locations } else if (op == OP_TYPE_POINTER && len >= 4 && id < bound) { pointee[id] = a[2]; } else if (op == OP_VARIABLE && len >= 4) { @@ -994,14 +998,20 @@ static void reflect_spirv(GfxShader *s) { int l = loc[var] - 1; uint32_t t = pointee[type] < bound ? pointee[type] : 0; GfxShaderIo io = {ttype[t], tcomps[t] ? tcomps[t] : 4, packed[var]}; - if (sc == SC_INPUT) s->in_mask |= 1u << l, s->in[l] = io; - else s->out_mask |= 1u << l, s->out[l] = io; + // VS -> FS arrays occupy every element's locations, even if only some elements are accessed. + // Other stages retain their existing reflection: mesh outputs have an outer per-vertex array. + uint32_t slots = ((s->stage == GFX_STAGE_VS && sc == SC_OUTPUT) || + (s->stage == GFX_STAGE_FS && sc == SC_INPUT)) && tlocs[t] ? tlocs[t] : 1; + for (uint32_t j = 0; j < slots && l + j < 32; j++) { + if (sc == SC_INPUT) s->in_mask |= 1u << (l + j), s->in[l + j] = io; + else s->out_mask |= 1u << (l + j), s->out[l + j] = io; + } } } i += len; } out: - free(loc), free(packed), free(ttype), free(tcomps), free(pointee); + free(loc), free(packed), free(ttype), free(tcomps), free(tlocs), free(pointee), free(value); } // Every gradient sample (OpImageSampleExplicitLod with Grad) whose two gradients are each built from two components gets From 4cb1a56e6c87cad18b498d8c258b4aebeff3b859 Mon Sep 17 00:00:00 2001 From: Michael Schienbein Date: Mon, 9 Nov 2026 15:36:43 -0600 Subject: [PATCH 07/15] Give --boot and --seconds the call sites of game 1.1.1. On 1.1.1 the SDK's nn::oe::InitializeApplet calls SetFocusHandlingMode itself, before DT_INIT, so the first call reached the boot checkpoint from sdk+0x598840 and --boot and --seconds stopped with a FATAL before the game started. Only a call from main is the checkpoint now. 1.1.1 also gets its own return addresses (nnMain's SetFocusHandlingMode at main+0x1202d44, the first nvnBootstrapLoader at main+0x346bf8c, read from x30 in the two host functions during a normal start) and its init_array counts, so a 1.1.1 run is no longer checked against the 1.2.1 values. Co-Authored-By: Claude Opus 5.5 --- runtime/boot.c | 30 ++++++++++++++++++------------ runtime/host.h | 3 ++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/runtime/boot.c b/runtime/boot.c index e3ab9415..abf95c5e 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -3,11 +3,12 @@ // The boot driver: the guest main thread runs the game's own nn::init::Start // with three host callbacks, the second of which runs DT_INIT of main, subsdk0 and sdk; Start then calls nnMain. // --boot checks two checkpoints on the way and ends at the first nvnBootstrapLoader call, at the return address of -// each in main (1.1.0, 1.2.1, 1.4.3): -// SetFocusHandlingMode main+0xeb8c14 (1.2.1: +0xe7a954, 1.4.3: +0xbdb48c) in nnMain, every module's constructor -// flag reads 2 -// nvnBootstrapLoader main+0xebcdec (1.2.1: +0xe7eb2c, 1.4.3: +0xbe0360) in the graphics bring-up; logs the -// run statistics, exits 0 +// each in main (1.1.1; 1.1.0, 1.2.1, 1.4.3 in brackets): +// SetFocusHandlingMode main+0x1202d44 (+0xeb8c14, +0xe7a954, +0xbdb48c) in nnMain, every module's constructor +// flag reads 2. 1.1.1's SDK calls it first, from nn::oe::InitializeApplet before DT_INIT; a +// call from outside main is not the checkpoint. +// nvnBootstrapLoader main+0x346bf8c (+0xebcdec, +0xe7eb2c, +0xbe0360) in the graphics bring-up; logs the run +// statistics, exits 0 // With --seconds S the run continues past that call; rt_frame_presented ends it once the world clock passed S. #include #include @@ -27,19 +28,20 @@ #include "platform/platform.h" #define SYM_INIT_START "_ZN2nn4init5StartEmmPFvvES2_S2_" -// main: return addresses of nnMain's SetFocusHandlingMode call (from the analysis of each build's main) -// and of the first nvnBootstrapLoader call, per game version. -#define CHECKPOINT_RET (RT_V143 ? 0xbdb48cull : RT_V110 ? 0xeb8c14ull : 0xe7a954ull) -#define NVN_RET (RT_V143 ? 0xbe0360ull : RT_V110 ? 0xebcdecull : 0xe7eb2cull) +// main: return addresses of nnMain's SetFocusHandlingMode call and of the first nvnBootstrapLoader call, per game +// version (from the analysis of each build's main; 1.1.1's are the x30 both host functions see in a normal start). +#define CHECKPOINT_RET (RT_V111 ? 0x1202d44ull : RT_V143 ? 0xbdb48cull : RT_V110 ? 0xeb8c14ull : 0xe7a954ull) +#define NVN_RET (RT_V111 ? 0x346bf8cull : RT_V143 ? 0xbe0360ull : RT_V110 ? 0xebcdecull : 0xe7eb2cull) #define CTOR_DONE 2 // a module's constructor flag after its DT_INIT ran the init_array -// The init_array sizes (1.1.0 and 1.2.1: counted in the modules; 1.4.3: DT_INIT_ARRAYSZ). -// The three tables carry the same rows, so cb_module_inits walks them by the size of one. +// The init_array sizes (1.1.0 and 1.2.1: counted in the modules; 1.1.1 and 1.4.3: DT_INIT_ARRAYSZ). +// The four tables carry the same rows, so cb_module_inits walks them by the size of one. typedef struct { const char *name; uint64_t entries; } InitCount; static const InitCount inits_110[] = {{"main", 10668}, {"subsdk0", 45}, {"sdk", 116}}; +static const InitCount inits_111[] = {{"main", 9783}, {"subsdk0", 0}, {"sdk", 96}}; static const InitCount inits_121[] = {{"main", 10673}, {"subsdk0", 45}, {"sdk", 116}}; static const InitCount inits_143[] = {{"main", 10717}, {"subsdk0", 45}, {"sdk", 83}}; -#define expected_inits (RT_V143 ? inits_143 : RT_V110 ? inits_110 : inits_121) +#define expected_inits (RT_V111 ? inits_111 : RT_V143 ? inits_143 : RT_V110 ? inits_110 : inits_121) static volatile int32_t inits_done, checkpoint_seen; uint64_t rt_guest_start_ns; @@ -123,6 +125,10 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) { uint64_t ret = c->x[30]; caller(c, at, sizeof at); if (!strcmp(what, "checkpoint")) { + if (ret - main_mod->base >= main_mod->size) { // 1.1.1's SDK sets the mode in nn::oe::InitializeApplet + rt_logf("boot: %s from %s, outside main: not the checkpoint\n", detail, at); + return; + } if (ret != main_mod->base + CHECKPOINT_RET) rt_fatal("boot: checkpoint %s reached from %s, expected main+%#llx", detail, at, (unsigned long long)CHECKPOINT_RET); diff --git a/runtime/host.h b/runtime/host.h index 83b23e18..033f1336 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -207,9 +207,10 @@ extern const int rt_exefs_sha256_count; extern const char rt_default_exefs[], rt_default_romfs[], rt_default_profile[]; extern const unsigned long long rt_app_pool_bytes; // the title's application memory pool (GetInfo 6) extern const int rt_nvn_api_version[2]; // NVN_DEVICE_INFO_API_{MAJOR,MINOR}_VERSION the title requires -extern const int rt_game_version; // the game build: 110 (1.1.0), 121 (1.2.1) or 143 (1.4.3) +extern const int rt_game_version; // the game build: 111 (1.1.1, make_game.py), 110 (1.1.0), 121 (1.2.1) or 143 (1.4.3) extern const char rt_port_version[]; // VERSION at the build's root: 0.1.0, 0.1.0+973c626 in a release #define RT_V110 (rt_game_version == 110) // 1.1.0: its own boot constants and data slots +#define RT_V111 (rt_game_version == 111) // 1.1.1: its own boot constants (boot.c) #define RT_V143 (rt_game_version >= 143) // ---- kernel (kernel.c) From 033bf85625d271c65dcf1bd9aac90e0d40aaf849 Mon Sep 17 00:00:00 2001 From: Michael Schienbein Date: Mon, 9 Nov 2026 14:21:16 -0600 Subject: [PATCH 08/15] Release Linux NGX before destroying the Vulkan device --- docs/building.md | 2 + runtime/vk_device.c | 1 + runtime/vk_dlaa.c | 114 ++++++++++++++++++++++++++++++++++++++++---- runtime/vk_dlaa.h | 5 ++ 4 files changed, 113 insertions(+), 9 deletions(-) diff --git a/docs/building.md b/docs/building.md index cafebb21..5a6726a6 100644 --- a/docs/building.md +++ b/docs/building.md @@ -258,6 +258,8 @@ change passes these, all run with `.venv`: A `--cpu v2` build passes the same differential tests with `--cflags=-march=x86-64-v2`; they compile `runtime/fma.c` too, so they check the software fma the v2 build links rather than the C runtime's. +On NVIDIA Linux, `NX2NAVI_DLAA=1 ./ocarina --probe-vkdevice` logs NGX initialization and shutdown when available. + Runtime probes, each a mode of `nx2navi.exe` (the list is at the top of `runtime/main.c`): `--probe-kernel`, `--probe-vkexec`, `--probe-nvn-tex` and `--probe-shader` cover most runtime changes. `--probe-shader` and the draw part of `--probe-vkexec` translate shaders of the census below: they look for `build//shaders/jobs.json` of the diff --git a/runtime/vk_device.c b/runtime/vk_device.c index e1398278..d8668c78 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -465,6 +465,7 @@ static long g_errors_total; // outlives the device so the probe can print it static void destroy_all(void) { gfx_window_release_vk(); if (g_vk.device) vkDeviceWaitIdle(g_vk.device); + if (g_vk.device) vk_dlaa_release(); // the DLSS features and NGX: the allocator and the device still live if (g_vk.device) vk_astc_release(); if (g_vk.device) vk_crumbs_release(); if (g_vk.timeline) vkDestroySemaphore(g_vk.device, g_vk.timeline, NULL); diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 31eecb48..a116a994 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -2,13 +2,16 @@ // NGX core: on Windows _nvngx.dll, found through HKLM\SOFTWARE\NVIDIA Corporation\Global\NGXCore; on Linux // libnvidia-ngx.so.1, which the NVIDIA driver installs on the library path. NGX loads the DLSS model from the folder of // the executable: nvngx_dlss.dll on Windows, libnvidia-ngx-dlss.so. on Linux. No NVIDIA SDK code or header is -// compiled in: the few NGX types used are declared here from the SDK's documented ABI. +// compiled in: the few NGX types used are declared here from the SDK ABI and the Linux core's shutdown interface. // // The core's parameter object is a C++ class whose virtual functions are called through its vtable: the Set overloads // in slots 0-7 and the Get overloads in 8-15. MSVC (the Windows core) lays out each group of overloads in reverse // declaration order, the Itanium C++ ABI (the Linux core, built with GCC) in declaration order. vtable_ok checks the // layout with a set-then-get round trip before anything relies on it. // +// NGX and DLAA's resources live as long as the device: vk_dlaa_init brings NGX up on the new device, and +// vk_dlaa_release (Linux) frees everything DLAA made and shuts NGX down before the device is destroyed. +// // Where DLAA runs in the frame, the jitter and the motion vectors: see the sections below. #ifdef _WIN32 #include @@ -60,6 +63,11 @@ typedef NgxResult (*PFN_DestroyParams)(NgxParam *); typedef NgxResult (*PFN_Create)(VkCommandBuffer, int feature, NgxParam *, NgxHandle **); typedef NgxResult (*PFN_Evaluate)(VkCommandBuffer, const NgxHandle *, const NgxParam *, void *callback); typedef NgxResult (*PFN_Release)(NgxHandle *); +#ifndef _WIN32 +// NVSDK_NGX_VULKAN_Shutdown1 as the Linux core exports it: the device and a pointer that receives the devices NGX still +// serves afterwards (vk_dlaa_release). +typedef NgxResult (*PFN_Shutdown1)(VkDevice, unsigned *remaining); +#endif static struct { void *core; // the NGX core library (an HMODULE on Windows) @@ -73,6 +81,12 @@ static struct { int tried, loaded, available; int ngx_up; // vk_dlaa_init brought NGX up: DLSS serves this run int wanted; // -1 not read yet +#ifndef _WIN32 + // Linux teardown (vk_dlaa_release) + PFN_Shutdown1 shutdown1; // required: core_load leaves DLAA off without it + VkDevice device; // what NGX was initialised with: set while vk_dlaa_release owes it a shutdown + NgxParam *caps_map; // GetCapabilityParameters' map: allocated for the caller, destroyed by vk_dlaa_release +#endif } N = {.wanted = -1}; typedef void (*SetI)(NgxParam *, const char *, int); @@ -157,6 +171,9 @@ static int core_load(void) { #define FN(f, name) N.f = plat_lib_sym(N.core, name) #endif FN(init, "NVSDK_NGX_VULKAN_Init_Ext2"); +#ifndef _WIN32 + FN(shutdown1, "NVSDK_NGX_VULKAN_Shutdown1"); // Linux: checked below +#endif FN(req_ext, "NVSDK_NGX_VULKAN_RequiredExtensions"); FN(caps, "NVSDK_NGX_VULKAN_GetCapabilityParameters"); FN(alloc_params, "NVSDK_NGX_VULKAN_AllocateParameters"); @@ -169,6 +186,16 @@ static int core_load(void) { rt_log("dlaa: the NGX core %s lacks Vulkan entry points: DLAA off", path); return 0; } +#ifndef _WIN32 + // Without Shutdown1 the device cannot be taken back from NGX (vk_dlaa_release), and the process then segfaults after + // the Vulkan teardown (NVIDIA 615.78.08): DLAA stays off rather than run with a teardown known to crash. The + // parameter maps are destroyed there too. + if (!N.shutdown1 || !N.destroy_params) { + rt_log("dlaa: the NGX core %s lacks NVSDK_NGX_VULKAN_Shutdown1 or DestroyParameters, which its teardown needs: " + "DLAA off", path); + return 0; + } +#endif N.loaded = 1; rt_log("dlaa: NGX core %s", path); return 1; @@ -273,10 +300,16 @@ void vk_dlaa_init(void) { rt_log("dlaa: NGX init failed (%08x): DLAA off", (unsigned)r); return; } +#ifndef _WIN32 + N.device = c->device; // NGX holds the device from here, also if the layout check below turns DLAA off +#endif if (!vtable_ok()) return; NgxParam *caps = NULL; int avail = 0, driver = 0; if (NGX_OK(N.caps(&caps)) && caps) { +#ifndef _WIN32 + N.caps_map = caps; // allocated for us (the SDK header: free it with DestroyParameters): vk_dlaa_release does +#endif p_geti(caps, "SuperSampling.Available", &avail); p_geti(caps, "SuperSampling.NeedsUpdatedDriver", &driver); } @@ -468,9 +501,10 @@ static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, return vkCreateImageView(c->device, &vi, NULL, view) == VK_SUCCESS; } -static void free_size(struct Size *e) { +// The images and views of a size, then its feature. The device must be idle: free_size waits for it, vk_dlaa_release +// has. Whatever was not made is skipped, so a half-made size goes the same way. +static void destroy_size(struct Size *e) { const VkCtx *c = vk_ctx(); - vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // a third scene size: the evicted images may still be in flight if (e->out_view) vkDestroyImageView(c->device, e->out_view, NULL); if (e->mv_view) vkDestroyImageView(c->device, e->mv_view, NULL); if (e->out) vmaDestroyImage(c->vma, e->out, e->out_mem); @@ -487,6 +521,11 @@ static void free_size(struct Size *e) { memset(e, 0, sizeof *e); } +static void free_size(struct Size *e) { + vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock(); // a third scene size: the evicted images may still be in flight + destroy_size(e); +} + // The entry for a scene and output size: its output (RGBA16F at the output size, written by DLSS, then blitted into // the target) and motion vectors (RG16F at the scene size, render pixels), both kept in GENERAL like every image of the // back end. NX2navi's dynamic resolution switches between a few sizes; each keeps its feature, the least recently used @@ -678,9 +717,11 @@ static int mv_init(void) { .layout = D.mv_layout}; VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &D.mv_pipe); vkDestroyShaderModule(c->device, m, NULL); + m = VK_NULL_HANDLE; if (r != VK_SUCCESS) goto fail; return D.mv_ok = 1; fail: + if (m) vkDestroyShaderModule(c->device, m, NULL); // made before a later step failed rt_log("dlaa: the motion-vector pass could not be set up: DLAA runs with zero motion"); return 0; } @@ -1023,16 +1064,18 @@ static int mask_init(void) { .layout = R.layout}; VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &R.pipe); vkDestroyShaderModule(c->device, m, NULL); + m = VK_NULL_HANDLE; if (r != VK_SUCCESS) goto fail; return R.ok = 1; fail: + if (m) vkDestroyShaderModule(c->device, m, NULL); // made before a later step failed rt_log("dlaa: the reactive mask pass could not be set up: DLSS runs without it"); return 0; } -static void free_snap(struct Snap *s) { +// A snapshot's images and views. The device must be idle: free_snap waits for it, vk_dlaa_release has. +static void destroy_snap(struct Snap *s) { const VkCtx *c = vk_ctx(); - vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // a fourth size: the evicted images may still be in flight if (s->before_view) vkDestroyImageView(c->device, s->before_view, NULL); if (s->mask_view) vkDestroyImageView(c->device, s->mask_view, NULL); if (s->before) vmaDestroyImage(c->vma, s->before, s->before_mem); @@ -1040,6 +1083,11 @@ static void free_snap(struct Snap *s) { memset(s, 0, sizeof *s); } +static void free_snap(struct Snap *s) { + vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock(); // a fourth size: the evicted images may still be in flight + destroy_snap(s); +} + static struct Snap *snap_for(VkCommandBuffer cb, uint32_t w, uint32_t h, VkFormat f) { static uint64_t tick; struct Snap *e = NULL, *lru = &R.s[0]; @@ -1115,6 +1163,55 @@ static struct Snap *mask_pass(VkCommandBuffer cb, VkImageView cv, uint32_t w, ui return s; } +// ---------------------------------------------------------------- teardown +// The HUD-less picture's images, a half-made pair too. The device must be idle, or the images not yet used. +static void hl_destroy(void) { + const VkCtx *c = vk_ctx(); + if (D.hl_view) vkDestroyImageView(c->device, D.hl_view, NULL); + if (D.hl) vmaDestroyImage(c->vma, D.hl, D.hl_mem); + if (D.hl_srgb) vmaDestroyImage(c->vma, D.hl_srgb, D.hl_srgb_mem); + D.hl_view = VK_NULL_HANDLE, D.hl = D.hl_srgb = VK_NULL_HANDLE; +} + +// The device is idle here. Release NGX features and parameters before their Vulkan resources, then shut down NGX even +// if initialization succeeded but the layout check failed. The Linux core's Shutdown1 takes an extra output pointer +// compared with the SDK's public wrapper; it reports the number of devices still using NGX. +void vk_dlaa_release(void) { +#ifndef _WIN32 + const VkCtx *c = vk_ctx(); + if (!c) return; + for (int i = 0; i < 5; i++) + if (D.sizes[i].feat) N.release(D.sizes[i].feat), D.sizes[i].feat = NULL; + if (D.params) N.destroy_params(D.params); + if (N.caps_map) N.destroy_params(N.caps_map); + for (int i = 0; i < 5; i++) destroy_size(&D.sizes[i]); + for (int i = 0; i < SNAPS; i++) destroy_snap(&R.s[i]); + hl_destroy(); + if (D.mv_pipe) vkDestroyPipeline(c->device, D.mv_pipe, NULL); + if (D.mv_layout) vkDestroyPipelineLayout(c->device, D.mv_layout, NULL); + if (D.mv_dsl) vkDestroyDescriptorSetLayout(c->device, D.mv_dsl, NULL); + if (D.mv_samp) vkDestroySampler(c->device, D.mv_samp, NULL); + if (D.camb) vmaDestroyBuffer(c->vma, D.camb, D.camb_mem); + if (D.mvm) vmaDestroyBuffer(c->vma, D.mvm, D.mvm_mem); // mapped for good: the destroy unmaps it + if (R.pipe) vkDestroyPipeline(c->device, R.pipe, NULL); + if (R.layout) vkDestroyPipelineLayout(c->device, R.layout, NULL); + if (R.dsl) vkDestroyDescriptorSetLayout(c->device, R.dsl, NULL); + if (T.pool) vkDestroyQueryPool(c->device, T.pool, NULL); + memset(&D, 0, sizeof D), D.mv_ok = -1, D.cfg_seen = ~0u; + memset(&R, 0, sizeof R), R.ok = -1, R.knob = -1; + memset(&T, 0, sizeof T), T.on = -1; + memset(&F, 0, sizeof F); + vk_dlaa_swap_img = NULL, vk_dlaa_swap_view = VK_NULL_HANDLE; + if (!N.device) return; // NGX never came up on this device: DLAA off, FSR 3.1, or its init failed + unsigned left = 0; + vk_queue_lock(); // NGX may wait for the device itself + NgxResult r = N.shutdown1(N.device, &left); + vk_queue_unlock(); + rt_log("dlaa: NGX shut down (%08x, %u devices left)", (unsigned)r, left); + N.device = NULL, N.caps_map = NULL, N.ngx_up = 0, N.available = 0; +#endif +} + // ---------------------------------------------------------------- jitter // The scene's draws (every pass drawing with the scene depth, until the DLAA point) have their viewport moved by a // sub-pixel Halton(2,3) offset, which shifts what they rasterise as a jittered projection would; full-screen passes @@ -1469,16 +1566,15 @@ static void hudless(VkCommandBuffer cb, struct Size *e, uint32_t w, uint32_t h) VkFormat fmt = hdr ? VK_FORMAT_R16G16B16A16_SFLOAT : VK_FORMAT_R8G8B8A8_UNORM; if (D.hl && (D.hl_w != w || D.hl_h != h || D.hl_fmt != fmt)) { vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); - vkDestroyImageView(c->device, D.hl_view, NULL); - vmaDestroyImage(c->vma, D.hl, D.hl_mem); - if (D.hl_srgb) vmaDestroyImage(c->vma, D.hl_srgb, D.hl_srgb_mem); - D.hl = D.hl_srgb = VK_NULL_HANDLE; + hl_destroy(); } if (!D.hl) { VkImageView v2 = VK_NULL_HANDLE; VkImageUsageFlags u = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; if ((!hdr && !image2d(w, h, VK_FORMAT_R8G8B8A8_SRGB, u, &D.hl_srgb, &D.hl_srgb_mem, &v2)) || !image2d(w, h, fmt, u | VK_IMAGE_USAGE_STORAGE_BIT, &D.hl, &D.hl_mem, &D.hl_view)) { + if (v2) vkDestroyImageView(c->device, v2, NULL); // the first image's view, when the second image failed + hl_destroy(); // and what of the two images was made rt_log("dlaa: the HUD-less picture's images could not be made: frame generation sees the HUD"); vk_dlaa_hudless_wanted = 0; return; diff --git a/runtime/vk_dlaa.h b/runtime/vk_dlaa.h index b225190b..771e1a65 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -33,6 +33,11 @@ int vk_dlaa_fg_camera(VkDlaaCamera *c); // ... and its temporal upscaler is void vk_dlaa_instance_exts(const char **ext, uint32_t *n, uint32_t max); void vk_dlaa_device_exts(const VkExtensionProperties *have, uint32_t nhave, const char **ext, uint32_t *n, uint32_t max); void vk_dlaa_init(void); +// vk_device.c destroy_all, once the device is idle and before the allocator and the device go (Linux): releases the NGX +// features and parameters, frees every image, buffer, pipeline and query pool DLAA made, resets its state for a later +// device and shuts NGX down for this one. Safe when NGX never came up, when nothing was made, and more than once. +// Windows: does nothing. +void vk_dlaa_release(void); // 1 when dlaa is on and NGX came up with DLSS (after vk_dlaa_init): a DLSS mode's smaller scene is scaled up by DLSS. int vk_dlaa_available(void); // vk_exec.c vk_exec_pass_begin, once the pass's targets are resolved and before its barrier: arms the frame on the From d8ea169fcb871872129f36b66a6c91a1127a4b4a Mon Sep 17 00:00:00 2001 From: Michael Schienbein Date: Mon, 9 Nov 2026 14:09:57 -0600 Subject: [PATCH 09/15] Fix CPU-backed texture fixtures in the Vulkan memory probe --- docs/building.md | 1 + runtime/tex_probe.c | 50 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/docs/building.md b/docs/building.md index 5a6726a6..e5e48ce5 100644 --- a/docs/building.md +++ b/docs/building.md @@ -264,6 +264,7 @@ Runtime probes, each a mode of `nx2navi.exe` (the list is at the top of `runtime `--probe-vkexec`, `--probe-nvn-tex` and `--probe-shader` cover most runtime changes. `--probe-shader` and the draw part of `--probe-vkexec` translate shaders of the census below: they look for `build//shaders/jobs.json` of the exe's own version first, then of the other one. +`--probe-vkmem` also checks CPU-backed texture uploads and rewrites, GPU-written texture aliases, and memory write watches. Shaders: `.venv-py314\Scripts\python tools\shader_census.py --version --translate` extracts every Maxwell shader to `build\\shaders\` and runs it through the translator and `spirv-val`; failure groups go to `failures.txt`. diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index 1788860f..8fb56bdb 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -463,7 +463,8 @@ static tex_t make_tex(pool_t *pool, uint64_t off, int format, uint32_t w, uint32 t.magic = MAGIC('T', 'E', 'X', 0); t.d.target = 1, t.d.format = format, t.d.width = w, t.d.height = h, t.d.depth = 1, t.d.levels = levels; nvn_tex_layout(&t.d, &t.L); - t.pool = pool, t.off = (int64_t)off, t.mem = pool->mem + off, t.serial = serial; + // pool_flags as nvnTextureInitialize records them: tex_mirrored (vk_texture.c) reads them, not the pool object + t.pool = pool, t.pool_flags = (uint16_t)pool->flags, t.off = (int64_t)off, t.mem = pool->mem + off, t.serial = serial; return t; } @@ -498,6 +499,35 @@ static int read_back(const tex_t *t, unsigned lv, uint8_t *const *lin, int gpu, return 0; } +// Level 0 of image (w x h RGBA8) read through a staging buffer after everything recorded so far has run: whether every +// texel is `want`, the way a draw sampling the image would see it. +static int image_is_color(VkImage image, uint32_t w, uint32_t h, const uint8_t want[4]) { + VkCtx *c = vk_ctx(); + VkBuffer rb; + VmaAllocation ra; + size_t n = (size_t)w * h * 4; + uint8_t *r = vk_staging(n, &rb, &ra); + VkCommandBuffer cb = vk_upload_begin(); + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(cb, &di); + VkBufferImageCopy rg = {.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .imageExtent = {w, h, 1}}; + vkCmdCopyImageToBuffer(cb, image, VK_IMAGE_LAYOUT_GENERAL, rb, 1, &rg); + mb = (VkMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_HOST_BIT, + .dstAccessMask = VK_ACCESS_2_HOST_READ_BIT}; + vkCmdPipelineBarrier2(cb, &di); + vk_upload_end(); + vk_upload_flush(1); + vmaInvalidateAllocation(c->vma, ra, 0, VK_WHOLE_SIZE); + int same = 1; + for (size_t i = 0; i < n; i += 4) same &= !memcmp(r + i, want, 4); + vmaDestroyBuffer(c->vma, rb, ra); + return same; +} + static int test_textures(pool_t *tpool, pool_t *phys) { uint8_t *lin[4] = {0}; // block-linear RGBA8 with 3 levels in a CPU-visible pool: uploaded through the GPU, read back from the image @@ -536,14 +566,28 @@ static int test_textures(pool_t *tpool, pool_t *phys) { vk_write_texels(&g, NULL, &all, gl[0], 256, 256 * 64); if (read_back(&g, 0, gl, 0, "physical-pool RGBA8 WriteTexels")) return 1; - // a slot of a finalized texture follows the live texture at the same storage + // a slot of a finalized texture follows the live texture at the same storage. The slot's own storage is CPU-written + // (mirrored), so it follows only a texture holding GPU content its bytes lack (vk_texture.c slot_resolve). The live + // texture is cleared on the GPU and noted as rendered into, as a draw into it would be; the slot's image must then + // show the clear, which neither the storage bytes nor a recreated image of the slot's own descriptor hold. vk_texture_register(5, &b, NULL); vk_texture_release(&b); tex_t b2 = make_tex(tpool, 0x40000, 0x25, 16, 12, 1, 4); const VkTexImage *live = vk_texture_get(&b2); + GCHECK(live && live->image && live->width == 16 && live->height == 12, "RGBA8 image of the texture at the slot's storage"); + vk_texture_gpu_written(&b2); // the clear below is its content, not the storage + const float clear[4] = {0.0f, 1.0f, 1.0f, 1.0f}; + const uint8_t cleared[4] = {0x00, 0xFF, 0xFF, 0xFF}; + VkClearColorValue cv; + memcpy(cv.float32, clear, sizeof cv.float32); + VkImageSubresourceRange rng = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; + VkCommandBuffer ccb = vk_upload_begin(); + vkCmdClearColorImage(ccb, live->image, VK_IMAGE_LAYOUT_GENERAL, &cv, 1, &rng); + vk_upload_end(); 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(image_is_color(slot->image, 16, 12, cleared), "the image of slot 5 does not hold the clear written into the live texture"); 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; @@ -551,7 +595,7 @@ 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 += 7; + g_gpu_checks += 9; vk_texture_release(&t), vk_texture_release(&b2), vk_texture_release(&g); for (int i = 0; i < 4; i++) free(lin[i]), free(bl[i]); free(gl[0]); From 6dd69eadbff38f788b4e40932dfba20a85adc515 Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Mon, 9 Nov 2026 19:48:29 -0500 Subject: [PATCH 10/15] Build the runtime library and refresh installed tools correctly on Linux. Use the native toolchain and archiver for runtime libraries, accept VMA_INCLUDE, and report missing Linux tools before compiling. Read the host CPU through compiler wrappers without changing the environment used by real compiles. Compare copied tools by content rather than normalized timestamps, then replace them atomically so read-only POSIX installs can update without changing executable modes. Cover equal-metadata copies and translator refresh during shader preparation, and correct the patch-application comment. --- tests/launcher_test.py | 24 +++++++++++++++++ tools/build_xlate.py | 5 ++-- tools/make_game.py | 58 +++++++++++++++++++++++++++++------------- 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 700cd10b..7aec751b 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1338,6 +1338,11 @@ class MakeGameTest(Tmp): moving, and passes its other lines on to the log.""" kit = self.tmp / 'kit' (kit / 'tools').mkdir(parents=True) + (kit / 'bin').mkdir() + self.out.mkdir() + for name in (self.mg.XLATE_NAME, 'nx2navi_xlate.id'): + (kit / 'bin' / name).write_bytes(b'current translator') + (self.out / name).write_bytes(b'old translator') (kit / 'tools' / 'prepare_shaders.py').write_text( 'import time\n' 'print("reading the inventory", flush=True)\n' @@ -1354,6 +1359,8 @@ class MakeGameTest(Tmp): buf = io.StringIO() with contextlib.redirect_stdout(buf): self.mg.step_shaders(self.out) + for name in (self.mg.XLATE_NAME, 'nx2navi_xlate.id'): + self.assertEqual((self.out / name).read_bytes(), b'current translator') lines = buf.getvalue().splitlines() events = [json.loads(ln[2:]) for ln in lines if ln.startswith('@@')] self.assertIn('reading the inventory', lines) @@ -1578,6 +1585,23 @@ class MakeGameTest(Tmp): text = 'C:/o/x.o: C:/src/x.c C:/inc/a\\ b.h \\\n C:/inc/c.h\n' self.assertEqual(self.mg.parse_deps(text), ['C:/src/x.c', 'C:/inc/a b.h', 'C:/inc/c.h']) + def test_copy_if_changed_equal_metadata(self): + # Windows has no POSIX executable mode and cannot replace read-only destinations. + modes = (0o666,) if os.name == 'nt' else (0o444, 0o555) + for mode in modes: + with self.subTest(mode=oct(mode)): + src, dst = self.tmp / f'source-{mode}.id', self.tmp / f'installed-{mode}.id' + for path, data in ((src, b'7' * 64 + b'\n'), (dst, b'1' * 64 + b'\n')): + path.write_bytes(data) + os.utime(path, (1, 1)) # Build artifacts can share a normalized modification time. + path.chmod(mode) + self.assertEqual(src.stat().st_size, dst.stat().st_size) + self.assertEqual(src.stat().st_mtime_ns, dst.stat().st_mtime_ns) + self.assertTrue(self.mg.copy_if_changed(src, dst)) + self.assertEqual(dst.read_bytes(), src.read_bytes()) + self.assertEqual(dst.stat().st_mode & 0o777, src.stat().st_mode & 0o777) + self.assertFalse(self.mg.copy_if_changed(src, dst)) + @unittest.skipUnless(shutil.which('cc'), 'needs a C compiler') def test_object_cache(self): src = self.tmp / 'src' diff --git a/tools/build_xlate.py b/tools/build_xlate.py index 87e84d91..35b28361 100644 --- a/tools/build_xlate.py +++ b/tools/build_xlate.py @@ -103,9 +103,8 @@ def prepare_sources() -> Path: sys.exit(f"expected 37 patches, found {len(patches)}") # build/xlate is inside this checkout, and git inside a repository reads a "diff --git" patch's paths from the # repository's top: those hunks fall outside the directory handed to --directory, git skips every file of the patch - # and still exits 0. 0021-prmt-byte-permute.patch is the only one written in that form, so it used to be skipped in - # silence and every byte permute stayed a no-op. GIT_CEILING_DIRECTORIES stops git from walking up to the checkout, - # so it reads the patch as it would outside a repository, and a "Skipped patch" line fails the build. + # and still exits 0. GIT_CEILING_DIRECTORIES stops git from walking up to the checkout, so it reads each patch as it + # would outside a repository, and a "Skipped patch" line fails the build (including 0021's PRMT implementation). env = dict(os.environ, GIT_CEILING_DIRECTORIES=str(BUILD.parent), LC_ALL="C") for patch in patches: for extra in (["--check"], []): diff --git a/tools/make_game.py b/tools/make_game.py index 2ba4c8bd..36873f7b 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -1,4 +1,4 @@ -"""Build Ocarina of Time (Switch 2, game 1.1.1) for Windows on the player's PC, from the player's own game files. +"""Build Ocarina of Time (Switch 2, game 1.1.1) natively for Windows or Linux from the player's own game files. python -I tools/make_game.py --nup "C:\\path\\game.nup" --out "C:\\Games\\Ocarina" python -I tools/make_game.py --exefs "C:\\path\\exefs" --romfs "C:\\path\\romfs" --out "C:\\Games\\Ocarina" @@ -47,10 +47,11 @@ version (fast_clang: the same objects, about a fifth faster). On Linux the same steps make /ocarina with the clang, clang++ and lld on PATH and the system's libraries (Vulkan, SDL3, Opus, libvpx, dav1d, zstd; libstdc++ static, rpath $ORIGIN), as tools/build_game.py's Linux build does: the runtime's *_sdl.* and platform/linux.c take the place of its *_win.*, platform/win32.c and -nx2navi.rc, and --msys is not used. README-BuildKit.md, "Linux", lists the packages. +nx2navi.rc, and --msys is not used. VMA_INCLUDE can name the directory holding vk_mem_alloc.h; otherwise the +system header or the pinned download is used. README-BuildKit.md, "Linux", lists the packages. -Nothing is written outside --out (but for --make-runtime-lib DIR, which writes DIR). The game folder then holds -only what this PC built from the player's files: never +Game content is written only to --out; a downloaded VMA header is cached in toolchain/vma, and --make-runtime-lib +writes DIR. The game folder holds only what this PC built from the player's files: never share it. Needs Python 3.10+ with numpy, lz4, zstandard and zbic (pip), with fonttools and pillow too when the kit has button art, and MSYS2 UCRT64's clang and libraries (README-BuildKit.md lists the commands). Delete /build-cache afterwards to free about 5 GB; the next build then compiles everything again. @@ -70,6 +71,7 @@ import shutil import struct import subprocess import sys +import tempfile import threading import time from pathlib import Path @@ -514,19 +516,23 @@ def pe_imports(path): def copy_if_changed(src, dst): - """Copy src over dst unless dst already has its size and modification time. A file the running game holds open - cannot be replaced on Windows: that stops the build with a message instead of a traceback.""" + """Atomically replace dst unless its contents match, even when source modification times are normalized. + A same-directory temporary copy preserves modes, replaces read-only files on POSIX and leaves dst intact on copy failure.""" + dst = Path(dst) s = os.stat(src) try: d = os.stat(dst) - if d.st_size == s.st_size and int(d.st_mtime) == int(s.st_mtime): + if d.st_size == s.st_size and sha256_file(src) == sha256_file(dst): return False except FileNotFoundError: pass try: - shutil.copy2(src, dst) + with tempfile.TemporaryDirectory(dir=dst.parent, prefix='.nx2navi-copy-') as tmp: + replacement = Path(tmp) / dst.name + shutil.copy2(src, replacement) + os.replace(replacement, dst) except PermissionError: - fail(f'{dst} is in use: close the game and try again') + fail(f'cannot replace {dst}: close the game and check output-directory permissions') return True @@ -894,8 +900,14 @@ def cpu_level(clang, env): tell. The game runs on the PC that builds it, so it takes this CPU's level, as tools/build_game.py --cpu does by hand. Without one the lifted code would be generic x86-64: every fused multiply-add of the game a call to runtime/fma.c's software fma, and a level the differential tests do not cover.""" + # Compiler wrappers can discard -march=native, even for this read-only host probe. Only the probe opts out: + # the actual compiles keep their environment and use the explicit, supported x86-64 level selected below. + probe_env = dict(env, NIX_ENFORCE_NO_NATIVE='0') + for name in env: + if name.startswith('NIX_ENFORCE_NO_NATIVE_'): + probe_env[name] = '0' p = subprocess.run([str(clang), '-march=native', '-E', '-dM', '-x', 'c', os.devnull], capture_output=True, - text=True, env=env, errors='replace') + text=True, env=probe_env, errors='replace') have = set(re.findall(r'^#define (__\w+) ', p.stdout, re.M)) if p.returncode == 0 else set() return 3 if CPU_V3 <= have else 2 if CPU_V2 <= have else 0 @@ -950,12 +962,11 @@ def make_runtime_lib(dest, msys, jobs, obj=None): dest.mkdir(parents=True, exist_ok=True) obj = Path(obj).resolve() if obj else dest / 'obj' obj.mkdir(parents=True, exist_ok=True) - clang, clangxx = msys / 'bin' / 'clang.exe', msys / 'bin' / 'clang++.exe' - env = dict(os.environ, PATH=str(msys / 'bin') + os.pathsep + os.environ.get('PATH', '')) + clang, clangxx, env, vma = toolchain(msys) rt = KIT / 'runtime' tainted = per_install_names() srcs = [(p, obj / name) for p, name in runtime_sources() if not per_install(p, tainted)] - inc = ['-I', str(rt), '-I', str(rt / 'third_party' / 'imgui'), '-I', str(msys / 'include' / 'vma')] + inc = ['-I', str(rt), '-I', str(rt / 'third_party' / 'imgui'), '-I', str(vma)] cache = ObjectCache(obj / 'objects.json', {'': KIT}) t0 = time.time() compiled, reused, bad = compile_all(srcs, runtime_cmd(clang, clangxx, inc), cache, jobs, env, @@ -969,7 +980,9 @@ def make_runtime_lib(dest, msys, jobs, obj=None): lib.unlink(missing_ok=True) rsp = dest / 'ar.rsp' rsp.write_text('\n'.join('"%s"' % o.as_posix() for _, o in srcs), encoding='utf-8') - ar = os.environ.get('NX2NAVI_AR') or str(msys / 'bin' / 'ar.exe') + ar = os.environ.get('NX2NAVI_AR') or (str(msys / 'bin' / 'ar.exe') if WIN else shutil.which('ar')) + if not ar: + fail('ar is not on PATH: install binutils or set NX2NAVI_AR') if subprocess.run([ar, 'rcs', str(lib), '@' + str(rsp)], env=env).returncode: fail('the runtime library could not be archived') meta = {'runtime': runtime_digest(), 'compiler': clang_version(clang, env), @@ -1088,13 +1101,22 @@ def compile_resources(msys, rt, obj, env, lld=False): def toolchain(msys): """(clang, clang++, environment, the folder holding vk_mem_alloc.h): MSYS2 UCRT64's on Windows, the ones on PATH - elsewhere, with tools/build_game.py's VMA lookup (the system's header, else the pinned one fetched into - toolchain/vma).""" + elsewhere. VMA_INCLUDE overrides tools/build_game.py's VMA lookup (the system's header, else the pinned one + fetched into toolchain/vma).""" if WIN: return (msys / 'bin' / 'clang.exe', msys / 'bin' / 'clang++.exe', dict(os.environ, PATH=str(msys / 'bin') + os.pathsep + os.environ.get('PATH', '')), msys / 'include' / 'vma') - from build_game import vma_include - return Path(shutil.which('clang')), Path(shutil.which('clang++')), dict(os.environ), vma_include() + for tool in ('clang', 'clang++', 'ld.lld'): + if not shutil.which(tool): + fail(f'{tool} is not on PATH: install the packages README-BuildKit.md lists under "Linux"') + if os.environ.get('VMA_INCLUDE'): + vma = Path(os.environ['VMA_INCLUDE']) + if not (vma / 'vk_mem_alloc.h').is_file(): + fail(f'{vma / "vk_mem_alloc.h"} is missing: correct VMA_INCLUDE') + else: + from build_game import vma_include + vma = vma_include() + return Path(shutil.which('clang')), Path(shutil.which('clang++')), dict(os.environ), vma def xlate_source(): From 4d41188d8b0c269c1f503f6059757371ba806574 Mon Sep 17 00:00:00 2001 From: Michael Schienbein Date: Mon, 9 Nov 2026 12:27:06 -0600 Subject: [PATCH 11/15] Size the lift's workers by free memory, hold off sleep and key the compile by the CPU on Linux. On Linux the three host probes of the build did nothing. available_ram returned None, so the lift started one worker per CPU whatever the memory and could run the machine out of it; keep_awake did nothing, so the machine could sleep in the middle of a build of 10 to 25 minutes; cpu_tag returned 'any', so the compile step's key ignored the processor and a game folder moved to a CPU without AVX2 kept its x86-64-v3 objects. available_ram now reads MemAvailable from /proc/meminfo and, when the process runs under a cgroup v2 memory limit (its own or a parent's), caps it by memory.max less memory.current. keep_awake starts systemd-inhibit with a lock on sleep (not idle: the screen may still blank and lock, as with ES_SYSTEM_REQUIRED on Windows) around a cat that reads a pipe this process holds open, so the lock ends with the process even when it is killed; without systemd-inhibit it does nothing. cpu_tag reads the flags of /proc/cpuinfo and returns the digits the Windows branch returns for SSE4.2, AVX2 and AVX-512F, counting a feature only when every logical processor has it. The compile key now holds the real tag instead of 'any', so an existing Linux build compiles once more. The compile --jobs defaults are unchanged. tests/host_probes_test.py covers the three probes over fake /proc and cgroup files and a fake systemd-inhibit. Co-Authored-By: Claude Sonnet 5.5 --- tests/host_probes_test.py | 303 ++++++++++++++++++++++++++++++++++++++ tools/hostmem.py | 52 ++++++- tools/make_game.py | 42 +++++- 3 files changed, 389 insertions(+), 8 deletions(-) create mode 100644 tests/host_probes_test.py diff --git a/tests/host_probes_test.py b/tests/host_probes_test.py new file mode 100644 index 00000000..b659c59f --- /dev/null +++ b/tests/host_probes_test.py @@ -0,0 +1,303 @@ +"""The build's host probes on Linux, over fake /proc and cgroup files and a fake systemd-inhibit: tools/hostmem.py's +available_ram (MemAvailable, capped by the cgroup v2 memory limits), the lift's worker count that follows it +(recomp/gen.py pick_jobs), tools/make_game.py's cpu_tag (the compile key's processor part) and keep_awake (the sleep +lock). No game files; stdlib only (the worker count test also needs numpy, as the lifter does). + +Run: python tests/host_probes_test.py +""" +import os +import signal +import stat +import subprocess +import sys +import tempfile +import time +import unittest +from pathlib import Path +from unittest import mock + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT / 'tools')) +sys.path.insert(0, str(ROOT / 'recomp')) + +import hostmem # noqa: E402 +import make_game # noqa: E402 + +try: + import gen +except ImportError: + gen = None + +LINUX = unittest.skipUnless(sys.platform.startswith('linux'), 'the Linux probes') +GIB = 1 << 30 + + +class Tmp(unittest.TestCase): + def setUp(self): + td = tempfile.TemporaryDirectory() + self.addCleanup(td.cleanup) + self.tmp = Path(td.name) + + +@LINUX +class AvailableRamTest(Tmp): + """available_ram on a fake /proc and a fake cgroup v2 mount.""" + + def setUp(self): + super().setUp() + self.proc, self.cgroup = self.tmp / 'proc', self.tmp / 'cgroup' + (self.proc / 'self').mkdir(parents=True) + self.cgroup.mkdir() + for name, path in (('PROC', self.proc), ('CGROUP', self.cgroup)): + patcher = mock.patch.object(hostmem, name, path) + patcher.start() + self.addCleanup(patcher.stop) + + def meminfo(self, avail_kib): + (self.proc / 'meminfo').write_text( + f'MemTotal: 65536000 kB\nMemFree: 1048576 kB\nMemAvailable: {avail_kib} kB\n' + 'Buffers: 102400 kB\nCached: 20971520 kB\n') + + def member_of(self, path): + (self.proc / 'self' / 'cgroup').write_text(f'0::{path}\n') + + def limit(self, path, maximum, current): + """memory.max and memory.current of the cgroup at path (relative to the mount).""" + folder = self.cgroup / path + folder.mkdir(parents=True, exist_ok=True) + (folder / 'memory.max').write_text(f'{maximum}\n') + (folder / 'memory.current').write_text(f'{current}\n') + + def test_without_a_limit_it_is_mem_available(self): + self.meminfo(10 * GIB // 1024) + self.member_of('/user.slice/app.scope') + self.limit('', 'max', 0) # unlimited cgroups are not a cap + self.limit('user.slice', 'max', 3 * GIB) + self.limit('user.slice/app.scope', 'max', GIB) + self.assertEqual(hostmem.available_ram(), 10 * GIB) + + def test_a_cgroup_without_memory_files_is_no_cap(self): + self.meminfo(10 * GIB // 1024) + self.member_of('/user.slice/app.scope') # the root cgroup has no memory.max, and here nothing else has + self.assertEqual(hostmem.available_ram(), 10 * GIB) + + def test_the_cgroup_limit_less_its_use_caps_it(self): + self.meminfo(40 * GIB // 1024) + self.member_of('/user.slice/app.scope') + self.limit('user.slice/app.scope', 12 * GIB, 5 * GIB) + self.assertEqual(hostmem.available_ram(), 7 * GIB) + + def test_a_limit_above_mem_available_changes_nothing(self): + self.meminfo(6 * GIB // 1024) + self.member_of('/app.scope') + self.limit('app.scope', 64 * GIB, GIB) + self.assertEqual(hostmem.available_ram(), 6 * GIB) + + def test_the_tightest_limit_up_the_tree_counts(self): + self.meminfo(40 * GIB // 1024) + self.member_of('/user.slice/user-1000.slice/app.scope') + self.limit('user.slice/user-1000.slice/app.scope', 'max', GIB) + self.limit('user.slice/user-1000.slice', 9 * GIB, 6 * GIB) # 3 GiB left + self.limit('user.slice', 30 * GIB, 2 * GIB) # 28 GiB left + self.assertEqual(hostmem.available_ram(), 3 * GIB) + + def test_a_leaf_limit_tighter_than_its_parents(self): + self.meminfo(40 * GIB // 1024) + self.member_of('/a/b') + self.limit('a/b', 4 * GIB, 3 * GIB) # 1 GiB left + self.limit('a', 20 * GIB, GIB) + self.assertEqual(hostmem.available_ram(), GIB) + + def test_a_group_over_its_limit_has_nothing_left(self): + self.meminfo(40 * GIB // 1024) + self.member_of('/app.scope') + self.limit('app.scope', 4 * GIB, 5 * GIB) + self.assertEqual(hostmem.available_ram(), 0) + + def test_a_container_sees_its_own_limit_at_the_mount(self): + # In a cgroup namespace the process is at "/" and the mount is its own cgroup. + self.meminfo(40 * GIB // 1024) + self.member_of('/') + self.limit('', 8 * GIB, 2 * GIB) + self.assertEqual(hostmem.available_ram(), 6 * GIB) + + def test_a_v1_system_has_only_mem_available(self): + self.meminfo(10 * GIB // 1024) + (self.proc / 'self' / 'cgroup').write_text('12:memory:/user.slice\n1:name=systemd:/user.slice\n') + self.limit('', 2 * GIB, 0) + self.assertEqual(hostmem.available_ram(), 10 * GIB) + + def test_unreadable_meminfo_is_none(self): + self.member_of('/app.scope') + self.limit('app.scope', 2 * GIB, 0) + self.assertIsNone(hostmem.available_ram()) + (self.proc / 'meminfo').write_text('MemTotal: 1 kB\n') # no MemAvailable (a kernel before 3.14) + self.assertIsNone(hostmem.available_ram()) + + @unittest.skipIf(gen is None, 'recomp/gen.py needs numpy') + def test_the_lift_sizes_its_workers_by_the_limit(self): + """A 32-processor machine under an 8 GiB cgroup gets 4 workers (8 GiB x 0.8 / 1.5 GiB), not 32.""" + self.meminfo(80 * GIB // 1024) + self.member_of('/app.scope') + self.limit('app.scope', 8 * GIB, 0) + with mock.patch.dict(os.environ), mock.patch.object(gen.os, 'cpu_count', return_value=32): + os.environ.pop('NX2NAVI_LIFT_JOBS', None) + jobs, why = gen.pick_jobs(None) + self.assertEqual(jobs, 4, why) + + +def cpuinfo(*flag_lists): + """A /proc/cpuinfo with one processor entry per flags list.""" + return ''.join(f'processor\t: {i}\nvendor_id\t: GenuineIntel\nmodel name\t: Fake CPU\n' + f'flags\t\t: {flags}\nbogomips\t: 5990.40\n\n' for i, flags in enumerate(flag_lists)) + + +V2 = 'fpu sse sse2 ssse3 sse4_1 sse4_2 popcnt' +V3 = V2 + ' avx avx2 fma bmi1 bmi2' +V4 = V3 + ' avx512f avx512bw avx512vl' + + +@LINUX +class CpuTagTest(Tmp): + """cpu_tag reads the same three features as its Windows branch: SSE4.2, AVX2 and AVX-512F.""" + + def tag(self, text): + path = self.tmp / 'cpuinfo' + path.write_text(text) + with mock.patch.object(make_game, 'CPUINFO', path): + return make_game.cpu_tag() + + def test_levels(self): + self.assertEqual(self.tag(cpuinfo('fpu sse sse2')), '000') + self.assertEqual(self.tag(cpuinfo(V2)), '100') + self.assertEqual(self.tag(cpuinfo(V3)), '110') + self.assertEqual(self.tag(cpuinfo(V4)), '111') + + def test_every_logical_processor_has_a_flags_line(self): + self.assertEqual(self.tag(cpuinfo(*[V3] * 8)), '110') + + def test_a_feature_only_some_cores_have_is_not_counted(self): + self.assertEqual(self.tag(cpuinfo(V4, V4, V3, V3)), '110') + self.assertEqual(self.tag(cpuinfo(V3, V2)), '100') + + def test_a_longer_flag_name_is_not_a_match(self): + self.assertEqual(self.tag(cpuinfo(V2 + ' avx2_vnni avx512f_extra')), '100') + + def test_unreadable_is_any(self): + with mock.patch.object(make_game, 'CPUINFO', self.tmp / 'missing'): + self.assertEqual(make_game.cpu_tag(), 'any') + self.assertEqual(self.tag(''), 'any') + self.assertEqual(self.tag('processor\t: 0\nFeatures\t: fp asimd evtstrm\n\n'), 'any') # not x86 + + def test_the_compile_key_follows_the_processor(self): + """The same lift, runtime and kit compile again on a processor without AVX2.""" + def key(flags): + path = self.tmp / 'cpuinfo' + path.write_text(cpuinfo(flags)) + with mock.patch.object(make_game, 'CPUINFO', path): + return make_game.step_key('compile', 'id', self.tmp) + + digests = {name: mock.Mock(return_value='x') for name in + ('exefs_digest', 'lift_digest', 'tree_digest', 'sha256_file', 'kit_version')} + with mock.patch.multiple(make_game, **digests): + self.assertEqual(key(V3), key(V3)) + self.assertEqual(len({key(V2), key(V3), key(V4)}), 3) + + +FAKE_INHIBIT = '''#!/bin/sh +# What systemd-inhibit does: take the lock (here: write the state), run the command, release the lock. +printf '%s\\n' "$@" > "{log}/args" +while [ "${{1#--}}" != "$1" ]; do shift; done +echo held > "{log}/state" +"$@" +echo released > "{log}/state" +''' + + +def wait_for(path, text, seconds=10): + """True once the file's content is text.""" + end = time.monotonic() + seconds + while time.monotonic() < end: + try: + if path.read_text().strip() == text: + return True + except OSError: + pass + time.sleep(0.02) + return False + + +@LINUX +class KeepAwakeTest(Tmp): + """keep_awake with a systemd-inhibit stand-in first on PATH.""" + + def setUp(self): + super().setUp() + self.bin, self.log = self.tmp / 'bin', self.tmp / 'log' + self.bin.mkdir() + self.log.mkdir() + self.state = self.log / 'state' + env = mock.patch.dict(os.environ, {'PATH': f'{self.bin}{os.pathsep}{os.environ["PATH"]}'}) + env.start() + self.addCleanup(env.stop) + awake = mock.patch.object(make_game, 'AWAKE', None) + awake.start() + self.addCleanup(awake.stop) + self.addCleanup(self.release) + + def release(self): + if make_game.AWAKE is not None: + make_game.AWAKE.stdin.close() + make_game.AWAKE.wait(timeout=10) + + def install(self, script=FAKE_INHIBIT): + path = self.bin / 'systemd-inhibit' + path.write_text(script.format(log=self.log)) + path.chmod(path.stat().st_mode | stat.S_IXUSR) + + def test_it_holds_sleep_until_released(self): + self.install() + make_game.keep_awake() + self.assertTrue(wait_for(self.state, 'held')) + args = (self.log / 'args').read_text().split() + what = next(a for a in args if a.startswith('--what=')) + self.assertEqual(what, '--what=sleep') # not idle: the screen may still blank and lock, as on Windows + self.assertIn('--mode=block', args) + time.sleep(0.3) + self.assertIsNone(make_game.AWAKE.poll()) # still held: it does not end by itself + self.assertEqual(self.state.read_text().strip(), 'held') + first = make_game.AWAKE + make_game.keep_awake() # a second call takes no second lock + self.assertIs(make_game.AWAKE, first) + first.stdin.close() # what the end of this process does + first.wait(timeout=10) + self.assertEqual(self.state.read_text().strip(), 'released') + + def test_a_killed_process_releases_it(self): + self.install() + code = ('import os, signal, sys, time\n' + 'from pathlib import Path\n' + 'sys.path.insert(0, sys.argv[1])\n' + 'import make_game\n' + 'make_game.keep_awake()\n' + 'state = Path(sys.argv[2])\n' + 'while not (state.is_file() and state.read_text().strip() == "held"):\n' + ' time.sleep(0.02)\n' + 'os.kill(os.getpid(), signal.SIGKILL)\n') + p = subprocess.run([sys.executable, '-c', code, str(ROOT / 'tools'), str(self.state)], timeout=30) + self.assertEqual(p.returncode, -signal.SIGKILL) + self.assertTrue(wait_for(self.state, 'released')) + + def test_without_systemd_inhibit_it_does_nothing(self): + with mock.patch.dict(os.environ, {'PATH': str(self.bin)}): + make_game.keep_awake() + self.assertIsNone(make_game.AWAKE) + + def test_a_refused_lock_is_not_an_error(self): + self.install('#!/bin/sh\necho "Failed to inhibit: no login manager" >&2\nexit 1\n') + make_game.keep_awake() + self.assertEqual(make_game.AWAKE.wait(timeout=10), 1) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/hostmem.py b/tools/hostmem.py index 7916d937..7c495592 100644 --- a/tools/hostmem.py +++ b/tools/hostmem.py @@ -1,7 +1,11 @@ -"""Memory the machine can still give to new processes, for sizing worker pools (stdlib only, Windows).""" +"""Memory the machine can still give to new processes, for sizing worker pools (stdlib only).""" import ctypes import ctypes.wintypes as wt import sys +from pathlib import Path + +PROC = Path('/proc') +CGROUP = Path('/sys/fs/cgroup') # where the unified (v2) cgroup hierarchy is mounted JOB_OBJECT_EXTENDED_LIMIT_INFORMATION = 9 LIMIT_PROCESS_MEMORY = 0x100 @@ -40,10 +44,50 @@ def _job_headroom(): return None -def available_ram(): - """Physical memory not in use right now, capped by the job memory limit if there is one. None off Windows.""" - if sys.platform != 'win32': +def _meminfo_available(): + """MemAvailable of /proc/meminfo in bytes: what new processes can use without swapping (free memory plus the page + cache and slab the kernel gives back). None when it cannot be read.""" + try: + for line in (PROC / 'meminfo').read_text(encoding='ascii', errors='replace').splitlines(): + if line.startswith('MemAvailable:'): + return int(line.split()[1]) * 1024 # the file says kB but means KiB + except (OSError, ValueError, IndexError): + pass + return None + + +def _cgroup_headroom(): + """Bytes left under the tightest memory limit of this process's cgroup and the cgroups above it (cgroup v2: + memory.max less memory.current), or None without such a limit.""" + try: + line = next(l for l in (PROC / 'self' / 'cgroup').read_text(encoding='ascii', errors='replace').splitlines() + if l.startswith('0::')) # the v2 entry; a v1 system has none + except (OSError, StopIteration): return None + parts = [p for p in line[3:].split('/') if p] + headroom = None + for n in range(len(parts), -1, -1): + folder = CGROUP.joinpath(*parts[:n]) + try: + limit = (folder / 'memory.max').read_text(encoding='ascii').strip() + if limit == 'max': + continue + left = max(0, int(limit) - int((folder / 'memory.current').read_text(encoding='ascii'))) + except (OSError, ValueError): + continue + headroom = left if headroom is None else min(headroom, left) + return headroom + + +def available_ram(): + """Physical memory not in use right now, capped by the memory limit this process runs under if there is one (the + job object's on Windows, the cgroup's on Linux). None where it cannot be read.""" + if sys.platform != 'win32': + avail = _meminfo_available() + if avail is None: + return None + headroom = _cgroup_headroom() + return avail if headroom is None else min(avail, headroom) status = _MemStatus() status.dwLength = ctypes.sizeof(status) if not ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(status)): diff --git a/tools/make_game.py b/tools/make_game.py index 36873f7b..a607c595 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -1524,10 +1524,25 @@ def step_prompts(out): 'to retry this step') +AWAKE = None # keep_awake's systemd-inhibit process on Linux; dropping it would close the pipe and end the hold + + def keep_awake(): - """Hold off sleep for this process's life (Windows): the build runs for many minutes with no input, and a PC that - sleeps in the middle stops the compiles. The hold ends with the process.""" + """Hold off sleep for this process's life: the build runs for many minutes with no input, and a PC that sleeps in + the middle stops the compiles. The hold ends with the process. Only sleep: the screen may still blank and lock, as + with Windows' ES_SYSTEM_REQUIRED. Linux: systemd-inhibit takes a lock on sleep and keeps it while its `cat` reads + the pipe this process holds open, so the lock also goes when this process is killed. Without systemd-inhibit (or a + login manager to take the lock) nothing happens.""" + global AWAKE if os.name != 'nt': + if AWAKE is None: + try: + AWAKE = subprocess.Popen(['systemd-inhibit', '--what=sleep', '--mode=block', '--who=NX2navi', + '--why=Building the game', 'cat'], stdin=subprocess.PIPE, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + start_new_session=True) # a Ctrl+C meant for the build leaves the lock + except OSError: + pass return try: import ctypes @@ -1551,12 +1566,31 @@ def load_state(path): return state +CPUINFO = Path('/proc/cpuinfo') + + def cpu_tag(): - """This processor's features the lifted code's level follows (cpu_level), as Windows reports them; part of the + """This processor's features the lifted code's level follows (cpu_level), as the system reports them: Windows + through IsProcessorFeaturePresent, Linux through the flags of /proc/cpuinfo (which lists a feature only when the + kernel runs with it). One digit each for SSE4.2, AVX2 and AVX-512F: '110' is AVX2 without AVX-512. Part of the compile key, so a game folder moved to a PC without them (or a new processor) is compiled again rather than kept with instructions the processor lacks. 'any' where it cannot be read.""" if os.name != 'nt': - return 'any' + try: + lines = CPUINFO.read_text(encoding='ascii', errors='replace').splitlines() + except OSError: + return 'any' + # One flags line per logical processor; a hybrid processor's cores can differ and the game runs on any of + # them, so only what all of them have counts. + flags = [] + for line in lines: + key, _, rest = line.partition(':') + if key.strip() == 'flags': + flags.append(set(rest.split())) + if not flags: # not x86 (its list is called Features) + return 'any' + have = set.intersection(*flags) + return ''.join('1' if f in have else '0' for f in ('sse4_2', 'avx2', 'avx512f')) try: import ctypes present = ctypes.windll.kernel32.IsProcessorFeaturePresent From a103673341d5f1fc2a87a63e3ad2e0f100ca093f Mon Sep 17 00:00:00 2001 From: Michael Schienbein Date: Mon, 9 Nov 2026 12:31:25 -0600 Subject: [PATCH 12/15] Make the SDL window honour monitor, --monitor and --list-monitors. On Linux the window ignored NX2NAVI_MONITOR (the log said it had no effect) and --list-monitors only printed that monitor selection is Win32-only. The SDL window now picks a monitor with the same rules as the Win32 one: auto (the system places it), primary, a 1-based index in listing order, or part of the monitor's name, ignoring case, the first match in list order winning. The monitors are SDL's displays; --list-monitors prints their index, name (the make and model, with the connector on Wayland and X11), pixel size and position, primary flag and display scale, then what NX2NAVI_MONITOR would pick, and the log says the same when the window opens. The window is created centred on the chosen monitor and centred again at its final size, and that position also names the display the fullscreen setting covers, which is how a Wayland window, which a client cannot move, opens fullscreen on the chosen output. On Wayland a windowed window opens where the compositor puts it; the log says so once. With a chosen monitor, the size widescreen.c reads for fullscreen before the window exists is that monitor's. Co-Authored-By: Claude Sonnet 5.5 --- docs/controls.md | 2 +- nx2navi.ini.example | 8 +- runtime/cfg.c | 2 +- runtime/gfx_window_sdl.c | 237 +++++++++++++++++++++++++++++++++++---- runtime/main.c | 7 +- 5 files changed, 225 insertions(+), 31 deletions(-) diff --git a/docs/controls.md b/docs/controls.md index 1f5127fe..7c139b2b 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -76,7 +76,7 @@ The game's *Swap Jump Controls* option trades B and X (jump on B, put away on X) | F10 | L3 + R3 (both stick clicks) | performance overlay: off, frame rate, full panel | | F11 | | borderless fullscreen on or off, saved to `nx2navi.ini` | -The window opens on the monitor `NX2NAVI_MONITOR` (`monitor` in `nx2navi.ini`) names, and F11 fullscreen covers the monitor the window is on. `nx2navi.exe --list-monitors` prints each monitor's index, device name, name, desktop rectangle, primary flag and DPI without game files; `nx2navi.exe --monitor ` sets `NX2NAVI_MONITOR` for one run and wins over the file. The value is `auto` (default), `primary`, an index from the list (counting from 1), or part of a device name (`display3`) or monitor name; when nothing matches, the log lists the monitors and the window is placed as with `auto`. Windows only. +The window opens on the monitor `NX2NAVI_MONITOR` (`monitor` in `nx2navi.ini`) names, and F11 fullscreen covers the monitor the window is on. `nx2navi.exe --list-monitors` prints each monitor's index, name (on Windows its device name too), desktop rectangle, primary flag and DPI without game files; `nx2navi.exe --monitor ` sets `NX2NAVI_MONITOR` for one run and wins over the file. The value is `auto` (default), `primary`, an index from the list (counting from 1), or part of a monitor name (on Windows also a device name such as `display3`; on Linux the name carries the connector, such as `dp-2`); when nothing matches, the log lists the monitors and the window is placed as with `auto`. On Wayland a window cannot place itself: only fullscreen (`fullscreen = 1`) opens on the chosen monitor, and a windowed window opens where the compositor puts it. The Minus+Plus combo never reaches the game. In the menu the arrow keys, the D-pad or the left stick move, Enter or A selects, Q and E or L and R switch tabs, Esc or B goes back (Switch button names, as in the table above). diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 1c324cce..d2abef85 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -223,9 +223,11 @@ # fullscreen = 0 # Which monitor the game window opens on, centred in its work area; fullscreen covers that monitor. auto (default): the # system places the window. primary; a number, counting from 1 in the order `nx2navi.exe --list-monitors` prints; or part -# of a GDI device name (display3) or of the monitor's name, ignoring case. For a virtual display that may not exist yet, -# use its name: when nothing matches, the log lists the monitors and the window is placed as with auto. The command line -# `--monitor ` overrides this for one run. Read at start; Windows only. +# of the monitor's name, ignoring case (on Windows also of a GDI device name, display3; on Linux the name carries the +# connector, such as dp-2). For a virtual display that may not exist yet, use its name: when nothing matches, the log +# lists the monitors and the window is placed as with auto. The command line `--monitor ` overrides this for one +# run. Read at start. On Wayland a window cannot place itself: only fullscreen opens on the chosen monitor, and a +# windowed window opens where the compositor puts it. # monitor = auto # 1, the default: the game keeps running while you are in another window; the keyboard stays quiet until the window is # in front again. 0: it pauses when its window loses focus, as it does on the Switch when Home is pressed; a load diff --git a/runtime/cfg.c b/runtime/cfg.c index 9e10c45e..3b434cd4 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -129,7 +129,7 @@ static const CfgMeta metas[] = { // Screen {"FULLSCREEN", "0", "0|1", LIVE}, {"WINDOW_SIZE", "1920x1080", NULL, LIVE}, {"VK_PRESENT", "mailbox", "mailbox|fifo|immediate", LIVE}, {"GPU_INDEX", "", NULL, RESTART}, - {"MONITOR", "auto", NULL, RESTART}, // auto|primary|<1-based index>| (gfx_window_win.c chosen_monitor) + {"MONITOR", "auto", NULL, RESTART}, // auto|primary|<1-based index>| (gfx_window_win.c, gfx_window_sdl.c chosen_monitor) {"PERF_OVERLAY", "off", "off|fps|full", LIVE}, {"OUTPUT_RES", "1920x1080", NULL, RESTART}, {"RENDER_SCALE", "game", "game|50|67|75|100|125|150|200", RESTART}, {"UPSCALER", "fsr", "fsr|bilinear", LIVE}, {"SHARPNESS", "80", "0..100", LIVE}, diff --git a/runtime/gfx_window_sdl.c b/runtime/gfx_window_sdl.c index 689fb911..8a1ef5dc 100644 --- a/runtime/gfx_window_sdl.c +++ b/runtime/gfx_window_sdl.c @@ -1,7 +1,7 @@ // The SDL3 back end of the window (gfx_wsys.h), for hosts without Win32: the window on a thread of its own that also -// runs SDL's event loop, F11 fullscreen (borderless, the desktop's mode), the settings NX2NAVI_WINDOW_SIZE and -// NX2NAVI_FULLSCREEN, the display's refresh rate, focus, the Vulkan surface. The swapchain and the present are -// gfx_present.c's. Keyboard and mouse events go to input_sdl.c and to the menu (ui/menu_sdl.cpp). +// runs SDL's event loop, F11 fullscreen (borderless, the desktop's mode), the settings NX2NAVI_WINDOW_SIZE, +// NX2NAVI_FULLSCREEN and NX2NAVI_MONITOR, the display's refresh rate, focus, the Vulkan surface. The swapchain and the +// present are gfx_present.c's. Keyboard and mouse events go to input_sdl.c and to the menu (ui/menu_sdl.cpp). // // SDL wants its video calls on the thread that initialised video, so this thread does them all: the others hand it // work through run_on_window (a user event and a wait) or post a user event. Video is initialised before the window @@ -121,7 +121,7 @@ void gfx_window_request_close(void) { if (g_win) post(EV_CLOSE); } -// ---- the display's refresh rate and size +// ---- the display's refresh rate static void refresh_rate_update(void) { const SDL_DisplayMode *m = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_win)); double hz = 0; @@ -130,15 +130,6 @@ static void refresh_rate_update(void) { gfx_window_set_refresh(hz); } -int gfx_window_monitor_size(int *w, int *h) { - if (!g_win) return 0; - const SDL_DisplayMode *m = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_win)); - if (!m) return 0; - *w = (int)(m->w * m->pixel_density + 0.5f); - *h = (int)(m->h * m->pixel_density + 0.5f); - return 1; -} - static void density_update(void) { float d = SDL_GetWindowPixelDensity(g_win); __atomic_store_n(&g_density_milli, (int32_t)((d > 0 ? d : 1) * 1000 + 0.5f), __ATOMIC_RELAXED); @@ -248,6 +239,187 @@ int gfx_window_vk_presentable(VkInstance inst, VkPhysicalDevice pd, uint32_t fam return video_up() && SDL_Vulkan_GetPresentationSupport(inst, pd, family); } +// ---- monitor choice (NX2NAVI_MONITOR, --monitor, --list-monitors) +// The monitors in SDL_GetDisplays order, which is the index the setting counts in (1-based) and --list-monitors prints. +// The video driver sets that order, which need not match the desktop's own list of monitors: it holds while the same +// monitors stay connected, but a monitor that appears or goes away can move the ones after it. SDL has one name per +// monitor: make and model, with the connector (such as DP-2) on Wayland and X11. The name pins one monitor more firmly +// than its index. These calls belong to the thread that initialised video: the window thread, or the main thread in +// --list-monitors. +#define MON_MAX 32 + +typedef struct { + SDL_DisplayID id; + SDL_Rect rc; // the monitor's pixels, at its position in the desktop's coordinates + int primary; + unsigned dpi; // the display scale as a DPI, 96 at 100% + char name[128]; // SDL_GetDisplayName; empty when SDL has none +} MonInfo; + +typedef struct { MonInfo m[MON_MAX]; int n; } MonList; + +static int enum_monitors(MonList *l) { + *l = (MonList){0}; + int n = 0; + SDL_DisplayID *ids = SDL_GetDisplays(&n); + if (!ids) return 0; + SDL_DisplayID primary = SDL_GetPrimaryDisplay(); + for (int i = 0; i < n && l->n < MON_MAX; i++) { + SDL_Rect rc; + if (!SDL_GetDisplayBounds(ids[i], &rc)) continue; + // A scaled desktop (Wayland at 125%) counts its coordinates in scaled units: the pixel density turns the size + // back into the monitor's pixels, and the content scale and the density together are the display scale + // (SDL_GetWindowDisplayScale gives a window the same product). The position stays in the desktop's units. + const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(ids[i]); + float density = mode && mode->pixel_density > 0 ? mode->pixel_density : 1, scale = SDL_GetDisplayContentScale(ids[i]); + if (!(scale > 0)) scale = 1; + const char *name = SDL_GetDisplayName(ids[i]); + MonInfo *m = &l->m[l->n++]; + *m = (MonInfo){.id = ids[i], .rc = {rc.x, rc.y, (int)(rc.w * density + 0.5f), (int)(rc.h * density + 0.5f)}, + .primary = ids[i] == primary, .dpi = (unsigned)(96 * scale * density + 0.5f)}; + snprintf(m->name, sizeof m->name, "%s", name ? name : ""); + } + SDL_free(ids); + return l->n; +} + +// `2 "HP Inc. HP E243 CNK0161ZY2 (DP-3)" 1920x1080 at 3456,756 dpi 120 (125%)`: one monitor, for the log and +// --list-monitors, with " primary" after the rectangle on the primary one. +static void monitor_line(const MonList *l, int i, char *out, size_t cap) { + const MonInfo *m = &l->m[i]; + snprintf(out, cap, "%d \"%s\" %dx%d at %d,%d%s dpi %u (%u%%)", i + 1, m->name, m->rc.w, m->rc.h, m->rc.x, m->rc.y, + m->primary ? " primary" : "", m->dpi, m->dpi * 100 / 96); +} + +static int monitor_unset(const char *v) { return !v || !*v || !SDL_strcasecmp(v, "auto"); } + +// Which monitor of the list a NX2NAVI_MONITOR value (not empty, not auto) names, as a 0-based index; -1 when none. +// Rules, in this order: "primary"; all digits, a 1-based index; any other text, a case-insensitive substring of a +// monitor's name (the first monitor in list order when several match). why gets the rule that matched, or the reason +// nothing did. The same rules as gfx_window_win.c resolve_monitor, which also matches the GDI device name. +static int resolve_monitor(const MonList *l, const char *want, char *why, size_t cap) { + if (!SDL_strcasecmp(want, "primary")) { + for (int i = 0; i < l->n; i++) + if (l->m[i].primary) { + snprintf(why, cap, "the primary monitor"); + return i; + } + snprintf(why, cap, "no monitor is marked primary"); + return -1; + } + if (want[strspn(want, "0123456789")] == 0) { + long idx = strtol(want, NULL, 10); + if (idx >= 1 && idx <= l->n) { + snprintf(why, cap, "index %ld of %d", idx, l->n); + return (int)idx - 1; + } + snprintf(why, cap, "index %s is outside 1..%d", want, l->n); + return -1; + } + int first = -1, hits = 0; + for (int i = 0; i < l->n; i++) + if (SDL_strcasestr(l->m[i].name, want)) { + if (first < 0) first = i; + hits++; + } + if (first < 0) { + snprintf(why, cap, "no monitor name contains it"); + return -1; + } + if (hits > 1) snprintf(why, cap, "monitor name contains it; the first of %d matches", hits); + else snprintf(why, cap, "monitor name contains it"); + return first; +} + +// The monitor NX2NAVI_MONITOR picks, copied to *out; 0 when it is unset or auto (the system places the window), or when +// it matches nothing now (the monitors present are logged, and the default placement applies). log: say what was decided +// and why. The window thread asks with log set; the size query below, which widescreen.c makes before the window +// exists, stays quiet. Window thread. +static int chosen_monitor(MonInfo *out, int log) { + const char *want = cfg_get("NX2NAVI_MONITOR"); + if (monitor_unset(want)) return 0; + MonList l; + char why[256], line[512]; + int n = enum_monitors(&l), pick = -1; + if (n) pick = resolve_monitor(&l, want, why, sizeof why); + else snprintf(why, sizeof why, "no monitor was found"); + if (pick < 0) { + if (log) { + rt_log("gfx: NX2NAVI_MONITOR=%s picks no monitor (%s); the window gets the default placement. Monitors present:", + want, why); + for (int i = 0; i < n; i++) { + monitor_line(&l, i, line, sizeof line); + rt_log("gfx: %s", line); + } + } + return 0; + } + if (log) { + monitor_line(&l, pick, line, sizeof line); + rt_log("gfx: NX2NAVI_MONITOR=%s picks monitor %s (%s)", want, line, why); + } + *out = l.m[pick]; + return 1; +} + +// --list-monitors (main.c): one line per monitor, then what the current NX2NAVI_MONITOR would pick. No game files. It +// runs on the main thread before any window thread exists, so that thread initialises video here and shuts it down again. +int list_monitors(void) { + if (!SDL_Init(SDL_INIT_VIDEO)) { + fprintf(stderr, "list-monitors: SDL video init failed (%s)\n", SDL_GetError()); + return 1; + } + MonList l; + char line[512], why[256]; + int n = enum_monitors(&l); + if (!n) { + fprintf(stderr, "list-monitors: no monitor found\n"); + SDL_Quit(); + return 1; + } + printf("index name desktop rectangle primary dpi\n"); + for (int i = 0; i < n; i++) { + monitor_line(&l, i, line, sizeof line); + printf("%s\n", line); + } + const char *want = cfg_get("NX2NAVI_MONITOR"); + if (monitor_unset(want)) { + printf("NX2NAVI_MONITOR is not set (auto): the system places the window\n"); + } else { + int pick = resolve_monitor(&l, want, why, sizeof why); + if (pick >= 0) printf("NX2NAVI_MONITOR=%s picks monitor %d (%s)\n", want, pick + 1, why); + else printf("NX2NAVI_MONITOR=%s picks no monitor (%s)\n", want, why); + } + SDL_Quit(); + return 0; +} + +typedef struct { int w, h, ok; } SizeReq; + +static void chosen_size(void *arg) { + SizeReq *r = arg; + MonInfo mon; + if (chosen_monitor(&mon, 0)) r->w = mon.rc.w, r->h = mon.rc.h, r->ok = 1; +} + +// Pixels of the monitor the window is on; 0 when unknown. Before the window exists (widescreen.c wide_init at start) a +// fullscreen window opens on the monitor NX2NAVI_MONITOR picks, so that one's size; without a choice the system decides +// where the window goes, which is not known yet. +int gfx_window_monitor_size(int *w, int *h) { + if (g_win) { + const SDL_DisplayMode *m = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_win)); + if (!m) return 0; + *w = (int)(m->w * m->pixel_density + 0.5f); + *h = (int)(m->h * m->pixel_density + 0.5f); + return 1; + } + SizeReq r = {0}; + if (monitor_unset(cfg_get("NX2NAVI_MONITOR")) || !video_up()) return 0; + run_on_window(chosen_size, &r); + *w = r.w, *h = r.h; + return r.ok; +} + // ---- gfx_wsys.h static int open_ok; @@ -255,21 +427,46 @@ static void open_window(void *arg) { (void)arg; int w, h; gfx_cfg_window_size(&w, &h); - g_win = SDL_CreateWindow("NX2navi", w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); + // NX2NAVI_MONITOR: the window is created centred on the chosen monitor; without a choice the system places it. + MonInfo mon; + int on_mon = chosen_monitor(&mon, 1); + SDL_PropertiesID props = SDL_CreateProperties(); + SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "NX2navi"); + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w); + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h); + SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_VULKAN_BOOLEAN, true); + SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, true); + SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true); + if (on_mon) { + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED_DISPLAY(mon.id)); + SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED_DISPLAY(mon.id)); + } + g_win = SDL_CreateWindowWithProperties(props); + SDL_DestroyProperties(props); if (!g_win) { rt_log("gfx: SDL_CreateWindow failed (%s)", SDL_GetError()); return; } set_pixel_size(w, h); // w x h were window units; the setting is in pixels + // Centred again at the size it ended up with. SDL also takes this position as the display the fullscreen below + // covers: a Wayland window cannot be moved, and this is how its fullscreen still opens on the chosen output. + if (on_mon) SDL_SetWindowPosition(g_win, SDL_WINDOWPOS_CENTERED_DISPLAY(mon.id), SDL_WINDOWPOS_CENTERED_DISPLAY(mon.id)); density_update(); note_applied(); refresh_rate_update(); if (cfg_get("NX2NAVI_BACKGROUND_WINDOW")) rt_log("gfx: NX2NAVI_BACKGROUND_WINDOW has no effect with SDL windows"); - const char *monitor = cfg_get("NX2NAVI_MONITOR"); // open_window runs once, so this is logged once - if (monitor && *monitor && strcasecmp(monitor, "auto")) - rt_log("gfx: NX2NAVI_MONITOR=%s has no effect with SDL windows: monitor selection is Win32-only (gfx_window_win.c)", monitor); + int wayland = !SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland"); + // open_window runs once, so this is logged once + if (on_mon && wayland) + rt_log("gfx: Wayland leaves the placement of a windowed window to the compositor, so NX2NAVI_MONITOR only decides " + "where the game opens in fullscreen"); if (gfx_cfg_fullscreen()) SDL_SetWindowFullscreen(g_win, true); SDL_SyncWindow(g_win); + SDL_DisplayID at = SDL_GetDisplayForWindow(g_win); + if (on_mon && (gfx_cfg_fullscreen() || !wayland) && at != mon.id) { + const char *name = SDL_GetDisplayName(at); + rt_log("gfx: the window is not on the chosen monitor; it is on \"%s\"", name ? name : "an unknown monitor"); + } input_sdl_window(g_win); open_ok = 1; } @@ -280,12 +477,6 @@ int wsys_open(void) { return open_ok; } -// --list-monitors (main.c) lists Win32 monitors only; see open_window for the setting. -int list_monitors(void) { - fprintf(stderr, "list-monitors: monitor selection is Win32-only; this build uses SDL windows\n"); - return 1; -} - typedef struct { VkInstance inst; VkSurfaceKHR surface; } SurfaceReq; static void make_surface(void *arg) { diff --git a/runtime/main.c b/runtime/main.c index b6b8dbb9..29624cb3 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -8,9 +8,10 @@ // show which device NX2NAVI_AUDIO_DEVICE picks; needs no game files // --monitor VALUE open the game window on one monitor: sets NX2NAVI_MONITOR before the settings are read, so it wins // over nx2navi.ini's `monitor =`. VALUE is auto (default), primary, a 1-based index as --list-monitors -// prints it, or part of a GDI device name (DISPLAY3) or of a monitor's name, case-insensitive -// --list-monitors print each monitor's index, GDI device name, name, desktop rectangle, primary flag and DPI; -// needs no game files +// prints it, or part of a monitor's name (on Windows also of its GDI device name, DISPLAY3), +// case-insensitive. Wayland places a windowed window itself: only fullscreen opens on the monitor +// --list-monitors print each monitor's index, name (on Windows also its GDI device name), desktop rectangle, +// primary flag and DPI; needs no game files // --selftest layout, dispatch table and host-function registry; needs no game files // --probe-mods the mods folder scan, order and switches (mods.c); needs no game files // --probe-loader load and relocate the modules, check them against the measured 1.2.1 values From a85f5c15d3567ada21eb1eb40aa843b1d04178f5 Mon Sep 17 00:00:00 2001 From: Michael Schienbein Date: Mon, 9 Nov 2026 15:36:43 -0600 Subject: [PATCH 13/15] Report where a Wayland fullscreen window ended up once the compositor placed it. SDL_SyncWindow returns before the compositor moves the fullscreen window to the chosen output, so the check right after it logged the output the window opened on, though the game then ran on the chosen monitor. On Wayland the report now waits until SDL sees the window on the chosen monitor, which it learns from a display, scale or mode change (Hyprland 0.56 sends only the latter two). Co-Authored-By: Claude Opus 5.5 --- runtime/gfx_window_sdl.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/runtime/gfx_window_sdl.c b/runtime/gfx_window_sdl.c index 8a1ef5dc..3b244869 100644 --- a/runtime/gfx_window_sdl.c +++ b/runtime/gfx_window_sdl.c @@ -136,6 +136,17 @@ static void density_update(void) { } // ---- the window thread +// NX2NAVI_MONITOR's display while a Wayland compositor has yet to place the fullscreen window (open_window): SDL learns +// the output a Wayland window is on later, with a display, scale or mode change (Hyprland sends only the latter two). +static SDL_DisplayID g_place_pending; + +static void report_placement(SDL_DisplayID want) { + SDL_DisplayID at = SDL_GetDisplayForWindow(g_win); + const char *name = SDL_GetDisplayName(at); + if (at == want) rt_log("gfx: the window is on the chosen monitor \"%s\"", name ? name : "an unknown monitor"); + else rt_log("gfx: the window is not on the chosen monitor; it is on \"%s\"", name ? name : "an unknown monitor"); +} + static void handle(const SDL_Event *e) { if (e->type == g_ev_base + EV_RUN) { RunReq *r = e->user.data1; @@ -172,6 +183,10 @@ static void handle(const SDL_Event *e) { density_update(); refresh_rate_update(); gfx_window_mark_dirty(); + if (g_place_pending && SDL_GetDisplayForWindow(g_win) == g_place_pending) { + report_placement(g_place_pending); + g_place_pending = 0; + } break; case SDL_EVENT_WINDOW_FOCUS_GAINED: case SDL_EVENT_WINDOW_FOCUS_LOST: @@ -462,10 +477,11 @@ static void open_window(void *arg) { "where the game opens in fullscreen"); if (gfx_cfg_fullscreen()) SDL_SetWindowFullscreen(g_win, true); SDL_SyncWindow(g_win); - SDL_DisplayID at = SDL_GetDisplayForWindow(g_win); - if (on_mon && (gfx_cfg_fullscreen() || !wayland) && at != mon.id) { - const char *name = SDL_GetDisplayName(at); - rt_log("gfx: the window is not on the chosen monitor; it is on \"%s\"", name ? name : "an unknown monitor"); + // A Wayland compositor places the fullscreen window after SDL_SyncWindow returns; handle reports it once the window + // is on the chosen monitor. + if (on_mon && (gfx_cfg_fullscreen() || !wayland)) { + if (wayland && SDL_GetDisplayForWindow(g_win) != mon.id) g_place_pending = mon.id; + else report_placement(mon.id); } input_sdl_window(g_win); open_ok = 1; From 47d9a2d5025ed7af59dd5d079547c89dc0448fa8 Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Mon, 9 Nov 2026 19:48:43 -0500 Subject: [PATCH 14/15] Seal the texture pool set when an early replay part is submitted. A replay part handed to the queue early kept reading the bindless pool set while later parts of the same submission wrote new entries into it. That updates descriptors a pending command buffer reads, and on RADV a texture whose memory was mapped after the part was submitted is absent from its page tables. This caused a gfxhub read fault in the 128x128 pass of the first world frame in Kokiri Forest. Seal the set when a part goes, take another ring set at the next sync that has entries to write, and keep recording the same part while every other set belongs to the submission. Document the descriptor lifetime contract in maxwell.md 4.9. Fixes #257. --- docs/maxwell.md | 1 + runtime/vk_exec.c | 7 ++++--- runtime/vk_exec.h | 4 ++++ runtime/vk_exec_draw.c | 33 ++++++++++++++++++++++++++++----- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/docs/maxwell.md b/docs/maxwell.md index ba9f1814..c51f3a32 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -89,6 +89,7 @@ Source shorthands: | 4.6 | **Compute shared memory**: 48 KiB per block on GM20B. | AMD 64 KiB. | Ours: `QueryComputeSharedMemorySize` from job (`Program.cs`). | OGD `clb1c0.h` | | 4.7 | **Virtual memory pools** map physical pool pages; the same bytes reachable at two GPU addresses. | Two `VkBuffer`s over one allocation need alias-aware barriers. | — | — | | 4.8 | **Command list calls**: a `CallCommands` / `CallList` is not a GPU subroutine. A call is stored as a control entry; at submit the CPU walks the entries and recurses into the called list, emitting gpfifo entries that point at the command words, which the GPU fetches later (prefetch can be switched off per entry). So list structure is fixed at submit, command words are read when fetched. | n/a | Ours: `OP_CALL` is walked at replay, `CallCommands` inlines the recording at record time (`vk_exec.c` `walk`, `nvn_api_cmd.c` `inline_recording`). Equivalent for control flow; deferred bind handles are the data read late (2.4). | — | +| 4.9 | **The texture pool is read as the GPU goes** (bindless `TEX.B` handles index it), and the GPU runs a submission right after it is submitted, so it sees the pool as of about the submit; Ocarina registers the next submission's textures right after submitting. | A descriptor a pending command buffer may read must not be updated (update-after-bind allows only unused ones), and on RADV a view of memory allocated after a submit is not in that submit's page tables. | A replay part handed to the queue early (`NX2NAVI_SPLIT_DRAWS`) kept reading the set later parts wrote: GPU page fault (`gfxhub`, TCP read) on the 128×128 probe pass of Kokiri Forest's first world frame, about 40 s in, nearly every run on RX 9070 XT. Ours: one pool set per submission from a ring, sealed when a part goes; the next sync that has entries to write takes another set, and no part is split off while every other set belongs to the same submission (`vk_exec_draw.c` `vk_exec_pool_split`, `pool_take_set`). | — | ## 5. Fixed function diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index ef82de21..621fe82d 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -870,7 +870,8 @@ static void set_rt(VkExec *ex, const p_set_rt *r) { // command-buffer state is lost there (bindings and dynamic state: vk_exec_draw_new_cb, ex->dirty); barriers, layouts and // queries are not: a barrier's first scope is everything earlier in submission order, image layouts belong to the image, // and occlusion queries end with their pass. Fences, completion callbacks and reports stay per submission: only the last -// part signals the value. +// part signals the value. The texture pool set a part binds is sealed when the part goes (vk_exec_pool_split): later +// parts write and bind another, and a part is not split off while no other set is free of this submission. // A part also spans at least NX2NAVI_SPLIT_US (1000) of replay time: a split costs about 40 us of the render thread // (vkQueueSubmit2, a command buffer, the mirror prologue), and a fight frame of 2,000 draws made 15 parts on the 7900X; // a millisecond of recording is a fraction of a millisecond of GPU work, so the GPU still starts early. @@ -922,8 +923,8 @@ static void maybe_split(VkExec *ex) { } split_draws = 0; split_at = split_n; - if (idle && ex->recorded != part_start) n_idle_parts++; - if (ex->recorded == part_start) return; + if (ex->recorded == part_start || !vk_exec_pool_split(ex->fence)) return; + if (idle) n_idle_parts++; if (ex->in_pass) vk_exec_pass_end(ex), n_pass_splits++; // the next draw begins the pass again, in the new part ex->cb = vk_sync_split(); part_start = ex->recorded; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 0c62b5b0..81e1a1bf 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -137,6 +137,10 @@ void vk_exec_inline_end(int reopen); // vk_exec_draw.c: forget what the command buffer being recorded has bound (pipeline, sets, dynamic state, vertex // buffers): the replay continues in a new one (vk_sync_split). void vk_exec_draw_new_cb(void); +// vk_exec_draw.c, before a part of submission fence goes to the queue (vk_exec.c maybe_split): 1 when it may go. The +// texture pool set it binds is then sealed and the next draw that needs the pools takes another; 0 when every other set +// is bound by this submission's earlier parts, and the replay keeps recording into the same part. +int vk_exec_pool_split(uint64_t fence); // vk_exec_draw.c: the vertex and fragment shader of the last draw recorded into this command buffer (NULL: none yet). void vk_exec_draw_shaders(const GfxShader **vs, const GfxShader **fs); VkBuffer vk_exec_inline_buffer(VkDeviceSize size); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 666ff689..b5425792 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -1702,8 +1702,13 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto // dialogue frames, a fifth of them; none once each submission waited for its GPU work, NX2NAVI_SUBMIT_FINISH). A // submission's first pool_sync takes the next set (pool_take_set), waits for the GPU only if that set's last // submission is still running, and brings it up to date from the change log (every entry written since the set was -// last current; the whole set when the log has wrapped). Writes within a submission go to its own set at once, as -// before. +// last current; the whole set when the log has wrapped). Writes within a submission go to its own set at once, until a +// part of the submission goes to the queue early (vk_exec.c maybe_split): that part may read any entry of the set, so +// the set is sealed (vk_exec_pool_split) and the next sync takes the following set as if a new submission began. +// Writing into it instead updated descriptors a pending command buffer reads, which Vulkan does not allow: the part +// sampled textures registered after it went, and on RADV a texture whose memory was mapped after the part's submit is +// absent from that submit's page tables, a GPU page fault on the 128x128 probe pass of Kokiri Forest's first frame +// (RADV's radv_wait_for_vm_map_updates hid it, as did NX2NAVI_SPLIT_DRAWS=0). volatile uint32_t vk_pool_kinds_used; #define POOL_SETS 4 #define POOL_LOG (1u << 20) // entries of the change log: binding * VK_POOL_TEX + id, one per write @@ -1722,7 +1727,7 @@ static struct { uint64_t switches, waits, catchup, written, rewrites, catch_ns, sync_ns, n_slots, n_refill, n_fresh; uint64_t n_by_reg, n_by_addr; // refills whose token changed by a registration, by an address generation only uint64_t dirty_pos; // vk_texture_dirty_slots_locked position (0: the next sync walks every slot) - int ready, uab; + int ready, uab, sealed; // sealed: a part of the current submission already reads sets[cur] uint32_t kinds, gen, epoch, sgen; int retry; // the last sync left slots to the creation budget uint64_t *tok; // per texture slot: vk_texture_slot_token it was filled under (0: stand-ins) @@ -1871,18 +1876,29 @@ static void pool_entry_to(VkDescriptorSet set, uint32_t e) { // A submission's first pool use: the next set of the ring, once the GPU is done with it. 1 when it switched; *from is // the log position the set holds writes up to (pool_catch_up brings it level once pool_sync has refreshed the entries: // a view written into a set must be alive, and the refresh is what replaces the views of destroyed textures). +static int pool_current(void); + static int pool_take_set(VkExec *ex, uint64_t *from) { - if (PL.cur_fence == ex->fence) return 0; + // a sealed set stays bound while it is current: reading it is what the pending part does too + if (PL.cur_fence == ex->fence && (!PL.sealed || pool_current())) return 0; int i = (PL.cur + 1) % PL.nsets; if (PL.nsets > 1) { const GfxBackend *b = gfx_backend(); if (b->fence_done() < PL.set_fence[i]) PL.waits++, b->fence_wait(PL.set_fence[i], UINT64_MAX); } *from = PL.set_synced[i]; - PL.cur = i, PL.set = PL.sets[i], PL.cur_fence = ex->fence, PL.set_fence[i] = ex->fence; + PL.cur = i, PL.set = PL.sets[i], PL.cur_fence = ex->fence, PL.set_fence[i] = ex->fence, PL.sealed = 0; return PL.nsets > 1; } +int vk_exec_pool_split(uint64_t fence) { + if (PL.cur_fence != fence || PL.sealed) return 1; // no part of this submission has the current set unsealed + // pool_take_set moves to the next set of the ring: it must not be bound by this submission's earlier parts too + if (PL.nsets < 2 || PL.set_fence[(PL.cur + 1) % PL.nsets] == fence) return 0; + PL.sealed = 1; + return 1; +} + // The current set gets the entries written into other sets in [from, to) of the log, as PL.view / PL.samp hold them now. static uint64_t pool_memo_hits; // pool_fill: slots that shared another slot's lookup (defined with pool_memo below) @@ -2008,6 +2024,7 @@ static void pool_sync(VkExec *ex) { if (!PL.layout) vk_pool_set_layout(); uint64_t from = 0, pre = PL.logn; int switched = pool_take_set(ex, &from); // every submission its own set, before the shortcut + if (PL.sealed) return; // current when checked: a change since waits for the next sync, which takes a new set uint64_t t0 = kern_now_ns(); pool_sync_entries(ex); uint64_t t1 = kern_now_ns(); @@ -2035,6 +2052,12 @@ static void pool_dirty_slot(int id, void *arg) { // vk_texture_dirty_slots_loc else d->retry = 1; } +// Every entry as the slots, kinds and samplers are now (pool_sync_entries has nothing to write). +static int pool_current(void) { + return PL.ready && !PL.retry && __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED) == PL.kinds && + vk_texture_slots_gen() == PL.gen && vk_texture_epoch() == PL.epoch && vk_sampler_gen() == PL.sgen; +} + static void pool_sync_entries(VkExec *ex) { uint32_t used = __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED); uint32_t gen = vk_texture_slots_gen(), ep = vk_texture_epoch(), sg = vk_sampler_gen(); From 6dcb11f93c3a2d769d774dae7e7cfb40c0501d08 Mon Sep 17 00:00:00 2001 From: tangerineturtle1 <178368288+tangerineturtle1@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:59:48 -0500 Subject: [PATCH 15/15] Read the upscaler choice once in the DLAA evaluation, so a change from the present's thread cannot split it between FSR 3.1 and DLSS. vk_dlaa_frame runs with the game's present and vk_dlaa_pass with the replay, which can be another thread. evaluate read the choice three times; when the menu's change from FSR 3.1 to a DLSS mode landed between two of the reads, the rest of the function took DLSS's path with NGX never brought up. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_dlaa.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index a116a994..8abd8911 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -1399,8 +1399,11 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { struct Size *e = size_for(cb, w, h, ow, oh); if (!e) return dl_fail("work image creation", 0); if (e != D.last) D.reset = 1, D.why[0]++, e->chars_ok = 0; // another size's history is from another moment - if (!use_fsr() && !N.ngx_up) return; // neither runs this frame: vk_dlaa_frame turns DLAA off at the next present - if (!use_fsr() && !D.params && !NGX_OK(N.alloc_params(&D.params))) return dl_fail("parameter allocation", 0); + // Read once: vk_dlaa_frame runs on the present's thread and can clear fsr_frame while this replay is between the + // checks below, which would send the rest of this function to DLSS's path without NGX after all. + const int fsr = use_fsr(); + if (!fsr && !N.ngx_up) return; // neither runs this frame: vk_dlaa_frame turns DLAA off at the next present + if (!fsr && !D.params && !NGX_OK(N.alloc_params(&D.params))) return dl_fail("parameter allocation", 0); NgxParam *p = D.params; VkImageView zv = vk_exec_target_view(z->img, z->format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT, z->level, z->base_layer, 1, 0); @@ -1424,7 +1427,7 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { uint64_t c0 = timed ? kern_now_ns() : 0; rec_drain(); // NGX records into the command buffer itself: the recorder thread must have made every call so far if (timed) T.cpu_drain += kern_now_ns() - c0; - if (use_fsr()) { // FSR 3.1 (vk_ffx.cpp) in DLSS's place: the same images, then the same blit below + if (fsr) { // FSR 3.1 (vk_ffx.cpp) in DLSS's place: the same images, then the same blit below if (t->level || t->base_layer || z->level || z->base_layer) return dl_fail("FSR 3.1 on a target that is not level 0, layer 0", 0); if (D.last_color != t->image) target_changed(t); uint64_t now = plat_now_ns();