Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 3 | * |
| 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 Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <assert.h> |
| 27 | #include <unordered_map> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 28 | #include "loader_platform.h" |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 29 | #include "vk_dispatch_table_helper.h" |
| 30 | #include "vkLayer.h" |
Ian Elliott | 655cad7 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 31 | // The following is #included again to catch certain OS-specific functions |
| 32 | // being used: |
| 33 | #include "loader_platform.h" |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 34 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 35 | static std::unordered_map<void *, VkLayerDispatchTable *> tableMap; |
Jon Ashburn | 8c5cbcf | 2015-05-07 10:27:37 -0600 | [diff] [blame^] | 36 | static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap; |
| 37 | |
| 38 | static VkLayerInstanceDispatchTable * initLayerInstanceTable(const VkBaseLayerObject *instancew) |
| 39 | { |
| 40 | VkLayerInstanceDispatchTable *pTable; |
| 41 | |
| 42 | assert(instancew); |
| 43 | std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap.find((void *) instancew->baseObject); |
| 44 | if (it == tableInstanceMap.end()) |
| 45 | { |
| 46 | pTable = new VkLayerInstanceDispatchTable; |
| 47 | tableInstanceMap[(void *) instancew->baseObject] = pTable; |
| 48 | } else |
| 49 | { |
| 50 | return it->second; |
| 51 | } |
| 52 | |
| 53 | layer_init_instance_dispatch_table(pTable, (PFN_vkGetInstanceProcAddr) instancew->pGPA, (VkInstance) instancew->nextObject); |
| 54 | |
| 55 | return pTable; |
| 56 | } |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 57 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 58 | static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *gpuw) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 59 | { |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 60 | VkLayerDispatchTable *pTable; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 61 | |
| 62 | assert(gpuw); |
Jon Ashburn | 4d9f465 | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 63 | std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) gpuw->baseObject); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 64 | if (it == tableMap.end()) |
| 65 | { |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 66 | pTable = new VkLayerDispatchTable; |
Jon Ashburn | 4d9f465 | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 67 | tableMap[(void *) gpuw->baseObject] = pTable; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 68 | } else |
| 69 | { |
| 70 | return it->second; |
| 71 | } |
Chia-I Wu | aa4121f | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 72 | |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 73 | layer_initialize_dispatch_table(pTable, (PFN_vkGetProcAddr) gpuw->pGPA, (VkPhysicalDevice) gpuw->nextObject); |
Chia-I Wu | aa4121f | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 74 | |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 75 | return pTable; |
| 76 | } |
| 77 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 78 | VK_LAYER_EXPORT VkResult VKAPI vkLayerExtension1(VkDevice device) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 79 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 80 | printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device); |
| 81 | printf("vkLayerExtension1 returning SUCCESS\n"); |
| 82 | return VK_SUCCESS; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 85 | struct extProps { |
| 86 | uint32_t version; |
| 87 | const char * const name; |
| 88 | }; |
| 89 | #define BASIC_LAYER_EXT_ARRAY_SIZE 2 |
| 90 | static const struct extProps basicExts[BASIC_LAYER_EXT_ARRAY_SIZE] = { |
| 91 | // TODO what is the version? |
| 92 | 0x10, "Basic", |
| 93 | 0x10, "vkLayerExtension1" |
| 94 | }; |
| 95 | |
| 96 | VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo( |
| 97 | VkExtensionInfoType infoType, |
| 98 | uint32_t extensionIndex, |
| 99 | size_t* pDataSize, |
| 100 | void* pData) |
| 101 | { |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 102 | /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */ |
| 103 | VkExtensionProperties *ext_props; |
| 104 | uint32_t *count; |
| 105 | |
| 106 | if (pDataSize == NULL) |
| 107 | return VK_ERROR_INVALID_POINTER; |
| 108 | |
| 109 | switch (infoType) { |
| 110 | case VK_EXTENSION_INFO_TYPE_COUNT: |
| 111 | *pDataSize = sizeof(uint32_t); |
| 112 | if (pData == NULL) |
| 113 | return VK_SUCCESS; |
| 114 | count = (uint32_t *) pData; |
| 115 | *count = BASIC_LAYER_EXT_ARRAY_SIZE; |
| 116 | break; |
| 117 | case VK_EXTENSION_INFO_TYPE_PROPERTIES: |
| 118 | *pDataSize = sizeof(VkExtensionProperties); |
| 119 | if (pData == NULL) |
| 120 | return VK_SUCCESS; |
| 121 | if (extensionIndex >= BASIC_LAYER_EXT_ARRAY_SIZE) |
| 122 | return VK_ERROR_INVALID_VALUE; |
| 123 | ext_props = (VkExtensionProperties *) pData; |
| 124 | ext_props->version = basicExts[extensionIndex].version; |
| 125 | strncpy(ext_props->extName, basicExts[extensionIndex].name, |
| 126 | VK_MAX_EXTENSION_NAME); |
| 127 | ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0'; |
| 128 | break; |
| 129 | default: |
| 130 | return VK_ERROR_INVALID_VALUE; |
| 131 | }; |
| 132 | |
| 133 | return VK_SUCCESS; |
| 134 | } |
| 135 | |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 136 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 137 | { |
Jon Ashburn | 4d9f465 | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 138 | VkLayerDispatchTable* pTable = tableMap[gpu]; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 139 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 140 | printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu); |
Jon Ashburn | 4d9f465 | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 141 | VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 142 | // create a mapping for the device object into the dispatch table |
| 143 | tableMap.emplace(*pDevice, pTable); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 144 | printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 145 | return result; |
| 146 | } |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 147 | VK_LAYER_EXPORT VkResult VKAPI vkGetFormatInfo(VkDevice device, VkFormat format, VkFormatInfoType infoType, size_t* pDataSize, void* pData) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 148 | { |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 149 | VkLayerDispatchTable* pTable = tableMap[device]; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 150 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 151 | printf("At start of wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device); |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 152 | VkResult result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 153 | printf("Completed wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 154 | return result; |
| 155 | } |
| 156 | |
Courtney Goeltzenleuchter | d9dc0c7 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 157 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 158 | { |
| 159 | if (gpu != NULL) |
| 160 | { |
Jon Ashburn | 4d9f465 | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 161 | VkLayerDispatchTable* pTable = initLayerTable((const VkBaseLayerObject *) gpu); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 162 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 163 | printf("At start of wrapped vkEnumerateLayers() call w/ gpu: %p\n", gpu); |
Courtney Goeltzenleuchter | d9dc0c7 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 164 | VkResult result = pTable->EnumerateLayers(gpu, maxStringSize, pLayerCount, pOutLayers, pReserved); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 165 | return result; |
| 166 | } else |
| 167 | { |
Courtney Goeltzenleuchter | d9dc0c7 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 168 | if (pLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 169 | return VK_ERROR_INVALID_POINTER; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 170 | |
| 171 | // Example of a layer that is only compatible with Intel's GPUs |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 172 | VkBaseLayerObject* gpuw = (VkBaseLayerObject*) pReserved; |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 173 | PFN_vkGetPhysicalDeviceInfo fpGetGpuInfo; |
| 174 | VkPhysicalDeviceProperties gpuProps; |
| 175 | size_t dataSize = sizeof(VkPhysicalDeviceProperties); |
| 176 | fpGetGpuInfo = (PFN_vkGetPhysicalDeviceInfo) gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, "vkGetPhysicalDeviceInfo"); |
| 177 | fpGetGpuInfo((VkPhysicalDevice) gpuw->nextObject, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES, &dataSize, &gpuProps); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 178 | if (gpuProps.vendorId == 0x8086) |
| 179 | { |
Courtney Goeltzenleuchter | d9dc0c7 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 180 | *pLayerCount = 1; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 181 | strncpy((char *) pOutLayers[0], "Basic", maxStringSize); |
| 182 | } else |
| 183 | { |
Courtney Goeltzenleuchter | d9dc0c7 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 184 | *pLayerCount = 0; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 185 | } |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 186 | return VK_SUCCESS; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 190 | VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char* pName) |
Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 191 | { |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 192 | if (gpu == NULL) |
| 193 | return NULL; |
Chia-I Wu | b665d94 | 2015-01-05 09:41:27 +0800 | [diff] [blame] | 194 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 195 | initLayerTable((const VkBaseLayerObject *) gpu); |
Chia-I Wu | b665d94 | 2015-01-05 09:41:27 +0800 | [diff] [blame] | 196 | |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 197 | if (!strcmp("vkGetProcAddr", pName)) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 198 | return (void *) vkGetProcAddr; |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 199 | if (!strcmp("vkCreateDevice", pName)) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 200 | return (void *) vkCreateDevice; |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 201 | if (!strcmp("GetGlobalExtensionInfo", pName)) |
| 202 | return (void*) vkGetGlobalExtensionInfo; |
| 203 | if (!strcmp("vkEnumerateLayers", pName)) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 204 | return (void *) vkEnumerateLayers; |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 205 | if (!strcmp("vkGetFormatInfo", pName)) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 206 | return (void *) vkGetFormatInfo; |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 207 | if (!strcmp("vkLayerExtension1", pName)) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 208 | return (void *) vkLayerExtension1; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 209 | else { |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 210 | VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 211 | if (gpuw->pGPA == NULL) |
| 212 | return NULL; |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 213 | return gpuw->pGPA((VkObject) gpuw->nextObject, pName); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* pName) |
| 218 | { |
| 219 | if (instance == NULL) |
| 220 | return NULL; |
| 221 | |
Jon Ashburn | 8c5cbcf | 2015-05-07 10:27:37 -0600 | [diff] [blame^] | 222 | initLayerInstanceTable((const VkBaseLayerObject *) instance); |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 223 | |
| 224 | if (!strcmp("vkGetInstanceProcAddr", pName)) |
| 225 | return (void *) vkGetInstanceProcAddr; |
| 226 | if (!strcmp("vkGetProcAddr", pName)) |
| 227 | return (void *) vkGetProcAddr; |
| 228 | if (!strcmp("GetGlobalExtensionInfo", pName)) |
| 229 | return (void*) vkGetGlobalExtensionInfo; |
| 230 | if (!strcmp("vkCreateDevice", pName)) |
| 231 | return (void *) vkCreateDevice; |
| 232 | if (!strcmp("vkEnumerateLayers", pName)) |
| 233 | return (void *) vkEnumerateLayers; |
| 234 | else { |
| 235 | VkBaseLayerObject* instancew = (VkBaseLayerObject *) instance; |
| 236 | if (instancew->pGPA == NULL) |
| 237 | return NULL; |
| 238 | return instancew->pGPA((VkObject) instancew->nextObject, pName); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 239 | } |
| 240 | } |