diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 98be5bfd..7270fe87 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -31,7 +31,7 @@ Code copied or adapted from another project is listed here with its source, the | libopus (MSYS2 `mingw-w64-ucrt-x86_64-opus`), dynamic link, `libopus-0.dll` copied next to `totk.exe` | https://opus-codec.org, Copyright (c) 2001-2023 Xiph.Org, Skype Limited, Octasic, Jean-Marc Valin, Timothy B. Terriberry, CSIRO, Gregory Maxwell, Mark Borgerding, Erik de Castro Lopo, Mozilla, Amazon | BSD-3-Clause | `runtime/ov_media.c` (nn::codec hardware Opus decoder) | | Dear ImGui 1.91.9 (core and Vulkan back end, unmodified), as vendored by LAS Recomp | https://github.com/ocornut/imgui, via https://github.com/peiikai/LASRecomp @ `da35b83a9611d046a9a681aae668d7085954d37f` (`third_party/imgui/`), Copyright (c) 2014-2025 Omar Cornut | MIT | `runtime/third_party/imgui/` (compiled into `totk.exe` for the in-game menu, `runtime/ui/`) | | AMD FidelityFX SDK 1.1.4 subset (public host and shader headers, frame interpolation and optical flow components, the Vulkan backend `ffx_vk.cpp` and its GLSL shaders, shared helpers), with three marked Savage Wind patches (64-byte aligned backend scratch arrays, doubled context size where `wchar_t` is 4 bytes, all six descriptor pool sizes) | https://github.com/GPUOpen-LibrariesAndSDKs/FidelityFX-SDK/tree/v1.1.4, Copyright (C) 2024 Advanced Micro Devices, Inc., via https://github.com/Revivedx/SavageWindRecomp @ `bf6146c14c37fee8781a601e5a544b93dff6d75e` (`third_party/FidelityFX/`) | MIT | `runtime/third_party/FidelityFX/` (compiled into `totk.exe`; licence text in `runtime/third_party/FidelityFX/LICENSE.txt`, shipped as `licenses/FidelityFX-SDK-LICENSE.txt`) | -| SavageWindRecomp FidelityFX build support and frame generation set-up: the SDK-to-clang compat header, the glslang SPIR-V shader generator with its resource reflection, the blob lookup and the backend and context creation of `runtime/nvn/vulkan/framegen.cpp` | https://github.com/Revivedx/SavageWindRecomp @ `bf6146c14c37fee8781a601e5a544b93dff6d75e` (`third_party/FidelityFX/ffx_compat.h`, `tools/ffx_shaders.py`, `runtime/nvn/vulkan/framegen.cpp`) | MIT | `runtime/third_party/FidelityFX/ffx_compat.h`, `runtime/third_party/FidelityFX/spirv/ffx_shaders_vk.h` (output of `tools/ffx_shaders.py`), `tools/ffx_shaders.py`, `runtime/vk_fg.cpp` | +| SavageWindRecomp FidelityFX build support and frame generation set-up: the SDK-to-clang compat header, the glslang SPIR-V shader generator with its resource reflection, the blob lookup and the backend and context creation of `runtime/nvn/vulkan/framegen.cpp`, the pair's present spacing of `runtime/nvn/vulkan/device.c` `present_wait_turn` | https://github.com/Revivedx/SavageWindRecomp @ `bf6146c14c37fee8781a601e5a544b93dff6d75e` (`third_party/FidelityFX/ffx_compat.h`, `tools/ffx_shaders.py`, `runtime/nvn/vulkan/framegen.cpp`, `runtime/nvn/vulkan/device.c`) | MIT | `runtime/third_party/FidelityFX/ffx_compat.h`, `runtime/third_party/FidelityFX/spirv/ffx_shaders_vk.h` (output of `tools/ffx_shaders.py`), `tools/ffx_shaders.py`, `runtime/vk_fg.cpp`, `runtime/vk_backend.c` (`present_pair`) | | SavageWindRecomp settings menu: two-pane option rows (row look, left / right stepping, hold speed-up, description pane), the menu style and tab bar | https://github.com/Revivedx/SavageWindRecomp @ `575ad88e02f9a89a9a885d90039d48af33c982f4` (`runtime/ui/option_rows.mm`, `runtime/ui/menu.mm`), Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai | MIT | `runtime/ui/option_rows.cpp`, `runtime/ui/option_rows.h`, `runtime/ui/menu.cpp` | | SavageWindRecomp descriptor sets allocated in growing batches per layout | https://github.com/Revivedx/SavageWindRecomp @ `740b18025fe11ba03c117dbb56668db304b5043e` (`runtime/nvn/vulkan/exec.c` vk_alloc_set), Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai | MIT | `runtime/vk_exec_draw.c` (alloc_set_batched) | | SavageWindRecomp performance overlay: the frame ring and its one-second summary, the NVML calls, the sampling loop, the full panel's layout | https://github.com/Revivedx/SavageWindRecomp @ `575ad88e02f9a89a9a885d90039d48af33c982f4` (`runtime/perfmon.c`, `runtime/perfmon.h`, `perf_overlay_draw` in `runtime/ui/menu.mm`), Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai | MIT | `runtime/perfmon.c`, `runtime/perfmon.h`, `runtime/ui/menu.cpp` | diff --git a/docs/settings.md b/docs/settings.md index a2277d0a..63f4e631 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -143,7 +143,7 @@ Many rows below are developer tools that only act together with another setting | `dyn_delta` (default `1`) | Records only the pipeline dynamic states (cull mode, depth and stencil tests, vertex input, blend enables, equations and write masks, ...) whose values changed since the last draw in the command buffer, also across pipeline changes; `0` records the full set at every change of pipeline state, as before; `ab` alternates with the `ab` blocks. | Fewer commands per pipeline change on the render and recorder threads; a driver that loses dynamic state across pipeline binds would draw with stale state. `TOTK_TRACE=perf` prints the calls and commands per submission. | Caution | | `feedback` | `0` samples a target that an earlier draw of the same pass wrote with no barrier; `restart` ends and restarts the pass behind a full barrier; otherwise the feedback-loop layout is used when the GPU supports it. | `0` brings back the old behaviour that the barrier fixed, so the picture can break in passes that read a target they draw into; `restart` costs extra passes. | Risky | | `flush_span` (default `0`) | `1` copies one span from the lowest to the highest flushed address of a pool, as an older version did. | Copies far more data to the GPU in crowded towns: an estimated 30-50 ms of GPU time per large copy over PCIe 3.0 x8. | Caution | -| `frame_gen` (default `off`) | The frame generator: `off`, or `fsr3`, which brings up AMD FSR 3 frame interpolation and optical flow (FidelityFX SDK 1.1.4) at start; with DLSS off the replay follows the scene and records the camera motion vectors, the picture before the HUD and the interpolation of a frame between each two the game draws (`runtime/vk_fg.cpp`). Off with DLSS, `replay_workers` above 0, `sync_ahead = 1` or HDR output, and while a menu background or a loading or event fade is open. Read at start. | Work in progress: the generated frames are made but not shown (`fg_test` shows them for checks). Costs about 1.5 ms of GPU and 0.55 ms of the replay thread per frame at 1080p (RX 9070, 7900X). | Caution | +| `frame_gen` (default `off`) | The frame generator: `off`, or `fsr3`, which brings up AMD FSR 3 frame interpolation and optical flow (FidelityFX SDK 1.1.4) at start; with DLSS off the replay follows the scene and records the camera motion vectors, the picture before the HUD and the interpolation of a frame between each two the game draws (`runtime/vk_fg.cpp`). Each frame then goes out as two presents: the generated picture once the GPU has drawn the frame (a timeline wait, not before the pacer's time), the game's half the smoothed interval later (`runtime/vk_backend.c` `present_pair`); `fps_limit` counts the pictures shown and the game draws half, 30 at least (`runtime/frame_rate.c` `frame_step_cap_ns`). Off with DLSS, `replay_workers` above 0, `sync_ahead = 1` or HDR output, and while a menu background or a loading or event fade is open; the game then draws at the whole limit. The swapchain gets one image more. Read at start. | The controls answer at the drawn rate, about half a frame later. Costs about 1.5 ms of GPU and 0.6 ms of the replay thread per drawn frame at 1080p (RX 9070, 7900X on 4 cores: 94 drawn without it, 86 drawn and 169 shown with it, no limit). For displays of 120 Hz and more. | Caution | | `fg_test` | Frame generation checks: `1` presents the generated frame in place of the game's, `2` the picture before the HUD. Read at start. | Shows the frames out of their order: for checks only. | Caution | | `fg_debug` | `1`: the generated frames hold the SDK's debug view (motion fields, optical flow, disocclusion). Read at start. | For checks only. | Caution | | `fg_shot` | Frame generation shots: a present number, or `w` of world time; the window texture, the generated frame and the picture before the HUD of three frames from there go to `fg_shot_dir` as `fg__.png`. | Writes PNGs. | Safe | diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 19014073..fafc3e6d 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -160,6 +160,11 @@ uint64_t frame_step_cap_ns(void) { double hz = limit_hz ? limit_hz : gfx_window_refresh_hz(); if (hz < LIMIT_MIN_HZ) hz = 60; // display rate unknown (no window yet) if (hz > LIMIT_MAX_HZ) hz = LIMIT_MAX_HZ; + // Frame generation (vk_backend.c present_pair) shows two pictures for each frame the game draws: the limit counts + // the pictures shown and the game draws half, 30 at least (Savage Wind's framepace_period_ns, PR #1089). Only + // while the last frame went out with a generated picture: a menu or a fade, which has none, keeps the whole limit. + // The real-time step follows the game's presents (frame_step_present), so it is the drawn frames' interval. + if (gfx_frame_gen_live()) hz = hz / 2 < LIMIT_MIN_HZ ? LIMIT_MIN_HZ : hz / 2; return (uint64_t)(1e9 / hz + 0.5); } diff --git a/runtime/gfx.h b/runtime/gfx.h index f5bf4aa1..c3dedfc8 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -23,6 +23,8 @@ uint64_t gfx_vsync_ticks(void); // ticks since start uint64_t gfx_vsync_time_ns(uint64_t tick); // kern_now_ns() time of a tick (pacing) double gfx_window_refresh_hz(void); // refresh rate of the window's monitor; 0 unknown int gfx_window_focused(void); +uint64_t gfx_presents_shown(void); // gfx_present.c: presents queued since the start (two a frame with frame generation) +int gfx_frame_gen_live(void); // vk_backend.c: the last frame was shown with a generated picture before it int gfx_picture_rect(float out[4]); // where the last present put the picture, fractions of the swapchain (x, y, w, h); 0 before one void gfx_present_hold(int on); // 1: the window keeps its last picture, nothing new is presented (quick load) // NVIDIA Reflex (gfx_present.c, VK_NV_low_latency2): the NVN front end and the input service mark a frame's stages. diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 3ad68607..e1b842d6 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -27,6 +27,7 @@ #include "platform/platform.h" #include "ui/menu.h" #include "vk.h" +#include "vk_fg.h" #define FRAMES 2 #define MAX_IMAGES 16 @@ -143,8 +144,14 @@ 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; +// vk_present_blit submitted the blits of swapchain images g_flip_idx[0 .. g_flip_n), oldest first; vk_present_flip +// presents the oldest. Two only with frame generation's pair (vk_present_blit_pair): the generated picture, then the +// game's frame. +static int g_flip_n; +static uint32_t g_flip_idx[2]; +static int g_flip_gen[2]; // that blit shows a generated picture: its present carries no present ID +static volatile int64_t g_shown; // presents queued since the start (gfx_presents_shown) +static int g_overlay_on; // the pair's generated picture laid the menu out: the game's frame draws it again // ---- 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, @@ -278,7 +285,7 @@ static uint64_t g_reflex_val; static uint64_t g_reflex_frame_next; // the last ID handed out static uint64_t g_reflex_frame; // the frame the game simulates now static _Thread_local uint64_t t_reflex_submit; // the frame whose work this thread submits; 0: g_reflex_frame -static uint64_t g_blit_pid, g_flip_pid; // the frame of the blit being made and of the present waiting (g_pl) +static uint64_t g_blit_pid, g_flip_pid[2]; // the frame of the blit being made and of each present waiting (g_pl) static uint64_t g_last_pid; // the last present ID presented (g_pl): present IDs only go up static int g_reflex_mode = -1; // 0=off, 1=on, 2=boost static uint32_t g_reflex_min_us = 1; @@ -468,19 +475,25 @@ static VkPresentModeKHR pick_present_mode(VkPhysicalDevice pd) { const char *gfx_window_present_mode(void) { return __atomic_load_n(&g_present_got, __ATOMIC_ACQUIRE); } -// 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; +// Queues the present of the oldest blit vk_present_blit left waiting (g_pl held). +static void flip_one(void) { + if (!g_flip_n) return; + uint32_t idx = g_flip_idx[0]; + uint64_t frame_pid = g_flip_pid[0]; + int generated = g_flip_gen[0]; + g_flip_idx[0] = g_flip_idx[1], g_flip_pid[0] = g_flip_pid[1], g_flip_gen[0] = g_flip_gen[1]; + g_flip_n--; if (!g_sc) return; VkCtx *c = vk_ctx(); uint64_t pid = 0; VkPresentIdKHR pid_info = {0}; - if (c && c->has_nv_reflex) { + // A generated picture goes out between two frames whose IDs follow each other: no ID is left for it, and the + // game's frame after it keeps the one its markers were set with (the rule of PR #1089) + int with_id = c && c->has_nv_reflex && !generated; + if (with_id) { // The frame's own ID; a present without one (a probe, a skipped frame's neighbour) takes the next free value, // as present IDs must grow on a swapchain - pid = g_flip_pid > g_last_pid ? g_flip_pid : g_last_pid + 1; + pid = frame_pid > g_last_pid ? frame_pid : g_last_pid + 1; g_last_pid = pid; pid_info.sType = VK_STRUCTURE_TYPE_PRESENT_ID_KHR; pid_info.swapchainCount = 1; @@ -488,17 +501,26 @@ static void flip_locked(void) { vk_reflex_marker(4, pid); // VK_LATENCY_MARKER_PRESENT_START_NV } VkPresentInfoKHR pi = {.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, - .pNext = (c && c->has_nv_reflex) ? &pid_info : NULL, + .pNext = with_id ? &pid_info : NULL, .waitSemaphoreCount = 1, - .pWaitSemaphores = &g_render_done[g_flip_idx], .swapchainCount = 1, .pSwapchains = &g_sc, - .pImageIndices = &g_flip_idx}; + .pWaitSemaphores = &g_render_done[idx], .swapchainCount = 1, .pSwapchains = &g_sc, + .pImageIndices = &idx}; vk_queue_lock(); VkResult r = vkQueuePresentKHR(c->gfx_queue, &pi); vk_queue_unlock(); - if (c && c->has_nv_reflex) vk_reflex_marker(5, pid); // VK_LATENCY_MARKER_PRESENT_END_NV - if (c) latency_note(c, pid ? pid : g_flip_pid); + if (with_id) vk_reflex_marker(5, pid); // VK_LATENCY_MARKER_PRESENT_END_NV + if (c && !generated) latency_note(c, pid ? pid : frame_pid); vk_device_fault_report(r); if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1); + __atomic_add_fetch(&g_shown, 1, __ATOMIC_RELAXED); +} + +uint64_t gfx_presents_shown(void) { return (uint64_t)__atomic_load_n(&g_shown, __ATOMIC_RELAXED); } + +// Every blit left waiting goes out, in order (g_pl held): before a swapchain rebuild and before the next blit (but the +// pair's second); the device release drops them. +static void flip_locked(void) { + while (g_flip_n) flip_one(); } static void free_capture(void) { @@ -533,7 +555,10 @@ static int build_swapchain(void) { if (fmts[i].format == VK_FORMAT_B8G8R8A8_UNORM && fmts[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { sf = fmts[i]; found = 1; } for (uint32_t i = 0; i < nf && !found; i++) if (fmts[i].format == VK_FORMAT_R8G8B8A8_UNORM && fmts[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { sf = fmts[i]; found = 1; } - uint32_t count = caps.minImageCount + 1; + // Frame generation's pair holds two images acquired at once (the generated picture's blit waits while the game's + // frame is blitted): one image more, while frame_gen names a generator + int fg_images = vk_fg_engine() != VK_FG_OFF; + uint32_t count = caps.minImageCount + 1 + (fg_images ? 1u : 0u); if (caps.maxImageCount && count > caps.maxImageCount) count = caps.maxImageCount; if (count > 8) count = 8; @@ -584,6 +609,12 @@ static int build_swapchain(void) { } free_capture(); vk_queue_unlock(); + if (fg_images) { + static uint32_t said; + if (said != g_sc_n) + said = g_sc_n, rt_log("gfx: swapchain of %u images (minimum %u; one more for frame generation's pair)", g_sc_n, + caps.minImageCount); + } // The Reflex sleep mode belongs to the swapchain: a new one (resize, fullscreen, out of date) starts with low // latency off, so the mode is set on it again even though the setting did not change. g_reflex_mode = -1; @@ -800,12 +831,16 @@ void gfx_fit_rect(int cw, int ch, int dw, int dh, int out[4]) { static volatile int32_t g_hold; void gfx_present_hold(int on) { plat_xchg32(&g_hold, on != 0); } -static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int h, const int crop[4], int defer) { - int general = layout == VK_IMAGE_LAYOUT_GENERAL; +// pair: 0 a present of its own; 1 frame generation's generated picture, the first of a pair; 2 the game's frame after +// it, which leaves the first blit's present waiting in front of its own and draws the menu as that blit laid it out. +// Returns 1 when the blit was submitted. +static int present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int h, const int crop[4], int defer, + int pair) { + int general = layout == VK_IMAGE_LAYOUT_GENERAL, done = 0; gfx_window_start(); VkCtx *c = vk_ctx(); plat_rmutex_lock(&g_pl); - flip_locked(); + if (pair != 2) flip_locked(); if (plat_cas32(&g_hold, 0, 0)) goto out; if (!g_sc || plat_cas32(&g_dirty, 0, 0)) { if (!wsys_minimized() && !create_swapchain() && !g_sc) goto out; @@ -846,10 +881,16 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w MenuTarget mt = {c->instance, c->phys, c->device, c->gfx_family, c->gfx_queue, g_sc_hdr ? VK_FORMAT_R8G8B8A8_UNORM : g_sc_fmt, g_sc_ext.width, g_sc_ext.height, cw, ch, g_scaler}; - int overlay = g_sc_view[idx] && + int overlay; + if (pair == 2) { + overlay = g_sc_view[idx] && g_overlay_on; + } else { + overlay = g_sc_view[idx] && menu_overlay_frame(__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), &mt, &menu_shot); + g_overlay_on = overlay; + } int cap = (g_capture || menu_shot) && ensure_capture_buffer(); - int thumb = g_th_n && ensure_thumbs(); + int thumb = pair != 1 && g_th_n && ensure_thumbs(); // the flight recorder keeps the game's frames VkCommandBuffer cb = g_cb[slot]; vkResetCommandBuffer(cb, 0); @@ -962,9 +1003,11 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w r = vkQueueSubmit2(c->gfx_queue, 1, &si, g_fence[slot]); vk_queue_unlock(); if (r == VK_SUCCESS) { - g_flip_on = 1; - g_flip_idx = idx; - g_flip_pid = g_blit_pid; // the frame this blit shows (vk_reflex_present_frame) + if (g_flip_n == 2) flip_one(); // (not reached: every blit but a pair's second flushes the waiting ones) + g_flip_pid[g_flip_n] = g_blit_pid; // the frame this blit shows (vk_reflex_present_frame) + g_flip_gen[g_flip_n] = pair == 1; + g_flip_idx[g_flip_n++] = idx; + done = 1; if (!defer) flip_locked(); } else { vk_device_fault_report(r); @@ -981,19 +1024,30 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w } out: plat_rmutex_unlock(&g_pl); + return done; } void vk_present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int h, const int crop[4]) { - present_image(src, layout, fmt, w, h, crop, 0); + present_image(src, layout, fmt, w, h, crop, 0, 0); } void vk_present_blit(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int h, const int crop[4]) { - present_image(src, layout, fmt, w, h, crop, 1); + present_image(src, layout, fmt, w, h, crop, 1, 0); +} + +int vk_present_blit_pair(VkImage gen, VkImage src, VkFormat fmt, int w, int h, const int crop[4]) { + // without the first blit (no swapchain, minimised, held) the game's frame goes alone, as any present + if (!present_image(gen, VK_IMAGE_LAYOUT_GENERAL, VK_FORMAT_R8G8B8A8_UNORM, w, h, crop, 1, 1)) { + present_image(src, VK_IMAGE_LAYOUT_GENERAL, fmt, w, h, crop, 1, 0); + return 0; + } + present_image(src, VK_IMAGE_LAYOUT_GENERAL, fmt, w, h, crop, 1, 2); + return 1; } void vk_present_flip(void) { plat_rmutex_lock(&g_pl); - flip_locked(); + flip_one(); plat_rmutex_unlock(&g_pl); } @@ -1013,7 +1067,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 + g_flip_n = 0; // their blits are done (device idle); the swapchain goes away unpresented menu_vk_release(); vk_fsr_release(); vk_color_release(); diff --git a/runtime/vk.h b/runtime/vk.h index 25913c93..2ad113f9 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -143,6 +143,9 @@ void vk_present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, in // 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, VkFormat fmt, int w, int h, const int crop[4]); +// Frame generation's pair: the blit of gen (RGBA8, GENERAL, the size of src), then of src, both left waiting, oldest +// first; each vk_present_flip presents one. 0: gen was not blitted (src went alone, one flip). +int vk_present_blit_pair(VkImage gen, VkImage src, VkFormat fmt, int w, int h, const int crop[4]); // FSR 1 on the present (vk_fsr.c): records EASU (+ RCAS) of src's crop rectangle {x, y, w, h} into the rectangle out // {x, y, w, h} of dst, a swapchain image in TRANSFER_DST; src is in GENERAL and fmt its format. 0 = nothing recorded // (the setting says bilinear, no upscale is needed, or FSR is not available): the caller blits. diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index b34b2708..64b61ed2 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -11,12 +11,25 @@ // 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. // +// Frame generation (vk_fg.cpp, frame_gen = fsr3): a frame that comes with a generated picture (the one between the +// last frame and this) is presented as a pair, both blits submitted at once (present_pair). The display shows a +// picture when the GPU has drawn it, not when the CPU hands it over, and both pictures of a pair are drawn within a +// millisecond of each other: spaced by the CPU clock alone they went on screen together and then left a gap (Savage +// Wind on FIFO and G-SYNC). So each present waits on the timeline until the GPU is done: the generated picture once +// the frame is drawn (and not before the pacer's display time), the game's half the smoothed interval between +// generated pictures after it, as AMD's frame interpolation swapchain and Savage Wind's present_wait_turn do. The game +// draws at half the frame limit meanwhile (frame_rate.c frame_step_cap_ns, gfx_frame_gen_live). The spacing is ported +// from Savage Wind (https://github.com/Revivedx/SavageWindRecomp @ bf6146c14c37fee8781a601e5a544b93dff6d75e, MIT, +// Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai), +// runtime/nvn/vulkan/device.c present_wait_turn. +// // Asynchronous replay (TOTK_REPLAY_ASYNC, default 0; 1 turns it on): nvnQueueSubmitCommands // and nvnQueuePresentTexture only queue. Each item gets its timeline value at enqueue (vk_sync_reserve), in queue // order; the replay thread runs the synchronous submit and present bodies in that order, so every value is signalled // by the submission it was handed out for. The game runs at most one frame ahead: queueing present N waits until the // replay thread handled present N-1. Calls that read or free GPU state that queued work may use drain the queue first // (replay_drain), each cause counted. +#include #include #include #include @@ -34,7 +47,7 @@ #define WEAK __attribute__((weak)) -typedef struct { VkImage image; VkFormat format; int w, h, has_crop, crop[4]; uint64_t frame, at, queued, value, pid; } PresentJob; // pid: the frame's present ID (vk_reflex_frame) +typedef struct { VkImage image, gen; VkFormat format; int w, h, has_crop, crop[4]; uint64_t frame, at, queued, value, pid; } PresentJob; // pid: the frame's present ID (vk_reflex_frame); gen: its generated picture (vk_fg_present_gen) static PlatMutex pl = PLAT_MUTEX_INIT; static PlatCond pcv = PLAT_COND_INIT; // job queued, job taken, job submitted @@ -48,6 +61,114 @@ static void wait_until(uint64_t at) { while (kern_now_ns() < at) plat_yield(); } +// ---- Frame generation's presents (present thread only) +static volatile int32_t fg_live; // the last frame went out with a generated picture before it (gfx_frame_gen_live) +int gfx_frame_gen_live(void) { return __atomic_load_n(&fg_live, __ATOMIC_RELAXED); } + +// Present intervals in the world while frame generation is on, logged every FS_EVERY presents and over the run: all of them, from +// a generated picture to the game's frame after it, and from the game's frame to the next generated picture. +// Fixed-size histograms of 0.05 ms bins (bounded whatever the session's length). +#define FS_BINS 2000 +#define FS_BIN_MS 0.05 +#define FS_EVERY 2400 +enum { FS_ALL, FS_GEN_REAL, FS_REAL_GEN, FS_N }; +typedef struct { + uint32_t hist[FS_N][FS_BINS]; + double sum[FS_N], sq[FS_N]; + uint64_t n[FS_N]; + uint64_t pairs, alone, t0, t1; // frames presented as a pair and alone, the window's first and last present + double gpu_wait, late; // ms: waited for the GPU before the generated picture; the game's frame past its time +} FgStats; +static FgStats fs_win, fs_run; +static uint64_t fs_last, fs_last_gen; // the last present (0 after a gap) and the last generated one +static double fs_iv; // ns between generated pictures, smoothed (factor 0.1, Savage Wind's) + +static void fs_add(FgStats *s, int k, double ms) { + unsigned b = ms <= 0 ? 0 : ms / FS_BIN_MS >= FS_BINS ? FS_BINS - 1 : (unsigned)(ms / FS_BIN_MS); + s->hist[k][b]++, s->sum[k] += ms, s->sq[k] += ms * ms, s->n[k]++; +} + +static double fs_p99(const FgStats *s, int k) { + uint64_t want = s->n[k] - s->n[k] / 100, got = 0; + for (unsigned b = 0; b < FS_BINS; b++) + if ((got += s->hist[k][b]) >= want) return (b + 1) * FS_BIN_MS; + return FS_BINS * FS_BIN_MS; +} + +static void fs_log(const FgStats *s, const char *what) { + if (!s->n[FS_ALL] || s->t1 <= s->t0) return; + double wall = (s->t1 - s->t0) / 1e9, m[FS_N], sd[FS_N]; + for (int k = 0; k < FS_N; k++) { + m[k] = s->n[k] ? s->sum[k] / s->n[k] : 0; + double v = s->n[k] ? s->sq[k] / s->n[k] - m[k] * m[k] : 0; + sd[k] = v > 0 ? sqrt(v) : 0; + } + rt_log("fg present %s: %.1f fps shown, %.1f drawn (%llu frames as pairs, %llu alone) over %.1f s; intervals mean %.3f " + "sd %.3f p99 %.2f ms; generated to game's %.3f sd %.3f, game's to generated %.3f sd %.3f; waited for the GPU " + "%.3f ms a pair, game's frame late %.3f ms a pair (means)", what, (s->n[FS_ALL] + 1) / wall, + (s->pairs + s->alone) / wall, (unsigned long long)s->pairs, (unsigned long long)s->alone, wall, m[FS_ALL], + sd[FS_ALL], fs_p99(s, FS_ALL), m[FS_GEN_REAL], sd[FS_GEN_REAL], m[FS_REAL_GEN], sd[FS_REAL_GEN], + s->pairs ? s->gpu_wait / s->pairs : 0, s->pairs ? s->late / s->pairs : 0); +} + +// A present at now of kind k (FS_GEN_REAL: the game's frame after a generated picture; FS_REAL_GEN: a generated +// picture; FS_ALL: a frame alone) +static void fs_present(uint64_t now, int k) { + if (!vk_fg_active()) return; + if (frame_step_world_us() <= 0) { // (the title screen and the first load: not counted) + fs_last = 0; + return; + } + FgStats *both[2] = {&fs_win, &fs_run}; + for (int i = 0; i < 2; i++) { + FgStats *s = both[i]; + if (!s->t0) s->t0 = now; + if (fs_last && now - fs_last < 100000000ull) { // (a gap: a load, a menu's hitch) + double ms = (now - fs_last) / 1e6; + fs_add(s, FS_ALL, ms); + if (k != FS_ALL) fs_add(s, k, ms); + } + s->t1 = now; + s->pairs += k == FS_GEN_REAL, s->alone += k == FS_ALL; + } + fs_last = now; + if (fs_win.n[FS_ALL] >= FS_EVERY) { + fs_log(&fs_win, "last presents"); + memset(&fs_win, 0, sizeof fs_win); + } +} + +static void gpu_wait(uint64_t value) { + VkCtx *c = vk_ctx(); + VkSemaphoreWaitInfo wi = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO, .semaphoreCount = 1, + .pSemaphores = &c->timeline, .pValues = &value}; + vkWaitSemaphores(c->device, &wi, 1000000000ull); // (drawn; a second at most) +} + +// The pair's two presents (both blits submitted): the generated picture once the GPU has drawn the frame (gpu: its +// last submission's value) and the pacer's time has come, the game's frame once its blit is drawn (f) and half the +// smoothed interval between generated pictures has passed. +static void present_pair(const PresentJob *j, uint64_t gpu, uint64_t f) { + uint64_t t0 = kern_now_ns(); + gpu_wait(gpu); + uint64_t t1 = kern_now_ns(); + wait_until(j->at); + uint64_t now = kern_now_ns(); + double dt = fs_last_gen ? (double)(now - fs_last_gen) : 0; + if (dt > 0 && dt < 1e8) fs_iv = fs_iv > 0 ? fs_iv + (dt - fs_iv) * 0.1 : dt; + fs_last_gen = now; + fs_present(now, FS_REAL_GEN); + vk_present_flip(); + gpu_wait(f); + uint64_t at = now + (uint64_t)(fs_iv / 2); + if (fs_iv > 0) wait_until(at); + uint64_t t2 = kern_now_ns(); + fs_present(t2, FS_GEN_REAL); + vk_present_flip(); + fs_win.gpu_wait += (t1 - t0) / 1e6, fs_run.gpu_wait += (t1 - t0) / 1e6; + if (fs_iv > 0 && t2 > at) fs_win.late += (t2 - at) / 1e6, fs_run.late += (t2 - at) / 1e6; +} + static void present_main(void *arg) { (void)arg; perf_core_thread(PCORE_PRESENT); @@ -63,7 +184,10 @@ static void present_main(void *arg) { if (flight_on) vk_thumbs_frame(j.frame); vk_reflex_present_frame(j.pid); // the present of this blit, and the submission after it, are the frame's vk_reflex_submit_frame(j.pid); - vk_present_blit(j.image, VK_IMAGE_LAYOUT_GENERAL, j.format, j.w, j.h, j.has_crop ? j.crop : NULL); + // the frame's last submission, its interpolation included: no submission goes out while busy + uint64_t drawn = j.gen ? vk_sync_issued() : 0; + int pair = j.gen && vk_present_blit_pair(j.gen, j.image, j.format, j.w, j.h, j.has_crop ? j.crop : NULL); + if (!j.gen) vk_present_blit(j.image, VK_IMAGE_LAYOUT_GENERAL, j.format, j.w, j.h, j.has_crop ? j.crop : NULL); uint64_t f = vk_sync_submit(NULL, 0); // a timeline value after the blit: deferred destruction waits for the blit too if (j.value && f != j.value) rt_fatal("vk: the present of frame %llu signalled %llu, queued as %llu", (unsigned long long)j.frame, @@ -73,9 +197,15 @@ static void present_main(void *arg) { plat_cond_broadcast(&pcv); plat_mutex_unlock(&pl); uint64_t t0 = kern_now_ns(); - wait_until(j.at); + if (pair) { + present_pair(&j, drawn, f); + } else { + wait_until(j.at); + fs_last_gen = 0; // (the next generated picture starts the spacing anew) + fs_present(kern_now_ns(), FS_ALL); + } uint64_t t1 = kern_now_ns(); - vk_present_flip(); + if (!pair) 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) @@ -126,6 +256,9 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin .at = at_ns, .queued = queued, .value = value, .pid = pid}; vk_hdr_present_source(img, &j.image, &j.format); // TOTK_HDR: the float picture the game copied into it, same size vk_fg_present_swap(&j.image, &j.format); // TOTK_FG_TEST (frame generation checks): another picture in its place + j.gen = vk_fg_present_gen(j.image); + if (__atomic_load_n(&fg_live, __ATOMIC_RELAXED) != (j.gen != VK_NULL_HANDLE)) + __atomic_store_n(&fg_live, j.gen != VK_NULL_HANDLE, __ATOMIC_RELAXED); if (crop) { j.has_crop = 1; for (int i = 0; i < 4; i++) j.crop[i] = crop[i]; @@ -341,6 +474,8 @@ void gfx_exit_log(void) { static int32_t done; rec_check_log(); vk_fg_exit_log(); + static int32_t fs_done; + if (!plat_xchg32(&fs_done, 1)) fs_log(&fs_run, "over the run"); 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_fg.cpp b/runtime/vk_fg.cpp index 69d043d7..b28d56cb 100644 --- a/runtime/vk_fg.cpp +++ b/runtime/vk_fg.cpp @@ -19,7 +19,8 @@ // 3. vk_fg_generate at the end of the submission that drew the window texture: the optical flow over the HUD-less // picture, then the interpolation between the last frame and this one, with this frame's window texture as the // picture with the HUD, into an RGBA8 image holding bytes as the window texture does (sRGB transfer function). -// Presenting it is not done here (TOTK_FG_TEST shows it in place of the frame, for checks only). +// The present (vk_backend.c present_pair) shows it before the frame, through vk_fg_present_gen (TOTK_FG_TEST shows it, or +// the picture before the HUD, in place of the frame instead, for checks only). #include "vk_fg.h" #include @@ -110,7 +111,8 @@ static struct { float near_m, far_m, fov_y, dt_ms; VkFormat win_fmt; // the window texture's format as last seen (UNORM twin) int k; // the pair of hudless/out images this frame writes - VkImage show_out, show_hl; // the frame just ended (vk_fg_frame), for vk_fg_present_swap + VkImage gen_win; // the window texture this frame's picture was generated against (vk_fg_generate) + VkImage show_out, show_hl, show_win; // the frame just ended (vk_fg_frame), for vk_fg_present_swap / _gen // images uint32_t iw, ih; Img hl_srgb, hl[2], out[2]; @@ -723,6 +725,7 @@ extern "C" int vk_fg_generate(VkCommandBuffer cb, uint64_t fence, uint64_t frame return 0; } g.gen_ok = true; + g.gen_win = win; g.gens++, g.resets += g.reset; if (g.gens == 1) rt_log("fg: first frame generated: %ux%u from a %ux%u scene (near %g m, far %g m, vertical field of view %.1f " @@ -745,6 +748,7 @@ extern "C" void vk_fg_frame(void) { if (!g.gen_ok) g.skipped++; } g.show_out = g.gen_ok ? g.out[g.k].img : VK_NULL_HANDLE; + g.show_win = g.gen_ok ? g.gen_win : VK_NULL_HANDLE; g.show_hl = g.hl_ok ? g.hl[g.k].img : VK_NULL_HANDLE; if (g.hl_ok) g.k ^= 1; g.chain = g.gen_ok; // a frame without a generated picture breaks the chain: the next one starts over @@ -779,6 +783,11 @@ extern "C" void vk_fg_exit_log(void) { (g.prep_run + g.prep_ns) / 1e6 / g.ncpu); } +extern "C" VkImage vk_fg_present_gen(VkImage presented) { + if (g.test || !g.show_out || presented != g.show_win) return VK_NULL_HANDLE; + return g.show_out; +} + extern "C" int vk_fg_present_swap(VkImage *img, VkFormat *fmt) { VkImage s = g.test == 1 ? g.show_out : g.test == 2 ? g.show_hl : VK_NULL_HANDLE; if (!s) return 0; diff --git a/runtime/vk_fg.h b/runtime/vk_fg.h index 358f0ac6..a9fbc0a4 100644 --- a/runtime/vk_fg.h +++ b/runtime/vk_fg.h @@ -73,6 +73,11 @@ void vk_fg_exit_log(void); // vk_backend.c gfx_exit_log: the run's counts and // vk_backend.c present_image (testing, TOTK_FG_TEST): 1 = the frame's generated picture, 2 = its picture before the // HUD in place of the window texture (*img, *fmt). 0: the present is left as it is. int vk_fg_present_swap(VkImage *img, VkFormat *fmt); +// vk_backend.c present_image, after vk_dlaa_frame: the picture generated between the last frame and the one just +// ended, when presented is the window texture it was generated against (RGBA8 holding the window texture's bytes, +// GENERAL, its size), else VK_NULL_HANDLE (none made, a test swap, or another texture presented). It stays unwritten +// until the replay of the frame after next: its blit is on the queue before the next frame's submissions. +VkImage vk_fg_present_gen(VkImage presented); #ifdef __cplusplus }