HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
Direct3D12.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 "Direct3D12.h"
26#include <wrl/client.h>
27#include <dxgi.h>
28#include <dxgi1_4.h>
29#include <d3d12.h>
30#include "DXGI.h"
31#include "Exceptions.hpp"
32
33using namespace HydraHook::Core::Exceptions;
34using Microsoft::WRL::ComPtr;
35
37 pd3dDevice(nullptr), pQueue(nullptr), pSwapChain(nullptr)
38{
39 const auto hModDXGI = GetModuleHandle("DXGI.dll");
40 const auto hModD3D12 = GetModuleHandle("D3D12.dll");
41
42 if (hModDXGI == nullptr)
43 {
44 throw ModuleNotFoundException("Could not get handle to DXGI.dll");
45 }
46
47 if (hModD3D12 == nullptr)
48 {
49 throw ModuleNotFoundException("Could not get handle to D3D12.dll");
50 }
51
52 const auto hD3D12CreateDevice = static_cast<LPVOID>(GetProcAddress(hModD3D12, "D3D12CreateDevice"));
53
54 if (hD3D12CreateDevice == nullptr)
55 {
56 throw ProcAddressNotFoundException("Could not get handle to D3D12CreateDevice");
57 }
58
59 const auto hCreateDXGIFactory1 = static_cast<LPVOID>(GetProcAddress(hModDXGI, "CreateDXGIFactory1"));
60 if (hCreateDXGIFactory1 == nullptr)
61 {
62 throw ProcAddressNotFoundException("Could not get handle to CreateDXGIFactory1");
63 }
64
65 ComPtr<IDXGIFactory4> pFactory;
66 auto hr = static_cast<HRESULT(WINAPI *)(
67 REFIID,
68 void**)>(hCreateDXGIFactory1)(IID_PPV_ARGS(&pFactory));
69
70 if (FAILED(hr))
71 {
72 throw DXAPIException("Could not create DXGI factory", hr);
73 }
74
75 const auto createDevice = static_cast<HRESULT(WINAPI *)(
76 IUnknown*,
77 D3D_FEATURE_LEVEL,
78 REFIID,
79 void**)>(hD3D12CreateDevice);
80
81 // Hardware first so a real GPU is preferred. Fall back to WARP when the
82 // machine has no usable adapter (headless CI runners). The DXGI and
83 // ID3D12CommandQueue method addresses are the same for both drivers.
84 ComPtr<ID3D12Device> device;
85 HRESULT hr12 = createDevice(
86 nullptr,
87 D3D_FEATURE_LEVEL_11_0,
88 IID_PPV_ARGS(&device));
89
90 if (FAILED(hr12))
91 {
92 ComPtr<IDXGIAdapter> pWarpAdapter;
93 if (SUCCEEDED(pFactory->EnumWarpAdapter(IID_PPV_ARGS(&pWarpAdapter))))
94 {
95 hr12 = createDevice(
96 pWarpAdapter.Get(),
97 D3D_FEATURE_LEVEL_11_0,
98 IID_PPV_ARGS(&device));
99 }
100 }
101
102 if (FAILED(hr12))
103 {
104 throw DXAPIException("Could not create D3D12 device", hr12);
105 }
106
107 D3D12_COMMAND_QUEUE_DESC queueDesc = {};
108 queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
109 queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
110
111 ComPtr<ID3D12CommandQueue> queue;
112 hr = device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&queue));
113
114 if (FAILED(hr))
115 {
116 throw DXAPIException("Command queue creation failed", hr);
117 }
118
119 temp_window = std::make_unique<Window>("TempDirect3D12OverlayWindow");
120
121 DXGI_SWAP_CHAIN_DESC scDesc;
122 ZeroMemory(&scDesc, sizeof(scDesc));
123 scDesc.BufferCount = 2;
124 scDesc.BufferDesc.Width = 0;
125 scDesc.BufferDesc.Height = 0;
126 scDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
127 scDesc.BufferDesc.RefreshRate.Numerator = 60;
128 scDesc.BufferDesc.RefreshRate.Denominator = 1;
129 scDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
130 scDesc.OutputWindow = temp_window->windowHandle();
131 scDesc.SampleDesc.Count = 1;
132 scDesc.SampleDesc.Quality = 0;
133 scDesc.Windowed = TRUE;
134 scDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
135
136 ComPtr<IDXGISwapChain> swapChain;
137 hr = pFactory->CreateSwapChain(
138 queue.Get(),
139 &scDesc,
140 &swapChain
141 );
142
143 if (FAILED(hr))
144 {
145 throw DXAPIException("Swap chain creation failed", hr);
146 }
147
148 pd3dDevice = device.Detach();
149 pQueue = queue.Detach();
150 pSwapChain = swapChain.Detach();
151}
152
153std::vector<size_t> Direct3D12Hooking::Direct3D12::vtable() const
154{
155 auto capture = [this](int count)
156 {
157 return std::vector<size_t>(*reinterpret_cast<size_t**>(pSwapChain),
158 *reinterpret_cast<size_t**>(pSwapChain) + count);
159 };
160
161 IDXGISwapChain3* swapChain3 = nullptr;
162 if (SUCCEEDED(pSwapChain->QueryInterface(IID_PPV_ARGS(&swapChain3))))
163 {
164 swapChain3->Release();
166 }
167
168 IDXGISwapChain1* swapChain1 = nullptr;
169 if (SUCCEEDED(pSwapChain->QueryInterface(IID_PPV_ARGS(&swapChain1))))
170 {
171 swapChain1->Release();
173 }
174
176}
177
179{
180 if (pSwapChain)
181 pSwapChain->Release();
182
183 if (pQueue)
184 pQueue->Release();
185
186 if (pd3dDevice)
187 pd3dDevice->Release();
188}
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)