/* * * Copyright (C) 2015 Valve Corporation * Copyright (C) 2015 Google 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. * * Author: David Pinedo * Author: Jon Ashburn */ #include #include #include #include "vk_dispatch_table_helper.h" #include "vulkan/vk_layer.h" #include "vk_layer_table.h" #include "vk_layer_extension_utils.h" static const VkLayerProperties globalLayerProps[] = { { "VK_LAYER_LUNARG_basic", VK_API_VERSION, // specVersion 1, // implementationVersion "LunarG Sample Layer", } }; VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkLayerExtension1(VkDevice device) { printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device); printf("vkLayerExtension1 returning SUCCESS\n"); return VK_SUCCESS; } static const VkLayerProperties basic_physicaldevice_layers[] = { { "VK_LAYER_LUNARG_basic", VK_API_VERSION, VK_MAKE_VERSION(0, 1, 0), "Sample layer: basic, implements vkLayerExtension1", } }; /* Must use Vulkan name so that loader finds it */ VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties( VkPhysicalDevice physicalDevice, uint32_t* pCount, VkLayerProperties* pProperties) { /* Mem tracker's physical device layers are the same as global */ return util_GetLayerProperties(ARRAY_SIZE(basic_physicaldevice_layers), basic_physicaldevice_layers, pCount, pProperties); } static const VkExtensionProperties basic_physicaldevice_extensions[] = { { "vkLayerExtension1", VK_MAKE_VERSION(0, 1, 0), } }; VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties( VkPhysicalDevice physicalDevice, const char *pLayerName, uint32_t *pCount, VkExtensionProperties *pProperties) { if (pLayerName == NULL) { return instance_dispatch_table(physicalDevice)->EnumerateDeviceExtensionProperties( physicalDevice, NULL, pCount, pProperties); } else { return util_GetExtensionProperties(ARRAY_SIZE(basic_physicaldevice_extensions), basic_physicaldevice_extensions, pCount, pProperties); } } VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL basic_CreateInstance( const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance) { VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); assert(chain_info->u.pLayerInfo); PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance) fpGetInstanceProcAddr(NULL, "vkCreateInstance"); if (fpCreateInstance == NULL) { return VK_ERROR_INITIALIZATION_FAILED; } // Advance the link info for the next element on the chain chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance); if (result != VK_SUCCESS) return result; initInstanceTable(*pInstance, fpGetInstanceProcAddr); return result; } VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL basic_EnumeratePhysicalDevices( VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices) { printf("At start of wrapped vkEnumeratePhysicalDevices() call w/ inst: %p\n", (void*)instance); VkResult result = instance_dispatch_table(instance)->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices); printf("Completed wrapped vkEnumeratePhysicalDevices() call w/ count %u\n", *pPhysicalDeviceCount); return result; } VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL basic_CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) { printf("VK_LAYER_LUNARG_Basic: At start of vkCreateDevice() call w/ gpu: %p\n", (void*)physicalDevice); VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); assert(chain_info->u.pLayerInfo); PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr; PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice) fpGetInstanceProcAddr(NULL, "vkCreateDevice"); if (fpCreateDevice == NULL) { return VK_ERROR_INITIALIZATION_FAILED; } // Advance the link info for the next element on the chain chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice); if (result != VK_SUCCESS) { return result; } initDeviceTable(*pDevice, fpGetDeviceProcAddr); printf("VK_LAYER_LUNARG_Basic: Completed vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice); return result; } /* hook DestroyDevice to remove tableMap entry */ VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL basic_DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) { dispatch_key key = get_dispatch_key(device); device_dispatch_table(device)->DestroyDevice(device, pAllocator); destroy_device_dispatch_table(key); } /* hook DestroyInstance to remove tableInstanceMap entry */ VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL basic_DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator) { dispatch_key key = get_dispatch_key(instance); instance_dispatch_table(instance)->DestroyInstance(instance, pAllocator); destroy_instance_dispatch_table(key); } VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL basic_GetPhysicalDeviceFormatProperties(VkPhysicalDevice gpu, VkFormat format, VkFormatProperties *pFormatInfo) { printf("At start of wrapped vkGetPhysicalDeviceFormatProperties() call w/ gpu: %p\n", (void*)gpu); instance_dispatch_table(gpu)->GetPhysicalDeviceFormatProperties(gpu, format, pFormatInfo); printf("Completed wrapped vkGetPhysicalDeviceFormatProperties() call w/ gpu: %p\n", (void*)gpu); } VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char* pName) { if (!strcmp("vkGetDeviceProcAddr", pName)) return (PFN_vkVoidFunction) vkGetDeviceProcAddr; if (!strcmp("vkDestroyDevice", pName)) return (PFN_vkVoidFunction) basic_DestroyDevice; if (!strcmp("vkLayerExtension1", pName)) return (PFN_vkVoidFunction) vkLayerExtension1; if (device == NULL) return NULL; if (device_dispatch_table(device)->GetDeviceProcAddr == NULL) return NULL; return device_dispatch_table(device)->GetDeviceProcAddr(device, pName); } VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* pName) { if (!strcmp("vkGetInstanceProcAddr", pName)) return (PFN_vkVoidFunction) vkGetInstanceProcAddr; if (!strcmp("vkEnumerateDeviceLayerProperties", pName)) return (PFN_vkVoidFunction) vkEnumerateDeviceLayerProperties; if (!strcmp("vkEnumerateDeviceExtensionProperties", pName)) return (PFN_vkVoidFunction) vkEnumerateDeviceExtensionProperties; if (!strcmp("vkGetPhysicalDeviceFormatProperties", pName)) return (PFN_vkVoidFunction) basic_GetPhysicalDeviceFormatProperties; if (!strcmp("vkCreateInstance", pName)) return (PFN_vkVoidFunction) basic_CreateInstance; if (!strcmp("vkDestroyInstance", pName)) return (PFN_vkVoidFunction) basic_DestroyInstance; if (!strcmp("vkCreateDevice", pName)) return (PFN_vkVoidFunction) basic_CreateDevice; if (!strcmp("vkEnumeratePhysicalDevices", pName)) return (PFN_vkVoidFunction) basic_EnumeratePhysicalDevices; if (instance == NULL) return NULL; if (instance_dispatch_table(instance)->GetInstanceProcAddr == NULL) return NULL; return instance_dispatch_table(instance)->GetInstanceProcAddr(instance, pName); } VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties* pProperties) { return util_GetExtensionProperties(0, NULL, pCount, pProperties); } VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties* pProperties) { return util_GetLayerProperties(ARRAY_SIZE(globalLayerProps), globalLayerProps, pCount, pProperties); }