mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-25 06:05:56 +00:00
The motion vectors follow the camera only, so a character, animal or held item that moves on its own drags a faint copy of itself through DLSS's history. TotK's G-buffer target 0 carries an object class in green: above 64/255 for characters, creatures and held items (Link 253, others 125, held items 160), 0 for terrain and 32 for foliage, checked in the field, in Goron City and on horseback. The motion-vector pass now also writes DLSS's bias mask (DLSS.Input.Bias.Current.Color.Mask) where such an object is now or was the frame before, read through the motion vector from the previous frame's class mask, so the history that holds the moving object is weighed down there. dlss_char_mask (0, 35, 65 or 100 percent, default 65; F1 > Screen > "DLSS character trails", live) sets the strength. A 640x360 pass of the G-buffer's shape, seen while loading, no longer replaces the scene's class buffer in a frame. The status line also prints after 300 frames, with the frames that had the mask.
871 lines
49 KiB
C
871 lines
49 KiB
C
// DLAA: NVIDIA DLSS at the scene's own size (TOTK_DLAA, totk.ini dlaa). NGX is loaded at run time from the driver's
|
|
// NGX core: on Windows _nvngx.dll, found through HKLM\SOFTWARE\NVIDIA Corporation\Global\NGXCore; on Linux
|
|
// libnvidia-ngx.so.1, which the NVIDIA driver installs on the library path. NGX loads the DLSS model from the folder of
|
|
// the executable: nvngx_dlss.dll on Windows, libnvidia-ngx-dlss.so.<version> on Linux. No NVIDIA SDK code or header is
|
|
// compiled in: the few NGX types used are declared here from the SDK's documented ABI, the way the Metroid Dread
|
|
// recomp's dlss.c reaches the core (read for behaviour only).
|
|
//
|
|
// The core's parameter object is a C++ class whose virtual functions are called through its vtable: the Set overloads
|
|
// in slots 0-7 and the Get overloads in 8-15. MSVC (the Windows core) lays out each group of overloads in reverse
|
|
// declaration order, the Itanium C++ ABI (the Linux core, built with GCC) in declaration order. vtable_ok checks the
|
|
// layout with a set-then-get round trip before anything relies on it.
|
|
//
|
|
// Where DLAA runs in the frame, the jitter and the motion vectors: see the sections below.
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <wchar.h>
|
|
|
|
#include "cfg.h"
|
|
#include "host.h"
|
|
#include "platform/platform.h"
|
|
#include "vk.h"
|
|
#include "vk_dlaa.h"
|
|
#include "vk_exec.h"
|
|
#include "vk_rec.h"
|
|
#include "dlaa_mv_spv.h"
|
|
|
|
typedef int NgxResult; // NVSDK_NGX_Result: 1 success, 0xBAD00000 | n failures
|
|
#define NGX_OK(r) (((unsigned)(r) & 0xFFF00000u) != 0xBAD00000u)
|
|
#define NGX_VERSION_API 0x15
|
|
#define NGX_FEATURE_SUPERSAMPLING 1
|
|
#define NGX_PERFQ_DLAA 5 // NVSDK_NGX_PerfQuality_Value: 0 performance, 1 balanced, 2 quality, 3 ultra performance
|
|
typedef struct NgxParam NgxParam;
|
|
typedef struct NgxHandle NgxHandle;
|
|
typedef struct { void *slot[17]; } NgxParamVtbl;
|
|
struct NgxParam { const NgxParamVtbl *vt; };
|
|
typedef struct { // NVSDK_NGX_ImageViewInfo_VK
|
|
VkImageView view;
|
|
VkImage image;
|
|
VkImageSubresourceRange range;
|
|
VkFormat format;
|
|
unsigned width, height;
|
|
} NgxImageView;
|
|
typedef struct { // NVSDK_NGX_Resource_VK
|
|
union { NgxImageView img; struct { VkBuffer buffer; unsigned bytes; } buf; } r;
|
|
int type; // 0 image view, 1 buffer
|
|
_Bool rw;
|
|
} NgxResourceVK;
|
|
|
|
typedef NgxResult (*PFN_Init)(unsigned long long app, const wchar_t *path, VkInstance, VkPhysicalDevice, VkDevice,
|
|
PFN_vkGetInstanceProcAddr, PFN_vkGetDeviceProcAddr, int version, const void *info);
|
|
typedef NgxResult (*PFN_ReqExt)(unsigned *ni, const char ***ie, unsigned *nd, const char ***de);
|
|
typedef NgxResult (*PFN_Params)(NgxParam **);
|
|
typedef NgxResult (*PFN_DestroyParams)(NgxParam *);
|
|
typedef NgxResult (*PFN_Create)(VkCommandBuffer, int feature, NgxParam *, NgxHandle **);
|
|
typedef NgxResult (*PFN_Evaluate)(VkCommandBuffer, const NgxHandle *, const NgxParam *, void *callback);
|
|
typedef NgxResult (*PFN_Release)(NgxHandle *);
|
|
|
|
static struct {
|
|
void *core; // the NGX core library (an HMODULE on Windows)
|
|
PFN_Init init;
|
|
PFN_ReqExt req_ext;
|
|
PFN_Params caps, alloc_params;
|
|
PFN_DestroyParams destroy_params;
|
|
PFN_Create create;
|
|
PFN_Evaluate evaluate;
|
|
PFN_Release release;
|
|
int tried, loaded, available;
|
|
int wanted; // -1 not read yet
|
|
} N = {.wanted = -1};
|
|
|
|
typedef void (*SetI)(NgxParam *, const char *, int);
|
|
typedef void (*SetU)(NgxParam *, const char *, unsigned);
|
|
typedef void (*SetF)(NgxParam *, const char *, float);
|
|
typedef void (*SetP)(NgxParam *, const char *, void *);
|
|
typedef NgxResult (*GetI)(const NgxParam *, const char *, int *);
|
|
typedef NgxResult (*GetU)(const NgxParam *, const char *, unsigned *);
|
|
typedef NgxResult (*GetF)(const NgxParam *, const char *, float *);
|
|
typedef NgxResult (*GetP)(const NgxParam *, const char *, void **);
|
|
// The SDK declares Set(u64), Set(float), Set(double), Set(u32), Set(int), Set(ID3D11Resource *), Set(ID3D12Resource *),
|
|
// Set(void *), then the Get overloads in the same order, then Reset.
|
|
#ifdef _WIN32
|
|
enum { S_P = 0, S_D12, S_D11, S_I, S_U32, S_D, S_F, S_U64, G_P, G_D12, G_D11, G_I, G_U32, G_D, G_F, G_U64 };
|
|
#else
|
|
enum { S_U64 = 0, S_F, S_D, S_U32, S_I, S_D11, S_D12, S_P, G_U64, G_F, G_D, G_U32, G_I, G_D11, G_D12, G_P };
|
|
#endif
|
|
static void p_seti(NgxParam *p, const char *n, int v) { ((SetI)p->vt->slot[S_I])(p, n, v); }
|
|
static void p_setu(NgxParam *p, const char *n, unsigned v) { ((SetU)p->vt->slot[S_U32])(p, n, v); }
|
|
static void p_setf(NgxParam *p, const char *n, float v) { ((SetF)p->vt->slot[S_F])(p, n, v); }
|
|
static void p_setp(NgxParam *p, const char *n, void *v) { ((SetP)p->vt->slot[S_P])(p, n, v); }
|
|
static int p_geti(const NgxParam *p, const char *n, int *v) { return NGX_OK(((GetI)p->vt->slot[G_I])(p, n, v)); }
|
|
static int p_getu(const NgxParam *p, const char *n, unsigned *v) { return NGX_OK(((GetU)p->vt->slot[G_U32])(p, n, v)); }
|
|
static int p_getf(const NgxParam *p, const char *n, float *v) { return NGX_OK(((GetF)p->vt->slot[G_F])(p, n, v)); }
|
|
static int p_getp(const NgxParam *p, const char *n, void **v) { return NGX_OK(((GetP)p->vt->slot[G_P])(p, n, v)); }
|
|
|
|
#ifdef _WIN32
|
|
#define DLSS_MODEL "nvngx_dlss.dll"
|
|
#else
|
|
#define DLSS_MODEL "libnvidia-ngx-dlss.so.<version>"
|
|
#endif
|
|
|
|
// NGX is brought up only when dlaa is on at start: its device extensions can only be asked for at device creation, and
|
|
// loading the driver's NGX core on every NVIDIA machine would cost start time and change the device with DLAA off. So
|
|
// dlaa applies after a restart; switched off, nothing else of DLAA runs.
|
|
int vk_dlaa_wanted(void) {
|
|
static int wanted = -1;
|
|
if (wanted < 0) wanted = cfg_bool("TOTK_DLAA");
|
|
return wanted;
|
|
}
|
|
|
|
static int core_load(void) {
|
|
if (N.tried) return N.loaded;
|
|
N.tried = 1;
|
|
#ifdef _WIN32
|
|
char dir[MAX_PATH] = {0}, path[MAX_PATH + 32];
|
|
DWORD n = sizeof dir;
|
|
if (RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\NVIDIA Corporation\\Global\\NGXCore", "FullPath", RRF_RT_REG_SZ,
|
|
NULL, dir, &n) != ERROR_SUCCESS) {
|
|
rt_log("dlaa: the driver has no NGX core (registry NGXCore missing): DLAA off");
|
|
return 0;
|
|
}
|
|
snprintf(path, sizeof path, "%s\\_nvngx.dll", dir);
|
|
N.core = LoadLibraryA(path);
|
|
if (!N.core) {
|
|
rt_log("dlaa: cannot load %s (error %lu): DLAA off", path, GetLastError());
|
|
return 0;
|
|
}
|
|
#define FN(f, name) N.f = (void *)GetProcAddress((HMODULE)N.core, name)
|
|
#else
|
|
// The driver's NGX core; the dynamic loader finds it where the driver put it (/usr/lib, /usr/lib/x86_64-linux-gnu).
|
|
const char *path = "libnvidia-ngx.so.1";
|
|
N.core = plat_lib_open(path);
|
|
if (!N.core) {
|
|
rt_log("dlaa: cannot load %s (not an NVIDIA driver, or one without NGX): DLAA off", path);
|
|
return 0;
|
|
}
|
|
#define FN(f, name) N.f = plat_lib_sym(N.core, name)
|
|
#endif
|
|
FN(init, "NVSDK_NGX_VULKAN_Init_Ext2");
|
|
FN(req_ext, "NVSDK_NGX_VULKAN_RequiredExtensions");
|
|
FN(caps, "NVSDK_NGX_VULKAN_GetCapabilityParameters");
|
|
FN(alloc_params, "NVSDK_NGX_VULKAN_AllocateParameters");
|
|
FN(destroy_params, "NVSDK_NGX_VULKAN_DestroyParameters");
|
|
FN(create, "NVSDK_NGX_VULKAN_CreateFeature");
|
|
FN(evaluate, "NVSDK_NGX_VULKAN_EvaluateFeature");
|
|
FN(release, "NVSDK_NGX_VULKAN_ReleaseFeature");
|
|
#undef FN
|
|
if (!N.init || !N.req_ext || !N.caps || !N.alloc_params || !N.create || !N.evaluate || !N.release) {
|
|
rt_log("dlaa: the NGX core %s lacks Vulkan entry points: DLAA off", path);
|
|
return 0;
|
|
}
|
|
N.loaded = 1;
|
|
rt_log("dlaa: NGX core %s", path);
|
|
return 1;
|
|
}
|
|
|
|
static int listed(const char *const *list, uint32_t n, const char *s) {
|
|
for (uint32_t i = 0; i < n; i++)
|
|
if (!strcmp(list[i], s)) return 1;
|
|
return 0;
|
|
}
|
|
|
|
void vk_dlaa_instance_exts(const char **ext, uint32_t *n, uint32_t max) {
|
|
unsigned ni = 0, nd = 0;
|
|
const char **ie = NULL, **de = NULL;
|
|
if (!vk_dlaa_wanted() || !core_load() || !NGX_OK(N.req_ext(&ni, &ie, &nd, &de))) return;
|
|
for (unsigned i = 0; i < ni; i++)
|
|
if (!listed(ext, *n, ie[i]) && *n < max) ext[(*n)++] = ie[i];
|
|
}
|
|
|
|
void vk_dlaa_device_exts(const VkExtensionProperties *have, uint32_t nhave, const char **ext, uint32_t *n,
|
|
uint32_t max) {
|
|
unsigned ni = 0, nd = 0;
|
|
const char **ie = NULL, **de = NULL;
|
|
if (!vk_dlaa_wanted() || !core_load() || !NGX_OK(N.req_ext(&ni, &ie, &nd, &de))) return;
|
|
for (unsigned i = 0; i < nd; i++) {
|
|
int ok = 0;
|
|
for (uint32_t k = 0; k < nhave && !ok; k++) ok = !strcmp(have[k].extensionName, de[i]);
|
|
if (!ok) rt_log("dlaa: the device lacks %s, which NGX asks for", de[i]);
|
|
else if (!listed(ext, *n, de[i]) && *n < max) ext[(*n)++] = de[i];
|
|
}
|
|
}
|
|
|
|
static int vtable_ok(void) {
|
|
NgxParam *p = NULL;
|
|
if (!NGX_OK(N.alloc_params(&p)) || !p) return 0;
|
|
int i = 0;
|
|
unsigned u = 0;
|
|
float f = 0;
|
|
void *v = NULL;
|
|
p_seti(p, "TotK.Test.I", -12345);
|
|
p_setu(p, "TotK.Test.U", 4000000000u);
|
|
p_setf(p, "TotK.Test.F", 1.25f);
|
|
p_setp(p, "TotK.Test.P", (void *)p);
|
|
int ok = p_geti(p, "TotK.Test.I", &i) && i == -12345 && p_getu(p, "TotK.Test.U", &u) && u == 4000000000u &&
|
|
p_getf(p, "TotK.Test.F", &f) && f == 1.25f && p_getp(p, "TotK.Test.P", &v) && v == (void *)p;
|
|
if (N.destroy_params) N.destroy_params(p);
|
|
if (!ok) rt_log("dlaa: NGX parameter layout check failed (i=%d u=%u f=%g p=%p): DLAA off", i, u, f, v);
|
|
return ok;
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
// UTF-8 to UTF-32 wchar_t, without the C library's locale (the runtime never sets one, so mbstowcs would stop at the
|
|
// first non-ASCII byte). Returns 0 on malformed input or when out has no room.
|
|
static int utf8_to_wide(const char *in, wchar_t *out, size_t cap) {
|
|
const unsigned char *u = (const unsigned char *)in;
|
|
size_t n = 0;
|
|
while (*u) {
|
|
unsigned c = *u++, more = c < 0x80 ? 0 : c >= 0xF0 ? 3 : c >= 0xE0 ? 2 : c >= 0xC0 ? 1 : 4;
|
|
if (more == 4) return 0;
|
|
if (more) c &= 0x3F >> more;
|
|
for (unsigned k = 0; k < more; k++, u++) {
|
|
if ((*u & 0xC0) != 0x80) return 0;
|
|
c = c << 6 | (*u & 0x3F);
|
|
}
|
|
if (n + 1 >= cap) return 0;
|
|
out[n++] = (wchar_t)c;
|
|
}
|
|
out[n] = 0;
|
|
return 1;
|
|
}
|
|
#endif
|
|
|
|
void vk_dlaa_init(void) {
|
|
if (!vk_dlaa_wanted() || !core_load()) return;
|
|
#ifdef _WIN32
|
|
wchar_t wdir[MAX_PATH];
|
|
DWORD len = GetModuleFileNameW(NULL, wdir, MAX_PATH);
|
|
while (len && wdir[len - 1] != L'\\') len--;
|
|
wdir[len] = 0;
|
|
#else
|
|
// The executable's folder, where NGX looks for libnvidia-ngx-dlss.so and writes its logs, as a wchar_t string:
|
|
// 32-bit code points here, as the Linux core expects.
|
|
char dir[PLAT_PATH_MAX];
|
|
wchar_t wdir[PLAT_PATH_MAX];
|
|
char *cut = plat_exe_path(dir, sizeof dir) ? plat_last_sep(dir) : NULL;
|
|
if (!cut || !utf8_to_wide((cut[1] = 0, dir), wdir, PLAT_PATH_MAX)) {
|
|
rt_log("dlaa: cannot name the executable's folder for NGX: DLAA off");
|
|
return;
|
|
}
|
|
#endif
|
|
// 231313132: the application id NVIDIA's DLSS samples use for builds without an NVIDIA-issued id
|
|
const VkCtx *c = vk_ctx();
|
|
NgxResult r = N.init(231313132ull, wdir, c->instance, c->phys, c->device, vkGetInstanceProcAddr,
|
|
vkGetDeviceProcAddr, NGX_VERSION_API, NULL);
|
|
if (!NGX_OK(r)) {
|
|
rt_log("dlaa: NGX init failed (%08x): DLAA off", (unsigned)r);
|
|
return;
|
|
}
|
|
if (!vtable_ok()) return;
|
|
NgxParam *caps = NULL;
|
|
int avail = 0, driver = 0;
|
|
if (NGX_OK(N.caps(&caps)) && caps) {
|
|
p_geti(caps, "SuperSampling.Available", &avail);
|
|
p_geti(caps, "SuperSampling.NeedsUpdatedDriver", &driver);
|
|
}
|
|
N.available = avail != 0;
|
|
rt_log("dlaa: NGX up; DLSS %s%s", avail ? "available" : "NOT available (is " DLSS_MODEL " next to the executable?)",
|
|
driver ? "; the driver needs an update" : "");
|
|
}
|
|
|
|
int vk_dlaa_available(void) { return vk_dlaa_wanted() && N.available; }
|
|
|
|
// ---------------------------------------------------------------- where DLAA runs in TotK's frame
|
|
// Measured (TOTK_DUMP, frame 2100 at Lookout Landing, 2560x1440 scene): the G-buffer passes draw into five targets with
|
|
// the scene depth (d32f at the scene size), one of them the R11G11B10F light target; lighting and the transparents
|
|
// then draw into that light target alone, the last time with the scene depth attached; the first pass narrower than a
|
|
// quarter of the scene starts the bloom chain, which reads it. After the bloom come the tonemap and post passes, then
|
|
// the HUD is drawn into the same target and a last pass copies it to the 8-bit output.
|
|
// So: after the G-buffer, a pass at the scene size with exactly one colour target (R11G11B10F or RGBA16F) and the D32F
|
|
// depth arms the frame with that pair; the next pass at most a quarter as wide ends the scene (no jitter after it) and,
|
|
// when the scene is as wide as the output, runs DLAA on the armed target first. Bloom, tonemap, post and HUD then all
|
|
// work on the anti-aliased picture.
|
|
// Upscaling (a scene smaller than the output: TOTK_DLSS quality/balanced/performance, or the game's own 5/6): measured
|
|
// at a 960x536 scene, 1920x1080 output, the bloom and its composite write back into the light target at the scene size,
|
|
// then one full-screen draw (the FXAA shader below, which also scales) reads it into an output-sized R11G11B10F target
|
|
// with no depth, and the HUD is drawn over that target at the output size. DLSS runs at the begin of that pass: it
|
|
// reads the light target and its result is blitted into the pass's target, whose scaling draw is then skipped. The
|
|
// HUD, drawn afterwards into the same target, is never jittered nor passed through DLSS.
|
|
static struct {
|
|
VkExecTarget color, depth; // the armed pair, valid in the frame it was armed
|
|
int armed, done, gbuf; // this frame: armed on the light target, DLAA ran, the G-buffer was drawn
|
|
int ended, up; // this frame: the scene is drawn (jitter over), DLSS wrote the scaled output
|
|
struct Size { // a DLSS feature and its work images per scene size (dynamic resolution flips it)
|
|
uint32_t w, h, ow, oh; // render size, output size
|
|
VkImage out, mv;
|
|
VmaAllocation out_mem, mv_mem;
|
|
VkImageView out_view, mv_view;
|
|
VkImage mask, chars[2]; // the bias mask DLSS reads, the character class mask of this and last frame (R8)
|
|
VmaAllocation mask_mem, chars_mem[2];
|
|
VkImageView mask_view, chars_view[2];
|
|
int chars_cur, chars_ok; // chars[chars_cur] is written next; chars_ok: the other holds last frame's
|
|
NgxHandle *feat;
|
|
uint64_t used; // evals count when it last ran
|
|
} sizes[3], *last; // last: the one the previous DLAA frame ran with
|
|
VkImage last_color; // the target DLAA ran on last frame: another one means a new history
|
|
NgxParam *params;
|
|
int failed, reset;
|
|
uint64_t evals, frames_missed;
|
|
// jitter (vk_dlaa_viewport)
|
|
VkImage jdepth; // the scene depth of the last frame DLAA ran on: passes drawing with it are jittered
|
|
float jx, jy; // the offset the next scene draws take, render pixels (window x right, y down)
|
|
float ux, uy; // the offset this frame's scene draws took (DLSS and the motion vectors get these)
|
|
uint32_t jframe;
|
|
int jit_ok, jit_used, hold; // jitter allowed this frame / a draw used it / frames left without it
|
|
uint64_t holds;
|
|
// camera (vk_dlaa_draw) and motion vectors (mv_pass)
|
|
uint64_t cam_addr; // this frame's scene camera block (c4 of a scene draw's vertex stage), 0: none seen
|
|
VkBuffer camb; // two 256-byte slots: this frame's block and last frame's
|
|
VmaAllocation camb_mem;
|
|
int cam_slot, cam_prev_ok; // the slot this frame writes / the other slot holds last frame's camera
|
|
VkDescriptorSetLayout mv_dsl;
|
|
VkPipelineLayout mv_layout;
|
|
VkPipeline mv_pipe;
|
|
VkSampler mv_samp;
|
|
int mv_ok; // -1 not built, 0 failed, 1 ready
|
|
uint64_t mv_frames, mv_none, masked; // masked: frames with the characters' bias mask
|
|
uint64_t why[3]; // history resets: scene size changed, no camera, scene target changed
|
|
int on; // the dlaa setting, read at each present (the menu switches it live)
|
|
int scaler_skipped; // this frame: the game's scaling draw over DLSS's picture was skipped
|
|
uint64_t scaler_missed; // frames DLSS scaled whose scaling draw was not found (vk_dlaa_skip_draw)
|
|
int preset, preset_dirty; // the DLSS model features are created with (preset_value); a change pending
|
|
VkExecTarget ids; // this frame's G-buffer target 0 (object classes in green), valid with ids_ok
|
|
int ids_ok;
|
|
unsigned cfg_seen; // cfg_version when preset was read
|
|
uint64_t last_eval_ns; // plat_now_ns of the previous evaluation (FrameTimeDeltaInMsec)
|
|
} D = {.mv_ok = -1, .cfg_seen = ~0u};
|
|
|
|
static int dl_active(void) { return N.available && !D.failed && D.on; }
|
|
|
|
static void dl_fail(const char *what, NgxResult r) {
|
|
rt_log("dlaa: %s failed (%08x): DLAA off for this run", what, (unsigned)r);
|
|
D.failed = 1;
|
|
}
|
|
|
|
static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, VkImage *img, VmaAllocation *mem,
|
|
VkImageView *view) {
|
|
const 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,
|
|
.tiling = VK_IMAGE_TILING_OPTIMAL, .usage = usage,
|
|
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED};
|
|
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
|
|
if (vmaCreateImage(c->vma, &ii, &ai, img, mem, NULL) != VK_SUCCESS) return 0;
|
|
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}};
|
|
return vkCreateImageView(c->device, &vi, NULL, view) == VK_SUCCESS;
|
|
}
|
|
|
|
static void free_size(struct Size *e) {
|
|
const VkCtx *c = vk_ctx();
|
|
vkDeviceWaitIdle(c->device); // a third scene size: the evicted images may still be in flight
|
|
if (e->out_view) vkDestroyImageView(c->device, e->out_view, NULL);
|
|
if (e->mv_view) vkDestroyImageView(c->device, e->mv_view, NULL);
|
|
if (e->out) vmaDestroyImage(c->vma, e->out, e->out_mem);
|
|
if (e->mv) vmaDestroyImage(c->vma, e->mv, e->mv_mem);
|
|
if (e->mask_view) vkDestroyImageView(c->device, e->mask_view, NULL);
|
|
if (e->mask) vmaDestroyImage(c->vma, e->mask, e->mask_mem);
|
|
for (int i = 0; i < 2; i++) {
|
|
if (e->chars_view[i]) vkDestroyImageView(c->device, e->chars_view[i], NULL);
|
|
if (e->chars[i]) vmaDestroyImage(c->vma, e->chars[i], e->chars_mem[i]);
|
|
}
|
|
if (e->feat) N.release(e->feat);
|
|
memset(e, 0, sizeof *e);
|
|
}
|
|
|
|
// The entry for a scene and output size: its output (RGBA16F at the output size, written by DLSS, then blitted into
|
|
// the target) and motion vectors (RG16F at the scene size, render pixels), both kept in GENERAL like every image of the
|
|
// back end. TotK's dynamic resolution switches between a few sizes; each keeps its feature, the least recently used
|
|
// goes for a new one.
|
|
static struct Size *size_for(VkCommandBuffer cb, uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
|
|
struct Size *e = NULL, *lru = &D.sizes[0];
|
|
for (int i = 0; i < 3; i++) {
|
|
if (D.sizes[i].w == w && D.sizes[i].h == h && D.sizes[i].ow == ow && D.sizes[i].oh == oh) return &D.sizes[i];
|
|
if (!D.sizes[i].out && !e) e = &D.sizes[i];
|
|
if (D.sizes[i].used < lru->used) lru = &D.sizes[i];
|
|
}
|
|
if (!e) {
|
|
if (D.last == lru) D.last = NULL;
|
|
free_size(e = lru);
|
|
}
|
|
VkImageUsageFlags u = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
|
|
VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
|
VkImageUsageFlags u8 = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
if (!image2d(ow, oh, VK_FORMAT_R16G16B16A16_SFLOAT, u, &e->out, &e->out_mem, &e->out_view) ||
|
|
!image2d(w, h, VK_FORMAT_R16G16_SFLOAT, u, &e->mv, &e->mv_mem, &e->mv_view) ||
|
|
!image2d(w, h, VK_FORMAT_R8_UNORM, u8, &e->mask, &e->mask_mem, &e->mask_view) ||
|
|
!image2d(w, h, VK_FORMAT_R8_UNORM, u8, &e->chars[0], &e->chars_mem[0], &e->chars_view[0]) ||
|
|
!image2d(w, h, VK_FORMAT_R8_UNORM, u8, &e->chars[1], &e->chars_mem[1], &e->chars_view[1]))
|
|
return NULL;
|
|
VkImageMemoryBarrier2 ib[5];
|
|
VkImage im[5] = {e->out, e->mv, e->mask, e->chars[0], e->chars[1]};
|
|
for (int i = 0; i < 5; i++)
|
|
ib[i] = (VkImageMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
|
.srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
|
|
.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
|
|
.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT,
|
|
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = im[i],
|
|
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
|
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 5,
|
|
.pImageMemoryBarriers = ib};
|
|
vkCmdPipelineBarrier2(cb, &di);
|
|
e->w = w, e->h = h, e->ow = ow, e->oh = oh;
|
|
return e;
|
|
}
|
|
|
|
static void full_barrier(VkCommandBuffer cb) {
|
|
VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
|
|
.srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
|
|
.srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT,
|
|
.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
|
|
.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT};
|
|
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb};
|
|
vkCmdPipelineBarrier2(cb, &di);
|
|
}
|
|
|
|
static NgxResourceVK res_image(VkImageView v, VkImage img, VkFormat f, uint32_t w, uint32_t h, VkImageAspectFlags a,
|
|
int rw) {
|
|
NgxResourceVK r;
|
|
memset(&r, 0, sizeof r);
|
|
r.r.img = (NgxImageView){v, img, {a, 0, 1, 0, 1}, f, w, h};
|
|
r.type = 0;
|
|
r.rw = rw;
|
|
return r;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- motion vectors
|
|
// Camera motion only (runtime/shaders/dlaa_mv.comp): this frame's scene camera block is copied out of the replay's
|
|
// uniform snapshot (vk_usnap.c, as of the submit call) into one slot of camb, last frame's stays in the other, and the
|
|
// compute pass reprojects every pixel's depth through both. Characters and anything else that moves on its own get
|
|
// the camera's motion only, which DLSS mostly copes with by rejecting their history.
|
|
#define CAM_BYTES 256u // vec4 rows 0-15: view, view-projection (3-6), projection, inverse view
|
|
|
|
static int mv_init(void) {
|
|
if (D.mv_ok >= 0) return D.mv_ok;
|
|
D.mv_ok = 0;
|
|
const VkCtx *c = vk_ctx();
|
|
VkDescriptorSetLayoutBinding b[8] = {
|
|
{0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL},
|
|
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL},
|
|
{2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL},
|
|
{3, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL},
|
|
{4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL},
|
|
{5, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL},
|
|
{6, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL},
|
|
{7, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}};
|
|
VkDescriptorSetLayoutCreateInfo dci = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
|
|
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
|
|
.bindingCount = 8, .pBindings = b};
|
|
VkPushConstantRange pr = {VK_SHADER_STAGE_COMPUTE_BIT, 0, 24};
|
|
VkSamplerCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = VK_FILTER_NEAREST,
|
|
.minFilter = VK_FILTER_NEAREST, .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
|
|
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
|
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
|
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE};
|
|
VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = sizeof dlaa_mv_spv,
|
|
.pCode = dlaa_mv_spv};
|
|
VkShaderModule m = VK_NULL_HANDLE;
|
|
VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = 2 * CAM_BYTES,
|
|
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT};
|
|
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
|
|
if (vkCreateDescriptorSetLayout(c->device, &dci, NULL, &D.mv_dsl) != VK_SUCCESS) goto fail;
|
|
VkPipelineLayoutCreateInfo li = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1,
|
|
.pSetLayouts = &D.mv_dsl, .pushConstantRangeCount = 1, .pPushConstantRanges = &pr};
|
|
if (vkCreatePipelineLayout(c->device, &li, NULL, &D.mv_layout) != VK_SUCCESS ||
|
|
vkCreateSampler(c->device, &si, NULL, &D.mv_samp) != VK_SUCCESS ||
|
|
vkCreateShaderModule(c->device, &mi, NULL, &m) != VK_SUCCESS ||
|
|
vmaCreateBuffer(c->vma, &bi, &ai, &D.camb, &D.camb_mem, NULL) != VK_SUCCESS)
|
|
goto fail;
|
|
VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
|
|
.stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
.stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = m, .pName = "main"},
|
|
.layout = D.mv_layout};
|
|
VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &D.mv_pipe);
|
|
vkDestroyShaderModule(c->device, m, NULL);
|
|
if (r != VK_SUCCESS) goto fail;
|
|
return D.mv_ok = 1;
|
|
fail:
|
|
rt_log("dlaa: the motion-vector pass could not be set up: DLAA runs with zero motion");
|
|
return 0;
|
|
}
|
|
|
|
// The pass, recorded through the recorder like the replay's own commands. 0: no camera this frame (the vectors stay
|
|
// zero and DLSS starts a new history). bias > 0 with this frame's G-buffer classes (D.ids) also writes e->mask, the
|
|
// bias mask (dlaa_mv.comp); *masked says whether it did.
|
|
static int mv_pass(VkCommandBuffer cb, struct Size *e, VkImageView zv, float jx, float jy, float bias, int *masked) {
|
|
*masked = 0;
|
|
uint32_t w = e->w, h = e->h;
|
|
VkDescriptorBufferInfo cam;
|
|
if (!mv_init() || !D.cam_addr || !vk_usnap_find(D.cam_addr, CAM_BYTES, 4, &cam)) {
|
|
D.mv_none++;
|
|
D.cam_prev_ok = 0;
|
|
e->chars_ok = 0;
|
|
return 0;
|
|
}
|
|
int s = D.cam_slot, had_prev = D.cam_prev_ok;
|
|
VkBufferCopy bc = {cam.offset, (VkDeviceSize)s * CAM_BYTES, CAM_BYTES};
|
|
vkCmdCopyBuffer(cb, cam.buffer, D.camb, 1, &bc);
|
|
full_barrier(cb);
|
|
D.cam_slot ^= 1, D.cam_prev_ok = 1;
|
|
if (!had_prev) { e->chars_ok = 0; return 0; } // nothing to reproject into: zero motion, new history
|
|
VkDescriptorImageInfo zi = {D.mv_samp, zv, VK_IMAGE_LAYOUT_GENERAL};
|
|
VkDescriptorImageInfo oi = {VK_NULL_HANDLE, e->mv_view, VK_IMAGE_LAYOUT_GENERAL};
|
|
// The class mask needs the G-buffer at the scene size; otherwise bindings 4-7 get the mask images as stand-ins
|
|
// (the shader returns before reading them with bias 0).
|
|
const VkExecTarget *ids = &D.ids;
|
|
int use = bias > 0 && D.ids_ok && ids->image && ids->width == w && ids->height == h && !ids->is3d;
|
|
VkImageView iv = use ? vk_exec_target_view(ids->img, ids->format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT,
|
|
ids->level, ids->base_layer, 1, 0)
|
|
: e->mask_view;
|
|
int cur_c = e->chars_cur;
|
|
VkDescriptorImageInfo ii = {D.mv_samp, iv, VK_IMAGE_LAYOUT_GENERAL};
|
|
VkDescriptorImageInfo mki = {VK_NULL_HANDLE, e->mask_view, VK_IMAGE_LAYOUT_GENERAL};
|
|
VkDescriptorImageInfo cpi = {VK_NULL_HANDLE, e->chars_view[cur_c ^ 1], VK_IMAGE_LAYOUT_GENERAL};
|
|
VkDescriptorImageInfo coi = {VK_NULL_HANDLE, e->chars_view[cur_c], VK_IMAGE_LAYOUT_GENERAL};
|
|
VkDescriptorBufferInfo cur = {D.camb, (VkDeviceSize)s * CAM_BYTES, CAM_BYTES};
|
|
VkDescriptorBufferInfo prev = {D.camb, (VkDeviceSize)(s ^ 1) * CAM_BYTES, CAM_BYTES};
|
|
VkWriteDescriptorSet wr[8] = {
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &zi},
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 1, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &oi},
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 2, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .pBufferInfo = &cur},
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 3, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .pBufferInfo = &prev},
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 4, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii},
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 5, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &mki},
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 6, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &cpi},
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 7, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &coi}};
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, D.mv_pipe);
|
|
rec_push(vk_pipeline_push_fn(), cb, VK_PIPELINE_BIND_POINT_COMPUTE, D.mv_layout, 0, 8, wr);
|
|
float pc[6] = {(float)w, (float)h, jx, jy, use ? bias : 0.0f, use && e->chars_ok ? 1.0f : 0.0f};
|
|
vkCmdPushConstants(cb, D.mv_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof pc, pc);
|
|
vkCmdDispatch(cb, (w + 7) / 8, (h + 7) / 8, 1);
|
|
full_barrier(cb);
|
|
D.mv_frames++;
|
|
if (use) e->chars_cur ^= 1, e->chars_ok = 1, D.masked++;
|
|
else e->chars_ok = 0;
|
|
*masked = use;
|
|
return 1;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- jitter
|
|
// The scene's draws (every pass drawing with the scene depth, until the DLAA point) have their viewport moved by a
|
|
// sub-pixel Halton(2,3) offset, which shifts what they rasterise as a jittered projection would; full-screen passes
|
|
// that shade by window position are unaffected. Jitter only follows a frame DLSS ran on: a jittered frame that DLSS
|
|
// does not resolve shakes on screen, so a missed DLAA point stops the jitter for a second.
|
|
static float halton(uint32_t i, uint32_t b) {
|
|
float f = 1, r = 0;
|
|
for (; i; i /= b) f /= (float)b, r += f * (float)(i % b);
|
|
return r;
|
|
}
|
|
|
|
void vk_dlaa_viewport(VkExec *ex, VkViewport *v) {
|
|
if (!D.jit_ok || D.ended || !ex->depth.image || ex->depth.image != D.jdepth) return;
|
|
v->x += D.jx, v->y += D.jy;
|
|
D.ux = D.jx, D.uy = D.jy, D.jit_used = 1;
|
|
}
|
|
|
|
void vk_dlaa_draw(VkExec *ex) {
|
|
if (!dl_active() || D.ended) return;
|
|
const p_bind_buf *b = &ex->ubo[0][1]; // constant bank c4: the replay keeps user banks from c3 on
|
|
const VkExecTarget *z = &ex->depth;
|
|
if (b->size == 0x940 && b->addr && z->image && z->width >= 640 && z->format == VK_FORMAT_D32_SFLOAT &&
|
|
(!D.jdepth || z->image == D.jdepth))
|
|
D.cam_addr = b->addr;
|
|
}
|
|
|
|
// The jitter moves on right after DLAA ran, not at the present: the replay can record the next frame's scene before
|
|
// the present of this one is handled, and a step taken there landed in the middle of a frame (worse after an off/on
|
|
// switch, which shifted that order). Jitter needs a frame DLSS ran on; a frame without the DLAA point stops it.
|
|
static void next_jitter(void) {
|
|
static int nj = -1;
|
|
if (nj < 0) nj = !rt_knob("DLAA_JITTER", "1"); // TOTK_DLAA_JITTER=0: no jitter (test)
|
|
uint32_t i = D.jframe++ % 16 + 1;
|
|
D.jx = halton(i, 2) - 0.5f, D.jy = halton(i, 3) - 0.5f;
|
|
D.jit_ok = !D.hold && !nj;
|
|
}
|
|
|
|
// The DLSS model (TOTK_DLSS_PRESET, live): default leaves the choice to NGX, which in the 310 releases is the
|
|
// transformer preset K for DLAA, Quality and Balanced and M for Performance. j is the other transformer model: NVIDIA
|
|
// describes it as slightly less ghosting at the cost of extra flicker. k pins K, also in Performance. The value is an
|
|
// NVSDK_NGX_DLSS_Hint_Render_Preset, read when a feature is created; a change recreates the features (preset_check).
|
|
static int preset_value(void) {
|
|
const char *v = cfg_get("TOTK_DLSS_PRESET");
|
|
if (v && (v[0] == 'k' || v[0] == 'K') && !v[1]) return 11;
|
|
if (v && (v[0] == 'j' || v[0] == 'J') && !v[1]) return 10;
|
|
return 0;
|
|
}
|
|
|
|
// How strongly DLSS favours the current frame on characters and where they just were (TOTK_DLSS_CHAR_MASK, percent,
|
|
// live; 0 off). The vectors are the camera's only, so a character that moves on its own drags a faint copy of itself
|
|
// through DLSS's history; the bias mask makes DLSS take less of the history there, at the cost of some of its
|
|
// anti-aliasing on those pixels.
|
|
static float char_bias(void) {
|
|
const char *v = cfg_get("TOTK_DLSS_CHAR_MASK");
|
|
long n = v && *v ? strtol(v, NULL, 10) : 65;
|
|
return n <= 0 ? 0.0f : n >= 100 ? 1.0f : (float)n / 100.0f;
|
|
}
|
|
|
|
// The quality setting DLSS is created with, from the scene's share of the output width.
|
|
static int perf_quality(uint32_t w, uint32_t ow) {
|
|
if (w >= ow) return NGX_PERFQ_DLAA;
|
|
double k = (double)w / ow;
|
|
return k >= 0.66 ? 2 : k >= 0.57 ? 1 : k >= 0.49 ? 0 : 3;
|
|
}
|
|
|
|
// dst: the target DLSS's picture goes to, NULL for the light target itself (DLAA in place).
|
|
static void evaluate(VkExec *ex, const VkExecTarget *dst) {
|
|
const VkExecTarget *t = &D.color, *z = &D.depth;
|
|
if (!dst) dst = t;
|
|
uint32_t w = t->width, h = t->height, ow = dst->width, oh = dst->height;
|
|
VkCommandBuffer cb = ex->cb;
|
|
vk_exec_barrier(ex); // what drew the scene is done (no pass is open here)
|
|
if (D.preset_dirty) { // another model: features are created with it, so every size gets a new one
|
|
for (int i = 0; i < 3; i++)
|
|
if (D.sizes[i].out) free_size(&D.sizes[i]);
|
|
D.last = NULL, D.preset_dirty = 0;
|
|
}
|
|
struct Size *e = size_for(cb, w, h, ow, oh);
|
|
if (!e) return dl_fail("work image creation", 0);
|
|
if (e != D.last) D.reset = 1, D.why[0]++, e->chars_ok = 0; // another size's history is from another moment
|
|
if (!D.params && !NGX_OK(N.alloc_params(&D.params))) return dl_fail("parameter allocation", 0);
|
|
NgxParam *p = D.params;
|
|
VkImageView zv = vk_exec_target_view(z->img, z->format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT, z->level,
|
|
z->base_layer, 1, 0);
|
|
float jx = D.jit_used ? D.ux : 0.0f, jy = D.jit_used ? D.uy : 0.0f;
|
|
static int no_mv = -1;
|
|
if (no_mv < 0) no_mv = !rt_knob("DLAA_MV", "1"); // TOTK_DLAA_MV=0: zero motion (test)
|
|
int masked = 0;
|
|
if (no_mv || !mv_pass(cb, e, zv, jx, jy, char_bias(), &masked)) {
|
|
VkClearColorValue zero = {{0, 0, 0, 0}};
|
|
VkImageSubresourceRange all = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
|
vkCmdClearColorImage(cb, e->mv, VK_IMAGE_LAYOUT_GENERAL, &zero, 1, &all);
|
|
full_barrier(cb);
|
|
if (!no_mv) D.reset = 1, D.why[1]++; // no camera: the history cannot be moved along
|
|
}
|
|
rec_drain(); // NGX records into the command buffer itself: the recorder thread must have made every call so far
|
|
if (!e->feat) {
|
|
p_setu(p, "CreationNodeMask", 1);
|
|
p_setu(p, "VisibilityNodeMask", 1);
|
|
p_setu(p, "Width", w);
|
|
p_setu(p, "Height", h);
|
|
p_setu(p, "OutWidth", ow);
|
|
p_setu(p, "OutHeight", oh);
|
|
p_seti(p, "PerfQualityValue", perf_quality(w, ow));
|
|
// IsHDR | MVLowRes | AutoExposure: linear scene light, motion vectors at the render size, no exposure texture
|
|
p_seti(p, "DLSS.Feature.Create.Flags", (1 << 0) | (1 << 1) | (1 << 6));
|
|
p_seti(p, "DLSS.Enable.Output.Subrects", 0);
|
|
static const char *const hint[] = {"DLSS.Hint.Render.Preset.DLAA", "DLSS.Hint.Render.Preset.Quality",
|
|
"DLSS.Hint.Render.Preset.Balanced", "DLSS.Hint.Render.Preset.Performance",
|
|
"DLSS.Hint.Render.Preset.UltraPerformance",
|
|
"DLSS.Hint.Render.Preset.UltraQuality"};
|
|
for (size_t i = 0; i < sizeof hint / sizeof *hint; i++) p_setu(p, hint[i], (unsigned)D.preset);
|
|
NgxResult r = N.create(cb, NGX_FEATURE_SUPERSAMPLING, p, &e->feat);
|
|
if (!NGX_OK(r) || !e->feat) return dl_fail("DLSS feature creation", r);
|
|
D.reset = 1;
|
|
rt_log("dlaa: DLSS feature created at %ux%u -> %ux%u, quality value %d, preset %s (scene format %d, depth "
|
|
"format %d)", w, h, ow, oh, perf_quality(w, ow), D.preset == 11 ? "K" : D.preset == 10 ? "J" : "default",
|
|
(int)t->format, (int)z->format);
|
|
}
|
|
if (D.last_color != t->image) D.reset = 1, D.why[2]++, D.last_color = t->image;
|
|
VkImageView cv = vk_exec_target_view(t->img, t->format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT, t->level,
|
|
t->base_layer, 1, 0);
|
|
NgxResourceVK rc = res_image(cv, t->image, t->format, w, h, VK_IMAGE_ASPECT_COLOR_BIT, 0);
|
|
NgxResourceVK ro = res_image(e->out_view, e->out, VK_FORMAT_R16G16B16A16_SFLOAT, ow, oh, VK_IMAGE_ASPECT_COLOR_BIT, 1);
|
|
NgxResourceVK rz = res_image(zv, z->image, z->format, w, h, VK_IMAGE_ASPECT_DEPTH_BIT, 0);
|
|
NgxResourceVK rm = res_image(e->mv_view, e->mv, VK_FORMAT_R16G16_SFLOAT, w, h, VK_IMAGE_ASPECT_COLOR_BIT, 0);
|
|
p_setp(p, "Color", &rc);
|
|
p_setp(p, "Output", &ro);
|
|
p_setp(p, "Depth", &rz);
|
|
p_setp(p, "MotionVectors", &rm);
|
|
p_setf(p, "Jitter.Offset.X", jx);
|
|
p_setf(p, "Jitter.Offset.Y", jy);
|
|
p_setf(p, "Sharpness", 0.0f);
|
|
p_seti(p, "Reset", D.reset);
|
|
// The characters' bias mask (mv_pass), or none: the parameter object is reused, so it is cleared explicitly.
|
|
NgxResourceVK rk = res_image(e->mask_view, e->mask, VK_FORMAT_R8_UNORM, w, h, VK_IMAGE_ASPECT_COLOR_BIT, 0);
|
|
p_setp(p, "DLSS.Input.Bias.Current.Color.Mask", masked ? (void *)&rk : NULL);
|
|
p_setu(p, "DLSS.Input.Bias.Current.Color.Subrect.Base.X", 0);
|
|
p_setu(p, "DLSS.Input.Bias.Current.Color.Subrect.Base.Y", 0);
|
|
p_setf(p, "MV.Scale.X", 1.0f);
|
|
p_setf(p, "MV.Scale.Y", 1.0f);
|
|
p_setu(p, "DLSS.Render.Subrect.Dimensions.Width", w);
|
|
p_setu(p, "DLSS.Render.Subrect.Dimensions.Height", h);
|
|
p_setf(p, "DLSS.Pre.Exposure", 1.0f);
|
|
p_setf(p, "DLSS.Exposure.Scale", 1.0f);
|
|
// The time since the previous evaluation, which DLSS weighs its history by; 60 fps before the first and after a
|
|
// gap (a pause, a load), where no history carries over anyway.
|
|
uint64_t now = plat_now_ns();
|
|
double dt = D.last_eval_ns ? (now - D.last_eval_ns) / 1e6 : 0;
|
|
D.last_eval_ns = now;
|
|
p_setf(p, "FrameTimeDeltaInMsec", dt > 0 && dt < 100 ? (float)dt : 16.67f);
|
|
NgxResult r = N.evaluate(cb, e->feat, p, NULL);
|
|
if (!NGX_OK(r)) return dl_fail("DLSS evaluation", r);
|
|
D.reset = 0;
|
|
D.last = e, e->used = D.evals + 1;
|
|
full_barrier(cb);
|
|
// the result into the scene target (DLAA: the bloom, the post passes and the HUD read it and draw over it) or into
|
|
// the scaling pass's target (the HUD draws over it)
|
|
VkImageBlit b = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
|
|
.srcOffsets = {{0, 0, 0}, {(int32_t)ow, (int32_t)oh, 1}},
|
|
.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, dst->level, dst->base_layer, 1},
|
|
.dstOffsets = {{0, 0, 0}, {(int32_t)ow, (int32_t)oh, 1}}};
|
|
vkCmdBlitImage(cb, e->out, VK_IMAGE_LAYOUT_GENERAL, dst->image, VK_IMAGE_LAYOUT_GENERAL, 1, &b, VK_FILTER_NEAREST);
|
|
full_barrier(cb);
|
|
ex->recorded += 4;
|
|
vk_exec_draw_new_cb(); // NGX bound pipelines and descriptor sets of its own: the next draw binds everything again
|
|
D.jdepth = z->image;
|
|
next_jitter();
|
|
if (++D.evals == 1 || D.evals == 300 || D.evals % 3600 == 0) // 300: one early line with the frames counted
|
|
rt_log("dlaa: %llu frames anti-aliased (scene %ux%u, output %ux%u; %llu frames without the DLAA point, %llu with camera "
|
|
"motion, %llu with the characters' mask, %llu without a camera, %llu jitter holds; resets: %llu size, %llu "
|
|
"camera, %llu target)",
|
|
(unsigned long long)D.evals, w, h, ow, oh, (unsigned long long)D.frames_missed,
|
|
(unsigned long long)D.mv_frames, (unsigned long long)D.masked, (unsigned long long)D.mv_none,
|
|
(unsigned long long)D.holds,
|
|
(unsigned long long)D.why[0], (unsigned long long)D.why[1], (unsigned long long)D.why[2]);
|
|
}
|
|
|
|
static int scene_color(VkFormat f) {
|
|
return f == VK_FORMAT_B10G11R11_UFLOAT_PACK32 || f == VK_FORMAT_R16G16B16A16_SFLOAT;
|
|
}
|
|
|
|
void vk_dlaa_pass(VkExec *ex) {
|
|
const VkExecTarget *c = &ex->color[0], *z = &ex->depth;
|
|
if (!dl_active() || D.done) return;
|
|
// The G-buffer: several colour targets with a D32F depth of at least 640 wide. Only the light target drawn after it
|
|
// arms the frame; at night the game draws into the light target alone with the scene depth before the G-buffer too,
|
|
// and DLAA then ran at a small pass ahead of the whole scene, which reached the screen jittered.
|
|
if (ex->pass_ncolor >= 3 && z->image && z->format == VK_FORMAT_D32_SFLOAT && z->width >= 640) {
|
|
D.gbuf = 1, D.armed = 0;
|
|
// target 0 is the object-class buffer (RGBA8, the scene size): the characters' bias mask reads its green. A
|
|
// smaller pass of the same shape (640x360, seen while loading) does not replace the scene's in the same frame.
|
|
if (c->image && c->format == VK_FORMAT_R8G8B8A8_UNORM && c->width == z->width && c->height == z->height &&
|
|
c->samples <= 1 && !c->is3d && (!D.ids_ok || c->width >= D.ids.width))
|
|
D.ids = *c, D.ids_ok = 1;
|
|
return;
|
|
}
|
|
// the scene depth is D32F; a D16 target of the scene size also serves one composite pass before the transparents
|
|
if (D.gbuf && ex->pass_ncolor == 1 && c->image && scene_color(c->format) && z->image && z->format == VK_FORMAT_D32_SFLOAT &&
|
|
c->width == z->width && c->height == z->height && c->width >= 640 &&
|
|
c->samples <= 1 && !c->is3d) {
|
|
D.color = *c, D.depth = *z, D.armed = 1;
|
|
return;
|
|
}
|
|
if (!D.armed) return;
|
|
if (!D.ended) {
|
|
if (!ex->pass_w || ex->pass_w * 4 > D.color.width) return;
|
|
D.ended = 1;
|
|
unsigned o[2], s[2];
|
|
res_sizes(o, s);
|
|
if (D.color.width >= o[0]) D.done = 1, evaluate(ex, NULL); // DLAA in place; else wait for the scaling pass
|
|
return;
|
|
}
|
|
// the game's scaling pass: one colour target wider than the scene, no depth
|
|
if (ex->pass_ncolor == 1 && c->image && !z->image && scene_color(c->format) && c->width > D.color.width &&
|
|
c->samples <= 1 && !c->is3d) {
|
|
D.done = D.up = 1;
|
|
evaluate(ex, c);
|
|
}
|
|
}
|
|
|
|
// The preset, read again when a setting changed (the menu); a different one recreates the features at the next DLAA run.
|
|
static void preset_check(void) {
|
|
unsigned v = cfg_version();
|
|
if (v == D.cfg_seen) return;
|
|
D.cfg_seen = v;
|
|
int p = preset_value();
|
|
if (p == D.preset) return;
|
|
if (D.evals) rt_log("dlaa: DLSS preset %s; the features are created again", p == 11 ? "K" : p == 10 ? "J" : "default");
|
|
D.preset = p, D.preset_dirty = 1, D.reset = 1;
|
|
}
|
|
|
|
void vk_dlaa_frame(void) {
|
|
preset_check();
|
|
int on = N.available && cfg_bool("TOTK_DLAA");
|
|
if (on != D.on) {
|
|
rt_log("dlaa: %s", on ? "on" : "off");
|
|
D.on = on;
|
|
// either way the next DLAA frame starts a new history from an unjittered frame
|
|
D.reset = 1, D.cam_prev_ok = 0, D.jit_ok = D.jit_used = D.armed = D.done = D.gbuf = D.ended = D.up = 0;
|
|
D.hold = 0;
|
|
return;
|
|
}
|
|
if (!dl_active()) return;
|
|
if (!D.done) D.frames_missed++;
|
|
if (D.jit_used && !D.done) D.hold = 60, D.holds++; // jittered but not resolved: that frame shook
|
|
else if (D.hold) D.hold--;
|
|
if (!D.done) D.jit_ok = 0;
|
|
if (D.up && !D.scaler_skipped && ++D.scaler_missed == 1)
|
|
rt_log("dlaa: the game's scaling draw over DLSS's picture was not found (its fragment shader is not in FXAA_FS): "
|
|
"the jittered scene reaches the screen and shakes");
|
|
D.armed = D.done = D.jit_used = D.gbuf = D.ended = D.up = D.scaler_skipped = D.ids_ok = 0;
|
|
D.cam_addr = 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the game's FXAA (TOTK_FXAA, live)
|
|
// TotK's FXAA is two full-screen passes after the bloom, just before the HUD: one copies the scene target into a
|
|
// scratch target, the other draws the anti-aliased result back over the scene (found by diffing frame dumps with the
|
|
// game's own switch, ModelSceneExtensionParam EnableFXAA, on and off). With fxaa = 0 the draws of that second pass's
|
|
// fragment shader are skipped, so the scene reaches the HUD as it was. Shaders are known by the FNV-1a hash of their
|
|
// SPIR-V, as vk_dump.c names them; a translator change gives new hashes and the switch then does nothing.
|
|
// With a scene smaller than the output (the game's own 5/6 render scale, or a DLSS mode) the same shader draws the
|
|
// scaled picture into the output-sized target, in a pass without the depth attachment the in-place FXAA has: there it
|
|
// is always drawn, whatever fxaa says (skipping it would leave the 3D picture out), unless DLSS already wrote that
|
|
// target this frame.
|
|
// The stock translator's hash, the one with tools/xlate patch 0010 (PCSS, PR #89) applied, and the one with patches
|
|
// 0010 and 0011 (the shadow filter spread) applied. A translator change that gives the shader another hash is reported
|
|
// by vk_dlaa_frame: the game's scaling draw then covers DLSS's picture with the jittered scene, which shakes.
|
|
static const uint64_t FXAA_FS[] = {0xf450003ebeb3dc52ull, 0x0a12f4bc2a9a4fbeull, 0x5c43ae18948d52cbull};
|
|
|
|
static uint64_t spirv_hash(const GfxShader *s) {
|
|
static struct { const GfxShader *s; uint64_t h; } tab[8192];
|
|
uintptr_t k = ((uintptr_t)s >> 4) * 0x9E3779B97F4A7C15ull;
|
|
for (uint32_t i = 0, j = (uint32_t)(k >> 51); i < 8192; i++, j = (j + 1) & 8191) {
|
|
if (tab[j].s == s) return tab[j].h;
|
|
if (!tab[j].s) {
|
|
uint64_t words = 0, h = 0xcbf29ce484222325ull;
|
|
const uint8_t *b = (const uint8_t *)gfx_shader_spirv(s, &words);
|
|
for (uint64_t n = 0; b && n < words * 4; n++) h = (h ^ b[n]) * 0x100000001b3ull;
|
|
tab[j].s = s, tab[j].h = h;
|
|
return h;
|
|
}
|
|
}
|
|
return 0; // table full: no skip
|
|
}
|
|
|
|
int vk_dlaa_skip_draw(VkExec *ex, const GfxShader *fs) {
|
|
static uint64_t frame_read = ~0ull;
|
|
static int fxaa = 1;
|
|
// fxaa = 0 reports once whether it found the game's FXAA draw: the first skip, or a minute of frames (3600) with
|
|
// none, which is what a translator change that moves FXAA_FS looks like on every GPU, with or without DLSS
|
|
static uint64_t fx_off_frames;
|
|
static int fx_found, fx_reported;
|
|
uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
|
if (f != frame_read) { // once a frame: the menu switches it live
|
|
frame_read = f, fxaa = cfg_bool("TOTK_FXAA");
|
|
if (!fxaa && !fx_found && !fx_reported && ++fx_off_frames == 3600) {
|
|
fx_reported = 1;
|
|
rt_log("dlaa: fxaa = 0 found no draw of the game's FXAA in %llu frames (its fragment shader is not in "
|
|
"FXAA_FS): the FXAA stays on", (unsigned long long)fx_off_frames);
|
|
}
|
|
}
|
|
if (fxaa && !D.up) return 0; // nothing to skip: no shader lookup per draw at the defaults
|
|
if (!fs) return 0;
|
|
uint64_t h = spirv_hash(fs);
|
|
int known = 0;
|
|
for (size_t i = 0; i < sizeof FXAA_FS / sizeof FXAA_FS[0]; i++) known |= h == FXAA_FS[i];
|
|
if (!known) return 0;
|
|
if (!ex->depth.image) return D.scaler_skipped = D.up; // the scaling pass
|
|
if (!fxaa && !fx_found) {
|
|
fx_found = 1;
|
|
rt_log("dlaa: fxaa = 0 skips the game's FXAA (fragment shader %016llx)", (unsigned long long)h);
|
|
}
|
|
return !fxaa;
|
|
}
|
|
|