Merge task/drs: with sync_ahead, timestamps the late frame wait did not cover read as the newest one written at that wait, so dynamic resolution no longer drops to its lowest level for half a second at a time (#1123)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-25 16:17:45 +03:00
commit 20e27a2d33
5 files changed
+107 -5

No files matched your search

+5
View File
@@ -50,6 +50,11 @@ uint64_t nvn_new_serial(void); // next tex_t / prog_
void nvn_sync_ahead_present(void);
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=1 and a wait site known for this version
// vk_exec_xfer.c, TOTK_SYNC_AHEAD: at the game's frame wait the newest timestamp written becomes the bound; the value
// the game reads for the timestamp t of the report at addr (t, or the bound for a report its frame wait did not cover)
void vk_counter_frame(void);
uint64_t vk_counter_timestamp(uint64_t addr, uint64_t t);
void vk_counter_log(void); // the run total, once (gfx_exit_log)
int vk_mem_shadow_on(void); // vk_memory.c: 1 with shadow storage on (valid once the Vulkan back end is up)
int vk_mem_mirror_on(void); // vk_memory.c: 1 when pools used as buffers get VRAM mirrors (memory = mirror or auto on a discrete GPU)
void vk_mem_shadow_fence(void); // vk_memory.c: returns once every shadow sync that read the old guard is done
+2
View File
@@ -581,6 +581,7 @@ NVN_API(nvnSyncWait) {
if (det) vk_ahead_frame_lag(s->fence, lag, behind);
b->cpu_synced(want == s->fence ? s->fence : b->fence_done());
if (det) vk_ahead_frame_done(1);
if (mode == 1) vk_counter_frame();
ret_x(c, NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED);
return;
}
@@ -592,6 +593,7 @@ NVN_API(nvnSyncWait) {
if (det) vk_ahead_frame_lag(s->fence, lag, behind);
if (ok) b->cpu_synced(want == s->fence ? s->fence : b->fence_done());
if (det) vk_ahead_frame_done(ok);
if (mode == 1) vk_counter_frame();
ret_x(c, ok ? NVN_SYNC_WAIT_RESULT_CONDITION_SATISFIED : NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED);
}
// void nvnEventBuilderSetDefaults(NVNeventBuilder *b)
+6 -2
View File
@@ -60,8 +60,12 @@ NVN_API(nvnDeviceSetAutomaticCheckpointsMask) { (void)c; }
static uint64_t ticks_to_ns(uint64_t t) { return t / 12 * 625 + t % 12 * 625 / 12; }
// uint64_t nvnDeviceGetCurrentTimestampInNanoseconds(const NVNdevice *d)
NVN_API(nvnDeviceGetCurrentTimestampInNanoseconds) { ret_x(c, ticks_to_ns(rt_ticks())); }
// uint64_t nvnDeviceGetTimestampInNanoseconds(const NVNdevice *d, const NVNcounterData *counter)
NVN_API(nvnDeviceGetTimestampInNanoseconds) { ret_x(c, ticks_to_ns(((const NVNcounterData *)arg_p(c, 1))->timestamp)); }
// uint64_t nvnDeviceGetTimestampInNanoseconds(const NVNdevice *d, const NVNcounterData *counter): with the frame wait
// one frame late, a report that wait did not cover reads as the newest one written by then (vk_exec_xfer.c)
NVN_API(nvnDeviceGetTimestampInNanoseconds) {
const NVNcounterData *d = arg_p(c, 1);
ret_x(c, ticks_to_ns(vk_counter_timestamp((uintptr_t)d, d->timestamp)));
}
// ---------- memory pools and buffers
// size_t nvnMemoryPoolGetSize(const NVNmemoryPool *p)
+1
View File
@@ -480,6 +480,7 @@ void gfx_exit_log(void) {
vk_mem_shadow_read_log();
vk_ahead_log_final();
vk_ahead_log_guards();
vk_counter_log();
if (gfx_replay_async() && r_started && !plat_xchg32(&done, 1)) replay_log(game_frames, 1);
}
+93 -3
View File
@@ -38,6 +38,7 @@
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
#include "host.h"
#include "nvn_api.h"
#include "nvn_tex.h"
@@ -92,9 +93,11 @@ static void event_cb(void *arg) {
}
// NVNcounterData = {counter, timestamp}; timestamps are Tegra 19.2 MHz ticks (the base of GetSystemTick).
static uint64_t counter_last; // the newest timestamp written (completion thread), for the bound below
static void write_counter(uint64_t addr, uint64_t counter) {
uint64_t rec[2] = {counter, (uint64_t)((unsigned __int128)kern_now_ns() * 12 / 625)};
memcpy((void *)(uintptr_t)addr, rec, sizeof rec);
__atomic_store_n(&counter_last, rec[1], __ATOMIC_RELEASE);
}
static void counter_cb(void *arg) {
@@ -104,11 +107,21 @@ static void counter_cb(void *arg) {
}
// Reports queued by the replay that the GPU has not finished. The condition of RENDER_COND reads a timestamp one as 0
// and a samples-passed one as unknown.
// and a samples-passed one as unknown. The replay thread writes the ring; game threads read it too
// (vk_counter_timestamp): an entry's fence is stored before its address, so a reader that finds the address sees its
// fence or a newer one, and an entry caught in between is judged by the newer fence (pending, the safe side).
#define PENDING 256
static struct { uint64_t addr, fence; int unknown; } pending[PENDING];
static unsigned pending_head;
static void pending_add(uint64_t addr, uint64_t fence, int unknown) {
unsigned i = pending_head % PENDING;
__atomic_store_n(&pending[i].fence, fence, __ATOMIC_RELAXED);
pending[i].unknown = unknown;
__atomic_store_n(&pending[i].addr, addr, __ATOMIC_RELEASE);
__atomic_store_n(&pending_head, pending_head + 1, __ATOMIC_RELEASE);
}
// ---- occlusion counts (samples passed)
//
// After a SAMPLES_PASSED reset, the first draw of a pass begins an occlusion query (precise when the device has
@@ -300,7 +313,84 @@ static void report_counter(VkExec *ex, const p_report_counter *p) {
*a = p->addr;
vk_completion_add(ex->fence, counter_cb, a);
}
if (!ex->chunked) pending[pending_head++ % PENDING] = (typeof(pending[0])){p->addr, ex->fence, samples}; // else the scan's
if (!ex->chunked) pending_add(p->addr, ex->fence, samples); // else the scan's
}
// ---- timestamps read one frame early (TOTK_SYNC_AHEAD)
//
// A report lands in guest memory when its submission's fence completes (write_counter). With the frame wait one frame
// late the game reads timestamps of work the GPU may not have finished. TotK's GPU time for its dynamic resolution
// controller (1.4.3 main+0xa1a50, 1.2.1 main+0x972bac) converts the end and then the begin report of a pair it issued
// two updates before, subtracts them unsigned and zeroes both reports. An end not written yet reads 0, and one written
// after the zeroing leaves the value of the report before it for the next read: either way the difference wraps to
// about 2^64, the controller sees an over-budget frame, and two of those in a row drop the game to its lowest quality
// level until 60 good frames bring it back (the half-second blur of #1123). Measured on 1.4.3 (islands): 12-20
// wrapped pairs in 40 s with the late wait, none without it.
//
// Every read is kept consistent with the order of the reports. At each frame wait the bound B becomes the newest
// timestamp written so far. A read of a report that holds 0, holds a time after B, or whose newest replayed report is
// not done yet reads B instead. Every report done before B was taken is written and holds at most B, so of two reports
// in submission order the first never reads later than the second: the difference lies between 0 and what the exact
// wait gives, never wrapped. Reports the exact wait would also have covered read as they are. Only the converted value
// changes (nvnDeviceGetTimestampInNanoseconds); guest memory, RENDER_COND and samples-passed counts stay as they were.
// TOTK_SYNC_AHEAD_TIMESTAMPS: 1 (default; acts only with the frame wait late), 0 off, check: counts what it would
// replace and leaves the reads as they are.
static uint64_t ts_bound; // 0: off
static int ts_mode = -1; // 0 off, 1 on, 2 check
static struct { uint64_t reads, zero, later, pending, last_log; } ts_stat; // game threads, approximate counts
static int ts_on(void) {
if (ts_mode < 0) {
const char *v = cfg_get("TOTK_SYNC_AHEAD_TIMESTAMPS");
ts_mode = !v || !*v ? 1 : !strcmp(v, "check") ? 2 : strcmp(v, "0") != 0;
rt_log("sync-ahead: timestamps the game reads before their frame is done read as the newest one written at "
"the frame wait: %s", ts_mode == 2 ? "check (counted, not replaced)" : ts_mode ? "on" : "off");
}
return ts_mode;
}
void vk_counter_frame(void) {
if (ts_on()) __atomic_store_n(&ts_bound, __atomic_load_n(&counter_last, __ATOMIC_ACQUIRE), __ATOMIC_RELEASE);
}
// 1: the newest replayed report to addr is not done
static int report_pending(uint64_t addr) {
uint64_t done = vk_sync_fence_done();
unsigned head = __atomic_load_n(&pending_head, __ATOMIC_ACQUIRE);
for (unsigned k = 1; k <= PENDING; k++) { // newest first: the last report to addr decides
unsigned i = (head - k) % PENDING;
if (__atomic_load_n(&pending[i].addr, __ATOMIC_ACQUIRE) == addr)
return __atomic_load_n(&pending[i].fence, __ATOMIC_RELAXED) > done;
}
return 0;
}
static void ts_log(const char *when) {
rt_log("sync-ahead: timestamps %s: %llu reads, %s %llu (zero %llu, after the bound %llu, report not done %llu)", when,
(unsigned long long)ts_stat.reads, ts_mode == 2 ? "would be replaced" : "replaced",
(unsigned long long)(ts_stat.zero + ts_stat.later + ts_stat.pending), (unsigned long long)ts_stat.zero,
(unsigned long long)ts_stat.later, (unsigned long long)ts_stat.pending);
}
uint64_t vk_counter_timestamp(uint64_t addr, uint64_t t) {
uint64_t b = __atomic_load_n(&ts_bound, __ATOMIC_ACQUIRE);
if (!b) return t;
ts_stat.reads++;
int why = !t ? 1 : t > b ? 2 : report_pending(addr) ? 3 : 0;
if (why == 1) ts_stat.zero++;
else if (why == 2) ts_stat.later++;
else if (why == 3) ts_stat.pending++;
uint64_t now = kern_now_ns();
if (now - ts_stat.last_log > 30000000000ull) { // every 30 s of host time
if (ts_stat.last_log) ts_log("so far");
ts_stat.last_log = now;
}
return why && ts_mode == 1 ? b : t;
}
void vk_counter_log(void) {
static int once;
if (ts_mode > 0 && !__atomic_exchange_n(&once, 1, __ATOMIC_RELAXED)) ts_log("over the run");
}
// ---- conditional rendering
@@ -694,7 +784,7 @@ int vk_exec_xfer_scan(VkExec *ex, uint32_t op, const void *payload) {
VkMemSpan m;
if (!nvn_gpu_only(p->addr) && !vk_mem_resolve(p->addr, sizeof(NVNcounterData), &m)) break; // the recording stops
if (samples) oq_report_state(ex);
if (!nvn_gpu_only(p->addr)) pending[pending_head++ % PENDING] = (typeof(pending[0])){p->addr, ex->fence, samples};
if (!nvn_gpu_only(p->addr)) pending_add(p->addr, ex->fence, samples);
break;
}
}