Add a floor scan that finds the floor carrying Link in guest memory on 1.1.1.

The Deku Tree's floating platform is not one of the engine's ground groups, and where its position lives is not known.
With floor_scan set (off by default), once Link has been carried at 60 fps for 30 frames with the left stick at rest,
one snapshot of the game's heap and alias regions keeps every float triplet near him; 30 frames later the triplets
that moved as far as he did are kept, and from then on each frame logs the twelve of them farthest from him with their
addresses, positions and moves. The snapshot logs how long it took.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 04:36:29 -05:00
parent 652ed4dcce
commit 2970b0c7d6
4 changed files with 210 additions and 2 deletions

View File

@@ -1014,6 +1014,10 @@
# 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 Link is carried by a moving floor at 60 fps and the left stick rests, one snapshot of the game's
# memory finds the values that move with him (the floor's position among them) and logs them each frame from then on.
# The snapshot holds the game for a moment once. 1 writes 6000 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

View File

@@ -34,7 +34,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",
@@ -100,7 +100,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"},
@@ -248,6 +248,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 = 6000 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

View File

@@ -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,195 @@ 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. While Link is carried at the 60 fps rate (a step of one frame, a horizontal
// move of 2 to 30 mm a present for FLOOR_SCAN_ARM presents in a row, the gamepad's left stick under 20%), the scan
// 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 within 4 m (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): Link's own copies (his actor, body, bones) and the
// floor's (its matrix, its body, its drive). From then on each present logs the step, Link, and the survivors farthest
// from him horizontally (the floor's points sit away from him; his own copies stay within half a metre), each with its
// address, position and move, so a run at a higher frame rate shows which of those move and how fast. A snapshot that
// leaves no survivor farther than half a metre arms the scan again, three times at most. The value is the number of
// lines to write (1 means 6000); 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 12 // survivors a line names
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 carry, 1: filtering, 2: logging
float link0[3], last[3];
int have_last;
uint64_t *addr;
float *val; // the triplet at the snapshot, later the last logged one
int n;
int show[FLOOR_SCAN_SHOW], shown;
} 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], z = f[i + 2];
if (!(fabsf(z - link[2]) < 6.0f) || !(fabsf(y - link[1]) < 4.0f)) 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 near 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 ? 6000 : 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;
memcpy(fs.last, link, 12);
int had = fs.have_last;
fs.have_last = 1;
if (fs.state == 0) {
double pct[2] = {0, 0};
float step = frame_step_frames();
int still = !input_pad_stick_percent(pct) || pct[0] < 20.0;
int carried = had && still && fabsf(step - 1.0f) < 0.05f && move > 0.002f && move < 0.03f;
fs.run = carried ? fs.run + 1 : 0;
if (fs.run >= FLOOR_SCAN_ARM && fs.tries < 3) {
fs.tries++, fs.run = 0;
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, far = 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[2])) continue;
float d = hypotf(v[0] - w[0], v[2] - w[2]);
if (!(fabsf(d - d_link) <= tol) || d < 0.01f) continue;
fs.addr[kept] = fs.addr[i];
memcpy(fs.val + 3 * kept, v, 12);
far += hdist(v, link) > 0.5f;
kept++;
}
fs.n = kept;
rt_log("floor scan: %d of the triplets moved with Link (%.3f m in %d presents), %d of them more than 0.5 m "
"from him",
kept, d_link, FLOOR_SCAN_ARM, far);
if (!far) { // Link's own copies alone: the next carry arms the scan again
fs.state = 0, fs.n = 0;
return;
}
// the survivors to show: the farthest from Link horizontally
fs.shown = 0;
for (int j = 0; j < FLOOR_SCAN_SHOW; j++) { // a selection: the farthest not yet shown, FLOOR_SCAN_SHOW times
int best = -1;
float best_d = -1;
for (int i = 0; i < fs.n; i++) {
int taken = 0;
for (int t = 0; t < fs.shown && !taken; t++) taken = fs.show[t] == i;
float d = hdist(fs.val + 3 * i, link);
if (!taken && d > best_d) best = i, best_d = d;
}
if (best < 0) break;
fs.show[fs.shown++] = best;
}
fs.state = 2;
return;
}
// state 2: one line a present for the shown survivors
float v[FLOOR_SCAN_SHOW][3];
int k = fs.shown;
for (int j = 0; j < k; j++) {
uint8_t ok = 0, *dst = (uint8_t *)v[j];
uint64_t src[1][2] = {{fs.addr[fs.show[j]], 12}};
mem_copy_readable(&dst, (const uint64_t (*)[2])src, 1, &ok);
if (!ok) v[j][0] = v[j][1] = v[j][2] = NAN;
}
char line[256 + FLOOR_SCAN_SHOW * 110];
int o = snprintf(line, sizeof line, "floor scan: step %.4f frames; link %.4f %.4f %.4f moved %.5f",
(double)frame_step_frames(), link[0], link[1], link[2], move);
for (int j = 0; j < k && o > 0 && o < (int)sizeof line; j++) {
float *w = fs.val + 3 * fs.show[j];
o += snprintf(line + o, sizeof line - o, "; %#llx at %.4f %.4f %.4f moved %.5f %.5f %.5f",
(unsigned long long)fs.addr[fs.show[j]], v[j][0], v[j][1], v[j][2], v[j][0] - w[0],
v[j][1] - w[1], v[j][2] - w[2]);
memcpy(w, v[j], 12);
}
rt_log("%s", line);
floor_scan_left--;
}
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 +1121,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

View File

@@ -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;