Skip to content

Bring PS5 GTA6 to 120FPS with DLSS 5. An advanced Windows video player and capture enhancement tool featuring real-time preview for videos, images, and capture card sources. Combine AI-powered Super Resolution, DLSS5 image enhancement, and Frame Generation to deliver a next-generation visual experience.

C++
6.1 KB
Raw Blame
#pragma once
#include <windows.h>
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <cwchar>
#include <mutex>
#include <string>
namespace veyra::gfx {
// Variable refresh (G-SYNC / G-SYNC Compatible) state of the monitor showing
// a window. Veyra never changes the driver setting; the user enables it in
// the NVIDIA Control Panel, and the 可变刷新 switch only decides how Veyra
// presents when it is available (VideoPresenter::configurePresentation).
// Minimal ABI declarations checked against NVIDIA/nvapi public interfaces
// (nvapi_interface.h function IDs, NV_GET_VRR_INFO_V1 and
// NV_MONITOR_CAPABILITIES_V1 layouts), MIT; same source and loading rules as
// ReflexSession: System32 nvapi64.dll only, pinned for the life of the
// process. No SDK header or runtime is distributed.
struct VrrState {
    bool known=false;      // NVIDIA driver answered for this monitor
    bool possible=false;   // VRR mode is possible on the display (bIsVRRPossible)
    bool requested=false;  // VRR mode is requested for the display (bIsVRRRequested)
    bool enabled=false;    // VRR mode is currently active on the display (bIsVRREnabled)
    bool indicator=false;  // the driver's VRR indicator overlay is on
    bool inVrrMode=false;  // the display itself reports VRR mode
    bool capsKnown=false;  // generic monitor capabilities answered
    bool supportsVrr=false;      // the monitor supports VRR at all (supportVRR)
    bool currentlyCapable=false; // ... with the applied display settings
    bool trueGsync=false;        // G-SYNC module, not adaptive sync
    bool simulated=false;        // VEYRA_TEST_VRR_LEVEL override
    int status=0;          // last NvAPI status (0 = OK)
    bool operator==(const VrrState&)const=default;
};
// A display may be capable before an application's VRR presentation starts.
// In particular G-SYNC Compatible can report all GetVRRInfo flags clear while
// generic monitor capabilities say that the applied mode supports VRR (#45).
// Permit the user's presentation request then, without inventing active flags
// or assuming that the NVIDIA Control Panel setting is off.
constexpr bool vrrPresentationAvailable(const VrrState& s){
    return s.enabled||s.inVrrMode||s.possible||s.requested
        ||(s.capsKnown&&s.supportsVrr&&s.currentlyCapable);
}
// 0 unknown, 1 unsupported, 2 supported but unavailable in the applied mode,
// 3 presentation may request VRR (activation unconfirmed), 4 driver reports active.
constexpr unsigned vrrLevel(const VrrState& s){
    if(!s.known)return 0;
    if(s.enabled||s.inVrrMode)return 4;
    if(vrrPresentationAvailable(s))return 3;
    if(s.supportsVrr||s.currentlyCapable)return 2;
    return 1;
}
inline VrrState queryVrr(HWND window){
    // Test hook (VEYRA_TEST_VRR_LEVEL=0..4): this rig's monitor reports no VRR
    // support, so UI and presentation-rule tests simulate a level.
    static const int testLevel=[]{wchar_t v[4]{};return GetEnvironmentVariableW(L"VEYRA_TEST_VRR_LEVEL",v,4)?std::clamp(_wtoi(v),0,4):-1;}();
    if(testLevel>=0){
        VrrState s;s.simulated=true;s.known=testLevel>0;s.capsKnown=s.known;
        s.supportsVrr=testLevel>=2;s.currentlyCapable=testLevel>=3;s.possible=s.requested=testLevel>=3;s.enabled=s.inVrrMode=testLevel>=4;
        return s;
    }
    struct Info {uint32_t version;uint32_t flags;uint32_t reservedEx[4];};
    static_assert(sizeof(Info)==24);
    // NV_MONITOR_CAPABILITIES_V1: the union starts right after the one-byte
    // bIsValidInfo; NV_MONITOR_CAPS_VSDB (49 bytes) is its largest member.
    struct Caps {uint32_t version;uint16_t size;uint32_t infoType;uint32_t connectorType;uint8_t valid;uint8_t data[49];};
    static_assert(sizeof(Caps)==68);
    using Query=void*(__cdecl*)(uint32_t);
    using Init=int(__cdecl*)();
    using IdByName=int(__cdecl*)(const char*,uint32_t*);
    using GetVrr=int(__cdecl*)(uint32_t,Info*);
    using GetCaps=int(__cdecl*)(uint32_t,Caps*);
    static std::once_flag once;static IdByName idByName=nullptr;static GetVrr getVrr=nullptr;static GetCaps getCaps=nullptr;static bool ready=false;
    std::call_once(once,[]{
        wchar_t system[MAX_PATH]{};GetSystemDirectoryW(system,MAX_PATH);
        const std::wstring path=std::wstring(system)+L"\\nvapi64.dll";
        HMODULE module=LoadLibraryExW(path.c_str(),nullptr,LOAD_LIBRARY_SEARCH_SYSTEM32);
        if(!module)return;
        HMODULE pinned=nullptr;GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_PIN,reinterpret_cast<LPCWSTR>(module),&pinned);
        auto query=reinterpret_cast<Query>(GetProcAddress(module,"nvapi_QueryInterface"));
        if(!query)return;
        auto init=reinterpret_cast<Init>(query(0x0150E828));
        idByName=reinterpret_cast<IdByName>(query(0xAE457190));
        getVrr=reinterpret_cast<GetVrr>(query(0xDF8FDA57));
        getCaps=reinterpret_cast<GetCaps>(query(0x3B05C7E1)); // NvAPI_DISP_GetMonitorCapabilities, optional
        ready=init&&idByName&&getVrr&&init()==0;
    });
    VrrState state;
    if(!ready||!window)return state;
    MONITORINFOEXW monitor{};monitor.cbSize=sizeof(monitor);
    if(!GetMonitorInfoW(MonitorFromWindow(window,MONITOR_DEFAULTTONEAREST),&monitor))return state;
    char name[CCHDEVICENAME*2]{};
    if(!WideCharToMultiByte(CP_ACP,0,monitor.szDevice,-1,name,int(sizeof(name)),nullptr,nullptr))return state;
    uint32_t displayId=0;
    state.status=idByName(name,&displayId);
    if(state.status!=0)return state;
    Info info{};info.version=uint32_t(sizeof(Info))|(1u<<16);
    state.status=getVrr(displayId,&info);
    if(state.status!=0)return state;
    state.known=true;
    state.enabled=info.flags&1u;state.possible=info.flags&2u;state.requested=info.flags&4u;
    state.indicator=info.flags&8u;state.inVrrMode=info.flags&16u;
    if(getCaps){
        Caps caps{};caps.version=uint32_t(sizeof(Caps))|(1u<<16);caps.size=uint16_t(sizeof(Caps));caps.infoType=0x1002; // NV_MONITOR_CAPS_TYPE_GENERIC
        if(getCaps(displayId,&caps)==0&&(caps.valid&1u)){
            state.capsKnown=true;
            state.supportsVrr=caps.data[0]&1u;state.trueGsync=caps.data[0]&4u;state.currentlyCapable=caps.data[0]&16u;
        }
    }
    return state;
}
}