Make an FSR context for each render size, as AMD's own sample does, instead of one per output size for every mode.

One FSR 4 context took every render scale of the menu, with another render size at each switch and without the dynamic resolution flag. The first frame after a switch from 67 % to 100 % ended the game with an access violation in AMD's driver inside FSR 4's dispatch. AMD's sample waits for the GPU and creates the context anew on every render resolution change, and sets FFX_UPSCALE_ENABLE_DYNAMIC_RESOLUTION only for its dynamic resolution mode.

FSR 3.1 and FSR 4 now keep a context per render size and output size. The previous one is destroyed once no command buffer can hold it (eight upscales later), so a mode switch costs one creation of about 50 ms and two contexts for a few frames. With the game's dynamic resolution on (NX2NAVI_DYN_RES), one context made with the flag takes every step. NX2NAVI_FFX_DEBUG=1 (test) turns on the library's own checks of each dispatch.

Tested on an RX 7900 XT with switches every 15 frames and every 90 frames between every mode and between FSR 4 and FSR 3.1 (32 switches): one context made per switch, the old one gone a few frames later, no failure and no crash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
epicmeme546
2026-11-12 10:21:47 -05:00
parent ef3466ce4f
commit 7e938ec897
3 changed files with 72 additions and 47 deletions

View File

