11// DX11 Present hook — cimgui-based ImGui overlay
22#include " hook_common.h"
3+ #include " cimgui.h"
4+ #include " kiero.hpp"
5+ #include " kiero_d3d11.hpp"
36
47typedef HRESULT (APIENTRY * PresentFn)(IDXGISwapChain*, UINT , UINT );
58
@@ -38,6 +41,8 @@ namespace DX11Hook {
3841 return CallWindowProcW (g_OriginalWndProc, h, m, w, l);
3942 }
4043
44+ ID3D11RenderTargetView *MainRTV = nullptr ;
45+
4146 HRESULT HookPresent (IDXGISwapChain* swapChain, UINT syncInterval, UINT flags) {
4247 if (!Initialised) {
4348 DXGI_SWAP_CHAIN_DESC desc;
@@ -48,6 +53,7 @@ namespace DX11Hook {
4853 swapChain->GetDevice (__uuidof (ID3D11Device), (void **)&device);
4954 device->GetImmediateContext (&DeviceContext);
5055 if (imgui_callbacks.init_callback ) imgui_callbacks.init_callback ();
56+
5157 if (ImGui_ImplWin32_Init (g_GameWindow) && ImGui_ImplDX11_Init (device, DeviceContext)) {
5258 SwapChain = swapChain;
5359 g_OriginalWndProc = (WNDPROC )SetWindowLongPtrW (g_GameWindow,
@@ -56,68 +62,59 @@ namespace DX11Hook {
5662 ImGui_Initialised = true ;
5763 DEBUG_LOG (" DX11Hook: ImGui initialised, hwnd=%p" , g_GameWindow);
5864 }
59-
60- device->Release ();
6165 }
6266
6367 if (Initialised) {
68+ /* if (MainRTV) { MainRTV->Release(); MainRTV = nullptr; }
69+ ID3D11Texture2D* bb = nullptr;
70+ swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&bb);
71+ ID3D11Device* device = nullptr;
72+ swapChain->GetDevice(__uuidof(ID3D11Device), (void**)&device);
73+ if (bb) { device->CreateRenderTargetView(bb, nullptr, &MainRTV); bb->Release(); }
74+ */
75+
6476 ImGui_ImplDX11_NewFrame ();
6577 ImGui_ImplWin32_NewFrame ();
78+ auto io = igGetIO ();
79+ DXGI_SWAP_CHAIN_DESC desc;
80+ swapChain->GetDesc (&desc);
81+ io->DisplaySize .x = desc.BufferDesc .Width ;
82+ io->DisplaySize .y = desc.BufferDesc .Height ;
6683 if (imgui_callbacks.render_callback ) imgui_callbacks.render_callback ();
6784 ImGui_ImplDX11_RenderDrawData (igGetDrawData ());
85+
86+ // if (MainRTV) DeviceContext->OMSetRenderTargets(1, &MainRTV, nullptr);
6887 }
6988
7089 return OriginalPresent (swapChain, syncInterval, flags);
7190 }
7291
7392 bool InstallHook () {
74- DEBUG_LOG (" DX11Hook::InstallHook: creating dummy device..." );
75-
76- WNDCLASSEXW wc = {};
77- wc.cbSize = sizeof (wc);
78- wc.lpfnWndProc = DefWindowProcW;
79- wc.hInstance = GetModuleHandleW (nullptr );
80- wc.lpszClassName = L" DX11HookDummy" ;
81- RegisterClassExW (&wc);
82-
83- HWND dummy = CreateWindowExW (0 , L" DX11HookDummy" , L" " , WS_POPUP ,
84- 0 , 0 , 1 , 1 , nullptr , nullptr , wc.hInstance , nullptr );
85-
86- DXGI_SWAP_CHAIN_DESC scd = {};
87- scd.BufferCount = 1 ;
88- scd.BufferDesc .Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
89- scd.BufferDesc .Width = 1 ;
90- scd.BufferDesc .Height = 1 ;
91- scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT ;
92- scd.OutputWindow = dummy;
93- scd.SampleDesc .Count = 1 ;
94- scd.Windowed = TRUE ;
95-
96- ID3D11Device* tmpDevice = nullptr ;
97- IDXGISwapChain* tmpSwapChain = nullptr ;
98- ID3D11DeviceContext* tmpContext = nullptr ;
99-
100- bool ok = false ;
101- if (SUCCEEDED (D3D11CreateDeviceAndSwapChain (
102- nullptr , D3D_DRIVER_TYPE_HARDWARE , nullptr , 0 ,
103- nullptr , 0 , D3D11_SDK_VERSION ,
104- &scd, &tmpSwapChain, &tmpDevice, nullptr , &tmpContext))) {
105-
106- MH_Initialize ();
107- void ** vt = *(void ***)tmpSwapChain;
108- ok = (MH_CreateHook (vt[8 ], (void *)HookPresent, (void **)&OriginalPresent) == MH_OK )
109- && (MH_EnableHook (vt[8 ]) == MH_OK );
110- DEBUG_LOG (" DX11Hook::InstallHook: Present hook %s" , ok ? " OK" : " FAILED" );
111-
112- tmpSwapChain->Release ();
113- tmpContext->Release ();
114- tmpDevice->Release ();
115- } else {
116- DEBUG_LOG (" DX11Hook::InstallHook: D3D11CreateDeviceAndSwapChain FAILED" );
93+ DEBUG_LOG (" DX11Hook::InstallHook: locating D3D11 methods via kiero..." );
94+
95+ kiero::D3D11Output output;
96+ auto err = kiero::locate<kiero::Implementation_D3D11>(nullptr , &output);
97+ if (err != kiero::Error_Nil) {
98+ DEBUG_LOG (" DX11Hook::InstallHook: kiero locate failed (err=%d)" , err);
99+ return false ;
100+ }
101+
102+ if (output.swapchain_methods .size () <= 8 || !output.swapchain_methods [8 ]) {
103+ DEBUG_LOG (" DX11Hook::InstallHook: Present method not found in vtable" );
104+ return false ;
105+ }
106+
107+ auto presentAddr = output.swapchain_methods [8 ];
108+ DEBUG_LOG (" DX11Hook::InstallHook: Present found at %p" , presentAddr);
109+
110+ MH_Initialize ();
111+ if (MH_CreateHook (presentAddr, (void *)HookPresent, (void **)&OriginalPresent) != MH_OK
112+ || MH_EnableHook (presentAddr) != MH_OK ) {
113+ DEBUG_LOG (" DX11Hook::InstallHook: MinHook failed" );
114+ return false ;
117115 }
118116
119- DestroyWindow (dummy);
120- UnregisterClassW (L" DX11HookDummy" , wc.hInstance );
121- return ok;
117+ DEBUG_LOG (" DX11Hook::InstallHook: hook installed successfully" );
118+ return true ;
122119 }
123120}
0 commit comments