diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 483875d5..ffd50bc8 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -27,6 +27,8 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | one flat ambient colour | a 1-layer 2D array sampled as 2D | the view type the shader expects | | upside-down text | lower-left window origin | origin mode in the log | | puddles and still water flicker: the reflection turns on and off in patches from frame to frame while the camera stands still, more with DLAA | the game's water volume mask: a volume's top face lies on the water surface and is tested LEQUAL against a depth rebuilt at quarter size, so rounding and the camera's sub-millimetre motion decide it (fixed: `vk_pipeline.c` `vk_pipeline_volume_bias` gives such draws a depth bias toward the viewer) | a `--capture` run: the puddle's brightness swing (`capture.py` frames, a region's mean) with `TOTK_VOLUME_MASK_BIAS=0` and without; in a frame dump, the quarter-size mask after the volume draw changes from frame to frame | +| with DLAA on, water seen at a low angle (ponds, streams) flickers in large patches with a period of about three frames, between the reflection of the trees and that of the sky; without DLAA it is still | the jitter moved the viewport of the scene's draws but not the game's camera, so its full-screen passes rebuilt positions half a pixel off the surface and its screen-space reflections flipped between hit and miss with the jitter's phase (fixed on 1.4.3: `vk_dlaa.c` puts the jitter into the camera blocks where the game copies its matrices, `vk_dlaa_camera_copied`) | `TOTK_DLAA_CAM_JITTER=0` brings the viewport jitter back; a `--capture` run: the region's brightness swing with DLAA, against `TOTK_DLAA_JITTER=0` | +| with DLAA on, the world behind the inventory smears into horizontal streaks on the right half, from the second time the inventory is opened (when Link's model is drawn there) | the camera jitter's read-back took the inventory camera's own off-centre projection (P[0][2] about -0.6) for a jitter and gave DLSS an offset of about 1152 pixels (fixed: `vk_dlaa.c` `cam_offset` takes only an offset it gave that block) | `TOTK_DLAA_CAM_JITTER=0` makes it go; a `--capture` run over the opened inventory: the background's horizontal gradient against its vertical one | | flicker or a texture swapped with a similar one | the newest texture picked at shared storage instead of the one with the matching view | texture lookup by id in the dump | | with DLAA on, dust and small particles in fog (shrines) vanish, smear or behave oddly; the fog looks processed | DLSS ran at the bloom, after the fog and the particles, which have no motion vectors or jitter of their own (fixed: `vk_dlaa.c` effects_point runs DLSS at the first pass into the light target after the lighting, and the effects go over its picture) | `dlaa_effects_after = 0` (Screen tab) brings the old point back; the count "before the effects" in the `dlaa:` log line against the frames anti-aliased | | with DLAA on, the picture flickers from frame to frame: a little outdoors; the whole scene in a fogged shrine with an ability's targeting view (Ultrahand) open, where the fog also goes back under DLSS | DLSS ran before some effects in one frame and after them in the next: their order changes from frame to frame (the yard's half-size effects pass is in some frames only; the ability highlight, drawn into the light target with a D16 depth of its own, comes before or after the fog layer), and a rule that waited for one of them or took any D16 pass for the lighting followed it (fixed: `vk_dlaa.c` effects_point knows the lighting by its depth and runs DLSS right after it) | the `dlaa:` log line: "before the effects" must equal the frames anti-aliased in the world; a log of the passes into the light target per frame | diff --git a/docs/settings.md b/docs/settings.md index 6f083969..23983cf4 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -175,6 +175,7 @@ Many rows below are developer tools that only act together with another setting | Setting | What it does | Effect of changing it | Safety | | --- | --- | --- | --- | +| `dlaa_cam_jitter` (default `1`) | Game 1.4.3: `1` puts the jitter fed to DLSS into the game's camera (the projection's centre terms and the view-projection, where the game copies them into its camera blocks); `0` moves the viewport of the scene's draws instead, as on the other game versions. Needs `ubo_snapshot`. | With `0`, water seen at a low angle flickers with DLAA: the game's full-screen passes rebuild positions with a camera the scene was not drawn with; a test setting. | Caution | | `dlaa_jitter` (default `1`) | `0` turns off the camera jitter fed to DLSS. | DLSS gets no jitter between frames; a test setting. | Caution | | `dlaa_maskdebug` (default `0`) | `1` shows the reactive mask in place of the picture. | The frame shows a debug image while it is set. | Caution | | `dlaa_mv` (default `1`) | `0` sends zero motion vectors to DLSS. | DLSS sees no motion, so moving content can smear; a test setting. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index 3b0464e1..cfaa5beb 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -40,7 +40,7 @@ static const char *const known[] = { "BIND_INC", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", "BIND_ZL", "BIND_ZR", "BARRIER_MERGE", "BLOCK_VIEWS_3D", "CLIMB_FIX", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", - "DESC_TEMPLATE", "DESWIZZLE", "DEVICE_PROFILE", "DIALOGUE_CLICK", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_EFFECTS_AFTER", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", + "DESC_TEMPLATE", "DESWIZZLE", "DEVICE_PROFILE", "DIALOGUE_CLICK", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_JITTER", "DLAA_EFFECTS_AFTER", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "DYNAMIC_RES", "DYN_DELTA", "EXEFS", "FEEDBACK", "FIXED_RES_PATCH", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FRAME_QUEUE", "FREEZE_IDLE", "FREE_CORES", 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_backend.c b/runtime/vk_backend.c index 09ec7e5e..497529a8 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -345,6 +345,7 @@ void gfx_exit_log(void) { static void vk_be_present(const tex_t *t, const int crop[4], uint64_t at_ns) { static _Thread_local int pcore; // the game's render thread: the one that presents if (!pcore) pcore = 1, perf_core_thread(PCORE_RENDER); + vk_dlaa_game_present(); // the camera jitter moves on with the game's frames (vk_dlaa.c) vk_sync_start(); if (!gfx_replay_async()) { uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED); diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 83e716bb..2b1e2f53 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; } @@ -830,7 +839,8 @@ static struct Snap *mask_pass(VkCommandBuffer cb, VkImageView cv, uint32_t w, ui } // ---------------------------------------------------------------- jitter -// The scene's draws (every pass drawing with the scene depth, until the DLAA point) have their viewport moved by a +// On 1.4.3 the game's camera carries the jitter instead (below, "jitter in the game's camera"); on the other versions +// the scene's draws (every pass drawing with the scene depth, until the DLAA point) have their viewport moved by a // sub-pixel Halton(2,3) offset, which shifts what they rasterise as a jittered projection would; full-screen passes // that shade by window position are unaffected. Jitter only follows a frame DLSS ran on: a jittered frame that DLSS // does not resolve shakes on screen, so a missed DLAA point stops the jitter for a second. @@ -849,6 +859,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 +872,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 +939,192 @@ 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 offset moves on at the first projection copied after each of the game's presents (vk_dlaa_game_present): the game +// fills two sets of blocks in turn, one per frame, so a block written again is not a new frame. +// 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. A block's centre terms count as an offset only when they are one +// this code gave that block (CJ.given): the game has off-centre projections of its own, such as the inventory's camera, +// which draws Link right of the screen's centre with P[0][2] about -0.6, and read as a jitter that sent DLSS an offset +// of 1152 pixels, smearing the world behind the inventory into horizontal streaks. 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 CAMJ_GIVEN 256 // offsets given lately, by block: about 40 frames' worth, the replay is a few behind +#define UNJ_SLOTS 4096u // 64-byte patches in flight (cam_unjitter, mv_pass): about 60 frames' worth + +static struct { + PlatMutex lk; + uint64_t presents, moved; // the game's presents, and their count when the offset last moved on + struct { uint64_t blk; float sx, sy; } cur[CAMJ_BLOCKS]; // this frame's blocks and the offset each projection took + int ncur; + struct { uint64_t blk; float sx, sy; } given[CAMJ_GIVEN]; // each offset given and its block, a ring + uint32_t ngiven; + 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; +} + +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) { + if ((lr != CAMJ_P_LR && lr != CAMJ_VP_LR) || !cam_mode() || !dl_active()) 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; + } + uint64_t pf = __atomic_load_n(&CJ.presents, __ATOMIC_ACQUIRE); + if (pf != CJ.moved) { // the first projection of a new frame + static int nj = -1; + if (nj < 0) nj = !rt_knob("DLAA_JITTER", "1"); + CJ.moved = pf, CJ.ncur = 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.sx != 0.0f || CJ.sy != 0.0f) { + 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; + } + } 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) { + 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]; + break; + } + } + 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. Centre terms this code +// did not give the block are the game's own and read as no offset. +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); + float x = -p[2], y = -p[6]; + if (x == 0.0f && y == 0.0f) return 0; + int ours = 0; + plat_mutex_lock(&CJ.lk); + for (uint32_t i = 0, n = CJ.ngiven < CAMJ_GIVEN ? CJ.ngiven : CAMJ_GIVEN; i < n && !ours; i++) + ours = CJ.given[i].blk == blk && CJ.given[i].sx == x && CJ.given[i].sy == y; + plat_mutex_unlock(&CJ.lk); + if (ours) *sx = x, *sy = y; + return ours; +} + +// 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]; // the blocks given an offset lately, newest first + int n = 0; + plat_mutex_lock(&CJ.lk); + for (uint32_t k = 0, m = CJ.ngiven < CAMJ_GIVEN ? CJ.ngiven : CAMJ_GIVEN; k < m && n < CAMJ_BLOCKS; k++) { + uint64_t blk = CJ.given[(CJ.ngiven - 1 - k) % CAMJ_GIVEN].blk; + if (!in_list(blocks, n, blk)) blocks[n++] = blk; + } + 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 +1294,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 +1514,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..f06b7a6d 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -42,6 +42,9 @@ 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); +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. 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 |