From fd08b5cee3787938d3f86e86a0bc9a6e996023e7 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Mon, 16 Nov 2026 03:22:06 +0300 Subject: [PATCH 01/20] DLSS is created without auto exposure, so large distant blocks behind the fog in the Depths no longer turn darker or lighter for a frame while the camera moves --- docs/agents/symptoms.md | 1 + runtime/vk_dlaa.c | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 5c9fb005..5a96e8a8 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -26,6 +26,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | no sun shadows, wrong lighting | unwritten vertex outputs read as zero instead of (0,0,0,1) | the shader's output map | | 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 | +| with DLAA/DLSS on, large distant blocks behind the fog (the Depths) turn darker or lighter for a frame while the camera moves | DLSS created with AutoExposure estimated the exposure of the dark scene itself, and the estimate moved from frame to frame (fixed: `vk_dlaa.c` evaluate creates the feature without it) | a flat fogged patch in `capture.py` frames: one value without DLAA, jumps with it | | 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/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 83e716bb..160c0322 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -1007,8 +1007,12 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { p_setu(p, "OutWidth", ow); p_setu(p, "OutHeight", oh); p_seti(p, "PerfQualityValue", perf_quality(w, ow)); - // IsHDR | MVLowRes | AutoExposure: linear scene light, motion vectors at the render size, no exposure texture - p_seti(p, "DLSS.Feature.Create.Flags", (1 << 0) | (1 << 1) | (1 << 6)); + // IsHDR | MVLowRes: linear scene light, motion vectors at the render size, no exposure texture. Not AutoExposure + // (1 << 6): DLSS then estimated the exposure from each frame itself, and in the Depths, where the whole scene is + // dark, the estimate moved with the camera; distant blocks under uniform fog came out up to 0.7 levels (of 255) + // darker or brighter for a frame, where the game's own picture held one value (a 64x64 patch at 30.79 without + // DLAA read 30.09-30.79 frame to frame with AutoExposure and 30.4-30.7, steady, without it). + p_seti(p, "DLSS.Feature.Create.Flags", (1 << 0) | (1 << 1)); p_seti(p, "DLSS.Enable.Output.Subrects", 0); // The model per mode (NVSDK_NGX_DLSS_Hint_Render_Preset: 1 A ... 11 K, 12 L, 13 M), as NVIDIA recommends for // DLSS 4.5 (310.x): K (transformer) for DLAA, Quality and Balanced, M for Performance, L for Ultra Performance. From 68078948f560213c2022850b95b970e657dfe1ba Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Mon, 16 Nov 2026 09:40:56 +0300 Subject: [PATCH 02/20] With sync_ahead an invalidate of pool storage waits for the GPU write of a value the game was told is done when the range holds no older result, so the pixels TotK reads back once through a buffer per request are no longer zeros or an answer from the ring's last round, which in the Depths put Link in gloom on clean ground; buffers the game reads every frame still get the result of the frame before without a wait --- docs/agents/symptoms.md | 1 + docs/architecture.md | 2 +- docs/pr-review-kit/04-knowledge.md | 1 + runtime/nvn_api.h | 3 ++ runtime/nvn_api_core.c | 3 +- runtime/nvn_ext.c | 1 + runtime/vk_memory.c | 51 ++++++++++++++++++++++++++++-- totk.ini.example | 2 +- 8 files changed, 58 insertions(+), 6 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 5c9fb005..4a242b12 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -39,6 +39,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | frame counter wanders 50-60 | two 60 Hz clocks (vsync event and present acquire) drifting apart | whether both follow one display tick | | a 30 Hz thread runs at 27 Hz, slow cutscenes | host sleep overshoot: Windows waitable timers wake 0.3-0.5 ms late | `TOTK_TRACE=sched` sleep asked versus took | | 20 fps after a scheduler change | a yield back-off that starts too early | yield handling against the console's | +| in the Depths Link takes gloom damage and wears the gloom haze while standing on clean ground, some runs only; only with `sync_ahead = 1` | the game read a GPU result of the frame it was told is done before the GPU wrote it: its one-pixel readbacks go through a buffer per request, which then holds zeros or an answer from the ring's last round (fixed: `vk_memory.c` `vk_mem_shadow_read` waits at the invalidate) | `sync_ahead = 0`; the shadow log line's "the game waited for a GPU write it reads" count | | climbing or movement wrong only above some fps | game code that assumes a fixed step count per frame | compare at 30, 60 and the failing rate | ## Threads and crashes diff --git a/docs/architecture.md b/docs/architecture.md index 8dd04a24..e9e637a6 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -90,7 +90,7 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app - Memory pools. Apple's unified memory let MacNativeGames use the game's pools zero-copy. The back end imports the pools as host memory (`VK_EXT_external_memory_host`). On a discrete GPU, pools bound as buffers are mirrored in device-local memory and the pages the CPU wrote (write watch, above) are uploaded before a replay; idle pool regions are made read-only so the write-watch queries skip them (`vk_memory.c`). GPU-rendered textures are written back when the CPU reads them: on a pool invalidate, on a CPU copy, and for small targets once the CPU waits for a fence (`vk_texture.c`). - Replay thread. `TOTK_REPLAY_ASYNC=1` moves the replay off the submitting thread onto a queue at most one frame behind the game; reads of GPU results wait for the newest writer instead of draining the queue (`vk_backend.c`). It is off by default. Where the game waits for the GPU it overlaps the replay with the game threads (a GTX 1650 in the Depths, issue #88), a gain measured only there; while it was the default, a GTX 1660 Ti at the 60 fps cap with the GPU at 26 % reported uneven motion (#659). It adds up to a frame of latency; `replay_async = 1` turns it on. - Chunked replay. A large submission is recorded in chunks by several threads (`TOTK_REPLAY_WORKERS`, 0 by default: the sequential replay; 4 is a good value from 8 logical processors) and the chunks go to the queue in order (`vk_exec.c`). A sequential scan keeps a copy of the queue state where each chunk starts and lists what a chunk would otherwise learn from the chunks before it: inline uniform updates, conditions, contact shadow marches, uniform drops. It also makes render targets live in replay order and notes the op, since a chunk can sample a storage before the chunk that renders it got there or after the scan went past a later target there: a chunk's texture slots resolve as of its own op, and the chunks after a texture with no entry yet wait for the chunk that makes it. DLSS's decisions (the G-buffer, the light target, the end of the scene, the scaling pass) are the scan's too, and its copy of the lit scene and its evaluation are records of the chunk that holds the op; a chunk's draws are jittered while they come before the scan's end of the scene. The evaluation waits until the chunks before it are recorded, as it reads what their draws noted (the camera block, the jitter, the per-object motion list, which chunks add to under one lock), and the chunks after it wait for it, as they sample its output. `TOTK_REPLAY_STATE_CHECK=1` compares the state after the last chunk with the scan's. -- Frame ahead. `TOTK_SYNC_AHEAD=1` (`sync_ahead`, off by default) makes the one `nvnSyncWait` per frame that waits for the frame presented last wait for the frame before it, so the game builds the next frame while the GPU still runs the previous one (`nvn_api_core.c`, `nvnSyncWait`). The game then writes memory that in-flight work still reads. This is safe only with shadow storage (`vk_memory.c`, the CPU-visible pools are host-visible copies of guest memory, on by default on Linux and Wine, `shadow = 1` (`TOTK_SHADOW=1`) on native Windows; without it the switch is ignored): a CPU write reaches the GPU only through the shadow copy made before each submit (`vk_mem_shadow_sync`), and a copy that would overwrite bytes an unfinished submission reads is queued as a GPU copy in the prologue command buffer of the part submitted next (`vk_mem_shadow_prologue`, `TOTK_SHADOW_DEFER=1`, the default with the feature) or, with `=0`, waited for on the replay thread (`shadow_reader_wait`). Only that one wait lags (identified by its call site, known for 1.1.0, 1.2.1 and 1.4.3); results the game reads back from the GPU after it can be a frame old. The fence write-back of small render targets (`vk_texture.c`, `write_back_n`) does not wait for the GPU with `TOTK_WB_EAGER=1` (`wb_eager`, the default, but only while frame-ahead is in effect: with `sync_ahead` off or refused the setting does nothing and the write-back reads the image when it runs): `vk_sync_submit` has `vk_texture_eager_record` copy the targets that value rendered into staging buffers inside the same command buffer, so once the value has passed the write-back only copies from staging into the target's storage. Only a target that was written back within the last 64 timeline values is copied (the first write-back of a target takes the blocking path), and the staging of a target past that window, no longer wanted or released goes through the deferred free (`vk_defer_buffer_until`). A snapshot is used only when its value is the one that last rendered the target, nothing recorded a write to the image since (`wr_fence`) and the GPU has finished it; otherwise the write-back reads the image as before, and the exit line `vk: wb_eager:` counts the reasons. With frame-ahead in effect the exit line `vk: fence write-backs waited for the GPU` counts the write-backs that waited for a copy and the time they waited, for every value of the setting. `check` also reads the old way and compares. +- Frame ahead. `TOTK_SYNC_AHEAD=1` (`sync_ahead`, off by default) makes the one `nvnSyncWait` per frame that waits for the frame presented last wait for the frame before it, so the game builds the next frame while the GPU still runs the previous one (`nvn_api_core.c`, `nvnSyncWait`). The game then writes memory that in-flight work still reads. This is safe only with shadow storage (`vk_memory.c`, the CPU-visible pools are host-visible copies of guest memory, on by default on Linux and Wine, `shadow = 1` (`TOTK_SHADOW=1`) on native Windows; without it the switch is ignored): a CPU write reaches the GPU only through the shadow copy made before each submit (`vk_mem_shadow_sync`), and a copy that would overwrite bytes an unfinished submission reads is queued as a GPU copy in the prologue command buffer of the part submitted next (`vk_mem_shadow_prologue`, `TOTK_SHADOW_DEFER=1`, the default with the feature) or, with `=0`, waited for on the replay thread (`shadow_reader_wait`). Only that one wait lags (identified by its call site, known for 1.1.0, 1.2.1 and 1.4.3). GPU writes into pool storage come back with their fence, so a buffer the game reads back every frame after that wait holds the result of the frame before; a buffer it has not read lately holds none, and its `nvnMemoryPoolInvalidateMappedRange` waits for the write (`vk_mem_shadow_read`): TotK reads single pixels through a buffer per request, and in the Depths zeros in one of them put Link in gloom. The fence write-back of small render targets (`vk_texture.c`, `write_back_n`) does not wait for the GPU with `TOTK_WB_EAGER=1` (`wb_eager`, the default, but only while frame-ahead is in effect: with `sync_ahead` off or refused the setting does nothing and the write-back reads the image when it runs): `vk_sync_submit` has `vk_texture_eager_record` copy the targets that value rendered into staging buffers inside the same command buffer, so once the value has passed the write-back only copies from staging into the target's storage. Only a target that was written back within the last 64 timeline values is copied (the first write-back of a target takes the blocking path), and the staging of a target past that window, no longer wanted or released goes through the deferred free (`vk_defer_buffer_until`). A snapshot is used only when its value is the one that last rendered the target, nothing recorded a write to the image since (`wr_fence`) and the GPU has finished it; otherwise the write-back reads the image as before, and the exit line `vk: wb_eager:` counts the reasons. With frame-ahead in effect the exit line `vk: fence write-backs waited for the GPU` counts the write-backs that waited for a copy and the time they waited, for every value of the setting. `check` also reads the old way and compares. - Recorder thread. The Vulkan calls the replay makes into its command buffer are made by a thread of their own (`vk_rec.c`, after Savage Wind Recomp and LAS Recomp): each `vkCmd*` copies its arguments into a packet of a ring and the recorder makes the calls in order; calls into other command buffers are made at once; the recorder also ends the command buffer, and the submit waits until it has. In the town on an RX 9070 the replay takes 17% less time on the submitting thread. It is off by default on AMD GPUs, where the driver loses the device or hangs the GPU at startup with it (`TOTK_VK_RECORDER=1` forces it on, `=0` makes every call on the submitting thread). - Uniform snapshot. The game rewrites per-object uniform blocks on other threads soon after it submits the frame that reads them, while the replay still records it. At the start of a submission's replay every uniform range it diff --git a/docs/pr-review-kit/04-knowledge.md b/docs/pr-review-kit/04-knowledge.md index 62eef26a..d70c7084 100644 --- a/docs/pr-review-kit/04-knowledge.md +++ b/docs/pr-review-kit/04-knowledge.md @@ -30,6 +30,7 @@ If a PR touches this code, confirm the fix still holds. | lime silhouettes in grass; rectangles in the sky | a target outside the output map must keep its texel; zero-filling it wiped the half-res depth | `vk_pipeline.c` `target_wmask` | | stale grass maps in the far terrain for a second after a hit | write-back of a texture the CPU had rewritten | `vk_texture.c` `write_back` | | cut grass stays standing; fire does not spread (#132) | CPU reads of GPU-rendered textures without an invalidate | `vk_texture.c` `vk_texture_write_back_small`, `nvn_ext.c` `nvn_pool_invalidate` | +| gloom damage on clean ground in the Depths with `sync_ahead` | the game read a one-request pixel buffer before the GPU of the frame in flight wrote it | `vk_memory.c` `vk_mem_shadow_read`, from `nvn_ext.c` `nvn_pool_invalidate` | | cloud and fog volumes empty for the session | a one-shot draw skipped instead of waiting for its pipeline | `vk_exec_draw.c` `prepare_draw` | | tile rectangles | storage buffers bound by Ryujinx first-use order, not the c0 slot | `vk_exec_draw.c` `ssbo_slot` | | black dialog background and loading map | 3D targets rejected, the grading LUT never written | `vk_exec.c` `resolve_target` | diff --git a/runtime/nvn_api.h b/runtime/nvn_api.h index 08ecd694..a5bcbe3e 100644 --- a/runtime/nvn_api.h +++ b/runtime/nvn_api.h @@ -52,6 +52,9 @@ uint64_t nvn_sync_ahead_guard(void); int nvn_sync_ahead_on(void); // 1 when the frame-ahead wait is in effect: TOTK_SYNC_AHEAD on and not refused (no shadow storage, no known wait site) int vk_mem_shadow_on(void); // vk_memory.c: 1 with shadow storage on (valid once the Vulkan back end is up) void vk_mem_shadow_fence(void); // vk_memory.c: returns once every shadow sync that read the old guard is done +// vk_memory.c: the game is about to read [addr, addr+size) of pool storage; waits for a GPU write into it that the game +// was told is done, unless the range still holds the result of the write before +void vk_mem_shadow_read(uint64_t addr, uint64_t size); void gfx_replay_wait_value(uint64_t v); // vk_backend.c: the replay thread submitted value v uint64_t nvn_fence_last(void); // fence value of the newest submit (0 before the first) int nvn_depth_minus_w(void); // 1 after DeviceSetDepthMode(NEAR_IS_MINUS_W), else 0 diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index 9ae562da..b1f92808 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -475,7 +475,8 @@ NVN_API(nvnQueueFenceSync) { // the frame it presented last; on the Switch that work is long done by then, here it is still in the replay and on the // GPU (about 11 ms from the submit), and the wait sets the frame time. With the switch on, that wait, and only that // one, waits for the frame before it instead. What the game reads back of GPU results after the wait can then be a -// frame old. The game's CPU writes to memory the GPU still reads reach the GPU only through shadow copies, which are +// frame old; a buffer that holds no older result is waited for when the game invalidates it (vk_memory.c +// vk_mem_shadow_read). The game's CPU writes to memory the GPU still reads reach the GPU only through shadow copies, which are // queued for the GPU or wait (nvn_sync_ahead_guard). The wait still lasts until the replay thread submitted the asked // value, so no copy of that value itself picks up those writes. pa_fence: the newest fence at the latest present and // the one before, present thread only for writes. pa_guard: the newest value the game was told is done early. diff --git a/runtime/nvn_ext.c b/runtime/nvn_ext.c index 2aa78324..c1b5b6e5 100644 --- a/runtime/nvn_ext.c +++ b/runtime/nvn_ext.c @@ -17,6 +17,7 @@ void nvn_pool_invalidate(const pool_t *p, uint64_t off, uint64_t size) { if (!p->mem || nvn_gpu_only((uintptr_t)p->mem) || off >= p->size || !size) return; if (size > p->size - off) size = p->size - off; + vk_mem_shadow_read((uintptr_t)p->mem + off, size); gfx_backend()->guest_read((uintptr_t)p->mem + off, size); } diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 3a5ad963..247b3e1c 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -1303,6 +1303,50 @@ void vk_mem_shadow_fence(void) { plat_mutex_unlock(&shadow_lock); } +// TOTK_SYNC_AHEAD, nvnMemoryPoolInvalidateMappedRange: the game is about to read [addr, addr+size) of what the GPU +// writes into pool storage. While the guard is not done, a write into the range that is still pending can belong to a +// value the game was told is done, and its bytes come back only with that value's fence (vk_gpu_written). A range the +// game reads again and again (its depth queries and visibility maps: one buffer written every frame or every few) +// then still holds the result of the write before, and the game gets that one, a sample late: what the switch trades +// for the frame in flight. A range it has not read lately holds no such result. TotK reads single pixels back +// (main+0x407ed8 on 1.4.3) through a 64-byte buffer per request, taken in turn from rings of 64 to 256 and read once, +// right after the frame wait: read early, the pixel is 0 0 0 0 in a buffer never used, and the answer to another +// request, about 27 s old, once the ring came round. In the Depths the zeros put Link in gloom where there is none. So +// a range with a pending write that is not known waits for the guard here. Known: the game invalidated this very +// range within the last SREAD_AGE fence values (about 30 frames), when it held a result or was waited for. +#define SREAD_N 64 +#define SREAD_AGE 64 +typedef struct { uint64_t addr, size, at; } SRead; +static SRead sread[SREAD_N]; // shadow_lock: the ranges the game invalidated last, with the value issued then +static uint64_t s_read_waits, s_read_ticks; // shadow_lock: since the last log + +void vk_mem_shadow_read(uint64_t addr, uint64_t size) { + if (!shadow_on || !size || !nvn_sync_ahead_guard()) return; + plat_mutex_lock(&shadow_lock); + uint64_t now = vk_sync_issued(); + SRead *e = &sread[0]; // the range's entry, else the oldest + for (int i = 0; i < SREAD_N; i++) { + if (sread[i].addr == addr && sread[i].size == size) { e = &sread[i]; break; } + if (sread[i].at < e->at) e = &sread[i]; + } + int known = e->addr == addr && e->size == size && e->at + SREAD_AGE >= now, pending = 0; + for (int i = 0; i < nspend && !pending; i++) pending = spend[i].lo < addr + size && spend[i].hi > addr; + uint64_t g = nvn_sync_ahead_guard(), wait = pending && !known && g > vk_sync_fence_done() ? g : 0; + // A pending write of a value the game was not told is done, in a range not known: the game reads ahead of its own + // wait and the range stays unknown. + if (known || !pending || wait) *e = (SRead){addr, size, now}; + plat_mutex_unlock(&shadow_lock); + if (!wait) return; + uint64_t t0 = qpc(); + { + NVN_BLOCKING(); + vk_sync_fence_wait(wait, UINT64_MAX); + } + plat_mutex_lock(&shadow_lock); + s_read_waits++, s_read_ticks += qpc() - t0; + plat_mutex_unlock(&shadow_lock); +} + int vk_mem_shadow_on(void) { return shadow_on; } void vk_mem_shadow_sync(uint64_t fence) { @@ -1408,10 +1452,11 @@ static void shadow_log(uint64_t frame) { qpc_ms(s_ticks), nspend); if (defer_on || s_waits) // only with TOTK_SYNC_AHEAD: the shadow log of everyone else stays as it was rt_log("mem: shadow deferred (%s): %llu pools, %.1f MiB in %llu copies; replay waited for a reader %llu times, " - "%.2f ms", defer_on ? "on" : "off", (unsigned long long)s_defer_pools, s_defer_bytes / 1048576.0, - (unsigned long long)s_defer_copies, (unsigned long long)s_waits, qpc_ms(s_wait_ticks)); + "%.2f ms; the game waited for a GPU write it reads %llu times, %.2f ms", defer_on ? "on" : "off", + (unsigned long long)s_defer_pools, s_defer_bytes / 1048576.0, (unsigned long long)s_defer_copies, + (unsigned long long)s_waits, qpc_ms(s_wait_ticks), (unsigned long long)s_read_waits, qpc_ms(s_read_ticks)); s_syncs = s_queries = s_copied = s_untracked = s_wb_bytes = s_ticks = 0; - s_defer_pools = s_defer_bytes = s_defer_copies = s_wait_ticks = s_waits = 0; + s_defer_pools = s_defer_bytes = s_defer_copies = s_wait_ticks = s_waits = s_read_waits = s_read_ticks = 0; plat_mutex_unlock(&shadow_lock); } diff --git a/totk.ini.example b/totk.ini.example index ee6c07ee..bf14ffc0 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -59,7 +59,7 @@ # Let the game build the next frame while the GPU still draws the previous one, 0 or 1. Default: off. Each frame the # game waits once for the GPU to finish the frame it presented last; with 1 that wait covers the frame before it # instead. On Great Sky Island (Linux, RX 9070 XT) with shadow_defer = 1 this took about 73 to 100 fps. Costs one more -# frame of input delay, and frame times vary more from frame to frame than with 0. GPU results the game reads back after that wait, for example occlusion queries, can be a frame old. +# frame of input delay, and frame times vary more from frame to frame than with 0. GPU results the game reads back every frame after that wait, for example its depth queries, can be a frame old; a result it reads only once is waited for. # Works on 1.1.0, 1.2.1 and 1.4.3. Needs shadow storage (shadow below), which is on by default on Linux and # Wine and needs shadow = 1 on native Windows; without it the setting is ignored and the log says so. # sync_ahead = 0 From ae5fff3d467569c752ab270bbf1079b41c61e91d Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Tue, 17 Nov 2026 04:07:35 +0300 Subject: [PATCH 03/20] With sync_ahead the game takes its depth query results when their frame number is one frame older than it expects, so in shrines the reflections of the lamps on the floor no longer go out on the frames where the GPU runs behind, which flickered at some camera angles --- docs/agents/symptoms.md | 1 + recomp/patches-v143.txt | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 4a242b12..2938b6f9 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -40,6 +40,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | a 30 Hz thread runs at 27 Hz, slow cutscenes | host sleep overshoot: Windows waitable timers wake 0.3-0.5 ms late | `TOTK_TRACE=sched` sleep asked versus took | | 20 fps after a scheduler change | a yield back-off that starts too early | yield handling against the console's | | in the Depths Link takes gloom damage and wears the gloom haze while standing on clean ground, some runs only; only with `sync_ahead = 1` | the game read a GPU result of the frame it was told is done before the GPU wrote it: its one-pixel readbacks go through a buffer per request, which then holds zeros or an answer from the ring's last round (fixed: `vk_memory.c` `vk_mem_shadow_read` waits at the invalidate) | `sync_ahead = 0`; the shadow log line's "the game waited for a GPU write it reads" count | +| in shrines the lamps' reflections on the floor go out for single frames, every other frame at some camera angles; only with `sync_ahead = 1`, more with DLAA (the GPU runs further behind) | the game reads its depth query results two frames later and refuses them unless their frame number is exactly the counter - 2; with sync_ahead it is the counter - 3 on frames where the GPU is behind (fixed: patch set `qstamp` in `recomp/patches-v143.txt` also takes the counter - 3) | `sync_ahead = 0`; a `--capture` run with `TOTK_SYNC_AHEAD` on, counting one-frame drops in floor regions | | climbing or movement wrong only above some fps | game code that assumes a fixed step count per frame | compare at 30, 60 and the failing rate | ## Threads and crashes diff --git a/recomp/patches-v143.txt b/recomp/patches-v143.txt index e9313391..8ecb5d7b 100644 --- a/recomp/patches-v143.txt +++ b/recomp/patches-v143.txt @@ -1185,6 +1185,21 @@ set report TOTK_REPORT_GUARD 1 site report main a22d8 2a010571 2a010571 37f80341 # orr w17, w11, w1, lsl #1; tbnz w1, #31, 0xa2340 site report main a2310 531f7821 530f7c24 531f7821 35000184 # lsr w4, w1, #15; lsl w1, w1, #1; cbnz w4, 0xa2340 +# Depth query results one frame older (TOTK_SYNC_AHEAD). A compute pass at the start of each frame samples the scene +# depth for the world queries and writes their results, with the frame number of the request, into a 23232-byte +# CPU-cached buffer that main+0x6e1220 invalidates and reads two frames later. It takes the results as this frame's +# (bit 2 of obj+0x10) only when the number at buffer +0x58e4 is exactly the frame counter (obj+0x14) - 2. With +# sync_ahead the game's frame wait covers the frame before, so on frames where the GPU runs behind the number read is +# the counter - 3 (the frame after it has not come back yet) and the results were refused: in shrines the reflections +# of the lamps on the floor went out on those frames, every other frame where the GPU was slow, which looked like +# flicker at some camera angles. The set also accepts the counter - 3: d = counter - 2 - number, unsigned d <= 1. +# Waiting for that frame when the buffer is invalidated (vk_memory.c vk_mem_shadow_read) also ends the flicker, but +# gives back the frame rate sync_ahead brings (63 against 86 fps on a field macro at 4K). +# w11 is loaded again on both paths at 0x6e1340 before any read; x8, the number's address, stays for 0x6e1420. +set qstamp TOTK_SYNC_AHEAD 0 +site qstamp main 6e1320 6b0b015f 4b0b014b 7100057f # cmp w10, w11 -> sub w11, w10, w11; cmp w11, #1 +site qstamp main 6e1324 540000c1 540000c8 # b.ne 0x6e133c -> b.hi 0x6e133c + # Function-entry hooks (FN_ENTER, runtime/cpu.h): only these functions check g_fnprof on entry, the others carry no # hook. main+0x585bd0 sets the render size (runtime/resolution.c SET_RENDER_SIZE): res_fn_hit takes the renderer from # the game's own call at start, so a DLSS mode can change while the game runs. From efb896235a3e103ea1c331b0c40ca6df3b4ef896 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 00:58:32 +0300 Subject: [PATCH 04/20] DLSS without auto exposure is a setting, dlss_fixed_exposure, next to the other DLSS settings, off by default: 0 creates DLSS with AutoExposure as before, 1 without it, and a change recreates the features at once --- docs/agents/symptoms.md | 2 +- runtime/cfg.c | 6 +++--- runtime/ui/menu.cpp | 1 + runtime/ui_text.c | 19 +++++++++++++++++++ runtime/ui_text.h | 1 + runtime/vk_dlaa.c | 27 ++++++++++++++++----------- totk.ini.example | 5 +++++ 7 files changed, 46 insertions(+), 15 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 5a96e8a8..4657dbc8 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -26,7 +26,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | no sun shadows, wrong lighting | unwritten vertex outputs read as zero instead of (0,0,0,1) | the shader's output map | | 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 | -| with DLAA/DLSS on, large distant blocks behind the fog (the Depths) turn darker or lighter for a frame while the camera moves | DLSS created with AutoExposure estimated the exposure of the dark scene itself, and the estimate moved from frame to frame (fixed: `vk_dlaa.c` evaluate creates the feature without it) | a flat fogged patch in `capture.py` frames: one value without DLAA, jumps with it | +| with DLAA/DLSS on, large distant blocks behind the fog (the Depths) turn darker or lighter for a frame while the camera moves | DLSS created with AutoExposure estimated the exposure of the dark scene itself, and the estimate moved from frame to frame (fixed with `dlss_fixed_exposure = 1`: `vk_dlaa.c` evaluate creates the feature without it) | a flat fogged patch in `capture.py` frames: one value without DLAA, jumps with it | | 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/runtime/cfg.c b/runtime/cfg.c index 7784a7e7..f6bb9c1d 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_EFFECTS_AFTER", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_FIXED_EXPOSURE", "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", @@ -84,7 +84,7 @@ static const struct { const char *key, *section; } sections[] = { {"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"}, {"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"}, {"HDR_UI_NITS", "display"}, {"HDR_HUE", "display"}, {"SHADOW_RES", "display"}, - {"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_CHAR_MASK", "display"}, {"DLAA_EFFECTS_AFTER", "display"}, {"REFLEX", "display"}, {"FXAA", "display"}, + {"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_CHAR_MASK", "display"}, {"DLSS_FIXED_EXPOSURE", "display"}, {"DLAA_EFFECTS_AFTER", "display"}, {"REFLEX", "display"}, {"FXAA", "display"}, {"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"}, {"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"}, {"SHADOW_SCALE", "display"}, {"SHADOW_RANGE", "display"}, {"GRASS_RANGE", "display"}, {"CONTACT_SHADOWS", "display"}, @@ -141,7 +141,7 @@ static const CfgMeta metas[] = { {"DYNAMIC_RES", "1", "0|1", LIVE}, // resolution.c res_dynres_live {"UPSCALER", "fsr", "fsr|bilinear", LIVE}, {"SHARPNESS", "80", "0..100", LIVE}, {"DLAA", "0", "0|1", RESTART}, {"DLSS", "dlaa", "dlaa|quality|balanced|performance|ultra_performance", LIVE}, - {"DLSS_PRESET", "default", "default|k|j", LIVE}, {"DLAA_EFFECTS_AFTER", "0", "0|1", LIVE}, + {"DLSS_PRESET", "default", "default|k|j", LIVE}, {"DLSS_FIXED_EXPOSURE", "0", "0|1", LIVE}, {"DLAA_EFFECTS_AFTER", "0", "0|1", LIVE}, {"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"REFLEX", "off", "off|on|boost", RESTART}, {"FXAA", "1", "0|1", LIVE}, {"LOD_STEADY", "0", "0|1", RESTART}, // sets steady {"ASPECT", "fit", "fit|stretch", LIVE}, {"MOVIE_ASPECT", "keep", "keep|stretch", LIVE}, {"FOV", "100", "70..140", LIVE}, diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 816d576a..656f5550 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -750,6 +750,7 @@ static void tab_screen(float scale) { static const UiTextId chars[] = {UI_MENU_DLSS_CHARS_OFF, UI_MENU_DLSS_CHARS_LIGHT, UI_MENU_DLSS_CHARS_MEDIUM, UI_MENU_DLSS_CHARS_FULL}; opt_choice("TOTK_DLSS_CHAR_MASK", UI_MENU_DLSS_CHARS, chars, 4, UI_MENU_DLSS_CHARS_DESC); + opt_toggle("TOTK_DLSS_FIXED_EXPOSURE", UI_MENU_DLSS_FIXED_EXPOSURE, UI_MENU_DLSS_FIXED_EXPOSURE_DESC); opt_toggle("TOTK_DLAA_EFFECTS_AFTER", UI_MENU_DLAA_EFFECTS_AFTER, UI_MENU_DLAA_EFFECTS_AFTER_DESC); static const UiTextId reflex[] = {UI_MENU_REFLEX_OFF, UI_MENU_REFLEX_ON, UI_MENU_REFLEX_BOOST}; opt_choice("TOTK_REFLEX", UI_MENU_REFLEX, reflex, 3, UI_MENU_REFLEX_DESC); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 61d45cf7..769272ca 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -518,6 +518,11 @@ static const char *const text_en[UI_TEXT_COUNT] = { "fog (shrines) no longer vanish or leave trails. Off: DLSS also processes " "these effects, as before. DLAA mode only; the upscaling DLSS modes always " "process them. Applies at once.", + [UI_MENU_DLSS_FIXED_EXPOSURE] = "Fixed DLSS exposure", + [UI_MENU_DLSS_FIXED_EXPOSURE_DESC] = "DLSS takes the brightness of the game's picture as it is instead of " + "estimating the exposure from each frame: in dark scenes (the Depths) large " + "fogged areas no longer turn darker or lighter for a frame while the camera " + "moves. Off: DLSS estimates the exposure itself, as before. Applies at once.", [UI_MENU_BIND_SKIP] = "Skip repeated binds", [UI_MENU_BIND_SKIP_DESC] = "Optimisation: skips bindings that did not change.", [UI_MENU_SHADER_MEMO] = "Shader memo", [UI_MENU_SHADER_MEMO_DESC] = "Optimisation: remembers shader lookups.", @@ -1120,6 +1125,11 @@ static const char *const text_ru[UI_TEXT_COUNT] = { "больше не пропадают и не оставляют шлейфов. Выкл.: DLSS обрабатывает и эти " "эффекты, как раньше. Только в режиме DLAA; режимы DLSS с повышением " "разрешения обрабатывают их всегда. Применяется сразу.", + [UI_MENU_DLSS_FIXED_EXPOSURE] = "Фиксированная экспозиция DLSS", + [UI_MENU_DLSS_FIXED_EXPOSURE_DESC] = "DLSS берёт яркость картинки игры как есть, а не оценивает экспозицию по " + "каждому кадру: в тёмных сценах (Глубины) большие участки в тумане больше не " + "темнеют и не светлеют на кадр при движении камеры. Выкл.: DLSS сам " + "оценивает экспозицию, как раньше. Применяется сразу.", [UI_MENU_BIND_SKIP] = "Пропуск повторных привязок", [UI_MENU_BIND_SKIP_DESC] = "Оптимизация: пропускает неизменившиеся привязки.", [UI_MENU_SHADER_MEMO] = "Память шейдеров", [UI_MENU_SHADER_MEMO_DESC] = "Оптимизация: запоминает поиск шейдеров.", @@ -1560,6 +1570,12 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { "pequenas na névoa (santuários) não somem mais nem deixam rastros. Desligado:" " o DLSS também processa esses efeitos, como antes. Só no modo DLAA; os modos" " de DLSS que aumentam a resolução sempre os processam. Aplica-se na hora.", + [UI_MENU_DLSS_FIXED_EXPOSURE] = "Exposição fixa do DLSS", + [UI_MENU_DLSS_FIXED_EXPOSURE_DESC] = "O DLSS usa o brilho da imagem do jogo como ele é, em vez de estimar a " + "exposição a cada quadro: em cenas escuras (as Profundezas), grandes áreas com " + "névoa não ficam mais escuras ou claras por um quadro enquanto a câmera se " + "move. Desligado: o DLSS estima a exposição sozinho, como antes. Aplica-se na " + "hora.", [UI_MENU_BIND_SKIP] = "Pular bindings repetidos", [UI_MENU_BIND_SKIP_DESC] = "Otimização: pula bindings que não mudaram.", [UI_MENU_SHADER_MEMO] = "Memória de shaders", [UI_MENU_SHADER_MEMO_DESC] = "Otimização: lembra as buscas de shaders.", @@ -2312,6 +2328,9 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_DLAA_EFFECTS_AFTER_DESC] = "开启 DLAA 时,雾、灰尘和其他粒子绘制在 DLSS 已平滑的画面之上,与游戏不用 DLSS 时的" "绘制方式相同:雾中的细小粒子(神庙中)不再消失,也不再留下拖影。关闭:DLSS 也处理这些效果,与以" "前相同。仅用于 DLAA 模式;提升分辨率的 DLSS 模式始终处理它们。立即生效。", + [UI_MENU_DLSS_FIXED_EXPOSURE] = "固定 DLSS 曝光", + [UI_MENU_DLSS_FIXED_EXPOSURE_DESC] = "DLSS 直接使用游戏画面的亮度,而不是逐帧自行估算曝光:在暗场景(地底)中," + "镜头移动时大片雾中区域不再忽暗忽亮一帧。关闭:DLSS 自行估算曝光,与以前相同。立即生效。", [UI_MENU_BIND_SKIP] = "跳过重复绑定", [UI_MENU_BIND_SKIP_DESC] = "优化:跳过没有变化的绑定。", [UI_MENU_SHADER_MEMO] = "着色器查找缓存", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 6b2b4c40..30a43eb0 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -245,6 +245,7 @@ typedef enum { UI_MENU_LOD_GUARD, UI_MENU_LOD_GUARD_DESC, UI_MENU_FIX_C0, UI_MENU_FIX_C0_DESC, UI_MENU_FIX_ZERO, UI_MENU_FIX_ZERO_DESC, UI_MENU_DLAA_EFFECTS_AFTER, UI_MENU_DLAA_EFFECTS_AFTER_DESC, + UI_MENU_DLSS_FIXED_EXPOSURE, UI_MENU_DLSS_FIXED_EXPOSURE_DESC, UI_MENU_BIND_SKIP, UI_MENU_BIND_SKIP_DESC, UI_MENU_SHADER_MEMO, UI_MENU_SHADER_MEMO_DESC, UI_MENU_PIPE_MEMO, UI_MENU_PIPE_MEMO_DESC, diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 160c0322..90bdb1ae 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -296,6 +296,7 @@ static struct { uint64_t why[3]; // history resets: scene size changed, no camera, scene target changed int on; // the dlaa setting, read at each present (the menu switches it live) int preset, preset_dirty; // the DLSS model features are created with (preset_value); a change pending + int fixed_exposure; // dlss_fixed_exposure the features are created with (preset_check) VkExecTarget ids; // this frame's G-buffer target 0 (object classes in green), valid with ids_ok int ids_ok; unsigned cfg_seen; // cfg_version when preset was read @@ -1007,12 +1008,13 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) { p_setu(p, "OutWidth", ow); p_setu(p, "OutHeight", oh); p_seti(p, "PerfQualityValue", perf_quality(w, ow)); - // IsHDR | MVLowRes: linear scene light, motion vectors at the render size, no exposure texture. Not AutoExposure - // (1 << 6): DLSS then estimated the exposure from each frame itself, and in the Depths, where the whole scene is - // dark, the estimate moved with the camera; distant blocks under uniform fog came out up to 0.7 levels (of 255) - // darker or brighter for a frame, where the game's own picture held one value (a 64x64 patch at 30.79 without - // DLAA read 30.09-30.79 frame to frame with AutoExposure and 30.4-30.7, steady, without it). - p_seti(p, "DLSS.Feature.Create.Flags", (1 << 0) | (1 << 1)); + // IsHDR | MVLowRes: linear scene light, motion vectors at the render size, no exposure texture; AutoExposure + // (1 << 6) unless dlss_fixed_exposure. With AutoExposure DLSS estimates the exposure from each frame itself, and + // in the Depths, where the whole scene is dark, the estimate moved with the camera; distant blocks under uniform + // fog came out up to 0.7 levels (of 255) darker or brighter for a frame, where the game's own picture held one + // value (a 64x64 patch at 30.79 without DLAA read 30.09-30.79 frame to frame with AutoExposure and 30.4-30.7, + // steady, without it). Without it DLSS takes the exposure as 1. + p_seti(p, "DLSS.Feature.Create.Flags", (1 << 0) | (1 << 1) | (D.fixed_exposure ? 0 : 1 << 6)); p_seti(p, "DLSS.Enable.Output.Subrects", 0); // The model per mode (NVSDK_NGX_DLSS_Hint_Render_Preset: 1 A ... 11 K, 12 L, 13 M), as NVIDIA recommends for // DLSS 4.5 (310.x): K (transformer) for DLAA, Quality and Balanced, M for Performance, L for Ultra Performance. @@ -1333,18 +1335,21 @@ static void dlss_mode(void) { } -// The preset, read again when a setting changed (the menu); a different one recreates the features at the next DLAA run. +// The preset and dlss_fixed_exposure, read again when a setting changed (the menu); a different one recreates the +// features at the next DLAA run. static void preset_check(void) { unsigned v = cfg_version(); if (v == D.cfg_seen) return; D.cfg_seen = v; - int p = preset_value(); - if (p == D.preset) return; - if (D.evals) { + int p = preset_value(), x = cfg_bool("TOTK_DLSS_FIXED_EXPOSURE"); + if (p == D.preset && x == D.fixed_exposure) return; + if (D.evals && p != D.preset) { if (p) rt_log("dlaa: DLSS preset %c in every mode; the features are created again", 'A' + p - 1); else rt_log("dlaa: DLSS presets per mode; the features are created again"); } - D.preset = p, D.preset_dirty = 1, D.reset = 1; + if (D.evals && x != D.fixed_exposure) + rt_log("dlaa: DLSS %s; the features are created again", x ? "exposure fixed at 1" : "auto exposure"); + D.preset = p, D.fixed_exposure = x, D.preset_dirty = 1, D.reset = 1; } void vk_dlaa_frame(void) { diff --git a/totk.ini.example b/totk.ini.example index ee6c07ee..42bb0b00 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -304,6 +304,11 @@ # frame before, DLSS is told to favour the current frame by this percentage (0, 35, 65 or 100; 0 off). Higher removes # more of the trails and leaves those edges a little less smooth. Changes while the game runs take effect at once. # dlss_char_mask = 65 +# 1: DLSS takes the brightness of the game's picture as it is (exposure 1) instead of estimating the exposure from each +# frame. In dark scenes (the Depths) the estimate moves with the camera, and large fogged areas turn darker or lighter +# for a frame. 0, the default: DLSS estimates the exposure itself, as before. Every DLSS mode. Changes while the game +# runs take effect at once. +# dlss_fixed_exposure = 0 # With DLAA (dlaa = 1, dlss = dlaa), fog, dust and other particles are drawn over the picture DLSS has already smoothed, # as the game draws them without DLSS, so small particles in fog (shrines) no longer vanish or leave trails. 0: DLSS # processes these effects too, as before (the default until the inventory preview, the scope and water edges are From 0f2cc1071a017cd1f5001503422798feb320d546 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 02:03:53 +0300 Subject: [PATCH 05/20] A mirror's copies from the shadow wait for the shadow sync: the prologue seals the mirror queues before it syncs the shadow and copies only those, and a mirror sync touches its shadow pool, so with replay_workers and shadow_defer a recording thread no longer queues a copy that reads the shadow before the CPU's write reached it, which left terrain in the Depths as stretched slabs over the ground for the rest of the session --- runtime/vk.h | 1 + runtime/vk_memory.c | 53 ++++++++++++++++++++++++++++++--------------- runtime/vk_sync.c | 4 +++- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 39cf6ccf..c3c1e8cb 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -291,6 +291,7 @@ int vk_mvprev_find(int index, uint64_t addr, uint64_t size, uint64_t *prev, int void vk_mvprev_frame(void); // each present, beside vk_dlaa_frame void vk_mem_mirror_begin(void); int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence); +void vk_mem_mirror_seal(void); // before vk_mem_shadow_sync: the copies the next vk_mem_mirror_prologue takes int vk_mem_mirror_pending(void); void vk_mem_mirror_prologue(VkCommandBuffer cb); // Shadow storage (vk_memory.c): vk_sync.c calls vk_mem_shadow_sync right before each submit; the replay notes every GPU diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 3a5ad963..8704dc60 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -1576,6 +1576,7 @@ static uint32_t mepoch; // replay: its write-watch snapshot epoch typedef struct { PoolRec *r; uint64_t off, size; } MRange; static MRange *pend, *wbl; // replay: copies for the next prologue; GPU writes into mirrors this submission static int npend, cpend, nwbl, cwbl; +static int pend_seal, snl_seal; // mirror_lock: the copies the next prologue takes (vk_mem_mirror_seal) static VkBufferCopy *mregions; // replay: vkCmdCopyBuffer regions static int cregions; static PlatMutex flush_lock = PLAT_MUTEX_INIT; @@ -1640,8 +1641,9 @@ static void mirror_mode(int discrete) { "slower than mirror (the auto default on a discrete GPU); use memory = auto unless testing"); } -static void range_add(MRange **a, int *n, int *cap, PoolRec *r, uint64_t off, uint64_t size) { - if (*n && (*a)[*n - 1].r == r && (*a)[*n - 1].off + (*a)[*n - 1].size == off) { +// keep: the entries before it are sealed and do not grow. +static void range_add(MRange **a, int *n, int *cap, int keep, PoolRec *r, uint64_t off, uint64_t size) { + if (*n > keep && (*a)[*n - 1].r == r && (*a)[*n - 1].off + (*a)[*n - 1].size == off) { (*a)[*n - 1].size += size; return; } @@ -1762,7 +1764,8 @@ static void mirror_create(PoolRec *r) { mlist[at] = r; nm++; plat_mutex_unlock(&pool_lock); - range_add(&pend, &npend, &cpend, r, 0, n); + range_add(&pend, &npend, &cpend, pend_seal, r, 0, n); + if (r->smap) shadow_touch(r); // the copy reads the shadow: see vk_mem_mirror_seal __atomic_store_n(&r->acc_sub, msub, __ATOMIC_RELEASE); __atomic_store_n(&r->mstate, MS_ON, __ATOMIC_RELEASE); mrep.first_n++, mrep.first_bytes += n; @@ -1826,7 +1829,7 @@ static void mirror_sync(PoolRec *r) { if (!ch) continue; for (uint64_t x = a; x < b; x += 1ull << GEN_PAGE_SHIFT) if (ch[(x >> GEN_PAGE_SHIFT) & (GEN_PER_CHUNK - 1)] > since) - range_add(&pend, &npend, &cpend, r, x - lo, 1ull << GEN_PAGE_SHIFT), mrep.page_bytes += 1ull << GEN_PAGE_SHIFT; + range_add(&pend, &npend, &cpend, pend_seal, r, x - lo, 1ull << GEN_PAGE_SHIFT), mrep.page_bytes += 1ull << GEN_PAGE_SHIFT; } r->mgen = gen_now; if (excl) plat_mutex_unlock(&gen_lock); @@ -1843,7 +1846,7 @@ static void mirror_sync(PoolRec *r) { for (int i = 0; i < n; i++) { uint64_t a = fl[i].lo > lo ? fl[i].lo : lo, b = fl[i].hi < hi ? fl[i].hi : hi; if (a >= b) continue; - range_add(&pend, &npend, &cpend, r, a - lo, b - a); + range_add(&pend, &npend, &cpend, pend_seal, r, a - lo, b - a); mrep.flush_n++, mrep.flush_bytes += b - a; if ((rt_trace & RT_TRACE_PERF) && b - a >= 16u << 20) rt_log("mem: mirror frame %llu: flush range %.1f MiB at +%.1f MiB of a %.1f MiB pool (%d ranges from %llu " @@ -1851,6 +1854,7 @@ static void mirror_sync(PoolRec *r) { (hi - lo) / 1048576.0, n, (unsigned long long)calls, sum / 1048576.0); } } + if (r->smap) shadow_touch(r); // the copies read the shadow: see vk_mem_mirror_seal mrep.syncs++; mrep.sync_t += qpc() - t0; __atomic_store_n(&r->acc_sub, msub, __ATOMIC_RELEASE); @@ -2029,12 +2033,12 @@ static int snap_add(PoolRec *r, uint64_t off, uint64_t size) { // Prologue: the snapshot copies of the ranges added since the last prologue. static void snap_copies(VkCommandBuffer cb) { - for (int i = snl_copied; i < nsnl; i++) { + for (int i = snl_copied; i < snl_seal; i++) { VkBufferCopy cp = {snl[i].off, snl[i].soff, snl[i].size}; vkCmdCopyBuffer(cb, snl[i].r->mbuf, chunks[snl[i].chunk].buf, 1, &cp); mrep.snap_bytes += snl[i].size; } - snl_copied = nsnl; + snl_copied = snl_seal; } static int snap_cmp(const void *x, const void *y) { @@ -2151,7 +2155,7 @@ static void gpu_wrote(const VkMemSpan *sp, uint64_t size, int shader) { plat_mutex_unlock(&mirror_lock); return; } - range_add(&wbl, &nwbl, &cwbl, r, sp->offset, size); + range_add(&wbl, &nwbl, &cwbl, 0, r, sp->offset, size); plat_mutex_unlock(&mirror_lock); } void vk_mem_gpu_wrote(const VkMemSpan *sp, uint64_t size) { gpu_wrote(sp, size, 0); } @@ -2252,7 +2256,7 @@ void vk_mem_mirror_begin(void) { return; } uint64_t t0 = qpc(); - nsnl = snl_copied = nchunks = 0; // the previous submission handed its chunks to snap_free + nsnl = snl_seal = snl_copied = nchunks = 0; // the previous submission handed its chunks to snap_free uint32_t prev = msub; msub = msub + 1 ? msub + 1 : 1; __atomic_add_fetch(&mirror_gen, 1, __ATOMIC_RELEASE); @@ -2284,30 +2288,45 @@ void vk_mem_mirror_begin(void) { mrep.presnap_t += qpc() - t0; } +// With shadow storage a mirror copies from the shadow, which vk_mem_shadow_sync brings up to date only for the pools +// touched before it runs; mirror_sync and mirror_create touch theirs after queuing the copies. In a chunked replay the +// recording threads queue copies while the replay thread runs a part's prologue: a copy queued after that prologue's +// shadow sync read the shadow before the CPU's write reached it (with TOTK_SHADOW_DEFER the write is a GPU copy of +// the next prologue, so always), and the mirror, marked synced, kept the old bytes until the CPU wrote the page again. +// In the Depths, terrain loaded with those pages stood as stretched slabs over the ground for the rest of the session. +// So the prologue seals the queues before the shadow sync and copies only what was sealed; the rest waits for the next +// prologue, whose shadow sync comes first. +void vk_mem_mirror_seal(void) { + plat_mutex_lock(&mirror_lock); + pend_seal = npend, snl_seal = nsnl; + plat_mutex_unlock(&mirror_lock); +} + int vk_mem_mirror_pending(void) { plat_mutex_lock(&mirror_lock); - int n = npend || snl_copied < nsnl; + int n = pend_seal || snl_copied < snl_seal; plat_mutex_unlock(&mirror_lock); return n; } void vk_mem_mirror_prologue(VkCommandBuffer cb) { plat_mutex_lock(&mirror_lock); - if (!npend && snl_copied == nsnl) { + if (!pend_seal && snl_copied == snl_seal) { plat_mutex_unlock(&mirror_lock); return; } mbarrier(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT); - copy_ranges(cb, pend, npend, 1, &mrep.upload_bytes, &mrep.upload_copies); - if (snl_copied < nsnl) { // the snapshots read what the uploads above wrote - if (npend) mbarrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, + copy_ranges(cb, pend, pend_seal, 1, &mrep.upload_bytes, &mrep.upload_copies); + if (snl_copied < snl_seal) { // the snapshots read what the uploads above wrote + if (pend_seal) mbarrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT); snap_copies(cb); } mbarrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT); - npend = 0; + memmove(pend, pend + pend_seal, sizeof *pend * (size_t)(npend - pend_seal)); + npend -= pend_seal, pend_seal = 0; mrep.prologues++; plat_mutex_unlock(&mirror_lock); } @@ -2410,7 +2429,7 @@ int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence) { VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_READ_BIT); int m = wb_merge_record(cb); if (!m) { // no range list: copy them plainly - for (int i = 0; i < nsnl; i++) range_add(&wbl, &nwbl, &cwbl, snl[i].r, snl[i].off, snl[i].size); + for (int i = 0; i < nsnl; i++) range_add(&wbl, &nwbl, &cwbl, 0, snl[i].r, snl[i].off, snl[i].size); } rec += m + 1; mbarrier(cb, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT, @@ -3114,7 +3133,7 @@ void vk_mem_shutdown(void) { plat_mutex_lock(&pool_lock); for (int i = 0; i < pool_index.n; i++) destroy_rec(c, pool_index.e[i].buf); pool_index.n = 0; - nm = npend = nwbl = nsnl = snl_copied = ndq = 0; // mirrors went with their pools + nm = npend = pend_seal = nwbl = nsnl = snl_seal = snl_copied = ndq = 0; // mirrors went with their pools plat_mutex_lock(&snap_pool_lock); for (int i = 0; i < nsnap_pool; i++) vmaDestroyBuffer(c->vma, snap_pool[i].buf, snap_pool[i].alloc); nsnap_pool = 0, snap_pool_bytes = 0; diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index ce168fe4..f48e237f 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -369,7 +369,9 @@ static CmdSlot *take_slot(VkCtx *c, uint64_t fence) { // (vk_sync_part, which the caller made the recorder idle for). static VkCommandBuffer prologue(VkCtx *c, uint64_t fence, int direct) { // Shadow storage: the CPU's writes go in before the part is submitted (TOTK_SYNC_AHEAD: queued for the GPU or - // made after the GPU finished the readers the game may think done, vk_memory.c). + // made after the GPU finished the readers the game may think done, vk_memory.c). The mirror copies queued before it + // are the ones this prologue makes, as they read the shadow (vk_mem_mirror_seal). + vk_mem_mirror_seal(); vk_mem_shadow_sync(fence); if (!vk_mem_mirror_pending() && !vk_mem_shadow_deferred()) return VK_NULL_HANDLE; int i = claim_slot(c, fence); // before reading slots: the claim may move it From 07f9760971153ad0a2ef8b44a245776e9f761dfc Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 02:23:21 +0300 Subject: [PATCH 06/20] Symptoms: stretched slabs over the ground in the Depths from a VRAM mirror copied from the shadow too early --- docs/agents/symptoms.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 5c9fb005..4de79ee6 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -18,6 +18,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | dark or magenta 16x16 blocks, missing characters | a pass samples its own render targets (G-buffer feedback) | feedback-loop layout and barriers on that pass | | tile rectangles, wrong data in tiles | storage buffers bound in the wrong slot order | `passes.txt`: storage buffers with their c0 slots | | streaks on cliffs, characters stretched into streaks | primitive restart ignored; integer vertex inputs against a float format | validation run; the draw's topology and vertex formats in the dump | +| huge stretched slabs over the ground, a ceiling over Link, the ground up to his chest, from the load on for the whole session (the Depths); some runs only, most at a high frame rate; with `sync_ahead`, `replay_workers` and a discrete GPU | a VRAM mirror copied from the shadow before the CPU's write reached it, and kept the old vertices while marked synced (fixed: `vk_memory.c` `vk_mem_mirror_seal`) | `TOTK_MEMORY=verify` mismatch lines; `memory = import` or `replay_workers = 0` makes it go; `fps_limit = 144` shows it in most runs | | garbage geometry on title or load screens | 8-bit indices widened on the CPU before a copy in the same submission wrote them | whether a copy to the index buffer precedes the draw in one submission | | black tiles, masks not cleared | clears with a partial channel or stencil mask skipped or done in full | `passes.txt` clears and their masks | | garbage in feedback effects at first use | new GPU-only images start undefined; the console starts them at zero | clear on creation and compare | From cc84958cee68965bd47895115cfb9e77d688086e Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 05:41:05 +0300 Subject: [PATCH 07/20] Give TotK's water volume mask draws a depth bias toward the viewer, so a puddle's surface, on which the volume's top face lies, counts as inside the volume in every frame instead of by rounding and the camera's sub-millimetre motion, which turned the puddle's reflection on and off in patches from frame to frame, more with DLAA --- docs/agents/symptoms.md | 1 + runtime/vk.h | 6 ++++++ runtime/vk_exec.h | 1 + runtime/vk_exec_draw.c | 10 +++++++--- runtime/vk_pipeline.c | 27 +++++++++++++++++++++++++-- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 5c9fb005..d17d6c75 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -26,6 +26,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | no sun shadows, wrong lighting | unwritten vertex outputs read as zero instead of (0,0,0,1) | the shader's output map | | 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 and without the fix; in a frame dump, the quarter-size mask after the volume draw changes from frame to frame | | 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/runtime/vk.h b/runtime/vk.h index 39cf6ccf..0b105c78 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -678,6 +678,12 @@ int vk_pipeline_vertex_input_dynamic(void); // 1: vertex input is VK_EXT_vert void vk_pipeline_answers(const VkPipeState *s, GfxShaderAnswers *vs, GfxShaderAnswers *fs); int vk_pipeline_tex_type(int nvn_target); // NVN_TEXTURE_TARGET_* -> GFX_TEX_* (GfxShaderAnswers.tex_type) uint32_t vk_pipeline_target_wmask(const VkPipeState *s, int i); // colour write mask of target i the pipeline uses +// The sign of the depth bias a volume mask draw gets (vk_pipeline.c, see there), -1 toward smaller depth, 1 toward +// larger, 0 for any other draw. vk_pipeline_set_dynamic enables the bias for it; vk_exec_draw.c record_fixed sets the +// values, VOLUME_BIAS_UNITS and VOLUME_BIAS_SLOPE times the sign. +int vk_pipeline_volume_bias(const VkPipeState *s); +#define VOLUME_BIAS_UNITS 16.0f +#define VOLUME_BIAS_SLOPE 1.0f VkPrimitiveTopology vk_pipeline_topology(int nvn_prim); // quads -> triangle list, line loop -> strip: the replay // rewrites those draws; polygon -> fan VkFormat vk_vertex_format(int nvn_format, int *type); // attribute format (supported fallback), GFX_TYPE_* in *type diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index ff3e5907..9e475ccb 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -119,6 +119,7 @@ typedef struct VkExec { uint32_t st_vmask[2], st_wmask[2], st_ref[2]; // OP_STENCIL per face (0 front, 1 back): compare mask, write mask, ref float point_size; // OP_BIND_STATE kind 6: NVN point size for the support buffer; 0 = never set = 1 p_poly_offset poly_offset; // OP_POLY_OFFSET: depth bias factor, units, clamp + int8_t volume_bias; // the sign of the volume bias recorded in place of poly_offset (record_fixed) p_prim_restart prim_restart; // OP_PRIM_RESTART: applied to indexed strip and fan draws (op_draw_idx) float blend_color[4]; // OP_BLEND_COLOR int64_t copy_stride; // OP_COPY_STRIDE: row stride of buffer <-> texture copies (vk_exec_xfer.c) diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 42513cc2..95c74f32 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2784,8 +2784,11 @@ static VkViewport draw_viewport(VkExec *ex) { return v; } -static void record_fixed(VkExec *ex) { +// s: the draw's state. A volume mask draw records its own depth bias (vk_pipeline_volume_bias) in place of the game's. +static void record_fixed(VkExec *ex, const VkPipeState *s) { VkCommandBuffer cb = ex->cb; + int vb = vk_pipeline_volume_bias(s); + if (vb != ex->volume_bias) ex->dirty |= VK_EXEC_DIRTY_DEPTH_BIAS, ex->volume_bias = (int8_t)vb; if (ex->dirty & VK_EXEC_DIRTY_VIEWPORT) { VkViewport v = draw_viewport(ex); vkCmdSetViewportWithCount(cb, 1, &v); @@ -2797,7 +2800,8 @@ static void record_fixed(VkExec *ex) { ex->recorded++; } if (ex->dirty & VK_EXEC_DIRTY_DEPTH_BIAS) { - vkCmdSetDepthBias(cb, ex->poly_offset.units, ex->poly_offset.clamp, ex->poly_offset.factor); + if (vb) vkCmdSetDepthBias(cb, VOLUME_BIAS_UNITS * (float)vb, 0.0f, VOLUME_BIAS_SLOPE * (float)vb); + else vkCmdSetDepthBias(cb, ex->poly_offset.units, ex->poly_offset.clamp, ex->poly_offset.factor); ex->recorded++; } if (ex->dirty & VK_EXEC_DIRTY_BLEND_CONST) { @@ -3431,7 +3435,7 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { vk_mvsurvey_draw(ex); vk_mvprev_draw(ex); if (mo) plat_mutex_unlock(&mo_lock); - record_fixed(ex); + record_fixed(ex, sp); repeat_stats(ex, info); if (bim) bi_gt(ex, bgt), bi_gb(ex, bgb); if (bim == 2) bi_census(ex, info, sp, bgt, bgb, bsg, sm_hit); // on counts nothing: it is what gets timed diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 08e4c066..7b366d69 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3234,7 +3234,7 @@ uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { compare_op(d->sf_b)); // NVN_POLYGON_OFFSET_ENABLE_* bit of the polygon mode in use: point 1, line 2, fill 4 unsigned mode = pl->mode <= NVN_POLYGON_MODE_FILL ? pl->mode : NVN_POLYGON_MODE_FILL; - vkCmdSetDepthBiasEnable(cb, (pl->offset_en >> mode & 1) != 0); + vkCmdSetDepthBiasEnable(cb, (pl->offset_en >> mode & 1) || vk_pipeline_volume_bias(s)); vkCmdSetRasterizerDiscardEnable(cb, s->raster_discard != 0); vkCmdSetPrimitiveRestartEnable(cb, s->prim_restart != 0); // The vertex bindings the draw reads: those of its vertex input, set here or made into the pipeline from the same @@ -3268,6 +3268,29 @@ uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { return streams; } +// TotK marks the pixels inside its water volumes (seen at puddles) in a quarter-size mask: it draws a volume's front +// faces with a LEQUAL depth test and no depth write, blending ONE/ONE with reverse subtract into one channel, against a +// depth it rebuilt at that size from its linear depth. The volume's top face lies on the water surface, so the test +// compares that surface with itself through two roundings and two sampling positions: on a puddle the face came out +// up to 7 ulp in front of or behind the rebuilt depth, and up to about 0.6 of a pixel of its depth slope (64 ulp) behind +// it. The camera's sub-millimetre idle motion moved that pattern every frame and the mask with it: the puddle's +// reflection, which the mask turns on, flickered over a few hundred pixels a frame, and more with DLAA, whose jitter +// moves the rebuilt depth but not the volume. Such a draw, when the game sets no polygon offset for it, gets a depth +// bias toward the viewer of VOLUME_BIAS_UNITS plus VOLUME_BIAS_SLOPE times its depth slope (one pixel of it), so a +// surface the face lies on counts as inside, as the test's equal case means; a surface farther from the face than a +// pixel of its slope is decided as before. +int vk_pipeline_volume_bias(const VkPipeState *s) { + const poly_st *pl = &s->poly; + const ds_st *d = &s->ds; + const blend_st *b = &s->blend[0]; + unsigned mode = pl->mode <= NVN_POLYGON_MODE_FILL ? pl->mode : NVN_POLYGON_MODE_FILL; + if (!d->dt || d->dw || (pl->offset_en >> mode & 1) || !(s->color.blend_en & 1) || + b->eq_rgb != NVN_BLEND_EQUATION_REVERSE_SUB || b->src_rgb != NVN_BLEND_FUNC_ONE || b->dst_rgb != NVN_BLEND_FUNC_ONE) + return 0; + if (d->df == NVN_DEPTH_FUNC_LESS || d->df == NVN_DEPTH_FUNC_LEQUAL) return -1; + return d->df == NVN_DEPTH_FUNC_GREATER || d->df == NVN_DEPTH_FUNC_GEQUAL; +} + // The values vk_pipeline_set_dynamic records for a state, as the commands take them. typedef struct { VkCullModeFlags cull; @@ -3296,7 +3319,7 @@ static void dyn_vals(const VkPipeState *s, DynVals *o) { o->sop[1][0] = stencil_op(d->fail_b), o->sop[1][1] = stencil_op(d->zpass_b), o->sop[1][2] = stencil_op(d->zfail_b); o->scmp[0] = compare_op(d->sf_f), o->scmp[1] = compare_op(d->sf_b); unsigned mode = pl->mode <= NVN_POLYGON_MODE_FILL ? pl->mode : NVN_POLYGON_MODE_FILL; - o->bias = (pl->offset_en >> mode & 1) != 0; + o->bias = (pl->offset_en >> mode & 1) || vk_pipeline_volume_bias(s); o->discard = s->raster_discard != 0, o->restart = s->prim_restart != 0; o->clamp = s->depth_clamp != 0, o->a2c = s->ms.a2c != 0; o->poly = polygon_mode(pl->mode); From 121b4fdf330820b4fe1efc81b57d2dccc7687f98 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 08:13:26 +0300 Subject: [PATCH 08/20] 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 | From b5936a6aa95bfa31e697367418cebf28becb27c6 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 08:36:50 +0300 Subject: [PATCH 09/20] The camera jitter moves on with the game's presents instead of at a camera block written again: the game fills two sets of blocks in turn, so each phase went to two frames, and each view-projection takes the offset its own block's projection took --- runtime/vk_backend.c | 1 + runtime/vk_dlaa.c | 27 +++++++++++++++++++-------- runtime/vk_dlaa.h | 1 + 3 files changed, 21 insertions(+), 8 deletions(-) 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 d224dd47..b29c19db 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -951,6 +951,8 @@ static void next_jitter(uint32_t w, uint32_t ow) { // 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 @@ -964,8 +966,9 @@ static void next_jitter(uint32_t w, uint32_t ow) { static struct { PlatMutex lk; - uint64_t seen[CAMJ_BLOCKS]; // blocks whose projection was copied since the jitter last moved on - int nseen; + 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; uint64_t blocks[CAMJ_BLOCKS]; // every block that took an offset (cam_unjitter) int nblocks; uint32_t idx; // phases handed out @@ -988,6 +991,8 @@ static int in_list(const uint64_t *l, int n, uint64_t a) { 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()) return; @@ -1004,10 +1009,11 @@ void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst) { 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 + 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.nseen = 0, CJ.sx = CJ.sy = 0.0f; + 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); @@ -1017,14 +1023,19 @@ 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.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; + 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 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]; + } 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); } diff --git a/runtime/vk_dlaa.h b/runtime/vk_dlaa.h index 719c5171..f06b7a6d 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -44,6 +44,7 @@ int vk_mvobj_execute(VkExec *ex, uint64_t cam, VkImageView mv, VkImageView depth 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); From 0eab4580302b5077304087f77e821ba1ad768398 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 08:50:22 +0300 Subject: [PATCH 10/20] Symptoms: water at a low angle flickering with DLAA from the viewport jitter the game's camera did not share; cfg knows DLAA_CAM_JITTER --- docs/agents/symptoms.md | 1 + runtime/cfg.c | 2 +- runtime/vk_dlaa.c | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 5c9fb005..63122dd9 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -26,6 +26,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | no sun shadows, wrong lighting | unwritten vertex outputs read as zero instead of (0,0,0,1) | the shader's output map | | 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 | +| 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` | | 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/runtime/cfg.c b/runtime/cfg.c index 7784a7e7..45c4c25e 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/vk_dlaa.c b/runtime/vk_dlaa.c index b29c19db..3475a75a 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -839,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. From 77cb787de78018ae43b5d130b37bcc8e9dea164c Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 09:00:48 +0300 Subject: [PATCH 11/20] The water volume mask's depth bias can be turned off with volume_mask_bias = 0 (or TOTK_VOLUME_MASK_BIAS), a test setting to compare against --- docs/agents/symptoms.md | 2 +- docs/settings.md | 1 + runtime/cfg.c | 2 +- runtime/vk_pipeline.c | 5 ++++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index d17d6c75..483875d5 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -26,7 +26,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | no sun shadows, wrong lighting | unwritten vertex outputs read as zero instead of (0,0,0,1) | the shader's output map | | 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 and without the fix; in a frame dump, the quarter-size mask after the volume draw changes from frame to frame | +| 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 | | 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 18e61333..6f083969 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -164,6 +164,7 @@ Many rows below are developer tools that only act together with another setting | `vk_no_float64` (default `0`) | `1` clears the 64-bit float feature, so the translator lowers FP64 to FP32. | Shaders change, so the shader cache entries for it are made again; precision of FP64 shaders drops. | Caution | | `vk_ssbo_align` (default unset) | Power of two that storage buffer offsets are treated as needing, to test the unaligned path. | The translator and the draw code round storage offsets down to it; shaders are translated again. | Caution | | `vk_subgroup` (default `32`) | Subgroup size to pin where the driver allows it. | A value other than 32 changes what the translator is told about the GPU, and shaders are translated again. | Caution | +| `volume_mask_bias` (default `1`) | `0` leaves the game's water volume mask draws (a volume's front faces tested LEQUAL without depth writes, blended ONE/ONE with reverse subtract) without the depth bias toward the viewer that `vk_pipeline.c` gives them; `ab` alternates with `TOTK_AB`. | Puddles and still water then flicker again where the volume's top face lies on the water surface; a test setting. | Caution | | `vram_headroom` (default `1`) | `0` turns off the spare half-block of video memory that a background thread keeps per memory type. | Texture allocations on the render thread can wait for a new block again. | Caution | | `vs_outs` | `all` declares all 32 vertex shader output locations; otherwise only the union of the paired fragment inputs. | Larger vertex shaders and new pipelines; unwritten outputs read the same as before. | Caution | | `wb_eager` (default `1`) | With `sync_ahead` on, `1` copies small GPU-drawn targets that the game reads back to CPU-readable memory at the end of the frame that drew them, so the write-back at a fence wait does not wait for the GPU; `0` waits as before; `check` reads both ways and compares. Without `sync_ahead` it does nothing. | With `0` the game thread stalls about every ninth frame while the copy queues behind the frame on the GPU, which takes back part of the frame-ahead gain. `check` is slow and for testing. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index 7784a7e7..14b44d2e 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -66,7 +66,7 @@ static const char *const known[] = { "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SYNC_AHEAD", "SWKBD_TEST", "TEST_OOM", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_BLOCK_MIB", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_RELEASED_MIB", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", - "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VMA_BLOCK_MIB", "VOLUME", "VOLUME_MUSIC", "VOLUME_SFX", "VOLUME_VOICE", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", + "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VMA_BLOCK_MIB", "VOLUME", "VOLUME_MASK_BIAS", "VOLUME_MUSIC", "VOLUME_SFX", "VOLUME_VOICE", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EAGER", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", }; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 7b366d69..ea1f420a 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3278,8 +3278,11 @@ uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { // moves the rebuilt depth but not the volume. Such a draw, when the game sets no polygon offset for it, gets a depth // bias toward the viewer of VOLUME_BIAS_UNITS plus VOLUME_BIAS_SLOPE times its depth slope (one pixel of it), so a // surface the face lies on counts as inside, as the test's equal case means; a surface farther from the face than a -// pixel of its slope is decided as before. +// pixel of its slope is decided as before. TOTK_VOLUME_MASK_BIAS=0 (test) leaves such draws as the game sets them. int vk_pipeline_volume_bias(const VkPipeState *s) { + static int knob = -1; + if (knob < 0) knob = rt_knob("VOLUME_MASK_BIAS", "1"); + if (!RT_KNOB_ON(knob)) return 0; const poly_st *pl = &s->poly; const ds_st *d = &s->ds; const blend_st *b = &s->blend[0]; From d5d62ab4062b496f0a6c2b6ced5d4da12031ae7c Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 09:01:16 +0300 Subject: [PATCH 12/20] The camera jitter is described as dlaa_cam_jitter, and the game's matrix copies skip it at once while DLSS is not running --- docs/settings.md | 1 + runtime/vk_dlaa.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/settings.md b/docs/settings.md index 18e61333..90547633 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -174,6 +174,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/vk_dlaa.c b/runtime/vk_dlaa.c index 3475a75a..ce2e601c 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -996,7 +996,7 @@ void vk_dlaa_game_present(void) { __atomic_add_fetch(&CJ.presents, 1, __ATOMIC_R // 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; + 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); From 04146289acbb7d686df55d00eaeca936392eca15 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 10:42:23 +0300 Subject: [PATCH 13/20] The camera jitter's read-back takes a block's centre terms as an offset only when this code gave that block that offset: the inventory's camera has an off-centre projection of its own, which read as a jitter of 1152 pixels, so DLSS smeared the world behind the inventory into horizontal streaks --- runtime/vk_dlaa.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index ce2e601c..2b1e2f53 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -957,12 +957,15 @@ static void next_jitter(uint32_t w, uint32_t ow) { // 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. +// 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 { @@ -970,8 +973,8 @@ static struct { 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; - uint64_t blocks[CAMJ_BLOCKS]; // every block that took an offset (cam_unjitter) - int nblocks; + 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 @@ -1026,7 +1029,8 @@ void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst) { } 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; + 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; @@ -1042,14 +1046,22 @@ void vk_dlaa_camera_copied(uint64_t lr, uint64_t dst) { } // 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. +// 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); - *sx = -p[2], *sy = -p[6]; - return *sx != 0.0f || *sy != 0.0f; + 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: @@ -1093,10 +1105,13 @@ static void cam_unjitter(VkExec *ex) { if (!dup && nd < 32) done[nd++] = ex->cb; plat_mutex_unlock(&lk); if (dup) return; - uint64_t blocks[CAMJ_BLOCKS]; + uint64_t blocks[CAMJ_BLOCKS]; // the blocks given an offset lately, newest first + int n = 0; plat_mutex_lock(&CJ.lk); - int n = CJ.nblocks; - memcpy(blocks, CJ.blocks, sizeof blocks); + 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++) { From 85888bc9ba36d0c07313fe60a3405530b36008c6 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 10:50:58 +0300 Subject: [PATCH 14/20] Symptoms: the world behind the inventory streaked with DLAA, from the camera jitter's read-back taking the inventory camera's off-centre projection for a jitter --- docs/agents/symptoms.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 63122dd9..724c462a 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -27,6 +27,7 @@ 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 | | 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 | From 593eeeb8ed983b4c145110d8e3652545beeb84b1 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 18 Nov 2026 18:08:44 +0300 Subject: [PATCH 15/20] The lock order comments name the new edge: mirror_sync and mirror_create take shadow_lock under mirror_lock Co-Authored-By: Claude Opus 5.5 --- runtime/vk_memory.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 8704dc60..2ca18b17 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -1000,7 +1000,7 @@ static int query_stamp(uint64_t lo, uint64_t hi, uint32_t g) { // skips the pending ranges in the CPU copy and copies them back whole, as before. // TOTK_SHADOW=1/0 (shadow in totk.ini) forces the mode on or off; the default is on for Linux and Wine. typedef struct { uint64_t lo, hi; uint8_t *base; int refs; } SRange; -static PlatMutex shadow_lock = PLAT_MUTEX_INIT; // after pool_lock; before gen_lock +static PlatMutex shadow_lock = PLAT_MUTEX_INIT; // after mirror_lock and pool_lock; before gen_lock static PoolRec **stouch; // shadow_lock: pools resolved since the last sync static int nstouch, cstouch; static SRange *spend; // shadow_lock: distinct GPU writes recorded and not copied back yet @@ -1583,7 +1583,8 @@ static PlatMutex flush_lock = PLAT_MUTEX_INIT; static int flush_cap = FLUSH_RANGES; // ranges a pool keeps; 1 with TOTK_FLUSH_SPAN=1 // The first uses of a submission's mirrors, the copies they queue, the GPU writes noted and their snapshots (pend, wbl, snl, mrep): threads // recording chunks of one submission take it. A mirror synced in this submission is read without it (vk_mem_use): its -// acc_sub is stored last. Before pool_lock, gen_lock and flush_lock. +// acc_sub is stored last. Before pool_lock, shadow_lock (mirror_sync and mirror_create touch the shadow), gen_lock and +// flush_lock. static PlatMutex mirror_lock = PLAT_MUTEX_INIT; typedef struct { From 0350d91b7273d599e2f5362948a1b551b17f6184 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 18 Nov 2026 18:18:22 +0300 Subject: [PATCH 16/20] The water volume mask bias is decided once per state change and only for LEQUAL and GEQUAL draws (#1126) prepare_draw sets ex->volume_bias where the draw's dynamic state changes, so record_fixed reads a flag instead of checking the pipeline state on every draw. A census of the matching draws in the field, the town and rain found two more states with the same blend and depth signature, both tested GREATER: the volume's back faces into the same channel and another shader's front faces into red. GREATER's equal case fails, and the bias toward passing turned the game's tie the other way for them; they are now left as the game sets them. Co-Authored-By: Claude Opus 5.5 --- runtime/vk.h | 5 +++-- runtime/vk_exec.h | 2 +- runtime/vk_exec_draw.c | 14 +++++++++----- runtime/vk_pipeline.c | 9 ++++++--- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 0b105c78..53f06c17 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -679,8 +679,9 @@ void vk_pipeline_answers(const VkPipeState *s, GfxShaderAnswers *vs, GfxShaderAn int vk_pipeline_tex_type(int nvn_target); // NVN_TEXTURE_TARGET_* -> GFX_TEX_* (GfxShaderAnswers.tex_type) uint32_t vk_pipeline_target_wmask(const VkPipeState *s, int i); // colour write mask of target i the pipeline uses // The sign of the depth bias a volume mask draw gets (vk_pipeline.c, see there), -1 toward smaller depth, 1 toward -// larger, 0 for any other draw. vk_pipeline_set_dynamic enables the bias for it; vk_exec_draw.c record_fixed sets the -// values, VOLUME_BIAS_UNITS and VOLUME_BIAS_SLOPE times the sign. +// larger, 0 for any other draw. vk_pipeline_set_dynamic enables the bias for it; vk_exec_draw.c prepare_draw decides it +// where the draw's state changes and record_fixed sets the values, VOLUME_BIAS_UNITS and VOLUME_BIAS_SLOPE times the +// sign. int vk_pipeline_volume_bias(const VkPipeState *s); #define VOLUME_BIAS_UNITS 16.0f #define VOLUME_BIAS_SLOPE 1.0f diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 9e475ccb..a08b6124 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -119,7 +119,7 @@ typedef struct VkExec { uint32_t st_vmask[2], st_wmask[2], st_ref[2]; // OP_STENCIL per face (0 front, 1 back): compare mask, write mask, ref float point_size; // OP_BIND_STATE kind 6: NVN point size for the support buffer; 0 = never set = 1 p_poly_offset poly_offset; // OP_POLY_OFFSET: depth bias factor, units, clamp - int8_t volume_bias; // the sign of the volume bias recorded in place of poly_offset (record_fixed) + int8_t volume_bias; // the sign of the volume bias recorded in place of poly_offset: set by prepare_draw, used by record_fixed p_prim_restart prim_restart; // OP_PRIM_RESTART: applied to indexed strip and fan draws (op_draw_idx) float blend_color[4]; // OP_BLEND_COLOR int64_t copy_stride; // OP_COPY_STRIDE: row stride of buffer <-> texture copies (vk_exec_xfer.c) diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 95c74f32..1a217027 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2784,11 +2784,11 @@ static VkViewport draw_viewport(VkExec *ex) { return v; } -// s: the draw's state. A volume mask draw records its own depth bias (vk_pipeline_volume_bias) in place of the game's. -static void record_fixed(VkExec *ex, const VkPipeState *s) { +// A volume mask draw records its own depth bias in place of the game's: ex->volume_bias, which prepare_draw sets from +// vk_pipeline_volume_bias where the draw's dynamic state changes. +static void record_fixed(VkExec *ex) { VkCommandBuffer cb = ex->cb; - int vb = vk_pipeline_volume_bias(s); - if (vb != ex->volume_bias) ex->dirty |= VK_EXEC_DIRTY_DEPTH_BIAS, ex->volume_bias = (int8_t)vb; + int vb = ex->volume_bias; if (ex->dirty & VK_EXEC_DIRTY_VIEWPORT) { VkViewport v = draw_viewport(ex); vkCmdSetViewportWithCount(cb, 1, &v); @@ -3425,6 +3425,10 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { __atomic_fetch_add(&DD.calls, 1, __ATOMIC_RELAXED), __atomic_fetch_add(&DD.cmds, cmds, __ATOMIC_RELAXED); if (!cmds) __atomic_fetch_add(&DD.empty, 1, __ATOMIC_RELAXED); ex->recorded += cmds != 0; + // The volume bias follows the state alone, so it is decided here, once per state change, and a draw with the + // state recorded last keeps it. A new command buffer or a pipeline bind clears st_valid and comes here. + int vb = vk_pipeline_volume_bias(sp); + if (vb != ex->volume_bias) ex->dirty |= VK_EXEC_DIRTY_DEPTH_BIAS, ex->volume_bias = (int8_t)vb; MX_CPY(&D->B.st, sp, sizeof *sp), D->B.st_valid = 1; if (ex->mark && ex->fr.draws == ex->mark_draws0) ex->mark->first[0] = vs, ex->mark->first[1] = fs; } @@ -3435,7 +3439,7 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { vk_mvsurvey_draw(ex); vk_mvprev_draw(ex); if (mo) plat_mutex_unlock(&mo_lock); - record_fixed(ex, sp); + record_fixed(ex); repeat_stats(ex, info); if (bim) bi_gt(ex, bgt), bi_gb(ex, bgb); if (bim == 2) bi_census(ex, info, sp, bgt, bgb, bsg, sm_hit); // on counts nothing: it is what gets timed diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index ea1f420a..b144140c 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3278,7 +3278,11 @@ uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { // moves the rebuilt depth but not the volume. Such a draw, when the game sets no polygon offset for it, gets a depth // bias toward the viewer of VOLUME_BIAS_UNITS plus VOLUME_BIAS_SLOPE times its depth slope (one pixel of it), so a // surface the face lies on counts as inside, as the test's equal case means; a surface farther from the face than a -// pixel of its slope is decided as before. TOTK_VOLUME_MASK_BIAS=0 (test) leaves such draws as the game sets them. +// pixel of its slope is decided as before. Only tests whose equal case passes (LEQUAL, GEQUAL) get the bias, toward +// passing. The same pass also draws state-alike draws with GREATER, whose equal case fails: the volume's back faces +// into the same channel and, in towns and in rain, another shader's front faces into the red channel. Biasing those +// toward passing would turn the game's tie the other way, so they are left as the game sets them. +// TOTK_VOLUME_MASK_BIAS=0 (test) leaves every such draw as the game sets it. int vk_pipeline_volume_bias(const VkPipeState *s) { static int knob = -1; if (knob < 0) knob = rt_knob("VOLUME_MASK_BIAS", "1"); @@ -3290,8 +3294,7 @@ int vk_pipeline_volume_bias(const VkPipeState *s) { if (!d->dt || d->dw || (pl->offset_en >> mode & 1) || !(s->color.blend_en & 1) || b->eq_rgb != NVN_BLEND_EQUATION_REVERSE_SUB || b->src_rgb != NVN_BLEND_FUNC_ONE || b->dst_rgb != NVN_BLEND_FUNC_ONE) return 0; - if (d->df == NVN_DEPTH_FUNC_LESS || d->df == NVN_DEPTH_FUNC_LEQUAL) return -1; - return d->df == NVN_DEPTH_FUNC_GREATER || d->df == NVN_DEPTH_FUNC_GEQUAL; + return d->df == NVN_DEPTH_FUNC_LEQUAL ? -1 : d->df == NVN_DEPTH_FUNC_GEQUAL; } // The values vk_pipeline_set_dynamic records for a state, as the commands take them. From c145f2bd5da1394a12d93dd4b310662db8c03b63 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 18 Nov 2026 18:29:28 +0300 Subject: [PATCH 17/20] The camera jitter checks where each copy puts its matrix in the block: f_70000e31d4 takes the offsets from the shader's layout (x8), so only a projection at 0x70 and a view-projection at 0x30 take the offset, a projection whose view-projection lies elsewhere gets its centre terms back, and a block counts as given only once both matrices carry it (#1126) Co-Authored-By: Claude Opus 5.5 --- runtime/nvn_api_core.c | 11 +++++++---- runtime/vk_dlaa.c | 30 +++++++++++++++++++++--------- runtime/vk_dlaa.h | 2 +- 3 files changed, 29 insertions(+), 14 deletions(-) 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. From bc579c62e8470235281def23e605cab525586ebb Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 18 Nov 2026 18:55:12 +0300 Subject: [PATCH 18/20] The qstamp patch set is switched off when sync_ahead is refused, so TOTK_SYNC_AHEAD=1 without shadow storage leaves the game's depth query check as it was Co-Authored-By: Claude Opus 5.5 --- runtime/nvn_api_core.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index e61df50d..96c65e2b 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -511,6 +511,9 @@ static int sync_ahead(void) { "GPU as before"); on = 0; } + // The depth query patch (set qstamp, patches-v143.txt) is switched by TOTK_SYNC_AHEAD too: a refused switch + // leaves the game's own frame number check, as with the switch off. + if (!on) rt_patch_switch(rt_patch_set("qstamp"), 0); if (on) rt_log("nvn: TOTK_SYNC_AHEAD: a wait for the frame presented last waits for the frame before it"); } return on; From 234a10f413cf2bd9bbf2c84874f825cd3ee42bcf Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 18 Nov 2026 18:55:13 +0300 Subject: [PATCH 19/20] With sync_ahead an exit line gives the run total of the waits at a pool invalidate, as the shadow log lines stop at frame 1800 without TOTK_TRACE=perf Co-Authored-By: Claude Opus 5.5 --- docs/agents/symptoms.md | 2 +- docs/architecture.md | 2 +- runtime/vk.h | 1 + runtime/vk_backend.c | 1 + runtime/vk_memory.c | 17 ++++++++++++++++- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index c4769ad0..378eb709 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -44,7 +44,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | frame counter wanders 50-60 | two 60 Hz clocks (vsync event and present acquire) drifting apart | whether both follow one display tick | | a 30 Hz thread runs at 27 Hz, slow cutscenes | host sleep overshoot: Windows waitable timers wake 0.3-0.5 ms late | `TOTK_TRACE=sched` sleep asked versus took | | 20 fps after a scheduler change | a yield back-off that starts too early | yield handling against the console's | -| in the Depths Link takes gloom damage and wears the gloom haze while standing on clean ground, some runs only; only with `sync_ahead = 1` | the game read a GPU result of the frame it was told is done before the GPU wrote it: its one-pixel readbacks go through a buffer per request, which then holds zeros or an answer from the ring's last round (fixed: `vk_memory.c` `vk_mem_shadow_read` waits at the invalidate) | `sync_ahead = 0`; the shadow log line's "the game waited for a GPU write it reads" count | +| in the Depths Link takes gloom damage and wears the gloom haze while standing on clean ground, some runs only; only with `sync_ahead = 1` | the game read a GPU result of the frame it was told is done before the GPU wrote it: its one-pixel readbacks go through a buffer per request, which then holds zeros or an answer from the ring's last round (fixed: `vk_memory.c` `vk_mem_shadow_read` waits at the invalidate) | `sync_ahead = 0`; the exit line `mem: the game waited for a GPU write it reads` | | in shrines the lamps' reflections on the floor go out for single frames, every other frame at some camera angles; only with `sync_ahead = 1`, more with DLAA (the GPU runs further behind) | the game reads its depth query results two frames later and refuses them unless their frame number is exactly the counter - 2; with sync_ahead it is the counter - 3 on frames where the GPU is behind (fixed: patch set `qstamp` in `recomp/patches-v143.txt` also takes the counter - 3) | `sync_ahead = 0`; a `--capture` run with `TOTK_SYNC_AHEAD` on, counting one-frame drops in floor regions | | climbing or movement wrong only above some fps | game code that assumes a fixed step count per frame | compare at 30, 60 and the failing rate | diff --git a/docs/architecture.md b/docs/architecture.md index e9e637a6..70d5ddad 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -90,7 +90,7 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app - Memory pools. Apple's unified memory let MacNativeGames use the game's pools zero-copy. The back end imports the pools as host memory (`VK_EXT_external_memory_host`). On a discrete GPU, pools bound as buffers are mirrored in device-local memory and the pages the CPU wrote (write watch, above) are uploaded before a replay; idle pool regions are made read-only so the write-watch queries skip them (`vk_memory.c`). GPU-rendered textures are written back when the CPU reads them: on a pool invalidate, on a CPU copy, and for small targets once the CPU waits for a fence (`vk_texture.c`). - Replay thread. `TOTK_REPLAY_ASYNC=1` moves the replay off the submitting thread onto a queue at most one frame behind the game; reads of GPU results wait for the newest writer instead of draining the queue (`vk_backend.c`). It is off by default. Where the game waits for the GPU it overlaps the replay with the game threads (a GTX 1650 in the Depths, issue #88), a gain measured only there; while it was the default, a GTX 1660 Ti at the 60 fps cap with the GPU at 26 % reported uneven motion (#659). It adds up to a frame of latency; `replay_async = 1` turns it on. - Chunked replay. A large submission is recorded in chunks by several threads (`TOTK_REPLAY_WORKERS`, 0 by default: the sequential replay; 4 is a good value from 8 logical processors) and the chunks go to the queue in order (`vk_exec.c`). A sequential scan keeps a copy of the queue state where each chunk starts and lists what a chunk would otherwise learn from the chunks before it: inline uniform updates, conditions, contact shadow marches, uniform drops. It also makes render targets live in replay order and notes the op, since a chunk can sample a storage before the chunk that renders it got there or after the scan went past a later target there: a chunk's texture slots resolve as of its own op, and the chunks after a texture with no entry yet wait for the chunk that makes it. DLSS's decisions (the G-buffer, the light target, the end of the scene, the scaling pass) are the scan's too, and its copy of the lit scene and its evaluation are records of the chunk that holds the op; a chunk's draws are jittered while they come before the scan's end of the scene. The evaluation waits until the chunks before it are recorded, as it reads what their draws noted (the camera block, the jitter, the per-object motion list, which chunks add to under one lock), and the chunks after it wait for it, as they sample its output. `TOTK_REPLAY_STATE_CHECK=1` compares the state after the last chunk with the scan's. -- Frame ahead. `TOTK_SYNC_AHEAD=1` (`sync_ahead`, off by default) makes the one `nvnSyncWait` per frame that waits for the frame presented last wait for the frame before it, so the game builds the next frame while the GPU still runs the previous one (`nvn_api_core.c`, `nvnSyncWait`). The game then writes memory that in-flight work still reads. This is safe only with shadow storage (`vk_memory.c`, the CPU-visible pools are host-visible copies of guest memory, on by default on Linux and Wine, `shadow = 1` (`TOTK_SHADOW=1`) on native Windows; without it the switch is ignored): a CPU write reaches the GPU only through the shadow copy made before each submit (`vk_mem_shadow_sync`), and a copy that would overwrite bytes an unfinished submission reads is queued as a GPU copy in the prologue command buffer of the part submitted next (`vk_mem_shadow_prologue`, `TOTK_SHADOW_DEFER=1`, the default with the feature) or, with `=0`, waited for on the replay thread (`shadow_reader_wait`). Only that one wait lags (identified by its call site, known for 1.1.0, 1.2.1 and 1.4.3). GPU writes into pool storage come back with their fence, so a buffer the game reads back every frame after that wait holds the result of the frame before; a buffer it has not read lately holds none, and its `nvnMemoryPoolInvalidateMappedRange` waits for the write (`vk_mem_shadow_read`): TotK reads single pixels through a buffer per request, and in the Depths zeros in one of them put Link in gloom. The fence write-back of small render targets (`vk_texture.c`, `write_back_n`) does not wait for the GPU with `TOTK_WB_EAGER=1` (`wb_eager`, the default, but only while frame-ahead is in effect: with `sync_ahead` off or refused the setting does nothing and the write-back reads the image when it runs): `vk_sync_submit` has `vk_texture_eager_record` copy the targets that value rendered into staging buffers inside the same command buffer, so once the value has passed the write-back only copies from staging into the target's storage. Only a target that was written back within the last 64 timeline values is copied (the first write-back of a target takes the blocking path), and the staging of a target past that window, no longer wanted or released goes through the deferred free (`vk_defer_buffer_until`). A snapshot is used only when its value is the one that last rendered the target, nothing recorded a write to the image since (`wr_fence`) and the GPU has finished it; otherwise the write-back reads the image as before, and the exit line `vk: wb_eager:` counts the reasons. With frame-ahead in effect the exit line `vk: fence write-backs waited for the GPU` counts the write-backs that waited for a copy and the time they waited, for every value of the setting. `check` also reads the old way and compares. +- Frame ahead. `TOTK_SYNC_AHEAD=1` (`sync_ahead`, off by default) makes the one `nvnSyncWait` per frame that waits for the frame presented last wait for the frame before it, so the game builds the next frame while the GPU still runs the previous one (`nvn_api_core.c`, `nvnSyncWait`). The game then writes memory that in-flight work still reads. This is safe only with shadow storage (`vk_memory.c`, the CPU-visible pools are host-visible copies of guest memory, on by default on Linux and Wine, `shadow = 1` (`TOTK_SHADOW=1`) on native Windows; without it the switch is ignored): a CPU write reaches the GPU only through the shadow copy made before each submit (`vk_mem_shadow_sync`), and a copy that would overwrite bytes an unfinished submission reads is queued as a GPU copy in the prologue command buffer of the part submitted next (`vk_mem_shadow_prologue`, `TOTK_SHADOW_DEFER=1`, the default with the feature) or, with `=0`, waited for on the replay thread (`shadow_reader_wait`). Only that one wait lags (identified by its call site, known for 1.1.0, 1.2.1 and 1.4.3). GPU writes into pool storage come back with their fence, so a buffer the game reads back every frame after that wait holds the result of the frame before; a buffer it has not read lately holds none, and its `nvnMemoryPoolInvalidateMappedRange` waits for the write (`vk_mem_shadow_read`): TotK reads single pixels through a buffer per request, and in the Depths zeros in one of them put Link in gloom. The exit line `mem: the game waited for a GPU write it reads` counts these waits and their time. On 1.4.3 the patch set `qstamp` (`recomp/patches-v143.txt`) lets the game take its depth query results one frame older; it follows the switch and is off when the switch is refused. The fence write-back of small render targets (`vk_texture.c`, `write_back_n`) does not wait for the GPU with `TOTK_WB_EAGER=1` (`wb_eager`, the default, but only while frame-ahead is in effect: with `sync_ahead` off or refused the setting does nothing and the write-back reads the image when it runs): `vk_sync_submit` has `vk_texture_eager_record` copy the targets that value rendered into staging buffers inside the same command buffer, so once the value has passed the write-back only copies from staging into the target's storage. Only a target that was written back within the last 64 timeline values is copied (the first write-back of a target takes the blocking path), and the staging of a target past that window, no longer wanted or released goes through the deferred free (`vk_defer_buffer_until`). A snapshot is used only when its value is the one that last rendered the target, nothing recorded a write to the image since (`wr_fence`) and the GPU has finished it; otherwise the write-back reads the image as before, and the exit line `vk: wb_eager:` counts the reasons. With frame-ahead in effect the exit line `vk: fence write-backs waited for the GPU` counts the write-backs that waited for a copy and the time they waited, for every value of the setting. `check` also reads the old way and compares. - Recorder thread. The Vulkan calls the replay makes into its command buffer are made by a thread of their own (`vk_rec.c`, after Savage Wind Recomp and LAS Recomp): each `vkCmd*` copies its arguments into a packet of a ring and the recorder makes the calls in order; calls into other command buffers are made at once; the recorder also ends the command buffer, and the submit waits until it has. In the town on an RX 9070 the replay takes 17% less time on the submitting thread. It is off by default on AMD GPUs, where the driver loses the device or hangs the GPU at startup with it (`TOTK_VK_RECORDER=1` forces it on, `=0` makes every call on the submitting thread). - Uniform snapshot. The game rewrites per-object uniform blocks on other threads soon after it submits the frame that reads them, while the replay still records it. At the start of a submission's replay every uniform range it diff --git a/runtime/vk.h b/runtime/vk.h index 5c57fd32..25913c93 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -304,6 +304,7 @@ void vk_mem_shadow_sync(uint64_t fence); // fence: the value of the part about // vk_mem_mirror_prologue's copies, which read the shadow. int vk_mem_shadow_deferred(void); void vk_mem_shadow_prologue(VkCommandBuffer cb, uint64_t fence); +void vk_mem_shadow_read_log(void); // at exit, with TOTK_SYNC_AHEAD: the run total of vk_mem_shadow_read's waits void vk_mem_gpu_pending(uint64_t addr, uint64_t size); void vk_gpu_written(uint64_t addr, uint64_t size); int vk_mem_flush_add_probe(void); // --probe-vkexec: the flush range list; 0 or the number of the failed check diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 497529a8..7a418463 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -339,6 +339,7 @@ void gfx_exit_log(void) { static int32_t done; rec_check_log(); vk_texture_eager_log(); + vk_mem_shadow_read_log(); if (gfx_replay_async() && r_started && !plat_xchg32(&done, 1)) replay_log(game_frames, 1); } diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index b02841c7..baaabef5 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -1319,6 +1319,7 @@ void vk_mem_shadow_fence(void) { typedef struct { uint64_t addr, size, at; } SRead; static SRead sread[SREAD_N]; // shadow_lock: the ranges the game invalidated last, with the value issued then static uint64_t s_read_waits, s_read_ticks; // shadow_lock: since the last log +static uint64_t r_read_waits, r_read_ticks, r_read_max; // shadow_lock: over the run (vk_mem_shadow_read_log) void vk_mem_shadow_read(uint64_t addr, uint64_t size) { if (!shadow_on || !size || !nvn_sync_ahead_guard()) return; @@ -1342,8 +1343,22 @@ void vk_mem_shadow_read(uint64_t addr, uint64_t size) { NVN_BLOCKING(); vk_sync_fence_wait(wait, UINT64_MAX); } + uint64_t dt = qpc() - t0; plat_mutex_lock(&shadow_lock); - s_read_waits++, s_read_ticks += qpc() - t0; + s_read_waits++, s_read_ticks += dt; + r_read_waits++, r_read_ticks += dt; + if (dt > r_read_max) r_read_max = dt; + plat_mutex_unlock(&shadow_lock); +} + +// The run total of the waits above, once at exit, while frame-ahead is in effect: without TOTK_TRACE=perf the shadow +// log lines come only at frames 600 and 1800. +void vk_mem_shadow_read_log(void) { + static int32_t done; + if (!shadow_on || !nvn_sync_ahead_on() || plat_xchg32(&done, 1)) return; + plat_mutex_lock(&shadow_lock); + rt_log("mem: the game waited for a GPU write it reads %llu times, %.1f ms in all, longest %.2f ms", + (unsigned long long)r_read_waits, qpc_ms(r_read_ticks), qpc_ms(r_read_max)); plat_mutex_unlock(&shadow_lock); } From a9abffdba671b1782dc56d1df899d5bf797e55e0 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 18 Nov 2026 18:57:50 +0300 Subject: [PATCH 20/20] tests/sync_ahead_check.py checks that a refused sync_ahead switches the qstamp set off on 1.4.3 Co-Authored-By: Claude Opus 5.5 --- tests/sync_ahead_check.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/sync_ahead_check.py b/tests/sync_ahead_check.py index a450a61e..ef6d0571 100644 --- a/tests/sync_ahead_check.py +++ b/tests/sync_ahead_check.py @@ -39,6 +39,10 @@ static int cfg_bool(const char *name) { assert(!strcmp(name, "TOTK_SYNC_AHEAD")) static int vk_mem_shadow_on(void) { return shadow; } static uint64_t nvn_fence_last(void) { return last; } static void rt_log(const char *format, ...) { (void)format; } +// The patch table: set qstamp exists on 1.4.3 only and starts as TOTK_SYNC_AHEAD says (runtime/patches.c). +static int qstamp_on; +static int rt_patch_set(const char *name) { return !strcmp(name, "qstamp") && rt_game_version == 143 ? 20 : -1; } +static void rt_patch_switch(int set, int on) { if (set == 20) qstamp_on = on; else assert(set == -1); } static void gfx_replay_wait_value(uint64_t value) { replay_value = value; replay_calls++; } static void vk_mem_shadow_fence(void) { assert(nvn_sync_ahead_guard() >= replay_value); @@ -59,10 +63,12 @@ int main(int argc, char **argv) { uint64_t site = rt_game_version == 110 ? 0x7000b39aa4ull : rt_game_version == 121 ? 0x7000b23dfcull : rt_game_version == 143 ? 0x70002db99cull : 0; assert(frame_wait_lr() == site); + qstamp_on = rt_game_version == 143 && enabled; last = 20; nvn_sync_ahead_present(); last = 40; nvn_sync_ahead_present(); assert(pa_fence[0] == (enabled && shadow && supported ? 40 : 0)); assert(pa_fence[1] == (enabled && shadow && supported ? 20 : 0)); + assert(qstamp_on == (rt_game_version == 143 && enabled && shadow)); // a refused switch leaves the game's check int relaxed_cases = 0, polls = 0, other_waits = 0; for (int i = 0; i < 100000; i++) { sync_t sync = {.fenced = random_word() % 2, .fence = random_word() % 65};