1/* 2 * Vulkan 3 * 4 * Copyright (C) 2015 Valve, Inc. 5 * Copyright (C) 2016 Google, Inc. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 */ 25#include <stdio.h> 26#include <stdlib.h> 27#include <string.h> 28#include <unordered_map> 29#include <list> 30 31#include "vk_loader_platform.h" 32#include "vulkan/vk_layer.h" 33#include "vk_layer_config.h" 34#include "vk_layer_extension_utils.h" 35#include "vk_layer_utils.h" 36#include "vk_enum_validate_helper.h" 37#include "vk_struct_validate_helper.h" 38#include "vk_layer_table.h" 39#include "vk_layer_logging.h" 40#include "threading.h" 41#include "vk_dispatch_table_helper.h" 42#include "vk_struct_string_helper_cpp.h" 43#include "vk_layer_data.h" 44#include "vk_layer_utils.h" 45 46#include "thread_check.h" 47 48static void initThreading(layer_data *my_data, const VkAllocationCallbacks *pAllocator) { 49 50 layer_debug_actions(my_data->report_data, my_data->logging_callback, pAllocator, "google_threading"); 51 52 if (!threadingLockInitialized) { 53 loader_platform_thread_create_mutex(&threadingLock); 54 loader_platform_thread_init_cond(&threadingCond); 55 threadingLockInitialized = 1; 56 } 57} 58 59VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL 60vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) { 61 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); 62 63 assert(chain_info->u.pLayerInfo); 64 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; 65 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance"); 66 if (fpCreateInstance == NULL) { 67 return VK_ERROR_INITIALIZATION_FAILED; 68 } 69 70 // Advance the link info for the next element on the chain 71 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; 72 73 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance); 74 if (result != VK_SUCCESS) 75 return result; 76 77 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map); 78 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable; 79 layer_init_instance_dispatch_table(*pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr); 80 81 my_data->report_data = debug_report_create_instance(my_data->instance_dispatch_table, *pInstance, 82 pCreateInfo->enabledExtensionCount, pCreateInfo->ppEnabledExtensionNames); 83 initThreading(my_data, pAllocator); 84 return result; 85} 86 87VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) { 88 dispatch_key key = get_dispatch_key(instance); 89 layer_data *my_data = get_my_data_ptr(key, layer_data_map); 90 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table; 91 startWriteObject(my_data, instance); 92 pTable->DestroyInstance(instance, pAllocator); 93 finishWriteObject(my_data, instance); 94 95 // Clean up logging callback, if any 96 while (my_data->logging_callback.size() > 0) { 97 VkDebugReportCallbackEXT callback = my_data->logging_callback.back(); 98 layer_destroy_msg_callback(my_data->report_data, callback, pAllocator); 99 my_data->logging_callback.pop_back(); 100 } 101 102 layer_debug_report_destroy_instance(my_data->report_data); 103 delete my_data->instance_dispatch_table; 104 layer_data_map.erase(key); 105 106 if (layer_data_map.empty()) { 107 // Release mutex when destroying last instance. 108 loader_platform_thread_delete_mutex(&threadingLock); 109 threadingLockInitialized = 0; 110 } 111} 112 113VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo, 114 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) { 115 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); 116 117 assert(chain_info->u.pLayerInfo); 118 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; 119 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr; 120 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice"); 121 if (fpCreateDevice == NULL) { 122 return VK_ERROR_INITIALIZATION_FAILED; 123 } 124 125 // Advance the link info for the next element on the chain 126 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; 127 128 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice); 129 if (result != VK_SUCCESS) { 130 return result; 131 } 132 133 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map); 134 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map); 135 136 // Setup device dispatch table 137 my_device_data->device_dispatch_table = new VkLayerDispatchTable; 138 layer_init_device_dispatch_table(*pDevice, my_device_data->device_dispatch_table, fpGetDeviceProcAddr); 139 140 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice); 141 return result; 142} 143 144VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) { 145 dispatch_key key = get_dispatch_key(device); 146 layer_data *dev_data = get_my_data_ptr(key, layer_data_map); 147 startWriteObject(dev_data, device); 148 dev_data->device_dispatch_table->DestroyDevice(device, pAllocator); 149 finishWriteObject(dev_data, device); 150 layer_data_map.erase(key); 151} 152 153static const VkExtensionProperties threading_extensions[] = { 154 {VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION}}; 155 156VK_LAYER_EXPORT VkResult VKAPI_CALL 157vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties *pProperties) { 158 return util_GetExtensionProperties(ARRAY_SIZE(threading_extensions), threading_extensions, pCount, pProperties); 159} 160 161static const VkLayerProperties globalLayerProps[] = {{ 162 "VK_LAYER_GOOGLE_threading", 163 VK_LAYER_API_VERSION, // specVersion 164 1, "Google Validation Layer", 165}}; 166 167VK_LAYER_EXPORT VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) { 168 return util_GetLayerProperties(ARRAY_SIZE(globalLayerProps), globalLayerProps, pCount, pProperties); 169} 170 171static const VkLayerProperties deviceLayerProps[] = {{ 172 "VK_LAYER_GOOGLE_threading", 173 VK_LAYER_API_VERSION, // specVersion 174 1, "Google Validation Layer", 175}}; 176 177VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, 178 const char *pLayerName, uint32_t *pCount, 179 VkExtensionProperties *pProperties) { 180 if (pLayerName == NULL) { 181 dispatch_key key = get_dispatch_key(physicalDevice); 182 layer_data *my_data = get_my_data_ptr(key, layer_data_map); 183 return my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties); 184 } else { 185 // Threading layer does not have any device extensions 186 return util_GetExtensionProperties(0, nullptr, pCount, pProperties); 187 } 188} 189 190VK_LAYER_EXPORT VkResult VKAPI_CALL 191vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties *pProperties) { 192 return util_GetLayerProperties(ARRAY_SIZE(deviceLayerProps), deviceLayerProps, pCount, pProperties); 193} 194 195static inline PFN_vkVoidFunction layer_intercept_proc(const char *name) { 196 for (int i = 0; i < sizeof(procmap) / sizeof(procmap[0]); i++) { 197 if (!strcmp(name, procmap[i].name)) 198 return procmap[i].pFunc; 199 } 200 return NULL; 201} 202 203static inline PFN_vkVoidFunction layer_intercept_instance_proc(const char *name) { 204 if (!name || name[0] != 'v' || name[1] != 'k') 205 return NULL; 206 207 name += 2; 208 if (!strcmp(name, "CreateInstance")) 209 return (PFN_vkVoidFunction)vkCreateInstance; 210 if (!strcmp(name, "DestroyInstance")) 211 return (PFN_vkVoidFunction)vkDestroyInstance; 212 if (!strcmp(name, "EnumerateInstanceExtensionProperties")) 213 return (PFN_vkVoidFunction)vkEnumerateInstanceExtensionProperties; 214 if (!strcmp(name, "EnumerateInstanceLayerProperties")) 215 return (PFN_vkVoidFunction)vkEnumerateInstanceLayerProperties; 216 if (!strcmp(name, "EnumerateDeviceExtensionProperties")) 217 return (PFN_vkVoidFunction)vkEnumerateDeviceExtensionProperties; 218 if (!strcmp(name, "EnumerateDeviceLayerProperties")) 219 return (PFN_vkVoidFunction)vkEnumerateDeviceLayerProperties; 220 if (!strcmp(name, "CreateDevice")) 221 return (PFN_vkVoidFunction)vkCreateDevice; 222 if (!strcmp(name, "GetInstanceProcAddr")) 223 return (PFN_vkVoidFunction)vkGetInstanceProcAddr; 224 225 return NULL; 226} 227 228VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char *funcName) { 229 PFN_vkVoidFunction addr; 230 layer_data *dev_data; 231 if (device == VK_NULL_HANDLE) { 232 return NULL; 233 } 234 235 addr = layer_intercept_proc(funcName); 236 if (addr) 237 return addr; 238 239 dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map); 240 VkLayerDispatchTable *pTable = dev_data->device_dispatch_table; 241 242 if (pTable->GetDeviceProcAddr == NULL) 243 return NULL; 244 return pTable->GetDeviceProcAddr(device, funcName); 245} 246 247VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) { 248 PFN_vkVoidFunction addr; 249 layer_data *my_data; 250 251 addr = layer_intercept_instance_proc(funcName); 252 if (addr) { 253 return addr; 254 } 255 256 if (instance == VK_NULL_HANDLE) { 257 return NULL; 258 } 259 260 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map); 261 addr = debug_report_get_instance_proc_addr(my_data->report_data, funcName); 262 if (addr) { 263 return addr; 264 } 265 266 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table; 267 if (pTable->GetInstanceProcAddr == NULL) { 268 return NULL; 269 } 270 return pTable->GetInstanceProcAddr(instance, funcName); 271} 272 273VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL 274vkCreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo, 275 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pMsgCallback) { 276 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map); 277 startReadObject(my_data, instance); 278 VkResult result = 279 my_data->instance_dispatch_table->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback); 280 if (VK_SUCCESS == result) { 281 result = layer_create_msg_callback(my_data->report_data, pCreateInfo, pAllocator, pMsgCallback); 282 } 283 finishReadObject(my_data, instance); 284 return result; 285} 286 287VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL 288vkDestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback, const VkAllocationCallbacks *pAllocator) { 289 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map); 290 startReadObject(my_data, instance); 291 startWriteObject(my_data, callback); 292 my_data->instance_dispatch_table->DestroyDebugReportCallbackEXT(instance, callback, pAllocator); 293 layer_destroy_msg_callback(my_data->report_data, callback, pAllocator); 294 finishReadObject(my_data, instance); 295 finishWriteObject(my_data, callback); 296} 297 298VkResult VKAPI_CALL 299vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo, VkCommandBuffer *pCommandBuffers) { 300 dispatch_key key = get_dispatch_key(device); 301 layer_data *my_data = get_my_data_ptr(key, layer_data_map); 302 VkLayerDispatchTable *pTable = my_data->device_dispatch_table; 303 VkResult result; 304 startReadObject(my_data, device); 305 startWriteObject(my_data, pAllocateInfo->commandPool); 306 307 result = pTable->AllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers); 308 finishReadObject(my_data, device); 309 finishWriteObject(my_data, pAllocateInfo->commandPool); 310 311 // Record mapping from command buffer to command pool 312 if (VK_SUCCESS == result) { 313 for (int index = 0; index < pAllocateInfo->commandBufferCount; index++) { 314 loader_platform_thread_lock_mutex(&threadingLock); 315 command_pool_map[pCommandBuffers[index]] = pAllocateInfo->commandPool; 316 loader_platform_thread_unlock_mutex(&threadingLock); 317 } 318 } 319 320 return result; 321} 322 323void VKAPI_CALL vkFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, 324 const VkCommandBuffer *pCommandBuffers) { 325 dispatch_key key = get_dispatch_key(device); 326 layer_data *my_data = get_my_data_ptr(key, layer_data_map); 327 VkLayerDispatchTable *pTable = my_data->device_dispatch_table; 328 const bool lockCommandPool = false; // pool is already directly locked 329 startReadObject(my_data, device); 330 startWriteObject(my_data, commandPool); 331 for (int index = 0; index < commandBufferCount; index++) { 332 startWriteObject(my_data, pCommandBuffers[index], lockCommandPool); 333 } 334 335 pTable->FreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers); 336 finishReadObject(my_data, device); 337 finishWriteObject(my_data, commandPool); 338 for (int index = 0; index < commandBufferCount; index++) { 339 finishWriteObject(my_data, pCommandBuffers[index], lockCommandPool); 340 loader_platform_thread_lock_mutex(&threadingLock); 341 command_pool_map.erase(pCommandBuffers[index]); 342 loader_platform_thread_unlock_mutex(&threadingLock); 343 } 344} 345