-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
dxgi-capture.cpp
367 lines (298 loc) · 9.05 KB
/
dxgi-capture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <d3d10_1.h>
#include <d3d11.h>
#include <dxgi1_2.h>
#include <d3dcompiler.h>
#include <inttypes.h>
#include "graphics-hook.h"
#include <detours.h>
#if COMPILE_D3D12_HOOK
#include <d3d12.h>
#endif
typedef HRESULT(STDMETHODCALLTYPE *resize_buffers_t)(IDXGISwapChain *, UINT,
UINT, UINT, DXGI_FORMAT,
UINT);
typedef HRESULT(STDMETHODCALLTYPE *present_t)(IDXGISwapChain *, UINT, UINT);
typedef HRESULT(STDMETHODCALLTYPE *present1_t)(IDXGISwapChain1 *, UINT, UINT,
const DXGI_PRESENT_PARAMETERS *);
resize_buffers_t RealResizeBuffers = nullptr;
present_t RealPresent = nullptr;
present1_t RealPresent1 = nullptr;
thread_local int dxgi_presenting = 0;
struct ID3D12CommandQueue *dxgi_possible_swap_queues[8]{};
size_t dxgi_possible_swap_queue_count;
bool dxgi_present_attempted = false;
struct dxgi_swap_data {
IDXGISwapChain *swap;
void (*capture)(void *, void *);
void (*free)(void);
};
static struct dxgi_swap_data data = {};
static int swap_chain_mismatch_count = 0;
constexpr int swap_chain_mismtach_limit = 16;
static void STDMETHODCALLTYPE SwapChainDestructed(void *pData)
{
if (pData == data.swap) {
data.swap = nullptr;
data.capture = nullptr;
memset(dxgi_possible_swap_queues, 0,
sizeof(dxgi_possible_swap_queues));
dxgi_possible_swap_queue_count = 0;
dxgi_present_attempted = false;
data.free();
data.free = nullptr;
}
}
static void init_swap_data(IDXGISwapChain *swap,
void (*capture)(void *, void *), void (*free)(void))
{
data.swap = swap;
data.capture = capture;
data.free = free;
ID3DDestructionNotifier *notifier;
if (SUCCEEDED(
swap->QueryInterface<ID3DDestructionNotifier>(¬ifier))) {
UINT callbackID;
notifier->RegisterDestructionCallback(&SwapChainDestructed,
swap, &callbackID);
notifier->Release();
}
}
static bool setup_dxgi(IDXGISwapChain *swap)
{
IUnknown *device;
HRESULT hr;
hr = swap->GetDevice(__uuidof(ID3D11Device), (void **)&device);
if (SUCCEEDED(hr)) {
ID3D11Device *d3d11 = static_cast<ID3D11Device *>(device);
D3D_FEATURE_LEVEL level = d3d11->GetFeatureLevel();
device->Release();
if (level >= D3D_FEATURE_LEVEL_11_0) {
hlog("Found D3D11 11.0 device on swap chain");
init_swap_data(swap, d3d11_capture, d3d11_free);
return true;
}
}
hr = swap->GetDevice(__uuidof(ID3D10Device), (void **)&device);
if (SUCCEEDED(hr)) {
device->Release();
hlog("Found D3D10 device on swap chain");
init_swap_data(swap, d3d10_capture, d3d10_free);
return true;
}
hr = swap->GetDevice(__uuidof(ID3D11Device), (void **)&device);
if (SUCCEEDED(hr)) {
device->Release();
hlog("Found D3D11 device on swap chain");
init_swap_data(swap, d3d11_capture, d3d11_free);
return true;
}
#if COMPILE_D3D12_HOOK
hr = swap->GetDevice(__uuidof(ID3D12Device), (void **)&device);
if (SUCCEEDED(hr)) {
device->Release();
hlog("Found D3D12 device on swap chain: swap=0x%" PRIX64
", device=0x%" PRIX64,
(uint64_t)(uintptr_t)swap, (uint64_t)(uintptr_t)device);
for (size_t i = 0; i < dxgi_possible_swap_queue_count; ++i) {
hlog(" queue=0x%" PRIX64,
(uint64_t)(uintptr_t)dxgi_possible_swap_queues[i]);
}
if (dxgi_possible_swap_queue_count > 0) {
init_swap_data(swap, d3d12_capture, d3d12_free);
return true;
}
}
#endif
hlog_verbose("Failed to setup DXGI");
return false;
}
static bool resize_buffers_called = false;
static HRESULT STDMETHODCALLTYPE hook_resize_buffers(IDXGISwapChain *swap,
UINT buffer_count,
UINT width, UINT height,
DXGI_FORMAT format,
UINT flags)
{
hlog_verbose("ResizeBuffers callback");
data.swap = nullptr;
data.capture = nullptr;
memset(dxgi_possible_swap_queues, 0, sizeof(dxgi_possible_swap_queues));
dxgi_possible_swap_queue_count = 0;
dxgi_present_attempted = false;
if (data.free)
data.free();
data.free = nullptr;
const HRESULT hr = RealResizeBuffers(swap, buffer_count, width, height,
format, flags);
resize_buffers_called = true;
return hr;
}
static inline IUnknown *get_dxgi_backbuffer(IDXGISwapChain *swap)
{
IUnknown *res = nullptr;
const HRESULT hr = swap->GetBuffer(0, IID_PPV_ARGS(&res));
if (FAILED(hr))
hlog_hr("get_dxgi_backbuffer: GetBuffer failed", hr);
return res;
}
static void update_mismatch_count(bool match)
{
if (match) {
swap_chain_mismatch_count = 0;
} else {
++swap_chain_mismatch_count;
if (swap_chain_mismatch_count == swap_chain_mismtach_limit) {
data.swap = nullptr;
data.capture = nullptr;
memset(dxgi_possible_swap_queues, 0,
sizeof(dxgi_possible_swap_queues));
dxgi_possible_swap_queue_count = 0;
dxgi_present_attempted = false;
data.free();
data.free = nullptr;
swap_chain_mismatch_count = 0;
}
}
}
static HRESULT STDMETHODCALLTYPE hook_present(IDXGISwapChain *swap,
UINT sync_interval, UINT flags)
{
if (should_passthrough()) {
dxgi_presenting = true;
const HRESULT hr = RealPresent(swap, sync_interval, flags);
dxgi_presenting = false;
return hr;
}
const bool capture_overlay = global_hook_info->capture_overlay;
const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
if (data.swap) {
update_mismatch_count(swap == data.swap);
}
if (!data.swap && !capture_active()) {
setup_dxgi(swap);
}
hlog_verbose(
"Present callback: sync_interval=%u, flags=%u, current_swap=0x%" PRIX64
", expected_swap=0x%" PRIX64,
sync_interval, flags, swap, data.swap);
const bool capture = !test_draw && swap == data.swap && data.capture;
if (capture && !capture_overlay) {
IUnknown *backbuffer = get_dxgi_backbuffer(swap);
if (backbuffer) {
data.capture(swap, backbuffer);
backbuffer->Release();
}
}
++dxgi_presenting;
const HRESULT hr = RealPresent(swap, sync_interval, flags);
--dxgi_presenting;
dxgi_present_attempted = true;
if (capture && capture_overlay) {
/*
* It seems that the first call to Present after ResizeBuffers
* will cause the backbuffer to be invalidated, so do not
* perform the post-overlay capture if ResizeBuffers has
* recently been called. (The backbuffer returned by
* get_dxgi_backbuffer *will* be invalid otherwise)
*/
if (resize_buffers_called) {
resize_buffers_called = false;
} else {
IUnknown *backbuffer = get_dxgi_backbuffer(swap);
if (backbuffer) {
data.capture(swap, backbuffer);
backbuffer->Release();
}
}
}
return hr;
}
static HRESULT STDMETHODCALLTYPE
hook_present1(IDXGISwapChain1 *swap, UINT sync_interval, UINT flags,
const DXGI_PRESENT_PARAMETERS *params)
{
if (should_passthrough()) {
dxgi_presenting = true;
const HRESULT hr =
RealPresent1(swap, sync_interval, flags, params);
dxgi_presenting = false;
return hr;
}
const bool capture_overlay = global_hook_info->capture_overlay;
const bool test_draw = (flags & DXGI_PRESENT_TEST) != 0;
if (data.swap) {
update_mismatch_count(swap == data.swap);
}
if (!data.swap && !capture_active()) {
setup_dxgi(swap);
}
hlog_verbose(
"Present1 callback: sync_interval=%u, flags=%u, current_swap=0x%" PRIX64
", expected_swap=0x%" PRIX64,
sync_interval, flags, swap, data.swap);
const bool capture = !test_draw && swap == data.swap && !!data.capture;
if (capture && !capture_overlay) {
IUnknown *backbuffer = get_dxgi_backbuffer(swap);
if (backbuffer) {
data.capture(swap, backbuffer);
backbuffer->Release();
}
}
++dxgi_presenting;
const HRESULT hr = RealPresent1(swap, sync_interval, flags, params);
--dxgi_presenting;
dxgi_present_attempted = true;
if (capture && capture_overlay) {
if (resize_buffers_called) {
resize_buffers_called = false;
} else {
IUnknown *backbuffer = get_dxgi_backbuffer(swap);
if (backbuffer) {
data.capture(swap, backbuffer);
backbuffer->Release();
}
}
}
return hr;
}
bool hook_dxgi(void)
{
HMODULE dxgi_module = get_system_module("dxgi.dll");
if (!dxgi_module) {
hlog_verbose("Failed to find dxgi.dll. Skipping hook attempt.");
return false;
}
/* ---------------------- */
void *present_addr = get_offset_addr(
dxgi_module, global_hook_info->offsets.dxgi.present);
void *resize_addr = get_offset_addr(
dxgi_module, global_hook_info->offsets.dxgi.resize);
void *present1_addr = nullptr;
if (global_hook_info->offsets.dxgi.present1)
present1_addr = get_offset_addr(
dxgi_module, global_hook_info->offsets.dxgi.present1);
DetourTransactionBegin();
RealPresent = (present_t)present_addr;
DetourAttach(&(PVOID &)RealPresent, hook_present);
RealResizeBuffers = (resize_buffers_t)resize_addr;
DetourAttach(&(PVOID &)RealResizeBuffers, hook_resize_buffers);
if (present1_addr) {
RealPresent1 = (present1_t)present1_addr;
DetourAttach(&(PVOID &)RealPresent1, hook_present1);
}
const LONG error = DetourTransactionCommit();
const bool success = error == NO_ERROR;
if (success) {
hlog("Hooked IDXGISwapChain::Present");
hlog("Hooked IDXGISwapChain::ResizeBuffers");
if (RealPresent1)
hlog("Hooked IDXGISwapChain1::Present1");
hlog("Hooked DXGI");
} else {
RealPresent = nullptr;
RealResizeBuffers = nullptr;
RealPresent1 = nullptr;
hlog("Failed to attach Detours hook: %ld", error);
}
return success;
}