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-09-01 20:53:16 +02:00
|
|
|
#include "../Renderer_Detector.h"
|
|
|
|
#include "../../dll/dll.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
|
2020-01-19 18:55:14 +01:00
|
|
|
#ifdef EMU_OVERLAY
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
2019-09-01 20:53:16 +02:00
|
|
|
#include <impls/windows/imgui_impl_dx12.h>
|
2019-07-25 23:21:03 +02:00
|
|
|
|
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-08-27 15:38:07 +02:00
|
|
|
if (!hooked)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-27 15:38:07 +02:00
|
|
|
if (!Windows_Hook::Inst()->start_hook())
|
2019-08-19 18:56:15 +02:00
|
|
|
return false;
|
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
PRINT_DEBUG("Hooked DirectX 12\n");
|
2019-08-27 15:38:07 +02:00
|
|
|
hooked = true;
|
2019-08-26 16:46:56 +02:00
|
|
|
|
2019-08-27 15:38:07 +02:00
|
|
|
Renderer_Detector::Inst().renderer_found(this);
|
2019-08-26 16:46:56 +02:00
|
|
|
|
|
|
|
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),
|
2019-10-15 08:41:07 +02:00
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX12_Hook::ExecuteCommandLists, &DX12_Hook::MyExecuteCommandLists)
|
2019-08-26 16:46:56 +02:00
|
|
|
);
|
|
|
|
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-10-15 08:41:07 +02:00
|
|
|
for (UINT i = 0; i < bufferCount; ++i)
|
2019-10-17 18:03:19 +02:00
|
|
|
{
|
2019-10-15 08:41:07 +02:00
|
|
|
pCmdAlloc[i]->Release();
|
2019-10-17 18:03:19 +02:00
|
|
|
pBackBuffer[i]->Release();
|
|
|
|
}
|
2019-10-15 08:41:07 +02:00
|
|
|
pRtvDescHeap->Release();
|
|
|
|
delete[]pCmdAlloc;
|
2019-10-17 18:03:19 +02:00
|
|
|
delete[]pBackBuffer;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
ImGui_ImplDX12_Shutdown();
|
2019-08-27 15:38: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-10-15 08:41:07 +02:00
|
|
|
if (pCmdQueue == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ID3D12CommandQueue* pCmdQueue = this->pCmdQueue;
|
|
|
|
|
|
|
|
IDXGISwapChain3* pSwapChain3 = nullptr;
|
|
|
|
DXGI_SWAP_CHAIN_DESC sc_desc;
|
|
|
|
pSwapChain->QueryInterface(IID_PPV_ARGS(&pSwapChain3));
|
|
|
|
if (pSwapChain3 == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pSwapChain3->GetDesc(&sc_desc);
|
2019-07-31 22:20:27 +02:00
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
if (!initialized)
|
|
|
|
{
|
2019-10-15 08:41:07 +02:00
|
|
|
UINT bufferIndex = pSwapChain3->GetCurrentBackBufferIndex();
|
2019-07-25 23:21:03 +02:00
|
|
|
ID3D12Device* pDevice;
|
2019-10-15 08:41:07 +02:00
|
|
|
if (pSwapChain3->GetDevice(IID_PPV_ARGS(&pDevice)) != S_OK)
|
2019-07-25 23:21:03 +02:00
|
|
|
return;
|
|
|
|
|
2019-10-15 08:41:07 +02:00
|
|
|
bufferCount = sc_desc.BufferCount;
|
|
|
|
|
2019-10-17 18:03:19 +02:00
|
|
|
mainRenderTargets.clear();
|
|
|
|
|
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();
|
2019-10-15 08:41:07 +02:00
|
|
|
pSwapChain3->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC desc = {};
|
|
|
|
desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
|
|
|
|
desc.NumDescriptors = bufferCount;
|
|
|
|
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
|
|
|
|
desc.NodeMask = 1;
|
|
|
|
if (pDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&pRtvDescHeap)) != S_OK)
|
|
|
|
{
|
|
|
|
pDevice->Release();
|
|
|
|
pSwapChain3->Release();
|
|
|
|
pSrvDescHeap->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIZE_T rtvDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
|
|
|
|
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = pRtvDescHeap->GetCPUDescriptorHandleForHeapStart();
|
|
|
|
pCmdAlloc = new ID3D12CommandAllocator * [bufferCount];
|
|
|
|
for (int i = 0; i < bufferCount; ++i)
|
|
|
|
{
|
2019-10-17 18:03:19 +02:00
|
|
|
mainRenderTargets.push_back(rtvHandle);
|
2019-10-15 08:41:07 +02:00
|
|
|
rtvHandle.ptr += rtvDescriptorSize;
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 18:03:19 +02:00
|
|
|
|
2019-10-15 08:41:07 +02:00
|
|
|
for (UINT i = 0; i < sc_desc.BufferCount; ++i)
|
|
|
|
{
|
|
|
|
if (pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&pCmdAlloc[i])) != S_OK)
|
|
|
|
{
|
|
|
|
pDevice->Release();
|
|
|
|
pSwapChain3->Release();
|
|
|
|
pSrvDescHeap->Release();
|
|
|
|
for (UINT j = 0; j < i; ++j)
|
|
|
|
{
|
|
|
|
pCmdAlloc[j]->Release();
|
|
|
|
}
|
|
|
|
pRtvDescHeap->Release();
|
|
|
|
delete[]pCmdAlloc;
|
2019-08-26 16:46:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-08-19 18:56:15 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 08:41:07 +02:00
|
|
|
if (pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, pCmdAlloc[0], NULL, IID_PPV_ARGS(&pCmdList)) != S_OK ||
|
|
|
|
pCmdList->Close() != S_OK)
|
|
|
|
{
|
|
|
|
pDevice->Release();
|
|
|
|
pSwapChain3->Release();
|
|
|
|
pSrvDescHeap->Release();
|
|
|
|
for (UINT i = 0; i < bufferCount; ++i)
|
|
|
|
pCmdAlloc[i]->Release();
|
|
|
|
pRtvDescHeap->Release();
|
|
|
|
delete[]pCmdAlloc;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-17 18:03:19 +02:00
|
|
|
pBackBuffer = new ID3D12Resource * [bufferCount];
|
2019-10-15 08:41:07 +02:00
|
|
|
for (UINT i = 0; i < bufferCount; i++)
|
|
|
|
{
|
2019-10-17 18:03:19 +02:00
|
|
|
pSwapChain3->GetBuffer(i, IID_PPV_ARGS(&pBackBuffer[i]));
|
|
|
|
pDevice->CreateRenderTargetView(pBackBuffer[i], NULL, mainRenderTargets[i]);
|
2019-10-15 08:41:07 +02:00
|
|
|
}
|
|
|
|
|
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-10-15 08:41:07 +02:00
|
|
|
ImGui_ImplDX12_Init(pDevice, bufferCount, DXGI_FORMAT_R8G8B8A8_UNORM,
|
2019-08-26 16:46:56 +02:00
|
|
|
pSrvDescHeap->GetCPUDescriptorHandleForHeapStart(),
|
|
|
|
pSrvDescHeap->GetGPUDescriptorHandleForHeapStart());
|
2019-10-15 08:41:07 +02:00
|
|
|
|
2019-09-05 09:00:02 +02:00
|
|
|
get_steam_client()->steam_overlay->CreateFonts();
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
initialized = true;
|
2019-10-15 08:41:07 +02:00
|
|
|
|
|
|
|
pDevice->Release();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
2019-10-15 08:41:07 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
if (ImGui_ImplDX12_NewFrame())
|
|
|
|
{
|
|
|
|
Windows_Hook::Inst()->prepareForOverlay(sc_desc.OutputWindow);
|
2019-10-15 08:41:07 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui::NewFrame();
|
2019-10-17 18:03:19 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
get_steam_client()->steam_overlay->OverlayProc();
|
2019-10-17 18:03:19 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
UINT bufferIndex = pSwapChain3->GetCurrentBackBufferIndex();
|
2019-10-15 08:41:07 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
D3D12_RESOURCE_BARRIER barrier = {};
|
|
|
|
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
|
|
|
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
|
|
|
|
barrier.Transition.pResource = pBackBuffer[bufferIndex];
|
|
|
|
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
|
|
|
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
|
|
|
|
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
|
|
|
|
|
|
|
|
pCmdAlloc[bufferIndex]->Reset();
|
|
|
|
pCmdList->Reset(pCmdAlloc[bufferIndex], NULL);
|
|
|
|
pCmdList->ResourceBarrier(1, &barrier);
|
|
|
|
pCmdList->OMSetRenderTargets(1, &mainRenderTargets[bufferIndex], FALSE, NULL);
|
|
|
|
pCmdList->SetDescriptorHeaps(1, &pSrvDescHeap);
|
|
|
|
|
|
|
|
ImGui::Render();
|
|
|
|
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), pCmdList);
|
|
|
|
|
|
|
|
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
|
|
|
|
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
|
|
|
|
pCmdList->ResourceBarrier(1, &barrier);
|
|
|
|
pCmdList->Close();
|
|
|
|
|
|
|
|
pCmdQueue->ExecuteCommandLists(1, (ID3D12CommandList**)&pCmdList);
|
|
|
|
}
|
2019-10-15 08:41:07 +02:00
|
|
|
pSwapChain3->Release();
|
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();
|
2019-10-15 08:41:07 +02:00
|
|
|
me->pCmdQueue = _this;
|
2019-10-17 18:03:19 +02:00
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
(_this->*DX12_Hook::Inst()->ExecuteCommandLists)(NumCommandLists, ppCommandLists);
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
DX12_Hook::DX12_Hook():
|
2019-08-27 15:38:07 +02:00
|
|
|
initialized(false),
|
2019-10-15 08:41:07 +02:00
|
|
|
pCmdQueue(nullptr),
|
|
|
|
bufferCount(0),
|
|
|
|
pCmdAlloc(nullptr),
|
2019-08-27 15:38:07 +02:00
|
|
|
pSrvDescHeap(nullptr),
|
2019-10-15 08:41:07 +02:00
|
|
|
pCmdList(nullptr),
|
|
|
|
pRtvDescHeap(nullptr),
|
|
|
|
hooked(false),
|
2019-08-27 15:38:07 +02:00
|
|
|
Present(nullptr),
|
|
|
|
ResizeBuffers(nullptr),
|
|
|
|
ResizeTarget(nullptr),
|
2019-10-15 08:41:07 +02:00
|
|
|
ExecuteCommandLists(nullptr)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2020-07-16 11:56:24 +02:00
|
|
|
_library = LoadLibrary(DX12_DLL);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-26 19:36:07 +02:00
|
|
|
PRINT_DEBUG("DX12 support is experimental, don't complain if it doesn't work as expected.\n");
|
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)
|
|
|
|
{
|
2019-10-15 15:01:25 +02:00
|
|
|
pSrvDescHeap->Release();
|
|
|
|
for (UINT i = 0; i < bufferCount; ++i)
|
2019-10-17 18:03:19 +02:00
|
|
|
{
|
2019-10-15 15:01:25 +02:00
|
|
|
pCmdAlloc[i]->Release();
|
2019-10-17 18:03:19 +02:00
|
|
|
pBackBuffer[i]->Release();
|
|
|
|
}
|
2019-10-15 15:01:25 +02:00
|
|
|
pRtvDescHeap->Release();
|
|
|
|
delete[]pCmdAlloc;
|
2019-10-17 18:03:19 +02:00
|
|
|
delete[]pBackBuffer;
|
2019-10-15 15:01:25 +02:00
|
|
|
|
2019-08-21 21:52:08 +02:00
|
|
|
ImGui_ImplDX12_InvalidateDeviceObjects();
|
|
|
|
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
|
|
|
{
|
2020-07-16 11:56:24 +02:00
|
|
|
return DX12_DLL;
|
2019-08-26 16:46:56 +02:00
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-10-15 08:41:07 +02:00
|
|
|
void DX12_Hook::loadFunctions(ID3D12CommandQueue* pCommandQueue, IDXGISwapChain *pSwapChain)
|
2019-08-26 16:46:56 +02:00
|
|
|
{
|
2019-08-26 19:36:07 +02:00
|
|
|
void** vTable;
|
2019-08-26 16:46:56 +02:00
|
|
|
|
2019-08-27 17:40:44 +02:00
|
|
|
vTable = *reinterpret_cast<void***>(pCommandQueue);
|
2019-08-27 15:38:07 +02:00
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)ID3D12CommandQueueVTable::X]
|
|
|
|
LOAD_FUNC(ExecuteCommandLists);
|
|
|
|
#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-14 14:55:31 +02:00
|
|
|
}
|
|
|
|
|
2020-01-19 18:55:14 +01:00
|
|
|
#endif//EMU_OVERLAY
|