Merge branch 'claude/amd-fight-crash-ef03a9' into test-all-fixes

This commit is contained in:
chrg6
2026-11-10 15:22:07 -05:00
24 changed files with 745 additions and 37 deletions

View File

@@ -138,7 +138,9 @@ static const struct { const char *key, *section; } sections[] = {
#define LIVE 1
#define RESTART 0
// The lazy write-back (memory.c "Lazy write-back") is on where it has run: its fetch inside the access-fault handler
// has only ever run on Windows. With shadow storage it stays off whatever is set (vk_memory.c wb_lazy).
// has only ever run on Windows. This is the platform's part of the default, which memory.c mem_init goes by to take
// the access faults; the back end then turns it off for a driver it is not trusted on while the key is unset
// (wb_lazy_choice.h: AMD's own), and with shadow storage whatever is set (vk_memory.c vk_mem_lazy_decide).
#ifdef _WIN32
#define WB_LAZY_DEFAULT "1"
#else
@@ -287,9 +289,45 @@ static int is_known(const char *key) {
return !strncmp(key, "CHEAT_", 6); // cheats.c and the cheat patch sets (tools/cheats.py)
}
// The keys of the file's lines that are not settings of this version (add), lower-case, ", " between them, each once;
// what did not fit is counted and left out. Written by cfg_load, before any other thread reads settings.
static char unknown_keys[384];
static int n_unknown, unknown_cut; // how many; whether the list left some out
static void note_unknown(const char *key) {
char low[64];
size_t n = 0;
for (; key[n] && n < sizeof low - 1; n++) {
// a key of other characters (a line of text with an '=' in it) is counted, not listed: the menu cuts the list
// by bytes to fit its row
if (!isalnum((unsigned char)key[n]) && key[n] != '_') break;
low[n] = (char)tolower((unsigned char)key[n]);
}
if (key[n]) {
n_unknown++, unknown_cut = 1;
return;
}
low[n] = 0;
size_t have = strlen(unknown_keys);
for (const char *p = unknown_keys; (p = strstr(p, low)) != NULL; p += n) // a repeated line counts once
if ((p == unknown_keys || p[-1] == ' ') && (p[n] == ',' || !p[n])) return;
n_unknown++;
if (have + n + 3 > sizeof unknown_keys) {
unknown_cut = 1;
return;
}
snprintf(unknown_keys + have, sizeof unknown_keys - have, "%s%s", have ? ", " : "", low);
}
const char *cfg_unknown_keys(int *count) {
if (count) *count = n_unknown;
return unknown_keys;
}
static void clear(void) {
for (int i = 0; i < n_entries; i++) free(entries[i].key), free(entries[i].value);
n_entries = 0;
unknown_keys[0] = 0, n_unknown = unknown_cut = 0;
}
static char *trim(char *s) {
@@ -310,7 +348,10 @@ static void add(char *key, char *value, int line) {
size_t vl = strlen(value);
if (vl >= 2 && value[0] == '"' && value[vl - 1] == '"') value[vl - 1] = 0, value++;
if (!*key) return;
if (!is_known(key)) rt_log("cfg: nx2navi.ini line %d: unknown key '%s'", line, key);
if (!is_known(key)) {
rt_log("cfg: nx2navi.ini line %d: unknown key '%s'", line, key);
note_unknown(key);
}
if (!*value) return; // "key =" leaves the setting at its default
for (int i = 0; i < n_entries; i++)
if (!strcmp(entries[i].key, key)) { // a repeated key: the later line wins
@@ -920,6 +961,33 @@ int cfg_save(void) {
return 0;
}
// What the file sets, in the log once at start: the settings of this version with their values, in lines of up to
// about 900 characters, and then the keys that are no settings of this version, which do nothing. A report's log then
// shows which settings its run had and which of the file's lines had no effect; before, it held only the count of
// keys, and a player who had added a line this version does not read could take it for the cause of what changed.
static void log_file_settings(void) {
char line[1024];
size_t k = 0;
for (int i = 0; i < n_entries; i++) {
if (!entries[i].value || !is_known(entries[i].key)) continue;
char key[64];
snprintf(key, sizeof key, "%s", entries[i].key);
for (char *p = key; *p; p++) *p = (char)tolower((unsigned char)*p);
size_t vl = strlen(entries[i].value);
if (vl > 200) vl = 200; // a long value (a list of mods) is cut: the line shows that the key is set
if (k && k + strlen(key) + vl + 5 > 900) {
rt_log("cfg: nx2navi.ini sets: %s", line);
k = 0;
}
k += (size_t)snprintf(line + k, sizeof line - k, "%s%s = %.*s", k ? ", " : "", key, (int)vl, entries[i].value);
}
if (k) rt_log("cfg: nx2navi.ini sets: %s", line);
if (n_unknown)
rt_log("cfg: nx2navi.ini has %d key%s that %s no setting of this version and %s nothing: %s%s", n_unknown,
n_unknown == 1 ? "" : "s", n_unknown == 1 ? "is" : "are", n_unknown == 1 ? "does" : "do", unknown_keys,
unknown_cut ? ", ..." : "");
}
static void load_ini(void) {
if (!find_ini()) return;
if (!cfg_load(ini_path)) {
@@ -927,6 +995,7 @@ static void load_ini(void) {
return;
}
rt_log("cfg: read %s: %d keys", ini_path, n_entries);
log_file_settings();
for (int i = 0; i < n_entries; i++) {
char var[128];
snprintf(var, sizeof var, PREFIX "%s", entries[i].key);

View File

@@ -25,6 +25,10 @@ int cfg_setenv(const char *name, const char *value);
int cfg_from_env(const char *name);
// 1 when the player set name (the environment or nx2navi.ini), 0 when cfg_get gives a device profile's default or NULL.
int cfg_is_set(const char *name);
// The keys of the file's lines that are no settings of this version, lower-case with ", " between them ("" when there
// are none; a list too long to keep ends early), and how many there are. Such a line does nothing: the log says so at
// start and the settings menu shows it (ui/menu.cpp), so that nobody takes one for the cause of what they see.
const char *cfg_unknown_keys(int *count);
// The device profile in use, "deck" or "desktop" (NX2NAVI_DEVICE_PROFILE, else what the machine is).
const char *cfg_device_profile(void);
// Writes the keys cfg_set changed since the last save into nx2navi.ini next to nx2navi.exe (cfg_save_to: the file at path).

View File

@@ -71,6 +71,11 @@ int probe_cfg(void) {
CHECK(cfg_get("NX2NAVI_CLOTH_REALTIME") == NULL, "absent key is NULL");
CHECK(cfg_get("NX2NAVI_comment") == NULL, "comment is not a key");
CHECK(EQ(cfg_get("NX2NAVI_NO_SUCH_SETTING"), "1"), "unknown key is still readable");
{
int n = -1;
const char *u = cfg_unknown_keys(&n);
CHECK(n == 2 && EQ(u, "no_such_setting, last"), "the keys that are no settings: listed lower-case, in the file's order");
}
cfg_setenv("NX2NAVI_FPS60", "1");
CHECK(EQ(cfg_get("NX2NAVI_FPS60"), "1"), "environment overrides the file");
cfg_setenv("NX2NAVI_FPS60", NULL);
@@ -78,6 +83,80 @@ int probe_cfg(void) {
plat_file_delete(path);
CHECK(cfg_load(path) == 0, "missing file is not an error");
CHECK(cfg_get("NX2NAVI_FPS60") == NULL, "missing file leaves no keys");
{
int n = -1;
CHECK(EQ(cfg_unknown_keys(&n), "") && n == 0, "a missing file leaves no unknown keys either");
}
// A player's file as a report showed it: settings the menu wrote, and three lines added by hand that are no
// settings of this version (the player took the first for the fix of a crash). The three are named as such, a
// repeated one once, a key of other characters is counted without being listed, and the menu's save keeps every
// line it does not change byte for byte, the three included: only the changed, the reset and the new key move.
#define PLAYER_HEAD \
"[display]\n" \
"async_compute = 1\n" \
"fullscreen = 1\n" \
"window_size = 1920x1080\n" \
"gpu_index = 0\n" \
"perf_overlay = fps\n" \
"vk_present = mailbox\n" \
"upscaler = bilinear\n" \
"dlaa = 1\n" \
"framegen = off\n" \
"taa = fsr3\n" \
"hdr = 0\n" \
"aspect = fit\n" \
"game_res = 1800\n"
#define PLAYER_MID \
"aniso = 2\n" \
"assume_focus = 1\n" \
"texture_detail = auto\n" \
"\n" \
"[input]\n" \
"bind_a = key:default, pad:south\n" \
"bind_b = key:default, pad:east\n" \
"\n" \
"[language]\n" \
"operation_mode = docked\n" \
"\n" \
"[performance]\n" \
"wb_lazy = 0\n" \
"vram_safe_mode = 1\n" \
"exception_handler = 1\n" \
"ASYNC_COMPUTE = 1\n" \
"f\xC3\xBCr sp\xC3\xA4ter = 1\n" \
"\n" \
"[frame rate]\n"
static const char player[] = PLAYER_HEAD "sharpness = 80\n" PLAYER_MID "fps_limit = 120\n";
static const char player_saved[] = PLAYER_HEAD PLAYER_MID "fps_limit = 60\n\n[audio]\nvolume = 55\n";
#undef PLAYER_HEAD
#undef PLAYER_MID
cfg_setenv("NX2NAVI_FPS_LIMIT", NULL);
cfg_setenv("NX2NAVI_VOLUME", NULL);
cfg_setenv("NX2NAVI_SHARPNESS", NULL);
cfg_setenv("NX2NAVI_WB_LAZY", NULL);
CHECK(plat_file_write(path, player, sizeof player - 1), "write the player's ini");
CHECK(cfg_load(path) == 1, "load the player's ini");
{
int n = -1;
const char *u = cfg_unknown_keys(&n);
CHECK(n == 4 && EQ(u, "async_compute, vram_safe_mode, exception_handler"),
"the player's three lines are named as no settings; a repeat counts once, a key of other characters is only counted");
CHECK(EQ(cfg_get("NX2NAVI_WB_LAZY"), "0") && cfg_is_set("NX2NAVI_WB_LAZY"), "wb_lazy = 0 is a setting, and set");
CHECK(!cfg_is_set("NX2NAVI_VK_NAN_PRESERVE"), "a key the file lacks is not set");
static const char *const reset[] = {"SHARPNESS"};
cfg_set("NX2NAVI_FPS_LIMIT", "60");
cfg_set("NX2NAVI_VOLUME", "55");
CHECK(cfg_reset(reset, 1) == 1 && cfg_save_to(path) == 1, "the menu changes, adds and resets a key and saves");
uint64_t pn = 0;
uint8_t *p = plat_file_read(path, &pn);
CHECK(p && pn == sizeof player_saved - 1 && !memcmp(p, player_saved, pn),
"the save keeps every other line byte for byte, the lines that are no settings included");
if (p && (pn != sizeof player_saved - 1 || memcmp(p, player_saved, pn))) fprintf(stderr, "---- saved:\n%s\n----\n", p);
free(p);
}
plat_file_delete(path);
CHECK(cfg_load(path) == 0, "the player's ini is gone again");
// cfg_set / cfg_save: only changed lines move, comments stay, a missing key goes under its section
static const char before[] =

View File

@@ -41,6 +41,22 @@ static uint64_t g_table_bytes, g_functions;
static struct { uint64_t addr; GuestFn fn; } g_host[HOST_CAP];
static uint64_t g_nhost;
static void (*fatal_notes[4])(void);
static int n_fatal_notes;
void rt_fatal_note_add(void (*fn)(void)) {
for (int i = 0; i < n_fatal_notes; i++)
if (fatal_notes[i] == fn) return;
if (n_fatal_notes < (int)(sizeof fatal_notes / sizeof *fatal_notes)) fatal_notes[n_fatal_notes++] = fn;
}
// Once per process: a second fatal error, on another thread or inside a note, adds none.
void rt_fatal_notes(void) {
static volatile int32_t done;
if (plat_xchg32(&done, 1)) return;
for (int i = 0; i < n_fatal_notes; i++) fatal_notes[i]();
}
void rt_fatal(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
@@ -54,6 +70,7 @@ void rt_fatal(const char *fmt, ...) {
double proc, used, limit;
plat_commit_info(&proc, &used, &limit);
rt_log("fatal: memory commit: this process %.0f MB, the system %.0f of %.0f MB", proc, used, limit);
rt_fatal_notes();
kern_free_cores_note();
prof_sampler_write(); // NX2NAVI_PROFILE: what was sampled until now, else a crash loses the whole profile
// RivaTuner Statistics Server's profile as it was: left with the game's limit in it, the next run would take that

View File

@@ -51,3 +51,8 @@ RtDispatchStats rt_dispatch_stats(void);
// Runtime fatal error: message to stderr, then exit. Used by the dispatcher and the stubs.
void rt_fatal(const char *fmt, ...) __attribute__((noreturn, format(printf, 1, 2)));
// Lines of context for a fatal error's report, which rt_fatal and the crash report (main.c crash_handler) write after
// their own: fn logs one with rt_log. It runs on the failing thread, which may hold any lock, so it reads only what
// needs none. At most four; adding one twice keeps one. rt_fatal_notes runs them, once per process.
void rt_fatal_note_add(void (*fn)(void));
void rt_fatal_notes(void);

View File

@@ -196,6 +196,7 @@ int mem_lazy_armed(uint64_t a, uint64_t n);
void mem_lazy_window(int open);
void mem_lazy_release(uint64_t a, uint64_t n);
void mem_lazy_report(uint64_t frame, uint64_t skipped);
void mem_lazy_note(char *out, size_t n); // its state as a phrase, without a lock (a fatal error's line)
int mem_lazy_pick(uint64_t *addr, int max); // test: addresses of armed pages
int mem_probe(uint64_t a, uint64_t n); // the 2 MiB regions inside [a, a+n) fault on their next access
int mem_probing(void); // the probe is on

View File

@@ -311,6 +311,7 @@ static void crash_handler(const PlatCrash *x) {
} else {
rt_logf(" not on a guest thread\n");
}
rt_fatal_notes(); // the card, its driver and the memory settings in effect (vk_device.c fatal_note)
kern_free_cores_note();
rtss_limit_restore(); // after the report: RivaTuner Statistics Server's profile as it was
fflush(stderr);

View File

@@ -108,7 +108,8 @@ static void unprobe_locked(uint64_t r) {
plat_dec32(&nprb);
}
// Lazy write-back (NX2NAVI_WB_LAZY, default 1 on Windows and 0 elsewhere, vk_memory.c "lazy write-back"). It was off
// Lazy write-back (NX2NAVI_WB_LAZY; unset: on under Windows except on AMD's own driver, off elsewhere, as
// wb_lazy_choice.h decides and gives the reasons for; vk_memory.c "lazy write-back"). It was off
// by default in 0.7.0: with it on, single frames came out with the whole scene's shadowing missing, 10 to 180 of 1,800
// presents standing in Hyrule Field with other programs keeping the CPU busy and none found with it off. That was
// not this code's doing: frames were drawn from copies taken before the game's signal that their data was written
@@ -148,6 +149,12 @@ static uint64_t lz_unclean; // frz_lock: arms refused since the last
// frz_lock: since the last report: faults by a read, by a write, other releases, fetches, their bytes and time
static uint64_t lz_faults[2], lz_other, lz_fetches, lz_fetch_bytes, lz_fetch_ns, lz_gaps;
static volatile uint32_t lz_win_tid; // the replay thread's id while its part is between check and hand-over; 0 else
// For a fatal error's summary line (mem_lazy_note): written under frz_lock and read without it, since the thread that
// fails may be the one inside a fetch.
static volatile uint64_t lz_fetch_total; // fetches since the start
static volatile uint32_t lz_fetch_ms; // plat_now_ms() | 1 at the end of the last one; 0: none yet
static volatile uint32_t lz_fetch_tid; // the thread inside a fetch now; 0: none
static volatile uint64_t lz_fetch_now; // that fetch's bytes
static void lazy_flags_locked(uint64_t lo, uint64_t hi) {
for (uint64_t r = lo >> FRZ_SHIFT; r <= (hi - 1) >> FRZ_SHIFT; r++) {
@@ -165,7 +172,12 @@ static void lazy_drop_locked(int i, int why) {
LzSeg s = lzs[i];
if (s.stale && lz_fetch) {
uint64_t t0 = plat_now_ns();
__atomic_store_n(&lz_fetch_now, s.hi - s.lo, __ATOMIC_RELAXED);
__atomic_store_n(&lz_fetch_tid, plat_thread_id(), __ATOMIC_RELEASE);
lz_fetch(s.h0, s.h1, (uint64_t)((int64_t)s.lo + s.delta), s.hi - s.lo);
__atomic_store_n(&lz_fetch_ms, (uint32_t)plat_now_ms() | 1, __ATOMIC_RELAXED);
__atomic_store_n(&lz_fetch_total, lz_fetch_total + 1, __ATOMIC_RELAXED);
__atomic_store_n(&lz_fetch_tid, 0, __ATOMIC_RELEASE);
lz_fetches++, lz_fetch_bytes += s.hi - s.lo, lz_fetch_ns += plat_now_ns() - t0;
}
for (uint64_t a = s.lo, b; a < s.hi; a = b)
@@ -368,6 +380,24 @@ int mem_lazy_pick(uint64_t *addr, int max) {
return k;
}
// The lazy write-back's state as a phrase for a fatal error's summary line, "" while nothing is armed by it. No lock:
// the count of armed ranges may be a moment old, and a fetch in progress is named with its thread, which tells a game
// that ended inside a fetch (the device lost under it, or its wait run out) from one that ended elsewhere.
void mem_lazy_note(char *out, size_t n) {
*out = 0;
if (!__atomic_load_n(&lz_on, __ATOMIC_RELAXED)) return;
uint32_t tid = __atomic_load_n(&lz_fetch_tid, __ATOMIC_ACQUIRE), last = __atomic_load_n(&lz_fetch_ms, __ATOMIC_RELAXED);
int k = snprintf(out, n, "%d ranges armed, %llu fetches so far", lz_n,
(unsigned long long)__atomic_load_n(&lz_fetch_total, __ATOMIC_RELAXED));
if (k < 0 || (size_t)k >= n) return;
if (tid)
snprintf(out + k, n - (size_t)k, ", one of %llu bytes in progress on thread %u%s",
(unsigned long long)__atomic_load_n(&lz_fetch_now, __ATOMIC_RELAXED), tid,
tid == plat_thread_id() ? " (this one)" : "");
else if (last)
snprintf(out + k, n - (size_t)k, ", the last ended %u ms ago", (uint32_t)plat_now_ms() - last);
}
// One log line since the last one; skipped: the bytes the back end left in the mirror since then.
void mem_lazy_report(uint64_t frame, uint64_t skipped) {
if (!__atomic_load_n(&lz_on, __ATOMIC_RELAXED)) return;
@@ -640,7 +670,7 @@ void mem_init(void) {
if (cfg_bool("NX2NAVI_WB_PROBE")) { // a diagnosis: off, no access fault reaches probe_fault and nothing is probed
plat_mem_access_hook(access_fault);
__atomic_store_n(&prb_on, 1, __ATOMIC_RELEASE);
} else if (cfg_bool("NX2NAVI_WB_LAZY")) { // armed only once the back end can fetch (mem_lazy_enable)
} else if (cfg_bool("NX2NAVI_WB_LAZY")) { // armed once the back end wants it and can fetch (mem_lazy_enable)
plat_mem_access_hook(access_fault);
lz_want = 1;
}

View File

@@ -1531,7 +1531,26 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) {
// the save menu's brown buttons, with the game's cursor on the selected one (ui/menu_skin.cpp)
float gap = st.ItemSpacing.x + skin_cursor_reach(scale);
// right-aligned; in a window too narrow for all three, from the left edge
ImGui::SetCursorPos(ImVec2(fmaxf(st.WindowPadding.x, ImGui::GetWindowWidth() - st.WindowPadding.x - bw * 3 - gap * 2), y));
const float buttons_x = fmaxf(st.WindowPadding.x, ImGui::GetWindowWidth() - st.WindowPadding.x - bw * 3 - gap * 2);
// Lines of nx2navi.ini that are no settings of this version do nothing (cfg.c cfg_unknown_keys). Said here, in the
// save status's place while saving works, so that a line added by hand is not taken for the cause of what the game
// does: a player took such a line for the fix of a crash. The list is cut to the room left of the buttons.
const char *unknown = cfg_unknown_keys(NULL);
if (cfg_last_save_ok() && *unknown) {
char note[512];
snprintf(note, sizeof note, menu_text(UI_MENU_INI_UNKNOWN), unknown);
size_t keep = strlen(note), head = keep > strlen(unknown) ? keep - strlen(unknown) : keep;
const float room = buttons_x - st.WindowPadding.x - gap;
while (keep > head && ImGui::CalcTextSize(note).x > room) {
keep--;
snprintf(note + keep, sizeof note - keep, "...");
}
if (keep > head) {
ImGui::SetCursorPos(ImVec2(st.WindowPadding.x, y + st.FramePadding.y));
ImGui::TextColored(ImVec4(0.90f, 0.80f, 0.50f, 1), "%s", note);
}
}
ImGui::SetCursorPos(ImVec2(buttons_x, y));
if (skin_widget_button(cont, ImVec2(bw, 0), scale)) set_open(0);
ImGui::SameLine(0, gap);
if (skin_widget_button(restart, ImVec2(bw, 0), scale)) g_confirm_quit = false, ImGui::OpenPopup("##confirm");

View File

@@ -46,6 +46,7 @@ static const char *const text_en[UI_TEXT_COUNT] = {
[UI_MENU_ON] = "On", [UI_MENU_OFF] = "Off", [UI_MENU_AUTO] = "Automatic", [UI_MENU_DEFAULT_MARK] = "default",
[UI_MENU_AFTER_RESTART] = "after restart", [UI_MENU_SET_BY_ENV] = "set by environment variable",
[UI_MENU_SAVE_FAILED] = "could not save settings",
[UI_MENU_INI_UNKNOWN] = "nx2navi.ini: not settings of this version, ignored: %s",
[UI_MENU_VERSION] = "NX2navi %s, game %s",
[UI_MENU_FULLSCREEN] = "Fullscreen", [UI_MENU_FULLSCREEN_DESC] = "Play in fullscreen. F11 switches it.",
@@ -609,6 +610,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = {
[UI_MENU_DEFAULT_MARK] = "по умолчанию",
[UI_MENU_AFTER_RESTART] = "после перезапуска", [UI_MENU_SET_BY_ENV] = "задано переменной окружения",
[UI_MENU_SAVE_FAILED] = "не удалось сохранить настройки",
[UI_MENU_INI_UNKNOWN] = "nx2navi.ini: не настройки этой версии, пропущены: %s",
[UI_MENU_VERSION] = "NX2navi %s, игра %s",
[UI_MENU_FULLSCREEN] = "Полный экран", [UI_MENU_FULLSCREEN_DESC] = "Игра на весь экран. F11 переключает.",
@@ -1166,6 +1168,7 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = {
[UI_MENU_DEFAULT_MARK] = "padrão",
[UI_MENU_AFTER_RESTART] = "após reiniciar", [UI_MENU_SET_BY_ENV] = "definido por variável de ambiente",
[UI_MENU_SAVE_FAILED] = "não foi possível salvar as configurações",
[UI_MENU_INI_UNKNOWN] = "nx2navi.ini: não são configurações desta versão, ignoradas: %s",
[UI_MENU_VERSION] = "NX2navi %s, jogo %s",
[UI_MENU_FULLSCREEN] = "Tela cheia", [UI_MENU_FULLSCREEN_DESC] = "Jogar em tela cheia. F11 alterna.",
@@ -1720,6 +1723,7 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = {
[UI_MENU_AFTER_RESTART] = "重启后生效",
[UI_MENU_SET_BY_ENV] = "由环境变量设定",
[UI_MENU_SAVE_FAILED] = "无法保存设置",
[UI_MENU_INI_UNKNOWN] = "nx2navi.ini:以下不是此版本的设置,已忽略:%s",
[UI_MENU_VERSION] = "NX2navi %s,游戏 %s",
[UI_MENU_FULLSCREEN] = "全屏",
[UI_MENU_FULLSCREEN_DESC] = "全屏运行。按 F11 切换。",

View File

@@ -22,6 +22,7 @@ typedef enum {
UI_MENU_AFTER_RESTART, // note on a setting that is read at start
UI_MENU_SET_BY_ENV, // note on a setting an environment variable fixes
UI_MENU_SAVE_FAILED,
UI_MENU_INI_UNKNOWN, // "...: %s": the keys of nx2navi.ini that are no settings of this version (cfg_unknown_keys)
UI_MENU_VERSION, // "NX2navi %s, game %s": the port version and the game update, beside the title
// Screen

View File

@@ -63,9 +63,12 @@ typedef struct {
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
uint32_t driver_id; // the device's VkDriverId
} VkCaps;
extern VkCaps vk_caps;
void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, amd_llpc, mesa
void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, amd_llpc, mesa, driver_id
// The card and its driver in one phrase, for a fatal error's summary line ("" before vk_caps_init). Any thread.
const char *vk_caps_summary(void);
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.
@@ -337,6 +340,8 @@ int vk_mem_mirror_pending(void);
void vk_mem_mirror_prologue(VkCommandBuffer cb);
void vk_mem_lazy_part_end(void); // lazy write-back: a part ends (before its prologue), and it is handed over
void vk_mem_lazy_handed(void);
void vk_mem_lazy_decide(void); // once the driver is known (after vk_caps_driver): on or off, with one log line
void vk_mem_fatal_note(char *out, size_t n); // the memory mode and the lazy write-back's state, for a fatal error's line
// Shadow storage (vk_memory.c): vk_sync.c calls vk_mem_shadow_sync right before each submit; the replay notes every GPU
// write into CPU-visible memory at record time (vk_mem_gpu_pending) and reports it at the fence with vk_gpu_written,
// which copies the bytes back into guest memory before telling the texture cache (vk_guest_written).
@@ -514,6 +519,7 @@ VkSampler vk_sampler_compare_by_id_locked(int id);
// Texture detail (vk_texture.c): what texture_detail = auto picks on this device, 0 full, 1 reduced, 2 low (the budget is
// read at the first call). For the menu's row.
int vk_texture_detail_auto(void);
const char *vk_texture_detail_name(void); // the detail in effect: full, reduced or low; a fatal error's line
// The slot's sampler for a texture made without its top `skip` mip levels (VkTexImage.skip): its LOD clamp counts the
// game's levels, so it moves down by skip. The slot's own sampler when its clamp is open or skip is 0.
VkSampler vk_sampler_detail_by_id_locked(int id, uint32_t skip);

View File

@@ -24,6 +24,9 @@
VkCaps vk_caps = {.float64 = 1, .sg_size = 32};
static int f64_supported;
static VkBool32 gpl_lib, gpl_fast; // the driver's graphicsPipelineLibrary (feature), ...FastLinking (property)
static char summary[832]; // vk_caps_summary: written once, in vk_caps_init, before any other thread reads it
const char *vk_caps_summary(void) { return summary; }
static uint32_t env_u32(const char *name, uint32_t def) {
const char *e = cfg_get(name);
@@ -41,6 +44,7 @@ void vk_caps_driver(VkPhysicalDevice p) {
VkPhysicalDeviceProperties2 p2 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &drv};
vkGetPhysicalDeviceProperties2(p, &p2);
vk_caps.amd = p2.properties.vendorID == 0x1002;
vk_caps.driver_id = (uint32_t)drv.driverID;
// 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");
@@ -132,6 +136,8 @@ void vk_caps_init(VkCtx *c) {
char dv[32];
driver_version(dv, sizeof dv, c->props.vendorID, c->props.driverVersion);
snprintf(summary, sizeof summary, "%s, driver %s \"%s\" version %s", c->props.deviceName, drv.driverName,
drv.driverInfo, dv);
uint32_t v = c->props.apiVersion;
VkPhysicalDevice p = c->phys;
rt_log("vk caps: %s, vendor %#06x device %#06x, driver %s \"%s\" (id %d, version %s, raw %#x), api %u.%u.%u",

View File

@@ -304,6 +304,15 @@ typedef struct {
VkPhysicalDeviceAddressBindingReportFeaturesEXT abr;
} FeatureChain;
// One line for a fatal error's report (dispatch.h rt_fatal_note_add): the card, its driver, and the settings in effect
// that change how the game's memory behaves. A report often arrives as the end of a log alone, or as a picture of it,
// and the lines that say these things are at its start. No lock is taken: the failing thread may hold any.
static void fatal_note(void) {
char mem[512];
vk_mem_fatal_note(mem, sizeof mem);
rt_log("fatal: gpu %s; %s; texture detail %s", vk_caps_summary(), mem, vk_texture_detail_name());
}
static int create_device(void) {
uint32_t ne = 0;
vkEnumerateDeviceExtensionProperties(g_vk.phys, NULL, &ne, NULL);
@@ -510,6 +519,8 @@ static int create_device(void) {
vkGetPhysicalDeviceProperties2(g_vk.phys, &p2);
g_vk.min_imported_host_alignment = (uint32_t)emh.minImportedHostPointerAlignment;
vk_caps_init(&g_vk);
vk_mem_lazy_decide(); // the driver is known: the lazy write-back's default depends on it
rt_fatal_note_add(fatal_note);
VmaAllocatorCreateInfo ai = {.physicalDevice = g_vk.phys, .device = g_vk.device, .instance = g_vk.instance,
.vulkanApiVersion = VK_API_VERSION_1_3,
@@ -679,6 +690,7 @@ int probe_vkdevice(void) {
c->has_conditional_rendering, c->has_transform_feedback, c->has_provoking_vertex, c->has_robustness2,
c->has_device_fault, c->has_memory_budget, c->has_buffer_device_address, c->has_fragment_shader_interlock,
c->has_buffer_marker, c->has_nv_checkpoints, c->validation);
fatal_note(); // the line a fatal error or a crash would add to the log, to read it without one
vk_shutdown();
printf("probe-vkdevice: %s (validation errors %ld)\n", g_errors_total ? "FAILED" : "passed", g_errors_total);
return g_errors_total ? 1 : 0;

View File

@@ -21,6 +21,7 @@
#include "vk_dlaa.h"
#include "vk_exec.h"
#include "vk_gpa.h"
#include "wb_lazy_choice.h"
#include "wb_merge_spv.h"
#include "platform/platform.h"
@@ -2536,7 +2537,7 @@ static void snap_free(void *arg) { // completion thread: the submission is don
free(f);
}
// ---- lazy write-back (NX2NAVI_WB_LAZY, default 1 on Windows; memory.c "Lazy write-back")
// ---- lazy write-back (NX2NAVI_WB_LAZY; the default by platform and driver, wb_lazy_choice.h; memory.c "Lazy write-back")
// A range a shader wrote goes back to guest memory once and is then armed (vk_gpu_written_back, mem_lazy_arm). While it
// stays armed, gpu_wrote leaves the later writes of its pages in the mirror: no snapshot, no merge, no copy. When the
// CPU touches a page, memory.c calls lazy_fetch with the mirror and the imported buffer of the range: a copy of its
@@ -2605,15 +2606,33 @@ static int lazy_clean(uint64_t lo, uint64_t hi, void *ctx) {
return mem_written_peek(lo, hi - lo, p, 1) == 0; // UINT64_MAX (not watched) counts as written
}
// Whether the lazy write-back is wanted on this device, decided and logged once, when the driver is known
// (vk_device.c create_device, after vk_caps_init): the player's setting, else the default of the platform and driver
// (wb_lazy_choice.h). Never in import mode, where no pool has a mirror; with shadow storage, where a fetch would copy
// the mirror into the pool's buffer, which is guest memory only when the pool is imported (a shadow's bytes reach
// guest memory in the submission's shadow_wb_cb); or with the write-back probe, which watches the same pages.
_Static_assert(WBL_DRIVER_AMD == VK_DRIVER_ID_AMD_PROPRIETARY, "wb_lazy_choice.h names AMD's own driver");
static int lz_wanted = -1; // -1 not decided, 0 off, 1 on
static const char *lz_why = ""; // the reason the log gave
void vk_mem_lazy_decide(void) {
const char *set = cfg_is_set("NX2NAVI_WB_LAZY") ? cfg_get("NX2NAVI_WB_LAZY") : NULL;
WblChoice ch = wb_lazy_choice(set, WBL_ON_WINDOWS, vk_caps.driver_id);
if (!vk_mirror_on) ch = (WblChoice){0, "no pool has a mirror in import mode"};
else if (shadow_on) ch = (WblChoice){0, "shadow storage keeps it off"};
else if (ch.on && cfg_bool("NX2NAVI_WB_PROBE")) ch = (WblChoice){0, "the write-back probe watches the same pages"};
lz_why = ch.why;
__atomic_store_n(&lz_wanted, ch.on, __ATOMIC_RELEASE);
rt_log("mem: lazy write-back %s: %s", ch.on ? "on" : "off", ch.why);
}
static int wb_lazy(void) {
int s = __atomic_load_n(&lz_state, __ATOMIC_ACQUIRE);
if (s) return s == 1;
plat_mutex_lock(&lz_init_lock);
if (!lz_state) {
VkCtx *c = vk_ctx();
// Not with shadow storage: a fetch copies the mirror into the pool's buffer, which is guest memory only when
// the pool is imported; a shadow's bytes reach guest memory in the submission's shadow_wb_cb.
int on = vk_mirror_on && !shadow_on && c && cfg_bool("NX2NAVI_WB_LAZY");
int on = c && __atomic_load_n(&lz_wanted, __ATOMIC_ACQUIRE) == 1;
if (on) {
VkCommandPoolCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.queueFamilyIndex = c->gfx_family};
@@ -2625,10 +2644,10 @@ static int wb_lazy(void) {
vkCreateFence(c->device, &fi, NULL, &lz_fence) == VK_SUCCESS;
on = on && mem_lazy_enable(lazy_fetch, lazy_clean);
}
rt_log("mem: what shaders write into mirrored pools %s (NX2NAVI_WB_LAZY%s)",
rt_log("mem: what shaders write into mirrored pools %s (lazy write-back: %s)",
on ? "goes back to guest memory once and then only when the CPU touches it"
: "goes back to guest memory at the end of every submission",
shadow_on ? "; shadow storage keeps it off" : "");
on || lz_wanted != 1 ? lz_why : "its command buffer and fence could not be made");
__atomic_store_n(&lz_state, on ? 1 : 2, __ATOMIC_RELEASE);
}
plat_mutex_unlock(&lz_init_lock);
@@ -3296,6 +3315,17 @@ void vk_mem_lost_report(void) {
exact ? "the driver's figures, every program counted in the budget" : "this game's own allocations only");
}
// The settings that change how the game's memory reaches the GPU, as they are in effect, for a fatal error's summary
// line (vk_device.c fatal_note): the memory mode and the lazy write-back with its reason and state. No lock, any thread.
void vk_mem_fatal_note(char *out, size_t n) {
char lazy[160];
mem_lazy_note(lazy, sizeof lazy);
int st = __atomic_load_n(&lz_state, __ATOMIC_ACQUIRE), want = __atomic_load_n(&lz_wanted, __ATOMIC_ACQUIRE);
snprintf(out, n, "memory mode %s%s, lazy write-back %s (%s%s%s)", mirror_verify ? "verify" : vk_mirror_on ? "mirror" : "import",
shadow_on ? " with shadow storage" : "", want < 0 ? "not decided" : st == 2 && want ? "failed to start" : want ? "on" : "off",
lz_why, *lazy ? "; " : "", lazy);
}
float vk_mem_device_used_mb(void) {
VkCtx *c = vk_ctx();
if (!c || !c->has_memory_budget) return -1;

View File

@@ -872,8 +872,16 @@ int vk_texture_detail_auto(void) {
return most;
}
static const char *const detail_names[3] = {"full", "reduced", "low"};
static int detail_chosen = -1; // detail_most's answer once it is made
// The texture detail in effect, for a fatal error's summary line. No lock, any thread.
const char *vk_texture_detail_name(void) {
int most = __atomic_load_n(&detail_chosen, __ATOMIC_RELAXED);
return most < 0 ? "not chosen yet (no texture made)" : detail_names[most];
}
static int detail_most(void) { // the most levels a texture is made without: 0 full, 1 reduced, 2 low
static const char *const names[3] = {"full", "reduced", "low"};
static int most = -1;
if (most >= 0) return most;
const char *v = cfg_value("NX2NAVI_TEXTURE_DETAIL");
@@ -886,10 +894,11 @@ static int detail_most(void) { // the most levels a texture is made without: 0
rt_log("vk: texture detail: full (texture_detail = %s; the device reports no video memory budget)", v);
else
rt_log("vk: texture detail: %s, chosen for a video memory budget of %lld MiB (texture_detail = %s; full from "
"%d MiB, low under %d MiB%s)", names[most], (long long)detail_budget_mb, v, DETAIL_FULL_MB, DETAIL_LOW_MB,
"%d MiB, low under %d MiB%s)", detail_names[most],(long long)detail_budget_mb, v, DETAIL_FULL_MB, DETAIL_LOW_MB,
vk_astc_mode() != VK_ASTC_NATIVE && !vk_astc_bc() && detail_budget_mb < DETAIL_RGBA8_MB
? "; one step lower because ASTC textures stay uncompressed here" : "");
}
__atomic_store_n(&detail_chosen, most, __ATOMIC_RELAXED);
return most;
}

56
runtime/wb_lazy_choice.h Normal file
View File

@@ -0,0 +1,56 @@
// Whether the lazy write-back runs (memory.c "Lazy write-back", vk_memory.c vk_mem_lazy_decide), from the player's
// setting, the platform and the GPU driver, with the reason the log gives. Own header so tests/wb_lazy_check.c checks
// it without Vulkan.
//
// The lazy write-back makes pages of guest memory inaccessible while a host-pointer import of them is bound to a buffer
// the GPU writes, and copies their bytes down from inside the access-fault handler of whichever thread touched them. A
// driver is free to handle both differently, so the default is on only where it has been played:
// - Windows, AMD's own driver: off. In the days after 0.7.1 turned it on for every card on Windows, two reports
// from AMD cards had the game end in play. An RX 6600 lost its device 3.5 minutes in with it on (146 fetches in
// the 1,200 frames before; the GPU's last mark was inside a draw pass, not a fetch). A player whose game ended in
// fights, on a card they did not name, then played for hours with a settings file that turns it off. Neither
// proves it the cause: the player changed several settings at once, cards with 8 GB lost their device the same
// way on NVIDIA before it existed, and no failure path was found in the code. But fights are where it is busiest
// (shaders write, the CPU reads back, every read faults and fetches), and nobody has played one on an AMD card
// under our eyes: builds with the default on were measured on an RX 7900 XT (driver 26.10.1) standing in Hyrule
// Field and walking through a village (vk_exec_xfer.c oq_take, vk_exec_draw.c), which says nothing about a fight,
// and none of us has a card of the driver branch for RDNA 1 and 2. Since the second report's card is unknown, the
// default is off on both of AMD's branches until a card of each has played fights with it on and a log to show.
// The cost is the frame rate in open areas, back to what it was before 0.7.1 (about 65 fps against 90 standing in
// Hyrule Field on an RTX 5090; not measured on AMD).
// - Windows, any other driver: on, as 0.7.1 shipped it. Measured and played on NVIDIA's; no report from Intel's.
// - elsewhere: off, as before. The fetch inside the fault handler has only run on Windows.
// A value the player sets wins everywhere: wb_lazy = 1 turns it on for an AMD card, wb_lazy = 0 off for any.
#pragma once
#include <stdint.h>
#include <string.h>
#define WBL_DRIVER_AMD 1 // VkDriverId of AMD's own driver, VK_DRIVER_ID_AMD_PROPRIETARY (vk_memory.c asserts the value)
#ifdef _WIN32
#define WBL_ON_WINDOWS 1
#else
#define WBL_ON_WINDOWS 0
#endif
typedef struct {
int on;
const char *why; // the end of the log line "mem: lazy write-back on|off: "
} WblChoice;
// set: the value the player gave NX2NAVI_WB_LAZY (the environment or nx2navi.ini), NULL or "" when they gave none.
// windows: the game runs on Windows (WBL_ON_WINDOWS). driver_id: the device's VkDriverId.
static inline WblChoice wb_lazy_choice(const char *set, int windows, uint32_t driver_id) {
if (set && *set) {
if (!strcmp(set, "0")) return (WblChoice){0, "wb_lazy = 0 is set"};
return (WblChoice){1, "wb_lazy is set to on"};
}
if (!windows)
return (WblChoice){0, "the default outside Windows, where its fetch inside the access-fault handler has not run "
"(wb_lazy = 1 turns it on)"};
if (driver_id == WBL_DRIVER_AMD)
return (WblChoice){0, "the default on AMD's driver, where reports had the game end in play with it on and the "
"cause is not found; what shaders write goes back at the end of every submission, which "
"costs frame rate in open areas (wb_lazy = 1 turns it on)"};
return (WblChoice){1, "the default on Windows (wb_lazy = 0 turns it off)"};
}