From 9ae7764ab55b6a69129ccf1a199384fac666198f Mon Sep 17 00:00:00 2001 From: epicmeme546 <178368684+epicmeme546@users.noreply.github.com> Date: Mon, 9 Nov 2026 23:27:02 -0500 Subject: [PATCH 01/13] Change a setting with the mouse only on the two framed arrows beside its value, and switch HDR without a restart. In the settings menu a click anywhere on a row stepped its setting to the next value, and the left arrow stood left of the value's text, so it moved whenever a value of another width came up: clicking the left arrow through a list landed on the row at the second click and stepped the other way. Each stepper now has its arrows in white frames at fixed places, the left one the width of the row's widest value from the right one, and the mouse changes the value on those frames only; a click on the rest of the row selects it. On and off rows have the same arrows. An arrow at the end of its values is drawn faint and does nothing, and a button held on an arrow repeats as a held key does. A, Enter and left and right work as before. NX2NAVI_MENU_TEST_CLICK clicks rows by their setting's name during a menu test run. On an RX 7900 XT at 3232x1352 the arrows stepped the performance overlay one value a click and stopped at both ends, and clicks on the body of four rows changed nothing. HDR between off and the tone mapper now follows the menu. The HDR path is set up at every start (vk_hdr_armed): the swapchain colour space and metadata extensions, the tonemap splice and the settings in the support buffer, with the spliced tonemap returning the game's own SDR result while the swapchain is SDR. A change of the setting makes the swapchain again in the other format. On the same card a run that started with HDR off went to an HDR10 swapchain and back, and one that started with HDR on went to SDR and back, with the picture right in each state. The game's native HDR still waits for a restart, as the game asks about the display once, and the row says so. NX2NAVI_HDR_LIVE = 0 leaves the HDR path out of a run that starts with HDR off. Co-Authored-By: Claude Fable 5.2 --- docs/architecture.md | 5 ++ docs/controls.md | 2 +- nx2navi.ini.example | 7 +- runtime/cfg.c | 14 ++-- runtime/gfx_present.c | 13 ++- runtime/ui/menu.cpp | 14 ++-- runtime/ui/option_rows.cpp | 160 ++++++++++++++++++++++++++++++------- runtime/ui/option_rows.h | 8 +- runtime/vk.h | 6 +- runtime/vk_device.c | 5 +- runtime/vk_exec_draw.c | 2 +- runtime/vk_hdr.c | 69 ++++++++++++---- 12 files changed, 236 insertions(+), 69 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index fa4f2aad..810973dd 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -124,6 +124,11 @@ keeps per-core data without locks and relies on that, so the host models it (`ru holds the HDR picture relative to the HUD's white; the game's FSR 1 pass loses its output clamp; the present reads the float picture in place of the window texture and `shaders/hdr_encode.comp` writes PQ for an HDR10 swapchain. `NX2NAVI_HDR=native` reports an HDR10 display to the game, which then renders and presents its own PQ picture. + The menu switches between off and `1` during a run: the splice, the HDR extensions and the settings in the support + buffer are there from the start whatever is set (`vk_hdr_armed`), the spliced tonemap returns the game's SDR result + while the swapchain is SDR, and a change of the setting makes the swapchain again in the other format + (`gfx_present.c` `window_settings_live`). Only `native` waits for a restart, as the game asks about the display + once. `NX2NAVI_HDR_LIVE=0` leaves the HDR path out of a run that starts with HDR off. - ASTC textures are uploaded as they are where the device samples ASTC, and decoded by a compute shader everywhere else (`vk_astc.c`); under 20 GiB of video memory the decoder encodes them to BC3. - Occlusion queries never block the CPU; a result that is not ready counts as visible. diff --git a/docs/controls.md b/docs/controls.md index 7c139b2b..add8c150 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -78,7 +78,7 @@ The game's *Swap Jump Controls* option trades B and X (jump on B, put away on X) The window opens on the monitor `NX2NAVI_MONITOR` (`monitor` in `nx2navi.ini`) names, and F11 fullscreen covers the monitor the window is on. `nx2navi.exe --list-monitors` prints each monitor's index, name (on Windows its device name too), desktop rectangle, primary flag and DPI without game files; `nx2navi.exe --monitor ` sets `NX2NAVI_MONITOR` for one run and wins over the file. The value is `auto` (default), `primary`, an index from the list (counting from 1), or part of a monitor name (on Windows also a device name such as `display3`; on Linux the name carries the connector, such as `dp-2`); when nothing matches, the log lists the monitors and the window is placed as with `auto`. On Wayland a window cannot place itself: only fullscreen (`fullscreen = 1`) opens on the chosen monitor, and a windowed window opens where the compositor puts it. -The Minus+Plus combo never reaches the game. In the menu the arrow keys, the D-pad or the left stick move, Enter or A selects, Q and E or L and R switch tabs, Esc or B goes back (Switch button names, as in the table above). +The Minus+Plus combo never reaches the game. In the menu the arrow keys, the D-pad or the left stick move, Enter or A selects, Q and E or L and R switch tabs, Esc or B goes back (Switch button names, as in the table above). With the mouse, a click on a row selects it and a setting changes on the two framed arrows beside its value (held, an arrow repeats); a click anywhere else on the row changes nothing. ## Audio output diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 38949930..1723ae53 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -343,9 +343,12 @@ # HDR output: hdr = 1 sends the picture as HDR10 through the HDR tone mapper, hdr = native as the game renders it in # its own HDR mode (both need HDR on in Windows, "Use HDR", or a Wayland compositor with colour management; without it # the picture stays SDR, the game's own). hdr_peak_nits is your display's peak; hdr_game_nits the picture's white -# (paper white, 203 is the HDR reference) and hdr_ui_nits the HUD's and the menu's. hdr is read at start, the rest at -# once. +# (paper white, 203 is the HDR reference) and hdr_ui_nits the HUD's and the menu's. Everything applies at once, also +# hdr between 0 and 1; only hdr = native, which the game itself takes up, waits for the next start. # hdr = 0 +# hdr_live = 0 leaves the HDR path out of a run that starts with hdr = 0, as before hdr followed the menu: switching +# HDR on then takes a restart. Only worth setting if the picture differs with it at 1. +# hdr_live = 1 # hdr_peak_nits = 1000 # hdr_game_nits = 203 # hdr_ui_nits = 203 diff --git a/runtime/cfg.c b/runtime/cfg.c index d766cf3e..7a2a59b4 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -45,9 +45,10 @@ static const char *const known[] = { "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", + "HDR_LIVE", "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", - "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", + "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC", @@ -79,7 +80,7 @@ static const struct { const char *key, *section; } sections[] = { {"SHARPNESS", "display"}, {"ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"}, {"COLOR_PRESET", "display"}, {"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"}, - {"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"}, + {"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_LIVE", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"}, {"HDR_UI_NITS", "display"}, {"HDR_GAMMA", "display"}, {"HDR_TONEMAP", "display"}, {"HDR_EXPOSURE", "display"}, {"HDR_HIGHLIGHTS", "display"}, {"HDR_SHADOWS", "display"}, {"HDR_CONTRAST", "display"}, {"HDR_SATURATION", "display"}, {"HDR_HIGHLIGHT_SAT", "display"}, {"HDR_BLOWOUT", "display"}, {"HDR_FLARE", "display"}, {"HDR_HUE", "display"}, @@ -149,9 +150,12 @@ static const CfgMeta metas[] = { {"COLOR_PRESET", "default", "default|vivid|clarity|custom", LIVE}, {"COLOR_BRIGHTNESS", "0", "-30..30", LIVE}, {"COLOR_CONTRAST", "100", "70..150", LIVE}, {"COLOR_SATURATION", "100", "50..200", LIVE}, {"COLOR_GAMMA", "1", "0.8..1.2", LIVE}, - // HDR output (vk_hdr.c): on at start (the HDR tonemap is spliced into the game's as shaders load); the rest apply at - // once. The grading sliders: 50 leaves the picture as it is, exposure in percent. - {"HDR", "0", "0|1|native", RESTART}, {"HDR_PEAK_NITS", "1000", "400..10000", LIVE}, {"HDR_GAME_NITS", "203", "48..500", LIVE}, + // HDR output (vk_hdr.c): off and the tone mapper apply at once (the HDR tonemap is spliced into the game's as + // shaders load whatever is set, vk_hdr_armed); native is the game's own and is asked for once, at start, which the + // menu's row says (vk_hdr_needs_restart). HDR_LIVE = 0 leaves the HDR path out of a run that starts with HDR off. + // The grading sliders: 50 leaves the picture as it is, exposure in percent. + {"HDR", "0", "0|1|native", LIVE}, {"HDR_LIVE", "1", "0|1", RESTART}, + {"HDR_PEAK_NITS", "1000", "400..10000", LIVE}, {"HDR_GAME_NITS", "203", "48..500", LIVE}, {"HDR_UI_NITS", "203", "48..500", LIVE}, {"HDR_GAMMA", "2.2", "off|2.2|2.4", LIVE}, {"HDR_TONEMAP", "renodrt", "vanilla|renodrt", LIVE}, {"HDR_EXPOSURE", "100", "0..200", LIVE}, {"HDR_HIGHLIGHTS", "50", "0..100", LIVE}, {"HDR_SHADOWS", "50", "0..100", LIVE}, {"HDR_CONTRAST", "50", "0..100", LIVE}, diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index cb2a9def..3a8d1592 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -238,7 +238,8 @@ int gfx_window_focused(void) { return g_focused; } unsigned gfx_window_focus_messages(void) { return (unsigned)g_focus_msgs; } // Present path (g_pl held): a settings change (cfg_version) reaches the window thread, which applies a changed size or -// fullscreen; a changed NX2NAVI_VK_PRESENT rebuilds the swapchain at this present. +// fullscreen; a changed NX2NAVI_VK_PRESENT or NX2NAVI_HDR rebuilds the swapchain at this present. +static int g_sc_hdr_wanted; // vk_hdr_wanted when the swapchain was made (create_swapchain) static void window_settings_live(void) { static unsigned seen; static int started; @@ -253,6 +254,9 @@ static void window_settings_live(void) { if (started) plat_xchg32(&g_dirty, 1); } if (started && vk_fg_wanted() != vk_fg_active()) plat_xchg32(&g_dirty, 1); // frame generation switched: its own swapchain + // HDR switched in the menu: a swapchain of the other format. Compared with what the swapchain was made for, not + // with what it got: on a display without HDR10 the setting stays on and the swapchain SDR, and that is settled. + if (started && vk_hdr_wanted() != g_sc_hdr_wanted) plat_xchg32(&g_dirty, 1); ov_operation_mode_live(); if (started) gfx_window_apply_settings(); started = 1; @@ -362,7 +366,8 @@ static int create_swapchain(void) { if (!nf) return plat_xchg32(&g_dirty, 1), 0; // UNORM: the blit writes the bytes the game wrote (no sRGB encode). NX2NAVI_HDR: HDR10 when the surface offers it. VkSurfaceFormatKHR sf = fmts[0]; - int hdr = vk_hdr_surface_format(fmts, nf, &sf), found = hdr; + g_sc_hdr_wanted = vk_hdr_wanted(); + int hdr = g_sc_hdr_wanted && vk_hdr_surface_format(fmts, nf, &sf), found = hdr; for (uint32_t i = 0; i < nf && !found; i++) if (fmts[i].format == VK_FORMAT_B8G8R8A8_UNORM && fmts[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { sf = fmts[i]; found = 1; } for (uint32_t i = 0; i < nf && !found; i++) @@ -428,6 +433,10 @@ static int create_swapchain(void) { drop_swapchain_locked(c); g_sc = made; g_sc_fmt = sf.format; + static int hdr_was = -1; // the log says when a later swapchain is of the other kind (the menu's HDR row) + if (hdr_was >= 0 && hdr_was != hdr) + rt_log("gfx: the swapchain is now %s (format %d)", hdr ? "HDR10" : "SDR", (int)sf.format); + hdr_was = hdr; g_sc_hdr = hdr; vk_hdr_set_active(hdr); if (hdr) vk_hdr_metadata(g_sc, 1); diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 20f15acd..0d38beeb 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -65,6 +65,7 @@ int wide_available(void); const char *const *vk_color_preset(const char *name); int vk_hdr_wanted(void); int vk_hdr_active(void); +int vk_hdr_needs_restart(void); int vk_dlaa_needs_restart(void); double frame_step_limit_hz(void); int frame_limit_follows_display(void); @@ -500,6 +501,7 @@ extern "C" void menu_vk_release(void) { // NX2NAVI_MENU_TEST=[:]: at that presented frame the menu opens on its first tab; each tab stays // frames (default 30) and its last frame's swapchain image goes to NX2NAVI_MENU_TEST_DIR (default: the // working directory) as menu-test--.png; after the last tab the menu closes. +// NX2NAVI_MENU_TEST_CLICK=:[,...]: mouse clicks on the rows of the page shown (ui/option_rows.cpp click_test). // NX2NAVI_MENU_TEST_SET=:=[,=...][;:...]: cfg_set of each key (NX2NAVI_ prefix // optional) at that presented frame; changes live settings with no input, menu open or not. nx2navi.ini is written only by // the pseudo-key SAVE=1 (cfg_save, as a row change does; a failed write shows the menu's save note). @@ -859,18 +861,20 @@ static void row_upscaling(void) { if (e.kind >= DLSS) opt_apply("NX2NAVI_TAA", e.kind == FSR3 ? "fsr3" : "dlss"); } -// "in use: off" under the HDR value when the game started with HDR on but the swapchain is not HDR10 (the display or -// Windows' HDR switch is off, vk_hdr_surface_format). The row's "after restart" comes from the setting itself (cfg.c). +// Under the HDR value: "after restart" when what is set waits for one (the game's own HDR, to or from it: +// vk_hdr_needs_restart), "in use: off" when HDR is on but the swapchain is not HDR10 (the display or Windows' HDR +// switch is off, vk_hdr_surface_format). static const char *hdr_note(void) { + if (vk_hdr_needs_restart()) return menu_text(UI_MENU_AFTER_RESTART); if (!vk_hdr_wanted() || vk_hdr_active() || !*gfx_window_present_mode() || !cfg_bool("NX2NAVI_HDR")) return nullptr; static char note[96]; snprintf(note, sizeof note, menu_text(UI_MENU_PRESENT_IN_USE), menu_text(UI_MENU_FRAMEGEN_OFF)); return note; } -// HDR (vk_hdr.c), after a restart: off, the HDR tone mapper over the game's picture or the game's native HDR. The -// brightness rows follow while it is on; the tone mapper's own rows only with the first (native HDR is the game's -// picture as it is), its grading rows only with its own curve chosen. +// HDR (vk_hdr.c): off or the HDR tone mapper over the game's picture, switched at once, or the game's native HDR, +// which the game takes up at its start. The brightness rows follow while it is on; the tone mapper's own rows only +// with the first (native HDR is the game's picture as it is), its grading rows only with its own curve chosen. static void row_hdr(void) { static const char *const values[] = {"0", "1", "native"}; const char *names[] = {menu_text(UI_MENU_FRAMEGEN_OFF), menu_text(UI_MENU_HDR_TONEMAPPED), menu_text(UI_MENU_HDR_NATIVE)}; diff --git a/runtime/ui/option_rows.cpp b/runtime/ui/option_rows.cpp index 9a7909a7..21937619 100644 --- a/runtime/ui/option_rows.cpp +++ b/runtime/ui/option_rows.cpp @@ -130,13 +130,83 @@ void opt_end(void) { struct RowView { const char *value; // right side; "" for none bool is_default; // the value is the setting's default: the "default" mark, the value not highlighted - bool arrows; // a stepper: < value >, the arrows click too + bool arrows; // a stepper: [<] value [>]; the mouse changes the value on the two framed arrows only const char *note; // under the value instead of the "default" mark; NULL for none + float col; // the width of the widest value the row can show (value_width); 0: this value's + bool end_l, end_r; // the value is the first / last one: that arrow is drawn faint and does nothing }; -// One row: draws it, handles A / click and left / right while selected. Returns 2 (A / click), -1 / +1 (left / right), -// 3 / 4 (move up / down, movable rows only) or 0; always 0 for a row the environment fixes (greyed out, still selectable -// so its description shows). movable: the top / left face buttons, Ctrl+Up / Ctrl+Down and two drawn arrows move it. +// The width of the widest of n values at the current font size: a stepper's left arrow stands that far from the right +// one whatever value shows, so it stays under the pointer while it is clicked through the values. +static float value_width(const char *const *names, int n) { + ImFont *font = ImGui::GetFont(); + float fs = ImGui::GetFontSize(), w = 0; + for (int i = 0; i < n; i++) + if (names[i]) w = fmaxf(w, font->CalcTextSizeA(fs, FLT_MAX, 0, names[i]).x); + return w; +} + +// One framed arrow of a stepper over [a, b]: a white frame with the arrow in its middle, filled a little while the +// pointer is on it and more while it is pressed; faint when it does nothing (dim: the row is greyed out or the value +// is at this end). +static void arrow_box(ImDrawList *dl, ImVec2 a, ImVec2 b, bool left, bool hot, bool down, bool dim, float s) { + const int al = dim ? 70 : hot ? 255 : 215; + const ImU32 white = IM_COL32(255, 255, 255, al); + if (hot && !dim) dl->AddRectFilled(a, b, IM_COL32(255, 255, 255, down ? 96 : 44), 4 * s); + dl->AddRect(a, b, white, 4 * s, 0, fmaxf(1.0f, (hot && !dim ? 2.2f : 1.6f) * s)); + float cx = (a.x + b.x) * 0.5f, cy = (a.y + b.y) * 0.5f, aw = (b.y - a.y) * 0.38f, ah = aw * 0.8f; + float tip = left ? -aw * 0.55f : aw * 0.55f, back = left ? aw * 0.45f : -aw * 0.45f; + dl->AddTriangleFilled(ImVec2(cx + tip, cy), ImVec2(cx + back, cy - ah), ImVec2(cx + back, cy + ah), white); +} + +// ---- test hook. NX2NAVI_MENU_TEST_CLICK=:[,:...], with NX2NAVI_MENU_TEST opening the menu on the +// page that has the rows (menu.cpp): mouse clicks on steppers by their setting's name (without NX2NAVI_), part l (the +// left arrow), r (the right arrow) or b (the row's body, on its label). One click after another, each over a dozen +// frames: the pointer goes there, the button goes down and up, and the log says what the setting is then. So a run +// checks that the arrows step a setting and that an arrow at its end and a click on the body do not, with the fonts +// and sizes of the game. +static struct ClickTest { int parsed, n, at, frame; struct { char key[56]; char part; } c[24]; } g_click; + +static void click_test(const char *key, ImVec2 la, ImVec2 lb, ImVec2 ra, ImVec2 rb, ImVec2 label) { + ClickTest &t = g_click; + if (!t.parsed) { + t.parsed = 1; + const char *s = cfg_get("NX2NAVI_MENU_TEST_CLICK"); + while (s && *s && t.n < 24) { + size_t n = strcspn(s, ","); + const char *colon = (const char *)memchr(s, ':', n); + if (colon) { + snprintf(t.c[t.n].key, sizeof t.c[0].key, "NX2NAVI_%.*s", (int)(colon - s), s); + t.c[t.n++].part = colon[1]; + } + s += n; + if (*s == ',') s++; + } + } + if (t.at >= t.n || strcmp(key, t.c[t.at].key)) return; + const char part = t.c[t.at].part; + const ImVec2 to = part == 'l' ? ImVec2((la.x + lb.x) * 0.5f, (la.y + lb.y) * 0.5f) + : part == 'r' ? ImVec2((ra.x + rb.x) * 0.5f, (ra.y + rb.y) * 0.5f) + : label; + ImGuiIO &io = ImGui::GetIO(); + const int f = t.frame++; + if (f == 0) io.AddMousePosEvent(to.x, to.y); + else if (f == 4) io.AddMouseButtonEvent(ImGuiMouseButton_Left, true); + else if (f == 7) io.AddMouseButtonEvent(ImGuiMouseButton_Left, false); + else if (f == 12) { + rt_log("menu: test hook: click on %s of %s: now %s", + part == 'l' ? "the left arrow" : part == 'r' ? "the right arrow" : "the body", key, cfg_value(key)); + t.at++, t.frame = 0; + } +} + +// One row: draws it, handles A / click and left / right while selected. Returns 2 (A, or a click on a row without +// arrows), -1 / +1 (left / right, or the mouse on a stepper's framed arrow), 3 / 4 (move up / down, movable rows only) +// or 0; always 0 for a row the environment fixes (greyed out, still selectable so its description shows). movable: the +// top / left face buttons, Ctrl+Up / Ctrl+Down and two drawn arrows move it. +// A stepper takes the mouse on its two framed arrows only: a click anywhere else on the row selects it and changes +// nothing. A click on the row used to step to the next value, and the left arrow stood left of the value's text, so +// it moved when a value of another width came up and the next click at the same place landed on the row instead. static int row_text(const char *key, const char *label, const char *desc, const RowView &v, bool movable) { const CfgMeta *m = cfg_meta(key); const char *disabled = g_disable_next ? g_disable_note : NULL; @@ -212,19 +282,38 @@ static int row_text(const char *key, const char *label, const char *desc, const vfs = fmaxf(fs * cap / tv, fs * 0.75f); tv = fminf(font->CalcTextSizeA(vfs, FLT_MAX, 0, v.value).x, cap); } - if (v.arrows && v.value[0]) { // < value >, drawn (the font has no arrow glyphs); a click on one steps - float aw = fs * 0.42f, gap = fs * 0.55f, cy = ty + fs * 0.52f; - float rx = right - aw, lx = rx - gap - tv - gap - aw; - ImU32 ac = off ? kGrey : hov || foc ? SKIN_TEXT : SKIN_TEXT_DIM; - dl->AddTriangleFilled(ImVec2(lx, cy), ImVec2(lx + aw, cy - aw * 0.75f), ImVec2(lx + aw, cy + aw * 0.75f), ac); - dl->AddTriangleFilled(ImVec2(rx + aw, cy), ImVec2(rx, cy - aw * 0.75f), ImVec2(rx, cy + aw * 0.75f), ac); - if (by_mouse && !off) { - float mx = ImGui::GetIO().MousePos.x; - if (mx >= lx - gap && mx <= lx + aw + gap) step = -1; - else if (mx >= rx - gap && mx <= rx + aw + gap) step = 1; + float value_mid = -1; // a stepper's value and its note are centred here, between the arrows + if (v.arrows) { // [<] value [>]: the arrows are drawn (the font has no arrow glyphs), each in a white frame + // The frames stand in fixed places: the right one at the row's right edge, the left one the width of the row's + // widest value before it. They fit over the note line, in the label line's height. + const float bw = floorf(fs * 1.3f), bh = floorf(fs * 1.02f), gap = floorf(fs * 0.4f), cy = floorf(ty + fs * 0.52f); + float col = fminf(fmaxf(v.col, tv), fmaxf(cap - 2 * (bw + gap), fs)); + if (tv > col) { // a value wider than the room between the arrows: smaller, then clipped + vfs = fmaxf(fs * col / font->CalcTextSizeA(fs, FLT_MAX, 0, v.value).x, fs * 0.75f); + tv = fminf(font->CalcTextSizeA(vfs, FLT_MAX, 0, v.value).x, col); } - right = rx - gap; - value_left = lx; + const ImVec2 ra(right - bw, cy - bh * 0.5f), rb(right, cy + bh * 0.5f); + const ImVec2 la(ra.x - gap - col - gap - bw, ra.y), lb(ra.x - gap - col - gap, rb.y); + if (!g_click.parsed || g_click.at < g_click.n) click_test(key, la, lb, ra, rb, ImVec2(p.x + w * 0.25f, p.y + h * 0.5f)); + const ImVec2 mp = ImGui::GetIO().MousePos; + // the pointer on an arrow: its frame and two pixels around it, so a click on the frame's own line counts + const float slop = 2 * g_scale; + auto on = [&](ImVec2 a, ImVec2 b) { + return hov && mp.x >= a.x - slop && mp.x < b.x + slop && mp.y >= a.y - slop && mp.y < b.y + slop; + }; + const bool hl = on(la, lb), hr = on(ra, rb), down = ImGui::IsMouseDown(ImGuiMouseButton_Left); + if (by_mouse) step = 0; // a click on the row itself only selects it + // The press steps, and a held button repeats as a held key does; after a second it also goes ten steps at a + // time over a long range (opt_slider), as held left / right do. + if (!off && ((hl && !v.end_l) || (hr && !v.end_r)) && ImGui::IsMouseClicked(ImGuiMouseButton_Left, true)) { + step = hl ? -1 : 1; + if (ImGui::GetIO().MouseDownDuration[ImGuiMouseButton_Left] > 1.0f) g_held = true; + } + arrow_box(dl, la, lb, true, hl, hl && down, off || v.end_l, g_scale); + arrow_box(dl, ra, rb, false, hr, hr && down, off || v.end_r, g_scale); + value_mid = (lb.x + ra.x) * 0.5f; + right = ra.x - gap; + value_left = la.x; } if (movable) { // up and down arrows left of the value; a click on one moves the row // a fixed value column (On / Off differ in width) keeps the arrows of the rows in one line @@ -241,8 +330,8 @@ static int row_text(const char *key, const char *label, const char *desc, const value_left = cx - gap; } if (v.value[0]) { - float vx = right - tv; - ImVec4 vclip(vx, p.y, right, p.y + h); + float vx = value_mid >= 0 ? floorf(value_mid - tv * 0.5f) : right - tv; + ImVec4 vclip(vx, p.y, vx + tv, p.y + h); dl->AddText(font, vfs, ImVec2(vx, ty + (fs - vfs) * 0.5f), off ? kGrey : v.is_default ? IM_COL32(232, 222, 198, 255) : IM_COL32(255, 255, 255, 255), v.value, NULL, 0, &vclip); @@ -252,10 +341,11 @@ static int row_text(const char *key, const char *label, const char *desc, const if (v.note || v.is_default) { // under the value, right-aligned with it; its start shows when it is too long const char *t = v.note ? v.note : menu_text(UI_MENU_DEFAULT_MARK); float tw = fminf(font->CalcTextSizeA(small, FLT_MAX, 0, t).x, cap); - ImVec4 nclip(right - tw, p.y, right, p.y + h); - dl->AddText(font, small, ImVec2(right - tw, ny), v.note ? IM_COL32(250, 206, 130, 255) : SKIN_TEXT_DIM, t, NULL, 0, - &nclip); - if (tw > 0) note_right = right - tw - fs * 0.6f; + // a stepper's note is centred under its value, as far as the row's right edge lets it + float nx = value_mid >= 0 ? fminf(floorf(value_mid - tw * 0.5f), p.x + w - pad - tw) : right - tw; + ImVec4 nclip(nx, p.y, nx + tw, p.y + h); + dl->AddText(font, small, ImVec2(nx, ny), v.note ? IM_COL32(250, 206, 130, 255) : SKIN_TEXT_DIM, t, NULL, 0, &nclip); + if (tw > 0) note_right = nx - fs * 0.6f; } // the label and its note, clipped before the value ImVec4 clip(p.x, p.y, value_left - fs * 0.6f, p.y + h); @@ -311,11 +401,17 @@ int opt_save_tick(int force) { // ---- rows +// An on / off row is a stepper too: either arrow, A, left or right flips it. +static float on_off_width(void) { + const char *const names[] = {menu_text(UI_MENU_ON), menu_text(UI_MENU_OFF)}; + return value_width(names, 2); +} + bool opt_toggle(const char *key, UiTextId label, UiTextId desc, bool inverted) { const char *d = cfg_default(key); bool set = cfg_bool(key) != 0, def = d && *d && strcmp(d, "0") != 0; bool on = set != inverted; // what the row shows - RowView v = {menu_text(on ? UI_MENU_ON : UI_MENU_OFF), set == def, false, NULL}; + RowView v = {menu_text(on ? UI_MENU_ON : UI_MENU_OFF), set == def, true, NULL, on_off_width()}; if (!row(key, label, desc, v)) return false; // any step flips it apply(key, set ? "0" : "1"); return true; @@ -326,7 +422,7 @@ bool opt_toggle_value(const char *key, UiTextId label, UiTextId desc, const char // when it is an on value, so turning the switch off and on again puts the profile's own value back const char *d = cfg_default(key); bool on = strcmp(cfg_value(key), off_value) != 0, def = d && strcmp(d, off_value) != 0; - RowView v = {menu_text(on ? UI_MENU_ON : UI_MENU_OFF), on == def, false, note}; + RowView v = {menu_text(on ? UI_MENU_ON : UI_MENU_OFF), on == def, true, note, on_off_width()}; if (!row(key, label, desc, v)) return false; // any step flips it apply(key, on ? off_value : def && *d ? d : on_value); return true; @@ -339,7 +435,8 @@ bool opt_button(const char *id, UiTextId label, UiTextId desc, const char *value bool opt_toggle_text(const char *id, const char *label, const char *desc, bool on, const char *value) { // off is the plain state: its value drawn dim, without the "default" mark - RowView v = {value ? value : menu_text(on ? UI_MENU_ON : UI_MENU_OFF), !on, false, on ? NULL : ""}; + RowView v = {value ? value : menu_text(on ? UI_MENU_ON : UI_MENU_OFF), !on, true, on ? NULL : "", + value ? 0 : on_off_width()}; return row_text(id, label, desc, v, false) != 0; // any step flips it } @@ -391,7 +488,7 @@ bool opt_choice_list(const char *key, UiTextId label, const char *const *values, int def = -1; for (int i = 0; d && i < n; i++) if (!strcmp(values[i], d)) def = i; - RowView v = {cur >= 0 ? names[cur] : now, cur == def, true, note}; + RowView v = {cur >= 0 ? names[cur] : now, cur == def, true, note, value_width(names, n), cur == 0, cur == n - 1}; int step = row(key, label, desc, v); if (!step || n <= 0) return false; int nv; @@ -405,7 +502,8 @@ bool opt_choice_list(const char *key, UiTextId label, const char *const *values, int opt_choice_index(const char *key, UiTextId label, const char *const *names, int n, int cur, int def, UiTextId desc, const char *note) { - RowView v = {cur >= 0 && cur < n ? names[cur] : "", cur == def, true, note}; + RowView v = {cur >= 0 && cur < n ? names[cur] : "", cur == def, true, note, value_width(names, n), cur == 0, + cur == n - 1}; int step = row(key, label, desc, v); if (!step || n <= 0) return -1; int nv = cur < 0 ? (step < 0 ? n - 1 : 0) : step == 2 ? (cur + 1) % n : cur + step < 0 ? 0 : cur + step >= n ? n - 1 : cur + step; @@ -452,9 +550,13 @@ bool opt_slider(const char *key, UiTextId label, float lo, float hi, UiTextId de // follows the fallback's default, not 0, even once the key is set. if (fallback && !(d && *d)) d = cfg_default(fallback); double v = cfg_double(src), def = d && *d ? strtod(d, NULL) : lo; - char shown[48]; + char shown[48], ends[2][48]; snprintf(shown, sizeof shown, fmt, v); - RowView rv = {shown, fabs(v - def) < step * 0.01, true, note}; + snprintf(ends[0], sizeof ends[0], fmt, (double)lo); + snprintf(ends[1], sizeof ends[1], fmt, (double)hi); + const char *const widest[] = {shown, ends[0], ends[1]}; + RowView rv = {shown, fabs(v - def) < step * 0.01, true, note, value_width(widest, 3), v <= lo + step * 0.01, + v >= hi - step * 0.01}; int s = row(key, label, desc, rv); if (!s) return false; int steps = g_held && (hi - lo) > 40 * step ? 10 : 1; // held: ten steps at a time over a long range diff --git a/runtime/ui/option_rows.h b/runtime/ui/option_rows.h index 7ce6fcce..e865b80b 100644 --- a/runtime/ui/option_rows.h +++ b/runtime/ui/option_rows.h @@ -1,7 +1,9 @@ // Settings pages in two panes (ui/option_rows.cpp): option rows on the left, the selected (controller) or hovered (mouse) -// row's description on the right. Each row shows a setting of the cfg table (cfg.h) and changes it through cfg_set: A or -// a click steps it (toggle, next value), left and right step it. A row marks the default value, says "after restart" when -// the setting is read at start, and is greyed out with a note when an environment variable fixes it. +// row's description on the right. Each row shows a setting of the cfg table (cfg.h) and changes it through cfg_set: A +// steps it (toggle, next value), left and right step it, and the mouse steps it on the two framed arrows beside its +// value and nowhere else on the row (a click on the row selects it; held on an arrow it repeats). A row marks the +// default value, says "after restart" when the setting is read at start, and is greyed out with a note when an +// environment variable fixes it. // // opt_begin("##screen", scale); // opt_toggle("NX2NAVI_FULLSCREEN", UI_MENU_FULLSCREEN, UI_MENU_FULLSCREEN_DESC); diff --git a/runtime/vk.h b/runtime/vk.h index 4ca82456..3e067c79 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -173,11 +173,13 @@ void vk_color_release(void); // before the device goes away // The brightness, contrast, saturation and gamma values (as the settings write them) of a colour profile, NULL for // custom or an unknown name (vk_color.c). const char *const *vk_color_preset(const char *name); -// HDR output (vk_hdr.c). vk_hdr_wanted: NX2NAVI_HDR, read once. vk_hdr_surface_format: the HDR10 surface format when wanted -// and offered (1). vk_hdr_set_active: gfx_present.c says whether the swapchain is HDR10 (vk_hdr_active). vk_hdr_encode: +// HDR output (vk_hdr.c). vk_hdr_armed: the HDR path is set up for this run (decided at the start). vk_hdr_wanted: +// NX2NAVI_HDR as the menu has it now. vk_hdr_surface_format: the HDR10 surface format when wanted and offered (1). vk_hdr_set_active: gfx_present.c says whether the swapchain is HDR10 (vk_hdr_active). vk_hdr_encode: // like vk_color_record, src's crop as PQ code values (RGBA16F, crop {0, 0, w, h}). vk_hdr_ui_target / vk_hdr_ui_compose: // the in-game menu's RGBA8 target, then blended onto the swapchain image. vk_hdr_to_srgb8: 10-bit PQ pixels to sRGB bytes. +int vk_hdr_armed(void); int vk_hdr_wanted(void); +int vk_hdr_needs_restart(void); // the menu's HDR row: the value set takes effect at the next start void vk_hdr_off(void); // probes that read back 8-bit swapchain pixels: SDR whatever the setting int vk_hdr_surface_format(const VkSurfaceFormatKHR *f, uint32_t n, VkSurfaceFormatKHR *out); void vk_hdr_metadata(VkSwapchainKHR sc, int new_sc); diff --git a/runtime/vk_device.c b/runtime/vk_device.c index d3482d3f..7b635f4f 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -86,7 +86,8 @@ static int create_instance(void) { // DLSS Frame Generation: Streamline's plugins call its naming functions even when they say it is off if (validate || (vk_dlssg_wanted() && instance_ext_present(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))) ext[n_ext++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; - if (vk_hdr_wanted() && instance_ext_present(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME)) // HDR10 surface formats + // armed, not wanted: the menu switches HDR on during the run (vk_hdr.c), and the extensions are fixed here + if (vk_hdr_armed() && instance_ext_present(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME)) // HDR10 surface formats ext[n_ext++] = VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME; vk_dlaa_instance_exts(ext, &n_ext, 12); VkApplicationInfo app = {.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "NX2navi", @@ -324,7 +325,7 @@ static int create_device(void) { // GPU timestamps on the CPU clock (vk_sync.c): the perf line splits GPU idle time into waiting for a submission or not. if (has_ext(ep, ne, VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME)) { g_vk.has_calibrated = 2; ext[n_ext++] = VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME; } else if (has_ext(ep, ne, VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME)) { g_vk.has_calibrated = 1; ext[n_ext++] = VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME; } - if (vk_hdr_wanted() && has_ext(ep, ne, VK_EXT_HDR_METADATA_EXTENSION_NAME)) { // the display's peak (vk_hdr_metadata) + if (vk_hdr_armed() && has_ext(ep, ne, VK_EXT_HDR_METADATA_EXTENSION_NAME)) { // the display's peak (vk_hdr_metadata) g_vk.has_hdr_metadata = 1; ext[n_ext++] = VK_EXT_HDR_METADATA_EXTENSION_NAME; } diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 6d00dccc..23fbc3ed 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2530,7 +2530,7 @@ static void support_fill(uint8_t *b, float w, float h, float point_size) { memcpy(b + 1408, pcss_params(), 16); // pcss (translator patch 0010): soft sun shadows in the cascade compares memcpy(b + 1424, shadow_spread_params(), 16); // shadow_spread (translator patch 0011): sun shadow filter width for (int i = 0; i < 73; i++) memcpy(b + 192 + 16 * i, &one, 4); // render scale 1: the host renders at guest size - if (vk_hdr_wanted()) { // read by the game's tonemap only, once vk_hdr.c tonemap_splice has grown its block past the translator's + if (vk_hdr_armed()) { // read by the game's tonemap only, once vk_hdr.c tonemap_splice has grown its block past the translator's float hdr[16]; vk_hdr_support(hdr); memcpy(b + 1440, hdr, sizeof hdr); diff --git a/runtime/vk_hdr.c b/runtime/vk_hdr.c index 836584bd..1b0d7962 100644 --- a/runtime/vk_hdr.c +++ b/runtime/vk_hdr.c @@ -1,8 +1,10 @@ // HDR output (NX2NAVI_HDR=1): an HDR10 swapchain (Rec. 2020 primaries, PQ) and a game picture tone mapped to the -// display's range. Settings: NX2NAVI_HDR (read at start); the nits NX2NAVI_HDR_PEAK_NITS (the display's peak), +// display's range. Settings: NX2NAVI_HDR, off and the tone mapper following the menu during a run (vk_hdr_wanted), the +// game's own HDR (native) from the start only; the nits NX2NAVI_HDR_PEAK_NITS (the display's peak), // NX2NAVI_HDR_GAME_NITS (the picture's white, "paper white") and NX2NAVI_HDR_UI_NITS (the HUD's and this menu's white); // NX2NAVI_HDR_GAMMA, NX2NAVI_HDR_TONEMAP and the grading sliders NX2NAVI_HDR_EXPOSURE .. NX2NAVI_HDR_GRADE, all read -// when the settings change (vk_hdr_support). With NX2NAVI_HDR=0 nothing here runs and the present is what it was. +// when the settings change (vk_hdr_support). With NX2NAVI_HDR=0 the swapchain is SDR, the spliced tonemap gives the +// game's own SDR picture (vk_hdr_support, p0.w = -1) and the present is what it was. // // The game's picture (docs/architecture.md, "HDR"). The game tonemaps its scene into an r11g11b10f target, upscales it // and draws the HUD into a second one (float, nothing in these passes clamps), and one last draw copies that into the @@ -45,24 +47,56 @@ // ---- settings -static int g_wanted = -1; +// What the start decides for the whole run (hdr_start, first asked when the Vulkan instance is made): +// - armed: the HDR path is set up, so the menu can switch HDR on and off without a restart: the instance and device +// get the HDR extensions (vk_device.c), the game's tonemap is spliced as its shaders load (vk_hdr_shader_swap) and +// every draw's support buffer carries the settings (vk_exec_draw.c). While the swapchain is SDR the spliced tonemap +// returns the game's own SDR result. Always, unless NX2NAVI_HDR_LIVE = 0 with NX2NAVI_HDR = 0 (the path left out as it +// was before the setting followed the menu: HDR then needs a restart) or a probe turned it off (vk_hdr_off). +// - native: NX2NAVI_HDR = native, the game's own HDR. The display is reported as HDR10 (ov_gfx.c +// QueryDisplayHdrStatus, with the peak and paper white of the settings), and the game then renders its HDR picture +// itself and presents it as PQ-encoded Rec. 2020 in A2B10G10R10 (format 64), which goes to the HDR10 swapchain as it +// is: no tonemap splice, no float picture, no encode (pictures still presented in 8 bits, before the switch, are +// encoded as for 1). The game asks once, so a change to or from native waits for a restart (vk_hdr_needs_restart). +static int g_armed = -1, g_native = -1; +static void hdr_start(void) { + if (g_armed >= 0) return; + g_native = !strcmp(cfg_value("NX2NAVI_HDR"), "native"); + g_armed = cfg_bool("NX2NAVI_HDR") || cfg_bool("NX2NAVI_HDR_LIVE"); // last: the mark that both are read +} + +int vk_hdr_armed(void) { + hdr_start(); + return g_armed; +} + +int vk_hdr_native(void) { return vk_hdr_armed() && g_native; } + +// HDR is on now: the setting as the menu has it, read again when the settings change. The swapchain follows at the +// next present (gfx_present.c window_settings_live makes it again) and vk_hdr_active then says what it got. A run that +// started with the game's own HDR keeps it: the game goes on presenting its PQ picture whatever the menu says. int vk_hdr_wanted(void) { - if (g_wanted < 0) g_wanted = cfg_bool("NX2NAVI_HDR"); - return g_wanted; + static unsigned seen = ~0u; + static int on; + if (!vk_hdr_armed()) return 0; + if (g_native) return 1; + unsigned v = cfg_version(); + if (__atomic_load_n(&seen, __ATOMIC_ACQUIRE) != v) { + __atomic_store_n(&on, cfg_bool("NX2NAVI_HDR") != 0, __ATOMIC_RELEASE); + __atomic_store_n(&seen, v, __ATOMIC_RELEASE); + } + return __atomic_load_n(&on, __ATOMIC_ACQUIRE); } -// NX2NAVI_HDR = native: the game's own HDR. The display is reported as HDR10 (ov_gfx.c QueryDisplayHdrStatus, with -// the peak and paper white of the settings), and the game then renders its HDR picture itself and presents it as -// PQ-encoded Rec. 2020 in A2B10G10R10 (format 64), which goes to the HDR10 swapchain as it is: no tonemap splice, no -// float picture, no encode (pictures still presented in 8 bits, before the switch, are encoded as for 1). -static int g_native = -1; -int vk_hdr_native(void) { - if (g_native < 0) g_native = !strcmp(cfg_value("NX2NAVI_HDR"), "native"); - return g_native && vk_hdr_wanted(); +// The menu's HDR row: what is set takes a restart. To or from the game's own HDR, and switching HDR on in a run that +// started without the HDR path (NX2NAVI_HDR_LIVE = 0). +int vk_hdr_needs_restart(void) { + hdr_start(); + return (!strcmp(cfg_value("NX2NAVI_HDR"), "native")) != g_native || (!g_armed && cfg_bool("NX2NAVI_HDR")); } -void vk_hdr_off(void) { g_wanted = 0; } +void vk_hdr_off(void) { g_native = 0, g_armed = 0; } // The settings, read again when they change. The replay (every draw's support buffer) and the present thread read // them: an update fills the snapshot not in use and then publishes it. @@ -529,7 +563,8 @@ fail: #undef PUT void vk_hdr_shader_swap(uint32_t **spirv, uint64_t *words) { - if (!vk_hdr_wanted() || vk_hdr_native() || !*spirv || *words < 5) return; + // armed, not wanted: a shader loaded while HDR is off must be ready for the menu switching it on + if (!vk_hdr_armed() || vk_hdr_native() || !*spirv || *words < 5) return; if (tonemap_splice(spirv, words)) { static volatile int32_t spliced; rt_log("hdr: the game's tonemap maps its picture onto the display's range (%d shaders so far)", @@ -618,8 +653,8 @@ void vk_hdr_image_gone(VkImage image) { // a texture image went away: forget i // ---- swapchain -// With NX2NAVI_HDR=1, the HDR10 format among the surface's (10-bit UNORM, ST 2084): 1 with *out set, else 0 and the -// reason logged once. +// While NX2NAVI_HDR is on (vk_hdr_wanted), the HDR10 format among the surface's (10-bit UNORM, ST 2084): 1 with *out +// set, else 0 and the reason logged once. int vk_hdr_surface_format(const VkSurfaceFormatKHR *f, uint32_t n, VkSurfaceFormatKHR *out) { if (!vk_hdr_wanted()) return 0; static int logged; From 44d409509443dc26a0acb6017f4e4e53948dfc52 Mon Sep 17 00:00:00 2001 From: epicmeme546 <178368684+epicmeme546@users.noreply.github.com> Date: Mon, 9 Nov 2026 23:27:02 -0500 Subject: [PATCH 02/13] Add Customize Launcher: colour schemes, a background picture of the player's own, animations and sound effects. The launcher's home screen has a Customize Launcher row. Color Scheme steps through six schemes (Kokiri Forest, the colours so far, Death Mountain, Zora's Domain, Gerudo Valley, Temple of Time and Shadow Temple): each is the first scheme with its woods, accents, backdrop and glow turned to other hues, so all keep the same light and dark steps and the same text. Background takes a picture file, cuts it to the window's shape and keeps a fitted copy in data/background.npy; Background Dimming darkens it and the panels turn a little see-through over it. Animations (on by default) make the cursor glide between rows, a new screen slide in, sparks drift over the backdrop and Navi bob in the title band; they move by the time that passed and stand still while the window is hidden. Sounds are short chimes made from sine tones, off until the player picks a loudness. Redrawing in another scheme takes about a third of a second: the blurred masks and the noise, which no colour changes, are kept, and compositing onto a solid picture is a plain mix. The selftest's screen capture passed window handles as plain ints, which failed for a handle above 2^31 and left some screens without their picture; its calls are typed now. Co-Authored-By: Claude Fable 5.2 --- docs/launcher.md | 30 +++- docs/releases/v0.6.2.md | 11 ++ launcher/settings.py | 5 + launcher/ui/app.py | 339 +++++++++++++++++++++++++++++++++++++--- launcher/ui/art.py | 255 ++++++++++++++++++++++++------ launcher/ui/picture.py | 218 ++++++++++++++++++++++++++ launcher/ui/selftest.py | 38 ++++- launcher/ui/sound.py | 134 ++++++++++++++++ launcher/winsys.py | 5 +- tests/launcher_test.py | 163 ++++++++++++++++++- 10 files changed, 1115 insertions(+), 83 deletions(-) create mode 100644 launcher/ui/picture.py create mode 100644 launcher/ui/sound.py diff --git a/docs/launcher.md b/docs/launcher.md index f61ac23a..971df128 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -33,6 +33,26 @@ Display: the launcher decodes it from the player's dump or built game into `data `runtime/menu_assets.c` does for the in-game menu) and uses the system's interface font until then. Every picture is drawn in code (`launcher/ui/art.py`); no texture, font or sound of the game is in the repository or the release. +Customize Launcher (on the home screen) makes the look the player's own: + +- Color Scheme: Kokiri Forest (the colours above), Death Mountain, Zora's Domain, Gerudo Valley, Temple of Time and + Shadow Temple. A scheme is the first one with its woods, accents, backdrop and glow turned to other hues + (`art.THEMES`, `art.recolor`), so each keeps the same light and dark steps and the same readable text. The window is + drawn again in the new colours at once. +- Background: a picture of the player's own behind the panels instead of the scheme's backdrop (`launcher/ui/picture.py`: + PNG, JPEG, WebP, BMP, GIF or TIFF, read with Pillow). It is cut to the window's shape around its middle and kept as + `data/background.npy`, so the launcher needs the file no more; Background Dimming darkens it (0 to 80 %) for the + text over it, the list panel and the description box turn a little see-through, and Remove Background brings the + scheme's backdrop back. +- Animations (on by default): the cursor glides from row to row, a new screen slides into place, sparks drift up + over the backdrop and Navi bobs in the title band with a pulsing halo (`App.animate`). They move by the time that + passed, stand still while the window is hidden or minimised, and Off stops them all. +- Sounds (off by default; Low, Medium or High): a short chime when the cursor moves, a choice changes, a row is + chosen, the game starts and a build ends or fails. The chimes are made from sine tones in `launcher/ui/sound.py` + and played from memory (PlaySound on Windows; `pw-play`, `paplay` or `aplay` elsewhere). + +The choices are `theme`, `background`, `background_dim`, `animations` and `sounds` in `data/launcher.json`. + Navi (`art.NAVI`, 22 x 21 pixels, drawn in whole pixels at every size) is also the icon of `NX2navi.exe`, `NX2navi-Setup.exe`, the window and its taskbar button (`art.navi_ico`, written into the release by `tools/make_launcher.py`). The window sets that icon on itself, and the setup's shortcuts carry the window's taskbar @@ -41,7 +61,8 @@ identity (`NX2navi.Launcher`), so the taskbar groups and pins it under NX2navi r Screens (`launcher/ui/app.py`): first run (dump, install folder, Desktop Shortcut, Check for Updates at Start, Update Channel, Begin Installation); the steps of an install, update or rebuild with their progress, the build log's last lines and Cancel, and after a failure Try Again and Copy Report; home (Play, Updates, Game Folder, Saves, -Rebuild the Game, Settings); saves (Export Save, Add Log and Settings, Export To, Import a Save, Import a Save Folder, +Rebuild the Game, Customize Launcher, Settings); customize (Color Scheme, Background, with a picture set Background +Dimming and Remove Background, Animations, Sounds, Back); saves (Export Save, Add Log and Settings, Export To, Import a Save, Import a Save Folder, Open Saves Folder, Back); settings (update check, Install Updates Automatically, Hide Launcher While Playing, Keep Build Cache, Update Channel, the folders, Open Logs Folder, Uninstall, Back); uninstall (keep or remove the built game). A screen with more rows than the window holds draws them closer together (`fit_rows`). @@ -66,7 +87,8 @@ app// launcher/, runtime/, recomp/, tools/, bin/nx2navi_xlate pydeps/ zbic, downloaded from PyPI on this PC toolchain/msys64/ucrt64 MSYS2's clang and libraries, downloaded from the MSYS2 repository on this PC toolchain/pkgs/ the downloaded package files -data/launcher.json settings; data/logs/launcher.log (and crash.log); data/cache/fonts +data/launcher.json settings; data/logs/launcher.log (and crash.log); data/cache/fonts; + data/background.npy (the player's background picture, fitted to the window) game/ the built game, unless another folder was chosen (its layout: README-BuildKit.md), with saves/ for saves to import and the backups imports make (below) ``` @@ -349,7 +371,9 @@ certificates made with the `openssl` command), the GitHub and URL feeds, staging pruning, unsafe archives, the background update (a release staged beside the running version without switching, the `staged.json` marker, a staged release installed after a restart, no install while the game runs, the install once it exited, nothing by itself with Install Updates Automatically off, a failed staging tried again after the next -check), the font decoding on a system font wrapped like the game's, `make_game.py` on a synthetic +check), the font decoding on a system font wrapped like the game's, the look (every colour scheme complete, the +background picture cut to the window's shape and kept, the sound effects as WAVs and off by default), `make_game.py` +on a synthetic `.nup` (events, skipped steps, `--rebuild` without the dump, step keys, the shaders key following the translator the build uses), the RomFS step beside the lift and compile and its failure stopping the build, the prompts step with a stand-in prompt tool (when it runs again, and that it diff --git a/docs/releases/v0.6.2.md b/docs/releases/v0.6.2.md index 976aaaf2..334ef247 100644 --- a/docs/releases/v0.6.2.md +++ b/docs/releases/v0.6.2.md @@ -7,3 +7,14 @@ Draft notes for the release after 0.6.1. The version number is not decided yet. import a zip someone shared. Attach the bug report zip to a report from a place in the game, so it can be reached and checked quickly. An import copies your current save to the `saves` folder of the game folder first; in the game it takes effect at the next start. +- Customize Launcher (on the launcher's home screen): six colour schemes, a background picture of your own with + adjustable dimming, animations (a gliding cursor, sliding screens, drifting sparks) and sound effects. The sounds + are off until you turn them on. +- HDR switches between off and the tone mapper in the settings menu without a restart. Only the game's native HDR + still waits for one. + +## Fixed + +- Settings menu, mouse: a setting changes only on the two framed arrows beside its value, and the arrows stay in one + place. A click anywhere on a row used to step the setting, and the left arrow moved with the value's width, so + clicking through the values landed on the row and stepped the wrong way. Holding the button on an arrow repeats. diff --git a/launcher/settings.py b/launcher/settings.py index 83d4c1fb..0418892d 100644 --- a/launcher/settings.py +++ b/launcher/settings.py @@ -15,6 +15,11 @@ DEFAULTS = { 'desktop_shortcut': True, 'save_export_dir': '', # where Saves > Export Save writes its zip; empty: the Downloads folder 'export_with_log': True, # Export Save adds the game's latest log and nx2navi.ini (for a bug report) + 'theme': 'kokiri', # the colour scheme (launcher/ui/art.py THEMES) + 'background': '', # the picture the player chose as the background; its fitted copy is data/background.npy + 'background_dim': 40, # how much that picture is darkened, in percent + 'animations': True, # the cursor glides, screens slide in, sparks drift, Navi bobs + 'sounds': 'off', # the chimes: off (until the player turns them on), low, medium or high 'feed_repo': '', # owner/name overriding feed.json, for testing a fork's releases 'github_token': '', # only for a private release repository; stored as plain text 'last_check': 0.0, diff --git a/launcher/ui/app.py b/launcher/ui/app.py index c6c14979..6f73aa65 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -8,7 +8,8 @@ The window is one Tk canvas at 1000 x 640 logical pixels, scaled to the display. flat cells and the orange choices are canvas items. Screens: first_run the dump, the install folder, three choices and Begin Installation - home Play, Updates, the game folder, Saves, Rebuild the Game, Settings + home Play, Updates, the game folder, Saves, Rebuild the Game, Customize Launcher, Settings + customize the colour scheme, the player's own background picture, animations and sounds saves Export Save (a zip for a bug report or another player), Import a Save (such a zip), a save folder job the steps of an install, update or rebuild, with their progress, and Cancel; after a failure Try Again and Copy Report @@ -16,9 +17,16 @@ flat cells and the orange choices are canvas items. Screens: uninstall what to remove, then Uninstall or Cancel Mouse, keyboard (arrows, Enter or Space, Esc, F5) and an XInput gamepad (d-pad or stick, A, B, Y) all move the cursor. + +The look is the player's (Customize Launcher): art.set_theme fills the palette every picture is drawn from, a picture +of the player's own can replace the backdrop (picture.py), and unless Animations is off the cursor glides between +rows, a new screen slides in, sparks drift over the backdrop and Navi bobs in the title band (animate). sound.py +plays a short chime for a move, a change, a choice and the end of a job. """ import argparse +import math import os +import random import shutil import sys import time @@ -29,7 +37,7 @@ from tkinter import filedialog from .. import NAME, build, feed, gameini, gamefiles, jobs, saves, versions, winsys from ..context import Context -from . import art +from . import art, picture, sound W, H = 1000, 640 BAND = (0, 1000, 56) # x0, x1, height: the whole top of the window, which is also its title bar @@ -39,7 +47,12 @@ ROWS = (140, 552, 96) # x0, x1, top LABEL_W = 222 # 54 % of the row GAP = 12 DESC = (614, 336) # x, width -P = art.PALETTE +P = art.PALETTE # the colour scheme in use: art.set_theme changes it in place +FRAME_MS = 15 # between two steps of the animations; they move by the time that really passed +SPARKS = 22 # the dust drifting over the backdrop +DEPTH = {'first_run': 0, 'home': 0, 'job': 1, 'saves': 1, 'settings': 1, 'customize': 1, 'uninstall': 2} +DIMS = (0, 20, 40, 60, 80) # Background Dimming, in percent +SOUND_LEVELS = (('off', 'Off'), ('low', 'Low'), ('medium', 'Medium'), ('high', 'High')) class Row: @@ -154,6 +167,7 @@ class App: enabled=not self.layout.dev and not args.selftest) self.row_gap = GAP self.message = '' + self.look_message = '' # why the last background picture was not taken, in Customize Launcher's descriptions self.save_message = '' # the last import's result, shown in the import rows' descriptions self.export_message = '' # the last Export Save result, shown in its description self.dumps = None @@ -166,6 +180,23 @@ class App: self.tb_hover = None self.drag = None self.ico = None + # the look: the colour scheme, the player's background picture at the window's size (None: the scheme's own + # backdrop), the sounds (none while the selftest draws the screens) + art.set_theme(self.settings.get('theme')) + self.bg_store = self.layout.data / 'background.npy' + self.picture = None + self.sounds = sound.Sounds(self.layout.cache, 'off' if args.selftest else self.settings.get('sounds'), ctx.log) + # the animations' state (animate): the clock and the last step's time, the cursor's place on its way to the + # focused row, how far the screen still stands from its place after a change, the sparks, Navi and its halo + self.anim_t = 0.0 + self.anim_last = time.perf_counter() + self.cur_xy = self.cur_target = None + self.cur_screen = None + self.slide = 0.0 + self.slide_applied = 0 + self.sparks = [] + self.navi_xy = None + self.halo = [] root.title(NAME) root.resizable(False, False) @@ -175,6 +206,7 @@ class App: self.canvas.pack() self.set_icon() self.load_fonts() + self.load_picture() self.draw_static() self.bind_input() self.start() @@ -300,15 +332,35 @@ class App: self.sprites[key] = art.photo(self.root, make()) return self.sprites[key] + def animated(self): + """Animations on (Customize Launcher); never while the selftest draws the screens, whose pictures must not + depend on the moment they are taken.""" + return bool(self.settings.get('animations')) and not self.args.selftest + + def load_picture(self): + """The player's background at the window's size, when one is set and its kept copy is readable.""" + self.picture = None + if self.settings.get('background'): + self.picture = picture.kept(self.bg_store, self.X(W), self.X(H)) + if self.picture is None: + self.ctx.log(f'background: {self.bg_store} is missing or unreadable; the colour scheme\'s backdrop') + + def see_through(self): + """How solid the panel and the description box are: a little see-through over the player's own picture.""" + return 0.8 if self.picture is not None else 1.0 + def draw_static(self): c, s = self.canvas, self.s c.delete('static') - c.create_image(0, 0, anchor='nw', image=self.sprite('backdrop', lambda: art.backdrop(self.X(W), self.X(H), s)), - tags='static') + dim = max(0, min(100, self.settings.get('background_dim', 40))) / 100.0 + c.create_image(0, 0, anchor='nw', tags='static', + image=self.sprite('backdrop', lambda: art.backdrop(self.X(W), self.X(H), s, self.picture, dim))) + self.make_sparks() # over the backdrop, under the panel and the band bx0, bx1, bh = BAND px0, ptop, px1 = PANEL c.create_image(self.X(px0), self.X(ptop), anchor='nw', tags='static', - image=self.sprite('panel', lambda: art.panel(self.X(px1) - self.X(px0), self.X(H - ptop), s))) + image=self.sprite('panel', lambda: art.panel(self.X(px1) - self.X(px0), self.X(H - ptop), s, + self.see_through()))) c.create_image(self.X(bx0), 0, anchor='nw', tags='static', image=self.sprite('band', lambda: art.band(self.X(bx1) - self.X(bx0), self.X(bh), s))) k = max(1, int(round(2 * s))) # Navi in whole pixels: two screen pixels per artwork pixel at 100 % @@ -317,16 +369,50 @@ class App: title_w = self.f['band'].measure(NAME) gap = self.X(12) x = self.X(W / 2) - (ew + gap + title_w) // 2 - c.create_image(x, self.X(bh / 2), anchor='w', image=emblem, tags='static') + # Navi's halo, behind her: a soft light that pulses while the animations run + size = self.X(bh) - 2 + if ('halo', size) not in self.sprites: + self.sprites[('halo', size)] = [art.photo(self.root, f) for f in art.halo_frames(size)] + self.halo = self.sprites[('halo', size)] + c.create_image(x + ew // 2, self.X(bh / 2), image=self.halo[0], tags=('static', 'halo')) + self.navi_xy = (x, self.X(bh / 2)) + c.create_image(x, self.X(bh / 2), anchor='w', image=emblem, tags=('static', 'navi')) tx = x + ew + gap - c.create_text(tx + 1, self.X(bh / 2) + self.X(2), text=NAME, anchor='w', font=self.f['band'], fill='#1a120a', - tags='static') + c.create_text(tx + 1, self.X(bh / 2) + self.X(2), text=NAME, anchor='w', font=self.f['band'], + fill=P['band_shadow'], tags='static') c.create_text(tx, self.X(bh / 2), text=NAME, anchor='w', font=self.f['band'], fill=P['band_text'], tags='static') - c.create_rectangle(0, 0, self.X(W) - 1, self.X(H) - 1, outline='#140e09', tags='static') + c.create_rectangle(0, 0, self.X(W) - 1, self.X(H) - 1, outline=P['frame'], tags='static') c.tag_lower('static') self.draw_title_buttons() + def make_sparks(self): + """The dust over the backdrop: SPARKS soft dots of three sizes, each with its own speed, sway and twinkle, at + places of their own from the first frame on. None while Animations is off.""" + self.sparks = [] + if not self.animated(): + return + rnd = random.Random(7) + for i in range(SPARKS): + size = self.X((5, 8, 12)[i % 3]) + pics = [self.sprite(('spark', size, a), lambda sz=size, al=a: art.spark(sz, al)) for a in (0.9, 0.55, 0.25)] + # logical pixels: its speed upwards a second, how far it sways to each side and how fast + sp = {'x': rnd.uniform(0, W), 'y': rnd.uniform(BAND[2], H), 'vy': rnd.uniform(4, 14), + 'sway': rnd.uniform(4, 16), 'rate': rnd.uniform(0.4, 1.1), 'phase': rnd.uniform(0, 6.28), + 'pics': pics, 'shown': 0} + sp['id'] = self.canvas.create_image(self.X(sp['x']), self.X(sp['y']), image=pics[0], tags=('static', 'spark')) + self.sparks.append(sp) + + def apply_look(self): + """Draw everything again after the colour scheme, the background or Animations changed.""" + art.set_theme(self.settings.get('theme')) + self.sprites.clear() + self.root.configure(background=P['window']) + self.canvas.configure(background=P['window']) + self.load_picture() + self.draw_static() + self.render() + def draw_title_buttons(self): """Minimise and close at the band's right end, lit when the mouse is over them.""" c, X = self.canvas, self.X @@ -385,7 +471,10 @@ class App: if not idx: return pos = idx.index(self.focus) if self.focus in idx else 0 + was = self.focus self.focus = idx[max(0, min(len(idx) - 1, pos + d))] + if self.focus != was: + self.sounds.play('move') self.render() def focused(self): @@ -403,6 +492,7 @@ class App: r.pick = max(0, min(len(r.actions) - 1, getattr(r, 'pick', 0) + d)) else: return + self.sounds.play('move' if r.kind == 'pair' else 'toggle') self.render() def activate(self): @@ -410,24 +500,36 @@ class App: if not r or not r.enabled: return if r.kind == 'toggle': + self.sounds.play('toggle') r.on(not r.get()) elif r.kind == 'enum': + self.sounds.play('toggle') r.right() elif r.kind == 'pair': + self.sounds.play('select') r.actions[getattr(r, 'pick', 0)][1]() return elif r.on: + self.sounds.play('back' if r.kind == 'action' and r.label == 'Back' else 'select') r.on() return self.render() + def home_row(self, label): + """The index of the home screen's row with this label, for coming back to it.""" + return next((i for i, r in enumerate(self.rows_home()) if r.label == label), None) + def back(self): + if self.screen in ('settings', 'uninstall', 'saves', 'customize', 'job'): + self.sounds.play('back') if self.screen == 'settings': - self.show('home') + self.show('home', self.home_row('Settings')) elif self.screen == 'uninstall': self.show('settings') elif self.screen == 'saves': - self.show('home', next((i for i, r in enumerate(self.rows_home()) if r.label == 'Saves'), None)) + self.show('home', self.home_row('Saves')) + elif self.screen == 'customize': + self.show('home', self.home_row('Customize Launcher')) elif self.screen == 'job' and self.job and self.job.result is None: self.cancel_job() elif self.screen == 'job': @@ -459,6 +561,7 @@ class App: r = self.rows[i] if r.kind == 'pair': r.pick = 0 if e.x < (r.rect[0] + r.rect[2]) / 2 else 1 + self.sounds.play('move') self.render() elif i is not None and self.rows[i].kind == 'pair': r = self.rows[i] @@ -494,14 +597,17 @@ class App: return if r.kind == 'toggle' and r.value_rect: x0, _, x1, _ = r.value_rect + self.sounds.play('toggle') r.on(e.x < (x0 + x1) / 2) self.render() elif r.kind == 'enum' and r.value_rect: x0, _, x1, _ = r.value_rect + self.sounds.play('toggle') (r.left if e.x < x0 + (x1 - x0) / 3 else r.right)() self.render() elif r.kind == 'pair': r.pick = 0 if e.x < (r.rect[0] + r.rect[2]) / 2 else 1 + self.sounds.play('select') r.actions[r.pick][1]() else: self.activate() @@ -511,6 +617,7 @@ class App: self.job.cancel() if self.bg.staging(): # what it downloaded is resumed, and its unfinished folder replaced, next time self.bg.job.cancel() + self.sounds.stop() self.root.destroy() # ---------------------------------------------------------------- screens @@ -550,6 +657,10 @@ class App: f'{versions.display(self.layout.version)}.') def show(self, screen, focus=None): + if screen != self.screen and self.screen is not None and self.animated(): + # another screen slides into place: from the right when it lies deeper, from the left on the way back + deeper = DEPTH.get(screen, 1) >= DEPTH.get(self.screen, 0) + self.slide = float(self.X(44) if deeper else -self.X(44)) self.screen = screen self.rows = getattr(self, 'rows_' + screen)() idx = self.focusables() @@ -764,10 +875,113 @@ class App: Row('value', 'Rebuild the Game', rebuild_value, rebuild_style, on=self.start_rebuild, desc=('Build the game again from the files of the last build. Only what changed is compiled. Use it ' 'after changing build tools, or if the game stopped starting.',)), + Row('value', 'Customize Launcher', art.THEMES[art.theme]['label'], 'arrow', + on=lambda: self.show('customize'), + desc=('Colour scheme, background picture, animations and sounds.',)), Row('value', 'Settings', '', 'arrow', on=lambda: self.show('settings'), desc=('Updates, the game folder, the logs and uninstalling.',)), ] + # ---- customize + + def rows_customize(self): + names = list(art.THEMES) + custom = bool(self.settings.get('background')) + + def scheme(d): + self.settings['theme'] = names[(names.index(art.theme) + d) % len(names)] + self.settings.save() + self.apply_look() + + def dim(d): + now = self.settings.get('background_dim', 40) + at = min(range(len(DIMS)), key=lambda i: abs(DIMS[i] - now)) + self.settings['background_dim'] = DIMS[max(0, min(len(DIMS) - 1, at + d))] + self.settings.save() + self.apply_look() + + def animations(v): + self.settings['animations'] = bool(v) + self.settings.save() + self.cur_xy = None + self.slide = 0.0 + self.apply_look() + + def sounds(d): + keys = [k for k, _ in SOUND_LEVELS] + now = self.settings.get('sounds') + at = keys.index(now) if now in keys else 0 + self.settings['sounds'] = keys[max(0, min(len(keys) - 1, at + d))] + self.settings.save() + self.sounds.set_level(self.settings['sounds']) + self.sounds.play('toggle') # the new loudness, heard at once + + bg_desc = ('Use a picture of your own as the background.',) + if self.look_message: + bg_desc = (self.look_message,) + bg_desc + rows = [ + Row('enum', 'Color Scheme', get=lambda: art.THEMES[art.theme]['label'], + left=lambda: scheme(-1), right=lambda: scheme(1), + desc=('The launcher\'s colours.',)), + Row('value', 'Background', Path(self.settings['background']).name if custom else 'Choose a Picture', + 'arrow', on=self.choose_background, desc=bg_desc), + ] + if custom: + rows += [ + Row('enum', 'Background Dimming', get=lambda: f'{self.settings.get("background_dim", 40)} %', + left=lambda: dim(-1), right=lambda: dim(1), + desc=('Darkens your picture so the text stays easy to read.',)), + Row('value', 'Remove Background', '', 'arrow', on=self.clear_background, + desc=('Go back to the colour scheme\'s own background.',)), + ] + rows += [ + Row('divider'), + Row('toggle', 'Animations', on=animations, get=lambda: bool(self.settings.get('animations')), + desc=('A gliding cursor, sliding screens and drifting sparks.',)), + Row('enum', 'Sounds', get=lambda: dict(SOUND_LEVELS).get(self.settings.get('sounds'), 'Off'), + left=lambda: sounds(-1), right=lambda: sounds(1), + desc=('Short chimes when you move and choose. Off by default.',)), + Row('divider'), + Row('action', 'Back', on=lambda: self.show('home', self.home_row('Customize Launcher')), height=40, + desc=('Back to the main menu.',)), + ] + return rows + + def choose_background(self): + start = self.settings.get('background') + start = str(Path(start).parent) if start and Path(start).parent.is_dir() else \ + str(winsys.known_folder('pictures') or Path.home()) + pattern = ' '.join('*' + t for t in picture.TYPES) + path = filedialog.askopenfilename(parent=self.root, title='Choose a background picture', initialdir=start, + filetypes=[('Pictures', pattern), ('All files', '*.*')]) + if not path: + return + try: + picture.keep(path, self.bg_store, self.root) + except (picture.PictureError, OSError, MemoryError, ValueError) as e: + self.ctx.log(f'background: {path}: {e!r}') + self.look_message = str(e) if isinstance(e, picture.PictureError) else \ + f'{Path(path).name} could not be used as the background: {e}' + self.sounds.play('error') + self.show('customize', self.focus) + return + self.look_message = '' + self.settings['background'] = str(Path(path)) + self.settings.save() + self.apply_look() + self.show('customize', self.focus) + + def clear_background(self): + self.settings['background'] = '' + self.settings.save() + try: + self.bg_store.unlink(missing_ok=True) + except OSError as e: + self.ctx.log(f'background: {e}') + self.look_message = '' + self.apply_look() + self.show('customize', 1) + def save_waiting(self, gd): """A save put into the game folder's saves folder (a shared zip, a save folder), waiting to be imported.""" if gd and Path(gd).is_dir(): @@ -999,6 +1213,7 @@ class App: self.render() return self.ctx.log(f'play: {exe}') + self.sounds.play('start') if self.settings.get('hide_while_playing'): self.root.withdraw() self.root.after(1000, self.watch_game) @@ -1275,6 +1490,8 @@ class App: j = self.job winsys.keep_awake(False) self.ctx.log(f'job {j.kind}: {j.result} {j.error}') + if j.result in ('ok', 'failed'): + self.sounds.play('done' if j.result == 'ok' else 'error') if j.result == 'ok': if j.after == 'restart': if winsys.restart(self.layout, ['--continue-update', versions.display(self.layout.version)]): @@ -1358,10 +1575,66 @@ class App: self.root.after(60000, self.minute) def animate(self): - if self.cursor_frames: - self.cursor_k = (self.cursor_k + 1) % len(self.cursor_frames) - self.canvas.itemconfigure('cursor', image=self.cursor_frames[self.cursor_k]) - self.root.after(250, self.animate) + """One step of everything that moves: the cursor on its way to the focused row and its breath, the screen + sliding into place, the sparks and Navi. Each moves by the time since the last step (the timer comes when + Tk gets to it, 16 to 50 ms apart), at most a tenth of a second's worth. With Animations off, and while the + window is hidden or minimised (the game is running), nothing moves and the timer runs slowly.""" + moving = False + try: + now = time.perf_counter() + dt = min(0.1, max(0.0, now - self.anim_last)) + self.anim_last = now + moving = self.animated() and self.root.state() == 'normal' + if moving: + self.anim_t += dt + self.step_slide(dt) + self.step_cursor(dt) + self.step_backdrop(dt) + except Exception as e: # a drawing error must not stop the timer + self.ctx.log(f'animate: {e!r}') + self.root.after(FRAME_MS if moving else 300, self.animate) + + def step_slide(self, dt): + if not self.slide: + return + left = self.slide * math.exp(-dt * 16) # most of the way in a fifth of a second + if abs(left) < 1: + left = 0.0 + self.canvas.move('dyn', int(left) - self.slide_applied, 0) + self.slide, self.slide_applied = left, int(left) + + def step_cursor(self, dt): + c = self.canvas + if self.cursor_frames: # one breath in 1.6 s + k = int(self.anim_t / 1.6 * len(self.cursor_frames)) % len(self.cursor_frames) + if k != self.cursor_k: + self.cursor_k = k + c.itemconfigure('cursor', image=self.cursor_frames[k]) + if self.cur_xy and self.cur_target and self.cur_xy != self.cur_target: + (x, y), (tx, ty) = self.cur_xy, self.cur_target + f = 1 - math.exp(-dt * 22) # there in about a tenth of a second + x, y = x + (tx - x) * f, y + (ty - y) * f + if abs(tx - x) < 0.6 and abs(ty - y) < 0.6: + x, y = tx, ty + self.cur_xy = (x, y) + c.coords('cursor', round(x) + self.slide_applied, round(y)) + + def step_backdrop(self, dt): + c, t = self.canvas, self.anim_t + for sp in self.sparks: # up, swaying, and back in at the bottom once under the title band + sp['y'] -= sp['vy'] * dt + if sp['y'] < BAND[2] - 8: + sp['y'] = H + 8 + c.coords(sp['id'], self.X(sp['x'] + math.sin(t * sp['rate'] + sp['phase']) * sp['sway']), self.X(sp['y'])) + k = int((math.sin(t * sp['rate'] * 2.3 + sp['phase'] * 3) + 1) * 1.49) # 0, 1 or 2: its twinkle + if k != sp['shown']: + sp['shown'] = k + c.itemconfigure(sp['id'], image=sp['pics'][k]) + if self.navi_xy: # Navi rises and sinks two pixels every 2.4 s, her halo pulses with her + x, y = self.navi_xy + c.coords('navi', x, y + round(math.sin(t * 2.6) * self.X(2))) + if self.halo: + c.itemconfigure('halo', image=self.halo[int(t / 2.4 * len(self.halo)) % len(self.halo)]) # ---------------------------------------------------------------- drawing @@ -1381,6 +1654,9 @@ class App: self.draw_status() self.draw_cursor() self.draw_desc() + self.slide_applied = int(self.slide) + if self.slide_applied: # drawn in place, then put where the slide has got to (step_slide) + c.move('dyn', self.slide_applied, 0) def fit_rows(self, top, bottom): """The height of each row and the gap between rows (logical pixels) that end the rows above bottom. A screen @@ -1421,7 +1697,7 @@ class App: def text(self, x, y, text, font, fill, anchor='w', shadow=True, width=0): c = self.canvas if shadow: - c.create_text(x + 1, y + 1, text=text, font=font, fill='#2a1e10', anchor=anchor, tags='dyn', + c.create_text(x + 1, y + 1, text=text, font=font, fill=P['text_shadow'], anchor=anchor, tags='dyn', width=width) return c.create_text(x, y, text=text, font=font, fill=fill, anchor=anchor, tags='dyn', width=width) @@ -1440,7 +1716,9 @@ class App: for k, (label, cb) in enumerate(parts): px0 = x0 + k * (w + gap) px1 = px0 + w if k < len(parts) - 1 else x1 - img = self.sprite(('label', px1 - px0, y1 - y0), lambda a=px1 - px0, b=y1 - y0: art.label_cell(a, b, s)) + lit = r is self.focused() and (r.kind == 'action' or getattr(r, 'pick', 0) == k) + img = self.sprite(('label', px1 - px0, y1 - y0, lit), + lambda a=px1 - px0, b=y1 - y0: art.label_cell(a, b, s, lit)) ring = max(1, int(round(s))) c.create_image(px0 - ring, y0 - ring, anchor='nw', image=img, tags='dyn') font = self.fit_font(self.f['action_big'] if r.h >= 50 else self.f['action'], label, @@ -1451,7 +1729,8 @@ class App: return lw = X(LABEL_W) lx1 = x0 + lw - img = self.sprite(('label', lw, y1 - y0), lambda: art.label_cell(lw, y1 - y0, s)) + lit = r is self.focused() + img = self.sprite(('label', lw, y1 - y0, lit), lambda: art.label_cell(lw, y1 - y0, s, lit)) ring = max(1, int(round(s))) c.create_image(x0 - ring, y0 - ring, anchor='nw', image=img, tags='dyn') tx = x0 + X(16) @@ -1468,8 +1747,8 @@ class App: size = X(22) b = self.sprite(('badge', size), lambda: art.badge(size, s)) c.create_image(bx, (y0 + y1) // 2, anchor='w', image=b, tags='dyn') - c.create_text(bx + (size + 4) // 2, (y0 + y1) // 2, text='!', font=self.f['badge'], fill='#3a2208', - tags='dyn') + c.create_text(bx + (size + 4) // 2, (y0 + y1) // 2, text='!', font=self.f['badge'], + fill=P['badge_text'], tags='dyn') vx0 = lx1 r.value_rect = (vx0, y0, x1, y1) c.create_rectangle(vx0, y0, x1 - 1, y1 - 1, fill=P['value'], outline=P['value_border'], width=1, tags='dyn') @@ -1500,7 +1779,7 @@ class App: fw = int((ix1 - ix0) * max(0.0, min(1.0, r.fraction))) if fw > 2: self.orange(ix0, iy0, ix0 + fw, iy1) - self.canvas.create_text(cx + 1, cy + 1, text=r.value, font=vfont, fill='#1a120a', tags='dyn') + self.canvas.create_text(cx + 1, cy + 1, text=r.value, font=vfont, fill=P['band_shadow'], tags='dyn') self.canvas.create_text(cx, cy, text=r.value, font=vfont, fill=P['on_text'], tags='dyn') elif r.style == 'orange_arrow': self.orange(ix0, iy0, ix1, iy1) @@ -1514,7 +1793,7 @@ class App: c.create_image(sx + label_w + X(10), cy, anchor='w', image=img, tags='dyn') elif r.style == 'arrow': cw, ch = X(9), X(12) - img = self.sprite(('chev', cw, ch, False, 'dim'), lambda: art.chevron(cw, ch, '#d9ccae')) + img = self.sprite(('chev', cw, ch, False, 'dim'), lambda: art.chevron(cw, ch, P['chevron_dim'])) c.create_image(x1 - X(16), cy, anchor='e', image=img, tags='dyn') if r.value: # a value, centred in the cell's room left of the arrow right = x1 - X(16) - cw - X(10) @@ -1546,6 +1825,7 @@ class App: r = self.focused() self.cursor_frames = [] if not r or not r.rect: + self.cur_xy = self.cur_target = None return if r.kind == 'pair' and r.parts: box = r.parts[getattr(r, 'pick', 0)][0] @@ -1557,7 +1837,14 @@ class App: frames, mg = self.glow(x1 - x0, y1 - y0) self.cursor_frames = frames self.cursor_k %= len(frames) - self.canvas.create_image(x0 - mg, y0 - mg, anchor='nw', image=frames[self.cursor_k], tags=('dyn', 'cursor')) + # The cursor is drawn where it has got to and animate takes it on to the row (step_cursor); on a new screen, + # while one slides in and with Animations off it stands on the row at once. + target = (float(x0 - mg), float(y0 - mg)) + glide = self.animated() and self.cur_xy and self.cur_screen == self.screen and not self.slide + self.cur_xy = self.cur_xy if glide else target + self.cur_target, self.cur_screen = target, self.screen + self.canvas.create_image(round(self.cur_xy[0]), round(self.cur_xy[1]), anchor='nw', + image=frames[self.cursor_k], tags=('dyn', 'cursor')) def glow(self, w, h): key = ('glow', w, h) @@ -1642,7 +1929,7 @@ class App: y += X(22) elif kind == 'bullet': (text,) = args - c.create_rectangle(x0 + padx + X(1), y + X(8), x0 + padx + X(6), y + X(13), fill='#c99a45', + c.create_rectangle(x0 + padx + X(1), y + X(8), x0 + padx + X(6), y + X(13), fill=P['bullet'], outline='', tags=('dyn', 'desc')) tid = c.create_text(x0 + padx + X(16), y, text=text, font=self.f['value'], fill=P['desc_text'], anchor='nw', width=inner - X(16), tags=('dyn', 'desc')) @@ -1670,7 +1957,7 @@ class App: hb = max(X(60), height) key = ('desc', w, hb // 4) if key not in self.sprites: - img, pad = art.desc_box(w, hb, self.s) + img, pad = art.desc_box(w, hb, self.s, self.see_through()) self.sprites[key] = (art.photo(self.root, img), pad) pic, pad = self.sprites[key] bid = c.create_image(x0 - pad, top - pad, anchor='nw', image=pic, tags='dyn') diff --git a/launcher/ui/art.py b/launcher/ui/art.py index fc0282c7..f1385bf5 100644 --- a/launcher/ui/art.py +++ b/launcher/ui/art.py @@ -6,9 +6,11 @@ the small icons. Tk's canvas draws lines and polygons without antialiasing on Wi shape is a picture; flat rectangles and text stay canvas items. Colours follow the game's options menu (docs/launcher.md, "Look"). Sizes are in logical pixels times the display -scale s. +scale s. PALETTE holds the colours of the colour scheme in use: set_theme fills it from THEMES, each scheme the +first one with its woods, accents, backdrop and glow turned to other hues. """ import base64 +import colorsys import re import struct import zlib @@ -17,7 +19,8 @@ import numpy as np # ---------------------------------------------------------------- colours -PALETTE = { +# The first colour scheme, Kokiri Forest: bronze wood on a misty dark green. +BASE = { 'window': '#06180d', 'panel': '#3a2b22', 'label_top': '#7a5c32', 'label_mid': '#775930', 'label_bottom': '#72552d', 'label_border': '#6a4f29', @@ -30,8 +33,88 @@ PALETTE = { 'ok': '#a7d68c', 'gold': '#e6b65a', 'band_text': '#f3ecdc', 'status': '#b9a988', 'log_text': '#cfc4ab', 'line': '#54442f', 'dot': '#5d4b36', 'filigree': '#7a6443', 'glow': '#e9ebee', 'button_hover': '#8a6c3f', 'close_hover': '#b0402c', + # the backdrop: the field's four heights, its mist, the see-through tint over it (edge, side, middle), the corners + 'scene_0': '#394244', 'scene_1': '#414b4c', 'scene_2': '#474234', 'scene_3': '#3e3523', 'mist': '#262a22', + 'tint_edge': '#05180d', 'tint_side': '#092615', 'tint_mid': '#0c2e1b', 'vignette': '#020e07', + # the title band's wood from top to bottom and the two lines under it + 'wood_0': '#4c3b26', 'wood_1': '#5a4529', 'wood_2': '#6b5232', 'wood_3': '#806239', + 'band_line_dark': '#2c2217', 'band_line_light': '#a9874f', + # the cursor's glow near and far from its frame, the sparks drifting over the backdrop, Navi's halo + 'glow_near': '#afc5e8', 'glow_far': '#96b4e6', 'spark': '#cfe6ff', 'halo': '#7fc4ff', + 'icon_frame': '#e9dcc0', 'icon_fill': '#efe4ca', 'chevron_dim': '#d9ccae', 'bullet': '#c99a45', + 'badge_top': '#fbbd45', 'badge_bottom': '#e2942a', 'badge_ring': '#3a2a14', 'badge_text': '#3a2208', + 'text_shadow': '#2a1e10', 'band_shadow': '#1a120a', 'frame': '#140e09', } +# The colours a scheme turns together, each group to one hue. +GROUPS = { + 'wood': ('panel', 'label_top', 'label_mid', 'label_bottom', 'label_border', 'ring', 'desc', 'desc_border', 'line', + 'dot', 'filigree', 'button_hover', 'wood_0', 'wood_1', 'wood_2', 'wood_3', 'band_line_dark', + 'band_line_light', 'value', 'value_border', 'text_shadow', 'band_shadow', 'frame'), + 'accent': ('orange', 'orange_light', 'orange_dark', 'gold', 'badge_top', 'badge_bottom', 'badge_ring', + 'badge_text', 'bullet'), + 'scene': ('window', 'scene_0', 'scene_1', 'scene_2', 'scene_3', 'mist', 'tint_edge', 'tint_side', 'tint_mid', + 'vignette'), + 'glow': ('glow', 'glow_near', 'glow_far', 'spark', 'halo'), + 'text': ('text', 'text_bright', 'value_text', 'desc_text', 'muted', 'band_text', 'status', 'log_text', 'on_text', + 'icon_frame', 'icon_fill', 'chevron_dim'), +} + +# The colour schemes of Customize Launcher, in the order the row steps through them. Each group: (hue in degrees or +# None to keep each colour's own, saturation factor, lightness factor, saturation added), applied to the first +# scheme's colours (recolor), so a scheme keeps the first one's light and dark steps. 'set': colours given as they are. +THEMES = { + 'kokiri': {'label': 'Kokiri Forest'}, + 'death_mountain': {'label': 'Death Mountain', 'wood': (14, 0.95, 0.80, 0), 'accent': (16, 1.05, 1.0, 0), + 'scene': (6, 1.0, 1.0, 0.25), 'glow': (30, 1.0, 1.0, 0), 'text': (30, 1.0, 1.0, 0)}, + 'zora': {'label': "Zora's Domain", 'wood': (207, 0.75, 0.92, 0), 'accent': (188, 0.95, 1.0, 0), + 'scene': (200, 1.0, 1.05, 0.2), 'glow': (185, 1.0, 1.0, 0), 'text': (200, 0.55, 1.02, 0)}, + 'gerudo': {'label': 'Gerudo Valley', 'wood': (26, 1.15, 1.0, 0), 'accent': (352, 0.9, 1.0, 0), + 'scene': (318, 0.8, 0.95, 0.15), 'glow': (45, 1.0, 1.0, 0), 'text': (36, 1.0, 1.0, 0)}, + 'temple': {'label': 'Temple of Time', 'wood': (220, 0.22, 0.95, 0), 'accent': (46, 0.95, 1.02, 0), + 'scene': (222, 0.55, 1.0, 0.05), 'glow': (48, 1.0, 1.0, 0.5), 'text': (48, 0.45, 1.02, 0)}, + 'shadow': {'label': 'Shadow Temple', 'wood': (268, 0.55, 0.78, 0), 'accent': (286, 0.75, 1.0, 0), + 'scene': (272, 0.9, 0.8, 0.2), 'glow': (280, 1.0, 1.0, 0.2), 'text': (270, 0.5, 1.0, 0)}, +} +DEFAULT_THEME = 'kokiri' + + +def recolor(hexcolor, hue, sat=1.0, light=1.0, add=0.0): + """hexcolor turned to hue (degrees; None keeps its own), its saturation times sat plus add, its lightness times + light.""" + h = hexcolor.lstrip('#') + r, g, b = (int(h[i:i + 2], 16) / 255.0 for i in (0, 2, 4)) + hh, ll, ss = colorsys.rgb_to_hls(r, g, b) + if hue is not None: + hh = (hue % 360) / 360.0 + r, g, b = colorsys.hls_to_rgb(hh, max(0.0, min(1.0, ll * light)), max(0.0, min(1.0, ss * sat + add))) + return '#%02x%02x%02x' % tuple(int(round(v * 255)) for v in (r, g, b)) + + +def theme_palette(name): + """The colours of scheme name (an unknown name: the first scheme's).""" + t = THEMES.get(name) or THEMES[DEFAULT_THEME] + pal = dict(BASE) + for group, keys in GROUPS.items(): + if group in t: + for k in keys: + pal[k] = recolor(BASE[k], *t[group]) + pal.update(t.get('set', {})) + return pal + + +PALETTE = dict(BASE) +theme = DEFAULT_THEME + + +def set_theme(name): + """Fills PALETTE (in place: the window holds a reference) with scheme name's colours; the name in effect.""" + global theme + theme = name if name in THEMES else DEFAULT_THEME + PALETTE.clear() + PALETTE.update(theme_palette(theme)) + return theme + def rgb(hexcolor): h = hexcolor.lstrip('#') @@ -89,6 +172,20 @@ def ico_bytes(images): # ---------------------------------------------------------------- drawing primitives +# The blurred masks and the noise depend on sizes only, not on colours: kept here, a change of the colour scheme +# paints them again in its colours without computing them again (most of the time a redraw took). The arrays kept +# are read, never written. +_KEPT = {} + + +def kept(key, make): + if key not in _KEPT: + if len(_KEPT) >= 160: # row and box sizes of every screen stay far below this + _KEPT.clear() + _KEPT[key] = make() + return _KEPT[key] + + def new(h, w, color=None, alpha=0.0): img = np.zeros((h, w, 4), dtype=np.float32) if color is not None: @@ -107,17 +204,37 @@ def over(dst, src, x=0, y=0): s = src[y0 - y:y1 - y, x0 - x:x1 - x] d = dst[y0:y1, x0:x1] sa, da = s[..., 3:4], d[..., 3:4] - oa = sa + da * (1 - sa) - oc = np.where(oa > 0, (s[..., :3] * sa + d[..., :3] * da * (1 - sa)) / np.maximum(oa, 1e-6), 0) + if da.min() >= 1: # onto a solid picture (the backdrop's layers): a plain mix, the result stays solid + d[..., :3] += (s[..., :3] - d[..., :3]) * sa + return dst + under = da * (1 - sa) + oa = sa + under + oc = s[..., :3] * sa + oc += d[..., :3] * under + oc /= np.maximum(oa, 1e-6) # where both are clear the sum above is 0, and stays 0 d[..., :3], d[..., 3:4] = oc, oa return dst def paint(img, mask, color, alpha=1.0): """Paint a colour through a coverage mask (HxW, 0..1) onto img.""" - layer = new(*mask.shape, color) - layer[..., 3] = np.clip(mask, 0, 1) * alpha - return over(img, layer) + if mask.shape != img.shape[:2]: + layer = new(*mask.shape, color) + layer[..., 3] = np.clip(mask, 0, 1) * alpha + return over(img, layer) + # over() with one colour for the whole layer, without making the layer + sa = (np.clip(mask, 0, 1) * alpha)[..., None].astype(np.float32) + c, da = rgb(color), img[..., 3:4] + if da.min() >= 1: + img[..., :3] += (c - img[..., :3]) * sa + return img + under = da * (1 - sa) + oa = sa + under + oc = img[..., :3] * under + oc += c * sa + oc /= np.maximum(oa, 1e-6) + img[..., :3], img[..., 3:4] = oc, oa + return img def vgradient(h, w, stops): @@ -174,6 +291,11 @@ def fractal(h, w, period_x, period_y=None, octaves=3, seed=0, contrast=0.4): def _fractal01(h, w, period_x, period_y=None, octaves=3, seed=0): + return kept(('fractal', h, w, period_x, period_y, octaves, seed), + lambda: _fractal01_make(h, w, period_x, period_y, octaves, seed)) + + +def _fractal01_make(h, w, period_x, period_y, octaves, seed): period_y = period_y or period_x out = np.zeros((h, w), dtype=np.float32) amp, total = 1.0, 0.0 @@ -340,56 +462,97 @@ FILIGREE_PATHS = ( # ---------------------------------------------------------------- the window's pieces -def backdrop(W, H, s): - """The whole window behind the panels: a dim misty field under a see-through dark green, darker at the sides.""" - img = new(H, W, PALETTE['window'], 1.0) +def backdrop(W, H, s, picture=None, dim=0.4): + """The whole window behind the panels: a dim misty field under a see-through tint of the colour scheme, darker at + the sides. picture: the player's own background instead of the field, an H x W x 3 array in 0..1 (the window's + size), darkened by dim (0 as it is .. 1 the scheme's darkest colour) so the text over it stays readable.""" + P = PALETTE + img = new(H, W, P['window'], 1.0) + pad = int(45 * s) + vig = kept(('vignette', W, H, s), lambda: 1 - box_blur(np.pad(rect_mask(H, W, 0, 0, W, H), pad, + constant_values=0), 45 * s)[pad:-pad, pad:-pad]) + if picture is not None: + img[..., :3] = picture[:H, :W] + paint(img, np.ones((H, W), dtype=np.float32), P['tint_edge'], max(0.0, min(1.0, dim))) + paint(img, np.clip(vig * 1.6, 0, 1), P['vignette'], 0.45) + return img m = int(30 * s) scene = new(H, W - 2 * m) - scene[..., :3] = vgradient(H, W - 2 * m, [(0, '#394244'), (0.36, '#414b4c'), (0.60, '#474234'), (1, '#3e3523')]) + scene[..., :3] = vgradient(H, W - 2 * m, [(0, P['scene_0']), (0.36, P['scene_1']), (0.60, P['scene_2']), + (1, P['scene_3'])]) scene[..., 3] = 1 over(img, scene, m, 0) - band = np.zeros((H, W), dtype=np.float32) - y0, y1 = int(250 * s), int(330 * s) - t = (np.arange(y1 - y0) + 0.5) / max(y1 - y0, 1) - band[y0:y1, m:W - m] = (np.interp(t, [0, 0.55, 1], [0, 0.85, 0]))[:, None] - paint(img, box_blur(band, 10 * s), '#262a22') - over(img, hgradient_rgba(H, W, [(0, '#05180d', 0.96), (0.14, '#092615', 0.82), (0.5, '#0c2e1b', 0.72), - (0.86, '#092615', 0.82), (1, '#05180d', 0.96)])) - inner = rect_mask(H, W, 0, 0, W, H) - vig = 1 - box_blur(np.pad(inner, int(45 * s), constant_values=0), 45 * s)[int(45 * s):-int(45 * s), - int(45 * s):-int(45 * s)] - paint(img, np.clip(vig * 1.6, 0, 1), '#020e07', 0.6) + def mist(): + band = np.zeros((H, W), dtype=np.float32) + y0, y1 = int(250 * s), int(330 * s) + t = (np.arange(y1 - y0) + 0.5) / max(y1 - y0, 1) + band[y0:y1, m:W - m] = (np.interp(t, [0, 0.55, 1], [0, 0.85, 0]))[:, None] + return box_blur(band, 10 * s) + paint(img, kept(('mist', W, H, s), mist), P['mist']) + over(img, hgradient_rgba(H, W, [(0, P['tint_edge'], 0.96), (0.14, P['tint_side'], 0.82), (0.5, P['tint_mid'], 0.72), + (0.86, P['tint_side'], 0.82), (1, P['tint_edge'], 0.96)])) + paint(img, np.clip(vig * 1.6, 0, 1), P['vignette'], 0.6) return img +def spark(size, alpha=1.0): + """One spark of the dust drifting over the backdrop: a soft round dot of the scheme's spark colour, size pixels + across, brightest in its middle.""" + ys = (np.arange(size, dtype=np.float32) + 0.5) / size * 2 - 1 + d = np.hypot(ys[:, None], ys[None, :]) + img = new(size, size, PALETTE['spark']) + img[..., 3] = np.clip(1 - d, 0, 1) ** 1.6 * alpha + core = np.clip(1 - d * 2.6, 0, 1) + img[..., :3] += (1 - img[..., :3]) * core[..., None] # a white middle + return img + + +def halo_frames(size, n=12): + """The light around Navi in the title band: a soft disc of the scheme's halo colour, n frames of one slow pulse.""" + ys = (np.arange(size, dtype=np.float32) + 0.5) / size * 2 - 1 + d = np.hypot(ys[:, None], ys[None, :]) + fall = np.clip(1 - d, 0, 1) ** 2 + frames = [] + for k in range(n): + t = 0.5 - 0.5 * np.cos(2 * np.pi * k / n) + img = new(size, size, PALETTE['halo']) + img[..., 3] = fall * (0.30 + 0.30 * t) + frames.append(img) + return frames + + def band(w, h, s): """The wooden title band, its two-line bottom edge and the shadow under it; (image, extra height below).""" pad = int(14 * s) img = new(h + pad, w) - wood = vgradient(h, w, [(0, '#4c3b26'), (0.30, '#5a4529'), (0.65, '#6b5232'), (1, '#806239')]) + wood = vgradient(h, w, [(0, PALETTE['wood_0']), (0.30, PALETTE['wood_1']), (0.65, PALETTE['wood_2']), + (1, PALETTE['wood_3'])]) wood = overlay(wood, fractal(h, w, 300 * s, 11 * s, 3, seed=9), 0.32) wood = overlay(wood, fractal(h, w, 33 * s, None, 3, seed=4), 0.2) - shade = rect_mask(h + pad, w, 0, 0, w, h + 4 * s) - over(img, shadow(h + pad, w, shade, '#000000', 0.6, 16 * s, 0, 5 * s)) + over(img, kept(('band shadow', w, h, s), lambda: shadow(h + pad, w, rect_mask(h + pad, w, 0, 0, w, h + 4 * s), + '#000000', 0.6, 16 * s, 0, 5 * s))) top = new(h, w) top[..., :3], top[..., 3] = wood, 1 over(img, top) - paint(img, rect_mask(h + pad, w, 0, h, w, h + 2 * s), '#2c2217') - paint(img, rect_mask(h + pad, w, 0, h + 2 * s, w, h + 4 * s), '#a9874f') + paint(img, rect_mask(h + pad, w, 0, h, w, h + 2 * s), PALETTE['band_line_dark']) + paint(img, rect_mask(h + pad, w, 0, h + 2 * s, w, h + 4 * s), PALETTE['band_line_light']) return img -def panel(w, h, s): - """The left list panel with the shadow on its right.""" +def panel(w, h, s, alpha=1.0): + """The left list panel with the shadow on its right. alpha below 1 lets the player's own background show through + it.""" sh = int(26 * s) img = new(h, w + sh) - body = rect_mask(h, w + sh, 0, 0, w, h) - over(img, shadow(h, w + sh, body, '#000000', 0.35, 18 * s, 6 * s, 0)) + over(img, kept(('panel shadow', w, h, s), lambda: shadow(h, w + sh, rect_mask(h, w + sh, 0, 0, w, h), '#000000', + 0.35, 18 * s, 6 * s, 0))) col = np.broadcast_to(rgb(PALETTE['panel']), (h, w, 3)).copy() col = overlay(col, fractal(h, w, 33 * s, None, 3, seed=4), 0.18) layer = new(h, w) layer[..., :3], layer[..., 3] = col, 1 over(img, layer) + if alpha < 1: + img[:, :w, 3] = alpha return img @@ -408,7 +571,7 @@ def label_cell(w, h, s, bright=False): return img -def desc_box(w, h, s): +def desc_box(w, h, s, alpha=1.0): pad = int(20 * s) img = new(h + 2 * pad, w + 2 * pad) body = rect_mask(h + 2 * pad, w + 2 * pad, pad, pad, pad + w, pad + h) @@ -418,26 +581,28 @@ def desc_box(w, h, s): layer = new(h, w) layer[..., :3], layer[..., 3] = col, 1 over(img, layer, pad, pad) + if alpha < 1: # the player's own background shows through the box, not through its border + img[pad:pad + h, pad:pad + w, 3] = alpha paint(img, ring_mask(h + 2 * pad, w + 2 * pad, pad, pad, pad + w, pad + h, max(1, round(s))), PALETTE['desc_border']) return img, pad -def glow_frames(w, h, s, n=8): - """The selection cursor around a w x h box: a 3 px white frame 6 px outside it with a soft blue glow inside and - out, n frames of one breath. Returns (frames, margin) where the box sits at (margin, margin).""" +def glow_frames(w, h, s, n=12): + """The selection cursor around a w x h box: a 3 px white frame 6 px outside it with a soft glow of the scheme's + colour inside and out, n frames of one breath. Returns (frames, margin) where the box sits at (margin, margin).""" gap, thick, spread = 6 * s, 3 * s, 16 * s mg = int(gap + spread) H, W = h + 2 * mg, w + 2 * mg ring = ring_mask(H, W, mg - gap, mg - gap, mg + w + gap, mg + h + gap, thick) - soft_out = box_blur(ring, 6 * s) - soft_far = box_blur(ring, 12 * s) + soft_out = kept(('glow near', w, h, s), lambda: np.clip(box_blur(ring, 6 * s) * 2.8, 0, 1)) + soft_far = kept(('glow far', w, h, s), lambda: np.clip(box_blur(ring, 12 * s) * 2.2, 0, 1)) frames = [] for k in range(n): t = 0.5 - 0.5 * np.cos(2 * np.pi * k / n) img = new(H, W) - paint(img, np.clip(soft_far * 2.2, 0, 1), '#96b4e6', 0.35 + 0.25 * t) - paint(img, np.clip(soft_out * 2.8, 0, 1), '#afc5e8', 0.7 + 0.3 * t) + paint(img, soft_far, PALETTE['glow_far'], 0.35 + 0.25 * t) + paint(img, soft_out, PALETTE['glow_near'], 0.7 + 0.3 * t) paint(img, ring, PALETTE['glow']) frames.append(img) return frames, mg @@ -574,12 +739,12 @@ def framed_icon(size, s, kind): """The square-framed icon of a big row (play or refresh), like the icon frames of the game's menu rows.""" img = new(size, size) t = 1.5 * s - paint(img, ring_mask(size, size, 0.5, 0.5, size - 0.5, size - 0.5, t), '#e9dcc0') + paint(img, ring_mask(size, size, 0.5, 0.5, size - 0.5, size - 0.5, t), PALETTE['icon_frame']) c = size / 2 if kind == 'play': k = size / 32 paint(img, polygon_mask(size, size, [(c - 5 * k, c - 7 * k), (c + 7 * k, c), (c - 5 * k, c + 7 * k)]), - '#efe4ca') + PALETTE['icon_fill']) elif kind == 'refresh': import math k = size / 32 @@ -594,7 +759,7 @@ def framed_icon(size, s, kind): [(c + r * math.cos(math.radians(160)) + 3.5 * k, c + r * math.sin(math.radians(160)) + 1 * k), (c + r * math.cos(math.radians(160)), c + r * math.sin(math.radians(160))), (c + r * math.cos(math.radians(160)) - 0.5 * k, c + r * math.sin(math.radians(160)) + 4 * k)]] - paint(img, stroke_mask(size, size, arcs + heads, 2.2 * k), '#efe4ca') + paint(img, stroke_mask(size, size, arcs + heads, 2.2 * k), PALETTE['icon_fill']) return img @@ -602,11 +767,11 @@ def badge(size, s): """The orange "!" disc beside Updates when a new version is ready (the "!" itself is text).""" img = new(size + 4, size + 4) ring = stroke_mask(size + 4, size + 4, [[((size + 4) / 2, (size + 4) / 2)]], size + 1.5 * s) - over(img, shadow(size + 4, size + 4, ring, '#fbbd45', 0.6, 6 * s)) - paint(img, ring, '#3a2a14') + over(img, shadow(size + 4, size + 4, ring, PALETTE['badge_top'], 0.6, 6 * s)) + paint(img, ring, PALETTE['badge_ring']) disc = stroke_mask(size + 4, size + 4, [[((size + 4) / 2, (size + 4) / 2)]], size) layer = new(size + 4, size + 4) - layer[..., :3] = vgradient(size + 4, size + 4, [(0, '#fbbd45'), (1, '#e2942a')]) + layer[..., :3] = vgradient(size + 4, size + 4, [(0, PALETTE['badge_top']), (1, PALETTE['badge_bottom'])]) layer[..., 3] = disc over(img, layer) return img diff --git a/launcher/ui/picture.py b/launcher/ui/picture.py new file mode 100644 index 00000000..ae9d6699 --- /dev/null +++ b/launcher/ui/picture.py @@ -0,0 +1,218 @@ +"""The player's own background picture (Customize Launcher): read a picture file into a numpy array, fit it to the +window's shape and keep that copy in data/background.npy, so the launcher shows it without the file it came from. + +Pillow reads the file where the launcher's Python has it (a release's does: tools/requirements-launcher.txt), turned +the way its EXIF data says. Without Pillow (a checkout's Python may lack it), GDI+ reads it on Windows (PNG, JPEG, +BMP, GIF, TIFF) and Tk elsewhere (PNG, GIF; the pixels come over as a PPM file). The kept copy is 8-bit RGB at +KEEP_W x KEEP_H, 1.6 times the window's logical size: enough for a display scaled to 200 %, and five megabytes on +disk. +""" +import ctypes +import os +import tempfile +from pathlib import Path + +import numpy as np + +KEEP_W, KEEP_H = 1600, 1024 # the window's 1000 x 640 times 1.6 +MAX_SIDE, MAX_PIXELS = 16384, 120_000_000 +try: + from PIL import Image, ImageOps +except ImportError: + Image = None +# the file types the dialog offers: what the reader in use takes (load) +TYPES = ('.png', '.jpg', '.jpeg', '.webp', '.bmp', '.gif', '.tif', '.tiff') if Image else ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tif', '.tiff') if os.name == 'nt' else ('.png', '.gif') + + +class PictureError(Exception): + """The file is no picture this PC can read; the text is for the player.""" + + +def _too_large(path, w, h): + return PictureError(f'{Path(path).name} is {w} x {h} pixels; the background takes up to {MAX_SIDE} pixels a side ' + f'and {MAX_PIXELS // 1_000_000} megapixels.') + + +def _load_pillow(path): + """The picture as an H x W x 3 uint8 array, read by Pillow and turned upright (a phone's photo says in its EXIF + data which way is up).""" + try: + with Image.open(path) as im: + w, h = im.size + if not w or not h or w > MAX_SIDE or h > MAX_SIDE or w * h > MAX_PIXELS: + raise _too_large(path, w, h) + return np.asarray(ImageOps.exif_transpose(im).convert('RGB')).copy() + except PictureError: + raise + except Exception as e: # Pillow raises its own errors, OSError and SyntaxError for files it cannot read + raise PictureError(f'{Path(path).name} is not a picture the launcher can read: {e}') from None + + +def _load_gdiplus(path): + """The picture as an H x W x 3 uint8 array, read by GDI+ (part of Windows).""" + from ctypes import wintypes + + class StartupInput(ctypes.Structure): + _fields_ = [('GdiplusVersion', ctypes.c_uint32), ('DebugEventCallback', ctypes.c_void_p), + ('SuppressBackgroundThread', wintypes.BOOL), ('SuppressExternalCodecs', wintypes.BOOL)] + + class BitmapData(ctypes.Structure): + _fields_ = [('Width', ctypes.c_uint), ('Height', ctypes.c_uint), ('Stride', ctypes.c_int), + ('PixelFormat', ctypes.c_int), ('Scan0', ctypes.c_void_p), ('Reserved', ctypes.c_void_p)] + + g = ctypes.WinDLL('gdiplus') + vp, pvp = ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p) + g.GdiplusStartup.argtypes = [pvp, ctypes.POINTER(StartupInput), vp] + g.GdiplusShutdown.argtypes = [vp] + g.GdipCreateBitmapFromFile.argtypes = [ctypes.c_wchar_p, pvp] + g.GdipGetImageWidth.argtypes = g.GdipGetImageHeight.argtypes = [vp, ctypes.POINTER(ctypes.c_uint)] + g.GdipBitmapLockBits.argtypes = [vp, ctypes.POINTER(ctypes.c_int * 4), ctypes.c_uint, ctypes.c_int, + ctypes.POINTER(BitmapData)] + g.GdipBitmapUnlockBits.argtypes = [vp, ctypes.POINTER(BitmapData)] + g.GdipDisposeImage.argtypes = [vp] + token = vp() + if g.GdiplusStartup(ctypes.byref(token), ctypes.byref(StartupInput(1, None, 0, 0)), None): + raise PictureError('Windows could not start its picture reader (GDI+).') + bmp = vp() + try: + if g.GdipCreateBitmapFromFile(str(path), ctypes.byref(bmp)) or not bmp: + raise PictureError(f'{Path(path).name} is not a picture Windows can read (PNG, JPEG, BMP, GIF or TIFF).') + w, h = ctypes.c_uint(), ctypes.c_uint() + g.GdipGetImageWidth(bmp, ctypes.byref(w)) + g.GdipGetImageHeight(bmp, ctypes.byref(h)) + w, h = w.value, h.value + if not w or not h or w > MAX_SIDE or h > MAX_SIDE or w * h > MAX_PIXELS: + raise _too_large(path, w, h) + data = BitmapData() + rect = (ctypes.c_int * 4)(0, 0, w, h) + # ImageLockModeRead, PixelFormat32bppARGB: GDI+ converts whatever the file holds + if g.GdipBitmapLockBits(bmp, ctypes.byref(rect), 1, 0x26200A, ctypes.byref(data)) or not data.Scan0: + raise PictureError(f'{Path(path).name} could not be decoded.') + try: + stride = abs(data.Stride) + raw = np.frombuffer(ctypes.string_at(data.Scan0, stride * h), dtype=np.uint8).reshape(h, stride) + bgra = raw[:, :w * 4].reshape(h, w, 4) + if data.Stride < 0: # bottom-up rows + bgra = bgra[::-1] + return bgra[..., [2, 1, 0]].copy() + finally: + g.GdipBitmapUnlockBits(bmp, ctypes.byref(data)) + finally: + if bmp: + g.GdipDisposeImage(bmp) + g.GdiplusShutdown(token) + + +def read_ppm(data): + """A binary PPM (P6, 8 bits a channel) as an H x W x 3 uint8 array.""" + fields, pos = [], 0 + while len(fields) < 4: # magic, width, height, maximum: separated by white space, comments from # to line end + while data[pos:pos + 1].isspace(): + pos += 1 + if data[pos:pos + 1] == b'#': + pos = data.index(b'\n', pos) + 1 + continue + end = pos + while end < len(data) and not data[end:end + 1].isspace(): + end += 1 + fields.append(data[pos:end]) + pos = end + if fields[0] != b'P6' or int(fields[3]) != 255: + raise PictureError('not an 8-bit binary PPM') + w, h = int(fields[1]), int(fields[2]) + return np.frombuffer(data, dtype=np.uint8, count=w * h * 3, offset=pos + 1).reshape(h, w, 3).copy() + + +def _load_tk(path, master): + """The picture read by Tk (PNG, GIF) and written out as a PPM, which is three bytes a pixel after a short header.""" + import tkinter as tk + try: + photo = tk.PhotoImage(master=master, file=str(path)) + except tk.TclError: + raise PictureError(f'{Path(path).name} is not a PNG or GIF picture.') from None + w, h = photo.width(), photo.height() + if not w or not h or w > MAX_SIDE or h > MAX_SIDE or w * h > MAX_PIXELS: + raise _too_large(path, w, h) + fd, tmp = tempfile.mkstemp(suffix='.ppm') + os.close(fd) + try: + photo.write(tmp, format='ppm') + return read_ppm(Path(tmp).read_bytes()) + finally: + os.unlink(tmp) + + +def load(path, master=None): + """The picture file as an H x W x 3 uint8 array. master: a Tk window, needed where Tk reads the file.""" + if not Path(path).is_file(): + raise PictureError(f'{path} is not there.') + if Image: + return _load_pillow(path) + if os.name == 'nt': + try: + return _load_gdiplus(path) + except OSError as e: # no gdiplus.dll: Tk's own reader + if master is None: + raise PictureError(f'{Path(path).name} could not be read: {e}') from None + return _load_tk(path, master) + + +def resize(img, w, h): + """img (H x W x 3) at w x h: whole pixels averaged first where it shrinks to less than half, then a bilinear + interpolation. Returns float32 when it interpolates.""" + H, W = img.shape[:2] + k = min(H // h, W // w) + if k >= 2: # average k x k blocks, so a much larger picture does not alias + img = img[:H // k * k, :W // k * k].reshape(H // k, k, W // k, k, 3).mean(axis=(1, 3), dtype=np.float32) + H, W = img.shape[:2] + if (H, W) == (h, w): + return img + ys = (np.arange(h, dtype=np.float32) + 0.5) * H / h - 0.5 + xs = (np.arange(w, dtype=np.float32) + 0.5) * W / w - 0.5 + y0 = np.clip(np.floor(ys).astype(int), 0, H - 1) + x0 = np.clip(np.floor(xs).astype(int), 0, W - 1) + y1, x1 = np.minimum(y0 + 1, H - 1), np.minimum(x0 + 1, W - 1) + fy = np.clip(ys - y0, 0, 1)[:, None, None] + fx = np.clip(xs - x0, 0, 1)[None, :, None] + a = img.astype(np.float32) + top = a[y0][:, x0] * (1 - fx) + a[y0][:, x1] * fx + bot = a[y1][:, x0] * (1 - fx) + a[y1][:, x1] * fx + return top * (1 - fy) + bot * fy + + +def cover(img, w, h): + """img cut to the shape w : h around its middle and resized to w x h, so it fills the window with nothing + stretched; uint8.""" + H, W = img.shape[:2] + if W * h > H * w: # wider than the window: cut the sides + cw = max(1, round(H * w / h)) + x = (W - cw) // 2 + img = img[:, x:x + cw] + else: # taller: cut top and bottom + ch = max(1, round(W * h / w)) + y = (H - ch) // 2 + img = img[y:y + ch] + out = resize(img, w, h) + return np.clip(np.rint(out), 0, 255).astype(np.uint8) + + +def keep(path, store, master=None): + """Reads the picture at path, fits it to the window's shape and writes it to store (data/background.npy).""" + fitted = cover(load(path, master), KEEP_W, KEEP_H) + store = Path(store) + tmp = store.with_name(store.name + '.tmp') + with open(tmp, 'wb') as f: + np.save(f, fitted, allow_pickle=False) + os.replace(tmp, store) + return fitted + + +def kept(store, w, h): + """The kept background at w x h as float32 in 0..1, or None when there is none (or it is not what keep wrote).""" + try: + a = np.load(store, allow_pickle=False) + except (OSError, ValueError): + return None + if a.dtype != np.uint8 or a.ndim != 3 or a.shape[2] != 3 or a.shape[0] < 8 or a.shape[1] < 8: + return None + return np.clip(resize(a, w, h).astype(np.float32) / 255.0, 0, 1) diff --git a/launcher/ui/selftest.py b/launcher/ui/selftest.py index bb97c8e8..d49e43be 100644 --- a/launcher/ui/selftest.py +++ b/launcher/ui/selftest.py @@ -15,11 +15,19 @@ def grab_gdi(x, y, w, h, path): from ctypes import wintypes import numpy as np from . import art - user32, gdi32 = ctypes.windll.user32, ctypes.windll.gdi32 - user32.GetDC.restype = wintypes.HDC - gdi32.CreateCompatibleDC.restype = wintypes.HDC - gdi32.CreateCompatibleBitmap.restype = wintypes.HBITMAP - gdi32.SelectObject.restype = wintypes.HGDIOBJ + # private handles with every argument typed: a handle is a pointer, and one above 2^31 passed as a plain int + # failed with "int too long to convert", so some screens went without their picture + user32, gdi32 = ctypes.WinDLL('user32'), ctypes.WinDLL('gdi32') + vp = ctypes.c_void_p + user32.GetDC.restype = gdi32.CreateCompatibleDC.restype = gdi32.CreateCompatibleBitmap.restype = vp + gdi32.SelectObject.restype = vp + user32.GetDC.argtypes = gdi32.CreateCompatibleDC.argtypes = gdi32.DeleteObject.argtypes = [vp] + gdi32.DeleteDC.argtypes = [vp] + user32.ReleaseDC.argtypes = gdi32.SelectObject.argtypes = [vp, vp] + gdi32.CreateCompatibleBitmap.argtypes = [vp, ctypes.c_int, ctypes.c_int] + gdi32.BitBlt.argtypes = [vp, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, vp, ctypes.c_int, + ctypes.c_int, wintypes.DWORD] + gdi32.GetDIBits.argtypes = [vp, vp, ctypes.c_uint, ctypes.c_uint, vp, vp, ctypes.c_uint] screen = user32.GetDC(None) mem = gdi32.CreateCompatibleDC(screen) bmp = gdi32.CreateCompatibleBitmap(screen, w, h) @@ -182,10 +190,28 @@ def states(app): app.release, app.message = rel, '' app.show('home', 1) + def customize(): + app.bg.ready = lambda release=None: None + app.release, app.message, app.look_message = None, '', '' + app.show('customize', 0) + + def color_scheme(): # another colour scheme on the home screen, then the first one again + from . import art + was = s.get('theme') + s['theme'] = list(art.THEMES)[2] + app.apply_look() + app.show('home', 0) + s['theme'] = was + + def restore_scheme(): + app.apply_look() + app.show('home', 0) + return [('1-first-run', first_run), ('2-installing', installing), ('3-home', home), ('4-update-ready', update_ready), ('5-updating', updating), ('6-settings', settings), ('7-build-failed', failed), ('8-uninstall', uninstall), ('9-import-saves', import_saves), - ('10-shaders', shaders), ('11-update-staged', update_staged), ('12-export-save', export_save)] + ('10-shaders', shaders), ('11-update-staged', update_staged), ('12-export-save', export_save), + ('13-customize', customize), ('14-color-scheme', color_scheme), ('15-first-scheme', restore_scheme)] def run(app, shots): diff --git a/launcher/ui/sound.py b/launcher/ui/sound.py new file mode 100644 index 00000000..f7814f60 --- /dev/null +++ b/launcher/ui/sound.py @@ -0,0 +1,134 @@ +"""The launcher's sound effects: short chimes made here from sine tones, so no sound file ships and none comes from +the game. + +They are off until the player picks a loudness in Customize Launcher (the Sounds row). Each effect is a few plucked +notes (a tone with two overtones, a quick attack and an exponential decay) written as a 16-bit mono WAV in memory. +Windows plays it with PlaySound from that memory; elsewhere the WAV goes to a file in the cache folder and the first +of pw-play, paplay and aplay found plays it. One effect sounds at a time: a new one cuts the last, which suits a +cursor moving quickly down a list. + + move the cursor went to another row + toggle a switch or a choice changed + select a row was chosen (another screen, a folder dialog) + back back to the screen before + start Play + done an install, update or rebuild finished + error something could not be done +""" +import ctypes +import os +import shutil +import struct +import subprocess + +import numpy as np + +RATE = 44100 +LEVELS = {'off': 0.0, 'low': 0.18, 'medium': 0.4, 'high': 0.75} # the Sounds choice: the loudest sample's share of full scale + +# Notes as (start in seconds, frequency in Hz, length in seconds, loudness 0..1). +C5, D5, E5, G5, A5, C6, D6, E6, G6, C7 = 523.25, 587.33, 659.25, 783.99, 880.0, 1046.5, 1174.66, 1318.51, 1567.98, 2093.0 +EFFECTS = { + 'move': [(0, E6, 0.07, 0.55)], + 'toggle': [(0, G5, 0.10, 0.8), (0.035, D6, 0.10, 0.5)], + 'select': [(0, C6, 0.12, 0.8), (0.07, G6, 0.20, 0.8)], + 'back': [(0, G5, 0.12, 0.8), (0.07, C5, 0.20, 0.7)], + 'start': [(0, C6, 0.14, 0.7), (0.08, E6, 0.14, 0.7), (0.16, G6, 0.14, 0.7), (0.24, C7, 0.45, 0.8)], + 'done': [(0, G5, 0.16, 0.8), (0.12, C6, 0.16, 0.8), (0.24, E6, 0.16, 0.8), (0.36, G6, 0.5, 0.9)], + 'error': [(0, 220.0, 0.16, 0.9), (0.14, 174.61, 0.3, 0.9)], +} + + +def pluck(freq, length, loudness): + """One note: the tone and two quieter overtones, in within 4 ms and dying away over its length.""" + n = int(RATE * length) + t = np.arange(n, dtype=np.float32) / RATE + wave = np.sin(2 * np.pi * freq * t) + 0.35 * np.sin(2 * np.pi * 2 * freq * t) + 0.12 * np.sin(2 * np.pi * 3 * freq * t) + env = np.minimum(t / 0.004, 1) * np.exp(-t * 5.5 / length) + tail = np.minimum((length - t) / 0.01, 1) # the last 10 ms go to nothing, so the end does not click + return wave * env * tail * loudness + + +def synth(name): + """The effect's samples in -1..1, its loudest at 1.""" + notes = EFFECTS[name] + total = int(RATE * max(start + length for start, _f, length, _l in notes)) + 1 + out = np.zeros(total, dtype=np.float32) + for start, freq, length, loudness in notes: + note = pluck(freq, length, loudness) + at = int(RATE * start) + out[at:at + len(note)] += note + peak = float(np.abs(out).max()) + return out / peak if peak > 0 else out + + +def wav_bytes(samples, level): + """A 16-bit mono WAV of samples (-1..1) at level (0..1 of full scale).""" + pcm = (np.clip(samples * level, -1, 1) * 32767).astype(' WAV bytes at the current level (Windows plays from this memory: it must stay) + self.files = {} # name -> the WAV file elsewhere + self.player = None # elsewhere: the program that plays a WAV file + self.winmm = None + self.failed = False + if os.name == 'nt': + try: + self.winmm = ctypes.WinDLL('winmm') + self.winmm.PlaySoundW.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint32] + self.winmm.PlaySoundW.restype = ctypes.c_int + except (OSError, AttributeError) as e: + self.log(f'sounds: {e}') + else: + self.player = next((p for p in ('pw-play', 'paplay', 'aplay') if shutil.which(p)), None) + self.set_level(level) + + def set_level(self, level): + level = level if level in LEVELS else 'off' + if level == self.level: + return + self.stop() + self.level = level + self.wavs, self.files = {}, {} + + def wav(self, name): + if name not in self.wavs: + self.wavs[name] = wav_bytes(synth(name), LEVELS[self.level]) + return self.wavs[name] + + def stop(self): + """Ends what is sounding, before the memory it plays from goes.""" + if self.winmm: + try: + self.winmm.PlaySoundW(None, None, 0) + except OSError: + pass + + def play(self, name): + if LEVELS.get(self.level, 0) <= 0 or self.failed or name not in EFFECTS: + return + try: + if self.winmm: + # SND_ASYNC | SND_NODEFAULT | SND_MEMORY: PlaySound reads the bytes while it plays, and self.wavs + # keeps them until the level changes (set_level stops the sound first) + self.winmm.PlaySoundW(self.wav(name), None, 0x0001 | 0x0002 | 0x0004) + elif self.player: + if name not in self.files: + path = self.cache / f'sound-{name}-{self.level}.wav' + path.write_bytes(self.wav(name)) + self.files[name] = path + subprocess.Popen([self.player, str(self.files[name])], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL) + except (OSError, ValueError) as e: # no device, no player: stay quiet from here on + self.failed = True + self.log(f'sounds: {e}') diff --git a/launcher/winsys.py b/launcher/winsys.py index 61d674c3..04789f62 100644 --- a/launcher/winsys.py +++ b/launcher/winsys.py @@ -159,11 +159,12 @@ def reveal(path): KNOWN_FOLDERS = {'downloads': '{374DE290-123F-4565-9164-39C4925E467B}', - 'desktop': '{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}'} + 'desktop': '{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}', + 'pictures': '{33E28130-4E1E-4676-835A-98395C3BC3BB}'} def known_folder(name): - """Windows: the folder the shell keeps for name ('downloads', 'desktop'), also when the player moved it to another + """Windows: the folder the shell keeps for name ('downloads', 'desktop', 'pictures'), also when the player moved it to another drive; None elsewhere or when the shell names none.""" if os.name != 'nt' or name not in KNOWN_FOLDERS: return None diff --git a/tests/launcher_test.py b/tests/launcher_test.py index e584c14e..83e36a37 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -18,6 +18,7 @@ Run: python tests/launcher_test.py (Python 3.14 for the zstd tests; the window a source checkout's build/xlate/), the prompts step with a stand-in prompt tool, the object cache of the compile step with the system's C compiler, and its windres call under folders with spaces (a script in windres's place) - jobs: an install driven by a fake make_game.py, with and without the prompts row +- the look: the colour schemes, the background picture's fitting and kept copy, the sound effects (off by default) - the window: --selftest draws every screen (skipped without a display) """ import contextlib @@ -2402,6 +2403,166 @@ class IconTest(unittest.TestCase): self.assertEqual(data[big:big + 4], b'\x89PNG') +class LookTest(Tmp): + """Customize Launcher: the colour schemes, the background picture and the sounds.""" + + def setUp(self): + super().setUp() + try: + from launcher.ui import art, picture, sound + except ImportError: + self.skipTest('needs numpy') + self.art, self.picture, self.sound = art, picture, sound + + def test_every_colour_scheme_has_every_colour(self): + art = self.art + self.assertEqual(art.theme_palette('kokiri'), art.BASE) # the first scheme is the colours as written + for name, t in art.THEMES.items(): + pal = art.theme_palette(name) + self.assertEqual(set(pal), set(art.BASE), name) + for key, value in pal.items(): + self.assertRegex(value, r'^#[0-9a-f]{6}$', f'{name} {key}') + self.assertTrue(t['label']) + grouped = [k for keys in art.GROUPS.values() for k in keys] + self.assertEqual(len(grouped), len(set(grouped)), 'a colour in two groups') + self.assertLessEqual(set(grouped), set(art.BASE)) + + def test_set_theme_changes_the_palette_in_place(self): + art = self.art + held = art.PALETTE + try: + self.assertEqual(art.set_theme('zora'), 'zora') + self.assertIs(art.PALETTE, held) + self.assertNotEqual(held['panel'], art.BASE['panel']) + self.assertEqual(held['salmon'], art.BASE['salmon']) # a warning stays a warning in every scheme + self.assertEqual(art.set_theme('no such scheme'), 'kokiri') + self.assertEqual(dict(held), art.BASE) + self.assertEqual(art.set_theme(None), 'kokiri') # launcher.json without the key + finally: + art.set_theme('kokiri') + + def test_recolor_keeps_light_and_dark(self): + import colorsys + art = self.art + + def hls(c): + return colorsys.rgb_to_hls(*(int(c[i:i + 2], 16) / 255 for i in (1, 3, 5))) + for c in ('#7a5c32', '#2a2019', '#d69127'): + h, l, _s = hls(art.recolor(c, 200)) + self.assertAlmostEqual(h * 360, 200, delta=4) + self.assertAlmostEqual(l, hls(c)[1], delta=0.01) + self.assertEqual(art.recolor('#7a5c32', None), '#7a5c32') + + def test_a_picture_is_cut_to_the_windows_shape(self): + import numpy as np + pic = self.picture + wide = np.zeros((100, 400, 3), dtype=np.uint8) + wide[:, 150:250] = 255 # the middle of a wide picture + out = pic.cover(wide, 160, 100) + self.assertEqual(out.shape, (100, 160, 3)) + self.assertGreater(out[:, 40:120].mean(), 200) # its middle fills most of the result + tall = np.zeros((400, 100, 3), dtype=np.uint8) + tall[150:250] = 255 + self.assertEqual(pic.cover(tall, 160, 100).shape, (100, 160, 3)) + self.assertGreater(pic.cover(tall, 160, 100).mean(), 120) + big = np.full((2048, 3200, 3), 77, dtype=np.uint8) # shrunk to half: averaged, the grey stays + self.assertTrue((pic.cover(big, 1600, 1024) == 77).all()) + + def test_the_kept_background(self): + import numpy as np + art, pic = self.art, self.picture + src = self.tmp / 'mine.png' + img = np.zeros((90, 300, 4), dtype=np.float32) + img[..., 0], img[..., 3] = 1.0, 1.0 # red + src.write_bytes(art.png_bytes(img)) + store = self.tmp / 'background.npy' + if pic.Image is None and os.name != 'nt': + self.skipTest('needs Pillow, or Tk with a display') + fitted = pic.keep(src, store) + self.assertEqual(fitted.shape, (pic.KEEP_H, pic.KEEP_W, 3)) + back = pic.kept(store, 1000, 640) + self.assertEqual(back.shape, (640, 1000, 3)) + self.assertEqual(str(back.dtype), 'float32') + self.assertAlmostEqual(float(back[..., 0].min()), 1.0, places=3) + self.assertAlmostEqual(float(back[..., 1:].max()), 0.0, places=3) + # the backdrop with it: the picture darkened by the dimming, solid everywhere + plain = art.backdrop(200, 128, 1.0, pic.kept(store, 200, 128), 0.0) + dimmed = art.backdrop(200, 128, 1.0, pic.kept(store, 200, 128), 0.8) + self.assertEqual(plain.shape, (128, 200, 4)) + self.assertGreater(plain[64, 100, 0], 0.8) # the corners' shade reaches in a little + self.assertLess(dimmed[64, 100, 0], 0.35) + self.assertEqual(float(dimmed[..., 3].min()), 1.0) + # what is not a kept background, and what is no picture + self.assertIsNone(pic.kept(self.tmp / 'none.npy', 100, 64)) + store.write_bytes(b'not numpy') + self.assertIsNone(pic.kept(store, 100, 64)) + text = self.tmp / 'notes.txt' + text.write_text('no picture') + with self.assertRaises(pic.PictureError): + pic.keep(text, store) + with self.assertRaises(pic.PictureError): + pic.load(self.tmp / 'gone.png') + + def test_ppm(self): + import numpy as np + px = bytes(range(18)) + a = self.picture.read_ppm(b'P6\n# a comment\n3 2\n255\n' + px) + self.assertEqual(a.shape, (2, 3, 3)) + self.assertEqual(a.tobytes(), px) + self.assertEqual(a.dtype, np.uint8) + with self.assertRaises(self.picture.PictureError): + self.picture.read_ppm(b'P5\n1 1\n255\n\0') + + def test_the_effects_are_short_wavs(self): + sound = self.sound + for name in sound.EFFECTS: + samples = sound.synth(name) + self.assertAlmostEqual(float(abs(samples).max()), 1.0, places=5, msg=name) + self.assertLess(len(samples) / sound.RATE, 1.0, name) + self.assertLess(abs(float(samples[-1])), 0.01, name) # it ends at nothing: no click + wav = sound.wav_bytes(samples, 0.4) + self.assertEqual(wav[:4] + wav[8:16], b'RIFFWAVEfmt ') + self.assertEqual(struct.unpack_from(' Date: Tue, 10 Nov 2026 00:21:31 -0500 Subject: [PATCH 03/13] Apply the camera's stick sensitivity and the speaker layout during a run. NX2NAVI_STICK_SENS was served to the game as a multiplied camera pack at start only. A change during the run is now written into the game's memory: the StickSensitivity array as the game holds it, at the factor in effect, is looked for in the guest's heap and every copy is overwritten at the new factor. The game reads the array each time it steps the camera, so the speed follows at once. The copies found are remembered, so a later step of the menu's slider takes no search, and a search runs again a moment after the game opens the pack anew. On Ocarina 1.1.1 the first change of a run found 4 copies in about 2 s on its own thread, and the same stick push for a third of a second turned the camera about 30 degrees at 1, 90 at 3, 10 at 0.3 and 30 again at 1, all in one run. NX2NAVI_AUDIO_CHANNELS was decided once. A change that gives another channel count now signals the device change events the game holds, as a console does when its output moves: the game asks GetActiveChannelCount again and mixes for the new count, and the mixer makes its SDL stream again with that count at its next render. On Ocarina 1.1.1 the centre input of the device sink went from 0.0006 rms to 0.013 within the 5 s after stereo became 5.1 and back to 0.0006 after stereo was set again, with no underrun. NX2NAVI_TRACE=audio logs the six inputs' levels with its level line. Both rows lose their "after restart" note. The graphics card, widescreen, the resolution, the game's language, the button prompts and the game's native HDR still take a restart: the game or the Vulkan device reads them once while starting. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 5 +- runtime/audio_mixer.c | 85 ++++++++++++++++++++--- runtime/audren.h | 1 + runtime/camera_speed.c | 151 +++++++++++++++++++++++++++++++++++++++-- runtime/cfg.c | 4 +- runtime/gfx_present.c | 3 + runtime/host.h | 2 + runtime/romfs.c | 3 +- 8 files changed, 237 insertions(+), 17 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 1723ae53..f915c9df 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -409,7 +409,8 @@ # Volume in percent (0 to 100), on top of the game's own. Changes while the game runs take effect at once. # volume = 100 # Speakers: stereo, 5.1 or auto (5.1 when the Windows speaker setup has six or more channels, stereo otherwise). The -# game is told the same channel count and mixes for it. Takes effect at the next start. +# game is told the same channel count and mixes for it. A change while the game runs takes effect at once: the game is +# told its output device changed, as on a console, and mixes for the new count. # audio_channels = stereo # Which output device the sound plays on. default (the default): the system default device, which the game follows when # the Windows default changes. Or a number, counting from 1 in the order `nx2navi.exe --list-audio-devices` prints; or part @@ -450,7 +451,7 @@ # stick_deadzone = 8 # Right-stick camera speed (0.1 to 3): multiplies the game's own camera turn rates, top speed included, at each of the # five camera sensitivities in the game's Options. Keys bound to the right stick follow it; the mouse keeps mouse_sens. -# Read at start. +# A change while the game runs takes effect at once (the first one of a run within a few seconds). # stick_sens = 1 # The dead zone of one stick, in place of stick_deadzone for that stick (0 to 50; unset = stick_deadzone). The Controls # tab shows each stick's deflection now, so a drifting stick's resting value can be read off. diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index e7255e90..0708e01a 100644 --- a/runtime/audio_mixer.c +++ b/runtime/audio_mixer.c @@ -71,6 +71,7 @@ typedef struct Mixer { unsigned reported; // one-time reports float voice_scratch[6][MAX_FRAME], filter_scratch[MAX_FRAME], output_scratch[MAX_FRAME * OUT_MAX]; double level_sum, level_peak; uint64_t level_count, level_frames; int first_sound_logged; + double input_sum[6]; // NX2NAVI_TRACE=audio: the device sink's inputs, squared and summed int prefill; // render blocks uint64_t trace_pass, trace_seen[1024]; // NX2NAVI_TRACE=audio: render passes; the pass each voice was last mixed in uint64_t trace_buf[1024]; // and the wave buffer address it read in that pass @@ -225,12 +226,13 @@ int list_audio_devices(void) { return 0; } -// NX2NAVI_AUDIO_CHANNELS: the output channels of this run, decided once. stereo gives 2; 5.1 gives 6 (FL FR C LFE RL RR, +// NX2NAVI_AUDIO_CHANNELS: the output channels, decided at the first call and again when the setting changes +// (audio_channels_live). stereo gives 2; 5.1 gives 6 (FL FR C LFE RL RR, // the device sink's order and SDL's); auto gives 6 when the chosen playback device (NX2NAVI_AUDIO_DEVICE; the system default unless set) has at least six channels (a 5.1 // or 7.1 speaker setup in Windows; SDL places 5.1 on 7.1) and 2 otherwise. IAudioDevice reports the same count to the // game (GetActiveChannelCount), which picks its sink layout from it. NX2NAVI_AUDIO=none: 2, without touching SDL. +static _Atomic int decided; // 0: output_channels decides at its next call static int output_channels(void) { - static _Atomic int decided; int n = atomic_load(&decided); if (n) return n; const char *want = cfg_value("NX2NAVI_AUDIO_CHANNELS"), *mode = cfg_get("NX2NAVI_AUDIO"); @@ -402,6 +404,29 @@ static void output_timing(Mixer *m, int rate) { dev.channels, m->prefill * 5, period, m->target * 1000.0 / rate); } +// Render thread (audio_mixer_render): the output takes another channel count after NX2NAVI_AUDIO_CHANNELS changed +// (audio_channels_live). The stream is made again with the new count on the device it was on: destroying it waits +// for the SDL callback, the only other reader of the ring and of m->channels, and what was queued in the old layout +// is dropped. +static void channels_switch(Mixer *m, int channels) { + if (m->stream) SDL_DestroyAudioStream(m->stream); + m->stream = NULL; + atomic_store_explicit(&m->rd, atomic_load(&m->wr), memory_order_release); + m->channels = channels; + m->ready = 0; + m->reported &= ~(1u << 6); // the device sink's line again, with the new output + atomic_store_explicit(&m->pulled_ns, 0, memory_order_release); + rt_log("audio: the output is now %s", channels == 6 ? "5.1" : "stereo"); + if (!m->device_ok) return; // mixing without a device + char err[256]; + if (stream_open(m, m->rate, m->device ? m->device : SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, err, sizeof err)) { + output_timing(m, m->rate); + } else { + m->device_ok = 0; + rt_log("audio: no output device (%s): mixing without sound", err); + } +} + // Render thread, from audio_mixer_pace_ns while the device takes nothing. An explicitly chosen device (NX2NAVI_AUDIO_DEVICE) // can disappear (a virtual sink removed, a USB headset unplugged); SDL keeps only the system default device alive across // that, so the stream of a chosen one just stops being pulled, and nothing in this file polls SDL's event queue for the @@ -523,6 +548,11 @@ static void level_note(AudioRenderer *r, Mixer *m, const float *out, int frames) (unsigned long long)atomic_load(&m->overruns), (unsigned long long)atomic_load(&m->missing_frames), (unsigned long long)atomic_load(&m->callback_frames), (unsigned long long)atomic_load(&m->callback_nonzero), queued * 1000.0 / r->param.sample_rate, (unsigned long long)atomic_load(&m->skipped_frames)); + double f = (double)m->level_frames; + rt_log("audio: device sink inputs rms FL %.4f FR %.4f C %.4f LFE %.4f RL %.4f RR %.4f, output %s", + sqrt(m->input_sum[0] / f), sqrt(m->input_sum[1] / f), sqrt(m->input_sum[2] / f), sqrt(m->input_sum[3] / f), + sqrt(m->input_sum[4] / f), sqrt(m->input_sum[5] / f), m->channels == 6 ? "5.1" : "stereo"); + memset(m->input_sum, 0, sizeof m->input_sum); m->level_sum = m->level_peak = 0; m->level_count = m->level_frames = 0; } @@ -1128,6 +1158,7 @@ void audio_mixer_render(AudioRenderer *r, double seconds) { if (r->sinks[i].type == SINK_DEVICE && !sink) sink = &r->sinks[i]; else if (r->sinks[i].type == SINK_CIRCULAR){mix_circular_sink(r,i);circular=1;} } else {m->sink_address[i]=0;m->sink_size[i]=m->sink_offset[i]=m->sink_old[i]=m->sink_written[i]=0;} + if (m->channels != output_channels()) channels_switch(m, output_channels()); const int oc = m->channels; float *out=m->output_scratch; memset(out, 0, sizeof(float) * (size_t)oc * (size_t)frames); @@ -1143,6 +1174,9 @@ void audio_mixer_render(AudioRenderer *r, double seconds) { const float *ch[6] = {0}; float raw[2 * MAX_FRAME]; for (uint32_t c = 0; c < sink->input_count; c++) if (sink->inputs[c] >= 0 && sink->inputs[c] < m->nbuf) ch[c] = mixbuf(m, sink->inputs[c]); + if (rt_trace & RT_TRACE_AUDIO) // what the game puts on each input: shows whether it mixes for 5.1 (level_note) + for (int c = 0; c < 6; c++) + for (int k = 0; ch[c] && k < frames; k++) m->input_sum[c] += (double)ch[c][k] * ch[c][k]; for (int k = 0; k < frames; k++) { float s[OUT_MAX] = {0}; /* FL FR C LFE RL RR */ if (sink->input_count >= 6 && oc == 6) { @@ -1170,6 +1204,41 @@ void audio_mixer_render(AudioRenderer *r, double seconds) { // ---- IAudioDevice: one output device, stereo or 5.1 (output_channels) static const char device_name[] = "AudioTvOutput"; +// The events the game took to hear of a change of the output device (IAudioDevice's, its notifications' and the +// virtual device manager's). audio_channels_live signals them all: the game then asks for the channel count again, as +// it does on a console whose output moves from the TV to headphones. +static uint32_t change_events[16]; +static _Atomic int n_change_events; + +static uint32_t change_event_new(void) { + uint32_t ev = kern_event_new(); + int i = atomic_fetch_add(&n_change_events, 1); + if (i < 16) change_events[i] = ev; + return ev; +} + +// Present path, after a settings change (gfx_present.c window_settings_live): a changed NX2NAVI_AUDIO_CHANNELS is +// decided again, and when it gives another channel count the game is told its output device changed. The game asks +// GetActiveChannelCount again and mixes for the new count; the mixer moves its device to that count at its next +// render (channels_switch). Measured on Ocarina 1.1.1: one event held, and the game asked within the same second. +void audio_channels_live(void) { + static char seen[16]; + const char *want = cfg_value("NX2NAVI_AUDIO_CHANNELS"); + if (!strncmp(want, seen, sizeof seen - 1)) return; + int first = !seen[0]; + snprintf(seen, sizeof seen, "%s", want); + int was = atomic_load(&decided); + if (first || !was) return; // nothing has asked yet: the first call decides + atomic_store(&decided, 0); + int now = output_channels(); + if (now == was) return; + int n = atomic_load(&n_change_events); + if (n > 16) n = 16; + for (int i = 0; i < n; i++) kern_event_signal(change_events[i]); + rt_log("audio: NX2NAVI_AUDIO_CHANNELS=%s: the output goes from %s to %s; the game is told the device changed " + "(%d events)", want, was == 6 ? "5.1" : "stereo", now == 6 ? "5.1" : "stereo", n); +} + static void put_name(const IpcBuffer *b, IpcResponse *rs, int pointer) { if (!b->size) return; char *dst = ipc_guest(b->addr, b->size); @@ -1185,7 +1254,7 @@ static IpcBuffer out_buffer(const IpcRequest *rq, int *pointer) { } void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs) { - static uint32_t change_event; // device change events: never signalled, the device does not change + static uint32_t change_event; // the device change event: signalled when the output's channels change (audio_channels_live) int pointer; switch (rq->cmd) { case 0: case 6: case 14: { // ListAudioDeviceName(Auto), ListAudioOutputDeviceName [13.0.0+] @@ -1210,7 +1279,7 @@ void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs) case 4: case 11: case 12: { // QueryAudioDeviceSystemEvent, ...InputEvent, ...OutputEvent uint32_t ev = __atomic_load_n(&change_event, __ATOMIC_ACQUIRE); if (!ev) { - uint32_t mine = kern_event_new(), expected = 0; + uint32_t mine = change_event_new(), expected = 0; if (__atomic_compare_exchange_n(&change_event, &expected, mine, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) ev = mine; else @@ -1222,9 +1291,9 @@ void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs) // 17.0.0+: Acquire/Release Audio{Input,Output}DeviceNotification (15-18), 18.0.0+: SetAudioDeviceOutputVolume- // AutoTuneEnabled (19) / IsAudioDeviceOutputVolumeAutoTuneEnabled (20), 21.0.0+: // IsActiveOutputDeviceEstimatedLowLatency (21). Command ids as the system answers them. The - // device never changes and the host applies no auto-tuned volume, so the answers are "off" / "no". - case 15: case 17: { // Acquire...Notification -> a readable event that is never signalled - uint32_t ev = kern_event_new(); + // host applies no auto-tuned volume, so the answers are "off" / "no". + case 15: case 17: { // Acquire...Notification -> a readable event, signalled with the others + uint32_t ev = change_event_new(); rs->copy[rs->ncopy++] = kern_event_readable(ev); return; } @@ -1293,7 +1362,7 @@ static void virtual_device_manager_handler(IpcSession *s, const IpcRequest *rq, put_block(rq, rs, block, sizeof block); return; } - case 500: rs->copy[rs->ncopy++] = kern_event_readable(kern_event_new()); return; // state change event + case 500: rs->copy[rs->ncopy++] = kern_event_readable(change_event_new()); return; // state change event case 700: case 800: return; // AttachStream / DetachStream default: { static volatile int32_t seen[64]; diff --git a/runtime/audren.h b/runtime/audren.h index e8289780..4f3d861e 100644 --- a/runtime/audren.h +++ b/runtime/audren.h @@ -194,5 +194,6 @@ int audio_mixer_out_stats(AudioRenderer *r, AudioOutStats *o); uint64_t audio_voice_consumed(const AudioRenderer *r, ServerVoice *v, int frames); // source samples over one frame; advances v->fraction int audio_buffer_empty(const AudrenWaveBuffer *b); // no samples to decode: played as soon as it is reached void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // IAudioDevice +void audio_channels_live(void); // after a settings change: another NX2NAVI_AUDIO_CHANNELS reaches the game and the mixer void audio_dmg_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // auddmg:u (Switch 2) void audio_sysmix_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // audsmx:u (Switch 2) diff --git a/runtime/camera_speed.c b/runtime/camera_speed.c index 447c3c93..db2b2fe7 100644 --- a/runtime/camera_speed.c +++ b/runtime/camera_speed.c @@ -1,4 +1,4 @@ -// Camera speed: NX2NAVI_STICK_SENS (0.1 to 3, default 1, read at start) multiplies the game's own right-stick camera speeds. +// Camera speed: NX2NAVI_STICK_SENS (0.1 to 3, default 1) multiplies the game's own right-stick camera speeds. // The game's Options menu offers five camera sensitivities, Very Low to Very High; their turn rates are the array // StickSensitivity {0.8, 1.2, 1.6, 2.0, 2.4} in Component/PlayerCameraParam/PlayerCamera.game__component__ // PlayerCameraParam.bgyml inside Pack/Actor/PlayerCamera.pack.zs. romfs.c serves a copy of that pack with every entry @@ -8,6 +8,13 @@ // // The pack is a SARC archive compressed with the dictionary pack.zsdic of Pack/ZsDic.pack.zs. A float in a BYML array // is stored in the array itself, so the multiplied values take the bytes of the old ones and no offset moves. +// +// During a run (cam_speed_tick): the pack served stays the one made at start, and the game has read it by the time the +// player changes the setting. So a change is written into the game's memory instead: the array as the game holds it +// (the five values in a row, times the factor in effect) is looked for in the guest's heap and every copy found is +// overwritten with the values times the new factor. The game reads the array each time it steps the camera, so the +// camera's speed follows at once. When the game opens the pack again (cam_pack_opened: romfs.c), what it reads holds +// the start's factor again, and the search runs once more a moment later. #include #include #include @@ -15,11 +22,15 @@ #include "cfg.h" #include "host.h" +#include "platform/platform.h" #define MEMBER "Component/PlayerCameraParam/PlayerCamera.game__component__PlayerCameraParam.bgyml" #define MAX_RAW (64u << 20) -static float applied = 1.0f; +static float applied = 1.0f; // the factor the game's values in memory carry: what cam_speed_applied reports +static float base[8]; // the game's own StickSensitivity values, from the pack as it is before multiplying +static int base_n; // how many; 0 until cam_pack_build has read the pack (or when it could not) +static float served_k = 1.0f; // the factor of the pack the game reads from the RomFS: the start's float cam_speed_wanted(void) { double v = cfg_double("NX2NAVI_STICK_SENS"); @@ -100,7 +111,6 @@ static int scale_sensitivity(uint8_t *p, size_t n, float k, float was[8]) { int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, void **out, size_t *out_n) { float k = cam_speed_wanted(); *out = NULL; - if (k == 1.0f) return 0; int ok = 0; uint8_t *dics = NULL, *raw = NULL; ZSTD_DDict *dd = NULL; @@ -135,6 +145,9 @@ int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, rt_log("camera speed: no StickSensitivity array in %s; the camera keeps its own speed", MEMBER); goto done; } + memcpy(base, was, sizeof(float) * (size_t)count); // what a change during the run looks for in memory (cam_speed_tick) + base_n = count; + if (k == 1.0f) goto done; // the game's own pack is served as it is cd = ZSTD_createCDict(dics + dic_at, dic_n, 9); size_t cap = ZSTD_compressBound((size_t)rn), zn = 0; ZSTD_CCtx *cctx = cd ? ZSTD_createCCtx() : NULL; @@ -158,4 +171,134 @@ done: return ok; } -void cam_pack_served(void) { applied = cam_speed_wanted(); } +void cam_pack_served(void) { applied = served_k = cam_speed_wanted(); } + +// ---- during a run + +#define COPIES 16 +static uint64_t copies[COPIES]; // where the game's memory held the array at the last search +static int ncopies; + +static void factor(float out[8], float k) { // the array at factor k, as cam_pack_build's multiplication gives it + for (int i = 0; i < base_n; i++) out[i] = base[i], out[i] *= k; +} + +// A pass over the guest's heap and alias regions for the array at factor k, byte for byte, in memory the game may +// read and write; each copy found becomes the array at factor to and is remembered (copies, when fresh is set). +// Returns how many were rewritten. Runs on its own thread: the regions are read 16 MiB at a time under mem_read_hold, +// so the game's own mapping calls wait for one piece at most. +static int rewrite(float k, float to, int fresh) { + enum { PIECE = 16 << 20 }; + float from[8], into[8]; + factor(from, k); + factor(into, to); + const size_t len = sizeof(float) * (size_t)base_n; + const uint8_t first = *(const uint8_t *)from; + const RtGuestLayout *l = mem_layout(); + const RtRange ranges[2] = {l->heap, l->alias}; + int found = 0; + if (fresh) ncopies = 0; + for (int r = 0; r < 2; r++) { + uint64_t a = ranges[r].base, end = ranges[r].base + ranges[r].size; + while (a < end) { + RtMemoryInfo mi; + if (mem_query(a, &mi) || !mi.size) break; + uint64_t stop = mi.base + mi.size < end ? mi.base + mi.size : end; + if (!mi.state || (mi.perm & 3) != 3) { // free, or not the game's to read and write + a = stop; + continue; + } + while (a < stop) { + uint64_t n = stop - a < PIECE ? stop - a : PIECE; + if (n >= len && mem_read_hold(a, n)) { + const uint8_t *p = (const uint8_t *)(uintptr_t)a, *e = p + n; + for (const uint8_t *q = p; q + len <= e && (q = memchr(q, first, (size_t)(e - q) - len + 1)); q++) { + if (memcmp(q, from, len)) continue; + mem_thaw((uint64_t)(uintptr_t)q, len); // a write-protected page of a tracked region + memcpy((void *)(uintptr_t)q, into, len); + if (ncopies < COPIES) copies[ncopies++] = (uint64_t)(uintptr_t)q; + found++; + } + mem_read_release(); + } + // a copy across two pieces: the next piece starts a little before this one's end + a += n < PIECE ? n : n - (len - 1); + } + } + } + return found; +} + +// The copies of the last search, still holding the array at factor k, rewritten to factor to: 1 when every one of them +// did (a step of the menu's slider then takes no search), 0 when one is gone or changed, with none of them touched. +static int rewrite_known(float k, float to) { + float from[8], into[8]; + factor(from, k); + factor(into, to); + const size_t len = sizeof(float) * (size_t)base_n; + if (!ncopies) return 0; + for (int pass = 0; pass < 2; pass++) // all checked first, then all written + for (int i = 0; i < ncopies; i++) { + if (!mem_read_hold(copies[i], len)) return 0; + int same = !memcmp((const void *)(uintptr_t)copies[i], pass ? into : from, len) || + (pass && !memcmp((const void *)(uintptr_t)copies[i], from, len)); + if (same && pass) { + mem_thaw(copies[i], len); + memcpy((void *)(uintptr_t)copies[i], into, len); + } + mem_read_release(); + if (!same) return 0; + } + return 1; +} + +static volatile int32_t searching; // the search thread runs +static volatile int32_t reopened; // the game opened the pack since the last search (cam_pack_opened) +static uint64_t due_ns; // when cam_speed_tick starts the next search; 0: none due +static int tries; // searches left after this one that found nothing +static float asked = 1.0f; // the factor the last search was started for + +static void search_main(void *arg) { + (void)arg; + float to = cam_speed_wanted(), was = applied; + uint64_t t0 = plat_now_ns(); + // what the game holds: the array at the factor in effect, and at the start's after it read the pack again + int n = to != was ? rewrite(was, to, 1) : 0; + if (served_k != was && served_k != to) n += rewrite(served_k, to, 0); // new copies beside the ones known + if (n) { + applied = to; + rt_log("camera speed: x%.2f now, written over %d cop%s of the game's StickSensitivity in memory (%.0f ms)", to, n, + n == 1 ? "y" : "ies", (plat_now_ns() - t0) / 1e6); + } else if (to != was) { + rt_log("camera speed: x%.2f asked for, but the game's StickSensitivity (x%.2f) is not in memory%s", to, was, + tries ? "; looking again in a moment" : ": the camera keeps its speed until the game loads it"); + } + plat_xchg32(&searching, 0); +} + +void cam_pack_opened(void) { plat_xchg32(&reopened, 1); } + +// Every present (gfx_present.c). A factor other than the one in effect goes to the copies the last search found at +// once; without them (the first change of a run, or the game moved them) a search starts, and a few more while one +// finds nothing (the game is still loading). A moment after the game opened the pack again a search looks for the +// start's factor, which is what the game has then read. +void cam_speed_tick(void) { + if (!base_n) return; + float want = cam_speed_wanted(); + if (plat_xchg32(&reopened, 0) && want != served_k) due_ns = plat_now_ns() + 1500000000ull, tries = 3; + if (want == applied && !due_ns) return; + if (plat_cas32(&searching, 0, 0)) return; // a search writes applied and the copies + uint64_t now = plat_now_ns(); + if (want != applied) { + if (rewrite_known(applied, want)) { + applied = asked = want; + return; + } + if (want != asked) asked = want, due_ns = now, tries = 3; + } + if (!due_ns || now < due_ns) return; + due_ns = 0; + if (tries > 0 && want != applied) tries--, due_ns = now + 4000000000ull; // again unless this one finds it + plat_xchg32(&searching, 1); + if (!plat_thread_spawn(search_main, NULL, "camera speed", 0)) plat_xchg32(&searching, 0); +} diff --git a/runtime/cfg.c b/runtime/cfg.c index 7a2a59b4..7c03acd2 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -178,14 +178,14 @@ static const CfgMeta metas[] = { {"OPERATION_MODE", "docked", "docked|handheld", LIVE}, {"ASSUME_FOCUS", "1", "0|1", LIVE}, // read on every focus change (ov_system.c) // Sound, Controls, Menu - {"VOLUME", "100", "0..100", LIVE}, {"MENU_SOUND_VOLUME", "80", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", RESTART}, + {"VOLUME", "100", "0..100", LIVE}, {"MENU_SOUND_VOLUME", "80", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", LIVE}, // audio_mixer.c audio_channels_live {"AUDIO_DEVICE", "default", NULL, RESTART}, // default|<1-based index>| (audio_mixer.c resolve_audio_device) {"MICROPHONE", "1", "0|1", RESTART}, // ov_audio_input.c mic_on: 0 refuses the game's input streams {"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE}, {"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "nintendo", "auto|nintendo|xbox|playstation", LIVE}, {"DS3", "0", "0|1", LIVE}, // input_ds3_win.c ds3_read: read a DualShock 3 directly; off: no HID scan {"PROMPTS", "nintendo", "nintendo|xbox|playstation|pc", RESTART}, - {"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", RESTART}, + {"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", LIVE}, // camera_speed.c cam_speed_tick {"LSTICK_DEADZONE", "", "0..50", LIVE}, {"RSTICK_DEADZONE", "", "0..50", LIVE}, {"MOUSE_MODE", "direct", "stick|direct", LIVE}, {"MOUSE_SENS", "1", "0..10", LIVE}, {"MENU_CURSOR", "1", "0|1", LIVE}, diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 3a8d1592..dad71666 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -15,6 +15,7 @@ #include #include +#include "audren.h" #include "cfg.h" #include "flight.h" #include "fs.h" @@ -258,6 +259,7 @@ static void window_settings_live(void) { // with what it got: on a display without HDR10 the setting stays on and the swapchain SDR, and that is settled. if (started && vk_hdr_wanted() != g_sc_hdr_wanted) plat_xchg32(&g_dirty, 1); ov_operation_mode_live(); + audio_channels_live(); if (started) gfx_window_apply_settings(); started = 1; } @@ -971,6 +973,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w if (cw <= 0 || ch <= 0) cx = cy = 0, cw = w, ch = h; // a crop outside the image (the game's nvnWindowSetCrop): // the whole image, not an inverted blit rectangle window_settings_live(); + cam_speed_tick(); // The menu overlay: laid out before recording (its first frame starts the ImGui back end and uploads the font) const char *menu_shot = NULL; MenuTarget mt = {c->instance, c->phys, c->device, c->gfx_family, c->gfx_queue, diff --git a/runtime/host.h b/runtime/host.h index 0d1feb45..0004a8c8 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -280,6 +280,8 @@ float cam_speed_applied(void); // the factor in effe int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, void **out, size_t *out_n); // Pack/Actor/PlayerCamera.pack.zs with the camera speeds // multiplied, malloc'd; 0 = factor 1 or cannot (logged) +void cam_pack_opened(void); // romfs.c: the game opened the camera's pack +void cam_speed_tick(void); // every present: a changed setting goes into the game's memory void cam_pack_served(void); // romfs.c serves the multiplied pack // ---- game resolution (resolution.c): NX2NAVI_OUTPUT_RES, NX2NAVI_RENDER_SCALE uint64_t res_heap_extra_bytes(void); // added to the application pool (mem_init), 2 MiB steps diff --git a/runtime/romfs.c b/runtime/romfs.c index fce2bc91..2e027fa4 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -769,7 +769,7 @@ static void heapdef_overlay(void) { // overlay that replaces the pack (a mod's camera stays), else the RomFS original; the dictionary is the RomFS's own. #define CAMPACK "Pack/Actor/PlayerCamera.pack.zs" static void camera_overlay(void) { - if (cam_speed_wanted() == 1.0f) return; + // also at factor 1: cam_pack_build reads the game's own values, which a change during the run needs static const char keeps[] = "the camera keeps its own speed"; Overlay *base, *dic_base; size_t n, dn; @@ -1158,6 +1158,7 @@ uint32_t romfs_open(uint64_t *out, const char *path, int mode) { uint64_t i = file_index(p); if (i == UINT64_MAX) return FS_R_PATH_NOT_FOUND; open_note(i, p); + if (!strcasecmp(p, CAMPACK)) cam_pack_opened(); // its values come into memory at the start's factor (camera_speed.c) uint32_t ov = overlay_slot(i); PlatFile *f = NULL; if (ov) { From 158ba315d558e3dfccff8cb99b8dbc05ffb5277f Mon Sep 17 00:00:00 2001 From: epicmeme546 <178368684+epicmeme546@users.noreply.github.com> Date: Tue, 10 Nov 2026 00:21:31 -0500 Subject: [PATCH 04/13] Play the game's own cursor, select and back sounds in the launcher. The launcher's sound effects were chimes made from sine tones. They are now the game's interface sounds, the ones its menus make for the cursor, a choice and going back: SE_Sys_Cursor00, SE_Sys_Do00 and SE_Sys_Cancel00, the files the in-game menu plays. The launcher decodes them from the player's built game or dump into data/cache/sounds (gamefiles.extract_sounds, a BWAV reader for PCM16 and DSP-ADPCM), beside the font it already reads that way, so no sound ships. The chimes stand in until the game has been read. The cursor's sound is a little under the others, as in the game's menu, and is left out for a quarter of a second after a choice, since one sound plays at a time and the pointer landing on a row of the next screen would cut the choice's sound short. Sounds stay off until the player picks a loudness. Co-Authored-By: Claude Fable 5.2 --- docs/launcher.md | 13 +++-- docs/releases/v0.6.2.md | 8 +-- launcher/gamefiles.py | 108 ++++++++++++++++++++++++++++++++++++++-- launcher/ui/app.py | 21 ++++++-- launcher/ui/sound.py | 88 +++++++++++++++++++++++++------- tests/launcher_test.py | 68 ++++++++++++++++++++++++- 6 files changed, 269 insertions(+), 37 deletions(-) diff --git a/docs/launcher.md b/docs/launcher.md index 971df128..b2f9c72d 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -47,9 +47,13 @@ Customize Launcher (on the home screen) makes the look the player's own: - Animations (on by default): the cursor glides from row to row, a new screen slides into place, sparks drift up over the backdrop and Navi bobs in the title band with a pulsing halo (`App.animate`). They move by the time that passed, stand still while the window is hidden or minimised, and Off stops them all. -- Sounds (off by default; Low, Medium or High): a short chime when the cursor moves, a choice changes, a row is - chosen, the game starts and a build ends or fails. The chimes are made from sine tones in `launcher/ui/sound.py` - and played from memory (PlaySound on Windows; `pw-play`, `paplay` or `aplay` elsewhere). +- Sounds (off by default; Low, Medium or High): the game's own interface sounds, the cursor, decide and cancel + sounds of its menus, when the cursor moves or a choice changes, a row is chosen and the player goes back. The + launcher decodes them from the player's built game or dump into `data/cache/sounds` (`gamefiles.extract_sounds`: + `Sound/Resource/Stream/SE_Sys_Cursor00.bwav`, `SE_Sys_Do00.bwav` and `SE_Sys_Cancel00.bwav`, the files + `runtime/menu_sound.c` plays in the in-game menu), so no sound ships; until they are decoded, chimes made from sine + tones in `launcher/ui/sound.py` stand in. They are played from memory (PlaySound on Windows; `pw-play`, `paplay` + or `aplay` elsewhere). The choices are `theme`, `background`, `background_dim`, `animations` and `sounds` in `data/launcher.json`. @@ -87,7 +91,8 @@ app// launcher/, runtime/, recomp/, tools/, bin/nx2navi_xlate pydeps/ zbic, downloaded from PyPI on this PC toolchain/msys64/ucrt64 MSYS2's clang and libraries, downloaded from the MSYS2 repository on this PC toolchain/pkgs/ the downloaded package files -data/launcher.json settings; data/logs/launcher.log (and crash.log); data/cache/fonts; +data/launcher.json settings; data/logs/launcher.log (and crash.log); data/cache/fonts and data/cache/sounds + (the game's font and interface sounds, decoded from the player's game); data/background.npy (the player's background picture, fitted to the window) game/ the built game, unless another folder was chosen (its layout: README-BuildKit.md), with saves/ for saves to import and the backups imports make (below) diff --git a/docs/releases/v0.6.2.md b/docs/releases/v0.6.2.md index 334ef247..28ab3581 100644 --- a/docs/releases/v0.6.2.md +++ b/docs/releases/v0.6.2.md @@ -8,10 +8,10 @@ Draft notes for the release after 0.6.1. The version number is not decided yet. and checked quickly. An import copies your current save to the `saves` folder of the game folder first; in the game it takes effect at the next start. - Customize Launcher (on the launcher's home screen): six colour schemes, a background picture of your own with - adjustable dimming, animations (a gliding cursor, sliding screens, drifting sparks) and sound effects. The sounds - are off until you turn them on. -- HDR switches between off and the tone mapper in the settings menu without a restart. Only the game's native HDR - still waits for one. + adjustable dimming, animations (a gliding cursor, sliding screens, drifting sparks) and sound effects: the game's + own cursor, select and back sounds, read from your game. The sounds are off until you turn them on. +- Three more settings take effect without a restart: HDR between off and the tone mapper (only the game's native + HDR still waits for one), the camera's stick sensitivity, and the speakers (stereo or 5.1). ## Fixed diff --git a/launcher/gamefiles.py b/launcher/gamefiles.py index 9efdc28e..5147a01b 100644 --- a/launcher/gamefiles.py +++ b/launcher/gamefiles.py @@ -1,14 +1,20 @@ -"""The player's game files, as far as the launcher reads them itself: finding a .nup dump, and the game's Cetra -Display font for the window (runtime/menu_assets.c reads the same font for the in-game menu's titles). +"""The player's game files, as far as the launcher reads them itself: finding a .nup dump, the game's Cetra +Display font for the window (runtime/menu_assets.c reads the same font for the in-game menu's titles) and the game's +interface sounds for the window's sound effects (runtime/menu_sound.c plays the same files in the in-game menu). -The font comes from the built game's romfs/ (romfs.bin and index.json, tools/make_game.py) or, before the first -build, straight from the .nup. They are decoded once into data/cache/fonts, which stays on the player's PC like the -rest of what is built from their game. Nothing of the game ships with the launcher. +They come from the built game's romfs/ (romfs.bin and index.json, tools/make_game.py) or, before the first +build, straight from the .nup. They are decoded once into data/cache/fonts and data/cache/sounds, which stay on the +player's PC like the rest of what is built from their game. Nothing of the game ships with the launcher. - .zs files are zstd frames, most with one of the dictionaries of Pack/ZsDic.pack.zs (a SARC, compressed without a dictionary; a frame names its dictionary by id). - A .bfotf font is an OpenType file behind an 8-byte header, every big-endian 32-bit word XORed with one key: the first word is 0x7f9a0218 under it, the second the payload size. +- A .bwav sound: "BWAV", a byte order mark, a version, a CRC32, a prefetch flag and the channel count (u16 at 0x0e), + then 0x4c bytes per channel from 0x10: the codec (u16: 0 PCM16, 1 DSP-ADPCM), the pan (u16: 0 left, 1 right, + 2 middle), the sample rate and the sample count (u32 each), 16 DSP coefficients (s16 from +0x10) and the offset of + the samples in the file (u32 at +0x30). DSP-ADPCM: 8-byte frames of a header byte (coefficient pair, scale + exponent) and 14 four-bit samples. """ import json import os @@ -20,6 +26,11 @@ TITLE_ID = 0x0400486024DEC000 FONTS = { # role -> RomFS path; the window sets all its text in Cetra Display 'title': 'Font2/Scalable/CetraDisplayPro-Medium.Nin_Ounce_NVN2.bfotf.zs', } +SOUNDS = { # the window's sound effect (launcher/ui/sound.py) -> the game's interface sound + 'move': 'Sound/Resource/Stream/SE_Sys_Cursor00.bwav', + 'select': 'Sound/Resource/Stream/SE_Sys_Do00.bwav', + 'back': 'Sound/Resource/Stream/SE_Sys_Cancel00.bwav', +} DICTS = 'Pack/ZsDic.pack.zs' DICT_NAMES = ('zs.zsdic', 'pack.zsdic', 'bcett.byml.zsdic', 'bkprb.zsdic') MAX_RAW = 64 << 20 @@ -320,3 +331,90 @@ def extract_fonts(layout, game_dir='', dump=''): (d / f'{role}.family').write_text(fam, encoding='utf-8') out[role] = (d / f'{role}.otf', fam) return out + + +# ---------------------------------------------------------------- interface sounds + +def bwav_to_pcm(b): + """A .bwav as (sample rate, frames): frames a list of (left, right) 16-bit samples, or None when it is no BWAV of + one or two channels this reads. A middle-panned channel goes to both sides at 0.707 when it is the only one.""" + if not b or len(b) < 0x10 or b[:4] != b'BWAV': + return None + channels = struct.unpack_from(' len(b): + return None + rate, frames = struct.unpack_from(' len(b): + return None + samples = struct.unpack_from(f'<{count}h', b, off) + elif codec == 1: + if off + (count + 13) // 14 * 8 > len(b): + return None + samples, h1, h2 = [], 0, 0 + for i in range(count): + fr = off + i // 14 * 8 + k = i % 14 + c1, c2 = coef[2 * (b[fr] >> 4 & 7)], coef[2 * (b[fr] >> 4 & 7) + 1] + nib = b[fr + 1 + k // 2] >> (0 if k & 1 else 4) & 15 + if nib >= 8: + nib -= 16 + v = (((nib << (b[fr] & 15)) << 11) + c1 * h1 + c2 * h2 + 1024) >> 11 + v = -32768 if v < -32768 else 32767 if v > 32767 else v + h2, h1 = h1, v + samples.append(v) + else: + return None + gain = 0.7071 if channels == 1 else 1.0 + for i, v in enumerate(samples): + if pan != 1: + left[i] += v * gain + if pan != 0: + right[i] += v * gain + clip = lambda v: -32768 if v < -32768 else 32767 if v > 32767 else int(round(v)) # noqa: E731 + return rate, [(clip(a), clip(c)) for a, c in zip(left, right)] + + +def pcm_to_wav(rate, frames): + """A 16-bit stereo WAV of frames, a list of (left, right) samples.""" + pcm = struct.pack(f'<{len(frames) * 2}h', *[v for fr in frames for v in fr]) + return (b'RIFF' + struct.pack(' 0 else out -def wav_bytes(samples, level): - """A 16-bit mono WAV of samples (-1..1) at level (0..1 of full scale).""" +def wav_bytes(samples, level, rate=RATE, channels=1): + """A 16-bit WAV of samples (-1..1; frames by channels when there are two) at level (0..1 of full scale).""" pcm = (np.clip(samples * level, -1, 1) * 32767).astype(' 0 else a + + class Sounds: """Plays the effects at the level of the Sounds choice. Every failure is silent: a PC without a sound device, or without a player program, just has a quiet launcher.""" @@ -77,6 +100,8 @@ class Sounds: self.cache = cache_dir self.log = log or (lambda line: None) self.level = None # set_level below: 'off' unless the player chose a loudness + self.game = {} # the game's sounds decoded from the player's files: name -> (rate, channels, samples) + self.last_choice = 0.0 # when a sound other than the cursor's last started self.wavs = {} # name -> WAV bytes at the current level (Windows plays from this memory: it must stay) self.files = {} # name -> the WAV file elsewhere self.player = None # elsewhere: the program that plays a WAV file @@ -92,6 +117,22 @@ class Sounds: else: self.player = next((p for p in ('pw-play', 'paplay', 'aplay') if shutil.which(p)), None) self.set_level(level) + self.load_game() + + def load_game(self): + """Takes up the game's sounds decoded into /sounds (again after gamefiles.extract_sounds wrote them).""" + game = {} + for name in {g for g, _gain in GAME_SOUND.values()}: + try: + got = read_wav((self.cache / 'sounds' / f'{name}.wav').read_bytes()) + except OSError: + got = None + if got: + game[name] = got + if set(game) != set(self.game): + self.stop() + self.game, self.wavs, self.files = game, {}, {} + return bool(game) def set_level(self, level): level = level if level in LEVELS else 'off' @@ -103,7 +144,12 @@ class Sounds: def wav(self, name): if name not in self.wavs: - self.wavs[name] = wav_bytes(synth(name), LEVELS[self.level]) + source, gain = GAME_SOUND[name] + if source in self.game: + rate, channels, samples = self.game[source] + self.wavs[name] = wav_bytes(samples, LEVELS[self.level] * gain, rate, channels) + else: + self.wavs[name] = wav_bytes(synth(name), LEVELS[self.level]) return self.wavs[name] def stop(self): @@ -117,6 +163,12 @@ class Sounds: def play(self, name): if LEVELS.get(self.level, 0) <= 0 or self.failed or name not in EFFECTS: return + now = time.monotonic() + if GAME_SOUND[name][0] == 'move': + if now - self.last_choice < QUIET_AFTER: + return + else: + self.last_choice = now try: if self.winmm: # SND_ASYNC | SND_NODEFAULT | SND_MEMORY: PlaySound reads the bytes while it plays, and self.wavs diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 83e36a37..4735feb5 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -18,7 +18,8 @@ Run: python tests/launcher_test.py (Python 3.14 for the zstd tests; the window a source checkout's build/xlate/), the prompts step with a stand-in prompt tool, the object cache of the compile step with the system's C compiler, and its windres call under folders with spaces (a script in windres's place) - jobs: an install driven by a fake make_game.py, with and without the prompts row -- the look: the colour schemes, the background picture's fitting and kept copy, the sound effects (off by default) +- the look: the colour schemes, the background picture's fitting and kept copy, the sound effects (off by default, + the game's own interface sounds decoded from a made-up .bwav) - the window: --selftest draws every screen (skipped without a display) """ import contextlib @@ -2549,6 +2550,71 @@ class LookTest(Tmp): s.play('move') popen.assert_called_once() + @staticmethod + def bwav(channels, rate=48000): + """A .bwav of channels given as (codec, pan, samples, data): the header, 0x4c bytes a channel, the data.""" + out = bytearray(b'BWAV' + struct.pack(' Date: Tue, 10 Nov 2026 00:44:36 -0500 Subject: [PATCH 05/13] Keep only the game's select and back sounds in the launcher, and make none of its own. The cursor's sound played at every move of the pointer and cut the other two short, as one sound plays at a time: it is gone, and moving the cursor is silent. A choice, a changed switch, Play and the end of a build play the game's decide sound; going back and a failure play its cancel sound. The chimes the launcher made from sine tones are removed too, so every sound it plays is decoded from the player's own game into data/cache/sounds and it is silent until the game has been read. No sound file is in the repository, the release or the setup. Co-Authored-By: Claude Fable 5.2 --- docs/launcher.md | 14 ++--- docs/releases/v0.6.2.md | 2 +- launcher/gamefiles.py | 5 +- launcher/ui/app.py | 27 +++++----- launcher/ui/sound.py | 93 +++++++-------------------------- tests/launcher_test.py | 111 ++++++++++++++++++---------------------- 6 files changed, 89 insertions(+), 163 deletions(-) diff --git a/docs/launcher.md b/docs/launcher.md index b2f9c72d..33c04bd1 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -47,13 +47,13 @@ Customize Launcher (on the home screen) makes the look the player's own: - Animations (on by default): the cursor glides from row to row, a new screen slides into place, sparks drift up over the backdrop and Navi bobs in the title band with a pulsing halo (`App.animate`). They move by the time that passed, stand still while the window is hidden or minimised, and Off stops them all. -- Sounds (off by default; Low, Medium or High): the game's own interface sounds, the cursor, decide and cancel - sounds of its menus, when the cursor moves or a choice changes, a row is chosen and the player goes back. The - launcher decodes them from the player's built game or dump into `data/cache/sounds` (`gamefiles.extract_sounds`: - `Sound/Resource/Stream/SE_Sys_Cursor00.bwav`, `SE_Sys_Do00.bwav` and `SE_Sys_Cancel00.bwav`, the files - `runtime/menu_sound.c` plays in the in-game menu), so no sound ships; until they are decoded, chimes made from sine - tones in `launcher/ui/sound.py` stand in. They are played from memory (PlaySound on Windows; `pw-play`, `paplay` - or `aplay` elsewhere). +- Sounds (off by default; Low, Medium or High): two of the game's own interface sounds, the decide and cancel sounds + of its menus, when a row is chosen or a choice changes and when the player goes back. The launcher decodes them + from the player's built game or dump into `data/cache/sounds` (`gamefiles.extract_sounds`: + `Sound/Resource/Stream/SE_Sys_Do00.bwav` and `SE_Sys_Cancel00.bwav`, the files `runtime/menu_sound.c` plays in the + in-game menu). No sound is in the repository, the release or the setup, and the launcher makes none of its own: + it is silent until the player's game has been read. Moving the cursor makes no sound. They are played from memory + (PlaySound on Windows; `pw-play`, `paplay` or `aplay` elsewhere). The choices are `theme`, `background`, `background_dim`, `animations` and `sounds` in `data/launcher.json`. diff --git a/docs/releases/v0.6.2.md b/docs/releases/v0.6.2.md index 28ab3581..2e96eae0 100644 --- a/docs/releases/v0.6.2.md +++ b/docs/releases/v0.6.2.md @@ -9,7 +9,7 @@ Draft notes for the release after 0.6.1. The version number is not decided yet. it takes effect at the next start. - Customize Launcher (on the launcher's home screen): six colour schemes, a background picture of your own with adjustable dimming, animations (a gliding cursor, sliding screens, drifting sparks) and sound effects: the game's - own cursor, select and back sounds, read from your game. The sounds are off until you turn them on. + own select and back sounds, read from your game. The sounds are off until you turn them on. - Three more settings take effect without a restart: HDR between off and the tone mapper (only the game's native HDR still waits for one), the camera's stick sensitivity, and the speakers (stereo or 5.1). diff --git a/launcher/gamefiles.py b/launcher/gamefiles.py index 5147a01b..bf3998a0 100644 --- a/launcher/gamefiles.py +++ b/launcher/gamefiles.py @@ -27,7 +27,6 @@ FONTS = { # role -> RomFS path; the window sets all its text in Cetra Display 'title': 'Font2/Scalable/CetraDisplayPro-Medium.Nin_Ounce_NVN2.bfotf.zs', } SOUNDS = { # the window's sound effect (launcher/ui/sound.py) -> the game's interface sound - 'move': 'Sound/Resource/Stream/SE_Sys_Cursor00.bwav', 'select': 'Sound/Resource/Stream/SE_Sys_Do00.bwav', 'back': 'Sound/Resource/Stream/SE_Sys_Cancel00.bwav', } @@ -398,8 +397,8 @@ def cached_sounds(layout): def extract_sounds(layout, game_dir='', dump=''): - """Decode the game's cursor, select and back sounds into data/cache/sounds as WAV files; {effect: path} of the - ones found.""" + """Decode the game's select and back sounds into data/cache/sounds as WAV files; {effect: path} of the ones + found.""" romfs = open_romfs(layout, game_dir, dump) if romfs is None: return {} diff --git a/launcher/ui/app.py b/launcher/ui/app.py index 39035e8a..a1ecb8ff 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -21,7 +21,7 @@ Mouse, keyboard (arrows, Enter or Space, Esc, F5) and an XInput gamepad (d-pad o The look is the player's (Customize Launcher): art.set_theme fills the palette every picture is drawn from, a picture of the player's own can replace the backdrop (picture.py), and unless Animations is off the cursor glides between rows, a new screen slides in, sparks drift over the backdrop and Navi bobs in the title band (animate). sound.py -plays a short chime for a move, a change, a choice and the end of a job. +plays the game's own decide and cancel sounds, read from the player's game, for a choice and for going back. """ import argparse import math @@ -482,10 +482,7 @@ class App: if not idx: return pos = idx.index(self.focus) if self.focus in idx else 0 - was = self.focus self.focus = idx[max(0, min(len(idx) - 1, pos + d))] - if self.focus != was: - self.sounds.play('move') self.render() def focused(self): @@ -503,7 +500,8 @@ class App: r.pick = max(0, min(len(r.actions) - 1, getattr(r, 'pick', 0) + d)) else: return - self.sounds.play('move' if r.kind == 'pair' else 'toggle') + if r.kind != 'pair': # a switch or a choice changed + self.sounds.play('select') self.render() def activate(self): @@ -511,10 +509,10 @@ class App: if not r or not r.enabled: return if r.kind == 'toggle': - self.sounds.play('toggle') + self.sounds.play('select') r.on(not r.get()) elif r.kind == 'enum': - self.sounds.play('toggle') + self.sounds.play('select') r.right() elif r.kind == 'pair': self.sounds.play('select') @@ -572,7 +570,6 @@ class App: r = self.rows[i] if r.kind == 'pair': r.pick = 0 if e.x < (r.rect[0] + r.rect[2]) / 2 else 1 - self.sounds.play('move') self.render() elif i is not None and self.rows[i].kind == 'pair': r = self.rows[i] @@ -608,12 +605,12 @@ class App: return if r.kind == 'toggle' and r.value_rect: x0, _, x1, _ = r.value_rect - self.sounds.play('toggle') + self.sounds.play('select') r.on(e.x < (x0 + x1) / 2) self.render() elif r.kind == 'enum' and r.value_rect: x0, _, x1, _ = r.value_rect - self.sounds.play('toggle') + self.sounds.play('select') (r.left if e.x < x0 + (x1 - x0) / 3 else r.right)() self.render() elif r.kind == 'pair': @@ -925,7 +922,7 @@ class App: self.settings['sounds'] = keys[max(0, min(len(keys) - 1, at + d))] self.settings.save() self.sounds.set_level(self.settings['sounds']) - self.sounds.play('toggle') # the new loudness, heard at once + self.sounds.play('select') # the new loudness, heard at once bg_desc = ('Use a picture of your own as the background.',) if self.look_message: @@ -951,7 +948,7 @@ class App: desc=('A gliding cursor, sliding screens and drifting sparks.',)), Row('enum', 'Sounds', get=lambda: dict(SOUND_LEVELS).get(self.settings.get('sounds'), 'Off'), left=lambda: sounds(-1), right=lambda: sounds(1), - desc=('The game\'s cursor, select and back sounds. Off by default.',)), + desc=('The game\'s select and back sounds. Off by default.',)), Row('divider'), Row('action', 'Back', on=lambda: self.show('home', self.home_row('Customize Launcher')), height=40, desc=('Back to the main menu.',)), @@ -973,7 +970,7 @@ class App: self.ctx.log(f'background: {path}: {e!r}') self.look_message = str(e) if isinstance(e, picture.PictureError) else \ f'{Path(path).name} could not be used as the background: {e}' - self.sounds.play('error') + self.sounds.play('back') self.show('customize', self.focus) return self.look_message = '' @@ -1224,7 +1221,7 @@ class App: self.render() return self.ctx.log(f'play: {exe}') - self.sounds.play('start') + self.sounds.play('select') if self.settings.get('hide_while_playing'): self.root.withdraw() self.root.after(1000, self.watch_game) @@ -1502,7 +1499,7 @@ class App: winsys.keep_awake(False) self.ctx.log(f'job {j.kind}: {j.result} {j.error}') if j.result in ('ok', 'failed'): - self.sounds.play('done' if j.result == 'ok' else 'error') + self.sounds.play('select' if j.result == 'ok' else 'back') if j.result == 'ok': if j.after == 'restart': if winsys.restart(self.layout, ['--continue-update', versions.display(self.layout.version)]): diff --git a/launcher/ui/sound.py b/launcher/ui/sound.py index 32047832..f49b7d52 100644 --- a/launcher/ui/sound.py +++ b/launcher/ui/sound.py @@ -1,77 +1,31 @@ -"""The launcher's sound effects: the game's own interface sounds, the ones its menus make for the cursor, a choice -and going back, read from the player's game (launcher/gamefiles.py extract_sounds decodes them into -data/cache/sounds, as runtime/menu_sound.c plays them in the in-game menu). No sound file ships: until the player's -game has been read, and for a sound it does not hold, short chimes made here from sine tones stand in. +"""The launcher's sound effects: two of the game's own interface sounds, the ones its menus make for a choice and for +going back, read from the player's game (launcher/gamefiles.py extract_sounds decodes them into data/cache/sounds, as +runtime/menu_sound.c plays them in the in-game menu). No sound ships with the launcher and none is made here: until +the player's game has been read, and for a sound it does not hold, the launcher is silent. -They are off until the player picks a loudness in Customize Launcher (the Sounds row). An effect is a 16-bit WAV in -memory: the game's sound scaled to the loudness (the cursor's a little under the others, as in the game's menu), or a -few plucked notes (a tone with two overtones, a quick attack and an exponential decay). Windows plays it with -PlaySound from that memory; elsewhere the WAV goes to a file in the cache folder and the first of pw-play, paplay -and aplay found plays it. One effect sounds at a time: a new one cuts the last, so the cursor's sound is left out -for a quarter of a second after a choice, which the pointer landing on a row of the next screen would otherwise cut. +They are off until the player picks a loudness in Customize Launcher (the Sounds row). An effect is the game's sound +as a 16-bit WAV in memory, scaled to the loudness. Windows plays it with PlaySound from that memory; elsewhere the WAV +goes to a file in the cache folder and the first of pw-play, paplay and aplay found plays it. One effect sounds at a +time: a new one cuts the last. Moving the cursor makes no sound, as it would cut a choice's sound short every time +the pointer lands on a row of the next screen. - move the cursor went to another row the game's cursor sound - toggle a switch or a choice changed the cursor sound - select a row was chosen (another screen) the game's decide sound - back back to the screen before the game's cancel sound - start Play the decide sound - done an install, update or rebuild finished the decide sound - error something could not be done the cancel sound + select a row was chosen, a switch or a choice changed, Play, the end of a build the game's decide sound + back back to the screen before, something could not be done the game's cancel sound """ import ctypes import os import shutil import struct import subprocess -import time import numpy as np -RATE = 44100 LEVELS = {'off': 0.0, 'low': 0.18, 'medium': 0.4, 'high': 0.75} # the Sounds choice: the loudest sample's share of full scale -# Which of the game's sounds (gamefiles.SOUNDS) each effect plays, and how loud beside the others. -GAME_SOUND = {'move': ('move', 0.6), 'toggle': ('move', 0.6), 'select': ('select', 1.0), 'start': ('select', 1.0), - 'done': ('select', 1.0), 'back': ('back', 1.0), 'error': ('back', 1.0)} -QUIET_AFTER = 0.25 # seconds after a choice in which the cursor's sound is left out - -# Notes as (start in seconds, frequency in Hz, length in seconds, loudness 0..1). -C5, D5, E5, G5, A5, C6, D6, E6, G6, C7 = 523.25, 587.33, 659.25, 783.99, 880.0, 1046.5, 1174.66, 1318.51, 1567.98, 2093.0 -EFFECTS = { - 'move': [(0, E6, 0.07, 0.55)], - 'toggle': [(0, G5, 0.10, 0.8), (0.035, D6, 0.10, 0.5)], - 'select': [(0, C6, 0.12, 0.8), (0.07, G6, 0.20, 0.8)], - 'back': [(0, G5, 0.12, 0.8), (0.07, C5, 0.20, 0.7)], - 'start': [(0, C6, 0.14, 0.7), (0.08, E6, 0.14, 0.7), (0.16, G6, 0.14, 0.7), (0.24, C7, 0.45, 0.8)], - 'done': [(0, G5, 0.16, 0.8), (0.12, C6, 0.16, 0.8), (0.24, E6, 0.16, 0.8), (0.36, G6, 0.5, 0.9)], - 'error': [(0, 220.0, 0.16, 0.9), (0.14, 174.61, 0.3, 0.9)], -} +EFFECTS = ('select', 'back') # the names play takes: the game's sounds of gamefiles.SOUNDS -def pluck(freq, length, loudness): - """One note: the tone and two quieter overtones, in within 4 ms and dying away over its length.""" - n = int(RATE * length) - t = np.arange(n, dtype=np.float32) / RATE - wave = np.sin(2 * np.pi * freq * t) + 0.35 * np.sin(2 * np.pi * 2 * freq * t) + 0.12 * np.sin(2 * np.pi * 3 * freq * t) - env = np.minimum(t / 0.004, 1) * np.exp(-t * 5.5 / length) - tail = np.minimum((length - t) / 0.01, 1) # the last 10 ms go to nothing, so the end does not click - return wave * env * tail * loudness - - -def synth(name): - """The effect's samples in -1..1, its loudest at 1.""" - notes = EFFECTS[name] - total = int(RATE * max(start + length for start, _f, length, _l in notes)) + 1 - out = np.zeros(total, dtype=np.float32) - for start, freq, length, loudness in notes: - note = pluck(freq, length, loudness) - at = int(RATE * start) - out[at:at + len(note)] += note - peak = float(np.abs(out).max()) - return out / peak if peak > 0 else out - - -def wav_bytes(samples, level, rate=RATE, channels=1): - """A 16-bit WAV of samples (-1..1; frames by channels when there are two) at level (0..1 of full scale).""" +def wav_bytes(samples, level, rate, channels): + """A 16-bit WAV of samples (-1..1, interleaved when there are two channels) at level (0..1 of full scale).""" pcm = (np.clip(samples * level, -1, 1) * 32767).astype(' (rate, channels, samples) - self.last_choice = 0.0 # when a sound other than the cursor's last started self.wavs = {} # name -> WAV bytes at the current level (Windows plays from this memory: it must stay) self.files = {} # name -> the WAV file elsewhere self.player = None # elsewhere: the program that plays a WAV file @@ -122,7 +75,7 @@ class Sounds: def load_game(self): """Takes up the game's sounds decoded into /sounds (again after gamefiles.extract_sounds wrote them).""" game = {} - for name in {g for g, _gain in GAME_SOUND.values()}: + for name in EFFECTS: try: got = read_wav((self.cache / 'sounds' / f'{name}.wav').read_bytes()) except OSError: @@ -144,12 +97,8 @@ class Sounds: def wav(self, name): if name not in self.wavs: - source, gain = GAME_SOUND[name] - if source in self.game: - rate, channels, samples = self.game[source] - self.wavs[name] = wav_bytes(samples, LEVELS[self.level] * gain, rate, channels) - else: - self.wavs[name] = wav_bytes(synth(name), LEVELS[self.level]) + rate, channels, samples = self.game[name] + self.wavs[name] = wav_bytes(samples, LEVELS[self.level], rate, channels) return self.wavs[name] def stop(self): @@ -161,14 +110,8 @@ class Sounds: pass def play(self, name): - if LEVELS.get(self.level, 0) <= 0 or self.failed or name not in EFFECTS: + if LEVELS.get(self.level, 0) <= 0 or self.failed or name not in self.game: return - now = time.monotonic() - if GAME_SOUND[name][0] == 'move': - if now - self.last_choice < QUIET_AFTER: - return - else: - self.last_choice = now try: if self.winmm: # SND_ASYNC | SND_NODEFAULT | SND_MEMORY: PlaySound reads the bytes while it plays, and self.wavs diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 4735feb5..9db77f43 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -19,7 +19,7 @@ Run: python tests/launcher_test.py (Python 3.14 for the zstd tests; the window step with the system's C compiler, and its windres call under folders with spaces (a script in windres's place) - jobs: an install driven by a fake make_game.py, with and without the prompts row - the look: the colour schemes, the background picture's fitting and kept copy, the sound effects (off by default, - the game's own interface sounds decoded from a made-up .bwav) + only the game's own select and back sounds, decoded from a made-up .bwav) - the window: --selftest draws every screen (skipped without a display) """ import contextlib @@ -2514,42 +2514,6 @@ class LookTest(Tmp): with self.assertRaises(self.picture.PictureError): self.picture.read_ppm(b'P5\n1 1\n255\n\0') - def test_the_effects_are_short_wavs(self): - sound = self.sound - for name in sound.EFFECTS: - samples = sound.synth(name) - self.assertAlmostEqual(float(abs(samples).max()), 1.0, places=5, msg=name) - self.assertLess(len(samples) / sound.RATE, 1.0, name) - self.assertLess(abs(float(samples[-1])), 0.01, name) # it ends at nothing: no click - wav = sound.wav_bytes(samples, 0.4) - self.assertEqual(wav[:4] + wav[8:16], b'RIFFWAVEfmt ') - self.assertEqual(struct.unpack_from(' Date: Tue, 10 Nov 2026 01:36:29 -0500 Subject: [PATCH 06/13] Keep the driver's cache of stage libraries warm, link a missing pipeline instead of skipping its draw, and count the texture cache's waits in time. The preparation builds every stage library once so the driver's disk cache holds it and a library a draw needs later takes under a millisecond. Turning dynamic vertex input off for AMD's RDNA 3 and newer driver changed the dynamic states every library lists, which gave each one another entry in the driver's cache, and nothing built them again: an update carried the prepared shaders over without the driver step. On an RX 7900 XT (26.10.1) all 42,142 prepared libraries were cold, 93 ms a vertex and 242 ms a fragment library on average. With no pipeline recorded, a walk through a village skipped 11,180 draws while their pipelines built and made 94 draws wait 5.5 s in all, four of them 1.0 to 2.0 s; a player's session log shows up to 114,834 draws skipped in 600 frames and 181 waits of 251 ms on average. Skipped ground and walls appear and disappear, and a skipped lighting pass is a black frame with the interface intact. - The game checks the driver's cache at every start (vk_pipeline.c lw_start): a sample of the prepared fragment libraries is built, and when their median time says the cache is cold the whole prepared list is built into it again on idle-priority threads while the game runs. shader-warm.txt records a warm in progress, so the next start goes on with one a run ended, and stops the tries on a driver that does not keep the libraries. This also covers a driver update and a cache the driver emptied. - An update that carries the prepared shaders over runs the game's --warm-shaders (tools/make_game.py warm_driver_cache): 11 s with the libraries there, about 8 minutes with all of them cold on that card. - A draw whose pipeline is missing waits for its fast link from a budget of 6 ms a presented frame, 3 ms at most each (NX2NAVI_PIPE_LINK_MS), before it is skipped: with warm libraries the link is done within that. - Full-screen draws wait from their 50 ms budget again instead of being skipped (NX2NAVI_PIPE_SKIP = any is the default; fullscreen stays selectable). With warm libraries the wait is a millisecond. The same walk afterwards: libraries 0.3 and 0.7 ms on average, 1,605 draws skipped, 1,473 of them in the scene's first 600 frames and 8 to 80 in each 600 after, and 19 waits of 60 ms in all. First visits to four other saves (a mountain pass, the Lost Woods, Kokiri Forest, a river) waited 12 to 84 ms in all except the Lost Woods, where mesh shader pipelines, which have no stage libraries, and vertex libraries that need added outputs still compiled once: 23 waits, 2.1 s in all, the longest 776 ms. The texture cache counted its waits in presents chosen at 60 a second: at 165 presents a second the 1800 that stood for thirty seconds were 11 s, so a released render target went sooner than meant and a texture out of view for 11 s was retired and showed its stand-in for some frames when it came back. The counts are now taken times the present rate over 60 (presents_for), and a texture is retired after two minutes without a sample instead of thirty seconds. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 12 ++- docs/releases/v0.6.2.md | 9 ++ nx2navi.ini.example | 11 ++- runtime/cfg.c | 8 +- runtime/vk_exec_draw.c | 57 +++++++++++-- runtime/vk_pipeline.c | 179 +++++++++++++++++++++++++++++++++++++++- runtime/vk_texture.c | 39 +++++++-- tests/launcher_test.py | 30 +++++++ tools/make_game.py | 80 ++++++++++++------ 9 files changed, 381 insertions(+), 44 deletions(-) diff --git a/docs/building.md b/docs/building.md index 7126091c..e177e585 100644 --- a/docs/building.md +++ b/docs/building.md @@ -127,7 +127,17 @@ of them. Without this step the first launch compiles shaders and pipelines during play and stutters until the caches fill. The AMD driver keeps its pipeline cache per exe path and holds a limited amount: preparing another build folder can - evict this one's. `nx2navi.exe --warm-shaders` rebuilds it from the shader cache. + evict this one's. `nx2navi.exe --warm-shaders` rebuilds it from the shader cache. A driver update empties the + driver's cache too, and a change to what a stage library is built with gives every library another entry in it. + So an update that carries the prepared shaders over runs `--warm-shaders` (`tools/make_game.py` + `warm_driver_cache`: seconds while the libraries are there, minutes when they are not), and the game itself checks + at every start: it builds a sample of the prepared fragment libraries and, when their median time says the cache + is cold, builds the whole prepared list into the driver's cache again on idle-priority threads while it runs + (`runtime/vk_pipeline.c` `lw_start`, `NX2NAVI_LIB_WARM`; `shader-warm.txt` next to the caches says where that + stands). With the cache cold every pipeline a draw first needs compiles in play: on an RX 7900 XT a library took + 93 ms (vertex) and 242 ms (fragment) on average against 0.3 and 0.6 ms from the cache, and a first walk through a + village skipped 11,180 draws and made 94 draws wait 5.5 s in all, against 1,605 skipped (1,473 of them in the + scene's first frames) and 60 ms of waits with the cache warm. When the driver's shader compiler crashes on a variant (NVIDIA 615.78.08 on an RTX 5080 does, issue #30), `prepare_shaders.py` runs the driver step again with `--warm-shaders`, up to three times, without the variants the driver was building at the crash. `shader-prepare-skip.txt` next to the exe lists them with the GPU and driver diff --git a/docs/releases/v0.6.2.md b/docs/releases/v0.6.2.md index 2e96eae0..153ca7ef 100644 --- a/docs/releases/v0.6.2.md +++ b/docs/releases/v0.6.2.md @@ -15,6 +15,15 @@ Draft notes for the release after 0.6.1. The version number is not decided yet. ## Fixed +- Ground, walls and characters that appeared and disappeared, black or unlit frames, and stalls of up to two seconds + wherever something was shown for the first time (a new area, a cutscene). The graphics driver's cache had lost the + shader libraries prepared at install, so every new pipeline compiled during play: after a driver update, or after + an update of NX2navi that changes how the libraries are built (this one does on AMD RX 7000 and RX 9000 cards, + where it also ends the 20 second start). The game now checks the driver's cache at every start and fills it again in the background by + itself, an update fills it again before you play, draws wait a moment for a pipeline that only needs linking + instead of being left out, and lighting passes are no longer left out at all. +- Textures out of view for 11 seconds at 165 fps (30 seconds were meant) could show a blank stand-in for a few frames + when they came back while memory was short. The wait is now two minutes of real time at any frame rate. - Settings menu, mouse: a setting changes only on the two framed arrows beside its value, and the arrows stay in one place. A click anywhere on a row used to step the setting, and the left arrow moved with the value's width, so clicking through the values landed on the row and stepped the wrong way. Holding the button on an arrow repeats. diff --git a/nx2navi.ini.example b/nx2navi.ini.example index f915c9df..f48db1d1 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -71,8 +71,15 @@ # Draws whose pipeline is still building: default waits on a reserved worker; draw is the previous scheduling. # pipe_wait = default # Which draws are dropped instead of waiting for their pipeline: strict (only draws into per-frame passes), any (also -# draws into passes whose target a recent frame rendered) or fullscreen (those and full-screen draws). -# pipe_skip = fullscreen +# draws into passes whose target a recent frame rendered; full-screen draws wait, up to 50 ms a frame) or fullscreen +# (those and full-screen draws: no wait, but a lighting or fog pass that is dropped shows as a black or unlit frame). +# pipe_skip = any +# How many milliseconds the draws of one frame may wait, in all, for a missing pipeline to be linked from the shader +# libraries in the graphics driver's cache before they are dropped (0 to 100; 0: dropped at once, as before). +# pipe_link_ms = 6 +# The graphics driver's cache of shader libraries is checked at every start and, when it lost them (a driver update, +# a cache the driver emptied), filled again in the background while the game runs. 0: no check; force: at every start. +# lib_warm = 1 # How long (ms) a draw into a target no recent frame rendered waits for a pipeline built for the first time, # then is skipped. The default waits, as such a target (a lookup table, a new area's target) can stay empty if # skipped; a lower value caps the stall. diff --git a/runtime/cfg.c b/runtime/cfg.c index 7c03acd2..3edea66f 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -47,7 +47,7 @@ static const char *const known[] = { "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_LIVE", "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", - "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", + "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "PIPE_LINK_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", @@ -111,7 +111,7 @@ static const struct { const char *key, *section; } sections[] = { {"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"POOL_CREATE_BUDGET", "performance"}, {"POOL_CREATE_MS", "performance"}, {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, - {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"}, + {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"TEX_DISUSE", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"BIND_SKIP", "performance"}, {"SHADER_CACHE_FLUSH", "performance"}, {"SHADER_CARRY", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, @@ -208,8 +208,10 @@ static const CfgMeta metas[] = { // held submissions handed to the queue together (vk_sync.c, submit batching) {"SUBMIT_BATCH", "4", "1..16", RESTART}, // shaders and pipelines - {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART}, + {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "any", "strict|any|fullscreen", RESTART}, + {"PIPE_LINK_MS", "6", "0..100", RESTART}, // vk_exec_draw.c link_budget {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, + {"LIB_WARM", "1", "0|1|force", RESTART}, // vk_pipeline.c lw_start: the driver's cache of stage libraries, warmed when cold {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART}, {"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART}, {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "0", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 23fbc3ed..49001d1c 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -3249,23 +3249,53 @@ static void foliage_shadow_cull(const VkExec *ex, VkPipeState *s) { // buffer or g-buffer with a colour target the frame before did not use: half-resolution effect buffers in fights). // Passes with none (volumes, LUTs, shadow layers seen for the first time) still wait, and so do passes whose only // recent target is the depth buffer while a colour target is rendered for the first time (item icon captures). -// fullscreen (2, default): as any, and full-screen draws into such passes are skipped instead of waiting from their +// Full-screen draws into such passes wait from their budget of 50 ms a presented frame. The default: a skipped +// full-screen draw is a lighting, fog or tone mapping pass that leaves its target as it was, which shows as a black +// or unlit frame with the interface intact, and with the stage libraries in the driver's cache the wait is a +// millisecond (vk_pipeline.c lw_start keeps them there). +// fullscreen (2): as any, and full-screen draws into such passes are skipped instead of waiting from their // budget. A full-screen pass whose stage libraries are cold in the driver cache took 86 ms on average (up to 194 ms) // in a fight on an RX 9070: three frames in a row each spent the 50 ms budget, and some of the draws were skipped -// after all. Skipping costs the pass for the frames until the pipeline is ready. +// after all. Skipping costs the pass for the frames until the pipeline is ready. It was the default until the +// libraries were kept warm. +// Any other draw whose pipeline is missing waits for its fast link from a small budget per presented frame +// (link_budget) before it is skipped. static int pipe_skip_mode(void) { static int mode = -1; if (mode < 0) { const char *e = cfg_value("NX2NAVI_PIPE_SKIP"); mode = !strcmp(e, "strict") || !strcmp(e, "0") ? 0 : !strcmp(e, "fullscreen") || !strcmp(e, "2") ? 2 : 1; static const char *const what[3] = {"only draws into per-frame passes (NX2NAVI_PIPE_SKIP=strict)", - "draws into passes with a target the previous frame rendered", + "draws into passes with a target the previous frame rendered; full-screen " + "draws wait (NX2NAVI_PIPE_SKIP=any)", "those and full-screen draws (NX2NAVI_PIPE_SKIP=fullscreen)"}; rt_log("vk_exec: draws skipped while their pipeline builds: %s", what[mode]); } return mode; } +// NX2NAVI_PIPE_LINK_MS (default 6; 0 = off): how long the draws of one presented frame may wait, in all, for the fast +// link of a pipeline that is missing, LINK_DRAW_MS at most each. A draw into a per-frame pass used to be skipped at +// once and for as long as its pipeline built, also when its stage libraries were in the driver's cache and the link +// took under a millisecond: geometry seen for the first time (ground, a wall, a character) was missing for a frame or +// more and then appeared. With the libraries warm the wait ends within the budget and nothing is skipped; with them +// cold it runs out after a few draws and the rest are skipped as before, so a burst of cold pipelines costs a frame +// this much and no more. Measured on an RX 7900 XT, first walk through a village with no pipeline recorded: 2,349 +// draws skipped without the budget, with the libraries warm in both runs. +#define LINK_DRAW_MS 3u +static uint64_t *link_budget(void) { + static uint64_t frame = ~0ull, left, per_frame = ~0ull; + if (per_frame == ~0ull) { + long long ms = cfg_int("NX2NAVI_PIPE_LINK_MS"); + per_frame = (uint64_t)(ms < 0 ? 0 : ms > 100 ? 100 : ms) * 1000000ull; + rt_log("vk_exec: draws wait up to %llu ms a frame in all for the fast link of a missing pipeline " + "(NX2NAVI_PIPE_LINK_MS)", (unsigned long long)(per_frame / 1000000)); + } + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame) frame = f, left = per_frame; + return &left; +} + // The previous draw's shaders, kept while its program, bound pipeline state, the vertex and fragment stages' first 32 // texture and image handles and every slot's resolution (vk_texture_slots_gen, read unlocked: a registration racing // with it is ordered after this draw, as answers_for's own check would order it) stay the same: answers_for reads @@ -3461,6 +3491,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int uint32_t wait_ms = 0; uint64_t t0 = 0; uint64_t *budget = NULL; // the per-frame budget a timed wait is charged to + int linking = 0; // the wait is the short one for a fast link (link_budget) int large = (uint64_t)ex->pass_w * ex->pass_h >= ONESHOT_LARGE_PX; if (oneshot > 0 && oneshot_budget_on()) { budget = oneshot_budget(large); @@ -3474,6 +3505,12 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int wait_ms = (uint32_t)(wait_left_ns / 1000000); if (wait_ms) t0 = kern_now_ns(); budget = &wait_left_ns; + } else { + budget = link_budget(); + wait_ms = (uint32_t)(*budget / 1000000); + if (wait_ms > LINK_DRAW_MS) wait_ms = LINK_DRAW_MS; + linking = wait_ms != 0; + if (!wait_ms) budget = NULL; } // The previous draw's pipeline while its state repeats (PM, NX2NAVI_PIPE_MEMO=0 turns it off): the cache key is made // from the state alone, and a pipeline once found only changes from its fast link to its optimised build, read @@ -3485,13 +3522,21 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int info = PM.info, pending = 0; p = __atomic_load_n(&info->pipeline, __ATOMIC_ACQUIRE); } else { - w0 = t0 ? t0 : wait_ms ? kern_now_ns() : 0; // the clock is read only for draws that may wait - p = vk_pipeline_graphics_async(&s, &info, &pending, wait_ms); + if (linking) { // nearly every draw finds its pipeline: the clock is read only for one that did not + p = vk_pipeline_graphics_async(&s, &info, &pending, 0); + if (pending) { + t0 = kern_now_ns(); + p = vk_pipeline_graphics_async(&s, &info, &pending, wait_ms); + } + } else { + w0 = t0 ? t0 : wait_ms ? kern_now_ns() : 0; // the clock is read only for draws that may wait + p = vk_pipeline_graphics_async(&s, &info, &pending, wait_ms); + } if (t0 && budget) { uint64_t spent = kern_now_ns() - t0; *budget = spent < *budget ? *budget - spent : 0; } - why = oneshot > 0 ? "one-shot, shader" : wait_ms ? "full-screen" : NULL; + why = oneshot > 0 ? "one-shot, shader" : linking ? NULL : wait_ms ? "full-screen" : NULL; if (pending && oneshot < 0 && !vk_exec_targets_recent(ex, skip >= 1)) { perf_count(PERF_CNT_ONESHOT_WAIT); why = "one-shot"; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 229887be..a30f99b8 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -4748,12 +4748,189 @@ static void lb_state(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) { LB.samples = (uint8_t)(1u << bf); } +// ---- the driver's cache of stage libraries, warmed again during play +// +// The preparation (lb_warm_prepared) builds every prepared variant's stage library once, so the driver's disk cache +// holds it and the library a draw needs later takes a few milliseconds instead of a compile: 3 ms against 100 ms for +// a vertex and 340 ms for a fragment library on an RX 7900 XT (driver 26.10.1). The driver's cache does not outlive +// everything, though: a driver update empties it, the AMD driver keeps a limited amount and drops old entries, and a +// change to what a library is built with (the dynamic states it lists, as when dynamic vertex input was turned off for +// this driver) gives every library another entry. After any of them each pipeline a draw first needs compiles cold: +// the draw is skipped for the frames that takes, or waits for it. Measured on that card with the cache cold, a walk +// through a village not seen before skipped 11,180 draws while their pipelines built and 94 draws waited 5.5 s in +// all, one of them 1.9 s. +// +// So every start checks: a sample of the prepared fragment libraries is built (lw_probe), and when their median time +// says the cache is cold, the whole prepared list is built again into the driver's cache on a few idle-priority +// threads while the game runs (lw_worker), as the preparation does but without stopping play. shader-warm.txt beside +// the caches says where that stands: "warming" while it runs, so a start after a run that ended meanwhile goes on, +// and "done" with the driver and a count of the times the cache was cold again right after a finished warm. At two in +// a row the driver does not keep stage libraries at all and warming stops being tried (NX2NAVI_LIB_WARM=0 turns it off; +// =force warms at every start). +#define LW_SAMPLE 12 +#define LW_COLD_MS 15.0 +#define LW_MAX_THREADS 6 +static struct { + GfxWarmEnt *e; + size_t n; + volatile int64_t next, done; + volatile int32_t running, stop, started; + PlatThread *th[LW_MAX_THREADS + 1]; + int threads, strikes; + double t0; + char stamp[PLAT_PATH_MAX * 2 + 32], id[64]; +} LW; + +static void lw_stamp(const char *state, int strikes) { + char line[128]; + int n = snprintf(line, sizeof line, "%s %s %d\n", state, LW.id, strikes); + if (!plat_file_write_atomic(LW.stamp, line, (size_t)n)) rt_log("vk_pipeline: cannot write %s", LW.stamp); +} + +// One prepared vertex or fragment variant's library into the driver's cache; its time in ms, negative when the +// variant is on the skip list, not in the shader cache or of another stage. +static double lw_build(const GfxWarmEnt *e) { + if (e->stage != GFX_STAGE_VS && e->stage != GFX_STAGE_FS) return -1; + if (skip_listed(e->iid)) return -1; + int owned = 0; + const GfxShader *sh = gfx_shader_load_private(e->iid, &owned); + if (!sh) return -1; + double t0 = now_ms(); + int st = gfx_shader_stage(sh); + if (st == GFX_STAGE_VS || st == GFX_STAGE_FS) lb_library(sh, st == GFX_STAGE_VS, 0); + double ms = now_ms() - t0; + if (owned) gfx_shader_release_private(sh); + return ms; +} + +static void lw_worker(void *arg) { + (void)arg; + plat_thread_priority(PLAT_PRIO_IDLE); + for (;;) { + size_t i = (size_t)plat_inc64(&LW.next) - 1; + if (i >= LW.n || LW.stop) break; + lw_build(&LW.e[i]); + int64_t d = plat_inc64(&LW.done); + if (d % 8000 == 0) + rt_log("vk_pipeline: library warm: %lld of %zu prepared variants in %.0f s", (long long)d, LW.n, + (now_ms() - LW.t0) / 1000); + } + if (!plat_dec32(&LW.running) && !LW.stop) { + lw_stamp("done", LW.strikes); + rt_log("vk_pipeline: library warm: done, %zu prepared variants in %.0f s on %d idle threads; new pipelines " + "link from the driver's cache from here on", LW.n, (now_ms() - LW.t0) / 1000, LW.threads); + } +} + +// The median time of LW_SAMPLE prepared fragment libraries spread over the list; negative without that many. +static double lw_probe(void) { + double ms[LW_SAMPLE]; + int got = 0; + size_t step = LW.n / LW_SAMPLE ? LW.n / LW_SAMPLE : 1, at = (size_t)(plat_now_ns() / 1000) % step; + for (size_t i = at; i < LW.n && got < LW_SAMPLE && !LW.stop; i += step) + for (size_t j = i; j < i + step && j < LW.n; j++) { // the next fragment variant from here + if (LW.e[j].stage != GFX_STAGE_FS) continue; + double t = lw_build(&LW.e[j]); + if (t >= 0) ms[got++] = t; + break; + } + if (got < LW_SAMPLE / 2) return -1; + for (int i = 1; i < got; i++) + for (int j = i; j > 0 && ms[j] < ms[j - 1]; j--) { + double t = ms[j]; + ms[j] = ms[j - 1], ms[j - 1] = t; + } + return ms[got / 2]; +} + +static void lw_main(void *arg) { + (void)arg; + plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); + char was[16] = "", id[64] = ""; + int strikes = 0; + uint64_t size = 0; + char *f = (char *)plat_file_read(LW.stamp, &size); + if (f && size < 200) { + char line[200]; + memcpy(line, f, size), line[size] = 0; + if (sscanf(line, "%15s %63s %d", was, id, &strikes) < 2) was[0] = 0; + } + free(f); + int same = !strcmp(id, LW.id), resume = same && !strcmp(was, "warming"); + const char *mode = cfg_value("NX2NAVI_LIB_WARM"); + double median = resume ? 0 : lw_probe(); + if (LW.stop) return; + int cold = median > LW_COLD_MS || !strcmp(mode, "force"); + if (!resume && !cold) { + if (same && strikes) lw_stamp("done", 0); // the driver kept them this time + rt_log("vk_pipeline: library warm: the driver's cache has the prepared libraries (a sample's median %.1f ms)", + median); + return; + } + if (!resume && same && strikes >= 2 && strcmp(mode, "force")) { + rt_log("vk_pipeline: library warm: the prepared libraries are cold again (a sample's median %.0f ms) after two " + "finished warms on this driver: it does not keep them, so pipelines compile when a draw first needs " + "them (NX2NAVI_LIB_WARM=force warms anyway)", median); + return; + } + LW.strikes = resume ? strikes : same && !strcmp(was, "done") ? strikes + 1 : 0; + lw_stamp("warming", LW.strikes); + int cpus = (int)plat_cpu_usable(), t = cpus / 4; + t = t < 2 ? 2 : t > LW_MAX_THREADS ? LW_MAX_THREADS : t; + if (resume) + rt_log("vk_pipeline: library warm: going on with the warm an earlier run began: %zu prepared variants on %d " + "idle threads", LW.n, t); + else + rt_log("vk_pipeline: library warm: the driver's cache does not have the prepared libraries (a sample's median " + "%.0f ms; a driver update or a change of what they are built with): building %zu prepared variants " + "into it on %d idle threads while the game runs. Until it is done, a pipeline a draw first needs may " + "compile, which skips that draw for some frames or makes it wait", median, LW.n, t); + LW.t0 = now_ms(); + LW.running = t; + int started = 0; + for (int i = 0; i < t; i++) + if ((LW.th[1 + started] = plat_thread_new(lw_worker, NULL, "lib warm"))) started++; + LW.threads = started; + if (plat_add32(&LW.running, started - t) == 0 && !started) rt_log("vk_pipeline: library warm: no thread started"); +} + +// Boot, once the library state is known (lb_state): the prepared list is read and the check starts on its own thread. +static void lw_start(void) { + const char *mode = cfg_value("NX2NAVI_LIB_WARM"); + if (!P.gpl || !strcmp(mode, "0") || plat_xchg32(&LW.started, 1)) return; + char dir[PLAT_PATH_MAX * 2], path[PLAT_PATH_MAX * 2 + 32]; + if (!cache_dir(dir, sizeof dir)) return; + snprintf(path, sizeof path, "%s/shader-prepared.bin", dir); + snprintf(LW.stamp, sizeof LW.stamp, "%s/shader-warm.txt", dir); + uint64_t size = 0; + uint8_t *f = plat_file_read(path, &size); + size_t n = f && size >= 16 ? (size_t)(size - 16) / 36 : 0; + LW.e = n ? calloc(n, sizeof *LW.e) : NULL; + for (size_t i = 0; LW.e && i < n; i++) + memcpy(LW.e[i].iid, f + 16 + 36 * i + 4, 32), LW.e[i].stage = f[16 + 36 * i], LW.e[i].prio = f[16 + 36 * i + 1]; + free(f); + if (!LW.e) return; // nothing prepared (a build without the preparation step) + LW.n = n; + VkPhysicalDeviceProperties pp; + vkGetPhysicalDeviceProperties(vk_ctx()->phys, &pp); + snprintf(LW.id, sizeof LW.id, "%04x:%04x:%08x", pp.vendorID, pp.deviceID, pp.driverVersion); + LW.th[0] = plat_thread_new(lw_main, NULL, "lib warm check"); +} + +static void lw_stop(void) { + plat_xchg32(&LW.stop, 1); + for (int i = 0; i <= LW_MAX_THREADS; i++) + if (LW.th[i]) plat_thread_join(LW.th[i]), LW.th[i] = NULL; +} + // Boot: compute shaders of the warm list (recorded and prepared), then the first KEEP recorded graphics variants. static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) { const char *e = cfg_value("NX2NAVI_LIB_PREBUILD"), *ke = cfg_get("NX2NAVI_LIB_PREBUILD_KEEP"); const char *te = cfg_get("NX2NAVI_LIB_PREBUILD_THREADS"); // no fixed default: 2 when unset (1 under 4 CPUs) const char *be = cfg_get("NX2NAVI_LIB_PREBUILD_BIG"); // SPIR-V words; LB_BIG_WORDS when unset, 0 no limit if (!A.on || !cfg_bool("NX2NAVI_LIB_PREBUILD")) return; + lb_state(pr_hist, fs_hist); // the libraries' state, also for lw_start + lw_start(); LB.big_words = be && *be ? strtoull(be, NULL, 10) : LB_BIG_WORDS; char dir[PLAT_PATH_MAX * 2]; if (cache_dir(dir, sizeof dir)) skip_init(dir); @@ -4773,7 +4950,6 @@ static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) { return; } LB.n = n; - lb_state(pr_hist, fs_hist); LB.idle = 1; int cpus = (int)plat_cpu_usable(); // Two threads by default, not half the CPUs. On a 32-thread CPU with NVIDIA's driver, 16 builders kept the driver's @@ -4802,6 +4978,7 @@ static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) { } static void lb_stop(void) { + lw_stop(); plat_xchg32(&LB.stop, 1); for (int i = 0; i < LB.threads; i++) if (LB.th[i]) plat_thread_join(LB.th[i]), LB.th[i] = NULL; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 5812141f..15c65edf 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1058,8 +1058,27 @@ int vk_texture_commit_low(void) { return __atomic_load_n(&commit_low, __ATOMIC_R // content exists only in its image waits GPU_STALE_FRAMES, or GAP_TIMES the longest it stayed released before the game // took it back when that is longer. An overwritten entry has no content left to wait for (overwrite_others): it goes // with the intact ones, whether or not a descriptor still names it. -#define STALE_FRAMES 120 -#define GPU_STALE_FRAMES 1800 +// +// The waits here and DISUSE_FRAMES below are counts of presents, chosen at 60 presents a second: two seconds, thirty, +// two minutes. The same count is less time at a higher frame rate: at 165 presents a second 1800 of them are 11 s, so +// a render target the game came back to after 12 s was gone and a texture out of view for 11 s was retired. Each +// count is therefore taken times the present rate over 60, measured over the last two seconds and never under 1 +// (presents_for): the waits are the same time at any frame rate, and no shorter where frames come slower than 60. +static uint64_t presents_for(uint64_t at_60) { // tex_lock held + static uint64_t t0, f0; + static double times = 1; + uint64_t now = plat_now_ns(), f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (!t0) { + t0 = now, f0 = f; + } else if (now - t0 >= 2000000000ull) { + double rate = (double)(f - f0) * 1e9 / (double)(now - t0) / 60.0; + times = rate > 1 ? rate : 1; + t0 = now, f0 = f; + } + return (uint64_t)((double)at_60 * times); +} +#define STALE_FRAMES presents_for(120) +#define GPU_STALE_FRAMES presents_for(1800) #define GAP_TIMES 8 static int idle(const Tex *e, uint64_t now) { return look_round - e->use_round >= IDLE_ROUNDS && now - e->use_frame >= STALE_FRAMES; @@ -1074,8 +1093,9 @@ static uint64_t stale_after(const Tex *e) { static void drop_stale(uint64_t keep, uint64_t *count) { uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); int dropped = 0; + const uint64_t stale = STALE_FRAMES; for (int any = 0; any < 2; any++) - for (Tex *e = rel_head, *next; e && now - e->rel_frame >= STALE_FRAMES; e = next) { // in release order + for (Tex *e = rel_head, *next; e && now - e->rel_frame >= stale; e = next) { // in release order if (dropped == PRESSURE_STEP || rel_bytes <= keep) return; next = e->rel_next; if (!e->bytes || (!any && !comes_back_intact(e) && !e->overwritten)) continue; @@ -1171,7 +1191,12 @@ static uint64_t evict_idle(uint64_t want, uint64_t used, uint64_t budget) { // // marks missed. NX2NAVI_TEX_DISUSE=2 retires and never destroys (stand-ins only, nothing freed): it tells the marks and // stand-ins from the freeing as the cause of a fault. Only while every module that indexes the pool was marked and two // reads arrived: an unmarked shader could sample what the marks call unused. -#define DISUSE_FRAMES 1800 +// Two minutes, not the thirty seconds it was: a texture out of view that long and then seen again shows its stand-in +// for the frames until its mark is read, which is a flash of missing ground or a dark object each time the camera +// turns back, and a batch of such textures gave back 0.4 to 31 MiB in a recorded session. The textures of an area +// left behind, which are what the memory grows by, still go two minutes after it. +#define DISUSE_SECONDS 120 +#define DISUSE_FRAMES presents_for(DISUSE_SECONDS * 60) #define HOLDS_MAX 65536 static nvn_hmap holds = {.klen = sizeof(TexKey)}; // value: 1 static uint32_t marks_reads; @@ -1240,10 +1265,10 @@ static uint64_t evict_disused(uint64_t want) { want -= retiring_bytes; Tex *v[EVICT_STEP]; int nv = 0; - uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), disuse = DISUSE_FRAMES; for (uint32_t i = 0; i < by_key.cap; i++) { // the EVICT_STEP least recently sampled candidates, oldest first Tex *e = by_key.vals[i]; - if (!e || e->released || e->retiring || !e->bytes || !comes_back_intact(e) || now - e->samp_frame < DISUSE_FRAMES) + if (!e || e->released || e->retiring || !e->bytes || !comes_back_intact(e) || now - e->samp_frame < disuse) continue; int at = nv; while (at > 0 && v[at - 1]->samp_frame > e->samp_frame) at--; @@ -1289,7 +1314,7 @@ static uint64_t evict_disused(uint64_t want) { disuse_logs++; rt_log("vk: %d textures no shader sampled for %d s or more evicted (%.1f MiB), least recently sampled first; their " "images go once the texture pool sets no longer name them, and one sampled again is made again within a " - "few frames", n, DISUSE_FRAMES / 60, freed / 1048576.0); + "few frames", n, DISUSE_SECONDS, freed / 1048576.0); } return retiring_bytes; } diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 9db77f43..db895a39 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1425,6 +1425,36 @@ class MakeGameTest(Tmp): self.assertIn('stay in use', buf.getvalue()) self.assertIn('shader cache packed', buf.getvalue()) + def test_carried_update_warms_the_drivers_cache(self): + """An update that carries the shaders over runs the game's --warm-shaders, in the game folder; a run that + fails is said and does not stop the build, and without a built game or a preparation nothing runs.""" + calls = [] + for name, value in (('shader_carry', lambda out: True), + ('shader_run', lambda cmd, cwd=None: calls.append((cmd, cwd)) or self.rc)): + self.addCleanup(setattr, self.mg, name, getattr(self.mg, name)) + setattr(self.mg, name, value) + self.rc = 0 + self.out.mkdir(parents=True, exist_ok=True) + with contextlib.redirect_stdout(io.StringIO()): + self.mg.step_shaders(self.out, update=True) + self.assertEqual(calls, []) # no exe, no prepared list + (self.out / self.mg.EXE_NAME).write_bytes(b'MZ') + with contextlib.redirect_stdout(io.StringIO()): + self.mg.step_shaders(self.out, update=True) + self.assertEqual(calls, []) # never prepared: nothing to build again + (self.out / 'shader-prepared.bin').write_bytes(bytes(16)) + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + self.mg.step_shaders(self.out, update=True) + self.assertEqual(calls, [([str(self.out / self.mg.EXE_NAME), '--warm-shaders'], str(self.out))]) + self.assertIn("driver's cache warm", buf.getvalue()) + self.rc = 3 + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + self.mg.step_shaders(self.out, update=True) # no exception: the game warms it itself + self.assertIn('did not finish (3)', buf.getvalue()) + self.assertEqual(len(calls), 2) + def test_romfs_failure_stops_the_build(self): ran, lifting, failed = [], threading.Event(), threading.Event() diff --git a/tools/make_game.py b/tools/make_game.py index a607c595..eb7e4cd9 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -1372,30 +1372,12 @@ def shader_carry(out): return (out / 'shader-prepared.bin').is_file() and bool(cache_identities(out)) -def step_shaders(out, update=False): - """Every shader of the game files into the game's shader cache and the driver's own cache, before the first start. - Its output is read as it comes: the counts nx2navi.exe prints become progress events, the other lines go on to - the log, and while nothing moves an event every few seconds keeps saying the step is running and for how long. - update: after a kit update the shaders prepared before stay in use and the game translates them again in the - background (shader_carry), so the update does not wait the minutes a full preparation takes.""" - # The game translates with the copy beside it. The compile step refreshes that copy, but a kit that changes only - # the translator runs this step again alone (step_key), which would then prepare with the old one. Only a kit that - # has one: the compile step, which always runs first, stops a build without it. - for d in (KIT / 'bin', KIT / 'build' / 'xlate'): - if (d / XLATE_NAME).is_file(): - note_cache_translator(out) - for name in (XLATE_NAME, 'nx2navi_xlate.id'): - copy_if_changed(d / name, out / name) - break - if update and shader_carry(out): - say(' the shaders prepared before this update stay in use; the game translates them again in the background') - if loose_shaders(out) >= PACK_LOOSE_MIN: - pack_shader_cache(out) - return - cmd = [sys.executable, '-u', str(KIT / 'tools' / 'prepare_shaders.py'), str(out), '--romfs', str(out / 'romfs'), - '--exefs', str(out / 'exefs'), '--no-list', '--exe', EXE_NAME] - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding='utf-8', - errors='replace', bufsize=1) +def shader_run(cmd, cwd=None): + """Runs a shader preparation command and returns its exit code. Its output is read as it comes: the counts + nx2navi.exe prints become progress events, the other lines go on to the log, and while nothing moves an event + every few seconds keeps saying the step is running and for how long.""" + proc = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, + encoding='utf-8', errors='replace', bufsize=1) t0 = time.monotonic() state = {'phase': None, 'done': 0, 'total': 0, 'moved': t0} lock = threading.Lock() @@ -1435,6 +1417,56 @@ def step_shaders(out, update=False): proc.stdout.close() stop.set() beat.join() + return rc + + +def warm_driver_cache(out): + """After an update that carries the shaders over: every prepared stage library is built into the graphics driver's + cache again (`nx2navi.exe --warm-shaders`). It takes seconds while the driver still has them. It takes minutes when + the update changed what a library is built with, which gives each one another entry in the driver's cache: without + this run every pipeline a draw first needed then compiled in play, which skipped that draw for some frames or made + it wait (on an RX 7900 XT 100 to 340 ms a library against 3 ms from the cache, after dynamic vertex input was + turned off for that driver). A run that fails is not an error of the build: the game checks the cache at every + start and warms it in the background itself (runtime/vk_pipeline.c lw_start).""" + exe = out / EXE_NAME + if not exe.is_file() or not (out / 'shader-prepared.bin').is_file(): + return + say(" the prepared shaders are built into the graphics driver's cache again") + t0 = time.monotonic() + try: + rc = shader_run([str(exe), '--warm-shaders'], cwd=str(out)) + except OSError as e: + rc = str(e) + if rc: + say(f' that did not finish ({rc}); the game does it in the background while it runs') + else: + say(f" driver's cache warm in {time.monotonic() - t0:.0f} s") + + +def step_shaders(out, update=False): + """Every shader of the game files into the game's shader cache and the driver's own cache, before the first start + (shader_run reports its progress). + update: after a kit update the shaders prepared before stay in use and the game translates them again in the + background (shader_carry), so the update does not wait the minutes a full preparation takes; the driver's cache + gets their stage libraries again (warm_driver_cache).""" + # The game translates with the copy beside it. The compile step refreshes that copy, but a kit that changes only + # the translator runs this step again alone (step_key), which would then prepare with the old one. Only a kit that + # has one: the compile step, which always runs first, stops a build without it. + for d in (KIT / 'bin', KIT / 'build' / 'xlate'): + if (d / XLATE_NAME).is_file(): + note_cache_translator(out) + for name in (XLATE_NAME, 'nx2navi_xlate.id'): + copy_if_changed(d / name, out / name) + break + if update and shader_carry(out): + say(' the shaders prepared before this update stay in use; the game translates them again in the background') + if loose_shaders(out) >= PACK_LOOSE_MIN: + pack_shader_cache(out) + warm_driver_cache(out) + return + cmd = [sys.executable, '-u', str(KIT / 'tools' / 'prepare_shaders.py'), str(out), '--romfs', str(out / 'romfs'), + '--exefs', str(out / 'exefs'), '--no-list', '--exe', EXE_NAME] + rc = shader_run(cmd) if rc: fail('preparing the shaders failed (the lines above say why); the game still runs, compiling shaders as it ' 'meets them. Run the build again to retry this step') From 5f4bbd208477d2e8e2d016e842ee5c32fc0ffc26 Mon Sep 17 00:00:00 2001 From: epicmeme546 <178368684+epicmeme546@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:02:45 -0500 Subject: [PATCH 07/13] Play the launcher's sounds at Low by default. A new launcher.json starts with sounds at low instead of off, and a value the file cannot hold falls back to low too (launcher/ui/sound.py DEFAULT_LEVEL). The player can still pick Off, Medium or High in Customize Launcher. The sounds are still only the game's select and back sounds, read from the player's own game. Co-Authored-By: Claude Opus 5.5 --- docs/launcher.md | 4 ++-- docs/releases/v0.6.2.md | 2 +- launcher/settings.py | 2 +- launcher/ui/app.py | 7 ++++--- launcher/ui/sound.py | 17 +++++++++-------- tests/launcher_test.py | 27 +++++++++++++++------------ 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/docs/launcher.md b/docs/launcher.md index 33c04bd1..fc616819 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -47,7 +47,7 @@ Customize Launcher (on the home screen) makes the look the player's own: - Animations (on by default): the cursor glides from row to row, a new screen slides into place, sparks drift up over the backdrop and Navi bobs in the title band with a pulsing halo (`App.animate`). They move by the time that passed, stand still while the window is hidden or minimised, and Off stops them all. -- Sounds (off by default; Low, Medium or High): two of the game's own interface sounds, the decide and cancel sounds +- Sounds (Low by default; Off, Medium or High): two of the game's own interface sounds, the decide and cancel sounds of its menus, when a row is chosen or a choice changes and when the player goes back. The launcher decodes them from the player's built game or dump into `data/cache/sounds` (`gamefiles.extract_sounds`: `Sound/Resource/Stream/SE_Sys_Do00.bwav` and `SE_Sys_Cancel00.bwav`, the files `runtime/menu_sound.c` plays in the @@ -377,7 +377,7 @@ pruning, unsafe archives, the background update (a release staged beside the run `staged.json` marker, a staged release installed after a restart, no install while the game runs, the install once it exited, nothing by itself with Install Updates Automatically off, a failed staging tried again after the next check), the font decoding on a system font wrapped like the game's, the look (every colour scheme complete, the -background picture cut to the window's shape and kept, the sound effects as WAVs and off by default), `make_game.py` +background picture cut to the window's shape and kept, the sound effects as WAVs and low by default), `make_game.py` on a synthetic `.nup` (events, skipped steps, `--rebuild` without the dump, step keys, the shaders key following the translator the build uses), the RomFS step beside the lift and compile diff --git a/docs/releases/v0.6.2.md b/docs/releases/v0.6.2.md index 153ca7ef..b5f201a9 100644 --- a/docs/releases/v0.6.2.md +++ b/docs/releases/v0.6.2.md @@ -9,7 +9,7 @@ Draft notes for the release after 0.6.1. The version number is not decided yet. it takes effect at the next start. - Customize Launcher (on the launcher's home screen): six colour schemes, a background picture of your own with adjustable dimming, animations (a gliding cursor, sliding screens, drifting sparks) and sound effects: the game's - own select and back sounds, read from your game. The sounds are off until you turn them on. + own select and back sounds, read from your game, quiet (Low) unless you pick another loudness or turn them off. - Three more settings take effect without a restart: HDR between off and the tone mapper (only the game's native HDR still waits for one), the camera's stick sensitivity, and the speakers (stereo or 5.1). diff --git a/launcher/settings.py b/launcher/settings.py index 0418892d..c654e21b 100644 --- a/launcher/settings.py +++ b/launcher/settings.py @@ -19,7 +19,7 @@ DEFAULTS = { 'background': '', # the picture the player chose as the background; its fitted copy is data/background.npy 'background_dim': 40, # how much that picture is darkened, in percent 'animations': True, # the cursor glides, screens slide in, sparks drift, Navi bobs - 'sounds': 'off', # the chimes: off (until the player turns them on), low, medium or high + 'sounds': 'low', # the game's select and back sounds: off, low (sound.py DEFAULT_LEVEL), medium, high 'feed_repo': '', # owner/name overriding feed.json, for testing a fork's releases 'github_token': '', # only for a private release repository; stored as plain text 'last_check': 0.0, diff --git a/launcher/ui/app.py b/launcher/ui/app.py index a1ecb8ff..6b73ce8e 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -918,7 +918,7 @@ class App: def sounds(d): keys = [k for k, _ in SOUND_LEVELS] now = self.settings.get('sounds') - at = keys.index(now) if now in keys else 0 + at = keys.index(now if now in keys else sound.DEFAULT_LEVEL) self.settings['sounds'] = keys[max(0, min(len(keys) - 1, at + d))] self.settings.save() self.sounds.set_level(self.settings['sounds']) @@ -946,9 +946,10 @@ class App: Row('divider'), Row('toggle', 'Animations', on=animations, get=lambda: bool(self.settings.get('animations')), desc=('A gliding cursor, sliding screens and drifting sparks.',)), - Row('enum', 'Sounds', get=lambda: dict(SOUND_LEVELS).get(self.settings.get('sounds'), 'Off'), + Row('enum', 'Sounds', get=lambda: dict(SOUND_LEVELS).get(self.settings.get('sounds'), + dict(SOUND_LEVELS)[sound.DEFAULT_LEVEL]), left=lambda: sounds(-1), right=lambda: sounds(1), - desc=('The game\'s select and back sounds. Off by default.',)), + desc=('The game\'s select and back sounds. Low by default.',)), Row('divider'), Row('action', 'Back', on=lambda: self.show('home', self.home_row('Customize Launcher')), height=40, desc=('Back to the main menu.',)), diff --git a/launcher/ui/sound.py b/launcher/ui/sound.py index f49b7d52..025fff88 100644 --- a/launcher/ui/sound.py +++ b/launcher/ui/sound.py @@ -3,11 +3,11 @@ going back, read from the player's game (launcher/gamefiles.py extract_sounds de runtime/menu_sound.c plays them in the in-game menu). No sound ships with the launcher and none is made here: until the player's game has been read, and for a sound it does not hold, the launcher is silent. -They are off until the player picks a loudness in Customize Launcher (the Sounds row). An effect is the game's sound -as a 16-bit WAV in memory, scaled to the loudness. Windows plays it with PlaySound from that memory; elsewhere the WAV -goes to a file in the cache folder and the first of pw-play, paplay and aplay found plays it. One effect sounds at a -time: a new one cuts the last. Moving the cursor makes no sound, as it would cut a choice's sound short every time -the pointer lands on a row of the next screen. +They play at Low until the player picks another loudness, or Off, in Customize Launcher (the Sounds row). An effect +is the game's sound as a 16-bit WAV in memory, scaled to the loudness. Windows plays it with PlaySound from that +memory; elsewhere the WAV goes to a file in the cache folder and the first of pw-play, paplay and aplay found plays +it. One effect sounds at a time: a new one cuts the last. Moving the cursor makes no sound, as it would cut a +choice's sound short every time the pointer lands on a row of the next screen. select a row was chosen, a switch or a choice changed, Play, the end of a build the game's decide sound back back to the screen before, something could not be done the game's cancel sound @@ -21,6 +21,7 @@ import subprocess import numpy as np LEVELS = {'off': 0.0, 'low': 0.18, 'medium': 0.4, 'high': 0.75} # the Sounds choice: the loudest sample's share of full scale +DEFAULT_LEVEL = 'low' # a new launcher.json's (launcher/settings.py DEFAULTS), also for a value it cannot hold EFFECTS = ('select', 'back') # the names play takes: the game's sounds of gamefiles.SOUNDS @@ -50,10 +51,10 @@ class Sounds: """Plays the effects at the level of the Sounds choice. Every failure is silent: a PC without a sound device, or without a player program, just has a quiet launcher.""" - def __init__(self, cache_dir, level='off', log=None): + def __init__(self, cache_dir, level=DEFAULT_LEVEL, log=None): self.cache = cache_dir self.log = log or (lambda line: None) - self.level = None # set_level below: 'off' unless the player chose a loudness + self.level = None # set_level below: DEFAULT_LEVEL unless the player chose another self.game = {} # the game's sounds decoded from the player's files: name -> (rate, channels, samples) self.wavs = {} # name -> WAV bytes at the current level (Windows plays from this memory: it must stay) self.files = {} # name -> the WAV file elsewhere @@ -88,7 +89,7 @@ class Sounds: return bool(game) def set_level(self, level): - level = level if level in LEVELS else 'off' + level = level if level in LEVELS else DEFAULT_LEVEL if level == self.level: return self.stop() diff --git a/tests/launcher_test.py b/tests/launcher_test.py index db895a39..6378f9ef 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -18,7 +18,7 @@ Run: python tests/launcher_test.py (Python 3.14 for the zstd tests; the window a source checkout's build/xlate/), the prompts step with a stand-in prompt tool, the object cache of the compile step with the system's C compiler, and its windres call under folders with spaces (a script in windres's place) - jobs: an install driven by a fake make_game.py, with and without the prompts row -- the look: the colour schemes, the background picture's fitting and kept copy, the sound effects (off by default, +- the look: the colour schemes, the background picture's fitting and kept copy, the sound effects (low by default, only the game's own select and back sounds, decoded from a made-up .bwav) - the window: --selftest draws every screen (skipped without a display) """ @@ -2599,9 +2599,10 @@ class LookTest(Tmp): self.assertAlmostEqual(peak / 32767, sound.LEVELS['high'], delta=0.01) self.assertFalse(sound.Sounds(self.tmp / 'empty', 'high').game) - def test_only_the_games_sounds_play_and_only_once_turned_on(self): + def test_only_the_games_sounds_play_at_low_unless_set(self): sound = self.sound - self.assertEqual(settings.DEFAULTS['sounds'], 'off') + self.assertEqual(settings.DEFAULTS['sounds'], 'low') + self.assertEqual(sound.DEFAULT_LEVEL, 'low') self.assertEqual(set(gamefiles.SOUNDS), set(sound.EFFECTS)) empty = self.tmp / 'empty' self.game_sounds() @@ -2616,27 +2617,29 @@ class LookTest(Tmp): self.assertTrue(quiet.load_game()) # the game's sounds, once decoded quiet.play('select') self.assertEqual(popen.call_count, 1) - s = sound.Sounds(self.layout.cache) # no level given, as a new launcher.json has it + s = sound.Sounds(self.layout.cache) # no level given, as a new launcher.json has it: low + self.assertEqual(s.level, 'low') s.play('select') - s.set_level('nonsense') # a level launcher.json cannot hold: off + self.assertEqual(popen.call_count, 2) + s.set_level('off') # the player turned them off s.play('select') - self.assertEqual(popen.call_count, 1) - s.set_level('low') + self.assertEqual(popen.call_count, 2) + s.set_level('nonsense') # a level launcher.json cannot hold: the default + self.assertEqual(s.level, 'low') s.play('back') s.play('move') # the cursor has no sound s.play('no such effect') - self.assertEqual(popen.call_count, 2) + self.assertEqual(popen.call_count, 3) with open(popen.call_args[0][0][1], 'rb') as f: self.assertEqual(f.read(4), b'RIFF') - s.set_level('off') - s.play('select') - self.assertEqual(popen.call_count, 2) + peak = max(abs(v) for v in struct.unpack_from('<8h', s.wav('back'), 44)) + self.assertAlmostEqual(peak / 32767, sound.LEVELS['low'], delta=0.01) def test_the_look_settings_survive_and_fall_back(self): path = self.tmp / 'launcher.json' s = settings.Settings(path) self.assertEqual((s['theme'], s['background'], s['background_dim'], s['animations'], s['sounds']), - ('kokiri', '', 40, True, 'off')) + ('kokiri', '', 40, True, 'low')) s['theme'], s['sounds'], s['background_dim'] = 'shadow', 'high', 60 s.save() t = settings.Settings(path) From ddc4d928128f20a2ec945d78f581971e369fca01 Mon Sep 17 00:00:00 2001 From: epicmeme546 <178368684+epicmeme546@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:05:21 -0500 Subject: [PATCH 08/13] Give the launcher's colour schemes plain names. The six schemes were named after places in the game. They are now Forest, Volcano, Lake, Desert, Temple and Shadow, in the same order and colours, and their ids in launcher.json follow (forest is the default). A launcher.json that names one of the old ids gets the default scheme, as with any name it does not know. Co-Authored-By: Claude Opus 5.5 --- docs/launcher.md | 8 ++++---- launcher/settings.py | 2 +- launcher/ui/art.py | 16 ++++++++-------- tests/launcher_test.py | 14 +++++++------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/launcher.md b/docs/launcher.md index fc616819..af1f76c9 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -35,10 +35,10 @@ drawn in code (`launcher/ui/art.py`); no texture, font or sound of the game is i Customize Launcher (on the home screen) makes the look the player's own: -- Color Scheme: Kokiri Forest (the colours above), Death Mountain, Zora's Domain, Gerudo Valley, Temple of Time and - Shadow Temple. A scheme is the first one with its woods, accents, backdrop and glow turned to other hues - (`art.THEMES`, `art.recolor`), so each keeps the same light and dark steps and the same readable text. The window is - drawn again in the new colours at once. +- Color Scheme: Forest (the colours above), Volcano, Lake, Desert, Temple and Shadow. A scheme is the first one with + its woods, accents, backdrop and glow turned to other hues (`art.THEMES`, `art.recolor`), so each keeps the same + light and dark steps and the same readable text. The window is drawn again in the new colours at once. The names + are plain words, not the game's. - Background: a picture of the player's own behind the panels instead of the scheme's backdrop (`launcher/ui/picture.py`: PNG, JPEG, WebP, BMP, GIF or TIFF, read with Pillow). It is cut to the window's shape around its middle and kept as `data/background.npy`, so the launcher needs the file no more; Background Dimming darkens it (0 to 80 %) for the diff --git a/launcher/settings.py b/launcher/settings.py index c654e21b..7842ea49 100644 --- a/launcher/settings.py +++ b/launcher/settings.py @@ -15,7 +15,7 @@ DEFAULTS = { 'desktop_shortcut': True, 'save_export_dir': '', # where Saves > Export Save writes its zip; empty: the Downloads folder 'export_with_log': True, # Export Save adds the game's latest log and nx2navi.ini (for a bug report) - 'theme': 'kokiri', # the colour scheme (launcher/ui/art.py THEMES) + 'theme': 'forest', # the colour scheme (launcher/ui/art.py THEMES) 'background': '', # the picture the player chose as the background; its fitted copy is data/background.npy 'background_dim': 40, # how much that picture is darkened, in percent 'animations': True, # the cursor glides, screens slide in, sparks drift, Navi bobs diff --git a/launcher/ui/art.py b/launcher/ui/art.py index f1385bf5..36cb336c 100644 --- a/launcher/ui/art.py +++ b/launcher/ui/art.py @@ -19,7 +19,7 @@ import numpy as np # ---------------------------------------------------------------- colours -# The first colour scheme, Kokiri Forest: bronze wood on a misty dark green. +# The first colour scheme, Forest: bronze wood on a misty dark green. BASE = { 'window': '#06180d', 'panel': '#3a2b22', @@ -64,19 +64,19 @@ GROUPS = { # None to keep each colour's own, saturation factor, lightness factor, saturation added), applied to the first # scheme's colours (recolor), so a scheme keeps the first one's light and dark steps. 'set': colours given as they are. THEMES = { - 'kokiri': {'label': 'Kokiri Forest'}, - 'death_mountain': {'label': 'Death Mountain', 'wood': (14, 0.95, 0.80, 0), 'accent': (16, 1.05, 1.0, 0), + 'forest': {'label': 'Forest'}, + 'volcano': {'label': 'Volcano', 'wood': (14, 0.95, 0.80, 0), 'accent': (16, 1.05, 1.0, 0), 'scene': (6, 1.0, 1.0, 0.25), 'glow': (30, 1.0, 1.0, 0), 'text': (30, 1.0, 1.0, 0)}, - 'zora': {'label': "Zora's Domain", 'wood': (207, 0.75, 0.92, 0), 'accent': (188, 0.95, 1.0, 0), + 'lake': {'label': 'Lake', 'wood': (207, 0.75, 0.92, 0), 'accent': (188, 0.95, 1.0, 0), 'scene': (200, 1.0, 1.05, 0.2), 'glow': (185, 1.0, 1.0, 0), 'text': (200, 0.55, 1.02, 0)}, - 'gerudo': {'label': 'Gerudo Valley', 'wood': (26, 1.15, 1.0, 0), 'accent': (352, 0.9, 1.0, 0), + 'desert': {'label': 'Desert', 'wood': (26, 1.15, 1.0, 0), 'accent': (352, 0.9, 1.0, 0), 'scene': (318, 0.8, 0.95, 0.15), 'glow': (45, 1.0, 1.0, 0), 'text': (36, 1.0, 1.0, 0)}, - 'temple': {'label': 'Temple of Time', 'wood': (220, 0.22, 0.95, 0), 'accent': (46, 0.95, 1.02, 0), + 'temple': {'label': 'Temple', 'wood': (220, 0.22, 0.95, 0), 'accent': (46, 0.95, 1.02, 0), 'scene': (222, 0.55, 1.0, 0.05), 'glow': (48, 1.0, 1.0, 0.5), 'text': (48, 0.45, 1.02, 0)}, - 'shadow': {'label': 'Shadow Temple', 'wood': (268, 0.55, 0.78, 0), 'accent': (286, 0.75, 1.0, 0), + 'shadow': {'label': 'Shadow', 'wood': (268, 0.55, 0.78, 0), 'accent': (286, 0.75, 1.0, 0), 'scene': (272, 0.9, 0.8, 0.2), 'glow': (280, 1.0, 1.0, 0.2), 'text': (270, 0.5, 1.0, 0)}, } -DEFAULT_THEME = 'kokiri' +DEFAULT_THEME = 'forest' def recolor(hexcolor, hue, sat=1.0, light=1.0, add=0.0): diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 6378f9ef..d733f0ed 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -2447,7 +2447,7 @@ class LookTest(Tmp): def test_every_colour_scheme_has_every_colour(self): art = self.art - self.assertEqual(art.theme_palette('kokiri'), art.BASE) # the first scheme is the colours as written + self.assertEqual(art.theme_palette('forest'), art.BASE) # the first scheme is the colours as written for name, t in art.THEMES.items(): pal = art.theme_palette(name) self.assertEqual(set(pal), set(art.BASE), name) @@ -2462,15 +2462,15 @@ class LookTest(Tmp): art = self.art held = art.PALETTE try: - self.assertEqual(art.set_theme('zora'), 'zora') + self.assertEqual(art.set_theme('lake'), 'lake') self.assertIs(art.PALETTE, held) self.assertNotEqual(held['panel'], art.BASE['panel']) self.assertEqual(held['salmon'], art.BASE['salmon']) # a warning stays a warning in every scheme - self.assertEqual(art.set_theme('no such scheme'), 'kokiri') + self.assertEqual(art.set_theme('no such scheme'), 'forest') self.assertEqual(dict(held), art.BASE) - self.assertEqual(art.set_theme(None), 'kokiri') # launcher.json without the key + self.assertEqual(art.set_theme(None), 'forest') # launcher.json without the key finally: - art.set_theme('kokiri') + art.set_theme('forest') def test_recolor_keeps_light_and_dark(self): import colorsys @@ -2639,14 +2639,14 @@ class LookTest(Tmp): path = self.tmp / 'launcher.json' s = settings.Settings(path) self.assertEqual((s['theme'], s['background'], s['background_dim'], s['animations'], s['sounds']), - ('kokiri', '', 40, True, 'low')) + ('forest', '', 40, True, 'low')) s['theme'], s['sounds'], s['background_dim'] = 'shadow', 'high', 60 s.save() t = settings.Settings(path) self.assertEqual((t['theme'], t['sounds'], t['background_dim']), ('shadow', 'high', 60)) path.write_text(json.dumps({'theme': 5, 'animations': 'yes', 'background_dim': '40'})) u = settings.Settings(path) # values of the wrong kind: the defaults - self.assertEqual((u['theme'], u['animations'], u['background_dim']), ('kokiri', True, 40)) + self.assertEqual((u['theme'], u['animations'], u['background_dim']), ('forest', True, 40)) # ---------------------------------------------------------------- the window From 74d3412736b4041abb60f36b4ff7d8c920a9c864 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 03:30:31 -0500 Subject: [PATCH 09/13] Release the DLSS features that stayed before one is made after a release, so the pause screen no longer comes out striped. A pause of 3,600 frames frees the gameplay feature; the unpause makes it again, and the pause screen's feature, which had stayed, drew alternating bands of rows black or noise at every later pause. A scripted pause, unpause, pause was striped in each of the 8 runs in which the gameplay feature had been freed and in none of the 12 in which it had not; setting the kept feature's sizes at each evaluation, tried first, changed nothing. Features made one after the other with no release in between are fine, so that is now the only way one is made. dlss_idle_free = 0 keeps every feature instead, 2 is the old behaviour. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 4 ++++ runtime/cfg.c | 6 +++--- runtime/vk_dlaa.c | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index f48db1d1..895b51f9 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -320,6 +320,10 @@ # characters, a little more flicker). Any other model by its letter, a to m: on slower RTX cards the older model e is # cheaper. Changes while the game runs take effect at once (DLSS starts a new history). # dlss_preset = default +# DLSS keeps one network per picture size (gameplay, the pause screen). 1: one unused for 3,600 frames is freed, and +# the others are made again with it when it comes back. 0: none is ever freed (about 0.5 GB more video memory after +# the first pause). 2: freed, the others kept (shows striped pause screens; for tests). +# dlss_idle_free = 1 # Per-object motion vectors for DLSS: characters, animals and moving objects get their own motion instead of the # camera's, so DLSS keeps their history instead of smearing or rejecting it. Costs some CPU time each frame (about 1-2 ms # on a laptop CPU); 0 leaves camera-only motion. Read at start. diff --git a/runtime/cfg.c b/runtime/cfg.c index 3edea66f..72f7dca8 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -40,7 +40,7 @@ static const char *const known[] = { "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", - "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", + "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLSS_IDLE_FREE", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CRASH_FILTER_GUARD", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", @@ -85,7 +85,7 @@ static const struct { const char *key, *section; } sections[] = { {"HDR_HIGHLIGHTS", "display"}, {"HDR_SHADOWS", "display"}, {"HDR_CONTRAST", "display"}, {"HDR_SATURATION", "display"}, {"HDR_HIGHLIGHT_SAT", "display"}, {"HDR_BLOWOUT", "display"}, {"HDR_FLARE", "display"}, {"HDR_HUE", "display"}, {"HDR_GRADE", "display"}, {"SHADOW_RES", "display"}, - {"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"}, + {"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_IDLE_FREE", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"}, {"FRAMEGEN", "display"}, {"FRAMEGEN_MULTIPLIER", "display"}, {"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"}, {"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"}, @@ -138,7 +138,7 @@ static const CfgMeta metas[] = { {"OUTPUT_RES", "1920x1080", NULL, RESTART}, {"RENDER_SCALE", "game", "game|50|67|75|100|125|150|200", RESTART}, {"UPSCALER", "fsr", "fsr|bilinear", LIVE}, {"SHARPNESS", "80", "0..100", LIVE}, {"DLAA", "0", "0|1", LIVE}, {"DLSS", "dlaa", "dlaa|quality|balanced|performance|ultra_performance", LIVE}, - {"DLSS_PRESET", "default", "default|k|j", LIVE}, {"TAA", "dlss", "dlss|fsr3", LIVE}, // vk_dlaa.c fsr3 + {"DLSS_PRESET", "default", "default|k|j", LIVE}, {"DLSS_IDLE_FREE", "1", "0|1|2", RESTART}, {"TAA", "dlss", "dlss|fsr3", LIVE}, // vk_dlaa.c fsr3 {"FRAMEGEN", "off", "off|fsr3|dlss", LIVE}, // vk_fg.cpp, vk_dlssg.c {"FRAMEGEN_MULTIPLIER", "2", "2|3|4", LIVE}, // vk_dlssg.c: frames on screen per rendered frame with DLSS {"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"FXAA", "1", "0|1", LIVE}, diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index cb93576f..683cf8c0 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -513,6 +513,26 @@ static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, // The images and views of a size, then its feature. The device must be idle: free_size waits for it, vk_dlaa_release // has. Whatever was not made is skipped, so a half-made size goes the same way. +// A feature was released since the last one was made (destroy_size), and what follows from it (NX2NAVI_DLSS_IDLE_FREE). +// A pause of 3,600 frames frees the gameplay feature (stale_sizes); the unpause makes it again, and the pause screen's +// feature, which had stayed, then drew alternating bands of rows black or noise at every later pause, for as long as +// the game stayed paused: in each of the 8 scripted runs of pause, unpause, pause in which the gameplay feature had +// been freed and in none of the 12 in which it had not, whatever the texture, write-back, HDR and frame generation +// settings. Giving the kept feature its own sizes again at each evaluation changed nothing. Features made one after +// the other with none released in between do not show it (the gameplay feature, then the pause screen's, at every +// first pause). So a feature is only ever made into that state: with a release since the last creation, the features +// that stayed are released first (release_kept) and made again when their size is next evaluated. +// 1 (default) idle features are freed, and the kept ones are released before the next creation +// 0 idle features are never freed +// 2 idle features are freed and the kept ones stay (as before; shows the fault) +static int feat_released; + +static int idle_free(void) { + static int m = -1; + if (m < 0) m = rt_knob("DLSS_IDLE_FREE", "1"); + return m; +} + static void destroy_size(struct Size *e) { const VkCtx *c = vk_ctx(); if (e->out_view) vkDestroyImageView(c->device, e->out_view, NULL); @@ -527,7 +547,7 @@ static void destroy_size(struct Size *e) { if (e->dist_view[i]) vkDestroyImageView(c->device, e->dist_view[i], NULL); if (e->dist[i]) vmaDestroyImage(c->vma, e->dist[i], e->dist_mem[i]); } - if (e->feat && N.release) N.release(e->feat); + if (e->feat && N.release) N.release(e->feat), feat_released = 1; memset(e, 0, sizeof *e); } @@ -1412,6 +1432,7 @@ static void query_vram(void) { // Frees the sizes other than cur that no evaluation used for SIZE_STALE evaluations: the GPU finished with them long // ago (the CPU is never that many presents ahead), so their images and feature go at once. static void stale_sizes(const struct Size *cur) { + if (!idle_free()) return; for (int i = 0; i < DLSS_SIZES; i++) { struct Size *s = &D.sizes[i]; if (s == cur || !s->out || D.evals < s->used + SIZE_STALE) continue; @@ -1422,6 +1443,20 @@ static void stale_sizes(const struct Size *cur) { } } +// Before a feature is made after a release (feat_released): the features of the other sizes go too, their images stay. +// The GPU may still be running an evaluation of theirs, so it finishes first, as in free_size. +static void release_kept(const struct Size *making) { + int n = 0; + for (int i = 0; i < DLSS_SIZES; i++) { + struct Size *s = &D.sizes[i]; + if (s == making || !s->feat) continue; + if (!n++) vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock(); + N.release(s->feat), s->feat = NULL; + if (D.last == s) D.last = NULL; + } + if (n) rt_log("dlaa: %d kept DLSS feature(s) released before a new one is made after a release; made again when used", n); +} + static void evaluate(VkExec *ex, const VkExecTarget *dst) { const VkExecTarget *t = &D.color, *z = &D.depth; if (!dst) dst = t; @@ -1481,6 +1516,8 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { goto evaluated; } if (!e->feat) { + if (feat_released && idle_free() == 1) release_kept(e); + feat_released = 0; p_setu(p, "CreationNodeMask", 1); p_setu(p, "VisibilityNodeMask", 1); p_setu(p, "Width", w); From 25ca858215bea7d909130fdcf2f0b6a9d73597af Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 03:11:28 -0500 Subject: [PATCH 10/13] Read dlss_idle_free as a number, so its value 2 is reachable, and put its description above the function's own. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_dlaa.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 683cf8c0..b78f8cec 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -511,8 +511,6 @@ static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, return vkCreateImageView(c->device, &vi, NULL, view) == VK_SUCCESS; } -// The images and views of a size, then its feature. The device must be idle: free_size waits for it, vk_dlaa_release -// has. Whatever was not made is skipped, so a half-made size goes the same way. // A feature was released since the last one was made (destroy_size), and what follows from it (NX2NAVI_DLSS_IDLE_FREE). // A pause of 3,600 frames frees the gameplay feature (stale_sizes); the unpause makes it again, and the pause screen's // feature, which had stayed, then drew alternating bands of rows black or noise at every later pause, for as long as @@ -521,7 +519,8 @@ static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, // settings. Giving the kept feature its own sizes again at each evaluation changed nothing. Features made one after // the other with none released in between do not show it (the gameplay feature, then the pause screen's, at every // first pause). So a feature is only ever made into that state: with a release since the last creation, the features -// that stayed are released first (release_kept) and made again when their size is next evaluated. +// that stayed are released first (release_kept) and made again when their size is next evaluated. With that rule none +// of 5 runs in which the gameplay feature had been freed was striped. // 1 (default) idle features are freed, and the kept ones are released before the next creation // 0 idle features are never freed // 2 idle features are freed and the kept ones stay (as before; shows the fault) @@ -529,10 +528,12 @@ static int feat_released; static int idle_free(void) { static int m = -1; - if (m < 0) m = rt_knob("DLSS_IDLE_FREE", "1"); + if (m < 0) m = cfg_int("NX2NAVI_DLSS_IDLE_FREE"); return m; } +// The images and views of a size, then its feature. The device must be idle: free_size waits for it, vk_dlaa_release +// has. Whatever was not made is skipped, so a half-made size goes the same way. static void destroy_size(struct Size *e) { const VkCtx *c = vk_ctx(); if (e->out_view) vkDestroyImageView(c->device, e->out_view, NULL); From d726a4fcded2ea5d57099d8a999f1bb19cf8480e Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 03:30:31 -0500 Subject: [PATCH 11/13] Release the DLSS features with a larger input before a smaller one is made, so a DLSS mode picked in the menu no longer stripes the pause screen. The rule that releases the kept features acted only after a feature had been freed. Picking ultra performance in play made a feature of a third size while the gameplay and pause features stayed, with nothing freed, and the next pause was striped as before. What the two cases share is a feature made while one with a larger input is alive; one made while a smaller one is alive has never shown it. Both halves now apply unless dlss_idle_free is 2, and 0 only stops idle features from being freed. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 6 +++--- runtime/vk_dlaa.c | 42 ++++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 895b51f9..bc9c49e8 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -320,9 +320,9 @@ # characters, a little more flicker). Any other model by its letter, a to m: on slower RTX cards the older model e is # cheaper. Changes while the game runs take effect at once (DLSS starts a new history). # dlss_preset = default -# DLSS keeps one network per picture size (gameplay, the pause screen). 1: one unused for 3,600 frames is freed, and -# the others are made again with it when it comes back. 0: none is ever freed (about 0.5 GB more video memory after -# the first pause). 2: freed, the others kept (shows striped pause screens; for tests). +# DLSS keeps one network per picture size (gameplay, the pause screen). 1: one unused for 3,600 frames is freed. +# 0: none is ever freed (about 0.5 GB more video memory after the first pause). 2: as 1, and the networks that stay +# are not made again when another is made (shows striped pause screens; for tests). # dlss_idle_free = 1 # Per-object motion vectors for DLSS: characters, animals and moving objects get their own motion instead of the # camera's, so DLSS keeps their history instead of smearing or rejecting it. Costs some CPU time each frame (about 1-2 ms diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index b78f8cec..3d02dae6 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -511,19 +511,22 @@ static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, return vkCreateImageView(c->device, &vi, NULL, view) == VK_SUCCESS; } -// A feature was released since the last one was made (destroy_size), and what follows from it (NX2NAVI_DLSS_IDLE_FREE). -// A pause of 3,600 frames frees the gameplay feature (stale_sizes); the unpause makes it again, and the pause screen's -// feature, which had stayed, then drew alternating bands of rows black or noise at every later pause, for as long as -// the game stayed paused: in each of the 8 scripted runs of pause, unpause, pause in which the gameplay feature had -// been freed and in none of the 12 in which it had not, whatever the texture, write-back, HDR and frame generation -// settings. Giving the kept feature its own sizes again at each evaluation changed nothing. Features made one after -// the other with none released in between do not show it (the gameplay feature, then the pause screen's, at every -// first pause). So a feature is only ever made into that state: with a release since the last creation, the features -// that stayed are released first (release_kept) and made again when their size is next evaluated. With that rule none -// of 5 runs in which the gameplay feature had been freed was striped. -// 1 (default) idle features are freed, and the kept ones are released before the next creation -// 0 idle features are never freed -// 2 idle features are freed and the kept ones stay (as before; shows the fault) +// Which features may stay alive when another is made (NX2NAVI_DLSS_IDLE_FREE). A feature made while one with a larger +// input size is alive leaves that larger one drawing alternating bands of rows black or noise from then on, for as long +// as it is used; seen for the pause screen's feature (the picture's size in and out), in two ways: +// - a pause of 3,600 frames frees the gameplay feature (stale_sizes) and the unpause makes it again: every later pause +// was striped, in each of 8 scripted runs of pause, unpause, pause in which it had been freed and in none of 12 in +// which it had not, whatever the texture, write-back, HDR and frame generation settings; +// - a DLSS mode picked in the menu makes a feature of a third size at the unpause, with nothing freed: the next pause +// was striped (performance to ultra performance in play). +// Giving the kept feature its own sizes again at each evaluation changed nothing. A feature made while a smaller one +// is alive leaves the smaller one as it was (the pause screen's after the gameplay one, at every first pause). So when +// a feature is made, the alive ones with a larger input are released first, and after a release since the last +// creation all of them (release_kept); their images stay and each is made again when its size is next evaluated. With +// the second half alone, none of 5 runs in which the gameplay feature had been freed was striped. +// 1 (default) idle features are freed +// 0 idle features are never freed (about 0.5 GB more video memory after the first pause) +// 2 idle features are freed and nothing is released when a feature is made (as before; shows the fault) static int feat_released; static int idle_free(void) { @@ -1444,18 +1447,21 @@ static void stale_sizes(const struct Size *cur) { } } -// Before a feature is made after a release (feat_released): the features of the other sizes go too, their images stay. -// The GPU may still be running an evaluation of theirs, so it finishes first, as in free_size. -static void release_kept(const struct Size *making) { +// Before a feature is made: the features of the other sizes with a larger input go, or all of them (all: a feature was +// released since the last creation, feat_released); their images stay. The GPU may still be running an evaluation of +// theirs, so it finishes first, as in free_size. +static void release_kept(const struct Size *making, int all) { int n = 0; for (int i = 0; i < DLSS_SIZES; i++) { struct Size *s = &D.sizes[i]; if (s == making || !s->feat) continue; + if (!all && (uint64_t)s->w * s->h <= (uint64_t)making->w * making->h) continue; if (!n++) vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock(); N.release(s->feat), s->feat = NULL; if (D.last == s) D.last = NULL; } - if (n) rt_log("dlaa: %d kept DLSS feature(s) released before a new one is made after a release; made again when used", n); + if (n) rt_log("dlaa: %d DLSS feature(s) released before the one for %ux%u is made (%s); made again when used", n, + making->w, making->h, all ? "after a release" : "larger inputs"); } static void evaluate(VkExec *ex, const VkExecTarget *dst) { @@ -1517,7 +1523,7 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { goto evaluated; } if (!e->feat) { - if (feat_released && idle_free() == 1) release_kept(e); + if (idle_free() != 2) release_kept(e, feat_released); feat_released = 0; p_setu(p, "CreationNodeMask", 1); p_setu(p, "VisibilityNodeMask", 1); From 0b4fd482e9a688dc71198abfcfc760d6967e9bd2 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 03:27:14 -0500 Subject: [PATCH 12/13] Record what the scripted runs showed about the DLSS release rule, and that the model may matter and not the size. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_dlaa.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 3d02dae6..aa70e8b0 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -522,8 +522,11 @@ static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, // Giving the kept feature its own sizes again at each evaluation changed nothing. A feature made while a smaller one // is alive leaves the smaller one as it was (the pause screen's after the gameplay one, at every first pause). So when // a feature is made, the alive ones with a larger input are released first, and after a release since the last -// creation all of them (release_kept); their images stay and each is made again when its size is next evaluated. With -// the second half alone, none of 5 runs in which the gameplay feature had been freed was striped. +// creation all of them (release_kept); their images stay and each is made again when its size is next evaluated. +// Scripted on one build, each way with the rule and with 2: the long pause and the mode change were striped without it +// and clean with it. The input size may not be what matters: in every striped case the feature made was of another +// model (performance's or ultra performance's) than the pause screen's, and the one run in which balanced's feature, +// of the pause screen's model, was made again beside it without the rule was clean. The rule covers both readings. // 1 (default) idle features are freed // 0 idle features are never freed (about 0.5 GB more video memory after the first pause) // 2 idle features are freed and nothing is released when a feature is made (as before; shows the fault) From 18be7383f8ed62d86482eda188c16ee5a1072855 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 03:14:21 -0500 Subject: [PATCH 13/13] End the Windows process without the libraries' detach calls, so a fault in NVIDIA's NGX detach no longer turns a clean exit into a FATAL. Co-Authored-By: Claude Fable 5.2 --- runtime/platform/win32.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 340fbec7..3e5fb806 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1098,9 +1098,16 @@ void plat_exit(int code) { #ifdef NX2NAVI_PGO_GEN __llvm_profile_dump(); #endif - fflush(stdout); - fflush(stderr); - ExitProcess((UINT)code); + fflush(NULL); // every open stream: nothing below runs the C runtime's own exit + // The process ends here, without the libraries' detach calls that ExitProcess makes. The callers have saved what + // is to be kept (gfx_present.c gfx_window_closed: settings, pipeline cache, profile, saves), and the game's other + // threads are still running when this is called. ExitProcess stops them wherever they are and then runs every + // library's detach on this thread: NVIDIA's NGX core, which still held the device and the DLSS features, read a + // dead pointer in its detach on the window thread after the window was closed (an access violation in + // _nvngx.dll after the last save, once in 5 scripted runs that had freed and made a DLSS feature again), and the + // crash handler wrote a FATAL for a run that had ended cleanly. + TerminateProcess(GetCurrentProcess(), (UINT)code); + ExitProcess((UINT)code); // not reached } // _putenv_s, not SetEnvironmentVariableA: getenv reads the C runtime's copy of the environment, which only the CRT's