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

@@ -352,6 +352,16 @@ A change to what the library warm logs when the driver's cache of stage librarie
`runtime/vk_pipeline.c` `lw_main`) passes `python tests\warm_hint_check.py`, which builds the header alone and checks `runtime/vk_pipeline.c` `lw_main`) passes `python tests\warm_hint_check.py`, which builds the header alone and checks
the added text by GPU vendor and by why the cache was cold. No game files. the added text by GPU vendor and by why the cache was cold. No game files.
A change to where the lazy write-back is on by default (`runtime/wb_lazy_choice.h`, `runtime/vk_memory.c`
`vk_mem_lazy_decide`) passes `python tests\wb_lazy_check.py`, which builds the header alone and checks the choice and
its reason by platform, by GPU driver and by what the player set. No game files, no GPU.
A change to what writes `nx2navi.ini` outside the game (`launcher/gameini.py`, `installer/ini.py`, `tools/make_game.py`,
`launcher/updater.py`) passes the settings-file tests of `tests\launcher_test.py` (`PlayerIniTest`, and the tests named
`..._settings_file` of `BackgroundUpdateTest` and `MakeGameTest`): a player's file with lines that are no settings of
the game comes through an update and a rebuild byte for byte. The game's own writer, the settings menu, is checked by
`--probe-cfg` with the same lines.
A change to the mouse camera's stick deflection (`runtime/input_map.c` `input_mouse_stick`) passes A change to the mouse camera's stick deflection (`runtime/input_map.c` `input_mouse_stick`) passes
`python tests\mouse_stick_check.py`, which builds that file with the rest of the runtime stubbed and checks the `python tests\mouse_stick_check.py`, which builds that file with the rest of the runtime stubbed and checks the
deflection at each camera speed: a slow movement is divided by the factor, a fast flick reaches full tilt. No game deflection at each camera speed: a slow movement is divided by the factor, a fast flick reaches full tilt. No game

View File

