C# · 12月 29, 2021

c# – 强制硬件加速渲染

我有一个用C编写的OpenGL库,它使用C#应用程序使用C/C++LI适配器.我的问题是,如果将应用程序用于使用Nvidia Optimus技术的笔记本电脑,该应用程序将不会使用硬件加速并失败.

我试图使用在Nvidias文档http://developer.download.nvidia.com/devzone/devcenter/gamegraphics/files/OptimusRenderingPolicies.pdf中找到的信息
关于将libs链接到我的C -dll,并从我的OpenGL库导出NvOptimusEnablement但是失败.
我想我不得不与.exe连接到.exe的.exe文件

对于我们来说,使用配置文件不是一个好选择,因为我们需要确保使用nvidia硬件.

有没有一些C#应用程序可以强制Optimus使用Nvidia芯片组而不是集成的英特尔芯片组?

解决方法 如果您的软件在Intel上失败,那么您将无法在50%的笔记本电脑上运行它.所以我建议修改这个.

不如说,您可以通过代码完美地创建配置文件.只需使用NvAPI.
这个代码完全是这样的,但要小心,你可能不应该混淆全局配置文件并创建自己的代码:

NvAPI_Status status;// (0) Initialize NVAPI. This must be done first of allstatus = NvAPI_Initialize();if (status != NVAPI_OK) PrintError(status,__LINE__);// (1) Create the session handle to access driver settingsNvDRSSessionHandle hSession = 0;status = NvAPI_DRS_CreateSession(&hSession);if (status != NVAPI_OK) PrintError(status,__LINE__);// (2) load all the system settings into the sessionstatus = NvAPI_DRS_LoadSettings(hSession);if (status != NVAPI_OK) PrintError(status,__LINE__);// (3) Obtain the Base profile. Any setting needs to be inside// a profile,putting a setting on the Base Profile enforces it// for all the processes on the systemNvDRSProfileHandle hProfile = 0;status = NvAPI_DRS_GetBaseProfile(hSession,&hProfile);if (status != NVAPI_OK) PrintError(status,__LINE__);NVDRS_SETTING dRSSetting1 = {0};dRSSetting1.version = NVDRS_SETTING_VER;dRSSetting1.settingId = SHIM_MCCOMPAT_ID;dRSSetting1.settingType = NVDRS_DWORD_TYPE;NVDRS_SETTING dRSSetting2 = {0};dRSSetting2.version = NVDRS_SETTING_VER;dRSSetting2.settingId = SHIM_RENDERING_MODE_ID;dRSSetting2.settingType = NVDRS_DWORD_TYPE;NVDRS_SETTING dRSSetting3 = {0};dRSSetting3.version = NVDRS_SETTING_VER;dRSSetting3.settingId = SHIM_RENDERING_OPTIONS_ID;dRSSetting3.settingType = NVDRS_DWORD_TYPE;if( ForceIntegrated ){ dRSSetting1.u32CurrentValue = SHIM_MCCOMPAT_INTEGRATED; dRSSetting2.u32CurrentValue = SHIM_RENDERING_MODE_INTEGRATED; dRSSetting3.u32CurrentValue = SHIM_RENDERING_OPTIONS_DEFAULT_RENDERING_MODE | SHIM_RENDERING_OPTIONS_IGPU_TRANSCODING;}else{ dRSSetting1.u32CurrentValue = SHIM_MCCOMPAT_ENABLE; dRSSetting2.u32CurrentValue = SHIM_RENDERING_MODE_ENABLE; dRSSetting3.u32CurrentValue = SHIM_RENDERING_OPTIONS_DEFAULT_RENDERING_MODE;}status = NvAPI_DRS_SetSetting(hSession,hProfile,&dRSSetting1);if (status != NVAPI_OK) PrintError(status,__LINE__);status = NvAPI_DRS_SetSetting(hSession,&dRSSetting2);if (status != NVAPI_OK) PrintError(status,&dRSSetting3);if (status != NVAPI_OK) PrintError(status,__LINE__);// (5) Now we apply (or save) our changes to the systemstatus = NvAPI_DRS_SaveSettings(hSession);if (status != NVAPI_OK) PrintError(status,__LINE__);// (6) We clean up. This is analogous to doing a free()NvAPI_DRS_DestroySession(hSession);hSession = 0;

在启动时,测试您的个人资料是否存在.如果没有,创建它(你可能也必须重新启动自己).
NvAPI是一个静态库,并且将在非NVIDIA硬件上正常返回错误代码,因此您可以安全地发货.

编辑:看起来有一个更简单的方法.来自GLFW 3源代码:

// Applications exporting this symbol with this value will be automatically// directed to the high-performance GPU on nVidia Optimus systems//GLFWAPI DWORD NvOptimusEnablement = 0x00000001;