Create FSR 3.1's pipelines through the pipeline caches kept across runs, so a context is not compiled again at every start the driver's own cache does not serve.

The library creates its pipelines itself and names no pipeline cache. It is now handed pipeline creates of the runtime's in the place of the device's two, and a create without a cache is asked of the pipeline cache first and compiled into the play cache otherwise. ffx_pipe_cache = 0 turns it off. --probe-ffx-cache makes one upscaling context without a window or the game and says where its pipelines came from, and tests/ffx_cache_check.py runs it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 05:22:56 -05:00
parent b55d2eb3e1
commit d073d80e70
11 changed files with 292 additions and 7 deletions

View File

@@ -10,6 +10,7 @@
#include <windows.h>
#endif
#include <atomic>
#include <chrono>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
@@ -61,6 +62,54 @@ void message(uint32_t type, const wchar_t *msg) {
if (told++ < 40) rt_log("ffx: %s %s", type == FFX_API_MESSAGE_TYPE_ERROR ? "error:" : "warning:", s);
}
// The library's pipeline creates (vk_ffx_proc_addr). It passes no pipeline cache, so one that names none goes through
// the runtime's (vk_pipeline.c vk_pipeline_create_foreign); one that names a cache is the library's own business.
VKAPI_ATTR VkResult VKAPI_CALL compute_pipelines(VkDevice dev, VkPipelineCache cache, uint32_t n,
const VkComputePipelineCreateInfo *ci, const VkAllocationCallbacks *a,
VkPipeline *out) {
if (cache) return vkCreateComputePipelines(dev, cache, n, ci, a, out);
return vk_pipeline_create_foreign(dev, 1, n, ci, a, out);
}
VKAPI_ATTR VkResult VKAPI_CALL graphics_pipelines(VkDevice dev, VkPipelineCache cache, uint32_t n,
const VkGraphicsPipelineCreateInfo *ci, const VkAllocationCallbacks *a,
VkPipeline *out) {
if (cache) return vkCreateGraphicsPipelines(dev, cache, n, ci, a, out);
return vk_pipeline_create_foreign(dev, 0, n, ci, a, out);
}
int pipe_cache_on(void) { // NX2NAVI_FFX_PIPE_CACHE, read once
static std::atomic<int> on{-1};
int v = on.load(std::memory_order_relaxed);
if (v < 0) on.store(v = cfg_bool("NX2NAVI_FFX_PIPE_CACHE"), std::memory_order_relaxed);
return v;
}
// ffxCreateContext with what it made in pipelines: the library compiles every pass of a context inside this call, on
// the thread that asked, so the line says what a context cost and where its pipelines came from.
ffxReturnCode_t create_counted(ffxContext *ctx, ffxCreateContextDescHeader *desc) {
VkForeignPipes p0, p1;
vk_pipeline_foreign_counts(&p0);
auto t0 = std::chrono::steady_clock::now();
ffxReturnCode_t r = A.create(ctx, desc, nullptr);
double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
vk_pipeline_foreign_counts(&p1);
unsigned long long made = p1.made - p0.made, hit = p1.from_cache - p0.from_cache, comp = p1.compiled - p0.compiled,
none = p1.uncached - p0.uncached;
if (r != FFX_API_RETURN_OK) return r;
if (!pipe_cache_on())
rt_log("ffx: context created in %.0f ms; its pipelines are made without the runtime's pipeline caches "
"(NX2NAVI_FFX_PIPE_CACHE=0)", ms);
else if (!made)
rt_log("ffx: context created in %.0f ms; it made no pipeline through the runtime's creates", ms);
else
rt_log("ffx: context created in %.0f ms; its %llu pipelines: %llu from the pipeline caches without a compile, "
"%llu compiled and kept in the play cache for the next start, %llu through the one pipeline cache (what "
"it held is not told apart), %llu with no pipeline cache (none is up yet, or pipeline binaries are on)",
ms, made, hit, comp, made - hit - comp - none, none);
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
@@ -119,7 +168,7 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
be.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_VK;
be.vkDevice = c->device;
be.vkPhysicalDevice = c->phys;
be.vkDeviceProcAddr = vkGetDeviceProcAddr;
be.vkDeviceProcAddr = vk_ffx_proc_addr;
ffxCreateContextDescUpscale d = {};
d.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE;
d.header.pNext = &be.header;
@@ -127,7 +176,7 @@ Up *context_for(uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) {
d.maxRenderSize = {w, h};
d.maxUpscaleSize = {ow, oh};
d.fpMessage = message;
ffxReturnCode_t r = A.create(&slot->ctx, &d.header, nullptr);
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);
slot->ctx = nullptr;
@@ -229,7 +278,55 @@ extern "C" int vk_ffx_upscale(VkCommandBuffer cb, const VkFfxImage *color, const
}
extern "C" ffxReturnCode_t vk_ffx_create(ffxContext *ctx, ffxCreateContextDescHeader *desc) {
return vk_ffx_load() ? A.create(ctx, desc, nullptr) : FFX_API_RETURN_ERROR;
return vk_ffx_load() ? create_counted(ctx, desc) : FFX_API_RETURN_ERROR;
}
// What the library is handed as ffxCreateBackendVKDesc.vkDeviceProcAddr: the device's functions, with the two
// pipeline creates replaced by ours (compute_pipelines, graphics_pipelines) unless NX2NAVI_FFX_PIPE_CACHE is 0.
extern "C" VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_ffx_proc_addr(VkDevice dev, const char *name) {
if (name && pipe_cache_on()) {
if (!strcmp(name, "vkCreateComputePipelines")) return (PFN_vkVoidFunction)compute_pipelines;
if (!strcmp(name, "vkCreateGraphicsPipelines")) return (PFN_vkVoidFunction)graphics_pipelines;
}
return vkGetDeviceProcAddr(dev, name);
}
// --probe-ffx-cache (host.h): one FSR 3.1 upscaling context on the device, without a window or the game, and where
// its pipelines came from. The pipeline caches are those beside the executable, or NX2NAVI_PIPELINE_CACHE's, and are
// saved as after a run, so a second run of the probe shows what the first one's files give
// (tests/ffx_cache_check.py runs it twice on a folder of its own). Fails when the context is not created, or when
// the library made no pipeline through the runtime's creates although they are on.
extern "C" int vk_ffx_probe_cache(void) {
if (!vk_init()) {
printf("probe-ffx-cache: SKIP (no Vulkan device)\n");
return 0;
}
if (!vk_ffx_load()) {
printf("probe-ffx-cache: SKIP (no amd_fidelityfx_vk.dll beside the executable, or a build without FSR 3.1)\n");
return 0;
}
vk_pipeline_push_fn(); // the first vk_pipeline_* call reads the pipeline caches
VkForeignPipes p0, p1;
vk_pipeline_foreign_counts(&p0);
auto t0 = std::chrono::steady_clock::now();
Up *u = context_for(1280, 720, 2560, 1440);
double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
vk_pipeline_foreign_counts(&p1);
unsigned long long made = p1.made - p0.made, hit = p1.from_cache - p0.from_cache, comp = p1.compiled - p0.compiled,
none = p1.uncached - p0.uncached;
int on = pipe_cache_on(), fails = 0;
printf("probe-ffx-cache: context %s in %.0f ms; pipelines made through the runtime's creates: %llu (from the "
"pipeline caches %llu, compiled into the play cache %llu, through the one cache %llu, with no cache %llu); "
"NX2NAVI_FFX_PIPE_CACHE %d\n", u ? "created" : "not created", ms, made, hit, comp, made - hit - comp - none,
none, on);
if (!u) fails++, printf("probe-ffx-cache: FAIL: no context\n");
if (u && on && !made) fails++, printf("probe-ffx-cache: FAIL: the library created its pipelines past the runtime\n");
if (u && !on && made) fails++, printf("probe-ffx-cache: FAIL: pipelines came through the runtime with the setting 0\n");
vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock();
vk_ffx_release();
vk_pipeline_save();
printf("probe-ffx-cache: %s\n", fails ? "FAILED" : "passed");
return fails ? 1 : 0;
}
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) {