@@ -33,3 +33,25 @@ Draft notes for the release after 0.7.1. The version number is not decided yet.
shader compiler"); with it each NaN test is now made on the bits of the number instead, which no compiler can drop. shader compiler"); with it each NaN test is now made on the bits of the number instead, which no compiler can drop.
It is on by default only with that driver (`vk_nan_bits`); the first start after the update compiles the pipelines It is on by default only with that driver (`vk_nan_bits`); the first start after the update compiles the pipelines
again. Untested on those cards before this release: please report whether the blocks are gone. again. Untested on those cards before this release: please report whether the blocks are gone.
- The older installer's last step no longer writes `nx2navi.ini` again as a whole to set the language: it changes the
`lang` line and keeps every other byte (it turned Windows line ends into Unix ones and dropped a byte order mark).
## Changed
- **AMD cards: the faster copy-back of 0.7.1 is off by default again.** 0.7.1 turned it on for every card on Windows
(what the game's shaders write is copied back from the video card once, and then only when the game's CPU code
touches it). Since then two reports from AMD cards had the game end in play: an RX 6600 lost its video device a
few minutes in with it on, and a player whose game ended in fights played for hours with it off. We found no fault
in the code and cannot say it was the cause, and none of us has played a fight on an AMD card with it on. Until
someone has, the old copy-back at the end of every GPU submission is the default with AMD's Windows driver; open
areas run slower for it, as they did in 0.7.0. The RX 7900 XT figures under *Faster* come from builds that had it
on by default. `wb_lazy = 1` in `nx2navi.ini` turns it on: if you try it, play a few fights and tell us whether the
game stays up, with the log. NVIDIA cards keep it on. The log says which is in effect and why ("mem: lazy
write-back ...").
- **A line in `nx2navi.ini` that is no setting of this version is pointed out.** Such a line does nothing; the game
only wrote "unknown key" into its log. The log now lists them in one line at the start, beside a line with every
setting the file does set, and the settings menu shows them in its bottom row. An update or a rebuild never rewrites
`nx2navi.ini`, and the menu keeps such lines when it saves, so nothing has to be added again after an update.
- When the game ends with a fatal error or a crash, the end of the log names the video card, its driver, the memory
mode, whether the lazy write-back was on (with its armed ranges, its fetches so far and one in progress) and the
texture detail, so a report that holds only the last lines of a log still says them.

View File

@@ -3,7 +3,8 @@
A new file starts from nx2navi.ini.example, whose lines are all commented defaults. The language goes into `lang` A new file starts from nx2navi.ini.example, whose lines are all commented defaults. The language goes into `lang`
(a Switch language code, runtime/ov_system.c): an active `lang = ...` line is rewritten in place, else the commented (a Switch language code, runtime/ov_system.c): an active `lang = ...` line is rewritten in place, else the commented
`# lang = ...` line of the example is activated, else the line is added to [language] (created at the end if `# lang = ...` line of the example is activated, else the line is added to [language] (created at the end if
missing). Other lines, comments and line order stay as they are. missing). Other lines, comments and line order stay as they are, byte for byte: the file's byte order mark, its line
ends and a last line without one are kept, so a player's own lines come through an install or an update unchanged.
""" """
import os import os
import re import re
@@ -21,6 +22,7 @@ NEED_SHARED_FONTS = ('ko', 'zh-Hans', 'zh-Hant')
ACTIVE = re.compile(r'^(\s*)(?:nx2navi_)?lang(\s*=\s*)(.*?)(\s*)$', re.IGNORECASE) ACTIVE = re.compile(r'^(\s*)(?:nx2navi_)?lang(\s*=\s*)(.*?)(\s*)$', re.IGNORECASE)
COMMENTED = re.compile(r'^\s*[#;]\s*(?:nx2navi_)?lang\s*=.*$', re.IGNORECASE) COMMENTED = re.compile(r'^\s*[#;]\s*(?:nx2navi_)?lang\s*=.*$', re.IGNORECASE)
SECTION = re.compile(r'^\s*\[(.*)\]\s*$') SECTION = re.compile(r'^\s*\[(.*)\]\s*$')
BOM = ''
def canonical_language(lang): def canonical_language(lang):
@@ -29,27 +31,43 @@ def canonical_language(lang):
def set_language(text, lang): def set_language(text, lang):
"""text with `lang = <lang>` (an alias written as its canonical code); see the module docstring.""" """text with `lang = <lang>` (an alias written as its canonical code); see the module docstring. Only that line
changes: a byte order mark, each line's own ending (LF or CRLF) and a last line without one stay as they are, and
a line that is added ends as the file's first line does."""
lang = canonical_language(lang) lang = canonical_language(lang)
lines = text.splitlines() bom = BOM if text.startswith(BOM) else ''
lines = text[len(bom):].splitlines(keepends=True)
nl = '\r\n' if lines and lines[0].endswith('\r\n') else '\n'
def split(line):
body = line.rstrip('\r\n')
return body, line[len(body):]
for i, line in enumerate(lines): for i, line in enumerate(lines):
m = ACTIVE.match(line) body, end = split(line)
m = ACTIVE.match(body)
if m: if m:
lines[i] = f'{m.group(1)}lang{m.group(2)}{lang}' lines[i] = f'{m.group(1)}lang{m.group(2)}{lang}{end}'
return '\n'.join(lines) + '\n' return bom + ''.join(lines)
for i, line in enumerate(lines): for i, line in enumerate(lines):
if COMMENTED.match(line): body, end = split(line)
lines[i] = f'lang = {lang}' if COMMENTED.match(body):
return '\n'.join(lines) + '\n' lines[i] = f'lang = {lang}{end}'
return bom + ''.join(lines)
for i, line in enumerate(lines): for i, line in enumerate(lines):
m = SECTION.match(line) body, end = split(line)
m = SECTION.match(body)
if m and m.group(1).strip().lower() == 'language': if m and m.group(1).strip().lower() == 'language':
lines.insert(i + 1, f'lang = {lang}') if not end: # the header is the file's last line, without an ending
return '\n'.join(lines) + '\n' lines[i] = body + nl
lines.insert(i + 1, f'lang = {lang}{nl}')
return bom + ''.join(lines)
if lines and not split(lines[-1])[1]:
lines[-1] += nl
if lines and lines[-1].strip(): if lines and lines[-1].strip():
lines.append('') lines.append(nl)
lines += ['[language]', f'lang = {lang}'] lines += [f'[language]{nl}', f'lang = {lang}{nl}']
return '\n'.join(lines) + '\n' return bom + ''.join(lines)
def get_language(text): def get_language(text):
@@ -60,19 +78,33 @@ def get_language(text):
return None return None
def read(path):
"""The file's text with its byte order mark and its own line ends. Bytes that are not UTF-8 come back as they are
(surrogate escapes), so a line in another encoding survives a rewrite. A file saved as UTF-16 (an editor's
"Unicode", which the game reads too: runtime/cfg.c utf16_text) is decoded as that and written back as UTF-8, as
the game's own menu writes it."""
raw = Path(path).read_bytes()
if raw[:2] in (b'\xff\xfe', b'\xfe\xff'):
return raw.decode('utf-16', errors='replace')
return raw.decode('utf-8', errors='surrogateescape')
def write(dest, lang, example=None): def write(dest, lang, example=None):
"""Write dest/nx2navi.ini with lang; an existing file keeps its other lines. Returns the path.""" """Write dest/nx2navi.ini with lang; an existing file keeps every other line byte for byte (set_language).
Returns the path."""
path = Path(dest) / 'nx2navi.ini' path = Path(dest) / 'nx2navi.ini'
old = None
if path.is_file(): if path.is_file():
text = path.read_text(encoding='utf-8-sig') text = old = read(path)
elif example and Path(example).is_file(): elif example and Path(example).is_file():
text = Path(example).read_text(encoding='utf-8') text = Path(example).read_text(encoding='utf-8')
else: else:
text = '' text = ''
new = set_language(text, lang) new = set_language(text, lang)
if path.is_file() and new == text: if new == old:
return path return path
tmp = path.with_name('nx2navi.ini.tmp') tmp = path.with_name('nx2navi.ini.tmp')
tmp.write_text(new, encoding='utf-8', newline='\n') with open(tmp, 'w', encoding='utf-8', errors='surrogateescape', newline='') as f:
f.write(new)
os.replace(tmp, path) os.replace(tmp, path)
return path return path

View File

@@ -2,6 +2,10 @@
# Each line shows the default; on a Steam Deck the device profile ([display]) changes a few of them. A NX2NAVI_* # 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; # 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. # '#' 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]
# Frame rate limit: 30, 40, 45, 60, 90, 120, 144, any whole rate from 30 to 240, display (the refresh rate of the # 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 # 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. # every frame). 0: the copy back is part of the submission, and the wait covers it.
# wb_split = 1 # 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, # 1: what the game's shaders write into its memory is copied back from the video card once, and after that only when
# 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 # 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
# 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. # 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. # Default: 1 on Windows, except 0 on AMD graphics cards (AMD's own driver), where two reports had the game end in play
# with it on and the cause is not found: set 1 there to try it, and back to 0 if the game ends in a fight (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 # wb_lazy = 1
# Barriers between the game's draw passes and compute dispatches. 1: only where the game itself asks for one, or where # 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. # the work changes between drawing and compute; this takes about a tenth off the graphics card's time for a frame.

View File

@@ -138,7 +138,9 @@ static const struct { const char *key, *section; } sections[] = {
#define LIVE 1 #define LIVE 1
#define RESTART 0 #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 // 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 #ifdef _WIN32
#define WB_LAZY_DEFAULT "1" #define WB_LAZY_DEFAULT "1"
#else #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) 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) { static void clear(void) {
for (int i = 0; i < n_entries; i++) free(entries[i].key), free(entries[i].value); for (int i = 0; i < n_entries; i++) free(entries[i].key), free(entries[i].value);
n_entries = 0; n_entries = 0;
unknown_keys[0] = 0, n_unknown = unknown_cut = 0;
} }
static char *trim(char *s) { static char *trim(char *s) {
@@ -310,7 +348,10 @@ static void add(char *key, char *value, int line) {
size_t vl = strlen(value); size_t vl = strlen(value);
if (vl >= 2 && value[0] == '"' && value[vl - 1] == '"') value[vl - 1] = 0, value++; if (vl >= 2 && value[0] == '"' && value[vl - 1] == '"') value[vl - 1] = 0, value++;
if (!*key) return; 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 if (!*value) return; // "key =" leaves the setting at its default
for (int i = 0; i < n_entries; i++) for (int i = 0; i < n_entries; i++)
if (!strcmp(entries[i].key, key)) { // a repeated key: the later line wins if (!strcmp(entries[i].key, key)) { // a repeated key: the later line wins
@@ -920,6 +961,33 @@ int cfg_save(void) {
return 0; 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) { static void load_ini(void) {
if (!find_ini()) return; if (!find_ini()) return;
if (!cfg_load(ini_path)) { if (!cfg_load(ini_path)) {
@@ -927,6 +995,7 @@ static void load_ini(void) {
return; return;
} }
rt_log("cfg: read %s: %d keys", ini_path, n_entries); rt_log("cfg: read %s: %d keys", ini_path, n_entries);
log_file_settings();
for (int i = 0; i < n_entries; i++) { for (int i = 0; i < n_entries; i++) {
char var[128]; char var[128];
snprintf(var, sizeof var, PREFIX "%s", entries[i].key); 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); 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. // 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); 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). // The device profile in use, "deck" or "desktop" (NX2NAVI_DEVICE_PROFILE, else what the machine is).
const char *cfg_device_profile(void); 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). // 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_CLOTH_REALTIME") == NULL, "absent key is NULL");
CHECK(cfg_get("NX2NAVI_comment") == NULL, "comment is not a key"); 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"); 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"); cfg_setenv("NX2NAVI_FPS60", "1");
CHECK(EQ(cfg_get("NX2NAVI_FPS60"), "1"), "environment overrides the file"); CHECK(EQ(cfg_get("NX2NAVI_FPS60"), "1"), "environment overrides the file");
cfg_setenv("NX2NAVI_FPS60", NULL); cfg_setenv("NX2NAVI_FPS60", NULL);
@@ -78,6 +83,80 @@ int probe_cfg(void) {
plat_file_delete(path); plat_file_delete(path);
CHECK(cfg_load(path) == 0, "missing file is not an error"); CHECK(cfg_load(path) == 0, "missing file is not an error");
CHECK(cfg_get("NX2NAVI_FPS60") == NULL, "missing file leaves no keys"); 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 // cfg_set / cfg_save: only changed lines move, comments stay, a missing key goes under its section
static const char before[] = 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 struct { uint64_t addr; GuestFn fn; } g_host[HOST_CAP];
static uint64_t g_nhost; 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, ...) { void rt_fatal(const char *fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
@@ -54,6 +70,7 @@ void rt_fatal(const char *fmt, ...) {
double proc, used, limit; double proc, used, limit;
plat_commit_info(&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_log("fatal: memory commit: this process %.0f MB, the system %.0f of %.0f MB", proc, used, limit);
rt_fatal_notes();
kern_free_cores_note(); kern_free_cores_note();
prof_sampler_write(); // NX2NAVI_PROFILE: what was sampled until now, else a crash loses the whole profile 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 // 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. // 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))); 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_window(int open);
void mem_lazy_release(uint64_t a, uint64_t n); void mem_lazy_release(uint64_t a, uint64_t n);
void mem_lazy_report(uint64_t frame, uint64_t skipped); 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_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_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 int mem_probing(void); // the probe is on

View File

@@ -311,6 +311,7 @@ static void crash_handler(const PlatCrash *x) {
} else { } else {
rt_logf(" not on a guest thread\n"); 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(); kern_free_cores_note();
rtss_limit_restore(); // after the report: RivaTuner Statistics Server's profile as it was rtss_limit_restore(); // after the report: RivaTuner Statistics Server's profile as it was
fflush(stderr); fflush(stderr);

View File

@@ -108,7 +108,8 @@ static void unprobe_locked(uint64_t r) {
plat_dec32(&nprb); 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 // 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 // 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 // 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 // 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 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 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) { static void lazy_flags_locked(uint64_t lo, uint64_t hi) {
for (uint64_t r = lo >> FRZ_SHIFT; r <= (hi - 1) >> FRZ_SHIFT; r++) { 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]; LzSeg s = lzs[i];
if (s.stale && lz_fetch) { if (s.stale && lz_fetch) {
uint64_t t0 = plat_now_ns(); 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); 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; 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) 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; 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. // 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) { void mem_lazy_report(uint64_t frame, uint64_t skipped) {
if (!__atomic_load_n(&lz_on, __ATOMIC_RELAXED)) return; 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 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); plat_mem_access_hook(access_fault);
__atomic_store_n(&prb_on, 1, __ATOMIC_RELEASE); __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); plat_mem_access_hook(access_fault);
lz_want = 1; 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) // 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); float gap = st.ItemSpacing.x + skin_cursor_reach(scale);
// right-aligned; in a window too narrow for all three, from the left edge // 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); if (skin_widget_button(cont, ImVec2(bw, 0), scale)) set_open(0);
ImGui::SameLine(0, gap); ImGui::SameLine(0, gap);
if (skin_widget_button(restart, ImVec2(bw, 0), scale)) g_confirm_quit = false, ImGui::OpenPopup("##confirm"); 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_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_AFTER_RESTART] = "after restart", [UI_MENU_SET_BY_ENV] = "set by environment variable",
[UI_MENU_SAVE_FAILED] = "could not save settings", [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_VERSION] = "NX2navi %s, game %s",
[UI_MENU_FULLSCREEN] = "Fullscreen", [UI_MENU_FULLSCREEN_DESC] = "Play in fullscreen. F11 switches it.", [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_DEFAULT_MARK] = "по умолчанию",
[UI_MENU_AFTER_RESTART] = "после перезапуска", [UI_MENU_SET_BY_ENV] = "задано переменной окружения", [UI_MENU_AFTER_RESTART] = "после перезапуска", [UI_MENU_SET_BY_ENV] = "задано переменной окружения",
[UI_MENU_SAVE_FAILED] = "не удалось сохранить настройки", [UI_MENU_SAVE_FAILED] = "не удалось сохранить настройки",
[UI_MENU_INI_UNKNOWN] = "nx2navi.ini: не настройки этой версии, пропущены: %s",
[UI_MENU_VERSION] = "NX2navi %s, игра %s", [UI_MENU_VERSION] = "NX2navi %s, игра %s",
[UI_MENU_FULLSCREEN] = "Полный экран", [UI_MENU_FULLSCREEN_DESC] = "Игра на весь экран. F11 переключает.", [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_DEFAULT_MARK] = "padrão",
[UI_MENU_AFTER_RESTART] = "após reiniciar", [UI_MENU_SET_BY_ENV] = "definido por variável de ambiente", [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_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_VERSION] = "NX2navi %s, jogo %s",
[UI_MENU_FULLSCREEN] = "Tela cheia", [UI_MENU_FULLSCREEN_DESC] = "Jogar em tela cheia. F11 alterna.", [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_AFTER_RESTART] = "重启后生效",
[UI_MENU_SET_BY_ENV] = "由环境变量设定", [UI_MENU_SET_BY_ENV] = "由环境变量设定",
[UI_MENU_SAVE_FAILED] = "无法保存设置", [UI_MENU_SAVE_FAILED] = "无法保存设置",
[UI_MENU_INI_UNKNOWN] = "nx2navi.ini:以下不是此版本的设置,已忽略:%s",
[UI_MENU_VERSION] = "NX2navi %s,游戏 %s", [UI_MENU_VERSION] = "NX2navi %s,游戏 %s",
[UI_MENU_FULLSCREEN] = "全屏", [UI_MENU_FULLSCREEN] = "全屏",
[UI_MENU_FULLSCREEN_DESC] = "全屏运行。按 F11 切换。", [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_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_SET_BY_ENV, // note on a setting an environment variable fixes
UI_MENU_SAVE_FAILED, 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 UI_MENU_VERSION, // "NX2navi %s, game %s": the port version and the game update, beside the title
// Screen // 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_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 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 int mesa; // a Mesa driver the game runs on: RADV, ANV or NVK
uint32_t driver_id; // the device's VkDriverId
} VkCaps; } VkCaps;
extern VkCaps vk_caps; 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) 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, // 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. // 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_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_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_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 // 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, // 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). // 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 // 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. // read at the first call). For the menu's row.
int vk_texture_detail_auto(void); 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 // 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. // 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); 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}; VkCaps vk_caps = {.float64 = 1, .sg_size = 32};
static int f64_supported; static int f64_supported;
static VkBool32 gpl_lib, gpl_fast; // the driver's graphicsPipelineLibrary (feature), ...FastLinking (property) 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) { static uint32_t env_u32(const char *name, uint32_t def) {
const char *e = cfg_get(name); 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}; VkPhysicalDeviceProperties2 p2 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &drv};
vkGetPhysicalDeviceProperties2(p, &p2); vkGetPhysicalDeviceProperties2(p, &p2);
vk_caps.amd = p2.properties.vendorID == 0x1002; 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 // 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. // "26.10.1 (AMD proprietary shader compiler)" where the RDNA 3 and 4 driver names LLPC.
vk_caps.amd_sc = drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY && strstr(drv.driverInfo, "shader compiler"); vk_caps.amd_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]; char dv[32];
driver_version(dv, sizeof dv, c->props.vendorID, c->props.driverVersion); 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; uint32_t v = c->props.apiVersion;
VkPhysicalDevice p = c->phys; 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", 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; VkPhysicalDeviceAddressBindingReportFeaturesEXT abr;
} FeatureChain; } 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) { static int create_device(void) {
uint32_t ne = 0; uint32_t ne = 0;
vkEnumerateDeviceExtensionProperties(g_vk.phys, NULL, &ne, NULL); vkEnumerateDeviceExtensionProperties(g_vk.phys, NULL, &ne, NULL);
@@ -510,6 +519,8 @@ static int create_device(void) {
vkGetPhysicalDeviceProperties2(g_vk.phys, &p2); vkGetPhysicalDeviceProperties2(g_vk.phys, &p2);
g_vk.min_imported_host_alignment = (uint32_t)emh.minImportedHostPointerAlignment; g_vk.min_imported_host_alignment = (uint32_t)emh.minImportedHostPointerAlignment;
vk_caps_init(&g_vk); 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, VmaAllocatorCreateInfo ai = {.physicalDevice = g_vk.phys, .device = g_vk.device, .instance = g_vk.instance,
.vulkanApiVersion = VK_API_VERSION_1_3, .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_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_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); 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(); vk_shutdown();
printf("probe-vkdevice: %s (validation errors %ld)\n", g_errors_total ? "FAILED" : "passed", g_errors_total); printf("probe-vkdevice: %s (validation errors %ld)\n", g_errors_total ? "FAILED" : "passed", g_errors_total);
return g_errors_total ? 1 : 0; return g_errors_total ? 1 : 0;

View File

@@ -21,6 +21,7 @@
#include "vk_dlaa.h" #include "vk_dlaa.h"
#include "vk_exec.h" #include "vk_exec.h"
#include "vk_gpa.h" #include "vk_gpa.h"
#include "wb_lazy_choice.h"
#include "wb_merge_spv.h" #include "wb_merge_spv.h"
#include "platform/platform.h" #include "platform/platform.h"
@@ -2536,7 +2537,7 @@ static void snap_free(void *arg) { // completion thread: the submission is don
free(f); 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 // 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 // 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 // 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 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) { static int wb_lazy(void) {
int s = __atomic_load_n(&lz_state, __ATOMIC_ACQUIRE); int s = __atomic_load_n(&lz_state, __ATOMIC_ACQUIRE);
if (s) return s == 1; if (s) return s == 1;
plat_mutex_lock(&lz_init_lock); plat_mutex_lock(&lz_init_lock);
if (!lz_state) { if (!lz_state) {
VkCtx *c = vk_ctx(); VkCtx *c = vk_ctx();
// Not with shadow storage: a fetch copies the mirror into the pool's buffer, which is guest memory only when int on = c && __atomic_load_n(&lz_wanted, __ATOMIC_ACQUIRE) == 1;
// 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");
if (on) { if (on) {
VkCommandPoolCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, VkCommandPoolCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.queueFamilyIndex = c->gfx_family}; .queueFamilyIndex = c->gfx_family};
@@ -2625,10 +2644,10 @@ static int wb_lazy(void) {
vkCreateFence(c->device, &fi, NULL, &lz_fence) == VK_SUCCESS; vkCreateFence(c->device, &fi, NULL, &lz_fence) == VK_SUCCESS;
on = on && mem_lazy_enable(lazy_fetch, lazy_clean); 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" 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", : "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); __atomic_store_n(&lz_state, on ? 1 : 2, __ATOMIC_RELEASE);
} }
plat_mutex_unlock(&lz_init_lock); 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"); 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) { float vk_mem_device_used_mb(void) {
VkCtx *c = vk_ctx(); VkCtx *c = vk_ctx();
if (!c || !c->has_memory_budget) return -1; if (!c || !c->has_memory_budget) return -1;

View File

@@ -872,8 +872,16 @@ int vk_texture_detail_auto(void) {
return most; 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 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; static int most = -1;
if (most >= 0) return most; if (most >= 0) return most;
const char *v = cfg_value("NX2NAVI_TEXTURE_DETAIL"); 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); rt_log("vk: texture detail: full (texture_detail = %s; the device reports no video memory budget)", v);
else else
rt_log("vk: texture detail: %s, chosen for a video memory budget of %lld MiB (texture_detail = %s; full from " 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 vk_astc_mode() != VK_ASTC_NATIVE && !vk_astc_bc() && detail_budget_mb < DETAIL_RGBA8_MB
? "; one step lower because ASTC textures stay uncompressed here" : ""); ? "; one step lower because ASTC textures stay uncompressed here" : "");
} }
__atomic_store_n(&detail_chosen, most, __ATOMIC_RELAXED);
return most; 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)"};
}

