Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2016 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2016 Valve Corporation |
| 3 | * Copyright (c) 2015-2016 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2016 Google Inc. |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 5 | * |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and/or associated documentation files (the "Materials"), to |
| 8 | * deal in the Materials without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Materials, and to permit persons to whom the Materials |
| 11 | * are furnished to do so, subject to the following conditions: |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 12 | * |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 13 | * The above copyright notice(s) and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Materials. |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 15 | * |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 16 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | * |
| 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 | * USE OR OTHER DEALINGS IN THE MATERIALS |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 24 | * |
| 25 | * Author: Tobin Ehlis <tobine@google.com> |
| 26 | */ |
| 27 | |
Jamie Madill | df5d573 | 2016-04-04 11:54:43 -0400 | [diff] [blame] | 28 | #include "vk_loader_platform.h" |
| 29 | #include "vulkan/vulkan.h" |
| 30 | |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <inttypes.h> |
| 35 | |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 36 | #include <unordered_map> |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 37 | #include <vector> |
| 38 | #include <mutex> |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 39 | |
| 40 | #include "vulkan/vk_layer.h" |
| 41 | #include "vk_layer_config.h" |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 42 | #include "vk_layer_table.h" |
| 43 | #include "vk_layer_data.h" |
| 44 | #include "vk_layer_logging.h" |
| 45 | #include "vk_layer_extension_utils.h" |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 46 | #include "vk_safe_struct.h" |
Jon Ashburn | dc9111c | 2016-03-22 12:57:13 -0600 | [diff] [blame] | 47 | #include "vk_layer_utils.h" |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 48 | |
| 49 | struct layer_data { |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 50 | bool wsi_enabled; |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 51 | uint64_t unique_id; // All increments are guarded by global_lock |
| 52 | std::unordered_map<uint64_t, uint64_t> unique_id_mapping; // Map uniqueID to actual object handle |
| 53 | VkPhysicalDevice gpu; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 54 | |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 55 | layer_data() : wsi_enabled(false), unique_id(1), gpu(VK_NULL_HANDLE){}; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct instExts { |
| 59 | bool wsi_enabled; |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 60 | bool xlib_enabled; |
| 61 | bool xcb_enabled; |
| 62 | bool wayland_enabled; |
| 63 | bool mir_enabled; |
| 64 | bool android_enabled; |
| 65 | bool win32_enabled; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 68 | static std::unordered_map<void *, struct instExts> instanceExtMap; |
| 69 | static std::unordered_map<void *, layer_data *> layer_data_map; |
| 70 | static device_table_map unique_objects_device_table_map; |
| 71 | static instance_table_map unique_objects_instance_table_map; |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 72 | static std::mutex global_lock; // Protect map accesses and unique_id increments |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 73 | |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 74 | // Handle CreateInstance |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 75 | static void createInstanceRegisterExtensions(const VkInstanceCreateInfo *pCreateInfo, VkInstance instance) { |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 76 | uint32_t i; |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 77 | VkLayerInstanceDispatchTable *pDisp = get_dispatch_table(unique_objects_instance_table_map, instance); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 78 | PFN_vkGetInstanceProcAddr gpa = pDisp->GetInstanceProcAddr; |
Michael Lentine | 56512bb | 2016-03-02 17:28:55 -0600 | [diff] [blame] | 79 | |
| 80 | pDisp->DestroySurfaceKHR = (PFN_vkDestroySurfaceKHR)gpa(instance, "vkDestroySurfaceKHR"); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 81 | pDisp->GetPhysicalDeviceSurfaceSupportKHR = |
| 82 | (PFN_vkGetPhysicalDeviceSurfaceSupportKHR)gpa(instance, "vkGetPhysicalDeviceSurfaceSupportKHR"); |
| 83 | pDisp->GetPhysicalDeviceSurfaceCapabilitiesKHR = |
| 84 | (PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR)gpa(instance, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR"); |
| 85 | pDisp->GetPhysicalDeviceSurfaceFormatsKHR = |
| 86 | (PFN_vkGetPhysicalDeviceSurfaceFormatsKHR)gpa(instance, "vkGetPhysicalDeviceSurfaceFormatsKHR"); |
| 87 | pDisp->GetPhysicalDeviceSurfacePresentModesKHR = |
| 88 | (PFN_vkGetPhysicalDeviceSurfacePresentModesKHR)gpa(instance, "vkGetPhysicalDeviceSurfacePresentModesKHR"); |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 89 | #ifdef VK_USE_PLATFORM_WIN32_KHR |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 90 | pDisp->CreateWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR)gpa(instance, "vkCreateWin32SurfaceKHR"); |
| 91 | pDisp->GetPhysicalDeviceWin32PresentationSupportKHR = |
| 92 | (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceWin32PresentationSupportKHR"); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 93 | #endif // VK_USE_PLATFORM_WIN32_KHR |
| 94 | #ifdef VK_USE_PLATFORM_XCB_KHR |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 95 | pDisp->CreateXcbSurfaceKHR = (PFN_vkCreateXcbSurfaceKHR)gpa(instance, "vkCreateXcbSurfaceKHR"); |
| 96 | pDisp->GetPhysicalDeviceXcbPresentationSupportKHR = |
| 97 | (PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceXcbPresentationSupportKHR"); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 98 | #endif // VK_USE_PLATFORM_XCB_KHR |
| 99 | #ifdef VK_USE_PLATFORM_XLIB_KHR |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 100 | pDisp->CreateXlibSurfaceKHR = (PFN_vkCreateXlibSurfaceKHR)gpa(instance, "vkCreateXlibSurfaceKHR"); |
| 101 | pDisp->GetPhysicalDeviceXlibPresentationSupportKHR = |
| 102 | (PFN_vkGetPhysicalDeviceXlibPresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceXlibPresentationSupportKHR"); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 103 | #endif // VK_USE_PLATFORM_XLIB_KHR |
| 104 | #ifdef VK_USE_PLATFORM_MIR_KHR |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 105 | pDisp->CreateMirSurfaceKHR = (PFN_vkCreateMirSurfaceKHR)gpa(instance, "vkCreateMirSurfaceKHR"); |
| 106 | pDisp->GetPhysicalDeviceMirPresentationSupportKHR = |
| 107 | (PFN_vkGetPhysicalDeviceMirPresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceMirPresentationSupportKHR"); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 108 | #endif // VK_USE_PLATFORM_MIR_KHR |
| 109 | #ifdef VK_USE_PLATFORM_WAYLAND_KHR |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 110 | pDisp->CreateWaylandSurfaceKHR = (PFN_vkCreateWaylandSurfaceKHR)gpa(instance, "vkCreateWaylandSurfaceKHR"); |
| 111 | pDisp->GetPhysicalDeviceWaylandPresentationSupportKHR = |
| 112 | (PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceWaylandPresentationSupportKHR"); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 113 | #endif // VK_USE_PLATFORM_WAYLAND_KHR |
| 114 | #ifdef VK_USE_PLATFORM_ANDROID_KHR |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 115 | pDisp->CreateAndroidSurfaceKHR = (PFN_vkCreateAndroidSurfaceKHR)gpa(instance, "vkCreateAndroidSurfaceKHR"); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 116 | #endif // VK_USE_PLATFORM_ANDROID_KHR |
| 117 | |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 118 | instanceExtMap[pDisp] = {}; |
Jon Ashburn | f19916e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 119 | for (i = 0; i < pCreateInfo->enabledExtensionCount; i++) { |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 120 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SURFACE_EXTENSION_NAME) == 0) |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 121 | instanceExtMap[pDisp].wsi_enabled = true; |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 122 | #ifdef VK_USE_PLATFORM_XLIB_KHR |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 123 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XLIB_SURFACE_EXTENSION_NAME) == 0) |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 124 | instanceExtMap[pDisp].xlib_enabled = true; |
| 125 | #endif |
| 126 | #ifdef VK_USE_PLATFORM_XCB_KHR |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 127 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XCB_SURFACE_EXTENSION_NAME) == 0) |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 128 | instanceExtMap[pDisp].xcb_enabled = true; |
| 129 | #endif |
| 130 | #ifdef VK_USE_PLATFORM_WAYLAND_KHR |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 131 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME) == 0) |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 132 | instanceExtMap[pDisp].wayland_enabled = true; |
| 133 | #endif |
| 134 | #ifdef VK_USE_PLATFORM_MIR_KHR |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 135 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_MIR_SURFACE_EXTENSION_NAME) == 0) |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 136 | instanceExtMap[pDisp].mir_enabled = true; |
| 137 | #endif |
| 138 | #ifdef VK_USE_PLATFORM_ANDROID_KHR |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 139 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_ANDROID_SURFACE_EXTENSION_NAME) == 0) |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 140 | instanceExtMap[pDisp].android_enabled = true; |
| 141 | #endif |
| 142 | #ifdef VK_USE_PLATFORM_WIN32_KHR |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 143 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WIN32_SURFACE_EXTENSION_NAME) == 0) |
Tobin Ehlis | a39c26a | 2016-01-05 16:34:59 -0700 | [diff] [blame] | 144 | instanceExtMap[pDisp].win32_enabled = true; |
| 145 | #endif |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 149 | VkResult explicit_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, |
| 150 | VkInstance *pInstance) { |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 151 | VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 152 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 153 | assert(chain_info->u.pLayerInfo); |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 154 | PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 155 | PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance"); |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 156 | if (fpCreateInstance == NULL) { |
| 157 | return VK_ERROR_INITIALIZATION_FAILED; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 158 | } |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 159 | |
| 160 | // Advance the link info for the next element on the chain |
| 161 | chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; |
| 162 | |
| 163 | VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance); |
| 164 | if (result != VK_SUCCESS) { |
| 165 | return result; |
| 166 | } |
| 167 | |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 168 | initInstanceTable(*pInstance, fpGetInstanceProcAddr, unique_objects_instance_table_map); |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 169 | |
| 170 | createInstanceRegisterExtensions(pCreateInfo, *pInstance); |
| 171 | |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 172 | return result; |
| 173 | } |
| 174 | |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 175 | void explicit_DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) { |
| 176 | get_dispatch_table(unique_objects_instance_table_map, instance)->DestroyInstance(instance, pAllocator); |
| 177 | layer_data_map.erase(get_dispatch_key(instance)); |
| 178 | } |
| 179 | |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 180 | // Handle CreateDevice |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 181 | static void createDeviceRegisterExtensions(const VkDeviceCreateInfo *pCreateInfo, VkDevice device) { |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 182 | layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map); |
| 183 | VkLayerDispatchTable *pDisp = get_dispatch_table(unique_objects_device_table_map, device); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 184 | PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 185 | pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR)gpa(device, "vkCreateSwapchainKHR"); |
| 186 | pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR)gpa(device, "vkDestroySwapchainKHR"); |
| 187 | pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR)gpa(device, "vkGetSwapchainImagesKHR"); |
| 188 | pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR)gpa(device, "vkAcquireNextImageKHR"); |
| 189 | pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR)gpa(device, "vkQueuePresentKHR"); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 190 | my_device_data->wsi_enabled = false; |
Jon Ashburn | f19916e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 191 | for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) { |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 192 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 193 | my_device_data->wsi_enabled = true; |
| 194 | } |
| 195 | } |
| 196 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 197 | VkResult explicit_CreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, |
| 198 | VkDevice *pDevice) { |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 199 | VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 200 | |
| 201 | assert(chain_info->u.pLayerInfo); |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 202 | PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; |
| 203 | PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 204 | PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice"); |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 205 | if (fpCreateDevice == NULL) { |
| 206 | return VK_ERROR_INITIALIZATION_FAILED; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 207 | } |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 208 | |
| 209 | // Advance the link info for the next element on the chain |
| 210 | chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; |
| 211 | |
| 212 | VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice); |
| 213 | if (result != VK_SUCCESS) { |
| 214 | return result; |
| 215 | } |
| 216 | |
| 217 | // Setup layer's device dispatch table |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 218 | initDeviceTable(*pDevice, fpGetDeviceProcAddr, unique_objects_device_table_map); |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 219 | |
| 220 | createDeviceRegisterExtensions(pCreateInfo, *pDevice); |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 221 | // Set gpu for this device in order to get at any objects mapped at instance level |
| 222 | layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map); |
| 223 | my_device_data->gpu = gpu; |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 224 | |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 225 | return result; |
| 226 | } |
| 227 | |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 228 | void explicit_DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) { |
| 229 | get_dispatch_table(unique_objects_device_table_map, device)->DestroyDevice(device, pAllocator); |
| 230 | layer_data_map.erase(get_dispatch_key(device)); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 233 | VkResult explicit_CreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, |
| 234 | const VkComputePipelineCreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator, |
| 235 | VkPipeline *pPipelines) { |
| 236 | // STRUCT USES:{'pipelineCache': 'VkPipelineCache', 'pCreateInfos[createInfoCount]': {'stage': {'module': 'VkShaderModule'}, |
| 237 | // 'layout': 'VkPipelineLayout', 'basePipelineHandle': 'VkPipeline'}} |
| 238 | // LOCAL DECLS:{'pCreateInfos': 'VkComputePipelineCreateInfo*'} |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 239 | layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 240 | safe_VkComputePipelineCreateInfo *local_pCreateInfos = NULL; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 241 | if (pCreateInfos) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 242 | std::lock_guard<std::mutex> lock(global_lock); |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 243 | local_pCreateInfos = new safe_VkComputePipelineCreateInfo[createInfoCount]; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 244 | for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) { |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 245 | local_pCreateInfos[idx0].initialize(&pCreateInfos[idx0]); |
| 246 | if (pCreateInfos[idx0].basePipelineHandle) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 247 | local_pCreateInfos[idx0].basePipelineHandle = |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 248 | (VkPipeline) |
| 249 | my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfos[idx0].basePipelineHandle)]; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 250 | } |
| 251 | if (pCreateInfos[idx0].layout) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 252 | local_pCreateInfos[idx0].layout = |
| 253 | (VkPipelineLayout)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfos[idx0].layout)]; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 254 | } |
| 255 | if (pCreateInfos[idx0].stage.module) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 256 | local_pCreateInfos[idx0].stage.module = |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 257 | (VkShaderModule)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfos[idx0].stage.module)]; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | if (pipelineCache) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 262 | std::lock_guard<std::mutex> lock(global_lock); |
| 263 | pipelineCache = (VkPipelineCache)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(pipelineCache)]; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 264 | } |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 265 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 266 | VkResult result = get_dispatch_table(unique_objects_device_table_map, device) |
| 267 | ->CreateComputePipelines(device, pipelineCache, createInfoCount, |
| 268 | (const VkComputePipelineCreateInfo *)local_pCreateInfos, pAllocator, pPipelines); |
Eric Engestrom | 53b513f | 2016-02-21 19:58:09 +0000 | [diff] [blame] | 269 | delete[] local_pCreateInfos; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 270 | if (VK_SUCCESS == result) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 271 | uint64_t unique_id = 0; |
| 272 | std::lock_guard<std::mutex> lock(global_lock); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 273 | for (uint32_t i = 0; i < createInfoCount; ++i) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 274 | unique_id = my_device_data->unique_id++; |
| 275 | my_device_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t>(pPipelines[i]); |
| 276 | pPipelines[i] = reinterpret_cast<VkPipeline>(unique_id); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | return result; |
| 280 | } |
| 281 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 282 | VkResult explicit_CreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, |
| 283 | const VkGraphicsPipelineCreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator, |
| 284 | VkPipeline *pPipelines) { |
| 285 | // STRUCT USES:{'pipelineCache': 'VkPipelineCache', 'pCreateInfos[createInfoCount]': {'layout': 'VkPipelineLayout', |
| 286 | // 'pStages[stageCount]': {'module': 'VkShaderModule'}, 'renderPass': 'VkRenderPass', 'basePipelineHandle': 'VkPipeline'}} |
| 287 | // LOCAL DECLS:{'pCreateInfos': 'VkGraphicsPipelineCreateInfo*'} |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 288 | layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 289 | safe_VkGraphicsPipelineCreateInfo *local_pCreateInfos = NULL; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 290 | if (pCreateInfos) { |
| 291 | local_pCreateInfos = new safe_VkGraphicsPipelineCreateInfo[createInfoCount]; |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 292 | std::lock_guard<std::mutex> lock(global_lock); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 293 | for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) { |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 294 | local_pCreateInfos[idx0].initialize(&pCreateInfos[idx0]); |
| 295 | if (pCreateInfos[idx0].basePipelineHandle) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 296 | local_pCreateInfos[idx0].basePipelineHandle = |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 297 | (VkPipeline) |
| 298 | my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfos[idx0].basePipelineHandle)]; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 299 | } |
| 300 | if (pCreateInfos[idx0].layout) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 301 | local_pCreateInfos[idx0].layout = |
| 302 | (VkPipelineLayout)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfos[idx0].layout)]; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 303 | } |
| 304 | if (pCreateInfos[idx0].pStages) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 305 | for (uint32_t idx1 = 0; idx1 < pCreateInfos[idx0].stageCount; ++idx1) { |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 306 | if (pCreateInfos[idx0].pStages[idx1].module) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 307 | local_pCreateInfos[idx0].pStages[idx1].module = |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 308 | (VkShaderModule)my_device_data |
| 309 | ->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfos[idx0].pStages[idx1].module)]; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | if (pCreateInfos[idx0].renderPass) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 314 | local_pCreateInfos[idx0].renderPass = |
| 315 | (VkRenderPass)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfos[idx0].renderPass)]; |
Tobin Ehlis | 8bb7c2f | 2016-02-10 15:38:45 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | if (pipelineCache) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 320 | std::lock_guard<std::mutex> lock(global_lock); |
| 321 | pipelineCache = (VkPipelineCache)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(pipelineCache)]; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 322 | } |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 323 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 324 | VkResult result = |
| 325 | get_dispatch_table(unique_objects_device_table_map, device) |
| 326 | ->CreateGraphicsPipelines(device, pipelineCache, createInfoCount, |
| 327 | (const VkGraphicsPipelineCreateInfo *)local_pCreateInfos, pAllocator, pPipelines); |
Eric Engestrom | 53b513f | 2016-02-21 19:58:09 +0000 | [diff] [blame] | 328 | delete[] local_pCreateInfos; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 329 | if (VK_SUCCESS == result) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 330 | uint64_t unique_id = 0; |
| 331 | std::lock_guard<std::mutex> lock(global_lock); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 332 | for (uint32_t i = 0; i < createInfoCount; ++i) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 333 | unique_id = my_device_data->unique_id++; |
| 334 | my_device_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t>(pPipelines[i]); |
| 335 | pPipelines[i] = reinterpret_cast<VkPipeline>(unique_id); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | return result; |
| 339 | } |
| 340 | |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 341 | VkResult explicit_CreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo, |
| 342 | const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain) { |
| 343 | layer_data *my_map_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map); |
| 344 | |
| 345 | safe_VkSwapchainCreateInfoKHR *local_pCreateInfo = NULL; |
| 346 | if (pCreateInfo) { |
| 347 | std::lock_guard<std::mutex> lock(global_lock); |
| 348 | local_pCreateInfo = new safe_VkSwapchainCreateInfoKHR(pCreateInfo); |
| 349 | local_pCreateInfo->oldSwapchain = |
| 350 | (VkSwapchainKHR)my_map_data->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfo->oldSwapchain)]; |
| 351 | // Need to pull surface mapping from the instance-level map |
| 352 | layer_data *instance_data = get_my_data_ptr(get_dispatch_key(my_map_data->gpu), layer_data_map); |
| 353 | local_pCreateInfo->surface = |
| 354 | (VkSurfaceKHR)instance_data->unique_id_mapping[reinterpret_cast<uint64_t>(pCreateInfo->surface)]; |
| 355 | } |
| 356 | |
| 357 | VkResult result = get_dispatch_table(unique_objects_device_table_map, device) |
| 358 | ->CreateSwapchainKHR(device, (const VkSwapchainCreateInfoKHR *)local_pCreateInfo, pAllocator, pSwapchain); |
| 359 | if (local_pCreateInfo) |
| 360 | delete local_pCreateInfo; |
| 361 | if (VK_SUCCESS == result) { |
| 362 | std::lock_guard<std::mutex> lock(global_lock); |
| 363 | uint64_t unique_id = my_map_data->unique_id++; |
| 364 | my_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t>(*pSwapchain); |
| 365 | *pSwapchain = reinterpret_cast<VkSwapchainKHR>(unique_id); |
| 366 | } |
| 367 | return result; |
| 368 | } |
| 369 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 370 | VkResult explicit_GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount, |
| 371 | VkImage *pSwapchainImages) { |
| 372 | // UNWRAP USES: |
| 373 | // 0 : swapchain,VkSwapchainKHR, pSwapchainImages,VkImage |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 374 | layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 375 | if (VK_NULL_HANDLE != swapchain) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 376 | std::lock_guard<std::mutex> lock(global_lock); |
| 377 | swapchain = (VkSwapchainKHR)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t>(swapchain)]; |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 378 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 379 | VkResult result = get_dispatch_table(unique_objects_device_table_map, device) |
| 380 | ->GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 381 | // TODO : Need to add corresponding code to delete these images |
| 382 | if (VK_SUCCESS == result) { |
| 383 | if ((*pSwapchainImageCount > 0) && pSwapchainImages) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 384 | uint64_t unique_id = 0; |
| 385 | std::lock_guard<std::mutex> lock(global_lock); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 386 | for (uint32_t i = 0; i < *pSwapchainImageCount; ++i) { |
Tobin Ehlis | 10ba1de | 2016-04-13 12:59:43 -0600 | [diff] [blame^] | 387 | unique_id = my_device_data->unique_id++; |
| 388 | my_device_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t>(pSwapchainImages[i]); |
| 389 | pSwapchainImages[i] = reinterpret_cast<VkImage>(unique_id); |
Tobin Ehlis | d34a4c5 | 2015-12-08 10:50:10 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | } |
| 393 | return result; |
| 394 | } |