Merge GitHub main into the test line: the launcher's customisation, the settings and sound changes, and the warm stage-library cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-10 03:32:30 -05:00
32 changed files with 2119 additions and 220 deletions

View File

@@ -71,6 +71,7 @@ typedef struct Mixer {
unsigned reported; // one-time reports
float voice_scratch[6][MAX_FRAME], filter_scratch[MAX_FRAME], output_scratch[MAX_FRAME * OUT_MAX];
double level_sum, level_peak; uint64_t level_count, level_frames; int first_sound_logged;
double input_sum[6]; // NX2NAVI_TRACE=audio: the device sink's inputs, squared and summed
int prefill; // render blocks
uint64_t trace_pass, trace_seen[1024]; // NX2NAVI_TRACE=audio: render passes; the pass each voice was last mixed in
uint64_t trace_buf[1024]; // and the wave buffer address it read in that pass
@@ -225,12 +226,13 @@ int list_audio_devices(void) {
return 0;
}
// NX2NAVI_AUDIO_CHANNELS: the output channels of this run, decided once. stereo gives 2; 5.1 gives 6 (FL FR C LFE RL RR,
// NX2NAVI_AUDIO_CHANNELS: the output channels, decided at the first call and again when the setting changes
// (audio_channels_live). stereo gives 2; 5.1 gives 6 (FL FR C LFE RL RR,
// the device sink's order and SDL's); auto gives 6 when the chosen playback device (NX2NAVI_AUDIO_DEVICE; the system default unless set) has at least six channels (a 5.1
// or 7.1 speaker setup in Windows; SDL places 5.1 on 7.1) and 2 otherwise. IAudioDevice reports the same count to the
// game (GetActiveChannelCount), which picks its sink layout from it. NX2NAVI_AUDIO=none: 2, without touching SDL.
static _Atomic int decided; // 0: output_channels decides at its next call
static int output_channels(void) {
static _Atomic int decided;
int n = atomic_load(&decided);
if (n) return n;
const char *want = cfg_value("NX2NAVI_AUDIO_CHANNELS"), *mode = cfg_get("NX2NAVI_AUDIO");
@@ -402,6 +404,29 @@ static void output_timing(Mixer *m, int rate) {
dev.channels, m->prefill * 5, period, m->target * 1000.0 / rate);
}
// Render thread (audio_mixer_render): the output takes another channel count after NX2NAVI_AUDIO_CHANNELS changed
// (audio_channels_live). The stream is made again with the new count on the device it was on: destroying it waits
// for the SDL callback, the only other reader of the ring and of m->channels, and what was queued in the old layout
// is dropped.
static void channels_switch(Mixer *m, int channels) {
if (m->stream) SDL_DestroyAudioStream(m->stream);
m->stream = NULL;
atomic_store_explicit(&m->rd, atomic_load(&m->wr), memory_order_release);
m->channels = channels;
m->ready = 0;
m->reported &= ~(1u << 6); // the device sink's line again, with the new output
atomic_store_explicit(&m->pulled_ns, 0, memory_order_release);
rt_log("audio: the output is now %s", channels == 6 ? "5.1" : "stereo");
if (!m->device_ok) return; // mixing without a device
char err[256];
if (stream_open(m, m->rate, m->device ? m->device : SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, err, sizeof err)) {
output_timing(m, m->rate);
} else {
m->device_ok = 0;
rt_log("audio: no output device (%s): mixing without sound", err);
}
}
// Render thread, from audio_mixer_pace_ns while the device takes nothing. An explicitly chosen device (NX2NAVI_AUDIO_DEVICE)
// can disappear (a virtual sink removed, a USB headset unplugged); SDL keeps only the system default device alive across
// that, so the stream of a chosen one just stops being pulled, and nothing in this file polls SDL's event queue for the
@@ -523,6 +548,11 @@ static void level_note(AudioRenderer *r, Mixer *m, const float *out, int frames)
(unsigned long long)atomic_load(&m->overruns), (unsigned long long)atomic_load(&m->missing_frames),
(unsigned long long)atomic_load(&m->callback_frames), (unsigned long long)atomic_load(&m->callback_nonzero),
queued * 1000.0 / r->param.sample_rate, (unsigned long long)atomic_load(&m->skipped_frames));
double f = (double)m->level_frames;
rt_log("audio: device sink inputs rms FL %.4f FR %.4f C %.4f LFE %.4f RL %.4f RR %.4f, output %s",
sqrt(m->input_sum[0] / f), sqrt(m->input_sum[1] / f), sqrt(m->input_sum[2] / f), sqrt(m->input_sum[3] / f),
sqrt(m->input_sum[4] / f), sqrt(m->input_sum[5] / f), m->channels == 6 ? "5.1" : "stereo");
memset(m->input_sum, 0, sizeof m->input_sum);
m->level_sum = m->level_peak = 0;
m->level_count = m->level_frames = 0;
}
@@ -1128,6 +1158,7 @@ void audio_mixer_render(AudioRenderer *r, double seconds) {
if (r->sinks[i].type == SINK_DEVICE && !sink) sink = &r->sinks[i];
else if (r->sinks[i].type == SINK_CIRCULAR){mix_circular_sink(r,i);circular=1;}
} else {m->sink_address[i]=0;m->sink_size[i]=m->sink_offset[i]=m->sink_old[i]=m->sink_written[i]=0;}
if (m->channels != output_channels()) channels_switch(m, output_channels());
const int oc = m->channels;
float *out=m->output_scratch;
memset(out, 0, sizeof(float) * (size_t)oc * (size_t)frames);
@@ -1143,6 +1174,9 @@ void audio_mixer_render(AudioRenderer *r, double seconds) {
const float *ch[6] = {0};
float raw[2 * MAX_FRAME];
for (uint32_t c = 0; c < sink->input_count; c++) if (sink->inputs[c] >= 0 && sink->inputs[c] < m->nbuf) ch[c] = mixbuf(m, sink->inputs[c]);
if (rt_trace & RT_TRACE_AUDIO) // what the game puts on each input: shows whether it mixes for 5.1 (level_note)
for (int c = 0; c < 6; c++)
for (int k = 0; ch[c] && k < frames; k++) m->input_sum[c] += (double)ch[c][k] * ch[c][k];
for (int k = 0; k < frames; k++) {
float s[OUT_MAX] = {0}; /* FL FR C LFE RL RR */
if (sink->input_count >= 6 && oc == 6) {
@@ -1170,6 +1204,41 @@ void audio_mixer_render(AudioRenderer *r, double seconds) {
// ---- IAudioDevice: one output device, stereo or 5.1 (output_channels)
static const char device_name[] = "AudioTvOutput";
// The events the game took to hear of a change of the output device (IAudioDevice's, its notifications' and the
// virtual device manager's). audio_channels_live signals them all: the game then asks for the channel count again, as
// it does on a console whose output moves from the TV to headphones.
static uint32_t change_events[16];
static _Atomic int n_change_events;
static uint32_t change_event_new(void) {
uint32_t ev = kern_event_new();
int i = atomic_fetch_add(&n_change_events, 1);
if (i < 16) change_events[i] = ev;
return ev;
}
// Present path, after a settings change (gfx_present.c window_settings_live): a changed NX2NAVI_AUDIO_CHANNELS is
// decided again, and when it gives another channel count the game is told its output device changed. The game asks
// GetActiveChannelCount again and mixes for the new count; the mixer moves its device to that count at its next
// render (channels_switch). Measured on Ocarina 1.1.1: one event held, and the game asked within the same second.
void audio_channels_live(void) {
static char seen[16];
const char *want = cfg_value("NX2NAVI_AUDIO_CHANNELS");
if (!strncmp(want, seen, sizeof seen - 1)) return;
int first = !seen[0];
snprintf(seen, sizeof seen, "%s", want);
int was = atomic_load(&decided);
if (first || !was) return; // nothing has asked yet: the first call decides
atomic_store(&decided, 0);
int now = output_channels();
if (now == was) return;
int n = atomic_load(&n_change_events);
if (n > 16) n = 16;
for (int i = 0; i < n; i++) kern_event_signal(change_events[i]);
rt_log("audio: NX2NAVI_AUDIO_CHANNELS=%s: the output goes from %s to %s; the game is told the device changed "
"(%d events)", want, was == 6 ? "5.1" : "stereo", now == 6 ? "5.1" : "stereo", n);
}
static void put_name(const IpcBuffer *b, IpcResponse *rs, int pointer) {
if (!b->size) return;
char *dst = ipc_guest(b->addr, b->size);
@@ -1185,7 +1254,7 @@ static IpcBuffer out_buffer(const IpcRequest *rq, int *pointer) {
}
void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs) {
static uint32_t change_event; // device change events: never signalled, the device does not change
static uint32_t change_event; // the device change event: signalled when the output's channels change (audio_channels_live)
int pointer;
switch (rq->cmd) {
case 0: case 6: case 14: { // ListAudioDeviceName(Auto), ListAudioOutputDeviceName [13.0.0+]
@@ -1210,7 +1279,7 @@ void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs)
case 4: case 11: case 12: { // QueryAudioDeviceSystemEvent, ...InputEvent, ...OutputEvent
uint32_t ev = __atomic_load_n(&change_event, __ATOMIC_ACQUIRE);
if (!ev) {
uint32_t mine = kern_event_new(), expected = 0;
uint32_t mine = change_event_new(), expected = 0;
if (__atomic_compare_exchange_n(&change_event, &expected, mine, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE))
ev = mine;
else
@@ -1222,9 +1291,9 @@ void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs)
// 17.0.0+: Acquire/Release Audio{Input,Output}DeviceNotification (15-18), 18.0.0+: SetAudioDeviceOutputVolume-
// AutoTuneEnabled (19) / IsAudioDeviceOutputVolumeAutoTuneEnabled (20), 21.0.0+:
// IsActiveOutputDeviceEstimatedLowLatency (21). Command ids as the system answers them. The
// device never changes and the host applies no auto-tuned volume, so the answers are "off" / "no".
case 15: case 17: { // Acquire...Notification -> a readable event that is never signalled
uint32_t ev = kern_event_new();
// host applies no auto-tuned volume, so the answers are "off" / "no".
case 15: case 17: { // Acquire...Notification -> a readable event, signalled with the others
uint32_t ev = change_event_new();
rs->copy[rs->ncopy++] = kern_event_readable(ev);
return;
}
@@ -1293,7 +1362,7 @@ static void virtual_device_manager_handler(IpcSession *s, const IpcRequest *rq,
put_block(rq, rs, block, sizeof block);
return;
}
case 500: rs->copy[rs->ncopy++] = kern_event_readable(kern_event_new()); return; // state change event
case 500: rs->copy[rs->ncopy++] = kern_event_readable(change_event_new()); return; // state change event
case 700: case 800: return; // AttachStream / DetachStream
default: {
static volatile int32_t seen[64];

View File

@@ -194,5 +194,6 @@ int audio_mixer_out_stats(AudioRenderer *r, AudioOutStats *o);
uint64_t audio_voice_consumed(const AudioRenderer *r, ServerVoice *v, int frames); // source samples over one frame; advances v->fraction
int audio_buffer_empty(const AudrenWaveBuffer *b); // no samples to decode: played as soon as it is reached
void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // IAudioDevice
void audio_channels_live(void); // after a settings change: another NX2NAVI_AUDIO_CHANNELS reaches the game and the mixer
void audio_dmg_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // auddmg:u (Switch 2)
void audio_sysmix_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // audsmx:u (Switch 2)

View File

@@ -1,4 +1,4 @@
// Camera speed: NX2NAVI_STICK_SENS (0.1 to 3, default 1, read at start) multiplies the game's own right-stick camera speeds.
// Camera speed: NX2NAVI_STICK_SENS (0.1 to 3, default 1) multiplies the game's own right-stick camera speeds.
// The game's Options menu offers five camera sensitivities, Very Low to Very High; their turn rates are the array
// StickSensitivity {0.8, 1.2, 1.6, 2.0, 2.4} in Component/PlayerCameraParam/PlayerCamera.game__component__
// PlayerCameraParam.bgyml inside Pack/Actor/PlayerCamera.pack.zs. romfs.c serves a copy of that pack with every entry
@@ -8,6 +8,13 @@
//
// The pack is a SARC archive compressed with the dictionary pack.zsdic of Pack/ZsDic.pack.zs. A float in a BYML array
// is stored in the array itself, so the multiplied values take the bytes of the old ones and no offset moves.
//
// During a run (cam_speed_tick): the pack served stays the one made at start, and the game has read it by the time the
// player changes the setting. So a change is written into the game's memory instead: the array as the game holds it
// (the five values in a row, times the factor in effect) is looked for in the guest's heap and every copy found is
// overwritten with the values times the new factor. The game reads the array each time it steps the camera, so the
// camera's speed follows at once. When the game opens the pack again (cam_pack_opened: romfs.c), what it reads holds
// the start's factor again, and the search runs once more a moment later.
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -15,11 +22,15 @@
#include "cfg.h"
#include "host.h"
#include "platform/platform.h"
#define MEMBER "Component/PlayerCameraParam/PlayerCamera.game__component__PlayerCameraParam.bgyml"
#define MAX_RAW (64u << 20)
static float applied = 1.0f;
static float applied = 1.0f; // the factor the game's values in memory carry: what cam_speed_applied reports
static float base[8]; // the game's own StickSensitivity values, from the pack as it is before multiplying
static int base_n; // how many; 0 until cam_pack_build has read the pack (or when it could not)
static float served_k = 1.0f; // the factor of the pack the game reads from the RomFS: the start's
float cam_speed_wanted(void) {
double v = cfg_double("NX2NAVI_STICK_SENS");
@@ -100,7 +111,6 @@ static int scale_sensitivity(uint8_t *p, size_t n, float k, float was[8]) {
int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, void **out, size_t *out_n) {
float k = cam_speed_wanted();
*out = NULL;
if (k == 1.0f) return 0;
int ok = 0;
uint8_t *dics = NULL, *raw = NULL;
ZSTD_DDict *dd = NULL;
@@ -135,6 +145,9 @@ int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n,
rt_log("camera speed: no StickSensitivity array in %s; the camera keeps its own speed", MEMBER);
goto done;
}
memcpy(base, was, sizeof(float) * (size_t)count); // what a change during the run looks for in memory (cam_speed_tick)
base_n = count;
if (k == 1.0f) goto done; // the game's own pack is served as it is
cd = ZSTD_createCDict(dics + dic_at, dic_n, 9);
size_t cap = ZSTD_compressBound((size_t)rn), zn = 0;
ZSTD_CCtx *cctx = cd ? ZSTD_createCCtx() : NULL;
@@ -158,4 +171,134 @@ done:
return ok;
}
void cam_pack_served(void) { applied = cam_speed_wanted(); }
void cam_pack_served(void) { applied = served_k = cam_speed_wanted(); }
// ---- during a run
#define COPIES 16
static uint64_t copies[COPIES]; // where the game's memory held the array at the last search
static int ncopies;
static void factor(float out[8], float k) { // the array at factor k, as cam_pack_build's multiplication gives it
for (int i = 0; i < base_n; i++) out[i] = base[i], out[i] *= k;
}
// A pass over the guest's heap and alias regions for the array at factor k, byte for byte, in memory the game may
// read and write; each copy found becomes the array at factor to and is remembered (copies, when fresh is set).
// Returns how many were rewritten. Runs on its own thread: the regions are read 16 MiB at a time under mem_read_hold,
// so the game's own mapping calls wait for one piece at most.
static int rewrite(float k, float to, int fresh) {
enum { PIECE = 16 << 20 };
float from[8], into[8];
factor(from, k);
factor(into, to);
const size_t len = sizeof(float) * (size_t)base_n;
const uint8_t first = *(const uint8_t *)from;
const RtGuestLayout *l = mem_layout();
const RtRange ranges[2] = {l->heap, l->alias};
int found = 0;
if (fresh) ncopies = 0;
for (int r = 0; r < 2; r++) {
uint64_t a = ranges[r].base, end = ranges[r].base + ranges[r].size;
while (a < end) {
RtMemoryInfo mi;
if (mem_query(a, &mi) || !mi.size) break;
uint64_t stop = mi.base + mi.size < end ? mi.base + mi.size : end;
if (!mi.state || (mi.perm & 3) != 3) { // free, or not the game's to read and write
a = stop;
continue;
}
while (a < stop) {
uint64_t n = stop - a < PIECE ? stop - a : PIECE;
if (n >= len && mem_read_hold(a, n)) {
const uint8_t *p = (const uint8_t *)(uintptr_t)a, *e = p + n;
for (const uint8_t *q = p; q + len <= e && (q = memchr(q, first, (size_t)(e - q) - len + 1)); q++) {
if (memcmp(q, from, len)) continue;
mem_thaw((uint64_t)(uintptr_t)q, len); // a write-protected page of a tracked region
memcpy((void *)(uintptr_t)q, into, len);
if (ncopies < COPIES) copies[ncopies++] = (uint64_t)(uintptr_t)q;
found++;
}
mem_read_release();
}
// a copy across two pieces: the next piece starts a little before this one's end
a += n < PIECE ? n : n - (len - 1);
}
}
}
return found;
}
// The copies of the last search, still holding the array at factor k, rewritten to factor to: 1 when every one of them
// did (a step of the menu's slider then takes no search), 0 when one is gone or changed, with none of them touched.
static int rewrite_known(float k, float to) {
float from[8], into[8];
factor(from, k);
factor(into, to);
const size_t len = sizeof(float) * (size_t)base_n;
if (!ncopies) return 0;
for (int pass = 0; pass < 2; pass++) // all checked first, then all written
for (int i = 0; i < ncopies; i++) {
if (!mem_read_hold(copies[i], len)) return 0;
int same = !memcmp((const void *)(uintptr_t)copies[i], pass ? into : from, len) ||
(pass && !memcmp((const void *)(uintptr_t)copies[i], from, len));
if (same && pass) {
mem_thaw(copies[i], len);
memcpy((void *)(uintptr_t)copies[i], into, len);
}
mem_read_release();
if (!same) return 0;
}
return 1;
}
static volatile int32_t searching; // the search thread runs
static volatile int32_t reopened; // the game opened the pack since the last search (cam_pack_opened)
static uint64_t due_ns; // when cam_speed_tick starts the next search; 0: none due
static int tries; // searches left after this one that found nothing
static float asked = 1.0f; // the factor the last search was started for
static void search_main(void *arg) {
(void)arg;
float to = cam_speed_wanted(), was = applied;
uint64_t t0 = plat_now_ns();
// what the game holds: the array at the factor in effect, and at the start's after it read the pack again
int n = to != was ? rewrite(was, to, 1) : 0;
if (served_k != was && served_k != to) n += rewrite(served_k, to, 0); // new copies beside the ones known
if (n) {
applied = to;
rt_log("camera speed: x%.2f now, written over %d cop%s of the game's StickSensitivity in memory (%.0f ms)", to, n,
n == 1 ? "y" : "ies", (plat_now_ns() - t0) / 1e6);
} else if (to != was) {
rt_log("camera speed: x%.2f asked for, but the game's StickSensitivity (x%.2f) is not in memory%s", to, was,
tries ? "; looking again in a moment" : ": the camera keeps its speed until the game loads it");
}
plat_xchg32(&searching, 0);
}
void cam_pack_opened(void) { plat_xchg32(&reopened, 1); }
// Every present (gfx_present.c). A factor other than the one in effect goes to the copies the last search found at
// once; without them (the first change of a run, or the game moved them) a search starts, and a few more while one
// finds nothing (the game is still loading). A moment after the game opened the pack again a search looks for the
// start's factor, which is what the game has then read.
void cam_speed_tick(void) {
if (!base_n) return;
float want = cam_speed_wanted();
if (plat_xchg32(&reopened, 0) && want != served_k) due_ns = plat_now_ns() + 1500000000ull, tries = 3;
if (want == applied && !due_ns) return;
if (plat_cas32(&searching, 0, 0)) return; // a search writes applied and the copies
uint64_t now = plat_now_ns();
if (want != applied) {
if (rewrite_known(applied, want)) {
applied = asked = want;
return;
}
if (want != asked) asked = want, due_ns = now, tries = 3;
}
if (!due_ns || now < due_ns) return;
due_ns = 0;
if (tries > 0 && want != applied) tries--, due_ns = now + 4000000000ull; // again unless this one finds it
plat_xchg32(&searching, 1);
if (!plat_thread_spawn(search_main, NULL, "camera speed", 0)) plat_xchg32(&searching, 0);
}

View File

@@ -45,9 +45,10 @@ static const char *const known[] = {
"FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES",
"FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST",
"HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE",
"HDR_LIVE",
"HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD",
"BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST",
"MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE",
"BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "PIPE_LINK_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST",
"MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE",
"MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK",
"MV_SURVEY",
"ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC",
@@ -80,7 +81,7 @@ static const struct { const char *key, *section; } sections[] = {
{"SHARPNESS", "display"}, {"ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"},
{"COLOR_PRESET", "display"},
{"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"},
{"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"},
{"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_LIVE", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"},
{"HDR_UI_NITS", "display"}, {"HDR_GAMMA", "display"}, {"HDR_TONEMAP", "display"}, {"HDR_EXPOSURE", "display"},
{"HDR_HIGHLIGHTS", "display"}, {"HDR_SHADOWS", "display"}, {"HDR_CONTRAST", "display"}, {"HDR_SATURATION", "display"},
{"HDR_HIGHLIGHT_SAT", "display"}, {"HDR_BLOWOUT", "display"}, {"HDR_FLARE", "display"}, {"HDR_HUE", "display"},
@@ -111,7 +112,7 @@ static const struct { const char *key, *section; } sections[] = {
{"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"POOL_CREATE_BUDGET", "performance"},
{"POOL_CREATE_MS", "performance"},
{"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"},
{"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"},
{"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_PREBUILD", "performance"},
{"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"},
{"TEX_ALIAS", "performance"}, {"TEX_DISUSE", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"},
{"BIND_SKIP", "performance"}, {"SHADER_CACHE_FLUSH", "performance"}, {"SHADER_CARRY", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"},
@@ -150,9 +151,12 @@ static const CfgMeta metas[] = {
{"COLOR_PRESET", "default", "default|vivid|clarity|custom", LIVE}, {"COLOR_BRIGHTNESS", "0", "-30..30", LIVE},
{"COLOR_CONTRAST", "100", "70..150", LIVE}, {"COLOR_SATURATION", "100", "50..200", LIVE},
{"COLOR_GAMMA", "1", "0.8..1.2", LIVE},
// HDR output (vk_hdr.c): on at start (the HDR tonemap is spliced into the game's as shaders load); the rest apply at
// once. The grading sliders: 50 leaves the picture as it is, exposure in percent.
{"HDR", "0", "0|1|native", RESTART}, {"HDR_PEAK_NITS", "1000", "400..10000", LIVE}, {"HDR_GAME_NITS", "203", "48..500", LIVE},
// HDR output (vk_hdr.c): off and the tone mapper apply at once (the HDR tonemap is spliced into the game's as
// shaders load whatever is set, vk_hdr_armed); native is the game's own and is asked for once, at start, which the
// menu's row says (vk_hdr_needs_restart). HDR_LIVE = 0 leaves the HDR path out of a run that starts with HDR off.
// The grading sliders: 50 leaves the picture as it is, exposure in percent.
{"HDR", "0", "0|1|native", LIVE}, {"HDR_LIVE", "1", "0|1", RESTART},
{"HDR_PEAK_NITS", "1000", "400..10000", LIVE}, {"HDR_GAME_NITS", "203", "48..500", LIVE},
{"HDR_UI_NITS", "203", "48..500", LIVE}, {"HDR_GAMMA", "2.2", "off|2.2|2.4", LIVE},
{"HDR_TONEMAP", "renodrt", "vanilla|renodrt", LIVE}, {"HDR_EXPOSURE", "100", "0..200", LIVE},
{"HDR_HIGHLIGHTS", "50", "0..100", LIVE}, {"HDR_SHADOWS", "50", "0..100", LIVE}, {"HDR_CONTRAST", "50", "0..100", LIVE},
@@ -175,14 +179,14 @@ static const CfgMeta metas[] = {
{"OPERATION_MODE", "docked", "docked|handheld", LIVE},
{"ASSUME_FOCUS", "1", "0|1", LIVE}, // read on every focus change (ov_system.c)
// Sound, Controls, Menu
{"VOLUME", "100", "0..100", LIVE}, {"MENU_SOUND_VOLUME", "80", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", RESTART},
{"VOLUME", "100", "0..100", LIVE}, {"MENU_SOUND_VOLUME", "80", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", LIVE}, // audio_mixer.c audio_channels_live
{"AUDIO_DEVICE", "default", NULL, RESTART}, // default|<1-based index>|<part of a device name> (audio_mixer.c resolve_audio_device)
{"MICROPHONE", "1", "0|1", RESTART}, // ov_audio_input.c mic_on: 0 refuses the game's input streams
{"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE},
{"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "nintendo", "auto|nintendo|xbox|playstation", LIVE},
{"DS3", "0", "0|1", LIVE}, // input_ds3_win.c ds3_read: read a DualShock 3 directly; off: no HID scan
{"PROMPTS", "nintendo", "nintendo|xbox|playstation|pc", RESTART},
{"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", RESTART},
{"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", LIVE}, // camera_speed.c cam_speed_tick
{"LSTICK_DEADZONE", "", "0..50", LIVE}, {"RSTICK_DEADZONE", "", "0..50", LIVE},
{"MOUSE_MODE", "direct", "stick|direct", LIVE},
{"MOUSE_SENS", "1", "0..10", LIVE}, {"MENU_CURSOR", "1", "0|1", LIVE},
@@ -205,8 +209,10 @@ static const CfgMeta metas[] = {
// held submissions handed to the queue together (vk_sync.c, submit batching)
{"SUBMIT_BATCH", "4", "1..16", RESTART},
// shaders and pipelines
{"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART},
{"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "any", "strict|any|fullscreen", RESTART},
{"PIPE_LINK_MS", "6", "0..100", RESTART}, // vk_exec_draw.c link_budget
{"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART},
{"LIB_WARM", "1", "0|1|force", RESTART}, // vk_pipeline.c lw_start: the driver's cache of stage libraries, warmed when cold
{"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART},
{"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART},
{"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "0", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART},

View File

@@ -15,6 +15,7 @@
#include <stdlib.h>
#include <string.h>
#include "audren.h"
#include "cfg.h"
#include "flight.h"
#include "fs.h"
@@ -238,7 +239,8 @@ int gfx_window_focused(void) { return g_focused; }
unsigned gfx_window_focus_messages(void) { return (unsigned)g_focus_msgs; }
// Present path (g_pl held): a settings change (cfg_version) reaches the window thread, which applies a changed size or
// fullscreen; a changed NX2NAVI_VK_PRESENT rebuilds the swapchain at this present.
// fullscreen; a changed NX2NAVI_VK_PRESENT or NX2NAVI_HDR rebuilds the swapchain at this present.
static int g_sc_hdr_wanted; // vk_hdr_wanted when the swapchain was made (create_swapchain)
static void window_settings_live(void) {
static unsigned seen;
static int started;
@@ -253,7 +255,11 @@ static void window_settings_live(void) {
if (started) plat_xchg32(&g_dirty, 1);
}
if (started && vk_fg_wanted() != vk_fg_active()) plat_xchg32(&g_dirty, 1); // frame generation switched: its own swapchain
// HDR switched in the menu: a swapchain of the other format. Compared with what the swapchain was made for, not
// with what it got: on a display without HDR10 the setting stays on and the swapchain SDR, and that is settled.
if (started && vk_hdr_wanted() != g_sc_hdr_wanted) plat_xchg32(&g_dirty, 1);
ov_operation_mode_live();
audio_channels_live();
if (started) gfx_window_apply_settings();
started = 1;
}
@@ -362,7 +368,8 @@ static int create_swapchain(void) {
if (!nf) return plat_xchg32(&g_dirty, 1), 0;
// UNORM: the blit writes the bytes the game wrote (no sRGB encode). NX2NAVI_HDR: HDR10 when the surface offers it.
VkSurfaceFormatKHR sf = fmts[0];
int hdr = vk_hdr_surface_format(fmts, nf, &sf), found = hdr;
g_sc_hdr_wanted = vk_hdr_wanted();
int hdr = g_sc_hdr_wanted && vk_hdr_surface_format(fmts, nf, &sf), found = hdr;
for (uint32_t i = 0; i < nf && !found; i++)
if (fmts[i].format == VK_FORMAT_B8G8R8A8_UNORM && fmts[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { sf = fmts[i]; found = 1; }
for (uint32_t i = 0; i < nf && !found; i++)
@@ -428,6 +435,10 @@ static int create_swapchain(void) {
drop_swapchain_locked(c);
g_sc = made;
g_sc_fmt = sf.format;
static int hdr_was = -1; // the log says when a later swapchain is of the other kind (the menu's HDR row)
if (hdr_was >= 0 && hdr_was != hdr)
rt_log("gfx: the swapchain is now %s (format %d)", hdr ? "HDR10" : "SDR", (int)sf.format);
hdr_was = hdr;
g_sc_hdr = hdr;
vk_hdr_set_active(hdr);
if (hdr) vk_hdr_metadata(g_sc, 1);
@@ -962,6 +973,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
if (cw <= 0 || ch <= 0) cx = cy = 0, cw = w, ch = h; // a crop outside the image (the game's nvnWindowSetCrop):
// the whole image, not an inverted blit rectangle
window_settings_live();
cam_speed_tick();
// The menu overlay: laid out before recording (its first frame starts the ImGui back end and uploads the font)
const char *menu_shot = NULL;
MenuTarget mt = {c->instance, c->phys, c->device, c->gfx_family, c->gfx_queue,

View File

@@ -280,6 +280,8 @@ float cam_speed_applied(void); // the factor in effe
int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, void **out, size_t *out_n);
// Pack/Actor/PlayerCamera.pack.zs with the camera speeds
// multiplied, malloc'd; 0 = factor 1 or cannot (logged)
void cam_pack_opened(void); // romfs.c: the game opened the camera's pack
void cam_speed_tick(void); // every present: a changed setting goes into the game's memory
void cam_pack_served(void); // romfs.c serves the multiplied pack
// ---- game resolution (resolution.c): NX2NAVI_OUTPUT_RES, NX2NAVI_RENDER_SCALE
uint64_t res_heap_extra_bytes(void); // added to the application pool (mem_init), 2 MiB steps

View File

@@ -769,7 +769,7 @@ static void heapdef_overlay(void) {
// overlay that replaces the pack (a mod's camera stays), else the RomFS original; the dictionary is the RomFS's own.
#define CAMPACK "Pack/Actor/PlayerCamera.pack.zs"
static void camera_overlay(void) {
if (cam_speed_wanted() == 1.0f) return;
// also at factor 1: cam_pack_build reads the game's own values, which a change during the run needs
static const char keeps[] = "the camera keeps its own speed";
Overlay *base, *dic_base;
size_t n, dn;
@@ -1158,6 +1158,7 @@ uint32_t romfs_open(uint64_t *out, const char *path, int mode) {
uint64_t i = file_index(p);
if (i == UINT64_MAX) return FS_R_PATH_NOT_FOUND;
open_note(i, p);
if (!strcasecmp(p, CAMPACK)) cam_pack_opened(); // its values come into memory at the start's factor (camera_speed.c)
uint32_t ov = overlay_slot(i);
PlatFile *f = NULL;
if (ov) {

View File

@@ -65,6 +65,7 @@ int wide_available(void);
const char *const *vk_color_preset(const char *name);
int vk_hdr_wanted(void);
int vk_hdr_active(void);
int vk_hdr_needs_restart(void);
int vk_dlaa_needs_restart(void);
double frame_step_limit_hz(void);
int frame_limit_follows_display(void);
@@ -500,6 +501,7 @@ extern "C" void menu_vk_release(void) {
// NX2NAVI_MENU_TEST=<frame>[:<frames per page>]: at that presented frame the menu opens on its first tab; each tab stays
// <frames per page> frames (default 30) and its last frame's swapchain image goes to NX2NAVI_MENU_TEST_DIR (default: the
// working directory) as menu-test-<frame>-<page>.png; after the last tab the menu closes.
// NX2NAVI_MENU_TEST_CLICK=<key>:<part>[,...]: mouse clicks on the rows of the page shown (ui/option_rows.cpp click_test).
// NX2NAVI_MENU_TEST_SET=<frame>:<key>=<value>[,<key>=<value>...][;<frame>:...]: cfg_set of each key (NX2NAVI_ prefix
// optional) at that presented frame; changes live settings with no input, menu open or not. nx2navi.ini is written only by
// the pseudo-key SAVE=1 (cfg_save, as a row change does; a failed write shows the menu's save note).
@@ -859,18 +861,20 @@ static void row_upscaling(void) {
if (e.kind >= DLSS) opt_apply("NX2NAVI_TAA", e.kind == FSR3 ? "fsr3" : "dlss");
}
// "in use: off" under the HDR value when the game started with HDR on but the swapchain is not HDR10 (the display or
// Windows' HDR switch is off, vk_hdr_surface_format). The row's "after restart" comes from the setting itself (cfg.c).
// Under the HDR value: "after restart" when what is set waits for one (the game's own HDR, to or from it:
// vk_hdr_needs_restart), "in use: off" when HDR is on but the swapchain is not HDR10 (the display or Windows' HDR
// switch is off, vk_hdr_surface_format).
static const char *hdr_note(void) {
if (vk_hdr_needs_restart()) return menu_text(UI_MENU_AFTER_RESTART);
if (!vk_hdr_wanted() || vk_hdr_active() || !*gfx_window_present_mode() || !cfg_bool("NX2NAVI_HDR")) return nullptr;
static char note[96];
snprintf(note, sizeof note, menu_text(UI_MENU_PRESENT_IN_USE), menu_text(UI_MENU_FRAMEGEN_OFF));
return note;
}
// HDR (vk_hdr.c), after a restart: off, the HDR tone mapper over the game's picture or the game's native HDR. The
// brightness rows follow while it is on; the tone mapper's own rows only with the first (native HDR is the game's
// picture as it is), its grading rows only with its own curve chosen.
// HDR (vk_hdr.c): off or the HDR tone mapper over the game's picture, switched at once, or the game's native HDR,
// which the game takes up at its start. The brightness rows follow while it is on; the tone mapper's own rows only
// with the first (native HDR is the game's picture as it is), its grading rows only with its own curve chosen.
static void row_hdr(void) {
static const char *const values[] = {"0", "1", "native"};
const char *names[] = {menu_text(UI_MENU_FRAMEGEN_OFF), menu_text(UI_MENU_HDR_TONEMAPPED), menu_text(UI_MENU_HDR_NATIVE)};

View File

@@ -130,13 +130,83 @@ void opt_end(void) {
struct RowView {
const char *value; // right side; "" for none
bool is_default; // the value is the setting's default: the "default" mark, the value not highlighted
bool arrows; // a stepper: < value >, the arrows click too
bool arrows; // a stepper: [<] value [>]; the mouse changes the value on the two framed arrows only
const char *note; // under the value instead of the "default" mark; NULL for none
float col; // the width of the widest value the row can show (value_width); 0: this value's
bool end_l, end_r; // the value is the first / last one: that arrow is drawn faint and does nothing
};
// One row: draws it, handles A / click and left / right while selected. Returns 2 (A / click), -1 / +1 (left / right),
// 3 / 4 (move up / down, movable rows only) or 0; always 0 for a row the environment fixes (greyed out, still selectable
// so its description shows). movable: the top / left face buttons, Ctrl+Up / Ctrl+Down and two drawn arrows move it.
// The width of the widest of n values at the current font size: a stepper's left arrow stands that far from the right
// one whatever value shows, so it stays under the pointer while it is clicked through the values.
static float value_width(const char *const *names, int n) {
ImFont *font = ImGui::GetFont();
float fs = ImGui::GetFontSize(), w = 0;
for (int i = 0; i < n; i++)
if (names[i]) w = fmaxf(w, font->CalcTextSizeA(fs, FLT_MAX, 0, names[i]).x);
return w;
}
// One framed arrow of a stepper over [a, b]: a white frame with the arrow in its middle, filled a little while the
// pointer is on it and more while it is pressed; faint when it does nothing (dim: the row is greyed out or the value
// is at this end).
static void arrow_box(ImDrawList *dl, ImVec2 a, ImVec2 b, bool left, bool hot, bool down, bool dim, float s) {
const int al = dim ? 70 : hot ? 255 : 215;
const ImU32 white = IM_COL32(255, 255, 255, al);
if (hot && !dim) dl->AddRectFilled(a, b, IM_COL32(255, 255, 255, down ? 96 : 44), 4 * s);
dl->AddRect(a, b, white, 4 * s, 0, fmaxf(1.0f, (hot && !dim ? 2.2f : 1.6f) * s));
float cx = (a.x + b.x) * 0.5f, cy = (a.y + b.y) * 0.5f, aw = (b.y - a.y) * 0.38f, ah = aw * 0.8f;
float tip = left ? -aw * 0.55f : aw * 0.55f, back = left ? aw * 0.45f : -aw * 0.45f;
dl->AddTriangleFilled(ImVec2(cx + tip, cy), ImVec2(cx + back, cy - ah), ImVec2(cx + back, cy + ah), white);
}
// ---- test hook. NX2NAVI_MENU_TEST_CLICK=<key>:<part>[,<key>:<part>...], with NX2NAVI_MENU_TEST opening the menu on the
// page that has the rows (menu.cpp): mouse clicks on steppers by their setting's name (without NX2NAVI_), part l (the
// left arrow), r (the right arrow) or b (the row's body, on its label). One click after another, each over a dozen
// frames: the pointer goes there, the button goes down and up, and the log says what the setting is then. So a run
// checks that the arrows step a setting and that an arrow at its end and a click on the body do not, with the fonts
// and sizes of the game.
static struct ClickTest { int parsed, n, at, frame; struct { char key[56]; char part; } c[24]; } g_click;
static void click_test(const char *key, ImVec2 la, ImVec2 lb, ImVec2 ra, ImVec2 rb, ImVec2 label) {
ClickTest &t = g_click;
if (!t.parsed) {
t.parsed = 1;
const char *s = cfg_get("NX2NAVI_MENU_TEST_CLICK");
while (s && *s && t.n < 24) {
size_t n = strcspn(s, ",");
const char *colon = (const char *)memchr(s, ':', n);
if (colon) {
snprintf(t.c[t.n].key, sizeof t.c[0].key, "NX2NAVI_%.*s", (int)(colon - s), s);
t.c[t.n++].part = colon[1];
}
s += n;
if (*s == ',') s++;
}
}
if (t.at >= t.n || strcmp(key, t.c[t.at].key)) return;
const char part = t.c[t.at].part;
const ImVec2 to = part == 'l' ? ImVec2((la.x + lb.x) * 0.5f, (la.y + lb.y) * 0.5f)
: part == 'r' ? ImVec2((ra.x + rb.x) * 0.5f, (ra.y + rb.y) * 0.5f)
: label;
ImGuiIO &io = ImGui::GetIO();
const int f = t.frame++;
if (f == 0) io.AddMousePosEvent(to.x, to.y);
else if (f == 4) io.AddMouseButtonEvent(ImGuiMouseButton_Left, true);
else if (f == 7) io.AddMouseButtonEvent(ImGuiMouseButton_Left, false);
else if (f == 12) {
rt_log("menu: test hook: click on %s of %s: now %s",
part == 'l' ? "the left arrow" : part == 'r' ? "the right arrow" : "the body", key, cfg_value(key));
t.at++, t.frame = 0;
}
}
// One row: draws it, handles A / click and left / right while selected. Returns 2 (A, or a click on a row without
// arrows), -1 / +1 (left / right, or the mouse on a stepper's framed arrow), 3 / 4 (move up / down, movable rows only)
// or 0; always 0 for a row the environment fixes (greyed out, still selectable so its description shows). movable: the
// top / left face buttons, Ctrl+Up / Ctrl+Down and two drawn arrows move it.
// A stepper takes the mouse on its two framed arrows only: a click anywhere else on the row selects it and changes
// nothing. A click on the row used to step to the next value, and the left arrow stood left of the value's text, so
// it moved when a value of another width came up and the next click at the same place landed on the row instead.
static int row_text(const char *key, const char *label, const char *desc, const RowView &v, bool movable) {
const CfgMeta *m = cfg_meta(key);
const char *disabled = g_disable_next ? g_disable_note : NULL;
@@ -212,19 +282,38 @@ static int row_text(const char *key, const char *label, const char *desc, const
vfs = fmaxf(fs * cap / tv, fs * 0.75f);
tv = fminf(font->CalcTextSizeA(vfs, FLT_MAX, 0, v.value).x, cap);
}
if (v.arrows && v.value[0]) { // < value >, drawn (the font has no arrow glyphs); a click on one steps
float aw = fs * 0.42f, gap = fs * 0.55f, cy = ty + fs * 0.52f;
float rx = right - aw, lx = rx - gap - tv - gap - aw;
ImU32 ac = off ? kGrey : hov || foc ? SKIN_TEXT : SKIN_TEXT_DIM;
dl->AddTriangleFilled(ImVec2(lx, cy), ImVec2(lx + aw, cy - aw * 0.75f), ImVec2(lx + aw, cy + aw * 0.75f), ac);
dl->AddTriangleFilled(ImVec2(rx + aw, cy), ImVec2(rx, cy - aw * 0.75f), ImVec2(rx, cy + aw * 0.75f), ac);
if (by_mouse && !off) {
float mx = ImGui::GetIO().MousePos.x;
if (mx >= lx - gap && mx <= lx + aw + gap) step = -1;
else if (mx >= rx - gap && mx <= rx + aw + gap) step = 1;
float value_mid = -1; // a stepper's value and its note are centred here, between the arrows
if (v.arrows) { // [<] value [>]: the arrows are drawn (the font has no arrow glyphs), each in a white frame
// The frames stand in fixed places: the right one at the row's right edge, the left one the width of the row's
// widest value before it. They fit over the note line, in the label line's height.
const float bw = floorf(fs * 1.3f), bh = floorf(fs * 1.02f), gap = floorf(fs * 0.4f), cy = floorf(ty + fs * 0.52f);
float col = fminf(fmaxf(v.col, tv), fmaxf(cap - 2 * (bw + gap), fs));
if (tv > col) { // a value wider than the room between the arrows: smaller, then clipped
vfs = fmaxf(fs * col / font->CalcTextSizeA(fs, FLT_MAX, 0, v.value).x, fs * 0.75f);
tv = fminf(font->CalcTextSizeA(vfs, FLT_MAX, 0, v.value).x, col);
}
right = rx - gap;
value_left = lx;
const ImVec2 ra(right - bw, cy - bh * 0.5f), rb(right, cy + bh * 0.5f);
const ImVec2 la(ra.x - gap - col - gap - bw, ra.y), lb(ra.x - gap - col - gap, rb.y);
if (!g_click.parsed || g_click.at < g_click.n) click_test(key, la, lb, ra, rb, ImVec2(p.x + w * 0.25f, p.y + h * 0.5f));
const ImVec2 mp = ImGui::GetIO().MousePos;
// the pointer on an arrow: its frame and two pixels around it, so a click on the frame's own line counts
const float slop = 2 * g_scale;
auto on = [&](ImVec2 a, ImVec2 b) {
return hov && mp.x >= a.x - slop && mp.x < b.x + slop && mp.y >= a.y - slop && mp.y < b.y + slop;
};
const bool hl = on(la, lb), hr = on(ra, rb), down = ImGui::IsMouseDown(ImGuiMouseButton_Left);
if (by_mouse) step = 0; // a click on the row itself only selects it
// The press steps, and a held button repeats as a held key does; after a second it also goes ten steps at a
// time over a long range (opt_slider), as held left / right do.
if (!off && ((hl && !v.end_l) || (hr && !v.end_r)) && ImGui::IsMouseClicked(ImGuiMouseButton_Left, true)) {
step = hl ? -1 : 1;
if (ImGui::GetIO().MouseDownDuration[ImGuiMouseButton_Left] > 1.0f) g_held = true;
}
arrow_box(dl, la, lb, true, hl, hl && down, off || v.end_l, g_scale);
arrow_box(dl, ra, rb, false, hr, hr && down, off || v.end_r, g_scale);
value_mid = (lb.x + ra.x) * 0.5f;
right = ra.x - gap;
value_left = la.x;
}
if (movable) { // up and down arrows left of the value; a click on one moves the row
// a fixed value column (On / Off differ in width) keeps the arrows of the rows in one line
@@ -241,8 +330,8 @@ static int row_text(const char *key, const char *label, const char *desc, const
value_left = cx - gap;
}
if (v.value[0]) {
float vx = right - tv;
ImVec4 vclip(vx, p.y, right, p.y + h);
float vx = value_mid >= 0 ? floorf(value_mid - tv * 0.5f) : right - tv;
ImVec4 vclip(vx, p.y, vx + tv, p.y + h);
dl->AddText(font, vfs, ImVec2(vx, ty + (fs - vfs) * 0.5f),
off ? kGrey : v.is_default ? IM_COL32(232, 222, 198, 255) : IM_COL32(255, 255, 255, 255), v.value, NULL,
0, &vclip);
@@ -252,10 +341,11 @@ static int row_text(const char *key, const char *label, const char *desc, const
if (v.note || v.is_default) { // under the value, right-aligned with it; its start shows when it is too long
const char *t = v.note ? v.note : menu_text(UI_MENU_DEFAULT_MARK);
float tw = fminf(font->CalcTextSizeA(small, FLT_MAX, 0, t).x, cap);
ImVec4 nclip(right - tw, p.y, right, p.y + h);
dl->AddText(font, small, ImVec2(right - tw, ny), v.note ? IM_COL32(250, 206, 130, 255) : SKIN_TEXT_DIM, t, NULL, 0,
&nclip);
if (tw > 0) note_right = right - tw - fs * 0.6f;
// a stepper's note is centred under its value, as far as the row's right edge lets it
float nx = value_mid >= 0 ? fminf(floorf(value_mid - tw * 0.5f), p.x + w - pad - tw) : right - tw;
ImVec4 nclip(nx, p.y, nx + tw, p.y + h);
dl->AddText(font, small, ImVec2(nx, ny), v.note ? IM_COL32(250, 206, 130, 255) : SKIN_TEXT_DIM, t, NULL, 0, &nclip);
if (tw > 0) note_right = nx - fs * 0.6f;
}
// the label and its note, clipped before the value
ImVec4 clip(p.x, p.y, value_left - fs * 0.6f, p.y + h);
@@ -311,11 +401,17 @@ int opt_save_tick(int force) {
// ---- rows
// An on / off row is a stepper too: either arrow, A, left or right flips it.
static float on_off_width(void) {
const char *const names[] = {menu_text(UI_MENU_ON), menu_text(UI_MENU_OFF)};
return value_width(names, 2);
}
bool opt_toggle(const char *key, UiTextId label, UiTextId desc, bool inverted) {
const char *d = cfg_default(key);
bool set = cfg_bool(key) != 0, def = d && *d && strcmp(d, "0") != 0;
bool on = set != inverted; // what the row shows
RowView v = {menu_text(on ? UI_MENU_ON : UI_MENU_OFF), set == def, false, NULL};
RowView v = {menu_text(on ? UI_MENU_ON : UI_MENU_OFF), set == def, true, NULL, on_off_width()};
if (!row(key, label, desc, v)) return false; // any step flips it
apply(key, set ? "0" : "1");
return true;
@@ -326,7 +422,7 @@ bool opt_toggle_value(const char *key, UiTextId label, UiTextId desc, const char
// when it is an on value, so turning the switch off and on again puts the profile's own value back
const char *d = cfg_default(key);
bool on = strcmp(cfg_value(key), off_value) != 0, def = d && strcmp(d, off_value) != 0;
RowView v = {menu_text(on ? UI_MENU_ON : UI_MENU_OFF), on == def, false, note};
RowView v = {menu_text(on ? UI_MENU_ON : UI_MENU_OFF), on == def, true, note, on_off_width()};
if (!row(key, label, desc, v)) return false; // any step flips it
apply(key, on ? off_value : def && *d ? d : on_value);
return true;
@@ -339,7 +435,8 @@ bool opt_button(const char *id, UiTextId label, UiTextId desc, const char *value
bool opt_toggle_text(const char *id, const char *label, const char *desc, bool on, const char *value) {
// off is the plain state: its value drawn dim, without the "default" mark
RowView v = {value ? value : menu_text(on ? UI_MENU_ON : UI_MENU_OFF), !on, false, on ? NULL : ""};
RowView v = {value ? value : menu_text(on ? UI_MENU_ON : UI_MENU_OFF), !on, true, on ? NULL : "",
value ? 0 : on_off_width()};
return row_text(id, label, desc, v, false) != 0; // any step flips it
}
@@ -391,7 +488,7 @@ bool opt_choice_list(const char *key, UiTextId label, const char *const *values,
int def = -1;
for (int i = 0; d && i < n; i++)
if (!strcmp(values[i], d)) def = i;
RowView v = {cur >= 0 ? names[cur] : now, cur == def, true, note};
RowView v = {cur >= 0 ? names[cur] : now, cur == def, true, note, value_width(names, n), cur == 0, cur == n - 1};
int step = row(key, label, desc, v);
if (!step || n <= 0) return false;
int nv;
@@ -405,7 +502,8 @@ bool opt_choice_list(const char *key, UiTextId label, const char *const *values,
int opt_choice_index(const char *key, UiTextId label, const char *const *names, int n, int cur, int def, UiTextId desc,
const char *note) {
RowView v = {cur >= 0 && cur < n ? names[cur] : "", cur == def, true, note};
RowView v = {cur >= 0 && cur < n ? names[cur] : "", cur == def, true, note, value_width(names, n), cur == 0,
cur == n - 1};
int step = row(key, label, desc, v);
if (!step || n <= 0) return -1;
int nv = cur < 0 ? (step < 0 ? n - 1 : 0) : step == 2 ? (cur + 1) % n : cur + step < 0 ? 0 : cur + step >= n ? n - 1 : cur + step;
@@ -452,9 +550,13 @@ bool opt_slider(const char *key, UiTextId label, float lo, float hi, UiTextId de
// follows the fallback's default, not 0, even once the key is set.
if (fallback && !(d && *d)) d = cfg_default(fallback);
double v = cfg_double(src), def = d && *d ? strtod(d, NULL) : lo;
char shown[48];
char shown[48], ends[2][48];
snprintf(shown, sizeof shown, fmt, v);
RowView rv = {shown, fabs(v - def) < step * 0.01, true, note};
snprintf(ends[0], sizeof ends[0], fmt, (double)lo);
snprintf(ends[1], sizeof ends[1], fmt, (double)hi);
const char *const widest[] = {shown, ends[0], ends[1]};
RowView rv = {shown, fabs(v - def) < step * 0.01, true, note, value_width(widest, 3), v <= lo + step * 0.01,
v >= hi - step * 0.01};
int s = row(key, label, desc, rv);
if (!s) return false;
int steps = g_held && (hi - lo) > 40 * step ? 10 : 1; // held: ten steps at a time over a long range

View File

@@ -1,7 +1,9 @@
// Settings pages in two panes (ui/option_rows.cpp): option rows on the left, the selected (controller) or hovered (mouse)
// row's description on the right. Each row shows a setting of the cfg table (cfg.h) and changes it through cfg_set: A or
// a click steps it (toggle, next value), left and right step it. A row marks the default value, says "after restart" when
// the setting is read at start, and is greyed out with a note when an environment variable fixes it.
// row's description on the right. Each row shows a setting of the cfg table (cfg.h) and changes it through cfg_set: A
// steps it (toggle, next value), left and right step it, and the mouse steps it on the two framed arrows beside its
// value and nowhere else on the row (a click on the row selects it; held on an arrow it repeats). A row marks the
// default value, says "after restart" when the setting is read at start, and is greyed out with a note when an
// environment variable fixes it.
//
// opt_begin("##screen", scale);
// opt_toggle("NX2NAVI_FULLSCREEN", UI_MENU_FULLSCREEN, UI_MENU_FULLSCREEN_DESC);

View File

@@ -173,11 +173,13 @@ void vk_color_release(void); // before the device goes away
// The brightness, contrast, saturation and gamma values (as the settings write them) of a colour profile, NULL for
// custom or an unknown name (vk_color.c).
const char *const *vk_color_preset(const char *name);
// HDR output (vk_hdr.c). vk_hdr_wanted: NX2NAVI_HDR, read once. vk_hdr_surface_format: the HDR10 surface format when wanted
// and offered (1). vk_hdr_set_active: gfx_present.c says whether the swapchain is HDR10 (vk_hdr_active). vk_hdr_encode:
// HDR output (vk_hdr.c). vk_hdr_armed: the HDR path is set up for this run (decided at the start). vk_hdr_wanted:
// NX2NAVI_HDR as the menu has it now. vk_hdr_surface_format: the HDR10 surface format when wanted and offered (1). vk_hdr_set_active: gfx_present.c says whether the swapchain is HDR10 (vk_hdr_active). vk_hdr_encode:
// like vk_color_record, src's crop as PQ code values (RGBA16F, crop {0, 0, w, h}). vk_hdr_ui_target / vk_hdr_ui_compose:
// the in-game menu's RGBA8 target, then blended onto the swapchain image. vk_hdr_to_srgb8: 10-bit PQ pixels to sRGB bytes.
int vk_hdr_armed(void);
int vk_hdr_wanted(void);
int vk_hdr_needs_restart(void); // the menu's HDR row: the value set takes effect at the next start
void vk_hdr_off(void); // probes that read back 8-bit swapchain pixels: SDR whatever the setting
int vk_hdr_surface_format(const VkSurfaceFormatKHR *f, uint32_t n, VkSurfaceFormatKHR *out);
void vk_hdr_metadata(VkSwapchainKHR sc, int new_sc);

View File

@@ -86,7 +86,8 @@ static int create_instance(void) {
// DLSS Frame Generation: Streamline's plugins call its naming functions even when they say it is off
if (validate || (vk_dlssg_wanted() && instance_ext_present(VK_EXT_DEBUG_UTILS_EXTENSION_NAME)))
ext[n_ext++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
if (vk_hdr_wanted() && instance_ext_present(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME)) // HDR10 surface formats
// armed, not wanted: the menu switches HDR on during the run (vk_hdr.c), and the extensions are fixed here
if (vk_hdr_armed() && instance_ext_present(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME)) // HDR10 surface formats
ext[n_ext++] = VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME;
vk_dlaa_instance_exts(ext, &n_ext, 12);
VkApplicationInfo app = {.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "NX2navi",
@@ -324,7 +325,7 @@ static int create_device(void) {
// GPU timestamps on the CPU clock (vk_sync.c): the perf line splits GPU idle time into waiting for a submission or not.
if (has_ext(ep, ne, VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME)) { g_vk.has_calibrated = 2; ext[n_ext++] = VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME; }
else if (has_ext(ep, ne, VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME)) { g_vk.has_calibrated = 1; ext[n_ext++] = VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME; }
if (vk_hdr_wanted() && has_ext(ep, ne, VK_EXT_HDR_METADATA_EXTENSION_NAME)) { // the display's peak (vk_hdr_metadata)
if (vk_hdr_armed() && has_ext(ep, ne, VK_EXT_HDR_METADATA_EXTENSION_NAME)) { // the display's peak (vk_hdr_metadata)
g_vk.has_hdr_metadata = 1;
ext[n_ext++] = VK_EXT_HDR_METADATA_EXTENSION_NAME;
}

View File

@@ -2532,7 +2532,7 @@ static void support_fill(uint8_t *b, float w, float h, float point_size) {
memcpy(b + 1408, pcss_params(), 16); // pcss (translator patch 0010): soft sun shadows in the cascade compares
memcpy(b + 1424, shadow_spread_params(), 16); // shadow_spread (translator patch 0011): sun shadow filter width
for (int i = 0; i < 73; i++) memcpy(b + 192 + 16 * i, &one, 4); // render scale 1: the host renders at guest size
if (vk_hdr_wanted()) { // read by the game's tonemap only, once vk_hdr.c tonemap_splice has grown its block past the translator's
if (vk_hdr_armed()) { // read by the game's tonemap only, once vk_hdr.c tonemap_splice has grown its block past the translator's
float hdr[16];
vk_hdr_support(hdr);
memcpy(b + 1440, hdr, sizeof hdr);
@@ -3251,23 +3251,53 @@ static void foliage_shadow_cull(const VkExec *ex, VkPipeState *s) {
// buffer or g-buffer with a colour target the frame before did not use: half-resolution effect buffers in fights).
// Passes with none (volumes, LUTs, shadow layers seen for the first time) still wait, and so do passes whose only
// recent target is the depth buffer while a colour target is rendered for the first time (item icon captures).
// fullscreen (2, default): as any, and full-screen draws into such passes are skipped instead of waiting from their
// Full-screen draws into such passes wait from their budget of 50 ms a presented frame. The default: a skipped
// full-screen draw is a lighting, fog or tone mapping pass that leaves its target as it was, which shows as a black
// or unlit frame with the interface intact, and with the stage libraries in the driver's cache the wait is a
// millisecond (vk_pipeline.c lw_start keeps them there).
// fullscreen (2): as any, and full-screen draws into such passes are skipped instead of waiting from their
// budget. A full-screen pass whose stage libraries are cold in the driver cache took 86 ms on average (up to 194 ms)
// in a fight on an RX 9070: three frames in a row each spent the 50 ms budget, and some of the draws were skipped
// after all. Skipping costs the pass for the frames until the pipeline is ready.
// after all. Skipping costs the pass for the frames until the pipeline is ready. It was the default until the
// libraries were kept warm.
// Any other draw whose pipeline is missing waits for its fast link from a small budget per presented frame
// (link_budget) before it is skipped.
static int pipe_skip_mode(void) {
static int mode = -1;
if (mode < 0) {
const char *e = cfg_value("NX2NAVI_PIPE_SKIP");
mode = !strcmp(e, "strict") || !strcmp(e, "0") ? 0 : !strcmp(e, "fullscreen") || !strcmp(e, "2") ? 2 : 1;
static const char *const what[3] = {"only draws into per-frame passes (NX2NAVI_PIPE_SKIP=strict)",
"draws into passes with a target the previous frame rendered",
"draws into passes with a target the previous frame rendered; full-screen "
"draws wait (NX2NAVI_PIPE_SKIP=any)",
"those and full-screen draws (NX2NAVI_PIPE_SKIP=fullscreen)"};
rt_log("vk_exec: draws skipped while their pipeline builds: %s", what[mode]);
}
return mode;
}
// NX2NAVI_PIPE_LINK_MS (default 6; 0 = off): how long the draws of one presented frame may wait, in all, for the fast
// link of a pipeline that is missing, LINK_DRAW_MS at most each. A draw into a per-frame pass used to be skipped at
// once and for as long as its pipeline built, also when its stage libraries were in the driver's cache and the link
// took under a millisecond: geometry seen for the first time (ground, a wall, a character) was missing for a frame or
// more and then appeared. With the libraries warm the wait ends within the budget and nothing is skipped; with them
// cold it runs out after a few draws and the rest are skipped as before, so a burst of cold pipelines costs a frame
// this much and no more. Measured on an RX 7900 XT, first walk through a village with no pipeline recorded: 2,349
// draws skipped without the budget, with the libraries warm in both runs.
#define LINK_DRAW_MS 3u
static uint64_t *link_budget(void) {
static uint64_t frame = ~0ull, left, per_frame = ~0ull;
if (per_frame == ~0ull) {
long long ms = cfg_int("NX2NAVI_PIPE_LINK_MS");
per_frame = (uint64_t)(ms < 0 ? 0 : ms > 100 ? 100 : ms) * 1000000ull;
rt_log("vk_exec: draws wait up to %llu ms a frame in all for the fast link of a missing pipeline "
"(NX2NAVI_PIPE_LINK_MS)", (unsigned long long)(per_frame / 1000000));
}
uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
if (f != frame) frame = f, left = per_frame;
return &left;
}
// The previous draw's shaders, kept while its program, bound pipeline state, the vertex and fragment stages' first 32
// texture and image handles and every slot's resolution (vk_texture_slots_gen, read unlocked: a registration racing
// with it is ordered after this draw, as answers_for's own check would order it) stay the same: answers_for reads
@@ -3463,6 +3493,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int
uint32_t wait_ms = 0;
uint64_t t0 = 0;
uint64_t *budget = NULL; // the per-frame budget a timed wait is charged to
int linking = 0; // the wait is the short one for a fast link (link_budget)
int large = (uint64_t)ex->pass_w * ex->pass_h >= ONESHOT_LARGE_PX;
if (oneshot > 0 && oneshot_budget_on()) {
budget = oneshot_budget(large);
@@ -3476,6 +3507,12 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int
wait_ms = (uint32_t)(wait_left_ns / 1000000);
if (wait_ms) t0 = kern_now_ns();
budget = &wait_left_ns;
} else {
budget = link_budget();
wait_ms = (uint32_t)(*budget / 1000000);
if (wait_ms > LINK_DRAW_MS) wait_ms = LINK_DRAW_MS;
linking = wait_ms != 0;
if (!wait_ms) budget = NULL;
}
// The previous draw's pipeline while its state repeats (PM, NX2NAVI_PIPE_MEMO=0 turns it off): the cache key is made
// from the state alone, and a pipeline once found only changes from its fast link to its optimised build, read
@@ -3487,13 +3524,21 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int
info = PM.info, pending = 0;
p = __atomic_load_n(&info->pipeline, __ATOMIC_ACQUIRE);
} else {
w0 = t0 ? t0 : wait_ms ? kern_now_ns() : 0; // the clock is read only for draws that may wait
p = vk_pipeline_graphics_async(&s, &info, &pending, wait_ms);
if (linking) { // nearly every draw finds its pipeline: the clock is read only for one that did not
p = vk_pipeline_graphics_async(&s, &info, &pending, 0);
if (pending) {
t0 = kern_now_ns();
p = vk_pipeline_graphics_async(&s, &info, &pending, wait_ms);
}
} else {
w0 = t0 ? t0 : wait_ms ? kern_now_ns() : 0; // the clock is read only for draws that may wait
p = vk_pipeline_graphics_async(&s, &info, &pending, wait_ms);
}
if (t0 && budget) {
uint64_t spent = kern_now_ns() - t0;
*budget = spent < *budget ? *budget - spent : 0;
}
why = oneshot > 0 ? "one-shot, shader" : wait_ms ? "full-screen" : NULL;
why = oneshot > 0 ? "one-shot, shader" : linking ? NULL : wait_ms ? "full-screen" : NULL;
if (pending && oneshot < 0 && !vk_exec_targets_recent(ex, skip >= 1)) {
perf_count(PERF_CNT_ONESHOT_WAIT);
why = "one-shot";

View File

@@ -1,8 +1,10 @@
// HDR output (NX2NAVI_HDR=1): an HDR10 swapchain (Rec. 2020 primaries, PQ) and a game picture tone mapped to the
// display's range. Settings: NX2NAVI_HDR (read at start); the nits NX2NAVI_HDR_PEAK_NITS (the display's peak),
// display's range. Settings: NX2NAVI_HDR, off and the tone mapper following the menu during a run (vk_hdr_wanted), the
// game's own HDR (native) from the start only; the nits NX2NAVI_HDR_PEAK_NITS (the display's peak),
// NX2NAVI_HDR_GAME_NITS (the picture's white, "paper white") and NX2NAVI_HDR_UI_NITS (the HUD's and this menu's white);
// NX2NAVI_HDR_GAMMA, NX2NAVI_HDR_TONEMAP and the grading sliders NX2NAVI_HDR_EXPOSURE .. NX2NAVI_HDR_GRADE, all read
// when the settings change (vk_hdr_support). With NX2NAVI_HDR=0 nothing here runs and the present is what it was.
// when the settings change (vk_hdr_support). With NX2NAVI_HDR=0 the swapchain is SDR, the spliced tonemap gives the
// game's own SDR picture (vk_hdr_support, p0.w = -1) and the present is what it was.
//
// The game's picture (docs/architecture.md, "HDR"). The game tonemaps its scene into an r11g11b10f target, upscales it
// and draws the HUD into a second one (float, nothing in these passes clamps), and one last draw copies that into the
@@ -45,24 +47,56 @@
// ---- settings
static int g_wanted = -1;
// What the start decides for the whole run (hdr_start, first asked when the Vulkan instance is made):
// - armed: the HDR path is set up, so the menu can switch HDR on and off without a restart: the instance and device
// get the HDR extensions (vk_device.c), the game's tonemap is spliced as its shaders load (vk_hdr_shader_swap) and
// every draw's support buffer carries the settings (vk_exec_draw.c). While the swapchain is SDR the spliced tonemap
// returns the game's own SDR result. Always, unless NX2NAVI_HDR_LIVE = 0 with NX2NAVI_HDR = 0 (the path left out as it
// was before the setting followed the menu: HDR then needs a restart) or a probe turned it off (vk_hdr_off).
// - native: NX2NAVI_HDR = native, the game's own HDR. The display is reported as HDR10 (ov_gfx.c
// QueryDisplayHdrStatus, with the peak and paper white of the settings), and the game then renders its HDR picture
// itself and presents it as PQ-encoded Rec. 2020 in A2B10G10R10 (format 64), which goes to the HDR10 swapchain as it
// is: no tonemap splice, no float picture, no encode (pictures still presented in 8 bits, before the switch, are
// encoded as for 1). The game asks once, so a change to or from native waits for a restart (vk_hdr_needs_restart).
static int g_armed = -1, g_native = -1;
static void hdr_start(void) {
if (g_armed >= 0) return;
g_native = !strcmp(cfg_value("NX2NAVI_HDR"), "native");
g_armed = cfg_bool("NX2NAVI_HDR") || cfg_bool("NX2NAVI_HDR_LIVE"); // last: the mark that both are read
}
int vk_hdr_armed(void) {
hdr_start();
return g_armed;
}
int vk_hdr_native(void) { return vk_hdr_armed() && g_native; }
// HDR is on now: the setting as the menu has it, read again when the settings change. The swapchain follows at the
// next present (gfx_present.c window_settings_live makes it again) and vk_hdr_active then says what it got. A run that
// started with the game's own HDR keeps it: the game goes on presenting its PQ picture whatever the menu says.
int vk_hdr_wanted(void) {
if (g_wanted < 0) g_wanted = cfg_bool("NX2NAVI_HDR");
return g_wanted;
static unsigned seen = ~0u;
static int on;
if (!vk_hdr_armed()) return 0;
if (g_native) return 1;
unsigned v = cfg_version();
if (__atomic_load_n(&seen, __ATOMIC_ACQUIRE) != v) {
__atomic_store_n(&on, cfg_bool("NX2NAVI_HDR") != 0, __ATOMIC_RELEASE);
__atomic_store_n(&seen, v, __ATOMIC_RELEASE);
}
return __atomic_load_n(&on, __ATOMIC_ACQUIRE);
}
// NX2NAVI_HDR = native: the game's own HDR. The display is reported as HDR10 (ov_gfx.c QueryDisplayHdrStatus, with
// the peak and paper white of the settings), and the game then renders its HDR picture itself and presents it as
// PQ-encoded Rec. 2020 in A2B10G10R10 (format 64), which goes to the HDR10 swapchain as it is: no tonemap splice, no
// float picture, no encode (pictures still presented in 8 bits, before the switch, are encoded as for 1).
static int g_native = -1;
int vk_hdr_native(void) {
if (g_native < 0) g_native = !strcmp(cfg_value("NX2NAVI_HDR"), "native");
return g_native && vk_hdr_wanted();
// The menu's HDR row: what is set takes a restart. To or from the game's own HDR, and switching HDR on in a run that
// started without the HDR path (NX2NAVI_HDR_LIVE = 0).
int vk_hdr_needs_restart(void) {
hdr_start();
return (!strcmp(cfg_value("NX2NAVI_HDR"), "native")) != g_native || (!g_armed && cfg_bool("NX2NAVI_HDR"));
}
void vk_hdr_off(void) { g_wanted = 0; }
void vk_hdr_off(void) { g_native = 0, g_armed = 0; }
// The settings, read again when they change. The replay (every draw's support buffer) and the present thread read
// them: an update fills the snapshot not in use and then publishes it.
@@ -529,7 +563,8 @@ fail:
#undef PUT
void vk_hdr_shader_swap(uint32_t **spirv, uint64_t *words) {
if (!vk_hdr_wanted() || vk_hdr_native() || !*spirv || *words < 5) return;
// armed, not wanted: a shader loaded while HDR is off must be ready for the menu switching it on
if (!vk_hdr_armed() || vk_hdr_native() || !*spirv || *words < 5) return;
if (tonemap_splice(spirv, words)) {
static volatile int32_t spliced;
rt_log("hdr: the game's tonemap maps its picture onto the display's range (%d shaders so far)",
@@ -618,8 +653,8 @@ void vk_hdr_image_gone(VkImage image) { // a texture image went away: forget i
// ---- swapchain
// With NX2NAVI_HDR=1, the HDR10 format among the surface's (10-bit UNORM, ST 2084): 1 with *out set, else 0 and the
// reason logged once.
// While NX2NAVI_HDR is on (vk_hdr_wanted), the HDR10 format among the surface's (10-bit UNORM, ST 2084): 1 with *out
// set, else 0 and the reason logged once.
int vk_hdr_surface_format(const VkSurfaceFormatKHR *f, uint32_t n, VkSurfaceFormatKHR *out) {
if (!vk_hdr_wanted()) return 0;
static int logged;

View File

@@ -4748,12 +4748,189 @@ static void lb_state(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) {
LB.samples = (uint8_t)(1u << bf);
}
// ---- the driver's cache of stage libraries, warmed again during play
//
// The preparation (lb_warm_prepared) builds every prepared variant's stage library once, so the driver's disk cache
// holds it and the library a draw needs later takes a few milliseconds instead of a compile: 3 ms against 100 ms for
// a vertex and 340 ms for a fragment library on an RX 7900 XT (driver 26.10.1). The driver's cache does not outlive
// everything, though: a driver update empties it, the AMD driver keeps a limited amount and drops old entries, and a
// change to what a library is built with (the dynamic states it lists, as when dynamic vertex input was turned off for
// this driver) gives every library another entry. After any of them each pipeline a draw first needs compiles cold:
// the draw is skipped for the frames that takes, or waits for it. Measured on that card with the cache cold, a walk
// through a village not seen before skipped 11,180 draws while their pipelines built and 94 draws waited 5.5 s in
// all, one of them 1.9 s.
//
// So every start checks: a sample of the prepared fragment libraries is built (lw_probe), and when their median time
// says the cache is cold, the whole prepared list is built again into the driver's cache on a few idle-priority
// threads while the game runs (lw_worker), as the preparation does but without stopping play. shader-warm.txt beside
// the caches says where that stands: "warming" while it runs, so a start after a run that ended meanwhile goes on,
// and "done" with the driver and a count of the times the cache was cold again right after a finished warm. At two in
// a row the driver does not keep stage libraries at all and warming stops being tried (NX2NAVI_LIB_WARM=0 turns it off;
// =force warms at every start).
#define LW_SAMPLE 12
#define LW_COLD_MS 15.0
#define LW_MAX_THREADS 6
static struct {
GfxWarmEnt *e;
size_t n;
volatile int64_t next, done;
volatile int32_t running, stop, started;
PlatThread *th[LW_MAX_THREADS + 1];
int threads, strikes;
double t0;
char stamp[PLAT_PATH_MAX * 2 + 32], id[64];
} LW;
static void lw_stamp(const char *state, int strikes) {
char line[128];
int n = snprintf(line, sizeof line, "%s %s %d\n", state, LW.id, strikes);
if (!plat_file_write_atomic(LW.stamp, line, (size_t)n)) rt_log("vk_pipeline: cannot write %s", LW.stamp);
}
// One prepared vertex or fragment variant's library into the driver's cache; its time in ms, negative when the
// variant is on the skip list, not in the shader cache or of another stage.
static double lw_build(const GfxWarmEnt *e) {
if (e->stage != GFX_STAGE_VS && e->stage != GFX_STAGE_FS) return -1;
if (skip_listed(e->iid)) return -1;
int owned = 0;
const GfxShader *sh = gfx_shader_load_private(e->iid, &owned);
if (!sh) return -1;
double t0 = now_ms();
int st = gfx_shader_stage(sh);
if (st == GFX_STAGE_VS || st == GFX_STAGE_FS) lb_library(sh, st == GFX_STAGE_VS, 0);
double ms = now_ms() - t0;
if (owned) gfx_shader_release_private(sh);
return ms;
}
static void lw_worker(void *arg) {
(void)arg;
plat_thread_priority(PLAT_PRIO_IDLE);
for (;;) {
size_t i = (size_t)plat_inc64(&LW.next) - 1;
if (i >= LW.n || LW.stop) break;
lw_build(&LW.e[i]);
int64_t d = plat_inc64(&LW.done);
if (d % 8000 == 0)
rt_log("vk_pipeline: library warm: %lld of %zu prepared variants in %.0f s", (long long)d, LW.n,
(now_ms() - LW.t0) / 1000);
}
if (!plat_dec32(&LW.running) && !LW.stop) {
lw_stamp("done", LW.strikes);
rt_log("vk_pipeline: library warm: done, %zu prepared variants in %.0f s on %d idle threads; new pipelines "
"link from the driver's cache from here on", LW.n, (now_ms() - LW.t0) / 1000, LW.threads);
}
}
// The median time of LW_SAMPLE prepared fragment libraries spread over the list; negative without that many.
static double lw_probe(void) {
double ms[LW_SAMPLE];
int got = 0;
size_t step = LW.n / LW_SAMPLE ? LW.n / LW_SAMPLE : 1, at = (size_t)(plat_now_ns() / 1000) % step;
for (size_t i = at; i < LW.n && got < LW_SAMPLE && !LW.stop; i += step)
for (size_t j = i; j < i + step && j < LW.n; j++) { // the next fragment variant from here
if (LW.e[j].stage != GFX_STAGE_FS) continue;
double t = lw_build(&LW.e[j]);
if (t >= 0) ms[got++] = t;
break;
}
if (got < LW_SAMPLE / 2) return -1;
for (int i = 1; i < got; i++)
for (int j = i; j > 0 && ms[j] < ms[j - 1]; j--) {
double t = ms[j];
ms[j] = ms[j - 1], ms[j - 1] = t;
}
return ms[got / 2];
}
static void lw_main(void *arg) {
(void)arg;
plat_thread_priority(PLAT_PRIO_BELOW_NORMAL);
char was[16] = "", id[64] = "";
int strikes = 0;
uint64_t size = 0;
char *f = (char *)plat_file_read(LW.stamp, &size);
if (f && size < 200) {
char line[200];
memcpy(line, f, size), line[size] = 0;
if (sscanf(line, "%15s %63s %d", was, id, &strikes) < 2) was[0] = 0;
}
free(f);
int same = !strcmp(id, LW.id), resume = same && !strcmp(was, "warming");
const char *mode = cfg_value("NX2NAVI_LIB_WARM");
double median = resume ? 0 : lw_probe();
if (LW.stop) return;
int cold = median > LW_COLD_MS || !strcmp(mode, "force");
if (!resume && !cold) {
if (same && strikes) lw_stamp("done", 0); // the driver kept them this time
rt_log("vk_pipeline: library warm: the driver's cache has the prepared libraries (a sample's median %.1f ms)",
median);
return;
}
if (!resume && same && strikes >= 2 && strcmp(mode, "force")) {
rt_log("vk_pipeline: library warm: the prepared libraries are cold again (a sample's median %.0f ms) after two "
"finished warms on this driver: it does not keep them, so pipelines compile when a draw first needs "
"them (NX2NAVI_LIB_WARM=force warms anyway)", median);
return;
}
LW.strikes = resume ? strikes : same && !strcmp(was, "done") ? strikes + 1 : 0;
lw_stamp("warming", LW.strikes);
int cpus = (int)plat_cpu_usable(), t = cpus / 4;
t = t < 2 ? 2 : t > LW_MAX_THREADS ? LW_MAX_THREADS : t;
if (resume)
rt_log("vk_pipeline: library warm: going on with the warm an earlier run began: %zu prepared variants on %d "
"idle threads", LW.n, t);
else
rt_log("vk_pipeline: library warm: the driver's cache does not have the prepared libraries (a sample's median "
"%.0f ms; a driver update or a change of what they are built with): building %zu prepared variants "
"into it on %d idle threads while the game runs. Until it is done, a pipeline a draw first needs may "
"compile, which skips that draw for some frames or makes it wait", median, LW.n, t);
LW.t0 = now_ms();
LW.running = t;
int started = 0;
for (int i = 0; i < t; i++)
if ((LW.th[1 + started] = plat_thread_new(lw_worker, NULL, "lib warm"))) started++;
LW.threads = started;
if (plat_add32(&LW.running, started - t) == 0 && !started) rt_log("vk_pipeline: library warm: no thread started");
}
// Boot, once the library state is known (lb_state): the prepared list is read and the check starts on its own thread.
static void lw_start(void) {
const char *mode = cfg_value("NX2NAVI_LIB_WARM");
if (!P.gpl || !strcmp(mode, "0") || plat_xchg32(&LW.started, 1)) return;
char dir[PLAT_PATH_MAX * 2], path[PLAT_PATH_MAX * 2 + 32];
if (!cache_dir(dir, sizeof dir)) return;
snprintf(path, sizeof path, "%s/shader-prepared.bin", dir);
snprintf(LW.stamp, sizeof LW.stamp, "%s/shader-warm.txt", dir);
uint64_t size = 0;
uint8_t *f = plat_file_read(path, &size);
size_t n = f && size >= 16 ? (size_t)(size - 16) / 36 : 0;
LW.e = n ? calloc(n, sizeof *LW.e) : NULL;
for (size_t i = 0; LW.e && i < n; i++)
memcpy(LW.e[i].iid, f + 16 + 36 * i + 4, 32), LW.e[i].stage = f[16 + 36 * i], LW.e[i].prio = f[16 + 36 * i + 1];
free(f);
if (!LW.e) return; // nothing prepared (a build without the preparation step)
LW.n = n;
VkPhysicalDeviceProperties pp;
vkGetPhysicalDeviceProperties(vk_ctx()->phys, &pp);
snprintf(LW.id, sizeof LW.id, "%04x:%04x:%08x", pp.vendorID, pp.deviceID, pp.driverVersion);
LW.th[0] = plat_thread_new(lw_main, NULL, "lib warm check");
}
static void lw_stop(void) {
plat_xchg32(&LW.stop, 1);
for (int i = 0; i <= LW_MAX_THREADS; i++)
if (LW.th[i]) plat_thread_join(LW.th[i]), LW.th[i] = NULL;
}
// Boot: compute shaders of the warm list (recorded and prepared), then the first KEEP recorded graphics variants.
static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) {
const char *e = cfg_value("NX2NAVI_LIB_PREBUILD"), *ke = cfg_get("NX2NAVI_LIB_PREBUILD_KEEP");
const char *te = cfg_get("NX2NAVI_LIB_PREBUILD_THREADS"); // no fixed default: 2 when unset (1 under 4 CPUs)
const char *be = cfg_get("NX2NAVI_LIB_PREBUILD_BIG"); // SPIR-V words; LB_BIG_WORDS when unset, 0 no limit
if (!A.on || !cfg_bool("NX2NAVI_LIB_PREBUILD")) return;
lb_state(pr_hist, fs_hist); // the libraries' state, also for lw_start
lw_start();
LB.big_words = be && *be ? strtoull(be, NULL, 10) : LB_BIG_WORDS;
char dir[PLAT_PATH_MAX * 2];
if (cache_dir(dir, sizeof dir)) skip_init(dir);
@@ -4773,7 +4950,6 @@ static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) {
return;
}
LB.n = n;
lb_state(pr_hist, fs_hist);
LB.idle = 1;
int cpus = (int)plat_cpu_usable();
// Two threads by default, not half the CPUs. On a 32-thread CPU with NVIDIA's driver, 16 builders kept the driver's
@@ -4802,6 +4978,7 @@ static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) {
}
static void lb_stop(void) {
lw_stop();
plat_xchg32(&LB.stop, 1);
for (int i = 0; i < LB.threads; i++)
if (LB.th[i]) plat_thread_join(LB.th[i]), LB.th[i] = NULL;

View File

@@ -1178,8 +1178,9 @@ static uint64_t stale_after(const Tex *e) {
static void drop_stale(uint64_t keep, uint64_t *count) {
uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
int dropped = 0;
const uint64_t stale = STALE_FRAMES;
for (int any = 0; any < 2; any++)
for (Tex *e = rel_head, *next; e && now - e->rel_frame >= STALE_FRAMES; e = next) { // in release order
for (Tex *e = rel_head, *next; e && now - e->rel_frame >= stale; 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) && !e->overwritten)) continue;
@@ -1275,8 +1276,12 @@ static uint64_t evict_idle(uint64_t want, uint64_t used, uint64_t budget) { //
// marks missed. NX2NAVI_TEX_DISUSE=2 retires and never destroys (stand-ins only, nothing freed): it tells the marks and
// stand-ins from the freeing as the cause of a fault. Only while every module that indexes the pool was marked and two
// reads arrived: an unmarked shader could sample what the marks call unused.
#define DISUSE_FRAMES PRESENTS(1800)
#define DISUSE_SECONDS 30
// Two minutes, not the thirty seconds it was: a texture out of view that long and then seen again shows its stand-in
// for the frames until its mark is read, which is a flash of missing ground or a dark object each time the camera
// turns back, and a batch of such textures gave back 0.4 to 31 MiB in a recorded session. The textures of an area
// left behind, which are what the memory grows by, still go two minutes after it.
#define DISUSE_SECONDS 120
#define DISUSE_FRAMES PRESENTS(DISUSE_SECONDS * 60)
#define HOLDS_MAX 65536
static nvn_hmap holds = {.klen = sizeof(TexKey)}; // value: 1
static uint32_t marks_reads;
@@ -1353,10 +1358,10 @@ static uint64_t evict_disused(uint64_t want) {
want -= retiring_bytes;
Tex *v[EVICT_STEP];
int nv = 0;
uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), disuse = DISUSE_FRAMES;
for (uint32_t i = 0; i < by_key.cap; i++) { // the EVICT_STEP least recently sampled candidates, oldest first
Tex *e = by_key.vals[i];
if (!e || e->released || e->retiring || !e->bytes || !comes_back_intact(e) || now - e->samp_frame < DISUSE_FRAMES)
if (!e || e->released || e->retiring || !e->bytes || !comes_back_intact(e) || now - e->samp_frame < disuse)
continue;
int at = nv;
while (at > 0 && v[at - 1]->samp_frame > e->samp_frame) at--;