diff --git a/runtime/vk.h b/runtime/vk.h index 3a30a322..750bd2c5 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -687,6 +687,8 @@ VkDescriptorSetLayout vk_empty_set_layout(void); // set 1 of layouts without // image, which it returns for the caller to blit into the target; VK_NULL_HANDLE when it cannot. edge: 1 lets it scale // with the edge-adaptive filter (NX2NAVI_STANDIN_FILTER), 0 keeps the linear one (another upscaler's picture replaces // this one). The caller binds its own compute pipeline again afterwards. +// The tonemap pass's views of img go (vk_defer_view): img is destroyed or replaced. Any thread. +void vk_tonemap_image_gone(VkImage img); VkImage vk_tonemap_record(VkCommandBuffer cb, VkImage src, VkFormat sf, uint32_t sw, uint32_t sh, uint32_t dw, uint32_t dh, int edge); // What vk_texture_by_id(id) returns can change only when this does: the slot is registered again, or a texture entry at diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 50c62df2..1169af3a 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -920,6 +920,7 @@ VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageV } int vk_exec_image_gone(VkImage image) { + vk_tonemap_image_gone(image); // its views in the tonemap pass, keyed by the handle a new image may get vk_exec_bc_gone(image); vk_hdr_image_gone(image); if (!views_by_image.klen) return 0; @@ -6827,12 +6828,38 @@ static int fmt_rgba8(VkFormat f) { f == VK_FORMAT_B8G8R8A8_SRGB || f == VK_FORMAT_A8B8G8R8_UNORM_PACK32 || f == VK_FORMAT_A8B8G8R8_SRGB_PACK32; } -// Scratch images for those blits, one per (size, format), kept for the life of the process (a handful). +// Scratch images for those blits, one per (size, format). Up to FB_SCRATCH are kept; a new size past them takes the +// place of the one used longest ago, which goes once the GPU is past the submission being recorded (vk_defer_image), +// with the tonemap pass's view of it. Before, the first eight sizes were kept for the process and a ninth got no +// scratch, so its blit was left out: the sizes come from the scene's resolution, which DLSS modes, the dynamic +// resolution and a window or picture size change move, at up to 32 MiB each at 2160 lines. One used in the +// submission being recorded is never the one replaced: the blit may have recorded it already. +#define FB_SCRATCH 8 static VkImage fallback_scratch(VkExec *ex, uint32_t w, uint32_t h, VkFormat f) { - static struct { uint32_t w, h; VkFormat f; VkImage img; VmaAllocation a; } cache[8]; + static struct { uint32_t w, h; VkFormat f; VkImage img; VmaAllocation a; uint64_t used; } cache[FB_SCRATCH]; static int n; - for (int i = 0; i < n; i++) if (cache[i].w == w && cache[i].h == h && cache[i].f == f) return cache[i].img; - if (n == 8) return VK_NULL_HANDLE; + static PlatMutex lock = PLAT_MUTEX_INIT; // replay workers record blits side by side + plat_mutex_lock(&lock); + for (int i = 0; i < n; i++) + if (cache[i].w == w && cache[i].h == h && cache[i].f == f) { + cache[i].used = ex->fence; + VkImage img = cache[i].img; + plat_mutex_unlock(&lock); + return img; + } + int slot = n; + if (n == FB_SCRATCH) { // the one used longest ago, unless the submission being recorded uses it + slot = -1; + for (int i = 0; i < n; i++) + if (cache[i].used < ex->fence && (slot < 0 || cache[i].used < cache[slot].used)) slot = i; + if (slot < 0) { + plat_mutex_unlock(&lock); + return VK_NULL_HANDLE; + } + vk_tonemap_image_gone(cache[slot].img); + vk_defer_image(cache[slot].img, cache[slot].a); + cache[slot].img = VK_NULL_HANDLE; + } VkCtx *c = vk_ctx(); VkImageCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = f, .extent = {w, h, 1}, .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, @@ -6842,7 +6869,11 @@ static VkImage fallback_scratch(VkExec *ex, uint32_t w, uint32_t h, VkFormat f) .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED}; VmaAllocationCreateInfo ia = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; VkImage img; VmaAllocation a; - if (vmaCreateImage(c->vma, &ii, &ia, &img, &a, NULL) != VK_SUCCESS) return VK_NULL_HANDLE; + if (vmaCreateImage(c->vma, &ii, &ia, &img, &a, NULL) != VK_SUCCESS) { + if (slot < n) cache[slot] = cache[--n]; // the replaced entry's place stays empty + plat_mutex_unlock(&lock); + return VK_NULL_HANDLE; + } VkImageMemoryBarrier2 bar = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, .dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT, @@ -6851,7 +6882,10 @@ static VkImage fallback_scratch(VkExec *ex, uint32_t w, uint32_t h, VkFormat f) .image = img, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &bar}; vkCmdPipelineBarrier2(ex->cb, &di); - cache[n].w = w, cache[n].h = h, cache[n].f = f, cache[n].img = img, cache[n].a = a, n++; + cache[slot].w = w, cache[slot].h = h, cache[slot].f = f, cache[slot].img = img, cache[slot].a = a; + cache[slot].used = ex->fence; + if (slot == n) n++; + plat_mutex_unlock(&lock); return img; } diff --git a/runtime/vk_tonemap.c b/runtime/vk_tonemap.c index 5b18b6e6..452447c8 100644 --- a/runtime/vk_tonemap.c +++ b/runtime/vk_tonemap.c @@ -1,5 +1,6 @@ // Decode-and-scale pass standing in for the scene upscaler (vk.h vk_tonemap_record; used by vk_exec_draw.c // untranslated_dispatch_fallback). +#include "platform/platform.h" #include "vk.h" #include @@ -161,19 +162,41 @@ static void tm_filter_log(int easu, int edge, uint32_t sw, uint32_t sh, uint32_t : "the edge-adaptive pass is not there"); } -// Views of the scratch images the pass samples, kept for the process (the scratch images live as long). +// Views of the images the pass samples: the fallback blit's scratch images, and a game image the pass reads as it is. +// A view goes with its image (vk_tonemap_image_gone: the scratch image's replacement, vk_exec_image_gone for a game +// image), so a new image that gets the old handle never finds the old image's view. Kept before for the process, with +// a game image's view left naming a destroyed image, and no view at all past sixteen. +static PlatMutex tm_view_lock = PLAT_MUTEX_INIT; // the replay records, a texture's destroy (any thread) takes views away + static VkImageView tm_view(VkImage img, VkFormat f) { + plat_mutex_lock(&tm_view_lock); for (int i = 0; i < TM.nviews; i++) - if (TM.views[i].img == img && TM.views[i].f == f) return TM.views[i].view; - if (TM.nviews == 16) return VK_NULL_HANDLE; + if (TM.views[i].img == img && TM.views[i].f == f) { + VkImageView v = TM.views[i].view; + plat_mutex_unlock(&tm_view_lock); + return v; + } + VkImageView v = VK_NULL_HANDLE; VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = img, .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = f, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; - VkImageView v; - if (vkCreateImageView(vk_ctx()->device, &vi, NULL, &v) != VK_SUCCESS) return VK_NULL_HANDLE; - TM.views[TM.nviews].img = img, TM.views[TM.nviews].f = f, TM.views[TM.nviews].view = v, TM.nviews++; + if (TM.nviews < 16 && vkCreateImageView(vk_ctx()->device, &vi, NULL, &v) == VK_SUCCESS) + TM.views[TM.nviews].img = img, TM.views[TM.nviews].f = f, TM.views[TM.nviews].view = v, TM.nviews++; + plat_mutex_unlock(&tm_view_lock); return v; } +void vk_tonemap_image_gone(VkImage img) { + plat_mutex_lock(&tm_view_lock); + for (int i = 0; i < TM.nviews;) + if (TM.views[i].img == img) { + vk_defer_view(TM.views[i].view); // a submission being recorded may sample it + TM.views[i] = TM.views[--TM.nviews]; + } else { + i++; + } + plat_mutex_unlock(&tm_view_lock); +} + // Records the pass from src (sampled, sw x sh) into the pass's own RGBA16F image of dw x dh and returns it (the caller // blits it into the target); VK_NULL_HANDLE when it cannot (the caller blits the source instead). edge: the picture is // what the screen shows, so the edge-adaptive filter may scale it (0: another upscaler's picture replaces it).