View File

@@ -262,6 +262,50 @@ class VersionTest(unittest.TestCase):
versions.parse('banana') versions.parse('banana')
# A player's settings file as a report showed it (an AMD card, 0.7.1): what the game's menu wrote, three lines added by
# hand that are no settings of the game (async_compute, vram_safe_mode, exception_handler: the player took the first for
# the fix of a crash and asked for it as a default, because "the configuration file gets overwritten on updates"), and a
# comment. Saved as an editor on Windows saves it: CRLF line ends, no line end after the last line. Nothing in an
# update may change a byte of it (PlayerIniTest, BackgroundUpdateTest and MakeGameTest each run their part of one).
PLAYER_INI = (
b'# my settings\r\n'
b'[display]\r\n'
b'async_compute = 1\r\n'
b'fullscreen = 1\r\n'
b'window_size = 1920x1080\r\n'
b'gpu_index = 0\r\n'
b'perf_overlay = fps\r\n'
b'vk_present = mailbox\r\n'
b'upscaler = bilinear\r\n'
b'dlaa = 1\r\n'
b'framegen = off\r\n'
b'taa = fsr3\r\n'
b'hdr = 0\r\n'
b'aspect = fit\r\n'
b'game_res = 1800\r\n'
b'sharpness = 80\r\n'
b'aniso = 2\r\n'
b'assume_focus = 1\r\n'
b'texture_detail = auto\r\n'
b'\r\n'
b'[input]\r\n'
b'bind_a = key:default, pad:south\r\n'
b'bind_b = key:default, pad:east\r\n'
b'bind_x = key:default, pad:north\r\n'
b'bind_y = key:default, pad:west\r\n'
b'\r\n'
b'[language]\r\n'
b'operation_mode = docked\r\n'
b'\r\n'
b'[performance]\r\n'
b'wb_lazy = 0\r\n'
b'vram_safe_mode = 1\r\n'
b'exception_handler = 1\r\n'
b'\r\n'
b'[frame rate]\r\n'
b'fps_limit = 120')
class GameIniTest(Tmp): class GameIniTest(Tmp):
"""The game settings the launcher switches (launcher/gameini.py), read as runtime/cfg.c reads nx2navi.ini.""" """The game settings the launcher switches (launcher/gameini.py), read as runtime/cfg.c reads nx2navi.ini."""
@@ -305,6 +349,45 @@ class GameIniTest(Tmp):
gameini.put(self.tmp, 'discord', 'discord_rpc', '1') gameini.put(self.tmp, 'discord', 'discord_rpc', '1')
self.assertTrue((self.tmp / 'nx2navi.ini').is_dir()) self.assertTrue((self.tmp / 'nx2navi.ini').is_dir())
class PlayerIniTest(Tmp):
"""The settings file of a player (PLAYER_INI) under the two writers outside the game: the launcher's Settings
(launcher/gameini.py) and the older installer's last step (installer/ini.py). Each changes the one line it is
for; the lines that are no settings of the game, the comment and every other byte stay."""
def test_the_launchers_setting_adds_one_line(self):
ini = self.tmp / 'nx2navi.ini'
ini.write_bytes(PLAYER_INI)
self.assertEqual(gameini.get(self.tmp, 'wb_lazy'), '0')
self.assertEqual(gameini.get(self.tmp, 'async_compute'), '1') # read like any line; the game ignores it
gameini.put(self.tmp, 'discord', 'discord_rpc', '0')
self.assertEqual(ini.read_bytes(), PLAYER_INI + b'\r\n\r\n[discord]\r\ndiscord_rpc = 0\r\n')
gameini.put(self.tmp, 'discord', 'discord_rpc', '1')
self.assertEqual(ini.read_bytes(), PLAYER_INI + b'\r\n\r\n[discord]\r\ndiscord_rpc = 1\r\n')
def test_the_older_installer_changes_the_language_line_only(self):
"""installer/ini.py sets the language, in the older installer's last step at every install and update: the
one line goes under [language] with the file's own line ends, and every other byte stays (it used to write
the whole file again with LF line ends, a line end after the last line and no byte order mark). A second run
leaves the file alone; a changed language changes that line; a byte order mark and a line that is not UTF-8
stay too."""
from installer import ini as inst_ini
path = self.tmp / 'nx2navi.ini'
path.write_bytes(PLAYER_INI)
inst_ini.write(self.tmp, 'en-US')
want = PLAYER_INI.replace(b'[language]\r\n', b'[language]\r\nlang = en-US\r\n')
self.assertEqual(path.read_bytes(), want)
self.assertEqual(want.count(b'lang = en-US'), 1)
stamp = path.stat().st_mtime_ns
inst_ini.write(self.tmp, 'en-US')
self.assertEqual((path.read_bytes(), path.stat().st_mtime_ns), (want, stamp))
inst_ini.write(self.tmp, 'de')
self.assertEqual(path.read_bytes(), want.replace(b'lang = en-US', b'lang = de'))
marked = b'\xef\xbb\xbf# caf\xe9\r\nlang = ja\r\nasync_compute = 1'
path.write_bytes(marked)
inst_ini.write(self.tmp, 'fr')
self.assertEqual(path.read_bytes(), marked.replace(b'lang = ja', b'lang = fr'))
self.assertFalse((self.tmp / 'nx2navi.ini.tmp').exists())
class RebuildArgsTest(Tmp): class RebuildArgsTest(Tmp):
"""Rebuild the Game compiles the game's code for this processor's level (--retune); an update keeps the level its """Rebuild the Game compiles the game's code for this processor's level (--retune); an update keeps the level its
@@ -898,6 +981,28 @@ class BackgroundUpdateTest(Tmp):
self.assertEqual(bg.install(self.game, rel), '') self.assertEqual(bg.install(self.game, rel), '')
self.assertEqual(len(self.restarts), 1) self.assertEqual(len(self.restarts), 1)
def test_an_update_leaves_the_games_settings_file(self):
"""The launcher's part of an update (the release staged beside the running version, the switch to it, the
cleanup after the rebuild) changes nothing in the game folder, also in its default place inside the
launcher's folder: the player's nx2navi.ini is the same bytes afterwards, and an uninstall that keeps the game
does not name it."""
game = self.layout.root / 'game'
(game / 'profile').mkdir(parents=True)
ini = game / 'nx2navi.ini'
ini.write_bytes(PLAYER_INI)
before = sorted(p.relative_to(game).as_posix() for p in game.rglob('*'))
bg = self.background(self.context())
rel = self.publish('0.5.0', runtime='py-2')
self.assertEqual(self.stage(bg, rel), 'staged')
self.assertEqual(ini.read_bytes(), PLAYER_INI)
self.assertEqual(bg.install(str(game), rel, why='the launcher started'), '0.5.0')
self.assertEqual(self.state(), ('0.5.0', '0.4.0', [['--continue-update', '0.4.0']]))
updater.prune(self.layout) # what ContinueUpdateJob ends with
self.assertEqual(ini.read_bytes(), PLAYER_INI)
self.assertEqual(sorted(p.relative_to(game).as_posix() for p in game.rglob('*')), before)
gone, _ = winsys.uninstall_plan(self.layout)
self.assertFalse([p for p in gone if p == game or p in ini.parents])
def test_no_install_while_the_game_runs(self): def test_no_install_while_the_game_runs(self):
"""A copy of the game started elsewhere, the copy the launcher started, and a download under way each hold """A copy of the game started elsewhere, the copy the launcher started, and a download under way each hold
the install back, also one the player asked for.""" the install back, also one the player asked for."""
@@ -1635,6 +1740,75 @@ class MakeGameTest(Tmp):
self.assertEqual(build(), ([], [{'ev': 'step', 'step': 'prompts', 'state': 'skip'}], 4)) self.assertEqual(build(), ([], [{'ev': 'step', 'step': 'prompts', 'state': 'skip'}], 4))
self.assertTrue((self.out / 'romfs-patches-xbox' / 'manifest.tsv').is_file()) self.assertTrue((self.out / 'romfs-patches-xbox' / 'manifest.tsv').is_file())
def test_an_update_keeps_the_settings_file(self):
"""The rebuild an update ends with (make_game.py --rebuild under a newer kit: the lift, the compile, the
shaders step and the button prompts all run again) leaves the game folder's nx2navi.ini as the player has it,
byte for byte. The build reads the file (the bind_ lines for the prompts, shader_carry for the shaders step)
and never writes it; so does a second rebuild that finds nothing to do, and one from the game files again.
The lift and the compile are stand-ins here (the real ones need the game and clang); the check, the ExeFS and
RomFS steps, the shaders step and the prompts step are the real ones."""
from unittest import mock
kit = self.tmp / 'kit'
for d in ('tools', 'bin', 'recomp'):
(kit / d).mkdir(parents=True)
(kit / 'VERSION').write_text('0.7.0\n')
(kit / 'recomp' / 'gen.py').write_text('# the lifter of 0.7.0\n')
(kit / 'bin' / self.mg.XLATE_NAME).write_bytes(b'translator')
(kit / 'bin' / 'nx2navi_xlate.id').write_text('xlate 1\n')
(kit / 'tools' / 'prepare_shaders.py').write_text('print("prepare-shaders: progress build 1 1", flush=True)\n')
for name in ('prompt_art.py', 'button_icons.py'):
shutil.copy2(ROOT / 'tools' / name, kit / 'tools' / name)
(kit / 'tools' / 'pad_prompts.py').write_text(FAKE_PAD_PROMPTS)
art = kit / 'prompt-art' / 'XGamepad' / 'Default'
art.mkdir(parents=True)
(art / 'T_X_A_White.png').write_bytes(b'picture one')
ran = []
def fake(name):
def step(*args, **kw):
ran.append(name)
return step
def build(*source):
"""One run of make_game.py; the steps that ran, in order."""
buf = io.StringIO()
argv = ['make_game.py', *source, '--out', str(self.out), '--msys', str(self.msys), '--events']
with mock.patch.object(sys, 'argv', argv), contextlib.redirect_stdout(buf), \
contextlib.redirect_stderr(io.StringIO()):
self.mg.main()
events = [json.loads(ln[2:]) for ln in buf.getvalue().splitlines() if ln.startswith('@@')]
return [e['step'] for e in events if e['ev'] == 'step' and e['state'] == 'start']
sys.path.insert(0, str(self.layout.pydeps)) # the fake zbic
self.addCleanup(sys.path.remove, str(self.layout.pydeps))
ini = self.out / 'nx2navi.ini'
with mock.patch.multiple(self.mg, KIT=kit, PROMPT_PACKAGES={}, EVENTS=False, step_lift=fake('lift'),
step_compile=fake('compile'), step_scan=fake('scan')):
self.assertEqual(build('--nup', str(self.nup)),
['check', 'exefs', 'romfs', 'lift', 'compile', 'shaders', 'prompts', 'scan'])
self.assertFalse(ini.exists()) # the build makes no settings file: the game's menu does
ini.write_bytes(PLAYER_INI)
names = sorted(p.name for p in self.out.iterdir())
# the update: a kit with another lifter and another version
(kit / 'VERSION').write_text('0.7.1\n')
(kit / 'recomp' / 'gen.py').write_text('# the lifter of 0.7.1\n')
ran.clear()
steps = build('--rebuild')
self.assertEqual(steps, ['check', 'lift', 'compile', 'shaders', 'prompts']) # the bind_ lines are new to it
self.assertEqual(ran, ['lift', 'compile'])
self.assertEqual(ini.read_bytes(), PLAYER_INI)
self.assertEqual(sorted(p.name for p in self.out.iterdir()), names) # no copy, no backup, no temporary file
self.assertEqual(json.loads((self.out / 'build-cache' / 'state.json').read_text())['kit'], '0.7.1')
self.assertEqual(build('--rebuild'), ['check']) # nothing to do
self.assertEqual(ini.read_bytes(), PLAYER_INI)
# from the game files again into the same folder (Rebuild the Game without a build to start from)
st = json.loads((self.out / 'build-cache' / 'state.json').read_text())
st['steps'] = {}
(self.out / 'build-cache' / 'state.json').write_text(json.dumps(st))
self.assertEqual(build('--nup', str(self.nup)),
['check', 'exefs', 'romfs', 'lift', 'compile', 'shaders', 'prompts', 'scan'])
self.assertEqual(ini.read_bytes(), PLAYER_INI)
@unittest.skipIf(os.name == 'nt', 'uses a POSIX script as the compiler') @unittest.skipIf(os.name == 'nt', 'uses a POSIX script as the compiler')
def test_largest_source_compiles_first(self): def test_largest_source_compiles_first(self):
src, obj = self.tmp / 'src', self.tmp / 'obj' src, obj = self.tmp / 'src', self.tmp / 'obj'

