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

@@ -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++)