mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 04:59:57 -04:00
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:
@@ -662,6 +662,11 @@ bytes goes first, what moves fits the free bytes outside the chosen blocks, a pl
|
||||
and 400 random tables keep those rules. The part of `runtime/vk_texture.c` that builds the table and moves the images
|
||||
(`NX2NAVI_TEX_COMPACT`) is not built by it. No GPU, no game files.
|
||||
|
||||
A change to which entry of the upscaler stand-in's scratch images serves a new size (`runtime/vk_scratch_lru.h`,
|
||||
`runtime/vk_exec_draw.c` `fallback_scratch`) passes `python tests\scratch_lru_check.py`, which builds the header alone
|
||||
and checks that a match is a hit, a ninth size takes a free entry or the one used longest ago instead of being
|
||||
refused, and two images of one frame never evict each other. No GPU, no game files.
|
||||
|
||||
A change to which frames FSR 3.1 frame generation is told are camera cuts (`runtime/vk_fg_reset.h`, `runtime/vk_fg.cpp`
|
||||
`generate` and `vk_fg_frame`) passes `python tests\fg_reset_check.py`, which builds the header alone and checks that each
|
||||
frame id gets its own frame's cut, that an id never noted or no longer held counts as a cut, and that a slot rewritten
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
38
runtime/vk_scratch_lru.h
Normal 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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
59
tests/scratch_lru_check.c
Normal file
59
tests/scratch_lru_check.c
Normal file
@@ -0,0 +1,59 @@
|
||||
// runtime/vk_scratch_lru.h: which entry of the stand-in's scratch image cache serves a request (vk_exec_draw.c
|
||||
// fallback_scratch). Built and run by tests/scratch_lru_check.py; prints each failed check and exits 1 on any.
|
||||
// The old cache kept its first eight sizes and refused the ninth: the sizes below are those a run walks through when
|
||||
// the window and the DLSS mode change a few times.
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "vk_scratch_lru.h"
|
||||
|
||||
static int fails;
|
||||
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
|
||||
|
||||
#define N 8
|
||||
static ScrEntry e[N];
|
||||
|
||||
// A request as fallback_scratch makes it: a hit returns its entry, a miss fills the entry it was given (after freeing
|
||||
// the evicted image). Returns the entry; *was_hit, *was_evict as scr_pick said.
|
||||
static int request(uint32_t w, uint32_t h, uint32_t f, uint64_t frame, int *was_hit, int *was_evict) {
|
||||
int hit, evict, i = scr_pick(e, N, w, h, f, frame, &hit, &evict);
|
||||
if (!hit) e[i] = (ScrEntry){w, h, f, frame, 1};
|
||||
if (was_hit) *was_hit = hit;
|
||||
if (was_evict) *was_evict = evict;
|
||||
return i;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int hit, evict;
|
||||
// ---- empty: free entries in order, nothing evicted
|
||||
for (int k = 0; k < N; k++) {
|
||||
int i = request(1920 + (uint32_t)k, 1080, 37, (uint64_t)k + 1, &hit, &evict);
|
||||
CHECK(i == k && !hit && !evict, "size %d took entry %d (hit %d, evict %d)", k, i, hit, evict);
|
||||
}
|
||||
// ---- a match is a hit and refreshes its frame
|
||||
int i = request(1923, 1080, 37, 100, &hit, &evict);
|
||||
CHECK(i == 3 && hit && !evict && e[3].used == 100, "the match was entry %d (hit %d, evict %d, used %llu)", i, hit,
|
||||
evict, (unsigned long long)e[3].used);
|
||||
// same size, another format: not a match
|
||||
i = request(1923, 1080, 43, 101, &hit, &evict);
|
||||
CHECK(!hit && evict, "another format matched (hit %d, evict %d)", hit, evict);
|
||||
// ---- the ninth size: the one used longest ago goes (entry 0, frame 1), not the full cache refusing it
|
||||
CHECK(i == 0, "the other format evicted entry %d, not the oldest (0)", i);
|
||||
i = request(5120, 2142, 37, 102, &hit, &evict);
|
||||
CHECK(i == 1 && evict, "the next new size evicted entry %d (evict %d), not entry 1", i, evict);
|
||||
// the refreshed entry 3 (frame 100) outlives the untouched ones
|
||||
for (int k = 0; k < 4; k++) request(800 + (uint32_t)k, 600, 37, 103 + (uint64_t)k, &hit, &evict);
|
||||
CHECK(e[3].live && e[3].w == 1923 && e[3].f == 37, "entry 3, used at frame 100, was evicted before older ones");
|
||||
// ---- two images in one frame never evict each other while older entries remain (g8 then g8s of one dispatch)
|
||||
int a = request(2560, 1440, 37, 200, &hit, &evict);
|
||||
int b = request(2560, 1440, 43, 200, &hit, &evict);
|
||||
CHECK(a != b && e[a].live && e[a].w == 2560 && e[a].f == 37, "the second image of a frame took the first's entry");
|
||||
CHECK(request(2560, 1440, 37, 201, &hit, NULL) == a && hit, "the first image of the pair was not kept");
|
||||
// ---- a freed entry (an image that failed to be made stays not live) is taken before any eviction
|
||||
e[5].live = 0;
|
||||
i = request(640, 360, 37, 300, &hit, &evict);
|
||||
CHECK(i == 5 && !hit && !evict, "a free entry was passed over (entry %d, evict %d)", i, evict);
|
||||
if (fails) return printf("scratch_lru_check: %d checks failed\n", fails), 1;
|
||||
printf("scratch_lru_check: all checks passed\n");
|
||||
return 0;
|
||||
}
|
||||
40
tests/scratch_lru_check.py
Normal file
40
tests/scratch_lru_check.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Build and run tests/scratch_lru_check.c: runtime/vk_scratch_lru.h, which entry of the upscaler stand-in's scratch
|
||||
image cache serves a new size (vk_exec_draw.c fallback_scratch): a free one, else the one used longest ago.
|
||||
|
||||
No GPU, no game files, any Python.
|
||||
|
||||
Usage: scratch_lru_check.py
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
||||
|
||||
|
||||
def compiler():
|
||||
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn)."""
|
||||
if os.environ.get('CC'):
|
||||
return os.environ['CC']
|
||||
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
||||
return str(MSYS_BIN / 'clang.exe')
|
||||
return shutil.which('clang') or 'clang'
|
||||
|
||||
|
||||
def main():
|
||||
work = ROOT / 'build' / 'test' / 'scratch_lru'
|
||||
work.mkdir(parents=True, exist_ok=True)
|
||||
exe = work / ('scratch_lru_check.exe' if os.name == 'nt' else 'scratch_lru_check')
|
||||
env = dict(os.environ)
|
||||
if os.name == 'nt' and MSYS_BIN.exists():
|
||||
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
||||
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
|
||||
str(ROOT / 'tests' / 'scratch_lru_check.c'), '-o', str(exe)], env=env)
|
||||
return subprocess.run([str(exe)], env=env).returncode
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user