Add shared pose motion vectors for Link and attached equipment

This commit is contained in:
automata.
2026-11-12 10:35:57 -05:00
committed by chrg6
parent 5b7703e08e
commit b25fba2e55
11 changed files with 900 additions and 59 deletions

322
runtime/motion_owner.c Normal file
View File

@@ -0,0 +1,322 @@
#include "motion_owner.h"
#include "host.h"
#include "layout.h"
#include "nvn_api.h"
#include "platform/platform.h"
#include <string.h>
#include <limits.h>
#include <stdlib.h>
// 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; i<O.count; ++i) {
MotionOwner owner=O.models[i];
uint32_t count=0;
if(!read_at(owner.model+0x20,&count,4)||count>128) continue;
uint64_t shapes=field(owner.model,0x100);
for(unsigned shape=0;shape<count;++shape) {
uint64_t blocks=field(shapes+(uint64_t)shape*0x70,0x20);
for(unsigned slot=0;slot<2;++slot) {
uint64_t buffer=field(blocks+(uint64_t)slot*0x48,8);
buf_t b; pool_t pool;
if(!read_at(buffer,&b,sizeof b)||b.magic!=MAGIC('B','U','F',0)||
!read_at((uint64_t)(uintptr_t)b.pool,&pool,sizeof pool)||pool.magic!=NVN_POOL_MAGIC||
b.off<0||b.size<256||b.size>65536||!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;i<O.count;++i) {
int hit=0;
for(unsigned age=0;age<2 && !hit;++age) {
MotionOwnerBinding *list=age?O.retired:O.bindings;
unsigned count=age?O.nretired:O.nbindings;
for(unsigned j=0;j<count;++j) {
MotionOwnerBinding *b=&list[j];
if(b->owner.model==O.models[i].model && b->address<address+size && address<b->address+b->size){hit=1;break;}
}
}
if(hit) O.models[i].lifetime=++O.serial;
}
plat_mutex_unlock(&owner_lock);
}

30
runtime/motion_owner.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <stdint.h>
#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);

View File

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

View File

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

View File

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

View File

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

View File

@@ -479,6 +479,9 @@ static struct {
VkImage dist[2]; // each pixel's view distance, this and last frame (R32F, dlaa_mv.comp depth_out)
VmaAllocation dist_mem[2];
VkImageView dist_view[2];
uint64_t pose_distance_frame;
float pose_history[4];
int pose_distance_valid;
int dist_cur, dist_ok; // as chars_cur and chars_ok
NgxHandle *feat;
uint64_t used; // evals count when it last ran
@@ -598,6 +601,15 @@ int vk_dlaa_fg_views(VkImageView *depth, VkImageView *mv) {
return D.fg_depth_view && D.fg_mv_view;
}
int vk_dlaa_pose_depth(uint64_t frame, uint32_t w, uint32_t h, VkDescriptorImageInfo *image, float history[4]) {
const struct Size *last = D.last;
if (!last || !last->pose_distance_valid || !last->dist_ok || last->w != w || last->h != h ||
!frame || last->pose_distance_frame != frame - 1 || fabsf(last->pose_history[2]) < 1e-6f) return 0;
*image = (VkDescriptorImageInfo){D.mv_samp, last->dist_view[last->dist_cur ^ 1], VK_IMAGE_LAYOUT_GENERAL};
memcpy(history, last->pose_history, sizeof last->pose_history);
return 1;
}
int vk_dlaa_fg_camera(VkDlaaCamera *c) {
if (!D.fg_cam_ok) return 0;
*c = D.fg_cam;
@@ -1251,7 +1263,10 @@ static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float j
vk_mvcheck_camera(cb, e->mv, w, h, jx, jy); // NX2NAVI_MV_CHECK (test)
// the characters' own motion over the camera's (vk_exec_draw.c, per-object motion)
uint64_t c0 = T.on > 0 ? kern_now_ns() : 0;
if (D.cam_size == CAM_BLOCK && vk_mvobj_execute(ex, D.cam_addr, e->mv_view, zv, D.depth.format, w, h)) full_barrier(cb);
int object_motion = D.cam_size == CAM_BLOCK;
if (D.cam_size == OCAM_BLOCK)
object_motion = vk_fg_pose_prepare(D.cam_addr, D.depth.image) && !D.reset;
if (object_motion && vk_mvobj_execute(ex, D.cam_addr, e->mv_view, zv, D.depth.format, w, h)) full_barrier(cb);
if (T.on > 0) T.cpu_mv += kern_now_ns() - c0;
times_mark(cb, T_OBJ);
if (T.on > 0) T.used[T.slot] = 1; // the DLSS marks follow in evaluate
@@ -1260,6 +1275,11 @@ static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float j
if (use) e->chars_cur ^= 1, e->chars_ok = 1, D.masked++;
else e->chars_ok = 0;
if (zfit && bias > 0) D.masked++;
e->pose_distance_valid = zfit && D.fg_cam_ok;
e->pose_distance_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
e->pose_history[0] = jx, e->pose_history[1] = jy;
e->pose_history[2] = D.fg_cam.view_to_clip[11];
e->pose_history[3] = D.fg_cam.view_to_clip[15];
e->dist_cur ^= 1, e->dist_ok = zfit;
*masked = use || (zfit && bias > 0);
return 1;

View File

@@ -95,3 +95,5 @@ void vk_mvcheck_output(VkCommandBuffer cb, VkImage out, uint32_t ow, uint32_t oh
// vk_exec_draw.c, once a draw's fragment shader is known: 1 skips the draw (the game's FXAA while NX2NAVI_FXAA is 0, its
// scaling draw when DLSS wrote the scaled picture).
int vk_dlaa_skip_draw(VkExec *ex, const GfxShader *fs);
int vk_dlaa_pose_depth(uint64_t frame, uint32_t w, uint32_t h, VkDescriptorImageInfo *image, float history[4]);

View File

@@ -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) {

276
runtime/vk_fg_pose.inc Normal file
View File

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

View File

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