The exit log counts the fence write-backs that waited for the GPU and their time with frame-ahead on

The new line is written for wb_eager 0, 1 and check alike, so the stall wb_eager removes can be compared between the settings. It costs a clock read only on the blocking path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-17 20:12:18 +03:00
1 parent 3b2bd1f6ba
commit 8232ac4505
2 files changed
+21 -4

No files matched your search

+1 -1
View File
@@ -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. `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); 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.
- 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
+20 -3
View File
@@ -1559,6 +1559,10 @@ static uint64_t eager_allocs, eager_snapshots, eager_taken, eager_fallback, eage
enum { EB_READY, EB_NONE, EB_OTHER_VALUE, EB_WR_FENCE, EB_NOT_DONE, EB_N };
static uint64_t eager_why[EB_N]; // tex_lock
static uint64_t eager_logged; // fallbacks logged one by one, up to EAGER_LOG_MAX
// Fence write-backs that waited for a staging copy (vk_upload_flush(1) in write_back_n) and the time they waited, the
// stall wb_eager removes; counted with wb_eager = 0 too while frame-ahead is in effect, so both settings compare (tex_lock).
static uint64_t wb_block_n, wb_block_ns, wb_block_max_ns;
static int wb_count_blocks; // frame-ahead in effect: the exit line is written whatever wb_eager is
enum { EAGER_LOG_MAX = 20 };
static void eager_release(Tex *e);
@@ -1566,7 +1570,8 @@ static void eager_release(Tex *e);
static void wb_eager_init(void) {
const char *v = cfg_get("TOTK_WB_EAGER");
int want = v && !strcmp(v, "0") ? EAGER_OFF : v && !strcmp(v, "check") ? EAGER_CHECK : EAGER_ON;
wb_eager = want != EAGER_OFF && !nvn_sync_ahead_on() ? EAGER_OFF : want;
wb_count_blocks = nvn_sync_ahead_on();
wb_eager = want != EAGER_OFF && !wb_count_blocks ? EAGER_OFF : want;
if (want != EAGER_OFF && wb_eager == EAGER_OFF)
rt_log("vk: wb_eager: off, frame-ahead (sync_ahead) is not in effect");
}
@@ -2320,7 +2325,11 @@ void vk_texture_eager_record(VkCommandBuffer cb, uint64_t fence) {
void vk_texture_eager_log(void) {
static int32_t done;
if (wb_eager <= EAGER_OFF || plat_xchg32(&done, 1)) return;
if (!wb_count_blocks || plat_xchg32(&done, 1)) return;
rt_log("vk: fence write-backs waited for the GPU %llu times, %.1f ms in all, longest %.2f ms (wb_eager %s)",
(unsigned long long)wb_block_n, (double)wb_block_ns / 1e6, (double)wb_block_max_ns / 1e6,
wb_eager == EAGER_CHECK ? "check, the compare reads included" : wb_eager == EAGER_ON ? "1" : "0");
if (wb_eager <= EAGER_OFF) return;
rt_log("vk: wb_eager: %llu snapshots, %llu taken without a wait, %llu fell back to the blocking path (%llu staging buffers allocated)",
(unsigned long long)eager_snapshots, (unsigned long long)eager_taken, (unsigned long long)eager_fallback,
(unsigned long long)eager_allocs);
@@ -2371,7 +2380,15 @@ static void write_back_n(Tex **v, int n) {
if (wb_region(e, lv, ly, &q->ep.g, &q->ep.r) && texels_read_record(e, &q->ep.g, &q->ep.r, &q->ep.t)) np++, nrec++;
}
}
if (nrec) vk_upload_flush(1);
if (nrec) {
uint64_t t0 = wb_count_blocks ? plat_now_ns() : 0;
vk_upload_flush(1);
if (wb_count_blocks) {
uint64_t d = plat_now_ns() - t0;
wb_block_n++, wb_block_ns += d;
if (d > wb_block_max_ns) wb_block_max_ns = d;
}
}
for (int i = 0; i < np; i++) {
WbPart *q = &p[i];
size_t bytes = q->ep.g.img * (size_t)q->ep.g.nz;