2019-07-25 23:21:03 +02:00
|
|
|
#include "DX11_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_dx11.h>
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX11_Hook* DX11_Hook::_inst = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
HRESULT GetDeviceAndCtxFromSwapchain(IDXGISwapChain* pSwapChain, ID3D11Device** ppDevice, ID3D11DeviceContext** ppContext)
|
|
|
|
{
|
2019-08-19 19:51:35 +02:00
|
|
|
HRESULT ret = pSwapChain->GetDevice(IID_PPV_ARGS(ppDevice));
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
if (SUCCEEDED(ret))
|
|
|
|
(*ppDevice)->GetImmediateContext(ppContext);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-16 19:10:12 +02:00
|
|
|
bool DX11_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-18 17:12:57 +02:00
|
|
|
return false;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-26 16:38:01 +02:00
|
|
|
PRINT_DEBUG("Hooked DirectX 11\n");
|
2019-08-27 15:38:07 +02:00
|
|
|
hooked = true;
|
2019-08-26 16:38:01 +02:00
|
|
|
|
2019-08-27 15:38:07 +02:00
|
|
|
Renderer_Detector::Inst().renderer_found(this);
|
2019-08-26 16:38:01 +02:00
|
|
|
|
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX11_Hook::Present, &DX11_Hook::MyPresent),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX11_Hook::ResizeTarget, &DX11_Hook::MyResizeTarget),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX11_Hook::ResizeBuffers, &DX11_Hook::MyResizeBuffers)
|
|
|
|
);
|
|
|
|
EndHook();
|
2019-08-20 23:27:17 +02:00
|
|
|
|
2019-08-26 16:38:01 +02:00
|
|
|
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 DX11_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
2020-02-03 01:06:44 +01:00
|
|
|
if (mainRenderTargetView) {
|
|
|
|
mainRenderTargetView->Release();
|
|
|
|
mainRenderTargetView = NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
pContext->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX11_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 DX11_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
|
|
|
|
{
|
|
|
|
DXGI_SWAP_CHAIN_DESC desc;
|
|
|
|
pSwapChain->GetDesc(&desc);
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
ID3D11Device* pDevice = nullptr;
|
|
|
|
if (FAILED(GetDeviceAndCtxFromSwapchain(pSwapChain, &pDevice, &pContext)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.IniFilename = NULL;
|
|
|
|
|
|
|
|
ID3D11Texture2D* pBackBuffer;
|
2019-08-19 19:51:35 +02:00
|
|
|
pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
|
2020-02-03 01:06:44 +01:00
|
|
|
|
|
|
|
ID3D11RenderTargetView *get_targets[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT] = {};
|
|
|
|
pContext->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, get_targets, NULL);
|
|
|
|
bool bind_target = true;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) {
|
|
|
|
if (get_targets[i]) {
|
|
|
|
ID3D11Resource *res = NULL;
|
|
|
|
get_targets[i]->GetResource(&res);
|
|
|
|
if (res) {
|
|
|
|
if (res == (ID3D11Resource*)pBackBuffer) {
|
|
|
|
bind_target = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
get_targets[i]->Release();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind_target) {
|
|
|
|
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView);
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
pBackBuffer->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX11_Init(pDevice, pContext);
|
2019-08-16 18:30:55 +02:00
|
|
|
|
2019-08-19 19:51:35 +02:00
|
|
|
pDevice->Release();
|
|
|
|
|
2019-09-05 09:00:02 +02:00
|
|
|
get_steam_client()->steam_overlay->CreateFonts();
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2021-01-18 04:58:42 +01:00
|
|
|
ImGui_ImplDX11_NewFrame();
|
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
{
|
|
|
|
Windows_Hook::Inst()->prepareForOverlay(desc.OutputWindow);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui::NewFrame();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
get_steam_client()->steam_overlay->OverlayProc();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui::Render();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2020-02-03 01:06:44 +01:00
|
|
|
if (mainRenderTargetView) {
|
|
|
|
pContext->OMSetRenderTargets(1, &mainRenderTargetView, NULL);
|
|
|
|
}
|
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX11_Hook::MyPresent(IDXGISwapChain *_this, UINT SyncInterval, UINT Flags)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX11_Hook::Inst()->prepareForOverlay(_this);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
return (_this->*DX11_Hook::Inst()->Present)(SyncInterval, Flags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX11_Hook::MyResizeTarget(IDXGISwapChain* _this, const DXGI_MODE_DESC* pNewTargetParameters)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX11_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX11_Hook::Inst()->ResizeTarget)(pNewTargetParameters);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX11_Hook::MyResizeBuffers(IDXGISwapChain* _this, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX11_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX11_Hook::Inst()->ResizeBuffers)(BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX11_Hook::DX11_Hook():
|
|
|
|
initialized(false),
|
2019-08-27 15:38:07 +02:00
|
|
|
hooked(false),
|
2019-07-25 23:21:03 +02:00
|
|
|
pContext(nullptr),
|
2019-08-26 19:36:07 +02:00
|
|
|
mainRenderTargetView(nullptr),
|
|
|
|
Present(nullptr),
|
|
|
|
ResizeBuffers(nullptr),
|
|
|
|
ResizeTarget(nullptr)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2020-07-16 11:56:24 +02:00
|
|
|
_library = LoadLibrary(DX11_DLL);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX11_Hook::~DX11_Hook()
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("DX11 Hook removed\n");
|
|
|
|
|
2019-08-21 21:52:08 +02:00
|
|
|
if (initialized)
|
|
|
|
{
|
2020-02-03 01:06:44 +01:00
|
|
|
if (mainRenderTargetView) {
|
|
|
|
mainRenderTargetView->Release();
|
|
|
|
mainRenderTargetView = NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-21 21:52:08 +02:00
|
|
|
pContext->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX11_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
|
|
|
DX11_Hook* DX11_Hook::Inst()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new DX11_Hook;
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 16:38:01 +02:00
|
|
|
const char* DX11_Hook::get_lib_name() const
|
|
|
|
{
|
2020-07-16 11:56:24 +02:00
|
|
|
return DX11_DLL;
|
2019-08-26 16:38:01 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 19:36:07 +02:00
|
|
|
void DX11_Hook::loadFunctions(IDXGISwapChain *pSwapChain)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-26 19:36:07 +02:00
|
|
|
void** vTable;
|
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
|