blob: 0742cb212432426f87ad7af17de46af5052ca879 [file] [log] [blame]
Jon Ashburn79113cc2014-12-01 14:22:40 -07001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan
Jon Ashburn79113cc2014-12-01 14:22:40 -07003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Jon Ashburn1fec4242014-11-26 11:10:26 -070024#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <unordered_map>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070028#include "loader_platform.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060029#include "vk_dispatch_table_helper.h"
30#include "vkLayer.h"
Ian Elliott655cad72015-02-12 17:08:34 -070031// The following is #included again to catch certain OS-specific functions
32// being used:
33#include "loader_platform.h"
Jon Ashburn1fec4242014-11-26 11:10:26 -070034
Jon Ashburnbacb0f52015-04-06 10:58:22 -060035static std::unordered_map<void *, VkLayerDispatchTable *> tableMap;
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -060036static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap;
37
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -060038/* Various dispatchable objects will use the same underlying dispatch table if they
39 * are created from that "parent" object. Thus use pointer to dispatch table
40 * as the key to these table maps.
41 * Instance -> PhysicalDevice
42 * Device -> CmdBuffer or Queue
43 * If use the object themselves as key to map then implies Create entrypoints have to be intercepted
44 * and a new key inserted into map */
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -060045static VkLayerInstanceDispatchTable * initLayerInstanceTable(const VkBaseLayerObject *instancew)
46{
47 VkLayerInstanceDispatchTable *pTable;
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -060048 assert(instancew);
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -060049 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instancew->baseObject;
50
51 std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap.find((void *) *ppDisp);
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -060052 if (it == tableInstanceMap.end())
53 {
54 pTable = new VkLayerInstanceDispatchTable;
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -060055 tableInstanceMap[(void *) *ppDisp] = pTable;
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -060056 } else
57 {
58 return it->second;
59 }
60
61 layer_init_instance_dispatch_table(pTable, (PFN_vkGetInstanceProcAddr) instancew->pGPA, (VkInstance) instancew->nextObject);
62
63 return pTable;
64}
Jon Ashburn1fec4242014-11-26 11:10:26 -070065
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -060066static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *devw)
Jon Ashburn1fec4242014-11-26 11:10:26 -070067{
Jon Ashburnbacb0f52015-04-06 10:58:22 -060068 VkLayerDispatchTable *pTable;
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -060069 assert(devw);
70 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) (devw->baseObject);
Jon Ashburn1fec4242014-11-26 11:10:26 -070071
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -060072 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) *ppDisp);
Jon Ashburn1fec4242014-11-26 11:10:26 -070073 if (it == tableMap.end())
74 {
Jon Ashburnbacb0f52015-04-06 10:58:22 -060075 pTable = new VkLayerDispatchTable;
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -060076 tableMap[(void *) *ppDisp] = pTable;
Jon Ashburn1fec4242014-11-26 11:10:26 -070077 } else
78 {
79 return it->second;
80 }
Chia-I Wuaa4121f2015-01-04 23:11:43 +080081
Jon Ashburn8d1b0b52015-05-18 13:20:15 -060082 layer_initialize_dispatch_table(pTable, (PFN_vkGetDeviceProcAddr) devw->pGPA, (VkDevice) devw->nextObject);
Chia-I Wuaa4121f2015-01-04 23:11:43 +080083
Jon Ashburn1fec4242014-11-26 11:10:26 -070084 return pTable;
85}
86
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060087VK_LAYER_EXPORT VkResult VKAPI vkLayerExtension1(VkDevice device)
Jon Ashburn1fec4242014-11-26 11:10:26 -070088{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060089 printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device);
90 printf("vkLayerExtension1 returning SUCCESS\n");
91 return VK_SUCCESS;
Jon Ashburn1fec4242014-11-26 11:10:26 -070092}
93
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060094#define BASIC_LAYER_EXT_ARRAY_SIZE 2
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060095
96static const VkExtensionProperties basicExts[BASIC_LAYER_EXT_ARRAY_SIZE] = {
97 {
98 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
99 "Basic",
100 0x10,
101 "Sample layer: Basic ",
102// 0,
103// NULL,
104 },
105 {
106 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
107 "vkLayerExtension1",
108 0x10,
109 "Sample layer: Basic",
110// 0,
111// NULL,
112 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600113};
114
115VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
116 VkExtensionInfoType infoType,
117 uint32_t extensionIndex,
118 size_t* pDataSize,
119 void* pData)
120{
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600121 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600122 uint32_t *count;
123
124 if (pDataSize == NULL)
125 return VK_ERROR_INVALID_POINTER;
126
127 switch (infoType) {
128 case VK_EXTENSION_INFO_TYPE_COUNT:
129 *pDataSize = sizeof(uint32_t);
130 if (pData == NULL)
131 return VK_SUCCESS;
132 count = (uint32_t *) pData;
133 *count = BASIC_LAYER_EXT_ARRAY_SIZE;
134 break;
135 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
136 *pDataSize = sizeof(VkExtensionProperties);
137 if (pData == NULL)
138 return VK_SUCCESS;
139 if (extensionIndex >= BASIC_LAYER_EXT_ARRAY_SIZE)
140 return VK_ERROR_INVALID_VALUE;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600141 memcpy((VkExtensionProperties *) pData, &basicExts[extensionIndex], sizeof(VkExtensionProperties));
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600142 break;
143 default:
144 return VK_ERROR_INVALID_VALUE;
145 };
146
147 return VK_SUCCESS;
148}
149
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600150VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
151 VkInstance instance,
152 uint32_t* pPhysicalDeviceCount,
153 VkPhysicalDevice* pPhysicalDevices)
154{
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -0600155 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instance;
156 VkLayerInstanceDispatchTable *pInstTable = tableInstanceMap[*ppDisp];
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600157 printf("At start of wrapped vkEnumeratePhysicalDevices() call w/ inst: %p\n", (void*)instance);
158 VkResult result = pInstTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600159 printf("Completed wrapped vkEnumeratePhysicalDevices() call w/ count %u\n", *pPhysicalDeviceCount);
160 return result;
161}
162
Tony Barbourd1c35722015-04-16 15:59:00 -0600163VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
Jon Ashburn1fec4242014-11-26 11:10:26 -0700164{
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -0600165 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) gpu;
166 VkLayerInstanceDispatchTable* pInstTable = tableInstanceMap[*ppDisp];
Jon Ashburn1fec4242014-11-26 11:10:26 -0700167
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600168 printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600169 VkResult result = pInstTable->CreateDevice(gpu, pCreateInfo, pDevice);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600170 printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
Jon Ashburn1fec4242014-11-26 11:10:26 -0700171 return result;
172}
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600173
Jon Ashburn9a8a2e22015-05-19 16:34:53 -0600174/* hook DestroyDevice to remove tableMap entry */
175VK_LAYER_EXPORT VkResult VKAPI vkDestroyDevice(VkDevice device)
176{
177 VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) device;
178 VkLayerDispatchTable *pTable = tableMap[pDisp];
179 VkResult res = pTable->DestroyDevice(device);
180 tableMap.erase(pDisp);
181 return res;
182}
183
184/* hook DestroyInstance to remove tableInstanceMap entry */
185VK_LAYER_EXPORT VkResult VKAPI vkDestroyInstance(VkInstance instance)
186{
187 VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) instance;
188 VkLayerInstanceDispatchTable *pTable = tableInstanceMap[pDisp];
189 VkResult res = pTable->DestroyInstance(instance);
190 tableInstanceMap.erase(pDisp);
191 return res;
192}
193
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600194VK_LAYER_EXPORT VkResult VKAPI vkGetFormatInfo(VkDevice device, VkFormat format, VkFormatInfoType infoType, size_t* pDataSize, void* pData)
Jon Ashburn1fec4242014-11-26 11:10:26 -0700195{
Jon Ashburnfe7ff9c2015-05-15 16:40:25 -0600196 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
197 VkLayerDispatchTable* pTable = tableMap[*ppDisp];
Jon Ashburn1fec4242014-11-26 11:10:26 -0700198
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600199 printf("At start of wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600200 VkResult result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600201 printf("Completed wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
Jon Ashburn1fec4242014-11-26 11:10:26 -0700202 return result;
203}
204
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600205VK_LAYER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char* pName)
Jon Ashburn79113cc2014-12-01 14:22:40 -0700206{
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600207 if (device == NULL)
Jon Ashburn1fec4242014-11-26 11:10:26 -0700208 return NULL;
Chia-I Wub665d942015-01-05 09:41:27 +0800209
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600210 initLayerTable((const VkBaseLayerObject *) device);
Chia-I Wub665d942015-01-05 09:41:27 +0800211
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600212 if (!strcmp("vkGetDeviceProcAddr", pName))
213 return (void *) vkGetDeviceProcAddr;
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600214 if (!strcmp("vkGetFormatInfo", pName))
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600215 return (void *) vkGetFormatInfo;
Jon Ashburn9a8a2e22015-05-19 16:34:53 -0600216 if (!strcmp("vkDestroyDevice", pName))
217 return (void *) vkDestroyDevice;
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600218 if (!strcmp("vkLayerExtension1", pName))
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600219 return (void *) vkLayerExtension1;
Jon Ashburn1fec4242014-11-26 11:10:26 -0700220 else {
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600221 VkBaseLayerObject* devw = (VkBaseLayerObject *) device;
222 if (devw->pGPA == NULL)
Jon Ashburn1fec4242014-11-26 11:10:26 -0700223 return NULL;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600224 return devw->pGPA((VkObject) devw->nextObject, pName);
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600225 }
226}
227
228VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* pName)
229{
230 if (instance == NULL)
231 return NULL;
232
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600233 initLayerInstanceTable((const VkBaseLayerObject *) instance);
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600234
235 if (!strcmp("vkGetInstanceProcAddr", pName))
236 return (void *) vkGetInstanceProcAddr;
Jon Ashburn9a8a2e22015-05-19 16:34:53 -0600237 if (!strcmp("vkDestroyInstance", pName))
238 return (void *) vkDestroyInstance;
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600239 if (!strcmp("vkEnumeratePhysicalDevices", pName))
240 return (void*) vkEnumeratePhysicalDevices;
241 if (!strcmp("vkGetGlobalExtensionInfo", pName))
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600242 return (void*) vkGetGlobalExtensionInfo;
243 if (!strcmp("vkCreateDevice", pName))
244 return (void *) vkCreateDevice;
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600245 else {
246 VkBaseLayerObject* instancew = (VkBaseLayerObject *) instance;
247 if (instancew->pGPA == NULL)
248 return NULL;
249 return instancew->pGPA((VkObject) instancew->nextObject, pName);
Jon Ashburn1fec4242014-11-26 11:10:26 -0700250 }
251}