mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-18 20:15:56 +00:00
Pace presents at the back edge, just before the host present
The pacer gave each frame its display time but the present thread queued vkQueuePresentKHR as soon as the game handed the frame over, so every present shifted with the frame's replay time: at 60.0 fps mean, 17-18 ms intervals alternated with 15-16 ms ones. The present thread now submits the blit at once (vk_present_blit, so vk_present_drain and queue order are unchanged) and holds the host present until the pacer's slot (vk_present_flip). Acquire keeps waiting until the image's slot plus one period, which is now the time the next frame really goes out. TOTK_TRACE=present logs one line per present: host interval, the game's present-call interval, wait and lateness. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d714537e7
commit
ebdba400df
9 files changed
+97
-26
No files matched your search
+3
-1
@@ -109,7 +109,9 @@ typedef struct {
|
||||
uint64_t (*submit)(const uint64_t *handles, int n); // replays; returns this submission's fence value
|
||||
int (*fence_wait)(uint64_t value, uint64_t timeout_ns); // 1 reached; caller has given its core up
|
||||
uint64_t (*fence_done)(void); // highest value whose side effects are written
|
||||
void (*present)(const tex_t *t, const int crop[4]); // queues the present; returns at once
|
||||
// Queues the present and returns at once; the host present goes out at at_ns (kern_now_ns clock), at once when that
|
||||
// has passed or at_ns is 0.
|
||||
void (*present)(const tex_t *t, const int crop[4], uint64_t at_ns);
|
||||
void (*finish)(void);
|
||||
void (*read_texels)(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint8_t *dst, uint64_t row, uint64_t img);
|
||||
void (*write_texels)(const tex_t *t, const view_t *v, const NVNcopyRegion *r, const uint8_t *src, uint64_t row, uint64_t img);
|
||||
|
||||
+2
-2
@@ -111,8 +111,8 @@ static int null_fence_wait(uint64_t value, uint64_t timeout_ns) {
|
||||
|
||||
static uint64_t null_fence_done(void) { return __atomic_load_n(&fence, __ATOMIC_ACQUIRE); }
|
||||
|
||||
static void null_present(const tex_t *t, const int crop[4]) {
|
||||
(void)t, (void)crop;
|
||||
static void null_present(const tex_t *t, const int crop[4], uint64_t at_ns) {
|
||||
(void)t, (void)crop, (void)at_ns;
|
||||
__atomic_fetch_add(&gfx_counters.frames, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
|
||||
+40
-8
@@ -58,6 +58,8 @@ static VmaAllocation g_cap_alloc;
|
||||
static uint8_t *g_cap_ptr;
|
||||
static VkDeviceSize g_cap_size;
|
||||
static int g_cap_w, g_cap_h, g_cap_valid;
|
||||
static int g_flip_on; // vk_present_blit submitted the blit of swapchain image g_flip_idx; vk_present_flip presents it
|
||||
static uint32_t g_flip_idx;
|
||||
|
||||
// ---- TOTK_FLIGHT thumbnails (flight.c; g_pl): each present also blits the swapchain image down into slot frame % g_th_n
|
||||
// of a host-visible ring. A slot's tag names the frame it holds complete; recording a new frame clears the tag first,
|
||||
@@ -261,6 +263,21 @@ static VkPresentModeKHR pick_present_mode(VkPhysicalDevice pd) {
|
||||
return VK_PRESENT_MODE_FIFO_KHR;
|
||||
}
|
||||
|
||||
// Queues the present of a blit vk_present_blit left waiting (g_pl held). A swapchain rebuild and the next blit flush it
|
||||
// first; the device release drops it.
|
||||
static void flip_locked(void) {
|
||||
if (!g_flip_on) return;
|
||||
g_flip_on = 0;
|
||||
if (!g_sc) return;
|
||||
VkPresentInfoKHR pi = {.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .waitSemaphoreCount = 1,
|
||||
.pWaitSemaphores = &g_render_done[g_flip_idx], .swapchainCount = 1, .pSwapchains = &g_sc,
|
||||
.pImageIndices = &g_flip_idx};
|
||||
vk_queue_lock();
|
||||
VkResult r = vkQueuePresentKHR(vk_ctx()->gfx_queue, &pi);
|
||||
vk_queue_unlock();
|
||||
if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) InterlockedExchange(&g_dirty, 1);
|
||||
}
|
||||
|
||||
static void free_capture(void) {
|
||||
VkCtx *c = vk_ctx();
|
||||
if (g_cap_buf && c) vmaDestroyBuffer(c->vma, g_cap_buf, g_cap_alloc);
|
||||
@@ -272,6 +289,7 @@ static void free_capture(void) {
|
||||
|
||||
// Returns 1 with a usable swapchain, 0 when the window has no area (minimised). Called with g_pl held.
|
||||
static int create_swapchain(void) {
|
||||
flip_locked(); // the acquired image goes out before its swapchain is replaced
|
||||
VkCtx *c = vk_ctx();
|
||||
VkSurfaceCapabilitiesKHR caps;
|
||||
if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR(c->phys, g_surface, &caps) != VK_SUCCESS) return 0;
|
||||
@@ -479,12 +497,12 @@ int vk_present_captured(const uint8_t **px, int *w, int *h, VkFormat *fmt) {
|
||||
return ok;
|
||||
}
|
||||
|
||||
void vk_present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int h, const int crop[4]) {
|
||||
(void)fmt;
|
||||
static void present_image(VkImage src, VkImageLayout layout, int w, int h, const int crop[4], int defer) {
|
||||
int general = layout == VK_IMAGE_LAYOUT_GENERAL;
|
||||
gfx_window_start();
|
||||
VkCtx *c = vk_ctx();
|
||||
EnterCriticalSection(&g_pl);
|
||||
flip_locked();
|
||||
if (!g_sc || InterlockedCompareExchange(&g_dirty, 0, 0)) {
|
||||
if (!IsIconic(g_hwnd) && !create_swapchain() && !g_sc) goto out;
|
||||
}
|
||||
@@ -569,16 +587,14 @@ void vk_present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, in
|
||||
.pSignalSemaphoreInfos = &sig};
|
||||
vk_queue_lock();
|
||||
r = vkQueueSubmit2(c->gfx_queue, 1, &si, g_fence[slot]);
|
||||
vk_queue_unlock();
|
||||
if (r == VK_SUCCESS) {
|
||||
VkPresentInfoKHR pi = {.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .waitSemaphoreCount = 1,
|
||||
.pWaitSemaphores = &g_render_done[idx], .swapchainCount = 1, .pSwapchains = &g_sc,
|
||||
.pImageIndices = &idx};
|
||||
r = vkQueuePresentKHR(c->gfx_queue, &pi);
|
||||
if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) InterlockedExchange(&g_dirty, 1);
|
||||
g_flip_on = 1;
|
||||
g_flip_idx = idx;
|
||||
if (!defer) flip_locked();
|
||||
} else {
|
||||
rt_log("gfx: present submit failed (%d)", (int)r);
|
||||
}
|
||||
vk_queue_unlock();
|
||||
g_frame++;
|
||||
if (cap) {
|
||||
vkWaitForFences(c->device, 1, &g_fence[slot], VK_TRUE, 5000000000ull);
|
||||
@@ -591,6 +607,21 @@ out:
|
||||
LeaveCriticalSection(&g_pl);
|
||||
}
|
||||
|
||||
void vk_present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int h, const int crop[4]) {
|
||||
(void)fmt;
|
||||
present_image(src, layout, w, h, crop, 0);
|
||||
}
|
||||
|
||||
void vk_present_blit(VkImage src, VkImageLayout layout, int w, int h, const int crop[4]) {
|
||||
present_image(src, layout, w, h, crop, 1);
|
||||
}
|
||||
|
||||
void vk_present_flip(void) {
|
||||
EnterCriticalSection(&g_pl);
|
||||
flip_locked();
|
||||
LeaveCriticalSection(&g_pl);
|
||||
}
|
||||
|
||||
void gfx_window_resize(int cw, int chh) {
|
||||
gfx_window_start();
|
||||
RECT r = {0, 0, cw, chh};
|
||||
@@ -609,6 +640,7 @@ void gfx_window_release_vk(void) {
|
||||
vk_queue_lock();
|
||||
vkDeviceWaitIdle(c->device);
|
||||
vk_queue_unlock();
|
||||
g_flip_on = 0; // its blit is done (device idle); the swapchain goes away unpresented
|
||||
free_capture();
|
||||
free_thumbs();
|
||||
for (int i = 0; i < FRAMES; i++) {
|
||||
|
||||
+2
-2
@@ -18,8 +18,8 @@
|
||||
// ---- logging and results
|
||||
void rt_log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); // "[host] <msg>\n" to stderr, one write
|
||||
enum { RT_TRACE_SVC = 1, RT_TRACE_HOSTCALL = 2, RT_TRACE_IPC = 4, RT_TRACE_FS = 8, RT_TRACE_GFX = 16, RT_TRACE_PERF = 32,
|
||||
RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128 };
|
||||
extern unsigned rt_trace; // from TOTK_TRACE=svc,host,ipc,fs,gfx,perf,sched (or all)
|
||||
RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256 };
|
||||
extern unsigned rt_trace; // from TOTK_TRACE=svc,host,ipc,fs,gfx,perf,sched,audio,present (or all)
|
||||
// prof_win.c: TOTK_PROFILE=<path> sampling profiler; TOTK_TRACE=perf frame timing (waits of the render thread)
|
||||
// PERF_TEX: texture uploads on the render thread (deswizzle, decode, staging), PERF_ASTC included
|
||||
enum { PERF_PACE, PERF_FENCE, PERF_DRAIN, PERF_PIPE, PERF_XLATE, PERF_TEX, PERF_ASTC, PERF_N };
|
||||
|
||||
+3
-2
@@ -50,13 +50,14 @@ static void parse_trace(void) {
|
||||
if (!e) return;
|
||||
static const struct { const char *name; unsigned bit; } names[] = {
|
||||
{"svc", RT_TRACE_SVC}, {"host", RT_TRACE_HOSTCALL}, {"ipc", RT_TRACE_IPC}, {"fs", RT_TRACE_FS},
|
||||
{"gfx", RT_TRACE_GFX}, {"perf", RT_TRACE_PERF}, {"sched", RT_TRACE_SCHED}, {"audio", RT_TRACE_AUDIO}, {"all", ~0u}};
|
||||
{"gfx", RT_TRACE_GFX}, {"perf", RT_TRACE_PERF}, {"sched", RT_TRACE_SCHED}, {"audio", RT_TRACE_AUDIO},
|
||||
{"present", RT_TRACE_PRESENT}, {"all", ~0u}};
|
||||
for (const char *p = e; *p;) {
|
||||
size_t n = strcspn(p, ",");
|
||||
int known = 0;
|
||||
for (unsigned i = 0; i < sizeof names / sizeof *names; i++)
|
||||
if (strlen(names[i].name) == n && !strncmp(p, names[i].name, n)) rt_trace |= names[i].bit, known = 1;
|
||||
if (!known && n) rt_log("TOTK_TRACE: unknown item '%.*s' (svc, host, ipc, fs, gfx, perf, sched, audio, all)", (int)n, p);
|
||||
if (!known && n) rt_log("TOTK_TRACE: unknown item '%.*s' (svc, host, ipc, fs, gfx, perf, sched, audio, present, all)", (int)n, p);
|
||||
p += n + (p[n] == ',');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -663,7 +663,7 @@ static void sleep_until(uint64_t deadline) {
|
||||
|
||||
// NVNwindowAcquireTextureResult nvnWindowAcquireTexture(NVNwindow *w, NVNsync *s, int *index): blocks here, not in the
|
||||
// present, until the image's previous present plus one period has passed (the game often presents with a lock held).
|
||||
// Presents return at once, so the image is free as soon as that time is reached.
|
||||
// That is the time the next frame goes to the display, so the image is free as soon as that time is reached.
|
||||
NVN_API(nvnWindowAcquireTexture) {
|
||||
win_t *o = arg_p(c, 0);
|
||||
sync_t *s = arg_p(c, 1);
|
||||
@@ -686,8 +686,10 @@ NVN_API(nvnWindowAcquireTexture) {
|
||||
ret_x(c, NVN_WINDOW_ACQUIRE_TEXTURE_RESULT_SUCCESS);
|
||||
}
|
||||
|
||||
// void nvnQueuePresentTexture(NVNqueue *q, NVNwindow *w, int index): queues the copy and returns at once; the display
|
||||
// time comes from the pacer and frees the image for the next acquire
|
||||
// void nvnQueuePresentTexture(NVNqueue *q, NVNwindow *w, int index): queues the copy and returns at once. The display
|
||||
// time comes from the pacer: the back end's present thread holds the host present until then (back-edge pacing, as the
|
||||
// Switch's display flips at vblank), so the present cadence does not follow the replay time; one period later the
|
||||
// image is free for the next acquire. Added latency: the slack of the frame (period minus its work), as on the Switch.
|
||||
NVN_API(nvnQueuePresentTexture) {
|
||||
win_t *o = arg_p(c, 1);
|
||||
int idx = (int)arg_x(c, 2);
|
||||
@@ -697,10 +699,10 @@ NVN_API(nvnQueuePresentTexture) {
|
||||
rt_fatal("nvn: QueuePresentTexture of image %d on window %p", idx, (void *)o);
|
||||
o->presents++;
|
||||
const tex_t *t = (const tex_t *)o->tex[idx];
|
||||
gfx_backend()->present(t, o->crop);
|
||||
o->ticket[idx] = o->presents;
|
||||
uint64_t period = present_period(o), now = kern_now_ns();
|
||||
uint64_t slot = now + pace(&o->pace, now, period);
|
||||
gfx_backend()->present(t, o->crop, slot);
|
||||
o->ticket[idx] = o->presents;
|
||||
o->avail[idx] = slot + period;
|
||||
frame_step_present(slot, o->interval); // frame_rate.c: the real-time step follows the display times
|
||||
rt_frame_presented(t, o->presents);
|
||||
|
||||
@@ -78,6 +78,10 @@ int vk_selftest(void); // 0 on success
|
||||
// the blit). All its writes must be submitted to gfx_queue before the call (queue order does the rest). Starts the
|
||||
// window on first use. A minimised window or a lost swapchain skips the frame. Calls serialise on an internal lock.
|
||||
void vk_present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int h, const int crop[4]);
|
||||
// vk_present_image split in two: vk_present_blit submits the blit and leaves the present waiting, vk_present_flip
|
||||
// queues it. The next blit or a swapchain rebuild queues a waiting present first.
|
||||
void vk_present_blit(VkImage src, VkImageLayout layout, int w, int h, const int crop[4]);
|
||||
void vk_present_flip(void);
|
||||
// Every submission to ctx->gfx_queue (and xfer_queue when shared) takes this lock; vk_present_image and the swapchain
|
||||
// rebuild do. Recursive.
|
||||
void vk_queue_lock(void);
|
||||
|
||||
+35
-5
@@ -5,7 +5,11 @@
|
||||
// Present: the game texture is resolved on the calling thread and its blit is handed to a present thread, so
|
||||
// nvnQueuePresentTexture returns at once (section 5). Queue order does the rest: the submissions that wrote the texture
|
||||
// are already on gfx_queue, and every call that may write or free GPU state the blit reads (submit, finish, pool and
|
||||
// texture teardown, texel writes) first waits until the queued present was submitted (vk_present_drain).
|
||||
// texture teardown, texel writes) first waits until the queued blit was submitted (vk_present_drain).
|
||||
//
|
||||
// Back-edge pacing: the present thread submits the blit at once, then holds the host present (vkQueuePresentKHR) until
|
||||
// the frame's display time from the pacer (nvn_api_obj.c), so present-to-present intervals stay even whatever the
|
||||
// replay took. The blit is already on the queue, so the wait blocks no submission.
|
||||
#include <windows.h>
|
||||
|
||||
#include "flight.h"
|
||||
@@ -17,15 +21,27 @@
|
||||
|
||||
#define WEAK __attribute__((weak))
|
||||
|
||||
typedef struct { VkImage image; VkFormat format; int w, h, has_crop, crop[4]; uint64_t frame; } PresentJob;
|
||||
typedef struct { VkImage image; VkFormat format; int w, h, has_crop, crop[4]; uint64_t frame, at, queued; } PresentJob;
|
||||
|
||||
static SRWLOCK pl = SRWLOCK_INIT;
|
||||
static CONDITION_VARIABLE pcv = CONDITION_VARIABLE_INIT; // job queued, job taken, job submitted
|
||||
static PresentJob job;
|
||||
static int pending, busy, present_thread;
|
||||
|
||||
// Sleeps on a high-resolution timer to 1 ms before at, then yields until at: the timer alone wakes up to ~0.5 ms late.
|
||||
static void wait_until(HANDLE timer, uint64_t at) {
|
||||
uint64_t now = kern_now_ns();
|
||||
if (at > now + 1500000 && timer) {
|
||||
LARGE_INTEGER d = {.QuadPart = -(LONGLONG)((at - now - 1000000) / 100)};
|
||||
if (SetWaitableTimer(timer, &d, 0, NULL, NULL, FALSE)) WaitForSingleObject(timer, INFINITE);
|
||||
}
|
||||
while (kern_now_ns() < at) SwitchToThread();
|
||||
}
|
||||
|
||||
static DWORD WINAPI present_main(void *arg) {
|
||||
(void)arg;
|
||||
HANDLE timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
|
||||
uint64_t last = 0, last_queued = 0;
|
||||
AcquireSRWLockExclusive(&pl);
|
||||
for (;;) {
|
||||
while (!pending) SleepConditionVariableSRW(&pcv, &pl, INFINITE, 0);
|
||||
@@ -35,12 +51,25 @@ static DWORD WINAPI present_main(void *arg) {
|
||||
WakeAllConditionVariable(&pcv);
|
||||
ReleaseSRWLockExclusive(&pl);
|
||||
if (flight_on) vk_thumbs_frame(j.frame);
|
||||
vk_present_image(j.image, VK_IMAGE_LAYOUT_GENERAL, j.format, j.w, j.h, j.has_crop ? j.crop : NULL);
|
||||
vk_present_blit(j.image, VK_IMAGE_LAYOUT_GENERAL, j.w, j.h, j.has_crop ? j.crop : NULL);
|
||||
vk_sync_submit(NULL, 0); // a timeline value after the blit: deferred destruction waits for the blit too
|
||||
AcquireSRWLockExclusive(&pl);
|
||||
busy = 0;
|
||||
WakeAllConditionVariable(&pcv);
|
||||
ReleaseSRWLockExclusive(&pl);
|
||||
uint64_t t0 = kern_now_ns();
|
||||
wait_until(timer, j.at);
|
||||
uint64_t t1 = kern_now_ns();
|
||||
vk_present_flip();
|
||||
// TOTK_TRACE=present: one line per present: the host present's interval, the interval of the game's present
|
||||
// calls (when the host present went out before back-edge pacing), the wait and how far past its target it went
|
||||
if (rt_trace & RT_TRACE_PRESENT)
|
||||
rt_log("present: frame %llu interval %.3f ms, game %.3f ms, waited %.3f ms, %.3f ms past target",
|
||||
(unsigned long long)j.frame, last ? (t1 - last) / 1e6 : 0.0,
|
||||
last_queued ? (j.queued - last_queued) / 1e6 : 0.0, (t1 - t0) / 1e6,
|
||||
j.at && t1 > j.at ? (t1 - j.at) / 1e6 : 0.0);
|
||||
last = t1;
|
||||
last_queued = j.queued;
|
||||
vk_pipeline_autosave(); // off the render thread, outside the present lock
|
||||
AcquireSRWLockExclusive(&pl);
|
||||
}
|
||||
@@ -55,7 +84,7 @@ void vk_present_drain(void) {
|
||||
perf_end(PERF_DRAIN, p0);
|
||||
}
|
||||
|
||||
static void vk_be_present(const tex_t *t, const int crop[4]) {
|
||||
static void vk_be_present(const tex_t *t, const int crop[4], uint64_t at_ns) {
|
||||
static volatile long n_skipped;
|
||||
vk_sync_start();
|
||||
uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED);
|
||||
@@ -69,7 +98,8 @@ static void vk_be_present(const tex_t *t, const int crop[4]) {
|
||||
rt_log("vk: present of texture %p (format %#x) skipped: no colour image", (const void *)t, t ? t->d.format : 0);
|
||||
return;
|
||||
}
|
||||
PresentJob j = {.image = img->image, .format = img->format, .w = (int)img->width, .h = (int)img->height, .frame = frame};
|
||||
PresentJob j = {.image = img->image, .format = img->format, .w = (int)img->width, .h = (int)img->height, .frame = frame,
|
||||
.at = at_ns, .queued = kern_now_ns()};
|
||||
if (crop) {
|
||||
j.has_crop = 1;
|
||||
for (int i = 0; i < 4; i++) j.crop[i] = crop[i];
|
||||
|
||||
@@ -128,7 +128,7 @@ static int test_present(const GfxBackend *b, uint8_t *guest, int *w_out, int *h_
|
||||
uint64_t frames = gfx_stats().frames;
|
||||
vk_present_capture(1);
|
||||
const int crop[4] = {0, 0, TEX_W, TEX_H};
|
||||
b->present(&t, crop);
|
||||
b->present(&t, crop, 0);
|
||||
b->finish(); // the queued present is submitted and, with capture on, read back
|
||||
const uint8_t *px;
|
||||
int w, h;
|
||||
|
||||
Reference in new issue
Block a user