diff --git a/.gitignore b/.gitignore index bea70934..b5647747 100644 --- a/.gitignore +++ b/.gitignore @@ -86,3 +86,6 @@ desktop.ini # Agent task worktrees (tools/worktree.py) /.worktrees/ + +# The release folder (tools/make_release.py): nx2navi_xlate.exe with its id file, hactool, ninja and the DLLs +/bin/ diff --git a/docs/README-BuildKit.md b/docs/README-BuildKit.md index 432586be..dd210b26 100644 --- a/docs/README-BuildKit.md +++ b/docs/README-BuildKit.md @@ -160,6 +160,10 @@ beside the game. driver keeps the prepared shaders' libraries in that cache, and a smaller limit drops the oldest as new ones come in (a 2048 MB cache could not hold them): every start then builds them again in the background, and play stutters, with some things missing for a few frames, until that is done. The log's `library warm` lines say when this happens. +- With 16 GB of RAM, give Windows a fixed page file of at least 32 GB (System, About, Advanced system settings, + Performance Settings, Advanced, Virtual memory: Custom size, then restart). Windows counts the game's video memory + against RAM plus page file, and a page file Windows sizes itself grows too slowly for Hyrule Field: the game then + lets textures go and the ground can flash white or black. The log says `system commit low` when this happens. - **Screen** in the menu has Texture filtering (anisotropic, up to 16x, at once), Texture detail (Reduced and Low leave out the largest versions of the big surface textures so that the game fits the video memory of 6 and 8 GB cards; Automatic picks by the card), Upscaling (DLSS on NVIDIA RTX cards, diff --git a/docs/audio-input.md b/docs/audio-input.md index ef51d705..00321082 100644 --- a/docs/audio-input.md +++ b/docs/audio-input.md @@ -4,24 +4,27 @@ The runtime connects Ocarina's polling `nn::audio` input calls to SDL3's default microphone or audio interface as the operating system's default input and enable the game's microphone control. Both a hummed note and an instrument received by that input go through the game's own note recognition. -Audio capture opens when the game calls `OpenInputStream` and starts only on `StartInputStream`. The runtime does -not write recordings to disk. SDL converts the input device's format to the requested PCM format and sample rate. +At `OpenInputStream` the runtime checks that a default input exists and prepares a buffer. It opens the recording +device at `StartInputStream` and releases it at stop or close. After a stop, the game can still read or purge the +buffered frames and start capture again. Recordings stay in memory. SDL converts the input device's format to the +requested PCM format and sample rate. The capture callback uses a bounded ring: unread overflow drops the oldest complete frames instead of retaining an unlimited recording while the game is paused. Stop, purge and close are distinct operations. Purging an unopened or failed stream is safe, including the null-stream path that previously crashed in `InputStreamImpl::Purge`. This implements the non-event polling overload used by Ocarina 1.1.1. Event-based streams, per-device selection, hot-unplug recovery and packed 24-bit PCM are not implemented. Statistics return a neutral 24-byte structure. -Unsupported formats and a denied or missing recording device return an input error rather than reporting a -successfully opened stream. Input currently follows the operating system's default recording device; there is no -new runtime menu setting. +An unsupported format or missing default device returns an error at open. If the device disappears or access is +denied when capture starts, start returns an input error and the game can retry. Input follows the operating system's +default recording device; there is no new runtime menu setting. ## Validation `python tests/audio_input_check.py --toolchain C:/msys64/ucrt64` builds and runs headless checks using SDL's dummy -audio driver. They require no game files and do not open a physical microphone. They cover sample ordering across -ring wrap and overflow, frame counts, start/stop, repeated open/close, purge, unsupported formats and purge/read -after a failed open. On Linux, omit `--toolchain` and install SDL3's development package and pkg-config. +audio driver. They require no game files or physical microphone. They check that idle streams leave the device +closed, that a denied start can be retried, and that stopping one stream releases its device and preserves unread +frames. They also cover ring wrap and overflow, frame counts, repeated open/close, unsupported formats and +purge/read after a failed open. On Linux, omit `--toolchain` and install SDL3's development package and pkg-config. A separate local real-device check received mono signed PCM16 at 48 kHz, verified the 4800-frame ring remained bounded, and opened and closed the device three times. In an Ocarina 1.1.1 Windows session built from the 0.5.0 diff --git a/docs/building.md b/docs/building.md index 558368e6..8dd611c2 100644 --- a/docs/building.md +++ b/docs/building.md @@ -362,6 +362,21 @@ A change to the button prompts (`tools/make_romfs_patches.py --prompts`, `tools/ `.venv-py314\Scripts\python tests\romfs_prompts_test.py`, which runs the tool on a RomFS it makes itself (no game files); a change to the settings passes `nx2navi.exe --probe-cfg`. +A change to the action prompt's diamond of face buttons (`runtime/action_prompt.c`) or to the SARC and zstd dictionary +helpers (`runtime/sarc.c`) passes `python tests\action_prompt_check.py`. It builds them alone, with the +undefined-behaviour sanitizer, on a layout file, archives and dictionaries it makes from the format `action_prompt.c`'s +header comment describes, and checks that: + +- by label every place, cross, flip and bubble moves and no other byte, in every copy of the layout in the archive; +- a second run changes nothing, and by label then by position gives the game's file back; +- a file that is not the expected one is refused and left as it was: one value off, half laid out, one copy of four, + damaged offsets, a pane record laid over the records that lead to another field, more than 16 copies, copies that + share some of their bytes; +- the packed file names the dictionary the original named, found by its id in the pack of dictionaries. + +No game files. `--romfs ` also runs it on the game's own archive: four copies laid +out, 208 fields, the same dictionary and unpacked size, and no byte changed outside the layout's copies. + A change to what the library warm logs when the driver's cache of stage libraries is cold (`runtime/vk_warm_hint.h`, `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. diff --git a/docs/controls.md b/docs/controls.md index 7e467309..3e85f537 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -21,6 +21,22 @@ The log names the layout in effect (`input: gamepad layout ...`) and each gamepa (`input: gamepad "...": first press`); the menu shows the layout under *Automatic*. The game's prompts show the Switch letters unless *Button prompts* says otherwise (below). +The action prompt (top right) shows the four face buttons as a diamond. The game draws it in the Switch places (A right, +B bottom, X top, Y left), which `nintendo` keeps. Under `xbox`, `playstation` and `auto` the diamond follows such a +pad instead (A bottom, B right, X left, Y top), whatever *Button prompts* is. + +The game reads the diamond once at start, before a gamepad is open: + +- `auto` gets the Xbox diamond, so a Switch Pro Controller under `auto` sees A at the bottom although its A is on the + right. Pick `nintendo` for that pad. +- A change of layout shows after a restart; until then *Button prompts* says *prompts change after restart*. +- A RomFS mod that changes the action prompt keeps its own diamond. A mod that replaces the same archive for other + reasons gets the diamond moved on top of its changes. + +The log says what happened in a line that starts with `action prompt:`. How the layout is changed: +`runtime/action_prompt.c` (`DoCommand_00` in `UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.blarc.zs`, served +from `/action-prompt/`). + ## Button prompts `NX2NAVI_PROMPTS` (`prompts`, the *Button prompts* row on the Controls tab) picks the buttons the game's prompts show: `nintendo` (default, the game's own), `xbox`, `playstation`, `steamdeck` or `pc` (*Keyboard and mouse*). It changes three things the game draws: diff --git a/docs/releases/v0.7.2.md b/docs/releases/v0.7.2.md index 744e8ae3..c645f21b 100644 --- a/docs/releases/v0.7.2.md +++ b/docs/releases/v0.7.2.md @@ -33,6 +33,13 @@ 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. 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. +- AMD RX 5000 and RX 6000 cards: the game no longer ends (code 71) when an enemy is killed. The Windows driver for + these cards faults when it links a burst of new pipelines from shader libraries, which is what the first kill of a + session asks for; players with an RX 6700 XT and an RX 5700 XT had it at every kill, and one of them found that + `gpl = 0` stops it. That is now the default with this driver (the log reads "gpl no ... off by default on AMD's + driver for RDNA 1 and 2"). The price: a pipeline the game needs for the first time is built whole while its draw + waits, so a new area hitches more on these cards. `gpl = 1` in `nx2navi.ini` turns the libraries back on. A + contributor's change, checked on their card; none of us has such a card. - 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). diff --git a/nx2navi.ini.example b/nx2navi.ini.example index a4e21170..686b88ac 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -184,7 +184,9 @@ # the new translator cannot translate keeps its old translation. 0: shaders the new translator has not translated yet # are translated before they are used (at start or when the game first draws them), as earlier versions did. # shader_carry = 1 -# Vulkan graphics pipeline library, 0 or 1. Default: on. +# Vulkan graphics pipeline library, 0 or 1. Default: on, except with AMD's Windows driver for RX 5000 and RX 6000 +# cards, which faults when it links a burst of new pipelines (the game ended at every enemy killed); there it is off, +# new pipelines are built whole, and gpl = 1 turns it on again. # gpl = 1 # Textures and memory. @@ -235,9 +237,10 @@ # which game memory gets no copy in video memory (memory = mirror), which costs far more frame time than a texture # uploaded again. 0: textures never give way for video memory. # tex_vram_pressure = 80 -# Megabytes this process may commit before textures give way as they do while the system's commit runs low (Windows -# counts video memory against the commit). Default: the installed memory, at least 24576, so that it does not depend -# on the size of the page file. 0: no bound of its own; only the system's commit counts. On Linux it counts the +# Megabytes this process may commit before textures give way as they do while the system's commit runs low. Windows +# counts video memory against the commit; with a discrete GPU the video memory in use is left out of the comparison, +# since it is not in RAM. Default: the installed memory, at least 24576, so that it does not depend on the size of the +# page file. 0: no bound of its own; only the system's commit counts. On Linux it counts the # process's resident anonymous memory, which stays under the installed memory, so there only a smaller value holds. # tex_proc_commit_mb = 32768 # 1: a texture the game releases or takes back, with nothing else changed at its address, puts the texture table's @@ -765,6 +768,11 @@ # first one, so their draw pass is not ended and begun again at each of them (about 40 passes fewer a frame in a # village). 0: each such draw ends its pass and prepares its own count, as before. # mesh_ahead = 1 +# Windows, 1 (experimental): the game's heap is committed a 64 KiB run at a time where it is first used, instead of +# all of it when the game sizes it. Windows counts committed memory against RAM plus the page file whether it is used +# or not, and in Castle Town after Hyrule Field about 3.4 of the heap's 6.8 GiB had never been used. 0: committed up +# front. Linux commits pages on first use anyway. +# heap_lazy = 0 # Linux, shadow storage: 1 clears the write watch's protection of a range in one call before the GPU's bytes are copied # back into the game's memory, instead of taking a fault at the first store into each page. # shadow_bulk_write = 0 diff --git a/runtime/action_prompt.c b/runtime/action_prompt.c new file mode 100644 index 00000000..22634380 --- /dev/null +++ b/runtime/action_prompt.c @@ -0,0 +1,406 @@ +// The action prompt's diamond of face buttons for the pad layout at start. The action prompt (top right) shows the +// four face buttons as a diamond in the Switch's places, A on the right. Under a layout by label (NX2NAVI_PAD_LAYOUT = +// xbox, playstation or auto; input_map.c input_pad_layout_pick) A is the bottom button, so romfs.c +// (action_prompt_overlay) serves the shared interface layouts with the diamond laid out as such a pad has its buttons: +// A bottom, B right, X left, Y top. The game reads the layouts once at start, so a change of layout shows after a +// restart. +// +// The layout is DoCommand_00.buil in the SARC UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.blarc.zs, a BFFH +// file of little-endian records that point at each other by 64-bit file offsets: +// - The header has a group of slots per table: at +0x00 the offset of the list of name offsets, at +0x08 that of the +// list of record offsets, at +0x10 that of the names' hashes, at +0x18 the count. Panes at 0x118, materials at 0x138. +// The group at 0x218 is the materials' texture blocks: no names, the list of the blocks' offsets in the third slot +// (0x228), the count at 0x230. +// - A pane record holds scale x and y at +0x10 and +0x14, translate x and y at +0x1c and +0x20, and flags at +0x38, in +// which 0x1000 puts a text pane's origin on its right edge. +// - A material record holds its block's index at +0x14. A block starts with the offset of its texture bindings and +// their count; a binding is 16 bytes, the offset of the texture's name at +0x08. +// Each face button K has a group N_K_00, shown while K's action is offered: P_BaseK_00, a cross whose lit arm points at +// K; three dots, the other buttons, each 80 units out in its own direction; and N_BtnK_00 at K's place, with K's +// letter, the bubble behind the action's name (W_BaseK_00, and W_BaseKDecide_00 under the null N_BaseKDecide_00, which +// the Decide animation scales) and the name (T_GuideK_00). The bubble opens to the right, or to the left for the left +// button, so the name stays clear of the diamond. The cross has two pictures, the lit arm right +// (DoCommandCrossBase_A_00 and DoCommandCrossBaseGrd_A_00) or down (_B_00), flipped for left and up. Each cross's +// material has texture bindings of its own; only the names' strings are shared, so a cross changes picture by taking +// the other picture's name offsets. No animation keys these positions, flips or flags, so the layout holds while the +// prompt animates. +#include +#include +#include +#include +#include + +#include "action_prompt.h" +#include "host.h" +#include "platform/platform.h" +#include "sarc.h" + +#define MEMBER "DoCommand_00.buil" +#define MAX_RAW (64u << 20) +enum { PANE_TABLE = 0x118, MATERIAL_TABLE = 0x138, BLOCKS = 0x228, BLOCK_COUNT = 0x230 }; +// The pane record's fields this reads; PANE_NEEDS: the bytes a record needs to hold all of them. The same for a +// material record. +enum { PANE_SCALE_X = 0x10, PANE_SCALE_Y = 0x14, PANE_X = 0x1c, PANE_Y = 0x20, PANE_FLAGS = 0x38, PANE_NEEDS = 0x3c }; +enum { MATERIAL_BLOCK = 0x14, MATERIAL_NEEDS = 0x18, BINDING_SIZE = 16, BINDING_NAME = 0x08, TEXT_RIGHT = 0x1000 }; + +// ---- the diamond in each layout +enum { EAST, SOUTH, NORTH, WEST }; +// A field is a float of a pane record, the text origin of a pane (F_RIGHT: 1 on its right edge, else 0) or the picture +// of a cross (F_CROSS: 0 the lit arm right, 1 down). The last two are held as floats too, so that one comparison checks +// every field. +enum { F_X, F_Y, F_SCALE_X, F_SCALE_Y, F_RIGHT, F_CROSS }; +static const unsigned field_at[] = { + [F_X] = PANE_X, [F_Y] = PANE_Y, [F_SCALE_X] = PANE_SCALE_X, [F_SCALE_Y] = PANE_SCALE_Y, [F_RIGHT] = PANE_FLAGS}; +static const char *const field_name[] = {"x", "y", "scale x", "scale y", "text origin", "cross picture"}; + +static const char buttons[4] = {'A', 'B', 'X', 'Y'}; +static const int direction[2][4] = {{EAST, SOUTH, NORTH, WEST}, // by position: the Switch's places + {SOUTH, EAST, WEST, NORTH}}; // by label: an Xbox pad's +static const float place[4][2] = {[EAST] = {80, 0}, [SOUTH] = {0, -80}, [NORTH] = {0, 80}, [WEST] = {-80, 0}}; +// The cross lit toward each direction: its picture and the flips that turn it. +static const struct { int picture; float sx, sy; } cross[4] = { + [EAST] = {0, 1, 1}, [SOUTH] = {1, 1, 1}, [NORTH] = {1, 1, -1}, [WEST] = {0, -1, 1}}; +// The panes at a face button's place: the other buttons' dots in each group, and each button (index into buttons). +static const struct { const char *pane; int button; } at_button[] = { + {"P_B_00", 1}, {"P_X_00", 2}, {"P_Y_00", 3}, {"P_A_00", 0}, {"P_X_02", 2}, {"P_Y_02", 3}, + {"P_A_02", 0}, {"P_B_03", 1}, {"P_Y_04", 3}, {"P_A_01", 0}, {"P_B_02", 1}, {"P_X_03", 2}, + {"N_BtnA_00", 0}, {"N_BtnB_00", 1}, {"N_BtnX_00", 2}, {"N_BtnY_00", 3}}; +enum { AT_BUTTON = sizeof at_button / sizeof *at_button }; + +typedef struct { + char pane[24]; // the pane; for F_CROSS the cross's pane, whose material has the same name + int field; + float value; +} Field; +// What diamond() sets: x and y of each pane at a button's place; each button's cross (picture, flips); for X and Y, +// which may be the left button, the flip and x of both bubbles and the name's x and origin. +enum { MAX_FIELDS = 2 * AT_BUTTON + 4 * 3 + 2 * 6 }; + +static int put(Field *f, int k, const char *pane, int field, float value) { + if (k >= MAX_FIELDS) return k + 1; // counted, not stored: action_prompt_lay_out refuses a count it does not expect + snprintf(f[k].pane, sizeof f[k].pane, "%s", pane); + f[k].field = field, f[k].value = value; + return k + 1; +} + +// Every field the diamond's layout sets, by label (label = 1) or by position; their count. +static int diamond(int label, Field *f) { + int k = 0; + char name[24]; + for (size_t i = 0; i < AT_BUTTON; i++) { + const float *at = place[direction[label][at_button[i].button]]; + k = put(f, k, at_button[i].pane, F_X, at[0]); + k = put(f, k, at_button[i].pane, F_Y, at[1]); + } + for (int b = 0; b < 4; b++) { + int d = direction[label][b]; + snprintf(name, sizeof name, "P_Base%c_00", buttons[b]); + k = put(f, k, name, F_CROSS, (float)cross[d].picture); + k = put(f, k, name, F_SCALE_X, cross[d].sx); + k = put(f, k, name, F_SCALE_Y, cross[d].sy); + if (b < 2) continue; // A and B are never the left button: their bubbles open to the right in both layouts + int left = d == WEST; + snprintf(name, sizeof name, "W_Base%c_00", buttons[b]); + k = put(f, k, name, F_SCALE_X, left ? -1.0f : 1.0f); + k = put(f, k, name, F_X, left ? 80.0f : -80.0f); + snprintf(name, sizeof name, "W_Base%cDecide_00", buttons[b]); + k = put(f, k, name, F_SCALE_X, left ? -1.0f : 1.0f); + k = put(f, k, name, F_X, left ? 80.0f : -80.0f); + snprintf(name, sizeof name, "T_Guide%c_00", buttons[b]); + k = put(f, k, name, F_X, left ? -68.0f : 64.0f); + k = put(f, k, name, F_RIGHT, (float)left); + } + return k; +} + +// ---- reading the layout, every offset checked against its size +static int u32_at(const uint8_t *p, size_t n, uint64_t off, uint32_t *v) { + if (off > n || n - off < 4) return 0; + memcpy(v, p + off, 4); + return 1; +} + +static int u64_at(const uint8_t *p, size_t n, uint64_t off, uint64_t *v) { + if (off > n || n - off < 8) return 0; + memcpy(v, p + off, 8); + return 1; +} + +static const char *str_at(const uint8_t *p, size_t n, uint64_t off) { + return off < n && memchr(p + off, 0, n - off) ? (const char *)p + off : NULL; +} + +// The record of the entry `name` of the table whose header slots start at slot, with at least size bytes; 0 when it +// is not there. +static uint64_t table_find(const uint8_t *p, size_t n, size_t slot, const char *name, size_t size) { + uint64_t names, records; + uint32_t count; + if (!u64_at(p, n, slot, &names) || !u64_at(p, n, slot + 8, &records) || !u32_at(p, n, slot + 24, &count) || + names > n || records > n || (n - names) / 8 < count || (n - records) / 8 < count) + return 0; + for (uint32_t i = 0; i < count; i++) { + uint64_t s, r; + const char *t; + if (!u64_at(p, n, names + 8 * (uint64_t)i, &s) || !(t = str_at(p, n, s)) || strcmp(t, name)) continue; + return u64_at(p, n, records + 8 * (uint64_t)i, &r) && r && r <= n && n - r >= size ? r : 0; + } + return 0; +} + +// The offsets of the name offsets of the cross `pane`'s picture and gradient (its material's first two texture +// bindings); 0 when they are not there. +static int cross_names(const uint8_t *p, size_t n, const char *pane, uint64_t at[2]) { + uint64_t m = table_find(p, n, MATERIAL_TABLE, pane, MATERIAL_NEEDS), blocks, block, bindings; + uint32_t index, count, bound; + if (!m || !u32_at(p, n, m + MATERIAL_BLOCK, &index) || !u64_at(p, n, BLOCKS, &blocks) || + !u32_at(p, n, BLOCK_COUNT, &count) || index >= count || !u64_at(p, n, blocks + 8 * (uint64_t)index, &block) || + !u64_at(p, n, block, &bindings) || !u32_at(p, n, block + 8, &bound) || bound < 2) + return 0; + for (int k = 0; k < 2; k++) { + uint64_t b, s; + if (!u64_at(p, n, bindings + 8 * (uint64_t)k, &b) || b > n || n - b < BINDING_SIZE || + !u64_at(p, n, b + BINDING_NAME, &s)) + return 0; + at[k] = b + BINDING_NAME; + } + return 1; +} + +// Which picture the names at at[] give the cross: 0 the lit arm right (A), 1 down (B), -1 neither. +static int cross_picture(const uint8_t *p, size_t n, const uint64_t at[2]) { + static const char *const names[2][2] = {{"DoCommandCrossBase_A_00", "DoCommandCrossBaseGrd_A_00"}, + {"DoCommandCrossBase_B_00", "DoCommandCrossBaseGrd_B_00"}}; + uint64_t s[2]; + const char *t[2]; + for (int k = 0; k < 2; k++) + if (!u64_at(p, n, at[k], &s[k]) || !(t[k] = str_at(p, n, s[k]))) return -1; + for (int c = 0; c < 2; c++) + if (!strcmp(t[0], names[c][0]) && !strcmp(t[1], names[c][1])) return c; + return -1; +} + +// Where a field is: the offset of its 4-byte word (the pane's flags for F_RIGHT), or for F_CROSS the offsets of the +// cross's two name offsets. Every offset is checked against the file's size when it is found. +typedef struct { + uint64_t at[2]; +} Where; + +static int field_find(const uint8_t *p, size_t n, const Field *f, Where *w) { + if (f->field == F_CROSS) return cross_names(p, n, f->pane, w->at); + uint64_t r = table_find(p, n, PANE_TABLE, f->pane, PANE_NEEDS); + w->at[0] = r + field_at[f->field], w->at[1] = 0; + return r != 0; +} + +// A found field's value; 0 when it is not one this knows (a cross with another picture). +static int field_get(const uint8_t *p, size_t n, const Field *f, const Where *w, float *v) { + if (f->field == F_CROSS) { + int c = cross_picture(p, n, w->at); + *v = (float)c; + return c >= 0; + } + if (f->field == F_RIGHT) { + uint32_t flags; + memcpy(&flags, p + w->at[0], 4); + *v = (flags & TEXT_RIGHT) ? 1.0f : 0.0f; + return 1; + } + memcpy(v, p + w->at[0], 4); + return 1; +} + +// The game stores some zeros as -2.8e-14. +static int same(float a, float b) { return fabsf(a - b) < 1e-3f; } + +int action_prompt_lay_out(uint8_t *p, size_t n, int label, char *why, size_t why_n) { + label = !!label; + Field from[MAX_FIELDS], to[MAX_FIELDS]; + int count = diamond(!label, from); + if (count != MAX_FIELDS || diamond(label, to) != MAX_FIELDS) { + snprintf(why, why_n, "the diamond has %d fields, not %d", count, MAX_FIELDS); + return -1; + } + if (n < 4 || memcmp(p, "BFFH", 4)) { + snprintf(why, why_n, "%s is not a BFFH layout", MEMBER); + return -1; + } + // Find and read every field before anything is written. + Where where[MAX_FIELDS]; + float now[MAX_FIELDS]; + int done = 1; + for (int k = 0; k < count; k++) { + if (!field_find(p, n, &from[k], &where[k]) || !field_get(p, n, &from[k], &where[k], &now[k])) { + snprintf(why, why_n, "%s has no %s %s", MEMBER, from[k].pane, field_name[from[k].field]); + return -1; + } + done &= same(now[k], to[k].value); + } + if (done) return 0; + for (int k = 0; k < count; k++) + if (!same(now[k], from[k].value)) { + snprintf(why, why_n, "%s %s %s is %g, not %g", MEMBER, from[k].pane, field_name[from[k].field], + (double)now[k], (double)from[k].value); + return -1; + } + // The name offsets of both pictures, from the crosses that show them now (the diamond by either layout shows both). + uint64_t names[2][2]; + for (int k = 0; k < count; k++) + if (from[k].field == F_CROSS) + for (int j = 0; j < 2; j++) memcpy(&names[(int)now[k]][j], p + where[k].at[j], 8); + // Write, keeping each word's bytes as they were. + uint8_t undo[MAX_FIELDS][2][8]; + int changed = 0; + for (int k = 0; k < count; k++) { + int words = to[k].field == F_CROSS ? 2 : 1, size = to[k].field == F_CROSS ? 8 : 4; + for (int j = 0; j < words; j++) memcpy(undo[k][j], p + where[k].at[j], size); + if (same(now[k], to[k].value)) continue; + changed++; + if (to[k].field == F_CROSS) { + for (int j = 0; j < 2; j++) memcpy(p + where[k].at[j], &names[(int)to[k].value][j], 8); + } else if (to[k].field == F_RIGHT) { + uint32_t flags; + memcpy(&flags, p + where[k].at[0], 4); + flags = to[k].value != 0 ? flags | TEXT_RIGHT : flags & ~(uint32_t)TEXT_RIGHT; + memcpy(p + where[k].at[0], &flags, 4); + } else { + memcpy(p + where[k].at[0], &to[k].value, 4); + } + } + // Read the layout again as the game will: each field where it was, with its new value. A file whose fields lie on + // the records that lead to them (or on each other) reads otherwise; it gets its bytes back, last written first. + for (int k = 0; k < count; k++) { + Where w; + float v; + if (field_find(p, n, &to[k], &w) && !memcmp(&w, &where[k], sizeof w) && field_get(p, n, &to[k], &w, &v) && + same(v, to[k].value)) + continue; + for (int i = count - 1; i >= 0; i--) { + int words = to[i].field == F_CROSS ? 2 : 1, size = to[i].field == F_CROSS ? 8 : 4; + for (int j = words - 1; j >= 0; j--) memcpy(p + where[i].at[j], undo[i][j], size); + } + snprintf(why, why_n, "%s %s %s overlaps another field or the records that lead to it", MEMBER, to[k].pane, + field_name[to[k].field]); + return -1; + } + return changed; +} + +// The game's archive holds each layout four times under the same name (628 members, 157 names; the four copies of +// DoCommand_00.buil are the same bytes, each at its own offset). Every copy is laid out, each checked first on a copy +// of its own, so either all of them change or none. +int action_prompt_archive(uint8_t *sarc, size_t n, int label, char *why, size_t why_n) { + enum { MAX_COPIES = 16 }; + size_t at[MAX_COPIES], size[MAX_COPIES], a, s; + unsigned next = 0; + int copies = 0, total = 0, found; + while ((found = sarc_next(sarc, n, MEMBER, &next, &a, &s)) == 1) { + if (copies == MAX_COPIES) { + snprintf(why, why_n, "the layout archive holds %s more than %d times", MEMBER, MAX_COPIES); + return -1; + } + at[copies] = a, size[copies++] = s; + } + if (found < 0) { + snprintf(why, why_n, "the layout archive is damaged"); + return -1; + } + if (!copies) { + snprintf(why, why_n, "the layout archive has no %s", MEMBER); + return -1; + } + // Copies that share their bytes are laid out once; copies that share only some of them cannot be laid out alone. + for (int c = 0; c < copies; c++) + for (int d = 0; d < c; d++) + if (!(at[c] == at[d] && size[c] == size[d]) && at[c] < at[d] + size[d] && at[d] < at[c] + size[c]) { + snprintf(why, why_n, "two copies of %s in the layout archive overlap", MEMBER); + return -1; + } + for (int c = 0; c < copies; c++) { + uint8_t *trial = malloc(size[c] ? size[c] : 1); + if (!trial) { + snprintf(why, why_n, "out of memory"); + return -1; + } + memcpy(trial, sarc + at[c], size[c]); + int r = action_prompt_lay_out(trial, size[c], label, why, why_n); + free(trial); + if (r < 0) return -1; + } + for (int c = 0; c < copies; c++) { + int r = action_prompt_lay_out(sarc + at[c], size[c], label, why, why_n); + if (r < 0) return -1; // not after a trial of the same bytes; kept so that a change here cannot hide one + total += r; + } + return total; +} + +int action_prompt_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, int label, void **out, + size_t *out_n) { + uint64_t t0 = plat_now_ns(); + label = !!label; + *out = NULL; + int result = -1; + uint8_t *dics = NULL, *raw = NULL; + void *z = NULL; + ZSTD_DDict *dd = NULL; + ZSTD_CDict *cd = NULL; + char why[200] = ""; + // The frame names its dictionary by id (0: none); Pack/ZsDic.pack.zs holds them. + unsigned id = ZSTD_getDictID_fromFrame(in, n); + size_t dn = 0, dic_at = 0, dic_n = 0; + if (id && (!(dics = zsdic_unpack(zsdic, zsdic_n, MAX_RAW, &dn)) || !zsdic_find(dics, dn, id, &dic_at, &dic_n))) { + snprintf(why, sizeof why, "no dictionary %u in Pack/ZsDic.pack.zs", id); + goto done; + } + if (id && (!(dd = ZSTD_createDDict(dics + dic_at, dic_n)) || !(cd = ZSTD_createCDict(dics + dic_at, dic_n, 3)))) { + snprintf(why, sizeof why, "dictionary %u of Pack/ZsDic.pack.zs cannot be loaded", id); + goto done; + } + unsigned long long rn = ZSTD_getFrameContentSize(in, n); + if (rn == ZSTD_CONTENTSIZE_UNKNOWN || rn == ZSTD_CONTENTSIZE_ERROR || !rn || rn > MAX_RAW) { + snprintf(why, sizeof why, "the layout archive is not a zstd frame with a size"); + goto done; + } + if (!(raw = malloc((size_t)rn))) { + snprintf(why, sizeof why, "out of memory"); + goto done; + } + ZSTD_DCtx *dctx = ZSTD_createDCtx(); + size_t got = !dctx ? 0 : dd ? ZSTD_decompress_usingDDict(dctx, raw, (size_t)rn, in, n, dd) + : ZSTD_decompressDCtx(dctx, raw, (size_t)rn, in, n); + ZSTD_freeDCtx(dctx); + if (ZSTD_isError(got) || got != rn) { + snprintf(why, sizeof why, "the layout archive does not decompress"); + goto done; + } + int changed = action_prompt_archive(raw, (size_t)rn, label, why, sizeof why); + if (changed <= 0) { + result = changed; + goto done; + } + // Level 3: the archive unpacks to about 10 MB and is packed again at every start (about 15 ms). + size_t cap = ZSTD_compressBound((size_t)rn), zn = 0; + ZSTD_CCtx *cctx = ZSTD_createCCtx(); + if (cctx && (z = malloc(cap))) + zn = cd ? ZSTD_compress_usingCDict(cctx, z, cap, raw, (size_t)rn, cd) + : ZSTD_compressCCtx(cctx, z, cap, raw, (size_t)rn, 3); + ZSTD_freeCCtx(cctx); + if (!z || !zn || ZSTD_isError(zn)) { + snprintf(why, sizeof why, "the layout archive cannot be compressed again"); + goto done; + } + *out = z, *out_n = zn, z = NULL; + result = 1; + rt_log("action prompt: face buttons laid out %s, %d fields (%.0f ms)", label ? "by label (A bottom)" : "by position", + changed, (double)(plat_now_ns() - t0) / 1e6); +done: + if (result == 0) + rt_log("action prompt: the layout archive has the face buttons %s already", label ? "by label" : "by position"); + if (result < 0) rt_log("action prompt: %s; the diamond keeps the Switch places", why); + free(z); + free(raw); + free(dics); + ZSTD_freeDDict(dd); + ZSTD_freeCDict(cd); + return result; +} diff --git a/runtime/action_prompt.h b/runtime/action_prompt.h new file mode 100644 index 00000000..1dc07c63 --- /dev/null +++ b/runtime/action_prompt.h @@ -0,0 +1,30 @@ +// The action prompt's diamond of face buttons laid out for a pad layout (action_prompt.c). +#ifndef ACTION_PROMPT_H +#define ACTION_PROMPT_H + +#include +#include + +// The shared interface layouts of the game, which hold the action prompt (DoCommand_00). +#define ACTION_PROMPT_ARCHIVE "UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.blarc.zs" + +// Lays the diamond of DoCommand_00.buil (p[0..n), in place) out for the face buttons by label (label nonzero: A +// bottom, B right, X left, Y top) or by position (label = 0: the Switch's places, A right, B bottom, X top, Y left). +// The runtime asks for label 1 only (romfs.c action_prompt_overlay); label 0 takes a layout by label back. Every field +// is found and checked before any is written, and read again after. Returns the number of fields changed; 0 when the +// diamond is laid out that way already; -1 when the file is not the layout this expects (why says which field +// differs; the file is left as it was). +int action_prompt_lay_out(uint8_t *p, size_t n, int label, char *why, size_t why_n); + +// The same on the decompressed Common layout archive (a SARC holding DoCommand_00.buil), on every copy of the layout +// it holds: all of them change or none. The count is over all copies; -1 also for a damaged archive, more than 16 +// copies, or copies that share some of their bytes. +int action_prompt_archive(uint8_t *sarc, size_t n, int label, char *why, size_t why_n); + +// The Common layout archive's zstd file in[0..n) with the diamond laid out for label, compressed again with the +// dictionary of Pack/ZsDic.pack.zs (zsdic) the original names: *out (malloc'd) and *out_n. Returns 1 when *out was +// made, 0 when the diamond is laid out that way already (nothing to serve), -1 when it cannot be (logged). +int action_prompt_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, int label, void **out, + size_t *out_n); + +#endif diff --git a/runtime/audio_capture.c b/runtime/audio_capture.c index 229bcafe..ad43d147 100644 --- a/runtime/audio_capture.c +++ b/runtime/audio_capture.c @@ -6,6 +6,7 @@ struct MicCapture { SDL_AudioStream *stream; + SDL_AudioDeviceID device; unsigned char *ring; int cap, head, used, bpf; }; @@ -95,24 +96,45 @@ MicCapture *mic_open(int format, int channels, int rate, int frames) { return NULL; } SDL_AudioSpec spec = {.format = sf, .channels = channels, .freq = rate}; - m->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec, received, m); - if (!m->stream) { - free(m->ring); - free(m); - SDL_QuitSubSystem(SDL_INIT_AUDIO); + SDL_AudioSpec source; + if (SDL_GetAudioDeviceFormat(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &source, NULL)) + m->stream = SDL_CreateAudioStream(&source, &spec); + if (!m->stream || !SDL_SetAudioStreamPutCallback(m->stream, received, m)) { + mic_close(m); return NULL; } return m; } -int mic_start(MicCapture *m) { return m && SDL_ResumeAudioStreamDevice(m->stream); } +int mic_start(MicCapture *m) { + if (!m) + return 0; + if (m->device) + return 1; + SDL_AudioSpec spec; + if (!SDL_GetAudioStreamFormat(m->stream, NULL, &spec)) + return 0; + SDL_AudioDeviceID device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec); + if (!device) + return 0; + if (!SDL_BindAudioStream(device, m->stream)) { + SDL_CloseAudioDevice(device); + return 0; + } + m->device = device; + return 1; +} void mic_stop(MicCapture *m) { - if (m) - SDL_PauseAudioStreamDevice(m->stream); + if (m && m->device) { + SDL_CloseAudioDevice(m->device); /* unbinds the stream; unread frames remain in the ring */ + m->device = 0; + } } void mic_close(MicCapture *m) { if (!m) return; - SDL_DestroyAudioStream(m->stream); /* joins capture before freeing userdata */ + mic_stop(m); + if (m->stream) + SDL_DestroyAudioStream(m->stream); free(m->ring); free(m); SDL_QuitSubSystem(SDL_INIT_AUDIO); diff --git a/runtime/camera_speed.c b/runtime/camera_speed.c index 6a2c11af..bd330519 100644 --- a/runtime/camera_speed.c +++ b/runtime/camera_speed.c @@ -24,6 +24,7 @@ #include "cfg.h" #include "host.h" #include "platform/platform.h" +#include "sarc.h" #define MEMBER "Component/PlayerCameraParam/PlayerCamera.game__component__PlayerCameraParam.bgyml" #define MAX_RAW (64u << 20) @@ -46,32 +47,6 @@ static int u32_at(const uint8_t *p, size_t n, size_t off, uint32_t *v) { return 1; } -// SARC member `name` of the archive p[0..n): its start and size; 0 when it is not there. -static int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size) { - uint32_t data; - uint16_t head, count, bom; - if (n < 0x14 || memcmp(p, "SARC", 4)) return 0; - memcpy(&head, p + 4, 2); - memcpy(&bom, p + 6, 2); - if (bom != 0xfeff || !u32_at(p, n, 12, &data) || head + 12u > n || memcmp(p + head, "SFAT", 4)) return 0; - memcpy(&count, p + head + 6, 2); - size_t names = head + 12 + (size_t)count * 16; - if (names + 8 > n || memcmp(p + names, "SFNT", 4)) return 0; - names += 8; - for (unsigned i = 0; i < count; i++) { - uint32_t attr, lo, hi; - size_t e = head + 12 + (size_t)i * 16; - if (!u32_at(p, n, e + 4, &attr) || !u32_at(p, n, e + 8, &lo) || !u32_at(p, n, e + 12, &hi)) return 0; - size_t s = names + (size_t)(attr & 0xffffff) * 4, len = strlen(name); - if (!(attr >> 24) || s + len + 1 > n || memcmp(p + s, name, len + 1)) continue; - if (lo > hi || data + (size_t)hi > n) return 0; - *at = data + lo; - *size = hi - lo; - return 1; - } - return 0; -} - // The StickSensitivity floats of the BYML p[0..n), multiplied by k; their count, 0 when the file has no such array. static int scale_sensitivity(uint8_t *p, size_t n, float k, float was[8]) { uint32_t keys, root, head; @@ -117,21 +92,19 @@ int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, ZSTD_DDict *dd = NULL; ZSTD_CDict *cd = NULL; void *z = NULL; - // The dictionaries: Pack/ZsDic.pack.zs is a SARC compressed without one. - unsigned long long dn = ZSTD_getFrameContentSize(zsdic, zsdic_n); - size_t dic_at, dic_n; - if (dn == ZSTD_CONTENTSIZE_UNKNOWN || dn == ZSTD_CONTENTSIZE_ERROR || dn > MAX_RAW || !(dics = malloc((size_t)dn)) || - ZSTD_decompress(dics, (size_t)dn, zsdic, zsdic_n) != dn || - !sarc_member(dics, (size_t)dn, "pack.zsdic", &dic_at, &dic_n)) { - rt_log("camera speed: no pack.zsdic in Pack/ZsDic.pack.zs; the camera keeps its own speed"); + // The frame names its dictionary (pack.zsdic) by id; Pack/ZsDic.pack.zs holds them. + unsigned id = ZSTD_getDictID_fromFrame(in, n); + size_t dn, dic_at, dic_n; + if (!id || !(dics = zsdic_unpack(zsdic, zsdic_n, MAX_RAW, &dn)) || !zsdic_find(dics, dn, id, &dic_at, &dic_n)) { + rt_log("camera speed: Pack/ZsDic.pack.zs has no dictionary %u for Pack/Actor/PlayerCamera.pack.zs; the camera " + "keeps its own speed", id); goto done; } unsigned long long rn = ZSTD_getFrameContentSize(in, n); - unsigned id = ZSTD_getDictID_fromFrame(in, n); dd = ZSTD_createDDict(dics + dic_at, dic_n); - if (!dd || id != ZSTD_getDictID_fromDDict(dd) || rn == ZSTD_CONTENTSIZE_UNKNOWN || - rn == ZSTD_CONTENTSIZE_ERROR || rn > MAX_RAW || !(raw = malloc((size_t)rn))) { - rt_log("camera speed: Pack/Actor/PlayerCamera.pack.zs is not a pack.zsdic frame with a size; the camera keeps its " + if (!dd || rn == ZSTD_CONTENTSIZE_UNKNOWN || rn == ZSTD_CONTENTSIZE_ERROR || rn > MAX_RAW || + !(raw = malloc((size_t)rn))) { + rt_log("camera speed: Pack/Actor/PlayerCamera.pack.zs is not a zstd frame with a size; the camera keeps its " "own speed"); goto done; } diff --git a/runtime/cfg.c b/runtime/cfg.c index 4a6d0a1c..8cf00fa0 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -46,7 +46,7 @@ static const char *const known[] = { "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLIGHT_EVERY", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FG_MULT", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPL_FEEDBACK", "PIPE_AHEAD", "PIPE_FAST_WORKERS", "PIPE_FRAME_WAIT_MS", "PIPE_LINK_DRAW_MS", "PIPE_SKIP_STEADY", "PROG_LEAD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", - "HDR_HUE_CLIP", "HDR_LIVE", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", + "HDR_HUE_CLIP", "HDR_LIVE", "HEAP_LAZY", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_TONE_SLOPE", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "GATE_WAIT_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", @@ -128,7 +128,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"VK_NAN_BITS", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, - {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"HEAP_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"VA_REPORT", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, }; @@ -253,7 +253,7 @@ static const CfgMeta metas[] = { {"GATE_WAIT_MS", "10", "0..100", RESTART}, // vk_exec.c vk_exec_gate_wait {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, {"LIB_WARM", "1", "0|1|force", RESTART}, // vk_pipeline.c lw_start: the driver's cache of stage libraries, warmed when cold - {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART}, + {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "", "0|1", RESTART}, // vk_caps.c vk_caps_gpl: by the driver, off with AMD's for RDNA 1 and 2 {"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART}, {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "1", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, {"BIG_BUILD_MAX_MB", "4096", "0..262144", RESTART}, @@ -281,7 +281,7 @@ static const CfgMeta metas[] = { {"VK_NOOPT", "0", "0|1", RESTART}, // vk_pipeline.c noopt_flag // diagnostics {"TRACE", "", NULL, RESTART}, {"FLIGHT", "0", "0|1", RESTART}, {"FLIGHT_SPIKE_MS", "100", "0..60000", RESTART}, - {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, {"MEM_CENSUS", "0", "0|1", RESTART}, {"WB_NO_REUP", "1", "0|1", RESTART}, {"WB_PROBE", "0", "0|1", RESTART}, {"WB_SPLIT", "1", "0|1|2", RESTART}, {"WB_LAZY", WB_LAZY_DEFAULT, "0|1", RESTART}, {"WB_LAZY_TOUCH", "0", "0..16", RESTART}, {"WB_LAZY_CHUNK", "1", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, {"GPA_TWIN_MAX_KB", "0", "0..1048576", RESTART}, + {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, {"MEM_CENSUS", "0", "0|1", RESTART}, {"WB_NO_REUP", "1", "0|1", RESTART}, {"WB_PROBE", "0", "0|1", RESTART}, {"WB_SPLIT", "1", "0|1|2", RESTART}, {"WB_LAZY", WB_LAZY_DEFAULT, "0|1", RESTART}, {"HEAP_LAZY", "0", "0|1", RESTART}, {"WB_LAZY_TOUCH", "0", "0..16", RESTART}, {"WB_LAZY_CHUNK", "1", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, {"GPA_TWIN_MAX_KB", "0", "0..1048576", RESTART}, {"WB_MERGE_MAX", "32", "1..256", RESTART}, {"TEXUP_LOG_MS", "0", "0..60000", RESTART}, // vk_texture.c vk_texstat_frame {"VMA_BLOCK_MB", "64", "16..256", RESTART}, // vk_headroom.c vk_vma_block_size {"ASTC_BC_PROBE", "0", "0|1", RESTART}, // vk_texture.c astc_probe_alpha diff --git a/runtime/dlssg/nx2_sl.cpp b/runtime/dlssg/nx2_sl.cpp index 12c11a31..0307e7e3 100644 --- a/runtime/dlssg/nx2_sl.cpp +++ b/runtime/dlssg/nx2_sl.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -151,12 +152,22 @@ extern "C" NX2SL_API int nx2sl_available(void) { sl::DLSSGState s{}; if (g_available && p_dlssg_state && p_dlssg_state(sl::ViewportHandle(0), s, nullptr) == sl::Result::eOk) g_max.store(s.numFramesToGenerateMax); - if (!g_available) say("dlssg: DLSS Frame Generation not available on this GPU or driver"); + if (!g_available) say("dlssg: frame generation unavailable (DLSS-G plugin loaded: %d, DLSS-G options: %d, PCL marker: %d)", + loaded, p_dlssg_options != nullptr, p_marker != nullptr); else if (g_max.load()) say("dlssg: DLSS Frame Generation available, up to %ux", g_max.load() + 1); else say("dlssg: DLSS Frame Generation available (its most frames per rendered frame not reported yet)"); return g_available; } +extern "C" NX2SL_API void nx2sl_log_unavailable(VkPhysicalDevice physical) { + auto supported = proc("slIsFeatureSupported"); + if (!supported) return say("dlssg: Streamline has no adapter support query"); + sl::AdapterInfo adapter{}; + adapter.vkPhysicalDevice = physical; + sl::Result r = supported(sl::kFeatureDLSS_G, adapter); + say("dlssg: frame generation support on the selected Vulkan adapter: %s (%d)", sl::getResultAsStr(r), (int)r); +} + // Streamline sets Reflex's mode on the swapchain it was told of last (VK_NV_low_latency2) and drops it while there is // none, so it is set again after each swapchain. extern "C" NX2SL_API void nx2sl_reflex(int mode) { diff --git a/runtime/dlssg/nx2_sl.h b/runtime/dlssg/nx2_sl.h index 9728e7db..ab97a5c0 100644 --- a/runtime/dlssg/nx2_sl.h +++ b/runtime/dlssg/nx2_sl.h @@ -44,6 +44,8 @@ typedef PFN_vkGetInstanceProcAddr (*PFN_nx2sl_instance_proc)(void); typedef PFN_vkGetDeviceProcAddr (*PFN_nx2sl_device_proc)(void); // 1 when DLSS-G loaded and supports this GPU (after the device exists) typedef int (*PFN_nx2sl_available)(void); +// Optional: log Streamline's support result on the selected Vulkan device after availability failed. +typedef void (*PFN_nx2sl_log_unavailable)(VkPhysicalDevice physical); // Reflex's low-latency mode, 0 off, 1 on, 2 on with boost: after the device, and after each swapchain the game creates // through the interposer (the mode is the swapchain's) typedef void (*PFN_nx2sl_reflex)(int mode); diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index e7430c9d..d40bbf90 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -298,7 +298,12 @@ static int probe = -1; static float last_pos[3]; static uint64_t last_pos_ns; -static int read_guest(uint64_t a, void *out, size_t n) { return a && plat_mem_read(a, out, n); } +// plat_mem_read copies through the kernel, which fails on a heap page NX2NAVI_HEAP_LAZY has not committed yet +static int read_guest(uint64_t a, void *out, size_t n) { + if (!a) return 0; + mem_kernel_access(a, n); + return plat_mem_read(a, out, n); +} static uint64_t read_ptr(uint64_t a) { uint64_t p = 0; return read_guest(a, &p, 8) ? p : 0; @@ -447,7 +452,7 @@ static void ocarina_step_write(double dt) { return; } uint64_t slot = read_ptr(MOD_MAIN_BASE + 0x4c7e890), mgr = slot ? read_ptr(slot) : 0; - if (!mgr || !plat_mem_readable(mgr + 0x1c0, 4)) return; + if (!mgr || !mem_host_readable(mgr + 0x1c0, 4)) return; _Atomic uint32_t *word = (_Atomic uint32_t *)(uintptr_t)(mgr + 0x1c0); uint32_t now_bits = atomic_load_explicit(word, memory_order_relaxed); float now_v; diff --git a/runtime/fs.h b/runtime/fs.h index 01876c57..9736600f 100644 --- a/runtime/fs.h +++ b/runtime/fs.h @@ -51,6 +51,9 @@ enum { ROMFS_PROMPTS_OK, ROMFS_PROMPTS_MISSING, ROMFS_PROMPTS_FAILED, ROMFS_PROM const char *romfs_prompts(void); int romfs_prompts_ready(const char *folder); int romfs_prompts_off(const char *folder); +// The pad layout (PAD_LAYOUT_*, input_map.h) the action prompt's diamond of face buttons has from start +// (action_prompt.c); -1 when the layout by label was asked for but the layout file could not be changed. +int romfs_action_prompt(void); const char *romfs_overlay(size_t k, const char **file, uint64_t *size, int *is_added); // RomFS path of overlay k void fs_trace(const char *what, const char *path); // NX2NAVI_TRACE=fs: one line per open diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 59b573b7..1bee63b9 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -1152,8 +1152,10 @@ static uint32_t write_file(uint64_t h, int64_t offset, const void *buffer, uint6 if (file->save) save_touched(); for (uint64_t done = 0; done < size;) { uint32_t chunk = size - done > 0x40000000 ? 0x40000000 : (uint32_t)(size - done); - // NX2NAVI_WB_PROBE: the kernel reads the buffer, and a probed page would fail the write instead of faulting + // NX2NAVI_WB_PROBE: the kernel reads the buffer, and a probed page would fail the write instead of faulting; + // so would a heap page NX2NAVI_HEAP_LAZY has not committed yet if (mem_probing()) mem_thaw((uintptr_t)buffer + done, chunk); + mem_kernel_access((uintptr_t)buffer + done, chunk); int64_t put = plat_file_pwrite(f, (const uint8_t *)buffer + done, chunk, (uint64_t)offset + done); if (put <= 0) rt_fatal("fs: host save write failure"); done += (uint64_t)put; diff --git a/runtime/host.h b/runtime/host.h index 39ad79e4..cb5b3560 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -154,6 +154,7 @@ void mem_init(void); // reserve every regi // (up to 20 times, NX2NAVI_REEXEC), then fatal const RtGuestLayout *mem_layout(void); void mem_guest_census(uint64_t frame); // NX2NAVI_TRACE=perf: committed bytes per guest region +void mem_lazy_heap_report(uint64_t frame); // NX2NAVI_TRACE=perf: the lazy heap's faults since the last line uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr); // 0x01 uint32_t mem_set_permission(uint64_t a, uint64_t n, uint32_t perm); // 0x02 uint32_t mem_set_attribute(uint64_t a, uint64_t n, uint32_t mask, uint32_t at); // 0x03 @@ -184,7 +185,12 @@ int mem_freeze(uint64_t a); // makes the 2 MiB re int mem_frozen(uint64_t a); // the 2 MiB region of a is frozen: no page in it changed // since its freeze void mem_thaw(uint64_t a, uint64_t n); // makes the frozen regions over [a, a+n) writable again; - // before a kernel write (file read) into guest memory + // before a kernel write (file read) into guest memory; + // also commits pages the lazy heap left reserved +void mem_kernel_access(uint64_t a, uint64_t n); // NX2NAVI_HEAP_LAZY: commits the reserved heap pages of + // [a, a+n) before the kernel reads them (a file write) +int mem_host_readable(uint64_t a, uint64_t n); // plat_mem_readable that first commits the untouched + // pages of the lazy heap in [a, a+n), which read zero void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws); // NX2NAVI_WB_PROBE=1 (memory.c, "Probed regions"): who reads what the GPU wrote back into guest memory. // NX2NAVI_WB_LAZY (memory.c, "Lazy write-back"): pages the GPU wrote that come down only when the CPU touches them. diff --git a/runtime/ipc.c b/runtime/ipc.c index 1cc4f113..4c4c7e65 100644 --- a/runtime/ipc.c +++ b/runtime/ipc.c @@ -243,7 +243,7 @@ void ipc_handle_closed(int kind, void *obj) { } // ---- helpers for services -static int readable(uint64_t a, uint64_t n) { return plat_mem_readable(a, n); } +static int readable(uint64_t a, uint64_t n) { return mem_host_readable(a, n); } void ipc_backtrace(void) { const Cpu *c = t_req; diff --git a/runtime/kernel.c b/runtime/kernel.c index e3cb0dda..57798e26 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -364,7 +364,7 @@ void kern_free_cores_note(void) { // NX2NAVI_CORES = 1..NCORE: how many virtual cores run threads (default all NCORE). A core at or above the count is // folded onto core (c mod count): a thread pinned to core 5 with NX2NAVI_CORES=3 runs on core 2, one thread per core -// at a time as always (diagnosis of races that more parallel cores expose). +// at a time as always (diagnosis of races that more parallel cores expose; the Steam Deck profile's four). static int core_allowed(const RtThread *t, int c) { if (c >= g_active_cores) return 0; for (int j = c; j < NCORE; j += g_active_cores) @@ -372,6 +372,21 @@ static int core_allowed(const RtThread *t, int c) { return 0; } +// The core number a thread sees (GetCurrentProcessorNumber) while it holds virtual core c. With all NCORE cores that +// is c. With fewer it is one of the thread's own cores that fold onto c, its ideal core first: the game keys per-core +// data by this number without locks, so a thread pinned to core 5 must go on using core 5's data when it takes +// turns with core 1's threads on one virtual core. Seeing 1 there, it used core 1's data in turn with core 1's own +// threads, and the game died in its draw set-up within seconds of the first frame in play (issue #445; with +// NX2NAVI_CORES=4 on any machine). No two threads that run guest code see the same number: they hold different +// virtual cores, and the numbers that fold onto different virtual cores differ. +static int core_seen(const RtThread *t, int c) { + if (g_active_cores >= NCORE) return c; + if (t->core >= 0 && t->core < NCORE && t->core % g_active_cores == c && ((t->mask >> t->core) & 1)) return t->core; + for (int j = c; j < NCORE; j += g_active_cores) + if ((t->mask >> j) & 1) return j; + return c; +} + // ---- asynchronous preemption (NX2NAVI_ASYNC_PREEMPT, on unless 0) // Contains code Copyright (c) 2026 MacNativeGames contributors (MIT; see THIRD_PARTY_NOTICES.md). // Horizon gives a core to a strictly better thread the moment that thread is ready. @@ -1707,9 +1722,9 @@ static int kern_svc_inner(Cpu *c, uint32_t imm) { unlock(); return 1; } - case 0x10: { // GetCurrentProcessorNumber: the virtual core held + case 0x10: { // GetCurrentProcessorNumber: the core seen on the virtual core held (core_seen) RtThread *t = cur(imm); - x[0] = (uint64_t)(uint32_t)(t->cur_core >= 0 ? t->cur_core : t->core); + x[0] = (uint64_t)(uint32_t)(t->cur_core >= 0 ? core_seen(t, t->cur_core) : t->core); return 1; } case 0x36: { // SynchronizePreemptionState: the SDK's spin locks call it after clearing TLR+0x100 (disable_count) diff --git a/runtime/kernel_probe.c b/runtime/kernel_probe.c index 0323177f..25efbc1e 100644 --- a/runtime/kernel_probe.c +++ b/runtime/kernel_probe.c @@ -474,6 +474,46 @@ static void test_cores(Cpu *c) { CHECK(ok || !gate, "virtual cores"); } +// ---- pinned threads: each sees the number of the core it is pinned to, also where NX2NAVI_CORES folds that core +// onto another one (kernel.c core_seen), and no two that run guest code at once see the same number. Run the probe +// with NX2NAVI_CORES=4 (the Steam Deck profile's value) to check the folded case; with all cores it holds trivially. +#define PIN_CORES 6 // kernel.c NCORE +static volatile int32_t g_pin_wrong, g_pin_clash, g_pin_use[PROBE_CORES]; +static uint64_t a_pin_worker; + +static void pin_worker(Cpu *c) { + uint32_t own = *(uint32_t *)arg_p(c, 0); + for (int i = 0; i < CORE_RUNS; i++) { + uint32_t core = sv(c, 0x10, 0, 0, 0, 0); + if (core != own) plat_inc32(&g_pin_wrong); + int known = core < PROBE_CORES; + if (!known || plat_inc32(&g_pin_use[core]) != 1) plat_inc32(&g_pin_clash); + uint64_t until = kern_now_ns() + 20000; + while (kern_now_ns() < until) {} + if (known) plat_dec32(&g_pin_use[core]); + sv(c, 0x0B, i % 8 == 7 ? 50000 : 0, 0, 0, 0); + } + exit_thread(c); +} + +static void test_pinned_cores(Cpu *c) { + static uint32_t own[2 * PIN_CORES]; + uint32_t h[2 * PIN_CORES]; + g_pin_wrong = g_pin_clash = 0; + for (int i = 0; i < 2 * PIN_CORES; i++) { // two threads per core, so each core's own threads take turns too + own[i] = (uint32_t)(i % PIN_CORES); + h[i] = create(c, a_pin_worker, &own[i], 44, i % PIN_CORES); + start(c, h[i]); + } + for (int i = 0; i < 2 * PIN_CORES; i++) join(c, h[i]); + int gate = !cfg_bool("NX2NAVI_FREE_CORES"); + int ok = !g_pin_wrong && !g_pin_clash; + printf("pinned cores: %d threads on %d cores (%d virtual cores run threads) x %d runs: %d saw another core's " + "number, %d core-number clashes: %s\n", 2 * PIN_CORES, PIN_CORES, kern_cores(), CORE_RUNS, g_pin_wrong, + g_pin_clash, !gate ? "not checked (NX2NAVI_FREE_CORES is set)" : ok ? "ok" : "FAIL"); + CHECK(ok || !gate, "pinned cores"); +} + // ---- fibers: two fibers switch to each other; each checks its callee-saved registers, sp and tpidr survive typedef struct { uint64_t self, other; int id; } FiberJob; static volatile int32_t g_switches, g_runs[3]; @@ -670,6 +710,7 @@ static void probe_main(Cpu *c) { test_arbiter(c); test_events(c); test_cores(c); + test_pinned_cores(c); test_fibers(c); test_exit(c); test_lifted_ticks(c); @@ -740,6 +781,7 @@ int probe_kernel(void) { a_arb_waiter = hostfn_register("probe-kernel arbiter waiter", arb_waiter); a_event_waiter = hostfn_register("probe-kernel event waiter", event_waiter); a_core_worker = hostfn_register("probe-kernel core worker", core_worker); + a_pin_worker = hostfn_register("probe-kernel pin worker", pin_worker); a_exit_entry = hostfn_register("probe-kernel exit entry", exit_entry); a_exit_nested = hostfn_register("probe-kernel exit nested", exit_nested); a_fiber_fn = hostfn_register("probe-kernel fiber", fiber_fn); diff --git a/runtime/memory.c b/runtime/memory.c index 1f31ee91..ff404b82 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -49,6 +49,9 @@ static RtGuestLayout L; static uint64_t mods_lo, mods_hi; // fixed module window static PlatMutex lock = PLAT_MUTEX_INIT; static uint64_t heap_size, alias_bytes; +static int heap_lazy; // NX2NAVI_HEAP_LAZY, Windows: the heap is committed page run by page run on first use +static uint64_t heap_lazy_faults, heap_lazy_bytes; // accesses that committed heap pages, and the bytes they committed +static uint64_t heap_lazy_ns, heap_lazy_max_ns; // time in those faults, lock wait included, since the last report static uint8_t *alias_pages; // 1 per mapped 4 KiB page of the alias region typedef struct { uint64_t dst, src, size; } StackMap; static StackMap stack_maps[MAX_STACK_MAPS]; @@ -615,8 +618,72 @@ static void thaw_range_locked(uint64_t a, uint64_t n, int why) { for (uint64_t r = r0; r <= r1; r++) thaw_locked(r, why); } +// Lazy heap (NX2NAVI_HEAP_LAZY, Windows only; off by default). Windows charges every committed page to the system's +// commit at once, and SetHeapSize asks for the whole application heap up front: in Castle Town after Hyrule Field the +// heap held 6.8 GiB of commit of which 3.4 GiB had ever been resident, with 0.45 GB in the page file. With it on, +// SetHeapSize only moves the heap's end, and a page of the heap is committed when something first reaches it: an access +// of the game's or the runtime's code faults into access_fault, which commits the reserved runs of the 64 KiB around +// it; a kernel read or write of a buffer (mem_thaw, mem_kernel_access) and a pointer check (mem_host_readable) +// commit their range first, as the kernel and VirtualQuery do not fault. On Linux pages are committed on first touch +// already. +// frz_lock held exclusively. Commits the reserved pages of [a, a + n) inside the heap; returns the bytes committed. A +// page another path committed is left alone: committing it again would make a read-only or inaccessible page (a +// freeze, a probe, an armed lazy write-back) read-write behind their backs. The regions it commits into are thawed +// first, as for any mapping change, so that no frozen region holds a read-write page. +static uint64_t heap_lazy_commit_locked(uint64_t a, uint64_t n) { + uint64_t lo = a > L.heap.base ? a : L.heap.base, top = L.heap.base + __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE); + uint64_t end = a + n < a ? UINT64_MAX : a + n, hi = end < top ? end : top, got = 0; + if (lo >= hi) return 0; + lo &= ~(PAGE - 1), hi = align_up(hi, PAGE); + int thawed = 0; + for (uint64_t x = lo, y; x < hi; x = y) { + if (plat_mem_state(x, hi, &y) != PLAT_MEM_RESERVED) continue; + if (!thawed) thaw_range_locked(lo, hi - lo, 3), thawed = 1; + if (!plat_mem_commit(x, y - x)) + rt_fatal("memory: committing the heap's pages %#llx+%#llx on first use failed: the system's commit is " + "used up", (unsigned long long)x, (unsigned long long)(y - x)); + got += y - x; + } + heap_lazy_bytes += got; + return got; +} + +// Commits the heap pages of [a, a + n) that the lazy heap left reserved; nothing when it is off. +void mem_kernel_access(uint64_t a, uint64_t n) { + if (!heap_lazy || !n) return; + plat_mutex_lock(&frz_lock); + heap_lazy_commit_locked(a, n); + plat_mutex_unlock(&frz_lock); +} + +// plat_mem_readable for a pointer the host is about to read: untouched heap pages of a lazy heap count as readable +// (they are committed here, and read zero as they would have). +int mem_host_readable(uint64_t a, uint64_t n) { + mem_kernel_access(a, n); + return plat_mem_readable(a, n); +} + +// An access fault on a reserved page of the lazy heap: the 64 KiB around it is committed and the access runs again. +// 0 when nothing was reserved there, so that a frozen, probed or armed page goes on to its own hook (a page another +// thread committed meanwhile is retried by thaw_fault or the end of access_fault, as the heap is watched). +static int heap_lazy_fault(uint64_t a) { + uint64_t heap = L.heap.base; + if (a < heap || a - heap >= __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE)) return 0; + uint64_t t0 = plat_now_ns(); + plat_mutex_lock(&frz_lock); + uint64_t got = heap_lazy_commit_locked(a & ~0xFFFFull, 0x10000); + if (got) { + uint64_t ns = plat_now_ns() - t0; + heap_lazy_faults++, heap_lazy_ns += ns; + if (ns > heap_lazy_max_ns) heap_lazy_max_ns = ns; + } + plat_mutex_unlock(&frz_lock); + return got != 0; +} + void mem_thaw(uint64_t a, uint64_t n) { plat_mutex_lock(&frz_lock); // also when nothing is frozen: the hold keeps a read's buffer writable + if (heap_lazy) heap_lazy_commit_locked(a, n); // the kernel's write would fail on a reserved page as well thaw_range_locked(a, n, 2); plat_mutex_unlock(&frz_lock); } @@ -718,9 +785,11 @@ static int probe_fault(uint64_t a, int write, uint64_t ip) { return plat_mem_state(a & ~(PAGE - 1), (a & ~(PAGE - 1)) + PAGE, &end) >= PLAT_PROT_READ; } -// The platform's access-fault hook: an armed page of the lazy write-back first, then the probe. A read that faulted on +// The platform's access-fault hook: a reserved page of the lazy heap first, then an armed page of the lazy write-back, +// then the probe. A read that faulted on // a page another thread gave back meanwhile runs again; a write goes on to thaw_fault. static int access_fault(uint64_t a, int write, uint64_t ip) { + if (heap_lazy && heap_lazy_fault(a)) return 1; if (__atomic_load_n(&lz_on, __ATOMIC_RELAXED) && lazy_fault(a, write)) return 1; if (__atomic_load_n(&prb_on, __ATOMIC_RELAXED)) return probe_fault(a, write, ip); if (write || a >= RT_ADDRESS_LIMIT || !watched(a, 1)) return 0; @@ -860,6 +929,13 @@ void mem_init(void) { .pool = pool ? strtoull(pool, NULL, 0) : DEFAULT_POOL + res_heap_extra_bytes(), // resolution.c }; if (!L.pool) rt_fatal("memory: NX2NAVI_APP_POOL is 0"); +#ifdef _WIN32 + if (cfg_bool("NX2NAVI_HEAP_LAZY")) { // set before the game's first SetHeapSize + plat_mem_access_hook(access_fault); + heap_lazy = 1; + rt_log("mem: the heap is committed on first use (NX2NAVI_HEAP_LAZY)"); + } +#endif } const RtGuestLayout *mem_layout(void) { return &L; } @@ -900,9 +976,10 @@ uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) { plat_mutex_unlock(&lock); return R_INVALID_STATE; } - if (size > heap_size) commit(L.heap.base + heap_size, size - heap_size, "heap"); - if (size < heap_size) decommit(L.heap.base + size, heap_size - size, "heap"); - heap_size = size; + uint64_t was = heap_size; + if (size > was && !heap_lazy) commit(L.heap.base + was, size - was, "heap"); + __atomic_store_n(&heap_size, size, __ATOMIC_RELEASE); // before a shrink: no first-use commit past the new end + if (size < was) decommit(L.heap.base + size, was - size, "heap"); // reserved pages in the range are fine __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); if (addr) *addr = L.heap.base; @@ -1337,6 +1414,23 @@ void mem_kernel_free(void *p, uint64_t size) { // NX2NAVI_TRACE=perf: how many bytes of each guest region are committed, so a process that has grown says which region // holds the memory. A region is reserved whole at start-up and committed page by page, so the walk reads each run of // pages of the same state; the alias window is the largest and costs the most steps when it is full. +// NX2NAVI_TRACE=perf: what the lazy heap's first-use faults cost since the last line (perf.c, every perf window that had +// some), so that a hitch can be told from them: the time includes the wait for frz_lock. +void mem_lazy_heap_report(uint64_t frame) { + static uint64_t faults_prv, bytes_prv; + if (!heap_lazy) return; + plat_mutex_lock(&frz_lock); + uint64_t faults = heap_lazy_faults, bytes = heap_lazy_bytes, ns = heap_lazy_ns, max = heap_lazy_max_ns; + heap_lazy_ns = heap_lazy_max_ns = 0; + plat_mutex_unlock(&frz_lock); + if (faults == faults_prv && bytes == bytes_prv) return; + rt_log("mem: lazy heap at frame %llu: %llu faults and %.1f MiB committed since the last line, %.2f ms in the faults " + "(longest %.3f ms); %.0f of %.0f MiB committed", (unsigned long long)frame, + (unsigned long long)(faults - faults_prv), (bytes - bytes_prv) / 1048576.0, ns / 1e6, max / 1e6, + bytes / 1048576.0, __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE) / 1048576.0); + faults_prv = faults, bytes_prv = bytes; +} + void mem_guest_census(uint64_t frame) { const RtGuestLayout *g = mem_layout(); const struct { const char *name; RtRange r; } reg[] = { @@ -1359,4 +1453,8 @@ void mem_guest_census(uint64_t frame) { o += snprintf(line + o, sizeof line - o, " %s %.0f", reg[i].name, committed / 1048576.0); } rt_log("%s; all %.0f", line, all / 1048576.0); + if (heap_lazy) + rt_log("mem: lazy heap at frame %llu: %.0f of %.0f MiB committed, by %llu faults and the rest by kernel " + "accesses and pointer checks", (unsigned long long)frame, heap_lazy_bytes / 1048576.0, + __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE) / 1048576.0, (unsigned long long)heap_lazy_faults); } diff --git a/runtime/menu_assets.c b/runtime/menu_assets.c index d4036a89..ea464846 100644 --- a/runtime/menu_assets.c +++ b/runtime/menu_assets.c @@ -19,6 +19,7 @@ #include "fs.h" #include "host.h" #include "menu_assets.h" +#include "sarc.h" #include "tex.h" #define MAX_RAW (512u << 20) @@ -28,26 +29,6 @@ static uint16_t le16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); } static uint64_t le64(const uint8_t *p) { return (uint64_t)le32(p) | (uint64_t)le32(p + 4) << 32; } static uint32_t be32(const uint8_t *p) { return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; } -// SARC member `name`: start and size inside p[0..n); 0 when absent or out of range. -static int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size) { - if (n < 0x14 || memcmp(p, "SARC", 4)) return 0; - size_t hl = le16(p + 4), data = le32(p + 0xc); - if (hl + 0xc > n || memcmp(p + hl, "SFAT", 4)) return 0; - size_t count = le16(p + hl + 6), ents = hl + 0xc, names = ents + 16 * count + 8; - if (names > n || memcmp(p + ents + 16 * count, "SFNT", 4)) return 0; - for (size_t i = 0; i < count; i++) { - const uint8_t *e = p + ents + 16 * i; - size_t no = names + (size_t)(le32(e + 4) & 0xffff) * 4, s = le32(e + 8), end = le32(e + 12); - if (no >= n || data + end > n || s > end) continue; - size_t len = strnlen((const char *)p + no, n - no); - if (len == strlen(name) && !memcmp(p + no, name, len)) { - *at = data + s, *size = end - s; - return 1; - } - } - return 0; -} - static ZSTD_DDict *dicts[8]; static unsigned dict_ids[8]; static int ndicts = -1; @@ -59,13 +40,11 @@ static void dicts_load(void) { uint8_t *z = romfs_load("Pack/ZsDic.pack.zs", &zn64); zn = (size_t)zn64; if (!z) return; - unsigned long long rn = ZSTD_getFrameContentSize(z, zn); - uint8_t *raw = rn && rn <= MAX_RAW ? malloc((size_t)rn) : NULL; - if (raw && ZSTD_decompress(raw, (size_t)rn, z, zn) == rn) { - static const char *const names[] = {"zs.zsdic", "pack.zsdic", "bcett.byml.zsdic", "bkprb.zsdic"}; - for (int i = 0; i < 4 && ndicts < 8; i++) { - size_t at, size; - if (!sarc_member(raw, (size_t)rn, names[i], &at, &size)) continue; + size_t rn = 0, at, size; + unsigned next = 0; + uint8_t *raw = zsdic_unpack(z, zn, MAX_RAW, &rn); + if (raw) { + while (ndicts < 8 && sarc_next(raw, rn, NULL, &next, &at, &size) == 1) { ZSTD_DDict *d = ZSTD_createDDict(raw + at, size); if (!d) continue; dict_ids[ndicts] = ZSTD_getDictID_fromDDict(d); diff --git a/runtime/menu_cursor.c b/runtime/menu_cursor.c index f9a5c39b..9cf132e0 100644 --- a/runtime/menu_cursor.c +++ b/runtime/menu_cursor.c @@ -52,7 +52,7 @@ enum { PANE_PARENT = 0x18, PANE_SIZE = 0x50, PANE_FLAG = 0x58, PANE_ALPHA = 0x59 static uint64_t main_lo, main_hi; static int ok_at(uint64_t p, uint64_t n) { - return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && plat_mem_readable(p, n); + return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && mem_host_readable(p, n); } static uint64_t rd64(uint64_t a) { uint64_t v; memcpy(&v, (const void *)(uintptr_t)a, 8); return v; } static uint32_t rd32(uint64_t a) { uint32_t v; memcpy(&v, (const void *)(uintptr_t)a, 4); return v; } @@ -156,7 +156,7 @@ static Focus focus_now(void) { static void layout_name(uint64_t scr, char *out, size_t n) { snprintf(out, n, "?"); uint64_t lyt = rd64(scr + SCR_LAYOUT), name = ok_at(lyt, LYT_NAME + 8) ? rd64(lyt + LYT_NAME) : 0; - if (name < 0x10000 || name >= (1ull << 47) || !plat_mem_readable(name, n)) return; + if (name < 0x10000 || name >= (1ull << 47) || !mem_host_readable(name, n)) return; memcpy(out, (const void *)(uintptr_t)name, n - 1); out[n - 1] = 0; for (char *q = out; *q; q++) if (*q < 0x20 || *q > 0x7e) { *q = 0; break; } diff --git a/runtime/ov_audio_input.c b/runtime/ov_audio_input.c index df3b9e9a..cff46a05 100644 --- a/runtime/ov_audio_input.c +++ b/runtime/ov_audio_input.c @@ -86,7 +86,7 @@ static void open_input(Cpu *c) { } MicCapture *m = mic_open(f[0], f[1], f[2], frames); if (!m) { - rt_log("mic: cannot open the default recording device: %s", mic_error()); + rt_log("mic: cannot prepare the input stream: %s", mic_error()); ret_x(c, BAD_INPUT); return; } @@ -97,7 +97,7 @@ static void open_input(Cpu *c) { s->next_log = (uint64_t)f[2] * 5; uint64_t impl = key + 8; memcpy(ipc_guest(key, 8), &impl, 8); - rt_log("mic: default recording device opened; bounded buffer %d frames", mic_capacity(m)); + rt_log("mic: input stream ready; bounded buffer %d frames; device opens when capture starts", mic_capacity(m)); ret_x(c, 0); } static void start_input(Cpu *c) { @@ -115,6 +115,8 @@ static void stop_input(Cpu *c) { Input *s = find(c->x[0]); if (s) { mic_stop(s->mic); + if (s->running) + rt_log("mic: capture stopped; recording device released"); s->running = 0; } ret_x(c, 0); diff --git a/runtime/perf.c b/runtime/perf.c index f801ca97..2b1a7a39 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -24,6 +24,7 @@ __attribute__((weak)) void prof_sampler_write(void) {} __attribute__((weak)) void prof_sampler_frame(uint64_t frame, uint64_t now, uint64_t d) { (void)frame, (void)now, (void)d; } __attribute__((weak)) void prof_commit_census(uint64_t frame) { (void)frame; } __attribute__((weak)) void mem_guest_census(uint64_t frame) { (void)frame; } +__attribute__((weak)) void mem_lazy_heap_report(uint64_t frame) { (void)frame; } // NX2NAVI_TRACE=perf, NX2NAVI_FLIGHT=1 or the full performance overlay: event counts are taken. Wait times are always taken // (frame-time summary). perf_base: the part fixed at start. @@ -456,6 +457,7 @@ static void perf_gpu_line(uint64_t frame, double interval_ms, double pace_share, if (census_on < 0) census_on = cfg_bool("NX2NAVI_MEM_CENSUS"); if (census_on && (frame % 3600 == 0 || prv >= census_prv + (1ull << 30))) prof_commit_census(frame), mem_guest_census(frame), census_prv = prv; + mem_lazy_heap_report(frame); if (g.have_gpu && g.have_idle) rt_log("perf: frame %llu: gpu idle per frame %.2f ms between timed parts: %.2f ms waiting for the next submission, " "%.2f ms with it queued (untimed work or waits); %.1f gaps over 0.5 ms, longest %.2f ms; submit to start avg %.2f ms", diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index ccb6eac0..7d382090 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -2229,6 +2229,9 @@ int plat_commit_note(char *out, size_t cap) { // The cgroup's memory.current and MemAvailable count system memory only: a graphics card's own memory is in neither. int plat_commit_counts_vram(void) { return 0; } +// Swap is not a page file Windows sizes; the limits plat_commit_info reads do not grow with it. +int plat_page_file_managed(void) { return -1; } + // ---- child processes struct PlatChild { diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 81c7f741..2ac9355b 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -488,6 +488,9 @@ int plat_commit_note(char *out, size_t cap); // Whether a discrete GPU's own memory counts against that limit: on Windows the driver charges device memory to the // system's commit; Linux's limits count system memory only. int plat_commit_counts_vram(void); +// Whether Windows sizes a page file by itself (System managed size): 1; 0 when every page file has a size set; -1 +// when unknown, there is none, or not Windows. +int plat_page_file_managed(void); // This process's memory in bytes: resident (the working set) and private (Windows: committed private bytes; Linux: the // resident anonymous pages). 0 when unknown. void plat_process_memory(uint64_t *resident, uint64_t *private_bytes); diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 9f8138d1..2bb6b679 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1922,6 +1922,28 @@ int plat_commit_note(char *out, size_t cap) { // The driver charges the device memory it allocates to the system's commit. int plat_commit_counts_vram(void) { return 1; } +// The PagingFiles value under Memory Management, one " " per page file: a path of +// "?:\..." (System managed size on every drive) or no sizes or "0 0" (System managed size on that drive) leaves the +// size to Windows. Read once; the setting takes effect only after a restart. +int plat_page_file_managed(void) { + static int managed = -2; + if (managed != -2) return managed; + wchar_t v[2048]; + DWORD size = sizeof v - sizeof(wchar_t) * 2; + int r = -1; + if (RegGetValueW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management", + L"PagingFiles", RRF_RT_REG_MULTI_SZ, NULL, v, &size) == ERROR_SUCCESS) { + v[size / sizeof(wchar_t)] = v[size / sizeof(wchar_t) + 1] = 0; + for (wchar_t *e = v; *e; e += wcslen(e) + 1) { + wchar_t *sp = wcschr(e, L' '); + unsigned long lo = sp ? wcstoul(sp, &sp, 10) : 0, hi = sp ? wcstoul(sp, NULL, 10) : 0; + int own = e[0] == L'?' || (!lo && !hi); + r = own ? 1 : r < 0 ? 0 : r; + } + } + return managed = r; +} + void plat_process_memory(uint64_t *resident, uint64_t *private_bytes) { PROCESS_MEMORY_COUNTERS_EX pm = {.cb = sizeof pm}; *resident = *private_bytes = 0; diff --git a/runtime/romfs.c b/runtime/romfs.c index 5316a9c1..616efad8 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -30,6 +30,7 @@ #include #include +#include "action_prompt.h" #include "cfg.h" #include "fs.h" #include "host.h" @@ -782,6 +783,32 @@ static void camera_overlay(void) { if (ok && serve_generated(CAMPACK, base, out, out_n, "camera", "camera speed", keeps)) cam_pack_served(); } +// The action prompt's diamond of face buttons for the pad layout at start (action_prompt.c). By position (the +// Switch's places) the game's own layout is right; by label the Common layout archive the game would read (a mod's, else +// the RomFS's) is served with the diamond laid out again, written to /action-prompt/. A pad is not open yet +// this early, so auto counts as by label, as for the prompts' folders (input_map.c input_prompts_folder). +static int action_prompt_for = -1; // romfs_action_prompt +static void action_prompt_overlay(void) { + int layout = input_pad_layout_pick(cfg_value("NX2NAVI_PAD_LAYOUT"), 0); + if (layout == PAD_LAYOUT_NINTENDO) { + action_prompt_for = layout; + return; + } + static const char keeps[] = "the action prompt keeps the Switch places"; + Overlay *base, *dic_base; + size_t n, dn, out_n = 0; + void *in = read_served(ACTION_PROMPT_ARCHIVE, &base, &n, keeps), *out = NULL; + void *dic = in ? read_served("Pack/ZsDic.pack.zs", &dic_base, &dn, keeps) : NULL; + int made = in && dic ? action_prompt_build(in, n, dic, dn, 1, &out, &out_n) : -1; + free(in); + free(dic); + if (made == 0 || (made > 0 && serve_generated(ACTION_PROMPT_ARCHIVE, base, out, out_n, "action-prompt", + "face buttons by label", keeps))) + action_prompt_for = layout; +} + +int romfs_action_prompt(void) { return action_prompt_for; } + // The button prompts' folders (input_prompts_folder) and whether each had a manifest next to romfs-index.bin at start; // the one the settings asked for at start, the one overlay_init loaded (NULL: the game's own prompts) and why the first // did not load (ROMFS_PROMPTS_*). The menu compares them with the settings in effect (ui/menu.cpp prompts_note). @@ -938,6 +965,7 @@ static void overlay_init(void) { restbl_overlay(); heapdef_overlay(); camera_overlay(); + action_prompt_overlay(); added_init(); if (noverlay) qsort(overlays, noverlay, sizeof *overlays, overlay_order); for (size_t k = 1; k < noverlay; k++) // overlay_contest left one per file; the generated ones replace in place diff --git a/runtime/sarc.c b/runtime/sarc.c new file mode 100644 index 00000000..25cf5412 --- /dev/null +++ b/runtime/sarc.c @@ -0,0 +1,71 @@ +// SARC archives and the zstd dictionaries of Pack/ZsDic.pack.zs. A SARC is a little-endian header ("SARC", its +// length at +4, the byte-order mark 0xfeff at +6, the start of the members' data at +12), then SFAT: its member count +// at +6 and from +12 one 16-byte entry per member (name hash, attributes, start and end relative to the data). An +// entry's attributes hold a nonzero top byte when the member has a name, and in the low 24 bits the name's offset in +// SFNT's string table divided by 4; SFNT's 8-byte header follows the entries. The game's archives may hold several +// members under one name (UI/Layout2Archive/Common...blarc.zs holds each layout four times), so members are walked +// one by one. +#include +#include +#include + +#include "sarc.h" + +int sarc_next(const uint8_t *p, size_t n, const char *name, unsigned *next, size_t *at, size_t *size) { + uint32_t data; + uint16_t head, count, bom; + if (n < 0x14 || memcmp(p, "SARC", 4)) return -1; + memcpy(&head, p + 4, 2); + memcpy(&bom, p + 6, 2); + memcpy(&data, p + 12, 4); + if (bom != 0xfeff || (size_t)head + 12 > n || memcmp(p + head, "SFAT", 4)) return -1; + memcpy(&count, p + head + 6, 2); + size_t names = (size_t)head + 12 + (size_t)count * 16; // the entries end where SFNT starts + if (names + 8 > n || memcmp(p + names, "SFNT", 4)) return -1; + names += 8; + size_t len = name ? strlen(name) : 0; + for (unsigned i = *next; i < count; i++) { + uint32_t attr, lo, hi; + const uint8_t *e = p + head + 12 + (size_t)i * 16; + memcpy(&attr, e + 4, 4); + memcpy(&lo, e + 8, 4); + memcpy(&hi, e + 12, 4); + if (name) { + size_t s = names + (size_t)(attr & 0xffffff) * 4; + if (!(attr >> 24) || s > n || n - s < len + 1 || memcmp(p + s, name, len + 1)) continue; + } + if (lo > hi || (size_t)data + hi > n) return -1; + *at = (size_t)data + lo; + *size = hi - lo; + *next = i + 1; + return 1; + } + *next = count; + return 0; +} + +int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size) { + unsigned next = 0; + return sarc_next(p, n, name, &next, at, size) == 1; +} + +uint8_t *zsdic_unpack(const void *z, size_t zn, size_t max, size_t *n) { + unsigned long long rn = ZSTD_getFrameContentSize(z, zn); + if (rn == ZSTD_CONTENTSIZE_UNKNOWN || rn == ZSTD_CONTENTSIZE_ERROR || !rn || rn > max) return NULL; + uint8_t *raw = malloc((size_t)rn); + size_t got = raw ? ZSTD_decompress(raw, (size_t)rn, z, zn) : 0; + if (!raw || ZSTD_isError(got) || got != rn) { + free(raw); + return NULL; + } + *n = (size_t)rn; + return raw; +} + +int zsdic_find(const uint8_t *dics, size_t n, unsigned id, size_t *at, size_t *size) { + unsigned next = 0; + if (!id) return 0; + while (sarc_next(dics, n, NULL, &next, at, size) == 1) + if (ZSTD_getDictID_fromDict(dics + *at, *size) == id) return 1; + return 0; +} diff --git a/runtime/sarc.h b/runtime/sarc.h new file mode 100644 index 00000000..3be54481 --- /dev/null +++ b/runtime/sarc.h @@ -0,0 +1,24 @@ +// SARC archives and the zstd dictionaries of Pack/ZsDic.pack.zs (sarc.c). +#ifndef SARC_H +#define SARC_H + +#include +#include + +// The next member of the SARC p[0..n) from member *next on that is named `name` (any member when name is NULL): 1 with +// its start and size, *next moved past it; 0 when no such member is left; -1 when the archive is damaged (its header, +// or the found member's range). +int sarc_next(const uint8_t *p, size_t n, const char *name, unsigned *next, size_t *at, size_t *size); + +// The first member named `name`: 1 with its start and size; 0 when there is none or the archive is damaged. +int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size); + +// Pack/ZsDic.pack.zs's archive (z[0..zn), a frame compressed without a dictionary) unpacked: malloc'd, *n its size; +// NULL when it is not a zstd frame that says its size, or that size is over max. +uint8_t *zsdic_unpack(const void *z, size_t zn, size_t max, size_t *n); + +// The dictionary with id `id` among the members of that archive (dics[0..n)): 1 with its start and size; 0 when none +// has it (and always for id 0, the id of a frame without a dictionary). +int zsdic_find(const uint8_t *dics, size_t n, unsigned id, size_t *at, size_t *size); + +#endif diff --git a/runtime/svc.c b/runtime/svc.c index c980ae6d..bfd5a83c 100644 --- a/runtime/svc.c +++ b/runtime/svc.c @@ -24,7 +24,7 @@ RtStats rt_stats(void) { return (RtStats){__atomic_load_n(&g_svcs, __ATOMIC_RELAXED), kern_threads_created(), hostfn_calls()}; } -static int readable(uint64_t a, uint64_t n) { return plat_mem_readable(a, n); } +static int readable(uint64_t a, uint64_t n) { return mem_host_readable(a, n); } void kern_backtrace(const Cpu *c) { char d[160]; diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index afa03aff..46962aad 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -1055,7 +1055,7 @@ static void row_framegen(void) { // NVIDIA's set since the start but not running: "in use: off" and why (vk_dlssg_status; the log has the details). // Through NGX that is NVIDIA's frame generation library missing beside the game, or a GPU it does not run on. const int st = cur != 2 || restart ? DLSSG_RUNS : ngx ? (vk_ngxfg_max() ? DLSSG_RUNS : DLSSG_NO_FILES) : vk_dlssg_status(); - if (st == DLSSG_NO_GPU) note = UI_MENU_FRAMEGEN_OFF_GPU; + if (st == DLSSG_UNAVAILABLE) note = UI_MENU_FRAMEGEN_UNAVAILABLE; else if (st == DLSSG_NO_FILES) note = UI_MENU_FRAMEGEN_OFF_FILES; int nv = opt_choice_index("NX2NAVI_FRAMEGEN", UI_MENU_FRAMEGEN, names, n, at, 0, desc, menu_text(note)); if (nv >= 0 && nv < n) opt_apply("NX2NAVI_FRAMEGEN", values[nv]); @@ -1281,13 +1281,17 @@ static void row_walk_key(void) { // layout, as a layout that puts A on the right button needs the face buttons named the other way round), but the game // reads its fonts and UI archive once at start (romfs.c overlay_init). While the folder in use differs: "files // missing" when the setup did not write the one asked for, why it was left out at start (damaged, a game resolution -// that is not 16:9, a RomFS mod with the same files, NX2NAVI_ROMFS_PATCHES), else "prompts change after restart", and only then -// *restart is set: a restart would bring the prompts asked for. NULL when they match. +// that is not 16:9, a RomFS mod with the same files, NX2NAVI_ROMFS_PATCHES), else "prompts change after restart" with +// *restart set: a restart would bring the prompts asked for. The action prompt's diamond of face buttons is laid out +// for the pad layout at start too (romfs.c action_prompt_overlay), whatever the prompts: while the layout asks for the +// other places, a restart would lay it out again, so *restart is set whatever the note, and with the folder in use the +// note is "prompts change after restart". NULL when both match. static const char *prompts_note(bool *restart) { - *restart = false; const char *layout = cfg_value("NX2NAVI_PAD_LAYOUT"), *used = romfs_prompts(); const char *want = input_prompts_folder(input_prompts_pick(cfg_get("NX2NAVI_PROMPTS"), layout), layout); - if (want == used || (want && used && !strcmp(want, used))) return nullptr; + int diamond = romfs_action_prompt(); // -1 when it could not be laid out: a restart would not change it + *restart = diamond >= 0 && diamond != input_pad_layout_pick(layout, 0); + if (want == used || (want && used && !strcmp(want, used))) return *restart ? menu_text(UI_MENU_PROMPTS_RESTART) : nullptr; // A game resolution of another shape needs a folder of its own for the PC prompts (romfs-patches-pc-x), which // the setup does not write: the shape is the reason then, not the setup. int off = romfs_prompts_off(want); diff --git a/runtime/ui/menu_art.cpp b/runtime/ui/menu_art.cpp index 3a623ca0..ad7f31f8 100644 --- a/runtime/ui/menu_art.cpp +++ b/runtime/ui/menu_art.cpp @@ -134,6 +134,11 @@ static int upload_rgba(Art &a, const uint8_t *rgba, uint32_t w, uint32_t h, uint } memcpy(map, chain, (size_t)bytes); free(chain); + // The batch flushes its own staging buffers. This one is caller-owned and can be noncoherent. + if (vmaFlushAllocation(c->vma, sa, 0, bytes) != VK_SUCCESS) { + vmaDestroyBuffer(c->vma, sb, sa); + return art_free(a), 0; + } VkCommandBuffer cb = vk_upload_begin(); VkImageMemoryBarrier2 b = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2}; b.srcStageMask = VK_PIPELINE_STAGE_2_NONE; diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 8462312b..53191f72 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -434,7 +434,8 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_PREWARM_XLATE] = "Translator prewarm", [UI_MENU_PREWARM_XLATE_DESC] = "Threads that translate known shaders at start; Automatic takes half the CPU threads, at most 16.", [UI_MENU_GPL] = "Graphics pipeline library", - [UI_MENU_GPL_DESC] = "Uses the Vulkan pipeline library extension; turn off if your driver misbehaves with it.", + [UI_MENU_GPL_DESC] = "Uses the Vulkan pipeline library extension. Default: off on AMD's driver for RDNA 1 and 2, " + "which crashes when it links a burst of new pipelines, on elsewhere.", [UI_MENU_ASTC] = "ASTC texture decoding", [UI_MENU_ASTC_DESC] = "Where compressed ASTC textures are decoded.", [UI_MENU_ASTC_AUTO] = "Automatic", [UI_MENU_ASTC_GPU] = "GPU shader", [UI_MENU_ASTC_NATIVE] = "Native", [UI_MENU_ASTC_CPU] = "CPU", @@ -592,7 +593,7 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_FRAMEGEN_MULT_RTX50] = "3x and 4x need an RTX 50 GPU", [UI_MENU_FRAMEGEN_MULT_UPTO] = "up to %dx on this GPU and driver", [UI_MENU_FRAMEGEN_MULT_OLD_DLL] = "2x only: update the DLSS frame generation files", - [UI_MENU_FRAMEGEN_OFF_GPU] = "in use: off, needs an RTX 40 GPU or newer", + [UI_MENU_FRAMEGEN_UNAVAILABLE] = "in use: off, see dlssg in the game log", [UI_MENU_FRAMEGEN_OFF_FILES] = "in use: off, its files are missing", [UI_MENU_GAME_RES] = "Resolution", [UI_MENU_GAME_RES_DESC] = "Height of the game's picture. Higher is sharper but costs more GPU power.", @@ -1007,7 +1008,8 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_PREWARM_XLATE] = "Прогрев транслятора", [UI_MENU_PREWARM_XLATE_DESC] = "Потоки, которые при запуске переводят известные шейдеры; «Автоматически» — половина потоков процессора, не больше 16.", [UI_MENU_GPL] = "Библиотека графических конвейеров", - [UI_MENU_GPL_DESC] = "Использует расширение Vulkan для библиотек конвейеров; выключите, если драйвер с ним сбоит.", + [UI_MENU_GPL_DESC] = "Использует расширение Vulkan для библиотек конвейеров. По умолчанию выключено на драйвере AMD " + "для RDNA 1 и 2, который падает, когда связывает много новых конвейеров, включено везде.", [UI_MENU_ASTC] = "Распаковка текстур ASTC", [UI_MENU_ASTC_DESC] = "Где распаковываются сжатые текстуры ASTC.", [UI_MENU_ASTC_AUTO] = "Автоматически", [UI_MENU_ASTC_GPU] = "Шейдер GPU", [UI_MENU_ASTC_NATIVE] = "Аппаратно", [UI_MENU_ASTC_CPU] = "Процессор", @@ -1397,7 +1399,8 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_PREWARM_XLATE] = "Pré-aquecimento do tradutor", [UI_MENU_PREWARM_XLATE_DESC] = "Threads que traduzem os shaders conhecidos ao iniciar; Automático usa metade das threads da CPU, no máximo 16.", [UI_MENU_GPL] = "Biblioteca de pipelines gráficos", - [UI_MENU_GPL_DESC] = "Usa a extensão de biblioteca de pipelines do Vulkan; desligue se o seu driver tiver problemas com ela.", + [UI_MENU_GPL_DESC] = "Usa a extensão de biblioteca de pipelines do Vulkan. Padrão: desligado no driver da AMD para " + "RDNA 1 e 2, que trava ao vincular uma rajada de pipelines novos, ligado nos demais.", [UI_MENU_ASTC] = "Decodificação de texturas ASTC", [UI_MENU_ASTC_DESC] = "Onde as texturas comprimidas em ASTC são decodificadas.", [UI_MENU_ASTC_AUTO] = "Automático", [UI_MENU_ASTC_GPU] = "Shader da GPU", [UI_MENU_ASTC_NATIVE] = "Nativo", @@ -2058,7 +2061,8 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_PREWARM_XLATE_DESC] = "启动时转译已知着色器的线程数;自动取 CPU 线程数的一半," "最多 16。", [UI_MENU_GPL] = "图形管线库", - [UI_MENU_GPL_DESC] = "使用 Vulkan 管线库扩展;驱动对此有问题时请关闭。", + [UI_MENU_GPL_DESC] = "使用 Vulkan 管线库扩展。默认:在 AMD RDNA 1 和 2 驱动上关闭,因为驱动在链接大量新管线时会崩溃," + "其他平台默认开启。", [UI_MENU_ASTC] = "ASTC 纹理解码", [UI_MENU_ASTC_DESC] = "压缩的 ASTC 纹理在哪里解码。", [UI_MENU_ASTC_AUTO] = "自动", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 668e9b3f..379e2172 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -295,7 +295,7 @@ typedef enum { UI_MENU_FRAMEGEN_MULT_RTX50, // this GPU and driver do 2x only UI_MENU_FRAMEGEN_MULT_UPTO, // printf: "%d", the highest multiplier this GPU and driver allow UI_MENU_FRAMEGEN_MULT_OLD_DLL, // nx2navi_dlssg.dll is older than multi frame generation - UI_MENU_FRAMEGEN_OFF_GPU, UI_MENU_FRAMEGEN_OFF_FILES, + UI_MENU_FRAMEGEN_UNAVAILABLE, UI_MENU_FRAMEGEN_OFF_FILES, UI_TEXT_COUNT } UiTextId; diff --git a/runtime/vk.h b/runtime/vk.h index cff0622a..8c2e1b03 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -58,7 +58,7 @@ typedef struct { uint32_t sbuf_align; // nonzero: storage buffers bind at their address rounded down to this uint32_t sg_size; // requiredSubgroupSize on sg_pin_stages (32) VkShaderStageFlags sg_pin_stages; // 0: no pin, stages run at the device's subgroupSize - int gpl; // VK_EXT_graphics_pipeline_library enabled with fast linking (NX2NAVI_GPL=0: off) + int gpl; // VK_EXT_graphics_pipeline_library enabled with fast linking (NX2NAVI_GPL=0/1; unset: off on AMD RDNA 1/2) int amd; // an AMD GPU (vendor 0x1002), any driver int amd_sc; // AMD's Windows driver branch for RDNA 1 and 2 ("AMD proprietary shader compiler") int amd_llpc; // AMD's own driver for RDNA 3 and newer (its driverInfo names LLPC) diff --git a/runtime/vk_astc.c b/runtime/vk_astc.c index daa4e5b9..04bda92b 100644 --- a/runtime/vk_astc.c +++ b/runtime/vk_astc.c @@ -483,13 +483,19 @@ static VkBuffer scratch_get(VkCtx *c, VkCommandBuffer cb, VkDeviceSize need, int VmaAllocationCreateInfo aci = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; VkBuffer b; VmaAllocation a; - if (vmaCreateBuffer(c->vma, &bi, &aci, &b, &a, NULL) != VK_SUCCESS) { + VkResult r = vmaCreateBuffer(c->vma, &bi, &aci, &b, &a, NULL); + if (r == VK_ERROR_OUT_OF_DEVICE_MEMORY && bi.size > need) { + // The usual scratch leaves room for later textures. This upload can still fit without the spare bytes. + bi.size = need; + r = vmaCreateBuffer(c->vma, &bi, &aci, &b, &a, NULL); + } + if (r != VK_SUCCESS) { plat_mutex_unlock(&g_lock); return VK_NULL_HANDLE; } // the old one may still be read by its command buffer: freed once the batches recorded so far completed if (S.buf && S.dev == c->device) vk_defer_buffer(S.buf, S.alloc); - S.buf = b, S.alloc = a, S.size = size, S.dev = c->device; + S.buf = b, S.alloc = a, S.size = bi.size, S.dev = c->device; n_scratch_new++; vk_texstat_time(VK_TS_SCRATCH, t0); } else { diff --git a/runtime/vk_caps.c b/runtime/vk_caps.c index 03ce9f2f..584e255c 100644 --- a/runtime/vk_caps.c +++ b/runtime/vk_caps.c @@ -6,7 +6,8 @@ // - minStorageBufferOffsetAlignment above 4 (NX2NAVI_VK_SSBO_ALIGN= raises it): the translator computes // storage offsets from the guest address rounded down to that alignment (the translator's unaligned storage path), and // vk_exec_draw.c ssbo_info binds the rounded-down address, so shader writes reach guest memory instead of a copy. -// - no graphics pipeline library with fast linking (NX2NAVI_GPL=0 forces it): vk_caps.gpl = 0, the extension stays disabled, and pipelines are built whole. +// - no graphics pipeline library with fast linking (NX2NAVI_GPL=0 forces it; it also defaults off on AMD's RDNA 1/2 +// Windows driver branch, see vk_caps_gpl): vk_caps.gpl = 0, the extension stays disabled, and pipelines are built whole. // - no 32-lane pin (no subgroupSizeControl, 32 outside min..max, or NX2NAVI_VK_SUBGROUP_PIN=0): stages run at the // device's subgroupSize (SPIR-V 1.3 without a required size) and the translator is told that size. // NX2NAVI_VK_SUBGROUP= pins n instead of 32 where the driver allows it. @@ -24,6 +25,7 @@ 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 const char *gpl_off; // why gpl is off, for the boot report; NULL when it is on 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; } @@ -53,18 +55,26 @@ void vk_caps_driver(VkPhysicalDevice p) { drv.driverID == VK_DRIVER_ID_MESA_NVK; } -// NX2NAVI_GPL=0 turns graphics pipeline libraries off; they are on wherever the driver has fast linking. AMD's RDNA 1/2 -// driver branch had them off for a while: its device losses at boot (issue #5) came with a cold pipeline cache, but -// their cause was GPU writes past 3D BC images (vk_exec.c, "3D BC render targets"). With that fixed, the RDNA 1/2 -// driver boots with GPL from a cold cache and fast-links without a failure, while with GPL off every new pipeline is -// built whole on the draw's path (frames of 0.5-1.2 s on an RX 6600, issue #381). RDNA 4 under RADV is reported to -// hang the GPU at the title screen with GPL. +// NX2NAVI_GPL=0 turns graphics pipeline libraries off and 1 turns them on; unset, they are on wherever the driver has +// fast linking, except on AMD's Windows driver for RDNA 1 and 2 (vk_caps.amd_sc). That driver faults inside +// vkCreateGraphicsPipelines when it links the stage libraries of a burst of new pipelines, which the game asks for when +// an enemy dies and its effects are drawn for the first time: the game ended with code 71 at every kill on an RX 6700 XT +// and an RX 5700 XT (issue #418), and no longer with gpl = 0. The price there is that a new pipeline is built whole on +// the draw's path (frames of 0.5-1.2 s on an RX 6600 while an area is new). The same driver had libraries off before, +// for device losses at boot (issue #5), whose cause was GPU writes past 3D BC images (vk_exec.c, "3D BC render +// targets"); it boots with them since that was fixed, so gpl = 1 is there to try on a newer driver. RDNA 4 under RADV +// is reported to hang the GPU at the title screen with GPL. int vk_caps_gpl(const VkPhysicalDeviceGraphicsPipelineLibraryFeaturesEXT *f, const VkPhysicalDeviceGraphicsPipelineLibraryPropertiesEXT *p) { gpl_lib = f->graphicsPipelineLibrary; gpl_fast = p->graphicsPipelineLibraryFastLinking; const char *e = cfg_get("NX2NAVI_GPL"); - vk_caps.gpl = gpl_lib && gpl_fast && !(e && !strcmp(e, "0")); + int want = !e || !*e ? !vk_caps.amd_sc : strcmp(e, "0") != 0; + vk_caps.gpl = gpl_lib && gpl_fast && want; + gpl_off = vk_caps.gpl ? NULL + : !gpl_lib || !gpl_fast ? "the driver has no fast linking" + : e && *e ? "NX2NAVI_GPL=0" + : "default on AMD's driver for RDNA 1 and 2 (NX2NAVI_GPL=1 turns it on)"; return vk_caps.gpl; } @@ -158,8 +168,11 @@ void vk_caps_init(VkCtx *c) { has_ext(ep, ne, "VK_EXT_graphics_pipeline_library"), depth_ok(p, VK_FORMAT_D16_UNORM), depth_ok(p, VK_FORMAT_D24_UNORM_S8_UINT), depth_ok(p, VK_FORMAT_X8_D24_UNORM_PACK32), depth_ok(p, VK_FORMAT_D32_SFLOAT), depth_ok(p, VK_FORMAT_D32_SFLOAT_S8_UINT)); + char gpl_note[128]; + if (gpl_off) snprintf(gpl_note, sizeof gpl_note, ", off by %s", gpl_off); + else gpl_note[0] = '\0'; rt_log("vk caps: gpl %s fast-link %s (graphicsPipelineLibrary %u%s)", vk_caps.gpl ? "yes" : "no", - gpl_fast ? "yes" : "no", (unsigned)gpl_lib, !gpl_lib || !gpl_fast || vk_caps.gpl ? "" : ", off by NX2NAVI_GPL=0"); + gpl_fast ? "yes" : "no", (unsigned)gpl_lib, gpl_note); rt_log("vk caps: fallbacks: float64 %s, storage buffers %s %u, subgroup pin %u on stages %#x, translator subgroup vs/fs/cs %u/%u/%u", vk_caps.float64 ? "native" : "softfloat64", vk_caps.sbuf_align ? "rounded down to" : "exact, alignment", (unsigned)l->minStorageBufferOffsetAlignment, want, (unsigned)vk_caps.sg_pin_stages, told[0], told[1], told[2]); diff --git a/runtime/vk_device.c b/runtime/vk_device.c index ec421e51..6e1384b5 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -499,7 +499,7 @@ static int create_device(void) { VkResult r = vk_dlssg_create_device(g_vk.instance, g_vk.phys, &di, &g_vk.device); free(c); if (r != VK_SUCCESS) { rt_log("vk: vkCreateDevice failed (%d)", (int)r); return 0; } - vk_dlssg_device_ready(g_vk.device); + vk_dlssg_device_ready(g_vk.phys, g_vk.device); if (vk_dlssg_dpa()) { // Streamline's, so its plugins hook every entry point the replay fetches vk_dpa = vk_dlssg_dpa(); rt_log("vk: device entry points come through Streamline's interposer (DLSS Frame Generation)"); diff --git a/runtime/vk_dlssg.c b/runtime/vk_dlssg.c index 3017248b..3851a36b 100644 --- a/runtime/vk_dlssg.c +++ b/runtime/vk_dlssg.c @@ -34,7 +34,7 @@ static struct { int wanted, loaded, active; - int fail; // DLSSG_NO_FILES or DLSSG_NO_GPU when it was wanted and does not run (vk_dlssg_status) + int fail; // DLSSG_NO_FILES or DLSSG_UNAVAILABLE when it was wanted and does not run (vk_dlssg_status) int mode, pending; // generation as the driver has it, and the frames since the host asked for the other state int shown_mult; // vk_dlssg_multiplier: the frames per game frame as last handed to the DLL (1 while off) PFN_nx2sl_frames_max frames_max; // NULL with an older DLL, which generates one frame @@ -42,6 +42,7 @@ static struct { PFN_nx2sl_instance_proc gipa; PFN_nx2sl_device_proc gdpa; PFN_nx2sl_available available; + PFN_nx2sl_log_unavailable log_unavailable; PFN_nx2sl_frame frame; PFN_nx2sl_present_marker marker; PFN_nx2sl_sleep sleep; @@ -103,6 +104,7 @@ int vk_dlssg_early(void) { S.gipa = (PFN_nx2sl_instance_proc)(void *)GetProcAddress(m, "nx2sl_instance_proc"); S.gdpa = (PFN_nx2sl_device_proc)(void *)GetProcAddress(m, "nx2sl_device_proc"); S.available = (PFN_nx2sl_available)(void *)GetProcAddress(m, "nx2sl_available"); + S.log_unavailable = (PFN_nx2sl_log_unavailable)(void *)GetProcAddress(m, "nx2sl_log_unavailable"); S.frame = (PFN_nx2sl_frame)(void *)GetProcAddress(m, "nx2sl_frame"); S.marker = (PFN_nx2sl_present_marker)(void *)GetProcAddress(m, "nx2sl_present_marker"); S.sleep = (PFN_nx2sl_sleep)(void *)GetProcAddress(m, "nx2sl_sleep"); @@ -151,10 +153,11 @@ VkResult vk_dlssg_create_device(VkInstance inst, VkPhysicalDevice pd, const VkDe return f ? f(pd, ci, NULL, out) : vkCreateDevice(pd, ci, NULL, out); } -void vk_dlssg_device_ready(VkDevice dev) { +void vk_dlssg_device_ready(VkPhysicalDevice physical, VkDevice dev) { if (S.loaded <= 0) return; if (!S.available()) { - S.fail = DLSSG_NO_GPU; + S.fail = DLSSG_UNAVAILABLE; + if (S.log_unavailable) S.log_unavailable(physical); return; } PFN_vkGetDeviceProcAddr g = S.gdpa(); diff --git a/runtime/vk_dlssg.h b/runtime/vk_dlssg.h index ac191860..8f0c91ab 100644 --- a/runtime/vk_dlssg.h +++ b/runtime/vk_dlssg.h @@ -10,7 +10,7 @@ VkResult vk_dlssg_create_instance(const VkInstanceCreateInfo *ci, VkInstance *ou VkResult vk_dlssg_enumerate(VkInstance inst, uint32_t *n, VkPhysicalDevice *pd); // through the interposer too PFN_vkVoidFunction vk_dlssg_instance_proc(VkInstance inst, const char *name); // the interposer's, or NULL VkResult vk_dlssg_create_device(VkInstance inst, VkPhysicalDevice pd, const VkDeviceCreateInfo *ci, VkDevice *out); -void vk_dlssg_device_ready(VkDevice dev); // after the device: checks DLSS-G and takes the swapchain hooks +void vk_dlssg_device_ready(VkPhysicalDevice physical, VkDevice dev); // after the device: checks DLSS-G and takes the swapchain hooks PFN_vkGetDeviceProcAddr vk_dlssg_dpa(void); // Streamline's device entry points, or NULL when it is not in use int vk_dlssg_active(void); // gfx_present.c's swapchain calls while active @@ -44,7 +44,7 @@ int vk_dlssg_multiplier_max(int *old_dll); // (the menu). frame_rate.c paces the game at the frame limit over it. Any thread. int vk_dlssg_multiplier(void); // Why DLSS-G runs or not: DLSSG_NOT_WANTED (NX2NAVI_FRAMEGEN was not dlss at start, or no DLSS or FSR 3.1 upscaling), -// DLSSG_NO_FILES (nx2navi_dlssg.dll, Streamline or its swapchain hooks missing or failing), DLSSG_NO_GPU (Streamline -// loaded, but DLSS-G does not run on this GPU or driver). -enum { DLSSG_NOT_WANTED, DLSSG_RUNS, DLSSG_NO_FILES, DLSSG_NO_GPU }; +// DLSSG_NO_FILES (nx2navi_dlssg.dll, Streamline or its swapchain hooks missing or failing), DLSSG_UNAVAILABLE (Streamline +// loaded but DLSS-G or a required plugin function was unavailable). +enum { DLSSG_NOT_WANTED, DLSSG_RUNS, DLSSG_NO_FILES, DLSSG_UNAVAILABLE }; int vk_dlssg_status(void); diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 68aa1b7d..c8a52b69 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -4018,6 +4018,12 @@ void *vk_staging(VkDeviceSize size, VkBuffer *buf, VmaAllocation *alloc) { VkBuffer b; VmaAllocation a; VkResult r = vmaCreateBuffer(c->vma, &bi, &aci, &b, &a, &info); + if (r == VK_ERROR_OUT_OF_DEVICE_MEMORY && aci.pool) { + // The staging pool can grow only in 64 MiB blocks of its one memory type. A smaller allocation may + // still fit in a default block or another host-visible type. Keep the same mapping requirements. + aci.pool = NULL; + r = vmaCreateBuffer(c->vma, &bi, &aci, &b, &a, &info); + } if (r != VK_SUCCESS) rt_fatal("vk: staging buffer of %llu bytes failed (%d)", (unsigned long long)size, (int)r); vk_texstat_add(VK_TS_STG_NEW, 1, bi.size); *buf = b; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 2cfa06f2..8f435b83 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -604,6 +604,19 @@ static void cache_load(VkCtx *c) { fseek(f, 0, SEEK_END); long len = ftell(f); fseek(f, 0, SEEK_SET); + // NX2NAVI_PIPELINE_CACHE_MAX_MB (default 512; 0 no limit): a larger file starts the run empty, and the run's + // saves replace it. The file only grows: entries of earlier translators and builds stay, and on NVIDIA + // (621.01) an entry another executable wrote is neither used nor written again, so a renamed or updated exe + // compiles while the file keeps its size (2026-11-09: 650 MB, 15-18 % of the pipelines read from it). The + // whole file sits in process memory twice at the boot (this read and the driver's copy) and is copied out + // at every save, which a 16 GB machine near its commit limit cannot spare. The size is checked before the + // read, so a file that is dropped never takes its size in memory. + uint64_t cap = (uint64_t)strtoull(cfg_value("NX2NAVI_PIPELINE_CACHE_MAX_MB"), NULL, 10) << 20; + if (len > 0 && cap && (uint64_t)len > cap) { + rt_log("vk_pipeline: %s holds %.0f MB, over NX2NAVI_PIPELINE_CACHE_MAX_MB %llu: starting empty; this run " + "rewrites it", P.cache_path, len / 1048576.0, (unsigned long long)(cap >> 20)); + len = 0; + } if (len > (long)sizeof(VkPipelineCacheHeaderVersionOne) && (data = malloc((size_t)len)) && fread(data, 1, (size_t)len, f) == (size_t)len) size = (size_t)len; @@ -615,18 +628,6 @@ static void cache_load(VkCtx *c) { rt_log("vk_pipeline: %s was written by another device or driver; starting empty", P.cache_path); size = 0; } - // NX2NAVI_PIPELINE_CACHE_MAX_MB (default 512; 0 no limit): a larger file starts the run empty, and the run's - // saves replace it. The file only grows: entries of earlier translators and builds stay, and on NVIDIA - // (621.01) an entry another executable wrote is neither used nor written again, so a renamed or updated exe - // compiles while the file keeps its size (2026-11-09: 650 MB, 15-18 % of the pipelines read from it). The - // whole file sits in process memory twice at the boot (this read and the driver's copy) and is copied out - // at every save, which a 16 GB machine near its commit limit cannot spare. - uint64_t cap = (uint64_t)strtoull(cfg_value("NX2NAVI_PIPELINE_CACHE_MAX_MB"), NULL, 10) << 20; - if (size && cap && size > cap) { - rt_log("vk_pipeline: %s holds %.0f MB, over NX2NAVI_PIPELINE_CACHE_MAX_MB %llu: starting empty; this run " - "rewrites it", P.cache_path, size / 1048576.0, (unsigned long long)(cap >> 20)); - size = 0; - } } VkPipelineCacheCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, .initialDataSize = size, .pInitialData = size ? data : NULL}; diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index 1fa32e43..6d122f6c 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -726,6 +726,10 @@ static int fence_wait_impl(uint64_t value, uint64_t timeout_ns) { // CPU side effects of its fence may still be to come. 1 when reached. int vk_sync_exec_wait(uint64_t value, uint64_t timeout_ns) { vk_sync_start(); + // With shadow storage (vk_mem_shadow_storage) what the GPU wrote reaches guest memory in the completion thread's + // fence callback, after the executed timeline: a wait that returned there let the game rewrite a pool that an + // older write-back then overwrote (Kokiri Forest: a sky coefficient the game had reset came back, and the world + // flickered washed out). So the wait covers the write-backs there, as without the split. if (!vk_sync_wb_split()) return vk_sync_fence_wait(value, timeout_ns); if (__atomic_load_n(&done, __ATOMIC_ACQUIRE) >= value) return 1; uint64_t p0 = perf_begin(); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 22c5a6f4..4e5622e7 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1550,6 +1550,11 @@ static int idle(const Tex *e, uint64_t now); // figure decides there, as before. On Linux the process's figure is its resident anonymous memory, which cannot pass // the installed memory, so only a bound set below that holds there; a cgroup's limit and the machine's RAM act through // the system's figure. +// Where a discrete GPU's memory counts against the commit (plat_commit_counts_vram: Windows), the bound compares the +// commit less the device-local memory the driver reports in use (vk_mem_vram): that memory is on the card, not in RAM +// or the page file, and the VRAM pressure and tex_disuse already bound it. Counted in, it held the bound all through +// Hyrule Field on a 16 GB PC with a 16 GB card (RTX 5060 Ti): the process at 28.9 to 30.7 GB, 9 to 10 GB of it video +// memory, while the system had 8 to 10 GB of its 49 GB limit free, and the ground flickered white as textures gave way. #define COMMIT_LOW_KEEP ((uint64_t)256 << 20) #define PROC_COMMIT_MIN_MB 24576 static uint64_t released_cap; // bytes; 0 = not read yet, UINT64_MAX = no limit @@ -1622,12 +1627,21 @@ static int commit_is_low(void) { double want = commit_free_mb > 0 ? (double)commit_free_mb : limit / 10 > 4096 ? limit / 10 : 4096; low = limit - used < want; } - int high = proc_commit_mb && proc > (double)(proc_high ? proc_commit_mb - proc_commit_mb / 32 : proc_commit_mb); + double dev = 0; // device-local memory in use, MB: in the commit (plat_commit_counts_vram), not in RAM (see above) + VkCtx *c = vk_ctx(); + uint64_t dev_used, dev_budget; + int exact; + if (proc_commit_mb && plat_commit_counts_vram() && c && + c->props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && vk_mem_vram(&dev_used, &dev_budget, &exact)) + dev = dev_used / 1048576.0 < proc ? dev_used / 1048576.0 : proc; + int high = proc_commit_mb && + proc - dev > (double)(proc_high ? proc_commit_mb - proc_commit_mb / 32 : proc_commit_mb); if (high != proc_high && commit_logs < 40) { commit_logs++; - rt_log("vk: this process's commit %s: %.0f MB, the bound is %lld MB (system: %.0f of %.0f MB in use); released " - "textures kept %d, %.0f MiB of own memory%s", high ? "high" : "no longer high", proc, proc_commit_mb, - used, limit, nreleased, rel_bytes / 1048576.0, high ? ": dropping the stale ones" : ""); + rt_log("vk: this process's commit %s: %.0f MB less %.0f MB of video memory, the bound is %lld MB (system: %.0f " + "of %.0f MB in use); released textures kept %d, %.0f MiB of own memory%s", high ? "high" : "no longer high", + proc, dev, proc_commit_mb, used, limit, nreleased, rel_bytes / 1048576.0, + high ? ": dropping the stale ones" : ""); } proc_high = high; if (low != sys_low && commit_logs < 40) { // a few lines, however often it flips @@ -1635,6 +1649,16 @@ static int commit_is_low(void) { rt_log("vk: system commit %s: %.0f of %.0f MB in use (this process %.0f MB); released textures kept %d, %.0f MiB " "of own memory%s", low ? "low" : "no longer low", used, limit, proc, nreleased, rel_bytes / 1048576.0, low ? ": dropping the stale ones" : ""); + // A page file Windows sizes grows only slowly and by little, so the limit stays near the installed memory plus + // what the page file held at the boot: on a 16 GB PC with a 16 GB card it ran low in Hyrule Field at 32 GB, + // and textures gave way (white or black ground). A fixed page file of 32 GB or more raised the limit to 49 GB. + static int hinted; + if (low && !hinted && plat_page_file_managed() == 1) { + hinted = 1; + rt_log("vk: the page file is sized by Windows, which grows it only slowly: a fixed page file of at least " + "32 GB keeps the system's commit from running low (System > About > Advanced system settings > " + "Performance Settings > Advanced > Virtual memory > Change: Custom size, then restart Windows)"); + } } sys_low = low; return commit_low = low || high; diff --git a/tests/action_prompt_check.c b/tests/action_prompt_check.c new file mode 100644 index 00000000..e4cae8f6 --- /dev/null +++ b/tests/action_prompt_check.c @@ -0,0 +1,596 @@ +// runtime/action_prompt.c: the action prompt's diamond of face buttons laid out by label. The layout file here is made +// by the test from the format as action_prompt.c's header comment describes it, with the values the game's 1.1.1 +// DoCommand_00.buil holds (A right, B bottom, X top, Y left), so the reader is checked against a writer of its own; +// the archives and zstd dictionaries are made here too. No game files. Given romfs.bin and the offsets and sizes of +// the layout archive and Pack/ZsDic.pack.zs in it, it also checks the game's own archive (game_archive). Built and +// run by tests/action_prompt_check.py; prints each failed check and exits 1 on any. +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "action_prompt.h" +#include "sarc.h" + +static int fails; +#define CHECK(c, ...) \ + do { \ + if (!(c)) { \ + fails++; \ + printf("FAIL line %d: ", __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } \ + } while (0) + +// ---- what action_prompt.c reaches in the runtime +static char last_log[512]; +void rt_log(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vsnprintf(last_log, sizeof last_log, fmt, ap); + va_end(ap); +} +uint64_t plat_now_ns(void) { return (uint64_t)((double)clock() / CLOCKS_PER_SEC * 1e9); } + +// ---- a BFFH layout as the game's DoCommand_00.buil has it +enum { SIZE = 0x8000, PANE_REC = 0x44 }; +static uint8_t file[SIZE]; +static size_t top; // the next free byte + +static uint64_t alloc(size_t n) { + top = (top + 7) & ~(size_t)7; + uint64_t at = top; + top += n; + if (top > SIZE) { + printf("the test's layout is too small\n"); + exit(2); + } + return at; +} +static void w32(uint64_t at, uint32_t v) { memcpy(file + at, &v, 4); } +static void w64(uint64_t at, uint64_t v) { memcpy(file + at, &v, 8); } +static void wf(uint64_t at, float v) { memcpy(file + at, &v, 4); } +static void set_f(uint8_t *p, uint64_t at, float v) { memcpy(p + at, &v, 4); } +static float rf(const uint8_t *p, uint64_t at) { + float v; + memcpy(&v, p + at, 4); + return v; +} +static uint32_t r32(const uint8_t *p, uint64_t at) { + uint32_t v; + memcpy(&v, p + at, 4); + return v; +} +static uint64_t r64(const uint8_t *p, uint64_t at) { + uint64_t v; + memcpy(&v, p + at, 8); + return v; +} +static uint64_t str(const char *s) { + uint64_t at = alloc(strlen(s) + 1); + memcpy(file + at, s, strlen(s) + 1); + return at; +} + +#define Z (-2.842171e-14f) // the game stores some zeros so +static const struct { + const char *name; + float x, y, sx, sy; + uint32_t flags; +} panes[] = { + {"RootPane", 0, 0, 1, 1, 0x02555000}, + {"P_BaseA_00", 0, 0, 1, 1, 0x02555089}, {"P_B_00", 0, -80, 1, 1, 0x0255508b}, {"P_X_00", 0, 80, 1, 1, 0x0255508b}, + {"P_Y_00", -80, 0, 1, 1, 0x0255508b}, {"N_BtnA_00", 80, 0, 1, 1, 0x0255500b}, + {"P_BaseB_00", Z, 0, 1, 1, 0x02555089}, {"P_A_00", 80, 0, 1, 1, 0x0255508b}, {"P_X_02", Z, 80, 1, 1, 0x0255508b}, + {"P_Y_02", -80, 0, 1, 1, 0x0255508b}, {"N_BtnB_00", 0, -80, 1, 1, 0x0255500b}, + {"P_BaseX_00", Z, 0, 1, -1, 0x02555089}, {"P_A_02", 80, 0, 1, 1, 0x0255508b}, {"P_B_03", Z, -80, 1, 1, 0x0255508b}, + {"P_Y_04", -80, 0, 1, 1, 0x0255508b}, {"N_BtnX_00", 0, 80, 1, 1, 0x0255500b}, + {"W_BaseX_00", -80, 0, 1, 1, 0x02554889}, {"W_BaseXDecide_00", -80, 0, 1, 1, 0x02554889}, + {"T_GuideX_00", 64, 0, 1, 1, 0x02554909}, + {"P_BaseY_00", Z, 0, -1, 1, 0x02555089}, {"P_A_01", 80, 0, 1, 1, 0x0255508b}, {"P_B_02", Z, -80, 1, 1, 0x0255508b}, + {"P_X_03", Z, 80, 1, 1, 0x0255508b}, {"N_BtnY_00", -80, 0, 1, 1, 0x0255500b}, + {"W_BaseY_00", 80, 0, -1, 1, 0x02554889}, {"W_BaseYDecide_00", 80, 0, -1, 1, 0x02554889}, + {"T_GuideY_00", -68, 0, 1, 1, 0x02555909}, + // left as they are: A's and L's bubbles, A's Decide null + {"W_BaseA_00", -80, 0, 1, 1, 0x02554889}, {"N_BaseADecide_00", -80, 0, 1, 1, 0x02555002}, + {"T_GuideL_00", 66, 0, 1, 1, 0x02554909}, +}; +enum { NPANES = sizeof panes / sizeof *panes }; +static uint64_t pane_at[NPANES]; +static const char *const materials[] = {"P_BaseA_00", "P_BaseB_00", "P_BaseX_00", "P_BaseY_00", "P_B_00"}; +static const char *const textures[][2] = {{"DoCommandCrossBase_A_00", "DoCommandCrossBaseGrd_A_00"}, + {"DoCommandCrossBase_B_00", "DoCommandCrossBaseGrd_B_00"}, + {"DoCommandCrossBase_B_00", "DoCommandCrossBaseGrd_B_00"}, + {"DoCommandCrossBase_A_00", "DoCommandCrossBaseGrd_A_00"}, + {"DoCommandSubBase_00", "DoCommandSubBaseGrd_00"}}; +enum { NMATS = sizeof materials / sizeof *materials }; +static uint64_t binding_at[NMATS][2]; // where each material's first two texture name offsets are +static uint64_t name_a[2], name_b[2]; // the offsets of the A and B pictures' names + +static size_t make_layout(void) { + memset(file, 0, sizeof file); + top = 0x240; + memcpy(file, "BFFH", 4); + // the names first, A's and B's pictures shared as the game shares them + name_a[0] = str(textures[0][0]), name_a[1] = str(textures[0][1]); + name_b[0] = str(textures[1][0]), name_b[1] = str(textures[1][1]); + // panes + uint64_t names = alloc(8 * NPANES), records = alloc(8 * NPANES); + for (int i = 0; i < NPANES; i++) { + uint64_t r = alloc(PANE_REC); + for (int k = 0; k < PANE_REC; k += 4) w32(r + k, 0xa5a50000u + (uint32_t)(i << 8) + (uint32_t)k); + wf(r + 0x10, panes[i].sx), wf(r + 0x14, panes[i].sy), wf(r + 0x1c, panes[i].x), wf(r + 0x20, panes[i].y); + w32(r + 0x38, panes[i].flags); + w64(names + 8 * i, str(panes[i].name)), w64(records + 8 * i, r); + pane_at[i] = r; + } + w64(0x118, names), w64(0x120, records), w32(0x130, NPANES), w32(0x134, 8); + // materials and their texture blocks + uint64_t mnames = alloc(8 * NMATS), mrecords = alloc(8 * NMATS), blocks = alloc(8 * NMATS); + for (int i = 0; i < NMATS; i++) { + uint64_t m = alloc(0x40), block = alloc(0x40), bindings = alloc(3 * 8); + w32(m + 0x14, (uint32_t)i); + w64(block, bindings), w32(block + 8, 3), w32(block + 12, 8); + for (int k = 0; k < 3; k++) { + uint64_t b = alloc(16); + w32(b, 0x01010202); + uint64_t s = k == 2 ? str("DecoNoise_00") + : i < 4 ? ((i == 0 || i == 3) ? name_a[k] : name_b[k]) + : str(textures[i][k]); + w64(b + 8, s); + w64(bindings + 8 * k, b); + if (k < 2) binding_at[i][k] = b + 8; + } + w64(mnames + 8 * i, str(materials[i])), w64(mrecords + 8 * i, m), w64(blocks + 8 * i, block); + } + w64(0x138, mnames), w64(0x140, mrecords), w32(0x150, NMATS), w32(0x154, 8); + w64(0x228, blocks), w32(0x230, NMATS), w32(0x234, 8); + return top; +} + +static int pane(const char *name) { + for (int i = 0; i < NPANES; i++) + if (!strcmp(panes[i].name, name)) return i; + printf("no pane %s in the test's table\n", name); + exit(2); +} +static float px(const uint8_t *p, const char *name, int off) { return rf(p, pane_at[pane(name)] + off); } + +// 1 when a differing byte at `at` is one the layout by label may change. +static int may_change(uint64_t at) { + static const char *const moved[] = {"P_B_00", "P_X_00", "P_Y_00", "N_BtnA_00", "P_A_00", "P_X_02", "P_Y_02", + "N_BtnB_00", "P_A_02", "P_B_03", "P_Y_04", "N_BtnX_00", "P_A_01", "P_B_02", + "P_X_03", "N_BtnY_00", "P_BaseX_00", "P_BaseY_00", "W_BaseX_00", + "W_BaseXDecide_00", "T_GuideX_00", "W_BaseY_00", "W_BaseYDecide_00", + "T_GuideY_00"}; + for (size_t i = 0; i < sizeof moved / sizeof *moved; i++) { + uint64_t r = pane_at[pane(moved[i])]; + if ((at >= r + 0x10 && at < r + 0x18) || (at >= r + 0x1c && at < r + 0x24) || (at >= r + 0x38 && at < r + 0x3c)) + return 1; + } + for (int i = 0; i < 4; i++) + for (int k = 0; k < 2; k++) + if (at >= binding_at[i][k] && at < binding_at[i][k] + 8) return 1; + return 0; +} + +// ---- a SARC: one other member, then the given layouts in order, each named `name` +enum { MAX_COPIES = 17 }; +static size_t member_at[MAX_COPIES]; // where each layout copy starts in the last archive made + +static size_t make_sarc(uint8_t *out, size_t cap, const uint8_t *const *layouts, int copies, size_t n, + const char *name) { + static const char other[] = "Other_00.buil"; + static const uint8_t other_data[64] = {1, 2, 3, 4, 5, 6, 7, 8}; + int count = 1 + copies; + size_t name_len = (strlen(name) + 1 + 3) & ~(size_t)3, other_len = (sizeof other + 3) & ~(size_t)3; + size_t sfat = 0x14, sfnt = sfat + 0xc + 16 * (size_t)count; + size_t data = (sfnt + 8 + other_len + name_len * copies + 0xff) & ~(size_t)0xff; + size_t total = (data + sizeof other_data + 0xff) & ~(size_t)0xff; + for (int c = 0; c < copies; c++) member_at[c] = total, total = (total + n + 0xff) & ~(size_t)0xff; + if (total > cap) return 0; + memset(out, 0, total); + memcpy(out, "SARC", 4); + uint16_t hl = 0x14, bom = 0xfeff, v = 0x100, fl = 0xc, cnt = (uint16_t)count, eight = 8; + uint32_t t32 = (uint32_t)total, d32 = (uint32_t)data, key = 0x65; + memcpy(out + 4, &hl, 2), memcpy(out + 6, &bom, 2); + memcpy(out + 8, &t32, 4), memcpy(out + 12, &d32, 4), memcpy(out + 16, &v, 2); + memcpy(out + sfat, "SFAT", 4), memcpy(out + sfat + 4, &fl, 2), memcpy(out + sfat + 6, &cnt, 2); + memcpy(out + sfat + 8, &key, 4); + memcpy(out + sfnt, "SFNT", 4), memcpy(out + sfnt + 4, &eight, 2); + size_t names = sfnt + 8; + memcpy(out + names, other, sizeof other); + uint32_t node[4] = {1, 0x01000000u, 0, (uint32_t)sizeof other_data}; + memcpy(out + sfat + 0xc, node, sizeof node); + memcpy(out + data, other_data, sizeof other_data); + for (int c = 0; c < copies; c++) { + size_t s = names + other_len + name_len * c; + memcpy(out + s, name, strlen(name) + 1); + uint32_t e[4] = {2, 0x01000000u | (uint32_t)((s - names) / 4), (uint32_t)(member_at[c] - data), + (uint32_t)(member_at[c] - data + n)}; + memcpy(out + sfat + 0xc + 16 * (size_t)(c + 1), e, sizeof e); + memcpy(out + member_at[c], layouts[c], n); + } + return total; +} + +// Points copy c of an archive make_sarc made (its SFAT entry c + 1) at s[at..at + size). +static void set_range(uint8_t *s, int c, size_t at, size_t size) { + uint32_t data, lo, hi; + memcpy(&data, s + 12, 4); + lo = (uint32_t)(at - data), hi = (uint32_t)(at + size - data); + memcpy(s + 0x20 + 16 * (size_t)(c + 1) + 8, &lo, 4); + memcpy(s + 0x20 + 16 * (size_t)(c + 1) + 12, &hi, 4); +} + +// A zstd dictionary with the id `id`, its content and samples taken from src[0..n); its size, 0 when it cannot be made. +static size_t make_dict(uint8_t *out, size_t cap, unsigned id, const uint8_t *src, size_t n) { + enum { SAMPLES = 32 }; + size_t sizes[SAMPLES]; + for (int i = 0; i < SAMPLES; i++) sizes[i] = n / SAMPLES; + ZDICT_params_t params = {3, 0, id}; + size_t r = ZDICT_finalizeDictionary(out, cap, src, n < 2048 ? n : 2048, src, sizes, SAMPLES, params); + return ZDICT_isError(r) ? 0 : r; +} + +// ---- the game's own archive, read from romfs.bin +static uint8_t *read_part(const char *path, const char *off, const char *size, size_t *n) { + long long at = strtoll(off, NULL, 10); + *n = (size_t)strtoull(size, NULL, 10); + FILE *f = fopen(path, "rb"); + uint8_t *p = f && *n ? malloc(*n) : NULL; +#ifdef _WIN32 + int ok = p && !_fseeki64(f, at, SEEK_SET) && fread(p, 1, *n, f) == *n; +#else + int ok = p && !fseeko(f, (off_t)at, SEEK_SET) && fread(p, 1, *n, f) == *n; +#endif + if (f) fclose(f); + if (!ok) free(p); + return ok ? p : NULL; +} + +// The .zs file z[0..zn) unpacked with the dictionary it names from the unpacked dictionaries dics[0..dn); NULL when it +// does not unpack. +static uint8_t *unpack(const void *z, size_t zn, const uint8_t *dics, size_t dn, size_t *n) { + unsigned id = ZSTD_getDictID_fromFrame(z, zn); + unsigned long long rn = ZSTD_getFrameContentSize(z, zn); + size_t at = 0, size = 0; + if (rn == ZSTD_CONTENTSIZE_UNKNOWN || rn == ZSTD_CONTENTSIZE_ERROR || (id && !zsdic_find(dics, dn, id, &at, &size))) + return NULL; + uint8_t *raw = malloc((size_t)rn); + ZSTD_DCtx *d = ZSTD_createDCtx(); + size_t got = !raw || !d ? 0 + : id ? ZSTD_decompress_usingDict(d, raw, (size_t)rn, z, zn, dics + at, size) + : ZSTD_decompressDCtx(d, raw, (size_t)rn, z, zn); + ZSTD_freeDCtx(d); + if (ZSTD_isError(got) || got != rn) { + free(raw); + return NULL; + } + *n = (size_t)rn; + return raw; +} + +// By label: made once, with the game's dictionary and unpacked size, and byte for byte the archive laid out by label, +// which differs from the game's only inside its four copies of the layout; then nothing more to make by label, nothing +// to make by position on the game's own, and by position again on the one by label. +static void game_archive(char **argv) { + size_t an, dn, pn = 0, rn = 0, on = 0, bn = 0; + uint8_t *a = read_part(argv[1], argv[2], argv[3], &an), *d = read_part(argv[1], argv[4], argv[5], &dn); + uint8_t *dics = d ? zsdic_unpack(d, dn, 64u << 20, &pn) : NULL; + uint8_t *raw = a && dics ? unpack(a, an, dics, pn, &rn) : NULL; + CHECK(raw != NULL, "game: cannot read and unpack the layout archive from %s", argv[1]); + if (!raw) { + free(dics), free(a), free(d); + return; + } + size_t at[16], size[16]; + unsigned next = 0; + int copies = 0; + while (copies < 16 && sarc_next(raw, rn, "DoCommand_00.buil", &next, &at[copies], &size[copies]) == 1) copies++; + char why[200] = ""; + uint8_t *expect = malloc(rn), *expect_back = malloc(rn); + memcpy(expect, raw, rn); + int fields = action_prompt_archive(expect, rn, 1, why, sizeof why); + CHECK(copies == 4 && fields == 4 * 52, "game: %d copies of the layout, %d fields changed (%s)", copies, fields, why); + memcpy(expect_back, expect, rn); + action_prompt_archive(expect_back, rn, 0, why, sizeof why); + + void *out = NULL, *again = NULL; + size_t out_n = 0, again_n = 0; + int made = action_prompt_build(a, an, d, dn, 1, &out, &out_n); + CHECK(made == 1, "game: build by label %d (%s)", made, last_log); + if (made == 1) { + printf("game: %s, %zu bytes packed (the game's %zu)\n", last_log, out_n, an); + CHECK(ZSTD_getDictID_fromFrame(out, out_n) == ZSTD_getDictID_fromFrame(a, an) && + ZSTD_getFrameContentSize(out, out_n) == rn, + "game: the built frame names another dictionary or size"); + uint8_t *got = unpack(out, out_n, dics, pn, &on); + CHECK(got && on == rn && !memcmp(got, expect, rn), "game: the built archive is not the archive laid out by label"); + size_t outside = 0; + for (size_t i = 0; got && on == rn && i < rn; i++) { + if (got[i] == raw[i]) continue; + int inside = 0; + for (int c = 0; c < copies; c++) inside |= i >= at[c] && i < at[c] + size[c]; + outside += !inside; + } + CHECK(!outside, "game: %zu bytes changed outside the layout's copies", outside); + CHECK(action_prompt_build(out, out_n, d, dn, 1, &again, &again_n) == 0 && !again, "game: second build: %s", + last_log); + made = action_prompt_build(out, out_n, d, dn, 0, &again, &again_n); + uint8_t *back = made == 1 ? unpack(again, again_n, dics, pn, &bn) : NULL; + CHECK(back && bn == rn && !memcmp(back, expect_back, rn), "game: by position again: %d (%s)", made, last_log); + free(back); + free(again); + free(got); + } + again = NULL; + CHECK(action_prompt_build(a, an, d, dn, 0, &again, &again_n) == 0 && !again, + "game: build by position on the game's archive: %s", last_log); + free(out); + free(expect); + free(expect_back); + free(raw); + free(dics); + free(a); + free(d); +} + +int main(int argc, char **argv) { + char why[200]; + size_t n = make_layout(); + static uint8_t before[SIZE], p[SIZE]; + memcpy(before, file, n); + + // By label: every place, cross, flip and bubble the game's diamond needs, and nothing else. + memcpy(p, before, n); + int changed = action_prompt_lay_out(p, n, 1, why, sizeof why); + CHECK(changed == 52, "fields changed by label: %d (%s)", changed, changed < 0 ? why : ""); + static const struct { const char *pane; float x, y; } places[] = { + {"N_BtnA_00", 0, -80}, {"N_BtnB_00", 80, 0}, {"N_BtnX_00", -80, 0}, {"N_BtnY_00", 0, 80}, + {"P_B_00", 80, 0}, {"P_X_00", -80, 0}, {"P_Y_00", 0, 80}, {"P_A_00", 0, -80}, + {"P_X_02", -80, 0}, {"P_Y_02", 0, 80}, {"P_A_02", 0, -80}, {"P_B_03", 80, 0}, + {"P_Y_04", 0, 80}, {"P_A_01", 0, -80}, {"P_B_02", 80, 0}, {"P_X_03", -80, 0}}; + for (size_t i = 0; i < sizeof places / sizeof *places; i++) + CHECK(px(p, places[i].pane, 0x1c) == places[i].x && px(p, places[i].pane, 0x20) == places[i].y, + "%s at %g %g, expected %g %g", places[i].pane, px(p, places[i].pane, 0x1c), px(p, places[i].pane, 0x20), + places[i].x, places[i].y); + // The crosses: A lit down (picture B), B right (A), X left (A flipped in x), Y up (B flipped in y). + CHECK(r64(p, binding_at[0][0]) == name_b[0] && r64(p, binding_at[0][1]) == name_b[1], "A's cross is not picture B"); + CHECK(r64(p, binding_at[1][0]) == name_a[0] && r64(p, binding_at[1][1]) == name_a[1], "B's cross is not picture A"); + CHECK(r64(p, binding_at[2][0]) == name_a[0] && r64(p, binding_at[2][1]) == name_a[1], "X's cross is not picture A"); + CHECK(r64(p, binding_at[3][0]) == name_b[0] && r64(p, binding_at[3][1]) == name_b[1], "Y's cross is not picture B"); + CHECK(px(p, "P_BaseA_00", 0x10) == 1 && px(p, "P_BaseA_00", 0x14) == 1, "A's cross flipped"); + CHECK(px(p, "P_BaseX_00", 0x10) == -1 && px(p, "P_BaseX_00", 0x14) == 1, "X's cross not flipped in x only"); + CHECK(px(p, "P_BaseY_00", 0x10) == 1 && px(p, "P_BaseY_00", 0x14) == -1, "Y's cross not flipped in y only"); + // X's bubble opens to the left, Y's to the right; the text origin follows. + CHECK(px(p, "W_BaseX_00", 0x10) == -1 && px(p, "W_BaseX_00", 0x1c) == 80, "X's bubble"); + CHECK(px(p, "W_BaseXDecide_00", 0x10) == -1 && px(p, "W_BaseXDecide_00", 0x1c) == 80, "X's Decide bubble"); + CHECK(px(p, "T_GuideX_00", 0x1c) == -68 && r32(p, pane_at[pane("T_GuideX_00")] + 0x38) == 0x02555909, "X's name"); + CHECK(px(p, "W_BaseY_00", 0x10) == 1 && px(p, "W_BaseY_00", 0x1c) == -80, "Y's bubble"); + CHECK(px(p, "W_BaseYDecide_00", 0x10) == 1 && px(p, "W_BaseYDecide_00", 0x1c) == -80, "Y's Decide bubble"); + CHECK(px(p, "T_GuideY_00", 0x1c) == 64 && r32(p, pane_at[pane("T_GuideY_00")] + 0x38) == 0x02554909, "Y's name"); + int stray = 0; + for (size_t i = 0; i < n; i++) + if (p[i] != before[i] && !may_change(i)) stray++; + CHECK(!stray, "%d bytes changed outside the diamond's fields", stray); + + // Laid out by label already: nothing changes, and it says so. + uint8_t *again = malloc(n); + memcpy(again, p, n); + CHECK(action_prompt_lay_out(again, n, 1, why, sizeof why) == 0 && !memcmp(again, p, n), "a second run changed it"); + // And back by position: the game's file again, its stored -2.8e-14 zeros now plain zeros. + changed = action_prompt_lay_out(again, n, 0, why, sizeof why); + CHECK(changed == 52, "fields changed back by position: %d (%s)", changed, changed < 0 ? why : ""); + uint8_t *expect = malloc(n); + memcpy(expect, before, n); + for (int i = 0; i < NPANES; i++) + if (panes[i].x == Z && strncmp(panes[i].name, "P_Base", 6)) set_f(expect, pane_at[i] + 0x1c, 0.0f); + CHECK(!memcmp(again, expect, n), "by label and back is not the game's layout"); + + // A file that is not the expected one is refused before anything is written. + struct { const char *pane; int off; float v; } wrong[] = { + {"P_Y_04", 0x20, 1.0f}, {"N_BtnA_00", 0x20, -80.0f}, {"P_BaseY_00", 0x10, 1.0f}, {"T_GuideY_00", 0x1c, -60.0f}}; + for (size_t i = 0; i < sizeof wrong / sizeof *wrong; i++) { + memcpy(p, before, n); + memcpy(p + pane_at[pane(wrong[i].pane)] + wrong[i].off, &wrong[i].v, 4); + memcpy(again, p, n); + int r = action_prompt_lay_out(p, n, 1, why, sizeof why); + CHECK(r == -1 && !memcmp(p, again, n) && strstr(why, wrong[i].pane), "%s changed: %d, %s", wrong[i].pane, r, why); + } + // Half laid out (A moved, the rest not): refused, untouched. + memcpy(p, before, n); + set_f(p, pane_at[pane("N_BtnA_00")] + 0x1c, 0), set_f(p, pane_at[pane("N_BtnA_00")] + 0x20, -80); + memcpy(again, p, n); + CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && !memcmp(p, again, n), "half laid out: %s", why); + // A cross with another picture, a missing pane, a missing material: refused. + memcpy(p, before, n); + memcpy(p + binding_at[0][0], &name_b[0], 8); + CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && strstr(why, "P_BaseA_00"), "cross picture: %s", why); + memcpy(p, before, n); + uint64_t s = r64(p, r64(p, 0x118) + 8 * (uint64_t)pane("P_X_03")); + p[s] = 'Q'; + CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && strstr(why, "P_X_03"), "missing pane: %s", why); + memcpy(p, before, n); + memset(p + 0x150, 0, 4); // no materials + CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && strstr(why, "P_BaseA_00"), "no materials: %s", why); + + // Offsets out of the file, a cut file and a wrong magic: refused, never read past the end. + memcpy(p, before, n); + uint64_t far = (uint64_t)n + 100; + memcpy(p + 0x118, &far, 8); + CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1, "pane names past the end"); + memcpy(p, before, n); + far = UINT64_MAX - 4; + memcpy(p + 0x120, &far, 8); + CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1, "pane records at 2^64"); + memcpy(p, before, n); + CHECK(action_prompt_lay_out(p, 0x200, 1, why, sizeof why) == -1, "a cut file"); + CHECK(action_prompt_lay_out(p, 3, 1, why, sizeof why) == -1, "a 3-byte file"); + p[0] = 'X'; + CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && strstr(why, "BFFH"), "wrong magic: %s", why); + // A label other than 1 counts as by label. + memcpy(p, before, n); + CHECK(action_prompt_lay_out(p, n, 2, why, sizeof why) == 52 && px(p, "N_BtnA_00", 0x20) == -80, "label 2: %s", why); + // Both bubbles of X on one record, their values the same in both layouts: laid out, the record moved once. + memcpy(p, before, n); + uint64_t shared = pane_at[pane("W_BaseX_00")]; + memcpy(p + r64(p, 0x120) + 8 * (uint64_t)pane("W_BaseXDecide_00"), &shared, 8); + changed = action_prompt_lay_out(p, n, 1, why, sizeof why); + CHECK(changed == 52 && px(p, "W_BaseX_00", 0x10) == -1 && px(p, "W_BaseX_00", 0x1c) == 80, + "bubbles on one record: %d (%s)", changed, why); + // P_B_00's record laid over P_BaseA_00's material, its x on the material's block index (0 as x is 0.0): the file + // reads as expected, but moving the dot would lose A's cross. Refused, and every byte as it was. + memcpy(p, before, n); + uint64_t mat = r64(p, r64(p, 0x140)), over = mat + 0x14 - 0x1c; + memcpy(p + r64(p, 0x120) + 8 * (uint64_t)pane("P_B_00"), &over, 8); + set_f(p, mat + 0x18, -80.0f); + memcpy(again, p, n); + changed = action_prompt_lay_out(p, n, 1, why, sizeof why); + CHECK(changed == -1 && !memcmp(p, again, n) && strstr(why, "overlaps"), "a pane on a material: %d, %s", changed, + why); + // Random damage, mostly to the header's tables: a refusal or a layout, never a crash. Each copy is an allocation + // of its own, so that AddressSanitizer, where the runner has it, sees a read past its end. + srand(1); + for (int t = 0; t < 20000; t++) { + uint8_t *q = malloc(n); + memcpy(q, before, n); + for (int k = 0; k < 1 + rand() % 4; k++) { + size_t at = rand() % 2 ? 0x110 + rand() % 0x130 : rand() % n; + q[at] ^= (uint8_t)(1 << rand() % 8); + } + action_prompt_lay_out(q, n, 1, why, sizeof why); + free(q); + } + + // The archive, holding the layout four times as the game's does: every copy changes, nothing else; one copy + // that is not the expected layout leaves all of them as they were; without the layout, refused. + static uint8_t sarc[SIZE * 20], sarc2[SIZE * 20]; + const uint8_t *four[MAX_COPIES] = {before, before, before, before}; + size_t sn = make_sarc(sarc, sizeof sarc, four, 4, n, "DoCommand_00.buil"); + memcpy(sarc2, sarc, sn); + changed = action_prompt_archive(sarc2, sn, 1, why, sizeof why); + memcpy(p, before, n); + action_prompt_lay_out(p, n, 1, why, sizeof why); + CHECK(changed == 4 * 52 && !memcmp(sarc2, sarc, member_at[0]), "archive: %d", changed); + for (int c = 0; c < 4; c++) CHECK(!memcmp(sarc2 + member_at[c], p, n), "copy %d not laid out by label", c); + // Two copies laid out already and two not (an archive half laid out by hand): the other two are laid out. + const uint8_t *mixed[MAX_COPIES] = {p, before, p, before}; + sn = make_sarc(sarc2, sizeof sarc2, mixed, 4, n, "DoCommand_00.buil"); + changed = action_prompt_archive(sarc2, sn, 1, why, sizeof why); + CHECK(changed == 2 * 52 && !memcmp(sarc2 + member_at[1], p, n) && !memcmp(sarc2 + member_at[3], p, n), + "archive half laid out: %d (%s)", changed, why); + memcpy(p, before, n); + set_f(p, pane_at[pane("P_Y_04")] + 0x20, 1.0f); + const uint8_t *one_off[MAX_COPIES] = {before, p, before, before}; + sn = make_sarc(sarc2, sizeof sarc2, one_off, 4, n, "DoCommand_00.buil"); + memcpy(sarc, sarc2, sn); + CHECK(action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && !memcmp(sarc, sarc2, sn) && + strstr(why, "P_Y_04"), + "archive with one copy not as expected: %s", why); + sn = make_sarc(sarc2, sizeof sarc2, four, 1, n, "DoCommand_01.buil"); + CHECK(action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && strstr(why, "DoCommand_00.buil"), + "archive without the layout: %s", why); + // More copies than it lays out: refused, not the first 16 laid out. + const uint8_t *many[MAX_COPIES]; + for (int c = 0; c < MAX_COPIES; c++) many[c] = before; + sn = make_sarc(sarc2, sizeof sarc2, many, MAX_COPIES, n, "DoCommand_00.buil"); + memcpy(sarc, sarc2, sn); + CHECK(sn && action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && !memcmp(sarc, sarc2, sn) && + strstr(why, "more than"), + "archive with %d copies: %s", MAX_COPIES, why); + // Copies that share their bytes are laid out once; copies that share some of them, refused. + sn = make_sarc(sarc2, sizeof sarc2, four, 4, n, "DoCommand_00.buil"); + set_range(sarc2, 1, member_at[0], n); + changed = action_prompt_archive(sarc2, sn, 1, why, sizeof why); + CHECK(changed == 3 * 52, "archive with two copies on the same bytes: %d (%s)", changed, why); + sn = make_sarc(sarc2, sizeof sarc2, four, 4, n, "DoCommand_00.buil"); + set_range(sarc2, 1, member_at[0] + 0x100, n); + memcpy(sarc, sarc2, sn); + CHECK(action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && !memcmp(sarc, sarc2, sn) && + strstr(why, "overlap"), + "archive with overlapping copies: %s", why); + // A copy that runs past the archive's end: damaged, refused. + sn = make_sarc(sarc2, sizeof sarc2, four, 4, n, "DoCommand_00.buil"); + set_range(sarc2, 2, member_at[2], sn); + memcpy(sarc, sarc2, sn); + CHECK(action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && !memcmp(sarc, sarc2, sn) && + strstr(why, "damaged"), + "archive with a copy past its end: %s", why); + sn = make_sarc(sarc, sizeof sarc, four, 4, n, "DoCommand_00.buil"); + + // The zstd file without a dictionary: made once, then nothing to make; a damaged frame refused. + size_t cap = ZSTD_compressBound(sn), zn; + uint8_t *z = malloc(cap); + zn = ZSTD_compress(z, cap, sarc, sn, 3); + uint8_t dic_frame[64]; + size_t dic_n = ZSTD_compress(dic_frame, sizeof dic_frame, "SARC", 4, 1); + void *out = NULL; + size_t out_n = 0; + int made = action_prompt_build(z, zn, dic_frame, dic_n, 1, &out, &out_n); + uint8_t *back = malloc(sn); + CHECK(made == 1 && out && ZSTD_decompress(back, sn, out, out_n) == sn, "build: %d (%s)", made, last_log); + memcpy(sarc2, sarc, sn); + action_prompt_archive(sarc2, sn, 1, why, sizeof why); + CHECK(made == 1 && !memcmp(back, sarc2, sn), "the built archive is not the archive laid out by label"); + void *out2 = NULL; + CHECK(action_prompt_build(out, out_n, dic_frame, dic_n, 1, &out2, &out_n) == 0 && !out2 && + strstr(last_log, "already"), + "build on a laid out archive: %s", last_log); + CHECK(action_prompt_build(z, zn / 2, dic_frame, dic_n, 1, &out2, &out_n) == -1 && !out2 && + strstr(last_log, "Switch places"), + "build on a cut frame: %s", last_log); + free(out); + + // The zstd file with a dictionary, as the game's: the pack of dictionaries holds two (ids 1234 and 77) beside a + // member that is not one; the archive's frame names 1234. Made, with that dictionary named again; a frame that + // names one the pack does not hold (4321), refused. + enum { DICT_CAP = 8192 }; + static uint8_t dicts[3][DICT_CAP]; + static const unsigned ids[3] = {1234, 77, 4321}; + for (int i = 0; i < 3; i++) + CHECK(make_dict(dicts[i], DICT_CAP, ids[i], sarc + member_at[0], n), "dictionary %u not made", ids[i]); + static uint8_t pack[SIZE * 4]; + const uint8_t *two[MAX_COPIES] = {dicts[0], dicts[1]}; + size_t pack_n = make_sarc(pack, sizeof pack, two, 2, DICT_CAP, "zs.zsdic"); + uint8_t *pack_z = malloc(ZSTD_compressBound(pack_n)); + size_t pack_zn = ZSTD_compress(pack_z, ZSTD_compressBound(pack_n), pack, pack_n, 3); + for (int i = 0; i < 3; i += 2) { + ZSTD_CDict *cd = ZSTD_createCDict(dicts[i], DICT_CAP, 3); + ZSTD_CCtx *cc = ZSTD_createCCtx(); + zn = ZSTD_compress_usingCDict(cc, z, cap, sarc, sn, cd); + ZSTD_freeCCtx(cc); + ZSTD_freeCDict(cd); + out = NULL; + made = action_prompt_build(z, zn, pack_z, pack_zn, 1, &out, &out_n); + if (i == 2) { + CHECK(made == -1 && !out && strstr(last_log, "no dictionary 4321"), "a frame of another dictionary: %s", + last_log); + break; + } + CHECK(made == 1 && ZSTD_getDictID_fromFrame(out, out_n) == 1234, "build with a dictionary: %d (%s)", made, + last_log); + ZSTD_DCtx *dc = ZSTD_createDCtx(); + size_t got = made == 1 ? ZSTD_decompress_usingDict(dc, back, sn, out, out_n, dicts[0], DICT_CAP) : 0; + ZSTD_freeDCtx(dc); + CHECK(got == sn && !memcmp(back, sarc2, sn), "the archive built with a dictionary is not the one laid out"); + free(out); + } + free(pack_z); + free(back); + free(z); + free(again); + free(expect); + + if (argc == 6) game_archive(argv); + else if (argc != 1) printf("usage: action_prompt_check [romfs.bin archive-offset size zsdic-offset size]\n"), fails++; + if (fails) printf("%d checks failed\n", fails); + else printf("action prompt: all checks passed\n"); + return fails != 0; +} diff --git a/tests/action_prompt_check.py b/tests/action_prompt_check.py new file mode 100644 index 00000000..3ee8a8d8 --- /dev/null +++ b/tests/action_prompt_check.py @@ -0,0 +1,66 @@ +"""Build and run tests/action_prompt_check.c: runtime/action_prompt.c, the action prompt's diamond of face buttons laid +out by label, on a layout file and archives the test makes itself. No game files. + +With --romfs, also on the game's own archive: the folder holds romfs.bin and its index.json, and the program reads the +archive and Pack/ZsDic.pack.zs from romfs.bin at the offsets the index gives. Nothing is written. + +The program is built with the undefined-behaviour sanitizer (traps), with locals that start as a pattern rather than +whatever the stack held, and with AddressSanitizer where the toolchain has it (not MSYS2's). + +Usage: action_prompt_check.py [--romfs DIR] +""" +import argparse +import json +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') +ARCHIVE = 'UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.blarc.zs' +DICTIONARIES = 'Pack/ZsDic.pack.zs' + + +def compiler(): + 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 game_args(folder): + """romfs.bin and the offset and size of the archive and the dictionaries in it.""" + files = {f['path']: f for f in json.loads((folder / 'index.json').read_text(encoding='utf-8'))['files']} + for rel in (ARCHIVE, DICTIONARIES): + if rel not in files: + raise SystemExit(f'{folder / "index.json"} has no {rel}') + return [str(folder / 'romfs.bin')] + [str(files[rel][k]) for rel in (ARCHIVE, DICTIONARIES) + for k in ('offset', 'size')] + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument('--romfs', type=Path, help='a folder with romfs.bin and index.json: also check the game\'s archive') + args = ap.parse_args() + work = ROOT / 'build' / 'test' / 'action_prompt' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('action_prompt_check.exe' if os.name == 'nt' else 'action_prompt_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + sanitize = ['-fsanitize=undefined', '-fsanitize-trap=all', '-ftrivial-auto-var-init=pattern'] + if os.name != 'nt': + sanitize.append('-fsanitize=address') + rt = ROOT / 'runtime' + subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wextra', '-Werror', *sanitize, '-I', str(rt), + str(ROOT / 'tests' / 'action_prompt_check.c'), str(rt / 'action_prompt.c'), + str(rt / 'sarc.c'), '-o', str(exe), '-lzstd', '-lm'], env=env) + run = [str(exe)] + (game_args(args.romfs) if args.romfs else []) + return subprocess.run(run, env=env, timeout=300).returncode + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/audio_input_check.c b/tests/audio_input_check.c index 900f8ce9..cf34eeca 100644 --- a/tests/audio_input_check.c +++ b/tests/audio_input_check.c @@ -1,7 +1,21 @@ /* No game data or physical microphone: exercise conversion, bounded capture and the polling ABI. */ -#include "../runtime/audio_capture.c" -#include "../runtime/ov_audio_input.c" +#include #include + +static bool deny_recording; +static int device_open_calls; +static SDL_AudioDeviceID open_test_device(SDL_AudioDeviceID device, const SDL_AudioSpec *spec) { + device_open_calls++; + if (deny_recording) { + SDL_SetError("simulated recording permission denial"); + return 0; + } + return SDL_OpenAudioDevice(device, spec); +} +#define SDL_OpenAudioDevice open_test_device +#include "../runtime/audio_capture.c" +#undef SDL_OpenAudioDevice +#include "../runtime/ov_audio_input.c" #include #include #include @@ -20,6 +34,10 @@ void *ipc_guest(uint64_t addr, uint64_t size) { return (void *)(uintptr_t)addr; } void rt_log(const char *fmt, ...) { (void)fmt; } +int rt_knob(const char *name, const char *def) { + assert(strcmp(name, "MICROPHONE") == 0); + return atoi(def); +} static void ring_check(void) { assert(SDL_InitSubSystem(SDL_INIT_AUDIO)); @@ -59,17 +77,38 @@ static void polling_check(void) { c.x[5] = 4800; open_input_locked(&c); assert(c.x[0] == 0); + MicCapture *m = find(key)->mic; + assert(SDL_GetAudioStreamDevice(m->stream) == 0); c.x[0] = key; state_locked(&c); assert(c.x[0] == 1); + deny_recording = true; + c.x[0] = key; + start_input_locked(&c); + assert(c.x[0] == BAD_INPUT); + assert(SDL_GetAudioStreamDevice(m->stream) == 0); + c.x[0] = key; + state_locked(&c); + assert(c.x[0] == 1); + deny_recording = false; c.x[0] = key; start_input_locked(&c); assert(c.x[0] == 0); + SDL_AudioDeviceID active = SDL_GetAudioStreamDevice(m->stream); + assert(active); + c.x[0] = key; + start_input_locked(&c); + assert(c.x[0] == 0 && SDL_GetAudioStreamDevice(m->stream) == active); c.x[0] = key; state_locked(&c); assert(c.x[0] == 2); SDL_Delay(350); /* overflow the 100 ms ring, not system memory */ c.x[0] = key; + stop_input_locked(&c); + SDL_AudioSpec spec; + assert(!SDL_GetAudioDeviceFormat(active, &spec, NULL)); + assert(SDL_GetAudioStreamDevice(m->stream) == 0); + c.x[0] = key; filled_locked(&c); assert(c.x[0] > 0 && c.x[0] <= 4800); c.x[0] = key; @@ -78,6 +117,9 @@ static void polling_check(void) { read_input_locked(&c); assert(c.x[0] > 0 && c.x[0] <= 4800); c.x[0] = key; + start_input_locked(&c); + assert(c.x[0] == 0 && SDL_GetAudioStreamDevice(m->stream) != 0); + c.x[0] = key; stop_input_locked(&c); c.x[0] = key; purge_input_locked(&c); @@ -120,13 +162,34 @@ static void polling_check(void) { assert(mic_available(NULL) == 0); } +static void independent_streams_check(void) { + int previous_calls = device_open_calls; + MicCapture *a = mic_open(2, 1, 48000, 4800); + MicCapture *b = mic_open(2, 1, 48000, 4800); + assert(a && b); + assert(device_open_calls == previous_calls); + assert(SDL_GetAudioStreamDevice(a->stream) == 0 && SDL_GetAudioStreamDevice(b->stream) == 0); + assert(mic_start(a)); + assert(SDL_GetAudioStreamDevice(b->stream) == 0); + assert(mic_start(b)); + assert(device_open_calls == previous_calls + 2); + SDL_AudioDeviceID active = SDL_GetAudioStreamDevice(b->stream); + assert(active && active != SDL_GetAudioStreamDevice(a->stream)); + mic_close(a); + assert(SDL_GetAudioStreamDevice(b->stream) == active); + mic_stop(b); + assert(SDL_GetAudioStreamDevice(b->stream) == 0); + mic_close(b); +} + int main(void) { assert(SDL_SetHint(SDL_HINT_AUDIO_DRIVER, "dummy")); registry_lock = SDL_CreateMutex(); assert(registry_lock); ring_check(); polling_check(); + independent_streams_check(); SDL_DestroyMutex(registry_lock); - puts("PASS: sample order, bounded overflow, polling frame units, reopen, purge and failed-open regression"); + puts("PASS: bounded capture, idle streams, start/stop, failed start and retry, independent streams and purge"); return 0; } diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 27589b5a..ce0c1a4e 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1627,9 +1627,10 @@ class MakeGameTest(Tmp): 'for d in (0, 50, 100):\n' ' print(f"prepare-shaders: progress translate {d} 100", flush=True)\n' ' time.sleep(0.05)\n' - 'print("prepare-shaders: progress build 10 100", flush=True)\n' + 'print("prepare-shaders: progress build 10000 100000", flush=True)\n' 'time.sleep(0.6)\n' - 'print("prepare-shaders: progress build 100 100", flush=True)\n') + 'for d in (99998, 99999, 100000):\n' + ' print(f"prepare-shaders: progress build {d} 100000", flush=True)\n') for name, value in (('KIT', kit), ('EVENTS', True), ('SHADER_BEAT_S', 0.1), ('SHADER_STALL_S', 0.3)): self.addCleanup(setattr, self.mg, name, getattr(self.mg, name)) setattr(self.mg, name, value) @@ -1650,7 +1651,10 @@ class MakeGameTest(Tmp): self.assertTrue(any(d.startswith("Reading the game's shaders") for d in details)) self.assertIn('Translated 50 of 100 shaders', details) self.assertGreaterEqual(sum(1 for e in events if e['fraction'] == 0.55), 2) # heartbeats while the build waits - self.assertTrue(any(d.startswith('Built 10 of 100') and 'still running' in d for d in details)) + self.assertTrue(any(d.startswith('Built 10,000 of 100,000') and 'still running' in d for d in details)) + for done in ('99,998', '99,999'): + event = next(e for e in events if e['detail'].startswith(f'Built {done} of 100,000')) + self.assertEqual(int(event['fraction'] * 100), 99) self.assertEqual([e['fraction'] for e in events if e['detail'] == 'Translated 50 of 100 shaders'][0], 0.25) def test_a_build_only_run_fills_the_whole_bar(self): @@ -3224,7 +3228,7 @@ class FileCheckTest(Tmp): # PROMPT_TOOLS a new value (every install then draws its prompts again) and record both here; if it cannot, record # only the new digests. PROMPT_LOGIC = ('ad59f2c4533f6ea2630b916ef5f3695f72daffe9b2b7663eea4168a4f08e4249', - {'pad_prompts.py': '1b008475289e0dabfefb8c61969c719c6faa79bbfc8504593e5fe1e79d4d4d33', + {'pad_prompts.py': '780c246b5fde7a0e9dbf3f59ad16372e1737592add6f2f2d9825bee6df4e296f', 'prompt_art.py': '87edacabcddb4952f5862373815d7d46a05c67f350e0734025ae4d2f83b12038', 'button_icons.py': '3804e371a030965bc9eb4226f333b611651f25efaa67ee523b404edc3257818a'}) diff --git a/tools/build_game.py b/tools/build_game.py index 7323de3e..3dd76eb4 100644 --- a/tools/build_game.py +++ b/tools/build_game.py @@ -151,8 +151,9 @@ if WIN: else: # -no-pie: the lifted modules' function tables (hundreds of thousands of pointers) need no load-time relocation. # $ORIGIN: a libSDL3.so.0 next to nx2navi comes before the system's (the release's portable folder). + # -Xlinker for the map path, as on Windows above: -Wl, would split it at any comma in the build dir path. LINK_FLAGS = ['-fuse-ld=lld', '-no-pie', '-Wl,-z,noexecstack', "'-Wl,-rpath,$$ORIGIN'", - '"-Wl,-Map=$builddir/nx2navi.map"'] + '-Xlinker', '"-Map=$builddir/nx2navi.map"'] LINK_LIBS = ['-lvulkan', '-lSDL3', '-lopus', '-ldav1d', '-lvpx', '-lzstd', '-l:libstdc++.a', '-lm', '-lpthread', '-ldl'] VMA_INCLUDE = Path('/usr/include') # vk_mem_alloc.h; Arch (AUR) vulkan-memory-allocator, Debian libvulkan-memory-allocator-dev # Without a system vk_mem_alloc.h, the header the Windows release pins (tools/make_release.py, VMA 3.4.0) is fetched once diff --git a/tools/make_game.py b/tools/make_game.py index 3e8c0fdd..bb4e304c 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -1701,8 +1701,9 @@ def step_compile(out, cache_dir, msys, jobs, romfs_job=None, update=False): ld = (['-fuse-ld=lld'] if (msys / 'bin' / 'ld.lld.exe').is_file() else []) + ['-Wl,--stack,16777216'] else: # tools/build_game.py's Linux flags; the main thread's stack is the process's (ulimit -s), not the linker's ld = (['-fuse-ld=lld'] if shutil.which('ld.lld') else []) + ['-no-pie', '-Wl,-z,noexecstack', '-Wl,-rpath,$ORIGIN'] - # the link map feeds tools/prof_report.py (symbols for an NX2NAVI_PROFILE sampling profile); both linkers write one - ld.append('-Wl,--Map=' + (cache_dir / 'ocarina.map').as_posix()) + # the link map feeds tools/prof_report.py (symbols for an NX2NAVI_PROFILE sampling profile); both linkers write one. + # -Xlinker passes the path as one argument; -Wl, would cut it at every comma of the game folder's path (issue #470) + ld += ['-Xlinker', '--Map=' + (cache_dir / 'ocarina.map').as_posix()] lib = ['-Wl,--whole-archive', str(prebuilt[0]), '-Wl,--no-whole-archive'] if prebuilt else [] stamp = [[o.name, o.stat().st_size, o.stat().st_mtime_ns] for o in objs] if prebuilt: @@ -1773,7 +1774,7 @@ def shader_progress(phase, done, total, quiet_s, elapsed_s, build_only=False): if quiet_s >= SHADER_STALL_S: detail += (f'. No change for {int(quiet_s) // 60} min, but it is still running: one large shader can take ' 'a while') - return {'step': 'shaders', 'fraction': round(fraction, 4), 'detail': detail} + return {'step': 'shaders', 'fraction': fraction, 'detail': detail} # In the game's shader-cache/: the translator identities its translations are of, newest first (runtime/gfx_shader.c, diff --git a/tools/pad_prompts.py b/tools/pad_prompts.py index 7cee1d02..400e2a88 100644 --- a/tools/pad_prompts.py +++ b/tools/pad_prompts.py @@ -747,6 +747,11 @@ def mask_of(img, rel): return out +def note_texture(art, rel, box): + pic = fit(load_art(art, rel), box, (93, 93)) + return astc_encode(bleed(pic)) + + def draw_textures(data, glyph, note, art, cache): """Paints the action prompt's letters (from glyph) and the ocarina's note buttons (from note; both Switch button -> picture) into the BNTX data (a bytearray) in place. A new letter takes the height and centre of the game's own @@ -772,8 +777,9 @@ def draw_textures(data, glyph, note, art, cache): assert t['fmt'] == ASTC_4X4_SRGB and (t['w'], t['h']) == (93, 93), f'{name}: format {t["fmt"]:#x}, ' \ f'{t["w"]}x{t["h"]}' box = NOTE_BOXES['shoulder' if button in 'LR' else 'disc'] - pic = fit(load_art(art, note[button]), box, (t['w'], t['h'])) - write_level(data, t, astc_encode(bleed(pic)), wb * 16, hb) + rel = note[button] + encoded = cache(('note', rel, box), lambda: note_texture(art, rel, box)) + write_level(data, t, encoded, wb * 16, hb) # Where the game's own note buttons are opaque in their 93x93 textures (1.1.1), which the new ones fill: the face