72
tests/wb_lazy_check.c Normal file
View File

@@ -0,0 +1,72 @@
// 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 <stdio.h>
#include <string.h>
#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 };
static int has(const char *s, const char *part) { return s && strstr(s, part) != NULL; }
int main(void) {
static const unsigned drivers[] = {AMD_PROPRIETARY, AMD_OPEN_SOURCE, MESA_RADV, NVIDIA_PROPRIETARY, INTEL_WINDOWS,
MESA_NVK, 0};
enum { ND = sizeof drivers / sizeof *drivers };
// Nothing set, Windows: on, except on AMD's own driver (both of its branches report this id).
WblChoice nv = wb_lazy_choice(NULL, WINDOWS, NVIDIA_PROPRIETARY);
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, INTEL_WINDOWS).on == 1, "Windows, Intel's driver, nothing set: on");
CHECK(wb_lazy_choice(NULL, WINDOWS, 0).on == 1, "Windows, a driver that reports no id: on, as 0.7.1 had it");
WblChoice amd = wb_lazy_choice(NULL, WINDOWS, AMD_PROPRIETARY);
CHECK(amd.on == 0, "Windows, AMD's driver, nothing set: off");
CHECK(has(amd.why, "AMD") && has(amd.why, "wb_lazy = 1"), "it names the driver 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);
// An empty value is no value (cfg.c add: "key =" leaves the setting at its default).
CHECK(wb_lazy_choice("", WINDOWS, AMD_PROPRIETARY).on == 0, "an empty value on AMD: the default, off");
CHECK(wb_lazy_choice("", WINDOWS, NVIDIA_PROPRIETARY).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]);
CHECK(c.on == 0 && has(c.why, "wb_lazy = 1"), "Linux, driver %u, nothing set: off, and how to turn it on: %s",
drivers[i], 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]), off = wb_lazy_choice("0", os, drivers[i]);
CHECK(on.on == 1 && has(on.why, "set"), "os %d driver %u, wb_lazy = 1: on (%s)", os, drivers[i], on.why);
CHECK(off.on == 0 && has(off.why, "set"), "os %d driver %u, wb_lazy = 0: off (%s)", os, drivers[i], off.why);
CHECK(!has(on.why, "default") && !has(off.why, "default"), "os %d driver %u: a set value is not a default", os,
drivers[i]);
}
// 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).on == 1, "wb_lazy = on: on");
// The exception is the driver, not the vendor: another driver for AMD cards follows its platform's default.
CHECK(wb_lazy_choice(NULL, WINDOWS, AMD_OPEN_SOURCE).on == 1 && wb_lazy_choice(NULL, WINDOWS, MESA_RADV).on == 1,
"the exception is AMD's own driver");
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;
}

42
tests/wb_lazy_check.py Normal file
View File

@@ -0,0 +1,42 @@
"""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.
Needs a C compiler only: no Vulkan headers, no GPU and no game files.
Usage: wb_lazy_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn)."""
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
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(), '-std=gnu11', '-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())