From d577f8c325190aa3974d3a00afc15aa58d89929c Mon Sep 17 00:00:00 2001 From: DeepDaddyTTV <32502468+DeepDaddyTTV@users.noreply.github.com> Date: Tue, 10 Nov 2026 19:19:00 -0500 Subject: [PATCH 1/4] Save the build state before the source generation starts, so a generation cut short after it wrote layout.json runs again on the next try instead of being taken as done. After Keep Build Cache removed the generated sources and the kit was updated, the lift ran because layout.json was gone while its key still matched. An interruption after layout.json was back left the saved state calling the lift complete, and the next run compiled unfinished sources. The pull request's two commits (one a merge of main) are applied here as their net change. Co-Authored-By: Claude Fable 5.2 --- tests/launcher_test.py | 38 +++++++++++++++++++++++++++++++++++--- tools/make_game.py | 4 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 51bf1202..02e21080 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1554,13 +1554,14 @@ class MakeGameTest(Tmp): (kits[1] / 'runtime' / 'h.h').write_text('#define V 2\n') # the new version changed the header self.assertEqual(go(kits[1])[:2], (1, 0)) - def main_with(self, romfs, lift, compile_): - """make_game.main() in this process for a first build, with the given step functions in place of the real + def main_with(self, romfs, lift, compile_, rebuild=False): + """make_game.main() in this process, with the given step functions in place of the real lift and compile (they need real modules and clang). Returns the SystemExit code, or 0.""" sys.path.insert(0, str(self.layout.pydeps)) # the fake zbic saved = (self.mg.step_romfs, self.mg.step_lift, self.mg.step_compile, sys.argv) self.mg.step_romfs, self.mg.step_lift, self.mg.step_compile = romfs, lift, compile_ - sys.argv = ['make_game.py', '--nup', str(self.nup), '--out', str(self.out), '--msys', str(self.msys), + source = ['--rebuild'] if rebuild else ['--nup', str(self.nup)] + sys.argv = ['make_game.py', *source, '--out', str(self.out), '--msys', str(self.msys), '--until', 'compile'] # the shaders step needs the shader translator try: with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): @@ -1572,6 +1573,37 @@ class MakeGameTest(Tmp): self.mg.step_romfs, self.mg.step_lift, self.mg.step_compile, sys.argv = saved sys.path.remove(str(self.layout.pydeps)) + def test_retry_interrupted_lift_after_cache_cleanup(self): + layout = self.out / 'build-cache' / 'lift' / 'layout.json' + ran = [] + + def lift(out, cache): + ran.append('lift') + layout.parent.mkdir(exist_ok=True) + layout.write_text('[]') + + def compile_(out, cache, msys, jobs, romfs_job, update): + ran.append('compile') + self.assertTrue(layout.is_file()) + + def interrupted(out, cache): + lift(out, cache) + raise SystemExit(1) # layout exists again, but the lift did not finish + + with mock.patch.object(self.mg, 'WIN', True): # use the fixture's MSYS toolchain check + self.assertEqual(self.main_with(self.mg.step_romfs, lift, compile_), 0) + self.nup.unlink() + ran.clear() + shutil.rmtree(layout.parent) + self.assertEqual(self.main_with(self.mg.step_romfs, lift, compile_, rebuild=True), 0) + self.assertEqual(ran, []) # cleaning the cache alone does not require a compile + + with mock.patch.object(self.mg, 'kit_version', return_value='updated kit'): + self.assertEqual(self.main_with(self.mg.step_romfs, interrupted, compile_, rebuild=True), 1) + self.assertEqual(self.main_with(self.mg.step_romfs, lift, compile_, rebuild=True), 0) + self.assertEqual(self.main_with(self.mg.step_romfs, lift, compile_, rebuild=True), 0) + self.assertEqual(ran, ['lift', 'lift', 'compile']) + def test_romfs_beside_lift_and_compile(self): """A first build packs the RomFS image while the lift and the compile run; the compile gets the running step and waits for it only for the RomFS index.""" diff --git a/tools/make_game.py b/tools/make_game.py index 4a4c228d..91dc0c17 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -2487,6 +2487,10 @@ def build_main(): for later in STEPS[STEPS.index(name):]: if later == name or not ({name, later} & set(OWN_KEY_STEPS) or (name, later) in UNAFFECTED): state['steps'].pop(later, None) + if name == 'lift': + # The generator writes layout.json before the sources. A retry must finish an interrupted lift + # even if the layout is back and the previous lift key still matches its inputs. + save() say(f'{name}:') emit('step', step=name, state='start') if name in ('shaders', 'prompts', 'scan') and romfs_job: # they read the RomFS image From 5b7703e08ed0c45cf833322b2e1b0520c8f79ecc Mon Sep 17 00:00:00 2001 From: DeepDaddyTTV <32502468+DeepDaddyTTV@users.noreply.github.com> Date: Tue, 10 Nov 2026 19:36:32 -0500 Subject: [PATCH 2/4] Record the draws rejected during preparation in a captured frame. A draw can be left out before it opens a render pass, and the pass list of a frame capture then showed nothing of it. passes.txt now ends with the rejected draws of the captured frame by mesh or vertex draw and reason, each with the first program it met, and says so when there were none. A draw left out by NX2NAVI_SKIP_PROG or NX2NAVI_SKIP_SHADERS is counted under a reason of its own. The frame to capture and its files are set together under the recorder's lock. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_dump.c | 58 ++++++++++++++++++++++++++++++------------ runtime/vk_exec.h | 4 +++ runtime/vk_exec_draw.c | 18 ++++++------- 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/runtime/vk_dump.c b/runtime/vk_dump.c index 0835c482..db18c1a3 100644 --- a/runtime/vk_dump.c +++ b/runtime/vk_dump.c @@ -8,7 +8,7 @@ // ..._tm.png float formats: x/(1+x) of the same image (the plain one clamps); // in both, a pixel with a NaN channel is magenta // passes.txt per pass or dispatch: targets, draws, each distinct pipeline + texture set with its shaders' SPIR-V -// hashes (sh_.spv written once) and the bound textures with their samplers +// hashes (sh_.spv written once) and the bound textures with their samplers; draw preparation rejections by reason while the capture ran // images.txt per image: channel min/max, distinct texel values over the image and over its top quarter // Sampled 8-bit 2D textures up to TEX2D_CAP texels are copied once each as pass__tex_... at the end of the pass // that samples them; texture lines carry the storage address, mirror state, upload count and the image's and slot's @@ -474,6 +474,32 @@ static int active(void) { // passes_f is NULL before a dump that opens its fil } static uint64_t frame_now(void) { return __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1; } +// A draw can be rejected before it opens a render pass. Count it against the captured frame as a whole. +static struct { uint64_t count, first_prog; } skipped[2][SK_COUNT]; + +void vk_dump_draw_skipped(const VkExec *ex, int mesh, int why) { + plat_mutex_lock(&rec_lock); + if (!finished && passes_f && frame_now() == dump_frame) { + if (!skipped[mesh][why].count) skipped[mesh][why].first_prog = ex->prog ? ex->prog->serial : 0; + skipped[mesh][why].count++; + } + plat_mutex_unlock(&rec_lock); +} + +static void write_skipped(uint64_t frame) { + uint64_t total = 0; + for (int m = 0; m < 2; m++) for (int k = 0; k < SK_COUNT; k++) { + uint64_t n = skipped[m][k].count; + if (!n) continue; + fprintf(passes_f, " skipped %s: %llu (%s, first program %llu)\n", m ? "mesh" : "vertex", + (unsigned long long)n, vk_draw_skip_names[k], (unsigned long long)skipped[m][k].first_prog); + total += n; + } + fprintf(passes_f, "frame %llu: %llu draws rejected during draw preparation while capture was active\n", + (unsigned long long)frame, (unsigned long long)total); + memset(skipped, 0, sizeof skipped); +} + typedef struct { char *s; size_t n, cap; } Str; __attribute__((format(printf, 2, 3))) static void sp(Str *b, const char *fmt, ...) { @@ -1226,6 +1252,15 @@ static int thumb_white(uint64_t f) { return bright * 100 > n * 97; } +// Publish the selected frame and its output files together to the recording hooks. +static void open_frame(uint64_t frame) { + plat_mutex_lock(&rec_lock); + snprintf(dir, sizeof dir, "%s/f%llu", base_dir, (unsigned long long)frame); + open_files(); + dump_frame = frame; + plat_mutex_unlock(&rec_lock); +} + static void white_check(uint64_t frame) { static int run; // consecutive white thumbnails: white runs are mostly 1-2 frames, too short for a dump 4 frames on if (frame < 1800) return; @@ -1234,9 +1269,7 @@ static void white_check(uint64_t frame) { run = w ? run + 1 : 0; if (run >= 3) { // frames frame-5..frame-3 white: the run is likely still on 4 frames later run = 0; - dump_frame = frame + 1; - snprintf(dir, sizeof dir, "%s/f%llu", base_dir, (unsigned long long)dump_frame); - open_files(); + open_frame(frame + 1); rt_log("vk_dump: frames %llu-%llu were white; frame %llu goes to %s", (unsigned long long)(frame - 5), (unsigned long long)(frame - 3), (unsigned long long)dump_frame, dir); } @@ -1251,18 +1284,14 @@ void vk_dump_present(uint64_t frame) { } if (dump_white && !white_caught && !white_last && dump_frame == UINT64_MAX) white_check(frame); if (movie_k && dump_frame == UINT64_MAX && __atomic_exchange_n(&movie_req, 0, __ATOMIC_ACQ_REL)) { - snprintf(dir, sizeof dir, "%s/f%llu", base_dir, (unsigned long long)(frame + 1)); - open_files(); // before the frame is named: its passes write to them - dump_frame = frame + 1; + open_frame(frame + 1); rt_log("vk_dump: frame %llu goes to %s", (unsigned long long)dump_frame, dir); } if (key_mode && dump_frame == UINT64_MAX) { // NX2NAVI_DUMP=key: a new press of Insert names the next frame static int held; int now = input_dump_key(); if (now && !held) { - snprintf(dir, sizeof dir, "%s/f%llu", base_dir, (unsigned long long)(frame + 1)); - open_files(); // before the frame is named: its passes write to them - dump_frame = frame + 1; + open_frame(frame + 1); rt_log("vk_dump: Insert: frame %llu goes to %s", (unsigned long long)dump_frame, dir); } held = now; @@ -1274,14 +1303,13 @@ void vk_dump_present(uint64_t frame) { free(txt); file_more = n > 8 ? 7 : n > 1 ? n - 1 : 0; plat_file_delete(dump_file); - snprintf(dir, sizeof dir, "%s/f%llu", base_dir, (unsigned long long)(frame + 1)); - open_files(); // before the frame is named: its passes write to them - dump_frame = frame + 1; + open_frame(frame + 1); rt_log("vk_dump: %s found: frame %llu goes to %s", dump_file, (unsigned long long)dump_frame, dir); } if (frame != dump_frame) return; plat_mutex_lock(&rec_lock); finished = 1; + write_skipped(frame); plat_mutex_unlock(&rec_lock); vk_sync_finish(); // the frame's copies are converted and written mq_write(); @@ -1305,9 +1333,7 @@ void vk_dump_present(uint64_t frame) { } if (file_more > 0) { // NX2NAVI_DUMP_FILE with a count: the next frame too file_more--; - snprintf(dir, sizeof dir, "%s/f%llu", base_dir, (unsigned long long)(frame + 1)); - open_files(); - dump_frame = frame + 1; + open_frame(frame + 1); rt_log("vk_dump: frame %llu goes to %s", (unsigned long long)dump_frame, dir); } } diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 3df3b9da..09f141a9 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -254,6 +254,10 @@ void vk_dump_init(void); // reads the environment; vk_exec_recor void vk_dump_pass_end(VkExec *ex); // after vkCmdEndRendering // after a draw's binds; mesh: a mesh draw (s->vs is its mesh stage, s->ts its task stage or NULL) void vk_dump_draw(VkExec *ex, const VkPipeState *s, VkPipeline p, const VkPipeInfo *info, int mesh); +enum { SK_DISABLED, SK_SCISSOR, SK_NO_PROGRAM, SK_NO_VERTEX_STAGE, SK_SHADER_PENDING, SK_SHADER_FAILED, SK_DLAA, + SK_PIPE_PENDING, SK_PIPE_FAILED, SK_POOL_OFF, SK_PROG_FILTER, SK_SHADER_FILTER, SK_COUNT }; +extern const char *const vk_draw_skip_names[SK_COUNT]; +void vk_dump_draw_skipped(const VkExec *ex, int mesh, int why); // Around a mesh draw of the dumped frame (after vk_dump_draw): a primitives-generated query, listed in mesh_prims.txt. // begin returns the query, or -1 when none was begun; end takes that value. int vk_dump_mesh_begin(VkExec *ex); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index c2924a6d..7f98b92f 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -4064,13 +4064,12 @@ static int pipe_memo_on(void) { // the draws issued and skipped since the last line (only when something was skipped). A black screen with draws // skipped for "shader failed" or "pipeline failed" points at the translator or the pipeline; draws issued and still // nothing on screen point at the shaders' results (NX2NAVI_DUMP). -enum { SK_DISABLED, SK_SCISSOR, SK_NO_PROGRAM, SK_NO_VERTEX_STAGE, SK_SHADER_PENDING, SK_SHADER_FAILED, SK_DLAA, - SK_PIPE_PENDING, SK_PIPE_FAILED, SK_POOL_OFF, SK_COUNT }; -static const char *const sk_names[SK_COUNT] = { - "rendering disabled (render condition)", "empty scissor", "no program bound", +const char *const vk_draw_skip_names[SK_COUNT] = { + "rendering disabled", "empty scissor", "no program bound", "no vertex (or mesh) stage in the program", "shader still translating", "shader failed to translate (see gfx_shader: translation failed)", "replaced by the upscaler", - "pipeline still building", "pipeline could not be created", "uses the texture pools (NX2NAVI_POOL=0)"}; + "pipeline still building", "pipeline could not be created", "uses the texture pools (NX2NAVI_POOL=0)", + "excluded by NX2NAVI_SKIP_PROG", "excluded by NX2NAVI_SKIP_SHADERS"}; #define DRAW_STATS_FRAMES 600 static struct { uint64_t issued[2], skipped[2][SK_COUNT], frame; uint32_t first[2]; } DRS; @@ -4105,7 +4104,7 @@ static void draw_stats_tick(void) { int o = snprintf(line, sizeof line, "vk: %s draws over %llu frames: %llu issued, skipped:", m ? "mesh" : "vertex", (unsigned long long)(f - DRS.frame), (unsigned long long)DRS.issued[m]); for (int k = 0; k < SK_COUNT && o < (int)sizeof line - 60; k++) - if (DRS.skipped[m][k]) o += snprintf(line + o, sizeof line - o, " %s %llu;", sk_names[k], (unsigned long long)DRS.skipped[m][k]); + if (DRS.skipped[m][k]) o += snprintf(line + o, sizeof line - o, " %s %llu;", vk_draw_skip_names[k], (unsigned long long)DRS.skipped[m][k]); rt_log("%s", line); } memset(&DRS, 0, sizeof DRS.issued + sizeof DRS.skipped); @@ -4124,11 +4123,12 @@ uint32_t vk_exec_pending_draws_take(void) { static int draw_skipped(const VkExec *ex, int mesh, int why) { DRS.skipped[mesh][why]++; + if (vk_dump_armed) vk_dump_draw_skipped(ex, mesh, why); if (why == SK_SHADER_PENDING || why == SK_PIPE_PENDING) pending_draws++; if (why == SK_PIPE_PENDING) perf_count(PERF_CNT_PIPE_SKIP); // skipped draws only: a lookup that then waited is none if (!(DRS.first[mesh] >> why & 1)) { DRS.first[mesh] |= 1u << why; - rt_log("vk: first %s draw skipped because: %s (program %llu)", mesh ? "mesh" : "vertex", sk_names[why], + rt_log("vk: first %s draw skipped because: %s (program %llu)", mesh ? "mesh" : "vertex", vk_draw_skip_names[why], ex->prog ? (unsigned long long)ex->prog->serial : 0ull); } draw_stats_tick(); @@ -4215,7 +4215,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int if (!sc.extent.width || !sc.extent.height) return draw_skipped(ex, mesh, SK_SCISSOR); if (!ex->prog) return draw_skipped(ex, mesh, SK_NO_PROGRAM); if (prog_lead_on()) prog_lead(ex->prog, mesh); - if (n_skip_prog && skip_prog_wanted(ex->prog->serial)) return draw_skipped(ex, mesh, SK_DISABLED); // -1 until read + if (n_skip_prog && skip_prog_wanted(ex->prog->serial)) return draw_skipped(ex, mesh, SK_PROG_FILTER); // -1 until read int has_vs, has_fs, vs_wait = 0, fs_wait = 0, memo = mesh ? 0 : shader_memo_on(); int has_ts = 0, ts_wait = 0, vst = mesh ? NVN_STAGE_MESH : NVN_STAGE_VS; const GfxShader *vs, *fs, *ts = NULL; @@ -4260,7 +4260,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int return perf_count(PERF_CNT_SHADER_SKIP), draw_skipped(ex, mesh, SK_SHADER_PENDING); } if (!vs || (has_fs && !fs) || (has_ts && !ts)) return draw_skipped(ex, mesh, SK_SHADER_FAILED); - if (shader_skipped(vs) || shader_skipped(fs)) return draw_skipped(ex, mesh, SK_POOL_OFF); // NX2NAVI_SKIP_SHADERS + if (shader_skipped(vs) || shader_skipped(fs)) return draw_skipped(ex, mesh, SK_SHADER_FILTER); if (vk_dlaa_skip_draw(ex, fs)) return draw_skipped(ex, mesh, SK_DLAA); // the game's FXAA off, or its scaling replaced by DLSS (vk_dlaa.c) uint32_t fbl = feedback_prepare(ex, fs); VkPipeState s = ex->pipe; From b25fba2e55491ce686038b25be9b7add3b953774 Mon Sep 17 00:00:00 2001 From: "automata." Date: Thu, 12 Nov 2026 10:35:57 -0500 Subject: [PATCH 3/4] Add shared pose motion vectors for Link and attached equipment --- runtime/motion_owner.c | 322 +++++++++++++++++++++++++++++++ runtime/motion_owner.h | 30 +++ runtime/nvn_api_core.c | 13 +- runtime/shaders/link_motion.frag | 28 +++ runtime/vk.h | 3 + runtime/vk_backend.c | 2 + runtime/vk_dlaa.c | 22 ++- runtime/vk_dlaa.h | 2 + runtime/vk_exec_draw.c | 225 +++++++++++++++------ runtime/vk_fg_pose.inc | 276 ++++++++++++++++++++++++++ runtime/vk_usnap.c | 36 ++++ 11 files changed, 900 insertions(+), 59 deletions(-) create mode 100644 runtime/motion_owner.c create mode 100644 runtime/motion_owner.h create mode 100644 runtime/shaders/link_motion.frag create mode 100644 runtime/vk_fg_pose.inc diff --git a/runtime/motion_owner.c b/runtime/motion_owner.c new file mode 100644 index 00000000..602bfd89 --- /dev/null +++ b/runtime/motion_owner.c @@ -0,0 +1,322 @@ +#include "motion_owner.h" +#include "host.h" +#include "layout.h" +#include "nvn_api.h" +#include "platform/platform.h" +#include +#include +#include + +// The supported game build exposes a player equipment component with generation-checked actor links. +// Selection follows those links and the equipment's model-binding parent; proximity is never an identity. +#define OWNER_MODELS 128 +#define OWNER_ACTORS 22 +typedef struct { + uint64_t actor, link; + uint32_t generation, kind; +} OwnerActor; +static struct { + MotionOwner models[OWNER_MODELS]; + unsigned count; + uint64_t frame, producer_frame, serial; + int valid; + MotionOwnerBinding bindings[MOTION_OWNER_BINDINGS]; + MotionOwnerBinding retired[MOTION_OWNER_BINDINGS]; + unsigned nbindings, nretired; + uint64_t captures, owned_queries, unknown_queries, conflicts, capacity_misses; + unsigned model_bind_failures; +} O; +static PlatMutex owner_lock = PLAT_MUTEX_INIT; +static PlatOnce owner_once = PLAT_ONCE_INIT; +static int owner_on; + +static void owner_init(void *unused) { + (void)unused; + owner_on = RT_V111 && rt_knob("SELECTIVE_MOTION", "1"); +} +int motion_owner_enabled(void) { + plat_once(&owner_once, owner_init, NULL); + return owner_on; +} +static int read_at(uint64_t a, void *out, size_t n) { + if (!a || a > UINT64_MAX - n) return 0; + mem_kernel_access(a, n); + if (!mem_read_hold(a, n)) return 0; + memcpy(out, (const void *)(uintptr_t)a, n); + mem_read_release(); + return 1; +} +static uint64_t ptr(uint64_t a) { + uint64_t p = 0; + return read_at(a, &p, sizeof p) ? p : 0; +} +static uint64_t field(uint64_t object, uint32_t offset) { return object ? ptr(object + offset) : 0; } +static uint64_t component(uint64_t actor, uint32_t offset) { + return field(field(actor, 0x270), offset); +} +static uint64_t player_actor(void) { + uint64_t p = field(ptr(MOD_MAIN_BASE + 0x4e371d0), 0x480); + char name[7]; + return p && read_at(field(p, 0x20), name, sizeof name) && !memcmp(name, "Player", 7) ? p : 0; +} +static int actor_link(uint64_t link, OwnerActor *out) { + struct { uint64_t table; uint32_t generation, padding; } a, b; + uint32_t generation, state; + if (!link || !read_at(link + 8, &a, sizeof a) || !a.table || a.generation == UINT32_MAX || + !read_at(a.table + 0x48, &generation, 4) || generation != a.generation) return 0; + uint64_t actor = field(a.table, 0x40); + if (!actor || !read_at(actor + 0x2c, &state, 4) || (uint32_t)(state - 7) < 5) return 0; + // Reject a slot replaced during the read. This is not a replacement for the game's actor locking: + // unknown layouts and any observed disagreement fail closed, and runtime lifetime checks remain required. + if (!read_at(link + 8, &b, sizeof b) || a.table != b.table || a.generation != b.generation || + !read_at(a.table + 0x48, &generation, 4) || generation != a.generation || field(a.table, 0x40) != actor) return 0; + *out = (OwnerActor){actor, a.table, generation, 0}; + return 1; +} +static int parent_is_player(uint64_t actor, uint64_t player) { + uint64_t seen[4] = {actor}; + for (unsigned depth = 0; depth < 4; ++depth) { + uint64_t bind = component(actor, 0x18); + OwnerActor parent; + if (!bind || !actor_link(bind + 0x70, &parent)) return 0; + if (parent.actor == player) return 1; + for (unsigned i = 0; i <= depth; ++i) if (seen[i] == parent.actor) return 0; + if (depth + 1 < 4) seen[depth + 1] = parent.actor; + actor = parent.actor; + } + return 0; +} +static uint32_t equipment_flags(uint64_t actor) { + // This classification is reached only for a generation-checked actor attached to the player. + // A matching resource elsewhere in the world therefore cannot acquire this policy. + // Head armor is deliberately excluded: its cap can deform independently of the attachment. + char name[64]; + if (!read_at(field(actor, 0x20), name, sizeof name)) return 0; + name[sizeof name - 1] = 0; + return !strncmp(name, "Weapon_Sword_", 13) || !strncmp(name, "Weapon_Shield_", 14) + ? MOTION_OWNER_RIGID_EQUIPMENT : 0; +} +static int collect_models(uint64_t player, OwnerActor actor) { + uint64_t model_component = component(actor.actor, 8); + if (!model_component) return 1; + // Only decode the implementation whose getter directly returns its model collection. + if (field(ptr(model_component), 0xf8) != MOD_MAIN_BASE + 0x9d7160) return 1; + uint64_t collection = field(model_component, 0x18), list = field(collection, 0x28); + int32_t count; + if (!collection || !read_at(collection + 0x20, &count, 4) || count < 0 || count > 16 || (count && !list)) return 0; + for (int i = 0; i < count; ++i) { + uint64_t model = ptr(ptr(list + (uint64_t)i * 8)); + if (!model) continue; + // Match the model implementation used by the traced skeleton and shape binding path. + if (field(ptr(model), 0x40) != MOD_MAIN_BASE + 0x735430) continue; + if (O.count == OWNER_MODELS) return 0; + O.models[O.count++] = (MotionOwner){.player = player, .actor = actor.actor, .link = actor.link, + .model = model, .generation = actor.generation, .kind = actor.kind, + .flags = actor.kind ? equipment_flags(actor.actor) : 0}; + } + return field(model_component, 0x18) == collection && field(collection, 0x28) == list; +} +static void refresh(uint64_t frame, int force) { + if (!force && O.valid && O.frame == frame) return; + MotionOwner previous[OWNER_MODELS]; + unsigned nprevious = O.count; + memcpy(previous, O.models, nprevious * sizeof *previous); + O.frame = frame; + O.valid = 1; + O.count = 0; + uint64_t player = player_actor(); + if (!player) return; + OwnerActor actors[OWNER_ACTORS] = {{player, 0, 0, 0}}; + unsigned count = 1; + uint64_t equipment = component(player, 0x200); + if (equipment) { + // Eight static and thirteen dynamic slots, each a 24-byte actor link. + for (unsigned slot = 0; slot < 21; ++slot) { + uint64_t link = equipment + (slot < 8 ? 0x20 + slot * 24 : 0xe0 + (slot - 8) * 24); + OwnerActor a; + if (!actor_link(link, &a)) continue; + if (!parent_is_player(a.actor, player)) { ++O.model_bind_failures; continue; } + unsigned i; + for (i = 0; i < count && actors[i].actor != a.actor; ++i) {} + if (i != count) continue; + a.kind = slot + 1; + actors[count++] = a; + } + } + for (unsigned i = 0; i < count; ++i) + if (!collect_models(player, actors[i])) { O.count = 0; break; } + if (player_actor() != player || component(player, 0x200) != equipment) O.count = 0; + for (unsigned i = 0; i < O.count; ++i) { + MotionOwner *m = &O.models[i]; + for (unsigned j = 0; j < nprevious; ++j) { + const MotionOwner *p = &previous[j]; + if (m->player == p->player && m->actor == p->actor && m->link == p->link && + m->generation == p->generation && m->model == p->model) { + m->lifetime = p->lifetime; + break; + } + } + if (!m->lifetime) m->lifetime = ++O.serial; + } +} +static int same_owner(const MotionOwner *a, const MotionOwner *b) { + return a->player == b->player && a->actor == b->actor && a->link == b->link && + a->generation == b->generation && a->model == b->model && a->lifetime == b->lifetime; +} +void motion_owner_observe(Cpu *c, uint64_t address, uint64_t size) { + if (!motion_owner_enabled() || !address || !size || address > UINT64_MAX - size) return; + uint64_t lr = c->x[30] - MOD_MAIN_BASE; + uint64_t draw = 0, model; + int shape_query = 0; + // Both inline binding paths retain the shape object in their stack frame. + // Read that saved pointer rather than temporary registers reused during binding. + switch (lr) { + case 0x35e4950: case 0x35e4b34: case 0x35e4cec: + if (c->x[29] < 16) return; + draw = ptr(c->x[29] - 16); shape_query = lr == 0x35e4cec; break; + case 0x5e088c: case 0x5e0c38: + draw = ptr(c->sp + 0xb8); shape_query = lr == 0x5e0c38; break; + case 0x187b40: case 0x188184: + if (c->x[29] < 24) return; + draw = ptr(c->x[29] - 24); shape_query = lr == 0x188184; break; + default: return; + } + unsigned char header[0x20]; + if (!read_at(draw, header, sizeof header)) return; + memcpy(&model, header + 8, sizeof model); + if (!model) return; + uint16_t shape = 0; + memcpy(&shape, header + 0x18, sizeof shape); + if (shape_query && size < 256) return; + plat_mutex_lock(&owner_lock); + uint64_t frame = O.producer_frame; + refresh(frame, !O.count); + const MotionOwner *owner = NULL; + for (unsigned i = 0; i < O.count; ++i) if (O.models[i].model == model) { owner = &O.models[i]; break; } + if (owner) ++O.owned_queries; + else ++O.unknown_queries; + uint64_t part = shape_query ? (uint64_t)shape + 1 : 0; + MotionOwnerBinding *existing = NULL; + for (unsigned i = 0; i < O.nbindings; ++i) { + MotionOwnerBinding *b = &O.bindings[i]; + if (address >= b->address + b->size || b->address >= address + size) continue; + if (b->address == address && b->size == size) existing = b; + if (!owner || !same_owner(&b->owner, owner) || b->owner.part != part || b->buffer != c->x[0]) { + O.conflicts += !b->ambiguous; + b->ambiguous = 1; + } + } + // Never evict an entry during this producer frame: a collision must not erase an ambiguity. + if (owner && !existing && O.nbindings < MOTION_OWNER_BINDINGS) { + MotionOwnerBinding *b = &O.bindings[O.nbindings++]; + *b = (MotionOwnerBinding){.address = address, .size = size, .observed = frame, + .buffer = c->x[0], .owner = *owner}; + b->owner.part = part; + for (unsigned i = 0; i + 1 < O.nbindings; ++i) + if (O.bindings[i].address < address + size && address < O.bindings[i].address + O.bindings[i].size) + b->ambiguous = 1; + } else if (owner && !existing) ++O.capacity_misses; + plat_mutex_unlock(&owner_lock); +} +static int binding_address_order(const void *a, const void *b) { + uint64_t x = ((const MotionOwnerBinding *)a)->address, y = ((const MotionOwnerBinding *)b)->address; + return x < y ? -1 : x > y; +} + +static void capture_model_buffers(MotionOwnerSnapshot *out) { + for (unsigned i=0; i128) continue; + uint64_t shapes=field(owner.model,0x100); + for(unsigned shape=0;shape65536||!pool.mem||(uint64_t)b.off>pool.size||b.size>pool.size-(uint64_t)b.off) continue; + uint64_t address=(uint64_t)(uintptr_t)pool.mem+(uint64_t)b.off; + if(out->count==MOTION_OWNER_BINDINGS){++O.capacity_misses;return;} + owner.part=(uint64_t)shape+1; + out->bindings[out->count++]=(MotionOwnerBinding){.address=address,.size=b.size,.observed=O.producer_frame,.buffer=buffer,.owner=owner}; + } + } + } +} + +void motion_owner_capture(MotionOwnerSnapshot *out) { + memset(out, 0, sizeof *out); + if (!motion_owner_enabled()) return; + plat_mutex_lock(&owner_lock); + refresh(O.producer_frame, 1); + ++O.captures; + capture_model_buffers(out); + O.nbindings=out->count; + memcpy(O.bindings,out->bindings,out->count*sizeof *O.bindings); + if(O.captures%720==0) rt_log("motion direct: %u shape bindings from %u models",out->count,O.count); + plat_mutex_unlock(&owner_lock); + qsort(out->bindings, out->count, sizeof *out->bindings, binding_address_order); +} + +void motion_owner_present(void) { + if (!motion_owner_enabled()) return; + plat_mutex_lock(&owner_lock); + ++O.producer_frame; + if (O.producer_frame % 120 == 0) + rt_log("motion ownership: %u models, %llu captures, %llu owned/%llu other queries, " + "%llu conflicts, %llu capacity misses, %u attachment rejections", + O.count, (unsigned long long)O.captures, (unsigned long long)O.owned_queries, + (unsigned long long)O.unknown_queries, (unsigned long long)O.conflicts, + (unsigned long long)O.capacity_misses, O.model_bind_failures); + memcpy(O.retired, O.bindings, O.nbindings * sizeof *O.bindings); + O.nretired = O.nbindings; + O.nbindings = 0; + O.valid = 0; + plat_mutex_unlock(&owner_lock); +} + +void motion_owner_forget(uint64_t buffer) { + if (!motion_owner_enabled()) return; + plat_mutex_lock(&owner_lock); + for (unsigned age = 0; age < 2; ++age) { + MotionOwnerBinding *list = age ? O.retired : O.bindings; + unsigned count = age ? O.nretired : O.nbindings; + for (unsigned i = 0; i < count; ++i) { + MotionOwnerBinding *b = &list[i]; + if (b->buffer != buffer) continue; + b->ambiguous = 1; + for (unsigned j = 0; j < O.count; ++j) + if (O.models[j].model == b->owner.model) O.models[j].lifetime = ++O.serial; + } + } + plat_mutex_unlock(&owner_lock); +} + +void motion_owner_reset(void) { + if (!motion_owner_enabled()) return; + plat_mutex_lock(&owner_lock); + O.nbindings = O.nretired = O.count = 0; + O.valid = 0; + ++O.serial; + plat_mutex_unlock(&owner_lock); +} + +void motion_owner_forget_range(uint64_t address, uint64_t size) { + if (!motion_owner_enabled() || !size || address>UINT64_MAX-size) return; + plat_mutex_lock(&owner_lock); + for(unsigned i=0;iowner.model==O.models[i].model && b->addressaddress+b->size){hit=1;break;} + } + } + if(hit) O.models[i].lifetime=++O.serial; + } + plat_mutex_unlock(&owner_lock); +} diff --git a/runtime/motion_owner.h b/runtime/motion_owner.h new file mode 100644 index 00000000..6fe193b0 --- /dev/null +++ b/runtime/motion_owner.h @@ -0,0 +1,30 @@ +#pragma once +#include + +#define MOTION_OWNER_BINDINGS 512 +#define MOTION_OWNER_RIGID_EQUIPMENT 1u +typedef struct { + uint64_t player, actor, link, model, part, lifetime; + uint32_t generation, kind, flags; +} MotionOwner; +typedef struct { + uint64_t address, size, observed, buffer; + MotionOwner owner; + int ambiguous; +} MotionOwnerBinding; +typedef struct { + MotionOwnerBinding bindings[MOTION_OWNER_BINDINGS]; + unsigned count; +} MotionOwnerSnapshot; + +struct Cpu; +// Record the model responsible for an address query in a recognized model-binding call site. +void motion_owner_observe(struct Cpu *cpu, uint64_t address, uint64_t size); +// Capture identities alongside the submission's uniform bytes, before asynchronous replay. +void motion_owner_capture(MotionOwnerSnapshot *out); +void motion_owner_present(void); +void motion_owner_forget(uint64_t buffer); +void motion_owner_reset(void); +void motion_owner_forget_range(uint64_t address, uint64_t size); +int vk_usnap_motion_owner(uint64_t address, uint64_t size, MotionOwner *out); +int motion_owner_enabled(void); diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index 320d7160..2f443c3c 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -11,6 +11,7 @@ #include "cfg.h" #include "gfx.h" +#include "motion_owner.h" #include "host.h" #include "nvn_api.h" #include "nvn_pool_index.h" @@ -323,6 +324,7 @@ NVN_API(nvnMemoryPoolFinalize) { NVN_BLOCKING(); // as nvnMemoryPoolInitialize pool_t *o = arg_p(c, 0); if (o->magic != NVN_POOL_MAGIC) return; + motion_owner_forget_range((uint64_t)(uintptr_t)o->mem,o->size); if (pool_gpu_visible(o)) index_del(o); gfx_backend()->pool_fini(o); if (nvn_gpu_only((uintptr_t)o->mem)) gva_free((uintptr_t)o->mem); @@ -411,6 +413,7 @@ static int check_request(const pool_t *v, const nvn_mapping_t *r, int i) { NVN_API(nvnMemoryPoolMapVirtual) { NVN_BLOCKING(); // as nvnMemoryPoolInitialize pool_t *v = arg_p(c, 0); + motion_owner_forget_range((uint64_t)(uintptr_t)v->mem,v->size); int n = (int)arg_x(c, 1); const nvn_mapping_t *r = arg_p(c, 2); if (v->magic != NVN_POOL_MAGIC || !(v->flags & NVN_MEMORY_POOL_FLAGS_VIRTUAL) || !v->mem) { @@ -458,11 +461,12 @@ NVN_API(nvnBufferInitialize) { ret_x(c, 0); return; } + motion_owner_forget((uintptr_t)o); *o = (buf_t){BUF_MAGIC, s->pool, s->off, s->size}; ret_x(c, 1); } // void nvnBufferFinalize(NVNbuffer *b) -NVN_API(nvnBufferFinalize) { memset(arg_p(c, 0), 0, sizeof(buf_t)); } +NVN_API(nvnBufferFinalize) { motion_owner_forget((uintptr_t)arg_p(c, 0)); memset(arg_p(c, 0), 0, sizeof(buf_t)); } static uint64_t buf_address(const buf_t *o) { return o->pool && o->pool->mem ? (uintptr_t)o->pool->mem + (uint64_t)o->off : 0; } // The mapping of a buffer with no pool behind it (no buffer, or one whose nvnBufferInitialize failed): memory of its // own, zero at first, instead of address 0, since the game's scene-loading thread has been seen filling such a mapping @@ -496,7 +500,12 @@ NVN_API(nvnBufferMap) { ret_x(c, nvn_gpu_only(a) ? 0 : a); } // NVNbufferAddress nvnBufferGetAddress(const NVNbuffer *b) -NVN_API(nvnBufferGetAddress) { ret_x(c, buf_address(arg_p(c, 0))); } +NVN_API(nvnBufferGetAddress) { + const buf_t *b = arg_p(c, 0); + uint64_t address = buf_address(b); + + ret_x(c, address); +} // NVNmemoryPool *nvnBufferGetMemoryPool(const NVNbuffer *b) NVN_API(nvnBufferGetMemoryPool) { ret_x(c, (uintptr_t)((const buf_t *)arg_p(c, 0))->pool); } // ptrdiff_t nvnBufferGetMemoryOffset(const NVNbuffer *b) diff --git a/runtime/shaders/link_motion.frag b/runtime/shaders/link_motion.frag new file mode 100644 index 00000000..1fffd34c --- /dev/null +++ b/runtime/shaders/link_motion.frag @@ -0,0 +1,28 @@ +#version 450 +// vk_exec_draw.c patches this input to the previous-position output of the game's vertex shader. +layout(location = 15) in vec4 previous; +layout(binding = 1, std140) uniform Motion { + vec4 viewport; // size, current jitter + vec4 history; // previous jitter, previous projection's clip.w coefficients +}; +layout(binding = 2) uniform sampler2D previousDistance; +layout(location = 0) out vec2 motion; + +void main() { + vec2 previousWindow = (previous.xy / previous.w * vec2(0.5, -0.5) + 0.5) * viewport.xy; + vec2 dx = dFdx(previousWindow), dy = dFdy(previousWindow); + // A triangle seen from the other side last frame, or edge-on then, has no matching patch of visible surface. + float area = dx.x * dy.y - dx.y * dy.x; + vec2 at = previousWindow + history.xy; + float expect = abs((previous.w - history.w) / history.z); + vec2 slopeNow = vec2(dFdx(expect), dFdy(expect)); + if (!(previous.w > 0.0 && area > 0.05 && all(greaterThanEqual(at, vec2(0))) && all(lessThan(at, viewport.xy)))) discard; + + float visible = texelFetch(previousDistance, ivec2(at), 0).r; + // Account for subpixel sampling on sloped triangles. Cap the tolerance so a nearby occluder still wins. + vec2 slopePrev = vec2(dy.y * slopeNow.x - dx.y * slopeNow.y, + dx.x * slopeNow.y - dy.x * slopeNow.x) / area; + float tolerance = min(0.01 * expect, 0.0005 * expect + dot(abs(slopePrev), vec2(0.75))); + if (!(visible > 0.0 && abs(visible - expect) <= tolerance)) discard; + motion = previousWindow - (gl_FragCoord.xy - viewport.zw); +} diff --git a/runtime/vk.h b/runtime/vk.h index 5c858e9f..df72c861 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -948,3 +948,6 @@ typedef struct { uint64_t made, from_cache, compiled, uncached; } VkForeignPipes void vk_pipeline_foreign_counts(VkForeignPipes *o); #include "vk_rec.h" // last: its macros route the vkCmd* calls of every file that includes vk.h + +// Select the main scene view for selective animated motion. +int vk_fg_pose_prepare(uint64_t cam, VkImage depth); diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 1893f6fc..49f1fcd6 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -24,6 +24,7 @@ #include "cfg.h" #include "flight.h" #include "gfx.h" +#include "motion_owner.h" #include "host.h" #include "nvn_api.h" #include "perfmon.h" @@ -535,6 +536,7 @@ void gfx_exit_log(void) { } static void vk_be_present(const tex_t *t, const int crop[4], uint64_t at_ns) { + motion_owner_present(); vk_sync_start(); gfx_tl_present(); vk_dlssg_game_present(); diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 3d58bd67..769f7881 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -479,6 +479,9 @@ static struct { VkImage dist[2]; // each pixel's view distance, this and last frame (R32F, dlaa_mv.comp depth_out) VmaAllocation dist_mem[2]; VkImageView dist_view[2]; + uint64_t pose_distance_frame; + float pose_history[4]; + int pose_distance_valid; int dist_cur, dist_ok; // as chars_cur and chars_ok NgxHandle *feat; uint64_t used; // evals count when it last ran @@ -598,6 +601,15 @@ int vk_dlaa_fg_views(VkImageView *depth, VkImageView *mv) { return D.fg_depth_view && D.fg_mv_view; } +int vk_dlaa_pose_depth(uint64_t frame, uint32_t w, uint32_t h, VkDescriptorImageInfo *image, float history[4]) { + const struct Size *last = D.last; + if (!last || !last->pose_distance_valid || !last->dist_ok || last->w != w || last->h != h || + !frame || last->pose_distance_frame != frame - 1 || fabsf(last->pose_history[2]) < 1e-6f) return 0; + *image = (VkDescriptorImageInfo){D.mv_samp, last->dist_view[last->dist_cur ^ 1], VK_IMAGE_LAYOUT_GENERAL}; + memcpy(history, last->pose_history, sizeof last->pose_history); + return 1; +} + int vk_dlaa_fg_camera(VkDlaaCamera *c) { if (!D.fg_cam_ok) return 0; *c = D.fg_cam; @@ -1251,7 +1263,10 @@ static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float j vk_mvcheck_camera(cb, e->mv, w, h, jx, jy); // NX2NAVI_MV_CHECK (test) // the characters' own motion over the camera's (vk_exec_draw.c, per-object motion) uint64_t c0 = T.on > 0 ? kern_now_ns() : 0; - if (D.cam_size == CAM_BLOCK && vk_mvobj_execute(ex, D.cam_addr, e->mv_view, zv, D.depth.format, w, h)) full_barrier(cb); + int object_motion = D.cam_size == CAM_BLOCK; + if (D.cam_size == OCAM_BLOCK) + object_motion = vk_fg_pose_prepare(D.cam_addr, D.depth.image) && !D.reset; + if (object_motion && vk_mvobj_execute(ex, D.cam_addr, e->mv_view, zv, D.depth.format, w, h)) full_barrier(cb); if (T.on > 0) T.cpu_mv += kern_now_ns() - c0; times_mark(cb, T_OBJ); if (T.on > 0) T.used[T.slot] = 1; // the DLSS marks follow in evaluate @@ -1260,6 +1275,11 @@ static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float j if (use) e->chars_cur ^= 1, e->chars_ok = 1, D.masked++; else e->chars_ok = 0; if (zfit && bias > 0) D.masked++; + e->pose_distance_valid = zfit && D.fg_cam_ok; + e->pose_distance_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + e->pose_history[0] = jx, e->pose_history[1] = jy; + e->pose_history[2] = D.fg_cam.view_to_clip[11]; + e->pose_history[3] = D.fg_cam.view_to_clip[15]; e->dist_cur ^= 1, e->dist_ok = zfit; *masked = use || (zfit && bias > 0); return 1; diff --git a/runtime/vk_dlaa.h b/runtime/vk_dlaa.h index 7f769894..7be7f775 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -95,3 +95,5 @@ void vk_mvcheck_output(VkCommandBuffer cb, VkImage out, uint32_t ow, uint32_t oh // vk_exec_draw.c, once a draw's fragment shader is known: 1 skips the draw (the game's FXAA while NX2NAVI_FXAA is 0, its // scaling draw when DLSS wrote the scaled picture). int vk_dlaa_skip_draw(VkExec *ex, const GfxShader *fs); + +int vk_dlaa_pose_depth(uint64_t frame, uint32_t w, uint32_t h, VkDescriptorImageInfo *image, float history[4]); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 7f98b92f..075f0915 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -580,7 +580,9 @@ static void twins_release(VkCtx *c); static void mesh_ring_release(VkCtx *c); static void pool_marks_release(VkCtx *c); +static void pose_shutdown(void); void vk_exec_draw_shutdown(void) { + pose_shutdown(); vk_meshfb_shutdown(); if (vk_ctx()) mesh_ring_release(vk_ctx()), pool_marks_release(vk_ctx()); // made without the stand-ins below too if (!Z.ready) return; @@ -3743,7 +3745,7 @@ static void bind_pipeline(VkExec *ex, VkPipelineBindPoint bp, VkPipeline p) { if (!k) cx->B.st_valid = 0; } -static VkViewport draw_viewport(VkExec *ex) { +static VkViewport base_viewport(VkExec *ex) { float x = 0, y = 0, w = (float)ex->pass_w, h = (float)ex->pass_h; if (ex->has_vp) { float k = ex->pass_scale > 1 ? (float)ex->pass_scale : 1.0f; @@ -3752,6 +3754,11 @@ static VkViewport draw_viewport(VkExec *ex) { if (w < 1) w = 1; if (h < 1) h = 1; VkViewport v = {x, y + h, w, -h, ex->depth_range[0], ex->depth_range[1]}; // the y flip: negative height + return v; +} + +static VkViewport draw_viewport(VkExec *ex) { + VkViewport v = base_viewport(ex); vk_dlaa_viewport(ex, &v); // the scene's draws move by the DLAA jitter return v; } @@ -4531,10 +4538,13 @@ void vk_exec_ahead_op(VkExec *sx, uint32_t op, const void *payload) { // building, or whose last-frame blocks are not in the snapshot, keeps the camera's motion. NX2NAVI_DLAA_OBJMV=0 turns it // off (vk_mvprev.c). #include "mvobj_fs_spv.h" +#include "link_motion_spv.h" static const char MO_FS_RES[] = "{\"key\":\"mvobj\",\"stage\":\"ps\",\"notes\":[],\"cbufs\":[{\"binding\":1,\"slot\":3}]," "\"sbufs\":[],\"textures\":[],\"images\":[]}"; +static const char LINK_FS_RES[] = "{\"key\":\"link-motion\",\"stage\":\"ps\",\"cbufs\":[{\"binding\":1,\"slot\":3}]," + "\"textures\":[{\"binding\":2,\"type\":3}],\"sbufs\":[],\"images\":[]}"; static struct { - const GfxShader *fs[16]; // per input Location + const GfxShader *fs[16], *link_fs[16]; // per input Location int failed; uint64_t cand, still, draws, nodata, building, ran, dropped; uint64_t ns_check, ns_rec, n_rec, ns_emit, ns_still; @@ -4564,7 +4574,8 @@ typedef struct { VkIndexType it; uint32_t count, instances, base_instance; int32_t base_vertex; - uint64_t cam; // the draw's camera block (c4), this frame's + uint64_t cam; // the draw's camera block, this frame's + int pose_index, pose_view; } MoDraw; typedef struct { VkBuffer b[17]; VkDeviceSize o[17], sz[17], str[17]; } MoVb; static struct { @@ -4580,7 +4591,7 @@ static struct { uint64_t vgen; float sup_in[3]; // the support buffer last filled for the list and its inputs VkDescriptorBufferInfo sup; - float par_in[4]; // the fragment shader's parameters last written and their inputs + float par_in[8]; // the fragment shader's parameters last written and their inputs VkDescriptorBufferInfo par; VkDescriptorSetLayout aset_layout[2]; // the last allocated set per set number, with its descriptors VkDescriptorSet aset[2]; @@ -4644,6 +4655,22 @@ static const GfxShader *mo_fs(int loc) { return MO.fs[loc]; } +static const GfxShader *link_motion_fs(int loc) { + if (loc < 0 || loc >= 16) return NULL; + if (MO.link_fs[loc]) return MO.link_fs[loc]; + uint32_t words[sizeof link_motion_spv / sizeof(uint32_t)]; + memcpy(words, link_motion_spv, sizeof words); + for (size_t i = 5; i < sizeof words / sizeof *words; i += words[i] >> 16) { + // OpDecorate Location 15 uniquely identifies the previous clip position in link_motion.frag. + if (words[i] == ((4u << 16) | 71u) && words[i + 2] == 30 && words[i + 3] == 15) { + words[i + 3] = (uint32_t)loc; + MO.link_fs[loc] = gfx_shader_own(words, sizeof words / sizeof *words, LINK_FS_RES); + break; + } + } + return MO.link_fs[loc]; +} + // The write of binding b from its packed descriptors. static VkWriteDescriptorSet mo_write(const VkPipeBinding *b, const VkDescSlot *d) { VkWriteDescriptorSet w = {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = b->binding, @@ -4661,7 +4688,7 @@ static VkWriteDescriptorSet mo_write(const VkPipeBinding *b, const VkDescSlot *d // mo_ovr), the support buffer and sampled textures. An allocated set is written here (or the last one reused when it // would hold the same); a pushed set is pushed by mo_emit. 0 for a pipeline with storage buffers, images or texel // buffers (not drawn again). -static int mo_desc(VkExec *ex, const VkPipeInfo *info, MoDraw *q) { +static int mo_desc(VkExec *ex, const VkPipeInfo *info, MoDraw *q, const VkDescriptorImageInfo *pose_depth) { if (info->n_bindings > MAX_BINDINGS || info->pool) return 0; uint32_t total = 0; for (uint32_t i = 0; i < info->n_bindings; i++) total += info->bindings[i].count ? info->bindings[i].count : 1; @@ -4692,6 +4719,9 @@ static int mo_desc(VkExec *ex, const VkPipeInfo *info, MoDraw *q) { for (uint32_t k = 0; k < cnt; k++) o[k].buffer = ubo_info(ex, st, r->slot + (int)k); } else if (b->kind == GFX_RES_SSBO && !(r->flags & 1)) { // read only: instance data of foliage and such for (uint32_t k = 0; k < cnt; k++) o[k].buffer = ssbo_info(ex, st, r, 0); + } else if (pose_depth && st == NVN_STAGE_FS && b->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { + if (cnt != 1) return 0; + o->image = *pose_depth; } else if (b->kind == GFX_RES_TEX && b->type != VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) { vk_texture_lock(); for (uint32_t k = 0; k < cnt; k++) { @@ -4819,9 +4849,11 @@ static void mo_emit(VkCommandBuffer cb) { } } -static int mvobj_pick(VkExec *ex, const GfxShader **vs_out, uint32_t *banks_out, uint64_t t0); +#include "vk_fg_pose.inc" + +static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, uint32_t *banks_out, MotionOwner *owner_out, uint64_t t0); static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSize ib_off, uint64_t t0, - const GfxShader *vs, uint32_t banks); + const GfxShader *vs, uint32_t banks, const MotionOwner *owner); // The chunks of a parallel group (vk_exec.c) add to the frame's list at once: whether a draw is listed at all is decided // by its own draw context (mvobj_pick, its memo per context), and only the frame's list, its memos and statistics go // under mo_lock there: under one lock for the whole check, the chunks' scene draws waited for each other. The order of @@ -4830,16 +4862,20 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi static PlatMutex mo_lock = PLAT_MUTEX_INIT; static void mvobj_draw(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSize ib_off) { - if (MO.failed || !vk_mvprev_on() || !ex->dc->B.st_valid) return; + if (MO.failed || !ex->dc->B.st_valid) return; + if (RT_V111) { + if (!vk_dlaa_available() || !pose_enabled()) return; + } else if (!vk_mvprev_on()) return; static int timed = -1; if (timed < 0) timed = rt_knob("MVOBJ_TIMES", "0"); // NX2NAVI_MVOBJ_TIMES=1: the summary's times (the clock reads cost) uint64_t t0 = timed ? kern_now_ns() : 0; const GfxShader *vs; uint32_t banks; - int listed = mvobj_pick(ex, &vs, &banks, t0); + MotionOwner owner = {0}; + int listed = mvobj_pick(ex, d, &vs, &banks, &owner, t0); int lk = listed && vk_exec_shared(); if (lk) plat_mutex_lock(&mo_lock); - if (listed) mvobj_draw_(ex, d, ib, ib_off, t0, vs, banks); + if (listed) mvobj_draw_(ex, d, ib, ib_off, t0, vs, banks, &owner); if (t0) __atomic_add_fetch(&MO.ns_check, kern_now_ns() - t0, __ATOMIC_RELAXED); if (lk) plat_mutex_unlock(&mo_lock); } @@ -4966,9 +5002,47 @@ static int mo_prev_cached(int index, const p_bind_buf *u, VkDescriptorBufferInfo return r; } +static int mo_legacy_previous(VkExec *ex, uint64_t frame, uint32_t banks, uint64_t t0, uint64_t tp, + VkDescriptorBufferInfo prevb[17], uint32_t *have_out, uint32_t *changed_mask) { + static struct { uint64_t frame, key; int moving; VkDescriptorBufferInfo b[17]; uint32_t have; } memo[8192]; + uint64_t key = banks; + for (int b = 3; b <= 16; b++) + if (banks >> b & 1 && b != 4) key = (key ^ ex->ubo[NVN_STAGE_VS][b - 3].addr) * 0x100000001b3ull; + uint32_t mi = (uint32_t)(key * 0xff51afd7ed558ccdull >> 51); + uint32_t have = 0; + if (memo[mi].frame == frame && memo[mi].key == key) { + if (!memo[mi].moving) { + if (t0) MO.ns_still += kern_now_ns() - tp; + MO.still++; + return 0; + } + memcpy(prevb, memo[mi].b, sizeof memo[mi].b), have = memo[mi].have; + } else { + MO.misses++; + int moving = 0; + for (int b = 3; b <= 16; b++) { + if (!(banks >> b & 1) || b == 4) continue; + const p_bind_buf *u = &ex->ubo[NVN_STAGE_VS][b - 3]; + int changed; + if (u->addr && u->size && mo_prev_cached(b - 3, u, &prevb[b], &changed)) + have |= 1u << b, moving |= changed << b; // the banks whose block changed + } + memo[mi].frame = frame, memo[mi].key = key, memo[mi].moving = moving; + memcpy(memo[mi].b, prevb, sizeof memo[mi].b), memo[mi].have = have; + if (!moving) { + if (t0) MO.ns_still += kern_now_ns() - tp; + MO.still++; + return 0; + } + } + *have_out = have; + *changed_mask = (uint32_t)memo[mi].moving; + return 1; +} + // Whether the draw goes on to the frame's list (mvobj_draw_), with its motion variant and the banks its position reads; // this draw context alone decides (no lock in a chunk): the draw's state, and the variant memo per context. -static int mvobj_pick(VkExec *ex, const GfxShader **vs_out, uint32_t *banks_out, uint64_t t0) { +static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, uint32_t *banks_out, MotionOwner *owner_out, uint64_t t0) { DrawCtx *const cx = ex->dc; if (!vk_exec_in_chunk()) mvobj_banks(ex); // a test's log of every scene draw (its tables are not shared) const VkExecTarget *z = &ex->depth; @@ -4977,7 +5051,21 @@ static int mvobj_pick(VkExec *ex, const GfxShader **vs_out, uint32_t *banks_out, return 0; // solid geometry only, which tests and writes the scene depth: effects, particles and screen quads write none, and // drawn again they would put their motion on the surfaces behind them (DLSS takes those from the surface) - if (!cx->B.st.ds.dt || !cx->B.st.ds.dw) return 0; + if (!cx->B.st.ds.dt) return 0; + if (!cx->B.st.ds.dw && !(RT_V111 && cx->B.st.ds.df == NVN_DEPTH_FUNC_EQUAL && ex->color[0].image && + (vk_pipeline_target_wmask(&cx->B.st, 0) & 7))) return 0; + // An opaque colour pass can use EQUAL against a depth prepass. Its geometry is already in the scene depth. + if (RT_V111) { + VkViewport v = base_viewport(ex); + if (ex->ubo[0][0].size != 0x3500 || d->instances > 1 || d->base_instance || cx->B.st.color.blend_en || cx->B.st.ds.st || + ex->pass_layers != 1 || z->level || z->base_layer || z->layers != 1 || + v.x != 0.0f || v.y != (float)z->height || + v.width != (float)z->width || v.height != -(float)z->height) return 0; + if (!pose_owner(ex, owner_out)) { + __atomic_add_fetch(&pose_unowned, 1, __ATOMIC_RELAXED); + return 0; + } + } // a vertex shader that reads a user bank besides the camera's (c4) may move on its own const GfxShaderRes *ur; int nu = gfx_shader_resources(cx->B.st.vs, GFX_RES_UBO, &ur), reads = 0; @@ -5008,7 +5096,7 @@ static int mvobj_pick(VkExec *ex, const GfxShader **vs_out, uint32_t *banks_out, uint32_t extra = gfx_shader_outputs(v, io) & ~gfx_shader_outputs(cx->B.st.vs, io); uint32_t banks = extra ? gfx_shader_mvprev_banks(v) & 0x1fff8u : 0; vmemo[vi].of = cx->B.st.vs, vmemo[vi].vs = v, vmemo[vi].banks = banks; - vk_mvprev_want((banks & ~(1u << 4)) >> 3); // binding index = bank - 3 + if (!RT_V111) vk_mvprev_want((banks & ~(1u << 4)) >> 3); // binding index = bank - 3 } const GfxShader *vs = vmemo[vi].vs; uint32_t banks = vmemo[vi].banks; @@ -5055,7 +5143,7 @@ static int mvobj_pick(VkExec *ex, const GfxShader **vs_out, uint32_t *banks_out, // A draw mvobj_pick took, onto the frame's list (in a chunk under mo_lock): the list begins at the first such draw of // the frame's submission (mo_emit drops a list of another frame, submission or depth format). static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSize ib_off, uint64_t t0, - const GfxShader *vs, uint32_t banks) { + const GfxShader *vs, uint32_t banks, const MotionOwner *owner) { DrawCtx *const cx = ex->dc; const VkExecTarget *z = &ex->depth; uint64_t tp = t0 ? kern_now_ns() : 0; @@ -5063,40 +5151,41 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi if (MQ.frame != frame || MQ.fence != ex->fence || MQ.depth_fmt != z->format) mo_begin(ex, frame); mo_mask_note(banks, cx->B.st.vs); // last frame's camera and blocks; held still when none of the position's blocks changed (most of the scenery) - VkDescriptorBufferInfo cam, prevb[17]; + PoseRange *pose = RT_V111 ? pose_match(ex, d, owner) : NULL; + if (RT_V111 && !pose) return; + VkDescriptorBufferInfo cam = {0}, prevb[17] = {0}; + uint32_t have = 0, changed = 0; + const ActorPose *previous_pose = NULL; + ActorPose *pose_view = NULL; + VkDescriptorImageInfo pose_depth = {0}; + int pose_visibility_guard = 0; + float history[4] = {0}; float jit[2]; - if (!vk_dlaa_prev_camera(&cam, jit)) { - MO.nodata++, MO.why[0]++; - return; - } - static struct { uint64_t frame, key; int moving; VkDescriptorBufferInfo b[17]; uint32_t have; } memo[8192]; - uint64_t key = banks; - for (int b = 3; b <= 16; b++) - if (banks >> b & 1 && b != 4) key = (key ^ ex->ubo[NVN_STAGE_VS][b - 3].addr) * 0x100000001b3ull; - uint32_t mi = (uint32_t)(key * 0xff51afd7ed558ccdull >> 51); - uint32_t have = 0; - if (memo[mi].frame == frame && memo[mi].key == key) { - if (!memo[mi].moving) { - if (t0) MO.ns_still += kern_now_ns() - tp; - return (void)MO.still++; + if (pose) { + const uint32_t required = (1u << 3) | (1u << 4) | (1u << 6); + if ((banks & required) != required || (banks & ~0xf8u)) return; + const GfxShaderRes *resources; + if (gfx_shader_resources(vs, GFX_RES_SSBO, &resources) || gfx_shader_resources(vs, GFX_RES_TEX, &resources) || + gfx_shader_resources(vs, GFX_RES_IMG, &resources)) return; + previous_pose = pose_previous(pose, ex, d, frame, banks, &pose_view); + if (!previous_pose) return; + pose_visibility_guard = pose_guard_needed(pose, pose_view, previous_pose); + if (pose_visibility_guard && !vk_dlaa_pose_depth(frame, z->width, z->height, &pose_depth, history)) { + // Missing visibility evidence is not evidence that this valid pose vector is wrong. + // Keep the raw vector, with the normal current-depth and history-validity checks. + pose_view->guard_missing_depth = 1; + pose_visibility_guard = 0; } - memcpy(prevb, memo[mi].b, sizeof prevb), have = memo[mi].have; + pose_view->visibility_guard = pose_visibility_guard; + VkViewport v = draw_viewport(ex); + jit[0] = v.x; + jit[1] = v.y - (float)z->height; } else { - MO.misses++; - int moving = 0; - for (int b = 3; b <= 16; b++) { - if (!(banks >> b & 1) || b == 4) continue; - const p_bind_buf *u = &ex->ubo[NVN_STAGE_VS][b - 3]; - int changed; - if (u->addr && u->size && mo_prev_cached(b - 3, u, &prevb[b], &changed)) - have |= 1u << b, moving |= changed << b; // the banks whose block changed - } - memo[mi].frame = frame, memo[mi].key = key, memo[mi].moving = moving; - memcpy(memo[mi].b, prevb, sizeof prevb), memo[mi].have = have; - if (!moving) { - if (t0) MO.ns_still += kern_now_ns() - tp; - return (void)MO.still++; + if (!vk_dlaa_prev_camera(&cam, jit)) { + MO.nodata++, MO.why[0]++; + return; } + if (!mo_legacy_previous(ex, frame, banks, t0, tp, prevb, &have, &changed)) return; } if (t0) MO.ns_part[1] += kern_now_ns() - tp, MO.n_part[1]++, tp = kern_now_ns(); GfxShaderIo io[32]; @@ -5106,7 +5195,8 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi MO.storage++; return; } - const GfxShader *fs = mo_fs(31 - __builtin_clz(extra)); + int loc = 31 - __builtin_clz(extra); + const GfxShader *fs = pose_visibility_guard ? link_motion_fs(loc) : mo_fs(loc); if (!fs) { MO.no_fs++; return; @@ -5124,7 +5214,7 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi static int eq = -1; if (eq < 0) eq = rt_knob("MVOBJ_EQUAL", "0"); int df = cx->B.st.ds.df; - df = eq ? NVN_DEPTH_FUNC_EQUAL : df == NVN_DEPTH_FUNC_LESS ? NVN_DEPTH_FUNC_LEQUAL + df = (eq || pose) ? NVN_DEPTH_FUNC_EQUAL : df == NVN_DEPTH_FUNC_LESS ? NVN_DEPTH_FUNC_LEQUAL : df == NVN_DEPTH_FUNC_GREATER ? NVN_DEPTH_FUNC_GEQUAL : df == NVN_DEPTH_FUNC_LEQUAL || df == NVN_DEPTH_FUNC_GEQUAL ? df : NVN_DEPTH_FUNC_EQUAL; ms.ds.dt = 1, ms.ds.dw = 0, ms.ds.df = (uint64_t)df, ms.ds.st = 0; @@ -5165,20 +5255,31 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi // into the frame's list, past the primary's binding caches (mo_desc); mo_emit records it MQ.d = mo_grow(MQ.d, &MQ.cap, MQ.n + 1, sizeof *MQ.d); MoDraw *q = &MQ.d[MQ.n]; - float par_in[4] = {(float)z->width, (float)z->height, jit[0], jit[1]}; + float par_in[8] = {(float)z->width, (float)z->height, jit[0], jit[1], history[0], history[1], history[2], history[3]}; if (!MQ.par.buffer || memcmp(par_in, MQ.par_in, sizeof par_in)) { VkBuffer pbuf; VkDeviceSize poff; - memcpy(scratch(ex, 16, &pbuf, &poff), par_in, sizeof par_in); - MQ.par = (VkDescriptorBufferInfo){pbuf, poff, 16}; + memcpy(scratch(ex, sizeof par_in, &pbuf, &poff), par_in, sizeof par_in); + MQ.par = (VkDescriptorBufferInfo){pbuf, poff, sizeof par_in}; memcpy(MQ.par_in, par_in, sizeof par_in); } memset(cx->mo_ovr, 0, sizeof cx->mo_ovr); - for (int bk = 3; bk <= 16; bk++) // last frame's block, or this frame's where none was found - cx->mo_ovr[NVN_STAGE_VS][bk + 15] = bk == 4 ? cam : have >> bk & 1 ? prevb[bk] : ubo_info(ex, NVN_STAGE_VS, bk); + if (previous_pose) { + const uint8_t *data = previous_pose->bytes; + for (int i = 0; i < POSE_BANKS; ++i) { + if (!previous_pose->size[i]) continue; + VkDescriptorBufferInfo *b = &cx->mo_ovr[NVN_STAGE_VS][18 + i]; + b->range = previous_pose->size[i]; + memcpy(scratch(ex, b->range, &b->buffer, &b->offset), data, b->range); + data += b->range; + } + } else { + for (int bk = 3; bk <= 16; bk++) + cx->mo_ovr[NVN_STAGE_VS][bk + 15] = bk == 4 ? cam : (have >> bk & 1) ? prevb[bk] : ubo_info(ex, NVN_STAGE_VS, bk); + } cx->mo_ovr[NVN_STAGE_FS][3] = MQ.par; cx->mo_ovr_on = 1; - int bound = mo_desc(ex, info, q); + int bound = mo_desc(ex, info, q, pose_visibility_guard ? &pose_depth : NULL); cx->mo_ovr_on = 0; if (!bound) { MO.storage++; @@ -5197,17 +5298,25 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi q->it = d->itype == NVN_INDEX_TYPE_UNSIGNED_SHORT ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32; q->count = (uint32_t)d->count, q->instances = d->instances > 0 ? (uint32_t)d->instances : 1; q->base_vertex = d->base_vertex, q->base_instance = (uint32_t)d->base_instance; - q->cam = ex->ubo[NVN_STAGE_VS][1].addr; + q->cam = ex->ubo[NVN_STAGE_VS][pose ? 0 : 1].addr; + q->pose_index = pose ? (int)(pose - pose_ranges) + 1 : 0; + q->pose_view = pose ? (int)(pose_view - pose->history[frame & 1].view) : 0; MQ.n++; MO.draws++; for (int i = 0; i < 16; i++) // the frame's draws per position banks and changed banks (vk_mvobj_frame_masks) - if (!MF[i].n || (MF[i].mask == banks && MF[i].chg == (uint32_t)memo[mi].moving)) { - MF[i].mask = banks, MF[i].chg = (uint32_t)memo[mi].moving, MF[i].n++; + if (!MF[i].n || (MF[i].mask == banks && MF[i].chg == changed)) { + MF[i].mask = banks, MF[i].chg = changed, MF[i].n++; break; } if (t0) MO.ns_rec += kern_now_ns() - t1, MO.n_rec++; } +int vk_fg_pose_prepare(uint64_t cam, VkImage depth) { + if (!RT_V111 || !pose_enabled()) return 0; + pose_select(__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), cam, depth); + return MQ.n != 0; +} + int vk_mvobj_execute(VkExec *ex, uint64_t cam, VkImageView mv, VkImageView depth, VkFormat depth_fmt, uint32_t w, uint32_t h) { static uint64_t frames; @@ -5278,7 +5387,11 @@ int vk_mvobj_execute(VkExec *ex, uint64_t cam, VkImageView mv, VkImageView depth // a draw of another view would move by the difference between the two views int kept = 0; for (int i = 0; i < MQ.n; i++) - if (MQ.d[i].cam == cam) MQ.d[kept++] = MQ.d[i]; + if (MQ.d[i].cam == cam && (!MQ.d[i].pose_index || + pose_ranges[MQ.d[i].pose_index - 1].history[frame & 1].view[MQ.d[i].pose_view].selected)) { + if (MQ.d[i].pose_index) ++pose_written[pose_ranges[MQ.d[i].pose_index - 1].actor]; + MQ.d[kept++] = MQ.d[i]; + } else MO.other_cam++; MQ.n = kept; if (!MQ.n) { diff --git a/runtime/vk_fg_pose.inc b/runtime/vk_fg_pose.inc new file mode 100644 index 00000000..a08f05ea --- /dev/null +++ b/runtime/vk_fg_pose.inc @@ -0,0 +1,276 @@ +// Final submitted pose inputs for owned player models. Replay workers change history under +// the motion-list lock; the submitting thread selects and consumes it after the group finishes. +// Model and shape identities come from producer-side ownership records, never from a mesh fingerprint. +#include "motion_owner.h" +#define POSE_ROWS 256 +#define POSE_BYTES (128u * 1024u) +#define POSE_BANKS 5 // c3 through c7 +#define POSE_VIEWS 8 +typedef struct { + uint64_t frame, cam, indices; + VkImage depth; + const GfxShader *shader; + NVNbufferRange vertex[16]; + uint64_t bindings[POSE_BANKS]; + uint32_t size[POSE_BANKS], capacity; + unsigned char *bytes; + uint32_t width, height; + int seen, selected, ambiguous; + int visibility_guard, guard_missing_depth; +} ActorPose; +typedef struct { + ActorPose view[POSE_VIEWS]; + ActorPose *scene; +} PoseFrame; +typedef struct { + uint32_t count, width, base, actor; + uint64_t indices, last_frame; + MotionOwner owner; + PoseFrame history[2]; +} PoseRange; +static PoseRange pose_ranges[POSE_ROWS]; +static int pose_count; +static unsigned pose_seen[3], pose_written[3], pose_missing, pose_full, pose_unowned; + +static int pose_enabled(void) { return motion_owner_enabled(); } +static int pose_raw(void) { + static int raw = -1; + if (raw < 0) raw = rt_knob("SELECTIVE_MOTION_RAW", "0"); + return raw; +} + +static int pose_same_owner(const MotionOwner *a, const MotionOwner *b) { + return a->player == b->player && a->actor == b->actor && a->link == b->link && + a->generation == b->generation && a->model == b->model && a->part == b->part && a->lifetime == b->lifetime && + a->flags == b->flags; +} + +static void pose_clear(PoseRange *r) { + for (int j = 0; j < 2; ++j) + for (int k = 0; k < POSE_VIEWS; ++k) free(r->history[j].view[k].bytes); + memset(r, 0, sizeof *r); +} + +static int pose_owner(VkExec *ex, MotionOwner *out) { + MotionOwner owner = {0}; + // Only vertex resources consumed by this shader can establish identity. Stale unused bindings cannot. + const GfxShaderRes *res; + int nres = gfx_shader_resources(ex->dc->B.st.vs, GFX_RES_UBO, &res); + for (int j = 0; j < nres; ++j) { + int bank = res[j].slot; + if (bank < 3 || bank > 7) continue; + const p_bind_buf *b = &ex->ubo[NVN_STAGE_VS][bank - 3]; + MotionOwner m; + if (!vk_usnap_motion_owner(b->addr, b->size, &m)) continue; + if (owner.model && (owner.model != m.model || owner.lifetime != m.lifetime || + (owner.part && m.part && owner.part != m.part))) return 0; + if (!owner.model || m.part) owner = m; + } + // The per-shape record is required even for skinned meshes; one palette can serve several pieces. + if (!owner.model || !owner.part) return 0; + *out = owner; + return 1; +} + +// The caller holds the motion-list lock while parallel replay workers record draws. +static PoseRange *pose_match(VkExec *ex, const p_draw_idx *d, const MotionOwner *identity) { + MotionOwner owner = *identity; + uint64_t frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + uint32_t width = d->itype == NVN_INDEX_TYPE_UNSIGNED_SHORT ? 2u : 4u; + PoseRange *free_row = NULL; + for (int i = 0; i < pose_count; ++i) { + PoseRange *r = &pose_ranges[i]; + if (pose_same_owner(&r->owner, &owner) && r->indices == d->ibuf && + r->count == (uint32_t)d->count && r->width == width && r->base == (uint32_t)d->base_vertex) { + r->last_frame = frame; + ++pose_seen[r->actor]; + return r; + } + if (!free_row && r->last_frame + 2 < frame) free_row = r; + } + if (pose_count < POSE_ROWS) free_row = &pose_ranges[pose_count++]; + if (!free_row) { ++pose_full; return NULL; } + pose_clear(free_row); + free_row->owner = owner; + free_row->indices = d->ibuf; + free_row->count = (uint32_t)d->count; + free_row->width = width; + free_row->base = (uint32_t)d->base_vertex; + free_row->actor = owner.kind ? 2 : 1; + free_row->last_frame = frame; + ++pose_seen[free_row->actor]; + return free_row; +} + +static ActorPose *pose_previous(PoseRange *r, VkExec *ex, const p_draw_idx *d, uint64_t frame, uint32_t banks, + ActorPose **candidate) { + PoseFrame *now = &r->history[frame & 1]; + ActorPose *prev = r->history[(frame & 1) ^ 1].scene; + uint64_t cam = ex->ubo[NVN_STAGE_VS][0].addr; + // The same mesh is drawn into several depth targets. Only the resolve knows which view supplies FG. + ActorPose *cur = NULL; + for (int i = 0; i < POSE_VIEWS; ++i) { + ActorPose *p = &now->view[i]; + if (!p->seen || p->frame != frame || (p->cam == cam && p->depth == ex->depth.image)) { + cur = p; + break; + } + } + *candidate = cur; + if (!cur) return NULL; + int repeated = cur->seen && cur->frame == frame; + if (repeated && cur->ambiguous) return NULL; + if (!repeated) { + cur->seen = 1; + cur->frame = frame; + cur->selected = cur->ambiguous = 0; + cur->visibility_guard = cur->guard_missing_depth = 0; + cur->cam = cam; + cur->depth = ex->depth.image; + cur->width = ex->depth.width; + cur->height = ex->depth.height; + } + + // Unused bindings retain the previous draw's values. Only the shader's vertex inputs identify this mesh. + NVNbufferRange vertex[16] = {0}; + uint32_t inputs = gfx_shader_inputs(ex->dc->B.st.vs, NULL); + for (int i = 0; i < 16; ++i) { + va_st a; + memcpy(&a, &ex->dc->B.st.vattr[i], sizeof a); + if ((inputs >> i & 1) && a.fmt && a.stream < 16) vertex[a.stream] = ex->vbuf[a.stream]; + } + uint32_t size[POSE_BANKS] = {0}, n = 0; + uint64_t bindings[POSE_BANKS] = {0}; + const uint8_t *part[POSE_BANKS] = {0}; + for (int i = 0; i < POSE_BANKS; ++i) { + if (!(banks & (1u << (i + 3)))) continue; + const p_bind_buf *u = &ex->ubo[NVN_STAGE_VS][i]; + VkDescriptorBufferInfo snap; + if (!u->size || u->size > LIM.ubo_max || u->size > POSE_BYTES - n || + !vk_usnap_find(u->addr, u->size, LIM.ubo_align, &snap)) { + cur->ambiguous = 1; + ++pose_missing; + return NULL; + } + part[i] = vk_usnap_map() + snap.offset; + size[i] = (uint32_t)u->size; + bindings[i] = u->addr; + n += size[i]; + } + if (repeated) { + // The same instance can be submitted again in the same pass. A second instance of this mesh + // can use another palette, or rewrite a palette at the same address between submissions. + cur->ambiguous = cur->depth != ex->depth.image || cur->indices != d->ibuf || + memcmp(vertex, cur->vertex, sizeof vertex) || memcmp(size, cur->size, sizeof size) || + memcmp(bindings, cur->bindings, sizeof bindings); + uint32_t offset = 0; + for (int i = 0; !cur->ambiguous && i < POSE_BANKS; ++i) { + if (size[i] && memcmp(cur->bytes + offset, part[i], size[i])) cur->ambiguous = 1; + offset += size[i]; + } + return NULL; + } + cur->shader = ex->dc->B.st.vs; + cur->indices = d->ibuf; + memcpy(cur->vertex, vertex, sizeof vertex); + memcpy(cur->bindings, bindings, sizeof bindings); + memcpy(cur->size, size, sizeof size); + if (n > cur->capacity) { + uint8_t *larger = realloc(cur->bytes, n); + if (!larger) { cur->ambiguous = 1; return NULL; } + cur->bytes = larger; + cur->capacity = n; + } + uint8_t *data = cur->bytes; + for (int i = 0; i < POSE_BANKS; ++i) { + if (part[i]) memcpy(data, part[i], cur->size[i]); + data += cur->size[i]; + } + return prev && frame && prev->frame == frame - 1 && prev->selected && !prev->ambiguous && + prev->width == cur->width && prev->height == cur->height && + prev->shader == cur->shader && prev->indices == cur->indices && cur->vertex[0].address && + !memcmp(prev->vertex, cur->vertex, sizeof cur->vertex) && + !memcmp(prev->size, cur->size, sizeof cur->size) ? prev : NULL; +} + +// c6 holds a part's submitted transform. Translation is deliberately excluded: a fast jump +// does not itself make a vector unreliable. Only a positively identified sharp rotation opts in. +static float pose_rotation_trace_limit(void) { + static int degrees; + static float limit; + if (!degrees) { + const char *setting = getenv("NX2NAVI_MOTION_GUARD_DEGREES"); + degrees = setting && !strcmp(setting, "35") ? 35 : setting && !strcmp(setting, "45") ? 45 : 60; + limit = degrees == 60 ? 2.0f : degrees == 45 ? 2.41421356f : 2.63830409f; + rt_log("motion policy: rotation guard threshold %d degrees per rendered frame", degrees); + } + return limit; +} +static int pose_sharp_rotation(const ActorPose *now, const ActorPose *prev) { + if (!now || !prev || prev->frame + 1 != now->frame || now->size[3] < 48 || prev->size[3] < 48) return 0; + float m[2][12]; + const ActorPose *p[2] = {now, prev}; + for (int k = 0; k < 2; ++k) { + memcpy(m[k], p[k]->bytes + p[k]->size[0] + p[k]->size[1] + p[k]->size[2], sizeof m[k]); + for (int a = 0; a < 3; ++a) { + for (int b = a; b < 3; ++b) { + float dot = 0.0f; + for (int j = 0; j < 3; ++j) dot += m[k][4 * a + j] * m[k][4 * b + j]; + float wanted = a == b ? 1.0f : 0.0f; + if (!(dot >= wanted - 0.005f && dot <= wanted + 0.005f)) return 0; + } + } + } + float trace = 0.0f; + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) trace += m[0][4 * i + j] * m[1][4 * i + j]; + // trace(R1 * transpose(R0)) = 1 + 2*cos(angle). The selected threshold controls + // when this part needs visibility filtering; equipment retains its separate raw policy. + return trace < pose_rotation_trace_limit(); +} + +static int pose_guard_needed(const PoseRange *range, const ActorPose *now, const ActorPose *previous) { + if (pose_raw() || (range->owner.flags & MOTION_OWNER_RIGID_EQUIPMENT)) return 0; + return pose_sharp_rotation(now, previous); +} + +static unsigned pose_guarded_parts, pose_raw_parts, pose_rigid_parts; +static void pose_select(uint64_t frame, uint64_t camera, VkImage depth) { + unsigned guarded = 0, raw = 0, rigid = 0, missing_depth = 0; + for (int i = 0; i < pose_count; ++i) { + PoseFrame *h = &pose_ranges[i].history[frame & 1]; + h->scene = NULL; + for (int j = 0; j < POSE_VIEWS; ++j) { + ActorPose *p = &h->view[j]; + p->selected = p->seen && p->frame == frame && !p->ambiguous && p->cam == camera && p->depth == depth; + if (p->selected) { + h->scene = p; + guarded += p->visibility_guard; + raw += !p->visibility_guard; + rigid += !!(pose_ranges[i].owner.flags & MOTION_OWNER_RIGID_EQUIPMENT); + missing_depth += p->guard_missing_depth; + } + } + } + // Selection preserves each part independently. No six-frame hold and no queue-wide drop: + // a risky body triangle cannot suppress the shield, sword, or another well-behaved part. + pose_guarded_parts += guarded; + pose_raw_parts += raw; + pose_rigid_parts += rigid; + if (frame % 120 == 0) + rt_log("selective motion: body %u/%u, attached equipment %u/%u matched/written; missing blocks %u, " + "unowned draws %u, history capacity misses %u; selected parts guarded %u, raw %u, rigid equipment %u", + pose_seen[1], pose_written[1], pose_seen[2], pose_written[2], pose_missing, pose_unowned, pose_full, + pose_guarded_parts, pose_raw_parts, pose_rigid_parts); + static int trace = -1; + if (trace < 0) trace = rt_knob("MOTION_POLICY_TRACE", "0"); + if (trace && (guarded || raw)) + rt_log("motion policy: world %.3f frame %llu guarded %u raw %u rigid %u depth-unavailable %u", + frame_step_world_us() / 1e6, (unsigned long long)frame, guarded, raw, rigid, missing_depth); +} + +static void pose_shutdown(void) { + for (int i = 0; i < pose_count; ++i) pose_clear(&pose_ranges[i]); + pose_count = 0; + pose_guarded_parts = pose_raw_parts = pose_rigid_parts = 0; +} diff --git a/runtime/vk_usnap.c b/runtime/vk_usnap.c index b02b1655..274a64ee 100644 --- a/runtime/vk_usnap.c +++ b/runtime/vk_usnap.c @@ -24,6 +24,7 @@ #include "host.h" #include "nvn_api.h" #include "vk.h" +#include "motion_owner.h" #include "vk_dlaa.h" #include "platform/platform.h" @@ -53,6 +54,7 @@ static struct { int on; // this submission has a snapshot uint64_t fence, gen; Arena *arena; + MotionOwnerSnapshot *owners; Rng *col; // collected ranges int ncol, ccol; Reg *reg; // merged, sorted by lo, disjoint @@ -72,6 +74,7 @@ static PlatMutex gather_lock = PLAT_MUTEX_INIT; // A copy taken at the submit call for the replay thread (vk_usnap_take), in submission order. typedef struct Taken { struct Taken *next; + MotionOwnerSnapshot *owners; Arena *arena; Reg *reg; int nreg, n; @@ -310,6 +313,10 @@ void vk_usnap_take(const uint64_t *handles, int n) { Taken *t = calloc(1, sizeof *t); if (!t) rt_fatal("vk_usnap: out of memory"); t->n = n, t->first = handles[0]; + if (motion_owner_enabled()) { + t->owners = malloc(sizeof *t->owners); + if (t->owners) motion_owner_capture(t->owners); + } int creg = 0; plat_mutex_lock(&gather_lock); U.ncol = 0; @@ -342,6 +349,7 @@ void vk_usnap_take(const uint64_t *handles, int n) { static void taken_free(Taken *t) { if (t->arena) arena_done(t->arena); + free(t->owners); free(t->reg); free(t); } @@ -370,6 +378,7 @@ static int adopt(const uint64_t *handles, int n) { if (wr_hits(U.reg[i].lo, U.reg[i].hi)) U.reg[i].dropped = 1, U.inflight++; U.arena = t->arena, t->arena = NULL; U.on = 1; + U.owners = t->owners, t->owners = NULL; U.subs++, U.regions += (uint64_t)U.nreg, U.bytes += t->total, U.copy_ns += t->ns; } taken_free(t); @@ -378,6 +387,7 @@ static int adopt(const uint64_t *handles, int n) { void vk_usnap_begin(const uint64_t *handles, int n, uint64_t fence) { if (!U.mode) U.mode = rt_knob("UBO_SNAPSHOT", "1") + 1; // stored +1: 0 is "not read yet" + free(U.owners), U.owners = NULL; U.on = 0, U.fence = fence, U.gen++; summary(); if (!RT_KNOB_ON(U.mode - 1)) { @@ -386,6 +396,11 @@ void vk_usnap_begin(const uint64_t *handles, int n, uint64_t fence) { return; } if (adopt(handles, n)) return; + // Without a producer snapshot, asynchronous replay must not read present-day actor ownership. + if (!gfx_replay_async() && motion_owner_enabled()) { + U.owners = malloc(sizeof *U.owners); + if (U.owners) motion_owner_capture(U.owners); + } uint64_t t0 = kern_now_ns(); plat_mutex_lock(&gather_lock); U.ncol = 0; @@ -424,6 +439,7 @@ void vk_usnap_begin(const uint64_t *handles, int n, uint64_t fence) { void vk_usnap_end(uint64_t fence) { if (U.arena) vk_completion_add(fence, arena_done, U.arena), U.arena = NULL; + free(U.owners), U.owners = NULL; U.on = 0, U.gen++; } @@ -529,6 +545,7 @@ const uint8_t *vk_usnap_bytes(uint64_t addr, uint64_t n) { // The arenas back on the free list (vk_exec_draw_shutdown: the device is idle, every completion ran). void vk_usnap_shutdown(void) { + free(U.owners), U.owners = NULL; plat_mutex_lock(&free_lock); for (Arena *a = free_arenas, *next; a; a = next) next = a->next, vmaDestroyBuffer(vk_ctx()->vma, a->buf, a->alloc), free(a->cpu), free(a); free_arenas = NULL; @@ -545,3 +562,22 @@ void vk_usnap_sizes(PerfSizes *s) { perf_size(s, "free uniform snapshot arenas", n, 0, b); perf_size(s, "uniform snapshot GPU writes", (uint64_t)__atomic_load_n(&U.nwr, __ATOMIC_RELAXED), 0, PERF_SIZE_NONE); } + +// Ownership travels with the uniform snapshot. A missing, GPU-written, or overlapping range is not a match. +int vk_usnap_motion_owner(uint64_t address, uint64_t size, MotionOwner *out) { + if (!U.on || !U.owners || !size || address > UINT64_MAX - size || + vk_usnap_why(address, size) != VK_USNAP_FOUND) return 0; + // The producer excludes overlapping identities and sorts the remaining disjoint ranges once. + // Most scene draws are unowned, so do not scan every player binding for each one. + unsigned lo = 0, hi = U.owners->count; + while (lo < hi) { + unsigned mid = lo + (hi - lo) / 2; + if (U.owners->bindings[mid].address <= address) lo = mid + 1; + else hi = mid; + } + if (!lo) return 0; + const MotionOwnerBinding *b = &U.owners->bindings[lo - 1]; + if (size > b->size || address - b->address > b->size - size) return 0; + *out = b->owner; + return 1; +} From 8946fc020101e7c83ca8f7b1c9d12ba8ff1acd15 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:44:12 -0500 Subject: [PATCH 4/4] Leave Link's own motion vectors off until they have been played, and take their settings through nx2navi.ini. The pose motion vectors for Link and his attached equipment were on by default for game 1.1.1 and had not been run in the game on this version. selective_motion = 1 turns them on. Its settings are listed among the known ones, so nx2navi.ini takes them without an unknown key line, and the rotation threshold is read through the settings instead of from the environment alone. nx2navi.ini.example describes the switch. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 7 +++++++ runtime/cfg.c | 1 + runtime/motion_owner.c | 6 +++++- runtime/vk_fg_pose.inc | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index d57fa19b..fe04570f 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -510,6 +510,13 @@ # 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. # dlaa_objmv = 1 +# Motion vectors of Link's own for DLSS, FSR 3.1 and frame generation (game 1.1.1): Link and the equipment attached to him are +# drawn a second time with last frame's pose, so fast turns, attacks and jumps move by their real motion instead of an +# estimate from colour and depth. 0, the default: the estimate, as before. 1: on, to try; it reads the player's +# equipment from the game's memory at every submission and has not been played on this version yet. Read at start. +# motion_guard_degrees (35, 45 or 60, the default): a part that turns more than this in one frame is checked against +# last frame's depth before its vector is written. +# selective_motion = 0 # Characters' trails: where a character, animal or held item moves without motion vectors of its own (all of them with # dlaa_objmv = 0), DLSS can drag a faint copy of it. The G-buffer marks those objects; on them and where they were the # frame before, DLSS is told to favour the current frame by this percentage (0, 35, 65 or 100; 0 off). Higher removes diff --git a/runtime/cfg.c b/runtime/cfg.c index a1057dd9..61ec856d 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -56,6 +56,7 @@ static const char *const known[] = { "HDR_TONEMAP", "HDR_TONE_SLOPE", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "BUILD_FREE_MB", "BUILD_RESERVE_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "LINE_WIDTH", "POOL_SHOWN_FIRST", "TEX_REUSE_NAMED", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "GATE_WAIT_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MEM_NEED_MB", "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", "MOUSE_TRACE", "MOVIE_HOST", "MOVIE_PACE", "MOVIE_CLOCK", "TEST_HITCH", + "MOTION_GUARD_DEGREES", "MOTION_POLICY_TRACE", "SELECTIVE_MOTION", "SELECTIVE_MOTION_RAW", // motion_owner.c, vk_fg_pose.inc "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_FREECAM", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPELINE_CACHE_PLAY", "PIPELINE_CACHE_PLAY_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SEED", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "TEX_FLAT3D", "TEX_KEEP_TAKEN", "TEX_STALE_KEPT", "PRESENT_HELD", "HELD_SYNC", diff --git a/runtime/motion_owner.c b/runtime/motion_owner.c index 602bfd89..d3435f72 100644 --- a/runtime/motion_owner.c +++ b/runtime/motion_owner.c @@ -32,7 +32,11 @@ static int owner_on; static void owner_init(void *unused) { (void)unused; - owner_on = RT_V111 && rt_knob("SELECTIVE_MOTION", "1"); + // NX2NAVI_SELECTIVE_MOTION (selective_motion; default 0, 1 on): off until it has been played. On, every guest + // submission walks the player's actor, equipment and models in guest memory (motion_owner_capture), each scene + // draw is looked up in that list (vk_fg_pose.inc pose_owner), and the player's meshes are drawn a second time + // into the motion vectors DLSS, FSR 3.1 and frame generation read. + owner_on = RT_V111 && rt_knob("SELECTIVE_MOTION", "0"); } int motion_owner_enabled(void) { plat_once(&owner_once, owner_init, NULL); diff --git a/runtime/vk_fg_pose.inc b/runtime/vk_fg_pose.inc index a08f05ea..d68d331a 100644 --- a/runtime/vk_fg_pose.inc +++ b/runtime/vk_fg_pose.inc @@ -199,7 +199,7 @@ static float pose_rotation_trace_limit(void) { static int degrees; static float limit; if (!degrees) { - const char *setting = getenv("NX2NAVI_MOTION_GUARD_DEGREES"); + const char *setting = cfg_get("NX2NAVI_MOTION_GUARD_DEGREES"); // motion_guard_degrees in nx2navi.ini too degrees = setting && !strcmp(setting, "35") ? 35 : setting && !strcmp(setting, "45") ? 45 : 60; limit = degrees == 60 ? 2.0f : degrees == 45 ? 2.41421356f : 2.63830409f; rt_log("motion policy: rotation guard threshold %d degrees per rendered frame", degrees);