Skip to content

Commit

Permalink
added missing d3d11 hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cdozdil committed Sep 18, 2024
1 parent 83c82bf commit 1130130
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 25 deletions.
5 changes: 2 additions & 3 deletions OptiScaler/NVNGX_DLSS_Dx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,15 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_Shutdown()

DLSSFeatureDx11::Shutdown(D3D11Device);

//if (Config::Instance()->OverlayMenu.value_or(true) && ImGuiOverlayDx11::IsInitedDx11())
// ImGuiOverlayDx11::ShutdownDx11();

if (Config::Instance()->DLSSEnabled.value_or(true) && NVNGXProxy::IsDx11Inited() && NVNGXProxy::D3D11_Shutdown() != nullptr)
{
auto result = NVNGXProxy::D3D11_Shutdown()();
NVNGXProxy::SetDx11Inited(false);
LOG_INFO("D3D11_Shutdown result: {0:X}", (UINT)result);
}

ImGuiOverlayDx::UnHookDx();

return NVSDK_NGX_Result_Success;
}

Expand Down
2 changes: 2 additions & 0 deletions OptiScaler/NVNGX_DLSS_Dx12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_Shutdown(void)
LOG_INFO("D3D12_Shutdown result: {0:X}", (UINT)result);
}

ImGuiOverlayDx::UnHookDx();

return NVSDK_NGX_Result_Success;
}

Expand Down
2 changes: 2 additions & 0 deletions OptiScaler/NVNGX_DLSS_Vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_VULKAN_Shutdown(void)
LOG_INFO("VULKAN_Shutdown result: {0:X}", (UINT)result);
}

ImGuiOverlayVk::UnHookVk();

return NVSDK_NGX_Result_Success;
}

Expand Down
2 changes: 1 addition & 1 deletion OptiScaler/OptiScaler.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<IncludePath>$(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)</IncludePath>
<LibraryPath>$(ProjectDir)fsr2\lib;$(ProjectDir)fsr2_212\lib;$(ProjectDir)vulkan;$(ProjectDir)d3dx;$(ProjectDir)detours;$(SolutionDir)external\xess\lib;$(LibraryPath)</LibraryPath>
<TargetName>dxgi</TargetName>
<OutDir>D:\Folders\Games\God of War\</OutDir>
<OutDir>D:\Folders\Games\Cyberpunk 2077\bin\x64\</OutDir>
<IntDir>.\x64\Debug</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
5 changes: 4 additions & 1 deletion OptiScaler/dllmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,8 @@ void AttachToAdapter(IUnknown* unkAdapter)

PVOID* pVTable = *(PVOID**)unkAdapter;

bool dxvkStatus = Config::Instance()->IsRunningOnDXVK;

IDXGIAdapter* adapter = nullptr;
bool adapterOk = unkAdapter->QueryInterface(__uuidof(IDXGIAdapter), (void**)&adapter) == S_OK;

Expand All @@ -1718,7 +1720,8 @@ void AttachToAdapter(IUnknown* unkAdapter)
((IDXGIAdapter*)dxvkAdapter)->Release();
}

LOG_INFO("IDXGIVkInteropDevice interface {0}", Config::Instance()->IsRunningOnDXVK ? "found" : "not found");
if(Config::Instance()->IsRunningOnDXVK != dxvkStatus)
LOG_INFO("IDXGIVkInteropDevice interface {0}", Config::Instance()->IsRunningOnDXVK ? "found" : "not found");

