2019-07-25 23:21:03 +02:00
|
|
|
#include "DX12_Hook.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
#include "Windows_Hook.h"
|
2019-07-25 23:21:03 +02:00
|
|
|
#include "Hook_Manager.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
#include "../dll/dll.h"
|
|
|
|
|
|
|
|
#ifndef NO_OVERLAY
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <impls/imgui_impl_dx12.h>
|
|
|
|
|
2019-08-19 18:56:15 +02:00
|
|
|
#include <dxgi1_4.h>
|
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX12_Hook* DX12_Hook::_inst = nullptr;
|
|
|
|
|
2019-08-16 19:10:12 +02:00
|
|
|
bool DX12_Hook::start_hook()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-25 21:22:25 +02:00
|
|
|
bool res = true;
|
2019-07-25 23:21:03 +02:00
|
|
|
if (!_hooked)
|
|
|
|
{
|
2019-08-19 18:56:15 +02:00
|
|
|
if (!Windows_Hook::Inst().start_hook())
|
|
|
|
return false;
|
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
PRINT_DEBUG("Hooked DirectX 12\n");
|
|
|
|
_hooked = true;
|
|
|
|
|
|
|
|
Hook_Manager::Inst().FoundRenderer(this);
|
|
|
|
|
|
|
|
UnhookAll();
|
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX12_Hook::Present, &DX12_Hook::MyPresent),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX12_Hook::ResizeTarget, &DX12_Hook::MyResizeTarget),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX12_Hook::ResizeBuffers, &DX12_Hook::MyResizeBuffers),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX12_Hook::ExecuteCommandLists, &DX12_Hook::MyExecuteCommandLists),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX12_Hook::Close, &DX12_Hook::MyClose)
|
|
|
|
);
|
|
|
|
EndHook();
|
|
|
|
|
|
|
|
get_steam_client()->steam_overlay->HookReady();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
2019-08-25 21:22:25 +02:00
|
|
|
return res;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX12_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
2019-08-26 16:46:56 +02:00
|
|
|
pSrvDescHeap->Release();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
ImGui_ImplDX12_Shutdown();
|
2019-08-18 16:22:07 +02:00
|
|
|
Windows_Hook::Inst().resetRenderState();
|
2019-07-31 22:20:27 +02:00
|
|
|
ImGui::DestroyContext();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 14:55:31 +02:00
|
|
|
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
2019-07-25 23:21:03 +02:00
|
|
|
void DX12_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
|
|
|
|
{
|
2019-08-26 16:46:56 +02:00
|
|
|
pSwapChain->GetDesc(&sc_desc);
|
|
|
|
|
|
|
|
IDXGISwapChain3* pSwapChain3;
|
|
|
|
pSwapChain->QueryInterface(IID_PPV_ARGS(&pSwapChain3));
|
2019-07-31 22:20:27 +02:00
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
ID3D12Device* pDevice;
|
2019-08-26 16:46:56 +02:00
|
|
|
if (pSwapChain->GetDevice(IID_PPV_ARGS(&pDevice)) != S_OK)
|
2019-07-25 23:21:03 +02:00
|
|
|
return;
|
|
|
|
|
2019-08-19 18:56:15 +02:00
|
|
|
{
|
2019-08-26 16:46:56 +02:00
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC desc = {};
|
|
|
|
desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
|
|
|
|
desc.NumDescriptors = 1;
|
|
|
|
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
|
|
|
|
if (pDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&pSrvDescHeap)) != S_OK)
|
|
|
|
{
|
|
|
|
pDevice->Release();
|
|
|
|
return;
|
|
|
|
}
|
2019-08-19 18:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.IniFilename = NULL;
|
2019-08-26 16:46:56 +02:00
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
ImGui_ImplDX12_Init(pDevice, 1, DXGI_FORMAT_R8G8B8A8_UNORM,
|
2019-08-26 16:46:56 +02:00
|
|
|
pSrvDescHeap->GetCPUDescriptorHandleForHeapStart(),
|
|
|
|
pSrvDescHeap->GetGPUDescriptorHandleForHeapStart());
|
2019-08-19 18:56:15 +02:00
|
|
|
|
|
|
|
pDevice->Release();
|
2019-08-26 16:46:56 +02:00
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DirectX 12 Initialization functions
|
2019-08-16 10:36:44 +02:00
|
|
|
//HRESULT WINAPI DX12_Hook::MyD3D12CreateDevice(
|
|
|
|
// _In_opt_ IUnknown* pAdapter,
|
|
|
|
// D3D_FEATURE_LEVEL MinimumFeatureLevel,
|
|
|
|
// _In_ REFIID riid, // Expected: ID3D12Device
|
|
|
|
// _COM_Outptr_opt_ void** ppDevice)
|
|
|
|
//{
|
|
|
|
// auto res = hook->D3D12CreateDevice(pAdapter, MinimumFeatureLevel, riid, ppDevice);
|
|
|
|
//
|
|
|
|
// if (SUCCEEDED(res))
|
|
|
|
// hook->hook_dx12(MinimumFeatureLevel);
|
|
|
|
//
|
|
|
|
// return res;
|
|
|
|
//}
|
2019-07-25 23:21:03 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyPresent(IDXGISwapChain *_this, UINT SyncInterval, UINT Flags)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX12_Hook::Inst()->prepareForOverlay(_this);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
return (_this->*DX12_Hook::Inst()->Present)(SyncInterval, Flags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyResizeTarget(IDXGISwapChain* _this, const DXGI_MODE_DESC* pNewTargetParameters)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX12_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX12_Hook::Inst()->ResizeTarget)(pNewTargetParameters);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyResizeBuffers(IDXGISwapChain* _this, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX12_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX12_Hook::Inst()->ResizeBuffers)(BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
void STDMETHODCALLTYPE DX12_Hook::MyExecuteCommandLists(ID3D12CommandQueue *_this, UINT NumCommandLists, ID3D12CommandList* const* ppCommandLists)
|
|
|
|
{
|
|
|
|
DX12_Hook* me = DX12_Hook::Inst();
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
// \/\/\/ TODO: Find a cleaner way to do all this \/\/\/ //
|
|
|
|
// I'd like to put it in IDXGISwapChain::Present //
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
if (me->initialized)
|
|
|
|
{
|
|
|
|
static std::recursive_mutex render_mutex; // Sniper Elite 4 where I test this uses multiple thread on this. ImGui is not reentrant
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(render_mutex);
|
|
|
|
for (int i = 0; i < NumCommandLists; ++i)
|
|
|
|
{
|
|
|
|
if (((ID3D12GraphicsCommandList*)ppCommandLists[i])->GetType() == D3D12_COMMAND_LIST_TYPE_DIRECT)
|
|
|
|
{
|
|
|
|
ImGui_ImplDX12_NewFrame();
|
|
|
|
Windows_Hook::Inst().prepareForOverlay(me->sc_desc.OutputWindow);
|
|
|
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
|
|
get_steam_client()->steam_overlay->OverlayProc(me->sc_desc.BufferDesc.Width, me->sc_desc.BufferDesc.Height);
|
|
|
|
|
|
|
|
ImGui::EndFrame();
|
|
|
|
|
|
|
|
((ID3D12GraphicsCommandList*)ppCommandLists[i])->SetDescriptorHeaps(1, &me->pSrvDescHeap);
|
|
|
|
ImGui::Render();
|
|
|
|
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), (ID3D12GraphicsCommandList*)ppCommandLists[i]);
|
|
|
|
(((ID3D12GraphicsCommandList*)ppCommandLists[i])->*me->Close)();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(_this->*DX12_Hook::Inst()->ExecuteCommandLists)(NumCommandLists, ppCommandLists);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyClose(ID3D12GraphicsCommandList* _this)
|
|
|
|
{
|
|
|
|
if (DX12_Hook::Inst()->initialized && _this->GetType() == D3D12_COMMAND_LIST_TYPE_DIRECT)
|
|
|
|
return S_OK;
|
|
|
|
else
|
|
|
|
return (_this->*DX12_Hook::Inst()->Close)();
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
DX12_Hook::DX12_Hook():
|
2019-08-26 16:46:56 +02:00
|
|
|
initialized(false)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-19 18:56:15 +02:00
|
|
|
_library = LoadLibrary(DLL_NAME);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
PRINT_DEBUG("Trying to hook DX12 but DX12_Hook is not implemented yet, please report to DEV with the game name.\n");
|
2019-08-14 14:55:31 +02:00
|
|
|
|
|
|
|
// Hook to D3D12CreateDevice and D3D12CreateDeviceAndSwapChain so we know when it gets called.
|
|
|
|
// If its called, then DX12 will be used to render the overlay.
|
|
|
|
//D3D12CreateDevice = (decltype(D3D12CreateDevice))GetProcAddress(_dll, "D3D12CreateDevice");
|
|
|
|
//
|
|
|
|
//BeginHook();
|
|
|
|
//HookFuncs(
|
|
|
|
// std::make_pair<void**, void*>(&(PVOID&)D3D12CreateDevice, &DX12_Hook::MyD3D12CreateDevice)
|
|
|
|
//);
|
|
|
|
//EndHook();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX12_Hook::~DX12_Hook()
|
|
|
|
{
|
2019-08-14 14:55:31 +02:00
|
|
|
PRINT_DEBUG("DX12 Hook removed\n");
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-21 21:52:08 +02:00
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
//ImGui_ImplDX12_Shutdown();
|
|
|
|
ImGui_ImplDX12_InvalidateDeviceObjects();
|
|
|
|
Windows_Hook::Inst().resetRenderState();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
}
|
2019-08-18 16:19:28 +02:00
|
|
|
|
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
_inst = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX12_Hook* DX12_Hook::Inst()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new DX12_Hook();
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
const char* DX12_Hook::get_lib_name() const
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-26 16:46:56 +02:00
|
|
|
return DLL_NAME;
|
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
void DX12_Hook::loadFunctions(ID3D12Device* pDevice, ID3D12CommandQueue* pCommandQueue, IDXGISwapChain *pSwapChain)
|
|
|
|
{
|
|
|
|
void** vTable = *reinterpret_cast<void***>(pDevice);
|
2019-07-25 23:21:03 +02:00
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)ID3D12DeviceVTable::X]
|
2019-08-26 16:46:56 +02:00
|
|
|
|
|
|
|
#undef LOAD_FUNC
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
vTable = *reinterpret_cast<void***>(pCommandQueue);
|
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)ID3D12CommandQueueVTable::X]
|
|
|
|
LOAD_FUNC(ExecuteCommandLists);
|
2019-07-25 23:21:03 +02:00
|
|
|
#undef LOAD_FUNC
|
2019-08-26 16:46:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
ID3D12CommandAllocator* pCommandAllocator;
|
|
|
|
ID3D12CommandList* pCommandList;
|
|
|
|
|
|
|
|
pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&pCommandAllocator));
|
|
|
|
pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, pCommandAllocator, NULL, IID_PPV_ARGS(&pCommandList));
|
|
|
|
|
|
|
|
vTable = *reinterpret_cast<void***>(pCommandList);
|
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)ID3D12GraphicsCommandListVTable::X]
|
|
|
|
LOAD_FUNC(Close);
|
|
|
|
#undef LOAD_FUNC
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
vTable = *reinterpret_cast<void***>(pSwapChain);
|
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)IDXGISwapChainVTable::X]
|
|
|
|
LOAD_FUNC(Present);
|
|
|
|
LOAD_FUNC(ResizeBuffers);
|
|
|
|
LOAD_FUNC(ResizeTarget);
|
|
|
|
#undef LOAD_FUNC
|
2019-08-26 16:46:56 +02:00
|
|
|
|
|
|
|
pCommandList->Release();
|
|
|
|
pCommandAllocator->Release();
|
2019-08-14 14:55:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 15:09:57 +02:00
|
|
|
#endif//NO_OVERLAY
|