From ce87d20f25cf09d425b6d99ba460c6fd26b9cf24 Mon Sep 17 00:00:00 2001 From: bicarus-dev <202771338+bicarus-dev@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:30:45 -0800 Subject: [PATCH] sdvx: cache result from K32GetPerformanceInfo to avoid performance issues on some PCs --- src/spice2x/games/sdvx/sdvx.cpp | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/spice2x/games/sdvx/sdvx.cpp b/src/spice2x/games/sdvx/sdvx.cpp index e20f94b9..e9519c41 100644 --- a/src/spice2x/games/sdvx/sdvx.cpp +++ b/src/spice2x/games/sdvx/sdvx.cpp @@ -9,6 +9,8 @@ #include "hooks/devicehook.h" #include "hooks/libraryhook.h" #ifdef SPICE64 +#include +#include #include "hooks/graphics/nvapi_impl.h" #endif #include "hooks/graphics/nvapi_hook.h" @@ -40,6 +42,9 @@ static decltype(RegEnumKeyA) *RegEnumKeyA_orig = nullptr; static decltype(RegOpenKeyA) *RegOpenKeyA_orig = nullptr; static decltype(RegOpenKeyExA) *RegOpenKeyExA_orig = nullptr; static decltype(RegQueryValueExA) *RegQueryValueExA_orig = nullptr; +#ifdef SPICE64 +static decltype(K32GetPerformanceInfo) *K32GetPerformanceInfo_orig = nullptr; +#endif namespace games::sdvx { @@ -200,6 +205,35 @@ namespace games::sdvx { } #ifdef SPICE64 + + static BOOL WINAPI K32GetPerformanceInfo_hook(PPERFORMANCE_INFORMATION info, DWORD cb) { + if (info == nullptr) { + SetLastError(ERROR_INVALID_PARAMETER); + return FALSE; + } + if (cb < sizeof(*info)) { + SetLastError(ERROR_BAD_LENGTH); + return FALSE; + } + + // store a cached copy of the first successful call to K32GetPerformanceInfo + static std::mutex cache_mutex; + static PERFORMANCE_INFORMATION cached {}; + static bool cached_valid = false; + std::lock_guard lock(cache_mutex); + if (!cached_valid) { + if (!K32GetPerformanceInfo_orig(info, cb)) { + return FALSE; + } + cached = *info; + cached_valid = true; + } + + // return the cached copy to avoid performance issues with the game calling this every frame + *info = cached; + return TRUE; + } + static bool sdvx64_spam_remover(void *user, const std::string &data, logger::Style style, std::string &out) { if (data.empty() || data[0] != '[') { return false; @@ -534,6 +568,14 @@ namespace games::sdvx { if (is_valkyrie_model()) { sysutils::hook_EnumDisplayDevicesA(); } + + // keep one real snapshot for the session instead of querying statistics every frame + K32GetPerformanceInfo_orig = detour::iat_try( + "K32GetPerformanceInfo", K32GetPerformanceInfo_hook, avs::game::DLL_INSTANCE); + if (K32GetPerformanceInfo_orig != nullptr) { + log_info("sdvx", "installed cached hook for K32GetPerformanceInfo"); + } + #endif }