if (ptrGetDesc == nullptr && adapterOk)
{
Expand Down
84 changes: 70 additions & 14 deletions OptiScaler/imgui/imgui_overlay_dx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static PFN_D3D12_CREATE_DEVICE o_D3D12CreateDevice = nullptr;
static PFN_CreateSampler o_CreateSampler = nullptr;

static PFN_D3D11_CREATE_DEVICE o_D3D11CreateDevice = nullptr;
static PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN o_D3D11CreateDeviceAndSwapChain = nullptr;
static PFN_CreateSamplerState o_CreateSamplerState = nullptr;
static PFN_D3D11ON12_CREATE_DEVICE o_D3D11On12CreateDevice = nullptr;
static ID3D11Device* d3d11Device = nullptr;
Expand Down Expand Up @@ -156,8 +157,6 @@ static void CleanupRenderTargetDx12(bool clearQueue)
if (!_isInited || !_dx12Device)
return;

LOG_DEBUG("clearQueue: {0}!", clearQueue);

for (UINT i = 0; i < NUM_BACK_BUFFERS; ++i)
{
if (g_mainRenderTargetResource[i])
Expand Down Expand Up @@ -206,15 +205,13 @@ static void CleanupRenderTargetDx12(bool clearQueue)

if (g_pd3dCommandQueue != nullptr)
{
auto count = g_pd3dCommandQueue->Release();
LOG_DEBUG("D3D12 Queue refcount: {}", count);
g_pd3dCommandQueue->Release();
g_pd3dCommandQueue = nullptr;
}

if (g_pd3dDeviceParam != nullptr)
{
auto count = g_pd3dDeviceParam->Release();
LOG_DEBUG("D3D12 device refcount: {}", count);
g_pd3dDeviceParam->Release();
g_pd3dDeviceParam = nullptr;
}

Expand Down Expand Up @@ -372,7 +369,7 @@ static HRESULT Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags
ImGuiOverlayDx::currentFrameIndex = (ImGuiOverlayDx::currentFrameIndex + 1) % ImGuiOverlayDx::QUERY_BUFFER_COUNT;
}

// DXVK & process hWnd check
// DXVK & process hWnd check
if (Config::Instance()->IsRunningOnDXVK || hWnd != Util::GetProcessWindow())
{
if (cq != nullptr)
Expand Down Expand Up @@ -430,11 +427,8 @@ static HRESULT Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags
g_pd3dCommandQueue = cq;
g_pd3dDeviceParam = device12;

auto count = g_pd3dCommandQueue->AddRef();
LOG_DEBUG("D3D12 Queue refcount: {}", count);

count = g_pd3dDeviceParam->AddRef();
LOG_DEBUG("D3D12 Device refcount: {}", count);
g_pd3dCommandQueue->AddRef();
g_pd3dDeviceParam->AddRef();

ImGuiOverlayBase::Dx12Ready();
_isInited = true;
Expand Down Expand Up @@ -972,6 +966,64 @@ static HRESULT hkD3D11CreateDevice(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE Drive
return result;
}

static HRESULT hkD3D11CreateDeviceAndSwapChain(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels, UINT SDKVersion, DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext)
{
LOG_FUNC();

IDXGISwapChain* buffer = nullptr;
auto result = o_D3D11CreateDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, pSwapChainDesc, &buffer, ppDevice, pFeatureLevel, ppImmediateContext);

if (result == S_OK && *ppDevice != nullptr)
{
LOG_INFO("Device captured");
d3d11Device = *ppDevice;

HookToDevice(d3d11Device);

WrappedIDXGISwapChain4* buf = nullptr;
if (buffer != nullptr && buffer->QueryInterface(IID_PPV_ARGS(&buf)) != S_OK)
{
// check for SL proxy
IID riid;
IDXGISwapChain* real = nullptr;
auto iidResult = IIDFromString(L"{ADEC44E2-61F0-45C3-AD9F-1B37379284FF}", &riid);

if (iidResult == S_OK)
{
auto qResult = buffer->QueryInterface(riid, (void**)&real);

if (qResult == S_OK && real != nullptr)
{
LOG_INFO("Streamline proxy found");
real->Release();
}
else
{
LOG_DEBUG("Streamline proxy not found");
}
}

Config::Instance()->ScreenWidth = pSwapChainDesc->BufferDesc.Width;
Config::Instance()->ScreenHeight = pSwapChainDesc->BufferDesc.Height;

LOG_DEBUG("created new swapchain: {0:X}, hWnd", (UINT64)buffer, (UINT64)pSwapChainDesc->OutputWindow);
*ppSwapChain = new WrappedIDXGISwapChain4(real == nullptr ? buffer : real, d3d11Device, pSwapChainDesc->OutputWindow, Present, CleanupRenderTarget);
LOG_DEBUG("created new WrappedIDXGISwapChain4: {0:X}, pDevice: {1:X}", (UINT64)buffer, (UINT64)d3d11Device);
}

if (buf != nullptr)
buf->Release();
}

if (buffer != nullptr)
buffer->Release();

LOG_FUNC_RESULT(result);

return result;
}

static HRESULT hkD3D12CreateDevice(IUnknown* pAdapter, D3D_FEATURE_LEVEL MinimumFeatureLevel, REFIID riid, void** ppDevice)
{
LOG_FUNC();
Expand Down Expand Up @@ -1380,7 +1432,7 @@ void DeatachAllHooks()
if (o_D3D11On12CreateDevice != nullptr)
{
DetourDetach(&(PVOID&)o_D3D11On12CreateDevice, hkD3D11On12CreateDevice);
o_D3D11CreateDevice = nullptr;
o_D3D11On12CreateDevice = nullptr;
}

if (o_D3D12CreateDevice != nullptr)
Expand Down Expand Up @@ -1439,8 +1491,9 @@ void ImGuiOverlayDx::HookDx()
}

o_D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)DetourFindFunction("d3d11.dll", "D3D11CreateDevice");
o_D3D11CreateDeviceAndSwapChain = (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)DetourFindFunction("d3d11.dll", "D3D11CreateDeviceAndSwapChain");
o_D3D11On12CreateDevice = (PFN_D3D11ON12_CREATE_DEVICE)DetourFindFunction("d3d11.dll", "D3D11On12CreateDevice");
if (o_D3D11CreateDevice != nullptr || o_D3D11On12CreateDevice != nullptr)
if (o_D3D11CreateDevice != nullptr || o_D3D11On12CreateDevice != nullptr || o_D3D11CreateDeviceAndSwapChain != nullptr)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
Expand All @@ -1451,6 +1504,9 @@ void ImGuiOverlayDx::HookDx()
if (o_D3D11On12CreateDevice != nullptr)
DetourAttach(&(PVOID&)o_D3D11On12CreateDevice, hkD3D11On12CreateDevice);

