sdvx: cache result from K32GetPerformanceInfo to avoid performance issues on some PCs

This commit is contained in:
bicarus-dev committed 2026-11-19 01:30:45 -08:00
1 parent 3e3db7a411
commit 5bb526c4ba
1 file changed
+35
+35
View File
@@ -9,6 +9,7 @@
#include "hooks/devicehook.h"
#include "hooks/libraryhook.h"
#ifdef SPICE64
#include <psapi.h>
#include "hooks/graphics/nvapi_impl.h"
#endif
#include "hooks/graphics/nvapi_hook.h"
@@ -40,6 +41,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 +204,29 @@ 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 PERFORMANCE_INFORMATION cached {};
static const BOOL cached_valid = K32GetPerformanceInfo_orig(&cached, sizeof(cached));
if (!cached_valid) {
return K32GetPerformanceInfo_orig(info, cb);
}
// 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 +561,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
}