From 1d4dc08cbf63dff2f68a79203abbd2759345a32c Mon Sep 17 00:00:00 2001 From: cdozdil Date: Fri, 9 Jan 2026 02:14:30 +0300 Subject: [PATCH] Added more D3D12 device hook points --- OptiScaler/hooks/D3D12_Hooks.cpp | 6 ++-- OptiScaler/hooks/D3D12_Hooks.h | 3 ++ OptiScaler/hooks/DxgiFactory_Hooks.cpp | 39 ++++++++++++++++++++++++ OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp | 4 ++- OptiScaler/wrapped/wrapped_swapchain.cpp | 3 ++ 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/OptiScaler/hooks/D3D12_Hooks.cpp b/OptiScaler/hooks/D3D12_Hooks.cpp index babee06d..61b32ca5 100644 --- a/OptiScaler/hooks/D3D12_Hooks.cpp +++ b/OptiScaler/hooks/D3D12_Hooks.cpp @@ -11,7 +11,6 @@ #include -#include #include #pragma intrinsic(_ReturnAddress) @@ -44,9 +43,6 @@ typedef HRESULT (*PFN_D3D12GetInterface)(REFCLSID rclsid, REFIID riid, void** pp typedef HRESULT (*PFN_CreateDevice)(ID3D12DeviceFactory* pFactory, IUnknown* adapter, D3D_FEATURE_LEVEL FeatureLevel, REFIID riid, void** ppvDevice); -typedef HRESULT(WINAPI* PFN_D3D12SerializeVersionedRootSignature)( - const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* pRootSignature, ID3DBlob** ppBlob, ID3DBlob** ppErrorBlob); - typedef ULONG (*PFN_Release)(IUnknown* This); static PFN_CreateSampler o_CreateSampler = nullptr; @@ -1014,3 +1010,5 @@ void D3D12Hooks::Unhook() DetourTransactionCommit(); } + +void D3D12Hooks::HookDevice(ID3D12Device* device) { HookToDevice(device); } diff --git a/OptiScaler/hooks/D3D12_Hooks.h b/OptiScaler/hooks/D3D12_Hooks.h index 891ff11d..87200031 100644 --- a/OptiScaler/hooks/D3D12_Hooks.h +++ b/OptiScaler/hooks/D3D12_Hooks.h @@ -2,10 +2,13 @@ #include +#include + class D3D12Hooks { public: static void Hook(); static void HookAgility(HMODULE module); + static void HookDevice(ID3D12Device* device); static void Unhook(); }; diff --git a/OptiScaler/hooks/DxgiFactory_Hooks.cpp b/OptiScaler/hooks/DxgiFactory_Hooks.cpp index 00072096..0ea4fed6 100644 --- a/OptiScaler/hooks/DxgiFactory_Hooks.cpp +++ b/OptiScaler/hooks/DxgiFactory_Hooks.cpp @@ -2,6 +2,8 @@ #include +#include "D3D12_Hooks.h" + #include #include @@ -258,6 +260,21 @@ HRESULT DxgiFactoryHooks::CreateSwapChain(IDXGIFactory* realFactory, IUnknown* p { cq->Release(); + if (State::Instance().currentD3D12Device == nullptr) + { + ID3D12Device* device = nullptr; + if (cq->GetDevice(IID_PPV_ARGS(&device)) == S_OK) + { + if (device != nullptr) + { + State::Instance().currentD3D12Device = device; + LOG_INFO("Captured D3D12 device from command queue: {:X}", (UINT64) device); + D3D12Hooks::HookDevice(State::Instance().currentD3D12Device); + device->Release(); + } + } + } + if (!Util::CheckForRealObject(__FUNCTION__, cq, &real)) real = cq; @@ -491,6 +508,28 @@ HRESULT DxgiFactoryHooks::CreateSwapChainForHwnd(IDXGIFactory2* realFactory, IUn { cq->Release(); + LOG_DEBUG("currentD3D12Device: {:X}", (UINT64) State::Instance().currentD3D12Device); + if (State::Instance().currentD3D12Device == nullptr) + { + LOG_DEBUG("Capturing D3D12 device from command queue"); + ID3D12Device* device = nullptr; + auto capRes = cq->GetDevice(IID_PPV_ARGS(&device)); + if (SUCCEEDED(capRes)) + { + if (device != nullptr) + { + State::Instance().currentD3D12Device = device; + LOG_INFO("Captured D3D12 device from command queue: {:X}", (UINT64) device); + D3D12Hooks::HookDevice(State::Instance().currentD3D12Device); + device->Release(); + } + } + else + { + LOG_DEBUG("Failed to get D3D12 device from command queue: {:X}", (UINT) capRes); + } + } + if (!Util::CheckForRealObject(__FUNCTION__, cq, &real)) real = cq; diff --git a/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp b/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp index b78c82de..c74caefe 100644 --- a/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp +++ b/OptiScaler/inputs/NVNGX_DLSS_Dx12.cpp @@ -14,6 +14,8 @@ #include +#include + #include #include #include "detours/detours.h" @@ -184,8 +186,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Init_Ext(unsigned long long InApp } D3D12Device = InDevice; - State::Instance().currentD3D12Device = InDevice; + D3D12Hooks::HookDevice(InDevice); if (!State::Instance().isWorkingAsNvngx) { diff --git a/OptiScaler/wrapped/wrapped_swapchain.cpp b/OptiScaler/wrapped/wrapped_swapchain.cpp index b282bcfd..46391000 100644 --- a/OptiScaler/wrapped/wrapped_swapchain.cpp +++ b/OptiScaler/wrapped/wrapped_swapchain.cpp @@ -5,6 +5,7 @@ #include #include +#include #include @@ -151,7 +152,9 @@ static HRESULT LocalPresent(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT LOG_DEBUG("D3D12Device captured"); _dx12Device = true; + State::Instance().currentD3D12Device = device12; + D3D12Hooks::HookDevice(device12); } }