From 121b4fdf330820b4fe1efc81b57d2dccc7687f98 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 08:13:26 +0300 Subject: [PATCH] On 1.4.3 DLAA's jitter goes into the game's camera instead of the viewport: the projection's centre terms and the view-projection of each perspective camera with the scene's aspect take the offset where the game copies them into their uniform blocks, so the game's full-screen passes rebuild positions with the camera the scene was drawn with, and screen-space reflections on water seen at a low angle no longer flip between hit and miss with the jitter's phase --- runtime/nvn_api_core.c | 3 + runtime/vk_dlaa.c | 183 ++++++++++++++++++++++++++++++++++++++++- runtime/vk_dlaa.h | 2 + runtime/vk_usnap.c | 3 +- 4 files changed, 188 insertions(+), 3 deletions(-) diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index 9ae562da..9b39926c 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -56,13 +56,16 @@ void nvn_guest_written(uint64_t addr, uint64_t size) { // memcpy of pool storage the GPU rendered into must copy what the GPU wrote: TotK moves its generated grass maps // between pools with memcpy. Copies below 1 KiB skip the check; the copy itself is memmove, as overlapping // arguments are undefined for memcpy anyway. +void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst); static void ov_memcpy(Cpu *c) { uint64_t dst = arg_x(c, 0), src = arg_x(c, 1), n = arg_x(c, 2); if (n >= 1024) gfx_backend()->guest_read(src, n); memmove((void *)(uintptr_t)dst, (const void *)(uintptr_t)src, n); + if (n == 64) vk_dlaa_camera_copied(c->x[30], dst); // TotK copies its camera matrices this way (vk_dlaa.c) ret_x(c, dst); } HOST_OVERRIDE("memcpy", ov_memcpy); +__attribute__((weak)) void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst) { (void)lr, (void)dst; } __attribute__((weak)) void nvn_submit_prepare(const uint64_t *handles, int n) { (void)handles, (void)n; } __attribute__((weak)) void nvn_submit_prepare_async(const uint64_t *handles, uint64_t *out, int n) { diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 83e716bb..d224dd47 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -466,6 +466,9 @@ static void times_begin(VkCommandBuffer cb) { } // ---------------------------------------------------------------- motion vectors +static int cam_mode(void); // the jitter in the game's camera (below, "jitter in the game's camera") +static int cam_offset(uint64_t blk, float *sx, float *sy); +static int unj_put(VkCommandBuffer cb, const float *blk, float sx, float sy, VkBuffer buf, VkDeviceSize off); // The camera's motion first (runtime/shaders/dlaa_mv.comp): this frame's scene camera block is copied out of the // replay's uniform snapshot (vk_usnap.c, as of the submit call) into one slot of camb, last frame's stays in the other, // and the compute pass reprojects every pixel's depth through the two (inv4 and mv_pass below). Then the per-object @@ -579,8 +582,11 @@ static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float j const float *cr = (const float *)vk_usnap_bytes(D.cam_addr + 48, 64); double cur_vp[16], m[16], ci[16]; int have_m = 0; + float osx = 0.0f, osy = 0.0f; + if (cam_mode()) cam_offset(D.cam_addr, &osx, &osy); if (cr) { for (int i = 0; i < 16; i++) cur_vp[i] = cr[i]; + for (int k = 0; k < 4; k++) cur_vp[k] -= osx * cr[12 + k], cur_vp[4 + k] -= osy * cr[12 + k]; if (D.cam_prev_ok && inv4(cur_vp, ci)) { for (int r = 0; r < 4; r++) for (int k = 0; k < 4; k++) { @@ -595,6 +601,8 @@ static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float j VkBufferCopy bc = {cam.offset, (VkDeviceSize)s * CAM_SLOT, CAM_BLOCK}; vkCmdCopyBuffer(cb, cam.buffer, D.camb, 1, &bc); full_barrier(cb); + const float *cb_bytes = osx != 0.0f || osy != 0.0f ? (const float *)vk_usnap_bytes(D.cam_addr, 0xb0) : NULL; + if (cb_bytes && unj_put(cb, cb_bytes, osx, osy, D.camb, (VkDeviceSize)s * CAM_SLOT)) full_barrier(cb); D.cam_slot ^= 1, D.cam_prev_ok = cr != NULL; if (cr) memcpy(D.cam_prev, cur_vp, sizeof cur_vp); if (!had_prev || !have_m) { // nothing to reproject into: zero motion, new history @@ -670,7 +678,8 @@ static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float j int vk_dlaa_prev_camera(VkDescriptorBufferInfo *out, float jitter[2]) { if (!dl_active() || !D.cam_prev_ok || !D.camb) return 0; *out = (VkDescriptorBufferInfo){D.camb, (VkDeviceSize)(D.cam_slot ^ 1) * CAM_SLOT, CAM_BLOCK}; - jitter[0] = D.jit_ok ? D.jx : 0.0f, jitter[1] = D.jit_ok ? D.jy : 0.0f; + if (cam_mode()) jitter[0] = D.jit_used ? D.ux : 0.0f, jitter[1] = D.jit_used ? D.uy : 0.0f; + else jitter[0] = D.jit_ok ? D.jx : 0.0f, jitter[1] = D.jit_ok ? D.jy : 0.0f; return 1; } @@ -849,6 +858,7 @@ static int ended_at(const VkExec *ex) { } void vk_dlaa_viewport(VkExec *ex, VkViewport *v) { + if (cam_mode()) return; // the camera carries the jitter if (!D.jit_ok || !ex->depth.image || ex->depth.image != D.jdepth || ended_at(ex)) return; v->x += D.jx, v->y += D.jy; D.ux = D.jx, D.uy = D.jy; @@ -861,8 +871,15 @@ static void note_camera(const VkExec *ex) { const p_bind_buf *b = &ex->ubo[0][1]; // constant bank c4: the replay keeps user banks from c3 on const VkExecTarget *z = &ex->depth; if (b->size == 0x940 && b->addr && z->image && z->width >= DLAA_SCENE_MIN_W && z->format == VK_FORMAT_D32_SFLOAT && - (!D.jdepth || z->image == D.jdepth)) + (!D.jdepth || z->image == D.jdepth)) { + if (cam_mode() && b->addr != D.cam_addr) { + float sx, sy; + int j = cam_offset(b->addr, &sx, &sy); + D.ux = sx * (float)z->width * 0.5f, D.uy = -sy * (float)z->height * 0.5f; + __atomic_store_n(&D.jit_used, j, __ATOMIC_RELAXED); + } D.cam_addr = b->addr; + } } void vk_dlaa_draw(VkExec *ex) { @@ -921,6 +938,166 @@ static void next_jitter(uint32_t w, uint32_t ow) { D.jit_ok = !D.hold && !nj; } +// ---------------------------------------------------------------- jitter in the game's camera (1.4.3) +// Moving the viewport jitters what the scene draws rasterise, but not the camera the game reconstructs positions with: +// its full-screen passes rebuild each pixel's view ray from the camera block (the half-size lighting's vertex shaders +// read the projection's centre terms, P[0][2] and P[1][2], with its tangents) and its depth, so with the depth half a +// pixel off they put a surface's points off the surface by half a pixel of its depth slope. On water seen at a low +// angle that decides whether a screen-space reflection ray hits or misses, and whole patches of a pond flipped between +// the trees' reflection and the sky's with the jitter's phase (a period of three frames), more than DLSS could resolve; +// measured without DLSS with the same jitter, the pond's brightness swung 2.3-3.0 levels with the viewport moved and +// 0.03-0.04 with the camera moved, the edges shimmering alike. So on 1.4.3 the jitter goes into the camera itself, where +// the game copies its matrices into each camera's uniform block (f_70000e31d4: the projection, rows 7-10, then the +// view-projection, rows 3-6, each a 64-byte memcpy, which ov_memcpy hands over): a perspective camera with the scene's +// aspect, the main one and its half-size copy among them, gets the offset in its projection's centre terms and the same +// in its view-projection, x_clip += sx w and y_clip += sy w. Its shadow cascades are orthographic and stay as they are. +// The replay reads the offset back from the snapshot of the scene camera's block (cam_offset, the snapshot is the block +// as of the submit) for DLSS, the motion vectors and the per-object pass, which reproject with the camera unjittered; +// after the DLSS point the snapshots of the jittered blocks are written back unjittered (cam_unjitter), so the effects +// drawn over DLSS's picture stay unjittered as before. The game's own projections are symmetric (P[0][2] = P[1][2] = 0 +// in every block seen), which is what lets the offset be read back. TOTK_DLAA_CAM_JITTER=0 (test) moves the viewport +// as on the other game versions. +#define CAMJ_P_LR 0x70000e3688ull // the return address of the projection's copy (rows 7-10) +#define CAMJ_VP_LR 0x70000e36f4ull // and of the view-projection's (rows 3-6) +#define CAMJ_BLOCKS 16 +#define UNJ_SLOTS 4096u // 64-byte patches in flight (cam_unjitter, mv_pass): about 60 frames' worth + +static struct { + PlatMutex lk; + uint64_t seen[CAMJ_BLOCKS]; // blocks whose projection was copied since the jitter last moved on + int nseen; + uint64_t blocks[CAMJ_BLOCKS]; // every block that took an offset (cam_unjitter) + int nblocks; + uint32_t idx; // phases handed out + float sx, sy; // this frame's offset, clip units (0: none) + VkBuffer unj; // UNJ_SLOTS host-visible patches the replay copies from + VmaAllocation unj_mem; + uint8_t *unj_map; + uint32_t unj_slot; +} CJ = {.lk = PLAT_MUTEX_INIT}; + +static int cam_mode(void) { + static int on = -1; + if (on < 0) on = rt_game_version == 143 && rt_knob("DLAA_CAM_JITTER", "1") && rt_knob("UBO_SNAPSHOT", "1"); + return on; +} + +static int in_list(const uint64_t *l, int n, uint64_t a) { + for (int i = 0; i < n; i++) + if (l[i] == a) return 1; + return 0; +} + +// The game's thread, after a 64-byte memcpy: lr is the copy's return address into the game, dst where it copied to. +void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst) { + if ((lr != CAMJ_P_LR && lr != CAMJ_VP_LR) || !cam_mode()) return; + float *m = (float *)(uintptr_t)dst; + uint64_t blk = dst - (lr == CAMJ_P_LR ? 0x70 : 0x30); + plat_mutex_lock(&CJ.lk); + if (lr == CAMJ_P_LR) { + unsigned o[2], s[2]; + res_sizes(o, s); + float asp = s[1] ? (float)s[0] / (float)s[1] : 0.0f; + // perspective (w = -z), symmetric, with the scene's aspect + if (m[14] != -1.0f || m[15] != 0.0f || m[2] != 0.0f || m[6] != 0.0f || !(m[0] > 0.0f) || + fabsf(m[5] / m[0] - asp) > 0.01f * asp) { + plat_mutex_unlock(&CJ.lk); + return; + } + if (in_list(CJ.seen, CJ.nseen, blk)) { // the game copies each camera once a frame: a block again is a new frame + static int nj = -1; + if (nj < 0) nj = !rt_knob("DLAA_JITTER", "1"); + CJ.nseen = 0, CJ.sx = CJ.sy = 0.0f; + if (dl_active() && !__atomic_load_n(&D.hold, __ATOMIC_RELAXED) && !nj && s[0] && s[1]) { + double r = (double)o[0] / s[0]; + uint32_t ph = (uint32_t)ceil(8.0 * r * r - 1e-6); + ph = ph < 16 ? 16 : ph > 72 ? 72 : ph; + uint32_t i = CJ.idx++ % ph + 1; + CJ.sx = 2.0f * (halton(i, 2) - 0.5f) / (float)s[0]; // content right by the offset in pixels + CJ.sy = -2.0f * (halton(i, 3) - 0.5f) / (float)s[1]; // and down: NDC +y is the top row + } + } + if (CJ.nseen < CAMJ_BLOCKS) CJ.seen[CJ.nseen++] = blk; + if (CJ.sx != 0.0f || CJ.sy != 0.0f) { + m[2] -= CJ.sx, m[6] -= CJ.sy; + if (!in_list(CJ.blocks, CJ.nblocks, blk) && CJ.nblocks < CAMJ_BLOCKS) CJ.blocks[CJ.nblocks++] = blk; + } + } else if ((CJ.sx != 0.0f || CJ.sy != 0.0f) && in_list(CJ.seen, CJ.nseen, blk) && + in_list(CJ.blocks, CJ.nblocks, blk)) { + for (int k = 0; k < 4; k++) m[k] += CJ.sx * m[12 + k], m[4 + k] += CJ.sy * m[12 + k]; + } + plat_mutex_unlock(&CJ.lk); +} + +// The offset a camera block took (clip units), from its snapshot, else from guest memory: a chunked replay's scan can +// come before the snapshot, and the block holds this frame's bytes until the game's next frame. +static int cam_offset(uint64_t blk, float *sx, float *sy) { + *sx = *sy = 0.0f; + if (!blk) return 0; + const float *p = (const float *)vk_usnap_bytes(blk + 0x70, 32); + if (!p) p = (const float *)(uintptr_t)(blk + 0x70); + *sx = -p[2], *sy = -p[6]; + return *sx != 0.0f || *sy != 0.0f; +} + +// Records copies that write a block's view-projection rows 3-4 and projection rows 7-8 back without the offset (blk: +// the block's bytes as the snapshot holds them) into buf at off, the block's place there. 0: no room. +static int unj_put(VkCommandBuffer cb, const float *blk, float sx, float sy, VkBuffer buf, VkDeviceSize off) { + if (!CJ.unj) { + VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = UNJ_SLOTS * 64u, + .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT}; + VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST, + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | + VMA_ALLOCATION_CREATE_MAPPED_BIT}; + VmaAllocationInfo inf; + if (vmaCreateBuffer(vk_ctx()->vma, &bi, &ai, &CJ.unj, &CJ.unj_mem, &inf) != VK_SUCCESS) return CJ.unj = 0, 0; + CJ.unj_map = inf.pMappedData; + } + uint32_t sl = __atomic_fetch_add(&CJ.unj_slot, 1, __ATOMIC_RELAXED) % UNJ_SLOTS; + float v[16]; + for (int k = 0; k < 4; k++) v[k] = blk[12 + k] - sx * blk[24 + k], v[4 + k] = blk[16 + k] - sy * blk[24 + k]; + memcpy(v + 8, blk + 28, 32); + v[10] = 0.0f, v[14] = 0.0f; + memcpy(CJ.unj_map + (size_t)sl * 64, v, sizeof v); + vmaFlushAllocation(vk_ctx()->vma, CJ.unj_mem, (VkDeviceSize)sl * 64, 64); + VkBufferCopy r[2] = {{(VkDeviceSize)sl * 64, off + 0x30, 32}, {(VkDeviceSize)sl * 64 + 32, off + 0x70, 32}}; + vkCmdCopyBuffer(cb, CJ.unj, buf, 2, r); + return 1; +} + +// After the DLSS point, once in each command buffer of the frame: the snapshots of the blocks that took an offset are +// written back without it, so the effects drawn over DLSS's picture use the camera unjittered, as before the move. +static void cam_unjitter(VkExec *ex) { + if (!cam_mode() || !dl_active() || !ended_at(ex)) return; + static PlatMutex lk = PLAT_MUTEX_INIT; + static VkCommandBuffer done[32]; + static int nd; + static uint64_t done_frame = UINT64_MAX; + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + plat_mutex_lock(&lk); + if (f != done_frame) done_frame = f, nd = 0; + int dup = 0; + for (int i = 0; i < nd && !dup; i++) dup = done[i] == ex->cb; + if (!dup && nd < 32) done[nd++] = ex->cb; + plat_mutex_unlock(&lk); + if (dup) return; + uint64_t blocks[CAMJ_BLOCKS]; + plat_mutex_lock(&CJ.lk); + int n = CJ.nblocks; + memcpy(blocks, CJ.blocks, sizeof blocks); + plat_mutex_unlock(&CJ.lk); + int any = 0; + for (int i = 0; i < n; i++) { + VkDescriptorBufferInfo bi; + float sx, sy; + const float *b = (const float *)vk_usnap_bytes(blocks[i], 0xb0); + if (!b || !cam_offset(blocks[i], &sx, &sy) || !vk_usnap_find(blocks[i], 0xb0, UINT32_MAX, 4, &bi)) continue; + if (!any) full_barrier(ex->cb); // the scene's draws read the jittered rows until here + any |= unj_put(ex->cb, b, sx, sy, bi.buffer, bi.offset); + } + if (any) full_barrier(ex->cb), ex->recorded += 3; +} + // The DLSS model (TOTK_DLSS_PRESET, live): default takes NVIDIA's model per mode (evaluate: K for DLAA, Quality and // Balanced, M for Performance, L for Ultra Performance), 0 here. A letter a..m takes that model in every mode: k pins // K, j is the other transformer model, which NVIDIA describes as slightly less ghosting at the cost of extra flicker, e @@ -1090,6 +1267,7 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { vk_exec_draw_new_cb(ex); // NGX bound pipelines and descriptor sets of its own: the next draw binds everything again D.jdepth = z->image; next_jitter(w, ow); + cam_unjitter(ex); if (++D.evals == 1 || D.evals == 300 || D.evals % 3600 == 0) // 300: one early line with the frames counted rt_log("dlaa: %llu frames anti-aliased (scene %ux%u, output %ux%u; %llu before the effects, %llu frames without the " "DLAA point, %llu with camera motion, %llu with the characters' mask, %llu without a camera, %llu jitter " @@ -1309,6 +1487,7 @@ void vk_dlaa_exec(VkExec *ex, const VkExecJob *j) { } void vk_dlaa_pass(VkExec *ex) { + cam_unjitter(ex); VkExecJob j; if (!ex->chunked && vk_dlaa_plan(ex, &j)) vk_dlaa_exec(ex, &j); } diff --git a/runtime/vk_dlaa.h b/runtime/vk_dlaa.h index cce8877d..719c5171 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -42,6 +42,8 @@ void vk_mvprev_draw(VkExec *ex); // vk_mvprev.c: notes the scene draw's object int vk_mvobj_execute(VkExec *ex, uint64_t cam, VkImageView mv, VkImageView depth, VkFormat depth_fmt, uint32_t w, uint32_t h); int vk_dlaa_prev_camera(VkDescriptorBufferInfo *out, float jitter[2]); +// nvn_api_core.c ov_memcpy, after a 64-byte copy on the game's thread: the camera jitter of 1.4.3 (vk_dlaa.c). +void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst); // vk_mvcheck.c (TOTK_MV_CHECK, test): mv_pass hands it the motion image after the camera pass and, after the object // pass, the motion image again and the scene light target it ran on; it measures how well the vectors fit the picture. void vk_mvcheck_camera(VkCommandBuffer cb, VkImage mv, uint32_t w, uint32_t h, float jx, float jy); diff --git a/runtime/vk_usnap.c b/runtime/vk_usnap.c index 660aa724..43356141 100644 --- a/runtime/vk_usnap.c +++ b/runtime/vk_usnap.c @@ -89,7 +89,8 @@ static Arena *arena_get(uint64_t need) { a->size = need < ARENA_MIN ? ARENA_MIN : (need + (1u << 20) - 1) & ~(uint64_t)((1u << 20) - 1); VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = a->size, // transfer source: DLAA copies the camera block out (vk_dlaa.c mv_pass) - .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT}; + .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | + VK_BUFFER_USAGE_TRANSFER_DST_BIT}; // Device-local through the BAR when the device has it (the GPU reads constants on every draw), else host memory. VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |