From caf50ecf1c6639974cb452fa66102730b6a9c7ff Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 15:14:25 -0500 Subject: [PATCH] Turn the lazy write-back off by default on AMD's driver for RX 5000 and 6000 cards, and say in the log which settings a run had. A player on an AMD card reported crashes in fights and that a line added to nx2navi.ini cured them. That line, async_compute = 1, is no setting of the game, and neither are two more in the same file; the one line of the file that changes how memory reaches the GPU is wb_lazy = 0. A second report, an RX 6600 on 0.7.1, lost its device 3.5 minutes into play with the lazy write-back on. Neither proves it the cause: the player changed several lines at once and sent no log, the RX 6600's last pass mark is inside a draw pass and not a fetch, cards with 8 GB lost their device the same way on NVIDIA before the lazy write-back existed, and reading the fault handler, the fetch, the arming and the buffers' lifetimes found no failure path. What depends on the driver and cannot be checked without the card: pages of guest memory are inaccessible while a host-pointer import of them is bound and written by the GPU, and Vulkan calls run inside the access-fault handler. The default is now decided once the driver is known (wb_lazy_choice.h, vk_memory.c vk_mem_lazy_decide): on under Windows as before, except on AMD's driver branch for RDNA 1 and 2, which none of us has run it on; AMD's driver for RDNA 3 and newer keeps it on, since builds with that default were measured on an RX 7900 XT. What the player sets wins everywhere, and one line at start says the choice and its reason. On those cards open areas run at the 0.7.0 frame rate again (on an RTX 5090 that was 65 fps against 90 in Hyrule Field; not measured on AMD). tests/wb_lazy_check.c checks the choice by platform, driver and setting without a GPU. For reports of this kind the log now holds what was missing. At start: every setting nx2navi.ini sets, and in one line the keys that are no settings of this version and do nothing; the settings menu shows those in its bottom row. At a fatal error or a crash: one line with the card, its driver, the memory mode, the lazy write-back with its reason, armed ranges, fetches so far and a fetch in progress, and the texture detail (dispatch.c rt_fatal_note_add, vk_device.c fatal_note), since a report is often the end of a log alone. --probe-cfg loads the player's file: the three lines are named, and the menu's save keeps them and every other line it does not change byte for byte. Nothing here ran on an AMD card, and no fatal error was provoked to read the new line. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 15 +++++-- runtime/cfg.c | 74 ++++++++++++++++++++++++++++++++- runtime/cfg.h | 4 ++ runtime/cfg_probe.c | 79 +++++++++++++++++++++++++++++++++++ runtime/dispatch.c | 17 ++++++++ runtime/dispatch.h | 5 +++ runtime/host.h | 1 + runtime/main.c | 1 + runtime/memory.c | 34 ++++++++++++++- runtime/ui/menu.cpp | 21 +++++++++- runtime/ui_text.c | 4 ++ runtime/ui_text.h | 1 + runtime/vk.h | 9 +++- runtime/vk_caps.c | 7 ++++ runtime/vk_device.c | 11 +++++ runtime/vk_memory.c | 42 ++++++++++++++++--- runtime/vk_texture.c | 13 +++++- runtime/wb_lazy_choice.h | 63 ++++++++++++++++++++++++++++ tests/wb_lazy_check.c | 90 ++++++++++++++++++++++++++++++++++++++++ tests/wb_lazy_check.py | 31 ++++++++++++++ 20 files changed, 504 insertions(+), 18 deletions(-) create mode 100644 runtime/wb_lazy_choice.h create mode 100644 tests/wb_lazy_check.c create mode 100644 tests/wb_lazy_check.py diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 891b1845..de5459e9 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -2,6 +2,10 @@ # Each line shows the default; on a Steam Deck the device profile ([display]) changes a few of them. A NX2NAVI_* # environment variable of the same name (NX2NAVI_FPS_LIMIT for fps_limit) overrides the file. Keys are case-insensitive; # '#' and ';' start a comment; [sections] only group lines. +# Only the keys in this file are settings. A line with any other key does nothing: the game's log names it at the +# start ("no setting of this version") and the settings menu (F1) shows it in its bottom row. +# Updates and rebuilds of the game never rewrite nx2navi.ini; the settings menu changes only the lines of the settings +# it changes and keeps every other line as it is. [frame rate] # Frame rate limit: 30, 40, 45, 60, 90, 120, 144, any whole rate from 30 to 240, display (the refresh rate of the @@ -729,10 +733,13 @@ # first submission ends when that submission's passes are through, without waiting for the copy back (3 to 4 ms of # every frame). 0: the copy back is part of the submission, and the wait covers it. # wb_split = 1 -# 1 (the default on Windows): what the game's shaders write into its memory is copied back from the video card once, -# and after that only when the game's CPU code touches it (most of it never is: 66 MiB a frame in Hyrule Field), for -# about a third more frames a second in open areas. 0: copied back at the end of every GPU submission, as in 0.7.0. -# With shadow storage (Linux, Wine) it stays off. +# 1: what the game's shaders write into its memory is copied back from the video card once, and after that only when +# the game's CPU code touches it (most of it never is: 66 MiB a frame in Hyrule Field), for about a third more frames +# a second in open areas. 0: copied back at the end of every GPU submission, as in 0.7.0. +# Default: 1 on Windows, except 0 with AMD's driver for RX 5000 and RX 6000 cards, where it has not been tried by us +# and one report lost its device with it on: set 1 there to try it, and back to 0 if the game ends in play (the log's +# "mem: lazy write-back" line says which is in effect and why). 0 elsewhere. With shadow storage (Linux, Wine) it +# stays off whatever is set. # wb_lazy = 1 # Barriers between the game's draw passes and compute dispatches. 1: only where the game itself asks for one, or where # the work changes between drawing and compute; this takes about a tenth off the graphics card's time for a frame. diff --git a/runtime/cfg.c b/runtime/cfg.c index c2a1d3c7..3815ae6a 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -138,7 +138,10 @@ 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 for RX 5000 and 6000 cards), and with shadow storage whatever is set (vk_memory.c +// vk_mem_lazy_decide). #ifdef _WIN32 #define WB_LAZY_DEFAULT "1" #else @@ -286,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) { @@ -309,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 @@ -919,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)) { @@ -926,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); diff --git a/runtime/cfg.h b/runtime/cfg.h index 62dc21ed..29753a53 100644 --- a/runtime/cfg.h +++ b/runtime/cfg.h @@ -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). diff --git a/runtime/cfg_probe.c b/runtime/cfg_probe.c index a10cd1d5..efe5e75e 100644 --- a/runtime/cfg_probe.c +++ b/runtime/cfg_probe.c @@ -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[] = diff --git a/runtime/dispatch.c b/runtime/dispatch.c index 75d28e01..76afb92b 100644 --- a/runtime/dispatch.c +++ b/runtime/dispatch.c @@ -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 diff --git a/runtime/dispatch.h b/runtime/dispatch.h index a46e66e3..519d6ea8 100644 --- a/runtime/dispatch.h +++ b/runtime/dispatch.h @@ -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); diff --git a/runtime/host.h b/runtime/host.h index e8af6dde..f2f4fb65 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -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 diff --git a/runtime/main.c b/runtime/main.c index 0462cedd..3a599f67 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -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); diff --git a/runtime/memory.c b/runtime/memory.c index c8bff935..2fd19de2 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -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 driver for RX 5000 and 6000 cards, 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; } diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 957d9f3c..9e7fd90b 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -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"); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index faaa02d3..61905516 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -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 切换。", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index d22313b1..7d3262b0 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -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 diff --git a/runtime/vk.h b/runtime/vk.h index 5e05bbf9..a6bc9b92 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -63,9 +63,13 @@ 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 + char driver_info[256]; // and its driverInfo (VK_MAX_DRIVER_INFO_SIZE) } 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 +341,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 +520,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); diff --git a/runtime/vk_caps.c b/runtime/vk_caps.c index c91041c3..7d9b62de 100644 --- a/runtime/vk_caps.c +++ b/runtime/vk_caps.c @@ -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,8 @@ 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; + snprintf(vk_caps.driver_info, sizeof vk_caps.driver_info, "%s", drv.driverInfo); // 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 +137,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", diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 485fcaef..980f8670 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -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, diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index e730abea..d6964751 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -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, vk_caps.driver_info); + 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; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 75d577c0..c72b452e 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -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; } diff --git a/runtime/wb_lazy_choice.h b/runtime/wb_lazy_choice.h new file mode 100644 index 00000000..cf2e5fd9 --- /dev/null +++ b/runtime/wb_lazy_choice.h @@ -0,0 +1,63 @@ +// 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 run: +// - Windows, NVIDIA's driver: on. Measured and played there. +// - Windows, AMD's driver for RDNA 3 and newer: on. Builds with this default 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 and vk_exec_draw.c +// have numbers from that card). Whether fights were played on it is not recorded. +// - Windows, AMD's driver branch for RDNA 1 and 2 (RX 5000 and 6000; its driverInfo reads "AMD proprietary shader +// compiler", as vk_caps.c amd_sc tells it): off. Nobody has run it there under our eyes, and the one log of it +// there is an RX 6600 that lost its device 3.5 minutes into play (146 fetches in the 1,200 frames before; the +// GPU's last mark was inside a draw pass, not a fetch). That log does not prove the lazy write-back the cause: +// no failure path was found in the code, and cards with 8 GB lost their device the same way on NVIDIA before it +// existed. A second report, of a game that ended in fights on an AMD card of unknown model and ran for hours with +// a settings file that turns it off, changed several settings at once. Until one of these cards has run it with +// a log to compare, what shaders write goes back at the end of every submission there, as before 0.7.1, at that +// cost (about 90 fps against 65 standing in Hyrule Field on an RTX 5090; not measured on these cards). +// - Windows, any other driver: on, as 0.7.1 shipped it. No report from one. +// - 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 RX 6000 card, wb_lazy = 0 off for any. +#pragma once + +#include +#include + +#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; + +// AMD's driver branch for RDNA 1 and 2: its own driver whose driverInfo names the older shader compiler. +static inline int wbl_amd_rdna12(uint32_t driver_id, const char *driver_info) { + return driver_id == WBL_DRIVER_AMD && driver_info && strstr(driver_info, "shader compiler") != NULL; +} + +// 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, driver_info: the device's VkDriverId and the +// driverInfo of its VkPhysicalDeviceDriverProperties. +static inline WblChoice wb_lazy_choice(const char *set, int windows, uint32_t driver_id, const char *driver_info) { + 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 (wbl_amd_rdna12(driver_id, driver_info)) + return (WblChoice){0, "the default on AMD's driver for RX 5000 and 6000 cards, where it has not been tried by " + "us and a report had the game lose its device with it on; 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)"}; +} diff --git a/tests/wb_lazy_check.c b/tests/wb_lazy_check.c new file mode 100644 index 00000000..d36f3a15 --- /dev/null +++ b/tests/wb_lazy_check.c @@ -0,0 +1,90 @@ +// runtime/wb_lazy_choice.h: whether the lazy write-back runs, by the player's setting, the platform and the GPU driver +// (runtime/vk_memory.c vk_mem_lazy_decide). Built and run by tests/wb_lazy_check.py; prints each failed check and +// exits 1 on any. +#include +#include + +#include "wb_lazy_choice.h" + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +// VkDriverId values (vulkan_core.h) +enum { AMD_PROPRIETARY = 1, AMD_OPEN_SOURCE = 2, MESA_RADV = 3, NVIDIA_PROPRIETARY = 4, INTEL_WINDOWS = 5, MESA_NVK = 24 }; +enum { LINUX = 0, WINDOWS = 1 }; + +// driverInfo as the drivers report it: AMD's branch for RDNA 1 and 2 (an RX 6600's log), its driver for RDNA 3 and +// newer, NVIDIA's, Mesa's. +static const char RDNA12[] = "26.10.2 (AMD proprietary shader compiler)"; +static const char RDNA34[] = "26.10.1 (LLPC)"; +static const char NVIDIA[] = "617.42"; +static const char MESA[] = "Mesa 26.0.1"; + +typedef struct { unsigned id; const char *info; const char *name; } Driver; +static const Driver drivers[] = { + {AMD_PROPRIETARY, RDNA12, "AMD RDNA 1/2"}, {AMD_PROPRIETARY, RDNA34, "AMD RDNA 3+"}, {AMD_OPEN_SOURCE, "", "AMD open"}, + {MESA_RADV, MESA, "RADV"}, {NVIDIA_PROPRIETARY, NVIDIA, "NVIDIA"}, {INTEL_WINDOWS, "", "Intel"}, {MESA_NVK, MESA, "NVK"}, + {0, NULL, "unknown"}, +}; +enum { ND = sizeof drivers / sizeof *drivers }; + +static int has(const char *s, const char *part) { return s && strstr(s, part) != NULL; } + +int main(void) { + // Nothing set, Windows: on, except on AMD's driver branch for RDNA 1 and 2. + WblChoice nv = wb_lazy_choice(NULL, WINDOWS, NVIDIA_PROPRIETARY, NVIDIA); + CHECK(nv.on == 1, "Windows, NVIDIA's driver, nothing set: on"); + CHECK(has(nv.why, "default") && has(nv.why, "wb_lazy = 0"), "it says it is the default and how to turn it off: %s", nv.why); + CHECK(wb_lazy_choice(NULL, WINDOWS, AMD_PROPRIETARY, RDNA34).on == 1, "Windows, AMD's driver for RDNA 3 and newer: on"); + CHECK(wb_lazy_choice(NULL, WINDOWS, INTEL_WINDOWS, "").on == 1, "Windows, Intel's driver, nothing set: on"); + CHECK(wb_lazy_choice(NULL, WINDOWS, 0, NULL).on == 1, "Windows, a driver that reports nothing: on, as 0.7.1 had it"); + WblChoice amd = wb_lazy_choice(NULL, WINDOWS, AMD_PROPRIETARY, RDNA12); + CHECK(amd.on == 0, "Windows, AMD's driver for RDNA 1 and 2, nothing set: off"); + CHECK(has(amd.why, "AMD") && has(amd.why, "RX 5000 and 6000") && has(amd.why, "wb_lazy = 1"), + "it names the driver's cards and how to turn it on: %s", amd.why); + CHECK(has(amd.why, "frame rate"), "it says what off costs: %s", amd.why); + CHECK(has(amd.why, "default"), "it says it is a default: %s", amd.why); + + // The branch is told by the driver and its driverInfo together, as vk_caps.c tells it: another driver whose + // driverInfo happened to hold the words is not AMD's. + CHECK(wbl_amd_rdna12(AMD_PROPRIETARY, RDNA12) && !wbl_amd_rdna12(AMD_PROPRIETARY, RDNA34), "the branch by driverInfo"); + CHECK(!wbl_amd_rdna12(AMD_PROPRIETARY, NULL) && !wbl_amd_rdna12(AMD_PROPRIETARY, ""), "no driverInfo: not the branch"); + CHECK(!wbl_amd_rdna12(NVIDIA_PROPRIETARY, RDNA12) && wb_lazy_choice(NULL, WINDOWS, MESA_RADV, RDNA12).on == 1, + "the words alone, under another driver: not the branch"); + + // An empty value is no value (cfg.c add: "key =" leaves the setting at its default). + CHECK(wb_lazy_choice("", WINDOWS, AMD_PROPRIETARY, RDNA12).on == 0, "an empty value on the RDNA 1/2 branch: the default, off"); + CHECK(wb_lazy_choice("", WINDOWS, NVIDIA_PROPRIETARY, NVIDIA).on == 1, "an empty value on NVIDIA: the default, on"); + + // Nothing set, elsewhere: off on every driver. + for (int i = 0; i < ND; i++) { + WblChoice c = wb_lazy_choice(NULL, LINUX, drivers[i].id, drivers[i].info); + CHECK(c.on == 0 && has(c.why, "wb_lazy = 1"), "Linux, %s, nothing set: off, and how to turn it on: %s", + drivers[i].name, c.why); + } + + // What the player sets wins on every platform and driver, and the reason says it was set. + for (int os = LINUX; os <= WINDOWS; os++) + for (int i = 0; i < ND; i++) { + WblChoice on = wb_lazy_choice("1", os, drivers[i].id, drivers[i].info); + WblChoice off = wb_lazy_choice("0", os, drivers[i].id, drivers[i].info); + CHECK(on.on == 1 && has(on.why, "set"), "os %d, %s, wb_lazy = 1: on (%s)", os, drivers[i].name, on.why); + CHECK(off.on == 0 && has(off.why, "set"), "os %d, %s, wb_lazy = 0: off (%s)", os, drivers[i].name, off.why); + CHECK(!has(on.why, "default") && !has(off.why, "default"), "os %d, %s: a set value is not a default", os, + drivers[i].name); + } + // Any value but 0 counts as on, as cfg_bool reads it for memory.c mem_init. + CHECK(wb_lazy_choice("on", WINDOWS, AMD_PROPRIETARY, RDNA12).on == 1, "wb_lazy = on: on"); + + CHECK(WBL_DRIVER_AMD == AMD_PROPRIETARY, "WBL_DRIVER_AMD is VK_DRIVER_ID_AMD_PROPRIETARY"); + + // The game passes the platform it was built for. +#ifdef _WIN32 + CHECK(WBL_ON_WINDOWS == 1, "a Windows build says so"); +#else + CHECK(WBL_ON_WINDOWS == 0, "a build elsewhere says so"); +#endif + + if (!fails) printf("wb lazy choice: all checks passed\n"); + return fails != 0; +} diff --git a/tests/wb_lazy_check.py b/tests/wb_lazy_check.py new file mode 100644 index 00000000..b066d7ae --- /dev/null +++ b/tests/wb_lazy_check.py @@ -0,0 +1,31 @@ +"""Build and run tests/wb_lazy_check.c: runtime/wb_lazy_choice.h, whether the lazy write-back runs, by the player's +setting, the platform and the GPU driver. + +Usage: wb_lazy_check.py +""" +import os +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests' / 'diff')) +from common import MSYS_BIN, compiler # noqa: E402 + + +def main(): + work = ROOT / 'build' / 'test' / 'wb_lazy' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('wb_lazy_check.exe' if os.name == 'nt' else 'wb_lazy_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + # every platform's and driver's choice is checked through wb_lazy_choice's arguments; WBL_ON_WINDOWS on the one + # built for + subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'wb_lazy_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main())