From 795d08522fc31125b6bcf2dfdc1b4966ec952117 Mon Sep 17 00:00:00 2001 From: cdozdil Date: Wed, 1 May 2024 15:39:15 +0300 Subject: [PATCH] dynamic libxess logic, feels complete now --- OptiScaler/NVNGX_DLSS_Dx11.cpp | 4 +- OptiScaler/NVNGX_DLSS_Dx12.cpp | 4 +- OptiScaler/backends/IFeature.h | 2 + OptiScaler/backends/fsr2/FSR2Feature.h | 1 + .../backends/fsr2_212/FSR2Feature_212.h | 1 + OptiScaler/backends/xess/XeSSFeature.cpp | 92 +++++++++++++------ OptiScaler/backends/xess/XeSSFeature.h | 5 +- 7 files changed, 72 insertions(+), 37 deletions(-) diff --git a/OptiScaler/NVNGX_DLSS_Dx11.cpp b/OptiScaler/NVNGX_DLSS_Dx11.cpp index 6ed58e7a..cdb3a535 100644 --- a/OptiScaler/NVNGX_DLSS_Dx11.cpp +++ b/OptiScaler/NVNGX_DLSS_Dx11.cpp @@ -215,8 +215,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_CreateFeature(ID3D11DeviceContext { Dx11Contexts[handleId] = std::make_unique(handleId, InParameters); - - if (!Dx11Contexts[handleId]->IsInited()) + + if (!Dx11Contexts[handleId]->ModuleLoaded()) { spdlog::error("NVSDK_NGX_D3D11_CreateFeature can't create new XeSS with Dx12 feature, Fallback to FSR2.2!"); diff --git a/OptiScaler/NVNGX_DLSS_Dx12.cpp b/OptiScaler/NVNGX_DLSS_Dx12.cpp index ee530d83..035b0b6e 100644 --- a/OptiScaler/NVNGX_DLSS_Dx12.cpp +++ b/OptiScaler/NVNGX_DLSS_Dx12.cpp @@ -379,8 +379,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_CreateFeature(ID3D12GraphicsComma if (upscalerChoice == 0) { Dx12Contexts[handleId] = std::make_unique(handleId, InParameters); - - if (!Dx12Contexts[handleId]->IsInited()) + + if (!Dx12Contexts[handleId]->ModuleLoaded()) { spdlog::error("NVSDK_NGX_D3D12_CreateFeature can't create new XeSS feature, Fallback to FSR2.1!"); diff --git a/OptiScaler/backends/IFeature.h b/OptiScaler/backends/IFeature.h index d0f214b9..fdaba82b 100644 --- a/OptiScaler/backends/IFeature.h +++ b/OptiScaler/backends/IFeature.h @@ -33,6 +33,7 @@ protected: unsigned int _displayWidth = 0; unsigned int _displayHeight = 0; long _frameCount = 0; + bool _moduleLoaded = false; void SetHandle(unsigned int InHandleId); bool SetInitParameters(const NVSDK_NGX_Parameter* InParameters); @@ -72,6 +73,7 @@ public: int InitFlags() const { return _initFlags; } virtual feature_version Version() = 0; virtual const char* Name() = 0; + bool ModuleLoaded() const { return _moduleLoaded; } IFeature(unsigned int InHandleId, const NVSDK_NGX_Parameter* InParameters) { diff --git a/OptiScaler/backends/fsr2/FSR2Feature.h b/OptiScaler/backends/fsr2/FSR2Feature.h index b1d7a980..bbd7ccc3 100644 --- a/OptiScaler/backends/fsr2/FSR2Feature.h +++ b/OptiScaler/backends/fsr2/FSR2Feature.h @@ -66,6 +66,7 @@ public: FSR2Feature(unsigned int InHandleId, const NVSDK_NGX_Parameter* InParameters) : IFeature(InHandleId, InParameters) { + _moduleLoaded = true; _lastFrameTime = MillisecondsNow(); } diff --git a/OptiScaler/backends/fsr2_212/FSR2Feature_212.h b/OptiScaler/backends/fsr2_212/FSR2Feature_212.h index 7e5a5006..7de05d2d 100644 --- a/OptiScaler/backends/fsr2_212/FSR2Feature_212.h +++ b/OptiScaler/backends/fsr2_212/FSR2Feature_212.h @@ -53,6 +53,7 @@ public: FSR2Feature212(unsigned int InHandleId, const NVSDK_NGX_Parameter* InParameters) : IFeature(InHandleId, InParameters) { + _moduleLoaded = true; _lastFrameTime = MillisecondsNow(); } diff --git a/OptiScaler/backends/xess/XeSSFeature.cpp b/OptiScaler/backends/xess/XeSSFeature.cpp index e2c400a3..e4650a8f 100644 --- a/OptiScaler/backends/xess/XeSSFeature.cpp +++ b/OptiScaler/backends/xess/XeSSFeature.cpp @@ -333,24 +333,35 @@ bool XeSSFeature::InitXeSS(ID3D12Device* device, const NVSDK_NGX_Parameter* InPa XeSSFeature::XeSSFeature(unsigned int handleId, const NVSDK_NGX_Parameter* InParameters) : IFeature(handleId, InParameters) { - _xessD3D12CreateContext = (PFN_xessD3D12CreateContext)DetourFindFunction("libxess.dll", "xessD3D12CreateContext"); - _xessD3D12BuildPipelines = (PFN_xessD3D12BuildPipelines)DetourFindFunction("libxess.dll", "xessD3D12BuildPipelines"); - _xessD3D12Init = (PRN_xessD3D12Init)DetourFindFunction("libxess.dll", "xessD3D12Init"); - _xessD3D12Execute = (PFN_xessD3D12Execute)DetourFindFunction("libxess.dll", "xessD3D12Execute"); - _xessSelectNetworkModel = (PFN_xessSelectNetworkModel)DetourFindFunction("libxess.dll", "xessSelectNetworkModel"); - _xessStartDump = (PFN_xessStartDump)DetourFindFunction("libxess.dll", "xessStartDump"); - _xessGetVersion = (PRN_xessGetVersion)DetourFindFunction("libxess.dll", "xessGetVersion"); - _xessIsOptimalDriver = (PFN_xessIsOptimalDriver)DetourFindFunction("libxess.dll", "xessIsOptimalDriver"); - _xessSetLoggingCallback = (PFN_xessSetLoggingCallback)DetourFindFunction("libxess.dll", "xessSetLoggingCallback"); - _xessGetProperties = (PFN_xessGetProperties)DetourFindFunction("libxess.dll", "xessGetProperties"); - _xessDestroyContext = (PFN_xessDestroyContext)DetourFindFunction("libxess.dll", "xessDestroyContext"); - _xessSetVelocityScale = (PFN_xessSetVelocityScale)DetourFindFunction("libxess.dll", "xessSetVelocityScale"); + PRN_xessGetVersion ptrMemoryGetVersion = (PRN_xessGetVersion)DetourFindFunction("libxess.dll", "xessGetVersion"); + PRN_xessGetVersion ptrDllGetVersion = nullptr; - if (_xessD3D12CreateContext && _xessD3D12BuildPipelines && _xessD3D12Init && _xessD3D12Execute && _xessSelectNetworkModel && _xessStartDump && - _xessGetVersion && _xessIsOptimalDriver && _xessSetLoggingCallback && _xessGetProperties && _xessDestroyContext && _xessSetVelocityScale) + xess_version_t memoryVersion{ 0,0,0,0 }; + xess_version_t dllVersion{ 0,0,0,0 }; + + // if there is libxess already loaded + if (ptrMemoryGetVersion) { - _moduleLoaded = true; - return; + // get it's version to compare with dll + ptrMemoryGetVersion(&memoryVersion); + + spdlog::info("XeSSFeature::XeSSFeature libxess.dll v{0}.{1}.{2} already loaded.", memoryVersion.major, memoryVersion.minor, memoryVersion.patch); + + _xessD3D12CreateContext = (PFN_xessD3D12CreateContext)DetourFindFunction("libxess.dll", "xessD3D12CreateContext"); + _xessD3D12BuildPipelines = (PFN_xessD3D12BuildPipelines)DetourFindFunction("libxess.dll", "xessD3D12BuildPipelines"); + _xessD3D12Init = (PRN_xessD3D12Init)DetourFindFunction("libxess.dll", "xessD3D12Init"); + _xessGetVersion = (PRN_xessGetVersion)DetourFindFunction("libxess.dll", "xessGetVersion"); + _xessD3D12Execute = (PFN_xessD3D12Execute)DetourFindFunction("libxess.dll", "xessD3D12Execute"); + _xessSelectNetworkModel = (PFN_xessSelectNetworkModel)DetourFindFunction("libxess.dll", "xessSelectNetworkModel"); + _xessStartDump = (PFN_xessStartDump)DetourFindFunction("libxess.dll", "xessStartDump"); + _xessIsOptimalDriver = (PFN_xessIsOptimalDriver)DetourFindFunction("libxess.dll", "xessIsOptimalDriver"); + _xessSetLoggingCallback = (PFN_xessSetLoggingCallback)DetourFindFunction("libxess.dll", "xessSetLoggingCallback"); + _xessGetProperties = (PFN_xessGetProperties)DetourFindFunction("libxess.dll", "xessGetProperties"); + _xessDestroyContext = (PFN_xessDestroyContext)DetourFindFunction("libxess.dll", "xessDestroyContext"); + _xessSetVelocityScale = (PFN_xessSetVelocityScale)DetourFindFunction("libxess.dll", "xessSetVelocityScale"); + + _moduleLoaded = _xessD3D12CreateContext && _xessD3D12BuildPipelines && _xessD3D12Init && _xessD3D12Execute && _xessSelectNetworkModel && _xessStartDump && + _xessGetVersion && _xessIsOptimalDriver && _xessSetLoggingCallback && _xessGetProperties && _xessDestroyContext && _xessSetVelocityScale; } if (Config::Instance()->XeSSLibrary.has_value()) @@ -368,20 +379,43 @@ XeSSFeature::XeSSFeature(unsigned int handleId, const NVSDK_NGX_Parameter* InPar if (_libxess) { - _xessD3D12CreateContext = (PFN_xessD3D12CreateContext)GetProcAddress(_libxess,"xessD3D12CreateContext"); - _xessD3D12BuildPipelines = (PFN_xessD3D12BuildPipelines)GetProcAddress(_libxess,"xessD3D12BuildPipelines"); - _xessD3D12Init = (PRN_xessD3D12Init)GetProcAddress(_libxess,"xessD3D12Init"); - _xessD3D12Execute = (PFN_xessD3D12Execute)GetProcAddress(_libxess,"xessD3D12Execute"); - _xessSelectNetworkModel = (PFN_xessSelectNetworkModel)GetProcAddress(_libxess,"xessSelectNetworkModel"); - _xessStartDump = (PFN_xessStartDump)GetProcAddress(_libxess,"xessStartDump"); - _xessGetVersion = (PRN_xessGetVersion)GetProcAddress(_libxess,"xessGetVersion"); - _xessIsOptimalDriver = (PFN_xessIsOptimalDriver)GetProcAddress(_libxess,"xessIsOptimalDriver"); - _xessSetLoggingCallback = (PFN_xessSetLoggingCallback)GetProcAddress(_libxess,"xessSetLoggingCallback"); - _xessGetProperties = (PFN_xessGetProperties)GetProcAddress(_libxess,"xessGetProperties"); - _xessDestroyContext = (PFN_xessDestroyContext)GetProcAddress(_libxess,"xessDestroyContext"); - _xessSetVelocityScale = (PFN_xessSetVelocityScale)GetProcAddress(_libxess, "xessSetVelocityScale"); + ptrDllGetVersion = (PRN_xessGetVersion)GetProcAddress(_libxess, "xessGetVersion"); - _moduleLoaded = true; + // query dll version + if (ptrDllGetVersion) + { + ptrDllGetVersion(&dllVersion); + spdlog::info("XeSSFeature::XeSSFeature Loaded libxess.dll v{0}.{1}.{2} file.", dllVersion.major, dllVersion.minor, dllVersion.patch); + } + + // check versions and ptr if they are same we are loaded same file prevent freelibrary call + if (dllVersion.major == memoryVersion.major && dllVersion.minor == memoryVersion.minor && + dllVersion.patch == memoryVersion.patch && dllVersion.reserved == memoryVersion.reserved && + ptrDllGetVersion == ptrMemoryGetVersion) + { + spdlog::info("XeSSFeature::XeSSFeature Both libxess.dll versions are same!"); + _libxess = nullptr; + } + else + { + spdlog::info("XeSSFeature::XeSSFeature Using loaded libxess.dll library!"); + + // we would like to prioritize file pointed at ini, use methods from loaded dll + _xessD3D12CreateContext = (PFN_xessD3D12CreateContext)GetProcAddress(_libxess, "xessD3D12CreateContext"); + _xessD3D12BuildPipelines = (PFN_xessD3D12BuildPipelines)GetProcAddress(_libxess, "xessD3D12BuildPipelines"); + _xessD3D12Init = (PRN_xessD3D12Init)GetProcAddress(_libxess, "xessD3D12Init"); + _xessD3D12Execute = (PFN_xessD3D12Execute)GetProcAddress(_libxess, "xessD3D12Execute"); + _xessSelectNetworkModel = (PFN_xessSelectNetworkModel)GetProcAddress(_libxess, "xessSelectNetworkModel"); + _xessStartDump = (PFN_xessStartDump)GetProcAddress(_libxess, "xessStartDump"); + _xessGetVersion = (PRN_xessGetVersion)GetProcAddress(_libxess, "xessGetVersion"); + _xessIsOptimalDriver = (PFN_xessIsOptimalDriver)GetProcAddress(_libxess, "xessIsOptimalDriver"); + _xessSetLoggingCallback = (PFN_xessSetLoggingCallback)GetProcAddress(_libxess, "xessSetLoggingCallback"); + _xessGetProperties = (PFN_xessGetProperties)GetProcAddress(_libxess, "xessGetProperties"); + _xessDestroyContext = (PFN_xessDestroyContext)GetProcAddress(_libxess, "xessDestroyContext"); + _xessSetVelocityScale = (PFN_xessSetVelocityScale)GetProcAddress(_libxess, "xessSetVelocityScale"); + + _moduleLoaded = true; + } } } diff --git a/OptiScaler/backends/xess/XeSSFeature.h b/OptiScaler/backends/xess/XeSSFeature.h index d171b1ad..f2e24d2b 100644 --- a/OptiScaler/backends/xess/XeSSFeature.h +++ b/OptiScaler/backends/xess/XeSSFeature.h @@ -67,7 +67,6 @@ private: PFN_xessSetVelocityScale _xessSetVelocityScale = nullptr; HMODULE _libxess = nullptr; - bool _moduleLoaded = false; protected: xess_context_handle_t _xessContext = nullptr; @@ -78,9 +77,7 @@ protected: float GetSharpness(const NVSDK_NGX_Parameter* InParameters); bool CreateBufferResource(ID3D12Device* InDevice, ID3D12Resource* InSource, ID3D12Resource** OutDest, D3D12_RESOURCE_STATES InDestState); - bool ModuleLoaded() const { return _moduleLoaded; } - - PFN_xessD3D12CreateContext D3D12CreateContext() const { return _xessD3D12CreateContext; } + PFN_xessD3D12CreateContext D3D12CreateContext() const { return _xessD3D12CreateContext; } PFN_xessD3D12BuildPipelines D3D12BuildPipelines() const { return _xessD3D12BuildPipelines; } PRN_xessD3D12Init D3D12Init() const { return _xessD3D12Init; } PFN_xessD3D12Execute D3D12Execute() const { return _xessD3D12Execute; }