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