diff --git a/docs/building.md b/docs/building.md index 1afc3618..9bc1be7e 100644 --- a/docs/building.md +++ b/docs/building.md @@ -405,6 +405,15 @@ value no machine has free. What the builders do when they are refused (`warm_bui `lw_worker`) shows in a run's log: start the game with `build_free_mb` set above the machine's free commit and read the `vk_pipeline:` lines that name the commit headroom. +A change to how FSR 3.1's pipelines reach the pipeline caches (`runtime/vk_ffx.cpp` `vk_ffx_proc_addr`, +`runtime/vk_pipeline.c` `vk_pipeline_create_foreign`) passes `python tests\ffx_cache_check.py `. It runs +`--probe-ffx-cache` (one upscaling context on the device; no window, no game) twice on pipeline cache files of its own +and once with `ffx_pipe_cache = 0`: the library's pipelines must come through the runtime's creates, the second run +must compile none of them, and with the setting off none may come through. The executable needs +`amd_fidelityfx_vk.dll` beside it; without it the probe and the test skip. The graphics driver's own cache can serve +the first run too, so to see the files alone do it, run the probe from a copy of the executable under a name the +driver has not seen: its first run compiles, its second does not. + A change to the start's memory warning (`runtime/mem_need.h`, `runtime/memory.c` `mem_start_note`) passes `python tests\mem_need_check.py []`, which builds the header alone and checks when the game's need counts as short. With the executable it also runs `--probe-memneed` (it prints the machine's commit and the answer and diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 1e447edc..d2d42264 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -180,6 +180,10 @@ # pipeline_cache_play = 1 # The size (MB) at which pipeline-cache-play.bin is folded into pipeline-cache.bin at a start. # pipeline_cache_play_mb = 64 +# 1: the pipelines FSR 3.1 makes for upscaling and frame generation are kept in the pipeline cache files, so a later +# start does not compile them again. 0: FSR 3.1 creates them without those files, as the graphics driver alone keeps +# them. +# ffx_pipe_cache = 1 # 1: where the driver has VK_KHR_pipeline_binary, compiled pipelines are kept in pipeline-binaries.bin under the # runtime's own keys, so a renamed or updated exe starts without compiling them again (pipeline-cache.bin is then # not used). Default 0 (pipeline-cache.bin only) while it is being tested. diff --git a/runtime/cfg.c b/runtime/cfg.c index c352c9c9..89f28e74 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -46,7 +46,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_CAM_KEEP", "DLSS_IDLE_FREE", "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", "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", - "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", "FREE_CORES", + "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", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "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", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_HUE_CLIP", "HDR_LIVE", "HEAP_LAZY", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", @@ -136,7 +136,7 @@ static const struct { const char *key, *section; } sections[] = { {"COMPUTE_KEEP", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, - {"BIND_SKIP", "performance"}, {"SHADER_CACHE_FLUSH", "performance"}, {"SHADER_CARRY", "performance"}, {"PIPELINE_CACHE_PLAY", "performance"}, {"PIPELINE_CACHE_PLAY_MB", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, + {"BIND_SKIP", "performance"}, {"SHADER_CACHE_FLUSH", "performance"}, {"SHADER_CARRY", "performance"}, {"PIPELINE_CACHE_PLAY", "performance"}, {"PIPELINE_CACHE_PLAY_MB", "performance"}, {"FFX_PIPE_CACHE", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_IDLE", "performance"}, {"SPLIT_IN_PASS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"}, {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"VK_NAN_BITS", "performance"}, @@ -281,6 +281,7 @@ static const CfgMeta metas[] = { {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "", "0|1", RESTART}, // vk_caps.c vk_caps_gpl: by the driver, off with AMD's for RDNA 2 and older {"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART}, {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, + {"FFX_PIPE_CACHE", "1", "0|1", RESTART}, // vk_ffx.cpp vk_ffx_proc_addr: FSR 3.1's pipelines through the pipeline caches {"PIPELINE_CACHE_PLAY", "1", "0|1", RESTART}, {"PIPELINE_CACHE_PLAY_MB", "64", "1..65536", RESTART}, // vk_pipeline.c "the play cache": what a run compiles goes into a file of its own {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "1", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, {"BIG_BUILD_MAX_MB", "4096", "0..262144", RESTART}, diff --git a/runtime/host.h b/runtime/host.h index 5c29f73d..37103c49 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -444,6 +444,7 @@ int vk_pipeline_warm_shaders(void); // vk_pipeline.c: --w int vk_pipeline_prepare_pipelines(void); // vk_pipeline.c: --prepare-pipelines (the shared pipeline list) int vk_pipeline_probe_prewarm(void); // vk_pipeline.c: --probe-prewarm int vk_pipeline_probe_buildgate(void); // vk_pipeline.c: --probe-buildgate +int vk_ffx_probe_cache(void); // vk_ffx.cpp: --probe-ffx-cache // vk_device.c: the drivers' own shader caches beside the exe (MESA_SHADER_CACHE_DIR, on NVIDIA's Linux driver // __GL_SHADER_DISK_CACHE_PATH and _SIZE) in the environment, once; before any thread can load a Vulkan driver void vk_driver_cache_env(void); diff --git a/runtime/main.c b/runtime/main.c index 5517868e..14b123c3 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -23,6 +23,8 @@ // --probe-nvn, --probe-nvn-tex, --probe-window, --probe-shader, --probe-vkmem, --probe-vkexec graphics probes // --probe-prewarm the boot's pipeline stages from the caches next to the executable, the memory each leaves and // what a compute pipeline's first dispatch costs; skips without recorded shaders, runs no game +// --probe-ffx-cache one FSR 3.1 upscaling context and where its pipelines came from (the pipeline caches kept +// across runs); skips without amd_fidelityfx_vk.dll, runs no game // --probe-astc the compute ASTC decoder against astc_decode_ldr; needs no game files // --probe-zstd host zstd streams (ov_zstd.c) on frames made here; needs no game files // --probe-input gamepad polls on a virtual gamepad, also while SDL's update takes long; needs no game files @@ -534,7 +536,7 @@ static int usage(const char *argv0) { "--probe-cfg | --probe-dpi | --probe-mods | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " "--probe-nvn | --probe-nvn-tex | --probe-window | --probe-shader | --probe-vkmem | " "--probe-vkexec | --probe-astc | --probe-zstd | --probe-input | --probe-movie | --probe-crash-call | --probe-crash-null | --probe-discord | --probe-coop | " - "--prepare-shaders | --warm-shaders | --prepare-pipelines | --probe-prewarm | --probe-buildgate | --probe-memneed | --probe-devlost | --boot |--seconds S [--shot PNG]]\n", argv0); + "--prepare-shaders | --warm-shaders | --prepare-pipelines | --probe-prewarm | --probe-buildgate | --probe-ffx-cache | --probe-memneed | --probe-devlost | --boot |--seconds S [--shot PNG]]\n", argv0); return 2; } @@ -696,6 +698,7 @@ int main(int argc, char **argv) { if (!strcmp(mode, "--prepare-pipelines")) return vk_pipeline_prepare_pipelines(); if (!strcmp(mode, "--probe-prewarm")) return vk_pipeline_probe_prewarm(); if (!strcmp(mode, "--probe-buildgate")) return vk_pipeline_probe_buildgate(); + if (!strcmp(mode, "--probe-ffx-cache")) return vk_ffx_probe_cache(); if (!strcmp(mode, "--probe-memneed")) { mem_start_note(1); return 0; } if (!strcmp(mode, "--boot")) { mem_start_note(0); cfg.boot = 1; rt_boot(); return 0; } return usage(argv[0]); diff --git a/runtime/vk.h b/runtime/vk.h index 3d2c4981..0ab3966a 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -874,5 +874,13 @@ void vk_pipeline_autosave(void); // vk_pipeline_save at most every 20 void vk_pipeline_shutdown(void); // waits for background builds, saves, destroys pipelines, layouts and the // cache; before vk_shutdown int vk_pipeline_selftest(void); // one graphics and one compute pipeline from census shaders; 0 on success +// Pipelines a library creates on our device without a pipeline cache of its own (FSR 3.1, vk_ffx.cpp), through the +// pipeline caches kept across runs; as vkCreateComputePipelines (compute 1) or vkCreateGraphicsPipelines with no cache. +VkResult vk_pipeline_create_foreign(VkDevice dev, int compute, uint32_t n, const void *infos, + const VkAllocationCallbacks *alloc, VkPipeline *out); +// How many it made so far: from_cache came without a compile and compiled went into the play cache (both only with a +// play cache, which is what can tell them apart); uncached were created with no cache of ours. +typedef struct { uint64_t made, from_cache, compiled, uncached; } VkForeignPipes; +void vk_pipeline_foreign_counts(VkForeignPipes *o); #include "vk_rec.h" // last: its macros route the vkCmd* calls of every file that includes vk.h diff --git a/runtime/vk_ffx.cpp b/runtime/vk_ffx.cpp index 8f4d3f16..2f232579 100644 --- a/runtime/vk_ffx.cpp +++ b/runtime/vk_ffx.cpp @@ -10,6 +10,7 @@ #include #endif #include +#include #include #include #include @@ -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 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(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(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) { diff --git a/runtime/vk_ffx.h b/runtime/vk_ffx.h index 3feb8e0d..ba1252f4 100644 --- a/runtime/vk_ffx.h +++ b/runtime/vk_ffx.h @@ -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 +// 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); #ifdef __cplusplus // The API's calls themselves, for vk_fg.cpp (0 = FFX_API_RETURN_OK): the library loaded first (vk_ffx_load). diff --git a/runtime/vk_fg.cpp b/runtime/vk_fg.cpp index 107c4717..f886644c 100644 --- a/runtime/vk_fg.cpp +++ b/runtime/vk_fg.cpp @@ -200,7 +200,7 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, 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; // the HUD-less picture has the swapchain's format (gfx_present.c hudless_record) ffxCreateContextDescFrameGenerationHudless hl = {}; hl.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATION_HUDLESS; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index cce67e01..960b746e 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -593,6 +593,82 @@ static VkResult pipe_create_in(int kind, void *ci, VkPipeline *pipe, VkPipelineC return r; } +// ---- pipelines of a library that draws on our device (FSR 3.1: vk_ffx.cpp vk_ffx_proc_addr) +// Such a library creates its pipelines itself and passes no pipeline cache, so nothing of ours kept them: every +// context it made (one per render and output size, and one for frame generation) compiled its passes again unless the +// driver's own disk cache still had them, which a renamed or updated exe on NVIDIA and a driver without a disk cache +// do not. vk_ffx.cpp hands the library these creates in the place of vkCreateComputePipelines and +// vkCreateGraphicsPipelines; a create that names no cache goes through ours as pipe_create_in does: asked of P.cache +// first without compiling, and compiled into the play cache when neither it nor the driver has the pipeline, so the +// file a run writes (pipeline-cache-play.bin, or pipeline-cache.bin without a play cache) holds it for the next start. +// Before the first pipeline of ours (P.ready 0) and with pipeline binaries on there is no cache to give: the create +// goes to the driver as the library asked. infos: n VkComputePipelineCreateInfo (compute 1) or +// VkGraphicsPipelineCreateInfo. Any thread. +static struct { volatile uint64_t made, from_cache, compiled, uncached; } FP; + +void vk_pipeline_foreign_counts(VkForeignPipes *o) { + o->made = __atomic_load_n(&FP.made, __ATOMIC_RELAXED), o->from_cache = __atomic_load_n(&FP.from_cache, __ATOMIC_RELAXED); + o->compiled = __atomic_load_n(&FP.compiled, __ATOMIC_RELAXED), o->uncached = __atomic_load_n(&FP.uncached, __ATOMIC_RELAXED); +} + +static VkResult foreign_one(VkDevice dev, int compute, VkPipelineCache cache, uint32_t n, const void *ci, + const VkAllocationCallbacks *alloc, VkPipeline *out) { + return compute ? vkCreateComputePipelines(dev, cache, n, ci, alloc, out) + : vkCreateGraphicsPipelines(dev, cache, n, ci, alloc, out); +} + +VkResult vk_pipeline_create_foreign(VkDevice dev, int compute, uint32_t n, const void *infos, + const VkAllocationCallbacks *alloc, VkPipeline *out) { + VkPipelineCache cache = __atomic_load_n(&P.ready, __ATOMIC_ACQUIRE) && dev == P.device ? P.cache : VK_NULL_HANDLE; + if (!cache) { + VkResult r = foreign_one(dev, compute, VK_NULL_HANDLE, n, infos, alloc, out); + if (r == VK_SUCCESS) __atomic_fetch_add(&FP.made, n, __ATOMIC_RELAXED), __atomic_fetch_add(&FP.uncached, n, __ATOMIC_RELAXED); + return r; + } + VkResult r = VK_SUCCESS; + plat_mutex_lock_shared(&cache_rw); + if (!P.play) { // one cache: whether it held them is not known here; its save looks at the data's size + r = foreign_one(dev, compute, cache, n, infos, alloc, out); + if (r == VK_SUCCESS) __atomic_fetch_add(&FP.made, n, __ATOMIC_RELAXED), __atomic_fetch_add(&cache_merges, n, __ATOMIC_RELAXED); + } + for (uint32_t i = 0; P.play && i < n; i++) { + // a copy of the create info: the flags are ours to change, the library's struct is not + VkComputePipelineCreateInfo cc; + VkGraphicsPipelineCreateInfo gc; + void *ci; + if (compute) cc = ((const VkComputePipelineCreateInfo *)infos)[i], ci = &cc; + else gc = ((const VkGraphicsPipelineCreateInfo *)infos)[i], ci = &gc; + PipeCiHead *hd = ci; + // Flags given in a VkPipelineCreateFlags2CreateInfo replace the struct's own, so the question "without + // compiling" cannot be put through hd->flags: such a create goes to the play cache at once. + int flags2 = 0; + for (const VkBaseInStructure *b = hd->pNext; b && !flags2; b = b->pNext) + flags2 = b->sType == VK_STRUCTURE_TYPE_PIPELINE_CREATE_FLAGS_2_CREATE_INFO_KHR; + VkPipelineCreateFlags flags = hd->flags; + VkResult ri = VK_PIPELINE_COMPILE_REQUIRED; + out[i] = VK_NULL_HANDLE; + if (!flags2) { + hd->flags = flags | VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT; + ri = foreign_one(dev, compute, cache, 1, ci, alloc, &out[i]); + hd->flags = flags; + if (ri == VK_SUCCESS) __atomic_fetch_add(&FP.from_cache, 1, __ATOMIC_RELAXED); + } + if (ri == VK_PIPELINE_COMPILE_REQUIRED) { + ri = foreign_one(dev, compute, P.play, 1, ci, alloc, &out[i]); + if (ri == VK_SUCCESS) { + __atomic_fetch_add(&FP.compiled, 1, __ATOMIC_RELAXED); + __atomic_fetch_add(&P.play_made, 1, __ATOMIC_RELAXED); + __atomic_fetch_add(&cache_merges, 1, __ATOMIC_RELAXED); // autosave_once: the play file is due + } + } + if (ri == VK_SUCCESS) __atomic_fetch_add(&FP.made, 1, __ATOMIC_RELAXED); + else if (r == VK_SUCCESS) r = ri; // the first that failed, as a create of several reports one result + if (ri != VK_SUCCESS) out[i] = VK_NULL_HANDLE; + } + plat_mutex_unlock_shared(&cache_rw); + return r; +} + static VkResult pipe_create(int kind, void *ci, VkPipeline *pipe) { PipeCiHead *hd = ci; VkResult r; diff --git a/tests/ffx_cache_check.py b/tests/ffx_cache_check.py new file mode 100644 index 00000000..6a4c66d4 --- /dev/null +++ b/tests/ffx_cache_check.py @@ -0,0 +1,83 @@ +"""Run the game executable's --probe-ffx-cache twice on pipeline cache files of the test's own +(runtime/vk_ffx.cpp vk_ffx_probe_cache: one FSR 3.1 upscaling context, without a window or the game). + +FSR 3.1's library creates its pipelines itself and names no pipeline cache; the runtime hands it pipeline creates that +go through the pipeline caches kept across runs (runtime/vk_pipeline.c vk_pipeline_create_foreign). Checked here: + +- the library's pipelines come through those creates (the first run counts them); +- a second run, which reads what the first one saved, compiles none of them: each comes from the caches; +- with ffx_pipe_cache = 0 none comes through the runtime. + +The executable needs amd_fidelityfx_vk.dll beside it and a Vulkan device; without either the probe says SKIP and so +does this test. The pipeline cache files go to build/test/ffx_cache, never beside the executable. + +Usage: ffx_cache_check.py PATH_TO_GAME_EXE +""" +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +LINE = re.compile(r'probe-ffx-cache: context (created|not created) in \d+ ms; pipelines made through the runtime\'s ' + r'creates: (\d+) \(from the pipeline caches (\d+), compiled into the play cache (\d+), through the ' + r'one cache (\d+), with no cache (\d+)\)') + + +def run(game, cache, setting): + """One probe run -> (exit code, last probe line, the counts or None).""" + env = dict(os.environ) + env['NX2NAVI_PIPELINE_CACHE'] = str(cache) + env.pop('NX2NAVI_PIPE_BINARIES', None) + env['NX2NAVI_FFX_PIPE_CACHE'] = setting + r = subprocess.run([game, '--probe-ffx-cache'], env=env, capture_output=True, text=True, errors='replace') + lines = [ln for ln in r.stdout.splitlines() if ln.startswith('probe-ffx-cache:')] + counts = None + for ln in lines: + m = LINE.match(ln) + if m: + counts = dict(zip(('made', 'cached', 'compiled', 'one', 'none'), map(int, m.groups()[1:]))) + print('\n'.join(lines)) + return r.returncode, lines[-1] if lines else '', counts + + +def main(): + if len(sys.argv) != 2: + print(__doc__) + return 2 + game = sys.argv[1] + work = ROOT / 'build' / 'test' / 'ffx_cache' + shutil.rmtree(work, ignore_errors=True) + work.mkdir(parents=True) + cache = work / 'pipeline-cache.bin' + rc, last, first = run(game, cache, '1') + if 'SKIP' in last: + print('ffx_cache_check: SKIP') + return 0 + fails = [] + if rc or not last.endswith('passed') or not first: + fails.append('the first run did not pass') + elif not first['made']: + fails.append('the first run made no pipeline through the runtime') + rc, last, second = run(game, cache, '1') + if rc or not last.endswith('passed') or not second: + fails.append('the second run did not pass') + elif first and (second['made'] != first['made'] or second['compiled']): + fails.append('the second run made %d pipelines and compiled %d of them: with the first run\'s files it must ' + 'make the same %d and compile none' % (second['made'], second['compiled'], first['made'])) + elif first and not first['one'] and not first['none'] and second['cached'] != second['made']: + fails.append('with a play cache every pipeline of the second run comes from the caches: %d of %d did' + % (second['cached'], second['made'])) + rc, last, off = run(game, cache, '0') + if rc or not last.endswith('passed') or not off or off['made']: + fails.append('with ffx_pipe_cache = 0 the run must pass with no pipeline made through the runtime') + for f in fails: + print('ffx_cache_check: FAIL: ' + f) + print('ffx_cache_check: %s' % ('FAILED' if fails else 'passed')) + return 1 if fails else 0 + + +if __name__ == '__main__': + sys.exit(main())