From 36752867474aa4850dbf31f8f3d79866901d12e7 Mon Sep 17 00:00:00 2001 From: lowlevel-larry <350257211+lowlevel-larry@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:40:51 -0300 Subject: [PATCH] Keep a texture's pool flags instead of reading its pool object later tex_mirrored (vk_texture.c) read t->pool->flags whenever the back end created the host entry of a texture, which can happen long after nvnTextureInitialize. A game may finalize a memory pool and free the memory the NVNmemoryPool object lived in while textures from it are still looked up (on a level change, for instance); reading the freed object then faults. The NVN driver keeps no pointer to the pool either. tex_t now records the pool's flags at Initialize, in the padding after layout[8], so NVNtexture's size is unchanged. Co-Authored-By: Claude Opus 5.5 --- runtime/nvn_api_obj.c | 1 + runtime/nvn_obj.h | 3 +++ runtime/vk_exec_probe.c | 2 +- runtime/vk_texture.c | 3 +-- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/runtime/nvn_api_obj.c b/runtime/nvn_api_obj.c index 6d3c3fcf..ffe0648a 100644 --- a/runtime/nvn_api_obj.c +++ b/runtime/nvn_api_obj.c @@ -197,6 +197,7 @@ NVN_API(nvnTextureInitialize) { o->magic = NVN_TEX_MAGIC; o->d = s->d; o->pool = s->pool; + o->pool_flags = s->pool ? (uint16_t)s->pool->flags : 0; o->off = s->off; o->has_layout = s->has_layout; memcpy(o->layout, s->layout, 8); diff --git a/runtime/nvn_obj.h b/runtime/nvn_obj.h index 662ba149..7b81c6ec 100644 --- a/runtime/nvn_obj.h +++ b/runtime/nvn_obj.h @@ -48,6 +48,9 @@ typedef struct { } tex_builder_t; FITS(tex_builder_t, NVNtextureBuilder); typedef struct { uint32_t magic; nvn_texdesc d; uint8_t has_layout, layout[8]; + uint16_t pool_flags; // the pool's flags at Initialize (in the padding): a game may finalize and free + // the pool object while the back end still looks the texture up, and the + // driver keeps no pointer to it either nvn_texlayout L; uint8_t *mem; pool_t *pool; int64_t off; uint64_t serial; // Initialization serial: the back end uses it to find its texture in the host table (the game may copy the object by value, so object pointers cannot be stored) } tex_t; FITS(tex_t, NVNtexture); diff --git a/runtime/vk_exec_probe.c b/runtime/vk_exec_probe.c index 3663e556..f3011c43 100644 --- a/runtime/vk_exec_probe.c +++ b/runtime/vk_exec_probe.c @@ -175,7 +175,7 @@ static void init_tex(tex_t *t, pool_t *pool, int64_t off, int format) { t->d.target = NVN_TEXTURE_TARGET_2D, t->d.format = format, t->d.width = TEX_W, t->d.height = TEX_H; t->d.depth = 1, t->d.levels = 1; nvn_tex_layout(&t->d, &t->L); - t->pool = pool, t->off = off, t->mem = pool->mem + off, t->serial = 100 + (uint64_t)off; + t->pool = pool, t->pool_flags = (uint16_t)pool->flags, t->off = off, t->mem = pool->mem + off, t->serial = 100 + (uint64_t)off; } // Pixel of the RGBA8 target after the stream: the scissored second clear inside the rectangle, the first elsewhere. diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 2a57ca68..995c613d 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -166,8 +166,7 @@ static int same_but_swizzle(const TexKey *a, const TexKey *b) { static int tex_mirrored(const tex_t *t) { if (!t->mem || nvn_gpu_only((uintptr_t)t->mem)) return 0; if (t->has_layout) return 1; - const pool_t *p = t->pool; - if (!p || !(p->flags & (NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED | NVN_MEMORY_POOL_FLAGS_CPU_CACHED))) return 0; + if (!(t->pool_flags & (NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED | NVN_MEMORY_POOL_FLAGS_CPU_CACHED))) return 0; if (t->d.flags & (NVN_TEXTURE_FLAGS_DISPLAY | NVN_TEXTURE_FLAGS_COMPRESSIBLE | NVN_TEXTURE_FLAGS_ADAPTIVE_ZCULL | NVN_TEXTURE_FLAGS_DEPTH16_PREFER_FAST_CLEAR | NVN_TEXTURE_FLAGS_ZCULL_SUPPORT_STENCIL)) return 0;