forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3d11_hooks.cpp
More file actions
353 lines (295 loc) · 11.4 KB
/
d3d11_hooks.cpp
File metadata and controls
353 lines (295 loc) · 11.4 KB
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
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "driver/d3d11/d3d11_device.h"
#include "driver/dxgi/dxgi_wrapped.h"
#include "hooks.h"
#define DLL_NAME "d3d11.dll"
class D3D11Hook : LibraryHook
{
public:
D3D11Hook() { LibraryHooks::GetInstance().RegisterHook(DLL_NAME, this); m_EnabledHooks = true; m_InsideCreate = false; }
bool CreateHooks(const char *libName)
{
bool success = true;
// require dxgi.dll hooked as well for proper operation
if(GetModuleHandleA("dxgi.dll") == NULL)
{
RDCWARN("Failed to load dxgi.dll - not inserting D3D11 hooks.");
return false;
}
// also require d3dcompiler_??.dll
if(GetD3DCompiler() == NULL)
{
RDCERR("Failed to load d3dcompiler_??.dll - not inserting D3D11 hooks.");
return false;
}
success &= CreateDevice.Initialize("D3D11CreateDevice", DLL_NAME, D3D11CreateDevice_hook);
success &= CreateDeviceAndSwapChain.Initialize("D3D11CreateDeviceAndSwapChain", DLL_NAME, D3D11CreateDeviceAndSwapChain_hook);
if(!success) return false;
// FRAPS compatibility. Save out the first 16 bytes (arbitrary number) of the 'real' function code.
// this should be
// jmp D3D11CreateDeviceAndSwapChain_hook
// push r12 <- this is where our trampoline jumps back to
// push r13
//
// FRAPS stomps over this with its own hook that we detect and handle later.
void *hooked_func_ptr = GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
if(hooked_func_ptr == NULL) return false;
memcpy(CreateDeviceAndSwapChain_ident, hooked_func_ptr, 16);
m_HasHooks = true;
m_EnabledHooks = true;
return true;
}
void EnableHooks(const char *libName, bool enable)
{
m_EnabledHooks = enable;
}
bool UseHooks()
{
return (d3d11hooks.m_HasHooks && d3d11hooks.m_EnabledHooks);
}
static HRESULT CreateWrappedDeviceAndSwapChain(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
return d3d11hooks.Create_Internal(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
}
private:
static D3D11Hook d3d11hooks;
bool m_HasHooks;
bool m_EnabledHooks;
byte CreateDeviceAndSwapChain_ident[16];
Hook<PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN> CreateDeviceAndSwapChain;
Hook<PFN_D3D11_CREATE_DEVICE> CreateDevice;
// re-entrancy detection (can happen in rare cases with e.g. fraps)
bool m_InsideCreate;
HRESULT Create_Internal(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
// if we're already inside a wrapped create i.e. this function, then DON'T do anything
// special. Just grab the trampolined function and call it.
if(m_InsideCreate)
{
PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN createFunc = NULL;
// shouldn't ever get in here if we're in the case without hooks but let's be safe.
if(m_HasHooks)
createFunc = CreateDeviceAndSwapChain();
else
createFunc = (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
return createFunc(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
}
m_InsideCreate = true;
RDCDEBUG("Call to Create_Internal Flags %x", Flags);
bool reading = RenderDoc::Inst().IsReplayApp();
if(reading)
{
RDCDEBUG("In replay app");
}
if(m_EnabledHooks)
{
if(!reading && RenderDoc::Inst().GetCaptureOptions().DebugDeviceMode)
{
Flags |= D3D11_CREATE_DEVICE_DEBUG;
}
else
{
Flags &= ~D3D11_CREATE_DEVICE_DEBUG;
}
}
DXGI_SWAP_CHAIN_DESC swapDesc;
DXGI_SWAP_CHAIN_DESC *pUsedSwapDesc = NULL;
if(pSwapChainDesc)
{
swapDesc = *pSwapChainDesc;
pUsedSwapDesc = &swapDesc;
}
if(pUsedSwapDesc && m_EnabledHooks && !RenderDoc::Inst().GetCaptureOptions().AllowFullscreen)
{
pUsedSwapDesc->Windowed = TRUE;
}
RDCDEBUG("Calling real createdevice...");
PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN createFunc = (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
if(createFunc)
{
byte ident[16];
memcpy(ident, createFunc, 16);
// FRAPS compatibility. If FRAPS has come in and modified the real code to something like
// mov rax, [ptrintofraps]
// jmp rax
// then we just jump straight there. If we jump to the trampoline, we'll come in half way through
// those instructions and things will go boom very quickly.
//
// Note that when we call this function FRAPS then restores the asm from before it got here, which is our
// trampolined code, so FRAPS is going to call back into this function. We detect this re-entrancy
// at the top and just go straight into the real d3d function now that FRAPS has restored our trampoline,
// and return right back here
// What a headache!
//
// in the non-fraps case no-one else has messed with this code so the idents will be the same and we
// will use our trampolines.
if(!memcmp(ident, CreateDeviceAndSwapChain_ident, 16) && m_HasHooks)
createFunc = CreateDeviceAndSwapChain();
}
// shouldn't ever get here, we should either have it from procaddress or the trampoline, but let's be
// safe.
if(createFunc == NULL)
{
RDCERR("Something went seriously wrong with the hooks!");
m_InsideCreate = false;
return E_UNEXPECTED;
}
HRESULT ret = createFunc(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pUsedSwapDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
RDCDEBUG("Called real createdevice...");
bool suppress = (Flags & D3D11_CREATE_DEVICE_PREVENT_ALTERING_LAYER_SETTINGS_FROM_REGISTRY) != 0;
if(suppress && !reading)
{
RDCDEBUG("Application requested not to be hooked.");
}
else if(SUCCEEDED(ret) && m_EnabledHooks && ppDevice)
{
RDCDEBUG("succeeded and hooking.");
if(!WrappedID3D11Device::IsAlloc(*ppDevice))
{
D3D11InitParams *params = new D3D11InitParams;
params->DriverType = DriverType;
params->Flags = Flags;
params->SDKVersion = SDKVersion;
params->NumFeatureLevels = FeatureLevels;
if(FeatureLevels > 0)
memcpy(params->FeatureLevels, pFeatureLevels, sizeof(D3D_FEATURE_LEVEL)*FeatureLevels);
WrappedID3D11Device *wrap = new WrappedID3D11Device(*ppDevice, params);
RDCDEBUG("created wrapped device.");
*ppDevice = wrap;
wrap->GetImmediateContext(ppImmediateContext);
if(ppSwapChain && *ppSwapChain)
*ppSwapChain = new WrappedIDXGISwapChain(*ppSwapChain, wrap);
}
}
else if(SUCCEEDED(ret))
{
RDCDEBUG("succeeded.");
}
else
{
RDCDEBUG("failed. 0x%08x", ret);
}
m_InsideCreate = false;
return ret;
}
static HRESULT WINAPI D3D11CreateDevice_hook(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
HRESULT ret = d3d11hooks.Create_Internal(
pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, NULL, NULL, ppDevice, pFeatureLevel, ppImmediateContext);
return ret;
}
static HRESULT WINAPI D3D11CreateDeviceAndSwapChain_hook(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
HRESULT ret = d3d11hooks.Create_Internal(
pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
return ret;
}
};
D3D11Hook D3D11Hook::d3d11hooks;
extern "C" __declspec(dllexport)
HRESULT __cdecl RENDERDOC_CreateWrappedD3D11DeviceAndSwapChain(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
return D3D11Hook::CreateWrappedDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
}
extern "C" __declspec(dllexport)
HRESULT __cdecl RENDERDOC_CreateWrappedD3D11Device(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
return D3D11Hook::CreateWrappedDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, NULL, NULL, ppDevice, pFeatureLevel, ppImmediateContext);
}