if (o_D3D11CreateDeviceAndSwapChain != nullptr)
DetourAttach(&(PVOID&)o_D3D11CreateDeviceAndSwapChain, hkD3D11CreateDeviceAndSwapChain);

DetourTransactionCommit();
}

Expand Down
29 changes: 25 additions & 4 deletions OptiScaler/imgui/wrapped_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ HRESULT STDMETHODCALLTYPE WrappedIDXGISwapChain4::QueryInterface(REFIID riid, vo
return E_NOINTERFACE;
}
}
else if (riid == __uuidof(this))
{
AddRef();
*ppvObject = (IUnknown*)this;
return S_OK;
}
else if (riid == __uuidof(IUnknown))
{
AddRef();
Expand All @@ -108,10 +114,18 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers(UINT BufferCount, UINT Width, UINT
{
LOG_FUNC();

_mutex.lock();

if (ClearTrig != nullptr)
ClearTrig(false, Handle);
ClearTrig(true, Handle);

auto result = m_pReal->ResizeBuffers(BufferCount, Width, Height, NewFormat, SwapChainFlags);

return m_pReal->ResizeBuffers(BufferCount, Width, Height, NewFormat, SwapChainFlags);
LOG_FUNC_RESULT(result);

_mutex.unlock();

return result;
}

HRESULT STDMETHODCALLTYPE WrappedIDXGISwapChain4::GetContainingOutput(IDXGIOutput** ppOutput)
Expand All @@ -124,10 +138,17 @@ HRESULT WrappedIDXGISwapChain4::ResizeBuffers1(UINT BufferCount, UINT Width, UIN
{
LOG_FUNC();

_mutex.lock();

if (ClearTrig != nullptr)
ClearTrig(false, Handle);
ClearTrig(true, Handle);


auto result = m_pReal3->ResizeBuffers1(BufferCount, Width, Height, Format, SwapChainFlags, pCreationNodeMask, ppPresentQueue);

_mutex.unlock();

return m_pReal3->ResizeBuffers1(BufferCount, Width, Height, Format, SwapChainFlags, pCreationNodeMask, ppPresentQueue);
return result;
}

HRESULT WrappedIDXGISwapChain4::SetFullscreenState(BOOL Fullscreen, IDXGIOutput* pTarget)
Expand Down
2 changes: 2 additions & 0 deletions OptiScaler/imgui/wrapped_swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,7 @@ struct DECLSPEC_UUID("3af622a3-82d0-49cd-994f-cce05122c222") WrappedIDXGISwapCha
PFN_SC_Clean ClearTrig = nullptr;
HWND Handle = nullptr;

std::mutex _mutex;

int id = 0;
};
3 changes: 2 additions & 1 deletion OptiScaler/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
// Enables logging of DLSS NV Parameters
//#define DLSS_PARAM_DUMP

inline HMODULE dllModule;
inline HMODULE dllModule = nullptr;
inline HMODULE skHandle = nullptr;
inline DWORD processId;

#define LOG_TRACE(msg, ...) \
Expand Down
2 changes: 1 addition & 1 deletion OptiScaler/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define VER_MAJOR_VERSION 0
#define VER_MINOR_VERSION 6
#define VER_HOTFIX_VERSION 7
#define VER_BUILD_NUMBER 8
#define VER_BUILD_NUMBER 9

#define VER_PRE_RELEASE

Expand Down

0 comments on commit 1130130

Please sign in to comment.