mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 22:19:57 -04:00
Merge main into the test line: the pull requests taken there and two render target fixes.
Main brought the fixes of eleven pull requests (the texture pool set sealed when an early replay part is submitted, every location reflected in interface arrays, RGB9E5 texels recognised for integer storage views, the upscaler a frame started with kept, GPU pass timestamps without vendor breadcrumbs, the Linux build, window and NGX fixes), render targets released when drawn over at another address, the vertex layout kept in the pipeline on newer AMD drivers, and the upscaler choice read once per evaluation. Four hunks conflicted and take both sides: pool_sync returns for a sealed set before the per-frame creation budget's clock (vk_exec_draw.c); make_module keeps the test line's early_ok argument and main's module hash (vk_pipeline.c); a texture taken back counts as sampled again and records how long it was away, and the texture line logs the disuse counters and the overwritten render targets (vk_texture.c). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -3,11 +3,12 @@
|
||||
// The boot driver: the guest main thread runs the game's own nn::init::Start
|
||||
// with three host callbacks, the second of which runs DT_INIT of main, subsdk0 and sdk; Start then calls nnMain.
|
||||
// --boot checks two checkpoints on the way and ends at the first nvnBootstrapLoader call, at the return address of
|
||||
// each in main (1.1.0, 1.2.1, 1.4.3):
|
||||
// SetFocusHandlingMode main+0xeb8c14 (1.2.1: +0xe7a954, 1.4.3: +0xbdb48c) in nnMain, every module's constructor
|
||||
// flag reads 2
|
||||
// nvnBootstrapLoader main+0xebcdec (1.2.1: +0xe7eb2c, 1.4.3: +0xbe0360) in the graphics bring-up; logs the
|
||||
// run statistics, exits 0
|
||||
// each in main (1.1.1; 1.1.0, 1.2.1, 1.4.3 in brackets):
|
||||
// SetFocusHandlingMode main+0x1202d44 (+0xeb8c14, +0xe7a954, +0xbdb48c) in nnMain, every module's constructor
|
||||
// flag reads 2. 1.1.1's SDK calls it first, from nn::oe::InitializeApplet before DT_INIT; a
|
||||
// call from outside main is not the checkpoint.
|
||||
// nvnBootstrapLoader main+0x346bf8c (+0xebcdec, +0xe7eb2c, +0xbe0360) in the graphics bring-up; logs the run
|
||||
// statistics, exits 0
|
||||
// With --seconds S the run continues past that call; rt_frame_presented ends it once the world clock passed S.
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -27,19 +28,20 @@
|
||||
#include "platform/platform.h"
|
||||
|
||||
#define SYM_INIT_START "_ZN2nn4init5StartEmmPFvvES2_S2_"
|
||||
// main: return addresses of nnMain's SetFocusHandlingMode call (from the analysis of each build's main)
|
||||
// and of the first nvnBootstrapLoader call, per game version.
|
||||
#define CHECKPOINT_RET (RT_V143 ? 0xbdb48cull : RT_V110 ? 0xeb8c14ull : 0xe7a954ull)
|
||||
#define NVN_RET (RT_V143 ? 0xbe0360ull : RT_V110 ? 0xebcdecull : 0xe7eb2cull)
|
||||
// main: return addresses of nnMain's SetFocusHandlingMode call and of the first nvnBootstrapLoader call, per game
|
||||
// version (from the analysis of each build's main; 1.1.1's are the x30 both host functions see in a normal start).
|
||||
#define CHECKPOINT_RET (RT_V111 ? 0x1202d44ull : RT_V143 ? 0xbdb48cull : RT_V110 ? 0xeb8c14ull : 0xe7a954ull)
|
||||
#define NVN_RET (RT_V111 ? 0x346bf8cull : RT_V143 ? 0xbe0360ull : RT_V110 ? 0xebcdecull : 0xe7eb2cull)
|
||||
#define CTOR_DONE 2 // a module's constructor flag after its DT_INIT ran the init_array
|
||||
|
||||
// The init_array sizes (1.1.0 and 1.2.1: counted in the modules; 1.4.3: DT_INIT_ARRAYSZ).
|
||||
// The three tables carry the same rows, so cb_module_inits walks them by the size of one.
|
||||
// The init_array sizes (1.1.0 and 1.2.1: counted in the modules; 1.1.1 and 1.4.3: DT_INIT_ARRAYSZ).
|
||||
// The four tables carry the same rows, so cb_module_inits walks them by the size of one.
|
||||
typedef struct { const char *name; uint64_t entries; } InitCount;
|
||||
static const InitCount inits_110[] = {{"main", 10668}, {"subsdk0", 45}, {"sdk", 116}};
|
||||
static const InitCount inits_111[] = {{"main", 9783}, {"subsdk0", 0}, {"sdk", 96}};
|
||||
static const InitCount inits_121[] = {{"main", 10673}, {"subsdk0", 45}, {"sdk", 116}};
|
||||
static const InitCount inits_143[] = {{"main", 10717}, {"subsdk0", 45}, {"sdk", 83}};
|
||||
#define expected_inits (RT_V143 ? inits_143 : RT_V110 ? inits_110 : inits_121)
|
||||
#define expected_inits (RT_V111 ? inits_111 : RT_V143 ? inits_143 : RT_V110 ? inits_110 : inits_121)
|
||||
|
||||
static volatile int32_t inits_done, checkpoint_seen;
|
||||
uint64_t rt_guest_start_ns;
|
||||
@@ -123,6 +125,10 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) {
|
||||
uint64_t ret = c->x[30];
|
||||
caller(c, at, sizeof at);
|
||||
if (!strcmp(what, "checkpoint")) {
|
||||
if (ret - main_mod->base >= main_mod->size) { // 1.1.1's SDK sets the mode in nn::oe::InitializeApplet
|
||||
rt_logf("boot: %s from %s, outside main: not the checkpoint\n", detail, at);
|
||||
return;
|
||||
}
|
||||
if (ret != main_mod->base + CHECKPOINT_RET)
|
||||
rt_fatal("boot: checkpoint %s reached from %s, expected main+%#llx", detail, at,
|
||||
(unsigned long long)CHECKPOINT_RET);
|
||||
|
||||
@@ -132,7 +132,7 @@ static const CfgMeta metas[] = {
|
||||
// Screen
|
||||
{"FULLSCREEN", "0", "0|1", LIVE}, {"WINDOW_SIZE", "1920x1080", NULL, LIVE},
|
||||
{"VK_PRESENT", "mailbox", "mailbox|fifo|immediate", LIVE}, {"GPU_INDEX", "", NULL, RESTART},
|
||||
{"MONITOR", "auto", NULL, RESTART}, // auto|primary|<1-based index>|<part of a device or monitor name> (gfx_window_win.c chosen_monitor)
|
||||
{"MONITOR", "auto", NULL, RESTART}, // auto|primary|<1-based index>|<part of a monitor name> (gfx_window_win.c, gfx_window_sdl.c chosen_monitor)
|
||||
{"PERF_OVERLAY", "off", "off|fps|full", LIVE},
|
||||
{"OUTPUT_RES", "1920x1080", NULL, RESTART}, {"RENDER_SCALE", "game", "game|50|67|75|100|125|150|200", RESTART},
|
||||
{"UPSCALER", "fsr", "fsr|bilinear", LIVE}, {"SHARPNESS", "80", "0..100", LIVE},
|
||||
|
||||
@@ -952,7 +952,7 @@ static void add_res(GfxShader *s, int kind, const char *o, const char *e) {
|
||||
|
||||
// SPIR-V opcodes and enumerants used below (SPIR-V 1.6 specification, section 3).
|
||||
enum { OP_ENTRY_POINT = 15, OP_EXECUTION_MODE = 16, OP_TYPE_INT = 21, OP_TYPE_FLOAT = 22, OP_TYPE_VECTOR = 23,
|
||||
OP_TYPE_ARRAY = 28, OP_TYPE_POINTER = 32, OP_VARIABLE = 59, OP_DECORATE = 71,
|
||||
OP_TYPE_ARRAY = 28, OP_TYPE_POINTER = 32, OP_CONSTANT = 43, OP_VARIABLE = 59, OP_DECORATE = 71,
|
||||
DEC_BUILTIN = 11, DEC_LOCATION = 30, DEC_COMPONENT = 31, SC_INPUT = 1, SC_OUTPUT = 3, EM_LOCAL_SIZE = 17 };
|
||||
|
||||
// Interface Locations and the compute workgroup size from the module. Ids beyond 2^20 are ignored (translated modules stay
|
||||
@@ -963,11 +963,11 @@ static void reflect_spirv(GfxShader *s) {
|
||||
s->local[0] = s->local[1] = s->local[2] = 1;
|
||||
if (n < 5) return;
|
||||
uint32_t bound = w[3] < (1u << 20) ? w[3] : (1u << 20);
|
||||
// per id: location + 1 (0 = none), component flag, scalar/vector description of types, pointee of pointers
|
||||
// per id: location + 1 (0 = none), component flag, scalar/vector type and array span, pointer target, constant value
|
||||
int32_t *loc = calloc(bound, sizeof *loc);
|
||||
uint8_t *packed = calloc(bound, 1), *ttype = calloc(bound, 1), *tcomps = calloc(bound, 1);
|
||||
uint32_t *pointee = calloc(bound, sizeof *pointee);
|
||||
if (!loc || !packed || !ttype || !tcomps || !pointee) goto out;
|
||||
uint8_t *packed = calloc(bound, 1), *ttype = calloc(bound, 1), *tcomps = calloc(bound, 1), *tlocs = calloc(bound, 1);
|
||||
uint32_t *pointee = calloc(bound, sizeof *pointee), *value = calloc(bound, sizeof *value);
|
||||
if (!loc || !packed || !ttype || !tcomps || !tlocs || !pointee || !value) goto out;
|
||||
for (uint64_t i = 5; i < n;) {
|
||||
uint32_t op = w[i] & 0xffff, len = w[i] >> 16;
|
||||
if (!len || i + len > n) break;
|
||||
@@ -979,13 +979,17 @@ static void reflect_spirv(GfxShader *s) {
|
||||
if (a[1] == DEC_LOCATION) loc[id] = (int32_t)a[2] + 1;
|
||||
else if (a[1] == DEC_COMPONENT) packed[id] = 1;
|
||||
} else if (op == OP_TYPE_INT && len >= 4 && id < bound) {
|
||||
ttype[id] = a[2] ? GFX_TYPE_SINT : GFX_TYPE_UINT, tcomps[id] = 1;
|
||||
ttype[id] = a[2] ? GFX_TYPE_SINT : GFX_TYPE_UINT, tcomps[id] = 1, tlocs[id] = 1;
|
||||
} else if (op == OP_TYPE_FLOAT && len >= 3 && id < bound) {
|
||||
ttype[id] = GFX_TYPE_FLOAT, tcomps[id] = 1;
|
||||
ttype[id] = GFX_TYPE_FLOAT, tcomps[id] = 1, tlocs[id] = 1;
|
||||
} else if (op == OP_TYPE_VECTOR && len >= 4 && id < bound && a[1] < bound) {
|
||||
ttype[id] = ttype[a[1]], tcomps[id] = (uint8_t)a[2];
|
||||
} else if (op == OP_TYPE_ARRAY && len >= 4 && id < bound && a[1] < bound) {
|
||||
ttype[id] = ttype[a[1]], tcomps[id] = tcomps[a[1]]; // element type (arrayed interface blocks)
|
||||
ttype[id] = ttype[a[1]], tcomps[id] = (uint8_t)a[2], tlocs[id] = 1;
|
||||
} else if (op == OP_CONSTANT && len == 4 && a[1] < bound) {
|
||||
value[a[1]] = a[2];
|
||||
} else if (op == OP_TYPE_ARRAY && len >= 4 && id < bound && a[1] < bound && a[2] < bound) {
|
||||
ttype[id] = ttype[a[1]], tcomps[id] = tcomps[a[1]];
|
||||
uint64_t slots = (uint64_t)tlocs[a[1]] * value[a[2]];
|
||||
tlocs[id] = (uint8_t)(slots < 32 ? slots : 32); // the interface masks cover 32 locations
|
||||
} else if (op == OP_TYPE_POINTER && len >= 4 && id < bound) {
|
||||
pointee[id] = a[2];
|
||||
} else if (op == OP_VARIABLE && len >= 4) {
|
||||
@@ -994,14 +998,20 @@ static void reflect_spirv(GfxShader *s) {
|
||||
int l = loc[var] - 1;
|
||||
uint32_t t = pointee[type] < bound ? pointee[type] : 0;
|
||||
GfxShaderIo io = {ttype[t], tcomps[t] ? tcomps[t] : 4, packed[var]};
|
||||
if (sc == SC_INPUT) s->in_mask |= 1u << l, s->in[l] = io;
|
||||
else s->out_mask |= 1u << l, s->out[l] = io;
|
||||
// VS -> FS arrays occupy every element's locations, even if only some elements are accessed.
|
||||
// Other stages retain their existing reflection: mesh outputs have an outer per-vertex array.
|
||||
uint32_t slots = ((s->stage == GFX_STAGE_VS && sc == SC_OUTPUT) ||
|
||||
(s->stage == GFX_STAGE_FS && sc == SC_INPUT)) && tlocs[t] ? tlocs[t] : 1;
|
||||
for (uint32_t j = 0; j < slots && l + j < 32; j++) {
|
||||
if (sc == SC_INPUT) s->in_mask |= 1u << (l + j), s->in[l + j] = io;
|
||||
else s->out_mask |= 1u << (l + j), s->out[l + j] = io;
|
||||
}
|
||||
}
|
||||
}
|
||||
i += len;
|
||||
}
|
||||
out:
|
||||
free(loc), free(packed), free(ttype), free(tcomps), free(pointee);
|
||||
free(loc), free(packed), free(ttype), free(tcomps), free(tlocs), free(pointee), free(value);
|
||||
}
|
||||
|
||||
// Every gradient sample (OpImageSampleExplicitLod with Grad) whose two gradients are each built from two components gets
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// The SDL3 back end of the window (gfx_wsys.h), for hosts without Win32: the window on a thread of its own that also
|
||||
// runs SDL's event loop, F11 fullscreen (borderless, the desktop's mode), the settings NX2NAVI_WINDOW_SIZE and
|
||||
// NX2NAVI_FULLSCREEN, the display's refresh rate, focus, the Vulkan surface. The swapchain and the present are
|
||||
// gfx_present.c's. Keyboard and mouse events go to input_sdl.c and to the menu (ui/menu_sdl.cpp).
|
||||
// runs SDL's event loop, F11 fullscreen (borderless, the desktop's mode), the settings NX2NAVI_WINDOW_SIZE,
|
||||
// NX2NAVI_FULLSCREEN and NX2NAVI_MONITOR, the display's refresh rate, focus, the Vulkan surface. The swapchain and the
|
||||
// present are gfx_present.c's. Keyboard and mouse events go to input_sdl.c and to the menu (ui/menu_sdl.cpp).
|
||||
//
|
||||
// SDL wants its video calls on the thread that initialised video, so this thread does them all: the others hand it
|
||||
// work through run_on_window (a user event and a wait) or post a user event. Video is initialised before the window
|
||||
@@ -121,7 +121,7 @@ void gfx_window_request_close(void) {
|
||||
if (g_win) post(EV_CLOSE);
|
||||
}
|
||||
|
||||
// ---- the display's refresh rate and size
|
||||
// ---- the display's refresh rate
|
||||
static void refresh_rate_update(void) {
|
||||
const SDL_DisplayMode *m = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_win));
|
||||
double hz = 0;
|
||||
@@ -130,21 +130,23 @@ static void refresh_rate_update(void) {
|
||||
gfx_window_set_refresh(hz);
|
||||
}
|
||||
|
||||
int gfx_window_monitor_size(int *w, int *h) {
|
||||
if (!g_win) return 0;
|
||||
const SDL_DisplayMode *m = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_win));
|
||||
if (!m) return 0;
|
||||
*w = (int)(m->w * m->pixel_density + 0.5f);
|
||||
*h = (int)(m->h * m->pixel_density + 0.5f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void density_update(void) {
|
||||
float d = SDL_GetWindowPixelDensity(g_win);
|
||||
__atomic_store_n(&g_density_milli, (int32_t)((d > 0 ? d : 1) * 1000 + 0.5f), __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
// ---- the window thread
|
||||
// NX2NAVI_MONITOR's display while a Wayland compositor has yet to place the fullscreen window (open_window): SDL learns
|
||||
// the output a Wayland window is on later, with a display, scale or mode change (Hyprland sends only the latter two).
|
||||
static SDL_DisplayID g_place_pending;
|
||||
|
||||
static void report_placement(SDL_DisplayID want) {
|
||||
SDL_DisplayID at = SDL_GetDisplayForWindow(g_win);
|
||||
const char *name = SDL_GetDisplayName(at);
|
||||
if (at == want) rt_log("gfx: the window is on the chosen monitor \"%s\"", name ? name : "an unknown monitor");
|
||||
else rt_log("gfx: the window is not on the chosen monitor; it is on \"%s\"", name ? name : "an unknown monitor");
|
||||
}
|
||||
|
||||
static void handle(const SDL_Event *e) {
|
||||
if (e->type == g_ev_base + EV_RUN) {
|
||||
RunReq *r = e->user.data1;
|
||||
@@ -181,6 +183,10 @@ static void handle(const SDL_Event *e) {
|
||||
density_update();
|
||||
refresh_rate_update();
|
||||
gfx_window_mark_dirty();
|
||||
if (g_place_pending && SDL_GetDisplayForWindow(g_win) == g_place_pending) {
|
||||
report_placement(g_place_pending);
|
||||
g_place_pending = 0;
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_FOCUS_GAINED:
|
||||
case SDL_EVENT_WINDOW_FOCUS_LOST:
|
||||
@@ -248,6 +254,187 @@ int gfx_window_vk_presentable(VkInstance inst, VkPhysicalDevice pd, uint32_t fam
|
||||
return video_up() && SDL_Vulkan_GetPresentationSupport(inst, pd, family);
|
||||
}
|
||||
|
||||
// ---- monitor choice (NX2NAVI_MONITOR, --monitor, --list-monitors)
|
||||
// The monitors in SDL_GetDisplays order, which is the index the setting counts in (1-based) and --list-monitors prints.
|
||||
// The video driver sets that order, which need not match the desktop's own list of monitors: it holds while the same
|
||||
// monitors stay connected, but a monitor that appears or goes away can move the ones after it. SDL has one name per
|
||||
// monitor: make and model, with the connector (such as DP-2) on Wayland and X11. The name pins one monitor more firmly
|
||||
// than its index. These calls belong to the thread that initialised video: the window thread, or the main thread in
|
||||
// --list-monitors.
|
||||
#define MON_MAX 32
|
||||
|
||||
typedef struct {
|
||||
SDL_DisplayID id;
|
||||
SDL_Rect rc; // the monitor's pixels, at its position in the desktop's coordinates
|
||||
int primary;
|
||||
unsigned dpi; // the display scale as a DPI, 96 at 100%
|
||||
char name[128]; // SDL_GetDisplayName; empty when SDL has none
|
||||
} MonInfo;
|
||||
|
||||
typedef struct { MonInfo m[MON_MAX]; int n; } MonList;
|
||||
|
||||
static int enum_monitors(MonList *l) {
|
||||
*l = (MonList){0};
|
||||
int n = 0;
|
||||
SDL_DisplayID *ids = SDL_GetDisplays(&n);
|
||||
if (!ids) return 0;
|
||||
SDL_DisplayID primary = SDL_GetPrimaryDisplay();
|
||||
for (int i = 0; i < n && l->n < MON_MAX; i++) {
|
||||
SDL_Rect rc;
|
||||
if (!SDL_GetDisplayBounds(ids[i], &rc)) continue;
|
||||
// A scaled desktop (Wayland at 125%) counts its coordinates in scaled units: the pixel density turns the size
|
||||
// back into the monitor's pixels, and the content scale and the density together are the display scale
|
||||
// (SDL_GetWindowDisplayScale gives a window the same product). The position stays in the desktop's units.
|
||||
const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(ids[i]);
|
||||
float density = mode && mode->pixel_density > 0 ? mode->pixel_density : 1, scale = SDL_GetDisplayContentScale(ids[i]);
|
||||
if (!(scale > 0)) scale = 1;
|
||||
const char *name = SDL_GetDisplayName(ids[i]);
|
||||
MonInfo *m = &l->m[l->n++];
|
||||
*m = (MonInfo){.id = ids[i], .rc = {rc.x, rc.y, (int)(rc.w * density + 0.5f), (int)(rc.h * density + 0.5f)},
|
||||
.primary = ids[i] == primary, .dpi = (unsigned)(96 * scale * density + 0.5f)};
|
||||
snprintf(m->name, sizeof m->name, "%s", name ? name : "");
|
||||
}
|
||||
SDL_free(ids);
|
||||
return l->n;
|
||||
}
|
||||
|
||||
// `2 "HP Inc. HP E243 CNK0161ZY2 (DP-3)" 1920x1080 at 3456,756 dpi 120 (125%)`: one monitor, for the log and
|
||||
// --list-monitors, with " primary" after the rectangle on the primary one.
|
||||
static void monitor_line(const MonList *l, int i, char *out, size_t cap) {
|
||||
const MonInfo *m = &l->m[i];
|
||||
snprintf(out, cap, "%d \"%s\" %dx%d at %d,%d%s dpi %u (%u%%)", i + 1, m->name, m->rc.w, m->rc.h, m->rc.x, m->rc.y,
|
||||
m->primary ? " primary" : "", m->dpi, m->dpi * 100 / 96);
|
||||
}
|
||||
|
||||
static int monitor_unset(const char *v) { return !v || !*v || !SDL_strcasecmp(v, "auto"); }
|
||||
|
||||
// Which monitor of the list a NX2NAVI_MONITOR value (not empty, not auto) names, as a 0-based index; -1 when none.
|
||||
// Rules, in this order: "primary"; all digits, a 1-based index; any other text, a case-insensitive substring of a
|
||||
// monitor's name (the first monitor in list order when several match). why gets the rule that matched, or the reason
|
||||
// nothing did. The same rules as gfx_window_win.c resolve_monitor, which also matches the GDI device name.
|
||||
static int resolve_monitor(const MonList *l, const char *want, char *why, size_t cap) {
|
||||
if (!SDL_strcasecmp(want, "primary")) {
|
||||
for (int i = 0; i < l->n; i++)
|
||||
if (l->m[i].primary) {
|
||||
snprintf(why, cap, "the primary monitor");
|
||||
return i;
|
||||
}
|
||||
snprintf(why, cap, "no monitor is marked primary");
|
||||
return -1;
|
||||
}
|
||||
if (want[strspn(want, "0123456789")] == 0) {
|
||||
long idx = strtol(want, NULL, 10);
|
||||
if (idx >= 1 && idx <= l->n) {
|
||||
snprintf(why, cap, "index %ld of %d", idx, l->n);
|
||||
return (int)idx - 1;
|
||||
}
|
||||
snprintf(why, cap, "index %s is outside 1..%d", want, l->n);
|
||||
return -1;
|
||||
}
|
||||
int first = -1, hits = 0;
|
||||
for (int i = 0; i < l->n; i++)
|
||||
if (SDL_strcasestr(l->m[i].name, want)) {
|
||||
if (first < 0) first = i;
|
||||
hits++;
|
||||
}
|
||||
if (first < 0) {
|
||||
snprintf(why, cap, "no monitor name contains it");
|
||||
return -1;
|
||||
}
|
||||
if (hits > 1) snprintf(why, cap, "monitor name contains it; the first of %d matches", hits);
|
||||
else snprintf(why, cap, "monitor name contains it");
|
||||
return first;
|
||||
}
|
||||
|
||||
// The monitor NX2NAVI_MONITOR picks, copied to *out; 0 when it is unset or auto (the system places the window), or when
|
||||
// it matches nothing now (the monitors present are logged, and the default placement applies). log: say what was decided
|
||||
// and why. The window thread asks with log set; the size query below, which widescreen.c makes before the window
|
||||
// exists, stays quiet. Window thread.
|
||||
static int chosen_monitor(MonInfo *out, int log) {
|
||||
const char *want = cfg_get("NX2NAVI_MONITOR");
|
||||
if (monitor_unset(want)) return 0;
|
||||
MonList l;
|
||||
char why[256], line[512];
|
||||
int n = enum_monitors(&l), pick = -1;
|
||||
if (n) pick = resolve_monitor(&l, want, why, sizeof why);
|
||||
else snprintf(why, sizeof why, "no monitor was found");
|
||||
if (pick < 0) {
|
||||
if (log) {
|
||||
rt_log("gfx: NX2NAVI_MONITOR=%s picks no monitor (%s); the window gets the default placement. Monitors present:",
|
||||
want, why);
|
||||
for (int i = 0; i < n; i++) {
|
||||
monitor_line(&l, i, line, sizeof line);
|
||||
rt_log("gfx: %s", line);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (log) {
|
||||
monitor_line(&l, pick, line, sizeof line);
|
||||
rt_log("gfx: NX2NAVI_MONITOR=%s picks monitor %s (%s)", want, line, why);
|
||||
}
|
||||
*out = l.m[pick];
|
||||
return 1;
|
||||
}
|
||||
|
||||
// --list-monitors (main.c): one line per monitor, then what the current NX2NAVI_MONITOR would pick. No game files. It
|
||||
// runs on the main thread before any window thread exists, so that thread initialises video here and shuts it down again.
|
||||
int list_monitors(void) {
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
fprintf(stderr, "list-monitors: SDL video init failed (%s)\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
MonList l;
|
||||
char line[512], why[256];
|
||||
int n = enum_monitors(&l);
|
||||
if (!n) {
|
||||
fprintf(stderr, "list-monitors: no monitor found\n");
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
printf("index name desktop rectangle primary dpi\n");
|
||||
for (int i = 0; i < n; i++) {
|
||||
monitor_line(&l, i, line, sizeof line);
|
||||
printf("%s\n", line);
|
||||
}
|
||||
const char *want = cfg_get("NX2NAVI_MONITOR");
|
||||
if (monitor_unset(want)) {
|
||||
printf("NX2NAVI_MONITOR is not set (auto): the system places the window\n");
|
||||
} else {
|
||||
int pick = resolve_monitor(&l, want, why, sizeof why);
|
||||
if (pick >= 0) printf("NX2NAVI_MONITOR=%s picks monitor %d (%s)\n", want, pick + 1, why);
|
||||
else printf("NX2NAVI_MONITOR=%s picks no monitor (%s)\n", want, why);
|
||||
}
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct { int w, h, ok; } SizeReq;
|
||||
|
||||
static void chosen_size(void *arg) {
|
||||
SizeReq *r = arg;
|
||||
MonInfo mon;
|
||||
if (chosen_monitor(&mon, 0)) r->w = mon.rc.w, r->h = mon.rc.h, r->ok = 1;
|
||||
}
|
||||
|
||||
// Pixels of the monitor the window is on; 0 when unknown. Before the window exists (widescreen.c wide_init at start) a
|
||||
// fullscreen window opens on the monitor NX2NAVI_MONITOR picks, so that one's size; without a choice the system decides
|
||||
// where the window goes, which is not known yet.
|
||||
int gfx_window_monitor_size(int *w, int *h) {
|
||||
if (g_win) {
|
||||
const SDL_DisplayMode *m = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_win));
|
||||
if (!m) return 0;
|
||||
*w = (int)(m->w * m->pixel_density + 0.5f);
|
||||
*h = (int)(m->h * m->pixel_density + 0.5f);
|
||||
return 1;
|
||||
}
|
||||
SizeReq r = {0};
|
||||
if (monitor_unset(cfg_get("NX2NAVI_MONITOR")) || !video_up()) return 0;
|
||||
run_on_window(chosen_size, &r);
|
||||
*w = r.w, *h = r.h;
|
||||
return r.ok;
|
||||
}
|
||||
|
||||
// ---- gfx_wsys.h
|
||||
static int open_ok;
|
||||
|
||||
@@ -255,21 +442,47 @@ static void open_window(void *arg) {
|
||||
(void)arg;
|
||||
int w, h;
|
||||
gfx_cfg_window_size(&w, &h);
|
||||
g_win = SDL_CreateWindow("NX2navi", w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY);
|
||||
// NX2NAVI_MONITOR: the window is created centred on the chosen monitor; without a choice the system places it.
|
||||
MonInfo mon;
|
||||
int on_mon = chosen_monitor(&mon, 1);
|
||||
SDL_PropertiesID props = SDL_CreateProperties();
|
||||
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "NX2navi");
|
||||
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
|
||||
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
|
||||
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_VULKAN_BOOLEAN, true);
|
||||
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, true);
|
||||
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
|
||||
if (on_mon) {
|
||||
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED_DISPLAY(mon.id));
|
||||
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED_DISPLAY(mon.id));
|
||||
}
|
||||
g_win = SDL_CreateWindowWithProperties(props);
|
||||
SDL_DestroyProperties(props);
|
||||
if (!g_win) {
|
||||
rt_log("gfx: SDL_CreateWindow failed (%s)", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
set_pixel_size(w, h); // w x h were window units; the setting is in pixels
|
||||
// Centred again at the size it ended up with. SDL also takes this position as the display the fullscreen below
|
||||
// covers: a Wayland window cannot be moved, and this is how its fullscreen still opens on the chosen output.
|
||||
if (on_mon) SDL_SetWindowPosition(g_win, SDL_WINDOWPOS_CENTERED_DISPLAY(mon.id), SDL_WINDOWPOS_CENTERED_DISPLAY(mon.id));
|
||||
density_update();
|
||||
note_applied();
|
||||
refresh_rate_update();
|
||||
if (cfg_get("NX2NAVI_BACKGROUND_WINDOW")) rt_log("gfx: NX2NAVI_BACKGROUND_WINDOW has no effect with SDL windows");
|
||||
const char *monitor = cfg_get("NX2NAVI_MONITOR"); // open_window runs once, so this is logged once
|
||||
if (monitor && *monitor && strcasecmp(monitor, "auto"))
|
||||
rt_log("gfx: NX2NAVI_MONITOR=%s has no effect with SDL windows: monitor selection is Win32-only (gfx_window_win.c)", monitor);
|
||||
int wayland = !SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland");
|
||||
// open_window runs once, so this is logged once
|
||||
if (on_mon && wayland)
|
||||
rt_log("gfx: Wayland leaves the placement of a windowed window to the compositor, so NX2NAVI_MONITOR only decides "
|
||||
"where the game opens in fullscreen");
|
||||
if (gfx_cfg_fullscreen()) SDL_SetWindowFullscreen(g_win, true);
|
||||
SDL_SyncWindow(g_win);
|
||||
// A Wayland compositor places the fullscreen window after SDL_SyncWindow returns; handle reports it once the window
|
||||
// is on the chosen monitor.
|
||||
if (on_mon && (gfx_cfg_fullscreen() || !wayland)) {
|
||||
if (wayland && SDL_GetDisplayForWindow(g_win) != mon.id) g_place_pending = mon.id;
|
||||
else report_placement(mon.id);
|
||||
}
|
||||
input_sdl_window(g_win);
|
||||
open_ok = 1;
|
||||
}
|
||||
@@ -280,12 +493,6 @@ int wsys_open(void) {
|
||||
return open_ok;
|
||||
}
|
||||
|
||||
// --list-monitors (main.c) lists Win32 monitors only; see open_window for the setting.
|
||||
int list_monitors(void) {
|
||||
fprintf(stderr, "list-monitors: monitor selection is Win32-only; this build uses SDL windows\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
typedef struct { VkInstance inst; VkSurfaceKHR surface; } SurfaceReq;
|
||||
|
||||
static void make_surface(void *arg) {
|
||||
|
||||
@@ -211,9 +211,10 @@ extern const int rt_exefs_sha256_count;
|
||||
extern const char rt_default_exefs[], rt_default_romfs[], rt_default_profile[];
|
||||
extern const unsigned long long rt_app_pool_bytes; // the title's application memory pool (GetInfo 6)
|
||||
extern const int rt_nvn_api_version[2]; // NVN_DEVICE_INFO_API_{MAJOR,MINOR}_VERSION the title requires
|
||||
extern const int rt_game_version; // the game build: 110 (1.1.0), 121 (1.2.1) or 143 (1.4.3)
|
||||
extern const int rt_game_version; // the game build: 111 (1.1.1, make_game.py), 110 (1.1.0), 121 (1.2.1) or 143 (1.4.3)
|
||||
extern const char rt_port_version[]; // VERSION at the build's root: 0.1.0, 0.1.0+973c626 in a release
|
||||
#define RT_V110 (rt_game_version == 110) // 1.1.0: its own boot constants and data slots
|
||||
#define RT_V111 (rt_game_version == 111) // 1.1.1: its own boot constants (boot.c)
|
||||
#define RT_V143 (rt_game_version >= 143)
|
||||
|
||||
// ---- kernel (kernel.c)
|
||||
|
||||
@@ -8,9 +8,10 @@
|
||||
// show which device NX2NAVI_AUDIO_DEVICE picks; needs no game files
|
||||
// --monitor VALUE open the game window on one monitor: sets NX2NAVI_MONITOR before the settings are read, so it wins
|
||||
// over nx2navi.ini's `monitor =`. VALUE is auto (default), primary, a 1-based index as --list-monitors
|
||||
// prints it, or part of a GDI device name (DISPLAY3) or of a monitor's name, case-insensitive
|
||||
// --list-monitors print each monitor's index, GDI device name, name, desktop rectangle, primary flag and DPI;
|
||||
// needs no game files
|
||||
// prints it, or part of a monitor's name (on Windows also of its GDI device name, DISPLAY3),
|
||||
// case-insensitive. Wayland places a windowed window itself: only fullscreen opens on the monitor
|
||||
// --list-monitors print each monitor's index, name (on Windows also its GDI device name), desktop rectangle,
|
||||
// primary flag and DPI; needs no game files
|
||||
// --selftest layout, dispatch table and host-function registry; needs no game files
|
||||
// --probe-mods the mods folder scan, order and switches (mods.c); needs no game files
|
||||
// --probe-loader load and relocate the modules, check them against the measured 1.2.1 values
|
||||
|
||||
@@ -463,7 +463,8 @@ static tex_t make_tex(pool_t *pool, uint64_t off, int format, uint32_t w, uint32
|
||||
t.magic = MAGIC('T', 'E', 'X', 0);
|
||||
t.d.target = 1, t.d.format = format, t.d.width = w, t.d.height = h, t.d.depth = 1, t.d.levels = levels;
|
||||
nvn_tex_layout(&t.d, &t.L);
|
||||
t.pool = pool, t.off = (int64_t)off, t.mem = pool->mem + off, t.serial = serial;
|
||||
// pool_flags as nvnTextureInitialize records them: tex_mirrored (vk_texture.c) reads them, not the pool object
|
||||
t.pool = pool, t.pool_flags = (uint16_t)pool->flags, t.off = (int64_t)off, t.mem = pool->mem + off, t.serial = serial;
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -498,6 +499,35 @@ static int read_back(const tex_t *t, unsigned lv, uint8_t *const *lin, int gpu,
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Level 0 of image (w x h RGBA8) read through a staging buffer after everything recorded so far has run: whether every
|
||||
// texel is `want`, the way a draw sampling the image would see it.
|
||||
static int image_is_color(VkImage image, uint32_t w, uint32_t h, const uint8_t want[4]) {
|
||||
VkCtx *c = vk_ctx();
|
||||
VkBuffer rb;
|
||||
VmaAllocation ra;
|
||||
size_t n = (size_t)w * h * 4;
|
||||
uint8_t *r = vk_staging(n, &rb, &ra);
|
||||
VkCommandBuffer cb = vk_upload_begin();
|
||||
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_COPY_BIT,
|
||||
.dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT};
|
||||
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb};
|
||||
vkCmdPipelineBarrier2(cb, &di);
|
||||
VkBufferImageCopy rg = {.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .imageExtent = {w, h, 1}};
|
||||
vkCmdCopyImageToBuffer(cb, image, VK_IMAGE_LAYOUT_GENERAL, rb, 1, &rg);
|
||||
mb = (VkMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_COPY_BIT,
|
||||
.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_HOST_BIT,
|
||||
.dstAccessMask = VK_ACCESS_2_HOST_READ_BIT};
|
||||
vkCmdPipelineBarrier2(cb, &di);
|
||||
vk_upload_end();
|
||||
vk_upload_flush(1);
|
||||
vmaInvalidateAllocation(c->vma, ra, 0, VK_WHOLE_SIZE);
|
||||
int same = 1;
|
||||
for (size_t i = 0; i < n; i += 4) same &= !memcmp(r + i, want, 4);
|
||||
vmaDestroyBuffer(c->vma, rb, ra);
|
||||
return same;
|
||||
}
|
||||
|
||||
static int test_textures(pool_t *tpool, pool_t *phys) {
|
||||
uint8_t *lin[4] = {0};
|
||||
// block-linear RGBA8 with 3 levels in a CPU-visible pool: uploaded through the GPU, read back from the image
|
||||
@@ -536,14 +566,28 @@ static int test_textures(pool_t *tpool, pool_t *phys) {
|
||||
vk_write_texels(&g, NULL, &all, gl[0], 256, 256 * 64);
|
||||
if (read_back(&g, 0, gl, 0, "physical-pool RGBA8 WriteTexels")) return 1;
|
||||
|
||||
// a slot of a finalized texture follows the live texture at the same storage
|
||||
// a slot of a finalized texture follows the live texture at the same storage. The slot's own storage is CPU-written
|
||||
// (mirrored), so it follows only a texture holding GPU content its bytes lack (vk_texture.c slot_resolve). The live
|
||||
// texture is cleared on the GPU and noted as rendered into, as a draw into it would be; the slot's image must then
|
||||
// show the clear, which neither the storage bytes nor a recreated image of the slot's own descriptor hold.
|
||||
vk_texture_register(5, &b, NULL);
|
||||
vk_texture_release(&b);
|
||||
tex_t b2 = make_tex(tpool, 0x40000, 0x25, 16, 12, 1, 4);
|
||||
const VkTexImage *live = vk_texture_get(&b2);
|
||||
GCHECK(live && live->image && live->width == 16 && live->height == 12, "RGBA8 image of the texture at the slot's storage");
|
||||
vk_texture_gpu_written(&b2); // the clear below is its content, not the storage
|
||||
const float clear[4] = {0.0f, 1.0f, 1.0f, 1.0f};
|
||||
const uint8_t cleared[4] = {0x00, 0xFF, 0xFF, 0xFF};
|
||||
VkClearColorValue cv;
|
||||
memcpy(cv.float32, clear, sizeof cv.float32);
|
||||
VkImageSubresourceRange rng = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
VkCommandBuffer ccb = vk_upload_begin();
|
||||
vkCmdClearColorImage(ccb, live->image, VK_IMAGE_LAYOUT_GENERAL, &cv, 1, &rng);
|
||||
vk_upload_end();
|
||||
VkImage live_img = live ? live->image : VK_NULL_HANDLE;
|
||||
const VkTexImage *slot = vk_texture_by_id(5, NULL, NULL);
|
||||
GCHECK(live && slot && slot->image == live_img, "slot 5 does not follow the live texture at its storage");
|
||||
GCHECK(image_is_color(slot->image, 16, 12, cleared), "the image of slot 5 does not hold the clear written into the live texture");
|
||||
samp_par sp;
|
||||
memset(&sp, 0, sizeof sp);
|
||||
sp.minf = NVN_MIN_FILTER_LINEAR_MIPMAP_LINEAR, sp.magf = NVN_MAG_FILTER_LINEAR, sp.ws = sp.wt = sp.wr = NVN_WRAP_MODE_REPEAT;
|
||||
@@ -551,7 +595,7 @@ static int test_textures(pool_t *tpool, pool_t *phys) {
|
||||
vk_sampler_register(0, &sp);
|
||||
VkSampler s0 = vk_sampler_by_id(0), s1 = vk_sampler_for(&sp);
|
||||
GCHECK(s0 && s0 == s1 && !vk_sampler_by_id(1), "sampler slots");
|
||||
g_gpu_checks += 7;
|
||||
g_gpu_checks += 9;
|
||||
vk_texture_release(&t), vk_texture_release(&b2), vk_texture_release(&g);
|
||||
for (int i = 0; i < 4; i++) free(lin[i]), free(bl[i]);
|
||||
free(gl[0]);
|
||||
|
||||
@@ -61,10 +61,11 @@ typedef struct {
|
||||
int gpl; // VK_EXT_graphics_pipeline_library enabled with fast linking (NX2NAVI_GPL=0: off)
|
||||
int amd; // an AMD GPU (vendor 0x1002), any driver
|
||||
int amd_sc; // AMD's Windows driver branch for RDNA 1 and 2 ("AMD proprietary shader compiler")
|
||||
int amd_llpc; // AMD's own driver for RDNA 3 and newer (its driverInfo names LLPC)
|
||||
int mesa; // a Mesa driver the game runs on: RADV, ANV or NVK
|
||||
} VkCaps;
|
||||
extern VkCaps vk_caps;
|
||||
void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, mesa
|
||||
void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, amd_llpc, mesa
|
||||
void vk_caps_features(VkPhysicalDeviceFeatures *f); // before vkCreateDevice (NX2NAVI_VK_NO_FLOAT64 clears shaderFloat64)
|
||||
// Before vkCreateDevice, with the driver's graphics pipeline library feature and properties: sets vk_caps.gpl,
|
||||
// 1 = enable the extensions (VK_KHR_pipeline_library, VK_EXT_graphics_pipeline_library) and the feature.
|
||||
|
||||
@@ -44,6 +44,7 @@ void vk_caps_driver(VkPhysicalDevice p) {
|
||||
// AMD split RDNA 1 and 2 off into a driver branch of their own; its driverInfo reads
|
||||
// "26.10.1 (AMD proprietary shader compiler)" where the RDNA 3 and 4 driver names LLPC.
|
||||
vk_caps.amd_sc = drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY && strstr(drv.driverInfo, "shader compiler");
|
||||
vk_caps.amd_llpc = drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY && !vk_caps.amd_sc;
|
||||
vk_caps.mesa = drv.driverID == VK_DRIVER_ID_MESA_RADV || drv.driverID == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA ||
|
||||
drv.driverID == VK_DRIVER_ID_MESA_NVK;
|
||||
}
|
||||
|
||||
@@ -105,8 +105,13 @@ static void pass_marks_init(VkCtx *c) {
|
||||
set_checkpoint = c->has_nv_checkpoints
|
||||
? (PFN_vkCmdSetCheckpointNV)vk_dpa(c->device, "vkCmdSetCheckpointNV") : NULL;
|
||||
pm_amd = !off && write_marker && words;
|
||||
vk_pass_marks_on = pm_amd || set_checkpoint;
|
||||
if (vk_pass_marks_on)
|
||||
// Timestamp profiling needs pass labels, but no vendor breadcrumb extension.
|
||||
const char *timing = cfg_get("NX2NAVI_GPU_PASSES");
|
||||
int timestamps = timing && *timing && strcmp(timing, "0");
|
||||
vk_pass_marks_on = pm_amd || set_checkpoint || timestamps;
|
||||
if (timestamps && !pm_amd && !set_checkpoint)
|
||||
rt_log("vk: pass labels on for timestamp profiling; vendor breadcrumbs unavailable");
|
||||
else if (vk_pass_marks_on)
|
||||
rt_log("vk: pass marks on (%s%s%s; NX2NAVI_PASS_CRUMBS=0 turns them off%s)", pm_amd ? "VK_AMD_buffer_marker" : "",
|
||||
pm_amd && set_checkpoint ? " and " : "", set_checkpoint ? "VK_NV_device_diagnostic_checkpoints" : "",
|
||||
set_checkpoint ? "" : ", =1 adds NVIDIA checkpoints where the driver has them");
|
||||
|
||||
@@ -466,6 +466,7 @@ static long g_errors_total; // outlives the device so the probe can print it
|
||||
static void destroy_all(void) {
|
||||
gfx_window_release_vk();
|
||||
if (g_vk.device) vkDeviceWaitIdle(g_vk.device);
|
||||
if (g_vk.device) vk_dlaa_release(); // the DLSS features and NGX: the allocator and the device still live
|
||||
if (g_vk.device) vk_astc_release();
|
||||
if (g_vk.device) vk_crumbs_release();
|
||||
if (g_vk.timeline) vkDestroySemaphore(g_vk.device, g_vk.timeline, NULL);
|
||||
|
||||
@@ -2,13 +2,16 @@
|
||||
// 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.
|
||||
// compiled in: the few NGX types used are declared here from the SDK ABI and the Linux core's shutdown interface.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// NGX and DLAA's resources live as long as the device: vk_dlaa_init brings NGX up on the new device, and
|
||||
// vk_dlaa_release (Linux) frees everything DLAA made and shuts NGX down before the device is destroyed.
|
||||
//
|
||||
// Where DLAA runs in the frame, the jitter and the motion vectors: see the sections below.
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@@ -60,6 +63,11 @@ 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 *);
|
||||
#ifndef _WIN32
|
||||
// NVSDK_NGX_VULKAN_Shutdown1 as the Linux core exports it: the device and a pointer that receives the devices NGX still
|
||||
// serves afterwards (vk_dlaa_release).
|
||||
typedef NgxResult (*PFN_Shutdown1)(VkDevice, unsigned *remaining);
|
||||
#endif
|
||||
|
||||
static struct {
|
||||
void *core; // the NGX core library (an HMODULE on Windows)
|
||||
@@ -74,6 +82,12 @@ static struct {
|
||||
int ngx_up; // vk_dlaa_init brought NGX up: DLSS serves this run
|
||||
NgxParam *capsp; // the capability parameters (vk_dlaa_init), which DLSS's statistics callback reads and fills
|
||||
int wanted; // -1 not read yet
|
||||
#ifndef _WIN32
|
||||
// Linux teardown (vk_dlaa_release)
|
||||
PFN_Shutdown1 shutdown1; // required: core_load leaves DLAA off without it
|
||||
VkDevice device; // what NGX was initialised with: set while vk_dlaa_release owes it a shutdown
|
||||
NgxParam *caps_map; // GetCapabilityParameters' map: allocated for the caller, destroyed by vk_dlaa_release
|
||||
#endif
|
||||
} N = {.wanted = -1};
|
||||
|
||||
typedef void (*SetI)(NgxParam *, const char *, int);
|
||||
@@ -123,6 +137,7 @@ int vk_dlaa_wanted(void) {
|
||||
// The temporal upscaler behind dlaa: NVIDIA DLSS through NGX, once NGX is up, or else AMD FSR 3.1, which takes the same
|
||||
// inputs. The DLSS mode setting (NX2NAVI_DLSS) is the render scale for both.
|
||||
static int fsr3(void) { return !N.ngx_up && !strcmp(cfg_value("NX2NAVI_TAA"), "fsr3"); }
|
||||
static int fsr_frame; // fsr3() as vk_dlaa_init and then vk_dlaa_frame read it: the upscaler this frame runs with
|
||||
int vk_dlaa_fsr3(void) { return vk_dlaa_wanted() && fsr3(); }
|
||||
|
||||
// The upscaler the menu has set cannot start in this run: DLSS when NGX was never tried (a device made without its
|
||||
@@ -161,6 +176,9 @@ static int core_load(void) {
|
||||
#define FN(f, name) N.f = plat_lib_sym(N.core, name)
|
||||
#endif
|
||||
FN(init, "NVSDK_NGX_VULKAN_Init_Ext2");
|
||||
#ifndef _WIN32
|
||||
FN(shutdown1, "NVSDK_NGX_VULKAN_Shutdown1"); // Linux: checked below
|
||||
#endif
|
||||
FN(req_ext, "NVSDK_NGX_VULKAN_RequiredExtensions");
|
||||
FN(caps, "NVSDK_NGX_VULKAN_GetCapabilityParameters");
|
||||
FN(alloc_params, "NVSDK_NGX_VULKAN_AllocateParameters");
|
||||
@@ -173,6 +191,16 @@ static int core_load(void) {
|
||||
rt_log("dlaa: the NGX core %s lacks Vulkan entry points: DLAA off", path);
|
||||
return 0;
|
||||
}
|
||||
#ifndef _WIN32
|
||||
// Without Shutdown1 the device cannot be taken back from NGX (vk_dlaa_release), and the process then segfaults after
|
||||
// the Vulkan teardown (NVIDIA 615.78.08): DLAA stays off rather than run with a teardown known to crash. The
|
||||
// parameter maps are destroyed there too.
|
||||
if (!N.shutdown1 || !N.destroy_params) {
|
||||
rt_log("dlaa: the NGX core %s lacks NVSDK_NGX_VULKAN_Shutdown1 or DestroyParameters, which its teardown needs: "
|
||||
"DLAA off", path);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
N.loaded = 1;
|
||||
rt_log("dlaa: NGX core %s", path);
|
||||
return 1;
|
||||
@@ -247,6 +275,7 @@ static int utf8_to_wide(const char *in, wchar_t *out, size_t cap) {
|
||||
|
||||
void vk_dlaa_init(void) {
|
||||
if (vk_dlaa_wanted() && fsr3()) {
|
||||
fsr_frame = 1;
|
||||
N.available = vk_ffx_load();
|
||||
rt_log("dlaa: the temporal upscaler is AMD FSR 3.1%s", N.available ? "" : ", which is missing: the game's own scaling");
|
||||
return;
|
||||
@@ -276,10 +305,16 @@ void vk_dlaa_init(void) {
|
||||
rt_log("dlaa: NGX init failed (%08x): DLAA off", (unsigned)r);
|
||||
return;
|
||||
}
|
||||
#ifndef _WIN32
|
||||
N.device = c->device; // NGX holds the device from here, also if the layout check below turns DLAA off
|
||||
#endif
|
||||
if (!vtable_ok()) return;
|
||||
NgxParam *caps = NULL;
|
||||
int avail = 0, driver = 0;
|
||||
if (NGX_OK(N.caps(&caps)) && caps) {
|
||||
#ifndef _WIN32
|
||||
N.caps_map = caps; // allocated for us (the SDK header: free it with DestroyParameters): vk_dlaa_release does
|
||||
#endif
|
||||
p_geti(caps, "SuperSampling.Available", &avail);
|
||||
p_geti(caps, "SuperSampling.NeedsUpdatedDriver", &driver);
|
||||
}
|
||||
@@ -451,6 +486,10 @@ const VkTexImage *vk_dlaa_swap_img;
|
||||
VkImageView vk_dlaa_swap_view;
|
||||
|
||||
static int dl_active(void) { return N.available && !D.failed && D.on; }
|
||||
// The upscaler this frame runs with, as vk_dlaa_frame read it. The menu can set NX2NAVI_TAA to dlss between that and
|
||||
// evaluate; read again there while NGX is not up (DLSS starts only after a restart), it sent evaluate to DLSS's path
|
||||
// with NGX's functions not loaded, a call to address 0 (issue #325).
|
||||
static int use_fsr(void) { return !N.ngx_up && fsr_frame; }
|
||||
|
||||
static void dl_fail(const char *what, NgxResult r) {
|
||||
rt_log("dlaa: %s failed (%08x): DLAA off for this run", what, (unsigned)r);
|
||||
@@ -472,9 +511,10 @@ static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage,
|
||||
return vkCreateImageView(c->device, &vi, NULL, view) == VK_SUCCESS;
|
||||
}
|
||||
|
||||
static void free_size(struct Size *e) {
|
||||
// The images and views of a size, then its feature. The device must be idle: free_size waits for it, vk_dlaa_release
|
||||
// has. Whatever was not made is skipped, so a half-made size goes the same way.
|
||||
static void destroy_size(struct Size *e) {
|
||||
const VkCtx *c = vk_ctx();
|
||||
vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // 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);
|
||||
@@ -491,6 +531,11 @@ static void free_size(struct Size *e) {
|
||||
memset(e, 0, sizeof *e);
|
||||
}
|
||||
|
||||
static void free_size(struct Size *e) {
|
||||
vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock(); // a third scene size: the evicted images may still be in flight
|
||||
destroy_size(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. NX2navi's dynamic resolution switches between a few sizes; each keeps its feature, the least recently used
|
||||
@@ -682,9 +727,11 @@ static int mv_init(void) {
|
||||
.layout = D.mv_layout};
|
||||
VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &D.mv_pipe);
|
||||
vkDestroyShaderModule(c->device, m, NULL);
|
||||
m = VK_NULL_HANDLE;
|
||||
if (r != VK_SUCCESS) goto fail;
|
||||
return D.mv_ok = 1;
|
||||
fail:
|
||||
if (m) vkDestroyShaderModule(c->device, m, NULL); // made before a later step failed
|
||||
rt_log("dlaa: the motion-vector pass could not be set up: DLAA runs with zero motion");
|
||||
return 0;
|
||||
}
|
||||
@@ -1027,16 +1074,18 @@ static int mask_init(void) {
|
||||
.layout = R.layout};
|
||||
VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &R.pipe);
|
||||
vkDestroyShaderModule(c->device, m, NULL);
|
||||
m = VK_NULL_HANDLE;
|
||||
if (r != VK_SUCCESS) goto fail;
|
||||
return R.ok = 1;
|
||||
fail:
|
||||
if (m) vkDestroyShaderModule(c->device, m, NULL); // made before a later step failed
|
||||
rt_log("dlaa: the reactive mask pass could not be set up: DLSS runs without it");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void free_snap(struct Snap *s) {
|
||||
// A snapshot's images and views. The device must be idle: free_snap waits for it, vk_dlaa_release has.
|
||||
static void destroy_snap(struct Snap *s) {
|
||||
const VkCtx *c = vk_ctx();
|
||||
vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // a fourth size: the evicted images may still be in flight
|
||||
if (s->before_view) vkDestroyImageView(c->device, s->before_view, NULL);
|
||||
if (s->mask_view) vkDestroyImageView(c->device, s->mask_view, NULL);
|
||||
if (s->before) vmaDestroyImage(c->vma, s->before, s->before_mem);
|
||||
@@ -1044,6 +1093,11 @@ static void free_snap(struct Snap *s) {
|
||||
memset(s, 0, sizeof *s);
|
||||
}
|
||||
|
||||
static void free_snap(struct Snap *s) {
|
||||
vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock(); // a fourth size: the evicted images may still be in flight
|
||||
destroy_snap(s);
|
||||
}
|
||||
|
||||
static struct Snap *snap_for(VkCommandBuffer cb, uint32_t w, uint32_t h, VkFormat f) {
|
||||
static uint64_t tick;
|
||||
struct Snap *e = NULL, *lru = &R.s[0];
|
||||
@@ -1119,6 +1173,55 @@ static struct Snap *mask_pass(VkCommandBuffer cb, VkImageView cv, uint32_t w, ui
|
||||
return s;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- teardown
|
||||
// The HUD-less picture's images, a half-made pair too. The device must be idle, or the images not yet used.
|
||||
static void hl_destroy(void) {
|
||||
const VkCtx *c = vk_ctx();
|
||||
if (D.hl_view) vkDestroyImageView(c->device, D.hl_view, NULL);
|
||||
if (D.hl) vmaDestroyImage(c->vma, D.hl, D.hl_mem);
|
||||
if (D.hl_srgb) vmaDestroyImage(c->vma, D.hl_srgb, D.hl_srgb_mem);
|
||||
D.hl_view = VK_NULL_HANDLE, D.hl = D.hl_srgb = VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
// The device is idle here. Release NGX features and parameters before their Vulkan resources, then shut down NGX even
|
||||
// if initialization succeeded but the layout check failed. The Linux core's Shutdown1 takes an extra output pointer
|
||||
// compared with the SDK's public wrapper; it reports the number of devices still using NGX.
|
||||
void vk_dlaa_release(void) {
|
||||
#ifndef _WIN32
|
||||
const VkCtx *c = vk_ctx();
|
||||
if (!c) return;
|
||||
for (int i = 0; i < 5; i++)
|
||||
if (D.sizes[i].feat) N.release(D.sizes[i].feat), D.sizes[i].feat = NULL;
|
||||
if (D.params) N.destroy_params(D.params);
|
||||
if (N.caps_map) N.destroy_params(N.caps_map);
|
||||
for (int i = 0; i < 5; i++) destroy_size(&D.sizes[i]);
|
||||
for (int i = 0; i < SNAPS; i++) destroy_snap(&R.s[i]);
|
||||
hl_destroy();
|
||||
if (D.mv_pipe) vkDestroyPipeline(c->device, D.mv_pipe, NULL);
|
||||
if (D.mv_layout) vkDestroyPipelineLayout(c->device, D.mv_layout, NULL);
|
||||
if (D.mv_dsl) vkDestroyDescriptorSetLayout(c->device, D.mv_dsl, NULL);
|
||||
if (D.mv_samp) vkDestroySampler(c->device, D.mv_samp, NULL);
|
||||
if (D.camb) vmaDestroyBuffer(c->vma, D.camb, D.camb_mem);
|
||||
if (D.mvm) vmaDestroyBuffer(c->vma, D.mvm, D.mvm_mem); // mapped for good: the destroy unmaps it
|
||||
if (R.pipe) vkDestroyPipeline(c->device, R.pipe, NULL);
|
||||
if (R.layout) vkDestroyPipelineLayout(c->device, R.layout, NULL);
|
||||
if (R.dsl) vkDestroyDescriptorSetLayout(c->device, R.dsl, NULL);
|
||||
if (T.pool) vkDestroyQueryPool(c->device, T.pool, NULL);
|
||||
memset(&D, 0, sizeof D), D.mv_ok = -1, D.cfg_seen = ~0u;
|
||||
memset(&R, 0, sizeof R), R.ok = -1, R.knob = -1;
|
||||
memset(&T, 0, sizeof T), T.on = -1;
|
||||
memset(&F, 0, sizeof F);
|
||||
vk_dlaa_swap_img = NULL, vk_dlaa_swap_view = VK_NULL_HANDLE;
|
||||
if (!N.device) return; // NGX never came up on this device: DLAA off, FSR 3.1, or its init failed
|
||||
unsigned left = 0;
|
||||
vk_queue_lock(); // NGX may wait for the device itself
|
||||
NgxResult r = N.shutdown1(N.device, &left);
|
||||
vk_queue_unlock();
|
||||
rt_log("dlaa: NGX shut down (%08x, %u devices left)", (unsigned)r, left);
|
||||
N.device = NULL, N.caps_map = NULL, N.ngx_up = 0, N.available = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 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
|
||||
@@ -1334,7 +1437,11 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) {
|
||||
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 (!fsr3() && !D.params && !NGX_OK(N.alloc_params(&D.params))) return dl_fail("parameter allocation", 0);
|
||||
// Read once: vk_dlaa_frame runs on the present's thread and can clear fsr_frame while this replay is between the
|
||||
// checks below, which would send the rest of this function to DLSS's path without NGX after all.
|
||||
const int fsr = use_fsr();
|
||||
if (!fsr && !N.ngx_up) return; // neither runs this frame: vk_dlaa_frame turns DLAA off at the next present
|
||||
if (!fsr && !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);
|
||||
@@ -1358,7 +1465,7 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) {
|
||||
uint64_t c0 = timed ? kern_now_ns() : 0;
|
||||
rec_drain(); // NGX records into the command buffer itself: the recorder thread must have made every call so far
|
||||
if (timed) T.cpu_drain += kern_now_ns() - c0;
|
||||
if (fsr3()) { // FSR 3.1 (vk_ffx.cpp) in DLSS's place: the same images, then the same blit below
|
||||
if (fsr) { // FSR 3.1 (vk_ffx.cpp) in DLSS's place: the same images, then the same blit below
|
||||
if (t->level || t->base_layer || z->level || z->base_layer) return dl_fail("FSR 3.1 on a target that is not level 0, layer 0", 0);
|
||||
if (D.last_color != t->image) target_changed(t);
|
||||
uint64_t now = plat_now_ns();
|
||||
@@ -1502,16 +1609,15 @@ static void hudless(VkCommandBuffer cb, struct Size *e, uint32_t w, uint32_t h)
|
||||
VkFormat fmt = hdr ? VK_FORMAT_R16G16B16A16_SFLOAT : VK_FORMAT_R8G8B8A8_UNORM;
|
||||
if (D.hl && (D.hl_w != w || D.hl_h != h || D.hl_fmt != fmt)) {
|
||||
vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock();
|
||||
vkDestroyImageView(c->device, D.hl_view, NULL);
|
||||
vmaDestroyImage(c->vma, D.hl, D.hl_mem);
|
||||
if (D.hl_srgb) vmaDestroyImage(c->vma, D.hl_srgb, D.hl_srgb_mem);
|
||||
D.hl = D.hl_srgb = VK_NULL_HANDLE;
|
||||
hl_destroy();
|
||||
}
|
||||
if (!D.hl) {
|
||||
VkImageView v2 = VK_NULL_HANDLE;
|
||||
VkImageUsageFlags u = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||
if ((!hdr && !image2d(w, h, VK_FORMAT_R8G8B8A8_SRGB, u, &D.hl_srgb, &D.hl_srgb_mem, &v2)) ||
|
||||
!image2d(w, h, fmt, u | VK_IMAGE_USAGE_STORAGE_BIT, &D.hl, &D.hl_mem, &D.hl_view)) {
|
||||
if (v2) vkDestroyImageView(c->device, v2, NULL); // the first image's view, when the second image failed
|
||||
hl_destroy(); // and what of the two images was made
|
||||
rt_log("dlaa: the HUD-less picture's images could not be made: frame generation sees the HUD");
|
||||
vk_dlaa_hudless_wanted = 0;
|
||||
return;
|
||||
@@ -1692,7 +1798,10 @@ void vk_dlaa_frame(void) {
|
||||
vk_dlaa_swap_img = NULL, vk_dlaa_swap_view = VK_NULL_HANDLE;
|
||||
dlss_mode();
|
||||
preset_check();
|
||||
if (!N.ngx_up) N.available = fsr3() && cfg_bool("NX2NAVI_DLAA") && vk_ffx_load(); // FSR 3.1 follows the setting
|
||||
if (!N.ngx_up) { // FSR 3.1 follows the setting
|
||||
fsr_frame = fsr3();
|
||||
N.available = fsr_frame && cfg_bool("NX2NAVI_DLAA") && vk_ffx_load();
|
||||
}
|
||||
int on = N.available && cfg_bool("NX2NAVI_DLAA");
|
||||
if (on != D.on) {
|
||||
rt_log("dlaa: %s", on ? "on" : "off");
|
||||
|
||||
@@ -33,6 +33,11 @@ int vk_dlaa_fg_camera(VkDlaaCamera *c); // ... and its temporal upscaler is
|
||||
void vk_dlaa_instance_exts(const char **ext, uint32_t *n, uint32_t max);
|
||||
void vk_dlaa_device_exts(const VkExtensionProperties *have, uint32_t nhave, const char **ext, uint32_t *n, uint32_t max);
|
||||
void vk_dlaa_init(void);
|
||||
// vk_device.c destroy_all, once the device is idle and before the allocator and the device go (Linux): releases the NGX
|
||||
// features and parameters, frees every image, buffer, pipeline and query pool DLAA made, resets its state for a later
|
||||
// device and shuts NGX down for this one. Safe when NGX never came up, when nothing was made, and more than once.
|
||||
// Windows: does nothing.
|
||||
void vk_dlaa_release(void);
|
||||
// 1 when dlaa is on and NGX came up with DLSS (after vk_dlaa_init): a DLSS mode's smaller scene is scaled up by DLSS.
|
||||
int vk_dlaa_available(void);
|
||||
// The video memory DLSS reports for its features (NGX's statistics), for the log; -1 while unknown.
|
||||
|
||||
@@ -873,7 +873,8 @@ static void set_rt(VkExec *ex, const p_set_rt *r) {
|
||||
// command-buffer state is lost there (bindings and dynamic state: vk_exec_draw_new_cb, ex->dirty); barriers, layouts and
|
||||
// queries are not: a barrier's first scope is everything earlier in submission order, image layouts belong to the image,
|
||||
// and occlusion queries end with their pass. Fences, completion callbacks and reports stay per submission: only the last
|
||||
// part signals the value.
|
||||
// part signals the value. The texture pool set a part binds is sealed when the part goes (vk_exec_pool_split): later
|
||||
// parts write and bind another, and a part is not split off while no other set is free of this submission.
|
||||
// A part also spans at least NX2NAVI_SPLIT_US (1000) of replay time: a split costs about 40 us of the render thread
|
||||
// (vkQueueSubmit2, a command buffer, the mirror prologue), and a fight frame of 2,000 draws made 15 parts on the 7900X;
|
||||
// a millisecond of recording is a fraction of a millisecond of GPU work, so the GPU still starts early.
|
||||
@@ -925,8 +926,8 @@ static void maybe_split(VkExec *ex) {
|
||||
}
|
||||
split_draws = 0;
|
||||
split_at = split_n;
|
||||
if (idle && ex->recorded != part_start) n_idle_parts++;
|
||||
if (ex->recorded == part_start) return;
|
||||
if (ex->recorded == part_start || !vk_exec_pool_split(ex->fence)) return;
|
||||
if (idle) n_idle_parts++;
|
||||
if (ex->in_pass) vk_exec_pass_end(ex), n_pass_splits++; // the next draw begins the pass again, in the new part
|
||||
ex->cb = vk_sync_split();
|
||||
part_start = ex->recorded;
|
||||
|
||||
@@ -137,6 +137,10 @@ void vk_exec_inline_end(int reopen);
|
||||
// vk_exec_draw.c: forget what the command buffer being recorded has bound (pipeline, sets, dynamic state, vertex
|
||||
// buffers): the replay continues in a new one (vk_sync_split).
|
||||
void vk_exec_draw_new_cb(void);
|
||||
// vk_exec_draw.c, before a part of submission fence goes to the queue (vk_exec.c maybe_split): 1 when it may go. The
|
||||
// texture pool set it binds is then sealed and the next draw that needs the pools takes another; 0 when every other set
|
||||
// is bound by this submission's earlier parts, and the replay keeps recording into the same part.
|
||||
int vk_exec_pool_split(uint64_t fence);
|
||||
// vk_exec_draw.c: the vertex and fragment shader of the last draw recorded into this command buffer (NULL: none yet).
|
||||
void vk_exec_draw_shaders(const GfxShader **vs, const GfxShader **fs);
|
||||
// vk_exec_draw.c "pool marks": at the end of a submission that recorded work, after its closing barrier. Hands the
|
||||
|
||||
@@ -578,6 +578,7 @@ static int texel_bytes(VkFormat f) {
|
||||
case VK_FORMAT_R16G16_SINT: case VK_FORMAT_R16G16_SFLOAT: case VK_FORMAT_R16G16_UNORM: case VK_FORMAT_R8G8B8A8_UINT:
|
||||
case VK_FORMAT_R8G8B8A8_SINT: case VK_FORMAT_R8G8B8A8_UNORM: case VK_FORMAT_B8G8R8A8_UNORM:
|
||||
case VK_FORMAT_A2B10G10R10_UINT_PACK32: case VK_FORMAT_A2B10G10R10_UNORM_PACK32: case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
|
||||
case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32:
|
||||
return 4;
|
||||
case VK_FORMAT_R32G32_UINT: case VK_FORMAT_R32G32_SINT: case VK_FORMAT_R32G32_SFLOAT: case VK_FORMAT_R16G16B16A16_UINT:
|
||||
case VK_FORMAT_R16G16B16A16_SINT: case VK_FORMAT_R16G16B16A16_SFLOAT: case VK_FORMAT_R16G16B16A16_UNORM: return 8;
|
||||
@@ -1706,8 +1707,13 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto
|
||||
// dialogue frames, a fifth of them; none once each submission waited for its GPU work, NX2NAVI_SUBMIT_FINISH). A
|
||||
// submission's first pool_sync takes the next set (pool_take_set), waits for the GPU only if that set's last
|
||||
// submission is still running, and brings it up to date from the change log (every entry written since the set was
|
||||
// last current; the whole set when the log has wrapped). Writes within a submission go to its own set at once, as
|
||||
// before.
|
||||
// last current; the whole set when the log has wrapped). Writes within a submission go to its own set at once, until a
|
||||
// part of the submission goes to the queue early (vk_exec.c maybe_split): that part may read any entry of the set, so
|
||||
// the set is sealed (vk_exec_pool_split) and the next sync takes the following set as if a new submission began.
|
||||
// Writing into it instead updated descriptors a pending command buffer reads, which Vulkan does not allow: the part
|
||||
// sampled textures registered after it went, and on RADV a texture whose memory was mapped after the part's submit is
|
||||
// absent from that submit's page tables, a GPU page fault on the 128x128 probe pass of Kokiri Forest's first frame
|
||||
// (RADV's radv_wait_for_vm_map_updates hid it, as did NX2NAVI_SPLIT_DRAWS=0).
|
||||
volatile uint32_t vk_pool_kinds_used;
|
||||
#define POOL_SETS 4
|
||||
#define POOL_LOG (1u << 20) // entries of the change log: binding * VK_POOL_TEX + id, one per write
|
||||
@@ -1726,7 +1732,7 @@ static struct {
|
||||
uint64_t switches, waits, catchup, written, rewrites, catch_ns, sync_ns, n_slots, n_refill, n_fresh;
|
||||
uint64_t n_by_reg, n_by_addr; // refills whose token changed by a registration, by an address generation only
|
||||
uint64_t dirty_pos; // vk_texture_dirty_slots_locked position (0: the next sync walks every slot)
|
||||
int ready, uab;
|
||||
int ready, uab, sealed; // sealed: a part of the current submission already reads sets[cur]
|
||||
uint32_t kinds, gen, epoch, sgen;
|
||||
int retry; // the last sync left slots to the creation budget
|
||||
uint64_t frame, frame_ns; // the frame of the last sync, and the sync time spent in it (pool_create_ns)
|
||||
@@ -1991,18 +1997,29 @@ static void pool_entry_to(VkDescriptorSet set, uint32_t e) {
|
||||
// A submission's first pool use: the next set of the ring, once the GPU is done with it. 1 when it switched; *from is
|
||||
// the log position the set holds writes up to (pool_catch_up brings it level once pool_sync has refreshed the entries:
|
||||
// a view written into a set must be alive, and the refresh is what replaces the views of destroyed textures).
|
||||
static int pool_current(void);
|
||||
|
||||
static int pool_take_set(VkExec *ex, uint64_t *from) {
|
||||
if (PL.cur_fence == ex->fence) return 0;
|
||||
// a sealed set stays bound while it is current: reading it is what the pending part does too
|
||||
if (PL.cur_fence == ex->fence && (!PL.sealed || pool_current())) return 0;
|
||||
int i = (PL.cur + 1) % PL.nsets;
|
||||
if (PL.nsets > 1) {
|
||||
const GfxBackend *b = gfx_backend();
|
||||
if (b->fence_done() < PL.set_fence[i]) PL.waits++, b->fence_wait(PL.set_fence[i], UINT64_MAX);
|
||||
}
|
||||
*from = PL.set_synced[i];
|
||||
PL.cur = i, PL.set = PL.sets[i], PL.cur_fence = ex->fence, PL.set_fence[i] = ex->fence;
|
||||
PL.cur = i, PL.set = PL.sets[i], PL.cur_fence = ex->fence, PL.set_fence[i] = ex->fence, PL.sealed = 0;
|
||||
return PL.nsets > 1;
|
||||
}
|
||||
|
||||
int vk_exec_pool_split(uint64_t fence) {
|
||||
if (PL.cur_fence != fence || PL.sealed) return 1; // no part of this submission has the current set unsealed
|
||||
// pool_take_set moves to the next set of the ring: it must not be bound by this submission's earlier parts too
|
||||
if (PL.nsets < 2 || PL.set_fence[(PL.cur + 1) % PL.nsets] == fence) return 0;
|
||||
PL.sealed = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The current set gets the entries written into other sets in [from, to) of the log, as PL.view / PL.samp hold them now.
|
||||
static uint64_t pool_memo_hits; // pool_fill: slots that shared another slot's lookup (defined with pool_memo below)
|
||||
|
||||
@@ -2130,6 +2147,7 @@ static void pool_sync(VkExec *ex) {
|
||||
if (!PL.layout) vk_pool_set_layout();
|
||||
uint64_t from = 0, pre = PL.logn;
|
||||
int switched = pool_take_set(ex, &from); // every submission its own set, before the shortcut
|
||||
if (PL.sealed) return; // current when checked: a change since waits for the next sync, which takes a new set
|
||||
uint64_t t0 = kern_now_ns(), f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
if (f != PL.frame) PL.frame = f, PL.frame_ns = 0;
|
||||
pool_sync_entries(ex);
|
||||
@@ -2169,6 +2187,12 @@ static void pool_dirty_slot(int id, void *arg) { // vk_texture_dirty_slots_loc
|
||||
else d->retry = 1;
|
||||
}
|
||||
|
||||
// Every entry as the slots, kinds and samplers are now (pool_sync_entries has nothing to write).
|
||||
static int pool_current(void) {
|
||||
return PL.ready && !PL.retry && __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED) == PL.kinds &&
|
||||
vk_texture_slots_gen() == PL.gen && vk_texture_epoch() == PL.epoch && vk_sampler_gen() == PL.sgen;
|
||||
}
|
||||
|
||||
static void pool_sync_entries(VkExec *ex) {
|
||||
uint32_t used = __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED);
|
||||
uint32_t gen = vk_texture_slots_gen(), ep = vk_texture_epoch(), sg = vk_sampler_gen();
|
||||
|
||||
@@ -79,7 +79,7 @@ static struct {
|
||||
int dyn_vi, dyn_clamp, dyn_poly, dyn_blend_en, dyn_blend_eq, dyn_wmask, dyn_a2c;
|
||||
int f_logic_op, f_depth_clamp, f_fill_non_solid, f_dual_src, f_full_subgroups;
|
||||
int cache_control; // pipelineCreationCacheControl: a create can ask for the pipeline caches only
|
||||
int feedback; // NX2NAVI_PIPE_FEEDBACK: 1 count pipeline cache hits, 2 also log each compute build
|
||||
int feedback; // NX2NAVI_PIPE_FEEDBACK: 1 count pipeline cache hits, 2 also log each compute and graphics build
|
||||
uint64_t cache_loaded; // bytes of pipeline-cache.bin the driver took at boot
|
||||
int pv_last; // provokingVertexLast enabled: NVN's default is the last vertex
|
||||
VkShaderStageFlags sg32_stages; // stages that accept requiredSubgroupSize = 32 (0: not available)
|
||||
@@ -509,11 +509,19 @@ typedef struct { VkStructureType sType; const void *pNext; VkPipelineCreateFlags
|
||||
// copy, reading through a null entry, with compute and library builds running beside the autosave.
|
||||
static PlatMutex cache_rw = PLAT_MUTEX_INIT;
|
||||
|
||||
// A pipeline cache of this thread's own that its creates build into in place of P.cache; VK_NULL_HANDLE (every thread
|
||||
// but the background builder of large compute shaders) for P.cache itself. A create into it does not hold cache_rw, so
|
||||
// a build of minutes does not keep pipeline-cache.bin from being saved; bg_merge moves its content into P.cache.
|
||||
static __thread VkPipelineCache own_cache;
|
||||
static volatile uint32_t cache_merges; // bg_merge's merges into P.cache: new content for autosave_once to save
|
||||
|
||||
static VkResult pipe_create_in(int kind, void *ci, VkPipeline *pipe, VkPipelineCache cache) {
|
||||
if (cache) plat_mutex_lock_shared(&cache_rw);
|
||||
int shared = cache && !own_cache;
|
||||
if (cache && own_cache) cache = own_cache;
|
||||
if (shared) plat_mutex_lock_shared(&cache_rw);
|
||||
VkResult r = kind == 2 ? vkCreateComputePipelines(P.device, cache, 1, ci, NULL, pipe)
|
||||
: vkCreateGraphicsPipelines(P.device, cache, 1, ci, NULL, pipe);
|
||||
if (cache) plat_mutex_unlock_shared(&cache_rw);
|
||||
if (shared) plat_mutex_unlock_shared(&cache_rw);
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -723,14 +731,30 @@ static int ensure_init(void) {
|
||||
} else {
|
||||
rt_log("vk_pipeline: descriptor update templates unavailable: sets are written with write structs");
|
||||
}
|
||||
// NX2NAVI_VK_DYN_VERTEX=0/1 forces dynamic vertex input off or on. Default off on Intel's frozen Windows driver for its
|
||||
// older integrated GPUs (UHD 630, Ice Lake: Vulkan 1.3.215 and older), which draws nothing with it.
|
||||
// NX2NAVI_VK_DYN_VERTEX=0/1 forces dynamic vertex input off or on. Default off on two drivers:
|
||||
// - Intel's frozen Windows driver for its older integrated GPUs (UHD 630, Ice Lake: Vulkan 1.3.215 and older), which
|
||||
// draws nothing with it.
|
||||
// - AMD's driver for RDNA 3 and newer (vk_caps.amd_llpc), which compiles a pipeline with dynamic vertex input again
|
||||
// at every boot: neither pipeline-cache.bin nor the driver's own disk cache gives it back. Measured on an
|
||||
// RX 7900 XT (26.10.1) with NX2NAVI_PIPE_FEEDBACK=2, booting one save twice from an empty cache: the second boot
|
||||
// compiled all 622 recorded non-mesh pipelines again at 83 ms each, with the module and state hashes of the first
|
||||
// boot (the same with NX2NAVI_GPL=0, so it is not the pipeline libraries or their layouts), while the mesh
|
||||
// pipelines, which have no vertex input, came from the file. With the vertex layout in the pipeline the second
|
||||
// boot built 787 pipelines in 170 ms and the third 898 of 898 from the file in 233 ms, with no build left for the
|
||||
// scene. A player's file of 3,979 records used the whole boot budget (PREWARM_PIPE_MS, 20 s) at every boot; with
|
||||
// the vertex layout in the pipeline its third boot built 4,273 pipelines in 0.9 s. The price is more pipelines,
|
||||
// one per vertex layout (787 records against 692 for that boot); records written with dynamic vertex input still
|
||||
// prebuild, each for the layout of the draw that recorded it. The RDNA 1 and 2 branch (vk_caps.amd_sc) is not
|
||||
// measured and keeps dynamic vertex input.
|
||||
const char *dv = cfg_get("NX2NAVI_VK_DYN_VERTEX"); // no fixed default: it depends on the GPU, see above
|
||||
int intel_legacy = c->props.vendorID == 0x8086 && c->props.apiVersion <= VK_MAKE_API_VERSION(0, 1, 3, 215);
|
||||
int dv_on = dv && *dv ? strcmp(dv, "0") != 0 : !intel_legacy;
|
||||
int dv_on = dv && *dv ? strcmp(dv, "0") != 0 : !intel_legacy && !vk_caps.amd_llpc;
|
||||
P.dyn_vi = dv_on && c->has_vertex_input_dynamic_state && PROC(set_vi, "vkCmdSetVertexInputEXT");
|
||||
if (!dv_on && c->has_vertex_input_dynamic_state)
|
||||
rt_log("vk_pipeline: dynamic vertex input off (%s)", dv && *dv ? "NX2NAVI_VK_DYN_VERTEX=0" : "Intel's older Windows driver draws nothing with it");
|
||||
rt_log("vk_pipeline: dynamic vertex input off (%s)",
|
||||
dv && *dv ? "NX2NAVI_VK_DYN_VERTEX=0"
|
||||
: intel_legacy ? "Intel's older Windows driver draws nothing with it"
|
||||
: "AMD's driver compiles pipelines that have it again at every boot");
|
||||
if (c->has_eds3) {
|
||||
P.dyn_clamp = e3.extendedDynamicState3DepthClampEnable && P.f_depth_clamp && PROC(set_clamp, "vkCmdSetDepthClampEnableEXT");
|
||||
P.dyn_poly = e3.extendedDynamicState3PolygonMode && PROC(set_poly, "vkCmdSetPolygonModeEXT");
|
||||
@@ -2227,7 +2251,8 @@ static VkPipelineCreateFlags noopt_flag(void) {
|
||||
// with the prewarm, the stage library prebuild and every cache save, so a log tells whether a slow boot compiled or
|
||||
// read its pipelines: on NVIDIA the driver's own disk cache is kept per executable, so a renamed or new exe gets nothing
|
||||
// from it, and only pipeline-cache.bin can make its first boot quick. =2 also logs every compute build with a hash of
|
||||
// its final module, so two runs can be compared for a module that changes between them.
|
||||
// its final module, and every graphics pipeline build with the hashes of its final modules and of its fixed state, so
|
||||
// two runs can be compared for a module or a state that changes between them.
|
||||
enum { FB_GRAPHICS, FB_LIBRARY, FB_COMPUTE, FB_KINDS };
|
||||
static struct {
|
||||
PlatMutex lock;
|
||||
@@ -2375,7 +2400,7 @@ static uint32_t vs_union_missing(const VkPipeState *s, GfxShaderIo fin[32]) {
|
||||
|
||||
static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Layout *l, uint32_t fs_off) {
|
||||
VkShaderModule vm = VK_NULL_HANDLE, fm = VK_NULL_HANDLE;
|
||||
uint64_t vw, fw;
|
||||
uint64_t vw, fw, vm_hash = 0, fm_hash = 0; // the hashes: of the final modules, for the PIPE_FEEDBACK 2 line
|
||||
const uint32_t *vcode = gfx_shader_spirv(s->vs, &vw);
|
||||
uint32_t *vpatched = NULL, *fpatched = NULL;
|
||||
int mesh = is_mesh(s);
|
||||
@@ -2426,9 +2451,9 @@ static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Lay
|
||||
fcode = fpatched = r;
|
||||
}
|
||||
// A bound colour target outside the shader's output map gets write mask 0 (target_wmask)
|
||||
if (fcode) fm = make_module(fcode, fw, !k->a2c);
|
||||
if (fcode) fm = make_module(fcode, fw, !k->a2c), fm_hash = mm_hash;
|
||||
}
|
||||
vm = make_module(vcode, vw, 0);
|
||||
vm = make_module(vcode, vw, 0), vm_hash = mm_hash;
|
||||
free(vpatched), free(fpatched);
|
||||
VkPipeline pipe = VK_NULL_HANDLE;
|
||||
if (!vm || (s->fs && !fm) || (mesh && s->ts && !tm)) goto out;
|
||||
@@ -2531,6 +2556,21 @@ static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Lay
|
||||
} else {
|
||||
perf_count(PERF_CNT_PIPE_BUILT);
|
||||
fb_note(FB_GRAPHICS, &fb);
|
||||
if (P.feedback >= 2) { // the state hash: what the create info takes from the key besides the modules
|
||||
uint64_t st = fnv(fnv(FNV0, cba, sizeof cba), k->color, sizeof k->color);
|
||||
if (!P.dyn_vi) // the vertex layout is pipeline state then (zero in a mesh pipeline's key)
|
||||
st = fnv(fnv(fnv(fnv(st, k->attr_fmt, sizeof k->attr_fmt), k->attr_off, sizeof k->attr_off),
|
||||
k->attr_bind, sizeof k->attr_bind), k->rate, sizeof k->rate);
|
||||
uint32_t bits[] = {(uint32_t)mesh, k->topo_class, k->minus_w, k->clamp, k->poly_mode, k->samples, k->a2c,
|
||||
k->logic_op, k->n_color, k->fbl, k->dsb, l->set1 != 0, fs_off, pi.flags, k->depth, k->stencil};
|
||||
rt_log("vk_pipeline: graphics: modules %016llx %016llx, state %016llx (mesh %d, topology class %u, samples %u, "
|
||||
"colors %u, feedback loop %u, depth format %u, stage-independent layout %d, flags 0x%x), %s, %.1f ms",
|
||||
(unsigned long long)vm_hash, (unsigned long long)fm_hash, (unsigned long long)fnv(st, bits, sizeof bits),
|
||||
mesh, (unsigned)k->topo_class, (unsigned)k->samples, (unsigned)k->n_color, (unsigned)k->fbl,
|
||||
(unsigned)k->depth, l->set1 != 0, (unsigned)pi.flags,
|
||||
pb_last ? "from pipeline-binaries.bin" : fb.flags & VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT ? "from pipeline-cache.bin" : "compiled",
|
||||
(double)fb.duration / 1e6);
|
||||
}
|
||||
}
|
||||
out:
|
||||
if (vm) vkDestroyShaderModule(P.device, vm, NULL);
|
||||
@@ -4394,6 +4434,25 @@ static double bg_growth_cap(void) {
|
||||
return cap;
|
||||
}
|
||||
|
||||
// The builder compiles into a pipeline cache of its own (own_cache at pipe_create_in), not into P.cache. A create into
|
||||
// P.cache holds cache_rw shared from start to end, and the cache is only saved at a moment when nobody holds it: on an
|
||||
// RX 7900 XT (26.10.1) the largest of these shaders (4,874,502 SPIR-V words) was still compiling 590 s after the boot,
|
||||
// pipeline-cache.bin was not written once in that time, and the game was closed before the build ended, so the next
|
||||
// boot found neither that shader nor the pipelines built in play and started the same build again. bg_merge moves the
|
||||
// result into P.cache when the build is done, at a moment with no build into P.cache running: the lock is tried as
|
||||
// cache_save tries it, because a waiting exclusive request would hold back every new build.
|
||||
static void bg_merge(VkPipelineCache own, int built) {
|
||||
int locked = 0;
|
||||
while (built && !LB.stop && !(locked = plat_mutex_trylock(&cache_rw))) plat_sleep_ms(2);
|
||||
if (locked) {
|
||||
VkResult r = vkMergePipelineCaches(P.device, P.cache, 1, &own);
|
||||
plat_mutex_unlock(&cache_rw);
|
||||
if (r == VK_SUCCESS) cache_merges++;
|
||||
else rt_log("vk_pipeline: background compute build: its cache did not merge into %s (%d)", P.cache_path, (int)r);
|
||||
}
|
||||
vkDestroyPipelineCache(P.device, own, NULL);
|
||||
}
|
||||
|
||||
static void bg_main(void *arg) {
|
||||
(void)arg;
|
||||
plat_thread_priority(PLAT_PRIO_BELOW_NORMAL);
|
||||
@@ -4466,8 +4525,13 @@ static void bg_main(void *arg) {
|
||||
else BG.failed++, rt_log("vk_pipeline: background compute build: %s", why ? why : "no layout");
|
||||
continue;
|
||||
}
|
||||
VkPipelineCache own = VK_NULL_HANDLE;
|
||||
VkPipelineCacheCreateInfo oci = {.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO};
|
||||
if (P.cache && vkCreatePipelineCache(P.device, &oci, NULL, &own) != VK_SUCCESS) own = VK_NULL_HANDLE;
|
||||
double t0 = now_ms();
|
||||
own_cache = own; // without one the build goes into P.cache as before
|
||||
VkPipeline pipe = compute_create(cs, l, 0);
|
||||
own_cache = VK_NULL_HANDLE;
|
||||
double ms = now_ms() - t0;
|
||||
plat_mutex_lock(&P.lock);
|
||||
if (flight) cs_flight_remove(cs);
|
||||
@@ -4479,6 +4543,7 @@ static void bg_main(void *arg) {
|
||||
vkDestroyPipeline(P.device, pipe, NULL);
|
||||
}
|
||||
plat_mutex_unlock(&P.lock);
|
||||
if (own) bg_merge(own, pipe != VK_NULL_HANDLE); // after the entry: a dispatch waiting for it does not wait for this
|
||||
if (plat_xchg32(&BG.raised, 0)) plat_thread_priority(PLAT_PRIO_BELOW_NORMAL);
|
||||
if (pipe) BG.built++, BG.ms += ms;
|
||||
else BG.failed++;
|
||||
@@ -5103,14 +5168,15 @@ static void autosave_once(void) {
|
||||
if (now - last_ms < 20000) return;
|
||||
VkPipeStats s;
|
||||
vk_pipeline_stats(&s);
|
||||
if (s.graphics + s.compute == last_n) return;
|
||||
uint64_t n = s.graphics + s.compute + cache_merges; // a merge comes after its pipeline was counted (bg_main)
|
||||
if (n == last_n) return;
|
||||
// Only at a moment with no build running (cache_save): while one runs the save waits for the next tick, 500 ms
|
||||
// on, before it logs or flushes anything, so a busy stretch does not repeat the statistics lines every tick.
|
||||
if (P.cache && P.cache_path[0]) {
|
||||
if (!plat_mutex_trylock(&cache_rw)) return;
|
||||
plat_mutex_unlock(&cache_rw);
|
||||
}
|
||||
last_n = s.graphics + s.compute, last_ms = now;
|
||||
last_n = n, last_ms = now;
|
||||
save_with(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,7 @@ typedef struct Tex {
|
||||
int uploaded, released;
|
||||
uint32_t rel_serial; // reg_serial when it was released: slots registered before then are stale (slot_resolve)
|
||||
uint64_t rel_frame; // gfx_counters.frames when it was released (drop_stale)
|
||||
uint32_t back_gap; // most presents between a release and its taking back so far (stale_after)
|
||||
uint32_t n_up; // uploads of this entry (vk_texture_debug)
|
||||
int last_ch; // last vk_mem_changed result of make_fresh: 1 written, 0 clean, -1 not tracked
|
||||
struct Tex *next_at; // other entries at the same storage address
|
||||
@@ -80,6 +81,11 @@ typedef struct Tex {
|
||||
uint8_t superseded; // released, and another descriptor's entry was made at its address since (start_at)
|
||||
uint32_t rel_epoch; // submit_epoch when it was released
|
||||
struct Tex *ru_prev, *ru_next; // the reuse list of ik, newest release first
|
||||
uint64_t gw_frame; // gfx_counters.frames at its creation or newest GPU write (overwrite_others)
|
||||
uint64_t scan_frame; // one more than the frame vk_texture_gpu_written last looked for overwritten entries
|
||||
uint64_t ow_frame; // gfx_counters.frames when overwritten was set
|
||||
uint8_t overwritten; // released, and a texture at another address was drawn over its storage (overwrite_others)
|
||||
uint8_t in_gwin; // in gwin[]
|
||||
} Tex;
|
||||
|
||||
// A slot keeps what vk_texture_by_id resolved for it, so a draw binding skips the key lookup and the address chain: the
|
||||
@@ -324,12 +330,75 @@ static int format_compressed(VkFormat f) { // vkCmdClearColorImage takes no bl
|
||||
// them move to new addresses, new descriptors whose images were made again: vkCreateImage, the memory requirements and
|
||||
// the binding of a dedicated allocation cost 8% of the replay in Kokiri Forest. The released entries of each parameter
|
||||
// set are kept in a list, newest release first; create_tex takes the one nearest in address of those released at least
|
||||
// two frames earlier, never during the current submission's replay, whose later parts may still use it.
|
||||
// two frames earlier, never during the current submission's replay, whose later parts may still use it, and an
|
||||
// overwritten one (below) before any other.
|
||||
static nvn_hmap reuse = {.klen = sizeof(ImgKey)}; // ImgKey -> the newest released reusable entry
|
||||
static int tex_reuse = -1;
|
||||
static uint64_t n_reused, n_reuse_new; // since the last perf line: images taken from a released entry, made anew
|
||||
static uint64_t n_restarts; // superseded entries taken back (start_at)
|
||||
|
||||
// Moved render targets. Ocarina keeps about a thousand texture objects for its per-frame render targets and lays the
|
||||
// targets out anew every frame with a linear allocator in one GPU-only pool. A target early in that layout changes its
|
||||
// size with an effect on screen (811x811, then 778x778 a frame later in Kakariko Village), and every target after it
|
||||
// then lies about 100 KB further on: eleven targets, 55 MiB of images, at new addresses in each frame the size
|
||||
// changes, each a new entry with an image of its own, as few of them can lie in the pool memory itself (pool_fits).
|
||||
// The game finalizes an old texture object only when it needs the object again, 90 to 300 frames later, and a
|
||||
// descriptor in the texture pool goes on naming it, so the entries were neither released nor idle: after 6,000 frames
|
||||
// of walking through the village 1,043 of them held 6.1 GiB of device memory, against 233 MiB in the targets still
|
||||
// drawn, and once released they waited GPU_STALE_FRAMES more. A driver that charges device memory to the system's
|
||||
// commit kept the commit low from then on, and the idle textures of the area went and came back (commit_evict).
|
||||
//
|
||||
// On the console those targets are views of one pool's memory: when another target is drawn over a target's bytes, its
|
||||
// content is gone. overwrite_others says so here. When the GPU writes a texture in the GPU-only address window
|
||||
// (nvn_gpu_only), each entry at another address whose storage overlaps it is marked overwritten and released as if
|
||||
// the game had finalized it. Its image then goes to the next texture made with the same parameters (create_tex), or
|
||||
// away OVERWRITTEN_KEEP presents later (drop_overwritten), or as early as an intact entry's while memory is short
|
||||
// (drop_stale). Left alone are an entry at the same address (start_at decides about those), an entry the written
|
||||
// texture lies inside (a level or a layer of it, which a game draws through a texture of its own) and an entry
|
||||
// written in this frame or the one before: two layouts that take turns within a frame keep their images. The mark
|
||||
// goes when the game takes the entry back (entry_of).
|
||||
// gwin: the entries this applies to (reusable, in the window), sorted by address; gwin_span: the largest storage size
|
||||
// one of them had, which bounds how far below an address an overlapping entry can start.
|
||||
#define OVERWRITE_FRAMES 2
|
||||
#define OVERWRITTEN_KEEP 600
|
||||
static struct Tex **gwin;
|
||||
static int ngwin, cgwin;
|
||||
static uint64_t gwin_span;
|
||||
static uint64_t n_overwritten, n_over_drops; // since the last perf line: entries marked, dropped by drop_overwritten
|
||||
|
||||
static int gwin_lower(uint64_t a) {
|
||||
int lo = 0, hi = ngwin;
|
||||
while (lo < hi) {
|
||||
int m = (lo + hi) / 2;
|
||||
if (gwin[m]->key.addr < a) lo = m + 1;
|
||||
else hi = m;
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
|
||||
static void gwin_add(Tex *e) {
|
||||
if (ngwin == cgwin) {
|
||||
cgwin = cgwin ? 2 * cgwin : 256;
|
||||
if (!(gwin = realloc(gwin, sizeof *gwin * (size_t)cgwin))) rt_fatal("vk: out of memory for the render target list");
|
||||
}
|
||||
int i = gwin_lower(e->key.addr);
|
||||
memmove(gwin + i + 1, gwin + i, sizeof *gwin * (size_t)(ngwin - i));
|
||||
gwin[i] = e, ngwin++;
|
||||
if (e->t.L.size > gwin_span) gwin_span = e->t.L.size;
|
||||
e->in_gwin = 1;
|
||||
}
|
||||
|
||||
static void gwin_remove(Tex *e) {
|
||||
if (!e->in_gwin) return;
|
||||
for (int i = gwin_lower(e->key.addr); i < ngwin && gwin[i]->key.addr == e->key.addr; i++)
|
||||
if (gwin[i] == e) {
|
||||
memmove(gwin + i, gwin + i + 1, sizeof *gwin * (size_t)(ngwin - i - 1));
|
||||
ngwin--;
|
||||
break;
|
||||
}
|
||||
e->in_gwin = 0;
|
||||
}
|
||||
|
||||
static int tex_reuse_on(void) {
|
||||
if (tex_reuse < 0) {
|
||||
tex_reuse = cfg_bool("NX2NAVI_TEX_REUSE");
|
||||
@@ -677,16 +746,20 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) {
|
||||
// again every frame, and DLSS's scene target (2560x1440 at a fixed address in Kokiri Forest) lost its image
|
||||
// to a moving target of the same size in between, and DLSS its history (vk_dlaa.c evaluate), several times a
|
||||
// second. Not one at this address either: its own descriptor comes back for it (start_at). Of the others the
|
||||
// one nearest in address, so a target the allocator moves keeps the image it had.
|
||||
// one nearest in address, so a target the allocator moves keeps the image it had; an overwritten one first
|
||||
// (overwrite_others): nothing can ask for its content again.
|
||||
uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
Tex *a = NULL;
|
||||
uint64_t best = UINT64_MAX;
|
||||
Tex *a = NULL, *kept = NULL;
|
||||
uint64_t best = UINT64_MAX, best_kept = UINT64_MAX;
|
||||
int n = 0;
|
||||
for (Tex *q = nvn_hm_get(&reuse, &e->ik); q && n < 128; q = q->ru_next, n++) {
|
||||
if (q->rel_epoch == submit_epoch || now - q->rel_frame < 2 || q->key.addr == k->addr) continue;
|
||||
uint64_t dist = q->key.addr > k->addr ? q->key.addr - k->addr : k->addr - q->key.addr;
|
||||
if (dist < best) best = dist, a = q;
|
||||
if (q->overwritten) {
|
||||
if (dist < best) best = dist, a = q;
|
||||
} else if (dist < best_kept) best_kept = dist, kept = q;
|
||||
}
|
||||
if (!a) a = kept;
|
||||
if (a) {
|
||||
img = a->pub.image, e->alloc = a->alloc;
|
||||
vk_exec_image_gone(img); // the views and records of the texture it was
|
||||
@@ -718,6 +791,8 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) {
|
||||
e->bytes = own.size;
|
||||
}
|
||||
e->reusable = e->alloc && gpu_only;
|
||||
e->gw_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
if (e->reusable && nvn_gpu_only(k->addr)) gwin_add(e);
|
||||
// A new image's handle can be one a destroyed image had. Views are purged with the image (destroy_tex), so none
|
||||
// should be cached under it: one that is was made from a destroyed texture's record after its purge, and would
|
||||
// reach this image's memory through the dead one's. Dropped here, and counted in the log.
|
||||
@@ -839,6 +914,7 @@ static void rel_unlink(Tex *e) {
|
||||
|
||||
static void destroy_tex(Tex *e) {
|
||||
own_remove(e);
|
||||
gwin_remove(e);
|
||||
n_site[0]++;
|
||||
tex_gen_bump(e->key.addr);
|
||||
nvn_hm_del(&by_key, &e->key);
|
||||
@@ -967,11 +1043,28 @@ int vk_texture_commit_low(void) { return __atomic_load_n(&commit_low, __ATOMIC_R
|
||||
// again, empty if it is a render target. Under commit pressure that a system-managed page file never relieves, dropping
|
||||
// young ones destroyed 2160p render targets about every frame for whole sessions. Frame generation also reads a frame's
|
||||
// depth target at the host present (vk_dlssg.c vk_dlssg_frame tags it), after the blit deferred destruction waits for.
|
||||
//
|
||||
// A released entry is not dead to the game. A descriptor registered for a render target still resolves to its released
|
||||
// entry (slot_resolve: a finalized slot with no live texture at its storage gets its own), and the game can sample it
|
||||
// or take it back long after the release. In Kakariko Village, with the system's commit low, render targets dropped at
|
||||
// STALE_FRAMES came back empty and the scene was drawn black for one frame every two to three seconds (interface
|
||||
// intact), for as long as the commit stayed low. So no entry goes while lookups still reach it (idle), and one whose
|
||||
// content exists only in its image waits GPU_STALE_FRAMES, or GAP_TIMES the longest it stayed released before the game
|
||||
// took it back when that is longer. An overwritten entry has no content left to wait for (overwrite_others): it goes
|
||||
// with the intact ones, whether or not a descriptor still names it.
|
||||
#define STALE_FRAMES 120
|
||||
#define GPU_STALE_FRAMES 1800
|
||||
#define GAP_TIMES 8
|
||||
static int idle(const Tex *e, uint64_t now) {
|
||||
return look_round - e->use_round >= IDLE_ROUNDS && now - e->use_frame >= STALE_FRAMES;
|
||||
}
|
||||
|
||||
static uint64_t stale_after(const Tex *e) {
|
||||
if (comes_back_intact(e) || e->overwritten) return STALE_FRAMES;
|
||||
uint64_t gap = (uint64_t)e->back_gap * GAP_TIMES;
|
||||
return gap > GPU_STALE_FRAMES ? gap : GPU_STALE_FRAMES;
|
||||
}
|
||||
|
||||
static void drop_stale(uint64_t keep, uint64_t *count) {
|
||||
uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
int dropped = 0;
|
||||
@@ -979,12 +1072,26 @@ static void drop_stale(uint64_t keep, uint64_t *count) {
|
||||
for (Tex *e = rel_head, *next; e && now - e->rel_frame >= STALE_FRAMES; e = next) { // in release order
|
||||
if (dropped == PRESSURE_STEP || rel_bytes <= keep) return;
|
||||
next = e->rel_next;
|
||||
if (!e->bytes || (!any && !comes_back_intact(e))) continue;
|
||||
if (!e->bytes || (!any && !comes_back_intact(e) && !e->overwritten)) continue;
|
||||
if (now - e->rel_frame < stale_after(e) || (!e->overwritten && !idle(e, now))) continue;
|
||||
destroy_tex(e);
|
||||
dropped++, (*count)++;
|
||||
}
|
||||
}
|
||||
|
||||
// Drops up to PRESSURE_STEP entries that were marked overwritten OVERWRITTEN_KEEP presents ago or earlier
|
||||
// (overwrite_others): the image reuse found no texture to give their images to in that time.
|
||||
static void drop_overwritten(void) {
|
||||
uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
int dropped = 0;
|
||||
for (Tex *e = rel_head, *next; e && now - e->rel_frame >= OVERWRITTEN_KEEP && dropped < PRESSURE_STEP; e = next) {
|
||||
next = e->rel_next;
|
||||
if (!e->overwritten || now - e->ow_frame < OVERWRITTEN_KEEP) continue;
|
||||
destroy_tex(e);
|
||||
dropped++, n_over_drops++;
|
||||
}
|
||||
}
|
||||
|
||||
// NX2NAVI_TEX_EVICT (default on; 0 = only released entries go under VRAM pressure, as before): above the VRAM pressure
|
||||
// threshold, once the released entries that come back intact are gone, live entries go too, least recently looked up
|
||||
// first: those that come back intact and that no lookup reached for IDLE_ROUNDS pool walks and STALE_FRAMES presents
|
||||
@@ -1188,6 +1295,7 @@ static void sweep_pressure(void) { // tex_lock held
|
||||
if (tex_evict && freed < over) evict_disused(over - freed);
|
||||
}
|
||||
if (rel_head) {
|
||||
drop_overwritten();
|
||||
uint64_t cap = released_cap_bytes();
|
||||
if (rel_bytes > cap) drop_stale(cap, &n_cap_drops);
|
||||
if (rel_bytes > COMMIT_LOW_KEEP && commit_is_low()) drop_stale(COMMIT_LOW_KEEP, &n_commit_drops);
|
||||
@@ -1870,7 +1978,10 @@ static Tex *entry_of(const tex_t *t) {
|
||||
}
|
||||
if (e->released) {
|
||||
e->samp_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); // in use again, as a new one
|
||||
uint64_t gap = e->samp_frame - e->rel_frame;
|
||||
if (gap > e->back_gap) e->back_gap = gap > UINT32_MAX ? UINT32_MAX : (uint32_t)gap;
|
||||
e->released = 0, nreleased--, rel_unlink(e), n_site[2]++, tex_gen_bump(k.addr);
|
||||
e->overwritten = 0;
|
||||
if (e->superseded) { // its image may still be in use: the first barrier waits for all earlier work
|
||||
e->superseded = 0, e->reused = 1;
|
||||
n_restarts++;
|
||||
@@ -1928,6 +2039,34 @@ static void wb_cfg_init(void) {
|
||||
// up to 9 frames in a row while a tile loads.
|
||||
static int wb_wanted(const Tex *e) { return e->t.L.size <= wb_limit || e->run < WB_RUN; }
|
||||
|
||||
static void mark_released(Tex *e) { // tex_lock held; e is not released
|
||||
e->released = 1;
|
||||
e->rel_epoch = submit_epoch;
|
||||
e->rel_serial = reg_serial;
|
||||
e->rel_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
nreleased++;
|
||||
rel_append(e);
|
||||
n_site[3]++;
|
||||
tex_gen_bump(e->key.addr);
|
||||
}
|
||||
|
||||
// e, in the GPU-only address window, is written by the GPU in frame fr: the entries its storage runs over are
|
||||
// overwritten (section "Moved render targets", above gwin).
|
||||
static void overwrite_others(const Tex *e, uint64_t fr) {
|
||||
uint64_t lo = e->key.addr, hi = lo + e->t.L.size;
|
||||
int released = 0;
|
||||
for (int i = gwin_lower(lo > gwin_span ? lo - gwin_span : 0); i < ngwin && gwin[i]->key.addr < hi; i++) {
|
||||
Tex *q = gwin[i];
|
||||
uint64_t qlo = q->key.addr, qhi = qlo + q->t.L.size;
|
||||
if (q->overwritten || qlo == lo || qhi <= lo || (lo >= qlo && hi <= qhi)) continue;
|
||||
if (fr - q->gw_frame < OVERWRITE_FRAMES) continue;
|
||||
q->overwritten = 1, q->ow_frame = fr;
|
||||
n_overwritten++;
|
||||
if (!q->released) mark_released(q), released = 1;
|
||||
}
|
||||
if (released) sweep_released();
|
||||
}
|
||||
|
||||
void vk_texture_gpu_written(const tex_t *t) {
|
||||
if (!t || !t->mem) return;
|
||||
vk_exec_frame_written(t);
|
||||
@@ -1935,6 +2074,12 @@ void vk_texture_gpu_written(const tex_t *t) {
|
||||
Tex *e = entry_of(t);
|
||||
if (e) {
|
||||
make_fresh(e); // a partial render keeps what the storage held before
|
||||
uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
e->gw_frame = now;
|
||||
if (e->scan_frame != now + 1 && nvn_gpu_only(e->key.addr)) { // once per frame and entry
|
||||
e->scan_frame = now + 1;
|
||||
overwrite_others(e, now);
|
||||
}
|
||||
e->pub.gpu_owned = 1;
|
||||
e->chash_ok = 0; // the image now holds the GPU's content: matching storage bytes no longer mean "unchanged"
|
||||
uint64_t f = vk_exec_recording_fence();
|
||||
@@ -1968,14 +2113,7 @@ void vk_texture_release(const tex_t *t) {
|
||||
lock();
|
||||
Tex *e = nvn_hm_get(&by_key, &k);
|
||||
if (e && !e->released) {
|
||||
e->released = 1;
|
||||
e->rel_epoch = submit_epoch;
|
||||
e->rel_serial = reg_serial;
|
||||
e->rel_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
nreleased++;
|
||||
rel_append(e);
|
||||
n_site[3]++;
|
||||
tex_gen_bump(k.addr);
|
||||
mark_released(e);
|
||||
sweep_released();
|
||||
}
|
||||
unlock();
|
||||
@@ -2268,15 +2406,17 @@ void vk_texture_new_submit(void) {
|
||||
rt_log("perf: frame %llu: released textures kept %d (%.0f MiB of own memory), dropped past the size limit %llu, "
|
||||
"under VRAM pressure %llu, under commit pressure %llu; idle live textures evicted %llu (%.1f MiB); "
|
||||
"unsampled ones evicted %llu (%.1f MiB), back %llu, held %u (marks read %u); images reused %llu, made "
|
||||
"%llu, pool bindings not tried %llu, superseded taken back %llu",
|
||||
"%llu, pool bindings not tried %llu, superseded taken back %llu; render targets overwritten at another "
|
||||
"address %llu, dropped unused %llu",
|
||||
(unsigned long long)f * 60, nreleased, rel_bytes / 1048576.0, (unsigned long long)n_cap_drops,
|
||||
(unsigned long long)n_pressure_drops, (unsigned long long)n_commit_drops, (unsigned long long)n_evict,
|
||||
n_evict_bytes / 1048576.0, (unsigned long long)n_disuse, n_disuse_bytes / 1048576.0,
|
||||
(unsigned long long)n_disuse_back, holds.n, marks_reads, (unsigned long long)n_reused,
|
||||
(unsigned long long)n_reuse_new, (unsigned long long)n_bind_skips, (unsigned long long)n_restarts);
|
||||
(unsigned long long)n_reuse_new, (unsigned long long)n_bind_skips, (unsigned long long)n_restarts,
|
||||
(unsigned long long)n_overwritten, (unsigned long long)n_over_drops);
|
||||
n_cap_drops = n_pressure_drops = n_commit_drops = n_evict = n_evict_bytes = 0;
|
||||
n_disuse = n_disuse_bytes = n_disuse_back = 0;
|
||||
n_reused = n_reuse_new = n_bind_skips = n_restarts = 0;
|
||||
n_reused = n_reuse_new = n_bind_skips = n_restarts = n_overwritten = n_over_drops = 0;
|
||||
}
|
||||
unlock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user