From f4bba7e22ea17bb419867a44c2f959b5cfeb3f43 Mon Sep 17 00:00:00 2001 From: cdozdil Date: Sat, 21 Dec 2024 15:23:59 +0300 Subject: [PATCH] added high performance adapter selection --- OptiScaler/Config.cpp | 4 ++ OptiScaler/Config.h | 1 + OptiScaler/OptiScaler.vcxproj | 2 +- OptiScaler/dllmain.h | 94 ++++++++++++++++++++++++++++++++++- OptiScaler/resource.h | 2 +- nvngx.ini | 4 ++ 6 files changed, 103 insertions(+), 4 deletions(-) diff --git a/OptiScaler/Config.cpp b/OptiScaler/Config.cpp index 1c2894fc..6495a986 100644 --- a/OptiScaler/Config.cpp +++ b/OptiScaler/Config.cpp @@ -473,6 +473,9 @@ bool Config::Reload(std::filesystem::path iniPath) if (!RestoreGraphicSignature.has_value()) RestoreGraphicSignature = readBool("Hotfix", "RestoreGraphicSignature"); + if (!PreferDedicatedGpu.has_value()) + PreferDedicatedGpu = readBool("Hotfix", "PreferDedicatedGpu"); + if (!SkipFirstFrames.has_value()) SkipFirstFrames = readInt("Hotfix", "SkipFirstFrames"); @@ -866,6 +869,7 @@ bool Config::SaveIni() ini.SetValue("Hotfix", "SkipFirstFrames", GetIntValue(Instance()->SkipFirstFrames).c_str()); ini.SetValue("Hotfix", "UsePrecompiledShaders", GetBoolValue(Instance()->UsePrecompiledShaders).c_str()); + ini.SetValue("Hotfix", "PreferDedicatedGpu", GetBoolValue(Instance()->PreferDedicatedGpu).c_str()); ini.SetValue("Hotfix", "UseGenericAppIdWithDlss", GetBoolValue(Instance()->UseGenericAppIdWithDlss).c_str()); diff --git a/OptiScaler/Config.h b/OptiScaler/Config.h index 232de9b7..bc9d87df 100644 --- a/OptiScaler/Config.h +++ b/OptiScaler/Config.h @@ -110,6 +110,7 @@ public: std::optional UsePrecompiledShaders; std::optional UseGenericAppIdWithDlss; + std::optional PreferDedicatedGpu; std::optional ColorResourceBarrier; std::optional MVResourceBarrier; diff --git a/OptiScaler/OptiScaler.vcxproj b/OptiScaler/OptiScaler.vcxproj index 9b5098d5..39bd1fb8 100644 --- a/OptiScaler/OptiScaler.vcxproj +++ b/OptiScaler/OptiScaler.vcxproj @@ -82,7 +82,7 @@ $(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;$(SolutionDir)external\freetype;$(IncludePath) $(ProjectDir)fsr2\lib;$(ProjectDir)fsr2_212\lib;$(ProjectDir)fsr31\lib;$(ProjectDir)vulkan;$(ProjectDir)d3dx;$(ProjectDir)detours;$(SolutionDir)external\xess\lib;$(SolutionDir)external\freetype;$(LibraryPath) dxgi - F:\source\optiscaler\external\FidelityFX-SDK\bin\ + D:\Folders\Games\Deep Rock Galactic\FSD\Binaries\Win64\ .\x64\Debug .dll diff --git a/OptiScaler/dllmain.h b/OptiScaler/dllmain.h index aaee87ea..3dfb9ab1 100644 --- a/OptiScaler/dllmain.h +++ b/OptiScaler/dllmain.h @@ -1585,7 +1585,52 @@ HRESULT WINAPI detEnumAdapters1(IDXGIFactory1* This, UINT Adapter, IDXGIAdapter1 { AttachToFactory(This); - auto result = ptrEnumAdapters1(This, Adapter, ppAdapter); + HRESULT result = S_OK; + + if (Config::Instance()->PreferDedicatedGpu.value_or(true)) + { + if (Adapter != 0) + { + LOG_DEBUG("{}, returning not found", Adapter); + return DXGI_ERROR_NOT_FOUND; + } + + IDXGIFactory6* factory6 = nullptr; + + if (This->QueryInterface(IID_PPV_ARGS(&factory6)) == S_OK && factory6 != nullptr) + { + LOG_DEBUG("Trying to select high performance adapter ({})", Adapter); + + result = factory6->EnumAdapterByGpuPreference(Adapter, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(ppAdapter)); + + if (result != S_OK) + { + LOG_ERROR("Can't get high performance adapter: {:X}, fallback to standard method", Adapter); + result = ptrEnumAdapters1(This, Adapter, ppAdapter); + } + + if (result == S_OK) + { + DXGI_ADAPTER_DESC desc; + if ((*ppAdapter)->GetDesc(&desc) == S_OK) + { + std::wstring name(desc.Description); + LOG_DEBUG("High performance adapter ({}) will be used", wstring_to_string(name)); + } + else + { + LOG_DEBUG("High performance adapter (Can't get description!) will be used"); + } + + } + + factory6->Release(); + } + } + else + { + result = ptrEnumAdapters1(This, Adapter, ppAdapter); + } if (result == S_OK) AttachToAdapter(*ppAdapter); @@ -1597,7 +1642,52 @@ HRESULT WINAPI detEnumAdapters(IDXGIFactory* This, UINT Adapter, IDXGIAdapter** { AttachToFactory(This); - auto result = ptrEnumAdapters(This, Adapter, ppAdapter); + HRESULT result = S_OK; + + if (Config::Instance()->PreferDedicatedGpu.value_or(true)) + { + if (Adapter > 0) + { + LOG_DEBUG("{}, returning not found", Adapter); + return DXGI_ERROR_NOT_FOUND; + } + + IDXGIFactory6* factory6 = nullptr; + + if (This->QueryInterface(IID_PPV_ARGS(&factory6)) == S_OK && factory6 != nullptr) + { + LOG_DEBUG("Trying to select high performance adapter ({})", Adapter); + + result = factory6->EnumAdapterByGpuPreference(Adapter, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(ppAdapter)); + + if (result != S_OK) + { + LOG_ERROR("Can't get high performance adapter: {:X}, fallback to standard method", Adapter); + result = ptrEnumAdapters(This, Adapter, ppAdapter); + } + + if (result == S_OK) + { + DXGI_ADAPTER_DESC desc; + if ((*ppAdapter)->GetDesc(&desc) == S_OK) + { + std::wstring name(desc.Description); + LOG_DEBUG("High performance adapter ({}) will be used", wstring_to_string(name)); + } + else + { + LOG_DEBUG("High performance adapter (Can't get description!) will be used"); + } + + } + + factory6->Release(); + } + } + else + { + result = ptrEnumAdapters(This, Adapter, ppAdapter); + } if (result == S_OK) AttachToAdapter(*ppAdapter); diff --git a/OptiScaler/resource.h b/OptiScaler/resource.h index 6cd3799a..ca8b2b54 100644 --- a/OptiScaler/resource.h +++ b/OptiScaler/resource.h @@ -21,7 +21,7 @@ #define VER_MAJOR_VERSION 0 #define VER_MINOR_VERSION 7 #define VER_HOTFIX_VERSION 0 -#define VER_BUILD_NUMBER 84 +#define VER_BUILD_NUMBER 85 #define VER_PRE_RELEASE diff --git a/nvngx.ini b/nvngx.ini index 4ea69e05..a4ea73f2 100644 --- a/nvngx.ini +++ b/nvngx.ini @@ -561,6 +561,10 @@ UseHDR10=auto ; ------------------------------------------------------- [Hotfix] ; ------------------------------------------------------- +; OptiScaler will try to force high performance GPU +; true or false - Default (auto) is true +PreferDedicatedGpu=auto + ; Override value for mipmap lod bias ; -15.0 - 15.0 - Default (auto) is disabled MipmapBiasOverride=auto