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
|
/*
* Vulkan
*
* Copyright (C) 2014 LunarG, Inc.
*
* 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 <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unordered_map>
#include "loader_platform.h"
#include "vk_dispatch_table_helper.h"
#include "vkLayer.h"
// The following is #included again to catch certain OS-specific functions
// being used:
#include "loader_platform.h"
static std::unordered_map<void *, VkLayerDispatchTable *> tableMap;
static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap;
/* Various dispatchable objects will use the same underlying dispatch table if they
* are created from that "parent" object. Thus use pointer to dispatch table
* as the key to these table maps.
* Instance -> PhysicalDevice
* Device -> CmdBuffer or Queue
* If use the object themselves as key to map then implies Create entrypoints have to be intercepted
* and a new key inserted into map */
static VkLayerInstanceDispatchTable * initLayerInstanceTable(const VkBaseLayerObject *instancew)
{
VkLayerInstanceDispatchTable *pTable;
assert(instancew);
VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instancew->baseObject;
std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap.find((void *) *ppDisp);
if (it == tableInstanceMap.end())
{
pTable = new VkLayerInstanceDispatchTable;
tableInstanceMap[(void *) *ppDisp] = pTable;
} else
{
return it->second;
}
layer_init_instance_dispatch_table(pTable, (PFN_vkGetInstanceProcAddr) instancew->pGPA, (VkInstance) instancew->nextObject);
return pTable;
}
static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *devw)
{
VkLayerDispatchTable *pTable;
assert(devw);
VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) (devw->baseObject);
std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) *ppDisp);
if (it == tableMap.end())
{
pTable = new VkLayerDispatchTable;
tableMap[(void *) *ppDisp] = pTable;
} else
{
return it->second;
}
layer_initialize_dispatch_table(pTable, (PFN_vkGetDeviceProcAddr) devw->pGPA, (VkDevice) devw->nextObject);
return pTable;
}
VK_LAYER_EXPORT VkResult VKAPI vkLayerExtension1(VkDevice device)
{
printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device);
printf("vkLayerExtension1 returning SUCCESS\n");
return VK_SUCCESS;
}
struct extProps {
uint32_t version;
const char * const name;
};
#define BASIC_LAYER_EXT_ARRAY_SIZE 2
static const struct extProps basicExts[BASIC_LAYER_EXT_ARRAY_SIZE] = {
// TODO what is the version?
0x10, "Basic",
0x10, "vkLayerExtension1"
};
VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
VkExtensionInfoType infoType,
uint32_t extensionIndex,
size_t* pDataSize,
void* pData)
{
/* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
VkExtensionProperties *ext_props;
uint32_t *count;
if (pDataSize == NULL)
return VK_ERROR_INVALID_POINTER;
switch (infoType) {
case VK_EXTENSION_INFO_TYPE_COUNT:
*pDataSize = sizeof(uint32_t);
if (pData == NULL)
return VK_SUCCESS;
count = (uint32_t *) pData;
*count = BASIC_LAYER_EXT_ARRAY_SIZE;
break;
case VK_EXTENSION_INFO_TYPE_PROPERTIES:
*pDataSize = sizeof(VkExtensionProperties);
if (pData == NULL)
return VK_SUCCESS;
if (extensionIndex >= BASIC_LAYER_EXT_ARRAY_SIZE)
return VK_ERROR_INVALID_VALUE;
ext_props = (VkExtensionProperties *) pData;
ext_props->version = basicExts[extensionIndex].version;
strncpy(ext_props->extName, basicExts[extensionIndex].name,
VK_MAX_EXTENSION_NAME);
ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
break;
default:
return VK_ERROR_INVALID_VALUE;
};
return VK_SUCCESS;
}
VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
VkInstance instance,
uint32_t* pPhysicalDeviceCount,
VkPhysicalDevice* pPhysicalDevices)
{
VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instance;
VkLayerInstanceDispatchTable *pInstTable = tableInstanceMap[*ppDisp];
printf("At start of wrapped vkEnumeratePhysicalDevices() call w/ inst: %p\n", (void*)instance);
VkResult result = pInstTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
printf("Completed wrapped vkEnumeratePhysicalDevices() call w/ count %u\n", *pPhysicalDeviceCount);
return result;
}
VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
{
VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) gpu;
VkLayerInstanceDispatchTable* pInstTable = tableInstanceMap[*ppDisp];
printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu);
VkResult result = pInstTable->CreateDevice(gpu, pCreateInfo, pDevice);
printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
return result;
}
VK_LAYER_EXPORT VkResult VKAPI vkGetFormatInfo(VkDevice device, VkFormat format, VkFormatInfoType infoType, size_t* pDataSize, void* pData)
{
VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
VkLayerDispatchTable* pTable = tableMap[*ppDisp];
printf("At start of wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
VkResult result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
printf("Completed wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
return result;
}
VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved)
{
if (gpu != NULL)
{
VkLayerInstanceDispatchTable* pTable = initLayerInstanceTable((const VkBaseLayerObject *) gpu);
printf("At start of wrapped vkEnumerateLayers() call w/ gpu: %p\n", gpu);
VkResult result = pTable->EnumerateLayers(gpu, maxStringSize, pLayerCount, pOutLayers, pReserved);
return result;
} else
{
if (pLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL)
return VK_ERROR_INVALID_POINTER;
// Example of a layer that is only compatible with Intel's GPUs
VkBaseLayerObject* gpuw = (VkBaseLayerObject*) pReserved;
PFN_vkGetPhysicalDeviceInfo fpGetGpuInfo;
VkPhysicalDeviceProperties gpuProps;
size_t dataSize = sizeof(VkPhysicalDeviceProperties);
fpGetGpuInfo = (PFN_vkGetPhysicalDeviceInfo) gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, "vkGetPhysicalDeviceInfo");
fpGetGpuInfo((VkPhysicalDevice) gpuw->nextObject, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES, &dataSize, &gpuProps);
if (gpuProps.vendorId == 0x8086)
{
*pLayerCount = 1;
strncpy((char *) pOutLayers[0], "Basic", maxStringSize);
} else
{
*pLayerCount = 0;
}
return VK_SUCCESS;
}
}
VK_LAYER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char* pName)
{
if (device == NULL)
return NULL;
initLayerTable((const VkBaseLayerObject *) device);
if (!strcmp("vkGetDeviceProcAddr", pName))
return (void *) vkGetDeviceProcAddr;
if (!strcmp("vkGetFormatInfo", pName))
return (void *) vkGetFormatInfo;
if (!strcmp("vkLayerExtension1", pName))
return (void *) vkLayerExtension1;
else {
VkBaseLayerObject* devw = (VkBaseLayerObject *) device;
if (devw->pGPA == NULL)
return NULL;
return devw->pGPA((VkObject) devw->nextObject, pName);
}
}
VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* pName)
{
if (instance == NULL)
return NULL;
initLayerInstanceTable((const VkBaseLayerObject *) instance);
if (!strcmp("vkGetInstanceProcAddr", pName))
return (void *) vkGetInstanceProcAddr;
if (!strcmp("vkEnumeratePhysicalDevices", pName))
return (void*) vkEnumeratePhysicalDevices;
if (!strcmp("vkGetGlobalExtensionInfo", pName))
return (void*) vkGetGlobalExtensionInfo;
if (!strcmp("vkCreateDevice", pName))
return (void *) vkCreateDevice;
if (!strcmp("vkEnumerateLayers", pName))
return (void *) vkEnumerateLayers;
else {
VkBaseLayerObject* instancew = (VkBaseLayerObject *) instance;
if (instancew->pGPA == NULL)
return NULL;
return instancew->pGPA((VkObject) instancew->nextObject, pName);
}
}
|