forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvk_layer.cpp
More file actions
324 lines (273 loc) · 13.6 KB
/
vk_layer.cpp
File metadata and controls
324 lines (273 loc) · 13.6 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
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2015-2017 Baldur Karlsson
*
* 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 <stdlib.h>
#include <string.h>
#include "api/replay/version.h"
#include "common/common.h"
#include "common/threading.h"
#include "hooks/hooks.h"
#include "os/os_specific.h"
#include "serialise/string_utils.h"
#include "vk_common.h"
#include "vk_core.h"
#include "vk_hookset_defs.h"
#include "vk_resources.h"
// this should be in the vulkan definition header
#if ENABLED(RDOC_WIN32)
#undef VK_LAYER_EXPORT
#define VK_LAYER_EXPORT extern "C" __declspec(dllexport)
#endif
// we don't actually hook any modules here. This is just used so that it's called
// at the right time in initialisation (after capture options are available) to
// set environment variables
class VulkanHook : LibraryHook
{
VulkanHook() { LibraryHooks::GetInstance().RegisterHook(VulkanLibraryName, this); }
bool CreateHooks(const char *libName)
{
// we assume the implicit layer is registered - the UI will prompt the user about installing it.
Process::RegisterEnvironmentModification(Process::EnvironmentModification(
Process::eEnvModification_Replace, "ENABLE_VULKAN_RENDERDOC_CAPTURE", "1"));
// check options to set further variables, and apply
OptionsUpdated(libName);
return true;
}
void EnableHooks(const char *libName, bool enable)
{
// set the env var to 0 to disable the implicit layer
Process::RegisterEnvironmentModification(Process::EnvironmentModification(
Process::eEnvModification_Replace, "ENABLE_VULKAN_RENDERDOC_CAPTURE", enable ? "1" : "0"));
Process::ApplyEnvironmentModification();
}
void OptionsUpdated(const char *libName)
{
if(RenderDoc::Inst().GetCaptureOptions().APIValidation)
{
Process::RegisterEnvironmentModification(Process::EnvironmentModification(
Process::eEnvModification_AppendPlatform, "VK_INSTANCE_LAYERS",
"VK_LAYER_LUNARG_standard_validation"));
Process::RegisterEnvironmentModification(Process::EnvironmentModification(
Process::eEnvModification_AppendPlatform, "VK_DEVICE_LAYERS",
"VK_LAYER_LUNARG_standard_validation"));
}
else
{
// can't disable if APIValidation is not set
}
Process::ApplyEnvironmentModification();
}
static VulkanHook vkhooks;
};
VulkanHook VulkanHook::vkhooks;
// RenderDoc State
// RenderDoc Intercepts, these must all be entry points with a dispatchable object
// as the first parameter
#define HookDefine1(ret, function, t1, p1) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1) { return CoreDisp(p1)->function(p1); }
#define HookDefine2(ret, function, t1, p1, t2, p2) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2) { return CoreDisp(p1)->function(p1, p2); }
#define HookDefine3(ret, function, t1, p1, t2, p2, t3, p3) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3) \
{ \
return CoreDisp(p1)->function(p1, p2, p3); \
}
#define HookDefine4(ret, function, t1, p1, t2, p2, t3, p3, t4, p4) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3, t4 p4) \
{ \
return CoreDisp(p1)->function(p1, p2, p3, p4); \
}
#define HookDefine5(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5) \
{ \
return CoreDisp(p1)->function(p1, p2, p3, p4, p5); \
}
#define HookDefine6(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6) \
{ \
return CoreDisp(p1)->function(p1, p2, p3, p4, p5, p6); \
}
#define HookDefine7(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7) \
{ \
return CoreDisp(p1)->function(p1, p2, p3, p4, p5, p6, p7); \
}
#define HookDefine8(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8) \
{ \
return CoreDisp(p1)->function(p1, p2, p3, p4, p5, p6, p7, p8); \
}
#define HookDefine9(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8, \
t9, p9) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, \
t9, p9) \
{ \
return CoreDisp(p1)->function(p1, p2, p3, p4, p5, p6, p7, p8, p9); \
}
#define HookDefine10(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, \
p8, t9, p9, t10, p10) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, \
t9 p9, t10 p10) \
{ \
return CoreDisp(p1)->function(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); \
}
#define HookDefine11(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, \
p8, t9, p9, t10, p10, t11, p11) \
ret VKAPI_CALL CONCAT(hooked_, function)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, \
t9 p9, t10 p10, t11 p11) \
{ \
return CoreDisp(p1)->function(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); \
}
DefineHooks();
// need to implement vkCreateInstance and vkDestroyInstance specially,
// to create and destroy the core WrappedVulkan object
VkResult VKAPI_CALL hooked_vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
VkInstance *pInstance)
{
WrappedVulkan *core = new WrappedVulkan("");
return core->vkCreateInstance(pCreateInfo, pAllocator, pInstance);
}
void VKAPI_CALL hooked_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator)
{
WrappedVulkan *core = CoreDisp(instance);
core->vkDestroyInstance(instance, pAllocator);
delete core;
}
// Layer Intercepts
#if ENABLED(RDOC_WIN32) && DISABLED(RDOC_X64)
// Win32 __stdcall will still mangle even with extern "C", set up aliases
#pragma comment( \
linker, \
"/EXPORT:VK_LAYER_RENDERDOC_CaptureEnumerateDeviceLayerProperties=_VK_LAYER_RENDERDOC_CaptureEnumerateDeviceLayerProperties@12")
#pragma comment( \
linker, \
"/EXPORT:VK_LAYER_RENDERDOC_CaptureEnumerateDeviceExtensionProperties=_VK_LAYER_RENDERDOC_CaptureEnumerateDeviceExtensionProperties@16")
#pragma comment( \
linker, \
"/EXPORT:VK_LAYER_RENDERDOC_CaptureGetDeviceProcAddr=_VK_LAYER_RENDERDOC_CaptureGetDeviceProcAddr@8")
#pragma comment( \
linker, \
"/EXPORT:VK_LAYER_RENDERDOC_CaptureGetInstanceProcAddr=_VK_LAYER_RENDERDOC_CaptureGetInstanceProcAddr@8")
#endif
extern "C" {
VK_LAYER_EXPORT VkResult VKAPI_CALL VK_LAYER_RENDERDOC_CaptureEnumerateDeviceLayerProperties(
VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount, VkLayerProperties *pProperties)
{
// must have a property count, either to fill out or use as a size
if(pPropertyCount == NULL)
return VK_INCOMPLETE;
// if we're not writing the properties, just say we have one layer
if(pProperties == NULL)
{
*pPropertyCount = 1;
return VK_SUCCESS;
}
else
{
// if the property count is somehow zero, return incomplete
if(*pPropertyCount == 0)
return VK_INCOMPLETE;
const VkLayerProperties layerProperties = {
RENDERDOC_LAYER_NAME, VK_API_VERSION_1_0,
VK_MAKE_VERSION(RENDERDOC_VERSION_MAJOR, RENDERDOC_VERSION_MINOR, 0),
"Debugging capture layer for RenderDoc",
};
// set the one layer property
*pProperties = layerProperties;
return VK_SUCCESS;
}
}
VK_LAYER_EXPORT VkResult VKAPI_CALL VK_LAYER_RENDERDOC_CaptureEnumerateDeviceExtensionProperties(
VkPhysicalDevice physicalDevice, const char *pLayerName, uint32_t *pPropertyCount,
VkExtensionProperties *pProperties)
{
// if pLayerName is NULL or not ours we're calling down through the layer chain to the ICD.
// This is our chance to filter out any reported extensions that we don't support
if(physicalDevice != NULL && (pLayerName == NULL || strcmp(pLayerName, RENDERDOC_LAYER_NAME)))
return CoreDisp(physicalDevice)
->FilterDeviceExtensionProperties(physicalDevice, pPropertyCount, pProperties);
return WrappedVulkan::GetProvidedExtensionProperties(pPropertyCount, pProperties);
}
#undef HookInit
#define HookInit(function) \
if(!strcmp(pName, STRINGIZE(CONCAT(vk, function)))) \
return (PFN_vkVoidFunction)&CONCAT(hooked_vk, function);
#undef HookInitExtension
#define HookInitExtension(ext, function) \
if(!strcmp(pName, STRINGIZE(CONCAT(vk, function)))) \
{ \
if(instDevInfo->ext_##ext) \
return (PFN_vkVoidFunction)&CONCAT(hooked_vk, function); \
}
// proc addr routines
VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL
VK_LAYER_RENDERDOC_CaptureGetDeviceProcAddr(VkDevice device, const char *pName)
{
if(!strcmp("vkGetDeviceProcAddr", pName))
return (PFN_vkVoidFunction)&VK_LAYER_RENDERDOC_CaptureGetDeviceProcAddr;
if(!strcmp("vkCreateDevice", pName))
return (PFN_vkVoidFunction)&hooked_vkCreateDevice;
if(!strcmp("vkDestroyDevice", pName))
return (PFN_vkVoidFunction)&hooked_vkDestroyDevice;
HookInitVulkanDevice();
if(device == VK_NULL_HANDLE)
return NULL;
InstanceDeviceInfo *instDevInfo = GetRecord(device)->instDevInfo;
HookInitVulkanDeviceExts();
if(GetDeviceDispatchTable(device)->GetDeviceProcAddr == NULL)
return NULL;
return GetDeviceDispatchTable(device)->GetDeviceProcAddr(Unwrap(device), pName);
}
VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL
VK_LAYER_RENDERDOC_CaptureGetInstanceProcAddr(VkInstance instance, const char *pName)
{
if(!strcmp("vkGetInstanceProcAddr", pName))
return (PFN_vkVoidFunction)&VK_LAYER_RENDERDOC_CaptureGetInstanceProcAddr;
if(!strcmp("vkEnumerateDeviceLayerProperties", pName))
return (PFN_vkVoidFunction)&VK_LAYER_RENDERDOC_CaptureEnumerateDeviceLayerProperties;
if(!strcmp("vkEnumerateDeviceExtensionProperties", pName))
return (PFN_vkVoidFunction)&VK_LAYER_RENDERDOC_CaptureEnumerateDeviceExtensionProperties;
if(!strcmp("vkGetDeviceProcAddr", pName))
return (PFN_vkVoidFunction)&VK_LAYER_RENDERDOC_CaptureGetDeviceProcAddr;
if(!strcmp("vkCreateDevice", pName))
return (PFN_vkVoidFunction)&hooked_vkCreateDevice;
if(!strcmp("vkDestroyDevice", pName))
return (PFN_vkVoidFunction)&hooked_vkDestroyDevice;
HookInitVulkanInstance();
if(instance == VK_NULL_HANDLE)
return NULL;
InstanceDeviceInfo *instDevInfo = GetRecord(instance)->instDevInfo;
HookInitVulkanInstanceExts();
// GetInstanceProcAddr must also unconditionally return all device functions
#undef HookInitExtension
#define HookInitExtension(ext, function) \
if(!strcmp(pName, STRINGIZE(CONCAT(vk, function)))) \
return (PFN_vkVoidFunction)&CONCAT(hooked_vk, function);
HookInitVulkanDevice();
HookInitVulkanDeviceExts();
if(GetInstanceDispatchTable(instance)->GetInstanceProcAddr == NULL)
return NULL;
return GetInstanceDispatchTable(instance)->GetInstanceProcAddr(Unwrap(instance), pName);
}
}