2019-07-25 21:21:03 +00:00
|
|
|
#include "DX12_Hook.h"
|
2019-08-18 14:22:07 +00:00
|
|
|
#include "Windows_Hook.h"
|
2019-07-25 21:21:03 +00:00
|
|
|
#include "Hook_Manager.h"
|
2019-08-18 14:22:07 +00:00
|
|
|
#include "../dll/dll.h"
|
|
|
|
|
|
|
|
#ifndef NO_OVERLAY
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <impls/imgui_impl_dx12.h>
|
|
|
|
|
2019-08-19 16:56:15 +00:00
|
|
|
#include <dxgi1_4.h>
|
|
|
|
|
2019-08-16 17:21:30 +00:00
|
|
|
DX12_Hook* DX12_Hook::_inst = nullptr;
|
|
|
|
|
2019-08-16 17:10:12 +00:00
|
|
|
bool DX12_Hook::start_hook()
|
2019-07-25 21:21:03 +00:00
|
|
|
{
|
2019-08-25 19:22:25 +00:00
|
|
|
bool res = true;
|
2019-07-25 21:21:03 +00:00
|
|
|
if (!_hooked)
|
|
|
|
{
|
2019-08-19 16:56:15 +00:00
|
|
|
if (!Windows_Hook::Inst().start_hook())
|
|
|
|
return false;
|
|
|
|
|
2019-08-20 21:27:17 +00:00
|
|
|
HWND hWnd = GetGameWindow();
|
|
|
|
if (!hWnd)
|
|
|
|
return false;
|
|
|
|
|
2019-08-19 16:56:15 +00:00
|
|
|
IDXGIFactory4* pDXGIFactory = nullptr;
|
|
|
|
IDXGISwapChain1* pSwapChain = nullptr;
|
|
|
|
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
|
|
|
|
ID3D12CommandQueue* pCommandQueue = nullptr;
|
|
|
|
ID3D12Device* pDevice = nullptr;
|
|
|
|
decltype(D3D12CreateDevice)* D3D12CreateDevice =
|
|
|
|
(decltype(D3D12CreateDevice))GetProcAddress(GetModuleHandle(DX12_Hook::DLL_NAME), "D3D12CreateDevice");
|
|
|
|
|
|
|
|
D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&pDevice));
|
|
|
|
|
|
|
|
if (pDevice)
|
|
|
|
{
|
|
|
|
DXGI_SWAP_CHAIN_DESC1 SwapChainDesc = {};
|
|
|
|
SwapChainDesc.Width = 0;
|
|
|
|
SwapChainDesc.Height = 0;
|
|
|
|
SwapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
SwapChainDesc.Stereo = FALSE;
|
|
|
|
SwapChainDesc.SampleDesc = { 1, 0 };
|
|
|
|
SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
|
|
|
SwapChainDesc.BufferCount = 3;
|
|
|
|
SwapChainDesc.Scaling = DXGI_SCALING_STRETCH;
|
|
|
|
SwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
|
|
|
SwapChainDesc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
|
|
|
|
|
|
|
|
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
|
|
|
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
|
|
|
|
pDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&pCommandQueue));
|
|
|
|
|
|
|
|
if (pCommandQueue)
|
|
|
|
{
|
|
|
|
reinterpret_cast<decltype(CreateDXGIFactory1)*>(GetProcAddress(GetModuleHandle("dxgi.dll"), "CreateDXGIFactory1"))(IID_PPV_ARGS(&pDXGIFactory));
|
2019-08-20 21:27:17 +00:00
|
|
|
pDXGIFactory->CreateSwapChainForHwnd(pCommandQueue, hWnd, &SwapChainDesc, NULL, NULL, &pSwapChain);
|
2019-08-19 16:56:15 +00:00
|
|
|
|
|
|
|
if (pSwapChain != nullptr)
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("Hooked DirectX 12\n");
|
|
|
|
|
|
|
|
_hooked = true;
|
|
|
|
Hook_Manager::Inst().FoundRenderer(this);
|
|
|
|
|
|
|
|
loadFunctions(pDevice, pSwapChain);
|
|
|
|
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
EndHook();
|
|
|
|
|
|
|
|
get_steam_client()->steam_overlay->HookReady();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("Failed to hook DirectX 12\n");
|
2019-08-25 19:22:25 +00:00
|
|
|
res = false;
|
2019-08-19 16:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-18 15:12:57 +00:00
|
|
|
|
2019-08-19 16:56:15 +00:00
|
|
|
if (pSwapChain) pSwapChain->Release();
|
|
|
|
if (pDXGIFactory) pDXGIFactory->Release();
|
|
|
|
if (pCommandQueue) pCommandQueue->Release();
|
|
|
|
if (pDevice) pDevice->Release();
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
2019-08-25 19:22:25 +00:00
|
|
|
return res;
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX12_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
pCmdAlloc->Release();
|
|
|
|
pCmdList->Release();
|
|
|
|
pDescriptorHeap->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX12_Shutdown();
|
2019-08-18 14:22:07 +00:00
|
|
|
Windows_Hook::Inst().resetRenderState();
|
2019-07-31 20:20:27 +00:00
|
|
|
ImGui::DestroyContext();
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:55:31 +00:00
|
|
|
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
2019-07-25 21:21:03 +00:00
|
|
|
void DX12_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
|
|
|
|
{
|
|
|
|
DXGI_SWAP_CHAIN_DESC desc;
|
|
|
|
pSwapChain->GetDesc(&desc);
|
2019-07-31 20:20:27 +00:00
|
|
|
|
2019-07-25 21:21:03 +00:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC d3d12_desc = {};
|
|
|
|
ID3D12Device* pDevice;
|
|
|
|
|
2019-08-19 16:56:15 +00:00
|
|
|
if (!SUCCEEDED(pSwapChain->GetDevice(IID_PPV_ARGS(&pDevice))))
|
|
|
|
return;
|
|
|
|
|
2019-07-25 21:21:03 +00:00
|
|
|
d3d12_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
|
|
|
|
d3d12_desc.NumDescriptors = 1;
|
|
|
|
d3d12_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
|
|
|
|
d3d12_desc.NodeMask = 1;
|
|
|
|
if (pDevice->CreateDescriptorHeap(&d3d12_desc, IID_PPV_ARGS(&pDescriptorHeap)) != S_OK)
|
|
|
|
{
|
|
|
|
pDevice->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:56:15 +00:00
|
|
|
mainRenderTargetDescriptor = pDescriptorHeap->GetCPUDescriptorHandleForHeapStart();
|
2019-07-25 21:21:03 +00:00
|
|
|
|
2019-08-19 16:56:15 +00:00
|
|
|
if (!SUCCEEDED(pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&pCmdAlloc))))
|
|
|
|
{
|
|
|
|
pDescriptorHeap->Release();
|
|
|
|
pDevice->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!SUCCEEDED(pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, pCmdAlloc, NULL, IID_PPV_ARGS(&pCmdList))))
|
|
|
|
{
|
2019-08-19 17:51:35 +00:00
|
|
|
pCmdAlloc->Release();
|
|
|
|
pDescriptorHeap->Release();
|
|
|
|
pDevice->Release();
|
2019-08-19 16:56:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.IniFilename = NULL;
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
ImGui_ImplDX12_Init(pDevice, 1, DXGI_FORMAT_R8G8B8A8_UNORM,
|
|
|
|
pDescriptorHeap->GetCPUDescriptorHandleForHeapStart(),
|
|
|
|
pDescriptorHeap->GetGPUDescriptorHandleForHeapStart());
|
2019-08-19 16:56:15 +00:00
|
|
|
|
2019-08-19 17:51:35 +00:00
|
|
|
pCmdList->Release();
|
2019-08-19 16:56:15 +00:00
|
|
|
pCmdAlloc->Release();
|
|
|
|
pDescriptorHeap->Release();
|
|
|
|
pDevice->Release();
|
2019-07-25 21:21:03 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplDX12_NewFrame();
|
2019-08-19 16:56:15 +00:00
|
|
|
Windows_Hook::Inst().prepareForOverlay(desc.OutputWindow);
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
2019-08-19 16:56:15 +00:00
|
|
|
get_steam_client()->steam_overlay->OverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height);
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
ImGui::EndFrame();
|
|
|
|
|
|
|
|
ImGui::Render();
|
|
|
|
|
|
|
|
pCmdAlloc->Reset();
|
|
|
|
pCmdList->Reset(pCmdAlloc, NULL);
|
|
|
|
//pCmdList->ClearRenderTargetView(mainRenderTargetDescriptor, (float*)& clear_color, 0, NULL);
|
|
|
|
pCmdList->OMSetRenderTargets(1, &mainRenderTargetDescriptor, FALSE, NULL);
|
|
|
|
pCmdList->SetDescriptorHeaps(1, &pDescriptorHeap);
|
|
|
|
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), pCmdList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DirectX 12 Initialization functions
|
2019-08-16 08:36:44 +00: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 21:21:03 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyPresent(IDXGISwapChain *_this, UINT SyncInterval, UINT Flags)
|
|
|
|
{
|
2019-08-16 17:21:30 +00:00
|
|
|
DX12_Hook::Inst()->prepareForOverlay(_this);
|
2019-07-25 21:21:03 +00:00
|
|
|
|
2019-08-16 17:21:30 +00:00
|
|
|
return (_this->*DX12_Hook::Inst()->Present)(SyncInterval, Flags);
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyResizeTarget(IDXGISwapChain* _this, const DXGI_MODE_DESC* pNewTargetParameters)
|
|
|
|
{
|
2019-08-16 17:21:30 +00:00
|
|
|
DX12_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX12_Hook::Inst()->ResizeTarget)(pNewTargetParameters);
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyResizeBuffers(IDXGISwapChain* _this, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
|
|
|
{
|
2019-08-16 17:21:30 +00:00
|
|
|
DX12_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX12_Hook::Inst()->ResizeBuffers)(BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DX12_Hook::DX12_Hook():
|
|
|
|
initialized(false),
|
|
|
|
pCmdAlloc(nullptr),
|
|
|
|
pCmdList(nullptr),
|
|
|
|
pDescriptorHeap(nullptr)
|
|
|
|
{
|
2019-08-19 16:56:15 +00:00
|
|
|
_library = LoadLibrary(DLL_NAME);
|
2019-07-25 21:21:03 +00:00
|
|
|
|
2019-08-14 12:55:31 +00:00
|
|
|
PRINT_DEBUG("Trying to hook DX12 but DX12_Hook is not implemented yet, please report to DEV with the game name.");
|
|
|
|
|
|
|
|
// 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 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DX12_Hook::~DX12_Hook()
|
|
|
|
{
|
2019-08-14 12:55:31 +00:00
|
|
|
PRINT_DEBUG("DX12 Hook removed\n");
|
2019-07-25 21:21:03 +00:00
|
|
|
|
2019-08-21 19:52:08 +00:00
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
pCmdAlloc->Release();
|
|
|
|
pCmdList->Release();
|
|
|
|
pDescriptorHeap->Release();
|
|
|
|
|
|
|
|
//ImGui_ImplDX12_Shutdown();
|
|
|
|
ImGui_ImplDX12_InvalidateDeviceObjects();
|
|
|
|
Windows_Hook::Inst().resetRenderState();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
}
|
2019-08-18 14:19:28 +00:00
|
|
|
|
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
2019-07-25 21:21:03 +00:00
|
|
|
|
2019-08-16 17:21:30 +00:00
|
|
|
_inst = nullptr;
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 17:21:30 +00:00
|
|
|
DX12_Hook* DX12_Hook::Inst()
|
2019-07-25 21:21:03 +00:00
|
|
|
{
|
2019-08-16 17:21:30 +00:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new DX12_Hook();
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX12_Hook::loadFunctions(ID3D12Device *pDevice, IDXGISwapChain *pSwapChain)
|
|
|
|
{
|
|
|
|
void** vTable = *reinterpret_cast<void***>(pDevice);
|
|
|
|
|
|
|
|
/*
|
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)ID3D12DeviceVTable::X]
|
|
|
|
|
|
|
|
#undef LOAD_FUNC
|
|
|
|
*/
|
|
|
|
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-14 12:55:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:09:57 +00:00
|
|
|
#endif//NO_OVERLAY
|