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);