Skip to content

Commit 86fb866

Browse files
committed
Merge pull request #85188 from aitorciki/nvdrs-fixes
[Windows] Fix NVIDIA app profile creation
2 parents 0404e3a + 159f0af commit 86fb866

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

platform/windows/gl_manager_windows_native.cpp

+38-16
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ typedef int(__cdecl *NvAPI_DRS_CreateApplication_t)(NvDRSSessionHandle, NvDRSPro
8888
typedef int(__cdecl *NvAPI_DRS_SaveSettings_t)(NvDRSSessionHandle);
8989
typedef int(__cdecl *NvAPI_DRS_SetSetting_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_SETTING *);
9090
typedef int(__cdecl *NvAPI_DRS_FindProfileByName_t)(NvDRSSessionHandle, NvAPI_UnicodeString, NvDRSProfileHandle *);
91-
typedef int(__cdecl *NvAPI_DRS_FindApplicationByName_t)(NvDRSSessionHandle, NvAPI_UnicodeString, NvDRSProfileHandle *, NVDRS_APPLICATION *);
91+
typedef int(__cdecl *NvAPI_DRS_GetApplicationInfo_t)(NvDRSSessionHandle, NvDRSProfileHandle, NvAPI_UnicodeString, NVDRS_APPLICATION *);
92+
typedef int(__cdecl *NvAPI_DRS_DeleteProfile_t)(NvDRSSessionHandle, NvDRSProfileHandle);
9293
NvAPI_GetErrorMessage_t NvAPI_GetErrorMessage__;
9394

9495
static bool nvapi_err_check(const char *msg, int status) {
@@ -139,7 +140,8 @@ void GLManagerNative_Windows::_nvapi_disable_threaded_optimization() {
139140
NvAPI_DRS_SaveSettings_t NvAPI_DRS_SaveSettings = (NvAPI_DRS_SaveSettings_t)NvAPI_QueryInterface(0xFCBC7E14);
140141
NvAPI_DRS_SetSetting_t NvAPI_DRS_SetSetting = (NvAPI_DRS_SetSetting_t)NvAPI_QueryInterface(0x577DD202);
141142
NvAPI_DRS_FindProfileByName_t NvAPI_DRS_FindProfileByName = (NvAPI_DRS_FindProfileByName_t)NvAPI_QueryInterface(0x7E4A9A0B);
142-
NvAPI_DRS_FindApplicationByName_t NvAPI_DRS_FindApplicationByName = (NvAPI_DRS_FindApplicationByName_t)NvAPI_QueryInterface(0xEEE566B2);
143+
NvAPI_DRS_GetApplicationInfo_t NvAPI_DRS_GetApplicationInfo = (NvAPI_DRS_GetApplicationInfo_t)NvAPI_QueryInterface(0xED1F8C69);
144+
NvAPI_DRS_DeleteProfile_t NvAPI_DRS_DeleteProfile = (NvAPI_DRS_DeleteProfile_t)NvAPI_QueryInterface(0x17093206);
143145

144146
if (!nvapi_err_check("NVAPI: Init failed", NvAPI_Initialize())) {
145147
return;
@@ -165,23 +167,45 @@ void GLManagerNative_Windows::_nvapi_disable_threaded_optimization() {
165167
}
166168

167169
String app_executable_name = OS::get_singleton()->get_executable_path().get_file();
168-
String app_friendly_name = GLOBAL_GET("application/config/name");
170+
String app_profile_name = GLOBAL_GET("application/config/name");
169171
// We need a name anyways, so let's use the engine name if an application name is not available
170172
// (this is used mostly by the Project Manager)
171-
if (app_friendly_name.is_empty()) {
172-
app_friendly_name = VERSION_NAME;
173+
if (app_profile_name.is_empty()) {
174+
app_profile_name = VERSION_NAME;
173175
}
174-
String app_profile_name = app_friendly_name + " Nvidia Profile";
176+
String old_profile_name = app_profile_name + " Nvidia Profile";
175177
Char16String app_profile_name_u16 = app_profile_name.utf16();
178+
Char16String old_profile_name_u16 = old_profile_name.utf16();
176179
Char16String app_executable_name_u16 = app_executable_name.utf16();
177-
Char16String app_friendly_name_u16 = app_friendly_name.utf16();
180+
181+
// A previous error in app creation logic could result in invalid profiles,
182+
// clean these if they exist before proceeding.
183+
NvDRSProfileHandle old_profile_handle;
184+
185+
int old_status = NvAPI_DRS_FindProfileByName(session_handle, (NvU16 *)(old_profile_name_u16.ptrw()), &old_profile_handle);
186+
187+
if (old_status == 0) {
188+
print_verbose("NVAPI: Deleting old profile...");
189+
190+
if (!nvapi_err_check("NVAPI: Error deleting old profile", NvAPI_DRS_DeleteProfile(session_handle, old_profile_handle))) {
191+
NvAPI_DRS_DestroySession(session_handle);
192+
NvAPI_Unload();
193+
return;
194+
}
195+
196+
if (!nvapi_err_check("NVAPI: Error deleting old profile", NvAPI_DRS_SaveSettings(session_handle))) {
197+
NvAPI_DRS_DestroySession(session_handle);
198+
NvAPI_Unload();
199+
return;
200+
}
201+
}
178202

179203
NvDRSProfileHandle profile_handle = nullptr;
180204

181205
int profile_status = NvAPI_DRS_FindProfileByName(session_handle, (NvU16 *)(app_profile_name_u16.ptrw()), &profile_handle);
182206

183207
if (profile_status != 0) {
184-
print_verbose("NVAPI: Profile not found, creating....");
208+
print_verbose("NVAPI: Profile not found, creating...");
185209

186210
NVDRS_PROFILE profile_info;
187211
profile_info.version = NVDRS_PROFILE_VER;
@@ -195,22 +219,18 @@ void GLManagerNative_Windows::_nvapi_disable_threaded_optimization() {
195219
}
196220
}
197221

198-
NvDRSProfileHandle app_profile_handle = nullptr;
199222
NVDRS_APPLICATION_V4 app;
200223
app.version = NVDRS_APPLICATION_VER_V4;
201224

202-
int app_status = NvAPI_DRS_FindApplicationByName(session_handle, (NvU16 *)(app_executable_name_u16.ptrw()), &app_profile_handle, &app);
225+
int app_status = NvAPI_DRS_GetApplicationInfo(session_handle, profile_handle, (NvU16 *)(app_executable_name_u16.ptrw()), &app);
203226

204227
if (app_status != 0) {
205-
print_verbose("NVAPI: Application not found, adding to profile...");
228+
print_verbose("NVAPI: Application not found in profile, creating...");
206229

207230
app.isPredefined = 0;
208-
app.isMetro = 1;
209-
app.isCommandLine = 1;
210231
memcpy(app.appName, app_executable_name_u16.get_data(), sizeof(char16_t) * app_executable_name_u16.size());
211-
memcpy(app.userFriendlyName, app_friendly_name_u16.get_data(), sizeof(char16_t) * app_friendly_name_u16.size());
212-
memcpy(app.launcher, L"", 1);
213-
memcpy(app.fileInFolder, L"", 1);
232+
memcpy(app.launcher, L"", sizeof(wchar_t));
233+
memcpy(app.fileInFolder, L"", sizeof(wchar_t));
214234

215235
if (!nvapi_err_check("NVAPI: Error creating application", NvAPI_DRS_CreateApplication(session_handle, profile_handle, &app))) {
216236
NvAPI_DRS_DestroySession(session_handle);
@@ -244,11 +264,13 @@ void GLManagerNative_Windows::_nvapi_disable_threaded_optimization() {
244264
NvAPI_Unload();
245265
return;
246266
}
267+
247268
if (thread_control_val == OGL_THREAD_CONTROL_DISABLE) {
248269
print_verbose("NVAPI: Disabled OpenGL threaded optimization successfully");
249270
} else {
250271
print_verbose("NVAPI: Enabled OpenGL threaded optimization successfully");
251272
}
273+
252274
NvAPI_DRS_DestroySession(session_handle);
253275
}
254276

0 commit comments

Comments
 (0)