mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 07:31:06 -04:00
Keep one FSR 4 and one FSR 3.1 context per output size, free the ones no upscale used for 90 seconds, and let FSR 4 recover from failed creations.
Each upscaling mode made a context of its own (about 217 MiB of video memory for FSR 4 at 3440x1440, 50 to 70 ms each), up to six were kept beside FSR 3.1's, and three FSR 4 creations that failed in a row turned FSR 4 off for the run: the menu then showed FSR 3.1 and no longer listed FSR 4. A player's log had the third failure (error 3) right after stepping through the modes with ten contexts alive. A context now takes every render size up to its output size, which the API allows at each dispatch; a creation that fails frees every context not in use, FSR 3.1's idle ones too, and is tried again; FSR 3.1 serves the frames between tries two seconds apart; only four failures in a row turn FSR 4 off, and choosing it in the menu again tries it anew. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -76,6 +76,7 @@ int vk_ffx_available(void); // vk_ffx.cpp: FSR 3.1 upscaling can run
|
||||
int vk_ffx_framegen_available(void); // ... and FSR 3.1 frame generation
|
||||
int vk_ffx4_available(void); // vk_ffx4.cpp: AMD's FSR 4 runs on this device (its libraries, card and driver)
|
||||
int vk_ffx_fsr4(void); // vk_ffx.cpp: ... and the setting asks for it (NX2NAVI_FSR4)
|
||||
void vk_ffx4_retry(void); // vk_ffx4.cpp: FSR 4 chosen again after failures that turned it off
|
||||
int vk_fg4_available(void); // vk_fg4.cpp: the frame generation of AMD's FSR 4 library runs on this device
|
||||
int vk_fg4_failed(void); // ... it failed in this run, FSR 3.1's generates the frames
|
||||
int vk_fg4_active(void); // ... its swapchain is in place
|
||||
@@ -974,6 +975,9 @@ static void row_upscaling(void) {
|
||||
if (e.kind >= DLSS) opt_apply("NX2NAVI_DLSS", modes[e.mode]);
|
||||
if (e.kind >= DLSS) opt_apply("NX2NAVI_TAA", e.kind >= FSR3 ? "fsr3" : "dlss");
|
||||
if (e.kind >= FSR3) opt_apply("NX2NAVI_FSR4", e.kind == FSR4 ? "1" : "0");
|
||||
// FSR 4 that failed in a row stays listed (it runs on this device) while FSR 3.1 serves: the row shows FSR 3.1, and
|
||||
// choosing FSR 4 again tries it anew
|
||||
if (e.kind == FSR4) vk_ffx4_retry();
|
||||
}
|
||||
|
||||
// Under the HDR value: "after restart" when what is set waits for one (the game's own HDR, to or from it:
|
||||
@@ -1157,7 +1161,7 @@ static void row_framegen(void) {
|
||||
opt_apply("NX2NAVI_FRAMEGEN", k == OFF ? "off" : k == DLSS ? "dlss" : "fsr3");
|
||||
if (k == FSR4LIB) {
|
||||
opt_apply("NX2NAVI_FSR4_FG", "1");
|
||||
if (!vk_ffx_fsr4()) opt_apply("NX2NAVI_TAA", "fsr3"), opt_apply("NX2NAVI_FSR4", "1");
|
||||
if (!vk_ffx_fsr4()) opt_apply("NX2NAVI_TAA", "fsr3"), opt_apply("NX2NAVI_FSR4", "1"), vk_ffx4_retry();
|
||||
} else if (k == FSR3 && cfg_bool("NX2NAVI_FSR4_FG")) {
|
||||
opt_apply("NX2NAVI_FSR4_FG", "0");
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
extern "C" {
|
||||
#include "cfg.h"
|
||||
#include "host.h"
|
||||
#include "platform/platform.h"
|
||||
#include "vk.h"
|
||||
#include "vk_ffx.h"
|
||||
}
|
||||
@@ -110,20 +111,35 @@ ffxReturnCode_t create_counted(ffxContext *ctx, ffxCreateContextDescHeader *desc
|
||||
return r;
|
||||
}
|
||||
|
||||
// One upscaling context per render and output size: each render scale the menu sets brings its own, and so do the
|
||||
// game's second scenes. When all are taken, the least recently used one is destroyed for the new size once the GPU is
|
||||
// idle, as vk_dlaa.c free_size does for DLSS. Without that, a seventh size found no context, and vk_dlaa.c turned
|
||||
// FSR 3.1 off for the rest of the run: stepping through the render scales in the menu was enough.
|
||||
// One upscaling context per output size, made for render sizes up to the output's: every render scale the menu sets
|
||||
// runs on it with its own render size at each dispatch (the API's dynamic resolution, up to maxRenderSize), and the
|
||||
// game's second scenes, of another output size, get their own. When all are taken, the least recently used one is
|
||||
// destroyed for the new size once the GPU is idle, as vk_dlaa.c free_size does for DLSS. One per render size, as
|
||||
// before, kept a context for each mode tried, each with its own video memory, and the seventh size found none.
|
||||
struct Up {
|
||||
ffxContext ctx = nullptr;
|
||||
uint32_t w = 0, h = 0, ow = 0, oh = 0;
|
||||
uint64_t used = 0; // up_tick when context_for last returned it
|
||||
} U[6];
|
||||
uint32_t mw = 0, mh = 0, ow = 0, oh = 0; // the largest render size it takes; the output size
|
||||
uint64_t used = 0, used_ns = 0; // up_tick and the time when context_for last returned it
|
||||
} U[4];
|
||||
uint64_t up_tick;
|
||||
// A context used in the last few calls may sit in a command buffer that is not submitted yet, where waiting for the
|
||||
// GPU does not cover it: a frame makes one or two calls (the scene, and a second scene after it), so this is a few
|
||||
// frames back.
|
||||
const uint64_t UP_RECENT = 8;
|
||||
// A context no upscale used for this long goes (trim): FSR 3.1's after a switch to FSR 4, and a second scene's (the
|
||||
// pause screen's) a while after it closed. Unused that long, no command buffer names it.
|
||||
const uint64_t UP_IDLE_NS = 90ull * 1000000000ull;
|
||||
|
||||
void destroy_up(Up *u, const char *why) {
|
||||
rt_log("ffx: the FSR 3.1 context for %ux%u (render sizes up to %ux%u) destroyed: %s", u->ow, u->oh, u->mw, u->mh, why);
|
||||
A.destroy(&u->ctx, nullptr);
|
||||
*u = Up();
|
||||
}
|
||||
|
||||
void trim(uint64_t now_ns) {
|
||||
for (Up &u : U)
|
||||
if (u.ctx && now_ns - u.used_ns > UP_IDLE_NS) destroy_up(&u, "no upscale used it for 90 s");
|
||||
}
|
||||
|
||||
FfxApiResource image(const VkFfxImage *i, uint32_t state, uint32_t usage) {
|
||||
VkImageCreateInfo ci = {};
|
||||
@@ -139,31 +155,34 @@ FfxApiResource image(const VkFfxImage *i, uint32_t state, uint32_t usage) {
|
||||
|
||||
Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
|
||||
up_tick++;
|
||||
uint64_t now = plat_now_ns();
|
||||
Up *small = nullptr; // a context of this output size made for smaller render sizes
|
||||
for (Up &u : U)
|
||||
if (u.ctx && u.w == w && u.h == h && u.ow == ow && u.oh == oh) {
|
||||
u.used = up_tick;
|
||||
return &u;
|
||||
if (u.ctx && u.ow == ow && u.oh == oh) {
|
||||
if (w <= u.mw && h <= u.mh) {
|
||||
u.used = up_tick, u.used_ns = now;
|
||||
return &u;
|
||||
}
|
||||
small = &u;
|
||||
}
|
||||
Up *slot = nullptr, *lru = nullptr;
|
||||
for (Up &u : U) {
|
||||
if (!u.ctx) {
|
||||
slot = &u;
|
||||
break;
|
||||
if (!slot) slot = &u;
|
||||
continue;
|
||||
}
|
||||
if (up_tick - u.used >= UP_RECENT && (!lru || u.used < lru->used)) lru = &u;
|
||||
}
|
||||
if (small && up_tick - small->used >= UP_RECENT) slot = nullptr, lru = small; // a larger one takes its place
|
||||
const VkCtx *c = vk_ctx();
|
||||
if (!slot && lru) {
|
||||
static int told;
|
||||
if (told++ < 20)
|
||||
rt_log("ffx: FSR 3.1 context %ux%u -> %ux%u destroyed for %ux%u -> %ux%u (all %d in use)", lru->w, lru->h, lru->ow,
|
||||
lru->oh, w, h, ow, oh, (int)(sizeof U / sizeof U[0]));
|
||||
vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // its work may still be in flight
|
||||
A.destroy(&lru->ctx, nullptr);
|
||||
*lru = Up();
|
||||
destroy_up(lru, lru == small ? "a larger render size came" : "its slot went to another output size");
|
||||
slot = lru;
|
||||
}
|
||||
if (!slot) return nullptr;
|
||||
// the render size up to the output size: every mode of the menu, and a scene drawn at the output size itself
|
||||
uint32_t mw = w > ow ? w : ow, mh = h > oh ? h : oh;
|
||||
ffxCreateBackendVKDesc be = {};
|
||||
be.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_VK;
|
||||
be.vkDevice = c->device;
|
||||
@@ -173,20 +192,21 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
|
||||
d.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE;
|
||||
d.header.pNext = &be.header;
|
||||
d.flags = FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE | FFX_UPSCALE_ENABLE_DEPTH_INFINITE | FFX_UPSCALE_ENABLE_AUTO_EXPOSURE;
|
||||
d.maxRenderSize = {w, h};
|
||||
d.maxRenderSize = {mw, mh};
|
||||
d.maxUpscaleSize = {ow, oh};
|
||||
d.fpMessage = message;
|
||||
ffxReturnCode_t r = create_counted(&slot->ctx, &d.header);
|
||||
if (r != FFX_API_RETURN_OK) {
|
||||
rt_log("ffx: FSR 3.1 context creation at %ux%u -> %ux%u failed (%u)", w, h, ow, oh, (unsigned)r);
|
||||
rt_log("ffx: FSR 3.1 context creation for %ux%u (render sizes up to %ux%u) failed (%u)", ow, oh, mw, mh, (unsigned)r);
|
||||
slot->ctx = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
slot->w = w, slot->h = h, slot->ow = ow, slot->oh = oh, slot->used = up_tick;
|
||||
slot->mw = mw, slot->mh = mh, slot->ow = ow, slot->oh = oh, slot->used = up_tick, slot->used_ns = now;
|
||||
ffxQueryGetProviderVersion v = {};
|
||||
v.header.type = FFX_API_QUERY_DESC_TYPE_GET_PROVIDER_VERSION;
|
||||
A.query(&slot->ctx, &v.header);
|
||||
rt_log("ffx: FSR 3.1 upscaling at %ux%u -> %ux%u (%s)", w, h, ow, oh, v.versionName ? v.versionName : "provider unknown");
|
||||
rt_log("ffx: FSR 3.1 upscaling to %ux%u from render sizes up to %ux%u (%s)", ow, oh, mw, mh,
|
||||
v.versionName ? v.versionName : "provider unknown");
|
||||
return slot;
|
||||
}
|
||||
|
||||
@@ -240,12 +260,22 @@ extern "C" int vk_ffx_available(void) {
|
||||
|
||||
extern "C" int vk_ffx_framegen_available(void) { return vk_ffx_available(); } // the same library, its API calls
|
||||
|
||||
// NX2NAVI_FSR4 (default 0, live): FSR 4 where it runs (vk_ffx4.cpp), else FSR 3.1.
|
||||
extern "C" int vk_ffx_fsr4(void) { return cfg_bool("NX2NAVI_FSR4") && vk_ffx4_available(); }
|
||||
// NX2NAVI_FSR4 (default 0, live): FSR 4 where it runs (vk_ffx4.cpp) and has not failed in a row, else FSR 3.1.
|
||||
extern "C" int vk_ffx_fsr4(void) { return cfg_bool("NX2NAVI_FSR4") && vk_ffx4_available() && !vk_ffx4_failed(); }
|
||||
|
||||
extern "C" int vk_ffx_trim_idle(uint64_t recent) {
|
||||
int n = 0;
|
||||
for (Up &u : U)
|
||||
if (u.ctx && up_tick - u.used >= recent) destroy_up(&u, "room for an FSR 4 context"), n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
extern "C" 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) {
|
||||
if (!vk_ffx_load()) return 0;
|
||||
uint64_t now = plat_now_ns();
|
||||
trim(now);
|
||||
vk_ffx4_trim(now);
|
||||
// Each upscaler keeps a history of its own: the frame after a change from one to the other starts it anew, or it
|
||||
// would blend this picture with whatever that upscaler last saw.
|
||||
static int last; // 3 or 4: which one made the frame before
|
||||
|
||||
@@ -22,6 +22,9 @@ int vk_ffx_framegen_available(void);
|
||||
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
|
||||
// FSR 3.1's contexts that no upscale used in the last `recent` calls, destroyed with the device idle (the caller waited
|
||||
// for it): room for an FSR 4 context whose creation failed. Their count.
|
||||
int vk_ffx_trim_idle(uint64_t recent);
|
||||
// The device's functions as the library is given them (ffxCreateBackendVKDesc.vkDeviceProcAddr): its pipeline creates
|
||||
// go through the runtime's pipeline caches (vk_pipeline.c vk_pipeline_create_foreign; NX2NAVI_FFX_PIPE_CACHE=0: not).
|
||||
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_ffx_proc_addr(VkDevice dev, const char *name);
|
||||
@@ -35,6 +38,9 @@ const char *vk_ffx4_name(void); // "AMD FSR 4.1.2" once vk_ffx4_available foun
|
||||
int vk_ffx4_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); // as vk_ffx_upscale
|
||||
void vk_ffx4_release(void);
|
||||
int vk_ffx4_failed(void); // it failed three times in a row: FSR 3.1 serves until vk_ffx4_retry
|
||||
void vk_ffx4_retry(void); // the menu chose FSR 4 again: try it anew after such failures
|
||||
void vk_ffx4_trim(uint64_t now_ns); // at each upscale: FSR 4's contexts unused for a while go
|
||||
int vk_ffx_fsr4(void); // the setting asks for FSR 4 and it runs here: what vk_ffx_upscale uses, for the menu
|
||||
|
||||
#if defined(__cplusplus) && !defined(VK_FFX_NO_API)
|
||||
|
||||
@@ -28,6 +28,9 @@ extern "C" int vk_ffx4_upscale(VkCommandBuffer, const VkFfxImage *, const VkFfxI
|
||||
return 0;
|
||||
}
|
||||
extern "C" void vk_ffx4_release(void) {}
|
||||
extern "C" int vk_ffx4_failed(void) { return 0; }
|
||||
extern "C" void vk_ffx4_retry(void) {}
|
||||
extern "C" void vk_ffx4_trim(uint64_t) {}
|
||||
#else
|
||||
#include <windows.h>
|
||||
#include <atomic>
|
||||
@@ -61,10 +64,15 @@ struct Api {
|
||||
|
||||
std::mutex g_lock; // the probe: the menu asks from its thread, the replay from its own
|
||||
std::atomic<int> g_state{-1}; // -1 not probed; 0 FSR 4 does not run here; 1 it does
|
||||
std::atomic<int> g_off{0}; // FAIL_MAX failures in a row: FSR 3.1 serves until the menu asks again (vk_ffx4_retry)
|
||||
uint64_t g_version; // the id of the 4.x version the loader listed for the device
|
||||
char g_name[48] = "AMD FSR 4"; // "AMD FSR 4.1.2", for the menu and the log
|
||||
int g_failures; // contexts or dispatches that failed; past FAIL_MAX FSR 4 is off for the run
|
||||
const int FAIL_MAX = 3;
|
||||
int g_failures; // contexts or dispatches that failed in a row
|
||||
const int FAIL_MAX = 4;
|
||||
// After a context creation failed, FSR 3.1 serves the frames until the next try this much later: a save loading fills
|
||||
// the video memory with the area's textures for a few frames, and three tries in three frames there turned FSR 4 off.
|
||||
uint64_t g_retry_ns;
|
||||
const uint64_t RETRY_NS = 2ull * 1000000000ull;
|
||||
|
||||
void message(uint32_t type, const wchar_t *msg) {
|
||||
char s[512];
|
||||
@@ -170,19 +178,32 @@ int probe(void) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// One context per render and output size, as vk_ffx.cpp keeps them for FSR 3.1; the descriptors a context was made
|
||||
// from stay with it, as the API asks ("must remain live until ffxDestroyContext").
|
||||
// One context per output size, made for render sizes up to the output's, as vk_ffx.cpp keeps them for FSR 3.1: every
|
||||
// render scale of the menu runs on it with its own render size at each dispatch, which the API allows up to the
|
||||
// context's maxRenderSize. With one context per render and output size, each mode tried made a context of its own,
|
||||
// about 217 MiB of video memory and 50 to 70 ms each, up to six were kept beside FSR 3.1's, and three creations that
|
||||
// failed (the card out of memory) turned FSR 4 off for the run. The descriptors a context was made from stay with it,
|
||||
// as the API asks ("must remain live until ffxDestroyContext").
|
||||
struct Up {
|
||||
ffxContext ctx = nullptr;
|
||||
uint32_t w = 0, h = 0, ow = 0, oh = 0;
|
||||
uint64_t used = 0;
|
||||
uint32_t mw = 0, mh = 0, ow = 0, oh = 0; // the largest render size it takes; the output size
|
||||
uint64_t used = 0, used_ns = 0; // up_tick and the time when context_for last returned it
|
||||
ffxCreateContextDescUpscale desc = {};
|
||||
ffxCreateBackendVKDesc backend = {};
|
||||
ffxOverrideVersion version = {};
|
||||
ffxCreateContextDescUpscaleVersion built = {};
|
||||
} U[6];
|
||||
} U[4];
|
||||
uint64_t up_tick;
|
||||
const uint64_t UP_RECENT = 8; // contexts used this recently may sit in a command buffer not submitted yet
|
||||
// A context no upscale used for this long goes (vk_ffx4_trim): FSR 4's after a switch to FSR 3.1, and a second scene's
|
||||
// (the pause screen's) a while after it closed. Unused that long, no command buffer names it.
|
||||
const uint64_t UP_IDLE_NS = 90ull * 1000000000ull;
|
||||
|
||||
void destroy_up(Up *u, const char *why) {
|
||||
rt_log("ffx4: the %ux%u context (render sizes up to %ux%u) destroyed: %s", u->ow, u->oh, u->mw, u->mh, why);
|
||||
A.destroy(&u->ctx, nullptr);
|
||||
*u = Up();
|
||||
}
|
||||
|
||||
FfxApiResource image(const VkFfxImage *i, uint32_t state, uint32_t usage) {
|
||||
VkImageCreateInfo ci = {};
|
||||
@@ -196,29 +217,9 @@ FfxApiResource image(const VkFfxImage *i, uint32_t state, uint32_t usage) {
|
||||
return ffxApiGetResourceVK((void *)i->image, ffxApiGetImageResourceDescriptionVK(i->image, ci, usage), state);
|
||||
}
|
||||
|
||||
Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
|
||||
up_tick++;
|
||||
for (Up &u : U)
|
||||
if (u.ctx && u.w == w && u.h == h && u.ow == ow && u.oh == oh) {
|
||||
u.used = up_tick;
|
||||
return &u;
|
||||
}
|
||||
Up *slot = nullptr, *lru = nullptr;
|
||||
for (Up &u : U) {
|
||||
if (!u.ctx) {
|
||||
slot = &u;
|
||||
break;
|
||||
}
|
||||
if (up_tick - u.used >= UP_RECENT && (!lru || u.used < lru->used)) lru = &u;
|
||||
}
|
||||
// A context in slot for render sizes up to mw x mh and the output size ow x oh; NULL when it is not made.
|
||||
Up *create(Up *slot, uint32_t mw, uint32_t mh, uint32_t ow, uint32_t oh) {
|
||||
const VkCtx *c = vk_ctx();
|
||||
if (!slot && lru) {
|
||||
vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // its work may still be in flight
|
||||
A.destroy(&lru->ctx, nullptr);
|
||||
*lru = Up();
|
||||
slot = lru;
|
||||
}
|
||||
if (!slot) return nullptr;
|
||||
*slot = Up();
|
||||
slot->built.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE_VERSION;
|
||||
slot->built.version = FFX_UPSCALER_VERSION;
|
||||
@@ -232,17 +233,18 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
|
||||
slot->desc.header.pNext = &slot->backend.header;
|
||||
slot->desc.flags =
|
||||
FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE | FFX_UPSCALE_ENABLE_DEPTH_INFINITE | FFX_UPSCALE_ENABLE_AUTO_EXPOSURE;
|
||||
slot->desc.maxRenderSize = {w, h};
|
||||
slot->desc.maxRenderSize = {mw, mh};
|
||||
slot->desc.maxUpscaleSize = {ow, oh};
|
||||
slot->desc.fpMessage = message;
|
||||
uint64_t t0 = plat_now_ns();
|
||||
ffxReturnCode_t r = A.create(&slot->ctx, &slot->desc.header, nullptr);
|
||||
if (r != FFX_API_RETURN_OK || !slot->ctx) {
|
||||
rt_log("ffx4: %s context creation at %ux%u -> %ux%u failed (%u)", g_name, w, h, ow, oh, (unsigned)r);
|
||||
rt_log("ffx4: %s context creation for %ux%u (render sizes up to %ux%u) failed (%u)", g_name, ow, oh, mw, mh,
|
||||
(unsigned)r);
|
||||
*slot = Up();
|
||||
return nullptr;
|
||||
}
|
||||
slot->w = w, slot->h = h, slot->ow = ow, slot->oh = oh, slot->used = up_tick;
|
||||
slot->mw = mw, slot->mh = mh, slot->ow = ow, slot->oh = oh;
|
||||
ffxQueryGetProviderVersion v = {};
|
||||
v.header.type = FFX_API_QUERY_DESC_TYPE_GET_PROVIDER_VERSION;
|
||||
A.query(&slot->ctx, &v.header);
|
||||
@@ -251,8 +253,8 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
|
||||
mq.header.type = FFX_API_QUERY_DESC_TYPE_UPSCALE_GPU_MEMORY_USAGE;
|
||||
mq.gpuMemoryUsageUpscaler = &mem;
|
||||
A.query(&slot->ctx, &mq.header);
|
||||
rt_log("ffx4: FSR %s upscaling at %ux%u -> %ux%u, made in %.0f ms, %.0f MiB of video memory",
|
||||
v.versionName ? v.versionName : "4", w, h, ow, oh, (double)(plat_now_ns() - t0) / 1e6,
|
||||
rt_log("ffx4: FSR %s upscaling to %ux%u from render sizes up to %ux%u, made in %.0f ms, %.0f MiB of video memory",
|
||||
v.versionName ? v.versionName : "4", ow, oh, mw, mh, (double)(plat_now_ns() - t0) / 1e6,
|
||||
(double)mem.totalUsageInBytes / 1048576.0);
|
||||
// The provider is asked for the version it runs: anything but a 4.x here means the loader gave another upscaler
|
||||
if (v.versionName && atoi(v.versionName) < 4) {
|
||||
@@ -264,10 +266,60 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
|
||||
return slot;
|
||||
}
|
||||
|
||||
// The context for a render size and an output size: the one of that output size that takes the render size, else a
|
||||
// new one, in a free slot or in place of the least recently used one, with the device idle first. A creation that
|
||||
// fails is tried once more after every context not in use has gone, FSR 3.1's idle ones too (vk_ffx_trim_idle): what
|
||||
// it lacked was most often video memory.
|
||||
Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
|
||||
up_tick++;
|
||||
uint64_t now = plat_now_ns();
|
||||
Up *small = nullptr; // a context of this output size made for smaller render sizes
|
||||
for (Up &u : U)
|
||||
if (u.ctx && u.ow == ow && u.oh == oh) {
|
||||
if (w <= u.mw && h <= u.mh) {
|
||||
u.used = up_tick, u.used_ns = now;
|
||||
return &u;
|
||||
}
|
||||
small = &u;
|
||||
}
|
||||
Up *slot = nullptr, *lru = nullptr;
|
||||
for (Up &u : U) {
|
||||
if (!u.ctx) {
|
||||
if (!slot) slot = &u;
|
||||
continue;
|
||||
}
|
||||
if (up_tick - u.used >= UP_RECENT && (!lru || u.used < lru->used)) lru = &u;
|
||||
}
|
||||
if (small && up_tick - small->used >= UP_RECENT) slot = nullptr, lru = small; // a larger one takes its place
|
||||
const VkCtx *c = vk_ctx();
|
||||
if (!slot && lru) {
|
||||
vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // its work may still be in flight
|
||||
destroy_up(lru, lru == small ? "a larger render size came" : "its slot went to another output size");
|
||||
slot = lru;
|
||||
}
|
||||
if (!slot) return nullptr;
|
||||
// the render size up to the output size: every mode of the menu, and a scene drawn at the output size itself
|
||||
uint32_t mw = w > ow ? w : ow, mh = h > oh ? h : oh;
|
||||
Up *u = create(slot, mw, mh, ow, oh);
|
||||
if (!u) {
|
||||
int freed = 0;
|
||||
vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock();
|
||||
for (Up &o : U)
|
||||
if (o.ctx && up_tick - o.used >= UP_RECENT) destroy_up(&o, "room for a context whose creation failed"), freed++;
|
||||
freed += vk_ffx_trim_idle(UP_RECENT);
|
||||
if (freed) u = create(slot, mw, mh, ow, oh);
|
||||
}
|
||||
if (u) u->used = up_tick, u->used_ns = now;
|
||||
return u;
|
||||
}
|
||||
|
||||
// Failures in a row: FAIL_MAX of them turn FSR 4 off until the menu asks for it again (vk_ffx4_retry); a success
|
||||
// starts the count again, so a creation that failed once in a while does not add up over a session.
|
||||
void failed(const char *what, unsigned code) {
|
||||
if (++g_failures == FAIL_MAX) {
|
||||
rt_log("ffx4: %s failed %d times (last %u): FSR 4 is off for this run, FSR 3.1 takes its place", what, FAIL_MAX, code);
|
||||
g_state.store(0, std::memory_order_release);
|
||||
rt_log("ffx4: %s failed %d times in a row (last %u): FSR 4 is off, FSR 3.1 takes its place until FSR 4 is "
|
||||
"chosen again in the menu", what, FAIL_MAX, code);
|
||||
g_off.store(1, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,11 +338,29 @@ extern "C" int vk_ffx4_available(void) {
|
||||
|
||||
extern "C" const char *vk_ffx4_name(void) { return g_name; }
|
||||
|
||||
extern "C" int vk_ffx4_failed(void) { return g_off.load(std::memory_order_acquire); }
|
||||
|
||||
// The menu chose FSR 4 again: after failures that turned it off, it is tried anew.
|
||||
extern "C" void vk_ffx4_retry(void) {
|
||||
if (!g_off.exchange(0, std::memory_order_acq_rel)) return;
|
||||
g_failures = 0, g_retry_ns = 0;
|
||||
rt_log("ffx4: FSR 4 chosen again in the menu: tried anew");
|
||||
}
|
||||
|
||||
// Called at every upscale, with FSR 4 or FSR 3.1 (vk_ffx.cpp vk_ffx_upscale), on the thread that records them.
|
||||
extern "C" void vk_ffx4_trim(uint64_t now_ns) {
|
||||
for (Up &u : U)
|
||||
if (u.ctx && now_ns - u.used_ns > UP_IDLE_NS) destroy_up(&u, "no upscale used it for 90 s");
|
||||
}
|
||||
|
||||
extern "C" int vk_ffx4_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) {
|
||||
if (!vk_ffx4_available()) return 0;
|
||||
if (!vk_ffx4_available() || g_off.load(std::memory_order_acquire)) return 0;
|
||||
uint64_t now = plat_now_ns();
|
||||
if (g_failures && now < g_retry_ns) return 0; // FSR 3.1 serves until the next try
|
||||
Up *u = context_for(color->width, color->height, out->width, out->height);
|
||||
if (!u) {
|
||||
g_retry_ns = now + RETRY_NS;
|
||||
failed("making a context", 0);
|
||||
return 0;
|
||||
}
|
||||
@@ -323,6 +393,7 @@ extern "C" int vk_ffx4_upscale(VkCommandBuffer cb, const VkFfxImage *color, cons
|
||||
failed("a dispatch", (unsigned)r);
|
||||
return 0;
|
||||
}
|
||||
g_failures = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user