blob: 77fd1633ddccb05c54dd0e853f4ec9ca52b6e28a [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* 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.
Ian Elliott0b4d6242015-09-22 10:51:24 -06005 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06006 * 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
Ian Elliott0b4d6242015-09-22 10:51:24 -06009 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Ian Elliott0b4d6242015-09-22 10:51:24 -060011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * 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.
Ian Elliott0b4d6242015-09-22 10:51:24 -060017 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060018 * Author: Ian Elliott <ian@lunarg.com>
Ian Elliott578e7e22016-01-05 14:03:16 -070019 * Author: Ian Elliott <ianelliott@google.com>
Ian Elliott0b4d6242015-09-22 10:51:24 -060020 */
21
Mike Weiblen6a27de52016-12-09 17:36:28 -070022// For Windows, this #include must come before other Vk headers.
23#include <vk_loader_platform.h>
24
25#include "swapchain.h"
26#include "vk_enum_string_helper.h"
27#include "vk_layer_extension_utils.h"
28#include "vk_layer_utils.h"
29#include "vk_validation_error_messages.h"
Jeremy Hayes9de0bd72016-04-13 11:57:20 -060030#include <mutex>
Ian Elliottd8c5db12015-10-07 11:32:31 -060031#include <stdio.h>
32#include <string.h>
Cody Northropd08141b2016-02-01 09:52:07 -070033#include <vulkan/vk_icd.h>
Ian Elliott68124ac2015-10-07 16:18:35 -060034
Chia-I Wu516b5082016-04-28 11:27:46 +080035namespace swapchain {
36
Jeremy Hayes9de0bd72016-04-13 11:57:20 -060037static std::mutex global_lock;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070038
Ian Elliott0b4d6242015-09-22 10:51:24 -060039// The following is for logging error messages:
Ian Elliott68124ac2015-10-07 16:18:35 -060040static std::unordered_map<void *, layer_data *> layer_data_map;
Ian Elliott0b4d6242015-09-22 10:51:24 -060041
Mark Young39389872017-01-19 21:10:49 -070042static uint32_t loader_layer_if_version = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
43
Jon Ashburn5484e0c2016-03-08 17:48:44 -070044static const VkExtensionProperties instance_extensions[] = {{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION}};
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -070045
Chia-I Wu40e25e72016-04-28 14:12:27 +080046static const VkLayerProperties swapchain_layer = {
Jon Ashburndc9111c2016-03-22 12:57:13 -060047 "VK_LAYER_LUNARG_swapchain", VK_LAYER_API_VERSION, 1, "LunarG Validation Layer",
Chia-I Wu40e25e72016-04-28 14:12:27 +080048};
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -070049
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -060050static void checkDeviceRegisterExtensions(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo, VkDevice device) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070051 layer_data *my_device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
52 layer_data *my_instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Ian Elliott0b4d6242015-09-22 10:51:24 -060053
Ian Elliott77f46ca2016-05-05 14:10:49 -060054 SwpPhysicalDevice *pPhysicalDevice = NULL;
55 {
56 auto it = my_instance_data->physicalDeviceMap.find(physicalDevice);
57 pPhysicalDevice = (it == my_instance_data->physicalDeviceMap.end()) ? NULL : &it->second;
58 }
Ian Elliott0b4d6242015-09-22 10:51:24 -060059 if (pPhysicalDevice) {
Mark Lobodzinski55cd49b2015-11-27 15:23:23 -070060 my_device_data->deviceMap[device].pPhysicalDevice = pPhysicalDevice;
61 pPhysicalDevice->pDevice = &my_device_data->deviceMap[device];
Ian Elliott0b4d6242015-09-22 10:51:24 -060062 } else {
Ian Elliott07adb112016-01-05 12:51:03 -070063 // TBD: Should we leave error in (since Swapchain really needs this
64 // link)?
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070065 log_msg(my_instance_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
Mike Weiblen6a27de52016-12-09 17:36:28 -070066 reinterpret_cast<uint64_t>(physicalDevice), __LINE__, VALIDATION_ERROR_00031, "Swapchain",
67 "vkCreateDevice() called with a non-valid VkPhysicalDevice. %s", validation_error_map[VALIDATION_ERROR_00031]);
Ian Elliott0b4d6242015-09-22 10:51:24 -060068 }
Mark Lobodzinski55cd49b2015-11-27 15:23:23 -070069 my_device_data->deviceMap[device].device = device;
Ian Elliott0b4d6242015-09-22 10:51:24 -060070}
71
Mark Youngaa1aa3a2016-07-05 16:41:50 -060072static void checkInstanceRegisterExtensions(const VkInstanceCreateInfo *pCreateInfo, VkInstance instance) {
Ian Elliott0b4d6242015-09-22 10:51:24 -060073 uint32_t i;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070074 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Ian Elliott0b4d6242015-09-22 10:51:24 -060075
Ian Elliott1dcd1092015-11-17 17:29:40 -070076 // Remember this instance, and whether the VK_KHR_surface extension
Ian Elliott0b4d6242015-09-22 10:51:24 -060077 // was enabled for it:
Tobin Ehlis711ff312015-10-29 12:58:13 -060078 my_data->instanceMap[instance].instance = instance;
Petros Bantolas2b40be72016-04-15 11:02:59 +010079 my_data->instanceMap[instance].displayExtensionEnabled = false;
Ian Elliott8dffaf32016-01-04 14:10:30 -070080
Ian Elliotted6b5ac2016-04-28 09:08:13 -060081 // Look for one or more debug report create info structures, and copy the
82 // callback(s) for each one found (for use by vkDestroyInstance)
83 layer_copy_tmp_callbacks(pCreateInfo->pNext, &my_data->num_tmp_callbacks, &my_data->tmp_dbg_create_infos,
84 &my_data->tmp_callbacks);
85
Ian Elliott0b4d6242015-09-22 10:51:24 -060086 // Record whether the WSI instance extension was enabled for this
87 // VkInstance. No need to check if the extension was advertised by
88 // vkEnumerateInstanceExtensionProperties(), since the loader handles that.
Jon Ashburnf19916e2016-01-11 13:12:43 -070089 for (i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Petros Bantolas2b40be72016-04-15 11:02:59 +010090 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_DISPLAY_EXTENSION_NAME) == 0) {
Petros Bantolas2b40be72016-04-15 11:02:59 +010091 my_data->instanceMap[instance].displayExtensionEnabled = true;
92 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070093 }
94}
95
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070096#include "vk_dispatch_table_helper.h"
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060097static void init_swapchain(layer_data *my_data, const VkAllocationCallbacks *pAllocator) {
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060098 layer_debug_actions(my_data->report_data, my_data->logging_callback, pAllocator, "lunarg_swapchain");
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070099}
Ian Elliott0b4d6242015-09-22 10:51:24 -0600100
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700101static const char *presentModeStr(VkPresentModeKHR value) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700102 // Return a string corresponding to the value:
103 return string_VkPresentModeKHR(value);
104}
Ian Elliott0b4d6242015-09-22 10:51:24 -0600105
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700106static const char *sharingModeStr(VkSharingMode value) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700107 // Return a string corresponding to the value:
108 return string_VkSharingMode(value);
109}
Ian Elliott07adb112016-01-05 12:51:03 -0700110
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600111VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
112 VkInstance *pInstance) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700113 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
Ian Elliott0b4d6242015-09-22 10:51:24 -0600114
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700115 assert(chain_info->u.pLayerInfo);
116 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700117 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700118 if (fpCreateInstance == NULL) {
119 return VK_ERROR_INITIALIZATION_FAILED;
120 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700121
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700122 // Advance the link info for the next element on the chain
123 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700124
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700125 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
126 if (result != VK_SUCCESS) {
127 return result;
128 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700129
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700130 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(*pInstance), layer_data_map);
Chia-I Wua6737532016-04-28 16:04:15 +0800131 my_data->instance = *pInstance;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700132 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
133 layer_init_instance_dispatch_table(*pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700134 my_data->report_data = debug_report_create_instance(my_data->instance_dispatch_table, *pInstance,
135 pCreateInfo->enabledExtensionCount, pCreateInfo->ppEnabledExtensionNames);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700136
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700137 // Call the following function after my_data is initialized:
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600138 checkInstanceRegisterExtensions(pCreateInfo, *pInstance);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600139 init_swapchain(my_data, pAllocator);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700140
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700141 return result;
142}
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700143
Chia-I Wufccbfe42016-04-28 14:01:30 +0800144VKAPI_ATTR void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700145 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700146 layer_data *my_data = GetLayerDataPtr(key, layer_data_map);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600147 SwpInstance *pInstance = NULL;
148 {
149 auto it = my_data->instanceMap.find(instance);
150 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
151 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700152
Ian Elliott32311832016-02-04 08:17:18 -0700153 // Call down the call chain:
154 my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700155
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600156 std::lock_guard<std::mutex> lock(global_lock);
Ian Elliott32311832016-02-04 08:17:18 -0700157
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600158 // Enable the temporary callback(s) here to catch cleanup issues:
159 bool callback_setup = false;
160 if (my_data->num_tmp_callbacks > 0) {
161 if (!layer_enable_tmp_callbacks(my_data->report_data, my_data->num_tmp_callbacks, my_data->tmp_dbg_create_infos,
162 my_data->tmp_callbacks)) {
163 callback_setup = true;
164 }
165 }
166
Ian Elliott32311832016-02-04 08:17:18 -0700167 // Do additional internal cleanup:
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700168 if (pInstance) {
169 // Delete all of the SwpPhysicalDevice's, SwpSurface's, and the
170 // SwpInstance associated with this instance:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700171 for (auto it = pInstance->physicalDevices.begin(); it != pInstance->physicalDevices.end(); it++) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700172 // Free memory that was allocated for/by this SwpPhysicalDevice:
173 SwpPhysicalDevice *pPhysicalDevice = it->second;
174 if (pPhysicalDevice) {
Ian Elliott458696a2016-02-04 06:11:17 -0700175 if (pPhysicalDevice->pDevice) {
Mark Lobodzinski9b482fa2016-08-08 09:38:42 -0600176 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
Mike Weiblen6a27de52016-12-09 17:36:28 -0700177 reinterpret_cast<uint64_t>(pPhysicalDevice->pDevice->device), __LINE__, VALIDATION_ERROR_00018,
178 swapchain_layer_name,
179 "VkDestroyInstance() called before all of its associated VkDevices were destroyed. %s",
180 validation_error_map[VALIDATION_ERROR_00018]);
Ian Elliott458696a2016-02-04 06:11:17 -0700181 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700182 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700183
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700184 // Erase the SwpPhysicalDevice's from the my_data->physicalDeviceMap (which
185 // are simply pointed to by the SwpInstance):
186 my_data->physicalDeviceMap.erase(it->second->physicalDevice);
187 }
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700188 for (auto it = pInstance->surfaces.begin(); it != pInstance->surfaces.end(); it++) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700189 // Free memory that was allocated for/by this SwpPhysicalDevice:
190 SwpSurface *pSurface = it->second;
191 if (pSurface) {
Mark Lobodzinski9b482fa2016-08-08 09:38:42 -0600192 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
Mike Weiblen6a27de52016-12-09 17:36:28 -0700193 reinterpret_cast<uint64_t>(pInstance->instance), __LINE__, VALIDATION_ERROR_00018, swapchain_layer_name,
194 "VkDestroyInstance() called before all of its associated VkSurfaceKHRs were destroyed. %s",
195 validation_error_map[VALIDATION_ERROR_00018]);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700196 }
197 }
198 my_data->instanceMap.erase(instance);
199 }
Mark Lobodzinski3c99d552016-02-04 13:50:23 -0700200
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600201 // Disable and cleanup the temporary callback(s):
202 if (callback_setup) {
203 layer_disable_tmp_callbacks(my_data->report_data, my_data->num_tmp_callbacks, my_data->tmp_callbacks);
204 }
205 if (my_data->num_tmp_callbacks > 0) {
206 layer_free_tmp_callbacks(my_data->tmp_dbg_create_infos, my_data->tmp_callbacks);
207 my_data->num_tmp_callbacks = 0;
208 }
209
Mark Lobodzinski3c99d552016-02-04 13:50:23 -0700210 // Clean up logging callback, if any
211 while (my_data->logging_callback.size() > 0) {
212 VkDebugReportCallbackEXT callback = my_data->logging_callback.back();
213 layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);
214 my_data->logging_callback.pop_back();
215 }
216 layer_debug_report_destroy_instance(my_data->report_data);
217
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700218 delete my_data->instance_dispatch_table;
219 layer_data_map.erase(key);
220}
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700221
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700222#ifdef VK_USE_PLATFORM_ANDROID_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600223VKAPI_ATTR VkResult VKAPI_CALL CreateAndroidSurfaceKHR(VkInstance instance, const VkAndroidSurfaceCreateInfoKHR *pCreateInfo,
224 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700225 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600226 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700227 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600228 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600229 SwpInstance *pInstance = NULL;
230 {
231 auto it = my_data->instanceMap.find(instance);
232 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
233 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700234
Ian Elliott970a2bd2016-06-21 11:08:43 -0600235 lock.unlock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700236
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600237 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700238 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700239 result = my_data->instance_dispatch_table->CreateAndroidSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600240 lock.lock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700241
Ian Elliott32311832016-02-04 08:17:18 -0700242 // Obtain this pointer again after locking:
Ian Elliott77f46ca2016-05-05 14:10:49 -0600243 {
244 auto it = my_data->instanceMap.find(instance);
245 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
246 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700247 if ((result == VK_SUCCESS) && pInstance && pSurface) {
248 // Record the VkSurfaceKHR returned by the ICD:
249 my_data->surfaceMap[*pSurface].surface = *pSurface;
250 my_data->surfaceMap[*pSurface].pInstance = pInstance;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700251 // Point to the associated SwpInstance:
252 pInstance->surfaces[*pSurface] = &my_data->surfaceMap[*pSurface];
253 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600254 lock.unlock();
255
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700256 return result;
257 }
258 return VK_ERROR_VALIDATION_FAILED_EXT;
259}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700260#endif // VK_USE_PLATFORM_ANDROID_KHR
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700261
262#ifdef VK_USE_PLATFORM_MIR_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600263VKAPI_ATTR VkResult VKAPI_CALL CreateMirSurfaceKHR(VkInstance instance, const VkMirSurfaceCreateInfoKHR *pCreateInfo,
264 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700265 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600266 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700267 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600268 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600269 SwpInstance *pInstance = NULL;
270 {
271 auto it = my_data->instanceMap.find(instance);
272 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
273 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700274
Ian Elliott970a2bd2016-06-21 11:08:43 -0600275 lock.unlock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700276
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600277 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700278 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700279 result = my_data->instance_dispatch_table->CreateMirSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600280 lock.lock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700281
Ian Elliott32311832016-02-04 08:17:18 -0700282 // Obtain this pointer again after locking:
Ian Elliott77f46ca2016-05-05 14:10:49 -0600283 {
284 auto it = my_data->instanceMap.find(instance);
285 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
286 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700287 if ((result == VK_SUCCESS) && pInstance && pSurface) {
288 // Record the VkSurfaceKHR returned by the ICD:
289 my_data->surfaceMap[*pSurface].surface = *pSurface;
290 my_data->surfaceMap[*pSurface].pInstance = pInstance;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700291 // Point to the associated SwpInstance:
292 pInstance->surfaces[*pSurface] = &my_data->surfaceMap[*pSurface];
293 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600294 lock.unlock();
295
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700296 return result;
297 }
298 return VK_ERROR_VALIDATION_FAILED_EXT;
299}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700300#endif // VK_USE_PLATFORM_MIR_KHR
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700301
302#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600303VKAPI_ATTR VkResult VKAPI_CALL CreateWaylandSurfaceKHR(VkInstance instance, const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
304 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700305 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600306 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700307 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600308 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600309 SwpInstance *pInstance = NULL;
310 {
311 auto it = my_data->instanceMap.find(instance);
312 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
313 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700314
Ian Elliott970a2bd2016-06-21 11:08:43 -0600315 lock.unlock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700316
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600317 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700318 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700319 result = my_data->instance_dispatch_table->CreateWaylandSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600320 lock.lock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700321
Ian Elliott32311832016-02-04 08:17:18 -0700322 // Obtain this pointer again after locking:
Ian Elliott77f46ca2016-05-05 14:10:49 -0600323 {
324 auto it = my_data->instanceMap.find(instance);
325 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
326 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700327 if ((result == VK_SUCCESS) && pInstance && pSurface) {
328 // Record the VkSurfaceKHR returned by the ICD:
329 my_data->surfaceMap[*pSurface].surface = *pSurface;
330 my_data->surfaceMap[*pSurface].pInstance = pInstance;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700331 // Point to the associated SwpInstance:
332 pInstance->surfaces[*pSurface] = &my_data->surfaceMap[*pSurface];
333 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600334 lock.unlock();
335
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700336 return result;
337 }
338 return VK_ERROR_VALIDATION_FAILED_EXT;
339}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700340#endif // VK_USE_PLATFORM_WAYLAND_KHR
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700341
342#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600343VKAPI_ATTR VkResult VKAPI_CALL CreateWin32SurfaceKHR(VkInstance instance, const VkWin32SurfaceCreateInfoKHR *pCreateInfo,
344 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700345 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600346 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700347 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600348 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600349 SwpInstance *pInstance = NULL;
350 {
351 auto it = my_data->instanceMap.find(instance);
352 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
353 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700354
Ian Elliott970a2bd2016-06-21 11:08:43 -0600355 lock.unlock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700356
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600357 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700358 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700359 result = my_data->instance_dispatch_table->CreateWin32SurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600360 lock.lock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700361
Ian Elliott32311832016-02-04 08:17:18 -0700362 // Obtain this pointer again after locking:
Ian Elliott77f46ca2016-05-05 14:10:49 -0600363 {
364 auto it = my_data->instanceMap.find(instance);
365 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
366 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700367 if ((result == VK_SUCCESS) && pInstance && pSurface) {
368 // Record the VkSurfaceKHR returned by the ICD:
369 my_data->surfaceMap[*pSurface].surface = *pSurface;
370 my_data->surfaceMap[*pSurface].pInstance = pInstance;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700371 // Point to the associated SwpInstance:
372 pInstance->surfaces[*pSurface] = &my_data->surfaceMap[*pSurface];
373 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600374 lock.unlock();
375
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700376 return result;
377 }
378 return VK_ERROR_VALIDATION_FAILED_EXT;
379}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700380#endif // VK_USE_PLATFORM_WIN32_KHR
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700381
382#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600383VKAPI_ATTR VkResult VKAPI_CALL CreateXcbSurfaceKHR(VkInstance instance, const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
384 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700385 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600386 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700387 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600388 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600389 SwpInstance *pInstance = NULL;
390 {
391 auto it = my_data->instanceMap.find(instance);
392 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
393 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700394
Ian Elliott970a2bd2016-06-21 11:08:43 -0600395 lock.unlock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700396
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600397 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700398 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700399 result = my_data->instance_dispatch_table->CreateXcbSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600400 lock.lock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700401
Ian Elliott32311832016-02-04 08:17:18 -0700402 // Obtain this pointer again after locking:
Ian Elliott77f46ca2016-05-05 14:10:49 -0600403 {
404 auto it = my_data->instanceMap.find(instance);
405 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
406 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700407 if ((result == VK_SUCCESS) && pInstance && pSurface) {
408 // Record the VkSurfaceKHR returned by the ICD:
409 my_data->surfaceMap[*pSurface].surface = *pSurface;
410 my_data->surfaceMap[*pSurface].pInstance = pInstance;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700411 // Point to the associated SwpInstance:
412 pInstance->surfaces[*pSurface] = &my_data->surfaceMap[*pSurface];
413 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600414 lock.unlock();
415
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700416 return result;
417 }
418 return VK_ERROR_VALIDATION_FAILED_EXT;
419}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700420#endif // VK_USE_PLATFORM_XCB_KHR
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700421
422#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600423VKAPI_ATTR VkResult VKAPI_CALL CreateXlibSurfaceKHR(VkInstance instance, const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
424 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700425 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600426 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700427 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600428 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600429 SwpInstance *pInstance = NULL;
430 {
431 auto it = my_data->instanceMap.find(instance);
432 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
433 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700434
Ian Elliott970a2bd2016-06-21 11:08:43 -0600435 lock.unlock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700436
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600437 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700438 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700439 result = my_data->instance_dispatch_table->CreateXlibSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600440 lock.lock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700441
Ian Elliott32311832016-02-04 08:17:18 -0700442 // Obtain this pointer again after locking:
Ian Elliott77f46ca2016-05-05 14:10:49 -0600443 {
444 auto it = my_data->instanceMap.find(instance);
445 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
446 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700447 if ((result == VK_SUCCESS) && pInstance && pSurface) {
448 // Record the VkSurfaceKHR returned by the ICD:
449 my_data->surfaceMap[*pSurface].surface = *pSurface;
450 my_data->surfaceMap[*pSurface].pInstance = pInstance;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700451 // Point to the associated SwpInstance:
452 pInstance->surfaces[*pSurface] = &my_data->surfaceMap[*pSurface];
453 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600454 lock.unlock();
455
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700456 return result;
457 }
458 return VK_ERROR_VALIDATION_FAILED_EXT;
459}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700460#endif // VK_USE_PLATFORM_XLIB_KHR
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700461
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600462VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
463 VkDisplayPlanePropertiesKHR *pProperties) {
Petros Bantolas2b40be72016-04-15 11:02:59 +0100464 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600465 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700466 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Petros Bantolas2b40be72016-04-15 11:02:59 +0100467 std::unique_lock<std::mutex> lock(global_lock);
468 SwpPhysicalDevice *pPhysicalDevice = NULL;
469 {
470 auto it = my_data->physicalDeviceMap.find(physicalDevice);
471 pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
472 }
Petros Bantolas2b40be72016-04-15 11:02:59 +0100473 lock.unlock();
474
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600475 if (!skip_call) {
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600476 result = my_data->instance_dispatch_table->GetPhysicalDeviceDisplayPlanePropertiesKHR(physicalDevice, pPropertyCount,
477 pProperties);
Petros Bantolas2b40be72016-04-15 11:02:59 +0100478
479 lock.lock();
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600480 if (!pPhysicalDevice->gotDisplayPlanePropertyCount) {
Petros Bantolas2b40be72016-04-15 11:02:59 +0100481 pPhysicalDevice->displayPlanePropertyCount = *pPropertyCount;
482 pPhysicalDevice->gotDisplayPlanePropertyCount = true;
483 }
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600484 // TODO store the properties for later checks
Petros Bantolas2b40be72016-04-15 11:02:59 +0100485 lock.unlock();
486
487 return result;
488 }
489 return VK_ERROR_VALIDATION_FAILED_EXT;
490}
491
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600492VKAPI_ATTR VkResult VKAPI_CALL GetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
493 uint32_t *pDisplayCount, VkDisplayKHR *pDisplays) {
Petros Bantolas2b40be72016-04-15 11:02:59 +0100494 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600495 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700496 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Petros Bantolas2b40be72016-04-15 11:02:59 +0100497 std::unique_lock<std::mutex> lock(global_lock);
498 SwpPhysicalDevice *pPhysicalDevice = NULL;
499 {
500 auto it = my_data->physicalDeviceMap.find(physicalDevice);
501 pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
502 }
503
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600504 if (!pPhysicalDevice->gotDisplayPlanePropertyCount) {
505 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
506 reinterpret_cast<uint64_t>(pPhysicalDevice->pInstance->instance), __LINE__,
507 SWAPCHAIN_GET_SUPPORTED_DISPLAYS_WITHOUT_QUERY, swapchain_layer_name,
508 "Potential problem with calling vkGetDisplayPlaneSupportedDisplaysKHR() without first "
509 "querying vkGetPhysicalDeviceDisplayPlanePropertiesKHR.");
510 }
511
512 if (pPhysicalDevice->gotDisplayPlanePropertyCount && planeIndex >= pPhysicalDevice->displayPlanePropertyCount) {
513 skip_call |=
514 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
Mike Weiblen6a27de52016-12-09 17:36:28 -0700515 reinterpret_cast<uint64_t>(pPhysicalDevice->pInstance->instance), __LINE__, VALIDATION_ERROR_01857,
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600516 swapchain_layer_name,
Mark Lobodzinski9b482fa2016-08-08 09:38:42 -0600517 "vkGetDisplayPlaneSupportedDisplaysKHR(): planeIndex must be in the range [0, %d] that was returned by "
Mike Weiblen6a27de52016-12-09 17:36:28 -0700518 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR. Do you have the plane index hardcoded? %s",
519 pPhysicalDevice->displayPlanePropertyCount - 1, validation_error_map[VALIDATION_ERROR_01857]);
Petros Bantolas2b40be72016-04-15 11:02:59 +0100520 }
521 lock.unlock();
522
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600523 if (!skip_call) {
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600524 result = my_data->instance_dispatch_table->GetDisplayPlaneSupportedDisplaysKHR(physicalDevice, planeIndex, pDisplayCount,
525 pDisplays);
Petros Bantolas2b40be72016-04-15 11:02:59 +0100526
527 return result;
528 }
Jon Ashburne699f442016-06-30 09:01:27 -0600529 // TODO validate the returned display objects
Petros Bantolas2b40be72016-04-15 11:02:59 +0100530 return VK_ERROR_VALIDATION_FAILED_EXT;
531}
532
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600533VKAPI_ATTR VkResult VKAPI_CALL GetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
534 uint32_t planeIndex, VkDisplayPlaneCapabilitiesKHR *pCapabilities) {
Petros Bantolas2b40be72016-04-15 11:02:59 +0100535 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600536 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700537 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Petros Bantolas2b40be72016-04-15 11:02:59 +0100538 std::unique_lock<std::mutex> lock(global_lock);
539 SwpPhysicalDevice *pPhysicalDevice = NULL;
540 {
541 auto it = my_data->physicalDeviceMap.find(physicalDevice);
542 pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
543 }
544
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600545 if (!pPhysicalDevice->gotDisplayPlanePropertyCount) {
546 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
547 reinterpret_cast<uint64_t>(pPhysicalDevice->pInstance->instance), __LINE__,
548 SWAPCHAIN_GET_SUPPORTED_DISPLAYS_WITHOUT_QUERY, swapchain_layer_name,
549 "Potential problem with calling vkGetDisplayPlaneCapabilitiesKHR() without first "
550 "querying vkGetPhysicalDeviceDisplayPlanePropertiesKHR.");
551 }
552
553 if (pPhysicalDevice->gotDisplayPlanePropertyCount && planeIndex >= pPhysicalDevice->displayPlanePropertyCount) {
554 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
555 reinterpret_cast<uint64_t>(pPhysicalDevice->pInstance->instance), __LINE__,
556 SWAPCHAIN_PLANE_INDEX_TOO_LARGE, swapchain_layer_name,
557 "vkGetDisplayPlaneCapabilitiesKHR(): planeIndex must be in the range [0, %d] that was returned by "
558 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR. Do you have the plane index hardcoded?",
559 pPhysicalDevice->displayPlanePropertyCount - 1);
Petros Bantolas2b40be72016-04-15 11:02:59 +0100560 }
561
Petros Bantolas2b40be72016-04-15 11:02:59 +0100562 lock.unlock();
563
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600564 if (!skip_call) {
Petros Bantolas2b40be72016-04-15 11:02:59 +0100565 result = my_data->instance_dispatch_table->GetDisplayPlaneCapabilitiesKHR(physicalDevice, mode, planeIndex, pCapabilities);
566 return result;
567 }
568
569 return VK_ERROR_VALIDATION_FAILED_EXT;
570}
571
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600572VKAPI_ATTR VkResult VKAPI_CALL CreateDisplayPlaneSurfaceKHR(VkInstance instance, const VkDisplaySurfaceCreateInfoKHR *pCreateInfo,
573 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
Petros Bantolas2b40be72016-04-15 11:02:59 +0100574 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600575 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700576 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Petros Bantolas2b40be72016-04-15 11:02:59 +0100577 std::unique_lock<std::mutex> lock(global_lock);
578 SwpInstance *pInstance = &(my_data->instanceMap[instance]);
579
Jon Ashburne699f442016-06-30 09:01:27 -0600580 // TODO more validation checks
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600581 if (!skip_call) {
Petros Bantolas2b40be72016-04-15 11:02:59 +0100582 // Call down the call chain:
583 lock.unlock();
584 result = my_data->instance_dispatch_table->CreateDisplayPlaneSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
585 lock.lock();
586
587 // Obtain this pointer again after locking:
588 pInstance = &(my_data->instanceMap[instance]);
589 if ((result == VK_SUCCESS) && pInstance && pSurface) {
590 // Record the VkSurfaceKHR returned by the ICD:
591 my_data->surfaceMap[*pSurface].surface = *pSurface;
592 my_data->surfaceMap[*pSurface].pInstance = pInstance;
Petros Bantolas2b40be72016-04-15 11:02:59 +0100593 // Point to the associated SwpInstance:
594 pInstance->surfaces[*pSurface] = &my_data->surfaceMap[*pSurface];
595 }
596 lock.unlock();
597 return result;
598 }
599 return VK_ERROR_VALIDATION_FAILED_EXT;
600}
601
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600602VKAPI_ATTR void VKAPI_CALL DestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface, const VkAllocationCallbacks *pAllocator) {
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600603 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700604 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600605 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600606 SwpSurface *pSurface = NULL;
607 {
608 auto it = my_data->surfaceMap.find(surface);
609 pSurface = (it == my_data->surfaceMap.end()) ? NULL : &it->second;
610 }
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700611
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600612 // Regardless of skip_call value, do some internal cleanup:
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700613 if (pSurface) {
614 // Delete the SwpSurface associated with this surface:
615 if (pSurface->pInstance) {
616 pSurface->pInstance->surfaces.erase(surface);
617 }
618 if (!pSurface->swapchains.empty()) {
Mike Weiblen6a27de52016-12-09 17:36:28 -0700619 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
620 reinterpret_cast<uint64_t>(instance), __LINE__, VALIDATION_ERROR_01844, swapchain_layer_name,
621 "vkDestroySurfaceKHR() called before all of its associated VkSwapchainKHRs were destroyed. %s",
622 validation_error_map[VALIDATION_ERROR_01844]);
Mark Lobodzinski9b482fa2016-08-08 09:38:42 -0600623
624 // Empty and then delete all SwpSwapchains
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700625 for (auto it = pSurface->swapchains.begin(); it != pSurface->swapchains.end(); it++) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700626 // Delete all SwpImage's
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700627 // In case the swapchain's device hasn't been destroyed yet
628 // (which isn't likely, but is possible), delete its
629 // association with this swapchain (i.e. so we can't point to
630 // this swpchain from that device, later on):
631 if (it->second->pDevice) {
632 it->second->pDevice->swapchains.clear();
633 }
634 }
635 pSurface->swapchains.clear();
636 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700637 my_data->surfaceMap.erase(surface);
638 }
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600639 lock.unlock();
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700640
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600641 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700642 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700643 my_data->instance_dispatch_table->DestroySurfaceKHR(instance, surface, pAllocator);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700644 }
645}
646
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600647VKAPI_ATTR VkResult VKAPI_CALL EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
648 VkPhysicalDevice *pPhysicalDevices) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700649 VkResult result = VK_SUCCESS;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700650 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Ian Elliott32311832016-02-04 08:17:18 -0700651
652 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700653 result = my_data->instance_dispatch_table->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Ian Elliott32311832016-02-04 08:17:18 -0700654
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600655 std::lock_guard<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600656 SwpInstance *pInstance = NULL;
657 {
658 auto it = my_data->instanceMap.find(instance);
659 pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
660 }
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700661 if ((result == VK_SUCCESS) && pInstance && pPhysicalDevices && (*pPhysicalDeviceCount > 0)) {
Ian Elliott32311832016-02-04 08:17:18 -0700662 // Record the VkPhysicalDevices returned by the ICD:
663 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700664 my_data->physicalDeviceMap[pPhysicalDevices[i]].physicalDevice = pPhysicalDevices[i];
Ian Elliott32311832016-02-04 08:17:18 -0700665 my_data->physicalDeviceMap[pPhysicalDevices[i]].pInstance = pInstance;
666 my_data->physicalDeviceMap[pPhysicalDevices[i]].pDevice = NULL;
Ian Elliott32311832016-02-04 08:17:18 -0700667 // Point to the associated SwpInstance:
668 if (pInstance) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700669 pInstance->physicalDevices[pPhysicalDevices[i]] = &my_data->physicalDeviceMap[pPhysicalDevices[i]];
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700670 }
671 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700672 }
Ian Elliotta3c69bc2016-02-04 15:34:59 -0700673 return result;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700674}
675
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600676VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
Chia-I Wufccbfe42016-04-28 14:01:30 +0800677 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700678 layer_data *my_instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700679 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
680
681 assert(chain_info->u.pLayerInfo);
682 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
683 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
Chia-I Wua6737532016-04-28 16:04:15 +0800684 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(my_instance_data->instance, "vkCreateDevice");
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700685 if (fpCreateDevice == NULL) {
686 return VK_ERROR_INITIALIZATION_FAILED;
687 }
688
689 // Advance the link info for the next element on the chain
690 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
691
692 VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
693 if (result != VK_SUCCESS) {
694 return result;
695 }
696
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600697 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700698 layer_data *my_device_data = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700699
700 // Setup device dispatch table
701 my_device_data->device_dispatch_table = new VkLayerDispatchTable;
702 layer_init_device_dispatch_table(*pDevice, my_device_data->device_dispatch_table, fpGetDeviceProcAddr);
703
704 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600705 checkDeviceRegisterExtensions(physicalDevice, pCreateInfo, *pDevice);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700706
707 return result;
708}
709
Chia-I Wufccbfe42016-04-28 14:01:30 +0800710VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700711 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700712 layer_data *my_data = GetLayerDataPtr(key, layer_data_map);
Ian Elliott32311832016-02-04 08:17:18 -0700713
714 // Call down the call chain:
715 my_data->device_dispatch_table->DestroyDevice(device, pAllocator);
716
717 // Do some internal cleanup:
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600718 std::lock_guard<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600719 SwpDevice *pDevice = NULL;
720 {
721 auto it = my_data->deviceMap.find(device);
722 pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
723 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700724 if (pDevice) {
725 // Delete the SwpDevice associated with this device:
726 if (pDevice->pPhysicalDevice) {
727 pDevice->pPhysicalDevice->pDevice = NULL;
728 }
729 if (!pDevice->swapchains.empty()) {
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600730 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
Mike Weiblen6a27de52016-12-09 17:36:28 -0700731 reinterpret_cast<uint64_t>(device), __LINE__, VALIDATION_ERROR_00049, swapchain_layer_name,
732 "vkDestroyDevice() called before all of its associated VkSwapchainKHRs were destroyed. %s",
733 validation_error_map[VALIDATION_ERROR_00049]);
Mark Lobodzinski9b482fa2016-08-08 09:38:42 -0600734
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700735 // Empty and then delete all SwpSwapchain's
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700736 for (auto it = pDevice->swapchains.begin(); it != pDevice->swapchains.end(); it++) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700737 // Delete all SwpImage's
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700738 // In case the swapchain's surface hasn't been destroyed yet
739 // (which is likely) delete its association with this swapchain
740 // (i.e. so we can't point to this swpchain from that surface,
741 // later on):
742 if (it->second->pSurface) {
743 it->second->pSurface->swapchains.clear();
744 }
745 }
746 pDevice->swapchains.clear();
747 }
748 my_data->deviceMap.erase(device);
749 }
750 delete my_data->device_dispatch_table;
751 layer_data_map.erase(key);
752}
753
Chia-I Wufccbfe42016-04-28 14:01:30 +0800754VKAPI_ATTR VkResult VKAPI_CALL CreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo,
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600755 const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700756 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Petr Kraus97291752017-05-11 01:05:16 +0200757
758 // Call down the call chain:
759 VkResult result = my_data->device_dispatch_table->CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600760 std::unique_lock<std::mutex> lock(global_lock);
Petr Kraus97291752017-05-11 01:05:16 +0200761
762 if (result == VK_SUCCESS) {
763 // Remember the swapchain's handle, and link it to the device:
764 SwpDevice *pDevice = NULL;
765 {
766 auto it = my_data->deviceMap.find(device);
767 pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
768 }
769
770 my_data->swapchainMap[*pSwapchain].swapchain = *pSwapchain;
771 if (pDevice) {
772 pDevice->swapchains[*pSwapchain] = &my_data->swapchainMap[*pSwapchain];
773 }
774 my_data->swapchainMap[*pSwapchain].pDevice = pDevice;
775 my_data->swapchainMap[*pSwapchain].imageCount = 0;
776 // Store a pointer to the surface
777 SwpPhysicalDevice *pPhysicalDevice = pDevice->pPhysicalDevice;
778 SwpInstance *pInstance = (pPhysicalDevice) ? pPhysicalDevice->pInstance : NULL;
779 layer_data *my_instance_data =
780 ((pInstance) ? GetLayerDataPtr(get_dispatch_key(pInstance->instance), layer_data_map) : NULL);
781 SwpSurface *pSurface = ((my_data && pCreateInfo) ? &my_instance_data->surfaceMap[pCreateInfo->surface] : NULL);
782 my_data->swapchainMap[*pSwapchain].pSurface = pSurface;
783 if (pSurface) {
784 pSurface->swapchains[*pSwapchain] = &my_data->swapchainMap[*pSwapchain];
785 }
786 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600787 lock.unlock();
Ian Elliott0b4d6242015-09-22 10:51:24 -0600788
Petr Kraus97291752017-05-11 01:05:16 +0200789 return result;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700790}
Ian Elliott0b4d6242015-09-22 10:51:24 -0600791
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600792VKAPI_ATTR void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700793 // TODOs:
794 //
795 // - Implement a check for validity language that reads: All uses of
Ian Elliotta5d13a92016-04-07 09:05:45 -0600796 // presentable images acquired from pname:swapchain must: have completed
797 // execution
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600798 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700799 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600800 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott0b4d6242015-09-22 10:51:24 -0600801
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600802 // Regardless of skip_call value, do some internal cleanup:
Ian Elliott77f46ca2016-05-05 14:10:49 -0600803 SwpSwapchain *pSwapchain = NULL;
804 {
805 auto it = my_data->swapchainMap.find(swapchain);
806 pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
807 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700808 if (pSwapchain) {
809 // Delete the SwpSwapchain associated with this swapchain:
810 if (pSwapchain->pDevice) {
811 pSwapchain->pDevice->swapchains.erase(swapchain);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700812 }
813 if (pSwapchain->pSurface) {
814 pSwapchain->pSurface->swapchains.erase(swapchain);
815 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700816 my_data->swapchainMap.erase(swapchain);
817 }
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600818 lock.unlock();
Ian Elliott0b4d6242015-09-22 10:51:24 -0600819
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600820 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700821 // Call down the call chain:
822 my_data->device_dispatch_table->DestroySwapchainKHR(device, swapchain, pAllocator);
823 }
824}
Ian Elliott0b4d6242015-09-22 10:51:24 -0600825
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600826VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount,
827 VkImage *pSwapchainImages) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700828 VkResult result = VK_SUCCESS;
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600829 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700830 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600831 std::unique_lock<std::mutex> lock(global_lock);
Ian Elliott8b9e2562016-01-05 13:00:50 -0700832
Ian Elliott77f46ca2016-05-05 14:10:49 -0600833 SwpSwapchain *pSwapchain = NULL;
834 {
835 auto it = my_data->swapchainMap.find(swapchain);
836 pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
837 }
Mark Lobodzinski0f73c0b2016-08-08 09:57:09 -0600838 if (pSwapchain && pSwapchainImages) {
839 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Ian Elliottfdf3ffa2016-05-05 14:06:53 -0600840 if (pSwapchain->imageCount == 0) {
Mark Lobodzinski0f73c0b2016-08-08 09:57:09 -0600841 // Since we haven't recorded a preliminary value of *pSwapchainImageCount, that likely means that the application didn't
842 // previously call this function with a NULL value of pSwapchainImages:
Mark Lobodzinski6a7e9332016-08-16 09:06:15 -0600843 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600844 reinterpret_cast<uint64_t>(device), __LINE__, SWAPCHAIN_PRIOR_COUNT, swapchain_layer_name,
845 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive "
846 "value has been seen for pSwapchainImages.");
Ian Elliottfdf3ffa2016-05-05 14:06:53 -0600847 } else if (*pSwapchainImageCount > pSwapchain->imageCount) {
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600848 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600849 reinterpret_cast<uint64_t>(device), __LINE__, SWAPCHAIN_INVALID_COUNT, swapchain_layer_name,
850 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount, and with "
851 "pSwapchainImages set to a value (%d) that is greater than the value (%d) that was returned when "
852 "pSwapchainImageCount was NULL.",
853 *pSwapchainImageCount, pSwapchain->imageCount);
Ian Elliottfdf3ffa2016-05-05 14:06:53 -0600854 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700855 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600856 lock.unlock();
Ian Elliott0b4d6242015-09-22 10:51:24 -0600857
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600858 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700859 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700860 result = my_data->device_dispatch_table->GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600861 lock.lock();
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700862
Ian Elliott32311832016-02-04 08:17:18 -0700863 // Obtain this pointer again after locking:
Ian Elliott77f46ca2016-05-05 14:10:49 -0600864 {
865 auto it = my_data->swapchainMap.find(swapchain);
866 pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
867 }
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700868 if ((result == VK_SUCCESS) && pSwapchain && !pSwapchainImages && pSwapchainImageCount) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700869 // Record the result of this preliminary query:
870 pSwapchain->imageCount = *pSwapchainImageCount;
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600871 } else if ((result == VK_SUCCESS) && pSwapchain && pSwapchainImages && pSwapchainImageCount &&
872 (*pSwapchainImageCount > 0)) {
Ian Elliottfdf3ffa2016-05-05 14:06:53 -0600873 // Record the images and their state:
874 pSwapchain->imageCount = *pSwapchainImageCount;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700875 }
Ian Elliott970a2bd2016-06-21 11:08:43 -0600876 lock.unlock();
877
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700878 return result;
879 }
880 return VK_ERROR_VALIDATION_FAILED_EXT;
881}
Ian Elliott0b4d6242015-09-22 10:51:24 -0600882
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700883VKAPI_ATTR void VKAPI_CALL GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue *pQueue) {
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600884 bool skip_call = false;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700885 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Ian Elliottc4db6952016-01-21 14:29:45 -0700886
Mark Lobodzinski38ece432016-08-08 14:36:58 -0600887 if (!skip_call) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700888 // Call down the call chain:
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700889 my_data->device_dispatch_table->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
Ian Elliottc4db6952016-01-21 14:29:45 -0700890
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700891 // Remember the queue's handle, and link it to the device:
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600892 std::lock_guard<std::mutex> lock(global_lock);
Ian Elliott77f46ca2016-05-05 14:10:49 -0600893 SwpDevice *pDevice = NULL;
894 {
895 auto it = my_data->deviceMap.find(device);
896 pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
897 }
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700898 my_data->queueMap[&pQueue].queue = *pQueue;
899 if (pDevice) {
900 pDevice->queues[*pQueue] = &my_data->queueMap[*pQueue];
901 }
902 my_data->queueMap[&pQueue].pDevice = pDevice;
903 my_data->queueMap[&pQueue].queueFamilyIndex = queueFamilyIndex;
904 }
905}
Ian Elliottc4db6952016-01-21 14:29:45 -0700906
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600907VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(VkInstance instance,
908 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
909 const VkAllocationCallbacks *pAllocator,
910 VkDebugReportCallbackEXT *pMsgCallback) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700911 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700912 VkResult result =
913 my_data->instance_dispatch_table->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700914 if (VK_SUCCESS == result) {
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600915 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600916 result = layer_create_msg_callback(my_data->report_data, false, pCreateInfo, pAllocator, pMsgCallback);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700917 }
918 return result;
919}
Ian Elliott0b4d6242015-09-22 10:51:24 -0600920
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600921VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback,
Chia-I Wufccbfe42016-04-28 14:01:30 +0800922 const VkAllocationCallbacks *pAllocator) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700923 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700924 my_data->instance_dispatch_table->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
Jeremy Hayes9de0bd72016-04-13 11:57:20 -0600925 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700926 layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);
927}
Ian Elliott0b4d6242015-09-22 10:51:24 -0600928
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600929VKAPI_ATTR void VKAPI_CALL DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
930 VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
931 int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700932 layer_data *my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700933 my_data->instance_dispatch_table->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix,
934 pMsg);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700935}
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700936
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600937VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
Chia-I Wub02600c2016-05-20 07:11:22 +0800938 return util_GetLayerProperties(1, &swapchain_layer, pCount, pProperties);
939}
940
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600941VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
942 VkLayerProperties *pProperties) {
Chia-I Wub02600c2016-05-20 07:11:22 +0800943 return util_GetLayerProperties(1, &swapchain_layer, pCount, pProperties);
944}
945
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600946VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
947 VkExtensionProperties *pProperties) {
Chia-I Wub02600c2016-05-20 07:11:22 +0800948 if (pLayerName && !strcmp(pLayerName, swapchain_layer.layerName))
949 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
950
951 return VK_ERROR_LAYER_NOT_PRESENT;
952}
953
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600954VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
955 uint32_t *pCount, VkExtensionProperties *pProperties) {
Chia-I Wu2b481252016-04-28 14:21:13 +0800956 if (pLayerName && !strcmp(pLayerName, swapchain_layer.layerName))
Chia-I Wu045209e2016-04-28 11:21:49 +0800957 return util_GetExtensionProperties(0, nullptr, pCount, pProperties);
Chia-I Wu2b481252016-04-28 14:21:13 +0800958
959 assert(physicalDevice);
960
961 dispatch_key key = get_dispatch_key(physicalDevice);
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700962 layer_data *my_data = GetLayerDataPtr(key, layer_data_map);
Chia-I Wu2b481252016-04-28 14:21:13 +0800963 return my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
Chia-I Wu045209e2016-04-28 11:21:49 +0800964}
965
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600966static PFN_vkVoidFunction intercept_core_instance_command(const char *name);
Chia-I Wu22813c72016-04-28 14:38:57 +0800967
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600968static PFN_vkVoidFunction intercept_khr_surface_command(const char *name, VkInstance instance);
Chia-I Wu22813c72016-04-28 14:38:57 +0800969
Mike Schuchardt046e3cf2017-03-10 14:12:35 -0700970static PFN_vkVoidFunction intercept_extension_instance_commands(const char *name);
971
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600972static PFN_vkVoidFunction intercept_core_device_command(const char *name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700973
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600974static PFN_vkVoidFunction intercept_khr_swapchain_command(const char *name, VkDevice dev);
Chia-I Wu83245952016-05-05 16:13:19 +0800975
Chia-I Wu9bc0b582016-04-28 14:38:57 +0800976VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
977 PFN_vkVoidFunction proc = intercept_core_device_command(funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700978 if (proc) return proc;
Chia-I Wu9bc0b582016-04-28 14:38:57 +0800979
980 assert(device);
Ian Elliott0b4d6242015-09-22 10:51:24 -0600981
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700982 layer_data *my_data;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600983
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700984 my_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -0600985 VkLayerDispatchTable *pDisp = my_data->device_dispatch_table;
Chia-I Wu83245952016-05-05 16:13:19 +0800986
987 proc = intercept_khr_swapchain_command(funcName, device);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700988 if (proc) return proc;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700989
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700990 if (pDisp->GetDeviceProcAddr == NULL) return NULL;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700991 return pDisp->GetDeviceProcAddr(device, funcName);
992}
Ian Elliott0b4d6242015-09-22 10:51:24 -0600993
Chia-I Wufccbfe42016-04-28 14:01:30 +0800994VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
Chia-I Wu22813c72016-04-28 14:38:57 +0800995 PFN_vkVoidFunction proc = intercept_core_instance_command(funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700996 if (!proc) proc = intercept_core_device_command(funcName);
997 if (!proc) proc = intercept_khr_swapchain_command(funcName, VK_NULL_HANDLE);
998 if (proc) return proc;
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -0700999
Chia-I Wu5ae2f652016-04-28 15:16:59 +08001000 assert(instance);
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -07001001
1002 layer_data *my_data;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -07001003 my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Jon Ashburn5484e0c2016-03-08 17:48:44 -07001004 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Ian Elliott68124ac2015-10-07 16:18:35 -06001005
Chia-I Wu5ae2f652016-04-28 15:16:59 +08001006 proc = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001007 if (!proc) proc = intercept_khr_surface_command(funcName, instance);
Mike Schuchardt046e3cf2017-03-10 14:12:35 -07001008 if (!proc) proc = intercept_extension_instance_commands(funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001009 if (proc) return proc;
Ian Elliott0b4d6242015-09-22 10:51:24 -06001010
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001011 if (pTable->GetInstanceProcAddr == NULL) return NULL;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -07001012 return pTable->GetInstanceProcAddr(instance, funcName);
1013}
Chia-I Wu045209e2016-04-28 11:21:49 +08001014
Mark Young39389872017-01-19 21:10:49 -07001015VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
1016 assert(instance);
1017
1018 layer_data *my_data;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -07001019 my_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Mark Young39389872017-01-19 21:10:49 -07001020 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
1021
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001022 if (pTable->GetPhysicalDeviceProcAddr == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07001023 return pTable->GetPhysicalDeviceProcAddr(instance, funcName);
1024}
1025
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001026static PFN_vkVoidFunction intercept_core_instance_command(const char *name) {
Chia-I Wu22813c72016-04-28 14:38:57 +08001027 static const struct {
1028 const char *name;
1029 PFN_vkVoidFunction proc;
1030 } core_instance_commands[] = {
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001031 {"vkGetInstanceProcAddr", reinterpret_cast<PFN_vkVoidFunction>(GetInstanceProcAddr)},
1032 {"vkCreateInstance", reinterpret_cast<PFN_vkVoidFunction>(CreateInstance)},
1033 {"vkDestroyInstance", reinterpret_cast<PFN_vkVoidFunction>(DestroyInstance)},
1034 {"vkCreateDevice", reinterpret_cast<PFN_vkVoidFunction>(CreateDevice)},
1035 {"vkEnumeratePhysicalDevices", reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDevices)},
Mark Young39389872017-01-19 21:10:49 -07001036 {"vk_layerGetPhysicalDeviceProcAddr", reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceProcAddr)},
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001037 {"vkEnumerateInstanceLayerProperties", reinterpret_cast<PFN_vkVoidFunction>(EnumerateInstanceLayerProperties)},
1038 {"vkEnumerateDeviceLayerProperties", reinterpret_cast<PFN_vkVoidFunction>(EnumerateDeviceLayerProperties)},
1039 {"vkEnumerateInstanceExtensionProperties", reinterpret_cast<PFN_vkVoidFunction>(EnumerateInstanceExtensionProperties)},
1040 {"vkEnumerateDeviceExtensionProperties", reinterpret_cast<PFN_vkVoidFunction>(EnumerateDeviceExtensionProperties)},
Chia-I Wu22813c72016-04-28 14:38:57 +08001041 };
1042
1043 for (size_t i = 0; i < ARRAY_SIZE(core_instance_commands); i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001044 if (!strcmp(core_instance_commands[i].name, name)) return core_instance_commands[i].proc;
Chia-I Wu22813c72016-04-28 14:38:57 +08001045 }
1046
1047 return nullptr;
1048}
1049
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001050static PFN_vkVoidFunction intercept_khr_surface_command(const char *name, VkInstance instance) {
Chia-I Wu22813c72016-04-28 14:38:57 +08001051 static const struct {
1052 const char *name;
1053 PFN_vkVoidFunction proc;
1054 } khr_surface_commands[] = {
1055#ifdef VK_USE_PLATFORM_ANDROID_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001056 {"vkCreateAndroidSurfaceKHR", reinterpret_cast<PFN_vkVoidFunction>(CreateAndroidSurfaceKHR)},
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001057#endif // VK_USE_PLATFORM_ANDROID_KHR
Chia-I Wu22813c72016-04-28 14:38:57 +08001058#ifdef VK_USE_PLATFORM_MIR_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001059 {"vkCreateMirSurfaceKHR", reinterpret_cast<PFN_vkVoidFunction>(CreateMirSurfaceKHR)},
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001060#endif // VK_USE_PLATFORM_MIR_KHR
Chia-I Wu22813c72016-04-28 14:38:57 +08001061#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001062 {"vkCreateWaylandSurfaceKHR", reinterpret_cast<PFN_vkVoidFunction>(CreateWaylandSurfaceKHR)},
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001063#endif // VK_USE_PLATFORM_WAYLAND_KHR
Chia-I Wu22813c72016-04-28 14:38:57 +08001064#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001065 {"vkCreateWin32SurfaceKHR", reinterpret_cast<PFN_vkVoidFunction>(CreateWin32SurfaceKHR)},
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001066#endif // VK_USE_PLATFORM_WIN32_KHR
Chia-I Wu22813c72016-04-28 14:38:57 +08001067#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001068 {"vkCreateXcbSurfaceKHR", reinterpret_cast<PFN_vkVoidFunction>(CreateXcbSurfaceKHR)},
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001069#endif // VK_USE_PLATFORM_XCB_KHR
Chia-I Wu22813c72016-04-28 14:38:57 +08001070#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001071 {"vkCreateXlibSurfaceKHR", reinterpret_cast<PFN_vkVoidFunction>(CreateXlibSurfaceKHR)},
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001072#endif // VK_USE_PLATFORM_XLIB_KHR
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001073 {"vkDestroySurfaceKHR", reinterpret_cast<PFN_vkVoidFunction>(DestroySurfaceKHR)},
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001074 {"vkGetPhysicalDeviceDisplayPlanePropertiesKHR",
1075 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceDisplayPlanePropertiesKHR)},
1076 {"vkGetDisplayPlaneSupportedDisplaysKHR", reinterpret_cast<PFN_vkVoidFunction>(GetDisplayPlaneSupportedDisplaysKHR)},
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001077 {"vkGetDisplayPlaneCapabilitiesKHR", reinterpret_cast<PFN_vkVoidFunction>(GetDisplayPlaneCapabilitiesKHR)},
1078 {"vkCreateDisplayPlaneSurfaceKHR", reinterpret_cast<PFN_vkVoidFunction>(CreateDisplayPlaneSurfaceKHR)},
Chia-I Wu22813c72016-04-28 14:38:57 +08001079 };
1080
1081 // do not check if VK_KHR_*_surface is enabled (why?)
1082
1083 for (size_t i = 0; i < ARRAY_SIZE(khr_surface_commands); i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001084 if (!strcmp(khr_surface_commands[i].name, name)) return khr_surface_commands[i].proc;
Chia-I Wu22813c72016-04-28 14:38:57 +08001085 }
1086
1087 return nullptr;
1088}
1089
Mike Schuchardt046e3cf2017-03-10 14:12:35 -07001090static PFN_vkVoidFunction intercept_extension_instance_commands(const char *name) {
Mike Schuchardt046e3cf2017-03-10 14:12:35 -07001091 return nullptr;
1092}
1093
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001094static PFN_vkVoidFunction intercept_core_device_command(const char *name) {
Chia-I Wu9bc0b582016-04-28 14:38:57 +08001095 static const struct {
1096 const char *name;
1097 PFN_vkVoidFunction proc;
1098 } core_device_commands[] = {
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001099 {"vkGetDeviceProcAddr", reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr)},
1100 {"vkDestroyDevice", reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice)},
1101 {"vkGetDeviceQueue", reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue)},
Chia-I Wu9bc0b582016-04-28 14:38:57 +08001102 };
1103
1104 for (size_t i = 0; i < ARRAY_SIZE(core_device_commands); i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001105 if (!strcmp(core_device_commands[i].name, name)) return core_device_commands[i].proc;
Chia-I Wu9bc0b582016-04-28 14:38:57 +08001106 }
1107
1108 return nullptr;
1109}
1110
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001111static PFN_vkVoidFunction intercept_khr_swapchain_command(const char *name, VkDevice dev) {
Chia-I Wu83245952016-05-05 16:13:19 +08001112 static const struct {
1113 const char *name;
1114 PFN_vkVoidFunction proc;
1115 } khr_swapchain_commands[] = {
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001116 {"vkCreateSwapchainKHR", reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR)},
1117 {"vkDestroySwapchainKHR", reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR)},
1118 {"vkGetSwapchainImagesKHR", reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR)},
Chia-I Wu83245952016-05-05 16:13:19 +08001119 };
1120
1121 // do not check if VK_KHR_swapchain is enabled (why?)
1122
1123 for (size_t i = 0; i < ARRAY_SIZE(khr_swapchain_commands); i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001124 if (!strcmp(khr_swapchain_commands[i].name, name)) return khr_swapchain_commands[i].proc;
Chia-I Wu83245952016-05-05 16:13:19 +08001125 }
1126
1127 return nullptr;
1128}
1129
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001130} // namespace swapchain
Chia-I Wu516b5082016-04-28 11:27:46 +08001131
1132// vk_layer_logging.h expects these to be defined
1133
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001134VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugReportCallbackEXT(VkInstance instance,
1135 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
1136 const VkAllocationCallbacks *pAllocator,
1137 VkDebugReportCallbackEXT *pMsgCallback) {
Chia-I Wufccbfe42016-04-28 14:01:30 +08001138 return swapchain::CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
Chia-I Wu516b5082016-04-28 11:27:46 +08001139}
1140
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001141VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback,
1142 const VkAllocationCallbacks *pAllocator) {
Chia-I Wufccbfe42016-04-28 14:01:30 +08001143 swapchain::DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
Chia-I Wu516b5082016-04-28 11:27:46 +08001144}
1145
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001146VKAPI_ATTR void VKAPI_CALL vkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
1147 VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
1148 int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
Chia-I Wufccbfe42016-04-28 14:01:30 +08001149 swapchain::DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
Chia-I Wu516b5082016-04-28 11:27:46 +08001150}
1151
Chia-I Wub02600c2016-05-20 07:11:22 +08001152// loader-layer interface v0, just wrappers since there is only a layer
Chia-I Wu516b5082016-04-28 11:27:46 +08001153
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001154VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
1155 VkExtensionProperties *pProperties) {
Chia-I Wub02600c2016-05-20 07:11:22 +08001156 return swapchain::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
Chia-I Wu045209e2016-04-28 11:21:49 +08001157}
1158
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001159VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
1160 VkLayerProperties *pProperties) {
Chia-I Wub02600c2016-05-20 07:11:22 +08001161 return swapchain::EnumerateInstanceLayerProperties(pCount, pProperties);
Chia-I Wu045209e2016-04-28 11:21:49 +08001162}
1163
Mark Lobodzinskid0ef5212016-08-08 14:41:55 -06001164VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
1165 VkLayerProperties *pProperties) {
Chia-I Wub02600c2016-05-20 07:11:22 +08001166 // the layer command handles VK_NULL_HANDLE just fine internally
1167 assert(physicalDevice == VK_NULL_HANDLE);
1168 return swapchain::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
Chia-I Wu516b5082016-04-28 11:27:46 +08001169}
1170
1171VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1172 const char *pLayerName, uint32_t *pCount,
1173 VkExtensionProperties *pProperties) {
Chia-I Wub02600c2016-05-20 07:11:22 +08001174 // the layer command handles VK_NULL_HANDLE just fine internally
1175 assert(physicalDevice == VK_NULL_HANDLE);
Chia-I Wu2b481252016-04-28 14:21:13 +08001176 return swapchain::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
Chia-I Wu516b5082016-04-28 11:27:46 +08001177}
1178
1179VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
Chia-I Wufccbfe42016-04-28 14:01:30 +08001180 return swapchain::GetDeviceProcAddr(dev, funcName);
Chia-I Wu516b5082016-04-28 11:27:46 +08001181}
1182
1183VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
Chia-I Wufccbfe42016-04-28 14:01:30 +08001184 return swapchain::GetInstanceProcAddr(instance, funcName);
Chia-I Wu045209e2016-04-28 11:21:49 +08001185}
Mark Young39389872017-01-19 21:10:49 -07001186
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001187VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1188 const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07001189 return swapchain::GetPhysicalDeviceProcAddr(instance, funcName);
1190}
1191
1192VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
1193 assert(pVersionStruct != NULL);
1194 assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
1195
1196 // Fill in the function pointers if our version is at least capable of having the structure contain them.
1197 if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
1198 pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
1199 pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
1200 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
1201 }
1202
1203 if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1204 swapchain::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
1205 } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1206 pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
1207 }
1208
1209 return VK_SUCCESS;
1210}