From a1964a0c31e720a3b7189b70188d0f1e793ed8a6 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 4 Nov 2026 14:58:04 +0300 Subject: [PATCH 1/8] Free texture pin copies once no recording names them Every texture initialization got a 128-byte host copy for recordings to point at, kept for the whole run. The game initializes its transient targets again every frame (about 29 new copies a frame on 1.4.3), so host memory grew by about 44 MiB per minute of play, standing still too, until the commit limit ended long sessions (#670). Each pin now counts the recording copies whose ops name it: counted in flatten_recording, uncounted when a copy or a per-submission clone is freed. A pin with no copy left is freed NVN_PIN_AGE (1200) submits after its last use, so a recording still in progress keeps its pins, and never while the replay holds it as a current render target, which carries over to the next submission and is compared by pointer. TOTK_PIN_SWEEP=0 keeps every copy. A 300 s standing-still run on the field save: host memory without VRAM grew 5 MiB, against about 220 MiB before. Co-Authored-By: Claude Opus 5.5 --- runtime/gfx_null.c | 1 + runtime/nvn_api.h | 11 ++- runtime/nvn_api_cmd.c | 188 ++++++++++++++++++++++++++++++++++++++---- runtime/nvn_probe.c | 50 +++++++++++ runtime/vk_exec.c | 4 + runtime/vk_exec.h | 2 +- 6 files changed, 236 insertions(+), 20 deletions(-) diff --git a/runtime/gfx_null.c b/runtime/gfx_null.c index 073c3182..0f89abc1 100644 --- a/runtime/gfx_null.c +++ b/runtime/gfx_null.c @@ -100,6 +100,7 @@ static void null_replay(uint64_t handle) { } static uint64_t null_submit(const uint64_t *handles, int n) { + nvn_pins_sweep(nvn_submit_count(), NULL, 0); // no replay state outlives a submission here for (int i = 0; i < n; i++) null_replay(handles[i]); return __atomic_add_fetch(&fence, 1, __ATOMIC_ACQ_REL); } diff --git a/runtime/nvn_api.h b/runtime/nvn_api.h index 879174fa..b74253ab 100644 --- a/runtime/nvn_api.h +++ b/runtime/nvn_api.h @@ -75,7 +75,8 @@ uint64_t nvn_tex_view_offset(const tex_t *t, const view_t *v); // A command handle (what QueueSubmitCommands receives) points at a host copy of one recording: a flat sequence of // 8-byte-aligned records {op_hdr, payload}, OP_BEGIN first and OP_END last. Called recordings are inlined, so a copy // holds no OP_CALL or OP_JUMP. Texture pointers in payloads (p_set_rt, p_copy_b2t, p_copy_t2t, p_clear_tex) are -// immutable host copies that live for the run, one per tex_t.serial. Deferred binds (p_bind_handles.src != 0) hold the +// immutable host copies ("pins"), one per tex_t.serial, that live while a recording copy names them and a while after +// (nvn_pins_sweep). Deferred binds (p_bind_handles.src != 0) hold the // handles read at submit (nvn_submit_prepare). Payloads of the ops nvn_obj.h does not define: typedef struct { volatile uint32_t *val; uint32_t mode, value; } p_signal_event; // OP_SIGNAL_EVENT; mode: NVNeventSignalMode typedef struct { uint64_t type, addr; } p_report_counter; // OP_REPORT_COUNTER: NVNcounterType, NVNcounterData address @@ -99,6 +100,14 @@ int nvn_handle_live(uint64_t handle); // 1 while the handle // not added again) uint64_t nvn_handle_bytes(uint64_t handle); // bytes of the copy, OP_BEGIN to OP_END; 0 if not live uint64_t nvn_submit_count(void); // QueueSubmitCommands calls so far +// Submits a texture pin outlives its last tex_pin and the release of the last recording copy naming it: about 20 s at +// one submit a frame (MacNativeGames frees after two 600-submit windows). +#define NVN_PIN_AGE 1200 +// Frees the texture pins no recording copy has named for NVN_PIN_AGE submits (nvn_api_cmd.c, "texture pins"), except keep[0..nkeep): +// the replay's current render targets, which carry over to the next submission. Called between submissions by the +// thread that replays them; now = nvn_submit_count(). +void nvn_pins_sweep(uint64_t now, const tex_t *const *keep, int nkeep); +void nvn_pins_stats(uint64_t *live, uint64_t *freed); // pins that exist, pins freed so far const char *nvn_op_name(uint32_t op); // "DRAW_IDX", ...; "?" past OP_COUNT_ // MemoryPoolInvalidateMappedRange: the CPU is about to read pool storage the GPU wrote. Weak no-op in nvn_ext.c; a back // end that renders into host-side copies defines it and writes them back. diff --git a/runtime/nvn_api_cmd.c b/runtime/nvn_api_cmd.c index 1dbff42c..21644b76 100644 --- a/runtime/nvn_api_cmd.c +++ b/runtime/nvn_api_cmd.c @@ -4,7 +4,7 @@ // games/totk/third_party/d2r_gfx/gfx/nvn/hle_nvn.c @ b75be8cf (MIT, Copyright (c) 2026 jhaizhou; origin d2r-mobile), // lines 647-1062, 1261-1286, 1486-1760 and 1975-1990, and the *Deferred and memory-callback entry points of // totk_nvn_ext.c in the same directory. Metal, mach, the translation thread, Apple diagnostics, the per-frame -// statistics and the long-session sweeps (copy marks in control memory, texture pin sweep) dropped. +// statistics and the copy marks in control memory dropped; the texture pin sweep replaced by counting (texture pins). // // Records are written into a growable host buffer per command buffer, never into the game's command memory: games hand // the same command memory to several command buffers recording at once (budgeted for real NVN encodings), and our @@ -17,6 +17,7 @@ #include #include +#include "cfg.h" #include "gfx.h" #include "host.h" #include "nvn_api.h" @@ -235,30 +236,169 @@ static void copy_view(view_t *dst, uint8_t *has, const NVNtextureView *v) { // ---------- texture pins // Recordings refer to textures by pointer, but the game may finalize or reuse an NVNtexture after recording and before // the submit (NVN itself captured only GPU state). Records therefore point at an immutable host copy per texture -// initialization (tex_t.serial). The copies are kept for the whole run (MacNativeGames sweeps them for long sessions). -static PlatMutex pin_lock = PLAT_MUTEX_INIT; -static nvn_hmap pins = {.klen = 8}; // serial -> tex_t copy +// initialization (tex_t.serial). The game initializes its transient targets again every frame, about 29 new serials a +// frame on 1.4.3, so copies kept for the run grew the heap by about 44 MiB per minute of play, standing still too, until +// the commit limit ended the game after a few hours. A copy is freed once nothing can read it any more: +// - refs counts the recording copies whose ops name it: live ones, released ones a queued submission still reads, and +// per-submission clones (pin_refs, at flatten_recording and wherever such a copy is freed or cloned); +// - a recording in progress holds pins no copy counts yet, through its own tex_pin calls and through ops inlined from a +// copy that was released since: a pin goes only NVN_PIN_AGE submits after its last tex_pin and after refs fell to 0; +// - the replay's current render targets carry over to later submissions and are compared by pointer (vk_exec.c set_rt), +// so a freed address reused by a new pin would read as the same target: nvn_pins_sweep runs between submissions on +// the thread that replays them and keeps the pins it is given. +// MacNativeGames hit the same growth (3.16 million copies, 405 MB after 27 minutes) and marks pins by walking every live +// recording each window (hle_nvn.c pin_scan); counting at copy and release walks each copy's ops once more, when it is +// freed. TOTK_PIN_SWEEP=0 keeps every copy. +typedef struct { + tex_t t; // first: ops hold pins as tex_t pointers + uint64_t last_use; // nsubmits at the last tex_pin or the last drop of refs + uint32_t refs; + uint8_t queued; // in pin_q +} pin_t; +_Static_assert(offsetof(pin_t, t) == 0, "ops hold pins as tex_t pointers"); +typedef struct { pin_t *p; uint64_t at; } pin_due; +static PlatMutex pin_lock = PLAT_MUTEX_INIT; // lock order: copy_lock, then pin_lock +static nvn_hmap pins = {.klen = 8}; // serial -> pin_t +static pin_due *pin_q; // pins with no counted copy, oldest first: [pin_qh, pin_qn) +static size_t pin_qh, pin_qn, pin_qcap; +static uint64_t pins_live, pins_freed; +static uint64_t nsubmits; // QueueSubmitCommands calls so far + +static void pin_queue(pin_t *p, uint64_t at) { // pin_lock held + if (p->queued) return; + if (pin_qh && pin_qh * 2 >= pin_qn) { + memmove(pin_q, pin_q + pin_qh, sizeof *pin_q * (pin_qn - pin_qh)); + pin_qn -= pin_qh, pin_qh = 0; + } + if (pin_qn == pin_qcap) { + pin_qcap = pin_qcap ? pin_qcap * 2 : 4096; + if (!(pin_q = realloc(pin_q, sizeof *pin_q * pin_qcap))) rt_fatal("nvn: out of memory (texture pin queue)"); + } + pin_q[pin_qn++] = (pin_due){p, at}; + p->queued = 1; +} static const tex_t *tex_pin(const NVNtexture *nt) { const tex_t *t = (const tex_t *)nt; if (!t) return NULL; uint64_t serial = t->serial; if (!serial) rt_fatal("nvn: a recording references the uninitialized texture object %p", (const void *)t); - plat_mutex_lock_shared(&pin_lock); - const tex_t *r = nvn_hm_get(&pins, &serial); + uint64_t now = __atomic_load_n(&nsubmits, __ATOMIC_RELAXED); + plat_mutex_lock_shared(&pin_lock); // nvn_pins_sweep frees under the exclusive lock + pin_t *r = nvn_hm_get(&pins, &serial); + if (r) __atomic_store_n(&r->last_use, now, __ATOMIC_RELAXED); plat_mutex_unlock_shared(&pin_lock); - if (r) return r; + if (r) return &r->t; plat_mutex_lock(&pin_lock); r = nvn_hm_get(&pins, &serial); if (!r) { - tex_t *copy = malloc(sizeof *copy); - if (!copy) rt_fatal("nvn: texture pin copy"); - *copy = *t; - nvn_hm_put(&pins, &serial, copy); - r = copy; + if (!(r = malloc(sizeof *r))) rt_fatal("nvn: texture pin copy"); + *r = (pin_t){.t = *t}; + nvn_hm_put(&pins, &serial, r); + pins_live++; + pin_queue(r, now); // freed like any other if no copy ever names it + } + __atomic_store_n(&r->last_use, now, __ATOMIC_RELAXED); + plat_mutex_unlock(&pin_lock); + return &r->t; +} + +// The pins an op's payload names, into out (at most 9); returns how many. +static int op_pins(uint32_t op, const void *a, const tex_t *out[9]) { + int k = 0; + switch (op) { + case OP_SET_RT: { + const p_set_rt *r = a; + int n = r->n < 0 ? 0 : r->n > 8 ? 8 : r->n; + for (int i = 0; i < n; i++) + if (r->color[i]) out[k++] = r->color[i]; + if (r->depth) out[k++] = r->depth; + break; + } + case OP_COPY_B2T: case OP_COPY_T2B: + if (((const p_copy_b2t *)a)->tex) out[k++] = ((const p_copy_b2t *)a)->tex; + break; + case OP_CLEAR_TEX: + if (((const p_clear_tex *)a)->tex) out[k++] = ((const p_clear_tex *)a)->tex; + break; + case OP_COPY_T2T: { + const p_copy_t2t *c = a; + if (c->src) out[k++] = c->src; + if (c->dst) out[k++] = c->dst; + break; + } + default: break; + } + return k; +} + +static void pin_ref(const tex_t *t, int delta, uint64_t now) { + pin_t *p = (pin_t *)(uintptr_t)t; + if (delta > 0) { + __atomic_add_fetch(&p->refs, 1, __ATOMIC_RELAXED); + return; + } + __atomic_store_n(&p->last_use, now, __ATOMIC_RELAXED); + if (__atomic_sub_fetch(&p->refs, 1, __ATOMIC_ACQ_REL)) return; + plat_mutex_lock(&pin_lock); + pin_queue(p, now); + plat_mutex_unlock(&pin_lock); +} + +// Counts (delta 1) or uncounts (-1) the pins named by the copy at handle, OP_BEGIN to OP_END; returns how many. +static uint32_t pin_refs(const uint8_t *copy, int delta) { + uint64_t now = __atomic_load_n(&nsubmits, __ATOMIC_RELAXED); + uint32_t n = 0; + for (const op_hdr *h = nvn_op_first((uintptr_t)copy); h->op != OP_END; h = nvn_op_next(h)) { + const tex_t *p[9]; + int k = op_pins(h->op, nvn_op_payload(h), p); + for (int i = 0; i < k; i++) pin_ref(p[i], delta, now); + n += (uint32_t)k; + } + return n; +} + +void nvn_pins_sweep(uint64_t now, const tex_t *const *keep, int nkeep) { + if (now < NVN_PIN_AGE) return; + static int on = -1; + if (on < 0) { + const char *s = cfg_get("TOTK_PIN_SWEEP"); + on = !(s && !strcmp(s, "0")); + if (!on) rt_log("nvn: TOTK_PIN_SWEEP=0: texture pin copies are kept for the run (the heap grows in long sessions)"); + } + if (!on) return; + static uint64_t logged; + plat_mutex_lock(&pin_lock); + for (int budget = 1024; pin_qh < pin_qn && budget; budget--) { + pin_due d = pin_q[pin_qh]; + if (d.at > now - NVN_PIN_AGE) break; + pin_qh++; + pin_t *p = d.p; + p->queued = 0; + if (__atomic_load_n(&p->refs, __ATOMIC_ACQUIRE)) continue; // queued again when the count falls to 0 + uint64_t last = __atomic_load_n(&p->last_use, __ATOMIC_RELAXED); + int kept = 0; + for (int i = 0; i < nkeep; i++) kept |= keep[i] == &p->t; + if (kept || last > now - NVN_PIN_AGE) { + pin_queue(p, kept ? now : last); + continue; + } + nvn_hm_del(&pins, &p->t.serial); + free(p); + pins_live--, pins_freed++; + } + if (now / 7200 != logged) { + logged = now / 7200; + rt_log("nvn: texture pins at submit %llu: %llu live, %llu waiting, %llu freed so far", (unsigned long long)now, + (unsigned long long)pins_live, (unsigned long long)(pin_qn - pin_qh), (unsigned long long)pins_freed); } plat_mutex_unlock(&pin_lock); - return r; +} + +void nvn_pins_stats(uint64_t *live, uint64_t *freed) { + plat_mutex_lock_shared(&pin_lock); + *live = pins_live, *freed = pins_freed; + plat_mutex_unlock_shared(&pin_lock); } // ---------- recording copies @@ -266,14 +406,15 @@ static const tex_t *tex_pin(const NVNtexture *nt) { // adding control memory again releases the copies recorded there without walking every live copy. 4 KiB pages cost // about 500 lookups per AddControlMemory under the exclusive lock (1.4.3 field: 53 calls a frame, 2 MiB ranges, 3.7 // copies per page), and the recording threads waited on it. -typedef struct { uint64_t magic, bytes, made_at, used_at, deferred; } copy_prefix; // deferred: 0 unknown, 1 none, 2 some +// deferred: 0 unknown, 1 none, 2 some; pins: texture pins its ops name, counted in their refs (pin_refs) +typedef struct { uint64_t magic, bytes, made_at, used_at, deferred, pins; } copy_prefix; _Static_assert(sizeof(copy_prefix) % 8 == 0, "records stay 8-byte aligned"); typedef struct { uintptr_t lo, hi; uint8_t *copy; } cmd_copy; typedef struct { cmd_copy *v; uint32_t n, cap; } copy_bucket; static PlatMutex copy_lock = PLAT_MUTEX_INIT; // guards the two maps, the counters and every prefix static nvn_hmap copy_pages = {.klen = 8}; // page -> copy_bucket static nvn_hmap copy_live = {.klen = 8}; // handle -> copy_prefix of a live copy -static uint64_t ncopies, copy_bytes, copies_made, nsubmits; +static uint64_t ncopies, copy_bytes, copies_made; // Asynchronous replay: copies released by the game, and per-submission clones, that a queued submission still reads. // Freed once gfx_replayed_submits() reaches their used_at. Empty with synchronous replay. static copy_prefix **zombies; @@ -288,10 +429,15 @@ static void zombie_add(copy_prefix *pf) { // copy_lock held zombies[nzombies++] = pf; } +static void copy_free(copy_prefix *pf) { // copy_lock held + if (pf->pins) pin_refs((const uint8_t *)(pf + 1), -1); + free(pf); +} + static void zombie_sweep(uint64_t replayed) { // copy_lock held for (uint32_t i = 0; i < nzombies;) { if (zombies[i]->used_at <= replayed) { - free(zombies[i]); + copy_free(zombies[i]); zombies[i] = zombies[--nzombies]; } else { i++; @@ -337,19 +483,24 @@ static uint8_t *flatten_recording(const uint8_t *p, size_t n) { uint8_t *out = realloc_wait(NULL, sizeof(copy_prefix) + n, "command copy"); if (!out) rt_fatal("nvn: command copy allocation"); memcpy(out + sizeof(copy_prefix), p, n); + uint64_t pins_named = 0; for (size_t at = 0;;) { // the records stay checked: a corrupt one stops here, not in the replay op_hdr h; memcpy(&h, p + at, sizeof h); if (h.size < 8 || (h.size & 7) || h.op >= OP_COUNT_ || h.op == OP_JUMP || h.op == OP_CALL || at + h.size > n || (!at && h.op != OP_BEGIN)) rt_fatal("nvn: corrupt recording at %p (op %u size %u)", (const void *)(p + at), h.op, h.size); + const tex_t *pinned[9]; + int k = op_pins(h.op, out + sizeof(copy_prefix) + at + 8, pinned); + for (int i = 0; i < k; i++) pin_ref(pinned[i], 1, 0); + pins_named += (uint64_t)k; at += h.size; if (h.op == OP_END) { if (at != n) rt_fatal("nvn: recording at %p ends at %zu of %zu bytes", (const void *)p, at, n); break; } } - *(copy_prefix *)out = (copy_prefix){COPY_MAGIC, n, 0, 0}; + *(copy_prefix *)out = (copy_prefix){.magic = COPY_MAGIC, .bytes = n, .pins = pins_named}; return out + sizeof(copy_prefix); } @@ -399,7 +550,7 @@ static void release_copies(uintptr_t lo, uintptr_t hi) { copy_bytes -= pf->bytes; pf->magic = 0; if (pf->used_at > replayed) zombie_add(pf); // a queued submission still reads it - else free(pf); + else copy_free(pf); b->v[i] = b->v[--b->n]; } if (!b->n) { @@ -1035,6 +1186,7 @@ void nvn_submit_prepare_async(const uint64_t *handles, uint64_t *out, int n) { copy_prefix *cl = malloc(sz); if (!cl) rt_fatal("nvn: out of memory (recording clone, %zu bytes)", sz); memcpy(cl, pf, sz); + if (cl->pins) pin_refs((const uint8_t *)(cl + 1), 1); cl->magic = 0; // not a live handle: the game never sees it cl->used_at = now; zombie_add(cl); diff --git a/runtime/nvn_probe.c b/runtime/nvn_probe.c index 9d1a79f8..8431d61a 100644 --- a/runtime/nvn_probe.c +++ b/runtime/nvn_probe.c @@ -694,6 +694,55 @@ static void parse_all(uint64_t h) { n_ops = OP_COUNT_ - OP_SIGNAL_EVENT; } +// A recording naming tex3 as its render target; *pin: the texture pin in its SET_RT payload. +static uint64_t record_target(uint8_t *ctl, const tex_t **pin) { + const NVNtexture *colors[1] = {&tex3}; + begin(&cmd2, ctl, 0x3a000); + CALL(nvnCommandBufferSetRenderTargets, U(&cmd2), 1, U(colors), 0, 0, 0); + uint64_t h = CALL(nvnCommandBufferEndRecording, U(&cmd2)); + *pin = NULL; + for (const op_hdr *o = nvn_op_first(h); o->op != OP_END; o = nvn_op_next(o)) + if (o->op == OP_SET_RT) *pin = ((const p_set_rt *)nvn_op_payload(o))->color[0]; + return h; +} + +// Texture pins are freed NVN_PIN_AGE submits after the last recording copy naming them is released, unless the replay +// keeps them as its current render targets; a texture named again gets a new pin. +static void probe_pins(void) { + uint8_t *ctl = mem_kernel_alloc(0x1000, 16); + set_tex(NVN_FORMAT_RGBA8, 16, 16, 1, 0x20000); + CHECK((uint8_t)CALL(nvnTextureInitialize, U(&tex3), U(&texb)) == 1, "pin probe texture"); + uint64_t serial = ((const tex_t *)(const void *)&tex3)->serial; + uint64_t live0, freed0, live, freed; + nvn_pins_stats(&live0, &freed0); + const tex_t *pin; + uint64_t h = record_target(ctl, &pin); + nvn_pins_stats(&live, &freed); + CHECK(pin && pin->serial == serial && live == live0 + 1, "a recording naming a new texture makes one pin"); + uint64_t s = nvn_submit_count(); + nvn_pins_sweep(s + 10 * NVN_PIN_AGE, NULL, 0); + nvn_pins_stats(&live, &freed); + CHECK(live == live0 + 1 && freed == freed0 && nvn_handle_live(h), "a pin a live recording names stays"); + CALL(nvnCommandBufferAddControlMemory, U(&cmd2), U(ctl), 0x1000); // retires h: the pin has no copy left + CHECK(!nvn_handle_live(h), "the pin's recording is retired"); + nvn_pins_sweep(s + NVN_PIN_AGE - 1, NULL, 0); + nvn_pins_stats(&live, &freed); + CHECK(live == live0 + 1 && freed == freed0, "a pin stays NVN_PIN_AGE submits after its last copy went"); + nvn_pins_sweep(s + NVN_PIN_AGE, &pin, 1); + nvn_pins_stats(&live, &freed); + CHECK(live == live0 + 1 && freed == freed0, "the replay's current render target is kept"); + nvn_pins_sweep(s + 2 * NVN_PIN_AGE, NULL, 0); + nvn_pins_stats(&live, &freed); + CHECK(live == live0 && freed == freed0 + 1, "an unnamed, unkept pin is freed (live %llu of %llu, freed %llu of %llu)", + (unsigned long long)live, (unsigned long long)live0, (unsigned long long)freed, (unsigned long long)freed0 + 1); + h = record_target(ctl, &pin); + nvn_pins_stats(&live, &freed); + CHECK(pin && pin->serial == serial && live == live0 + 1, "naming the texture again makes a new pin"); + CALL(nvnCommandBufferAddControlMemory, U(&cmd2), U(ctl), 0x1000); + CALL(nvnCommandBufferFinalize, U(&cmd2)); + CALL(nvnTextureFinalize, U(&tex3)); +} + static void probe_commands(void) { ctlmem1 = mem_kernel_alloc(0x1000, 16); ctlmem2 = mem_kernel_alloc(0x1000, 16); @@ -722,6 +771,7 @@ static void probe_commands(void) { CALL(nvnCommandBufferAddControlMemory, U(&cmd2), U(ctlmem2), 0x1000); CHECK(!nvn_handle_live(h2) && nvn_handle_live(h), "reusing control memory retires only the handles recorded in it"); CALL(nvnCommandBufferFinalize, U(&cmd2)); + probe_pins(); n_objects += 5; } diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 2709471d..5b863a16 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -919,6 +919,10 @@ static void walk(VkExec *ex, const op_hdr *h, int depth) { int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t fence) { VkExec *ex = &X; if (!x_ready) state_reset(ex), vk_dump_init(), x_ready = 1; + const tex_t *keep[9]; // the render targets carry over to this submission (set_rt compares them by pointer) + memcpy(keep, ex->rt.color, sizeof ex->rt.color); + keep[8] = ex->rt.depth; + nvn_pins_sweep(nvn_submit_count(), keep, 9); ex->cb = cb; ex->fence = fence; ex->recorded = 0; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 9e61e34a..897eadf5 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -24,7 +24,7 @@ enum { // One render target of OP_SET_RT, resolved to its vk_texture.c image for the current command buffer. typedef struct { - const tex_t *tex; // payload copy (lives for the run); NULL: no target in this slot + const tex_t *tex; // texture pin of ex->rt (nvn_pins_sweep keeps it); NULL: no target in this slot VkImage image; // VK_NULL_HANDLE: no target or not renderable (logged once) VkFormat format; // view format: the view's when it reinterprets a same-size format VkImageViewType view_type; // 2D, or 2D_ARRAY for more than one layer From a248cc9f546775bd16e9793b3aad44448dd0ead0 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 4 Nov 2026 15:33:07 +0300 Subject: [PATCH 2/8] Wait for the pipelines of item icon captures instead of skipping their draws The icon capture draws into a colour target rendered for the first time, with the per-frame depth buffer. Under pipe_skip any and fullscreen the recent depth made the pass look per-frame, so draws with a pipeline still building were dropped and the icon kept missing parts (a fused weapon without its blade) until the item was captured again. vk_exec_targets_recent now ignores a recent depth target while a colour target is rendered for the first time. On fight2 with a warm driver cache no draw waited. With a cold cache the first world frame after loading waits for the scene's new HDR target (2.9 s without prepared shaders), and one 64x64 capture waited 29 ms in the fight. Co-Authored-By: Claude Opus 5.5 --- runtime/vk_exec.c | 16 +++++++++++++--- runtime/vk_exec.h | 5 +++-- runtime/vk_exec_draw.c | 3 ++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 2709471d..ea91731c 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -491,18 +491,28 @@ static int tfr_recent(const VkExecTarget *t, uint64_t stamp) { return tfr_match(e, t) && (e->last == stamp ? e->prev : e->last) == stamp - 1; } +// No frame before the current one rendered the target (a slot another subresource took reads the same). +static int tfr_first(const VkExecTarget *t, uint64_t stamp) { + const TargetFrames *e = tfr_slot(t); + return !tfr_match(e, t) || !(e->last == stamp ? e->prev : e->last); +} + +// With any, a recent depth target alone does not make the pass per-frame when a colour target is rendered for the +// first time: the game's item icon capture (an inventory or Fuse icon) draws into a new colour target with the +// per-frame depth buffer, once. Its draws skipped there stay missing from the icon until the item is captured again. int vk_exec_targets_recent(VkExec *ex, int any) { resolve_rt(ex); uint64_t stamp = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1; - int n = 0, recent = 0; + int n = 0, color = 0, depth = 0, color_first = 0; for (int i = 0; i < 9; i++) { const VkExecTarget *t = i < 8 ? &ex->color[i] : &ex->depth; if (!t->image) continue; n++; - if (tfr_recent(t, stamp)) recent++; + if (tfr_recent(t, stamp)) *(i < 8 ? &color : &depth) = 1; else if (!any) return 0; + else if (i < 8 && tfr_first(t, stamp)) color_first = 1; } - return any ? recent > 0 : n > 0; + return any ? color || (depth && !color_first) : n > 0; } // One line naming each target of the current pass: format, extent, layer and the two frames it was last rendered in diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 9e61e34a..550cedce 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -142,8 +142,9 @@ void vk_hdr_draw(VkExec *ex, const VkPipeInfo *info); // Scissor 0 clipped to the render area of the open pass; extent 0: nothing to draw. VkRect2D vk_exec_scissor(const VkExec *ex); void vk_exec_noatt_dims(const VkExec *ex, uint32_t *w, uint32_t *h); // pass size when no attachment is set -// 1: every target of the current pass (any 1: at least one) was rendered in the previous presented frame (per-frame -// targets); 0 otherwise, or when the pass has none. +// 1: every target of the current pass (any 1: at least one, and not only the depth target while a colour target is +// rendered for the first time) was rendered in the previous presented frame (per-frame targets); 0 otherwise, or when +// the pass has none. int vk_exec_targets_recent(VkExec *ex, int any); // The current pass's targets with their formats, extents and the frames they were last rendered in (diagnostics). void vk_exec_targets_describe(VkExec *ex, char *buf, size_t cap); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 45bd084a..c54be52e 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2017,7 +2017,8 @@ static void bind_vertex_buffers(VkExec *ex) { // that was not counts as one-shot and waits. // any (1): also draws in a pass with at least one target rendered in the previous frame (a per-frame depth // buffer or g-buffer with a colour target the frame before did not use: half-resolution effect buffers in fights). -// Passes with none (volumes, LUTs, shadow layers seen for the first time) still wait. +// Passes with none (volumes, LUTs, shadow layers seen for the first time) still wait, and so do passes whose only +// recent target is the depth buffer while a colour target is rendered for the first time (item icon captures). // fullscreen (2, default): as any, and full-screen draws into such passes are skipped instead of waiting from their // budget. A full-screen pass whose stage libraries are cold in the driver cache took 86 ms on average (up to 194 ms) // in a fight on an RX 9070: three frames in a row each spent the 50 ms budget, and some of the draws were skipped From 3fab752a215021713ec334106c2adae6439354e2 Mon Sep 17 00:00:00 2001 From: itaskaev2 <165683571+itaskaev2@users.noreply.github.com> Date: Wed, 4 Nov 2026 13:35:46 +0100 Subject: [PATCH 3/8] Take out translator patch 0014 so DLSS stops shaking the picture on NVIDIA Patch 0014 gave every function-local variable of a translated shader an OpConstantNull initializer. On an RTX 4080 SUPER with game 1.4.3 that made the whole picture move by about 0.9 output pixels a frame whenever DLSS or DLAA runs, with a still camera (#612). The shake follows the DLAA jitter (TOTK_DLAA_JITTER=0 removes it) and goes away with the v0.3.7 runtime and a translator built without this one patch. Patches 0012, 0013 and 0015 stay. docs/maxwell.md 3.12 now says that registers read before any write have no fix for now; a zero start for Mesa only, or for the shaders that need it, is still open. Co-Authored-By: Claude Opus 5.5 --- docs/maxwell.md | 2 +- tools/build_xlate.py | 6 +++--- .../patches/0014-zero-initialised-locals.patch | 18 ------------------ 3 files changed, 4 insertions(+), 22 deletions(-) delete mode 100644 tools/xlate/patches/0014-zero-initialised-locals.patch diff --git a/docs/maxwell.md b/docs/maxwell.md index a02c4787..e8eba067 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -88,7 +88,7 @@ Source shorthands: | 3.9 | **Integer vertex inputs read as raw bits** (bone indices `R8UI`). | Float-declared inputs convert integers. | NXN Mario: stretched characters; re-translated with `uint` inputs. Ours: attribute types passed to `QueryGraphicsState` (`Program.cs`). | NXN | | 3.10 | **BFE / BFI fields past bit 31** (as PTX `bfe` / `bfi`): a field that runs past bit 31 is cut there; one that starts past it extracts 0 (signed: bit 31 repeated) and inserts nothing. TotK 1.4.3's Depths terrain vertex shaders (42 variants) take the position from a register, `((2y + x) * 8) & 0xff` with 5-bit x and y, so up to 248. | `OpBitField*Extract` / `OpBitFieldInsert` undefined when offset or offset + count exceeds 32; AMD's bitfield instructions take offset and width modulo 32. | Ryujinx passes the fields through. UltraTears saw the Depths walls and ground missing on the Steam Deck and a Linux RX 570 (RADV). Ours: clamped where the field is not a constant inside the word (translator patch 0012). RX 9070 on Windows drew the Depths the same before and after. | PTX ISA `bfe`, `bfi` | | 3.11 | **SHF by 0** gives the unshifted word (C for `SHF.L`, A for `SHF.R`). | Ryujinx's funnel shifts the other word by 32: undefined in SPIR-V; AMD shifts take the low 5 bits and give A \| C. | yuzu fixed Depths terrain gaps in its funnel shift (PR 10459). Ours: a select on a zero amount (translator patch 0013). In 1.4.3 every register SHF pairs its word with 0, so the result was right already. | — | -| 3.12 | **Registers read before any write** on a path (paths the structurizer adds included) have no defined value in the game's code; UltraTears reports 0 from the NVIDIA driver. | An `OpVariable` without an initializer loads an undefined value; Mesa folds undefined values freely. | Ours: every function-local variable starts at 0 (`OpConstantNull`, translator patch 0014). | — | +| 3.12 | **Registers read before any write** on a path (paths the structurizer adds included) have no defined value in the game's code; UltraTears reports 0 from the NVIDIA driver. | An `OpVariable` without an initializer loads an undefined value; Mesa folds undefined values freely. | Ours: none for now. Translator patch 0014 started every function-local variable at 0 (`OpConstantNull`); on NVIDIA (RTX 4080 SUPER, 1.4.3) that made the whole picture shake by about 0.9 output pixels a frame with DLSS or DLAA (#612; no shake with `TOTK_DLAA_JITTER=0` or without the patch), so it was taken out. A zero start for Mesa only, or for the shaders that need it, is still open. | — | ## 4. Memory model diff --git a/tools/build_xlate.py b/tools/build_xlate.py index 210c1acf..c13ed192 100644 --- a/tools/build_xlate.py +++ b/tools/build_xlate.py @@ -1,7 +1,7 @@ """Build the shader translator: build/xlate/totk_xlate.exe (Ryujinx 1.3.2 + patches, NativeAOT, SPIR-V for Vulkan). Copies tools/vendor/ryujinx to build/xlate/ryujinx, applies tools/xlate/patches/*.patch there (tools/vendor stays -byte-identical; 0001-0004 are MNG's, 0005-0014 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build +byte-identical; 0001-0004 are MNG's, 0005-0013 and 0015 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build tools and vswhere.exe (docs/building.md). Run with .venv. On Linux it builds build/xlate/totk_xlate for linux-x64, which NativeAOT links with the system's clang (no MSVC); NativeAOT builds for the OS it runs on only. @@ -99,8 +99,8 @@ def prepare_sources() -> Path: for junk in list(src.glob("src/*/obj")) + list(src.glob("src/*/bin")): shutil.rmtree(junk) patches = sorted((XLATE / "patches").glob("*.patch")) - if len(patches) != 15: - sys.exit(f"expected 15 patches, found {len(patches)}") + if len(patches) != 14: + sys.exit(f"expected 14 patches, found {len(patches)}") for patch in patches: # git apply works outside a repository when given a directory; --check first so a failure names the patch. for extra in (["--check"], []): diff --git a/tools/xlate/patches/0014-zero-initialised-locals.patch b/tools/xlate/patches/0014-zero-initialised-locals.patch deleted file mode 100644 index 3ffc7a9b..00000000 --- a/tools/xlate/patches/0014-zero-initialised-locals.patch +++ /dev/null @@ -1,18 +0,0 @@ ---- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs -+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs -@@ -33,8 +33,13 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv - { - foreach (AstOperand local in function.Locals) - { -- SpvInstruction localPointerType = context.TypePointer(StorageClass.Function, context.GetType(local.VarType)); -- SpvInstruction spvLocal = context.Variable(localPointerType, StorageClass.Function); -+ SpvInstruction localType = context.GetType(local.VarType); -+ SpvInstruction localPointerType = context.TypePointer(StorageClass.Function, localType); -+ -+ // TOTK: a register read on a path that did not write it reads 0. Without an initializer the load is -+ // undefined, and a compiler that folds undefined values (Mesa's, on the Steam Deck) may compute anything -+ // from it. -+ SpvInstruction spvLocal = context.Variable(localPointerType, StorageClass.Function, context.ConstantNull(localType)); - - context.AddLocalVariable(spvLocal); - context.DeclareLocal(local, spvLocal); From 1a9d089a3d7cf05ed1beed7135c64027c0cba335 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 4 Nov 2026 16:49:46 +0300 Subject: [PATCH 4/8] Write grass maps back at fence waits so fire spreads through grass, and batch the copies TotK's fire spread reads its 260x260 RG8 grass maps after a fence wait with no invalidate. The fence write-back took targets up to 64 KiB only, so the CPU saw stale maps and fire died where it hit (#132). A target over wb_fence_limit now goes back unless the GPU drew it in each of the last 16 frames. Raising the limit instead pulled in four 256x256 targets drawn every frame, about 2.7 ms a frame. Every level and layer of every texture in a write-back now shares one upload batch and one wait. A burst of 9 grass maps took 7.8 ms with one round trip per level and takes 2 to 2.5 ms. The fence write-back on the field replay fell from about 45 to 22 ms per second of play. Co-Authored-By: Claude Opus 5.5 --- runtime/vk_texture.c | 270 ++++++++++++++++++++++++++++--------------- totk.ini.example | 9 +- 2 files changed, 182 insertions(+), 97 deletions(-) diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 0a63a3bb..bd0c5be8 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -53,6 +53,8 @@ typedef struct Tex { uint32_t gpu_gen; // page generation when the GPU write that set gpu_dirty was recorded uint64_t gpu_seq; // order of that GPU write among all of them (gpu_writes) uint64_t gpu_fence; // fence of the submission that made that write (0: not recorded in one) + uint64_t run_frame; // mirrored: the frame of the newest GPU write + uint8_t run; // frames in a row up to run_frame with a GPU write, counted up to WB_RUN uint64_t wr_fence; // any entry: newest submission recorded to write its image (render, copy, clear, // storage binding); ReadTexels waits for it instead of draining the replay queue uint8_t listed; // in owned[] @@ -995,10 +997,11 @@ const VkTexImage *vk_texture_get(const tex_t *t) { return e ? &e->pub : NULL; } -// Small render targets the CPU reads without NVN's invalidate (vk_texture_write_back_small); small_pending: one may be -// dirty. wb_limit is the largest target (bytes) that goes back when the CPU waits for a fence: TOTK_WB_FENCE_LIMIT, -// read once. wb_trace: TOTK_TRACE=gfx, read together with it. -enum { WB_SMALL_DEFAULT = 64 << 10 }; +// Render targets the CPU reads without NVN's invalidate (vk_texture_write_back_small); small_pending: one may be dirty. +// wb_limit: targets up to this size (bytes) always go back when the CPU waits for a fence, TOTK_WB_FENCE_LIMIT, read +// once. A larger one goes back unless the GPU wrote it in each of the last WB_RUN frames (wb_wanted). wb_trace: +// TOTK_TRACE=gfx, read together with it. +enum { WB_SMALL_DEFAULT = 64 << 10, WB_RUN = 16 }; static int small_pending; static uint64_t wb_limit = WB_SMALL_DEFAULT; static int wb_trace; @@ -1013,6 +1016,12 @@ static void wb_cfg_init(void) { if (wb_limit != WB_SMALL_DEFAULT) rt_log("vk: fence write-back limit %llu bytes", (unsigned long long)wb_limit); } +// Whether the fence write-back takes a dirty target. A target the GPU draws every frame is scratch the CPU does not +// read: in Hyrule Field three 256x256 RGBA8 targets and a 256x256 RG8 one, which cost 2.7 ms a frame to write back. +// TotK's 260x260 RG8 grass maps, which its fire spread reads after a fence wait (#132), are drawn once per change, or +// 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; } + void vk_texture_gpu_written(const tex_t *t) { if (!t || !t->mem) return; vk_exec_frame_written(t); @@ -1032,8 +1041,13 @@ void vk_texture_gpu_written(const tex_t *t) { if (e->snap) memcpy(e->snap, (const void *)(uintptr_t)e->pub.addr, e->t.L.size); e->gpu_seq = ++gpu_writes; e->gpu_fence = vk_exec_recording_fence(); + uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (fr != e->run_frame || !e->run) { + e->run = fr == e->run_frame + 1 && e->run ? (uint8_t)(e->run < WB_RUN ? e->run + 1 : WB_RUN) : 1; + e->run_frame = fr; + } wb_cfg_init(); - if (e->t.L.size <= wb_limit) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE); + if (wb_wanted(e)) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE); own_add(e); } } @@ -1275,12 +1289,21 @@ static int read_aspect(const Tex *e, VkImageAspectFlags *a) { } } -// Image <-> caller rows through a staging buffer; the GPU copy is waited for. -static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t *mem, int write) { +// An image region read through a staging buffer. texels_read_record records the copy into the open upload batch; +// texels_read_take copies the rows out once that batch is done (vk_upload_flush(1)) and frees the buffer. Several +// records before one flush cost one GPU round trip instead of one each. +typedef struct { + VkBuffer sb; + VmaAllocation sa; + const uint8_t *stg; + size_t slice, ib; // ib: bytes per texel of an expanded RGB image, 0 when the image holds the storage layout + uint32_t w; +} TexRead; + +static int texels_read_record(Tex *e, const Region *g, const NVNcopyRegion *r, TexRead *t) { static volatile int32_t n_astc, n_ds; - VkCtx *c = vk_ctx(); VkImageAspectFlags aspect; - if (e->f.astc_bw && !write) { + if (e->f.astc_bw) { log_limited(&n_astc, "ReadTexels of ASTC texture %#llx: the image holds decoded RGBA8; zeros returned", (unsigned long long)e->pub.addr); return 0; @@ -1291,7 +1314,60 @@ static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t } if (g->level >= e->pub.levels) return 0; uint32_t w = (uint32_t)r->width, h = (uint32_t)r->height; - int rgb = e->f.expand_rgb, astc = e->f.astc_bw != 0, agpu = astc && write && vk_astc_gpu(); + t->w = w; + t->ib = e->f.expand_rgb ? (size_t)g->bpb / 3 * 4 : 0; + t->slice = t->ib ? (size_t)w * h * t->ib : g->wb * g->hb; + t->stg = vk_staging(t->slice * (size_t)g->nz, &t->sb, &t->sa); + VkBufferImageCopy *rg = calloc((size_t)g->nz, sizeof *rg); + if (!rg) rt_fatal("vk: out of memory for a texture read"); + for (int z = 0; z < g->nz; z++) + rg[z] = (VkBufferImageCopy){.bufferOffset = t->slice * (size_t)z, + .imageSubresource = {aspect, g->level, g->is3d ? 0 : g->layer0 + (uint32_t)r->zoffset + (uint32_t)z, 1}, + .imageOffset = {r->xoffset, r->yoffset, g->is3d ? r->zoffset + z : 0}, + .imageExtent = {w, h, 1}}; + VkCommandBuffer cb = vk_upload_begin(); + barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT); + vkCmdCopyImageToBuffer(cb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, t->sb, (uint32_t)g->nz, rg); + barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_HOST_BIT, + VK_ACCESS_2_HOST_READ_BIT); + vk_upload_end(); + free(rg); + return 1; +} + +static void texels_read_take(const Tex *e, const Region *g, const TexRead *t, uint8_t *mem) { + VkCtx *c = vk_ctx(); + vmaInvalidateAllocation(c->vma, t->sa, 0, VK_WHOLE_SIZE); + for (int z = 0; z < g->nz; z++) { + const uint8_t *s = t->stg + t->slice * (size_t)z; + uint8_t *m = mem + (size_t)z * g->img; + for (size_t y = 0; y < g->hb; y++) { + if (t->ib) shrink_rgb(e->t.d.format, s + y * t->w * t->ib, m + y * g->row, t->w); + else memcpy(m + y * g->row, s + y * g->wb, g->wb); + } + } + vmaDestroyBuffer(c->vma, t->sb, t->sa); +} + +// Image <-> caller rows through a staging buffer; the GPU copy is waited for. +static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t *mem, int write) { + static volatile int32_t n_astc, n_ds; + VkCtx *c = vk_ctx(); + VkImageAspectFlags aspect; + if (!write) { + TexRead t; + if (!texels_read_record(e, g, r, &t)) return 0; + vk_upload_flush(1); + texels_read_take(e, g, &t, mem); + return 1; + } + if (!read_aspect(e, &aspect)) { + log_limited(&n_ds, "texels of packed depth-stencil format %#x are not copied", e->t.d.format); + return 0; + } + if (g->level >= e->pub.levels) return 0; + uint32_t w = (uint32_t)r->width, h = (uint32_t)r->height; + int rgb = e->f.expand_rgb, astc = e->f.astc_bw != 0, agpu = astc && vk_astc_gpu(); size_t texels = (size_t)w * h, ib = agpu ? 0 : astc ? 4 : rgb ? (size_t)g->bpb / 3 * 4 : 0; size_t slice = ib ? texels * ib : g->wb * g->hb; // agpu: the raw blocks, decoded by vk_astc_record VkBuffer sb; @@ -1301,21 +1377,19 @@ static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t for (int z = 0; z < g->nz; z++) { uint8_t *s = stg + slice * (size_t)z; const uint8_t *m = mem + (size_t)z * g->img; - if (write) { - if (agpu) { - for (size_t y = 0; y < g->hb; y++) memcpy(s + y * g->wb, m + y * g->row, g->wb); - } else if (astc) { - uint8_t *tight = malloc(g->wb * g->hb); - for (size_t y = 0; y < g->hb; y++) memcpy(tight + y * g->wb, m + y * g->row, g->wb); - uint64_t a0 = perf_begin(); - astc_decode_ldr_mt(tight, w, h, e->f.astc_bw, e->f.astc_bh, e->f.astc_srgb, s); - perf_end(PERF_ASTC, a0); - free(tight); - } else if (rgb) { - for (size_t y = 0; y < g->hb; y++) expand_rgb(e->t.d.format, m + y * g->row, s + y * w * ib, w); - } else { - for (size_t y = 0; y < g->hb; y++) memcpy(s + y * g->wb, m + y * g->row, g->wb); - } + if (agpu) { + for (size_t y = 0; y < g->hb; y++) memcpy(s + y * g->wb, m + y * g->row, g->wb); + } else if (astc) { + uint8_t *tight = malloc(g->wb * g->hb); + for (size_t y = 0; y < g->hb; y++) memcpy(tight + y * g->wb, m + y * g->row, g->wb); + uint64_t a0 = perf_begin(); + astc_decode_ldr_mt(tight, w, h, e->f.astc_bw, e->f.astc_bh, e->f.astc_srgb, s); + perf_end(PERF_ASTC, a0); + free(tight); + } else if (rgb) { + for (size_t y = 0; y < g->hb; y++) expand_rgb(e->t.d.format, m + y * g->row, s + y * w * ib, w); + } else { + for (size_t y = 0; y < g->hb; y++) memcpy(s + y * g->wb, m + y * g->row, g->wb); } rg[z] = (VkBufferImageCopy){.bufferOffset = slice * (size_t)z, .imageSubresource = {aspect, g->level, g->is3d ? 0 : g->layer0 + (uint32_t)r->zoffset + (uint32_t)z, 1}, @@ -1323,49 +1397,31 @@ static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t .imageExtent = {w, h, 1}}; } VkCommandBuffer cb = vk_upload_begin(); - if (write) { - vmaFlushAllocation(c->vma, sa, 0, VK_WHOLE_SIZE); - barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); - int done = 0; - // A BC3 image takes whole 4x4 blocks, or a part that ends on the mip's edge; a region of a 5x5 or 6x6 footprint - // may end inside a block (vk_astc_record checks the start corner). - uint32_t mw = e->t.d.width >> g->level, mh = e->t.d.height >> g->level; - int bc_cut = e->f.astc_bc && (((w & 3) && (uint32_t)r->xoffset + w != (mw ? mw : 1)) || - ((h & 3) && (uint32_t)r->yoffset + h != (mh ? mh : 1))); - if (agpu && !bc_cut) { // bc_cut: nothing recorded, logged below as not decoded - VkAstcRegion *ar = calloc((size_t)g->nz, sizeof *ar); - for (int z = 0; z < g->nz; z++) - ar[z] = (VkAstcRegion){.src = slice * (size_t)z, .w = w, .h = h, .slices = 1, .level = rg[z].imageSubresource.mipLevel, - .layer = rg[z].imageSubresource.baseArrayLayer, .offset = rg[z].imageOffset}; - uint64_t a0 = perf_begin(); - done = vk_astc_record(cb, sb, slice * (size_t)g->nz, ar, g->nz, e->f.astc_bw, e->f.astc_bh, e->f.astc_srgb, e->f.astc_bc, - e->pub.image, aspect, VK_NULL_HANDLE); - perf_end(PERF_ASTC, a0); - free(ar); - } - if (!agpu) vkCmdCopyBufferToImage(cb, sb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, (uint32_t)g->nz, rg); - else if (!done) log_limited(&n_astc, "WriteTexels of ASTC texture %#llx not decoded on the GPU: left as it was", - (unsigned long long)e->pub.addr); - barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, ALL_RW); - } else { - barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT); - vkCmdCopyImageToBuffer(cb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, sb, (uint32_t)g->nz, rg); - barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_HOST_BIT, - VK_ACCESS_2_HOST_READ_BIT); + vmaFlushAllocation(c->vma, sa, 0, VK_WHOLE_SIZE); + barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); + int done = 0; + // A BC3 image takes whole 4x4 blocks, or a part that ends on the mip's edge; a region of a 5x5 or 6x6 footprint + // may end inside a block (vk_astc_record checks the start corner). + uint32_t mw = e->t.d.width >> g->level, mh = e->t.d.height >> g->level; + int bc_cut = e->f.astc_bc && (((w & 3) && (uint32_t)r->xoffset + w != (mw ? mw : 1)) || + ((h & 3) && (uint32_t)r->yoffset + h != (mh ? mh : 1))); + if (agpu && !bc_cut) { // bc_cut: nothing recorded, logged below as not decoded + VkAstcRegion *ar = calloc((size_t)g->nz, sizeof *ar); + for (int z = 0; z < g->nz; z++) + ar[z] = (VkAstcRegion){.src = slice * (size_t)z, .w = w, .h = h, .slices = 1, .level = rg[z].imageSubresource.mipLevel, + .layer = rg[z].imageSubresource.baseArrayLayer, .offset = rg[z].imageOffset}; + uint64_t a0 = perf_begin(); + done = vk_astc_record(cb, sb, slice * (size_t)g->nz, ar, g->nz, e->f.astc_bw, e->f.astc_bh, e->f.astc_srgb, e->f.astc_bc, + e->pub.image, aspect, VK_NULL_HANDLE); + perf_end(PERF_ASTC, a0); + free(ar); } + if (!agpu) vkCmdCopyBufferToImage(cb, sb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, (uint32_t)g->nz, rg); + else if (!done) log_limited(&n_astc, "WriteTexels of ASTC texture %#llx not decoded on the GPU: left as it was", + (unsigned long long)e->pub.addr); + barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, ALL_RW); vk_upload_end(); vk_upload_flush(1); - if (!write) { - vmaInvalidateAllocation(c->vma, sa, 0, VK_WHOLE_SIZE); - for (int z = 0; z < g->nz; z++) { - const uint8_t *s = stg + slice * (size_t)z; - uint8_t *m = mem + (size_t)z * g->img; - for (size_t y = 0; y < g->hb; y++) { - if (rgb) shrink_rgb(e->t.d.format, s + y * w * ib, m + y * g->row, w); - else memcpy(m + y * g->row, s + y * g->wb, g->wb); - } - } - } free(rg); vmaDestroyBuffer(c->vma, sb, sa); return 1; @@ -1441,24 +1497,51 @@ void vk_write_texels(const tex_t *t, const view_t *v, const NVNcopyRegion *r, co // A mirrored texture the GPU rendered into holds its texels in its image only; its storage keeps what the CPU last put // there. The game may still read that storage with the CPU: TotK moves its generated grass maps between pools with // memcpy once the GPU is done, and the copy then carried zeros. Such a read writes the image back first. -static void write_back(Tex *e) { - const nvn_texdesc *d = &e->t.d; - int linear = nvn_tex_is_linear(d), is3d = d->target == NVN_TEXTURE_TARGET_3D; - uint32_t levels = linear ? 1 : e->pub.levels, layers = linear || is3d ? 1 : e->pub.layers; - for (uint32_t lv = 0; lv < levels; lv++) - for (uint32_t ly = 0; ly < layers; ly++) { - view_t v = {.set_levels = 1, .set_layers = 1, .base_level = (uint8_t)lv, .min_layer = (uint16_t)ly}; - NVNcopyRegion r = {.width = (int)mip(d->width, lv), - .height = d->target == 0 || d->target == 3 ? 1 : (int)mip(d->height, lv), - .depth = is3d ? (int)mip(d->depth, lv) : 1}; - Region g; - if (!region_of(&e->t, &v, &r, 0, 0, &g)) continue; - uint8_t *buf = malloc(g.img * (size_t)g.nz); - if (!buf) rt_fatal("vk: out of memory for a texture write-back"); - if (image_texels(e, &g, &r, buf, 0)) storage_texels(&e->t, &g, &r, buf, 1); - free(buf); - } - e->gpu_dirty = 0; +// +// Every level and layer of every texture in v is copied to staging in one upload batch with one wait: one GPU round +// trip per level and layer took 0.1 ms each, and a 260x260 grass map has 9 levels. The storage is then written in the +// order of v, so where two textures share storage the later one wins. +typedef struct { + Tex *e; + Region g; + NVNcopyRegion r; + TexRead t; +} WbPart; + +static void write_back_n(Tex **v, int n) { + int np = 0, cap = 0; + WbPart *p = NULL; + for (int i = 0; i < n; i++) { + Tex *e = v[i]; + const nvn_texdesc *d = &e->t.d; + int linear = nvn_tex_is_linear(d), is3d = d->target == NVN_TEXTURE_TARGET_3D; + uint32_t levels = linear ? 1 : e->pub.levels, layers = linear || is3d ? 1 : e->pub.layers; + for (uint32_t lv = 0; lv < levels; lv++) + for (uint32_t ly = 0; ly < layers; ly++) { + if (np == cap) { + cap = cap ? cap * 2 : 16; + if (!(p = realloc(p, sizeof *p * (size_t)cap))) rt_fatal("vk: out of memory for a texture write-back"); + } + WbPart *q = &p[np]; + view_t vw = {.set_levels = 1, .set_layers = 1, .base_level = (uint8_t)lv, .min_layer = (uint16_t)ly}; + q->e = e; + q->r = (NVNcopyRegion){.width = (int)mip(d->width, lv), + .height = d->target == 0 || d->target == 3 ? 1 : (int)mip(d->height, lv), + .depth = is3d ? (int)mip(d->depth, lv) : 1}; + if (region_of(&e->t, &vw, &q->r, 0, 0, &q->g) && texels_read_record(e, &q->g, &q->r, &q->t)) np++; + } + } + if (np) vk_upload_flush(1); + for (int i = 0; i < np; i++) { + WbPart *q = &p[i]; + uint8_t *buf = malloc(q->g.img * (size_t)q->g.nz); + if (!buf) rt_fatal("vk: out of memory for a texture write-back"); + texels_read_take(q->e, &q->g, &q->t, buf); + storage_texels(&q->e->t, &q->g, &q->r, buf, 1); + free(buf); + } + free(p); + for (int i = 0; i < n; i++) v[i]->gpu_dirty = 0; } // Mirrored textures overlapping [addr, addr+size) that the GPU wrote since their last write-back. @@ -1539,7 +1622,7 @@ void vk_texture_write_back(uint64_t addr, uint64_t size) { Tex *x = hit[j]; hit[j] = hit[j - 1], hit[j - 1] = x; } - for (int i = 0; i < k; i++) write_back(hit[i]); + write_back_n(hit, k); for (int i = 0; i < k; i++) perf_count(PERF_CNT_WB); for (int i = k; i < n; i++) perf_count(PERF_CNT_WB_SKIP); free(hit); @@ -1549,13 +1632,14 @@ void vk_texture_write_back(uint64_t addr, uint64_t size) { // TotK's grass system renders a 128x128 RGBA8 map of cut and trampled grass into a CPU-cached pool every frame and its // CPU code reads the pool bytes without nvnMemoryPoolInvalidateMappedRange (cut reactions: blades, insects, the cut // itself). On the Switch the bytes are there once the frame's fence has passed. So when the -// CPU waits for a fence, mirrored targets up to wb_limit bytes rendered by work up to that fence go back to their -// storage, unless the CPU rewrote it since. Larger targets still go back on an invalidate, memcpy or ReadTexels. +// CPU waits for a fence, mirrored targets rendered by work up to that fence go back to their storage (wb_wanted: +// targets over wb_limit only when not drawn every frame), unless the CPU rewrote it since. Targets drawn every frame +// still go back on an invalidate, memcpy or ReadTexels. int vk_texture_small_dirty(void) { return __atomic_load_n(&small_pending, __ATOMIC_ACQUIRE); } -// TOTK_TRACE=gfx: a dirty target the fence write-back leaves alone because it is over the limit (what a CPU read -// after the wait would miss), or sends back although it is over the default limit (what a raised limit changed). One -// line per target address and outcome; the table is small and a full one stops logging. +// TOTK_TRACE=gfx: a dirty target the fence write-back leaves alone because it is over the limit and drawn every frame +// (what a CPU read after the wait would miss), or sends back although it is over the default limit. One line per +// target address and outcome; the table is small and a full one stops logging. static void wb_note(const Tex *e, int skipped) { static uint64_t seen[2][64]; static int n_seen[2]; @@ -1566,7 +1650,7 @@ static void wb_note(const Tex *e, int skipped) { const pool_t *p = e->t.pool; unsigned fl = p ? p->flags : 0; rt_log("vk: fence write-back %s %#llx %ux%u fmt %#x size %llu pool %s (limit %llu)", - skipped ? "skipped-for-size" : "over-default", (unsigned long long)e->pub.addr, e->t.d.width, e->t.d.height, + skipped ? "skipped-drawn-every-frame" : "over-default", (unsigned long long)e->pub.addr, e->t.d.width, e->t.d.height, e->t.d.format, (unsigned long long)e->t.L.size, !p ? "none" : (fl & NVN_MEMORY_POOL_FLAGS_CPU_CACHED) ? "cpu-cached" : (fl & NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED) ? "cpu-uncached" : "other", (unsigned long long)wb_limit); @@ -1578,14 +1662,14 @@ void vk_texture_write_back_small(uint64_t fence) { __atomic_store_n(&small_pending, 0, __ATOMIC_RELAXED); plat_mutex_lock_shared(&owned_lock); int n = 0, later = 0; - for (int i = 0; i < nowned; i++) n += owned[i]->gpu_dirty && owned[i]->t.L.size <= wb_limit; + for (int i = 0; i < nowned; i++) n += owned[i]->gpu_dirty && wb_wanted(owned[i]); Tex **hit = n ? malloc(sizeof *hit * (size_t)n) : NULL; if (n && !hit) rt_fatal("vk: out of memory for a texture write-back"); n = 0; for (int i = 0; i < nowned; i++) { Tex *e = owned[i]; if (!e->gpu_dirty) continue; - if (e->t.L.size > wb_limit) { + if (!wb_wanted(e)) { if (wb_trace && e->gpu_fence <= fence) { wb_note(e, 1); perf_count(PERF_CNT_WB_SKIP); @@ -1611,7 +1695,7 @@ void vk_texture_write_back_small(uint64_t fence) { Tex *x = hit[j]; hit[j] = hit[j - 1], hit[j - 1] = x; } - for (int i = 0; i < k; i++) write_back(hit[i]); + write_back_n(hit, k); for (int i = 0; i < k; i++) perf_count(PERF_CNT_WB); for (int i = k; i < n; i++) perf_count(PERF_CNT_WB_SKIP); free(hit); diff --git a/totk.ini.example b/totk.ini.example index f5c28b7e..0053aa84 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -367,8 +367,9 @@ # last one it finished. Unset: on AMD GPUs (VK_AMD_buffer_marker). 1: also on NVIDIA GPUs # (VK_NV_device_diagnostic_checkpoints), which NVIDIA says costs a little. 0: off. # pass_crumbs = 1 -# Largest render target (bytes, 0 to 1073741824) that goes back to its game memory when the game waits for a GPU fence. -# A game that reads a larger target without invalidating it sees stale bytes at the default. With trace = gfx the log -# names each dirty target over the limit that stays on the GPU ("fence write-back skipped-for-size") and each one over -# 65536 bytes that went back ("over-default"). Raising it costs a copy per such target and wait. +# Render targets up to this size (bytes, 0 to 1073741824) go back to their game memory whenever the game waits for a +# GPU fence. A larger target goes back too unless the GPU drew it in each of the last 16 frames. A game that reads a +# larger target drawn every frame without invalidating it sees stale bytes at the default. With trace = gfx the log +# names each dirty target that stays on the GPU ("fence write-back skipped-drawn-every-frame") and each one over 65536 +# bytes that went back ("over-default"). Raising it costs a copy per such target and wait. # wb_fence_limit = 65536 From 0e1ddb41e0420f0a104e9dde9ec141a68e784538 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 4 Nov 2026 17:48:48 +0300 Subject: [PATCH 5/8] Scale the 1.2.1 map cursor and vertical aim steps by the frame ratio The map cursor, the vertical Ultrahand aim and the vertical gyro aim moved a fixed step per frame, so above 30 fps they ran faster with the frame rate (#44, #394). The new set cursor multiplies each step by the game's own 30 Hz ratio, ported from MacNativeGames 363a6eb and switched by TOTK_HIGHFPS_FIX. Map pan with the same stick input on 1.2.1: 398 px/s at 30 fps, 1150 px/s at about 105 fps before, 402 px/s after. The Ultrahand and gyro sites were not exercised in game. 1.1.0 is not covered: its module is not available here to check the sites. Co-Authored-By: Claude Opus 5.5 --- THIRD_PARTY_NOTICES.md | 1 + recomp/patches-v121.txt | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 8b5e93b7..289e75a6 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -10,6 +10,7 @@ Code copied or adapted from another project is listed here with its source, the | MacNativeGames TotK host runtime: dynamic linking, identity check, module permissions, guest memory and SVCs, threads, IPC, audio renderer, system services, RomFS and save storage, boot sequence, the NVN proc name list | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/runtime/`, `games/totk/scripts/prepare_runtime.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `runtime/loader.c`, `runtime/memory.c`; `runtime/kernel.c`, `runtime/svc.c`, `runtime/ipc.c`, `runtime/ipc.h`, `runtime/audren.c`, `runtime/audren.h`, `runtime/audio_mixer.c`, `runtime/audio_effects.c`, `runtime/audio_effects.h`, `runtime/audio_reverb.c`, `runtime/audio_reverb.h` (SDL3 output replaces CoreAudio), `runtime/ov_system.c` (also `shared_font.c`: the BFTTF layout behind nn::pl), `runtime/ov_input.c` (also `hid_feedback.c`: vibration and six-axis handles and calls), `runtime/motion_fusion.c` (six-axis fusion of `motion_fusion.c`), `runtime/ov_media.c` (hardware Opus decoder over libopus), `runtime/swkbd_win.c` (`swkbd_host.c`: ShowKeyboardArg offsets, UTF-8/UTF-16 conversion, disabled-key filter, result codes; Win32 window replaces the macOS text bar), `runtime/ov_gfx.c`, `runtime/romfs.c`, `runtime/romfs_path.h`, `runtime/fs_host.c`, `runtime/fs.h`, `runtime/boot.c`; `runtime/gfx_window.c`; `tools/gen_nvn_procs.py`, `runtime/nvn_procs.inc` (`NVN/names.txt` and the `NVN_EXTRA` names of `totk_nvn_ext.c`) | | MacNativeGames TotK frame-rate patches: 60 fps present-interval controller, real-time frame step and its present sampling, hair and cloth clock, trampoline slow-motion check (instruction words and 1.2.1 sites), player position chain; quick-menu capture background RomFS overlay | https://github.com/ly05010419/MacNativeGames @ `ebe621de99bfc48f3c42f2d2aa73e04fbcc6eae2` (`games/totk/runtime/game_patches.c`, `game_version.h`, `world_survey.c`, `games/totk/scripts/make_romfs_patches.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `runtime/frame_rate.c`, `recomp/patches-v121.txt`, `tools/make_romfs_patches.py` | | MacNativeGames TotK 1.4.3 fade fix: the four distance / LOD and pre-actor fade sites, their instruction words and the floor of the fade step at 1 | https://github.com/ly05010419/MacNativeGames @ `3e9e7f67fdfdd3404134dcfa0f24969a9425a33f` (`games/totk/runtime/game_patches_v143.c` patch_fade_increment), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v143.txt` (set fade; the floor without flags, clz >> 5, as in pull request #78) | +| MacNativeGames TotK 1.2.1 cursor and aim steps: the map cursor, vertical Ultrahand aim and vertical gyro aim sites, their instruction words and the step scaled by the frame ratio | https://github.com/ly05010419/MacNativeGames @ `363a6eb50434387f7d623349934bcf1e910be48f` (`games/totk/runtime/game_patches.c` patch_cursor_speed), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v121.txt` (set cursor) | | MacNativeGames TotK aspect ratio: the camera and viewport sites that hand the perspective projection its aspect ratio, and the UI layout edits for a canvas wider than 16:9 | https://github.com/ly05010419/MacNativeGames @ `933b6a0d1438de160208a0a13eb5e8d7a2cb8094` (`games/totk/runtime/aspect_patch.h`, `aspect_math.h`, `game_patches_v143.c`, `games/totk/scripts/ultrawide_layout.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v143.txt` (set aspect), `tools/ultrawide_layout.py`, `tools/make_romfs_patches.py` (--aspect) | | MacNativeGames TotK button icons: the Xbox controller's shoulder buttons, triggers, Menu, View and d-pad drawn with letters from the game's own text fonts, the .bfotf key, the button guide's images and their block-linear layout, the resource size table raised for the archives that grew | https://github.com/ly05010419/MacNativeGames @ `933b6a0d1438de160208a0a13eb5e8d7a2cb8094` (`games/totk/scripts/make_button_icons.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `tools/button_icons.py`, `tools/make_romfs_patches.py` (--prompts) | | RenoDX TotK mod (game mod by souperman9, tonemapper by Musa, RenoDX framework by ShortFuse, RenoVK fork by Ritsu): the approach of the HDR tonemap (the game's 1 - e^-x curve kept below a pivot and continued along its tangent above it, the saturation grade applied to the colour scaled into 0..1) and the observation that the passes after the tonemap keep values above 1. No code copied; the shader is our own | https://github.com/clshortfuse/renodx @ `b6f87272ad1bfdc0e6444910a93606815944a085` (`src/games/totk/tonemap.slang`, `addon.cpp`), Copyright (c) 2025 Carlos Lopez Jr. | MIT | `runtime/shaders/hdr_tonemap.frag`, `runtime/vk_hdr.c` | diff --git a/recomp/patches-v121.txt b/recomp/patches-v121.txt index 2a5d1257..5d35c7b9 100644 --- a/recomp/patches-v121.txt +++ b/recomp/patches-v121.txt @@ -55,6 +55,16 @@ site cloth main 8222f0 9401d7cf @page:x8 bd400104 1e240842 1e240863 9401d7cf # # 100; compare +0x28 * +0x38 (30 / time scale) instead, the same value at 30 Hz and independent of the frame rate. site bounce main bda798 bd402900 bd402900 bd403901 1e210800 # ldr s0, [x8, #0x28]; ldr s1, [x8, #0x38]; fmul s0, s0, s1 +# Cursor and aim steps (upstream game_patches.c patch_cursor_speed, 363a6eb). These updates read the game's 30 Hz frame +# ratio for their smoothing but leave it out of the step itself, so the map cursor and the vertical Ultrahand aim moved +# 2x as fast at 60 fps and 4x at 120 as at the shipping 30 fps (issue #44). Each site multiplies the step by that ratio, +# so the speed is the shipping one at any frame rate. x9 and s16 are free: the next read of x9 follows a write, the +# function does not touch s16, and x20 keeps the update's frame object for the whole function. +set cursor TOTK_HIGHFPS_FIX 1 +site cursor main ec3d9c bd400361 f94002e9 bd400930 1e300800 bd400361 # map pan speed s0 *= [[x23] + 8], then ldr s1, [x27] +site cursor main 1d48d64 1e2a198c 1e2a198c bd400a90 1e30098c # fdiv s12, s12, s10, then s12 *= [x20 + 8]: vertical Ultrahand aim +site cursor main 1d484e4 1e200923 1e200923 bd400a90 1e300863 # fmul s3, s9, s0, then s3 *= [x20 + 8]: vertical gyro aim (Recall) + # Output size (upstream games/totk/runtime/game_patches.c patch_output_res; runtime/resolution.c switches the set on). # The swapchain size nnMain writes before NvInitializeGraphics comes from page +48 ({w, h} as one 64-bit value), and # the output size the renderer lays out its canvas, crop and UI in (main+0xcc3f00, read from the graphics parameters From 9dd50131ca2da11b50074c4d3f84e39e6b5c8597 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 4 Nov 2026 17:58:24 +0300 Subject: [PATCH 6/8] Start function-local variables at 0 on Mesa drivers only SPIR-V leaves a register read before any write undefined and Mesa folds undefined values freely. The unconditional patch 0014 guarded against that and was taken out in #701 because the whole picture shook with DLSS on NVIDIA. Patch 0016 brings the zero start back behind a zero_locals job flag, which vk_caps_driver turns on for RADV, ANV and NVK. TOTK_ZERO_LOCALS overrides it, and gfx_shader_set_host mixes it into the translator identity so the variant has its own cache keys. FXAA_FS gains the flag-on hash of the game's FXAA shader, so vk_dlaa_skip_draw knows it on Mesa too. A game run with TOTK_ZERO_LOCALS=1 on an RX 9070 produced that hash; a TOTK_FXAA=0 run logged no skip with either hash, so the switch itself is not confirmed. All 29736 stored 1.4.3 inputs translate with the flag off and on, 0 spirv-val failures. FXAA hashes to 5c43ae18948d52cb with the flag off and aad42238801d31d8 with it on. Flag-on output matches the old translator byte for byte on a sample of 312 shaders. Not run on a Mesa driver. Co-Authored-By: Claude Opus 5.5 --- docs/maxwell.md | 2 +- runtime/gfx.h | 2 +- runtime/gfx_shader.c | 14 ++-- runtime/vk.h | 3 +- runtime/vk_caps.c | 7 +- runtime/vk_dlaa.c | 8 ++- tools/build_xlate.py | 6 +- tools/xlate/Program.cs | 4 +- .../patches/0016-zero-locals-job-flag.patch | 71 +++++++++++++++++++ 9 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 tools/xlate/patches/0016-zero-locals-job-flag.patch diff --git a/docs/maxwell.md b/docs/maxwell.md index e8eba067..bd2bb90b 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -88,7 +88,7 @@ Source shorthands: | 3.9 | **Integer vertex inputs read as raw bits** (bone indices `R8UI`). | Float-declared inputs convert integers. | NXN Mario: stretched characters; re-translated with `uint` inputs. Ours: attribute types passed to `QueryGraphicsState` (`Program.cs`). | NXN | | 3.10 | **BFE / BFI fields past bit 31** (as PTX `bfe` / `bfi`): a field that runs past bit 31 is cut there; one that starts past it extracts 0 (signed: bit 31 repeated) and inserts nothing. TotK 1.4.3's Depths terrain vertex shaders (42 variants) take the position from a register, `((2y + x) * 8) & 0xff` with 5-bit x and y, so up to 248. | `OpBitField*Extract` / `OpBitFieldInsert` undefined when offset or offset + count exceeds 32; AMD's bitfield instructions take offset and width modulo 32. | Ryujinx passes the fields through. UltraTears saw the Depths walls and ground missing on the Steam Deck and a Linux RX 570 (RADV). Ours: clamped where the field is not a constant inside the word (translator patch 0012). RX 9070 on Windows drew the Depths the same before and after. | PTX ISA `bfe`, `bfi` | | 3.11 | **SHF by 0** gives the unshifted word (C for `SHF.L`, A for `SHF.R`). | Ryujinx's funnel shifts the other word by 32: undefined in SPIR-V; AMD shifts take the low 5 bits and give A \| C. | yuzu fixed Depths terrain gaps in its funnel shift (PR 10459). Ours: a select on a zero amount (translator patch 0013). In 1.4.3 every register SHF pairs its word with 0, so the result was right already. | — | -| 3.12 | **Registers read before any write** on a path (paths the structurizer adds included) have no defined value in the game's code; UltraTears reports 0 from the NVIDIA driver. | An `OpVariable` without an initializer loads an undefined value; Mesa folds undefined values freely. | Ours: none for now. Translator patch 0014 started every function-local variable at 0 (`OpConstantNull`); on NVIDIA (RTX 4080 SUPER, 1.4.3) that made the whole picture shake by about 0.9 output pixels a frame with DLSS or DLAA (#612; no shake with `TOTK_DLAA_JITTER=0` or without the patch), so it was taken out. A zero start for Mesa only, or for the shaders that need it, is still open. | — | +| 3.12 | **Registers read before any write** on a path (paths the structurizer adds included) have no defined value in the game's code; UltraTears reports 0 from the NVIDIA driver. | An `OpVariable` without an initializer loads an undefined value; Mesa folds undefined values freely. | Ours: on Mesa drivers (RADV, ANV, NVK; `vk_caps_driver`) every function-local variable starts at 0 (`OpConstantNull`, job flag `zero_locals`, translator patch 0016; `TOTK_ZERO_LOCALS` overrides). Elsewhere the load stays undefined: a zero start on every driver (the old patch 0014) made the whole picture shake by about 0.9 output pixels a frame with DLSS or DLAA on an RTX 4080 SUPER (#612). It also moved the SPIR-V hash of the game's FXAA shader out of `FXAA_FS` (`vk_dlaa.c` `vk_dlaa_skip_draw`), the likely cause of the shake; that path is not traced. | — | ## 4. Memory model diff --git a/runtime/gfx.h b/runtime/gfx.h index fae3de28..0bed28d1 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -53,7 +53,7 @@ const GfxShader *gfx_shader_await(int stage, const uint8_t *control, const uint8 uint64_t gfx_shader_async_done(void); // translations finished by the background worker void gfx_shader_counts(uint64_t *keys, uint64_t *translated); // distinct keys in memory, translations done (TOTK_TRACE=perf) // Translator answers for this device (vk_caps.c): shaderFloat64, storage offset rounding, subgroup size per vs/fs/cs. -void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3], uint32_t gather_bits); +void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3], uint32_t gather_bits, int zero_locals); const uint8_t *gfx_shader_key(const GfxShader *s); // the 32-byte cache key (stable while the translator is) const uint8_t *gfx_shader_input_id(const GfxShader *s); // the 32-byte input id (stable across translators) // By input id from memory or disk; xlate 1 also translates it from its stored inputs. NULL if absent. diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 881fa86d..e5c0359c 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -119,7 +119,7 @@ static struct { // translations in fl static char cache_dir[PATH_CAP]; static char xlate_exe[PATH_CAP]; static uint8_t xlate_id[32]; // translator identity (see the keys at the top) -static int host_f64 = 1, host_set; // gfx_shader_set_host +static int host_f64 = 1, host_zero_locals, host_set; // gfx_shader_set_host static uint32_t host_sbuf, host_sg[3] = {32, 32, 32}, host_gather; static PlatOnce paths_once = PLAT_ONCE_INIT; @@ -313,18 +313,19 @@ static void init_paths(void *param) { static void paths(void) { plat_once(&paths_once, init_paths, NULL); } // Host answers (vk_caps.c, at device creation, before any translation). Answers other than the defaults (Float64, -// exact storage offsets, 32 lanes, no gather bias) are mixed into the translator identity, so they get their own cache -// keys. -void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3], uint32_t gather_bits) { +// exact storage offsets, 32 lanes, no gather bias, undefined locals) are mixed into the translator identity, so they +// get their own cache keys. +void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3], uint32_t gather_bits, int zero_locals) { paths(); if (host_set++) return; - host_f64 = float64, host_sbuf = sbuf_align, host_gather = gather_bits; + host_f64 = float64, host_sbuf = sbuf_align, host_gather = gather_bits, host_zero_locals = zero_locals; memcpy(host_sg, subgroup, sizeof host_sg); - if (float64 && !sbuf_align && subgroup[0] == 32 && subgroup[1] == 32 && subgroup[2] == 32 && !gather_bits) return; + if (float64 && !sbuf_align && subgroup[0] == 32 && subgroup[1] == 32 && subgroup[2] == 32 && !gather_bits && !zero_locals) return; uint8_t buf[32 + 96]; int n = snprintf((char *)buf + 32, 96, "TKHO f64=%d sbuf=%u sg=%u,%u,%u", float64, sbuf_align, subgroup[0], subgroup[1], subgroup[2]); if (gather_bits) n += snprintf((char *)buf + 32 + n, (size_t)(96 - n), " gather=%u", gather_bits); + if (zero_locals) n += snprintf((char *)buf + 32 + n, (size_t)(96 - n), " zero_locals"); memcpy(buf, xlate_id, 32); rt_sha256(buf, 32 + (size_t)n, xlate_id); rt_log("gfx_shader: host options %s: own shader cache keys", (char *)buf + 37); @@ -1174,6 +1175,7 @@ static size_t build_request(char *o0, const char *hex, const char *stage, const if (host_sbuf) o += sprintf(o, ",\"sbuf_align\":%u", host_sbuf); if (host_sg[si] != 32) o += sprintf(o, ",\"subgroup\":%u", host_sg[si]); if (host_gather) o += sprintf(o, ",\"gather_bias\":%u", host_gather); + if (host_zero_locals) o += sprintf(o, ",\"zero_locals\":true"); o += sprintf(o, "},\"out\":\""); json_esc(&o, cache_dir); o += sprintf(o, "\"}"); diff --git a/runtime/vk.h b/runtime/vk.h index 3bc6f7ef..a544a0d5 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -57,9 +57,10 @@ typedef struct { int gpl; // VK_EXT_graphics_pipeline_library enabled with fast linking (TOTK_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 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 +void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, mesa void vk_caps_features(VkPhysicalDeviceFeatures *f); // before vkCreateDevice (TOTK_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 0c3a86d8..17fe85d3 100644 --- a/runtime/vk_caps.c +++ b/runtime/vk_caps.c @@ -43,6 +43,8 @@ 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.mesa = drv.driverID == VK_DRIVER_ID_MESA_RADV || drv.driverID == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA || + drv.driverID == VK_DRIVER_ID_MESA_NVK; } // TOTK_GPL=0 turns graphics pipeline libraries off; they are on wherever the driver has fast linking. AMD's RDNA 1/2 @@ -114,7 +116,10 @@ void vk_caps_init(VkCtx *c) { // BotW and TotK on AMD). The translator biases gather coordinates by the host's sub-texel precision; NVIDIA rounds // as Maxwell does. TOTK_GATHER_BIAS= forces it (0: off). uint32_t gather = env_u32("TOTK_GATHER_BIAS", c->props.vendorID == 0x10de ? 0 : l->subTexelPrecisionBits); - gfx_shader_set_host(vk_caps.float64, vk_caps.sbuf_align, told, gather); + // A register read before any write is an undefined load in SPIR-V, and Mesa folds undefined values freely, so on + // Mesa function-local variables start at 0 (docs/maxwell.md 3.12). TOTK_ZERO_LOCALS=1 forces it, 0 turns it off. + uint32_t zero_locals = env_u32("TOTK_ZERO_LOCALS", (uint32_t)vk_caps.mesa); + gfx_shader_set_host(vk_caps.float64, vk_caps.sbuf_align, told, gather, zero_locals != 0); if (gather) rt_log("vk caps: gather coordinates biased for %u sub-texel bits (TOTK_GATHER_BIAS)", gather); char dv[32]; diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index db375da9..7ffff8c8 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -1218,10 +1218,12 @@ void vk_dlaa_frame(void) { // scaled picture into the output-sized target, in a pass without the depth attachment the in-place FXAA has: there it // is always drawn, whatever fxaa says (skipping it would leave the 3D picture out), unless DLSS already wrote that // target this frame. -// The stock translator's hash, the one with tools/xlate patch 0010 (PCSS, PR #89) applied, and the one with patches -// 0010 and 0011 (the shadow filter spread) applied. A scaling draw this list misses still scales DLSS's picture, not +// The stock translator's hash, the one with tools/xlate patch 0010 (PCSS, PR #89) applied, the one with patches 0010 +// and 0011 (the shadow filter spread) applied, and that one with the zero_locals job flag (Mesa, vk_caps.c). +// A scaling draw this list misses still scales DLSS's picture, not // the jittered scene: after DLSS ran, passes larger than the light target sample DLSS's output (vk_dlaa_swap_img). -static const uint64_t FXAA_FS[] = {0xf450003ebeb3dc52ull, 0x0a12f4bc2a9a4fbeull, 0x5c43ae18948d52cbull}; +static const uint64_t FXAA_FS[] = {0xf450003ebeb3dc52ull, 0x0a12f4bc2a9a4fbeull, 0x5c43ae18948d52cbull, + 0xaad42238801d31d8ull}; static uint64_t spirv_hash(const GfxShader *s) { static struct { const GfxShader *s; uint64_t h; } tab[8192]; diff --git a/tools/build_xlate.py b/tools/build_xlate.py index c13ed192..9e3381e2 100644 --- a/tools/build_xlate.py +++ b/tools/build_xlate.py @@ -1,7 +1,7 @@ """Build the shader translator: build/xlate/totk_xlate.exe (Ryujinx 1.3.2 + patches, NativeAOT, SPIR-V for Vulkan). Copies tools/vendor/ryujinx to build/xlate/ryujinx, applies tools/xlate/patches/*.patch there (tools/vendor stays -byte-identical; 0001-0004 are MNG's, 0005-0013 and 0015 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build +byte-identical; 0001-0004 are MNG's, 0005-0013, 0015 and 0016 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build tools and vswhere.exe (docs/building.md). Run with .venv. On Linux it builds build/xlate/totk_xlate for linux-x64, which NativeAOT links with the system's clang (no MSVC); NativeAOT builds for the OS it runs on only. @@ -99,8 +99,8 @@ def prepare_sources() -> Path: for junk in list(src.glob("src/*/obj")) + list(src.glob("src/*/bin")): shutil.rmtree(junk) patches = sorted((XLATE / "patches").glob("*.patch")) - if len(patches) != 14: - sys.exit(f"expected 14 patches, found {len(patches)}") + if len(patches) != 15: + sys.exit(f"expected 15 patches, found {len(patches)}") for patch in patches: # git apply works outside a repository when given a directory; --check first so a failure names the patch. for extra in (["--check"], []): diff --git a/tools/xlate/Program.cs b/tools/xlate/Program.cs index 75ee5cb2..df74cb45 100644 --- a/tools/xlate/Program.cs +++ b/tools/xlate/Program.cs @@ -8,7 +8,7 @@ // totk_xlate batch mode: a JSON array of jobs, prints a summary, exit 1 if any failed. // job: { "key", "stage": "vs|ps|cs", "code": path (graphics = 0x50-byte SPH + instructions, compute = instructions), "c1": hex, // "tex": {slot: type}, "img": {slot: {type, fmt}}, "attr": {loc: float|sint|uint}, "fout": {idx: float|sint|uint}, -// "local": [x, y, z], "shared": bytes, "debug": bool, "reduced": bool, "softfloat64": bool, +// "local": [x, y, z], "shared": bytes, "debug": bool, "reduced": bool, "softfloat64": bool, "zero_locals": bool, // "sbuf_align": int, "subgroup": int } // Output: /.spv and /.res.json (bindings, queried slots, notes). using Ryujinx.Common.Memory; @@ -121,6 +121,8 @@ static class SwshXlate // the user banks c3-c16 (patch 0015, runtime/vk_mvprev.c); res.json's "mvprev" names the output location (-1: // none) and "mvprev_banks" the banks the position depends on (bit n: cn). public bool QueryMotionPrevious() => _job["mvprev"] is JsonValue v && v.TryGetValue(out bool b) && b; + // "zero_locals": true: function-local variables start at 0 (patch 0016, runtime/vk_caps.c decides per driver). + public bool QueryZeroLocals() => _job["zero_locals"] is JsonValue v && v.TryGetValue(out bool b) && b; public int MotionPreviousLocation = -1, MotionPreviousBanks; public void ReportMotionPrevious(int location, int banks) => (MotionPreviousLocation, MotionPreviousBanks) = (location, banks); } diff --git a/tools/xlate/patches/0016-zero-locals-job-flag.patch b/tools/xlate/patches/0016-zero-locals-job-flag.patch new file mode 100644 index 00000000..eee136ed --- /dev/null +++ b/tools/xlate/patches/0016-zero-locals-job-flag.patch @@ -0,0 +1,71 @@ +--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs ++++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs +@@ -33,8 +33,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv + { + foreach (AstOperand local in function.Locals) + { +- SpvInstruction localPointerType = context.TypePointer(StorageClass.Function, context.GetType(local.VarType)); +- SpvInstruction spvLocal = context.Variable(localPointerType, StorageClass.Function); ++ SpvInstruction localType = context.GetType(local.VarType); ++ SpvInstruction localPointerType = context.TypePointer(StorageClass.Function, localType); ++ ++ // TOTK: on hosts that ask for it (Mesa's compilers fold undefined values freely), a register read on a ++ // path that did not write it reads 0 instead of an undefined value. ++ SpvInstruction spvLocal = context.HostCapabilities.ZeroLocals ++ ? context.Variable(localPointerType, StorageClass.Function, context.ConstantNull(localType)) ++ : context.Variable(localPointerType, StorageClass.Function); + + context.AddLocalVariable(spvLocal); + context.DeclareLocal(local, spvLocal); +--- a/src/Ryujinx.Graphics.Shader/IGpuAccessor.cs ++++ b/src/Ryujinx.Graphics.Shader/IGpuAccessor.cs +@@ -188,6 +188,9 @@ namespace Ryujinx.Graphics.Shader + // the user output location it went to (-1: none) and the banks the position depends on (bit n: cn). + bool QueryMotionPrevious() => false; + ++ // TOTK: true when function-local variables start at 0 (CodeGen/Spirv/Declarations.cs DeclareLocals). ++ bool QueryZeroLocals() => false; ++ + void ReportMotionPrevious(int location, int banks) + { + } +--- a/src/Ryujinx.Graphics.Shader/Translation/HostCapabilities.cs ++++ b/src/Ryujinx.Graphics.Shader/Translation/HostCapabilities.cs +@@ -12,6 +12,7 @@ namespace Ryujinx.Graphics.Shader.Translation + public readonly bool SupportsShaderNonUniformIndexing; + public readonly bool SupportsTextureShadowLod; + public readonly bool SupportsViewportMask; ++ public readonly bool ZeroLocals; + + public HostCapabilities( + bool reducedPrecision, +@@ -23,7 +24,8 @@ namespace Ryujinx.Graphics.Shader.Translation + bool supportsShaderFloat64, + bool supportsShaderNonUniformIndexing, + bool supportsTextureShadowLod, +- bool supportsViewportMask) ++ bool supportsViewportMask, ++ bool zeroLocals) + { + ReducedPrecision = reducedPrecision; + SupportsFragmentShaderInterlock = supportsFragmentShaderInterlock; +@@ -35,6 +37,7 @@ namespace Ryujinx.Graphics.Shader.Translation + SupportsShaderNonUniformIndexing = supportsShaderNonUniformIndexing; + SupportsTextureShadowLod = supportsTextureShadowLod; + SupportsViewportMask = supportsViewportMask; ++ ZeroLocals = zeroLocals; + } + } + } +--- a/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs ++++ b/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs +@@ -372,7 +372,8 @@ namespace Ryujinx.Graphics.Shader.Translation + GpuAccessor.QueryHostSupportsShaderFloat64(), + GpuAccessor.QueryHostSupportsShaderNonUniformIndexing(), + GpuAccessor.QueryHostSupportsTextureShadowLod(), +- GpuAccessor.QueryHostSupportsViewportMask()); ++ GpuAccessor.QueryHostSupportsViewportMask(), ++ GpuAccessor.QueryZeroLocals()); + + CodeGenParameters parameters = new CodeGenParameters(attributeUsage, definitions, resourceManager.Properties, hostCapabilities, GpuAccessor, Options.TargetApi); + From 835310412cc045da68c80f2a5332e182f988df8f Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 4 Nov 2026 18:41:54 +0300 Subject: [PATCH 7/8] Copy a mirrored pool's flushes as exact ranges, so far-apart small flushes stop copying the pool between them nvn_pool_flushed kept one span from the lowest to the highest flush of a pool and mirror_sync copied all of it. In Kakariko one 252 MiB pool copied 22,121 MiB for 847 MiB flushed, in spans of up to the whole pool. Over PCIe 3.0 x8 (RX 6600, Ryzen 5 5500, issue #698) a typical span takes about 30 ms of GPU time, which fits the frame spikes there. A pool now keeps up to 32 sorted ranges and merges the two nearest above that. Writes between ranges still reach the mirror through the write watch. Over a walk in Kakariko the upload fell from 472.8 to 125.5 MiB and no flush range reached 16 MiB. TOTK_FLUSH_SPAN=1 restores the span. The mirror summary gains a line with the upload by source, and --probe-vkexec checks the range list and that two far-apart flushes copy only their pages. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 2 +- runtime/vk.h | 2 + runtime/vk_exec_probe.c | 19 +++++++ runtime/vk_memory.c | 112 ++++++++++++++++++++++++++++++++++------ 4 files changed, 119 insertions(+), 16 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 9166879f..164c9c2b 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -40,7 +40,7 @@ static const char *const known[] = { "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", - "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FREEZE_IDLE", "FREE_CORES", + "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_TEST", diff --git a/runtime/vk.h b/runtime/vk.h index 3bc6f7ef..35ab35e9 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -279,6 +279,8 @@ void vk_mem_mirror_prologue(VkCommandBuffer cb); void vk_mem_shadow_sync(void); void vk_mem_gpu_pending(uint64_t addr, uint64_t size); void vk_gpu_written(uint64_t addr, uint64_t size); +int vk_mem_flush_add_probe(void); // --probe-vkexec: the flush range list; 0 or the number of the failed check +uint64_t vk_mem_mirror_uploaded(void); // --probe-vkexec: bytes copied into mirrors since the last summary line int vk_mem_mirror_probe(uint64_t addr); // --probe-vkexec: the pool at addr gets its mirror as on a first buffer use; // 1 when it has one void vk_mem_class_log(const VkPhysicalDeviceProperties *p, const VkPhysicalDeviceMemoryProperties *m); // once diff --git a/runtime/vk_exec_probe.c b/runtime/vk_exec_probe.c index cbd7f2f4..9d244cdf 100644 --- a/runtime/vk_exec_probe.c +++ b/runtime/vk_exec_probe.c @@ -912,6 +912,25 @@ static int test_mirror(const GfxBackend *b, uint8_t *guest, int *bytes) { CHECK(xf_submit(b, stream, p), "fence_wait of the third mirror submission timed out"); for (int i = 0; i < 0x800; i++) CHECK(m[0x4000 + i] == 0x11, "CPU byte %#x beside a fill is %02x", i, m[0x4000 + i]); for (int i = 0; i < 0x800; i++) CHECK(m[0x4800 + i] == 0x77, "fill byte %#x is %02x", i, m[0x4800 + i]); + // 4: two small flushes at the ends of the pool copy their own pages, not what lies between them. + int fl_bad = vk_mem_flush_add_probe(); + CHECK(!fl_bad, "flush range list check %d failed", fl_bad); + p = put(stream, OP_BEGIN, NULL, 0); + const p_copy_b2b c4 = {a, a + 0x3000, 4096}; + p = put(p, OP_COPY_B2B, &c4, sizeof c4); + CHECK(xf_submit(b, stream, p), "fence_wait of the mirror submission before the flushes timed out"); + uint64_t up0 = vk_mem_mirror_uploaded(); + nvn_pool_flushed(&pool, 0x6000, 0x1000); + nvn_pool_flushed(&pool, 0xF0000, 0x1000); + p = put(stream, OP_BEGIN, NULL, 0); + p = put(p, OP_COPY_B2B, &c4, sizeof c4); + CHECK(xf_submit(b, stream, p), "fence_wait of the mirror submission after the flushes timed out"); + // A submission's copies reach the counter with the next one, and c4's own write-back adds its destination page. + CHECK(xf_submit(b, stream, p), "fence_wait of the mirror submission that counts the flushes timed out"); + uint64_t up = vk_mem_mirror_uploaded() - up0; + if (!rt_knob("FLUSH_SPAN", "0")) + CHECK(up >= 0x2000 && up < 0x10000, "two flushed pages copied %#llx bytes, 0xeb000 would be the span", + (unsigned long long)up); b->pool_fini(&pool); *bytes = 4096 + 0x1000 + 4096 + 0x1000; return 0; diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index d17a23e4..120de322 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -34,6 +34,9 @@ enum { MS_NONE, MS_ON, MS_REFUSED }; typedef struct { struct PoolRec *phys; uint64_t off; } VPage; // virtual page -> physical pool and offset +#define FLUSH_RANGES 32 // flush ranges a mirrored pool keeps between two syncs (flush_add) +typedef struct { uint64_t lo, hi; } FlushRange; + typedef struct PoolRec { const pool_t *key; int kind; @@ -59,7 +62,9 @@ typedef struct PoolRec { uint32_t wb_sub; // replay: the last submission that wrote mirror bytes back to guest memory uint32_t mgen; // gen_lock: every page write stamped up to this generation is in the mirror // (or in a copy queued before its next use) - uint64_t fl_lo, fl_hi; // flush_lock: nvnMemoryPoolFlushMappedRange bytes not copied yet (fl_hi 0: none) + FlushRange fl[FLUSH_RANGES + 1]; // flush_lock: nvnMemoryPoolFlushMappedRange pages not copied yet (flush_add) + int fl_n; // flush_lock: ranges in fl; read without the lock to skip an empty list + uint64_t fl_calls, fl_sum; // flush_lock: the calls and bytes behind fl, for mirror_sync's statistics // Shadow storage (POOL_IMPORT in shadow mode, "shadow storage" below): buf is a host-visible copy of the guest pages. VmaAllocation salloc; uint8_t *smap; // its mapping; offsets as in buf (from import_lo) @@ -504,7 +509,10 @@ static int dead_pending(void); static void vram_log(uint64_t frame); +static volatile uint64_t mirror_frame; // the frame vk_mem_census last saw, for mirror_sync's log lines + void vk_mem_census(uint64_t frame) { + mirror_frame = frame; static uint64_t window_start; if ((rt_trace & RT_TRACE_PERF) && frame % 600 == 0) vram_log(frame); if ((rt_trace & RT_TRACE_PERF) && !vk_mirror_on) dirty_frame(frame); @@ -1132,7 +1140,10 @@ static void dirty_frame(uint64_t frame) { // - CPU -> mirror: the first access of a submission to a mirror syncs it (mirror_sync): the 2 MiB write-watch // snapshots of its regions for this epoch (snap_regions, shared with the texture checks: the OS bits keep one // consumer, the shared page generations) and every page stamped after the mirror's mgen goes into a copy; so do the -// nvnMemoryPoolFlushMappedRange ranges. The first use copies the whole range. The copies run in a prologue command +// nvnMemoryPoolFlushMappedRange ranges, each on its own: one span from the lowest to the highest flush of a 252 MiB +// pool copied up to the whole pool for a few MiB flushed, 30-50 ms of GPU time over PCIe 3.0 x8 in crowded towns +// (TOTK_FLUSH_SPAN=1 restores it). Writes between the ranges need no copy: the write watch sees them. The first use +// copies the whole range. The copies run in a prologue command // buffer that vk_sync.c submits right before the part that accessed the mirror first, between full barriers. // vk_mem_mirror_begin snaps the regions of the pools the previous submission accessed in a few merged queries. // - GPU -> guest: what the replay writes into a mirror (storage buffers its shaders write, buffer copies, fills, @@ -1160,10 +1171,12 @@ static int npend, cpend, nwbl, cwbl; static VkBufferCopy *mregions; // replay: vkCmdCopyBuffer regions static int cregions; static PlatMutex flush_lock = PLAT_MUTEX_INIT; +static int flush_cap = FLUSH_RANGES; // ranges a pool keeps; 1 with TOTK_FLUSH_SPAN=1 typedef struct { uint64_t subs, syncs, upload_bytes, upload_copies, first_n, first_bytes, wb_bytes, wb_copies, wb_ranges; uint64_t presnap_queries, sync_queries, presnap_t, sync_t, prologues, presnap_bytes; + uint64_t page_bytes, flush_n, flush_bytes, flush_calls, flush_sum; // what mirror_sync queues, by source } MirrorAcc; static MirrorAcc mrep; // replay: this submission static MirrorAcc macc; // mirror_stat_lock: since the last summary @@ -1188,6 +1201,10 @@ static void mirror_mode(int discrete) { if (mirror_gap < 0) mirror_gap = 0; // Submissions a region stays unwritten before it is frozen. 60 rather than 300: in a fight 100 of the 257 MiB // queried a frame lay in regions idle for 30-299 submissions, and a thaw by a write costs about 60 us. + if (rt_knob("FLUSH_SPAN", "0")) { + flush_cap = 1; + rt_log("mem: a pool's flushes copy one span from the lowest to the highest (TOTK_FLUSH_SPAN=1)"); + } const char *fi = cfg_get("TOTK_FREEZE_IDLE"); freeze_idle = fi && *fi ? atoi(fi) : 60; if (freeze_idle < 0) freeze_idle = 0; @@ -1390,19 +1407,30 @@ static void mirror_sync(PoolRec *r) { if (!ch) continue; for (uint64_t x = a; x < b; x += 1ull << GEN_PAGE_SHIFT) if (ch[(x >> GEN_PAGE_SHIFT) & (GEN_PER_CHUNK - 1)] > since) - range_add(&pend, &npend, &cpend, r, x - lo, 1ull << GEN_PAGE_SHIFT); + range_add(&pend, &npend, &cpend, r, x - lo, 1ull << GEN_PAGE_SHIFT), mrep.page_bytes += 1ull << GEN_PAGE_SHIFT; } r->mgen = gen_now; if (excl) plat_mutex_unlock(&gen_lock); else plat_mutex_unlock_shared(&gen_lock); - if (__atomic_load_n(&r->fl_hi, __ATOMIC_ACQUIRE)) { + if (__atomic_load_n(&r->fl_n, __ATOMIC_ACQUIRE)) { + FlushRange fl[FLUSH_RANGES + 1]; plat_mutex_lock(&flush_lock); - uint64_t a = r->fl_lo, b = r->fl_hi; - r->fl_hi = 0; + int n = r->fl_n; + memcpy(fl, r->fl, (size_t)n * sizeof *fl); + uint64_t calls = r->fl_calls, sum = r->fl_sum; + r->fl_n = 0, r->fl_calls = r->fl_sum = 0; plat_mutex_unlock(&flush_lock); - a = a > lo ? a & ~0xfffull : lo; - b = b < hi ? (b + 0xfff) & ~0xfffull : hi; - if (a < b) range_add(&pend, &npend, &cpend, r, a - lo, b - a); + mrep.flush_calls += calls, mrep.flush_sum += sum; + for (int i = 0; i < n; i++) { + uint64_t a = fl[i].lo > lo ? fl[i].lo : lo, b = fl[i].hi < hi ? fl[i].hi : hi; + if (a >= b) continue; + range_add(&pend, &npend, &cpend, r, a - lo, b - a); + mrep.flush_n++, mrep.flush_bytes += b - a; + if ((rt_trace & RT_TRACE_PERF) && b - a >= 16u << 20) + rt_log("mem: mirror frame %llu: flush range %.1f MiB at +%.1f MiB of a %.1f MiB pool (%d ranges from %llu " + "flushes of %.3f MiB)", (unsigned long long)mirror_frame, (b - a) / 1048576.0, (a - lo) / 1048576.0, + (hi - lo) / 1048576.0, n, (unsigned long long)calls, sum / 1048576.0); + } } mrep.syncs++; mrep.sync_t += qpc() - t0; @@ -1437,6 +1465,60 @@ void vk_mem_gpu_wrote(const VkMemSpan *sp, uint64_t size) { range_add(&wbl, &nwbl, &cwbl, r, sp->offset, size); } +// Adds [a, b) to l, n sorted ranges that do not touch; returns the new count. Above cap ranges (l holds cap + 1) the two +// nearest merge, so a full list copies the fewest bytes that were not flushed; cap 1 keeps one span over all flushes. +static int flush_add(FlushRange *l, int n, uint64_t a, uint64_t b, int cap) { + int i = 0; + while (i < n && l[i].hi < a) i++; + int j = i; + for (; j < n && l[j].lo <= b; j++) { + if (l[j].lo < a) a = l[j].lo; + if (l[j].hi > b) b = l[j].hi; + } + memmove(l + i + 1, l + j, (size_t)(n - j) * sizeof *l); + l[i] = (FlushRange){a, b}; + n += 1 - (j - i); + if (n > cap) { + int k = 0; + for (int x = 1; x + 1 < n; x++) + if (l[x + 1].lo - l[x].hi < l[k + 1].lo - l[k].hi) k = x; + l[k].hi = l[k + 1].hi; + memmove(l + k + 1, l + k + 2, (size_t)(n - k - 2) * sizeof *l); + n--; + } + return n; +} + +int vk_mem_flush_add_probe(void) { + FlushRange l[5]; + int n = 0; + n = flush_add(l, n, 0x8000, 0x9000, 4); + n = flush_add(l, n, 0x1000, 0x2000, 4); + n = flush_add(l, n, 0x4000, 0x5000, 4); + if (n != 3 || l[0].lo != 0x1000 || l[1].lo != 0x4000 || l[2].hi != 0x9000) return 1; // kept apart, sorted + n = flush_add(l, n, 0x2000, 0x3000, 4); + if (n != 3 || l[0].hi != 0x3000) return 2; // touching ranges join + n = flush_add(l, n, 0x800, 0x8800, 4); + if (n != 1 || l[0].lo != 0x800 || l[0].hi != 0x9000) return 3; // one covers several + n = 0; + n = flush_add(l, n, 0x10000, 0x11000, 2); + n = flush_add(l, n, 0x40000, 0x41000, 2); + n = flush_add(l, n, 0x12000, 0x13000, 2); // over the cap: nearest merge + if (n != 2 || l[0].lo != 0x10000 || l[0].hi != 0x13000 || l[1].lo != 0x40000) return 4; + n = 0; + n = flush_add(l, n, 0x10000, 0x11000, 1); + n = flush_add(l, n, 0x40000, 0x41000, 1); + if (n != 1 || l[0].lo != 0x10000 || l[0].hi != 0x41000) return 5; // TOTK_FLUSH_SPAN=1 + return 0; +} + +uint64_t vk_mem_mirror_uploaded(void) { + plat_mutex_lock(&mirror_stat_lock); + uint64_t n = macc.upload_bytes; + plat_mutex_unlock(&mirror_stat_lock); + return n; +} + void nvn_pool_flushed(const pool_t *p, uint64_t off, uint64_t size) { if (!vk_mirror_on || !size || !p->mem) return; plat_inc64(&m_flush_calls); @@ -1445,13 +1527,10 @@ void nvn_pool_flushed(const pool_t *p, uint64_t off, uint64_t size) { const pidx_ent *e = pidx_find(&pool_index, (uintptr_t)p->mem, 0); PoolRec *r = e && e->key == p ? e->buf : NULL; if (r && r->mstate == MS_ON) { - uint64_t a = (uintptr_t)p->mem + off, b = a + size; + uint64_t a = ((uintptr_t)p->mem + off) & ~0xfffull, b = ((uintptr_t)p->mem + off + size + 0xfff) & ~0xfffull; plat_mutex_lock(&flush_lock); - if (!r->fl_hi) r->fl_lo = a, r->fl_hi = b; - else { - if (a < r->fl_lo) r->fl_lo = a; - if (b > r->fl_hi) r->fl_hi = b; - } + __atomic_store_n(&r->fl_n, flush_add(r->fl, r->fl_n, a, b, flush_cap), __ATOMIC_RELEASE); + r->fl_calls++, r->fl_sum += size; plat_mutex_unlock(&flush_lock); } plat_mutex_unlock_shared(&pool_lock); @@ -1677,6 +1756,9 @@ static void mirror_log(uint64_t frame) { a.first_n / f, a.first_bytes / f / M, a.wb_bytes / f / M, a.wb_copies / f, fc / f, fb / f / M, a.presnap_queries / f, a.presnap_bytes / f / M, a.sync_queries / f, a.prologues / f, qpc_ms(a.presnap_t + a.sync_t) / f, qpc_ms(a.presnap_t) / f, qpc_ms(a.sync_t) / f, ver); + rt_log("mem: mirror frame %llu: upload sources per frame, overlaps counted twice: written pages %.3f MiB; flush ranges " + "%.2f, %.3f MiB, from %.1f flushes of %.3f MiB", (unsigned long long)frame, a.page_bytes / f / M, a.flush_n / f, + a.flush_bytes / f / M, a.flush_calls / f, a.flush_sum / f / M); } // With TOTK_TRACE=perf every 600 frames: what holds device-local memory, to see what grows over a long session. From 072cccdb417f7adea2e7cb0f34e6bb3841ae4621 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 4 Nov 2026 19:10:09 +0300 Subject: [PATCH 8/8] Up version 0.3.10 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 0f826853..5503126d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.7 +0.3.10