Track NGX clients and only shut down for the last one

This commit is contained in:
cdozdil committed 2026-11-07 01:15:32 +03:00
1 parent 6bcce9ca5a
commit 4ef89e9291
4 files changed
+87

No files matched your search

+49
View File
@@ -1,5 +1,54 @@
#pragma once
#include <Util.h>
#include <intrin.h>
#include <set>
// Modules using an NGX instance, the game, Streamline and mods can share one
class NVNGXClients
{
private:
std::mutex _mutex;
std::set<HMODULE> _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<std::mutex> 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<std::mutex> 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);
+15
View File
@@ -24,6 +24,7 @@ static ankerl::unordered_dense::map<unsigned int, ContextData<IFeature_Dx11>> 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() &&
+11
View File
@@ -48,6 +48,7 @@ static std::atomic<bool> _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;
+12
View File
@@ -27,6 +27,7 @@ static ankerl::unordered_dense::map<unsigned int, ContextData<IFeature_Vk>> 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() &&