From bd7d140a5433ab04491c0583ada19a4de23b033e Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 09:31:04 -0500 Subject: [PATCH 01/14] Draw lines as wide as the game sets them. The game's line width was dropped at the call and every line drawn 1 pixel wide; the game draws two lines a frame at 4.5 pixels into a 144x144 mask. The width is now recorded with the draw state and set as dynamic state for draws of the line topology class, held to the device's range. Only pipelines of that class list the new dynamic state, so the others keep their entries in the pipeline caches and the driver's. line_width = 0 draws every line 1 pixel wide as before. Co-Authored-By: Claude Fable 5.2 --- docs/maxwell.md | 5 ++++- nx2navi.ini.example | 3 +++ runtime/cfg.c | 5 +++-- runtime/nvn_api_cmd.c | 7 +++++++ runtime/nvn_ext.c | 14 +++++-------- runtime/nvn_obj.h | 2 +- runtime/vk.h | 3 +++ runtime/vk_exec.h | 1 + runtime/vk_exec_draw.c | 2 ++ runtime/vk_pipeline.c | 47 +++++++++++++++++++++++++++++++++++++++++- 10 files changed, 75 insertions(+), 14 deletions(-) diff --git a/docs/maxwell.md b/docs/maxwell.md index b173af4b..e2758b6a 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -174,7 +174,10 @@ Grep-level look on 2026-10-22, statuses updated 2026-10-26; "saw" is what the gr where the driver keeps NaNs (3.13). 14. **Point size / line width.** Census: `SetLineWidth 4.5` ~1/frame (2 line draws into a 144x144 R8 target), `SetPointSize 5` ~14/frame; 4 point VS rely on it. Point size: translator patch 0008 reads it from the support - buffer, **fixed**. Line width: 1 px, non-default values logged (`nvn_ext.c`), **mismatch, minor**. + buffer, **fixed**. Line width: recorded with the draw state (`nvn_api_cmd.c`, `OP_BIND_STATE` kind 7) and set as + dynamic state for draws of the line topology class (`vk_pipeline.c` `vk_pipeline_line_width`), held to the + device's `lineWidthRange`; 1 px on a device without `wideLines` and for triangles drawn in the line polygon mode. + **Implemented; not compared with the hardware's wide-line coverage.** 15. **Block height.** `nvn_tex.c` truncates like NVN (2.1); every other step of the layout matches. **OK.** 16. **Depth mode and clamp.** `vk_pipeline.c` `create_graphics` (`negativeOneToOne` from `nvn_depth_minus_w`, `depthClampEnable` from NVN state). Matches the hardware z maths (1.5) and clamp switch (1.6). **OK.** diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 99ca6beb..d57fa19b 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -313,6 +313,9 @@ # drawn as a rectangle over solid ground at the Hyrule Castle moat and the Castle Town canal from some camera # positions, and can turn into a red beam there. 0: the water's shaders as they are. # water_projective = 1 +# Lines are drawn as wide as the game sets them (it draws two a frame 4.5 pixels wide into a small mask). 0: every +# line 1 pixel wide, as up to 0.9.1. +# line_width = 1 # Optimisations, 0 turns each off: skip bindings that did not change, remember shader lookups, reuse the previous # draw's pipeline when its state repeats, remember texture descriptors between draws, update descriptors in bulk, # skip push constants that did not change. diff --git a/runtime/cfg.c b/runtime/cfg.c index 4e4cec2b..6da339d8 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -54,7 +54,7 @@ static const char *const known[] = { "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_HUE_CLIP", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", "HDR_SATURATION", "HDR_SHADOWS", "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", "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", + "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", "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", "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", @@ -132,7 +132,7 @@ static const struct { const char *key, *section; } sections[] = { {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, {"PIPE_FRAME_WAIT_MS", "performance"}, {"PIPE_SKIP_STEADY", "performance"}, {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, - {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"}, + {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"}, {"COMPUTE_KEEP", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_SPLIT_FILL", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"WATER_PROJECTIVE", "performance"}, @@ -271,6 +271,7 @@ static const CfgMeta metas[] = { {"MESH_FALLBACK", "auto", "auto|0|1", RESTART}, {"MESH_FALLBACK_MB", "128", "16..4096", RESTART}, {"MESH_FALLBACK_WORKERS", "auto", "auto|1|2|3|4", RESTART}, // vk_exec_draw.c mfb_worker_count {"PIPE_AHEAD", "1", "0|1", RESTART}, // vk_exec.c vk_exec_ahead, vk_pipeline.c vk_pipeline_hint + {"LINE_WIDTH", "1", "0|1", RESTART}, // vk_pipeline.c key_lines {"GPL_FEEDBACK", "1", "0|1", RESTART}, // vk_pipeline.c gpl_feedback {"PIPE_FAST_WORKERS", "1", "0|1", RESTART}, // vk_pipeline.c lib_worker, fast_take {"FRAME_HOLD_MS", "120", "0..500", RESTART}, // vk_backend.c frame_hold diff --git a/runtime/nvn_api_cmd.c b/runtime/nvn_api_cmd.c index e3a11d6e..257f1ff4 100644 --- a/runtime/nvn_api_cmd.c +++ b/runtime/nvn_api_cmd.c @@ -902,6 +902,13 @@ NVN_API(nvnCommandBufferSetPointSize) { r->kind = 6; memcpy(r->bytes, &s, sizeof s); } +// void nvnCommandBufferSetLineWidth(NVNcommandBuffer *cmd, float width): bind-state kind 7, as the point size +NVN_API(nvnCommandBufferSetLineWidth) { + p_bind_state *r = REC(cmd_of(c), OP_BIND_STATE, p_bind_state); + float w = arg_s(c, 0); + r->kind = 7; + memcpy(r->bytes, &w, sizeof w); +} // void nvnCommandBufferBindVertexAttribState(NVNcommandBuffer *cmd, int n, const NVNvertexAttribState *states) NVN_API(nvnCommandBufferBindVertexAttribState) { cmd_t *o = cmd_of(c); diff --git a/runtime/nvn_ext.c b/runtime/nvn_ext.c index 238f0789..40b5e17a 100644 --- a/runtime/nvn_ext.c +++ b/runtime/nvn_ext.c @@ -379,16 +379,12 @@ NVN_API(nvnVertexStreamStateGetStride) { ret_x(c, ST(vs_st)->stride); } NVN_API(nvnVertexStreamStateGetDivisor) { ret_x(c, ST(vs_st)->divisor); } #undef ST -// ---------- rasterisation state without a host equivalent: lines are 1 pixel wide and point size comes from the vertex -// shader. Non-default values are reported once; the drawing proceeds without them. +// ---------- rasterisation state without a host equivalent. Non-default values are reported once; the drawing proceeds +// without them. #define ONCE(...) do { static int once_; if (!once_++) rt_log("nvn: " __VA_ARGS__); } while (0) -// void nvnCommandBufferSetLineWidth / SetPointSize(NVNcommandBuffer *cmd, float value) -NVN_API(nvnCommandBufferSetLineWidth) { - float w = arg_s(c, 0); - if (w != 1.0f) ONCE("SetLineWidth %g: lines are drawn 1 pixel wide", w); -} -// SetPointSize is recorded in nvn_api_cmd.c (OP_BIND_STATE kind 6): the replay feeds it to the vertex shaders through the -// support buffer (translator patch 0008). +// SetPointSize and SetLineWidth are recorded in nvn_api_cmd.c (OP_BIND_STATE kinds 6 and 7): the replay feeds the point +// size to the vertex shaders through the support buffer (translator patch 0008) and sets the line width for the draws +// of lines (vk_pipeline.c vk_pipeline_line_width). // void nvnCommandBufferSetConservativeRasterEnable(NVNcommandBuffer *cmd, NVNboolean enable) NVN_API(nvnCommandBufferSetConservativeRasterEnable) { if (nvn_arg_bool(c, 1)) ONCE("conservative rasterisation requested: drawn normally"); diff --git a/runtime/nvn_obj.h b/runtime/nvn_obj.h index 526ea503..ca80ff3a 100644 --- a/runtime/nvn_obj.h +++ b/runtime/nvn_obj.h @@ -142,7 +142,7 @@ typedef struct { int32_t mode; int32_t pad; uint64_t addr; } p_render_cond; // typedef struct { int32_t enable; uint32_t index; } p_prim_restart; // SetPrimitiveRestart: indexed strips restart at index typedef struct { float n, f; } p_depth_range; typedef struct { const prog_t *prog; int32_t stages; } p_bind_program; -typedef struct { int32_t kind; uint8_t bytes[0x18]; } p_bind_state; // kind: 0 blend 1 color 2 chmask 3 ms 4 poly 5 ds 6 point size (float) +typedef struct { int32_t kind; uint8_t bytes[0x18]; } p_bind_state; // kind: 0 blend 1 color 2 chmask 3 ms 4 poly 5 ds 6 point size (float) 7 line width (float) typedef struct { int32_t first, n; uint32_t st[16]; } p_bind_vattr; typedef struct { int32_t first, n; vs_st st[16]; } p_bind_vstream; typedef struct { int32_t first, n; NVNbufferRange r[16]; } p_bind_vbuf; diff --git a/runtime/vk.h b/runtime/vk.h index 292f4453..16a86659 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -871,6 +871,9 @@ VkPipeline vk_pipeline_compute(const GfxShader *cs, const VkPipeInfo **info); // write masks and references; vertex buffers with vkCmdBindVertexBuffers2 (strides NULL when // vk_pipeline_vertex_input_dynamic(), else from VkPipeState.vstream). void vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s); +// The line width for a draw of lines with the state s (width in pixels as the game set it; 0 = 1): recorded when s +// draws lines and the device has wide lines, whose pipelines take the width as dynamic state. Nothing otherwise. +void vk_pipeline_line_width(VkCommandBuffer cb, const VkPipeState *s, float width); // Vertex input as vkCmdSetVertexInputEXT takes it (vk_pipeline.c vertex_input). typedef struct { uint32_t n_attr, n_bind; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index b09a4092..569d6752 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -80,6 +80,7 @@ typedef struct VkExec { float depth_range[2]; // OP_DEPTH_RANGE: overrides the viewports' near and far uint32_t st_vmask[2], st_wmask[2], st_ref[2]; // OP_STENCIL per face (0 front, 1 back): compare mask, write mask, ref float point_size; // OP_BIND_STATE kind 6: NVN point size for the support buffer; 0 = never set = 1 + float line_width; // OP_BIND_STATE kind 7: NVN line width in pixels; 0 = never set = 1 p_poly_offset poly_offset; // OP_POLY_OFFSET: depth bias factor, units, clamp p_prim_restart prim_restart; // OP_PRIM_RESTART: applied to indexed strip and fan draws (op_draw_idx) float blend_color[4]; // OP_BLEND_COLOR diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index d3b04b8d..8b251219 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -4331,6 +4331,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int cx->B.st = s, cx->B.st_valid = 1; if (ex->mark && vk_exec_frame.draws == ex->mark_draws0) ex->mark->first[0] = vs, ex->mark->first[1] = fs; } + vk_pipeline_line_width(ex->cb, &s, ex->line_width); // a draw of lines: the game's width; else nothing if (!mesh) { vk_dlaa_draw(ex); vk_mvsurvey_draw(ex); @@ -6457,6 +6458,7 @@ static void op_draw_impl(VkExec *ex, uint32_t op, const void *payload) { case 4: memcpy(&ex->pipe.poly, b->bytes, sizeof ex->pipe.poly); break; case 5: memcpy(&ex->pipe.ds, b->bytes, sizeof ex->pipe.ds); break; case 6: memcpy(&ex->point_size, b->bytes, sizeof ex->point_size); break; + case 7: memcpy(&ex->line_width, b->bytes, sizeof ex->line_width); break; } break; } diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 15a41f5e..225bd84f 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -84,6 +84,8 @@ static struct { int vs_outs_all; // NX2NAVI_VS_OUTS: 0 union of paired fragment inputs, 1 (all) all 32 output locations int dyn_vi, dyn_clamp, dyn_poly, dyn_blend_en, dyn_blend_eq, dyn_wmask, dyn_a2c; int f_logic_op, f_depth_clamp, f_fill_non_solid, f_dual_src, f_full_subgroups; + int wide_lines; // the device draws lines of other widths than 1 (vk_pipeline_line_width) + float line_w[2], line_gran; // its range of widths and their step int cache_control; // pipelineCreationCacheControl: a create can ask for the pipeline caches only int feedback; // NX2NAVI_PIPE_FEEDBACK: 1 count pipeline cache hits, 2 also log each compute and graphics build uint64_t cache_loaded; // bytes of pipeline-cache.bin the driver took at boot @@ -958,6 +960,12 @@ static int ensure_init(void) { P.f_depth_clamp = f2.features.depthClamp; P.f_fill_non_solid = f2.features.fillModeNonSolid; P.f_dual_src = f2.features.dualSrcBlend; + P.wide_lines = f2.features.wideLines && cfg_bool("NX2NAVI_LINE_WIDTH"); + P.line_w[0] = c->props.limits.lineWidthRange[0], P.line_w[1] = c->props.limits.lineWidthRange[1]; + P.line_gran = c->props.limits.lineWidthGranularity; + if (!P.wide_lines) + rt_log("vk_pipeline: lines are drawn 1 pixel wide whatever width the game sets (%s)", + f2.features.wideLines ? "NX2NAVI_LINE_WIDTH=0" : "this device has no wide lines"); #define PROC(field, name) (P.field = (void *)vk_dpa(c->device, name)) PROC(push, "vkCmdPushDescriptorSetKHR"); // Descriptor update templates (core 1.1; the push variant comes with VK_KHR_push_descriptor on 1.1): the driver @@ -3106,6 +3114,29 @@ static int water_mode(const VkPipeState *s, const GKey *k) { return mode == 1 && k->dsb ? 0 : mode; } +// ---- line width (NX2NAVI_LINE_WIDTH, default 1; 0: every line 1 pixel wide, as before) +// The game sets a width for its lines (nvnCommandBufferSetLineWidth, recorded as OP_BIND_STATE kind 7): 4.5 pixels +// for two line draws a frame into a 144x144 R8 target, which were drawn 1 pixel wide. A pipeline of the line topology +// class lists the width as dynamic state, in its whole build and in its pre-rasterization library (PrKey.lines), and +// each draw of lines records the width the game set, held to the device's range. Pipelines of the other classes are +// built as before, so their entries in the pipeline caches and the driver's stay the ones in use. A polygon mode that +// draws triangles as lines keeps 1 pixel: the mode can be dynamic state, which no pipeline key tells apart. +static int key_lines(const GKey *k) { return P.wide_lines && k->topo_class == 1; } + +void vk_pipeline_line_width(VkCommandBuffer cb, const VkPipeState *s, float width) { + if (!P.wide_lines || is_mesh(s) || topology_class(vk_pipeline_topology(s->prim)) != 1) return; + float w = width > 0 ? width : 1.0f; + if (P.line_gran > 0) w = P.line_w[0] + P.line_gran * (float)(int)((w - P.line_w[0]) / P.line_gran + 0.5f); + w = w < P.line_w[0] ? P.line_w[0] : w > P.line_w[1] ? P.line_w[1] : w; + static float logged; + if (w != 1.0f && w != logged) { // one line for each width in a row: the game uses one or two + logged = w; + rt_log("vk_pipeline: lines drawn %g pixels wide (the game set %g; the device draws %g to %g in steps of %g)", + (double)w, (double)width, (double)P.line_w[0], (double)P.line_w[1], (double)P.line_gran); + } + vkCmdSetLineWidth(cb, w); +} + static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Layout *l, uint32_t fs_off) { VkShaderModule vm = VK_NULL_HANDLE, fm = VK_NULL_HANDLE; uint64_t vw, fw, vm_hash = 0, fm_hash = 0; // the hashes: of the final modules, for the PIPE_FEEDBACK 2 line @@ -3244,6 +3275,11 @@ static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Lay VkPipelineDynamicStateCreateInfo dy = {.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, .dynamicStateCount = P.n_dyn, .pDynamicStates = P.dyn}; VkDynamicState mdyn[sizeof P.dyn / sizeof P.dyn[0] + 1]; + if (!mesh && key_lines(k)) { // lines take the game's width per draw (vk_pipeline_line_width) + memcpy(mdyn, P.dyn, P.n_dyn * sizeof *mdyn); + mdyn[P.n_dyn] = VK_DYNAMIC_STATE_LINE_WIDTH; + dy.dynamicStateCount = P.n_dyn + 1, dy.pDynamicStates = mdyn; + } if (mesh) { // a mesh pipeline must not list vertex input, topology or primitive restart as dynamic uint32_t n = 0; for (uint32_t i = 0; i < P.n_dyn; i++) @@ -3331,7 +3367,9 @@ typedef struct Lib { struct Lib *next; uint64_t hash; VkPipeline pipe; uint32_t static Lib *libs[LIB_BUCKETS]; typedef struct { uint32_t kind, topo_class, attr_fmt[32], attr_off[32]; uint8_t attr_bind[32], rate[17], pad[3]; } ViKey; -typedef struct { uint32_t kind, missing; const GfxShader *vs; uint8_t clamp, poly_mode, minus_w, water_projective; GfxShaderIo fin[32]; } PrKey; +typedef struct { uint32_t kind, missing; const GfxShader *vs; uint8_t clamp, poly_mode, minus_w, water_projective; + uint8_t lines; // key_lines: the library lists the line width as dynamic state + GfxShaderIo fin[32]; } PrKey; typedef struct { uint32_t kind; uint8_t dsb, samples, a2c, water_projective; const GfxShader *fs; } FsKey; typedef struct { uint32_t kind; uint8_t n_color, samples, a2c, logic_op, blend_en, fbl, pad[2]; uint32_t color[8], depth, stencil, wmask; uint8_t blend[8][6]; } FoKey; // fbl: GKey.fbl, the feedback loop flags of the library (gpl_feedback) @@ -3360,6 +3398,12 @@ static VkPipeline lib_create(int kind, const VkPipeState *s, const GKey *k, VkPi .dynamicStateCount = P.n_dyn, .pDynamicStates = P.dyn}; VkGraphicsPipelineCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .pNext = &ri, .flags = VK_PIPELINE_CREATE_LIBRARY_BIT_KHR | noopt_flag(), .pDynamicState = &dy}; + VkDynamicState ldyn[sizeof P.dyn / sizeof P.dyn[0] + 1]; + if (kind == LIB_PR && ((const PrKey *)key)->lines) { // the rasterization state is this library's: as create_graphics + memcpy(ldyn, P.dyn, P.n_dyn * sizeof *ldyn); + ldyn[P.n_dyn] = VK_DYNAMIC_STATE_LINE_WIDTH; + dy.dynamicStateCount = P.n_dyn + 1, dy.pDynamicStates = ldyn; + } VkShaderModule m = VK_NULL_HANDLE; VkPipelineShaderStageCreateInfo st = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pName = "main"}; VkPipelineShaderStageRequiredSubgroupSizeCreateInfo rq; @@ -3627,6 +3671,7 @@ static void lib_keys(const VkPipeState *s, const GKey *k, ViKey *v, PrKey *pk, F memset(pk, 0, sizeof *pk); GfxShaderIo fin[32]; pk->kind = LIB_PR, pk->vs = s->vs, pk->clamp = k->clamp, pk->poly_mode = k->poly_mode, pk->minus_w = k->minus_w; + pk->lines = (uint8_t)key_lines(k); pk->missing = vs_union_missing(s, fin); for (int i = 0; i < 32; i++) if (pk->missing >> i & 1) pk->fin[i] = fin[i]; From daf7298dbad41a2548e2e22e95a2ae229c7a0121 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 09:31:49 -0500 Subject: [PATCH 02/14] Bring the graphics notes' checks up to the code: dual-source blend and depth compare in compute are done, and what the timestamp mismatch can change. Co-Authored-By: Claude Fable 5.2 --- docs/maxwell.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/maxwell.md b/docs/maxwell.md index e2758b6a..1def9ebc 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -166,7 +166,10 @@ Grep-level look on 2026-10-22, statuses updated 2026-10-26; "saw" is what the gr black; integer formats use the float path. Confirm the RX 9070 reports the feature (grass map needs 0.5). **OK if supported.** 12. **Dual-source blend.** `vk_pipeline.c` `blend_factor` maps SRC1 factors; `Program.cs` `QueryGraphicsState` always passes - `dualSourceBlendEnable = false`, so no `Index 1` output exists. Log pipelines with SRC1 factors. **Mismatch if used.** + `dualSourceBlendEnable = false`, so no `Index 1` output exists in the translation. `vk_pipeline.c` gives a + pipeline whose blend state uses a SRC1 factor (`uses_src1`, `GKey.dsb`) a fragment module with the second output + at `Index 1` (`spirv_dual_source`) where the device has `dualSrcBlend`. **Fixed** (checked in the code on + 2026-11-12, not by a capture). 13. **FMZ / NaN.** Count `FMUL/FFMA/FMUL32I/FFMA32I` words with the `Fmz` field set (`Decoders/InstDecoders.cs:1901` bit positions) in `build/v121/shaders/`; FMin/FMax/FClamp NaN results are undefined (3.1). Census (2026-10-22): FMZ in 1 instruction of 12397 shaders, FTZ in 12241 shaders, FMNMX in 7457. The translator emits GLSL.std.450 @@ -186,7 +189,10 @@ Grep-level look on 2026-10-22, statuses updated 2026-10-26; "saw" is what the gr 19. **Viewport / scissor arrays.** Single viewport (`vk_exec_draw.c` `record_fixed`), extra scissors logged. Census: 0 differing. **OK.** 20. **Depth-compare sampling in compute** (VUID-vkCmdDispatch-None-06479): one compute shader samples a 4x4 R32F - CPU-written placeholder through an R32_SFLOAT view with compare on, ~1/frame. Needs a D32 copy. **Minor.** + CPU-written placeholder through an R32_SFLOAT view with compare on, ~1/frame. Where the driver does not compare + the colour format, the dispatch samples a depth twin of the image, copied right before it (`vk_exec_draw.c`, + "depth twins"; R32_SFLOAT and R16_UNORM). **Fixed for compute passes**; a draw that samples such an image keeps + the colour view. 21. **Report timestamps: unit and instant.** `write_counter` (`vk_exec_xfer.c`) writes host `ns * 12 / 625` (19.2 MHz ticks) at fence completion for every report; `nvnDeviceGetTimestampInNanoseconds` converts back with the same factor (`nvn_ext.c`). On the hardware, GPU ticks convert at 625/384 ns (614.4 MHz), TIMESTAMP is written when the ROP has retired @@ -194,4 +200,8 @@ Grep-level look on 2026-10-22, statuses updated 2026-10-26; "saw" is what the gr (a) if the game's consumer scales raw report ticks with its own constant rather than the NVN conversion call, our 19.2 MHz unit is 32x off from the hardware's; find that consumer before fixing the unit; (b) the delta needs `vkCmdWriteTimestamp2` with `TOP_OF_PIPE` for TIMESTAMP_TOP and `BOTTOM_OF_PIPE` for TIMESTAMP, per submission - segment, converted to whichever unit (a) shows. **Mismatch (instant); unit unverified.** + segment, converted to whichever unit (a) shows. **Mismatch (instant); unit unverified.** What it can change: + both differences make the GPU time the game computes shorter than the hardware's (a report written at fence + completion spans less than top to bottom of the pipeline, and 19.2 MHz ticks read as 614.4 MHz ones are a + thirty-second), so an engine that lowers its resolution or detail under GPU load would stay at its highest + here. That is reasoning from the two units, not something measured; no picture difference is tied to it. From 57a15e288d0a1a180bc16398325fdb5d9df28ebd Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 09:47:27 -0500 Subject: [PATCH 03/14] Count only the making of textures against a frame's texture creation time. The limit on what a frame's pool refreshes may spend creating textures was charged with the whole refresh, including its check of every entry the pools hold. In Kakariko that check takes 0.8 ms a submission, six submissions a frame, so most of the 6 ms was gone before a texture was made and the frame's later refreshes made none: 87 refreshes in 600 frames, with 1,090 slots left between them. The game moves about thirty textures to new storage every four or five frames there, and the ones a refresh left were on screen as their grey stand-ins for a frame; a player's recording shows three lamp posts turn grey for one frame that way. The limit now counts the time the lookups spend making entries and uploading them the first time. Co-Authored-By: Claude Fable 5.2 --- runtime/vk.h | 1 + runtime/vk_exec_draw.c | 18 +++++++++++++----- runtime/vk_texture.c | 13 +++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 16a86659..76c1fc75 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -585,6 +585,7 @@ const VkTexImage *vk_texture_exact_by_id_locked(int id, view_t *view, int *has_v uint32_t vk_texture_slots_gen(void); int vk_texture_slot_count_locked(void); void vk_texture_create_budget_locked(int n); // slot lookups create at most n entries (mapped storage only); -1 any +uint64_t vk_texture_pool_make_ns_take(void); // ns the pool's lookups spent making and first uploading entries since the last call int vk_texture_create_deferred_locked(void); // a lookup since the last call left a creation to the budget // one past the highest texture slot registered so far (or more) int vk_sampler_slot_count_locked(void); // Bindless pools (vk_exec_draw.c, "texture and sampler pools"): set 2 of every pipeline layout. Shaders that index diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 8b251219..e382fce3 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -1965,7 +1965,7 @@ static struct { int ready, uab, sealed; // sealed: a part of the current submission already reads sets[cur] uint32_t kinds, gen, epoch, sgen; int retry; // the last sync left slots to the creation budget - uint64_t frame, frame_ns; // the frame of the last sync, and the sync time spent in it (pool_create_ns) + uint64_t frame, frame_ns; // the frame of the last sync, and the time its syncs spent making textures (pool_create_ns) uint64_t n_frame_stops, n_defer_skips; // syncs that created nothing for the frame's time, lookups they skipped uint64_t *tok; // per texture slot: vk_texture_slot_token it was filled under (0: stand-ins) VkImageView *view; // [kind * VK_POOL_TEX + id]: what the entry holds @@ -2198,9 +2198,16 @@ static int pool_create_budget(void) { // The refresh runs before every draw that binds the pools, not once per submission, so the count above held for each // draw: an area load made 4,925 entries (1.7 GB of uploads) in one 430 ms frame, and while slots were left over every // draw walked the whole table and looked each of them up again (key and hash), about 100 ms of that frame on its own. -// Once a frame's refreshes have taken NX2NAVI_POOL_CREATE_MS (default 6; 0: no limit), its later ones create nothing, -// and skip the slots left earlier in the frame (pool_defer) instead of looking them up again; the first refresh of -// each frame keeps its count budget. The slots left keep their stand-in, a few frames longer in a large load. +// Once a frame's refreshes have spent NX2NAVI_POOL_CREATE_MS (default 6; 0: no limit) making textures, its later ones +// create nothing, and skip the slots left earlier in the frame (pool_defer) instead of looking them up again; the +// first refresh of each frame keeps its count budget. The slots left keep their stand-in, a few frames longer in a +// large load. +// The time counted is what the lookups spend making entries and uploading them the first time +// (vk_texture_pool_make_ns_take). It was the whole refresh, with its check of every entry the pools hold: in Kakariko +// that check alone takes 0.8 ms a submission, six submissions a frame, so most of the 6 ms was gone before a texture +// was made, and the frame's later refreshes made none. The game moves about thirty of its textures to new storage +// every four or five frames there; the ones a refresh left were on screen as their grey stand-ins for a frame (three +// lamp posts in one recorded frame), 87 refreshes in 600 frames with 1,090 slots left between them. static uint64_t pool_create_ns(void) { static int64_t ns = -1, load_ns; if (ns < 0) { @@ -2551,10 +2558,11 @@ static void pool_sync(VkExec *ex) { if (PL.sealed) return; // current when checked: a change since waits for the next sync, which takes a new set uint64_t t0 = kern_now_ns(), f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); if (f != PL.frame) PL.frame = f, PL.frame_ns = 0; + vk_texture_pool_make_ns_take(); // what other lookups left: only this sync's making counts for the frame pool_sync_entries(ex); uint64_t t1 = kern_now_ns(); PL.sync_ns += t1 - t0; - PL.frame_ns += t1 - t0; + PL.frame_ns += vk_texture_pool_make_ns_take(); if (switched) { vk_texture_lock(); pool_catch_up(from, pre); // what the other sets got since; this sync's own writes went into it already diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 940f2ad8..582c2b12 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1521,6 +1521,7 @@ static int pool_presnap(void) { if (mode < 0) mode = rt_knob("POOL_PRESNAP", "1"); return RT_KNOB_ON(mode); } +static void pool_made(uint64_t t0); // below, with the pool's creation budget // The submission being recorded checked the pool's entries by this list: for the idle clock it walked the pool // (vk_texture_new_submit). @@ -1556,7 +1557,9 @@ int vk_texture_pool_fresh_locked(void) { } if (e->released) continue; if (!e->uploaded || e->checked != submit_epoch) { + uint64_t m0 = e->uploaded ? 0 : kern_now_ns(); // a first upload is part of making the entry make_fresh_snap(e); + if (m0) pool_made(m0); e->checked = submit_epoch; n_pool_checks++; } @@ -4123,6 +4126,12 @@ static int want_take(void) { } void vk_texture_create_budget_locked(int n) { create_budget = n, create_deferred = 0; } + +// What the pool's lookups have spent making entries (entry_of under the budget) and uploading them the first time, +// since the last call: the time vk_exec_draw.c pool_create_ns limits in a frame. Texture lock held where it is added. +static uint64_t pool_make_ns; +uint64_t vk_texture_pool_make_ns_take(void) { return __atomic_exchange_n(&pool_make_ns, 0, __ATOMIC_RELAXED); } +static void pool_made(uint64_t t0) { __atomic_fetch_add(&pool_make_ns, kern_now_ns() - t0, __ATOMIC_RELAXED); } int vk_texture_create_deferred_locked(void) { int d = create_deferred; create_deferred = 0; return d; } // NX2NAVI_TEX_STALE_KEPT (default 1; 0: the stand-in, as before): a finalized slot of a CPU-written texture resolves to @@ -4189,7 +4198,9 @@ static Tex *slot_resolve(TexSlot *s, view_t *view, int *has_view) { else if (!mem_readable((uint64_t)(uintptr_t)s->t.mem, s->t.L.size)) e = NULL; else if (!create_budget && !(s->cwant && want_take())) create_deferred = 1; else { + uint64_t m0 = kern_now_ns(); e = entry_of(&s->t); + pool_made(m0); if (create_budget > 0) create_budget--; } if (e) s->cwant = 0; @@ -4320,7 +4331,9 @@ const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view) { round_checks++; // a released entry here is a finalized slot's own (stale_kept): the storage may be the game's to free or reuse if (e && !e->released && (!e->uploaded || e->checked != submit_epoch)) { + uint64_t m0 = pool_ctx && !e->uploaded ? kern_now_ns() : 0; // the pool's lookup: as in its list check make_fresh_snap(e); + if (m0) pool_made(m0); e->checked = submit_epoch; touch(e); } From 67a21469484906124b5430d5b6f18c0e811c437b Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:04:25 -0500 Subject: [PATCH 04/14] Record what an upload read, slot registrations and sampled stand-ins in the flight recorder. Three more texture events for a dump's events.tsv: after each upload a hash of 64 probes of the storage and how many of them were all zero, so a second upload a frame later tells new bytes from the same ones and a first upload of unfilled storage shows; a slot registered with a CPU-written texture, with what the slot named before; and a slot a shader sampled that has no image of its own and follows none, so its stand-in was on screen. They are for telling why a texture shows wrong for one frame while the game streams textures in. Co-Authored-By: Claude Fable 5.2 --- runtime/flight.c | 12 +++++++++++- runtime/flight.h | 6 ++++++ runtime/vk_texture.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/runtime/flight.c b/runtime/flight.c index e316af0a..c55f23dc 100644 --- a/runtime/flight.c +++ b/runtime/flight.c @@ -156,7 +156,8 @@ static void copy_events(DumpJob *j, uint64_t first) { } } -static const char *const tex_kind_names[FLIGHT_TEX_N] = {"new", "up", "reup", "release", "takeback", "drop"}; +static const char *const tex_kind_names[FLIGHT_TEX_N] = {"new", "up", "reup", "release", "takeback", "drop", "bytes", + "reg", "blank"}; // /events.tsv: one line per texture event of the dump's frames (flight_tex). static void write_events(const DumpJob *j) { @@ -185,6 +186,15 @@ static void write_events(const DumpJob *j) { else if (e->kind == FLIGHT_TEX_DROP) snprintf(detail, sizeof detail, "%s", vk_texture_drop_name((int)e->detail)); else if (e->kind == FLIGHT_TEX_UP || e->kind == FLIGHT_TEX_REUP) snprintf(detail, sizeof detail, "%u WriteTexels", e->detail); + else if (e->kind == FLIGHT_TEX_BYTES) + snprintf(detail, sizeof detail, "probe hash %08x, %u of 64 probes all zero", e->detail, e->us); + else if (e->kind == FLIGHT_TEX_REG) { + static const char *const was[4] = {"an unused slot", "the slot named another descriptor at this storage", + "the slot named other storage", "the slot named a finalized texture"}; + snprintf(detail, sizeof detail, "slot %u: %s", e->us, was[e->detail & 3]); + } else if (e->kind == FLIGHT_TEX_BLANK) + snprintf(detail, sizeof detail, "slot %u sampled as its stand-in%s%s", e->us, e->detail & 1 ? ", finalized" : "", + e->detail & 2 ? ", held out" : ""); else detail[0] = 0; fprintf(f, "%llu\t%s\t%#llx\t%#x\t%u\t%u\t%u\t%s\t%s\t%u\n", (unsigned long long)e->frame, e->kind < FLIGHT_TEX_N ? tex_kind_names[e->kind] : "?", (unsigned long long)e->addr, e->format, e->w, diff --git a/runtime/flight.h b/runtime/flight.h index 33eb5c46..5a15dcad 100644 --- a/runtime/flight.h +++ b/runtime/flight.h @@ -20,6 +20,12 @@ void flight_frame(uint64_t frame, uint64_t now_ns, uint64_t interval_ns, const u // carries the frame whose counts in frames.tsv it is part of. Any thread: the ring takes no lock. A no-op until // flight_init turned the recorder on. enum { FLIGHT_TEX_NEW, FLIGHT_TEX_UP, FLIGHT_TEX_REUP, FLIGHT_TEX_RELEASE, FLIGHT_TEX_TAKEBACK, FLIGHT_TEX_DROP, + FLIGHT_TEX_BYTES, // after UP and REUP: what the upload read (detail: a hash of 64 probes of its storage; us: + // how many of the probes were all zero), so a second upload tells new bytes from the same + FLIGHT_TEX_REG, // a slot registered with a CPU-written texture (us: the slot; detail: what it named before: + // 0 nothing, 1 another descriptor at this storage, 2 other storage, 3 a finalized texture) + FLIGHT_TEX_BLANK, // a shader sampled a slot that has no image of its own and follows none: its stand-in was + // on screen (us: the slot; detail: bit 0 the slot is finalized, bit 1 it is held out) FLIGHT_TEX_N }; // The texture's flags: a depth or stencil image, mirrored (the CPU writes its storage), bound into pool memory, an ASTC // texture, written by the GPU (VkTexImage.gpu_owned). diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 582c2b12..ccf82760 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1734,6 +1734,25 @@ static void flight_ev(const Tex *e, int kind, uint32_t detail, uint32_t us) { static uint64_t frames_now(void) { return __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); } +// For the flight recorder, after an upload of e: FNV-1a over 64 probes of 64 bytes spread over its storage, and how many +// of them were all zero. Two uploads of one texture with the same hash read the same bytes at the probes; a first one +// with zero probes read storage the game had not filled. +static void flight_bytes(const Tex *e) { + if (!flight_on || !e->pub.mirrored || e->t.L.size < 64 * 64) return; + if (!mem_read_hold(e->pub.addr, e->t.L.size)) return; + const uint8_t *b = (const uint8_t *)(uintptr_t)e->pub.addr; + uint64_t step = (e->t.L.size - 64) / 63; + uint32_t h = 0x811c9dc5u, zero = 0; + for (int i = 0; i < 64; i++) { + const uint8_t *p = b + (uint64_t)i * step; + uint8_t any = 0; + for (int k = 0; k < 64; k++) h = (h ^ p[k]) * 0x01000193u, any |= p[k]; + zero += !any; + } + mem_read_release(); + flight_ev(e, FLIGHT_TEX_BYTES, h, zero); +} + static uint64_t key_hash(const TexKey *k) { // FNV-1a; make_key zeroes the key's padding uint64_t h = 0xcbf29ce484222325ull; const uint8_t *b = (const uint8_t *)k; @@ -2722,6 +2741,10 @@ void vk_pool_marks_read(const uint32_t *marks, uint32_t n) { followed = 1; if (trace) marks_check(p, id, now, 1); } + if (flight_on && !followed) // nothing of its own and nothing followed: its stand-in was on screen + flight_tex(FLIGHT_TEX_BLANK, k.addr, (uint32_t)s->t.d.format, s->t.d.width, s->t.d.height, 0, + tex_mirrored(&s->t) ? FLIGHT_TF_MIRRORED : 0, + (s->finalized ? 1u : 0) | (holds.n && nvn_hm_get(&holds, &k) ? 2u : 0), id); // The slot's own key may be held although an entry at its address took the mark: the slot shows the stand-in // then (slot_resolve follows fewer entries than the loop above credits), and only this drop makes its texture // again. Left to the case where the loop found nothing, such a slot kept the stand-in for good. @@ -3348,6 +3371,7 @@ static void upload(Tex *e) { n_uploads++, perf_count(PERF_CNT_TEX_UP); } flight_ev(e, e->uploaded ? FLIGHT_TEX_REUP : FLIGHT_TEX_UP, e->wt_n, (uint32_t)(dt / 1000)); + flight_bytes(e); e->wt_n = 0; e->uploaded = 1; e->n_up++; @@ -4073,6 +4097,10 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { unlock(); return; } + if (flight_on && tex_mirrored(t)) + flight_tex(FLIGHT_TEX_REG, (uint64_t)(uintptr_t)t->mem, (uint32_t)t->d.format, t->d.width, t->d.height, 0, + FLIGHT_TF_MIRRORED, + !s->used ? 0 : s->finalized ? 3 : (uintptr_t)s->t.mem == (uintptr_t)t->mem ? 1 : 2, (uint32_t)id); s->t = *t; if (!s->used && id < VK_POOL_TEX) pool_slots++; s->used = 1; From 76c040d35b320213bf3614fee4a66088f03bf21b Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:12:23 -0500 Subject: [PATCH 05/14] Record a pool slot losing its picture in the flight recorder. When the pool's entry of a slot goes from a texture's view to the stand-in, events.tsv gets a line with the slot and what its own texture was at that moment: finalized, released, superseded, overwritten, uploaded, its storage readable, held out. The set is written with update-after-bind, so every draw of the submission that samples the slot reads the stand-in from then on; the check at the marks' read-back comes a frame later and finds the slot resolved again. Co-Authored-By: Claude Fable 5.2 --- runtime/flight.c | 8 +++++++- runtime/flight.h | 2 ++ runtime/vk.h | 4 ++++ runtime/vk_exec_draw.c | 29 +++++++++++++++++++++++++++-- runtime/vk_texture.c | 15 +++++++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/runtime/flight.c b/runtime/flight.c index c55f23dc..89827e7a 100644 --- a/runtime/flight.c +++ b/runtime/flight.c @@ -157,7 +157,7 @@ static void copy_events(DumpJob *j, uint64_t first) { } static const char *const tex_kind_names[FLIGHT_TEX_N] = {"new", "up", "reup", "release", "takeback", "drop", "bytes", - "reg", "blank"}; + "reg", "blank", "toblank"}; // /events.tsv: one line per texture event of the dump's frames (flight_tex). static void write_events(const DumpJob *j) { @@ -192,6 +192,12 @@ static void write_events(const DumpJob *j) { static const char *const was[4] = {"an unused slot", "the slot named another descriptor at this storage", "the slot named other storage", "the slot named a finalized texture"}; snprintf(detail, sizeof detail, "slot %u: %s", e->us, was[e->detail & 3]); + } else if (e->kind == FLIGHT_TEX_TOBLANK) { + static const char *const bit[] = {"finalized", "own entry", "released", "superseded", "overwritten", "uploaded", + "storage readable", "held out", "CPU-written", "GPU-owned", "retiring"}; + int o = snprintf(detail, sizeof detail, "slot %u lost its picture:", e->us); + for (int b = 0; b < 11 && o < (int)sizeof detail - 20; b++) + if (e->detail & 1u << b) o += snprintf(detail + o, sizeof detail - (size_t)o, " %s;", bit[b]); } else if (e->kind == FLIGHT_TEX_BLANK) snprintf(detail, sizeof detail, "slot %u sampled as its stand-in%s%s", e->us, e->detail & 1 ? ", finalized" : "", e->detail & 2 ? ", held out" : ""); diff --git a/runtime/flight.h b/runtime/flight.h index 5a15dcad..d29c982d 100644 --- a/runtime/flight.h +++ b/runtime/flight.h @@ -26,6 +26,8 @@ enum { FLIGHT_TEX_NEW, FLIGHT_TEX_UP, FLIGHT_TEX_REUP, FLIGHT_TEX_RELEASE, FLIGH // 0 nothing, 1 another descriptor at this storage, 2 other storage, 3 a finalized texture) FLIGHT_TEX_BLANK, // a shader sampled a slot that has no image of its own and follows none: its stand-in was // on screen (us: the slot; detail: bit 0 the slot is finalized, bit 1 it is held out) + FLIGHT_TEX_TOBLANK, // the pool's entry of a slot went from a texture's view to the stand-in (us: the slot; + // detail: vk_texture_slot_why_locked's bits, what the slot's own texture was then) FLIGHT_TEX_N }; // The texture's flags: a depth or stencil image, mirrored (the CPU writes its storage), bound into pool memory, an ASTC // texture, written by the GPU (VkTexImage.gpu_owned). diff --git a/runtime/vk.h b/runtime/vk.h index 76c1fc75..763ac012 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -585,6 +585,10 @@ const VkTexImage *vk_texture_exact_by_id_locked(int id, view_t *view, int *has_v uint32_t vk_texture_slots_gen(void); int vk_texture_slot_count_locked(void); void vk_texture_create_budget_locked(int n); // slot lookups create at most n entries (mapped storage only); -1 any +// For the flight recorder: what slot id's own texture is now, as bits (1 the slot is finalized, 2 an entry of its own +// descriptor exists, 4 that entry is released, 8 superseded, 16 overwritten, 32 uploaded, 64 the storage is readable, +// 128 held out by disuse, 256 CPU-written, 512 GPU-owned, 1024 retiring), with the slot's storage and size. +uint32_t vk_texture_slot_why_locked(int id, uint64_t *addr, uint32_t *format, uint32_t *w, uint32_t *h); uint64_t vk_texture_pool_make_ns_take(void); // ns the pool's lookups spent making and first uploading entries since the last call int vk_texture_create_deferred_locked(void); // a lookup since the last call left a creation to the budget // one past the highest texture slot registered so far (or more) int vk_sampler_slot_count_locked(void); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index e382fce3..f28c02bd 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -19,6 +19,7 @@ #include #include "cfg.h" +#include "flight.h" #include "host.h" #include "nvn_api.h" #include "nvn_hmap.h" @@ -2446,7 +2447,25 @@ static void pool_unres(uint32_t id, int got) { PL.unres[PL.nunres++] = (int)id; } +// For the flight recorder: v is one of the stand-in views. +static int pool_view_blank(VkImageView v) { + for (int i = 0; i < DV_COUNT; i++) + if (v == Z.img[i].view || v == Z.img[i].sview) return 1; + return 0; +} + +// Slot id's entry of a kind went from the view was to now: noted when a texture's view gives way to the stand-in, which +// every draw of the submission that samples the slot then reads (the set is written with update-after-bind). +static void pool_note_blank(uint32_t id, VkImageView was, VkImageView now, int *noted) { + if (!flight_on || *noted || !was || pool_view_blank(was) || !pool_view_blank(now)) return; + uint64_t addr; + uint32_t fmt, w, h, why = vk_texture_slot_why_locked((int)id, &addr, &fmt, &w, &h); + flight_tex(FLIGHT_TEX_TOBLANK, addr, fmt, w, h, 0, 0, why, id); + *noted = 1; +} + static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { + int noted = 0; vk_texture_create_deferred_locked(); PoolSlot *ps = &PL.ps[id]; uint64_t kh = 0; @@ -2468,7 +2487,10 @@ static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { for (int k = 0; k < VK_POOL_KINDS; k++) { if (!(mask >> k & 1)) continue; VkImageView *e = &PL.view[(size_t)k * VK_POOL_TEX + id]; - if (*e != m->view[k]) *e = m->view[k], pool_write((uint32_t)k, id, m->view[k], VK_NULL_HANDLE); + if (*e != m->view[k]) { + pool_note_blank(id, *e, m->view[k], ¬ed); + *e = m->view[k], pool_write((uint32_t)k, id, m->view[k], VK_NULL_HANDLE); + } } ps->fmask = ps->stamp == m->stamp ? ps->fmask | mask : mask; ps->stamp = m->stamp; @@ -2482,7 +2504,10 @@ static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { VkImage img; tex_info_full(ex, &r, tok ? id | 1ull << 63 : 0, 0, 1, &ii, &img); VkImageView *e = &PL.view[(size_t)k * VK_POOL_TEX + id]; - if (*e != ii.imageView) *e = ii.imageView, pool_write((uint32_t)k, id, ii.imageView, VK_NULL_HANDLE); + if (*e != ii.imageView) { + pool_note_blank(id, *e, ii.imageView, ¬ed); + *e = ii.imageView, pool_write((uint32_t)k, id, ii.imageView, VK_NULL_HANDLE); + } } int ok = !vk_texture_create_deferred_locked(); uint32_t ent = 0; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index ccf82760..adeb458a 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -4256,6 +4256,21 @@ static Tex *slot_resolve(TexSlot *s, view_t *view, int *has_view) { return e; } +uint32_t vk_texture_slot_why_locked(int id, uint64_t *addr, uint32_t *format, uint32_t *w, uint32_t *h) { + *addr = 0, *format = *w = *h = 0; + if (id < 0 || id >= ntslots || !tslots[id].used) return 0; + const TexSlot *s = &tslots[id]; + TexKey k; + make_key(&s->t, &k); + const Tex *own = nvn_hm_get(&by_key, &k); + *addr = k.addr, *format = (uint32_t)s->t.d.format, *w = s->t.d.width, *h = s->t.d.height; + return (s->finalized ? 1u : 0) | (own ? 2u : 0) | (own && own->released ? 4u : 0) | (own && own->superseded ? 8u : 0) | + (own && own->overwritten ? 16u : 0) | (own && own->uploaded ? 32u : 0) | + (s->t.mem && mem_readable((uint64_t)(uintptr_t)s->t.mem, s->t.L.size) ? 64u : 0) | + (holds.n && nvn_hm_get(&holds, &k) ? 128u : 0) | (tex_mirrored(&s->t) ? 256u : 0) | + (own && own->pub.gpu_owned ? 512u : 0) | (own && own->retiring ? 1024u : 0); +} + void vk_texture_lock(void) { lock(); } void vk_texture_unlock(void) { unlock(); } uint32_t vk_texture_slots_gen(void) { return slots_gen; } From b93219c39425ac56ac52d0d39ce5fd52957c04af Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:19:19 -0500 Subject: [PATCH 06/14] Make a slot's new texture at once when the slot is showing another one. Ocarina streams its textures by detail level: while the player moves it registers about 32 pool slots with their next level every four or five frames. A pool refresh made 16 of them and wrote the stand-in into the set for the rest until its next round; the set is read as it is when its submission goes to the queue, so an object whose slot was among the rest could be drawn grey for a frame. A player's recording shows 1,525 such slots in 600 frames, each with its texture made later in the same frame. A slot whose entry in the set is a texture's view when it is looked up now has its texture made by that lookup, beside the count budget and the frame's creation time, 64 of them a present; pool_shown_first = 0 leaves them to the budget. The flight recorder also notes a slot that still had no picture when its submission ended. Co-Authored-By: Claude Fable 5.2 --- runtime/cfg.c | 5 +++-- runtime/flight.c | 7 ++++--- runtime/flight.h | 2 ++ runtime/vk.h | 3 +++ runtime/vk_exec_draw.c | 37 +++++++++++++++++++++++++++++++++++++ runtime/vk_texture.c | 26 +++++++++++++++++++++++++- 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 6da339d8..64831cb9 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -54,7 +54,7 @@ static const char *const known[] = { "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_HUE_CLIP", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", "HDR_SATURATION", "HDR_SHADOWS", "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", "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", + "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", "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", "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", @@ -132,7 +132,7 @@ static const struct { const char *key, *section; } sections[] = { {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, {"PIPE_FRAME_WAIT_MS", "performance"}, {"PIPE_SKIP_STEADY", "performance"}, {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, - {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"}, + {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"POOL_SHOWN_FIRST", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"}, {"COMPUTE_KEEP", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_SPLIT_FILL", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"WATER_PROJECTIVE", "performance"}, @@ -272,6 +272,7 @@ static const CfgMeta metas[] = { {"MESH_FALLBACK_WORKERS", "auto", "auto|1|2|3|4", RESTART}, // vk_exec_draw.c mfb_worker_count {"PIPE_AHEAD", "1", "0|1", RESTART}, // vk_exec.c vk_exec_ahead, vk_pipeline.c vk_pipeline_hint {"LINE_WIDTH", "1", "0|1", RESTART}, // vk_pipeline.c key_lines + {"POOL_SHOWN_FIRST", "1", "0|1", RESTART}, // vk_texture.c shown_take {"GPL_FEEDBACK", "1", "0|1", RESTART}, // vk_pipeline.c gpl_feedback {"PIPE_FAST_WORKERS", "1", "0|1", RESTART}, // vk_pipeline.c lib_worker, fast_take {"FRAME_HOLD_MS", "120", "0..500", RESTART}, // vk_backend.c frame_hold diff --git a/runtime/flight.c b/runtime/flight.c index 89827e7a..3ae714c4 100644 --- a/runtime/flight.c +++ b/runtime/flight.c @@ -157,7 +157,7 @@ static void copy_events(DumpJob *j, uint64_t first) { } static const char *const tex_kind_names[FLIGHT_TEX_N] = {"new", "up", "reup", "release", "takeback", "drop", "bytes", - "reg", "blank", "toblank"}; + "reg", "blank", "toblank", "blanksub"}; // /events.tsv: one line per texture event of the dump's frames (flight_tex). static void write_events(const DumpJob *j) { @@ -192,10 +192,11 @@ static void write_events(const DumpJob *j) { static const char *const was[4] = {"an unused slot", "the slot named another descriptor at this storage", "the slot named other storage", "the slot named a finalized texture"}; snprintf(detail, sizeof detail, "slot %u: %s", e->us, was[e->detail & 3]); - } else if (e->kind == FLIGHT_TEX_TOBLANK) { + } else if (e->kind == FLIGHT_TEX_TOBLANK || e->kind == FLIGHT_TEX_BLANKSUB) { static const char *const bit[] = {"finalized", "own entry", "released", "superseded", "overwritten", "uploaded", "storage readable", "held out", "CPU-written", "GPU-owned", "retiring"}; - int o = snprintf(detail, sizeof detail, "slot %u lost its picture:", e->us); + int o = snprintf(detail, sizeof detail, e->kind == FLIGHT_TEX_TOBLANK ? "slot %u lost its picture:" + : "slot %u had no picture when its submission ended:", e->us); for (int b = 0; b < 11 && o < (int)sizeof detail - 20; b++) if (e->detail & 1u << b) o += snprintf(detail + o, sizeof detail - (size_t)o, " %s;", bit[b]); } else if (e->kind == FLIGHT_TEX_BLANK) diff --git a/runtime/flight.h b/runtime/flight.h index d29c982d..c5926458 100644 --- a/runtime/flight.h +++ b/runtime/flight.h @@ -28,6 +28,8 @@ enum { FLIGHT_TEX_NEW, FLIGHT_TEX_UP, FLIGHT_TEX_REUP, FLIGHT_TEX_RELEASE, FLIGH // on screen (us: the slot; detail: bit 0 the slot is finalized, bit 1 it is held out) FLIGHT_TEX_TOBLANK, // the pool's entry of a slot went from a texture's view to the stand-in (us: the slot; // detail: vk_texture_slot_why_locked's bits, what the slot's own texture was then) + FLIGHT_TEX_BLANKSUB,// a slot that lost its picture during a submission still had none when the next one began: + // the submission's draws read its stand-in (us and detail as TOBLANK) FLIGHT_TEX_N }; // The texture's flags: a depth or stencil image, mirrored (the CPU writes its storage), bound into pool memory, an ASTC // texture, written by the GPU (VkTexImage.gpu_owned). diff --git a/runtime/vk.h b/runtime/vk.h index 763ac012..5c858e9f 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -589,6 +589,9 @@ void vk_texture_create_budget_locked(int n); // slot lookups create at m // descriptor exists, 4 that entry is released, 8 superseded, 16 overwritten, 32 uploaded, 64 the storage is readable, // 128 held out by disuse, 256 CPU-written, 512 GPU-owned, 1024 retiring), with the slot's storage and size. uint32_t vk_texture_slot_why_locked(int id, uint64_t *addr, uint32_t *format, uint32_t *w, uint32_t *h); +// The pool's next lookup is of a slot whose entry in the set is a texture's view now (1) or a stand-in (0): a slot on +// screen whose texture is not made is made by that lookup beside the creation budget (vk_texture.c slot_resolve). +void vk_texture_pool_shown_locked(int shown); uint64_t vk_texture_pool_make_ns_take(void); // ns the pool's lookups spent making and first uploading entries since the last call int vk_texture_create_deferred_locked(void); // a lookup since the last call left a creation to the budget // one past the highest texture slot registered so far (or more) int vk_sampler_slot_count_locked(void); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index f28c02bd..49010724 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2456,14 +2456,48 @@ static int pool_view_blank(VkImageView v) { // Slot id's entry of a kind went from the view was to now: noted when a texture's view gives way to the stand-in, which // every draw of the submission that samples the slot then reads (the set is written with update-after-bind). +static uint32_t pool_blanked[512]; // the slots that lost their picture since the last pool_blank_check +static int pool_nblanked; static void pool_note_blank(uint32_t id, VkImageView was, VkImageView now, int *noted) { if (!flight_on || *noted || !was || pool_view_blank(was) || !pool_view_blank(now)) return; uint64_t addr; uint32_t fmt, w, h, why = vk_texture_slot_why_locked((int)id, &addr, &fmt, &w, &h); flight_tex(FLIGHT_TEX_TOBLANK, addr, fmt, w, h, 0, 0, why, id); + if (pool_nblanked < (int)(sizeof pool_blanked / sizeof *pool_blanked)) pool_blanked[pool_nblanked++] = id; *noted = 1; } +// A submission's first sync, before it changes anything: the slots that lost their picture during the submission +// before and have none in any kind now went to the queue with their stand-ins. Texture lock taken here. +static void pool_blank_check(void) { + if (!pool_nblanked) return; + vk_texture_lock(); + for (int i = 0; i < pool_nblanked; i++) { + uint32_t id = pool_blanked[i]; + int real = 0; + for (int k = 0; k < VK_POOL_KINDS && !real; k++) { + VkImageView v = PL.view[(size_t)k * VK_POOL_TEX + id]; + real = v && !pool_view_blank(v); + } + if (real) continue; + uint64_t addr; + uint32_t fmt, w, h, why = vk_texture_slot_why_locked((int)id, &addr, &fmt, &w, &h); + flight_tex(FLIGHT_TEX_BLANKSUB, addr, fmt, w, h, 0, 0, why, id); + } + vk_texture_unlock(); + pool_nblanked = 0; +} + +// Whether slot id's entry of a kind in mask is a texture's view now (vk_texture_pool_shown_locked). +static int pool_slot_shown(uint32_t id, uint32_t mask) { + for (int k = 0; k < VK_POOL_KINDS; k++) { + if (!(mask >> k & 1)) continue; + VkImageView v = PL.view[(size_t)k * VK_POOL_TEX + id]; + if (v && !pool_view_blank(v)) return 1; + } + return 0; +} + static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { int noted = 0; vk_texture_create_deferred_locked(); @@ -2496,6 +2530,7 @@ static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { ps->stamp = m->stamp; return 1; } + vk_texture_pool_shown_locked(tok && pool_slot_shown(id, mask)); for (int k = 0; k < VK_POOL_KINDS; k++) { if (!(mask >> k & 1)) continue; GfxShaderRes r = {0}; @@ -2509,6 +2544,7 @@ static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { *e = ii.imageView, pool_write((uint32_t)k, id, ii.imageView, VK_NULL_HANDLE); } } + vk_texture_pool_shown_locked(0); int ok = !vk_texture_create_deferred_locked(); uint32_t ent = 0; int got = vk_texture_pool_note_locked((int)id, &ent); @@ -2579,6 +2615,7 @@ static void pool_sync(VkExec *ex) { if (!PL.layout) vk_pool_set_layout(); uint64_t from = 0, pre = PL.logn; int took = PL.cur_fence != ex->fence; + if (took && flight_on) pool_blank_check(); // what the submission before went to the queue with int switched = pool_take_set(ex, &from); // every submission its own set, before the shortcut if (PL.sealed) return; // current when checked: a change since waits for the next sync, which takes a new set uint64_t t0 = kern_now_ns(), f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index adeb458a..ad901336 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -4155,6 +4155,30 @@ static int want_take(void) { void vk_texture_create_budget_locked(int n) { create_budget = n, create_deferred = 0; } +// NX2NAVI_POOL_SHOWN_FIRST (default 1; 0: such a slot waits for the budget like any other): the slot being looked up +// shows a texture in the pool's set now (vk_exec_draw.c pool_fill). When its own texture is not made, the game has +// registered the slot with another texture, and the draws that sampled the old one sample this one next: it is made at +// this lookup, beside the count budget and the frame's creation time, SHOWN_PER_FRAME of them a present. Ocarina +// streams its textures by detail level and registers about 32 slots with their next level every four or five frames +// while the player moves (2,300 to 3,800 registrations in 600 frames of two recordings). A refresh made 16 of them and +// wrote the stand-in into the set for the others until its next round, and the set is read as it is when its +// submission goes to the queue: an object whose slot was among the others was drawn grey for a frame (three lamp +// posts, the windows of a house front in the recordings). An area load registers thousands of slots that showed the +// area before; past the per-present count they wait for the budget as before. +#define SHOWN_PER_FRAME 64 +static int pool_shown; +void vk_texture_pool_shown_locked(int shown) { pool_shown = shown; } +static int shown_take(void) { + static int on = -1; + static uint64_t frame = ~0ull; + static int left; + if (on < 0) on = cfg_bool("NX2NAVI_POOL_SHOWN_FIRST"); + if (!on || !pool_shown) return 0; + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame) frame = f, left = SHOWN_PER_FRAME; + return left > 0 ? left-- : 0; +} + // What the pool's lookups have spent making entries (entry_of under the budget) and uploading them the first time, // since the last call: the time vk_exec_draw.c pool_create_ns limits in a frame. Texture lock held where it is added. static uint64_t pool_make_ns; @@ -4224,7 +4248,7 @@ static Tex *slot_resolve(TexSlot *s, view_t *view, int *has_view) { // was about a thousand slots at once, which vk_exec_draw.c pool_sync_entries took for an area load, several // times a second in some places, and each time lifted the count budget and the frame's creation time. else if (!mem_readable((uint64_t)(uintptr_t)s->t.mem, s->t.L.size)) e = NULL; - else if (!create_budget && !(s->cwant && want_take())) create_deferred = 1; + else if (!create_budget && !(s->cwant && want_take()) && !shown_take()) create_deferred = 1; else { uint64_t m0 = kern_now_ns(); e = entry_of(&s->t); From a654d079e1de0a8a85c57e6b2b4c606d42c4ca95 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:25:41 -0500 Subject: [PATCH 07/14] Keep a released texture's image while a registered slot still names it. The game finalizes a texture object while another one it initialized at the same storage with the same parameters stays registered in the pool, and the entry the two share is released all the same. Two frames later its image went to the next texture of its parameters and the entry was dropped; the slot then had nothing to show, because the pool makes no entry for storage the CPU cannot read, and kept its stand-in until the game drew into the texture again. Ocarina's 384x384 shadow maps are such textures: a player's recording has four slots of them sampled as stand-ins in the frame after three of their images had gone to other shadow maps, and a house front without its shadow in that frame. The reuse now passes over a released entry that a slot which is not finalized names; tex_reuse_named = 1 restores the old choice. Co-Authored-By: Claude Fable 5.2 --- runtime/cfg.c | 5 +++-- runtime/vk_texture.c | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 64831cb9..cfad52b5 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -54,7 +54,7 @@ static const char *const known[] = { "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_HUE_CLIP", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", "HDR_SATURATION", "HDR_SHADOWS", "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", "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", + "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", "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", @@ -132,7 +132,7 @@ static const struct { const char *key, *section; } sections[] = { {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, {"PIPE_FRAME_WAIT_MS", "performance"}, {"PIPE_SKIP_STEADY", "performance"}, {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, - {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"POOL_SHOWN_FIRST", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"}, + {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"POOL_SHOWN_FIRST", "performance"}, {"TEX_REUSE_NAMED", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"}, {"COMPUTE_KEEP", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_SPLIT_FILL", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"WATER_PROJECTIVE", "performance"}, @@ -273,6 +273,7 @@ static const CfgMeta metas[] = { {"PIPE_AHEAD", "1", "0|1", RESTART}, // vk_exec.c vk_exec_ahead, vk_pipeline.c vk_pipeline_hint {"LINE_WIDTH", "1", "0|1", RESTART}, // vk_pipeline.c key_lines {"POOL_SHOWN_FIRST", "1", "0|1", RESTART}, // vk_texture.c shown_take + {"TEX_REUSE_NAMED", "0", "0|1", RESTART}, // vk_texture.c slot_names {"GPL_FEEDBACK", "1", "0|1", RESTART}, // vk_pipeline.c gpl_feedback {"PIPE_FAST_WORKERS", "1", "0|1", RESTART}, // vk_pipeline.c lib_worker, fast_take {"FRAME_HOLD_MS", "120", "0..500", RESTART}, // vk_backend.c frame_hold diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index ad901336..56fa56a2 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -674,6 +674,36 @@ static int tex_reuse_on(void) { static void destroy_tex(Tex *e); +// NX2NAVI_TEX_REUSE_NAMED (default 0; 1: as before, any released entry's image may go): a registered slot that is not +// finalized names e's descriptor, so a shader can still sample e through the pool. The game finalizes a texture object +// while another one it initialized at the same storage with the same parameters stays registered (vk_texture_release +// leaves that slot current), and the entry they share is released all the same. Its image then went to the next +// texture of its parameters two frames later (create_tex, reuse), the entry was dropped, and the slot had nothing to +// show: the pool does not make an entry for storage the CPU cannot read, so the slot kept its stand-in until the game +// drew into the texture again. Ocarina's 384x384 shadow maps are such textures: in a player's recording four slots of +// them were sampled as stand-ins in the frame after three of their images had gone to other shadow maps, and the +// front of a house lost its shadow for that frame. Such an entry keeps its image. +static uint64_t n_reuse_named; // released entries left out of the reuse because a current slot names them +static int slot_names(const Tex *e) { + static int off = -1; + if (off < 0) off = cfg_bool("NX2NAVI_TEX_REUSE_NAMED"); + if (off || !sinfo) return 0; + for (int id = bucket_head[agen_idx(e->key.addr)]; id >= 0; id = slot_next[id]) { + const TexSlot *s = &tslots[id]; + if (s->finalized || (uintptr_t)s->t.mem != e->key.addr) continue; + TexKey sk; + make_key(&s->t, &sk); + if (memcmp(&sk, &e->key, sizeof sk)) continue; + if (!n_reuse_named++ || !(n_reuse_named & (n_reuse_named - 1))) // the first, then at each power of two + rt_log("vk: a released texture keeps its image, a registered slot still names it: %#llx fmt %#x %ux%u, slot " + "%d (%llu so far; NX2NAVI_TEX_REUSE_NAMED=1 lets such an image go to another texture)", + (unsigned long long)e->key.addr, e->t.d.format, e->t.d.width, e->t.d.height, id, + (unsigned long long)n_reuse_named); + return 1; + } + return 0; +} + // Why an entry was destroyed (drop_tex), in the order of drop_names. enum { DROP_NONE, DROP_MAX, DROP_CAP, DROP_COMMIT, DROP_VRAM, DROP_IDLE_VRAM, DROP_IDLE_COMMIT, DROP_DISUSE, DROP_OVERWRITTEN, DROP_REPLACED, DROP_REUSED, DROP_RANGE, DROP_EXACT, DROP_SHUTDOWN, DROP_N }; @@ -1244,9 +1274,10 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { for (Tex *q = nvn_hm_get(&reuse, &e->ik); q && n < 128; q = q->ru_next, n++) { if (q->rel_epoch == submit_epoch || now - q->rel_frame < 2 || q->key.addr == k->addr) continue; uint64_t dist = q->key.addr > k->addr ? q->key.addr - k->addr : k->addr - q->key.addr; + // not one a current slot still names (slot_names), asked only of a candidate that would be the best if (q->overwritten) { - if (dist < best) best = dist, a = q; - } else if (dist < best_kept) best_kept = dist, kept = q; + if (dist < best && !slot_names(q)) best = dist, a = q; + } else if (dist < best_kept && !slot_names(q)) best_kept = dist, kept = q; } if (!a) a = kept; if (a) { From d991c04284e2b59f2afe0e32e3db133209159a52 Mon Sep 17 00:00:00 2001 From: Leonardo Chappuis Date: Thu, 12 Nov 2026 12:23:32 -0300 Subject: [PATCH 08/14] Fix a deadlock between the replay workers in vk_exec_pass_end. vk_exec_pass_end took tfr_lock and then, in vk_texture_gpu_written, the texture lock. A chunk recorded by another thread can hold the texture lock in the pool sync and end its pass there (a lookup's alias copy through vk_exec_inline_begin), and then wait for tfr_lock. The two chunks waited for each other and the other chunks spun in their turns, so loading after Continue never finished with replay_workers = 3. The targets are now marked written before tfr_lock is taken, which guards only the target history. Co-Authored-By: Claude Opus 5.5 --- runtime/vk_exec.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 9dcbb025..8e8371b2 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -899,16 +899,24 @@ void vk_exec_pass_end(VkExec *ex) { bc_copy_back(ex, &ex->color[i]); if (vk_dump_armed) vk_dump_pass_end(ex); uint64_t stamp = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1; + // The targets are marked written outside tfr_lock: vk_texture_gpu_written takes the texture lock, and a chunk that + // holds the texture lock can end its pass here (a lookup's alias copy, vk_exec_inline_begin) and then wait for + // tfr_lock. Taken the other way round, a chunk holding tfr_lock and waiting for the texture lock deadlocked with it, + // the other chunks spinning in their turns (the loading screen after Continue, 3 replay workers). tfr_lock guards + // only the target history and takes no other lock inside. + for (int i = 0; i < 8; i++) + if (ex->color[i].image) vk_texture_gpu_written(ex->color[i].tex); + if (ex->depth.image) vk_texture_gpu_written(ex->depth.tex); int lk = vk_exec_shared(); if (lk) plat_mutex_lock(&tfr_lock); for (int i = 0; i < 8; i++) if (ex->color[i].image) { TfrTarget ft = tfr_of(&ex->color[i]); - vk_texture_gpu_written(ex->color[i].tex), tfr_note(&ft, stamp); + tfr_note(&ft, stamp); } if (ex->depth.image) { TfrTarget ft = tfr_of(&ex->depth); - vk_texture_gpu_written(ex->depth.tex), tfr_note(&ft, stamp); + tfr_note(&ft, stamp); } if (lk) plat_mutex_unlock(&tfr_lock); vk_dlaa_pass_end(ex); // a jittered pass left its targets' outer rows or columns undrawn From 87611f150c8a61b349045bb954f01d603e9221ca 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 09/14] 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 38afed16d5bbdbb8b4ccf14665704f3661161023 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 10/14] 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 569d6752..773986b2 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -255,6 +255,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 49010724..08279761 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 b29cbbe6a6acaccebfa57f57a0ea65530d3a28e8 Mon Sep 17 00:00:00 2001 From: "automata." Date: Thu, 12 Nov 2026 10:35:57 -0500 Subject: [PATCH 11/14] 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 0aaabd79..83669245 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; @@ -1255,7 +1267,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 @@ -1264,6 +1279,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 d150e472..45f31c86 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -99,3 +99,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 08279761..60d67d7b 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 0985e80466473409fb1f5c1801f5faff15fdbe8c Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 10:44:12 -0500 Subject: [PATCH 12/14] 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 cfad52b5..e389119a 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); From bfa486c2aa98b1b16d9b68a6141ae1269e5939c7 Mon Sep 17 00:00:00 2001 From: Vasilis Manetas Date: Thu, 12 Nov 2026 16:26:26 +0100 Subject: [PATCH 13/14] Hand every queue submission to the driver's own submit thread on Linux, so RADV's vkQueueSubmit2 leaves the replaying thread. Mesa's Vulkan drivers keep timeline semaphores as kernel sync objects, and their queues submit on the calling thread unless a submission waits on a value not submitted yet. The replay hands a part of the frame to the queue each time it splits one off (vk_exec.c maybe_split), so every such vkQueueSubmit2, with the command stream's checks and the kernel's ioctl, ran inside the replay, the thread that limits the frame rate on an RX 6800 XT under RADV. MESA_VK_ENABLE_SUBMIT_THREAD=1 sends every submission to a thread of the queue's own. vk_device.c mesa_submit_thread sets it before any thread can load the driver, unless the environment sets it already. submit_thread in nx2navi.ini (NX2NAVI_SUBMIT_THREAD) is 1 by default on Linux and 0 on Windows; 0 leaves the driver's own choice. On a Mesa driver the log says which one applies. Kokiri Forest, 0.9.1, RX 6800 XT, Mesa 26.2.6, 3 pairs of 45 s runs of one build with the setting on and off: the replay took 13.9 ms a frame against 15.0, lower in every pair; frames over 25 ms fell from 910 to 406; the frame rate went from 40.0 to 40.8 fps, within the runs' noise. --- nx2navi.ini.example | 4 ++++ runtime/cfg.c | 9 +++++++-- runtime/vk_device.c | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index fe04570f..725cde21 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -89,6 +89,10 @@ # recorder_core = 0 # Submissions handed to the GPU queue together in one call, 1 to 16 (1 = each one at once). # submit_batch = 4 +# Mesa's Vulkan drivers (RADV, ANV, NVK): every submission to the GPU queue goes to a thread of the queue's own, so its +# kernel call leaves the thread that prepares the frames, 0 or 1. Default: on (Linux), off (Windows). 0 leaves the +# driver's own choice. Unused when MESA_VK_ENABLE_SUBMIT_THREAD is set. +# submit_thread = 1 # Occlusion query results are read back from the video card in batches, 0 or 1. Default: on. 0 reads each query with a # call of its own, as before, which on AMD's Windows driver held every frame's "GPU done" back by about 4 ms. # oq_batch = 1 diff --git a/runtime/cfg.c b/runtime/cfg.c index e389119a..a6524eb5 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -75,7 +75,7 @@ static const char *const known[] = { "VK_PRESENT", "VK_RECONVERGE", "VK_NAN_BITS", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", "WATER_PROJECTIVE", "WAIT_WATCH_S", "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WB_NO_REUP", "WB_MERGE_MAX", "WB_PROBE", "WB_SPLIT", "WB_LAZY", "WB_LAZY_CHUNK", "WB_LAZY_TOUCH", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", - "BIND_REUSE", "DYN_DELTA", "STG_ARENA", "SUBMIT_BATCH", "ASTC_GPU_DESWIZZLE", "ASTC_SCRATCH_KEEP", "ASTC_ARENA", "ASTC_BC1", + "BIND_REUSE", "DYN_DELTA", "STG_ARENA", "SUBMIT_BATCH", "SUBMIT_THREAD", "ASTC_GPU_DESWIZZLE", "ASTC_SCRATCH_KEEP", "ASTC_ARENA", "ASTC_BC1", "TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", "TEXTURE_DETAIL", "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_FINI_IDLE", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS", "VMA_BLOCK_MB", "SYNC_WAKE_ALL", "LOG_ASYNC", "DEVICE_LOST_AT", "STAGING_FAIL_AT", "VRAM_BALLAST_MB", @@ -126,7 +126,7 @@ static const struct { const char *key, *section; } sections[] = { {"FREE_CORES", "performance"}, {"DYN_RES", "performance"}, {"DYN_RES_TARGET", "performance"}, {"DYN_RES_STEP", "performance"}, {"RECORDER_CORE", "performance"}, {"RECORDER_PRIO", "performance"}, {"BIND_REUSE", "performance"}, - {"DYN_DELTA", "performance"}, {"STG_ARENA", "performance"}, {"SUBMIT_BATCH", "performance"}, + {"DYN_DELTA", "performance"}, {"STG_ARENA", "performance"}, {"SUBMIT_BATCH", "performance"}, {"SUBMIT_THREAD", "performance"}, {"BUILD_FREE_MB", "performance"}, {"BUILD_RESERVE_MB", "performance"}, {"MEM_NEED_MB", "diagnostics"}, {"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"ASTC_BC1", "performance"}, {"POOL_CREATE_BUDGET", "performance"}, {"POOL_CREATE_MS", "performance"}, {"POOL_FINI_IDLE", "performance"}, {"FREEZE_BUDGET_US", "performance"}, {"POOL_LOAD_BACKLOG", "performance"}, {"POOL_LOAD_MS", "performance"}, @@ -259,6 +259,11 @@ static const CfgMeta metas[] = { {"RECORDER_CORE", "0", "auto|0|1", RESTART}, {"RECORDER_PRIO", "1", "0|1", RESTART}, {"REPLAY_TIME", "0", "0|1", RESTART}, {"LOCK_TIME", "0", "0|1", RESTART}, {"POOL_CREATE_BUDGET", "16", "1..4096", RESTART}, {"POOL_CREATE_MS", "6", "0..100", RESTART}, {"POOL_FINI_IDLE", "0", "0|1", RESTART}, {"FREEZE_BUDGET_US", "1000", "0..100000", RESTART}, {"POOL_LOAD_BACKLOG", "512", "0..65536", RESTART}, {"POOL_LOAD_MS", "50", "1..1000", RESTART}, // held submissions handed to the queue together (vk_sync.c, submit batching) {"SUBMIT_BATCH", "4", "1..16", RESTART}, +#ifdef _WIN32 + {"SUBMIT_THREAD", "0", "0|1", RESTART}, // vk_device.c mesa_submit_thread: Mesa's submit thread (RADV) +#else + {"SUBMIT_THREAD", "1", "0|1", RESTART}, // vk_device.c mesa_submit_thread: Mesa's submit thread (RADV) +#endif {"CACHE_DIE", "1", "0|1|ab", RESTART}, {"ONE_PER_CORE", "0", "0|1|ab", RESTART}, // perf.c cpu_placement // shaders and pipelines {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "any", "strict|any|fullscreen", RESTART}, diff --git a/runtime/vk_device.c b/runtime/vk_device.c index b790a10c..9effdb76 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -123,6 +123,30 @@ static void nvidia_cache_beside_exe(void) { } #endif +// Mesa's Vulkan drivers on Linux (RADV, ANV, NVK) keep timeline semaphores as kernel sync objects, which cannot wait +// for a value not submitted yet. Their queues therefore submit on the calling thread and use a thread of the queue's +// own only for a submission that waits on such a value. Every other vkQueueSubmit2, the command stream's checks and +// the kernel's ioctl, runs on the replaying thread, once for every part the replay hands to the queue early (vk_exec.c +// maybe_split). MESA_VK_ENABLE_SUBMIT_THREAD=1 sends every submission to the queue's thread: in Kokiri Forest on an +// RX 6800 XT (Mesa 26.2) the replay took 13.9 ms a frame with it and 15.0 ms without. NX2NAVI_SUBMIT_THREAD (default +// 1 on Linux, 0 on Windows) sets it unless the environment already sets it; 0 leaves the driver's own choice. Drivers +// that do not read the variable ignore it. vk_driver_cache_env calls this before any thread can load the driver. +static const char *submit_thread_why; // create_device logs it on a Mesa driver + +static void mesa_submit_thread(void) { + char cur[16]; + if (plat_getenv("MESA_VK_ENABLE_SUBMIT_THREAD", cur, sizeof cur)) { + submit_thread_why = "MESA_VK_ENABLE_SUBMIT_THREAD is set in the environment"; + return; + } + if (!cfg_bool("NX2NAVI_SUBMIT_THREAD")) { + submit_thread_why = "NX2NAVI_SUBMIT_THREAD=0: the driver's own choice"; + return; + } + plat_setenv("MESA_VK_ENABLE_SUBMIT_THREAD", "1"); + submit_thread_why = "every one on the queue's own thread (NX2NAVI_SUBMIT_THREAD=0 leaves the driver's own choice)"; +} + // A driver may read these variables when it loads, and a thread can load the Vulkan driver before create_instance: // widescreen.c's wide_init asks for the monitor's size (fullscreen with a monitor chosen and the picture at the // window's size), and SDL's load of the Vulkan library there lists the instance extensions, which loads the drivers, @@ -134,6 +158,7 @@ void vk_driver_cache_env(void) { if (done) return; done = 1; mesa_cache_beside_exe(); + mesa_submit_thread(); #ifdef __linux__ nvidia_cache_beside_exe(); #endif @@ -507,6 +532,7 @@ static int create_device(void) { VkResult r = vk_dlssg_create_device(g_vk.instance, g_vk.phys, &di, &g_vk.device); free(c); if (r != VK_SUCCESS) { rt_log("vk: vkCreateDevice failed (%d)", (int)r); return 0; } + if (submit_thread_why && vk_caps.mesa) rt_log("vk: queue submissions: %s", submit_thread_why); vk_dlssg_device_ready(g_vk.phys, g_vk.device); if (vk_dlssg_dpa()) { // Streamline's, so its plugins hook every entry point the replay fetches vk_dpa = vk_dlssg_dpa(); From ac4af779735106301ca8b488cd15841739ca87fc Mon Sep 17 00:00:00 2001 From: FIocker <4461558+FIocker@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:17:31 +0100 Subject: [PATCH 14/14] Tell FSR 3.1 frame generation of camera cuts, so a generated frame is never blended from two unrelated pictures. vk_dlaa_fg_inputs hands out DLSS's new history with the frame's depth and motion, and FSR 4's and NVIDIA's frame generation pass it on, but vk_fg_frame dropped it: FSR 3.1 takes a cut only in the generation callback, which its swapchain calls later on its own thread. vk_fg_frame now notes each frame id's cut in a small ring (runtime/vk_fg_reset.h), and generate sets the callback's reset from it. A frame after one that was not prepared (the menu open, no inputs) counts as a cut too, and so does an id the ring no longer holds. Co-Authored-By: Claude Opus 5.5 --- docs/building.md | 5 +++++ runtime/vk_fg.cpp | 16 ++++++++++++-- runtime/vk_fg_reset.h | 32 ++++++++++++++++++++++++++++ tests/fg_reset_check.c | 46 +++++++++++++++++++++++++++++++++++++++++ tests/fg_reset_check.py | 40 +++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 runtime/vk_fg_reset.h create mode 100644 tests/fg_reset_check.c create mode 100644 tests/fg_reset_check.py diff --git a/docs/building.md b/docs/building.md index 1c243528..c6c0e9d8 100644 --- a/docs/building.md +++ b/docs/building.md @@ -473,6 +473,11 @@ builds the header alone and replays an area change after a loading screen from a half-resolution passes are per-frame again at once, a lookup table, a volume's slices and an icon capture still wait, and an image taken over by another texture starts without the history of the one before. No GPU, no game files. +A change to which frames FSR 3.1 frame generation is told are camera cuts (`runtime/vk_fg_reset.h`, `runtime/vk_fg.cpp` +`generate` and `vk_fg_frame`) passes `python tests\fg_reset_check.py`, which builds the header alone and checks that each +frame id gets its own frame's cut, that an id never noted or no longer held counts as a cut, and that a slot rewritten +on another thread never answers an old id with a newer frame's value. No GPU, no game files. + The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter change passes these, all run with `.venv`: diff --git a/runtime/vk_fg.cpp b/runtime/vk_fg.cpp index 3ed69bef..25f03cc8 100644 --- a/runtime/vk_fg.cpp +++ b/runtime/vk_fg.cpp @@ -28,6 +28,7 @@ extern "C" { #include "vk_dlssg.h" #include "vk_ffx.h" #include "vk_fg.h" +#include "vk_fg_reset.h" #include "ui/menu.h" } @@ -45,6 +46,9 @@ struct { int failed = 0; uint64_t generated = 0, paused = 0, no_inputs = 0, hudless = 0, no_hudless = 0; int on = 0; // the last configured frame had generation on (vk_fg_generating) + FgrRing cuts; // each frame id's camera cut, for the generation callback (vk_fg_reset.h) + uint64_t cut_frames = 0; // frames handed to the callback as cuts + int prepared = 0; // the last frame was prepared (generation on with its inputs) } F; VkResult submit_game(uint32_t n, const VkSubmitInfo *si, VkFence fence) { @@ -56,6 +60,8 @@ VkResult submit_game(uint32_t n, const VkSubmitInfo *si, VkFence fence) { ffxReturnCode_t generate(ffxDispatchDescFrameGeneration *d, void *user) { F.generated++; + // the swapchain's own reset (a frame id that did not follow the last) stays; a cut vk_fg_frame noted is added + if (fgr_cut(&F.cuts, d->frameID)) d->reset = true; return vk_ffx_dispatch((ffxContext *)user, &d->header); } @@ -267,6 +273,11 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, else if (!hudless) F.no_hudless++; else if (!on) F.paused++; F.frame++; + // A cut: DLSS started a new history (vk_dlaa_fg_inputs), or the frame before was not prepared (generation off for + // the menu, no inputs), so the frame between the two would be made from a stale depth and motion + fgr_note(&F.cuts, F.frame, reset || !F.prepared); + if ((reset || !F.prepared) && on) F.cut_frames++; + F.prepared = on; ffxConfigureDescFrameGeneration cfg = {}; cfg.header.type = FFX_API_CONFIGURE_DESC_TYPE_FRAMEGENERATION; cfg.swapChain = (void *)sc; @@ -312,9 +323,9 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, if (r != FFX_API_RETURN_OK) return fail("frame generation preparation", r); if (F.frame == 2 || F.frame % 3600 == 0) rt_log("fg: %llu frames presented, %llu generated (%llu with the HUD kept out); paused %llu (menu), %llu without depth " - "and motion, %llu without the HUD-less picture", (unsigned long long)F.frame, (unsigned long long)F.generated, + "and motion, %llu without the HUD-less picture; %llu after a camera cut", (unsigned long long)F.frame, (unsigned long long)F.generated, (unsigned long long)F.hudless, (unsigned long long)F.paused, (unsigned long long)F.no_inputs, - (unsigned long long)F.no_hudless); + (unsigned long long)F.no_hudless, (unsigned long long)F.cut_frames); } extern "C" void vk_fg_release(void) { @@ -322,4 +333,5 @@ extern "C" void vk_fg_release(void) { if (F.fg) vk_ffx_destroy(&F.fg), F.fg = nullptr; if (F.sc) vk_ffx_destroy(&F.sc), F.sc = nullptr; __atomic_store_n(&F.on, 0, __ATOMIC_RELAXED); + F.prepared = 0; } diff --git a/runtime/vk_fg_reset.h b/runtime/vk_fg_reset.h new file mode 100644 index 00000000..280a82cc --- /dev/null +++ b/runtime/vk_fg_reset.h @@ -0,0 +1,32 @@ +// Camera cuts for FSR 3.1 frame generation (vk_fg.cpp). DLSS starts a new history on a cut, another scene size or +// another target, and vk_dlaa.c vk_dlaa_fg_inputs hands that out with the frame's depth and motion vectors. FSR 3.1 +// takes it only in the generation callback (ffxDispatchDescFrameGeneration.reset), which its swapchain calls later, +// on its own present thread, for the frame id the configuration gave. So vk_fg_frame notes each frame's cut here +// under its id, and the callback looks it up. Without it every generated frame of a cut was blended from two unrelated +// pictures: an area load or a cutscene's cut flashed a frame of both. +// +// A ring of FGR_SLOTS frames; each slot holds the frame id and its cut as one atomic word, so a slot being rewritten +// never pairs an old id with a new frame's answer. An id the ring no longer holds (the callback fell that far behind, +// or the slot was never written) counts as a cut: a lost history costs one plain frame, a wrong one blends two. +// Own header so tests/fg_reset_check.c checks it without a GPU. +#pragma once +#include + +#define FGR_SLOTS 32 + +typedef struct { + uint64_t slot[FGR_SLOTS]; // (frame id << 1) | cut, written by vk_fg_frame; 0 never matches (ids start at 1) +} FgrRing; + +// Frame ids above this do not fit beside the cut bit; they always count as a cut. +#define FGR_MAX_ID (UINT64_MAX >> 1) + +static inline void fgr_note(FgrRing *r, uint64_t id, int cut) { + if (id && id <= FGR_MAX_ID) __atomic_store_n(&r->slot[id % FGR_SLOTS], id << 1 | (cut != 0), __ATOMIC_RELEASE); +} + +static inline int fgr_cut(const FgrRing *r, uint64_t id) { + if (!id || id > FGR_MAX_ID) return 1; + uint64_t v = __atomic_load_n(&r->slot[id % FGR_SLOTS], __ATOMIC_ACQUIRE); + return v >> 1 != id || (v & 1); +} diff --git a/tests/fg_reset_check.c b/tests/fg_reset_check.c new file mode 100644 index 00000000..c4aaa7e2 --- /dev/null +++ b/tests/fg_reset_check.c @@ -0,0 +1,46 @@ +// runtime/vk_fg_reset.h: the camera cut FSR 3.1 frame generation's callback is told of for a frame id. Built and run +// by tests/fg_reset_check.py; prints each failed check and exits 1 on any. +#include +#include +#include + +#include "vk_fg_reset.h" + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static FgrRing ring; +static volatile int stop; + +// Rewrites one slot over and over: frame 3, a cut, and frame 3 + FGR_SLOTS, none. +static void *writer(void *arg) { + (void)arg; + while (!stop) fgr_note(&ring, 3, 1), fgr_note(&ring, 3 + FGR_SLOTS, 0); + return NULL; +} + +int main(void) { + // nothing noted: every id is a cut, id 0 and ids past the range too + CHECK(fgr_cut(&ring, 0) && fgr_cut(&ring, 1) && fgr_cut(&ring, 77), "an id never noted is a cut"); + CHECK(fgr_cut(&ring, FGR_MAX_ID + 1) && fgr_cut(&ring, UINT64_MAX), "an id past the range is a cut"); + // a run of frames, a cut at 40: the callback for each id gets that frame's answer + for (uint64_t f = 1; f <= 60; f++) fgr_note(&ring, f, f == 40); + int wrong = 0; + for (uint64_t f = 60 - FGR_SLOTS + 1; f <= 60; f++) wrong += fgr_cut(&ring, f) != (f == 40); + CHECK(!wrong, "%d of the last %d frames answered wrong", wrong, FGR_SLOTS); + // the callback fell behind by more than the ring: its frame's slot holds a later one, so it is a cut + CHECK(fgr_cut(&ring, 60 - FGR_SLOTS), "a frame the ring no longer holds is a cut"); + CHECK(fgr_cut(&ring, 61), "a frame not noted yet is a cut"); + // the id and its cut are one word: a slot rewritten on another thread never answers frame 3 with the next one's + // "no cut" + memset(&ring, 0, sizeof ring); + pthread_t t; + pthread_create(&t, NULL, writer, NULL); + int paired = 0; + for (int i = 0; i < 2000000; i++) paired += !fgr_cut(&ring, 3); + stop = 1; + pthread_join(t, NULL); + CHECK(!paired, "frame 3 was answered with the other frame's value %d times", paired); + printf(fails ? "fg_reset_check: %d failed\n" : "fg_reset_check: ok\n", fails); + return fails != 0; +} diff --git a/tests/fg_reset_check.py b/tests/fg_reset_check.py new file mode 100644 index 00000000..1f2e960c --- /dev/null +++ b/tests/fg_reset_check.py @@ -0,0 +1,40 @@ +"""Build and run tests/fg_reset_check.c: runtime/vk_fg_reset.h, which frames FSR 3.1 frame generation's callback is +told are camera cuts. + +No GPU, no game files, any Python. + +Usage: fg_reset_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + """$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn).""" + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'fg_reset' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('fg_reset_check.exe' if os.name == 'nt' else 'fg_reset_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-pthread', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'fg_reset_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main())