Let the upscaler stand-in's scratch images make room for a new size instead of refusing it.

The cache kept the first eight sizes and formats for the whole run and refused every other one, so after a few
window, resolution or DLSS mode changes the stand-in's blit of a new size was left out, while the old sizes' images
stayed. A new size now takes a free entry or the one used longest ago, whose image goes through the deferred
destruction after the tonemap pass drops its views of it (vk_tonemap_forget), before another image can take its
handle. tests/scratch_lru_check.py checks the choice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-12 23:17:36 -05:00
parent d77572c69c
commit a4310491de
7 changed files with 173 additions and 7 deletions

View File

@@ -702,6 +702,9 @@ VkDescriptorSetLayout vk_empty_set_layout(void); // set 1 of layouts without
// this one). The caller binds its own compute pipeline again afterwards.
VkImage vk_tonemap_record(VkCommandBuffer cb, VkImage src, VkFormat sf, uint32_t sw, uint32_t sh, uint32_t dw,
uint32_t dh, int edge);
// A source image of the stand-in goes (vk_exec_draw.c fallback_scratch): its views go through the deferred destruction,
// before another image can take its handle. Called on the replaying thread, as vk_tonemap_record.
void vk_tonemap_forget(VkImage src);
// What vk_texture_by_id(id) returns can change only when this does: the slot is registered again, or a texture entry at
// its storage address is created, dropped, released or taken back into use. Unlocked; read it before the lookup it
// guards (a lookup may create an entry). 0 when the slot was never registered: nothing is kept under 0.

View File

@@ -32,6 +32,7 @@
#include "vk_gpa.h"
#include "mesh_fb.h"
#include "vk_mesh_fb.h"
#include "vk_scratch_lru.h"
#include "platform/platform.h"
#include "vk_dlaa.h"
#include "mesh_emu_spirv.h"
@@ -6827,12 +6828,24 @@ 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), at most SCRATCH_N; a new size takes the place of the one used
// longest ago (vk_scratch_lru.h), whose image goes through the deferred destruction.
#define SCRATCH_N 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 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 ScrEntry ent[SCRATCH_N];
static struct { VkImage img; VmaAllocation a; } cache[SCRATCH_N];
static int evictions;
int hit, evict, i = scr_pick(ent, SCRATCH_N, w, h, (uint32_t)f,
__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), &hit, &evict);
if (hit) return cache[i].img;
if (evict) {
if (evictions++ < 8)
rt_log("vk: a stand-in scratch image of %ux%u (format %d) makes room for one of %ux%u (format %d)",
ent[i].w, ent[i].h, (int)ent[i].f, w, h, (int)f);
vk_tonemap_forget(cache[i].img); // the stand-in's views of it, before a new image can take its handle
vk_defer_image(cache[i].img, cache[i].a);
ent[i].live = 0, cache[i].img = VK_NULL_HANDLE, cache[i].a = NULL;
}
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,
@@ -6851,7 +6864,8 @@ 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++;
ent[i] = (ScrEntry){w, h, (uint32_t)f, __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), 1};
cache[i].img = img, cache[i].a = a;
return img;
}

38
runtime/vk_scratch_lru.h Normal file
View File

@@ -0,0 +1,38 @@
// Which entry of a small cache of scratch images serves a request for an image of one size and format
// (vk_exec_draw.c fallback_scratch). Header only, so that tests/scratch_lru_check.c checks it without a device.
//
// The cache held its first SCRATCH_N images for the whole run and refused every other size: each scene size the
// stand-in of the game's upscaler blits through takes one, and after a run of window, resolution or DLSS mode
// changes the blit of a new size found no image and was left out. Now a request that matches no live entry takes a
// free one, else the one used longest ago, which the caller frees (deferred: a submission may still name it) before
// it makes the new image there. The images in use stay; one used in no frame for a while is what goes.
#pragma once
#include <stdint.h>
typedef struct {
uint32_t w, h, f; // the image's size and format (VkFormat)
uint64_t used; // the frame of its last request
int live; // holds an image
} ScrEntry;
// The entry for (w, h, f) among e[0..n-1]: a live match (*hit 1), else a free entry, else the live one used longest ago
// (*evict 1: its image is to be freed before the slot is reused). A hit's used becomes now; the caller fills a new
// entry once its image is made.
static inline int scr_pick(ScrEntry *e, int n, uint32_t w, uint32_t h, uint32_t f, uint64_t now, int *hit, int *evict) {
int free_at = -1, lru = -1;
*hit = *evict = 0;
for (int i = 0; i < n; i++) {
if (!e[i].live) {
if (free_at < 0) free_at = i;
continue;
}
if (e[i].w == w && e[i].h == h && e[i].f == f) {
e[i].used = now, *hit = 1;
return i;
}
if (lru < 0 || e[i].used < e[lru].used) lru = i;
}
if (free_at >= 0) return free_at;
*evict = 1;
return lru;
}

View File

@@ -161,7 +161,14 @@ 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 scratch images the pass samples, kept as long as the image: vk_tonemap_forget drops those of an image
// fallback_scratch frees, which also gives their places back.
void vk_tonemap_forget(VkImage src) {
for (int i = 0; i < TM.nviews;)
if (TM.views[i].img == src) vk_defer_view(TM.views[i].view), TM.views[i] = TM.views[--TM.nviews];
else i++;
}
static VkImageView tm_view(VkImage img, VkFormat f) {
for (int i = 0; i < TM.nviews; i++)
if (TM.views[i].img == img && TM.views[i].f == f) return TM.views[i].view;