mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 11:29:58 -04:00
Add frame generation through AMD's FSR SDK 2.4 as a setting to try with FSR 4, and never generate a frame without the HUD-less picture.
runtime/vk_fg4.cpp makes the frame generation swapchain and context with the libraries FSR 4 comes with (amd_fidelityfx_loader_vk.dll and amd_fidelityfx_framegeneration_vk.dll beside the executable). vk_fg.cpp takes that path only with fsr4_fg = 1 while FSR 4 is the upscaler; the default stays the FSR 3.1 library. The loader picks the version for the card: the machine-learned FSR Frame Generation 4 on Radeon RX 9000 cards with Windows 11, which is untested, and 3.1.6 elsewhere. On an RX 7900 XT (driver 26.10.1) it lists frame generation 3.1.6 and the swapchains 4.0.0, which the driver brings and the loader chooses, and 3.1.7. The swapchain in place is wanted only while its library is the one to use, so a change of the setting or of the upscaler, or a failure of this path, takes it down as switching frame generation off does and the next present makes it with the other library. The preparation gives this library the camera's position and axes, which FSR Frame Generation 4 requires, and a scene scale of one metre a unit: with the 0.01 the FSR 3.1 path uses, version 3.1.6 drew grey translucent shapes into the sky of its frames while the camera turned (160 captures of the screen during a camera sweep; clean with 1). Both paths now generate a frame only when the picture without the HUD is there for it: generated from the presented picture alone, the HUD would be warped with the scene behind it. In four configurations (21:9 with either library, a 16:9 picture in a 21:9 window, a 1080 picture without HDR) every generated frame had it, and the log counts the frames without. On screen, of the 6,404 pixels of the HUD's item button that stayed fixed through a camera sweep with the FSR 3.1 library, 6,162 stayed fixed with this one. Standing in Hyrule Field at 3440x1440 with FSR 4 Quality the game draws 43.0 frames a second with this path and 42.2 with the FSR 3.1 library, each shown twice over. One run changed the present mode, HDR, fullscreen, frame generation, FSR 4, this setting and the upscaler in turn: 216 screenshots without a bad frame, and no failure. The SDK's frame generation headers join runtime/third_party/ffx_sdk, unmodified. tools/make_kit.py and tools/make_launcher.py take the third library from the --ffx4 folder when it is there, and tools/make_game.py copies it beside the game. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -43,7 +43,7 @@ struct {
|
||||
ffxQueryDescSwapchainReplacementFunctionsVK fn = {};
|
||||
uint64_t frame = 0;
|
||||
int failed = 0;
|
||||
uint64_t generated = 0, paused = 0, no_inputs = 0, hudless = 0;
|
||||
uint64_t generated = 0, paused = 0, no_inputs = 0, hudless = 0, no_hudless = 0;
|
||||
int on = 0; // the last configured frame had generation on (vk_fg_generating)
|
||||
} F;
|
||||
|
||||
@@ -85,10 +85,23 @@ void fail(const char *what, unsigned r) {
|
||||
// NVIDIA's driver when the menu switched from NVIDIA's frame generation to AMD's. That switch waits for a restart,
|
||||
// as the menu says (menu.cpp row_framegen). Not after a failure (fail): the present code then takes AMD's swapchain
|
||||
// down as it does for a switch in the menu, and a plain one follows.
|
||||
//
|
||||
// Two libraries can make the swapchain: the FSR 3.1 library, here, or AMD's FSR SDK 2.4 (vk_fg4.cpp) where the
|
||||
// setting asks for it while FSR 4 is the upscaler (use4). The swapchain in place is wanted only while its library is
|
||||
// still the one to use: a change of that setting or of the upscaler between FSR 3.1 and FSR 4, or a failure of the SDK
|
||||
// 2.4 path, takes it down as switching frame generation off does, and the next present makes it with the other library.
|
||||
static int use4(void) { // NX2NAVI_FSR4_FG (default 0, live): 1 takes the SDK 2.4's frame generation with FSR 4
|
||||
return cfg_bool("NX2NAVI_FSR4_FG") && vk_ffx_fsr4() && !vk_fg4_failed() && vk_fg4_available();
|
||||
}
|
||||
|
||||
extern "C" int vk_fg_wanted(void) {
|
||||
if (F.failed || strcmp(cfg_value("NX2NAVI_FRAMEGEN"), "fsr3") || !cfg_bool("NX2NAVI_DLAA") || !vk_dlaa_wanted() ||
|
||||
vk_dlssg_dpa())
|
||||
if (strcmp(cfg_value("NX2NAVI_FRAMEGEN"), "fsr3") || !cfg_bool("NX2NAVI_DLAA") || !vk_dlaa_wanted() || vk_dlssg_dpa())
|
||||
return 0;
|
||||
int four = use4();
|
||||
if (vk_fg4_active()) return four;
|
||||
if (F.sc) return !F.failed && !four;
|
||||
if (four) return 1;
|
||||
if (F.failed) return 0;
|
||||
if (vk_ffx_framegen_available()) return 1;
|
||||
static int told;
|
||||
if (!__atomic_exchange_n(&told, 1, __ATOMIC_RELAXED))
|
||||
@@ -99,16 +112,28 @@ extern "C" int vk_fg_wanted(void) {
|
||||
// Always the three queues: the setting can be switched on during a run, and idle queues cost nothing.
|
||||
extern "C" uint32_t vk_fg_queue_count(void) { return 3; }
|
||||
|
||||
extern "C" void vk_fg_queues(VkQueue present, VkQueue acquire) { F.present = present, F.acquire = acquire; }
|
||||
extern "C" void vk_fg_queues(VkQueue present, VkQueue acquire) {
|
||||
F.present = present, F.acquire = acquire;
|
||||
vk_fg4_queues(present, acquire);
|
||||
}
|
||||
|
||||
// AMD's swapchain is in place: the window's swapchain handle is AMD's object, and every swapchain call has to go
|
||||
// through AMD's functions. That holds after a failure too, until the present code has replaced the swapchain
|
||||
// (vk_fg_wanted): the plain Vulkan calls must never get that handle.
|
||||
extern "C" int vk_fg_active(void) { return F.sc != nullptr; }
|
||||
extern "C" int vk_fg_active(void) { return F.sc != nullptr || vk_fg4_active(); }
|
||||
|
||||
extern "C" int vk_fg_generating(void) { return vk_fg_active() && !F.failed && __atomic_load_n(&F.on, __ATOMIC_RELAXED); }
|
||||
extern "C" int vk_fg_generating(void) {
|
||||
if (vk_fg4_active()) return vk_fg4_generating();
|
||||
return F.sc && !F.failed && __atomic_load_n(&F.on, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
extern "C" VkResult vk_fg_create_swapchain(const VkSwapchainCreateInfoKHR *ci, VkSwapchainKHR *sc) {
|
||||
if (vk_fg4_active()) return vk_fg4_create_swapchain(ci, sc);
|
||||
if (!F.sc && use4()) {
|
||||
VkResult r = vk_fg4_create_swapchain(ci, sc);
|
||||
if (r == VK_SUCCESS || vk_fg4_active()) return r;
|
||||
// it failed before its swapchain existed and said so (use4 is 0 from here on): the FSR 3.1 library's below
|
||||
}
|
||||
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();
|
||||
@@ -142,28 +167,34 @@ extern "C" VkResult vk_fg_create_swapchain(const VkSwapchainCreateInfoKHR *ci, V
|
||||
}
|
||||
|
||||
extern "C" void vk_fg_destroy_swapchain(VkSwapchainKHR sc) {
|
||||
if (vk_fg4_active()) return vk_fg4_destroy_swapchain(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) {
|
||||
if (vk_fg4_active()) return vk_fg4_get_images(sc, n, 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) {
|
||||
if (vk_fg4_active()) return vk_fg4_acquire(sc, timeout, sem, idx);
|
||||
return F.fn.pOutAcquireNextImageKHR(vk_ctx()->device, sc, timeout, sem, VK_NULL_HANDLE, idx);
|
||||
}
|
||||
|
||||
extern "C" VkResult vk_fg_present(const VkPresentInfoKHR *pi) {
|
||||
if (vk_fg4_active()) return vk_fg4_present(pi);
|
||||
return F.fn.pOutQueuePresentKHR(vk_ctx()->gfx_queue, pi);
|
||||
}
|
||||
|
||||
extern "C" uint64_t vk_fg_presents(VkSwapchainKHR sc) {
|
||||
return vk_fg_active() && F.fn.pOutGetLastPresentCountFFXAPI && sc ? F.fn.pOutGetLastPresentCountFFXAPI(sc) : 0;
|
||||
if (vk_fg4_active()) return vk_fg4_presents(sc);
|
||||
return F.sc && F.fn.pOutGetLastPresentCountFFXAPI && sc ? F.fn.pOutGetLastPresentCountFFXAPI(sc) : 0;
|
||||
}
|
||||
|
||||
// The HDR10 metadata goes to AMD's swapchain through its own call (vk_hdr.c vk_hdr_metadata).
|
||||
extern "C" PFN_vkSetHdrMetadataEXT vk_fg_hdr_metadata_fn(void) {
|
||||
return vk_fg_active() ? F.fn.pOutSetHdrMetadataEXT : nullptr;
|
||||
if (vk_fg4_active()) return vk_fg4_hdr_metadata_fn();
|
||||
return F.sc ? F.fn.pOutSetHdrMetadataEXT : nullptr;
|
||||
}
|
||||
|
||||
// An HDR10 swapchain (10-bit PQ or 16-bit float): frame generation is told its inputs are HDR.
|
||||
@@ -175,7 +206,8 @@ static bool hdr_format(VkFormat f) {
|
||||
extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, VkExtent2D ext, VkImage hudless,
|
||||
VkImageView hudless_view, const int pic[4]) {
|
||||
(void)hudless_view;
|
||||
if (!vk_fg_active() || F.failed) return;
|
||||
if (vk_fg4_active()) return vk_fg4_frame(cb, sc, fmt, ext, hudless, pic);
|
||||
if (!F.sc || F.failed) return;
|
||||
vk_dlaa_hudless_wanted = 1; // the upscaling pass keeps a copy of the picture before the HUD (vk_dlaa.c hudless)
|
||||
const VkCtx *c = vk_ctx();
|
||||
VkFfxImage depth, mv;
|
||||
@@ -226,9 +258,12 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt,
|
||||
depth.height, F.fg_max.width, F.fg_max.height);
|
||||
have = 0;
|
||||
}
|
||||
int on = have && !menu_is_open();
|
||||
// Only with the picture without the HUD: generated from the presented picture alone, the HUD would be warped along
|
||||
// with the scene behind it and trail when the camera turns. A frame without that picture is shown as it is.
|
||||
int on = have && hudless && !menu_is_open();
|
||||
__atomic_store_n(&F.on, on, __ATOMIC_RELAXED);
|
||||
if (!have) F.no_inputs++;
|
||||
else if (!hudless) F.no_hudless++;
|
||||
else if (!on) F.paused++;
|
||||
F.frame++;
|
||||
ffxConfigureDescFrameGeneration cfg = {};
|
||||
@@ -242,7 +277,7 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt,
|
||||
// whole swapchain warped the picture's edges into the bars
|
||||
cfg.generationRect = pic && pic[2] > 0 && pic[3] > 0 ? FfxApiRect2D{pic[0], pic[1], pic[2], pic[3]}
|
||||
: FfxApiRect2D{0, 0, (int32_t)ext.width, (int32_t)ext.height};
|
||||
if (on && hudless) {
|
||||
if (on) {
|
||||
VkFfxImage h = {hudless, fmt, ext.width, ext.height, 1};
|
||||
cfg.HUDLessColor = image(&h);
|
||||
F.hudless++;
|
||||
@@ -273,11 +308,13 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt,
|
||||
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 (%llu with the HUD kept out); paused %llu (menu), %llu without depth "
|
||||
"and motion", (unsigned long long)F.frame, (unsigned long long)F.generated, (unsigned long long)F.hudless,
|
||||
(unsigned long long)F.paused, (unsigned long long)F.no_inputs);
|
||||
"and motion, %llu without the HUD-less picture", (unsigned long long)F.frame, (unsigned long long)F.generated,
|
||||
(unsigned long long)F.hudless, (unsigned long long)F.paused, (unsigned long long)F.no_inputs,
|
||||
(unsigned long long)F.no_hudless);
|
||||
}
|
||||
|
||||
extern "C" void vk_fg_release(void) {
|
||||
vk_fg4_release();
|
||||
if (F.fg) vk_ffx_destroy(&F.fg), F.fg = nullptr;
|
||||
if (F.sc) vk_ffx_destroy(&F.sc), F.sc = nullptr;
|
||||
__atomic_store_n(&F.on, 0, __ATOMIC_RELAXED);
|
||||
|
||||
Reference in New Issue
Block a user