When the global FXAA is mistakenly turn ON (which is by default OFF), some of the applications may become blurry and others may not. Either because they are not using 3d rendering or they are whitelisted by the driver internally.

Luckily, we can create our own profile and set custom settings to override the global settings. Here is how.

Download SDK

Go to https://developer.nvidia.com/rtx/path-tracing/nvapi/get-started, sign in with your nVidia account, and download SDK.

Coding

  1. Headers
    Include headers we need.

    #include <nvapi.h>
    #include <NvApiDriverSettings.h>
    
    #pragma comment(lib, "nvapi.lib")
  2. Write a function that initializes NVAPI and finalizes it on complete:

    EXTERN_C bool nvAutoProfiler() {
     NvAPI_Status status = NvAPI_Initialize();
     if (status == NVAPI_OK) {
         bool success = checkAndDisableFXAA();
         NvAPI_Unload();
         return success;
     }
     else {
         showmsg(L"nvapi not available");
         return true;
     }
    }
  3. Create a session to read profiles and its application settings.

     NvDRSSessionHandle hSession = NULL;
     NvDRSProfileHandle hProfile = NULL;
    
     // Create a DRS session
     NvAPI_Status status = NvAPI_DRS_CreateSession(&hSession);
     if (status != NVAPI_OK) {
         showmsg(L"create session failed");
         return false;
     }
     status = NvAPI_DRS_LoadSettings(hSession);
     if (status != NVAPI_OK) {
         showmsg(L"load settings failed");
         return false;
     }
  4. We need to check if the global FXAA is ON, or we don't need to do anything.

    bool isGlobalFXAAEnabled(NvDRSSessionHandle session) {
     NvDRSProfileHandle hGlobalProfile = NULL;
     NvAPI_Status status = NvAPI_DRS_GetCurrentGlobalProfile(session, &hGlobalProfile);
     if (status == NVAPI_OK) {
         // check global settings
         NVDRS_SETTING drsSetting = { 0 };
         drsSetting.version = NVDRS_SETTING_VER;
    
         status = NvAPI_DRS_GetSetting(session, hGlobalProfile, FXAA_ENABLE_ID, &drsSetting);
         if (status == NVAPI_OK && drsSetting.u32CurrentValue == FXAA_ENABLE_OFF) {
             showmsg(L"Global FXAA is off");
             return false;
         }
     }
     showmsg(L"Global FXAA is on");
     return true;
    }
  5. In case FXAA is on, we should fetch existing profile.

    // source from: https://github.com/bo3b/3Dmigoto/blob/02770f1f8033518f3645637d3ea4bcc51609f591/DirectX9/nvprofile.cpp
    
    static wchar_t* exe_path;
    
    // Replacement for GetModuleFileName that will return the passed in filename if
    // we are being run as a privileged helper.
    static DWORD get_exe_path(wchar_t* filename, DWORD size)
    {
     if (exe_path)
         return !wcscpy_s(filename, size, exe_path);
    
     return GetModuleFileName(0, filename, size);
    }
    
    
    static NvDRSProfileHandle get_cur_nv_profile(NvDRSSessionHandle session)
    {
     NvDRSProfileHandle profile = 0;
     NvAPI_Status status = NVAPI_OK;
     NVDRS_APPLICATION app = { 0 };
     wchar_t path[MAX_PATH];
    
     if (!get_exe_path(path, MAX_PATH)) {
         return 0;
     }
    
     app.version = NVDRS_APPLICATION_VER;
     status = NvAPI_DRS_FindApplicationByName(session, (NvU16*)path, &profile, &app);
     if (status != NVAPI_OK) {
    
     }
    
     return profile;
    }
  6. When profile is not found, we create our own one

    static void get_lower_exe_name(wchar_t* name)
    {
     wchar_t path[MAX_PATH];
     wchar_t* exe, * c;
    
     if (!get_exe_path(path, MAX_PATH))
         return;
    
     exe = &wcsrchr(path, L'\\')[1];
     for (c = exe; *c; c++)
         *c = towlower(*c);
    
     wcscpy_s(name, NVAPI_UNICODE_STRING_MAX, exe);
    }
    
    static int add_exe_to_profile(NvDRSSessionHandle session, NvDRSProfileHandle profile, const wchar_t* exe = NULL)
    {
     NvAPI_Status status = NVAPI_OK;
     NVDRS_APPLICATION app = { 0 };
    
     app.version = NVDRS_APPLICATION_VER;
     // Could probably add the full path, but then we would be more likely
     // to hit cases where the profile exists but the executable isn't in
     // it. For now just use the executable name unless this proves to be a
     // problem in practice.
     if (exe) {
         wcscpy_s((wchar_t*)app.appName, NVAPI_UNICODE_STRING_MAX, exe);
     }
     else {
         get_lower_exe_name((wchar_t*)app.appName);
     }
    
     showmsg( (wchar_t*)app.appName);
     status = NvAPI_DRS_CreateApplication(session, profile, &app);
     if (status != NVAPI_OK) {
         showmsg("Error adding app to profile: ");
         return -1;
     }
    
     return 0;
    }
    
    static void generate_profile_name(wchar_t* name)
    {
     wchar_t path[MAX_PATH];
     wchar_t* exe, * ext;
    
     if (!get_exe_path(path, MAX_PATH))
         return;
    
     exe = &wcsrchr(path, L'\\')[1];
     ext = wcsrchr(path, L'.');
     if (ext)
         *ext = L'\0';
    
     wcscpy_s(name, NVAPI_UNICODE_STRING_MAX, exe);
    }
    
    static NvDRSProfileHandle create_profile(NvDRSSessionHandle session)
    {
     NvAPI_Status status = NVAPI_OK;
     NvDRSProfileHandle profile = 0;
     NVDRS_PROFILE info = { 0 };
    
     info.version = NVDRS_PROFILE_VER;
     generate_profile_name((wchar_t*)info.profileName);
    
     // In the event that the profile name already exists, add us to it:
     status = NvAPI_DRS_FindProfileByName(session, info.profileName, &profile);
     if (status == NVAPI_OK) {
         showmsg((wchar_t*)info.profileName);
         if (add_exe_to_profile(session, profile)) {
             showmsg("adding exe failed");
             return 0;
         }
         if (add_exe_to_profile(session, profile, L"more executables.exe")) {
             showmsg("adding more files failed");
             return 0;
         }
         return profile;
     }
    
     showmsg((wchar_t*)info.profileName);
     status = NvAPI_DRS_CreateProfile(session, &info, &profile);
     if (status != NVAPI_OK) {
         showmsg("Error creating profile: ");
         return 0;
     }
    
     if (add_exe_to_profile(session, profile)) {
         showmsg("adding exe failed");
         return 0;
     }
     if (add_exe_to_profile(session, profile, L"more executables.exe")) {
         showmsg("adding more files failed");
         return 0;
     }
     return profile;
    }
  7. Now we got our profile, check if its settings need update

    // source from: https://github.com/GaijinEntertainment/DagorEngine/blob/71a26585082f16df80011e06e7a4e95302f5bb7f/prog/engine/drv/drv3d_commonCode/gpuConfig.cpp
    
     NVDRS_SETTING drsSetting = { 0 };
     drsSetting.version = NVDRS_SETTING_VER;
    
     status = NvAPI_DRS_GetSetting(hSession, hProfile, FXAA_ENABLE_ID, &drsSetting);
     if (status == NVAPI_OK && drsSetting.u32CurrentValue != FXAA_ENABLE_OFF)
     {
         drsSetting.u32CurrentValue = FXAA_ENABLE_OFF;
         status = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
         if (status != NVAPI_OK) {
             showmsg(L"set settings failed");
             return false;
         }
         status = NvAPI_DRS_SaveSettings(hSession);
         if (status != NVAPI_OK) {
             showmsg(L"save settings failed");
             return false;
         }
     }
  8. Destroy the session and return

     NvAPI_DRS_DestroySession(hSession);
     showmsg(L"profile updated");
     return true;