From 1096efef129937fd762fedb7f187632dd7f33559 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 11 Nov 2026 01:18:11 -0300 Subject: [PATCH 1/2] Keep a texture's slots current when the game finalizes another texture with the same parameters at its storage, which left streamed terrain heightmaps stale and drew those chunks at height zero, far below the ground (issue #453). Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 5 ++++- runtime/nvn_api_obj.c | 3 ++- runtime/tex_probe.c | 8 ++++++++ runtime/vk.h | 5 +++++ runtime/vk_backend.c | 17 ++++++++++++++--- runtime/vk_texture.c | 19 +++++++++++++++++-- 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 69b58511..e6b0e62c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -98,7 +98,10 @@ keeps per-core data without locks and relies on that, so the host models it (`ru (`vk_exec_draw.c pool_sync`); slots that resolve alike share one lookup. Finalization is retained on registered slots independently of image-cache eviction: a stale CPU-backed descriptor cannot recreate an image from freed storage or follow newly reused CPU bytes. Registration makes it current again; - finalized descriptors may still follow live GPU-owned content. + finalized descriptors may still follow live GPU-owned content. A finalize marks none while another texture with the + same parameters is live at the storage, as the game reuses terrain heightmap storage that way + (`vk_backend.c vk_be_texture_release`), and only the slots registered before it (the asynchronous replay applies + releases late, registrations at once). - Shaders: Ampere (SM86) code is lowered to Maxwell (`tools/xlate/AmpereLowering.cs`) and translated to SPIR-V by the pinned shader translator (`tools/vendor/ryujinx`, 1.3.2) with our patches (`tools/xlate/patches/`), built with .NET 10 NativeAOT as `nx2navi_xlate.exe` and run as separate processes. Translations are cached on disk; shaders and pipelines seen in diff --git a/runtime/nvn_api_obj.c b/runtime/nvn_api_obj.c index 55758efe..d4ef5c82 100644 --- a/runtime/nvn_api_obj.c +++ b/runtime/nvn_api_obj.c @@ -149,7 +149,8 @@ NVN_API(nvnTextureBuilderGetStorageAlignment) { // ---------- Textures // The game re-initializes transient textures (G-buffer and the like) every frame at the same pool memory without // registering the descriptor again, as an NVN descriptor refers to memory and stays valid on hardware. The table maps -// storage address -> a copy of the live texture so the back end can redirect a registration whose serial was finalized. +// storage address -> a copy of the live texture, which tells the back end a finalize whose storage another texture with +// the same parameters still holds (vk_backend.c vk_be_texture_release). static nvn_hmap live = {.klen = 8}; static PlatMutex live_lock = PLAT_MUTEX_INIT; diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index b568e790..331010d9 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -840,6 +840,14 @@ static int test_textures(pool_t *tpool, pool_t *phys) { const VkTexImage *renewed = vk_texture_by_id(5, NULL, NULL); GCHECK(renewed && renewed->format == VK_FORMAT_BC1_RGBA_UNORM_BLOCK && renewed->width == 64 && renewed->height == 48, "re-registered CPU slot 5 did not resolve its own descriptor"); + // A release applied after a later registration of its descriptor (the asynchronous replay queues releases, a + // registration takes effect at once) leaves that registration current: the texture made again at the storage. + uint32_t mark = vk_texture_reg_mark(); + vk_texture_register(6, &b, NULL); + vk_texture_release_at(&b, mark); + const VkTexImage *kept = vk_texture_by_id(6, NULL, NULL); + GCHECK(kept && kept->width == 64 && kept->height == 48 && !vk_texture_by_id(5, NULL, NULL), + "a release finalized slot 6, registered after it, or left slot 5, registered before it, current"); vk_texture_release(&b); // Once the live texture at its storage holds GPU content the storage bytes lack, the finalized CPU slot follows it. // The live texture is cleared on the GPU and noted as rendered into, as a draw into it would be; the slot's image diff --git a/runtime/vk.h b/runtime/vk.h index 8a1face5..1918f904 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -234,6 +234,11 @@ int vk_pool_map_virtual(pool_t *v, int n, const void *requests); // nvn_mappi void vk_texture_register(int id, const tex_t *t, const view_t *v); // texture descriptor slot (copied) void vk_sampler_register(int id, const samp_par *s); // sampler descriptor slot (copied) void vk_texture_release(const tex_t *t); // TextureFinalize: the image stays for slots that still name its storage +// As vk_texture_release, for the slots registered up to mark (vk_texture_reg_mark when the game finalized the texture): +// later registrations stay current. +void vk_texture_release_at(const tex_t *t, uint32_t mark); +uint32_t vk_texture_reg_mark(void); // the serial of the latest slot registration +int vk_texture_same_key(const tex_t *a, const tex_t *b); // the same storage, descriptor and layout: one image void vk_read_texels(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint8_t *dst, uint64_t row, uint64_t img); void vk_write_texels(const tex_t *t, const view_t *v, const NVNcopyRegion *r, const uint8_t *src, uint64_t row, uint64_t img); void vk_guest_written(uint64_t addr, uint64_t size); // a host write write-watch may miss: textures there re-upload diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 87576bd3..75e18952 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -267,6 +267,7 @@ typedef struct { uint64_t value, submit_no, at, queued; uint64_t *h; const tex_t *tex; // RI_RELEASE: a copy the item owns (the game may reuse the object's memory) + uint32_t reg_mark; // RI_RELEASE: vk_texture_reg_mark when the game finalized it } ReplayItem; #define RQ_CAP 1024 // a frame queues 1 submit and 1 present, and up to a few hundred releases enum { DR_POOL_FINI, DR_MAP_VIRTUAL, DR_TEX_RELEASE, DR_PROG_RELEASE, DR_FINISH, DR_READ_TEXELS, DR_WRITE_TEXELS, @@ -424,7 +425,7 @@ static void replay_main(void *arg) { free(it.h); } else if (it.kind == RI_RELEASE) { vk_present_drain(); - vk_texture_release(it.tex); + vk_texture_release_at(it.tex, it.reg_mark); free((void *)it.tex); } else { uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED); @@ -593,18 +594,28 @@ static void vk_be_sampler_register(int id, const samp_par *s) { vk_sync_start(); // Asynchronous: the release is queued behind the submissions before it, so their replay still resolves the texture's // slots to its image (a released slot follows another live texture at the storage), and the ones after it see it // released, the order of the synchronous replay. vk_texture_release reads only the object's key fields: a copy. +// Registrations are not queued: the release carries the registration serial of the finalize, and the slots registered +// after it stay current (vk_texture_release_at). static void vk_be_texture_release(const tex_t *t) { vk_sync_start(); + if (!t || !t->mem) return; + // Another texture object initialized at the storage with the same parameters is still live (the front end's live + // texture table, nvn_api_obj.c nvn_tex_live_at): the image and the slots naming that descriptor are its own, as an + // NVN descriptor refers to memory, and the release changes nothing. Ocarina streams terrain heightmaps (R32F + // 260x260) through a fixed set of storage and initializes and registers the next chunk's texture there before it + // finalizes the previous one: releasing left the slot stale, the draws sampled the zero stand-in, and the chunk was + // drawn at height zero, tens of metres below the ground around it. + tex_t live; + if (nvn_tex_live_at(t->mem, &live) && live.serial != t->serial && vk_texture_same_key(&live, t)) return; if (!gfx_replay_async()) { vk_present_drain(); vk_texture_release(t); return; } - if (!t || !t->mem) return; tex_t *copy = malloc(sizeof *copy); if (!copy) rt_fatal("vk: out of memory (replay queue)"); *copy = *t; - ReplayItem it = {.kind = RI_RELEASE, .tex = copy}; + ReplayItem it = {.kind = RI_RELEASE, .tex = copy, .reg_mark = vk_texture_reg_mark()}; plat_mutex_lock(&rl); dr_calls[DR_TEX_RELEASE]++, dr_tcalls[DR_TEX_RELEASE]++; plat_mutex_unlock(&rl); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 2f2f2723..a8ec8318 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -2523,7 +2523,17 @@ void vk_texture_gpu_written(const tex_t *t) { unlock(); } -void vk_texture_release(const tex_t *t) { +uint32_t vk_texture_reg_mark(void) { return __atomic_load_n(®_serial, __ATOMIC_ACQUIRE); } + +int vk_texture_same_key(const tex_t *a, const tex_t *b) { + TexKey ka, kb; + make_key(a, &ka), make_key(b, &kb); + return !memcmp(&ka, &kb, sizeof ka); +} + +void vk_texture_release(const tex_t *t) { vk_texture_release_at(t, vk_texture_reg_mark()); } + +void vk_texture_release_at(const tex_t *t, uint32_t mark) { if (!t || !t->mem) return; TexKey k; make_key(t, &k); @@ -2532,11 +2542,16 @@ void vk_texture_release(const tex_t *t) { // Staleness belongs to the registered descriptor, not the cached image: a released Tex can be destroyed before the // slot looks again (eviction, the released cap, start_at for a mirrored one). A registered texture may also be // finalized before its image is created. + // Only the slots registered before the game finalized the texture (mark): the asynchronous replay applies a + // release behind the submissions queued before it (vk_backend.c vk_be_texture_release), while a registration takes + // effect at once. A texture the game initializes at the same storage with the same parameters after the finalize, + // and registers before the replay reaches the release, keeps its slot. int changed = 0; if (sinfo) for (int id = bucket_head[agen_idx(k.addr)]; id >= 0; id = slot_next[id]) { TexSlot *s = &tslots[id]; if (s->finalized || (uintptr_t)s->t.mem != k.addr) continue; + if ((int32_t)((uint32_t)(sinfo[id] >> 16) - mark) > 0) continue; // registered after the finalize TexKey sk; make_key(&s->t, &sk); if (!memcmp(&sk, &k, sizeof k)) s->finalized = 1, s->ce = NULL, changed = 1; @@ -2605,7 +2620,7 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { s->ce = NULL; s->cheld = 0; slots_gen++; - if (!++reg_serial) reg_serial = 1; + __atomic_store_n(®_serial, reg_serial + 1 ? reg_serial + 1 : 1, __ATOMIC_RELEASE); // read by vk_texture_reg_mark __atomic_store_n(&sinfo[id], (uint64_t)reg_serial << 16 | agen_idx((uintptr_t)t->mem), __ATOMIC_RELEASE); slot_rebucket(id, agen_idx((uintptr_t)t->mem)); dirty_push((uint32_t)id); From 2765a752b0fee6836aa2becb3dbfcdba737a16d7 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 11 Nov 2026 09:30:37 -0300 Subject: [PATCH 2/2] Finalize only the slots a texture object registered itself, so a finalize no longer touches another live texture's slots at the same storage, and drop the early return that skipped the whole release when such a texture was live, which also kept textures the game expected released (Kakariko fog lines). Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 7 +++---- runtime/nvn_api_obj.c | 3 +-- runtime/tex_probe.c | 15 ++++++++------- runtime/vk.h | 5 ----- runtime/vk_backend.c | 17 +++-------------- runtime/vk_texture.c | 26 ++++++++------------------ 6 files changed, 23 insertions(+), 50 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index e6b0e62c..a5e9126d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -98,10 +98,9 @@ keeps per-core data without locks and relies on that, so the host models it (`ru (`vk_exec_draw.c pool_sync`); slots that resolve alike share one lookup. Finalization is retained on registered slots independently of image-cache eviction: a stale CPU-backed descriptor cannot recreate an image from freed storage or follow newly reused CPU bytes. Registration makes it current again; - finalized descriptors may still follow live GPU-owned content. A finalize marks none while another texture with the - same parameters is live at the storage, as the game reuses terrain heightmap storage that way - (`vk_backend.c vk_be_texture_release`), and only the slots registered before it (the asynchronous replay applies - releases late, registrations at once). + finalized descriptors may still follow live GPU-owned content. A finalize marks only the slots its own texture + object registered (by initialization serial): the game initializes and registers the next terrain heightmap at the + storage of the previous one before it finalizes that one (`vk_texture.c vk_texture_release`). - Shaders: Ampere (SM86) code is lowered to Maxwell (`tools/xlate/AmpereLowering.cs`) and translated to SPIR-V by the pinned shader translator (`tools/vendor/ryujinx`, 1.3.2) with our patches (`tools/xlate/patches/`), built with .NET 10 NativeAOT as `nx2navi_xlate.exe` and run as separate processes. Translations are cached on disk; shaders and pipelines seen in diff --git a/runtime/nvn_api_obj.c b/runtime/nvn_api_obj.c index d4ef5c82..55758efe 100644 --- a/runtime/nvn_api_obj.c +++ b/runtime/nvn_api_obj.c @@ -149,8 +149,7 @@ NVN_API(nvnTextureBuilderGetStorageAlignment) { // ---------- Textures // The game re-initializes transient textures (G-buffer and the like) every frame at the same pool memory without // registering the descriptor again, as an NVN descriptor refers to memory and stays valid on hardware. The table maps -// storage address -> a copy of the live texture, which tells the back end a finalize whose storage another texture with -// the same parameters still holds (vk_backend.c vk_be_texture_release). +// storage address -> a copy of the live texture so the back end can redirect a registration whose serial was finalized. static nvn_hmap live = {.klen = 8}; static PlatMutex live_lock = PLAT_MUTEX_INIT; diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index 331010d9..10a71eea 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -840,15 +840,16 @@ static int test_textures(pool_t *tpool, pool_t *phys) { const VkTexImage *renewed = vk_texture_by_id(5, NULL, NULL); GCHECK(renewed && renewed->format == VK_FORMAT_BC1_RGBA_UNORM_BLOCK && renewed->width == 64 && renewed->height == 48, "re-registered CPU slot 5 did not resolve its own descriptor"); - // A release applied after a later registration of its descriptor (the asynchronous replay queues releases, a - // registration takes effect at once) leaves that registration current: the texture made again at the storage. - uint32_t mark = vk_texture_reg_mark(); - vk_texture_register(6, &b, NULL); - vk_texture_release_at(&b, mark); + // A release finalizes only the slots of its own texture object: another texture initialized at the same storage + // with the same parameters before the finalize keeps its slot (vk_texture_release, terrain heightmaps). + tex_t bn = b; + bn.serial = 6; + vk_texture_register(6, &bn, NULL); + vk_texture_release(&b); const VkTexImage *kept = vk_texture_by_id(6, NULL, NULL); GCHECK(kept && kept->width == 64 && kept->height == 48 && !vk_texture_by_id(5, NULL, NULL), - "a release finalized slot 6, registered after it, or left slot 5, registered before it, current"); - vk_texture_release(&b); + "a release finalized slot 6 of another live texture, or left its own slot 5 current"); + vk_texture_release(&bn); // Once the live texture at its storage holds GPU content the storage bytes lack, the finalized CPU slot follows it. // The live texture is cleared on the GPU and noted as rendered into, as a draw into it would be; the slot's image // must then show the clear, which neither the storage bytes nor a recreated image of the slot's own descriptor hold. diff --git a/runtime/vk.h b/runtime/vk.h index 1918f904..8a1face5 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -234,11 +234,6 @@ int vk_pool_map_virtual(pool_t *v, int n, const void *requests); // nvn_mappi void vk_texture_register(int id, const tex_t *t, const view_t *v); // texture descriptor slot (copied) void vk_sampler_register(int id, const samp_par *s); // sampler descriptor slot (copied) void vk_texture_release(const tex_t *t); // TextureFinalize: the image stays for slots that still name its storage -// As vk_texture_release, for the slots registered up to mark (vk_texture_reg_mark when the game finalized the texture): -// later registrations stay current. -void vk_texture_release_at(const tex_t *t, uint32_t mark); -uint32_t vk_texture_reg_mark(void); // the serial of the latest slot registration -int vk_texture_same_key(const tex_t *a, const tex_t *b); // the same storage, descriptor and layout: one image void vk_read_texels(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint8_t *dst, uint64_t row, uint64_t img); void vk_write_texels(const tex_t *t, const view_t *v, const NVNcopyRegion *r, const uint8_t *src, uint64_t row, uint64_t img); void vk_guest_written(uint64_t addr, uint64_t size); // a host write write-watch may miss: textures there re-upload diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 75e18952..87576bd3 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -267,7 +267,6 @@ typedef struct { uint64_t value, submit_no, at, queued; uint64_t *h; const tex_t *tex; // RI_RELEASE: a copy the item owns (the game may reuse the object's memory) - uint32_t reg_mark; // RI_RELEASE: vk_texture_reg_mark when the game finalized it } ReplayItem; #define RQ_CAP 1024 // a frame queues 1 submit and 1 present, and up to a few hundred releases enum { DR_POOL_FINI, DR_MAP_VIRTUAL, DR_TEX_RELEASE, DR_PROG_RELEASE, DR_FINISH, DR_READ_TEXELS, DR_WRITE_TEXELS, @@ -425,7 +424,7 @@ static void replay_main(void *arg) { free(it.h); } else if (it.kind == RI_RELEASE) { vk_present_drain(); - vk_texture_release_at(it.tex, it.reg_mark); + vk_texture_release(it.tex); free((void *)it.tex); } else { uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED); @@ -594,28 +593,18 @@ static void vk_be_sampler_register(int id, const samp_par *s) { vk_sync_start(); // Asynchronous: the release is queued behind the submissions before it, so their replay still resolves the texture's // slots to its image (a released slot follows another live texture at the storage), and the ones after it see it // released, the order of the synchronous replay. vk_texture_release reads only the object's key fields: a copy. -// Registrations are not queued: the release carries the registration serial of the finalize, and the slots registered -// after it stay current (vk_texture_release_at). static void vk_be_texture_release(const tex_t *t) { vk_sync_start(); - if (!t || !t->mem) return; - // Another texture object initialized at the storage with the same parameters is still live (the front end's live - // texture table, nvn_api_obj.c nvn_tex_live_at): the image and the slots naming that descriptor are its own, as an - // NVN descriptor refers to memory, and the release changes nothing. Ocarina streams terrain heightmaps (R32F - // 260x260) through a fixed set of storage and initializes and registers the next chunk's texture there before it - // finalizes the previous one: releasing left the slot stale, the draws sampled the zero stand-in, and the chunk was - // drawn at height zero, tens of metres below the ground around it. - tex_t live; - if (nvn_tex_live_at(t->mem, &live) && live.serial != t->serial && vk_texture_same_key(&live, t)) return; if (!gfx_replay_async()) { vk_present_drain(); vk_texture_release(t); return; } + if (!t || !t->mem) return; tex_t *copy = malloc(sizeof *copy); if (!copy) rt_fatal("vk: out of memory (replay queue)"); *copy = *t; - ReplayItem it = {.kind = RI_RELEASE, .tex = copy, .reg_mark = vk_texture_reg_mark()}; + ReplayItem it = {.kind = RI_RELEASE, .tex = copy}; plat_mutex_lock(&rl); dr_calls[DR_TEX_RELEASE]++, dr_tcalls[DR_TEX_RELEASE]++; plat_mutex_unlock(&rl); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index a8ec8318..66b3b6fb 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -2523,17 +2523,7 @@ void vk_texture_gpu_written(const tex_t *t) { unlock(); } -uint32_t vk_texture_reg_mark(void) { return __atomic_load_n(®_serial, __ATOMIC_ACQUIRE); } - -int vk_texture_same_key(const tex_t *a, const tex_t *b) { - TexKey ka, kb; - make_key(a, &ka), make_key(b, &kb); - return !memcmp(&ka, &kb, sizeof ka); -} - -void vk_texture_release(const tex_t *t) { vk_texture_release_at(t, vk_texture_reg_mark()); } - -void vk_texture_release_at(const tex_t *t, uint32_t mark) { +void vk_texture_release(const tex_t *t) { if (!t || !t->mem) return; TexKey k; make_key(t, &k); @@ -2542,16 +2532,16 @@ void vk_texture_release_at(const tex_t *t, uint32_t mark) { // Staleness belongs to the registered descriptor, not the cached image: a released Tex can be destroyed before the // slot looks again (eviction, the released cap, start_at for a mirrored one). A registered texture may also be // finalized before its image is created. - // Only the slots registered before the game finalized the texture (mark): the asynchronous replay applies a - // release behind the submissions queued before it (vk_backend.c vk_be_texture_release), while a registration takes - // effect at once. A texture the game initializes at the same storage with the same parameters after the finalize, - // and registers before the replay reaches the release, keeps its slot. + // Only the slots this texture object registered (its initialization serial): a slot registered with another + // texture initialized at the same storage with the same parameters names that one, which is still live. Ocarina + // streams terrain heightmaps (R32F 260x260) through a fixed set of storage and initializes and registers the next + // chunk's texture there before it finalizes the previous one; finalizing the new registration left the slot stale, + // the draws sampled the zero stand-in, and the chunk was drawn at height zero, tens of metres below the ground. int changed = 0; if (sinfo) for (int id = bucket_head[agen_idx(k.addr)]; id >= 0; id = slot_next[id]) { TexSlot *s = &tslots[id]; - if (s->finalized || (uintptr_t)s->t.mem != k.addr) continue; - if ((int32_t)((uint32_t)(sinfo[id] >> 16) - mark) > 0) continue; // registered after the finalize + if (s->finalized || (uintptr_t)s->t.mem != k.addr || s->t.serial != t->serial) continue; TexKey sk; make_key(&s->t, &sk); if (!memcmp(&sk, &k, sizeof k)) s->finalized = 1, s->ce = NULL, changed = 1; @@ -2620,7 +2610,7 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { s->ce = NULL; s->cheld = 0; slots_gen++; - __atomic_store_n(®_serial, reg_serial + 1 ? reg_serial + 1 : 1, __ATOMIC_RELEASE); // read by vk_texture_reg_mark + if (!++reg_serial) reg_serial = 1; __atomic_store_n(&sinfo[id], (uint64_t)reg_serial << 16 | agen_idx((uintptr_t)t->mem), __ATOMIC_RELEASE); slot_rebucket(id, agen_idx((uintptr_t)t->mem)); dirty_push((uint32_t)id);