From 0fc70a2a35b1adfbdecb41750dee69dc12bc719a Mon Sep 17 00:00:00 2001 From: cdozdil Date: Wed, 23 Oct 2024 12:05:39 +0300 Subject: [PATCH] seperate ffxapi to proxy --- OptiScaler/FfxApi_Proxy.h | 255 ++++++++++++++++++ OptiScaler/NVNGX_DLSS_Dx12.cpp | 135 +--------- OptiScaler/NVNGX_Proxy.h | 1 - OptiScaler/OptiScaler.vcxproj | 5 +- OptiScaler/XeSS_Proxy.h | 2 +- OptiScaler/backends/IFeature.h | 8 +- OptiScaler/backends/fsr31/FSR31Feature.cpp | 2 - OptiScaler/backends/fsr31/FSR31Feature.h | 23 +- .../backends/fsr31/FSR31Feature_Dx11.cpp | 1 + .../backends/fsr31/FSR31Feature_Dx11On12.cpp | 48 +--- .../backends/fsr31/FSR31Feature_Dx11On12.h | 6 +- .../backends/fsr31/FSR31Feature_Dx12.cpp | 48 +--- OptiScaler/backends/fsr31/FSR31Feature_Dx12.h | 6 +- OptiScaler/backends/fsr31/FSR31Feature_Vk.cpp | 47 +--- OptiScaler/backends/fsr31/FSR31Feature_Vk.h | 6 + OptiScaler/dllmain.cpp | 16 +- OptiScaler/hooks/HooksDx.cpp | 78 ++---- OptiScaler/hooks/HooksDx.h | 3 +- OptiScaler/pch.h | 7 + 19 files changed, 347 insertions(+), 350 deletions(-) create mode 100644 OptiScaler/FfxApi_Proxy.h diff --git a/OptiScaler/FfxApi_Proxy.h b/OptiScaler/FfxApi_Proxy.h new file mode 100644 index 00000000..fe769a78 --- /dev/null +++ b/OptiScaler/FfxApi_Proxy.h @@ -0,0 +1,255 @@ +#pragma once + +#include "pch.h" +#include "Util.h" +#include "Config.h" +#include "Logger.h" + +#include "ffx_api.h" + +#include "detours/detours.h" + +class FfxApiProxy +{ +private: + inline static HMODULE _dllDx12 = nullptr; + inline static feature_version _versionDx12{ 0, 0, 0 }; + inline static PfnFfxCreateContext _D3D12_CreateContext = nullptr; + inline static PfnFfxDestroyContext _D3D12_DestroyContext = nullptr; + inline static PfnFfxConfigure _D3D12_Configure = nullptr; + inline static PfnFfxQuery _D3D12_Query = nullptr; + inline static PfnFfxDispatch _D3D12_Dispatch = nullptr; + + inline static HMODULE _dllVk = nullptr; + inline static feature_version _versionVk{ 0, 0, 0 }; + inline static PfnFfxCreateContext _VULKAN_CreateContext = nullptr; + inline static PfnFfxDestroyContext _VULKAN_DestroyContext = nullptr; + inline static PfnFfxConfigure _VULKAN_Configure = nullptr; + inline static PfnFfxQuery _VULKAN_Query = nullptr; + inline static PfnFfxDispatch _VULKAN_Dispatch = nullptr; + + static inline void parse_version(const char* version_str, feature_version* _version) + { + if (sscanf_s(version_str, "%u.%u.%u", &_version->major, &_version->minor, &_version->patch) != 3) + LOG_WARN("can't parse {0}", version_str); + } + +public: + static bool InitFfxDx12() + { + // if dll already loaded + if (_dllDx12 != nullptr || _D3D12_CreateContext != nullptr) + return true; + + spdlog::info(""); + + Config::Instance()->upscalerDisableHook = true; + Config::Instance()->dxgiSkipSpoofing = true; + + LOG_DEBUG("Loading amd_fidelityfx_dx12.dll methods"); + + auto file = Util::DllPath().parent_path() / "amd_fidelityfx_dx12.dll"; + LOG_INFO("Trying to load {}", file.string()); + + _dllDx12 = LoadLibrary(file.wstring().c_str()); + if (_dllDx12 != nullptr) + { + _D3D12_Configure = (PfnFfxConfigure)GetProcAddress(_dllDx12, "ffxConfigure"); + _D3D12_CreateContext = (PfnFfxCreateContext)GetProcAddress(_dllDx12, "ffxCreateContext"); + _D3D12_DestroyContext = (PfnFfxDestroyContext)GetProcAddress(_dllDx12, "ffxDestroyContext"); + _D3D12_Dispatch = (PfnFfxDispatch)GetProcAddress(_dllDx12, "ffxDispatch"); + _D3D12_Query = (PfnFfxQuery)GetProcAddress(_dllDx12, "ffxQuery"); + } + + if (_D3D12_CreateContext == nullptr) + { + LOG_INFO("Trying to load amd_fidelityfx_dx12.dll with detours"); + + _D3D12_Configure = (PfnFfxConfigure)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxConfigure"); + _D3D12_CreateContext = (PfnFfxCreateContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxCreateContext"); + _D3D12_DestroyContext = (PfnFfxDestroyContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDestroyContext"); + _D3D12_Dispatch = (PfnFfxDispatch)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDispatch"); + _D3D12_Query = (PfnFfxQuery)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxQuery"); + } + + Config::Instance()->dxgiSkipSpoofing = false; + Config::Instance()->upscalerDisableHook = false; + + bool loadResult = _D3D12_CreateContext != nullptr; + + LOG_INFO("LoadResult: {}", loadResult); + + if (loadResult) + VersionDx12(); + + return loadResult; + } + + static feature_version VersionDx12() + { + if (_versionDx12.major == 0 && _D3D12_Query != nullptr/* && device != nullptr*/) + { + ffxQueryDescGetVersions versionQuery{}; + versionQuery.header.type = FFX_API_QUERY_DESC_TYPE_GET_VERSIONS; + versionQuery.createDescType = 0x00010000u; // FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE + uint64_t versionCount = 0; + versionQuery.outputCount = &versionCount; + + auto queryResult = _D3D12_Query(nullptr, &versionQuery.header); + + // get number of versions for allocation + if (versionCount > 0 && queryResult == FFX_API_RETURN_OK) + { + + std::vector versionIds; + std::vector versionNames; + versionIds.resize(versionCount); + versionNames.resize(versionCount); + versionQuery.versionIds = versionIds.data(); + versionQuery.versionNames = versionNames.data(); + + // fill version ids and names arrays. + queryResult = _D3D12_Query(nullptr, &versionQuery.header); + + if (queryResult == FFX_API_RETURN_OK) + { + parse_version(versionNames[0], &_versionDx12); + LOG_INFO("FfxApi Dx12 version: {}.{}.{}", _versionDx12.major, _versionDx12.minor, _versionDx12.patch); + } + else + { + LOG_WARN("_D3D12_Query 2 result: {}", (UINT)queryResult); + } + } + else + { + LOG_WARN("_D3D12_Query result: {}", (UINT)queryResult); + } + } + + return _versionDx12; + } + + static PfnFfxCreateContext D3D12_CreateContext() { return _D3D12_CreateContext; } + static PfnFfxDestroyContext D3D12_DestroyContext() { return _D3D12_DestroyContext; } + static PfnFfxConfigure D3D12_Configure() { return _D3D12_Configure; } + static PfnFfxQuery D3D12_Query() { return _D3D12_Query; } + static PfnFfxDispatch D3D12_Dispatch() { return _D3D12_Dispatch; } + + static bool InitFfxVk() + { + // if dll already loaded + if (_dllVk != nullptr || _VULKAN_CreateContext != nullptr) + return true; + + spdlog::info(""); + + Config::Instance()->upscalerDisableHook = true; + Config::Instance()->dxgiSkipSpoofing = true; + + LOG_DEBUG("Loading amd_fidelityfx_vk.dll methods"); + + auto file = Util::DllPath().parent_path() / "amd_fidelityfx_vk.dll"; + LOG_INFO("Trying to load {}", file.string()); + + _dllVk = LoadLibrary(file.wstring().c_str()); + if (_dllVk != nullptr) + { + _VULKAN_Configure = (PfnFfxConfigure)GetProcAddress(_dllVk, "ffxConfigure"); + _VULKAN_CreateContext = (PfnFfxCreateContext)GetProcAddress(_dllVk, "ffxCreateContext"); + _VULKAN_DestroyContext = (PfnFfxDestroyContext)GetProcAddress(_dllVk, "ffxDestroyContext"); + _VULKAN_Dispatch = (PfnFfxDispatch)GetProcAddress(_dllVk, "ffxDispatch"); + _VULKAN_Query = (PfnFfxQuery)GetProcAddress(_dllVk, "ffxQuery"); + } + + if (_VULKAN_CreateContext == nullptr) + { + LOG_INFO("Trying to load amd_fidelityfx_vk.dll with detours"); + + _VULKAN_Configure = (PfnFfxConfigure)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxConfigure"); + _VULKAN_CreateContext = (PfnFfxCreateContext)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxCreateContext"); + _VULKAN_DestroyContext = (PfnFfxDestroyContext)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxDestroyContext"); + _VULKAN_Dispatch = (PfnFfxDispatch)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxDispatch"); + _VULKAN_Query = (PfnFfxQuery)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxQuery"); + } + + Config::Instance()->dxgiSkipSpoofing = false; + Config::Instance()->upscalerDisableHook = false; + + bool loadResult = _VULKAN_CreateContext != nullptr; + + LOG_INFO("LoadResult: {}", loadResult); + + if (loadResult) + VersionVk(); + + return loadResult; + } + + static feature_version VersionVk() + { + if (_versionVk.major == 0 && _VULKAN_Query != nullptr) + { + ffxQueryDescGetVersions versionQuery{}; + versionQuery.header.type = FFX_API_QUERY_DESC_TYPE_GET_VERSIONS; + versionQuery.createDescType = 0x00010000u; // FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE + uint64_t versionCount = 0; + versionQuery.outputCount = &versionCount; + + auto queryResult = _VULKAN_Query(nullptr, &versionQuery.header); + + // get number of versions for allocation + if (versionCount > 0 && queryResult == FFX_API_RETURN_OK) + { + + std::vector versionIds; + std::vector versionNames; + versionIds.resize(versionCount); + versionNames.resize(versionCount); + versionQuery.versionIds = versionIds.data(); + versionQuery.versionNames = versionNames.data(); + + queryResult = _VULKAN_Query(nullptr, &versionQuery.header); + + if (queryResult == FFX_API_RETURN_OK) + { + parse_version(versionNames[0], &_versionVk); + LOG_INFO("FfxApi Vulkan version: {}.{}.{}", _versionVk.major, _versionVk.minor, _versionVk.patch); + } + else + { + LOG_WARN("_VULKAN_Query 2 result: {}", (UINT)queryResult); + } + } + else + { + LOG_WARN("_VULKAN_Query result: {}", (UINT)queryResult); + } + } + + return _versionVk; + } + + static PfnFfxCreateContext VULKAN_CreateContext() { return _VULKAN_CreateContext; } + static PfnFfxDestroyContext VULKAN_DestroyContext() { return _VULKAN_DestroyContext; } + static PfnFfxConfigure VULKAN_Configure() { return _VULKAN_Configure; } + static PfnFfxQuery VULKAN_Query() { return _VULKAN_Query; } + static PfnFfxDispatch VULKAN_Dispatch() { return _VULKAN_Dispatch; } + + static std::string ReturnCodeToString(ffxReturnCode_t result) + { + switch (result) + { + case FFX_API_RETURN_OK: return "The oparation was successful."; + case FFX_API_RETURN_ERROR: return "An error occurred that is not further specified."; + case FFX_API_RETURN_ERROR_UNKNOWN_DESCTYPE: return "The structure type given was not recognized for the function or context with which it was used. This is likely a programming error."; + case FFX_API_RETURN_ERROR_RUNTIME_ERROR: return "The underlying runtime (e.g. D3D12, Vulkan) or effect returned an error code."; + case FFX_API_RETURN_NO_PROVIDER: return "No provider was found for the given structure type. This is likely a programming error."; + case FFX_API_RETURN_ERROR_MEMORY: return "A memory allocation failed."; + case FFX_API_RETURN_ERROR_PARAMETER: return "A parameter was invalid, e.g. a null pointer, empty resource or out-of-bounds enum value."; + default: return "Unknown"; + } + } + + +}; \ No newline at end of file diff --git a/OptiScaler/NVNGX_DLSS_Dx12.cpp b/OptiScaler/NVNGX_DLSS_Dx12.cpp index afe03258..22192e59 100644 --- a/OptiScaler/NVNGX_DLSS_Dx12.cpp +++ b/OptiScaler/NVNGX_DLSS_Dx12.cpp @@ -18,7 +18,7 @@ #include #include -#include +#include "FfxApi_Proxy.h" #include #include "depth_upscale/DU_Dx12.h" @@ -29,11 +29,6 @@ #define DONT_USE_DEPTH_MV_COPIES #endif -static PfnFfxCreateContext _createContext = nullptr; -static PfnFfxDestroyContext _destroyContext = nullptr; -static PfnFfxConfigure _configure = nullptr; -static PfnFfxQuery _query = nullptr; -static PfnFfxDispatch _dispatch = nullptr; static UINT64 fgLastFrameTime = 0; static UINT64 fgLastFGFrame = 0; @@ -107,50 +102,13 @@ static bool CreateBufferResource(LPCWSTR Name, ID3D12Device* InDevice, ID3D12Res return true; } - -static void LoadFSR31Funcs() -{ - LOG_DEBUG("Loading amd_fidelityfx_dx12.dll methods"); - - auto file = Util::DllPath().parent_path() / "amd_fidelityfx_dx12.dll"; - LOG_INFO("Trying to load {}", file.string()); - - auto _dll = LoadLibrary(file.wstring().c_str()); - if (_dll != nullptr) - { - _configure = (PfnFfxConfigure)GetProcAddress(_dll, "ffxConfigure"); - _createContext = (PfnFfxCreateContext)GetProcAddress(_dll, "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)GetProcAddress(_dll, "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)GetProcAddress(_dll, "ffxDispatch"); - _query = (PfnFfxQuery)GetProcAddress(_dll, "ffxQuery"); - } - - if (_configure == nullptr) - { - LOG_INFO("Trying to load amd_fidelityfx_dx12.dll with detours"); - - _configure = (PfnFfxConfigure)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxConfigure"); - _createContext = (PfnFfxCreateContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDispatch"); - _query = (PfnFfxQuery)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxQuery"); - } - - if (_configure != nullptr) - LOG_INFO("amd_fidelityfx_dx12.dll methods loaded!"); - else - LOG_ERROR("can't load amd_fidelityfx_dx12.dll methods!"); -} - - #pragma region Hooks -typedef void(__fastcall* PFN_SetComputeRootSignature)(ID3D12GraphicsCommandList* commandList, ID3D12RootSignature* pRootSignature); -typedef void(__fastcall* PFN_CreateSampler)(ID3D12Device* device, const D3D12_SAMPLER_DESC* pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor); +typedef void(*PFN_SetComputeRootSignature)(ID3D12GraphicsCommandList* commandList, ID3D12RootSignature* pRootSignature); +typedef void(*PFN_CreateSampler)(ID3D12Device* device, const D3D12_SAMPLER_DESC* pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor); static PFN_SetComputeRootSignature orgSetComputeRootSignature = nullptr; static PFN_SetComputeRootSignature orgSetGraphicRootSignature = nullptr; -static PFN_CreateSampler orgCreateSampler = nullptr; static ID3D12RootSignature* rootSigCompute = nullptr; static ID3D12RootSignature* rootSigGraphic = nullptr; @@ -196,56 +154,6 @@ static void hkSetGraphicRootSignature(ID3D12GraphicsCommandList* commandList, ID return orgSetGraphicRootSignature(commandList, pRootSignature); } -static void hkCreateSampler(ID3D12Device* device, const D3D12_SAMPLER_DESC* pDesc, D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor) -{ - if (pDesc == nullptr || device == nullptr) - return; - - D3D12_SAMPLER_DESC newDesc{}; - - newDesc.AddressU = pDesc->AddressU; - newDesc.AddressV = pDesc->AddressV; - newDesc.AddressW = pDesc->AddressW; - newDesc.BorderColor[0] = pDesc->BorderColor[0]; - newDesc.BorderColor[1] = pDesc->BorderColor[1]; - newDesc.BorderColor[2] = pDesc->BorderColor[2]; - newDesc.BorderColor[3] = pDesc->BorderColor[3]; - newDesc.ComparisonFunc = pDesc->ComparisonFunc; - - if (Config::Instance()->AnisotropyOverride.has_value() && - (pDesc->Filter == D3D12_FILTER_MIN_LINEAR_MAG_MIP_POINT || - pDesc->Filter == D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT || - pDesc->Filter == D3D12_FILTER_MIN_MAG_MIP_LINEAR || - pDesc->Filter == D3D12_FILTER_ANISOTROPIC)) - { - LOG_INFO("Overriding Anisotrpic ({2}) filtering {0} -> {1}", pDesc->MaxAnisotropy, Config::Instance()->AnisotropyOverride.value(), (UINT)pDesc->Filter); - newDesc.Filter = D3D12_FILTER_ANISOTROPIC; - newDesc.MaxAnisotropy = Config::Instance()->AnisotropyOverride.value(); - } - else - { - newDesc.Filter = pDesc->Filter; - newDesc.MaxAnisotropy = pDesc->MaxAnisotropy; - } - - newDesc.MaxLOD = pDesc->MaxLOD; - newDesc.MinLOD = pDesc->MinLOD; - newDesc.MipLODBias = pDesc->MipLODBias; - - if (newDesc.MipLODBias < 0.0f) - { - if (Config::Instance()->MipmapBiasOverride.has_value()) - { - LOG_INFO("Overriding mipmap bias {0} -> {1}", pDesc->MipLODBias, Config::Instance()->MipmapBiasOverride.value()); - newDesc.MipLODBias = Config::Instance()->MipmapBiasOverride.value(); - } - - Config::Instance()->lastMipBias = newDesc.MipLODBias; - } - - return orgCreateSampler(device, &newDesc, DestDescriptor); -} - void HookToCommandList(ID3D12GraphicsCommandList* InCmdList) { if (orgSetComputeRootSignature != nullptr || orgSetGraphicRootSignature != nullptr) @@ -271,26 +179,6 @@ void HookToCommandList(ID3D12GraphicsCommandList* InCmdList) } } -//void HookToDevice(ID3D12Device* InDevice) -//{ -// //if (!ImGuiOverlayDx12::IsEarlyBind() && orgCreateSampler != nullptr || InDevice == nullptr) -// // return; -// -// PVOID* pVTable = *(PVOID**)InDevice; -// -// orgCreateSampler = (PFN_CreateSampler)pVTable[22]; -// -// if (orgCreateSampler != nullptr) -// { -// DetourTransactionBegin(); -// DetourUpdateThread(GetCurrentThread()); -// -// DetourAttach(&(PVOID&)orgCreateSampler, hkCreateSampler); -// -// DetourTransactionCommit(); -// } -//} - void UnhookAll() { DetourTransactionBegin(); @@ -308,12 +196,6 @@ void UnhookAll() orgSetGraphicRootSignature = nullptr; } - if (orgCreateSampler != nullptr) - { - DetourDetach(&(PVOID&)orgCreateSampler, hkCreateSampler); - orgCreateSampler = nullptr; - } - DetourTransactionCommit(); } @@ -398,9 +280,6 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Init_Ext(unsigned long long InApp heapProps.Type = D3D12_HEAP_TYPE_READBACK; result = InDevice->CreateCommittedResource(&heapProps, D3D12_HEAP_FLAG_NONE, &bufferDesc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&HooksDx::readbackBuffer)); - if (_createContext == nullptr) - LoadFSR31Funcs(); - return NVSDK_NGX_Result_Success; } @@ -1298,7 +1177,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom if (Config::Instance()->FGUseFGSwapChain.value_or(true) && Config::Instance()->OverlayMenu.value_or(true)) { if (!Config::Instance()->FGChanged && HooksDx::fgTarget < deviceContext->FrameCount() && Config::Instance()->FGEnabled.value_or(false) && - _createContext != nullptr && !HooksDx::fgIsActive && HooksDx::currentSwapchain != nullptr && + FfxApiProxy::InitFfxDx12() && !HooksDx::fgIsActive && HooksDx::currentSwapchain != nullptr && HooksDx::swapchainFormat != DXGI_FORMAT_UNKNOWN) { HooksDx::CreateFGObjects(D3D12Device); @@ -1472,7 +1351,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom if (Config::Instance()->CurrentFeature != nullptr) fgLastFGFrame = Config::Instance()->CurrentFeature->FrameCount(); - auto result = _dispatch(reinterpret_cast(pUserCtx), ¶ms->header); + auto result = FfxApiProxy::D3D12_Dispatch()(reinterpret_cast(pUserCtx), ¶ms->header); ID3D12CommandList* cl[1] = { nullptr }; result = HooksDx::fgCopyCommandList->Close(); @@ -1501,7 +1380,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom m_FrameGenerationConfig.header.pNext = &debugDesc.header; Config::Instance()->dxgiSkipSpoofing = true; - ffxReturnCode_t retCode = _configure(&HooksDx::fgContext, &m_FrameGenerationConfig.header); + ffxReturnCode_t retCode = FfxApiProxy::D3D12_Configure()(&HooksDx::fgContext, &m_FrameGenerationConfig.header); Config::Instance()->dxgiSkipSpoofing = false; LOG_DEBUG(" FG _configure result: {0:X}", retCode); @@ -1591,7 +1470,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom dfgPrepare.frameTimeDelta = msDelta; Config::Instance()->dxgiSkipSpoofing = true; - retCode = _dispatch(&HooksDx::fgContext, &dfgPrepare.header); + retCode = FfxApiProxy::D3D12_Dispatch()(&HooksDx::fgContext, &dfgPrepare.header); Config::Instance()->dxgiSkipSpoofing = false; LOG_DEBUG(" FG _dispatch result: {0}", retCode); } diff --git a/OptiScaler/NVNGX_Proxy.h b/OptiScaler/NVNGX_Proxy.h index 8e3c09e8..4784ded5 100644 --- a/OptiScaler/NVNGX_Proxy.h +++ b/OptiScaler/NVNGX_Proxy.h @@ -379,7 +379,6 @@ public: return; LOG_INFO(""); - LOG_FUNC(); Config::Instance()->upscalerDisableHook = true; diff --git a/OptiScaler/OptiScaler.vcxproj b/OptiScaler/OptiScaler.vcxproj index aca8b0f7..7ed36fbf 100644 --- a/OptiScaler/OptiScaler.vcxproj +++ b/OptiScaler/OptiScaler.vcxproj @@ -81,8 +81,8 @@ $(SolutionDir)external\vulkan\include;$(SolutionDir)external\nvngx_dlss_sdk;$(SolutionDir)external\xess\inc\xess;$(SolutionDir)external\FidelityFX-SDK\ffx-api\include\ffx_api;$(SolutionDir)external\simpleini;$(SolutionDir)external\unordered_dense\include;$(SolutionDir)external\spdlog\include;$(IncludePath) $(ProjectDir)fsr2\lib;$(ProjectDir)fsr2_212\lib;$(ProjectDir)fsr31\lib;$(ProjectDir)vulkan;$(ProjectDir)d3dx;$(ProjectDir)detours;$(SolutionDir)external\xess\lib;$(LibraryPath) - version - D:\Folders\Games\No Man%27s Sky\Binaries\ + dxgi + D:\Folders\Games\Deep Rock Galactic\FSD\Binaries\Win64\ .\x64\Debug @@ -208,6 +208,7 @@ copy $(SolutionDir)nvngx.ini $(SolutionDir)x64\Release\a\ + diff --git a/OptiScaler/XeSS_Proxy.h b/OptiScaler/XeSS_Proxy.h index d64cedf7..926dbaaf 100644 --- a/OptiScaler/XeSS_Proxy.h +++ b/OptiScaler/XeSS_Proxy.h @@ -139,7 +139,7 @@ public: static bool InitXeSS() { // if dll already loaded - if (_dll != nullptr && _xessD3D12CreateContext != nullptr) + if (_dll != nullptr || _xessD3D12CreateContext != nullptr) return true; spdlog::info(""); diff --git a/OptiScaler/backends/IFeature.h b/OptiScaler/backends/IFeature.h index 4eb0d134..39fdb858 100644 --- a/OptiScaler/backends/IFeature.h +++ b/OptiScaler/backends/IFeature.h @@ -1,4 +1,5 @@ #pragma once +#include "../pch.h" #include #include @@ -43,13 +44,6 @@ protected: virtual void SetInit(bool InValue) { _isInited = InValue; } public: - typedef struct _feature_version - { - unsigned int major; - unsigned int minor; - unsigned int patch; - } feature_version; - NVSDK_NGX_Handle* Handle() const { return _handle; }; static unsigned int GetNextHandleId() { return handleCounter++; } diff --git a/OptiScaler/backends/fsr31/FSR31Feature.cpp b/OptiScaler/backends/fsr31/FSR31Feature.cpp index 0e59d65e..dd8c190c 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature.cpp +++ b/OptiScaler/backends/fsr31/FSR31Feature.cpp @@ -46,8 +46,6 @@ FSR31Feature::~FSR31Feature() if (!IsInited()) return; - _destroyContext(&_context, NULL); - SetInit(false); } diff --git a/OptiScaler/backends/fsr31/FSR31Feature.h b/OptiScaler/backends/fsr31/FSR31Feature.h index 086ec92b..c15fa033 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature.h +++ b/OptiScaler/backends/fsr31/FSR31Feature.h @@ -1,25 +1,10 @@ #pragma once -#include "ffx_api.h" +#include "../../FfxApi_Proxy.h" #include "ffx_upscale.h" #include "../IFeature.h" #include "../../detours/detours.h" -inline static std::string ResultToString(ffxReturnCode_t result) -{ - switch (result) - { - case FFX_API_RETURN_OK: return "The oparation was successful."; - case FFX_API_RETURN_ERROR: return "An error occurred that is not further specified."; - case FFX_API_RETURN_ERROR_UNKNOWN_DESCTYPE: return "The structure type given was not recognized for the function or context with which it was used. This is likely a programming error."; - case FFX_API_RETURN_ERROR_RUNTIME_ERROR: return "The underlying runtime (e.g. D3D12, Vulkan) or effect returned an error code."; - case FFX_API_RETURN_NO_PROVIDER: return "No provider was found for the given structure type. This is likely a programming error."; - case FFX_API_RETURN_ERROR_MEMORY: return "A memory allocation failed."; - case FFX_API_RETURN_ERROR_PARAMETER: return "A parameter was invalid, e.g. a null pointer, empty resource or out-of-bounds enum value."; - default: return "Unknown"; - } -} - inline static void FfxLogCallback(uint32_t type, const wchar_t* message) { std::wstring string(message); @@ -47,12 +32,6 @@ protected: ffxContext _context = nullptr; ffxCreateContextDescUpscale _contextDesc = {}; - PfnFfxCreateContext _createContext = nullptr; - PfnFfxDestroyContext _destroyContext = nullptr; - PfnFfxConfigure _configure = nullptr; - PfnFfxQuery _query = nullptr; - PfnFfxDispatch _dispatch = nullptr; - virtual bool InitFSR3(const NVSDK_NGX_Parameter* InParameters) = 0; double MillisecondsNow(); diff --git a/OptiScaler/backends/fsr31/FSR31Feature_Dx11.cpp b/OptiScaler/backends/fsr31/FSR31Feature_Dx11.cpp index 44e5e283..c3b264e8 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature_Dx11.cpp +++ b/OptiScaler/backends/fsr31/FSR31Feature_Dx11.cpp @@ -15,6 +15,7 @@ do { \ (p) = nullptr; \ } \ } while((void)0, 0); + FSR31FeatureDx11::FSR31FeatureDx11(unsigned int InHandleId, NVSDK_NGX_Parameter * InParameters) : FSR31Feature(InHandleId, InParameters), IFeature_Dx11(InHandleId, InParameters), IFeature(InHandleId, InParameters) { _moduleLoaded = true; diff --git a/OptiScaler/backends/fsr31/FSR31Feature_Dx11On12.cpp b/OptiScaler/backends/fsr31/FSR31Feature_Dx11On12.cpp index 96945e78..6e2126e8 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature_Dx11On12.cpp +++ b/OptiScaler/backends/fsr31/FSR31Feature_Dx11On12.cpp @@ -7,35 +7,7 @@ FSR31FeatureDx11on12::FSR31FeatureDx11on12(unsigned int InHandleId, NVSDK_NGX_Parameter* InParameters) : FSR31Feature(InHandleId, InParameters), IFeature_Dx11wDx12(InHandleId, InParameters), IFeature_Dx11(InHandleId, InParameters), IFeature(InHandleId, InParameters) { - LOG_DEBUG("Loading amd_fidelityfx_dx12.dll methods"); - - auto file = Util::DllPath().parent_path() / "amd_fidelityfx_dx12.dll"; - LOG_INFO("Trying to load {}", file.string()); - - auto _dll = LoadLibrary(file.wstring().c_str()); - if (_dll != nullptr) - { - _configure = (PfnFfxConfigure)GetProcAddress(_dll, "ffxConfigure"); - _createContext = (PfnFfxCreateContext)GetProcAddress(_dll, "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)GetProcAddress(_dll, "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)GetProcAddress(_dll, "ffxDispatch"); - _query = (PfnFfxQuery)GetProcAddress(_dll, "ffxQuery"); - - _moduleLoaded = _configure != nullptr; - } - - if (!_moduleLoaded) - { - LOG_INFO("Trying to load amd_fidelityfx_dx12.dll with detours"); - - _configure = (PfnFfxConfigure)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxConfigure"); - _createContext = (PfnFfxCreateContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDispatch"); - _query = (PfnFfxQuery)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxQuery"); - - _moduleLoaded = _configure != nullptr; - } + _moduleLoaded = FfxApiProxy::InitFfxDx12(); if (_moduleLoaded) LOG_INFO("amd_fidelityfx_dx12.dll methods loaded!"); @@ -353,18 +325,18 @@ bool FSR31FeatureDx11on12::Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_ m_upscalerKeyValueConfig.header.type = FFX_API_CONFIGURE_DESC_TYPE_UPSCALE_KEYVALUE; m_upscalerKeyValueConfig.key = FFX_API_CONFIGURE_UPSCALE_KEY_FVELOCITYFACTOR; m_upscalerKeyValueConfig.ptr = &_velocity; - auto result = _configure(&_context, &m_upscalerKeyValueConfig.header); + auto result = FfxApiProxy::D3D12_Configure()(&_context, &m_upscalerKeyValueConfig.header); if (result != FFX_API_RETURN_OK) LOG_WARN("Velocity configure result: {}", (UINT)result); } LOG_DEBUG("Dispatch!!"); - auto ffxresult = _dispatch(&_context, ¶ms.header); + auto ffxresult = FfxApiProxy::D3D12_Dispatch()(&_context, ¶ms.header); if (ffxresult != FFX_API_RETURN_OK) { - LOG_ERROR("ffxFsr2ContextDispatch error: {0}", ResultToString(ffxresult)); + LOG_ERROR("ffxFsr2ContextDispatch error: {0}", FfxApiProxy::ReturnCodeToString(ffxresult)); Dx12CommandList->Close(); ID3D12CommandList* ppCommandLists[] = { Dx12CommandList }; @@ -549,10 +521,6 @@ bool FSR31FeatureDx11on12::Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_ return true; } -FSR31FeatureDx11on12::~FSR31FeatureDx11on12() -{ -} - bool FSR31FeatureDx11on12::InitFSR3(const NVSDK_NGX_Parameter* InParameters) { LOG_FUNC(); @@ -578,14 +546,14 @@ bool FSR31FeatureDx11on12::InitFSR3(const NVSDK_NGX_Parameter* InParameters) uint64_t versionCount = 0; versionQuery.outputCount = &versionCount; // get number of versions for allocation - _query(nullptr, &versionQuery.header); + FfxApiProxy::D3D12_Query()(nullptr, &versionQuery.header); Config::Instance()->fsr3xVersionIds.resize(versionCount); Config::Instance()->fsr3xVersionNames.resize(versionCount); versionQuery.versionIds = Config::Instance()->fsr3xVersionIds.data(); versionQuery.versionNames = Config::Instance()->fsr3xVersionNames.data(); // fill version ids and names arrays. - _query(nullptr, &versionQuery.header); + FfxApiProxy::D3D12_Query()(nullptr, &versionQuery.header); _contextDesc.flags = 0; @@ -728,12 +696,12 @@ bool FSR31FeatureDx11on12::InitFSR3(const NVSDK_NGX_Parameter* InParameters) LOG_DEBUG("_createContext!"); Config::Instance()->SkipHeapCapture = true; - auto ret = _createContext(&_context, &_contextDesc.header, NULL); + auto ret = FfxApiProxy::D3D12_CreateContext()(&_context, &_contextDesc.header, NULL); Config::Instance()->SkipHeapCapture = false; if (ret != FFX_API_RETURN_OK) { - LOG_ERROR("_createContext error: {0}", ResultToString(ret)); + LOG_ERROR("_createContext error: {0}", FfxApiProxy::ReturnCodeToString(ret)); return false; } diff --git a/OptiScaler/backends/fsr31/FSR31Feature_Dx11On12.h b/OptiScaler/backends/fsr31/FSR31Feature_Dx11On12.h index 9cd8d549..834effd7 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature_Dx11On12.h +++ b/OptiScaler/backends/fsr31/FSR31Feature_Dx11On12.h @@ -21,5 +21,9 @@ public: bool Init(ID3D11Device* InDevice, ID3D11DeviceContext* InContext, NVSDK_NGX_Parameter* InParameters) override; bool Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_NGX_Parameter* InParameters) override; - ~FSR31FeatureDx11on12(); + ~FSR31FeatureDx11on12() + { + if (_context != nullptr) + FfxApiProxy::D3D12_DestroyContext()(&_context, NULL); + } }; \ No newline at end of file diff --git a/OptiScaler/backends/fsr31/FSR31Feature_Dx12.cpp b/OptiScaler/backends/fsr31/FSR31Feature_Dx12.cpp index d350968f..d22d5616 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature_Dx12.cpp +++ b/OptiScaler/backends/fsr31/FSR31Feature_Dx12.cpp @@ -7,35 +7,7 @@ FSR31FeatureDx12::FSR31FeatureDx12(unsigned int InHandleId, NVSDK_NGX_Parameter* InParameters) : FSR31Feature(InHandleId, InParameters), IFeature_Dx12(InHandleId, InParameters), IFeature(InHandleId, InParameters) { - LOG_DEBUG("Loading amd_fidelityfx_dx12.dll methods"); - - auto file = Util::DllPath().parent_path() / "amd_fidelityfx_dx12.dll"; - LOG_INFO("Trying to load {}", file.string()); - - auto _dll = LoadLibrary(file.wstring().c_str()); - if (_dll != nullptr) - { - _configure = (PfnFfxConfigure)GetProcAddress(_dll, "ffxConfigure"); - _createContext = (PfnFfxCreateContext)GetProcAddress(_dll, "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)GetProcAddress(_dll, "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)GetProcAddress(_dll, "ffxDispatch"); - _query = (PfnFfxQuery)GetProcAddress(_dll, "ffxQuery"); - - _moduleLoaded = _configure != nullptr; - } - - if (!_moduleLoaded) - { - LOG_INFO("Trying to load amd_fidelityfx_dx12.dll with detours"); - - _configure = (PfnFfxConfigure)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxConfigure"); - _createContext = (PfnFfxCreateContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDispatch"); - _query = (PfnFfxQuery)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxQuery"); - - _moduleLoaded = _configure != nullptr; - } + _moduleLoaded = FfxApiProxy::InitFfxDx12(); if (_moduleLoaded) LOG_INFO("amd_fidelityfx_dx12.dll methods loaded!"); @@ -378,18 +350,18 @@ bool FSR31FeatureDx12::Evaluate(ID3D12GraphicsCommandList* InCommandList, NVSDK_ m_upscalerKeyValueConfig.header.type = FFX_API_CONFIGURE_DESC_TYPE_UPSCALE_KEYVALUE; m_upscalerKeyValueConfig.key = FFX_API_CONFIGURE_UPSCALE_KEY_FVELOCITYFACTOR; m_upscalerKeyValueConfig.ptr = &_velocity; - auto result = _configure(&_context, &m_upscalerKeyValueConfig.header); + auto result = FfxApiProxy::D3D12_Configure()(&_context, &m_upscalerKeyValueConfig.header); if (result != FFX_API_RETURN_OK) LOG_WARN("Velocity configure result: {}", (UINT)result); } LOG_DEBUG("Dispatch!!"); - auto result = _dispatch(&_context, ¶ms.header); + auto result = FfxApiProxy::D3D12_Dispatch()(&_context, ¶ms.header); if (result != FFX_API_RETURN_OK) { - LOG_ERROR("_dispatch error: {0}", ResultToString(result)); + LOG_ERROR("_dispatch error: {0}", FfxApiProxy::ReturnCodeToString(result)); return false; } @@ -502,10 +474,6 @@ bool FSR31FeatureDx12::Evaluate(ID3D12GraphicsCommandList* InCommandList, NVSDK_ return true; } -FSR31FeatureDx12::~FSR31FeatureDx12() -{ -} - bool FSR31FeatureDx12::InitFSR3(const NVSDK_NGX_Parameter* InParameters) { LOG_FUNC(); @@ -531,14 +499,14 @@ bool FSR31FeatureDx12::InitFSR3(const NVSDK_NGX_Parameter* InParameters) uint64_t versionCount = 0; versionQuery.outputCount = &versionCount; // get number of versions for allocation - _query(nullptr, &versionQuery.header); + FfxApiProxy::D3D12_Query()(nullptr, &versionQuery.header); Config::Instance()->fsr3xVersionIds.resize(versionCount); Config::Instance()->fsr3xVersionNames.resize(versionCount); versionQuery.versionIds = Config::Instance()->fsr3xVersionIds.data(); versionQuery.versionNames = Config::Instance()->fsr3xVersionNames.data(); // fill version ids and names arrays. - _query(nullptr, &versionQuery.header); + FfxApiProxy::D3D12_Query()(nullptr, &versionQuery.header); _contextDesc.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE; @@ -680,12 +648,12 @@ bool FSR31FeatureDx12::InitFSR3(const NVSDK_NGX_Parameter* InParameters) LOG_DEBUG("_createContext!"); Config::Instance()->SkipHeapCapture = true; - auto ret = _createContext(&_context, &_contextDesc.header, NULL); + auto ret = FfxApiProxy::D3D12_CreateContext()(&_context, &_contextDesc.header, NULL); Config::Instance()->SkipHeapCapture = false; if (ret != FFX_API_RETURN_OK) { - LOG_ERROR("_createContext error: {0}", ResultToString(ret)); + LOG_ERROR("_createContext error: {0}", FfxApiProxy::ReturnCodeToString(ret)); return false; } diff --git a/OptiScaler/backends/fsr31/FSR31Feature_Dx12.h b/OptiScaler/backends/fsr31/FSR31Feature_Dx12.h index ba08033e..e4ed3376 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature_Dx12.h +++ b/OptiScaler/backends/fsr31/FSR31Feature_Dx12.h @@ -17,5 +17,9 @@ public: bool Init(ID3D12Device* InDevice, ID3D12GraphicsCommandList* InCommandList, NVSDK_NGX_Parameter* InParameters) override; bool Evaluate(ID3D12GraphicsCommandList* InCommandList, NVSDK_NGX_Parameter* InParameters) override; - ~FSR31FeatureDx12(); + ~FSR31FeatureDx12() + { + if(_context != nullptr) + FfxApiProxy::D3D12_DestroyContext()(&_context, NULL); + } }; \ No newline at end of file diff --git a/OptiScaler/backends/fsr31/FSR31Feature_Vk.cpp b/OptiScaler/backends/fsr31/FSR31Feature_Vk.cpp index 9f2dac14..c24e1c4e 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature_Vk.cpp +++ b/OptiScaler/backends/fsr31/FSR31Feature_Vk.cpp @@ -41,41 +41,12 @@ static inline FfxApiResourceDescription ffxApiGetImageResourceDescriptionVKLocal FSR31FeatureVk::FSR31FeatureVk(unsigned int InHandleId, NVSDK_NGX_Parameter* InParameters) : FSR31Feature(InHandleId, InParameters), IFeature_Vk(InHandleId, InParameters), IFeature(InHandleId, InParameters) { - LOG_DEBUG("Loading amd_fidelityfx_vk.dll methods"); - - auto file = Util::DllPath().parent_path() / "amd_fidelityfx_vk.dll"; - LOG_INFO("Trying to load {}", file.string()); - - auto _dll = LoadLibrary(file.wstring().c_str()); - - if (_dll != nullptr) - { - _configure = (PfnFfxConfigure)GetProcAddress(_dll, "ffxConfigure"); - _createContext = (PfnFfxCreateContext)GetProcAddress(_dll, "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)GetProcAddress(_dll, "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)GetProcAddress(_dll, "ffxDispatch"); - _query = (PfnFfxQuery)GetProcAddress(_dll, "ffxQuery"); - - _moduleLoaded = _configure != nullptr; - } - - if (!_moduleLoaded) - { - LOG_INFO("Trying to load amd_fidelityfx_vk.dll with detours"); - - _configure = (PfnFfxConfigure)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxConfigure"); - _createContext = (PfnFfxCreateContext)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxDispatch"); - _query = (PfnFfxQuery)DetourFindFunction("amd_fidelityfx_vk.dll", "ffxQuery"); - - _moduleLoaded = _configure != nullptr; - } + _moduleLoaded = FfxApiProxy::InitFfxVk(); if (_moduleLoaded) LOG_INFO("amd_fidelityfx_vk.dll methods loaded!"); else - LOG_ERROR("can't load amd_fidelityfx_vk.dll methods!"); + LOG_ERROR("Can't load amd_fidelityfx_vk.dll methods!"); } bool FSR31FeatureVk::InitFSR3(const NVSDK_NGX_Parameter* InParameters) @@ -103,14 +74,14 @@ bool FSR31FeatureVk::InitFSR3(const NVSDK_NGX_Parameter* InParameters) uint64_t versionCount = 0; versionQuery.outputCount = &versionCount; // get number of versions for allocation - _query(nullptr, &versionQuery.header); + FfxApiProxy::VULKAN_Query()(nullptr, &versionQuery.header); Config::Instance()->fsr3xVersionIds.resize(versionCount); Config::Instance()->fsr3xVersionNames.resize(versionCount); versionQuery.versionIds = Config::Instance()->fsr3xVersionIds.data(); versionQuery.versionNames = Config::Instance()->fsr3xVersionNames.data(); // fill version ids and names arrays. - _query(nullptr, &versionQuery.header); + FfxApiProxy::VULKAN_Query()(nullptr, &versionQuery.header); _contextDesc.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE; _contextDesc.fpMessage = FfxLogCallback; @@ -213,13 +184,13 @@ bool FSR31FeatureVk::InitFSR3(const NVSDK_NGX_Parameter* InParameters) backendDesc.header.pNext = &ov.header; LOG_DEBUG("_createContext!"); - auto ret = _createContext(&_context, &_contextDesc.header, NULL); + auto ret = FfxApiProxy::VULKAN_CreateContext()(&_context, &_contextDesc.header, NULL); Config::Instance()->dxgiSkipSpoofing = false; if (ret != FFX_API_RETURN_OK) { - LOG_ERROR("_createContext error: {0}", ResultToString(ret)); + LOG_ERROR("_createContext error: {0}", FfxApiProxy::ReturnCodeToString(ret)); return false; } @@ -484,18 +455,18 @@ bool FSR31FeatureVk::Evaluate(VkCommandBuffer InCmdBuffer, NVSDK_NGX_Parameter* m_upscalerKeyValueConfig.header.type = FFX_API_CONFIGURE_DESC_TYPE_UPSCALE_KEYVALUE; m_upscalerKeyValueConfig.key = FFX_API_CONFIGURE_UPSCALE_KEY_FVELOCITYFACTOR; m_upscalerKeyValueConfig.ptr = &_velocity; - auto result = _configure(&_context, &m_upscalerKeyValueConfig.header); + auto result = FfxApiProxy::VULKAN_Configure()(&_context, &m_upscalerKeyValueConfig.header); if (result != FFX_API_RETURN_OK) LOG_WARN("Velocity configure result: {}", (UINT)result); } LOG_DEBUG("Dispatch!!"); - auto result = _dispatch(&_context, ¶ms.header); + auto result = FfxApiProxy::VULKAN_Dispatch()(&_context, ¶ms.header); if (result != FFX_API_RETURN_OK) { - LOG_ERROR("ffxFsr2ContextDispatch error: {0}", ResultToString(result)); + LOG_ERROR("ffxFsr2ContextDispatch error: {0}", FfxApiProxy::ReturnCodeToString(result)); return false; } diff --git a/OptiScaler/backends/fsr31/FSR31Feature_Vk.h b/OptiScaler/backends/fsr31/FSR31Feature_Vk.h index 72955dcf..3b546731 100644 --- a/OptiScaler/backends/fsr31/FSR31Feature_Vk.h +++ b/OptiScaler/backends/fsr31/FSR31Feature_Vk.h @@ -16,4 +16,10 @@ public: bool Init(VkInstance InInstance, VkPhysicalDevice InPD, VkDevice InDevice, VkCommandBuffer InCmdList, PFN_vkGetInstanceProcAddr InGIPA, PFN_vkGetDeviceProcAddr InGDPA, NVSDK_NGX_Parameter* InParameters) override; bool Evaluate(VkCommandBuffer InCmdBuffer, NVSDK_NGX_Parameter* InParameters) override; + + ~FSR31FeatureVk() + { + if (_context != nullptr) + FfxApiProxy::VULKAN_DestroyContext()(&_context, NULL); + } }; diff --git a/OptiScaler/dllmain.cpp b/OptiScaler/dllmain.cpp index 5e06ef75..5b4ad90f 100644 --- a/OptiScaler/dllmain.cpp +++ b/OptiScaler/dllmain.cpp @@ -6,6 +6,7 @@ #include "Util.h" #include "NVNGX_Proxy.h" #include "XeSS_Proxy.h" +#include "FfxApi_Proxy.h" #include "hooks/HooksDx.h" #include "hooks/HooksVk.h" @@ -1595,6 +1596,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: + DisableThreadLibraryCalls(hModule); + if (loadCount > 1) { LOG_INFO("DLL_PROCESS_ATTACH from module: {0:X}, count: {1}", (UINT64)hModule, loadCount); @@ -1605,8 +1608,6 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv dllModule = hModule; processId = GetCurrentProcessId(); - DisableThreadLibraryCalls(hModule); - loadCount++; #ifdef VER_PRE_RELEASE @@ -1671,6 +1672,13 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv if (!XeSSProxy::InitXeSS()) spdlog::warn("Can't init XeSS!"); + // Init FfxApi proxy + if (!FfxApiProxy::InitFfxDx12()) + spdlog::warn("Can't init Dx12 FfxApi!"); + + if (!FfxApiProxy::InitFfxVk()) + spdlog::warn("Can't init Vulkan FfxApi!"); + // Check for working mode and attach hooks spdlog::info(""); CheckWorkingMode(); @@ -1704,11 +1712,11 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv break; case DLL_THREAD_ATTACH: - LOG_TRACE("DLL_THREAD_ATTACH from module: {0:X}, count: {1}", (UINT64)hModule, loadCount); + LOG_DEBUG_ONLY("DLL_THREAD_ATTACH from module: {0:X}, count: {1}", (UINT64)hModule, loadCount); break; case DLL_THREAD_DETACH: - LOG_TRACE("DLL_THREAD_DETACH from module: {0:X}, count: {1}", (UINT64)hModule, loadCount); + LOG_DEBUG_ONLY("DLL_THREAD_DETACH from module: {0:X}, count: {1}", (UINT64)hModule, loadCount); break; default: diff --git a/OptiScaler/hooks/HooksDx.cpp b/OptiScaler/hooks/HooksDx.cpp index 83b0caed..74452db3 100644 --- a/OptiScaler/hooks/HooksDx.cpp +++ b/OptiScaler/hooks/HooksDx.cpp @@ -149,13 +149,6 @@ static PFN_Dispatch o_Dispatch = nullptr; static PFN_DiscardResource o_DiscardResource = nullptr; #endif -// FSR 3.x methods -static PfnFfxCreateContext _createContext = nullptr; -static PfnFfxDestroyContext _destroyContext = nullptr; -static PfnFfxConfigure _configure = nullptr; -static PfnFfxQuery _query = nullptr; -static PfnFfxDispatch _dispatch = nullptr; - // swapchains variables static ankerl::unordered_dense::map fgSwapChains; static bool fgSkipSCWrapping = false; @@ -271,45 +264,6 @@ static HRESULT hkEnumAdapters1(IDXGIFactory1* This, UINT Adapter, IUnknown** ppA static HRESULT hkEnumAdapterByLuid(IDXGIFactory4* This, LUID AdapterLuid, REFIID riid, IUnknown** ppvAdapter); static HRESULT hkEnumAdapterByGpuPreference(IDXGIFactory6* This, UINT Adapter, DXGI_GPU_PREFERENCE GpuPreference, REFIID riid, IUnknown** ppvAdapter); -static void LoadFSR31Funcs() -{ - - ID3D12Resource* textureResource; - ID3D12DescriptorHeap* srvHeap; - D3D12_GPU_DESCRIPTOR_HANDLE srvGpuHandle; - - LOG_DEBUG("Loading amd_fidelityfx_dx12.dll methods"); - - auto file = Util::DllPath().parent_path() / "amd_fidelityfx_dx12.dll"; - LOG_INFO("Trying to load {}", file.string()); - - auto _dll = LoadLibrary(file.wstring().c_str()); - if (_dll != nullptr) - { - _configure = (PfnFfxConfigure)GetProcAddress(_dll, "ffxConfigure"); - _createContext = (PfnFfxCreateContext)GetProcAddress(_dll, "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)GetProcAddress(_dll, "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)GetProcAddress(_dll, "ffxDispatch"); - _query = (PfnFfxQuery)GetProcAddress(_dll, "ffxQuery"); - } - - if (_configure == nullptr) - { - LOG_INFO("Trying to load amd_fidelityfx_dx12.dll with detours"); - - _configure = (PfnFfxConfigure)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxConfigure"); - _createContext = (PfnFfxCreateContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxCreateContext"); - _destroyContext = (PfnFfxDestroyContext)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDestroyContext"); - _dispatch = (PfnFfxDispatch)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxDispatch"); - _query = (PfnFfxQuery)DetourFindFunction("amd_fidelityfx_dx12.dll", "ffxQuery"); - } - - if (_configure != nullptr) - LOG_INFO("amd_fidelityfx_dx12.dll methods loaded!"); - else - LOG_ERROR("can't load amd_fidelityfx_dx12.dll methods!"); -} - static void FfxFgLogCallback(uint32_t type, const wchar_t* message) { std::wstring string(message); @@ -524,7 +478,7 @@ static void GetHudless(ID3D12GraphicsCommandList* This) if (Config::Instance()->CurrentFeature != nullptr) fgLastFGFrame = Config::Instance()->CurrentFeature->FrameCount(); - dispatchResult = _dispatch(reinterpret_cast(pUserCtx), ¶ms->header); + dispatchResult = FfxApiProxy::D3D12_Dispatch()(reinterpret_cast(pUserCtx), ¶ms->header); ID3D12CommandList* cl[1] = { nullptr }; result = HooksDx::fgCopyCommandList->Close(); cl[0] = HooksDx::fgCopyCommandList; @@ -551,7 +505,7 @@ static void GetHudless(ID3D12GraphicsCommandList* This) m_FrameGenerationConfig.header.pNext = &debugDesc.header; Config::Instance()->dxgiSkipSpoofing = true; - ffxReturnCode_t retCode = _configure(&HooksDx::fgContext, &m_FrameGenerationConfig.header); + ffxReturnCode_t retCode = FfxApiProxy::D3D12_Configure()(&HooksDx::fgContext, &m_FrameGenerationConfig.header); Config::Instance()->dxgiSkipSpoofing = false; LOG_DEBUG("_configure result: {0:X}, frame: {1}", retCode, frame); @@ -605,7 +559,7 @@ static void GetHudless(ID3D12GraphicsCommandList* This) } Config::Instance()->dxgiSkipSpoofing = true; - retCode = _dispatch(&HooksDx::fgContext, &dfgPrepare.header); + retCode = FfxApiProxy::D3D12_Dispatch()(&HooksDx::fgContext, &dfgPrepare.header); fgDispatchCalled = true; Config::Instance()->dxgiSkipSpoofing = false; LOG_DEBUG("_dispatch result: {0}, frame: {1}", retCode, frame); @@ -1724,7 +1678,7 @@ static HRESULT hkCreateSwapChain(IDXGIFactory* pFactory, IUnknown* pDevice, DXGI } ID3D12CommandQueue* cq = nullptr; - if (Config::Instance()->FGUseFGSwapChain.value_or(true) && !fgSkipSCWrapping && _createContext != nullptr && pDevice->QueryInterface(IID_PPV_ARGS(&cq)) == S_OK) + if (Config::Instance()->FGUseFGSwapChain.value_or(true) && !fgSkipSCWrapping && FfxApiProxy::InitFfxDx12() && pDevice->QueryInterface(IID_PPV_ARGS(&cq)) == S_OK) { cq->SetName(L"GameQueue"); SwapChainInfo scInfo{}; @@ -1744,7 +1698,7 @@ static HRESULT hkCreateSwapChain(IDXGIFactory* pFactory, IUnknown* pDevice, DXGI Config::Instance()->dxgiSkipSpoofing = true; Config::Instance()->SkipHeapCapture = true; - auto result = _createContext(&HooksDx::fgSwapChainContext, &createSwapChainDesc.header, nullptr); + auto result = FfxApiProxy::D3D12_CreateContext()(&HooksDx::fgSwapChainContext, &createSwapChainDesc.header, nullptr); Config::Instance()->SkipHeapCapture = false; Config::Instance()->dxgiSkipSpoofing = false; @@ -1756,10 +1710,12 @@ static HRESULT hkCreateSwapChain(IDXGIFactory* pFactory, IUnknown* pDevice, DXGI scInfo.swapChainBufferCount = pDesc->BufferCount; scInfo.swapChain = (IDXGISwapChain4*)*ppSwapChain; fgSwapChains.insert_or_assign(pDesc->OutputWindow, scInfo); + + LOG_DEBUG("Created FSR-FG swapchain"); return S_OK; } - LOG_ERROR("_createContext error: {}", result); + LOG_ERROR("D3D12_CreateContext error: {}", result); return E_INVALIDARG; } @@ -1830,7 +1786,7 @@ static HRESULT hkCreateSwapChainForHwnd(IDXGIFactory* This, IUnknown* pDevice, H } ID3D12CommandQueue* cq = nullptr; - if (Config::Instance()->FGUseFGSwapChain.value_or(true) && !fgSkipSCWrapping && _createContext != nullptr && pDevice->QueryInterface(IID_PPV_ARGS(&cq)) == S_OK) + if (Config::Instance()->FGUseFGSwapChain.value_or(true) && !fgSkipSCWrapping && FfxApiProxy::InitFfxDx12() && pDevice->QueryInterface(IID_PPV_ARGS(&cq)) == S_OK) { SwapChainInfo scInfo{}; scInfo.gameCommandQueue = cq; @@ -1854,7 +1810,7 @@ static HRESULT hkCreateSwapChainForHwnd(IDXGIFactory* This, IUnknown* pDevice, H fgSkipSCWrapping = true; Config::Instance()->SkipHeapCapture = true; - auto result = _createContext(&HooksDx::fgSwapChainContext, &createSwapChainDesc.header, nullptr); + auto result = FfxApiProxy::D3D12_CreateContext()(&HooksDx::fgSwapChainContext, &createSwapChainDesc.header, nullptr); Config::Instance()->SkipHeapCapture = false; fgSkipSCWrapping = false; @@ -1866,6 +1822,8 @@ static HRESULT hkCreateSwapChainForHwnd(IDXGIFactory* This, IUnknown* pDevice, H scInfo.swapChainBufferCount = pDesc->BufferCount; scInfo.swapChain = (IDXGISwapChain4*)*ppSwapChain; fgSwapChains.insert_or_assign(hWnd, scInfo); + + LOG_DEBUG("Created FSR-FG swapchain"); return S_OK; } @@ -2622,8 +2580,6 @@ void HooksDx::HookDx() DetourTransactionCommit(); } - - LoadFSR31Funcs(); } UINT HooksDx::ClearFrameResources() @@ -2768,10 +2724,10 @@ void HooksDx::CreateFGContext(ID3D12Device* InDevice, IFeature* deviceContext) m_FrameGenerationConfig.header.type = FFX_API_CONFIGURE_DESC_TYPE_FRAMEGENERATION; m_FrameGenerationConfig.frameGenerationEnabled = true; m_FrameGenerationConfig.swapChain = HooksDx::currentSwapchain; - //m_FrameGenerationConfig.presentCallback = nullptr; + m_FrameGenerationConfig.presentCallback = nullptr; m_FrameGenerationConfig.HUDLessColor = FfxApiResource({}); - auto result = _configure(&HooksDx::fgContext, &m_FrameGenerationConfig.header); + auto result = FfxApiProxy::D3D12_Configure()(&HooksDx::fgContext, &m_FrameGenerationConfig.header); HooksDx::fgIsActive = (result == FFX_API_RETURN_OK); @@ -2810,7 +2766,7 @@ void HooksDx::CreateFGContext(ID3D12Device* InDevice, IFeature* deviceContext) Config::Instance()->dxgiSkipSpoofing = true; Config::Instance()->SkipHeapCapture = true; - ffxReturnCode_t retCode = _createContext(&HooksDx::fgContext, &createFg.header, nullptr); + ffxReturnCode_t retCode = FfxApiProxy::D3D12_CreateContext()(&HooksDx::fgContext, &createFg.header, nullptr); Config::Instance()->SkipHeapCapture = false; Config::Instance()->dxgiSkipSpoofing = false; LOG_INFO("_createContext result: {0:X}", retCode); @@ -2836,7 +2792,7 @@ void HooksDx::StopAndDestroyFGContext(bool destroy, bool shutDown) m_FrameGenerationConfig.presentCallback = nullptr; m_FrameGenerationConfig.HUDLessColor = FfxApiResource({}); - auto result = _configure(&HooksDx::fgContext, &m_FrameGenerationConfig.header); + auto result = FfxApiProxy::D3D12_Configure()(&HooksDx::fgContext, &m_FrameGenerationConfig.header); HooksDx::fgIsActive = false; @@ -2846,7 +2802,7 @@ void HooksDx::StopAndDestroyFGContext(bool destroy, bool shutDown) if (destroy && HooksDx::fgContext != nullptr) { - auto result = _destroyContext(&HooksDx::fgContext, nullptr); + auto result = FfxApiProxy::D3D12_DestroyContext()(&HooksDx::fgContext, nullptr); if (!shutDown) LOG_INFO(" FG _destroyContext result: {0:X}", result); diff --git a/OptiScaler/hooks/HooksDx.h b/OptiScaler/hooks/HooksDx.h index 7d07fa47..e5045059 100644 --- a/OptiScaler/hooks/HooksDx.h +++ b/OptiScaler/hooks/HooksDx.h @@ -4,12 +4,11 @@ #include "../format_transfer/FT_Dx12.h" #include "../backends/IFeature.h" - #include #include #include -#include +#include "../FfxApi_Proxy.h" #include #include diff --git a/OptiScaler/pch.h b/OptiScaler/pch.h index e19087ff..9cba73a0 100644 --- a/OptiScaler/pch.h +++ b/OptiScaler/pch.h @@ -67,6 +67,13 @@ inline DWORD processId; #define LOG_FUNC_RESULT(result) \ spdlog::trace(__FUNCTION__ " result: {0:X}" , (UINT64)result) +typedef struct _feature_version +{ + unsigned int major; + unsigned int minor; + unsigned int patch; +} feature_version; + inline static std::string wstring_to_string(const std::wstring& wide_str) { std::string str(wide_str.length(), 0);