diff --git a/nx2navi.ini.example b/nx2navi.ini.example index d139e7c7..dbddb364 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -964,6 +964,11 @@ # Diagnosis: one log line per frame while Link stands near a moving floor (the floating platforms): the frame's step, # how far he moved and how far the floor moved. 1 writes 4000 lines, a larger number that many; 0 is off. # floor_probe = 0 +# Diagnosis: while a floor carries Link sideways at 60 fps and the left stick rests, one snapshot of the game's memory +# finds the values below him that move with him (the floor's position among them) and logs them once a second from +# then on. The snapshot holds the game for about a second once. 1 writes 20000 lines, a larger number that many; 0 is +# off. +# floor_scan = 0 # 1: with trace = perf, walks of the process's address space count its private and write-combined memory every 600 # frames (the "mem: process" line) and where its committed memory lies every 3600 frames (the "commit census" lines). # Each walk holds the address-space lock for 100-240 ms and delays the next frame by as much, so they are off unless diff --git a/runtime/cfg.c b/runtime/cfg.c index 1d86a01e..92d9d6ed 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -33,7 +33,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line. static const char *const known[] = { - "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "PICKUP_COUNT", "WATER_STEPS", "FLOOR_PROBE", "MOVIE_PTS", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) + "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "PICKUP_COUNT", "WATER_STEPS", "FLOOR_PROBE", "FLOOR_SCAN", "MOVIE_PTS", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) "AA", "AA_LOG", "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CATCHUP", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_RAMP", "AUDIO_STATS", "BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "MESH_FALLBACK_WORKERS", "PROBE_MESHFB_DIR", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", @@ -99,7 +99,7 @@ static const struct { const char *key, *section; } sections[] = { {"VRAM_HEAP_LIMIT", "performance"}, {"VRAM_RESERVE_MB", "performance"}, {"UPLOAD_BATCH_MB", "performance"}, {"MESH_EMULATION", "performance"}, {"MESH_EMU_MB", "performance"}, {"MESH_EMU_TASKS", "performance"}, {"MESH_EMU_BATCH", "performance"}, {"VEC_INDEX_FIX", "performance"}, {"GUEST_OPT", "performance"}, {"PASS_TIMES", "diagnostics"}, {"DUMP_KEY", "diagnostics"}, - {"FLOOR_PROBE", "diagnostics"}, + {"FLOOR_PROBE", "diagnostics"}, {"FLOOR_SCAN", "diagnostics"}, {"FPS_LIMIT", "frame rate"}, {"FPS60", "frame rate"}, {"RTSS", "frame rate"}, {"REALTIME", "frame rate"}, {"CLOTH_REALTIME", "frame rate"}, {"HIGHFPS_FIX", "frame rate"}, {"PHYSICS_STEPS", "frame rate"}, {"PICKUP_COUNT", "frame rate"}, {"WATER_STEPS", "frame rate"}, @@ -245,6 +245,8 @@ static const CfgMeta metas[] = { {"WATER_STEPS", "1", "0|1", RESTART}, // frame_rate.c floor_probe: lines about Link on a moving floor (0 off, 1 = 4000 lines, or a count) {"FLOOR_PROBE", "0", NULL, RESTART}, + // frame_rate.c floor_scan: finds the moving floor under Link in guest memory (0 off, 1 = 20000 lines, or a count) + {"FLOOR_SCAN", "0", NULL, RESTART}, {"LANG", "", "ja|en-US|fr|de|it|es|zh-CN|ko|nl|pt|ru|zh-TW|en-GB|fr-CA|es-419|zh-Hans|zh-Hant|pt-BR|pl", RESTART}, {"OPERATION_MODE", "docked", "docked|handheld", LIVE}, {"TEXT_SPEED", "normal", "normal|fast|instant", LIVE}, // text_speed.c: the message window's typing diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 5d4ed6e2..66d81f40 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -51,6 +51,7 @@ #include "cfg.h" #include "gfx.h" #include "host.h" +#include "input.h" #include "layout.h" #include "platform/platform.h" #include "vk_dlssg.h" @@ -899,6 +900,193 @@ static void floor_probe(void) { floor_link_ok = link_ok; } +// ---- NX2NAVI_FLOOR_SCAN (floor_scan in nx2navi.ini, 0 by default): finds the moving floor under Link in guest memory +// (diagnosis, 1.1.1). The Deku Tree's floating platform is not one of the engine's ground groups (floor_probe), and +// where its position lives is not known. The scan arms while Link drifts at the 60 fps rate: a step of one frame, the +// gamepad's left stick under 20%, and for FLOOR_SCAN_ARM presents in a row a horizontal move of 2 to 30 mm a present, +// his height within 5 mm of the present before, and the whole stretch straight (its end within 80% of the sum of its +// moves), so a floor that only sinks does not arm it. It then takes one snapshot of every 4-byte-aligned float triplet +// in the game's heap and alias regions whose x and z lie within 6 m of Link and whose y lies from 4 m below him to +// 0.5 m above (walked as the debug menu's memory search walks them, debug_tools.c dbg_search_new), and logs how long +// that took. FLOOR_SCAN_ARM presents later it keeps the triplets that moved horizontally as far as Link (within a +// fifth, 2 cm at least) and lie more than 0.3 m below him or more than 0.5 m from him horizontally: the floor's +// matrix, body and drive, but not Link's own copies (his actor, body, feet) nor what rides above him. A snapshot that +// keeps none arms the scan again, three times at most. From then on, once a second, it logs Link's position and move +// over that second and one line per survivor (FLOOR_SCAN_SHOW at most) with its address, position and move over the +// same second, so a stretch at a higher frame rate shows each one's own speed. The value is the number of lines to +// write (1 means 20000); off it costs one test per present. +#define FLOOR_SCAN_ARM 30 +#define FLOOR_SCAN_MAX 2000000 // candidate triplets a snapshot keeps +#define FLOOR_SCAN_SHOW 64 // survivors logged each second +static int floor_scan_left = -1; // lines still to write; -1 before the setting was read +static struct { + int state, run, tries; // state 0: waiting for a drift, 1: filtering, 2: logging + float link0[3], last[3], start[3], path, sec_link[3]; + int have_last; + uint64_t *addr, sec_ns; + float *val; // the triplets at the snapshot, then at the last line + int n; +} fs; + +static void floor_scan_snapshot(const float link[3]) { + enum { PIECE = 16 << 20 }; + if (!fs.addr) fs.addr = malloc(sizeof(uint64_t) * FLOOR_SCAN_MAX); + if (!fs.val) fs.val = malloc(sizeof(float) * 3 * FLOOR_SCAN_MAX); + if (!fs.addr || !fs.val) return; + uint64_t t0 = plat_now_ns(), bytes = 0; + int over = 0; + fs.n = 0; + const RtGuestLayout *l = mem_layout(); + const RtRange ranges[2] = {l->heap, l->alias}; + 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) { + a = stop; + continue; + } + while (a < stop) { + uint64_t n = stop - a < PIECE ? stop - a : PIECE; + if (n >= 12 && mem_read_hold(a, n)) { + const float *f = (const float *)(uintptr_t)a; + uint64_t words = n / 4; + bytes += n; + for (uint64_t i = 0; i + 2 < words; i++) { + float x = f[i]; + if (!(fabsf(x - link[0]) < 6.0f)) continue; // the cheap test first: most words fail it + float y = f[i + 1] - link[1], z = f[i + 2]; + if (!(fabsf(z - link[2]) < 6.0f) || !(y > -4.0f && y < 0.5f)) continue; + if (fs.n < FLOOR_SCAN_MAX) { + fs.addr[fs.n] = a + 4 * i; + memcpy(fs.val + 3 * fs.n, f + i, 12); + fs.n++; + } else + over = 1; + } + mem_read_release(); + } + a += n; + } + } + } + memcpy(fs.link0, link, 12); + rt_log("floor scan: snapshot of %.0f MB in %.0f ms: %d float triplets around and below Link at %.3f %.3f %.3f%s", + bytes / 1e6, (plat_now_ns() - t0) / 1e6, fs.n, link[0], link[1], link[2], + over ? " (more were left out)" : ""); +} + +// Reads the n triplets at fs.addr into out (NAN where the memory could not be read), in one batched copy. +static void floor_scan_read(int n, float *out) { + static uint64_t (*src)[2]; + static uint8_t **dst, *ok; + if (!src) src = malloc(sizeof *src * FLOOR_SCAN_MAX); + if (!dst) dst = malloc(sizeof *dst * FLOOR_SCAN_MAX); + if (!ok) ok = malloc(FLOOR_SCAN_MAX); + if (!src || !dst || !ok) return; + for (int i = 0; i < n; i++) src[i][0] = fs.addr[i], src[i][1] = 12, dst[i] = (uint8_t *)(out + 3 * i); + mem_copy_readable(dst, (const uint64_t (*)[2])src, n, ok); + for (int i = 0; i < n; i++) + if (!ok[i]) out[3 * i] = out[3 * i + 1] = out[3 * i + 2] = NAN; +} + +static float hdist(const float a[3], const float b[3]) { return hypotf(a[0] - b[0], a[2] - b[2]); } + +static void floor_scan(const float link[3], int link_ok) { + if (floor_scan_left < 0) { + const char *e = cfg_get("NX2NAVI_FLOOR_SCAN"); + int n = e && *e ? atoi(e) : 0; + floor_scan_left = !RT_V111 || n <= 0 ? 0 : n == 1 ? 20000 : n; + if (floor_scan_left) rt_log("floor scan: on for %d lines", floor_scan_left); + } + if (!floor_scan_left) return; + if (!link_ok) { + fs.have_last = 0, fs.run = 0; + return; + } + float move = fs.have_last ? hdist(link, fs.last) : 0, rise = fs.have_last ? fabsf(link[1] - fs.last[1]) : 0; + int had = fs.have_last; + memcpy(fs.last, link, 12); + fs.have_last = 1; + if (fs.state == 0) { + double pct[2] = {0, 0}; + int still = !input_pad_stick_percent(pct) || pct[0] < 20.0; + int drift = had && still && fabsf(frame_step_frames() - 1.0f) < 0.05f && move > 0.002f && move < 0.03f && + rise < 0.005f; + if (!drift) { + fs.run = 0; + return; + } + if (!fs.run) memcpy(fs.start, link, 12), fs.path = 0; + fs.run++, fs.path += move; + if (fs.run >= FLOOR_SCAN_ARM) { + int straight = hdist(link, fs.start) >= 0.8f * fs.path; + fs.run = 0; + if (straight && fs.tries < 3) { + fs.tries++; + floor_scan_snapshot(link); + if (fs.n) fs.state = 1; + } + } + return; + } + if (fs.state == 1) { + if (++fs.run < FLOOR_SCAN_ARM) return; + fs.run = 0; + float d_link = hdist(link, fs.link0); + static float *now; + if (!now) now = malloc(sizeof(float) * 3 * FLOOR_SCAN_MAX); + if (!now) return; + floor_scan_read(fs.n, now); + int kept = 0, moved = 0; + float tol = d_link * 0.2f > 0.02f ? d_link * 0.2f : 0.02f; + for (int i = 0; i < fs.n; i++) { + const float *v = now + 3 * i, *w = fs.val + 3 * i; + if (!isfinite(v[0]) || !isfinite(v[1]) || !isfinite(v[2])) continue; + float d = hypotf(v[0] - w[0], v[2] - w[2]); + if (!(fabsf(d - d_link) <= tol) || d < 0.01f) continue; + moved++; + if (!(v[1] < link[1] - 0.3f || hdist(v, link) > 0.5f)) continue; // Link's own copies stay at his feet + if (kept < FLOOR_SCAN_SHOW) fs.addr[kept] = fs.addr[i], memcpy(fs.val + 3 * kept, v, 12); + kept++; + } + rt_log("floor scan: %d of %d triplets moved with Link (%.3f m in %d presents); %d of those lie below him or " + "away from his feet%s", moved, fs.n, d_link, FLOOR_SCAN_ARM, kept, + kept > FLOOR_SCAN_SHOW ? " (the first of them are logged)" : ""); + if (!kept) { // nothing below him: the next drift arms the scan again + fs.state = 0, fs.n = 0; + return; + } + fs.n = kept < FLOOR_SCAN_SHOW ? kept : FLOOR_SCAN_SHOW; + memcpy(fs.sec_link, link, 12); + fs.sec_ns = plat_now_ns(); + fs.state = 2; + return; + } + // state 2: once a second, Link and each survivor with its move over that second + uint64_t t = plat_now_ns(); + if (t - fs.sec_ns < 1000000000ull) return; + double secs = (t - fs.sec_ns) / 1e9; + fs.sec_ns = t; + float v[FLOOR_SCAN_SHOW * 3]; + floor_scan_read(fs.n, v); + rt_log("floor scan: step %.4f frames; link %.4f %.4f %.4f moved %.4f %.4f %.4f in %.3f s (%.3f m/s)", + (double)frame_step_frames(), link[0], link[1], link[2], link[0] - fs.sec_link[0], link[1] - fs.sec_link[1], + link[2] - fs.sec_link[2], secs, hdist(link, fs.sec_link) / secs); + memcpy(fs.sec_link, link, 12); + floor_scan_left--; + for (int k = 0; k < fs.n && floor_scan_left > 0; k++) { + float *w = fs.val + 3 * k, *u = v + 3 * k; + rt_log("floor scan: %#llx at %.4f %.4f %.4f moved %.4f %.4f %.4f (%.3f m/s)", (unsigned long long)fs.addr[k], + u[0], u[1], u[2], u[0] - w[0], u[1] - w[1], u[2] - w[2], hdist(u, w) / secs); + memcpy(w, u, 12); + floor_scan_left--; + } + if (floor_scan_left < 0) floor_scan_left = 0; +} + void frame_step_present(uint64_t display_ns, int interval) { atomic_fetch_add_explicit(&guest_presents, 1, memory_order_relaxed); game_interval = interval > 0 ? interval : 1; @@ -931,6 +1119,11 @@ void frame_step_present(uint64_t display_ns, int interval) { memory_order_relaxed); world_clock(dt, step); floor_probe(); + if (floor_scan_left) { // after its first read the scan costs this test alone while off + float link[3]; + int ok = floor_scan_left > 0 ? player_pos(link) : 0; + floor_scan(link, ok); + } if (rt_trace & RT_TRACE_PERF) { time_dump(); if (frame_free_running()) { // the game's own step, as its time manager holds it diff --git a/tests/frame_step_check.c b/tests/frame_step_check.c index 8607fd0a..285fd9e2 100644 --- a/tests/frame_step_check.c +++ b/tests/frame_step_check.c @@ -45,6 +45,13 @@ int plat_mem_readable(uint64_t a, size_t n) { (void)a, (void)n; return 0; } void mem_kernel_access(uint64_t a, uint64_t n) { (void)a, (void)n; } int mem_host_readable(uint64_t a, uint64_t n) { (void)a, (void)n; return 0; } int mem_info(uint32_t id, uint64_t *out) { (void)id, (void)out; return 0; } // the floor probe (off here) +// the floor scan (off here) +static RtGuestLayout layout_stub; +const RtGuestLayout *mem_layout(void) { return &layout_stub; } +uint32_t mem_query(uint64_t a, RtMemoryInfo *out) { (void)a, memset(out, 0, sizeof *out); return 1; } +int mem_read_hold(uint64_t a, uint64_t n) { (void)a, (void)n; return 0; } +void mem_read_release(void) {} +int input_pad_stick_percent(double pct[2]) { pct[0] = pct[1] = 0; return 0; } int mem_copy_readable(uint8_t *const *dst, const uint64_t (*src)[2], int n, uint8_t *ok) { (void)dst, (void)src, (void)n, (void)ok; return 0;