misc: various boot optimizations (#942)

## Link to GitHub Issue or related Pull Request, if one exists
Various fixes copied from https://github.com/l4kr/spicefast

## Description of change
* make local Windows event logs appear empty to game DLLs, avoiding
startup scans without modifying system logs or remote reads.
* when -nvprofile is on, skip writes and profile saves when settings
already match
* defer SetupAPI device name lookups during HID enumeration until it's
absolutely needed
* remove the fixed 100 ms delay after AVS boot

## Testing
This commit is contained in:
bicarus authored and GitHub committed 2026-12-05 03:37:32 -08:00
1 parent 1e0de890bc
commit af66dd5b50
8 files changed
+221 -90

No files matched your search

+49 -78
View File
@@ -155,7 +155,8 @@ namespace nvapi {
log_misc("nvapi", "NVAPI module initialized");
}
NvAPI_Status create_profile_if_needed(NvDRSSessionHandle h_session, NvDRSProfileHandle *h_profile) {
static NvAPI_Status create_profile_if_needed(NvDRSSessionHandle h_session,
NvDRSProfileHandle *h_profile, bool &changed) {
NvAPI_UnicodeString app_name;
#ifdef SPICE64
memcpy_s(app_name, sizeof(app_name), L"spice64.exe", 12 * sizeof(wchar_t));
@@ -203,57 +204,38 @@ namespace nvapi {
return status;
}
changed = true;
return NVAPI_OK;
}
NvAPI_Status set_gpu_power_state(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = PREFERRED_PSTATE_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = PREFERRED_PSTATE_PREFER_MAX;
drs_setting.u32CurrentValue = PREFERRED_PSTATE_PREFER_MAX;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
}
static void apply_dword_setting(NvDRSSessionHandle session, NvDRSProfileHandle profile,
NvU32 id, NvU32 value, const char *name, bool &changed) {
NVDRS_SETTING setting {};
setting.version = NVDRS_SETTING_VER;
auto status = NvAPI_DRS_GetSetting(session, profile, id, &setting);
if (status == NVAPI_OK) {
// an inherited or predefined value is not an explicit application override
if (setting.settingLocation == NVDRS_CURRENT_PROFILE_LOCATION &&
!setting.isCurrentPredefined && setting.settingType == NVDRS_DWORD_TYPE &&
setting.u32CurrentValue == value) {
return;
}
} else if (status != NVAPI_SETTING_NOT_FOUND) {
log_warning("nvapi", "could not read {}: {}", name, get_error_message(status));
}
NvAPI_Status set_gsync_mode(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = VRR_APP_OVERRIDE_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = VRR_APP_OVERRIDE_FIXED_REFRESH;
drs_setting.u32CurrentValue = VRR_APP_OVERRIDE_FIXED_REFRESH;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
}
NvAPI_Status set_vsync_mode(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = VSYNCMODE_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = VSYNCMODE_PASSIVE;
drs_setting.u32CurrentValue = VSYNCMODE_PASSIVE;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
}
NvAPI_Status set_refresh_rate(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = REFRESH_RATE_OVERRIDE_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = REFRESH_RATE_OVERRIDE_APPLICATION_CONTROLLED;
drs_setting.u32CurrentValue = REFRESH_RATE_OVERRIDE_APPLICATION_CONTROLLED;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
}
NvAPI_Status set_max_fps(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = FRL_FPS_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = FRL_FPS_DISABLED;
drs_setting.u32CurrentValue = FRL_FPS_DISABLED;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
setting = {};
setting.version = NVDRS_SETTING_VER;
setting.settingId = id;
setting.settingType = NVDRS_DWORD_TYPE;
setting.u32PredefinedValue = value;
setting.u32CurrentValue = value;
status = NvAPI_DRS_SetSetting(session, profile, &setting);
if (status == NVAPI_OK) {
changed = true;
} else {
log_warning("nvapi", "could not set {}: {}", name, get_error_message(status));
}
}
void set_profile_settings() {
@@ -278,41 +260,30 @@ namespace nvapi {
NvDRSProfileHandle h_profile = nullptr;
log_info("nvapi", "creating NVIDIA profile for spice(64).exe...");
if (create_profile_if_needed(h_session, &h_profile) != NVAPI_OK) {
bool changed = false;
if (create_profile_if_needed(h_session, &h_profile, changed) != NVAPI_OK) {
NvAPI_DRS_DestroySession(h_session);
return;
}
log_info("nvapi", "setting VSync to Application Controlled...");
if ((status = set_vsync_mode(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not set VSync mode: {}", get_error_message(status));
}
apply_dword_setting(h_session, h_profile, VSYNCMODE_ID, VSYNCMODE_PASSIVE,
"VSync mode", changed);
apply_dword_setting(h_session, h_profile, PREFERRED_PSTATE_ID, PREFERRED_PSTATE_PREFER_MAX,
"preferred PState", changed);
apply_dword_setting(h_session, h_profile, VRR_APP_OVERRIDE_ID, VRR_APP_OVERRIDE_FIXED_REFRESH,
"G-SYNC mode", changed);
apply_dword_setting(h_session, h_profile, REFRESH_RATE_OVERRIDE_ID,
REFRESH_RATE_OVERRIDE_APPLICATION_CONTROLLED, "preferred refresh rate", changed);
apply_dword_setting(h_session, h_profile, FRL_FPS_ID, FRL_FPS_DISABLED,
"max framerate", changed);
log_info("nvapi", "applying preferred PState to Maximum Performance...");
if ((status = set_gpu_power_state(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not set preferred PState: {}", get_error_message(status));
}
log_info("nvapi", "disabling G-SYNC...");
if ((status = set_gsync_mode(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not set G-Sync mode: {}", get_error_message(status));
}
log_info("nvapi", "setting preferred refresh rate to Application Controlled...");
if ((status = set_refresh_rate(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not set preferred refresh rate: {}", get_error_message(status));
}
log_info("nvapi", "disabling max framerate...");
if ((status = set_max_fps(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not disable max framerate: {}", get_error_message(status));
}
log_info("nvapi", "saving settings for DRS session...");
if ((status = NvAPI_DRS_SaveSettings(h_session)) != NVAPI_OK) {
log_warning("nvapi", "could not save driver settings: {}", get_error_message(status));
NvAPI_DRS_DestroySession(h_session);
return;
if (changed) {
log_info("nvapi", "saving settings for DRS session...");
if ((status = NvAPI_DRS_SaveSettings(h_session)) != NVAPI_OK) {
log_warning("nvapi", "could not save driver settings: {}", get_error_message(status));
}
} else {
log_misc("nvapi", "no driver profile changes to save");
}
log_info("nvapi", "destroying DRS session...");