From 4ef89e9291bc267f211a91004dbbc31378c757fc Mon Sep 17 00:00:00 2001 From: cdozdil Date: Sat, 7 Nov 2026 01:15:32 +0300 Subject: [PATCH] Track NGX clients and only shut down for the last one --- OptiScaler/inputs/NVNGX_DLSS.h | 49 +++++++++++++++++++++++++++ OptiScaler/inputs/NVNGX_DLSS_Dx11.cpp | 15 ++++++++ OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp | 11 ++++++ OptiScaler/inputs/NVNGX_DLSS_Vk.cpp | 12 +++++++ 4 files changed, 87 insertions(+) diff --git a/OptiScaler/inputs/NVNGX_DLSS.h b/OptiScaler/inputs/NVNGX_DLSS.h index 48d478a8..cf650b04 100644 --- a/OptiScaler/inputs/NVNGX_DLSS.h +++ b/OptiScaler/inputs/NVNGX_DLSS.h @@ -1,5 +1,54 @@ #pragma once +#include + +#include +#include + +// Modules using an NGX instance, the game, Streamline and mods can share one +class NVNGXClients +{ + private: + std::mutex _mutex; + std::set _modules; + + public: + void Add(void* returnAddress) + { + auto module = Util::GetCallerModule(returnAddress); + + // Nested calls and Opti's own inputs, they never shut down + if (module == dllModule) + return; + + std::lock_guard lock(_mutex); + + if (_modules.insert(module).second) + LOG_INFO("{}", Util::WhoIsTheCaller(returnAddress)); + } + + // False while another module still uses the instance, also for a module's second shutdown + bool Remove(void* returnAddress) + { + auto module = Util::GetCallerModule(returnAddress); + + if (module == dllModule) + return true; + + std::lock_guard lock(_mutex); + + _modules.erase(module); + + if (_modules.empty()) + return true; + + LOG_INFO("Skipping shutdown from {}, {} other module(s) still use NGX", Util::WhoIsTheCaller(returnAddress), + _modules.size()); + + return false; + } +}; + void NVNGXDx11DeviceDestroyed(IUnknown* device); void NVNGXDx12DeviceDestroyed(IUnknown* device); void NVNGXVkDeviceDestroyed(VkDevice device); diff --git a/OptiScaler/inputs/NVNGX_DLSS_Dx11.cpp b/OptiScaler/inputs/NVNGX_DLSS_Dx11.cpp index eab40ec5..7395183f 100644 --- a/OptiScaler/inputs/NVNGX_DLSS_Dx11.cpp +++ b/OptiScaler/inputs/NVNGX_DLSS_Dx11.cpp @@ -24,6 +24,7 @@ static ankerl::unordered_dense::map> Dx static int evalCounter = 0; static bool shutdown = false; static bool _skipInit = false; +static NVNGXClients _clients; class ScopedInitDx11 { @@ -93,6 +94,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Init_Ext(unsigned long long InApp NVSDK_NGX_Version InSDKVersion, const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo) { + _clients.Add(_ReturnAddress()); + NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; if (InFeatureInfo != nullptr) @@ -162,6 +165,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Init(unsigned long long InApplica const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo, NVSDK_NGX_Version InSDKVersion) { + _clients.Add(_ReturnAddress()); + NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; if (InFeatureInfo != nullptr) @@ -205,6 +210,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Init_ProjectID(const char* InProj ID3D11Device* InDevice, NVSDK_NGX_Version InSDKVersion, const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo) { + _clients.Add(_ReturnAddress()); + NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; if (InFeatureInfo != nullptr) @@ -256,6 +263,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Init_with_ProjectID( const wchar_t* InApplicationDataPath, ID3D11Device* InDevice, const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo, NVSDK_NGX_Version InSDKVersion) { + _clients.Add(_ReturnAddress()); + auto result = NVSDK_NGX_D3D11_Init_Ext(0x1337, InApplicationDataPath, InDevice, InSDKVersion, InFeatureInfo); LOG_INFO("InProjectId: {0}", InProjectId); @@ -275,6 +284,9 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Init_with_ProjectID( NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Shutdown() { + if (!_clients.Remove(_ReturnAddress())) + return NVSDK_NGX_Result_Success; + shutdown = true; // for (auto const& [key, val] : Dx11Contexts) @@ -309,6 +321,9 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Shutdown() NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Shutdown1(ID3D11Device* InDevice) { + if (!_clients.Remove(_ReturnAddress())) + return NVSDK_NGX_Result_Success; + shutdown = true; if (Config::Instance()->DLSSEnabled.value_or_default() && NVNGXProxy::IsDx11Inited() && diff --git a/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp b/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp index 70aaf766..975eb772 100644 --- a/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp +++ b/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp @@ -48,6 +48,7 @@ static std::atomic _d3d12DeviceDestroyed { false }; static int evalCounter = 0; static bool shutdown = false; static bool _skipInit = false; +static NVNGXClients _clients; class ScopedInitDx12 { @@ -77,6 +78,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Init_Ext(unsigned long long InApp const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; @@ -160,6 +162,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Init(unsigned long long InApplica NVSDK_NGX_Version InSDKVersion) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; @@ -218,6 +221,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Init_ProjectID(const char* InProj const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; @@ -276,6 +280,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Init_with_ProjectID( NVSDK_NGX_Version InSDKVersion) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); LOG_INFO("InProjectId: {0}", InProjectId); LOG_INFO("InEngineType: {0}", (int) InEngineType); @@ -308,6 +313,9 @@ void NVNGXDx12DeviceDestroyed(IUnknown* device) NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Shutdown(void) { + if (!_clients.Remove(_ReturnAddress())) + return NVSDK_NGX_Result_Success; + shutdown = true; State::Instance().nvngxDx12Inited = false; @@ -366,6 +374,9 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Shutdown(void) NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Shutdown1(ID3D12Device* InDevice) { + if (!_clients.Remove(_ReturnAddress())) + return NVSDK_NGX_Result_Success; + shutdown = true; State::Instance().nvngxDx12Inited = false; diff --git a/OptiScaler/inputs/NVNGX_DLSS_Vk.cpp b/OptiScaler/inputs/NVNGX_DLSS_Vk.cpp index 7381a1a6..70c0f984 100644 --- a/OptiScaler/inputs/NVNGX_DLSS_Vk.cpp +++ b/OptiScaler/inputs/NVNGX_DLSS_Vk.cpp @@ -27,6 +27,7 @@ static ankerl::unordered_dense::map> VkCo static int evalCounter = 0; static bool shutdown = false; static bool _skipInit = false; +static NVNGXClients _clients; class ScopedInitVk { @@ -57,6 +58,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_Init_Ext2( NVSDK_NGX_Version InSDKVersion, const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; @@ -167,6 +169,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_Init_Ext(unsigned long long InAp const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; @@ -211,6 +214,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_Init_ProjectID_Ext( const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; @@ -261,6 +265,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_Init(unsigned long long InApplic NVSDK_NGX_Version InSDKVersion) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; @@ -305,6 +310,7 @@ NVSDK_NGX_VULKAN_Init_ProjectID(const char* InProjectId, NVSDK_NGX_EngineType In NVSDK_NGX_Version InSDKVersion, const NVSDK_NGX_FeatureCommonInfo* InFeatureInfo) { LOG_FUNC(); + _clients.Add(_ReturnAddress()); NVSDK_NGX_FeatureCommonInfo localFeatureInfo = {}; @@ -989,6 +995,9 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_EvaluateFeature(VkCommandBuffer NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_Shutdown(void) { + if (!_clients.Remove(_ReturnAddress())) + return NVSDK_NGX_Result_Success; + shutdown = true; // for (auto const& [key, val] : VkContexts) { @@ -1025,6 +1034,9 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_Shutdown(void) NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_Shutdown1(VkDevice InDevice) { + if (!_clients.Remove(_ReturnAddress())) + return NVSDK_NGX_Result_Success; + shutdown = true; if (Config::Instance()->DLSSEnabled.value_or_default() && NVNGXProxy::IsVulkanInited() &&