@@ -88,7 +88,8 @@ typedef int(__cdecl *NvAPI_DRS_CreateApplication_t)(NvDRSSessionHandle, NvDRSPro
88
88
typedef int (__cdecl *NvAPI_DRS_SaveSettings_t)(NvDRSSessionHandle);
89
89
typedef int (__cdecl *NvAPI_DRS_SetSetting_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_SETTING *);
90
90
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);
92
93
NvAPI_GetErrorMessage_t NvAPI_GetErrorMessage__;
93
94
94
95
static bool nvapi_err_check (const char *msg, int status) {
@@ -139,7 +140,8 @@ void GLManagerNative_Windows::_nvapi_disable_threaded_optimization() {
139
140
NvAPI_DRS_SaveSettings_t NvAPI_DRS_SaveSettings = (NvAPI_DRS_SaveSettings_t)NvAPI_QueryInterface (0xFCBC7E14 );
140
141
NvAPI_DRS_SetSetting_t NvAPI_DRS_SetSetting = (NvAPI_DRS_SetSetting_t)NvAPI_QueryInterface (0x577DD202 );
141
142
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 );
143
145
144
146
if (!nvapi_err_check (" NVAPI: Init failed" , NvAPI_Initialize ())) {
145
147
return ;
@@ -165,23 +167,45 @@ void GLManagerNative_Windows::_nvapi_disable_threaded_optimization() {
165
167
}
166
168
167
169
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" );
169
171
// We need a name anyways, so let's use the engine name if an application name is not available
170
172
// (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;
173
175
}
174
- String app_profile_name = app_friendly_name + " Nvidia Profile" ;
176
+ String old_profile_name = app_profile_name + " Nvidia Profile" ;
175
177
Char16String app_profile_name_u16 = app_profile_name.utf16 ();
178
+ Char16String old_profile_name_u16 = old_profile_name.utf16 ();
176
179
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
+ }
178
202
179
203
NvDRSProfileHandle profile_handle = nullptr ;
180
204
181
205
int profile_status = NvAPI_DRS_FindProfileByName (session_handle, (NvU16 *)(app_profile_name_u16.ptrw ()), &profile_handle);
182
206
183
207
if (profile_status != 0 ) {
184
- print_verbose (" NVAPI: Profile not found, creating.... " );
208
+ print_verbose (" NVAPI: Profile not found, creating..." );
185
209
186
210
NVDRS_PROFILE profile_info;
187
211
profile_info.version = NVDRS_PROFILE_VER;
@@ -195,22 +219,18 @@ void GLManagerNative_Windows::_nvapi_disable_threaded_optimization() {
195
219
}
196
220
}
197
221
198
- NvDRSProfileHandle app_profile_handle = nullptr ;
199
222
NVDRS_APPLICATION_V4 app;
200
223
app.version = NVDRS_APPLICATION_VER_V4;
201
224
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);
203
226
204
227
if (app_status != 0 ) {
205
- print_verbose (" NVAPI: Application not found, adding to profile..." );
228
+ print_verbose (" NVAPI: Application not found in profile, creating ..." );
206
229
207
230
app.isPredefined = 0 ;
208
- app.isMetro = 1 ;
209
- app.isCommandLine = 1 ;
210
231
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 ));
214
234
215
235
if (!nvapi_err_check (" NVAPI: Error creating application" , NvAPI_DRS_CreateApplication (session_handle, profile_handle, &app))) {
216
236
NvAPI_DRS_DestroySession (session_handle);
@@ -244,11 +264,13 @@ void GLManagerNative_Windows::_nvapi_disable_threaded_optimization() {
244
264
NvAPI_Unload ();
245
265
return ;
246
266
}
267
+
247
268
if (thread_control_val == OGL_THREAD_CONTROL_DISABLE) {
248
269
print_verbose (" NVAPI: Disabled OpenGL threaded optimization successfully" );
249
270
} else {
250
271
print_verbose (" NVAPI: Enabled OpenGL threaded optimization successfully" );
251
272
}
273
+
252
274
NvAPI_DRS_DestroySession (session_handle);
253
275
}
254
276
0 commit comments