@@ -44,7 +44,7 @@ static const char *const known[] = {
"BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y",
"BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CACHE_DIE", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COMPUTE_KEEP", "COLOR_CONTRAST", "COLOR_GAMMA",
"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_CAM_KEEP", "DLAA_CUT", "DLSS_IDLE_FREE", "DLAA_EDGE_FILL", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TARGET_KEEP", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR",
"DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLAA_CUT", "DLSS_IDLE_FREE", "DLAA_EDGE_FILL", "FFX_DEBUG", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TARGET_KEEP", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR",
"DUMP_LOG", "DUMP_RAW", "DUMP_SHADERS", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CRASH_FILTER_GUARD", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "GPU_TIMELINE",
"FFX_PIPE_CACHE", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLIGHT_EVERY", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FG_MULT", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREEZE_ASYNC", "FREEZE_BUDGET_US", "FREE_CORES",
"FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "MIRROR_CAPTURE_KB", "WB_SKIP_MB", "GPL", "GPL_DRAW_BUILD", "GPL_FEEDBACK", "PIPE_AHEAD", "PIPE_FAST_WORKERS", "PIPE_FRAME_WAIT_MS", "PIPE_LINK_DRAW_MS", "PIPE_SKIP_STEADY", "PROG_LEAD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR",

View File

@@ -112,14 +112,20 @@ ffxReturnCode_t create_counted(ffxContext *ctx, ffxCreateContextDescHeader *desc
return r;
}
// 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.
// One upscaling context per render size and output size, as AMD's own sample keeps them: it waits for the GPU and makes
// the context anew whenever the render resolution changes, and sets FFX_UPSCALE_ENABLE_DYNAMIC_RESOLUTION only for its
// dynamic resolution mode. One context per output size that every mode of the menu ran on, with another render size at
// each dispatch and without that flag, preceded an access violation in AMD's driver inside FSR 4's dispatch at the
// first frame after a switch from 67 % to 100 % (vk_ffx4.cpp keeps its contexts the same way). With the game's dynamic
// resolution on (NX2NAVI_DYN_RES, dynres.c), whose steps change the render size every few seconds, one context with
// that flag takes them all. A context of another render size goes once no command buffer can still hold it
// (UP_RECENT), so a switch keeps at most two of an output size for a few frames, and the game's second scenes, of
// another output size, keep their own. When all slots 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.
struct Up {
ffxContext ctx = nullptr;
uint32_t mw = 0, mh = 0, ow = 0, oh = 0; // the largest render size it takes; the output size
uint32_t mw = 0, mh = 0, ow = 0, oh = 0; // the render size it was made for (the largest it takes with drs); the output size
bool drs = false; // made for dynamic resolution (FFX_UPSCALE_ENABLE_DYNAMIC_RESOLUTION)
uint64_t used = 0, used_ns = 0; // up_tick and the time when context_for last returned it
} U[4];
uint64_t up_tick;
@@ -157,15 +163,22 @@ 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
const bool drs = cfg_bool("NX2NAVI_DYN_RES");
Up *found = nullptr;
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;
if (u.ctx && u.ow == ow && u.oh == oh && u.drs == drs && (drs ? w <= u.mw && h <= u.mh : w == u.mw && h == u.mh))
found = &u;
const VkCtx *c = vk_ctx();
int waited = 0;
for (Up &u : U) // the contexts of this output size for another render size, once no command buffer holds them
if (u.ctx && &u != found && u.ow == ow && u.oh == oh && up_tick - u.used >= UP_RECENT) {
if (!waited++) vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // their work may be in flight
destroy_up(&u, "the render size changed");
}
if (found) {
found->used = up_tick, found->used_ns = now;
return found;
}
Up *slot = nullptr, *lru = nullptr;
for (Up &u : U) {
if (!u.ctx) {
@@ -174,16 +187,14 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
}
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");
destroy_up(lru, "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;
// the render size itself; for dynamic resolution, every size up to the output's (and a scene at the output size)
uint32_t mw = !drs ? w : w > ow ? w : ow, mh = !drs ? h : h > oh ? h : oh;
ffxCreateBackendVKDesc be = {};
be.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_VK;
be.vkDevice = c->device;
@@ -194,6 +205,9 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
d.header.pNext = &be.header;
// no infinite-depth flag: the game's far plane is finite (vk_dlaa.c proj_planes) and each dispatch gives it
d.flags = FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE | FFX_UPSCALE_ENABLE_AUTO_EXPOSURE;
// NX2NAVI_FFX_DEBUG=1 (test): the library checks the values of each dispatch and says what it finds (message)
if (rt_knob("FFX_DEBUG", "0")) d.flags |= FFX_UPSCALE_ENABLE_DEBUG_CHECKING;
if (drs) d.flags |= FFX_UPSCALE_ENABLE_DYNAMIC_RESOLUTION;
d.maxRenderSize = {mw, mh};
d.maxUpscaleSize = {ow, oh};
d.fpMessage = message;
@@ -203,7 +217,7 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
slot->ctx = nullptr;
return nullptr;
}
slot->mw = mw, slot->mh = mh, slot->ow = ow, slot->oh = oh, slot->used = up_tick, slot->used_ns = now;
slot->mw = mw, slot->mh = mh, slot->ow = ow, slot->oh = oh, slot->drs = drs, 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);

View File

@@ -178,15 +178,18 @@ int probe(void) {
return 1;
}
// 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").
// One context per render size and output size, kept as vk_ffx.cpp keeps FSR 3.1's (see there): a new render size
// makes a context of its own (50 to 70 ms and up to about 217 MiB of video memory) and the one before goes a few frames
// later, so a mode change costs a creation; with the game's dynamic resolution on one context with
// FFX_UPSCALE_ENABLE_DYNAMIC_RESOLUTION takes every step. Contexts kept for every mode tried, as once, piled up to six
// beside FSR 3.1's and three creations that failed (the card out of memory) turned FSR 4 off for the run; one context
// for every render size, as next, preceded an access violation in AMD's driver inside the dispatch after a switch from
// 67 % to 100 %. 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 mw = 0, mh = 0, ow = 0, oh = 0; // the largest render size it takes; the output size
uint32_t mw = 0, mh = 0, ow = 0, oh = 0; // the render size it was made for (the largest it takes with drs); the output size
bool drs = false; // made for dynamic resolution (FFX_UPSCALE_ENABLE_DYNAMIC_RESOLUTION)
uint64_t used = 0, used_ns = 0; // up_tick and the time when context_for last returned it
ffxCreateContextDescUpscale desc = {};
ffxCreateBackendVKDesc backend = {};
@@ -218,7 +221,7 @@ FfxApiResource image(const VkFfxImage *i, uint32_t state, uint32_t usage) {
}
// 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) {
Up *create(Up *slot, uint32_t mw, uint32_t mh, uint32_t ow, uint32_t oh, bool drs) {
const VkCtx *c = vk_ctx();
*slot = Up();
slot->built.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE_VERSION;
@@ -233,6 +236,9 @@ Up *create(Up *slot, uint32_t mw, uint32_t mh, 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_AUTO_EXPOSURE; // a finite far plane, as FSR 3.1's
// NX2NAVI_FFX_DEBUG=1 (test): the library checks the values of each dispatch and says what it finds (message)
if (rt_knob("FFX_DEBUG", "0")) slot->desc.flags |= FFX_UPSCALE_ENABLE_DEBUG_CHECKING;
if (drs) slot->desc.flags |= FFX_UPSCALE_ENABLE_DYNAMIC_RESOLUTION;
slot->desc.maxRenderSize = {mw, mh};
slot->desc.maxUpscaleSize = {ow, oh};
slot->desc.fpMessage = message;
@@ -244,7 +250,7 @@ Up *create(Up *slot, uint32_t mw, uint32_t mh, uint32_t ow, uint32_t oh) {
*slot = Up();
return nullptr;
}
slot->mw = mw, slot->mh = mh, slot->ow = ow, slot->oh = oh;
slot->mw = mw, slot->mh = mh, slot->ow = ow, slot->oh = oh, slot->drs = drs;
ffxQueryGetProviderVersion v = {};
v.header.type = FFX_API_QUERY_DESC_TYPE_GET_PROVIDER_VERSION;
A.query(&slot->ctx, &v.header);
@@ -266,22 +272,29 @@ Up *create(Up *slot, uint32_t mw, uint32_t mh, 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.
// The context for a render size and an output size: the one made for them, else a new one, in a free slot or in place
// of the least recently used one, with the device idle first; the others of that output size go once no command
// buffer can hold them. 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
const bool drs = cfg_bool("NX2NAVI_DYN_RES");
Up *found = nullptr;
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;
if (u.ctx && u.ow == ow && u.oh == oh && u.drs == drs && (drs ? w <= u.mw && h <= u.mh : w == u.mw && h == u.mh))
found = &u;
const VkCtx *c = vk_ctx();
int waited = 0;
for (Up &u : U) // the contexts of this output size for another render size, once no command buffer holds them
if (u.ctx && &u != found && u.ow == ow && u.oh == oh && up_tick - u.used >= UP_RECENT) {
if (!waited++) vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // their work may be in flight
destroy_up(&u, "the render size changed");
}
if (found) {
found->used = up_tick, found->used_ns = now;
return found;
}
Up *slot = nullptr, *lru = nullptr;
for (Up &u : U) {
if (!u.ctx) {
@@ -290,24 +303,22 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
}
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");
destroy_up(lru, "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);
// the render size itself; for dynamic resolution, every size up to the output's (and a scene at the output size)
uint32_t mw = !drs ? w : w > ow ? w : ow, mh = !drs ? h : h > oh ? h : oh;
Up *u = create(slot, mw, mh, ow, oh, drs);
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 (freed) u = create(slot, mw, mh, ow, oh, drs);
}
if (u) u->used = up_tick, u->used_ns = now;
return u;