Free a destroyed texture's record only after the GPU is past the submission being recorded, and cache no view of it.

A device lost seen on RTX 4090, 5090 and 5070 Ti cards in 0.5.0 and 0.6.0 (#177, #61, and a 0.6.0 test run): a
"read invalid" at the start of a 128x128 scene capture pass, re-rendered at area loads. destroy_tex purged the views of
the image and deferred the image, then freed its record at once, on whichever thread released the texture. The replay
keeps &e->pub as a render target for the rest of a command buffer (vk_exec.c resolve_target) and DLSS for the frame:
a pass that began again (each indirect mesh draw ends the pass) read the freed record and cached a view of the dead
image, which nothing removed. Vulkan gives a destroyed image's handle to a later image, which then got that view, and
the pass loaded its attachments from freed memory.
- destroy_tex marks the record gone and frees it through the deferred destruction (vk_defer_free, a host kind stamped
  with the submission being recorded, as the image is).
- A gone texture's attachment view is made per command buffer (vk_exec.c view_of) or deferred with it
  (vk_exec_target_view, for DLSS and the contact shadows), never cached.
- A new image drops any view still cached under its handle, and the log counts it ("cached views of a destroyed image").
No rendering changes: the views and images live exactly as long as before, the records slightly longer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-09 09:58:26 -05:00
parent 0101a87079
commit e3bfa3d40a
5 changed files with 51 additions and 12 deletions

View File

@@ -377,7 +377,10 @@ void vk_texstat_add(int kind, uint64_t n, uint64_t bytes);
// Deferred destruction: freed once the upload batches recorded so far and the timeline value last noted have completed.
void vk_defer_image(VkImage img, VmaAllocation alloc); // alloc NULL: vkDestroyImage only
void vk_defer_view(VkImageView view);
void vk_exec_image_gone(VkImage image); // destroy_tex, texture lock held: the replay's views of image go with it
// free(p) once the GPU is past the submission being recorded, as vk_defer_image: a host record other threads may still
// read (a destroyed texture's, vk_texture.c destroy_tex).
void vk_defer_free(void *p);
int vk_exec_image_gone(VkImage image); // texture lock held: the replay's views of image go with it; returns how many
void vk_defer_buffer(VkBuffer buf, VmaAllocation alloc);
// ---- ASTC (vk_astc.c). NX2NAVI_ASTC = auto | gpu | native | cpu | check; see vk_astc.c.
enum { VK_ASTC_CPU, VK_ASTC_GPU, VK_ASTC_NATIVE, VK_ASTC_CHECK };
@@ -437,6 +440,9 @@ typedef struct {
uint8_t scale; // 1, or NX2NAVI_SHADOW_SCALE for a shadow map made larger than the game's (vk_texture.c
// shadow_scale_of): width and height above are the larger ones, the game's regions
// are multiplied by it
uint8_t gone; // destroyed (vk_texture.c destroy_tex, texture lock held): the record stays readable until
// the GPU is past the submission being recorded (vk_defer_free), as the replay and DLSS
// keep pointers to render targets across a command buffer; no view of it is cached
} VkTexImage;
// The window texture the backend presents (vk_backend.c): *image and *format become the float picture the game copied
// into it, when the last draw into it was that copy and the swapchain is HDR10.

View File

@@ -438,9 +438,10 @@ static void resolve_rt(VkExec *ex) {
}
// The attachment view of a target. A registered texture's view lives as long as its image (vk_exec_target_view); any
// other image's view is made for this command buffer and freed at its fence.
// other image's view, and that of a texture destroyed since the target was resolved (gone: its record is freed only
// after this submission, vk_defer_free), is made for this command buffer and freed at its fence.
static VkImageView view_of(VkExec *ex, const VkExecTarget *t) {
if (t->img && t->img->image == t->image)
if (t->img && t->img->image == t->image && !t->img->gone)
return vk_exec_target_view(t->img, t->format, t->view_type, t->aspect, t->level, t->base_layer, t->layers, t->blocks);
ViewEnt *v = ex->views;
for (int i = 0; i < ex->nviews; i++)

View File

@@ -701,10 +701,14 @@ static VkImageView get_view(VkExec *ex, const VkTexImage *img, const view_t *v,
// selectors, each below 8). The texture lock guards the tables, as for get_view; vk_exec_pass_begin may already hold it.
VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageViewType type, uint32_t aspect,
uint32_t level, uint32_t base, uint32_t layers, int blocks) {
vk_texture_lock(); // destroy_tex sets gone and purges the image's views with it held
ViewKey k = {img->image, format, type, aspect, level, 1, base, layers, blocks ? 0xfffffffeu : 0xffffffffu};
vk_texture_lock();
if (!view_of.klen) nvn_hm_init(&view_of, sizeof(ViewKey)), nvn_hm_init(&views_by_image, sizeof(VkImage));
VkImageView view = (VkImageView)nvn_hm_get(&view_of, &k);
// A texture destroyed while a command buffer or DLSS still held it (img->gone): its image lives on until the GPU is
// past the submission being recorded (vk_defer_image), so a view for this use is made, and goes the same way. Cached,
// it outlived the image: Vulkan hands the handle to a later image, which then got the view of the dead image's memory
// and the GPU faulted reading it at the start of a pass ("read invalid", a 128x128 scene capture, #177).
VkImageView view = img->gone ? VK_NULL_HANDLE : (VkImageView)nvn_hm_get(&view_of, &k);
if (!view) {
VkImageViewCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = img->image,
.viewType = type, .format = format, .subresourceRange = {aspect, level, 1, base, layers}};
@@ -713,6 +717,11 @@ VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageV
if (blocks) ci.pNext = &use; // not the BC image's sampling and transfer usages
if (vkCreateImageView(vk_ctx()->device, &ci, NULL, &view) != VK_SUCCESS)
rt_fatal("vk: render target view (format %d) could not be created", (int)format);
if (img->gone) {
vk_defer_view(view);
vk_texture_unlock();
return view;
}
ViewNode *n = malloc(sizeof *n);
if (!n) rt_fatal("vk: out of memory");
*n = (ViewNode){nvn_hm_get(&views_by_image, &img->image), k, view};
@@ -723,16 +732,18 @@ VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageV
return view;
}
void vk_exec_image_gone(VkImage image) {
int vk_exec_image_gone(VkImage image) {
vk_exec_bc_gone(image);
vk_hdr_image_gone(image);
if (!views_by_image.klen) return;
for (ViewNode *n = nvn_hm_del(&views_by_image, &image), *next; n; n = next) {
if (!views_by_image.klen) return 0;
int purged = 0;
for (ViewNode *n = nvn_hm_del(&views_by_image, &image), *next; n; n = next, purged++) {
next = n->next;
nvn_hm_del(&view_of, &n->k);
vk_defer_view(n->view);
free(n);
}
return purged;
}
// ---- program to shaders

View File

@@ -2615,7 +2615,7 @@ typedef struct {
VkDeviceSize chunk_used; // bytes of the last chunk handed out
} Batch;
enum { DEAD_IMAGE = 1, DEAD_IMAGE_VMA, DEAD_VIEW, DEAD_BUFFER_VMA };
enum { DEAD_IMAGE = 1, DEAD_IMAGE_VMA, DEAD_VIEW, DEAD_BUFFER_VMA, DEAD_HOST };
typedef struct { int kind; uint64_t handle; VmaAllocation alloc; uint64_t seq, tl; } Dead;
static PlatRMutex cmd_lock;
@@ -2728,6 +2728,10 @@ static void collect_locked(VkCtx *c) {
for (int i = 0; i < ndead; i++) {
Dead *d = &dead[i];
if (d->seq > done_seq || d->tl > tl) { dead[k++] = *d; continue; }
if (d->kind == DEAD_HOST) { // not a Vulkan object: nothing for the recorder's handle check
free((void *)(uintptr_t)d->handle);
continue;
}
rec_check_handle(d->kind == DEAD_VIEW ? REC_VIEW : d->kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, d->handle, 1,
d->tl, tl);
switch (d->kind) {
@@ -2937,8 +2941,9 @@ static void defer(int kind, uint64_t handle, VmaAllocation alloc) {
if (stamp && !rf) rf = vk_sync_issued() + 1;
if (rf > tl) tl = rf;
dead[ndead++] = (Dead){kind, handle, alloc, open_active ? open_b.seq : next_seq, tl};
rec_check_handle(kind == DEAD_VIEW ? REC_VIEW : kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, handle, 0, tl,
vk_sync_fence_done());
if (kind != DEAD_HOST)
rec_check_handle(kind == DEAD_VIEW ? REC_VIEW : kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, handle, 0, tl,
vk_sync_fence_done());
plat_rmutex_unlock(&cmd_lock);
}
@@ -2954,6 +2959,7 @@ void vk_defer_image(VkImage img, VmaAllocation alloc) {
if (img) defer(alloc ? DEAD_IMAGE_VMA : DEAD_IMAGE, (uint64_t)(uintptr_t)img, alloc);
}
void vk_defer_view(VkImageView view) { if (view) defer(DEAD_VIEW, (uint64_t)(uintptr_t)view, NULL); }
void vk_defer_free(void *p) { if (p) defer(DEAD_HOST, (uint64_t)(uintptr_t)p, NULL); }
void vk_defer_buffer(VkBuffer buf, VmaAllocation alloc) {
if (buf) defer(DEAD_BUFFER_VMA, (uint64_t)(uintptr_t)buf, alloc);
}

View File

@@ -707,6 +707,16 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) {
e->bytes = own.size;
}
e->reusable = e->alloc && gpu_only;
// A new image's handle can be one a destroyed image had. Views are purged with the image (destroy_tex), so none
// should be cached under it: one that is was made from a destroyed texture's record after its purge, and would
// reach this image's memory through the dead one's. Dropped here, and counted in the log.
if (!e->reused) {
static volatile int32_t n_stale;
int stale = vk_exec_image_gone(img);
if (stale)
log_limited(&n_stale, "new image %#llx had %d cached views of a destroyed image with its handle; dropped",
(unsigned long long)(uintptr_t)img, stale);
}
VkTexImage *p = &e->pub;
p->image = img;
p->format = f.vk;
@@ -833,7 +843,12 @@ static void destroy_tex(Tex *e) {
if (e->pub.image) vk_exec_image_gone(e->pub.image); // none when another texture took it (create_tex, reuse)
vk_defer_image(e->pub.image, e->alloc);
free(e->snap);
free(e);
// The record goes once the GPU is past the submission being recorded, as its image: the replay keeps &e->pub as a
// render target for the rest of a command buffer (vk_exec.c resolve_target) and DLSS for the frame, and this runs on
// whichever thread released the texture. Freed here, a pass that began again read the freed record, cached a view of
// the dead image (vk_exec_target_view), and a later image given the same handle drew through it into freed memory.
e->pub.gone = 1;
vk_defer_free(e);
}
// Released entries with another descriptor at the same address are replaced; past RELEASED_MAX released entries the