HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
Direct3D11.cpp
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2018-2026 Benjamin Höglinger-Stelzer
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#include "Direct3D11.h"
26#include <dxgi1_4.h>
27#include "DXGI.h"
28#include "Exceptions.hpp"
29
30using namespace HydraHook::Core::Exceptions;
31
33 pd3dDevice(nullptr), pd3dDeviceContext(nullptr), pSwapChain(nullptr)
34{
35 temp_window = std::make_unique<Window>("TempDirect3D11OverlayWindow");
36
37 const auto hModDXGI = GetModuleHandle("DXGI.dll");
38 const auto hModD3D11 = GetModuleHandle("D3D11.dll");
39
40 if (hModDXGI == nullptr)
41 {
42 throw ModuleNotFoundException("Could not get handle to DXGI.dll");
43 }
44
45 if (hModD3D11 == nullptr)
46 {
47 throw ModuleNotFoundException("Could not get handle to D3D11.dll");
48 }
49
50 // Setup swap chain
51 DXGI_SWAP_CHAIN_DESC sd;
52 {
53 ZeroMemory(&sd, sizeof(sd));
54 sd.BufferCount = 2;
55 sd.BufferDesc.Width = 0;
56 sd.BufferDesc.Height = 0;
57 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
58 sd.BufferDesc.RefreshRate.Numerator = 60;
59 sd.BufferDesc.RefreshRate.Denominator = 1;
60 sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
61 sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
62 sd.OutputWindow = temp_window->windowHandle();
63 sd.SampleDesc.Count = 1;
64 sd.SampleDesc.Quality = 0;
65 sd.Windowed = TRUE;
66 sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
67 }
68
69 UINT createDeviceFlags = 0;
70 D3D_FEATURE_LEVEL featureLevel;
71 // Note: requesting lower feature level in case of missing hardware support
72 const D3D_FEATURE_LEVEL featureLevelArray[] = {
73 D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0
74 };
75
76 const auto hD3D11CreateDeviceAndSwapChain = static_cast<LPVOID>(GetProcAddress(
77 hModD3D11,
78 "D3D11CreateDeviceAndSwapChain"
79 ));
80 if (hD3D11CreateDeviceAndSwapChain == nullptr)
81 {
82 throw ProcAddressNotFoundException("Could not get handle to D3D11CreateDeviceAndSwapChain");
83 }
84
85 const auto createDevice = static_cast<HRESULT(WINAPI *)(
86 IDXGIAdapter*,
87 D3D_DRIVER_TYPE,
88 HMODULE,
89 UINT,
90 const D3D_FEATURE_LEVEL*,
91 UINT,
92 UINT,
93 const DXGI_SWAP_CHAIN_DESC*,
94 IDXGISwapChain**,
95 ID3D11Device**,
96 D3D_FEATURE_LEVEL*,
97 ID3D11DeviceContext**)>(hD3D11CreateDeviceAndSwapChain);
98
99 // Hardware first so a real GPU is preferred. Fall back to WARP when the
100 // machine has no usable adapter (headless CI runners). The DXGI method
101 // addresses are the same for both drivers.
102 HRESULT hr11 = createDevice(
103 nullptr,
104 D3D_DRIVER_TYPE_HARDWARE,
105 nullptr,
106 createDeviceFlags,
107 featureLevelArray,
108 ARRAYSIZE(featureLevelArray),
109 D3D11_SDK_VERSION,
110 &sd,
111 &pSwapChain,
112 &pd3dDevice,
113 &featureLevel,
114 &pd3dDeviceContext);
115
116 if (FAILED(hr11))
117 {
118 hr11 = createDevice(
119 nullptr,
120 D3D_DRIVER_TYPE_WARP,
121 nullptr,
122 createDeviceFlags,
123 featureLevelArray,
124 ARRAYSIZE(featureLevelArray),
125 D3D11_SDK_VERSION,
126 &sd,
127 &pSwapChain,
128 &pd3dDevice,
129 &featureLevel,
130 &pd3dDeviceContext);
131 }
132
133 if (FAILED(hr11))
134 {
135 throw DXAPIException("Could not create D3D11 device", hr11);
136 }
137}
138
139std::vector<size_t> Direct3D11Hooking::Direct3D11::vtable() const
140{
141 auto capture = [this](int count)
142 {
143 return std::vector<size_t>(*reinterpret_cast<size_t**>(pSwapChain),
144 *reinterpret_cast<size_t**>(pSwapChain) + count);
145 };
146
147 IDXGISwapChain3* swapChain3 = nullptr;
148 if (SUCCEEDED(pSwapChain->QueryInterface(IID_PPV_ARGS(&swapChain3))))
149 {
150 swapChain3->Release();
152 }
153
154 IDXGISwapChain1* swapChain1 = nullptr;
155 if (SUCCEEDED(pSwapChain->QueryInterface(IID_PPV_ARGS(&swapChain1))))
156 {
157 swapChain1->Release();
159 }
160
162}
163
165{
166 if (pSwapChain)
167 pSwapChain->Release();
168
169 if (pd3dDevice)
170 pd3dDevice->Release();
171
172 if (pd3dDeviceContext)
173 pd3dDeviceContext->Release();
174}
IDXGISwapChain vtable indices for D3D10/11/12 hooking.
HydraHook exception types (Detours, Win32, generic).
static const int SwapChainVTableElements
Definition DXGI.h:150
static const int SwapChain1VTableElements
Definition DXGI.h:151
static const int SwapChain3VTableElements
Definition DXGI.h:152
std::vector< size_t > vtable() const override
Returns vtable indices for hookable methods (Present, Reset, etc.).
std::unique_ptr< Window > temp_window
DXAPIException(const std::string &msg, HRESULT result)