mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 06:09:58 -04:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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.
|
(`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
|
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;
|
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
|
finalized descriptors may still follow live GPU-owned content. A finalize marks only the slots its own texture
|
||||||
same parameters is live at the storage, as the game reuses terrain heightmap storage that way
|
object registered (by initialization serial): the game initializes and registers the next terrain heightmap at the
|
||||||
(`vk_backend.c vk_be_texture_release`), and only the slots registered before it (the asynchronous replay applies
|
storage of the previous one before it finalizes that one (`vk_texture.c vk_texture_release`).
|
||||||
releases late, registrations at once).
|
|
||||||
- Shaders: Ampere (SM86) code is lowered to Maxwell (`tools/xlate/AmpereLowering.cs`) and translated to SPIR-V by the
|
- 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
|
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
|
`nx2navi_xlate.exe` and run as separate processes. Translations are cached on disk; shaders and pipelines seen in
|
||||||
|
|||||||
@@ -149,8 +149,7 @@ NVN_API(nvnTextureBuilderGetStorageAlignment) {
|
|||||||
// ---------- Textures
|
// ---------- Textures
|
||||||
// The game re-initializes transient textures (G-buffer and the like) every frame at the same pool memory without
|
// 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
|
// 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
|
// storage address -> a copy of the live texture so the back end can redirect a registration whose serial was finalized.
|
||||||
// the same parameters still holds (vk_backend.c vk_be_texture_release).
|
|
||||||
static nvn_hmap live = {.klen = 8};
|
static nvn_hmap live = {.klen = 8};
|
||||||
static PlatMutex live_lock = PLAT_MUTEX_INIT;
|
static PlatMutex live_lock = PLAT_MUTEX_INIT;
|
||||||
|
|
||||||
|
|||||||
@@ -840,15 +840,16 @@ static int test_textures(pool_t *tpool, pool_t *phys) {
|
|||||||
const VkTexImage *renewed = vk_texture_by_id(5, NULL, NULL);
|
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,
|
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");
|
"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
|
// A release finalizes only the slots of its own texture object: another texture initialized at the same storage
|
||||||
// registration takes effect at once) leaves that registration current: the texture made again at the storage.
|
// with the same parameters before the finalize keeps its slot (vk_texture_release, terrain heightmaps).
|
||||||
uint32_t mark = vk_texture_reg_mark();
|
tex_t bn = b;
|
||||||
vk_texture_register(6, &b, NULL);
|
bn.serial = 6;
|
||||||
vk_texture_release_at(&b, mark);
|
vk_texture_register(6, &bn, NULL);
|
||||||
|
vk_texture_release(&b);
|
||||||
const VkTexImage *kept = vk_texture_by_id(6, NULL, NULL);
|
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),
|
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");
|
"a release finalized slot 6 of another live texture, or left its own slot 5 current");
|
||||||
vk_texture_release(&b);
|
vk_texture_release(&bn);
|
||||||
// Once the live texture at its storage holds GPU content the storage bytes lack, the finalized CPU slot follows it.
|
// 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
|
// 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.
|
// must then show the clear, which neither the storage bytes nor a recreated image of the slot's own descriptor hold.
|
||||||
|
|||||||
@@ -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_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_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
|
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_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_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
|
void vk_guest_written(uint64_t addr, uint64_t size); // a host write write-watch may miss: textures there re-upload
|
||||||
|
|||||||
@@ -267,7 +267,6 @@ typedef struct {
|
|||||||
uint64_t value, submit_no, at, queued;
|
uint64_t value, submit_no, at, queued;
|
||||||
uint64_t *h;
|
uint64_t *h;
|
||||||
const tex_t *tex; // RI_RELEASE: a copy the item owns (the game may reuse the object's memory)
|
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;
|
} ReplayItem;
|
||||||
#define RQ_CAP 1024 // a frame queues 1 submit and 1 present, and up to a few hundred releases
|
#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,
|
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);
|
free(it.h);
|
||||||
} else if (it.kind == RI_RELEASE) {
|
} else if (it.kind == RI_RELEASE) {
|
||||||
vk_present_drain();
|
vk_present_drain();
|
||||||
vk_texture_release_at(it.tex, it.reg_mark);
|
vk_texture_release(it.tex);
|
||||||
free((void *)it.tex);
|
free((void *)it.tex);
|
||||||
} else {
|
} else {
|
||||||
uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED);
|
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
|
// 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
|
// 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.
|
// 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) {
|
static void vk_be_texture_release(const tex_t *t) {
|
||||||
vk_sync_start();
|
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()) {
|
if (!gfx_replay_async()) {
|
||||||
vk_present_drain();
|
vk_present_drain();
|
||||||
vk_texture_release(t);
|
vk_texture_release(t);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!t || !t->mem) return;
|
||||||
tex_t *copy = malloc(sizeof *copy);
|
tex_t *copy = malloc(sizeof *copy);
|
||||||
if (!copy) rt_fatal("vk: out of memory (replay queue)");
|
if (!copy) rt_fatal("vk: out of memory (replay queue)");
|
||||||
*copy = *t;
|
*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);
|
plat_mutex_lock(&rl);
|
||||||
dr_calls[DR_TEX_RELEASE]++, dr_tcalls[DR_TEX_RELEASE]++;
|
dr_calls[DR_TEX_RELEASE]++, dr_tcalls[DR_TEX_RELEASE]++;
|
||||||
plat_mutex_unlock(&rl);
|
plat_mutex_unlock(&rl);
|
||||||
|
|||||||
@@ -2523,17 +2523,7 @@ void vk_texture_gpu_written(const tex_t *t) {
|
|||||||
unlock();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t vk_texture_reg_mark(void) { return __atomic_load_n(®_serial, __ATOMIC_ACQUIRE); }
|
void vk_texture_release(const tex_t *t) {
|
||||||
|
|
||||||
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;
|
if (!t || !t->mem) return;
|
||||||
TexKey k;
|
TexKey k;
|
||||||
make_key(t, &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
|
// 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
|
// 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.
|
// finalized before its image is created.
|
||||||
// Only the slots registered before the game finalized the texture (mark): the asynchronous replay applies a
|
// Only the slots this texture object registered (its initialization serial): a slot registered with another
|
||||||
// release behind the submissions queued before it (vk_backend.c vk_be_texture_release), while a registration takes
|
// texture initialized at the same storage with the same parameters names that one, which is still live. Ocarina
|
||||||
// effect at once. A texture the game initializes at the same storage with the same parameters after the finalize,
|
// streams terrain heightmaps (R32F 260x260) through a fixed set of storage and initializes and registers the next
|
||||||
// and registers before the replay reaches the release, keeps its slot.
|
// 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;
|
int changed = 0;
|
||||||
if (sinfo)
|
if (sinfo)
|
||||||
for (int id = bucket_head[agen_idx(k.addr)]; id >= 0; id = slot_next[id]) {
|
for (int id = bucket_head[agen_idx(k.addr)]; id >= 0; id = slot_next[id]) {
|
||||||
TexSlot *s = &tslots[id];
|
TexSlot *s = &tslots[id];
|
||||||
if (s->finalized || (uintptr_t)s->t.mem != k.addr) continue;
|
if (s->finalized || (uintptr_t)s->t.mem != k.addr || s->t.serial != t->serial) continue;
|
||||||
if ((int32_t)((uint32_t)(sinfo[id] >> 16) - mark) > 0) continue; // registered after the finalize
|
|
||||||
TexKey sk;
|
TexKey sk;
|
||||||
make_key(&s->t, &sk);
|
make_key(&s->t, &sk);
|
||||||
if (!memcmp(&sk, &k, sizeof k)) s->finalized = 1, s->ce = NULL, changed = 1;
|
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->ce = NULL;
|
||||||
s->cheld = 0;
|
s->cheld = 0;
|
||||||
slots_gen++;
|
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);
|
__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));
|
slot_rebucket(id, agen_idx((uintptr_t)t->mem));
|
||||||
dirty_push((uint32_t)id);
|
dirty_push((uint32_t)id);
|
||||||
|
|||||||
Reference in New Issue
Block a user