diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index 9b39926c..0462c66a 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -53,19 +53,22 @@ void nvn_guest_written(uint64_t addr, uint64_t size) { if (size) gfx_backend()->guest_written(addr, size); } +// The camera jitter of 1.4.3 (vk_dlaa.c); a weak default like the hooks below. +void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst, uint64_t off); +__attribute__((weak)) void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst, uint64_t off) { (void)lr, (void)dst, (void)off; } + // 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); +// arguments are undefined for memcpy anyway. TotK 1.4.3 copies its camera matrices with 64-byte memcpys +// (f_70000e31d4); x8 holds the member's offset in the block there (vk_dlaa.c vk_dlaa_camera_copied). 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) + if (n == 64) vk_dlaa_camera_copied(c->x[30], dst, c->x[8]); 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 2b1e2f53..b00d5af1 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -997,11 +997,15 @@ static int in_list(const uint64_t *l, int n, uint64_t a) { void vk_dlaa_game_present(void) { __atomic_add_fetch(&CJ.presents, 1, __ATOMIC_RELEASE); } -// 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) { +// The game's thread, after a 64-byte memcpy: lr is the copy's return address into the game, dst where it copied to, off +// the member's offset in the block. f_70000e31d4 takes each offset from the shader's layout of the block (x8 at both +// copies, dst = the block's base + x8), so a block takes the offset only with its matrices where the rest of this +// code reads and writes them, the projection at 0x70 and the view-projection at 0x30, as in the scene camera's block. +void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst, uint64_t off) { if ((lr != CAMJ_P_LR && lr != CAMJ_VP_LR) || !cam_mode() || !dl_active()) return; + if (lr == CAMJ_P_LR && off != 0x70) return; float *m = (float *)(uintptr_t)dst; - uint64_t blk = dst - (lr == CAMJ_P_LR ? 0x70 : 0x30); + uint64_t blk = dst - off; plat_mutex_lock(&CJ.lk); if (lr == CAMJ_P_LR) { unsigned o[2], s[2]; @@ -1027,18 +1031,26 @@ void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst) { CJ.sy = -2.0f * (halton(i, 3) - 0.5f) / (float)s[1]; // and down: NDC +y is the top row } } - if (CJ.sx != 0.0f || CJ.sy != 0.0f) { + int i = 0; + while (i < CJ.ncur && CJ.cur[i].blk != blk) i++; + if ((CJ.sx != 0.0f || CJ.sy != 0.0f) && i < CAMJ_BLOCKS) { // no room: no offset, its view-projection takes none m[2] -= CJ.sx, m[6] -= CJ.sy; - uint32_t g = CJ.ngiven++ % CAMJ_GIVEN; - CJ.given[g].blk = blk, CJ.given[g].sx = CJ.sx, CJ.given[g].sy = CJ.sy; - int i = 0; - while (i < CJ.ncur && CJ.cur[i].blk != blk) i++; - if (i < CAMJ_BLOCKS) CJ.cur[i].blk = blk, CJ.cur[i].sx = CJ.sx, CJ.cur[i].sy = CJ.sy, CJ.ncur += i == CJ.ncur; + CJ.cur[i].blk = blk, CJ.cur[i].sx = CJ.sx, CJ.cur[i].sy = CJ.sy, CJ.ncur += i == CJ.ncur; } } else { // the view-projection takes the offset its block's projection took this frame for (int i = 0; i < CJ.ncur; i++) if (CJ.cur[i].blk == blk) { + if (off != 0x30) { // elsewhere in this block: the projection goes back to its centre terms of 0 + float *pm = (float *)(uintptr_t)(blk + 0x70); + pm[2] += CJ.cur[i].sx, pm[6] += CJ.cur[i].sy; + CJ.cur[i] = CJ.cur[--CJ.ncur]; + break; + } for (int k = 0; k < 4; k++) m[k] += CJ.cur[i].sx * m[12 + k], m[4 + k] += CJ.cur[i].sy * m[12 + k]; + // Given only now that both matrices carry it: a block whose view-projection did not take it is never + // read back as jittered, nor written back by cam_unjitter. + uint32_t g = CJ.ngiven++ % CAMJ_GIVEN; + CJ.given[g].blk = blk, CJ.given[g].sx = CJ.cur[i].sx, CJ.given[g].sy = CJ.cur[i].sy; break; } } diff --git a/runtime/vk_dlaa.h b/runtime/vk_dlaa.h index f06b7a6d..1275c27e 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -43,7 +43,7 @@ int vk_mvobj_execute(VkExec *ex, uint64_t cam, VkImageView mv, VkImageView depth 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); +void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst, uint64_t off); void vk_dlaa_game_present(void); // vk_backend.c, each of the game's presents, on its thread // 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.