diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 37cf860d..3e752333 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -579,6 +579,14 @@ # 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 = 1 +# Motion vectors of their own for every other skinned character (game 1.1.1): people, animals and enemies are drawn a +# second time with last frame's pose too, so DLSS, FSR 3.1 and frame generation follow them as they move instead of +# smearing them along the camera's motion. A character's draw is matched with last frame's by its mesh and shaders; +# where the match is in doubt (it just appeared, another one of the same model appeared or left) it keeps the camera's +# motion for a few frames. 1, the default; 0: the camera's motion for all of them, as before. Nothing runs without +# DLSS or FSR 3.1. The "selective motion" log line counts the skinned draws and why some kept the camera's motion. +# Read at start. +# skin_motion = 1 # 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 c7c55de7..77d70e4d 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -57,6 +57,7 @@ static const char *const known[] = { "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", "POOL_DATA_FIRST", "TEX_REUSE_NAMED", "TEX_OVERWRITTEN_USED", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "CLEAR_SMALL_CPU", "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 + "SKIN_MOTION", // vk_skin_motion.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_OCCLUSION_OFF", "PATCH_WIDE_SAVE", "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_SAME_BIG", "TEX_FLAT3D", "TEX_KEEP_TAKEN", "TEX_STALE_KEPT", "PRESENT_HELD", "HELD_SYNC", @@ -110,7 +111,7 @@ static const struct { const char *key, *section; } sections[] = { {"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_LIVE", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"}, {"HDR_UI_NITS", "display"}, {"SHADOW_RES", "display"}, {"DLAA", "display"}, {"FSR4", "display"}, {"FSR4_FG", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_IDLE_FREE", "display"}, {"DLSS_RANGE", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"}, {"AA", "display"}, - {"FRAMEGEN", "display"}, {"FRAMEGEN_MULTIPLIER", "display"}, {"FG_MULT", "display"}, + {"FRAMEGEN", "display"}, {"FRAMEGEN_MULTIPLIER", "display"}, {"FG_MULT", "display"}, {"SKIN_MOTION", "display"}, {"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"}, {"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"}, {"COOP", "coop"}, {"COOP_BETA", "coop"}, {"COOP_NAME", "coop"}, {"COOP_SERVER", "coop"}, {"COOP_PORT", "coop"}, @@ -194,6 +195,8 @@ static const CfgMeta metas[] = { // the same where DLSS's frame generation runs through NGX itself (gfx_present.c ng_multiplier): 2x to 6x or dynamic {"FG_MULT", "2", "2|3|4|5|6|dynamic", LIVE}, {"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"FXAA", "1", "0|1", LIVE}, + // vk_skin_motion.inc skin_enabled: motion vectors of their own for every skinned character, not the player alone + {"SKIN_MOTION", "1", "0|1", RESTART}, {"AA", "game", "game|off|fxaa|smaa|taa", LIVE}, // vk_aa.c: what runs in place of the game's SMAA {"GAME_ASPECT", "16:9", "16:9|window|21:9|32:9", RESTART}, // widescreen.c {"GAME_RES", "1440", "1080|1440|1800|2160|2880|window", RESTART}, // widescreen.c: the picture's height diff --git a/runtime/vk.h b/runtime/vk.h index 04471119..bf160d35 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -1021,5 +1021,9 @@ 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); +// Select the main scene view for selective animated motion, and decide which of the frame's other skinned draws +// continue a draw of last frame (vk_skin_motion.inc skin_resolve). ex: the submission that records the motion vectors, +// which are made for the camera block cam and tested against the depth image depth of w x h. 1: the frame has motion +// draws to record. +struct VkExec; +int vk_fg_pose_prepare(struct VkExec *ex, uint64_t cam, VkImage depth, uint32_t w, uint32_t h); diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 0707ae95..c8ba78f1 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -1289,7 +1289,7 @@ static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float j // Not on a frame that starts a new history, nor on the first frame of another render size (D.stepped, evaluate): // that frame was left out before the history could stay across a size, and it still is. if (D.cam_size == OCAM_BLOCK) - object_motion = vk_fg_pose_prepare(D.cam_addr, D.depth.image) && !D.reset && !D.stepped; + object_motion = vk_fg_pose_prepare(ex, D.cam_addr, D.depth.image, w, h) && !D.reset && !D.stepped; 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); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 069cd253..1e076103 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -629,8 +629,10 @@ static void mesh_ring_release(VkCtx *c); static void pool_marks_release(VkCtx *c); static void pose_shutdown(void); +static void skin_shutdown(void); void vk_exec_draw_shutdown(void) { pose_shutdown(); + skin_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; @@ -4917,6 +4919,8 @@ typedef struct { int32_t base_vertex; uint64_t cam; // the draw's camera block, this frame's int pose_index, pose_view; + int skin; // vk_skin_motion.inc: 0 not a skinned character's draw; 1 one, 2 one checked against + // last frame's depth; -1 one skin_resolve left to the camera } MoDraw; typedef struct { VkBuffer b[17]; VkDeviceSize o[17], sz[17], str[17]; } MoVb; static struct { @@ -4973,9 +4977,11 @@ static uint64_t mo_gen = 1; // the list's snapshot (mo_prev_cached), bumped by static struct { uint32_t mask, chg, n; } MF[16]; // this list's draws per position banks and changed banks static char fm_last[400]; // the last executed list's, formatted (vk_mvobj_frame_masks) +static void skin_begin(uint64_t frame); // Starts this frame's list (the old one, never executed, is dropped). static void mo_begin(VkExec *ex, uint64_t frame) { mo_drop(); + skin_begin(frame); mo_gen++; memset(MF, 0, sizeof MF); MQ.frame = frame, MQ.fence = ex->fence, MQ.depth_fmt = ex->depth.format; @@ -5192,10 +5198,12 @@ static void mo_emit(VkCommandBuffer cb) { } #include "vk_fg_pose.inc" +#include "vk_skin_motion.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 int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, uint32_t *banks_out, MotionOwner *owner_out, + SkinPick *skin_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 MotionOwner *owner); + const GfxShader *vs, uint32_t banks, const MotionOwner *owner, const SkinPick *skin); // 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 @@ -5206,7 +5214,7 @@ 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 || !ex->dc->B.st_valid) return; if (RT_V111) { - if (!vk_dlaa_available() || !pose_enabled()) return; + if (!vk_dlaa_available() || (!pose_enabled() && !skin_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) @@ -5214,10 +5222,11 @@ static void mvobj_draw(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSiz const GfxShader *vs; uint32_t banks; MotionOwner owner = {0}; - int listed = mvobj_pick(ex, d, &vs, &banks, &owner, t0); + SkinPick skin = {0}; + int listed = mvobj_pick(ex, d, &vs, &banks, &owner, &skin, 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, &owner); + if (listed) mvobj_draw_(ex, d, ib, ib_off, t0, vs, banks, &owner, skin.stride ? &skin : NULL); if (t0) __atomic_add_fetch(&MO.ns_check, kern_now_ns() - t0, __ATOMIC_RELAXED); if (lk) plat_mutex_unlock(&mo_lock); } @@ -5384,7 +5393,8 @@ static int mo_legacy_previous(VkExec *ex, uint64_t frame, uint32_t banks, uint64 // 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 p_draw_idx *d, const GfxShader **vs_out, uint32_t *banks_out, MotionOwner *owner_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, + SkinPick *skin_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; @@ -5403,16 +5413,17 @@ static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, 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; + // the player's own draw (ownership records), else any other skinned character's (vk_skin_motion.inc) if (!pose_owner(ex, owner_out)) { __atomic_add_fetch(&pose_unowned, 1, __ATOMIC_RELAXED); - return 0; + if (!skin_pick(ex, skin_out)) 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; for (int i = 0; i < nu; i++) reads |= ur[i].slot >= 3 && ur[i].slot <= 16 && ur[i].slot != 4; - if (!reads) return 0; + if (!reads) return skin_pick_left(skin_out, SKIN_BANKS_OTHER); __atomic_add_fetch(&MO.cand, 1, __ATOMIC_RELAXED); if (t0) { __atomic_add_fetch(&MO.ns_part[0], kern_now_ns() - t0, __ATOMIC_RELAXED); @@ -5432,7 +5443,7 @@ static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, cx->sf_mvprev = 0; if (!v || pending) { __atomic_add_fetch(&MO.building, 1, __ATOMIC_RELAXED); - return 0; + return skin_pick_left(skin_out, SKIN_NOT_READY); } GfxShaderIo io[32]; uint32_t extra = gfx_shader_outputs(v, io) & ~gfx_shader_outputs(cx->B.st.vs, io); @@ -5444,7 +5455,7 @@ static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, uint32_t banks = vmemo[vi].banks; if (!banks || !(banks & ~(1u << 4))) { __atomic_add_fetch(&MO.no_out, 1, __ATOMIC_RELAXED); - return 0; + return skin_pick_left(skin_out, SKIN_BANKS_OTHER); } // a position without the scene camera (c4) has a view of its own (c8 c9 c10 [c13]: its c8 is at a new address every // frame, last frame's is never found): drawn again it would get this frame's view and no camera motion @@ -5453,7 +5464,7 @@ static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, if (noc4 < 0) noc4 = rt_knob("MVOBJ_NOC4", "0"); // NX2NAVI_MVOBJ_NOC4=1 (test): drawn again all the same if (!(banks >> 4 & 1) && !noc4) { __atomic_add_fetch(&MO.no_out, 1, __ATOMIC_RELAXED); - return 0; + return skin_pick_left(skin_out, SKIN_BANKS_OTHER); } // only positions that read an object's own blocks: bones (c5), place (c7) or c6. The rest (terrain and grass: c14 // c15 c16, with or without c9) measured no better than the camera's motion (NX2NAVI_MV_CHECK, turning, those draws @@ -5463,7 +5474,7 @@ static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, if (all < 0) all = rt_knob("MVOBJ_ALL", "0"); if (!all && !(banks & (1u << 5 | 1u << 6 | 1u << 7))) { __atomic_add_fetch(&MO.no_out, 1, __ATOMIC_RELAXED); - return 0; + return skin_pick_left(skin_out, SKIN_BANKS_OTHER); } // NX2NAVI_MVOBJ_ONLY= (test): only draws whose position reads no bank outside the mask (bit n: cn) static long only = -2; @@ -5471,13 +5482,13 @@ static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_out, const char *e = cfg_get("NX2NAVI_MVOBJ_ONLY"); only = e && *e ? strtol(e, NULL, 16) : -1; } - if (only >= 0 && banks & ~(uint32_t)only & ~(1u << 4)) return 0; + if (only >= 0 && banks & ~(uint32_t)only & ~(1u << 4)) return skin_pick_left(skin_out, SKIN_BANKS_OTHER); static long exact = -2; // NX2NAVI_MVOBJ_EXACT= (test): only draws with exactly these position banks if (exact == -2) { const char *e = cfg_get("NX2NAVI_MVOBJ_EXACT"); exact = e && *e ? strtol(e, NULL, 16) : -1; } - if (exact >= 0 && banks != (uint32_t)exact) return 0; + if (exact >= 0 && banks != (uint32_t)exact) return skin_pick_left(skin_out, SKIN_BANKS_OTHER); *vs_out = vs, *banks_out = banks; return 1; } @@ -5485,7 +5496,7 @@ static int mvobj_pick(VkExec *ex, const p_draw_idx *d, const GfxShader **vs_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 MotionOwner *owner) { + const GfxShader *vs, uint32_t banks, const MotionOwner *owner, const SkinPick *skin) { DrawCtx *const cx = ex->dc; const VkExecTarget *z = &ex->depth; uint64_t tp = t0 ? kern_now_ns() : 0; @@ -5493,8 +5504,11 @@ 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) - PoseRange *pose = RT_V111 ? pose_match(ex, d, owner) : NULL; - if (RT_V111 && !pose) return; + PoseRange *pose = RT_V111 && !skin ? pose_match(ex, d, owner) : NULL; + if (RT_V111 && !pose && !skin) return; + SkinDraw skin_draw; + SkinEntry skin_entry; + uint64_t skin_listed = 0; VkDescriptorBufferInfo cam = {0}, prevb[17] = {0}; uint32_t have = 0, changed = 0; const ActorPose *previous_pose = NULL; @@ -5522,6 +5536,15 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi VkViewport v = draw_viewport(ex); jit[0] = v.x; jit[1] = v.y - (float)z->height; + } else if (skin) { + // another skinned character's draw: its blocks, and whether a bone turned sharply (vk_skin_motion.inc); which + // draw of last frame it continues is decided when the vectors are made (skin_resolve) + if (!skin_try(ex, d, frame, vs, banks, skin, &skin_draw, &skin_entry, &skin_listed, &pose_visibility_guard)) return; + if (pose_visibility_guard && !vk_dlaa_pose_depth(frame, z->width, z->height, &pose_depth, history)) + pose_visibility_guard = 0; // as for the player: no depth of last frame to check against, the plain vector + VkViewport v = draw_viewport(ex); + jit[0] = v.x; + jit[1] = v.y - (float)z->height; } else { if (!vk_dlaa_prev_camera(&cam, jit)) { MO.nodata++, MO.why[0]++; @@ -5541,6 +5564,7 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi const GfxShader *fs = pose_visibility_guard ? link_motion_fs(loc) : mo_fs(loc); if (!fs) { MO.no_fs++; + if (skin) skin_left(SKIN_NOT_READY); return; } VkPipeState ms = cx->B.st; @@ -5556,7 +5580,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 || pose) ? NVN_DEPTH_FUNC_EQUAL : df == NVN_DEPTH_FUNC_LESS ? NVN_DEPTH_FUNC_LEQUAL + df = (eq || pose || skin) ? 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; @@ -5581,11 +5605,15 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi if (pmemo[pi].used && !memcmp(&pmemo[pi].st, &ms, sizeof ms)) { info = pmemo[pi].info; pipe = __atomic_load_n(&info->pipeline, __ATOMIC_ACQUIRE); // its optimised build once swapped in - if (!pipe) return; + if (!pipe) { + if (skin) skin_left(SKIN_NOT_READY); + return; + } } else { pipe = vk_pipeline_graphics_async(&ms, &info, &pending, 0); if (pending || !pipe || !info) { MO.building += pending != 0; + if (skin) skin_left(SKIN_NOT_READY); return; } pmemo[pi].st = ms, pmemo[pi].p = pipe, pmemo[pi].info = info, pmemo[pi].used = 1; @@ -5615,6 +5643,10 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi memcpy(scratch(ex, b->range, &b->buffer, &b->offset), data, b->range); data += b->range; } + } else if (skin) { + // where skin_resolve puts last frame's bytes of each block, once it knows whose they are + for (int i = 0; i < SKIN_BANKS; i++) + if (skin_draw.block[i] >= 0) cx->mo_ovr[NVN_STAGE_VS][18 + i] = SK.f[SK.cur].b[skin_draw.block[i]].was; } 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); @@ -5625,6 +5657,7 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi cx->mo_ovr_on = 0; if (!bound) { MO.storage++; + if (skin) skin_left(SKIN_STORAGE); return; } q->pipe = pipe, q->info = info; @@ -5640,9 +5673,11 @@ 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][pose ? 0 : 1].addr; + q->cam = ex->ubo[NVN_STAGE_VS][pose || skin ? 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; + q->skin = skin ? 1 + pose_visibility_guard : 0; + if (skin) skin_commit(&skin_draw, &skin_entry, skin_listed, MQ.n); 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) @@ -5653,9 +5688,12 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi 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); +int vk_fg_pose_prepare(VkExec *ex, uint64_t cam, VkImage depth, uint32_t w, uint32_t h) { + if (!RT_V111 || (!pose_enabled() && !skin_enabled())) return 0; + uint64_t frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + pose_select(frame, cam, depth); + skin_resolve(ex, frame, cam, depth, w, h); + pose_status(frame); return MQ.n != 0; } @@ -5730,11 +5768,13 @@ int vk_mvobj_execute(VkExec *ex, uint64_t cam, VkImageView mv, VkImageView depth int kept = 0; for (int i = 0; i < MQ.n; 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)) { + pose_ranges[MQ.d[i].pose_index - 1].history[frame & 1].view[MQ.d[i].pose_view].selected) && + (!MQ.d[i].skin || (MQ.d[i].skin > 0 && SK.resolved))) { // a skinned draw: one skin_resolve paired if (MQ.d[i].pose_index) ++pose_written[pose_ranges[MQ.d[i].pose_index - 1].actor]; + if (MQ.d[i].skin) skin_recorded(MQ.d[i].skin == 2); MQ.d[kept++] = MQ.d[i]; } - else MO.other_cam++; + else if (!MQ.d[i].skin) MO.other_cam++; // a skinned draw left out has its own count (skin_status) MQ.n = kept; if (!MQ.n) { mo_drop(); diff --git a/runtime/vk_fg_pose.inc b/runtime/vk_fg_pose.inc index d68d331a..33daed6b 100644 --- a/runtime/vk_fg_pose.inc +++ b/runtime/vk_fg_pose.inc @@ -206,12 +206,13 @@ static float pose_rotation_trace_limit(void) { } 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; +// Two 3x4 matrices (rows of four floats) of one part or bone, this frame's and last frame's: 1 when both are plain +// rotations and the turn between them is past the guard threshold. vk_skin_motion.inc asks it for each bone of a palette. +static int pose_matrix_sharp(const uint8_t *now, const uint8_t *prev) { float m[2][12]; - const ActorPose *p[2] = {now, prev}; + const uint8_t *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]); + memcpy(m[k], p[k], sizeof m[k]); for (int a = 0; a < 3; ++a) { for (int b = a; b < 3; ++b) { float dot = 0.0f; @@ -228,6 +229,11 @@ static int pose_sharp_rotation(const ActorPose *now, const ActorPose *prev) { // when this part needs visibility filtering; equipment retains its separate raw policy. return trace < pose_rotation_trace_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; + return pose_matrix_sharp(now->bytes + now->size[0] + now->size[1] + now->size[2], + prev->bytes + prev->size[0] + prev->size[1] + prev->size[2]); +} 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; @@ -257,11 +263,6 @@ static void pose_select(uint64_t frame, uint64_t camera, VkImage depth) { 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)) @@ -269,6 +270,19 @@ static void pose_select(uint64_t frame, uint64_t camera, VkImage depth) { frame_step_world_us() / 1e6, (unsigned long long)frame, guarded, raw, rigid, missing_depth); } +// The motion status line, every 120 frames with motion vectors: the player's parts (ownership records), then the +// other skinned draws (vk_skin_motion.inc skin_status). +static int skin_status(char *out, size_t cap); +static void pose_status(uint64_t frame) { + if (frame % 120) return; + char skinned[1200]; + skin_status(skinned, sizeof skinned); + 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%s", + 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, skinned); +} + static void pose_shutdown(void) { for (int i = 0; i < pose_count; ++i) pose_clear(&pose_ranges[i]); pose_count = 0; diff --git a/runtime/vk_skin_motion.inc b/runtime/vk_skin_motion.inc new file mode 100644 index 00000000..ea7cd35b --- /dev/null +++ b/runtime/vk_skin_motion.inc @@ -0,0 +1,416 @@ +// Motion vectors for every skinned character (NX2NAVI_SKIN_MOTION, skin_motion in nx2navi.ini; game 1.1.1). The +// player's meshes get their own motion through ownership records (motion_owner.c, vk_fg_pose.inc): a draw is the +// player's when a block it binds belongs to a model of the player's actor. People, animals and enemies have no such +// record, so the camera's motion was all they got, and DLSS and FSR 3.1 smeared them as they moved. This file gives +// them the same second draw, with last frame's pose and camera into the motion vector target, without knowing whose +// they are. A draw the player's path takes (pose_owner) never comes here. +// +// A skinned scene draw (skin_pick): a draw that passes the scene filter of mvobj_pick (solid, one instance, into a +// scene-size depth target with the scene context at c3), whose vertex shader reads a matrix palette +// (gfx_shader.c reflect_palette states that rule and what it matched) from one of the uniform banks c3 to c7, and +// whose vertex layout feeds each joint input of that shader an unsigned integer attribute. Weights are not asked +// for: a mesh that binds each vertex to one joint carries none and moves with its skeleton all the same. +// +// Its second draw uses the vertex shader's last-frame variant (translator patch 0015), which computes the position +// again from the blocks bound at bank + 15. The variant's position may read c3 to c7 only, among them the scene +// context c3 and the palette, and no storage buffer or texture: as for the player. Each block such a draw binds is +// kept once a frame, by address (many pieces share a palette, all share the scene context): its bytes go into the +// frame's arena for the next frame, and a stretch of the submission's scratch memory is set aside where last frame's +// bytes of the same object are put once that is known (SkinBlock). +// +// Which draw of last frame a draw continues is decided when the motion vectors are made (skin_resolve, called from +// vk_fg_pose_prepare), not at the draw: the replay's threads reach the draws of one pass in any order, and the +// number that tells two characters of one model apart counts in the order of the command stream (skin_pair.h, which +// lists the cases that leave a draw to the camera). There the frame's draws are first cut down to the scene's own +// view (the camera block and size the motion vectors are made for) and to one draw for each mesh and palette: the game +// draws a mesh into a depth pre-pass and again, against a copy of that depth, into the light target, and the draw into +// the depth image the vectors are tested against is kept, as for the player; the pre-pass draw stands in where the +// other could not be listed (its vertex shader reads storage buffers), and from the next frame on a mesh with a +// light-target draw has its pre-pass draw left out at the draw already. Then: +// - a key drawn more than once must also have each palette near last frame's (skin_near, SKIN_NEAR_UNITS); +// - every bank of the paired draws must have the same size; +// - the draws that share a block must agree on which block of last frame it continues, or all of them are left out. +// What passes has its blocks' scratch filled with last frame's bytes and stays in the list; the rest is taken out of +// it (MoDraw.skin), so those pixels keep the camera's motion. +// +// The guard for parts that turn sharply (motion_guard_degrees) is chosen at the draw, as it picks the fragment shader: +// a draw is checked against last frame's depth (shaders/link_motion.frag) when a bone of its palette turned past the +// threshold against last frame's palette of any draw with its key. For a key drawn once that is the player's rule; +// for more it errs towards the check. +// +// Cost on the replay thread, for a skinned draw: the palette lookup in the shader's reflection, a hash lookup of the +// listed meshes, for each block its position reads a lookup in the uniform snapshot and one in the frame's blocks, a +// binary search in last frame's list, and for the first draw that binds a block a copy of its bytes; then what any +// motion draw costs (mvobj_draw_). Nothing runs while no upscaler is active (mvobj_draw, skin_pick). +#include "skin_pair.h" + +#define SKIN_BANKS 5 // c3 to c7, as POSE_BANKS +#define SKIN_SLOTS 4096 // hash slots of a frame's blocks, listed draws and lit meshes +#define SKIN_FILL 2048 // entries each holds at most +#define SKIN_NEAR_UNITS 1.0f // a palette of the same key more than this from last frame's is another character's + +// Why a skinned draw kept the camera's motion: skin_pair.h's, then this file's. +enum { SKIN_NOT_READY = SKIN_PAIR_WHYS, // its variant, fragment shader or pipeline is still being made + SKIN_BANKS_OTHER, // the position reads a bank outside c3 to c7, or not the palette, or the + // palette is not in a uniform bank + SKIN_STORAGE, // the variant reads a storage buffer, a texture or an image + SKIN_SNAPSHOT, // a block is not in the uniform snapshot + SKIN_TWIN, // another draw of the same mesh and palette carries the vector + SKIN_VIEW, // drawn with another camera block or size than the scene's + SKIN_SHARED, // the draws sharing one of its blocks disagree on last frame's + SKIN_FAR, // skin_near + SKIN_FULL, // a table is full + SKIN_UNUSED, // the frame's list was not recorded (another submission made the vectors) + SKIN_WHYS }; + +typedef struct { uint8_t stride, bank; } SkinPick; // skin_pick: the bytes of a bone matrix (0: not a skinned draw) + // and the palette's bank, counted from c3 + +typedef struct { + uint64_t addr; + uint32_t size, at; // at: this frame's bytes in the frame's arena + VkDescriptorBufferInfo was; // where last frame's bytes of the same object go; the motion draws bind it + uint8_t *was_map; + int32_t last; // last frame's block it continues: -1 none named, -2 two draws named different ones + int32_t sharp_with, sharp; // skin_sharp's answer for last frame's block sharp_with +} SkinBlock; + +typedef struct { + int32_t block[SKIN_BANKS]; // the frame's block per bank, -1 for a bank the position does not read + uint8_t palette, stride; // the palette's bank, the bytes of a matrix + uint32_t zw, zh; // the depth target it was drawn into + VkImage depth; + uint64_t cam; // its scene context block + uint64_t mesh, twin; // hashes: the mesh; the mesh and its palette's address + int32_t draw; // its MoDraw +} SkinDraw; + +typedef struct { + uint64_t frame; + VkImage scene_depth; // once resolved: the depth image the vectors were tested against + SkinDraw *d; // the listed draws + SkinEntry *e; // while listing one per draw (tag: its index); once resolved the kept ones, + int n, ne, cap_d, cap_e; // numbered (skin_number), ne of them + SkinBlock *b; + int nb, cap_b; + uint8_t *bytes; // the blocks' bytes + uint32_t used, size; + uint16_t block_at[SKIN_SLOTS]; // by address and size: index + 1 + uint64_t seen[SKIN_SLOTS]; // listed draws by mesh, palette, camera block and depth image + uint64_t lit[SKIN_SLOTS]; // once resolved: the meshes with a kept draw into scene_depth + int nseen; +} SkinFrame; + +static struct { + SkinFrame f[2]; + int cur; // f[cur]: of the list being made; f[cur ^ 1]: the last one resolved + int resolved; // the list being made was resolved (its draws carry their verdicts) + uint32_t pending; // its draws not counted yet + uint64_t seen, own, guarded, why[SKIN_WHYS]; +} SK; + +static int skin_enabled(void) { + static int on = -1; + if (on < 0) on = RT_V111 && cfg_bool("NX2NAVI_SKIN_MOTION"); + return on; +} + +static void skin_left(int why) { __atomic_add_fetch(&SK.why[why], 1, __ATOMIC_RELAXED); } + +// A hash set of SKIN_SLOTS words (0: empty). skin_has: whether v is in it; skin_put: adds it. +static int skin_has(const uint64_t *set, uint64_t v) { + v |= 1; + for (uint32_t i = (uint32_t)(v >> 40) & (SKIN_SLOTS - 1); set[i]; i = (i + 1) & (SKIN_SLOTS - 1)) + if (set[i] == v) return 1; + return 0; +} +static void skin_put(uint64_t *set, uint64_t v) { + v |= 1; + uint32_t i = (uint32_t)(v >> 40) & (SKIN_SLOTS - 1); + while (set[i] && set[i] != v) i = (i + 1) & (SKIN_SLOTS - 1); + set[i] = v; +} + +// An unsigned integer vertex format: what joint indices are stored as. +static int skin_joint_format(uint32_t fmt) { + switch (fmt) { + case NVN_FORMAT_R8UI: case NVN_FORMAT_RG8UI: case NVN_FORMAT_RGB8UI: case NVN_FORMAT_RGBA8UI: + case NVN_FORMAT_R16UI: case NVN_FORMAT_RG16UI: case NVN_FORMAT_RGB16UI: case NVN_FORMAT_RGBA16UI: + case NVN_FORMAT_R32UI: case NVN_FORMAT_RG32UI: case NVN_FORMAT_RGB32UI: case NVN_FORMAT_RGBA32UI: return 1; + default: return 0; + } +} + +// Whether the draw being replayed is a skinned scene draw (the rule at the top; the caller has applied the scene +// filter). Reads the draw's own state only: the chunks of a parallel group ask at once. +static int skin_pick(VkExec *ex, SkinPick *out) { + out->stride = 0; + if (!skin_enabled() || !vk_dlaa_active()) return 0; + const DrawCtx *cx = ex->dc; + int slot, storage; + uint32_t joints; + int stride = gfx_shader_palette(cx->B.st.vs, &slot, &storage, &joints); + if (!stride || joints >> 16) return 0; + for (int l = 0; l < 16; l++) { + va_st a; + memcpy(&a, &cx->B.st.vattr[l], sizeof a); + if ((joints >> l & 1) && !skin_joint_format(a.fmt)) return 0; + } + __atomic_add_fetch(&SK.seen, 1, __ATOMIC_RELAXED); + if (storage || slot < 3 || slot >= 3 + SKIN_BANKS) { + skin_left(SKIN_BANKS_OTHER); + return 0; + } + out->stride = (uint8_t)stride, out->bank = (uint8_t)(slot - 3); + return 1; +} + +// mvobj_pick left a draw skin_pick took: counted, 0. +static int skin_pick_left(const SkinPick *s, int why) { + if (s->stride) skin_left(why); + return 0; +} + +// Starts the frame's list (mo_begin): the draws of the one before it that nothing counted were never recorded. +static void skin_begin(uint64_t frame) { + SK.why[SKIN_UNUSED] += SK.pending; + SK.pending = 0, SK.resolved = 0; + if (!skin_enabled()) return; + SkinFrame *f = &SK.f[SK.cur]; + f->frame = frame, f->scene_depth = VK_NULL_HANDLE; + f->n = f->ne = f->nb = f->nseen = 0, f->used = 0; + memset(f->block_at, 0, sizeof f->block_at); + memset(f->seen, 0, sizeof f->seen); +} + +// The frame's block for a uniform binding, made at its first draw: -1 when it is not in the snapshot, -2 when the +// table is full. +static int skin_block(VkExec *ex, SkinFrame *f, const p_bind_buf *u) { + // asked of the snapshot for every draw: a block the submission itself writes between two draws is dropped from + // it there, and the bytes kept for the first draw are not the second one's + VkDescriptorBufferInfo snap; + if (!u->addr || !u->size || u->size > LIM.ubo_max || u->size > POSE_BYTES || + !vk_usnap_find(u->addr, u->size, LIM.ubo_align, &snap)) + return -1; + uint32_t h = (uint32_t)(skin_mix(u->addr, u->size) >> 40) & (SKIN_SLOTS - 1); + for (; f->block_at[h]; h = (h + 1) & (SKIN_SLOTS - 1)) { + const SkinBlock *b = &f->b[f->block_at[h] - 1]; + if (b->addr == u->addr && b->size == u->size) return f->block_at[h] - 1; + } + if (f->nb == SKIN_FILL) return -2; + uint32_t size = (uint32_t)u->size; + if (f->used + size > f->size) { + uint32_t want = f->size ? f->size * 2 : 1u << 20; + while (want < f->used + size) want *= 2; + uint8_t *bytes = realloc(f->bytes, want); + if (!bytes) return -2; + f->bytes = bytes, f->size = want; + } + f->b = mo_grow(f->b, &f->cap_b, f->nb + 1, sizeof *f->b); + SkinBlock *b = &f->b[f->nb]; + *b = (SkinBlock){.addr = u->addr, .size = size, .at = f->used, .last = -1, .sharp_with = -1}; + memcpy(f->bytes + b->at, vk_usnap_map() + snap.offset, size); + f->used += size; + b->was.range = size; + b->was_map = scratch(ex, size, &b->was.buffer, &b->was.offset); + f->block_at[h] = (uint16_t)(f->nb + 1); + return f->nb++; +} + +// Whether a bone of the palette block c of this frame turned sharply against the palette block l of last frame +// (pose_matrix_sharp); kept for the pieces that share the palette. +static int skin_sharp(SkinFrame *f, const SkinFrame *last, int c, int l, uint32_t stride) { + SkinBlock *b = &f->b[c]; + const SkinBlock *o = &last->b[l]; + if (b->sharp_with == l) return b->sharp; + b->sharp_with = l, b->sharp = 0; + if (b->size != o->size) return 0; + const uint8_t *now = f->bytes + b->at, *then = last->bytes + o->at; + for (uint32_t at = 0; at + stride <= b->size && !b->sharp; at += stride) + if (memcmp(now + at, then + at, 48)) b->sharp = pose_matrix_sharp(now + at, then + at); + return b->sharp; +} + +// A skinned draw mvobj_pick took, before it goes onto the frame's list (the caller holds the motion-list lock): its +// blocks, key and place in the stream into *sd and *se, and into *guard whether it is checked against last frame's +// depth. 0: left to the camera, counted. vs: the last-frame variant, banks: the banks its position reads. +static int skin_try(VkExec *ex, const p_draw_idx *d, uint64_t frame, const GfxShader *vs, uint32_t banks, + const SkinPick *pick, SkinDraw *sd, SkinEntry *se, uint64_t *listed, int *guard) { + const DrawCtx *cx = ex->dc; + SkinFrame *f = &SK.f[SK.cur]; + const SkinFrame *last = &SK.f[SK.cur ^ 1]; + int consecutive = last->frame + 1 == frame; + const GfxShaderRes *res; + *guard = 0; + // a draw after the frame's vectors were made (skin_resolve made that list last frame's, and f is not begun) + if (SK.resolved || f->frame != frame) return skin_pick_left(pick, SKIN_UNUSED); + if (!(banks >> 3 & 1) || !(banks >> (pick->bank + 3) & 1) || (banks & ~0xf8u)) return skin_pick_left(pick, SKIN_BANKS_OTHER); + if (gfx_shader_resources(vs, GFX_RES_SSBO, &res) || gfx_shader_resources(vs, GFX_RES_TEX, &res) || + gfx_shader_resources(vs, GFX_RES_IMG, &res)) + return skin_pick_left(pick, SKIN_STORAGE); + // the mesh: the vertex buffers the shader's inputs read (a binding no input reads keeps an earlier draw's) and + // the indices + uint32_t inputs = gfx_shader_inputs(cx->B.st.vs, NULL), streams = 0; + for (int i = 0; i < 16; i++) { + va_st a; + memcpy(&a, &cx->B.st.vattr[i], sizeof a); + if ((inputs >> i & 1) && a.fmt && a.stream < 16) streams |= 1u << a.stream; + } + uint64_t mesh = skin_mix(skin_mix(d->ibuf, (uint64_t)(uint32_t)d->count << 32 | (uint32_t)d->base_vertex), (uint64_t)d->itype); + for (int s = 0; s < 16; s++) + if (streams >> s & 1) mesh = skin_mix(skin_mix(mesh, ex->vbuf[s].address), ex->vbuf[s].size ^ (uint64_t)s << 56); + const p_bind_buf *pal = &ex->ubo[NVN_STAGE_VS][pick->bank]; + const VkExecTarget *z = &ex->depth; + uint64_t twin = skin_mix(mesh, pal->addr), cam = ex->ubo[NVN_STAGE_VS][0].addr; + uint64_t here = skin_mix(skin_mix(twin, cam), (uint64_t)(uintptr_t)z->image); + // the same mesh with the same palette in the same view: a second pass over it, or the draw submitted twice + if (skin_has(f->seen, here)) return skin_pick_left(pick, SKIN_TWIN); + // the pre-pass draw (it writes depth, into another image than the vectors are tested against) of a mesh whose + // light-target draw carried the vector last frame + if (consecutive && cx->B.st.ds.dw && last->scene_depth && z->image != last->scene_depth && skin_has(last->lit, mesh)) + return skin_pick_left(pick, SKIN_TWIN); + if (f->nseen == SKIN_FILL) return skin_pick_left(pick, SKIN_FULL); + for (int i = 0; i < SKIN_BANKS; i++) { + sd->block[i] = -1; + if (!(banks >> (i + 3) & 1)) continue; + sd->block[i] = skin_block(ex, f, &ex->ubo[NVN_STAGE_VS][i]); + if (sd->block[i] < 0) return skin_pick_left(pick, sd->block[i] == -1 ? SKIN_SNAPSHOT : SKIN_FULL); + } + sd->palette = pick->bank, sd->stride = pick->stride; + sd->zw = z->width, sd->zh = z->height, sd->depth = z->image; + sd->cam = cam, sd->mesh = mesh, sd->twin = twin; + *se = (SkinEntry){.key = skin_mix(skin_mix(mesh, (uint64_t)(uintptr_t)cx->B.st.vs), (uint64_t)(uintptr_t)cx->B.st.fs), + .order = vk_exec_stream_pos() << 24 | (uint64_t)f->n, .palette = pal->addr, + .bytes = (uint32_t)pal->size}; + *listed = here; + int at = consecutive && !pose_raw() ? skin_find(last->e, last->ne, se->key) : -1; + for (uint32_t k = 0; at >= 0 && k < last->e[at].of && !*guard; k++) { + const SkinDraw *ld = &last->d[last->e[at + (int)k].tag]; + if (ld->palette == sd->palette) *guard = skin_sharp(f, last, sd->block[sd->palette], ld->block[ld->palette], sd->stride); + } + return 1; +} + +// The draw skin_try prepared is on the frame's list as MQ.d[draw]. +static void skin_commit(const SkinDraw *sd, const SkinEntry *se, uint64_t listed, int draw) { + SkinFrame *f = &SK.f[SK.cur]; + f->d = mo_grow(f->d, &f->cap_d, f->n + 1, sizeof *f->d); + f->e = mo_grow(f->e, &f->cap_e, f->n + 1, sizeof *f->e); + f->d[f->n] = *sd, f->d[f->n].draw = draw; + f->e[f->n] = *se, f->e[f->n].tag = (uint32_t)f->n; + f->n++; + skin_put(f->seen, listed), f->nseen++; + SK.pending++; +} + +// Takes a listed draw out of the frame's list, counted. +static void skin_refuse(const SkinDraw *sd, int why) { + MQ.d[sd->draw].skin = -1; + SK.why[why]++, SK.pending--; +} + +static const SkinFrame *skin_sort_frame; // skin_twin_cmp's frame: the entries' draws +// By mesh and palette, the draw into the scene depth first, then by place in the stream. +static int skin_twin_cmp(const void *a, const void *b) { + const SkinEntry *x = a, *y = b; + const SkinDraw *dx = &skin_sort_frame->d[x->tag], *dy = &skin_sort_frame->d[y->tag]; + if (dx->twin != dy->twin) return dx->twin < dy->twin ? -1 : 1; + int ox = dx->depth != skin_sort_frame->scene_depth, oy = dy->depth != skin_sort_frame->scene_depth; + if (ox != oy) return ox - oy; + return x->order < y->order ? -1 : x->order > y->order; +} + +// The frame's list is complete and its motion vectors are about to be made, for the scene camera block cam, tested +// against the depth image depth of w x h (vk_fg_pose_prepare, on the thread that records them; ex: its submission). +// Decides for each listed skinned draw whether it continues a draw of last frame (the steps at the top), fills the +// blocks of those that do with last frame's bytes, takes the others out of the list, and makes this frame's list the +// one the next frame is paired with. +static void skin_resolve(VkExec *ex, uint64_t frame, uint64_t cam, VkImage depth, uint32_t w, uint32_t h) { + SkinFrame *f = &SK.f[SK.cur]; + const SkinFrame *last = &SK.f[SK.cur ^ 1]; + // only the list of this submission: its blocks' scratch memory lives as long as the submission + if (!skin_enabled() || SK.resolved || MQ.frame != frame || MQ.fence != ex->fence || f->frame != frame) return; + SK.resolved = 1; + f->scene_depth = depth; + int ne = 0; + for (int i = 0; i < f->n; i++) { + const SkinDraw *sd = &f->d[i]; + if (sd->cam != cam || sd->zw != w || sd->zh != h) skin_refuse(sd, SKIN_VIEW); + else f->e[ne++] = f->e[i]; + } + skin_sort_frame = f; + qsort(f->e, (size_t)ne, sizeof *f->e, skin_twin_cmp); + int kept = 0; + for (int i = 0; i < ne; i++) { + if (kept && f->d[f->e[kept - 1].tag].twin == f->d[f->e[i].tag].twin) skin_refuse(&f->d[f->e[i].tag], SKIN_TWIN); + else f->e[kept++] = f->e[i]; + } + f->ne = ne = kept; + skin_number(f->e, ne); + skin_pair(f->e, ne, last->e, last->ne, last->frame + 1 == frame); + for (int i = 0; i < ne; i++) { + SkinEntry *e = &f->e[i]; + const SkinDraw *sd = &f->d[e->tag]; + if (e->why != SKIN_PAIRED) continue; + const SkinDraw *ld = &last->d[last->e[e->prev].tag]; + if (ld->palette != sd->palette || ld->stride != sd->stride) e->why = SKIN_SIZE; + for (int k = 0; k < SKIN_BANKS && e->why == SKIN_PAIRED; k++) + if ((sd->block[k] < 0) != (ld->block[k] < 0) || + (sd->block[k] >= 0 && f->b[sd->block[k]].size != last->b[ld->block[k]].size)) + e->why = SKIN_SIZE; + if (e->why == SKIN_PAIRED && e->of > 1 && + !skin_near(f->bytes + f->b[sd->block[sd->palette]].at, last->bytes + last->b[ld->block[ld->palette]].at, + e->bytes, sd->stride, SKIN_NEAR_UNITS)) + e->why = SKIN_FAR; + for (int k = 0; k < SKIN_BANKS && e->why == SKIN_PAIRED; k++) { + if (sd->block[k] < 0) continue; + SkinBlock *b = &f->b[sd->block[k]]; + b->last = b->last == -1 || b->last == ld->block[k] ? ld->block[k] : -2; + } + } + memset(f->lit, 0, sizeof f->lit); + for (int i = 0; i < ne; i++) { + SkinEntry *e = &f->e[i]; + const SkinDraw *sd = &f->d[e->tag]; + if (sd->depth == depth) skin_put(f->lit, sd->mesh); + for (int k = 0; k < SKIN_BANKS && e->why == SKIN_PAIRED; k++) + if (sd->block[k] >= 0 && f->b[sd->block[k]].last == -2) e->why = SKIN_SHARED; + if (e->why != SKIN_PAIRED) skin_refuse(sd, e->why); + } + for (int i = 0; i < f->nb; i++) { + const SkinBlock *b = &f->b[i]; + if (b->last >= 0) memcpy(b->was_map, last->bytes + last->b[b->last].at, b->size); + } + SK.cur ^= 1; +} + +// A skinned draw vk_mvobj_execute records (guarded: checked against last frame's depth). +static void skin_recorded(int guarded) { + SK.own++, SK.guarded += guarded != 0, SK.pending--; +} + +// The skinned draws' part of the motion status line (pose_status): since the start, every draw skin_pick took, the +// ones drawn with their own motion, and the others by why they kept the camera's. +static int skin_status(char *out, size_t cap) { + out[0] = 0; + if (!skin_enabled()) return 0; + const uint64_t *y = SK.why; + return snprintf(out, cap, "; skinned draws seen %llu, drawn with their own motion %llu (checked against last frame's " + "depth %llu), left to the camera: carried by another draw of the mesh %llu, not drawn last frame %llu, " + "drawn another number of times %llu, palette of another size %llu, palette at another address %llu, " + "too far from last frame's %llu, shared block disagrees %llu, another view %llu, shader or pipeline " + "not ready %llu, position reads other blocks %llu, storage in the vertex shader %llu, block not in the " + "snapshot %llu, tables full %llu, list not recorded %llu", + (unsigned long long)__atomic_load_n(&SK.seen, __ATOMIC_RELAXED), (unsigned long long)SK.own, + (unsigned long long)SK.guarded, (unsigned long long)y[SKIN_TWIN], (unsigned long long)y[SKIN_NEW], + (unsigned long long)y[SKIN_COUNT], (unsigned long long)y[SKIN_SIZE], (unsigned long long)y[SKIN_ADDRESS], + (unsigned long long)y[SKIN_FAR], (unsigned long long)y[SKIN_SHARED], (unsigned long long)y[SKIN_VIEW], + (unsigned long long)y[SKIN_NOT_READY], (unsigned long long)y[SKIN_BANKS_OTHER], + (unsigned long long)y[SKIN_STORAGE], (unsigned long long)y[SKIN_SNAPSHOT], (unsigned long long)y[SKIN_FULL], + (unsigned long long)y[SKIN_UNUSED]); +} + +static void skin_shutdown(void) { + for (int i = 0; i < 2; i++) free(SK.f[i].d), free(SK.f[i].e), free(SK.f[i].b), free(SK.f[i].bytes); + memset(&SK, 0, sizeof SK); +}