forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd3d8-capture.cpp
406 lines (328 loc) · 8.58 KB
/
d3d8-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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <dxgi.h>
#include "../d3d8-api/d3d8.h"
#include "graphics-hook.h"
#include <detours.h>
typedef HRESULT(STDMETHODCALLTYPE *reset_t)(IDirect3DDevice8 *,
D3DPRESENT_PARAMETERS *);
typedef HRESULT(STDMETHODCALLTYPE *present_t)(IDirect3DDevice8 *, CONST RECT *,
CONST RECT *, HWND,
CONST RGNDATA *);
reset_t RealReset = NULL;
present_t RealPresent = NULL;
struct d3d8_data {
HMODULE d3d8;
uint32_t cx;
uint32_t cy;
D3DFORMAT d3d8_format;
DXGI_FORMAT dxgi_format;
struct shmem_data *shmem_info;
HWND window;
uint32_t pitch;
IDirect3DSurface8 *copy_surfaces[NUM_BUFFERS];
bool texture_ready[NUM_BUFFERS];
bool surface_locked[NUM_BUFFERS];
int cur_surface;
int copy_wait;
};
static d3d8_data data = {};
static DXGI_FORMAT d3d8_to_dxgi_format(D3DFORMAT format)
{
switch (format) {
case D3DFMT_X1R5G5B5:
case D3DFMT_A1R5G5B5:
return DXGI_FORMAT_B5G5R5A1_UNORM;
case D3DFMT_R5G6B5:
return DXGI_FORMAT_B5G6R5_UNORM;
case D3DFMT_A8R8G8B8:
return DXGI_FORMAT_B8G8R8A8_UNORM;
case D3DFMT_X8R8G8B8:
return DXGI_FORMAT_B8G8R8X8_UNORM;
}
return DXGI_FORMAT_UNKNOWN;
}
static IDirect3DSurface8 *d3d8_get_backbuffer(IDirect3DDevice8 *device)
{
IDirect3DSurface8 *backbuffer;
HRESULT hr;
hr = device->GetRenderTarget(&backbuffer);
if (FAILED(hr)) {
hlog_hr("d3d8_get_backbuffer: Failed to get backbuffer", hr);
backbuffer = nullptr;
}
return backbuffer;
}
static bool d3d8_get_window_handle(IDirect3DDevice8 *device)
{
D3DDEVICE_CREATION_PARAMETERS parameters;
HRESULT hr;
hr = device->GetCreationParameters(¶meters);
if (FAILED(hr)) {
hlog_hr("d3d8_get_window_handle: Failed to get "
"device creation parameters",
hr);
return false;
}
data.window = parameters.hFocusWindow;
return true;
}
static bool d3d8_init_format_backbuffer(IDirect3DDevice8 *device)
{
IDirect3DSurface8 *backbuffer;
D3DSURFACE_DESC desc;
HRESULT hr;
if (!d3d8_get_window_handle(device))
return false;
backbuffer = d3d8_get_backbuffer(device);
if (!backbuffer)
return false;
hr = backbuffer->GetDesc(&desc);
backbuffer->Release();
if (FAILED(hr)) {
hlog_hr("d3d8_init_format_backbuffer: Failed to get "
"backbuffer descriptor",
hr);
return false;
}
data.d3d8_format = desc.Format;
data.dxgi_format = d3d8_to_dxgi_format(desc.Format);
data.cx = desc.Width;
data.cy = desc.Height;
return true;
}
static bool d3d8_shmem_init_buffer(IDirect3DDevice8 *device, int idx)
{
HRESULT hr;
hr = device->CreateImageSurface(data.cx, data.cy, data.d3d8_format,
&data.copy_surfaces[idx]);
if (FAILED(hr)) {
hlog_hr("d3d8_shmem_init_buffer: Failed to create surface", hr);
return false;
}
if (idx == 0) {
D3DLOCKED_RECT rect;
hr = data.copy_surfaces[0]->LockRect(&rect, nullptr,
D3DLOCK_READONLY);
if (FAILED(hr)) {
hlog_hr("d3d8_shmem_init_buffer: Failed to lock buffer",
hr);
return false;
}
data.pitch = rect.Pitch;
data.copy_surfaces[0]->UnlockRect();
}
return true;
}
static bool d3d8_shmem_init(IDirect3DDevice8 *device)
{
for (int i = 0; i < NUM_BUFFERS; i++) {
if (!d3d8_shmem_init_buffer(device, i)) {
return false;
}
}
if (!capture_init_shmem(&data.shmem_info, data.window, data.cx, data.cy,
data.pitch, data.dxgi_format, false)) {
return false;
}
hlog("d3d8 memory capture successful");
return true;
}
static void d3d8_free()
{
capture_free();
for (size_t i = 0; i < NUM_BUFFERS; i++) {
if (data.copy_surfaces[i]) {
if (data.surface_locked[i])
data.copy_surfaces[i]->UnlockRect();
data.copy_surfaces[i]->Release();
}
}
memset(&data, 0, sizeof(data));
hlog("----------------- d3d8 capture freed -----------------");
}
static void d3d8_init(IDirect3DDevice8 *device)
{
data.d3d8 = get_system_module("d3d8.dll");
if (!d3d8_init_format_backbuffer(device))
return;
if (!d3d8_shmem_init(device))
d3d8_free();
}
static void d3d8_shmem_capture_copy(int idx)
{
D3DLOCKED_RECT rect;
HRESULT hr;
if (data.texture_ready[idx]) {
data.texture_ready[idx] = false;
IDirect3DSurface8 *target = data.copy_surfaces[idx];
hr = target->LockRect(&rect, nullptr, D3DLOCK_READONLY);
if (SUCCEEDED(hr)) {
data.surface_locked[idx] = true;
shmem_copy_data(idx, rect.pBits);
}
}
}
static void d3d8_shmem_capture(IDirect3DDevice8 *device,
IDirect3DSurface8 *backbuffer)
{
int next_surface;
HRESULT hr;
next_surface = (data.cur_surface + 1) % NUM_BUFFERS;
d3d8_shmem_capture_copy(next_surface);
if (data.copy_wait < NUM_BUFFERS - 1) {
data.copy_wait++;
} else {
IDirect3DSurface8 *src = backbuffer;
IDirect3DSurface8 *dst = data.copy_surfaces[data.cur_surface];
if (shmem_texture_data_lock(data.cur_surface)) {
dst->UnlockRect();
data.surface_locked[data.cur_surface] = false;
shmem_texture_data_unlock(data.cur_surface);
}
hr = device->CopyRects(src, nullptr, 0, dst, nullptr);
if (FAILED(hr)) {
hlog_hr("d3d8_shmem_capture: CopyRects "
"failed",
hr);
}
data.texture_ready[data.cur_surface] = true;
}
data.cur_surface = next_surface;
}
static void d3d8_capture(IDirect3DDevice8 *device,
IDirect3DSurface8 *backbuffer)
{
if (capture_should_stop()) {
d3d8_free();
}
if (capture_should_init()) {
d3d8_init(device);
}
if (capture_ready()) {
d3d8_shmem_capture(device, backbuffer);
shmem_update_timestamp();
signal_frame_ready();
}
}
static HRESULT STDMETHODCALLTYPE hook_reset(IDirect3DDevice8 *device,
D3DPRESENT_PARAMETERS *parameters)
{
if (capture_active())
d3d8_free();
return RealReset(device, parameters);
}
static bool hooked_reset = false;
static void setup_reset_hooks(IDirect3DDevice8 *device)
{
uintptr_t *vtable = *(uintptr_t **)device;
DetourTransactionBegin();
RealReset = (reset_t)vtable[14];
DetourAttach((PVOID *)&RealReset, hook_reset);
const LONG error = DetourTransactionCommit();
const bool success = error == NO_ERROR;
if (success) {
hlog("Hooked IDirect3DDevice8::Reset");
hooked_reset = true;
} else {
RealReset = nullptr;
}
}
static HRESULT STDMETHODCALLTYPE hook_present(IDirect3DDevice8 *device,
CONST RECT *src_rect,
CONST RECT *dst_rect,
HWND override_window,
CONST RGNDATA *dirty_region)
{
IDirect3DSurface8 *backbuffer;
if (!hooked_reset)
setup_reset_hooks(device);
backbuffer = d3d8_get_backbuffer(device);
if (backbuffer) {
d3d8_capture(device, backbuffer);
backbuffer->Release();
}
return RealPresent(device, src_rect, dst_rect, override_window,
dirty_region);
}
typedef IDirect3D8 *(WINAPI *d3d8create_t)(UINT);
static bool manually_get_d3d8_present_addr(HMODULE d3d8_module,
void **present_addr)
{
d3d8create_t create;
D3DPRESENT_PARAMETERS pp;
HRESULT hr;
IDirect3DDevice8 *device;
IDirect3D8 *d3d8;
hlog("D3D8 value invalid, manually obtaining");
create = (d3d8create_t)GetProcAddress(d3d8_module, "Direct3DCreate8");
if (!create) {
hlog("Failed to load Direct3DCreate8");
return false;
}
d3d8 = create(D3D_SDK_VERSION);
if (!d3d8) {
hlog("Failed to create D3D8 context");
return false;
}
memset(&pp, 0, sizeof(pp));
pp.Windowed = true;
pp.SwapEffect = D3DSWAPEFFECT_FLIP;
pp.BackBufferFormat = D3DFMT_A8R8G8B8;
pp.BackBufferWidth = 2;
pp.BackBufferHeight = 2;
pp.BackBufferCount = 1;
pp.hDeviceWindow = dummy_window;
hr = d3d8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
dummy_window,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp,
&device);
d3d8->Release();
if (SUCCEEDED(hr)) {
uintptr_t *vtable = *(uintptr_t **)device;
*present_addr = (void *)vtable[15];
device->Release();
} else {
hlog("Failed to create D3D8 device");
return false;
}
return true;
}
bool hook_d3d8(void)
{
HMODULE d3d8_module = get_system_module("d3d8.dll");
uint32_t d3d8_size;
void *present_addr = nullptr;
if (!d3d8_module) {
return false;
}
d3d8_size = module_size(d3d8_module);
if (global_hook_info->offsets.d3d8.present < d3d8_size) {
present_addr = get_offset_addr(
d3d8_module, global_hook_info->offsets.d3d8.present);
} else {
if (!dummy_window) {
return false;
}
if (!manually_get_d3d8_present_addr(d3d8_module,
&present_addr)) {
hlog("Failed to get D3D8 value");
return true;
}
}
if (!present_addr) {
hlog("Invalid D3D8 value");
return true;
}
DetourTransactionBegin();
RealPresent = (present_t)present_addr;
DetourAttach((PVOID *)&RealPresent, hook_present);
const LONG error = DetourTransactionCommit();
const bool success = error == NO_ERROR;
if (success) {
hlog("Hooked IDirect3DDevice8::Present");
hlog("Hooked D3D8");
} else {
RealPresent = nullptr;
hlog("Failed to attach Detours hook: %ld", error);
}
return success;
}