From 7c11ff681687083af16e811b166b95664dcba5df Mon Sep 17 00:00:00 2001 From: chrg6 Date: Sat, 7 Nov 2026 12:25:25 -0500 Subject: [PATCH] Add AMD FSR 3.1 frame generation: a generated frame between every two the game draws. With Frame generation set to AMD FSR 3.1 (after a restart), AMD's frame generation swapchain takes the place of the present path's own: the device creates two more graphics queues for its presents and image acquires, the present path acquires, gets images and presents through its replacement functions, and every frame configures generation and records its preparation from the depth and camera motion vectors the DLSS or FSR 3.1 upscaling pass made (one of them must be on). Generation pauses while the menu is open. On an RTX 5090 at a 60 fps game, about one frame was generated for every frame drawn in play. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 3 +- runtime/gfx_present.c | 37 +++++-- runtime/ui/menu.cpp | 11 +++ runtime/ui_text.c | 7 ++ runtime/ui_text.h | 3 +- runtime/vk_device.c | 20 +++- runtime/vk_dlaa.c | 14 +++ runtime/vk_dlaa.h | 7 +- runtime/vk_ffx.cpp | 14 +++ runtime/vk_ffx.h | 12 +++ runtime/vk_fg.cpp | 219 ++++++++++++++++++++++++++++++++++++++++++ runtime/vk_fg.h | 20 ++++ 12 files changed, 352 insertions(+), 15 deletions(-) create mode 100644 runtime/vk_fg.cpp create mode 100644 runtime/vk_fg.h diff --git a/runtime/cfg.c b/runtime/cfg.c index bdf64b7e..208c446f 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -41,7 +41,7 @@ static const char *const known[] = { "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "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", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", - "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FREEZE_IDLE", "FREE_CORES", + "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", @@ -119,6 +119,7 @@ static const CfgMeta metas[] = { {"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}, {"TAA", "dlss", "dlss|fsr3", RESTART}, // vk_dlaa.c fsr3 + {"FRAMEGEN", "off", "off|fsr3", RESTART}, // vk_fg.cpp {"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"FXAA", "1", "0|1", LIVE}, {"GAME_ASPECT", "16:9", "16:9|window|21:9|32:9", RESTART}, // widescreen.c {"GAME_RES", "1440", "1440|1800|2160|2880|window", RESTART}, // widescreen.c: the picture's height diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index f2b02a7e..68420af4 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -24,6 +24,7 @@ #include "platform/platform.h" #include "ui/menu.h" #include "vk.h" +#include "vk_fg.h" #define FRAMES 2 #define MAX_IMAGES 16 @@ -289,7 +290,7 @@ static void flip_locked(void) { .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); + VkResult r = vk_fg_active() ? vk_fg_present(&pi) : vkQueuePresentKHR(vk_ctx()->gfx_queue, &pi); vk_queue_unlock(); vk_device_fault_report(r); if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1); @@ -343,7 +344,17 @@ static int create_swapchain(void) { .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE, .preTransform = caps.currentTransform, .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, .presentMode = pick_present_mode(c->phys), .clipped = VK_TRUE, .oldSwapchain = old}; - VkResult r = vkCreateSwapchainKHR(c->device, &ci, NULL, &g_sc); + // Frame generation (vk_fg.cpp): AMD's swapchain in this one's place. Its swapchain is replaced only after the old + // one is gone; a first creation that fails leaves the plain swapchain below. + VkResult r = VK_ERROR_FEATURE_NOT_PRESENT; + if (vk_fg_wanted()) { + if (old && vk_fg_active()) vk_fg_destroy_swapchain(old); + else if (old) vkDestroySwapchainKHR(c->device, old, NULL); + ci.oldSwapchain = VK_NULL_HANDLE; + old = g_sc = VK_NULL_HANDLE; + r = vk_fg_create_swapchain(&ci, &g_sc); + } + if (r != VK_SUCCESS) r = vkCreateSwapchainKHR(c->device, &ci, NULL, &g_sc); if (r != VK_SUCCESS) { rt_log("gfx: vkCreateSwapchainKHR failed (%d)", (int)r); g_sc = old; @@ -359,10 +370,16 @@ static int create_swapchain(void) { g_sc_fmt = sf.format; g_sc_hdr = hdr; vk_hdr_set_active(hdr); - if (hdr) vk_hdr_metadata(g_sc, 1); + if (hdr && !vk_fg_active()) vk_hdr_metadata(g_sc, 1); g_sc_ext = ext; g_sc_n = MAX_IMAGES; - vkGetSwapchainImagesKHR(c->device, g_sc, &g_sc_n, g_sc_img); + if (vk_fg_active()) { // AMD's swapchain: the count first (a larger capacity is not cut down to it) + uint32_t n = 0; + vk_fg_get_images(g_sc, &n, NULL); + g_sc_n = n < MAX_IMAGES ? n : MAX_IMAGES; + memset(g_sc_img, 0, sizeof g_sc_img); + vk_fg_get_images(g_sc, &g_sc_n, g_sc_img); + } else vkGetSwapchainImagesKHR(c->device, g_sc, &g_sc_n, g_sc_img); VkSemaphoreCreateInfo semi = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; for (uint32_t i = 0; i < g_sc_n; i++) { vkCreateSemaphore(c->device, &semi, NULL, &g_render_done[i]); @@ -730,17 +747,19 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w goto out; } uint32_t idx = 0; - VkResult r = vkAcquireNextImageKHR(c->device, g_sc, 1000000000ull, g_acquire[slot], VK_NULL_HANDLE, &idx); + VkResult r = vk_fg_active() ? vk_fg_acquire(g_sc, 1000000000ull, g_acquire[slot], &idx) + : vkAcquireNextImageKHR(c->device, g_sc, 1000000000ull, g_acquire[slot], VK_NULL_HANDLE, &idx); if (r == VK_ERROR_OUT_OF_DATE_KHR) { plat_xchg32(&g_dirty, 1); if (!create_swapchain()) goto out; - r = vkAcquireNextImageKHR(c->device, g_sc, 1000000000ull, g_acquire[slot], VK_NULL_HANDLE, &idx); + r = vk_fg_active() ? vk_fg_acquire(g_sc, 1000000000ull, g_acquire[slot], &idx) + : vkAcquireNextImageKHR(c->device, g_sc, 1000000000ull, g_acquire[slot], VK_NULL_HANDLE, &idx); } vk_device_fault_report(r); if (r != VK_SUCCESS && r != VK_SUBOPTIMAL_KHR) goto out; // lost, not ready: skip the frame if (r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1); vkResetFences(c->device, 1, &g_fence[slot]); - if (g_sc_hdr) vk_hdr_metadata(g_sc, 0); // a changed peak or white goes to the display + if (g_sc_hdr && !vk_fg_active()) vk_hdr_metadata(g_sc, 0); // a changed peak or white goes to the display if (g_th_pending[slot]) { // that fence has signalled: the thumbnail its submission wrote is complete g_th_tag[g_th_pending[slot] % (uint64_t)g_th_n] = g_th_pending[slot]; g_th_pending[slot] = 0; @@ -882,6 +901,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w last_write = 0; record_thumb(cb, dst, slot); } + if (src) vk_fg_frame(cb, g_sc, g_sc_fmt, g_sc_ext); // frame generation: this frame's preparation (vk_fg.cpp) sync_barrier(cb, dst, last, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, last_stage, last_write, VK_PIPELINE_STAGE_2_NONE, 0); vk_crumb(cb, VK_CRUMB_PRESENT_END, crumb); vkEndCommandBuffer(cb); @@ -970,7 +990,8 @@ void gfx_window_release_vk(void) { g_sc_view[i] = VK_NULL_HANDLE; } if (g_pool) vkDestroyCommandPool(c->device, g_pool, NULL); - if (g_sc) vkDestroySwapchainKHR(c->device, g_sc, NULL); + if (g_sc && vk_fg_active()) vk_fg_destroy_swapchain(g_sc), vk_fg_release(); + else if (g_sc) vkDestroySwapchainKHR(c->device, g_sc, NULL); if (g_surface) vkDestroySurfaceKHR(c->instance, g_surface, NULL); g_sc = VK_NULL_HANDLE; g_surface = VK_NULL_HANDLE; g_pool = VK_NULL_HANDLE; g_sc_n = 0; g_started = 0; // the window and its thread stay; the next gfx_window_start makes a new surface diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 72236845..db0211d5 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -757,6 +757,16 @@ static void row_upscaling(void) { if (nv >= 2) opt_apply("NX2NAVI_TAA", nv >= 7 ? "fsr3" : "dlss"); } +// Frame generation (vk_fg.cpp), after a restart; it needs a temporal upscaler's motion vectors +static void row_framegen(void) { + static const char *const values[] = {"off", "fsr3"}; + const char *names[] = {menu_text(UI_MENU_FRAMEGEN_OFF), "AMD FSR 3.1"}; + int cur = strcmp(cfg_value("NX2NAVI_FRAMEGEN"), "fsr3") ? 0 : 1; + int nv = opt_choice_index("NX2NAVI_FRAMEGEN", UI_MENU_FRAMEGEN, names, 2, cur, 1, UI_MENU_FRAMEGEN_DESC, + menu_text(UI_MENU_FRAMEGEN_NOTE)); + if (nv >= 0) opt_apply("NX2NAVI_FRAMEGEN", values[nv]); +} + // The picture's height (widescreen.c picture_height): the game's 1440 or more. static void row_game_res(void) { static const char *const values[] = {"1440", "1800", "2160", "2880", "window"}; @@ -783,6 +793,7 @@ static void tab_screen(float scale) { if (wide_available()) opt_choice("NX2NAVI_GAME_ASPECT", UI_MENU_GAME_ASPECT, wide, 4, UI_MENU_GAME_ASPECT_DESC); if (wide_available()) row_game_res(); row_upscaling(); + row_framegen(); // RCAS runs only under FSR (vk_fsr.c vk_fsr_record): with bilinear or DLSS the slider would do nothing if (strcmp(cfg_value("NX2NAVI_UPSCALER"), "fsr") || cfg_bool("NX2NAVI_DLAA")) opt_disable_next(UI_MENU_SHARPNESS_NEEDS_FSR); opt_slider("NX2NAVI_SHARPNESS", UI_MENU_SHARPNESS, 0, 100, UI_MENU_SHARPNESS_DESC, 5, "%.0f %%"); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 8ca4ce79..ffc7a816 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -513,6 +513,13 @@ static const char *const text_en[UI_TEXT_COUNT] = { "Turning DLSS or FSR 3.1 on or off, or changing between them, takes effect after a " "restart; the mode changes at once.", [UI_MENU_UPSCALING_DLSS_NOTE] = "DLSS or FSR 3.1 on or off: after restart", + [UI_MENU_FRAMEGEN] = "Frame generation", + [UI_MENU_FRAMEGEN_DESC] = "Shows a generated frame between every two the game draws, for about twice the frame rate " + "on a fast display. It needs DLSS or FSR 3.1 under Upscaling (any mode), whose motion " + "vectors it uses. AMD FSR 3.1 frame generation runs on any GPU (amd_fidelityfx_vk.dll next " + "to the game's .exe). It pauses while this menu is open. Takes effect after a restart.", + [UI_MENU_FRAMEGEN_OFF] = "Off", + [UI_MENU_FRAMEGEN_NOTE] = "after restart; needs DLSS or FSR 3.1", [UI_MENU_GAME_RES] = "Resolution", [UI_MENU_GAME_RES_DESC] = "The height of the game's picture: 1440p is the game's own; higher draws a sharper picture " "and its scene in proportion, at a matching cost on the graphics card. Native: the " diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 27001345..ef6bf247 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -243,7 +243,8 @@ typedef enum { UI_BOOT_SHADERS, UI_BOOT_PIPELINES, // printf: "%lld" done, "%lld" total // the settings menu's Picture rows for this game (ui/menu.cpp tab_screen): one upscaling choice (bilinear, FSR 1 // or a DLSS mode) and the picture's resolution - UI_MENU_UPSCALING, UI_MENU_UPSCALING_DESC, UI_MENU_UPSCALING_DLSS_NOTE, + UI_MENU_UPSCALING, UI_MENU_UPSCALING_DESC, UI_MENU_UPSCALING_DLSS_NOTE, UI_MENU_FRAMEGEN, UI_MENU_FRAMEGEN_DESC, + UI_MENU_FRAMEGEN_OFF, UI_MENU_FRAMEGEN_NOTE, UI_MENU_GAME_RES, UI_MENU_GAME_RES_DESC, UI_MENU_GAME_RES_NATIVE, UI_TEXT_COUNT } UiTextId; diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 5db2d912..2fafa763 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -10,6 +10,9 @@ #include "vk.h" #include "platform/platform.h" #include "vk_dlaa.h" +#include "vk_fg.h" + +static uint32_t g_gfx_queues = 1; // queues the graphics family offers (pick_device) static VkCtx g_vk; static int g_ready; @@ -184,6 +187,7 @@ static int pick_device(void) { VkQueueFamilyProperties *qp = calloc(nq ? nq : 1, sizeof *qp); vkGetPhysicalDeviceQueueFamilyProperties(g_vk.phys, &nq, qp); g_vk.gfx_family = find_gfx_family(g_vk.phys, qp, nq); + g_gfx_queues = qp[g_vk.gfx_family].queueCount; g_vk.xfer_family = g_vk.gfx_family; for (uint32_t i = 0; i < nq; i++) // a family that does transfer only (async DMA engine) if ((qp[i].queueFlags & VK_QUEUE_TRANSFER_BIT) && !(qp[i].queueFlags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT))) { @@ -340,14 +344,16 @@ static int create_device(void) { has_fbl && c->fbl.attachmentFeedbackLoopLayout, has_lr && c->lr.dynamicRenderingLocalRead); } - float prio = 1.0f; + // Frame generation (vk_fg.cpp) presents on a second queue of the graphics family and acquires on a third + static const float prio[3] = {1.0f, 1.0f, 1.0f}; + uint32_t nq_gfx = vk_fg_queue_count() < g_gfx_queues ? vk_fg_queue_count() : g_gfx_queues; VkDeviceQueueCreateInfo qi[2] = { - {.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueFamilyIndex = g_vk.gfx_family, .queueCount = 1, - .pQueuePriorities = &prio}}; + {.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueFamilyIndex = g_vk.gfx_family, .queueCount = nq_gfx, + .pQueuePriorities = prio}}; uint32_t nqi = 1; if (g_vk.xfer_family != g_vk.gfx_family) { qi[1] = (VkDeviceQueueCreateInfo){.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, - .queueFamilyIndex = g_vk.xfer_family, .queueCount = 1, .pQueuePriorities = &prio}; + .queueFamilyIndex = g_vk.xfer_family, .queueCount = 1, .pQueuePriorities = prio}; nqi = 2; } VkDeviceCreateInfo di = {.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .pNext = &c->f2, .queueCreateInfoCount = nqi, @@ -356,6 +362,12 @@ static int create_device(void) { free(c); if (r != VK_SUCCESS) { rt_log("vk: vkCreateDevice failed (%d)", (int)r); return 0; } vkGetDeviceQueue(g_vk.device, g_vk.gfx_family, 0, &g_vk.gfx_queue); + if (nq_gfx >= 2) { + VkQueue present, acquire; + vkGetDeviceQueue(g_vk.device, g_vk.gfx_family, 1, &present); + vkGetDeviceQueue(g_vk.device, g_vk.gfx_family, nq_gfx - 1, &acquire); + vk_fg_queues(present, acquire); + } if (g_vk.xfer_family != g_vk.gfx_family) vkGetDeviceQueue(g_vk.device, g_vk.xfer_family, 0, &g_vk.xfer_queue); else { g_vk.xfer_queue = g_vk.gfx_queue; g_vk.xfer_shared = 1; } diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index b3c96865..1a4b7611 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -354,8 +354,18 @@ static struct { int ids_ok; unsigned cfg_seen; // cfg_version when preset was read uint64_t last_eval_ns; // plat_now_ns of the previous evaluation (FrameTimeDeltaInMsec) + VkFfxImage fg_depth, fg_mv; // the last evaluation's depth and motion vectors, for frame generation + float fg_jx, fg_jy; + int fg_fresh, fg_reset; // fresh: not handed out yet (vk_dlaa_fg_inputs) } D = {.mv_ok = -1, .cfg_seen = ~0u}; +int vk_dlaa_fg_inputs(VkFfxImage *depth, VkFfxImage *mv, float *jx, float *jy, int *reset) { + if (!D.fg_fresh || D.failed) return 0; + D.fg_fresh = 0; + *depth = D.fg_depth, *mv = D.fg_mv, *jx = D.fg_jx, *jy = D.fg_jy, *reset = 0; + return 1; +} + const VkTexImage *vk_dlaa_swap_img; VkImageView vk_dlaa_swap_view; @@ -1172,6 +1182,10 @@ evaluated: ex->recorded += 4; vk_exec_draw_new_cb(); // NGX bound pipelines and descriptor sets of its own: the next draw binds everything again D.jdepth = z->image; + // for frame generation at this frame's present (vk_dlaa_fg_inputs) + D.fg_depth = (VkFfxImage){z->image, z->format, w, h, 0}; + D.fg_mv = (VkFfxImage){e->mv, VK_FORMAT_R16G16_SFLOAT, w, h, 1}; + D.fg_jx = jx, D.fg_jy = jy, D.fg_fresh = 1; next_jitter(w, ow); 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 frames without the DLAA point, %llu with camera " diff --git a/runtime/vk_dlaa.h b/runtime/vk_dlaa.h index 064f2e70..a120a81b 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -1,13 +1,18 @@ // DLAA (vk_dlaa.c): NVIDIA DLSS at the scene's own size, behind NX2NAVI_DLAA (nx2navi.ini dlaa = 1). #pragma once #include "vk.h" +#include "vk_ffx.h" // The narrowest scene the DLSS pass and the motion passes recognise by its D32F depth: Ultra Performance at a 1080p // output renders 640 wide, smaller windows less. #define DLAA_SCENE_MIN_W 320u int vk_dlaa_wanted(void); // the setting is on (the device may still lack DLSS) -int vk_dlaa_fsr3(void); // ... and its temporal upscaler is AMD FSR 3.1 (NX2NAVI_TAA = fsr3) instead of DLSS +int vk_dlaa_fsr3(void); +// Frame generation (vk_fg.cpp): the depth and motion vectors of the frame DLSS or FSR 3.1 last ran on, with its jitter +// and whether its history reset; 1 once per evaluation (0: none since the last call). The images stay valid until the +// next frame's scene draws over them, after this present. +int vk_dlaa_fg_inputs(VkFfxImage *depth, VkFfxImage *mv, float *jx, float *jy, int *reset); // ... and its temporal upscaler is AMD FSR 3.1 (NX2NAVI_TAA = fsr3) instead of DLSS // vk_device.c: NGX's instance and device extensions join the lists (room for max names); vk_dlaa_init after the // device exists brings NGX up and checks that DLSS is available. void vk_dlaa_instance_exts(const char **ext, uint32_t *n, uint32_t max); diff --git a/runtime/vk_ffx.cpp b/runtime/vk_ffx.cpp index fd1666cc..0c44e1ae 100644 --- a/runtime/vk_ffx.cpp +++ b/runtime/vk_ffx.cpp @@ -164,6 +164,20 @@ extern "C" int vk_ffx_upscale(VkCommandBuffer cb, const VkFfxImage *color, const return 1; } +extern "C" ffxReturnCode_t vk_ffx_create(ffxContext *ctx, ffxCreateContextDescHeader *desc) { + return vk_ffx_load() ? A.create(ctx, desc, nullptr) : FFX_API_RETURN_ERROR; +} +extern "C" ffxReturnCode_t vk_ffx_destroy(ffxContext *ctx) { return A.ok ? A.destroy(ctx, nullptr) : FFX_API_RETURN_ERROR; } +extern "C" ffxReturnCode_t vk_ffx_configure(ffxContext *ctx, const ffxConfigureDescHeader *desc) { + return A.ok ? A.configure(ctx, desc) : FFX_API_RETURN_ERROR; +} +extern "C" ffxReturnCode_t vk_ffx_query(ffxContext *ctx, ffxQueryDescHeader *desc) { + return A.ok ? A.query(ctx, desc) : FFX_API_RETURN_ERROR; +} +extern "C" ffxReturnCode_t vk_ffx_dispatch(ffxContext *ctx, const ffxDispatchDescHeader *desc) { + return A.ok ? A.dispatch(ctx, desc) : FFX_API_RETURN_ERROR; +} + extern "C" void vk_ffx_release(void) { if (!A.ok) return; for (Up &u : U) diff --git a/runtime/vk_ffx.h b/runtime/vk_ffx.h index e91bda5e..f1f640f1 100644 --- a/runtime/vk_ffx.h +++ b/runtime/vk_ffx.h @@ -16,3 +16,15 @@ int vk_ffx_load(void); // 1 when the library is there with its entry points (l int vk_ffx_upscale(VkCommandBuffer cb, const VkFfxImage *color, const VkFfxImage *depth, const VkFfxImage *mv, const VkFfxImage *out, float jx, float jy, int reset, float dt_ms, float fov_y); void vk_ffx_release(void); // before the device goes away + +#ifdef __cplusplus +// The API's calls themselves, for vk_fg.cpp (0 = FFX_API_RETURN_OK): the library loaded first (vk_ffx_load). +#include "third_party/ffx_api/ffx_api.h" +extern "C" { +ffxReturnCode_t vk_ffx_create(ffxContext *ctx, ffxCreateContextDescHeader *desc); +ffxReturnCode_t vk_ffx_destroy(ffxContext *ctx); +ffxReturnCode_t vk_ffx_configure(ffxContext *ctx, const ffxConfigureDescHeader *desc); +ffxReturnCode_t vk_ffx_query(ffxContext *ctx, ffxQueryDescHeader *desc); +ffxReturnCode_t vk_ffx_dispatch(ffxContext *ctx, const ffxDispatchDescHeader *desc); +} +#endif diff --git a/runtime/vk_fg.cpp b/runtime/vk_fg.cpp new file mode 100644 index 00000000..07cfdd68 --- /dev/null +++ b/runtime/vk_fg.cpp @@ -0,0 +1,219 @@ +// Frame generation with AMD FSR 3.1 (NX2NAVI_FRAMEGEN = fsr3; applies after a restart): AMD's frame generation +// swapchain (amd_fidelityfx_vk.dll, vk_ffx.cpp loads it) takes the place of gfx_present.c's swapchain. Every frame +// the game presents, the swapchain presents a generated frame between it and the one before, on its own present +// queue and pacing threads, so the screen shows twice the game's frame rate. +// +// The generated frame is built from the presented picture and the scene's depth and camera motion vectors, which +// vk_dlaa.c makes for DLSS and FSR 3.1 upscaling: frame generation needs one of those on (DLAA or Native AA included). +// The HUD is interpolated with the picture (it hardly moves); the menu is not, since generation pauses while it is +// open. The device creates two more queues of the graphics family for it (vk_device.c, vk_fg_queue_count). +#ifdef _WIN32 +#include +#endif +#include +#include +#include +#include + +#include "third_party/ffx_api/ffx_api.h" +#include "third_party/ffx_api/ffx_framegeneration.h" +#include "third_party/ffx_api/vk/ffx_api_vk.h" + +extern "C" { +#include "cfg.h" +#include "host.h" +#include "platform/platform.h" +#include "vk.h" +#include "vk_dlaa.h" +#include "vk_ffx.h" +#include "vk_fg.h" +#include "ui/menu.h" +} + +namespace { + +struct { + int wanted = -1; + VkQueue present = VK_NULL_HANDLE, acquire = VK_NULL_HANDLE; + ffxContext sc = nullptr; // the swapchain context + ffxContext fg = nullptr; // the frame generation context + VkExtent2D fg_ext = {0, 0}; + VkFormat fg_fmt = VK_FORMAT_UNDEFINED; + ffxQueryDescSwapchainReplacementFunctionsVK fn = {}; + uint64_t frame = 0; + int failed = 0; + uint64_t generated = 0, paused = 0, no_inputs = 0; +} F; + +VkResult submit_game(uint32_t n, const VkSubmitInfo *si, VkFence fence) { + vk_queue_lock(); // the game's queue is shared with the recorder and the present path + VkResult r = vkQueueSubmit(vk_ctx()->gfx_queue, n, si, fence); + vk_queue_unlock(); + return r; +} + +ffxReturnCode_t generate(ffxDispatchDescFrameGeneration *d, void *user) { + F.generated++; + return vk_ffx_dispatch((ffxContext *)user, &d->header); +} + +FfxApiResource image(const VkFfxImage *i) { + VkImageCreateInfo ci = {}; + ci.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; + ci.imageType = VK_IMAGE_TYPE_2D; + ci.format = i->format; + ci.extent = {i->width, i->height, 1}; + ci.mipLevels = 1; + ci.arrayLayers = 1; + return ffxApiGetResourceVK((void *)i->image, ffxApiGetImageResourceDescriptionVK(i->image, ci, 0), + FFX_API_RESOURCE_STATE_UNORDERED_ACCESS); +} + +void fail(const char *what, unsigned r) { + rt_log("fg: %s failed (%u): frame generation off for this run", what, r); + F.failed = 1; +} + +} // namespace + +extern "C" int vk_fg_wanted(void) { + if (F.wanted < 0) { + F.wanted = !strcmp(cfg_value("NX2NAVI_FRAMEGEN"), "fsr3"); + if (F.wanted && !vk_dlaa_wanted()) { + rt_log("fg: frame generation needs DLSS or FSR 3.1 upscaling (any mode): off"); + F.wanted = 0; + } + } + return F.wanted; +} + +extern "C" uint32_t vk_fg_queue_count(void) { return vk_fg_wanted() ? 3 : 1; } + +extern "C" void vk_fg_queues(VkQueue present, VkQueue acquire) { F.present = present, F.acquire = acquire; } + +extern "C" int vk_fg_active(void) { return F.sc != nullptr && !F.failed; } + +extern "C" VkResult vk_fg_create_swapchain(const VkSwapchainCreateInfoKHR *ci, VkSwapchainKHR *sc) { + if (F.sc) return F.fn.pOutCreateSwapchainFFXAPI(vk_ctx()->device, ci, nullptr, sc, F.sc); + if (!vk_fg_wanted() || F.failed || !F.present || !vk_ffx_load()) return VK_ERROR_FEATURE_NOT_PRESENT; + const VkCtx *c = vk_ctx(); + ffxCreateContextDescFrameGenerationSwapChainVK d = {}; + d.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FGSWAPCHAIN_VK; + d.physicalDevice = c->phys; + d.device = c->device; + VkSwapchainKHR none = VK_NULL_HANDLE; + d.swapchain = &none; + d.createInfo = *ci; + d.createInfo.oldSwapchain = VK_NULL_HANDLE; + d.gameQueue = {c->gfx_queue, c->gfx_family, submit_game}; + d.asyncComputeQueue = {VK_NULL_HANDLE, c->gfx_family, nullptr}; + d.presentQueue = {F.present, c->gfx_family, nullptr}; + d.imageAcquireQueue = {F.acquire, c->gfx_family, nullptr}; + ffxReturnCode_t r = vk_ffx_create(&F.sc, &d.header); + if (r != FFX_API_RETURN_OK) { + F.sc = nullptr; + fail("the frame generation swapchain", r); + return VK_ERROR_INITIALIZATION_FAILED; + } + F.fn.header.type = FFX_API_QUERY_DESC_TYPE_FGSWAPCHAIN_FUNCTIONS_VK; + if (vk_ffx_query(&F.sc, &F.fn.header) != FFX_API_RETURN_OK || !F.fn.pOutQueuePresentKHR) { + fail("the swapchain functions", 0); + return VK_ERROR_INITIALIZATION_FAILED; + } + *sc = none; + rt_log("fg: AMD FSR 3.1 frame generation swapchain, %ux%u", ci->imageExtent.width, ci->imageExtent.height); + return VK_SUCCESS; +} + +extern "C" void vk_fg_destroy_swapchain(VkSwapchainKHR sc) { + if (F.sc && sc) F.fn.pOutDestroySwapchainFFXAPI(vk_ctx()->device, sc, nullptr, F.sc); +} + +extern "C" VkResult vk_fg_get_images(VkSwapchainKHR sc, uint32_t *n, VkImage *img) { + return F.fn.pOutGetSwapchainImagesKHR(vk_ctx()->device, sc, n, img); +} + +extern "C" VkResult vk_fg_acquire(VkSwapchainKHR sc, uint64_t timeout, VkSemaphore sem, uint32_t *idx) { + return F.fn.pOutAcquireNextImageKHR(vk_ctx()->device, sc, timeout, sem, VK_NULL_HANDLE, idx); +} + +extern "C" VkResult vk_fg_present(const VkPresentInfoKHR *pi) { + return F.fn.pOutQueuePresentKHR(vk_ctx()->gfx_queue, pi); +} + +extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, VkExtent2D ext) { + if (!vk_fg_active()) return; + const VkCtx *c = vk_ctx(); + if (F.fg && (F.fg_ext.width != ext.width || F.fg_ext.height != ext.height || F.fg_fmt != fmt)) { + vkDeviceWaitIdle(c->device); // a new window size: the context is made for its display size + vk_ffx_destroy(&F.fg); + F.fg = nullptr; + } + if (!F.fg) { + ffxCreateBackendVKDesc be = {}; + be.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_VK; + be.vkDevice = c->device; + be.vkPhysicalDevice = c->phys; + be.vkDeviceProcAddr = vkGetDeviceProcAddr; + ffxCreateContextDescFrameGeneration d = {}; + d.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATION; + d.header.pNext = &be.header; + d.flags = FFX_FRAMEGENERATION_ENABLE_DEPTH_INFINITE; + d.displaySize = {ext.width, ext.height}; + d.maxRenderSize = {ext.width, ext.height}; + d.backBufferFormat = ffxApiGetSurfaceFormatVK(fmt); + ffxReturnCode_t r = vk_ffx_create(&F.fg, &d.header); + if (r != FFX_API_RETURN_OK) return F.fg = nullptr, fail("the frame generation context", r); + F.fg_ext = ext, F.fg_fmt = fmt; + rt_log("fg: frame generation context for %ux%u", ext.width, ext.height); + } + VkFfxImage depth, mv; + float jx = 0, jy = 0; + int reset = 0; + int have = vk_dlaa_fg_inputs(&depth, &mv, &jx, &jy, &reset); + int on = have && !menu_is_open(); + if (!have) F.no_inputs++; + else if (!on) F.paused++; + F.frame++; + ffxConfigureDescFrameGeneration cfg = {}; + cfg.header.type = FFX_API_CONFIGURE_DESC_TYPE_FRAMEGENERATION; + cfg.swapChain = (void *)sc; + cfg.frameGenerationCallback = generate; + cfg.frameGenerationCallbackUserContext = &F.fg; + cfg.frameGenerationEnabled = on != 0; + cfg.allowAsyncWorkloads = false; + cfg.generationRect = {0, 0, (int32_t)ext.width, (int32_t)ext.height}; + cfg.frameID = F.frame; + ffxReturnCode_t r = vk_ffx_configure(&F.fg, &cfg.header); + if (r != FFX_API_RETURN_OK) return fail("frame generation configuration", r); + if (!on) return; + ffxDispatchDescFrameGenerationPrepare p = {}; + p.header.type = FFX_API_DISPATCH_DESC_TYPE_FRAMEGENERATION_PREPARE; + p.frameID = F.frame; + p.commandList = cb; + p.renderSize = {depth.width, depth.height}; + p.jitterOffset = {jx, jy}; + p.motionVectorScale = {1.0f, 1.0f}; // render pixels, current to previous (vk_dlaa.c mv_pass), as FSR takes them + static uint64_t last_ns; + uint64_t now = plat_now_ns(); + double dt = last_ns ? (now - last_ns) / 1e6 : 16.67; + last_ns = now; + p.frameTimeDelta = dt > 0 && dt < 100 ? (float)dt : 16.67f; + p.cameraNear = 0.1f; // Ocarina's projection: near 0.1, far at infinity (vk_ffx.cpp) + p.cameraFar = 1e8f; + p.cameraFovAngleVertical = 1.0f; + p.viewSpaceToMetersFactor = 0.01f; + p.depth = image(&depth); + p.motionVectors = image(&mv); + r = vk_ffx_dispatch(&F.fg, &p.header); + if (r != FFX_API_RETURN_OK) return fail("frame generation preparation", r); + if (F.frame == 2 || F.frame % 3600 == 0) + rt_log("fg: %llu frames presented, %llu generated; paused %llu (menu), %llu without depth and motion", + (unsigned long long)F.frame, (unsigned long long)F.generated, (unsigned long long)F.paused, + (unsigned long long)F.no_inputs); +} + +extern "C" void vk_fg_release(void) { + if (F.fg) vk_ffx_destroy(&F.fg), F.fg = nullptr; + if (F.sc) vk_ffx_destroy(&F.sc), F.sc = nullptr; +} diff --git a/runtime/vk_fg.h b/runtime/vk_fg.h new file mode 100644 index 00000000..2218a86d --- /dev/null +++ b/runtime/vk_fg.h @@ -0,0 +1,20 @@ +// Frame generation with AMD FSR 3.1 (vk_fg.cpp): AMD's frame generation swapchain in gfx_present.c's place. +#pragma once +#include +#include + +int vk_fg_wanted(void); // NX2NAVI_FRAMEGEN = fsr3 with DLSS or FSR 3.1 upscaling on (read once) +uint32_t vk_fg_queue_count(void); // graphics-family queues the device creates: 3 with frame generation, else 1 +void vk_fg_queues(VkQueue present, VkQueue acquire); // vk_device.c, after the device exists +int vk_fg_active(void); // the frame generation swapchain exists +// gfx_present.c's swapchain calls while vk_fg_wanted: the first create makes AMD's swapchain context, later ones +// (window resizes) its swapchain again. Non-success from the first: the plain swapchain path stays. +VkResult vk_fg_create_swapchain(const VkSwapchainCreateInfoKHR *ci, VkSwapchainKHR *sc); +void vk_fg_destroy_swapchain(VkSwapchainKHR sc); +VkResult vk_fg_get_images(VkSwapchainKHR sc, uint32_t *n, VkImage *img); +VkResult vk_fg_acquire(VkSwapchainKHR sc, uint64_t timeout, VkSemaphore sem, uint32_t *idx); +VkResult vk_fg_present(const VkPresentInfoKHR *pi); // on the game's queue, its lock held by the caller +// Each present, before its command buffer is submitted: configures generation for this frame and records its +// preparation (the frame's depth and motion vectors, vk_dlaa_fg_inputs) into cb. +void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, VkExtent2D ext); +void vk_fg_release(void);