英伟达于2010年发布了管理显卡的C语言编程库NvApi,用它可以方便地读取英伟达显卡的硬件、驱动信息和修改驱动设置。调用opengl、directX或者CUDA提供的API,可以直接使用GPU进行计算。但是当我们需要监控GPU的运行情况时,比如需要监控温度和功耗, 或者修改驱动的参数时,比如动态地调整GPU的电源管理模式和渲染采样方式等,就需要使用到NvApi.



这个库使用起来比较简单,但是网络上的资料都有些老了,而NvApi也有了些更新,不能一味照抄。


int main(){
//初始化NVAPI库
NvAPI_Status ret = NvAPI_Initialize();
if (ret != NVAPI_OK)
{
NvAPI_ShortString errorMsg;
NvAPI_GetErrorMessage(ret, errorMsg);
printf(“初始化NVAPI失败:%s\n”, errorMsg);
return 1;
}
printf(“成功初始化NVAPI\n”);
//连接驱动设置服务
NvDRSSessionHandle hSession;
ret = NvAPI_DRS_CreateSession(&hSession);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("DRS会话创建失败:%s\n", errorMsg);
    return 2;
}
printf("成功连接驱动设置服务\n");

ret = NvAPI_DRS_LoadSettings(hSession);
if (ret != NVAPI_OK) {
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("加载设置失败:%s\n", errorMsg);
    return 2;
}
printf("成功加载设置\n");

NvDRSProfileHandle hProfile;
ret = NvAPI_DRS_GetCurrentGlobalProfile(hSession, &hProfile);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("获取当前配置失败:%s\n", errorMsg);
    return 2;
}
printf("成功获取当前配置\n");

//垂直同步
NVDRS_SETTING drsSetting = { 0 };
drsSetting.version = NVDRS_SETTING_VER;
drsSetting.settingId = VSYNCMODE_ID;
drsSetting.settingType = NVDRS_DWORD_TYPE;
drsSetting.u32CurrentValue = VSYNCMODE_FORCEON;
ret = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("修改垂直同步失败:%s\n", errorMsg);
    return 2;
}
printf("成功修改垂直同步设置\n");

//平滑处理 - FXAA
drsSetting = { 0 };
drsSetting.version = NVDRS_SETTING_VER;
drsSetting.settingId = FXAA_ENABLE_ID;
drsSetting.settingType = NVDRS_DWORD_TYPE;
drsSetting.u32CurrentValue = FXAA_ENABLE_ON;
ret = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("修改平滑处理 - FXAA失败:%s\n", errorMsg);
    return 2;
}
printf("成功修改平滑处理 - FXAA设置\n");

//平滑处理 - 模式
drsSetting = { 0 };
drsSetting.version = NVDRS_SETTING_VER;
drsSetting.settingId = AA_MODE_SELECTOR_ID;
drsSetting.settingType = NVDRS_DWORD_TYPE;
drsSetting.u32CurrentValue = AA_MODE_SELECTOR_OVERRIDE;
ret = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("修改平滑处理 - 模式失败:%s\n", errorMsg);
    return 2;
}
printf("成功修改平滑处理 - 模式设置\n");

//平滑处理 - 灰度校正
drsSetting = { 0 };
drsSetting.version = NVDRS_SETTING_VER;
drsSetting.settingId = AA_MODE_GAMMACORRECTION_ID;
drsSetting.settingType = NVDRS_DWORD_TYPE;
drsSetting.u32CurrentValue = AA_MODE_GAMMACORRECTION_ON_ALWAYS;
ret = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("修改平滑处理 - 灰度校正失败:%s\n", errorMsg);
    return 2;
}
printf("成功修改平滑处理 - 灰度校正设置\n");

//平滑处理 - 设置
drsSetting = { 0 };
drsSetting.version = NVDRS_SETTING_VER;
drsSetting.settingId = AA_MODE_METHOD_ID;
drsSetting.settingType = NVDRS_DWORD_TYPE;
drsSetting.u32CurrentValue = AA_MODE_METHOD_MULTISAMPLE_8X;
ret = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("修改平滑处理 - 设置失败:%s\n", errorMsg);
    return 2;
}
printf("成功修改平滑处理 - 设置设置\n");

//平滑处理 - 透明度

drsSetting = { 0 };
drsSetting.version = NVDRS_SETTING_VER;
drsSetting.settingId = AA_MODE_REPLAY_ID;
drsSetting.settingType = NVDRS_DWORD_TYPE;
drsSetting.u32CurrentValue = 0x34;
ret = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("修改平滑处理 - 透明度失败:%s\n", errorMsg);
    return 2;
}
printf("成功修改平滑处理 - 透明度设置\n");

//电源管理模式
drsSetting = { 0 };
drsSetting.version = NVDRS_SETTING_VER;
drsSetting.settingId = PREFERRED_PSTATE_ID;
drsSetting.settingType = NVDRS_DWORD_TYPE;
drsSetting.u32CurrentValue = PREFERRED_PSTATE_PREFER_MAX;
ret = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("修改电源管理模式失败:%s\n", errorMsg);
    return 2;
}
printf("成功修改电源管理模式设置\n");

// 保存设置
ret = NvAPI_DRS_SaveSettings(hSession);
if (ret != NVAPI_OK)
{
    NvAPI_ShortString errorMsg;
    NvAPI_GetErrorMessage(ret, errorMsg);
    printf("应用设置失败:%s\n", errorMsg);
    return 2;
}
printf("Success ALL\n 成功应用设置\n");

NvAPI_DRS_DestroySession(hSession);
hSession = 0;

return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据