blob: e45b672f90b8f52553b3fae9e21c97e2382d19f5 [file] [log] [blame]
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -06001/*
2 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 * Copyright (c) 2015-2016 Google, Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Tobin Ehlis <tobine@google.com>
20 * Author: Mark Lobodzinski <mark@lunarg.com>
21 */
22
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -070023#define NOMINMAX
24
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unordered_map>
29#include <vector>
30#include <list>
31#include <memory>
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -070032#include <algorithm>
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060033
Mike Weiblen6a27de52016-12-09 17:36:28 -070034// For Windows, this #include must come before other Vk headers.
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060035#include "vk_loader_platform.h"
Mike Weiblen6a27de52016-12-09 17:36:28 -070036
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060037#include "unique_objects.h"
38#include "vk_dispatch_table_helper.h"
Mike Weiblen6a27de52016-12-09 17:36:28 -070039#include "vk_layer_config.h"
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060040#include "vk_layer_data.h"
Mike Weiblen6a27de52016-12-09 17:36:28 -070041#include "vk_layer_extension_utils.h"
42#include "vk_layer_logging.h"
43#include "vk_layer_table.h"
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060044#include "vk_layer_utils.h"
Mike Weiblen6a27de52016-12-09 17:36:28 -070045#include "vk_layer_utils.h"
Mark Lobodzinski9acd2e32016-12-21 15:22:39 -070046#include "vk_enum_string_helper.h"
Mike Weiblen6a27de52016-12-09 17:36:28 -070047#include "vk_validation_error_messages.h"
Mark Lobodzinski2d9de652017-04-24 08:58:52 -060048#include "vk_object_types.h"
Mark Lobodzinski75a46312018-01-03 11:23:55 -070049#include "vk_extension_helper.h"
Mike Weiblen6a27de52016-12-09 17:36:28 -070050#include "vulkan/vk_layer.h"
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060051
Mike Stroyanb985fca2016-11-01 11:50:16 -060052// This intentionally includes a cpp file
53#include "vk_safe_struct.cpp"
54
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060055#include "unique_objects_wrappers.h"
56
57namespace unique_objects {
58
Mark Young39389872017-01-19 21:10:49 -070059static uint32_t loader_layer_if_version = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
60
Chris Forbes5279a8c2017-05-02 16:26:23 -070061static void initUniqueObjects(instance_layer_data *instance_data, const VkAllocationCallbacks *pAllocator) {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060062 layer_debug_actions(instance_data->report_data, instance_data->logging_callback, pAllocator, "google_unique_objects");
63}
64
Mark Lobodzinskic0141472017-06-09 09:51:34 -060065// Check enabled instance extensions against supported instance extension whitelist
66static void InstanceExtensionWhitelist(const VkInstanceCreateInfo *pCreateInfo, VkInstance instance) {
Chris Forbes5279a8c2017-05-02 16:26:23 -070067 instance_layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), instance_layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060068
Mark Lobodzinski38686e92017-06-07 16:04:50 -060069 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060070 // Check for recognized instance extensions
Mark Lobodzinski75a46312018-01-03 11:23:55 -070071 if (!white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060072 log_msg(instance_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mike Weiblen6a27de52016-12-09 17:36:28 -070073 VALIDATION_ERROR_UNDEFINED, "UniqueObjects",
Dave Houltona9df0ce2018-02-07 10:51:23 -070074 "Instance Extension %s is not supported by this layer. Using this extension may adversely affect validation "
75 "results and/or produce undefined behavior.",
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060076 pCreateInfo->ppEnabledExtensionNames[i]);
77 }
78 }
79}
80
Mark Lobodzinskic0141472017-06-09 09:51:34 -060081// Check enabled device extensions against supported device extension whitelist
82static void DeviceExtensionWhitelist(const VkDeviceCreateInfo *pCreateInfo, VkDevice device) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070083 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060084
85 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060086 // Check for recognized device extensions
Mark Lobodzinski75a46312018-01-03 11:23:55 -070087 if (!white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060088 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mike Weiblen6a27de52016-12-09 17:36:28 -070089 VALIDATION_ERROR_UNDEFINED, "UniqueObjects",
Dave Houltona9df0ce2018-02-07 10:51:23 -070090 "Device Extension %s is not supported by this layer. Using this extension may adversely affect validation "
91 "results and/or produce undefined behavior.",
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060092 pCreateInfo->ppEnabledExtensionNames[i]);
93 }
94 }
95}
96
97VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
98 VkInstance *pInstance) {
99 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
100
101 assert(chain_info->u.pLayerInfo);
102 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
103 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
104 if (fpCreateInstance == NULL) {
105 return VK_ERROR_INITIALIZATION_FAILED;
106 }
107
108 // Advance the link info for the next element on the chain
109 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
110
111 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
112 if (result != VK_SUCCESS) {
113 return result;
114 }
115
Chris Forbes5279a8c2017-05-02 16:26:23 -0700116 instance_layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(*pInstance), instance_layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600117 instance_data->instance = *pInstance;
Chris Forbes44c05302017-05-02 16:42:55 -0700118 layer_init_instance_dispatch_table(*pInstance, &instance_data->dispatch_table, fpGetInstanceProcAddr);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600119
120 instance_data->instance = *pInstance;
Dave Houltona9df0ce2018-02-07 10:51:23 -0700121 instance_data->report_data = debug_report_create_instance(
122 &instance_data->dispatch_table, *pInstance, pCreateInfo->enabledExtensionCount, pCreateInfo->ppEnabledExtensionNames);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600123
124 // Set up temporary debug callbacks to output messages at CreateInstance-time
125 if (!layer_copy_tmp_callbacks(pCreateInfo->pNext, &instance_data->num_tmp_callbacks, &instance_data->tmp_dbg_create_infos,
126 &instance_data->tmp_callbacks)) {
127 if (instance_data->num_tmp_callbacks > 0) {
128 if (layer_enable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks,
129 instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks)) {
130 layer_free_tmp_callbacks(instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks);
131 instance_data->num_tmp_callbacks = 0;
132 }
133 }
134 }
135
136 initUniqueObjects(instance_data, pAllocator);
Mark Lobodzinskic0141472017-06-09 09:51:34 -0600137 InstanceExtensionWhitelist(pCreateInfo, *pInstance);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600138
139 // Disable and free tmp callbacks, no longer necessary
140 if (instance_data->num_tmp_callbacks > 0) {
141 layer_disable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks, instance_data->tmp_callbacks);
142 layer_free_tmp_callbacks(instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks);
143 instance_data->num_tmp_callbacks = 0;
144 }
145
146 return result;
147}
148
149VKAPI_ATTR void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
150 dispatch_key key = get_dispatch_key(instance);
Chris Forbes5279a8c2017-05-02 16:26:23 -0700151 instance_layer_data *instance_data = GetLayerDataPtr(key, instance_layer_data_map);
Chris Forbes44c05302017-05-02 16:42:55 -0700152 VkLayerInstanceDispatchTable *disp_table = &instance_data->dispatch_table;
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600153 disp_table->DestroyInstance(instance, pAllocator);
154
155 // Clean up logging callback, if any
156 while (instance_data->logging_callback.size() > 0) {
157 VkDebugReportCallbackEXT callback = instance_data->logging_callback.back();
158 layer_destroy_msg_callback(instance_data->report_data, callback, pAllocator);
159 instance_data->logging_callback.pop_back();
160 }
161
162 layer_debug_report_destroy_instance(instance_data->report_data);
Gabríel Arthúr Pétursson2c5e7502017-06-03 23:27:59 +0000163 FreeLayerDataPtr(key, instance_layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600164}
165
166VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
167 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
Chris Forbes5279a8c2017-05-02 16:26:23 -0700168 instance_layer_data *my_instance_data = GetLayerDataPtr(get_dispatch_key(gpu), instance_layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600169 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
170
171 assert(chain_info->u.pLayerInfo);
172 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
173 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
174 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(my_instance_data->instance, "vkCreateDevice");
175 if (fpCreateDevice == NULL) {
176 return VK_ERROR_INITIALIZATION_FAILED;
177 }
178
179 // Advance the link info for the next element on the chain
180 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
181
182 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
183 if (result != VK_SUCCESS) {
184 return result;
185 }
186
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700187 layer_data *my_device_data = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600188 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
189
190 // Setup layer's device dispatch table
Chris Forbes44c05302017-05-02 16:42:55 -0700191 layer_init_device_dispatch_table(*pDevice, &my_device_data->dispatch_table, fpGetDeviceProcAddr);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600192
Mark Lobodzinskic0141472017-06-09 09:51:34 -0600193 DeviceExtensionWhitelist(pCreateInfo, *pDevice);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600194
Mark Lobodzinskic0141472017-06-09 09:51:34 -0600195 // Set gpu for this device in order to get at any objects mapped at instance level
Chris Forbes7fcfde12017-05-02 16:54:24 -0700196 my_device_data->instance_data = my_instance_data;
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600197
198 return result;
199}
200
201VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
202 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700203 layer_data *dev_data = GetLayerDataPtr(key, layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600204
205 layer_debug_report_destroy_device(device);
Chris Forbes44c05302017-05-02 16:42:55 -0700206 dev_data->dispatch_table.DestroyDevice(device, pAllocator);
Gabríel Arthúr Pétursson2c5e7502017-06-03 23:27:59 +0000207
208 FreeLayerDataPtr(key, layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600209}
210
211static const VkLayerProperties globalLayerProps = {"VK_LAYER_GOOGLE_unique_objects",
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700212 VK_LAYER_API_VERSION, // specVersion
213 1, // implementationVersion
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600214 "Google Validation Layer"};
215
Mark Young39389872017-01-19 21:10:49 -0700216/// Declare prototype for these functions
217VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName);
218
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600219VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
220 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
221}
222
223VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
224 VkLayerProperties *pProperties) {
225 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
226}
227
228VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
229 VkExtensionProperties *pProperties) {
230 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
231 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
232
233 return VK_ERROR_LAYER_NOT_PRESENT;
234}
235
236VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
237 uint32_t *pCount, VkExtensionProperties *pProperties) {
238 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
239 return util_GetExtensionProperties(0, nullptr, pCount, pProperties);
240
241 assert(physicalDevice);
242
243 dispatch_key key = get_dispatch_key(physicalDevice);
Chris Forbes5279a8c2017-05-02 16:26:23 -0700244 instance_layer_data *instance_data = GetLayerDataPtr(key, instance_layer_data_map);
Chris Forbes44c05302017-05-02 16:42:55 -0700245 return instance_data->dispatch_table.EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600246}
247
248VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600249 const auto item = name_to_funcptr_map.find(funcName);
250 if (item != name_to_funcptr_map.end()) {
251 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600252 }
253
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600254 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
255 const auto &table = device_data->dispatch_table;
256 if (!table.GetDeviceProcAddr) return nullptr;
257 return table.GetDeviceProcAddr(device, funcName);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600258}
259
260VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600261 const auto item = name_to_funcptr_map.find(funcName);
262 if (item != name_to_funcptr_map.end()) {
263 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600264 }
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600265
Chris Forbes5279a8c2017-05-02 16:26:23 -0700266 instance_layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), instance_layer_data_map);
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600267 const auto &table = instance_data->dispatch_table;
268 if (!table.GetInstanceProcAddr) return nullptr;
269 return table.GetInstanceProcAddr(instance, funcName);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600270}
271
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600272VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
Chris Forbes5279a8c2017-05-02 16:26:23 -0700273 instance_layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), instance_layer_data_map);
Chris Forbes44c05302017-05-02 16:42:55 -0700274 VkLayerInstanceDispatchTable *disp_table = &instance_data->dispatch_table;
Mark Young39389872017-01-19 21:10:49 -0700275 if (disp_table->GetPhysicalDeviceProcAddr == NULL) {
276 return NULL;
277 }
278 return disp_table->GetPhysicalDeviceProcAddr(instance, funcName);
279}
280
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600281VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
282 const VkComputePipelineCreateInfo *pCreateInfos,
283 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
Chris Forbes6956a312017-05-02 17:36:28 -0700284 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600285 safe_VkComputePipelineCreateInfo *local_pCreateInfos = NULL;
286 if (pCreateInfos) {
287 std::lock_guard<std::mutex> lock(global_lock);
288 local_pCreateInfos = new safe_VkComputePipelineCreateInfo[createInfoCount];
289 for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) {
290 local_pCreateInfos[idx0].initialize(&pCreateInfos[idx0]);
291 if (pCreateInfos[idx0].basePipelineHandle) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700292 local_pCreateInfos[idx0].basePipelineHandle = Unwrap(pCreateInfos[idx0].basePipelineHandle);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600293 }
294 if (pCreateInfos[idx0].layout) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700295 local_pCreateInfos[idx0].layout = Unwrap(pCreateInfos[idx0].layout);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600296 }
297 if (pCreateInfos[idx0].stage.module) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700298 local_pCreateInfos[idx0].stage.module = Unwrap(pCreateInfos[idx0].stage.module);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600299 }
300 }
301 }
302 if (pipelineCache) {
303 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700304 pipelineCache = Unwrap(pipelineCache);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600305 }
306
Dave Houltona9df0ce2018-02-07 10:51:23 -0700307 VkResult result = device_data->dispatch_table.CreateComputePipelines(device, pipelineCache, createInfoCount,
308 local_pCreateInfos->ptr(), pAllocator, pPipelines);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600309 delete[] local_pCreateInfos;
Maciej Jesionowski42200702016-11-23 10:44:34 +0100310 {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600311 std::lock_guard<std::mutex> lock(global_lock);
312 for (uint32_t i = 0; i < createInfoCount; ++i) {
Maciej Jesionowski42200702016-11-23 10:44:34 +0100313 if (pPipelines[i] != VK_NULL_HANDLE) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700314 pPipelines[i] = WrapNew(pPipelines[i]);
Maciej Jesionowski42200702016-11-23 10:44:34 +0100315 }
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600316 }
317 }
318 return result;
319}
320
321VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
322 const VkGraphicsPipelineCreateInfo *pCreateInfos,
323 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
Chris Forbes6956a312017-05-02 17:36:28 -0700324 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
325 safe_VkGraphicsPipelineCreateInfo *local_pCreateInfos = nullptr;
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600326 if (pCreateInfos) {
327 local_pCreateInfos = new safe_VkGraphicsPipelineCreateInfo[createInfoCount];
328 std::lock_guard<std::mutex> lock(global_lock);
329 for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) {
Petr Krause91f7a12017-12-14 20:57:36 +0100330 bool uses_color_attachment = false;
331 bool uses_depthstencil_attachment = false;
332 {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700333 const auto subpasses_uses_it = device_data->renderpasses_states.find(Unwrap(pCreateInfos[idx0].renderPass));
Petr Krause91f7a12017-12-14 20:57:36 +0100334 if (subpasses_uses_it != device_data->renderpasses_states.end()) {
335 const auto &subpasses_uses = subpasses_uses_it->second;
336 if (subpasses_uses.subpasses_using_color_attachment.count(pCreateInfos[idx0].subpass))
337 uses_color_attachment = true;
338 if (subpasses_uses.subpasses_using_depthstencil_attachment.count(pCreateInfos[idx0].subpass))
339 uses_depthstencil_attachment = true;
340 }
341 }
342
343 local_pCreateInfos[idx0].initialize(&pCreateInfos[idx0], uses_color_attachment, uses_depthstencil_attachment);
344
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600345 if (pCreateInfos[idx0].basePipelineHandle) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700346 local_pCreateInfos[idx0].basePipelineHandle = Unwrap(pCreateInfos[idx0].basePipelineHandle);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600347 }
348 if (pCreateInfos[idx0].layout) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700349 local_pCreateInfos[idx0].layout = Unwrap(pCreateInfos[idx0].layout);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600350 }
351 if (pCreateInfos[idx0].pStages) {
352 for (uint32_t idx1 = 0; idx1 < pCreateInfos[idx0].stageCount; ++idx1) {
353 if (pCreateInfos[idx0].pStages[idx1].module) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700354 local_pCreateInfos[idx0].pStages[idx1].module = Unwrap(pCreateInfos[idx0].pStages[idx1].module);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600355 }
356 }
357 }
358 if (pCreateInfos[idx0].renderPass) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700359 local_pCreateInfos[idx0].renderPass = Unwrap(pCreateInfos[idx0].renderPass);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600360 }
361 }
362 }
363 if (pipelineCache) {
364 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700365 pipelineCache = Unwrap(pipelineCache);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600366 }
367
Dave Houltona9df0ce2018-02-07 10:51:23 -0700368 VkResult result = device_data->dispatch_table.CreateGraphicsPipelines(device, pipelineCache, createInfoCount,
369 local_pCreateInfos->ptr(), pAllocator, pPipelines);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600370 delete[] local_pCreateInfos;
Maciej Jesionowski42200702016-11-23 10:44:34 +0100371 {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600372 std::lock_guard<std::mutex> lock(global_lock);
373 for (uint32_t i = 0; i < createInfoCount; ++i) {
Maciej Jesionowski42200702016-11-23 10:44:34 +0100374 if (pPipelines[i] != VK_NULL_HANDLE) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700375 pPipelines[i] = WrapNew(pPipelines[i]);
Maciej Jesionowski42200702016-11-23 10:44:34 +0100376 }
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600377 }
378 }
379 return result;
380}
381
Petr Krause91f7a12017-12-14 20:57:36 +0100382static void PostCallCreateRenderPass(layer_data *dev_data, const VkRenderPassCreateInfo *pCreateInfo, VkRenderPass renderPass) {
383 auto &renderpass_state = dev_data->renderpasses_states[renderPass];
384
385 for (uint32_t subpass = 0; subpass < pCreateInfo->subpassCount; ++subpass) {
386 bool uses_color = false;
387 for (uint32_t i = 0; i < pCreateInfo->pSubpasses[subpass].colorAttachmentCount && !uses_color; ++i)
388 if (pCreateInfo->pSubpasses[subpass].pColorAttachments[i].attachment != VK_ATTACHMENT_UNUSED) uses_color = true;
389
390 bool uses_depthstencil = false;
391 if (pCreateInfo->pSubpasses[subpass].pDepthStencilAttachment)
392 if (pCreateInfo->pSubpasses[subpass].pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)
393 uses_depthstencil = true;
394
395 if (uses_color) renderpass_state.subpasses_using_color_attachment.insert(subpass);
396 if (uses_depthstencil) renderpass_state.subpasses_using_depthstencil_attachment.insert(subpass);
397 }
398}
399
400VKAPI_ATTR VkResult VKAPI_CALL CreateRenderPass(VkDevice device, const VkRenderPassCreateInfo *pCreateInfo,
401 const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass) {
402 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
403 VkResult result = dev_data->dispatch_table.CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
404 if (VK_SUCCESS == result) {
405 std::lock_guard<std::mutex> lock(global_lock);
406
407 PostCallCreateRenderPass(dev_data, pCreateInfo, *pRenderPass);
408
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700409 *pRenderPass = WrapNew(*pRenderPass);
Petr Krause91f7a12017-12-14 20:57:36 +0100410 }
411 return result;
412}
413
414static void PostCallDestroyRenderPass(layer_data *dev_data, VkRenderPass renderPass) {
415 dev_data->renderpasses_states.erase(renderPass);
416}
417
418VKAPI_ATTR void VKAPI_CALL DestroyRenderPass(VkDevice device, VkRenderPass renderPass, const VkAllocationCallbacks *pAllocator) {
419 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
420 std::unique_lock<std::mutex> lock(global_lock);
421 uint64_t renderPass_id = reinterpret_cast<uint64_t &>(renderPass);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700422 renderPass = (VkRenderPass)unique_id_mapping[renderPass_id];
423 unique_id_mapping.erase(renderPass_id);
Petr Krause91f7a12017-12-14 20:57:36 +0100424 lock.unlock();
425 dev_data->dispatch_table.DestroyRenderPass(device, renderPass, pAllocator);
426
427 lock.lock();
428 PostCallDestroyRenderPass(dev_data, renderPass);
429}
430
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600431VKAPI_ATTR VkResult VKAPI_CALL CreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo,
432 const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700433 layer_data *my_map_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600434 safe_VkSwapchainCreateInfoKHR *local_pCreateInfo = NULL;
435 if (pCreateInfo) {
436 std::lock_guard<std::mutex> lock(global_lock);
437 local_pCreateInfo = new safe_VkSwapchainCreateInfoKHR(pCreateInfo);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700438 local_pCreateInfo->oldSwapchain = Unwrap(pCreateInfo->oldSwapchain);
Chris Forbes6956a312017-05-02 17:36:28 -0700439 // Surface is instance-level object
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700440 local_pCreateInfo->surface = Unwrap(pCreateInfo->surface);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600441 }
442
Dave Houltona9df0ce2018-02-07 10:51:23 -0700443 VkResult result = my_map_data->dispatch_table.CreateSwapchainKHR(device, local_pCreateInfo->ptr(), pAllocator, pSwapchain);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600444 if (local_pCreateInfo) {
445 delete local_pCreateInfo;
446 }
447 if (VK_SUCCESS == result) {
448 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700449 *pSwapchain = WrapNew(*pSwapchain);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600450 }
451 return result;
452}
453
Dustin Graves9a6eb052017-03-28 14:18:54 -0600454VKAPI_ATTR VkResult VKAPI_CALL CreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
455 const VkSwapchainCreateInfoKHR *pCreateInfos,
456 const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchains) {
457 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
458 safe_VkSwapchainCreateInfoKHR *local_pCreateInfos = NULL;
459 {
460 std::lock_guard<std::mutex> lock(global_lock);
461 if (pCreateInfos) {
Dustin Graves9a6eb052017-03-28 14:18:54 -0600462 local_pCreateInfos = new safe_VkSwapchainCreateInfoKHR[swapchainCount];
463 for (uint32_t i = 0; i < swapchainCount; ++i) {
464 local_pCreateInfos[i].initialize(&pCreateInfos[i]);
465 if (pCreateInfos[i].surface) {
Chris Forbes6956a312017-05-02 17:36:28 -0700466 // Surface is instance-level object
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700467 local_pCreateInfos[i].surface = Unwrap(pCreateInfos[i].surface);
Dustin Graves9a6eb052017-03-28 14:18:54 -0600468 }
469 if (pCreateInfos[i].oldSwapchain) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700470 local_pCreateInfos[i].oldSwapchain = Unwrap(pCreateInfos[i].oldSwapchain);
Dustin Graves9a6eb052017-03-28 14:18:54 -0600471 }
472 }
473 }
474 }
Dave Houltona9df0ce2018-02-07 10:51:23 -0700475 VkResult result = dev_data->dispatch_table.CreateSharedSwapchainsKHR(device, swapchainCount, local_pCreateInfos->ptr(),
476 pAllocator, pSwapchains);
Dustin Graves9a6eb052017-03-28 14:18:54 -0600477 if (local_pCreateInfos) delete[] local_pCreateInfos;
478 if (VK_SUCCESS == result) {
479 std::lock_guard<std::mutex> lock(global_lock);
480 for (uint32_t i = 0; i < swapchainCount; i++) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700481 pSwapchains[i] = WrapNew(pSwapchains[i]);
Dustin Graves9a6eb052017-03-28 14:18:54 -0600482 }
483 }
484 return result;
485}
486
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600487VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount,
488 VkImage *pSwapchainImages) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700489 layer_data *my_device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski2eb39bc2018-02-16 11:24:21 -0700490 VkSwapchainKHR wrapped_swapchain_handle = swapchain;
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600491 if (VK_NULL_HANDLE != swapchain) {
492 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700493 swapchain = Unwrap(swapchain);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600494 }
495 VkResult result =
Chris Forbes44c05302017-05-02 16:42:55 -0700496 my_device_data->dispatch_table.GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
Mark Lobodzinski2eb39bc2018-02-16 11:24:21 -0700497 if ((VK_SUCCESS == result) || (VK_INCOMPLETE == result)) {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600498 if ((*pSwapchainImageCount > 0) && pSwapchainImages) {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600499 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinski2eb39bc2018-02-16 11:24:21 -0700500 auto &wrapped_swapchain_image_handles = my_device_data->swapchain_wrapped_image_handle_map[wrapped_swapchain_handle];
501 for (uint32_t i = static_cast<uint32_t>(wrapped_swapchain_image_handles.size()); i < *pSwapchainImageCount; i++) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700502 wrapped_swapchain_image_handles.emplace_back(WrapNew(pSwapchainImages[i]));
Mark Lobodzinski2eb39bc2018-02-16 11:24:21 -0700503 }
504 for (uint32_t i = 0; i < *pSwapchainImageCount; i++) {
505 pSwapchainImages[i] = wrapped_swapchain_image_handles[i];
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600506 }
507 }
508 }
509 return result;
510}
511
Mark Lobodzinski1ce83f42018-02-16 09:58:07 -0700512VKAPI_ATTR void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
513 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
514 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski2eb39bc2018-02-16 11:24:21 -0700515
516 auto &image_array = dev_data->swapchain_wrapped_image_handle_map[swapchain];
517 for (auto &image_handle : image_array) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700518 unique_id_mapping.erase(HandleToUint64(image_handle));
Mark Lobodzinski2eb39bc2018-02-16 11:24:21 -0700519 }
520 dev_data->swapchain_wrapped_image_handle_map.erase(swapchain);
521
522 uint64_t swapchain_id = HandleToUint64(swapchain);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700523 swapchain = (VkSwapchainKHR)unique_id_mapping[swapchain_id];
524 unique_id_mapping.erase(swapchain_id);
Mark Lobodzinski1ce83f42018-02-16 09:58:07 -0700525 lock.unlock();
526 dev_data->dispatch_table.DestroySwapchainKHR(device, swapchain, pAllocator);
527}
528
Chris Forbes0f507f22017-04-16 13:13:17 +1200529VKAPI_ATTR VkResult VKAPI_CALL QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) {
530 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(queue), layer_data_map);
531 safe_VkPresentInfoKHR *local_pPresentInfo = NULL;
532 {
533 std::lock_guard<std::mutex> lock(global_lock);
534 if (pPresentInfo) {
535 local_pPresentInfo = new safe_VkPresentInfoKHR(pPresentInfo);
536 if (local_pPresentInfo->pWaitSemaphores) {
537 for (uint32_t index1 = 0; index1 < local_pPresentInfo->waitSemaphoreCount; ++index1) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700538 local_pPresentInfo->pWaitSemaphores[index1] = Unwrap(pPresentInfo->pWaitSemaphores[index1]);
Chris Forbes0f507f22017-04-16 13:13:17 +1200539 }
540 }
541 if (local_pPresentInfo->pSwapchains) {
542 for (uint32_t index1 = 0; index1 < local_pPresentInfo->swapchainCount; ++index1) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700543 local_pPresentInfo->pSwapchains[index1] = Unwrap(pPresentInfo->pSwapchains[index1]);
Chris Forbes0f507f22017-04-16 13:13:17 +1200544 }
545 }
546 }
547 }
Chris Forbes6956a312017-05-02 17:36:28 -0700548 VkResult result = dev_data->dispatch_table.QueuePresentKHR(queue, local_pPresentInfo->ptr());
Chris Forbes0f507f22017-04-16 13:13:17 +1200549
550 // pResults is an output array embedded in a structure. The code generator neglects to copy back from the safe_* version,
551 // so handle it as a special case here:
552 if (pPresentInfo && pPresentInfo->pResults) {
553 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
554 pPresentInfo->pResults[i] = local_pPresentInfo->pResults[i];
555 }
556 }
557
558 if (local_pPresentInfo) delete local_pPresentInfo;
559 return result;
560}
561
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700562VKAPI_ATTR VkResult VKAPI_CALL CreateDescriptorUpdateTemplateKHR(VkDevice device,
563 const VkDescriptorUpdateTemplateCreateInfoKHR *pCreateInfo,
564 const VkAllocationCallbacks *pAllocator,
565 VkDescriptorUpdateTemplateKHR *pDescriptorUpdateTemplate) {
566 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700567 safe_VkDescriptorUpdateTemplateCreateInfoKHR *local_create_info = NULL;
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700568 {
569 std::lock_guard<std::mutex> lock(global_lock);
570 if (pCreateInfo) {
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700571 local_create_info = new safe_VkDescriptorUpdateTemplateCreateInfoKHR(pCreateInfo);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700572 if (pCreateInfo->descriptorSetLayout) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700573 local_create_info->descriptorSetLayout = Unwrap(pCreateInfo->descriptorSetLayout);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700574 }
575 if (pCreateInfo->pipelineLayout) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700576 local_create_info->pipelineLayout = Unwrap(pCreateInfo->pipelineLayout);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700577 }
578 }
579 }
Dave Houltona9df0ce2018-02-07 10:51:23 -0700580 VkResult result = dev_data->dispatch_table.CreateDescriptorUpdateTemplateKHR(device, local_create_info->ptr(), pAllocator,
581 pDescriptorUpdateTemplate);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700582 if (VK_SUCCESS == result) {
583 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700584 *pDescriptorUpdateTemplate = WrapNew(*pDescriptorUpdateTemplate);
Mark Lobodzinski4f3ce672017-03-03 10:28:21 -0700585
586 // Shadow template createInfo for later updates
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700587 std::unique_ptr<TEMPLATE_STATE> template_state(new TEMPLATE_STATE(*pDescriptorUpdateTemplate, local_create_info));
Chris Forbes6956a312017-05-02 17:36:28 -0700588 dev_data->desc_template_map[(uint64_t)*pDescriptorUpdateTemplate] = std::move(template_state);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700589 }
590 return result;
591}
592
593VKAPI_ATTR void VKAPI_CALL DestroyDescriptorUpdateTemplateKHR(VkDevice device,
594 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
595 const VkAllocationCallbacks *pAllocator) {
596 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
597 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700598 uint64_t descriptor_update_template_id = reinterpret_cast<uint64_t &>(descriptorUpdateTemplate);
599 dev_data->desc_template_map.erase(descriptor_update_template_id);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700600 descriptorUpdateTemplate = (VkDescriptorUpdateTemplateKHR)unique_id_mapping[descriptor_update_template_id];
601 unique_id_mapping.erase(descriptor_update_template_id);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700602 lock.unlock();
Chris Forbes44c05302017-05-02 16:42:55 -0700603 dev_data->dispatch_table.DestroyDescriptorUpdateTemplateKHR(device, descriptorUpdateTemplate, pAllocator);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700604}
605
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700606void *BuildUnwrappedUpdateTemplateBuffer(layer_data *dev_data, uint64_t descriptorUpdateTemplate, const void *pData) {
607 auto const template_map_entry = dev_data->desc_template_map.find(descriptorUpdateTemplate);
608 if (template_map_entry == dev_data->desc_template_map.end()) {
609 assert(0);
610 }
611 auto const &create_info = template_map_entry->second->create_info;
612 size_t allocation_size = 0;
Mark Lobodzinski2d9de652017-04-24 08:58:52 -0600613 std::vector<std::tuple<size_t, VulkanObjectType, void *>> template_entries;
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700614
615 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
616 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
617 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
618 char *update_entry = (char *)(pData) + offset;
619
620 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
621 case VK_DESCRIPTOR_TYPE_SAMPLER:
622 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
623 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
624 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
625 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
626 auto image_entry = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
627 allocation_size = std::max(allocation_size, offset + sizeof(VkDescriptorImageInfo));
628
629 VkDescriptorImageInfo *wrapped_entry = new VkDescriptorImageInfo(*image_entry);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700630 wrapped_entry->sampler = Unwrap(image_entry->sampler);
631 wrapped_entry->imageView = Unwrap(image_entry->imageView);
Mark Lobodzinski2d9de652017-04-24 08:58:52 -0600632 template_entries.emplace_back(offset, kVulkanObjectTypeImage, reinterpret_cast<void *>(wrapped_entry));
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700633 } break;
634
635 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
636 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
637 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
638 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
639 auto buffer_entry = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
640 allocation_size = std::max(allocation_size, offset + sizeof(VkDescriptorBufferInfo));
641
642 VkDescriptorBufferInfo *wrapped_entry = new VkDescriptorBufferInfo(*buffer_entry);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700643 wrapped_entry->buffer = Unwrap(buffer_entry->buffer);
Mark Lobodzinski2d9de652017-04-24 08:58:52 -0600644 template_entries.emplace_back(offset, kVulkanObjectTypeBuffer, reinterpret_cast<void *>(wrapped_entry));
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700645 } break;
646
647 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
648 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
Chris Forbes6956a312017-05-02 17:36:28 -0700649 auto buffer_view_handle = reinterpret_cast<VkBufferView *>(update_entry);
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700650 allocation_size = std::max(allocation_size, offset + sizeof(VkBufferView));
651
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700652 VkBufferView wrapped_entry = Unwrap(*buffer_view_handle);
Mark Lobodzinski2d9de652017-04-24 08:58:52 -0600653 template_entries.emplace_back(offset, kVulkanObjectTypeBufferView, reinterpret_cast<void *>(wrapped_entry));
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700654 } break;
655 default:
656 assert(0);
657 break;
658 }
659 }
660 }
661 // Allocate required buffer size and populate with source/unwrapped data
662 void *unwrapped_data = malloc(allocation_size);
663 for (auto &this_entry : template_entries) {
Mark Lobodzinski2d9de652017-04-24 08:58:52 -0600664 VulkanObjectType type = std::get<1>(this_entry);
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700665 void *destination = (char *)unwrapped_data + std::get<0>(this_entry);
666 void *source = (char *)std::get<2>(this_entry);
667
668 switch (type) {
Mark Lobodzinski2d9de652017-04-24 08:58:52 -0600669 case kVulkanObjectTypeImage:
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700670 *(reinterpret_cast<VkDescriptorImageInfo *>(destination)) = *(reinterpret_cast<VkDescriptorImageInfo *>(source));
671 delete reinterpret_cast<VkDescriptorImageInfo *>(source);
672 break;
Mark Lobodzinski2d9de652017-04-24 08:58:52 -0600673 case kVulkanObjectTypeBuffer:
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700674 *(reinterpret_cast<VkDescriptorBufferInfo *>(destination)) = *(reinterpret_cast<VkDescriptorBufferInfo *>(source));
675 delete reinterpret_cast<VkDescriptorBufferInfo *>(source);
676 break;
Mark Lobodzinski2d9de652017-04-24 08:58:52 -0600677 case kVulkanObjectTypeBufferView:
Mark Lobodzinskid5197d02017-03-15 13:13:49 -0600678 *(reinterpret_cast<VkBufferView *>(destination)) = reinterpret_cast<VkBufferView>(source);
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700679 break;
680 default:
681 assert(0);
682 break;
683 }
684 }
685 return (void *)unwrapped_data;
686}
687
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700688VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSetWithTemplateKHR(VkDevice device, VkDescriptorSet descriptorSet,
689 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
690 const void *pData) {
691 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700692 uint64_t template_handle = reinterpret_cast<uint64_t &>(descriptorUpdateTemplate);
Mark Lobodzinskic8a0c9b2017-11-13 09:42:58 -0700693 void *unwrapped_buffer = nullptr;
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700694 {
695 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700696 descriptorSet = Unwrap(descriptorSet);
697 descriptorUpdateTemplate = (VkDescriptorUpdateTemplateKHR)unique_id_mapping[template_handle];
Mark Lobodzinskic8a0c9b2017-11-13 09:42:58 -0700698 unwrapped_buffer = BuildUnwrappedUpdateTemplateBuffer(dev_data, template_handle, pData);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700699 }
Dave Houltona9df0ce2018-02-07 10:51:23 -0700700 dev_data->dispatch_table.UpdateDescriptorSetWithTemplateKHR(device, descriptorSet, descriptorUpdateTemplate, unwrapped_buffer);
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700701 free(unwrapped_buffer);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700702}
703
704VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSetWithTemplateKHR(VkCommandBuffer commandBuffer,
705 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
706 VkPipelineLayout layout, uint32_t set, const void *pData) {
707 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(commandBuffer), layer_data_map);
Mark Lobodzinski1c471262017-03-28 16:22:56 -0600708 uint64_t template_handle = reinterpret_cast<uint64_t &>(descriptorUpdateTemplate);
Mark Lobodzinskic8a0c9b2017-11-13 09:42:58 -0700709 void *unwrapped_buffer = nullptr;
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700710 {
711 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700712 descriptorUpdateTemplate = Unwrap(descriptorUpdateTemplate);
713 layout = Unwrap(layout);
Mark Lobodzinskic8a0c9b2017-11-13 09:42:58 -0700714 unwrapped_buffer = BuildUnwrappedUpdateTemplateBuffer(dev_data, template_handle, pData);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700715 }
Chris Forbes44c05302017-05-02 16:42:55 -0700716 dev_data->dispatch_table.CmdPushDescriptorSetWithTemplateKHR(commandBuffer, descriptorUpdateTemplate, layout, set,
Dave Houltona9df0ce2018-02-07 10:51:23 -0700717 unwrapped_buffer);
Mark Lobodzinski1c471262017-03-28 16:22:56 -0600718 free(unwrapped_buffer);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700719}
720
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600721#ifndef __ANDROID__
722VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceDisplayPropertiesKHR(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
723 VkDisplayPropertiesKHR *pProperties) {
Chris Forbes5279a8c2017-05-02 16:26:23 -0700724 instance_layer_data *my_map_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), instance_layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600725
Dave Houltona9df0ce2018-02-07 10:51:23 -0700726 VkResult result =
727 my_map_data->dispatch_table.GetPhysicalDeviceDisplayPropertiesKHR(physicalDevice, pPropertyCount, pProperties);
Chris Forbesf1e49bf2017-05-02 17:36:57 -0700728 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pProperties) {
729 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600730 for (uint32_t idx0 = 0; idx0 < *pPropertyCount; ++idx0) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700731 pProperties[idx0].display = WrapNew(pProperties[idx0].display);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600732 }
733 }
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600734 return result;
735}
736
737VKAPI_ATTR VkResult VKAPI_CALL GetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
738 uint32_t *pDisplayCount, VkDisplayKHR *pDisplays) {
Chris Forbes5279a8c2017-05-02 16:26:23 -0700739 instance_layer_data *my_map_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), instance_layer_data_map);
Dave Houltona9df0ce2018-02-07 10:51:23 -0700740 VkResult result =
741 my_map_data->dispatch_table.GetDisplayPlaneSupportedDisplaysKHR(physicalDevice, planeIndex, pDisplayCount, pDisplays);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600742 if (VK_SUCCESS == result) {
743 if ((*pDisplayCount > 0) && pDisplays) {
744 std::lock_guard<std::mutex> lock(global_lock);
745 for (uint32_t i = 0; i < *pDisplayCount; i++) {
Chris Forbes824c1172017-05-02 17:45:29 -0700746 // TODO: this looks like it really wants a /reverse/ mapping. What's going on here?
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700747 auto it = unique_id_mapping.find(reinterpret_cast<const uint64_t &>(pDisplays[i]));
748 assert(it != unique_id_mapping.end());
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600749 pDisplays[i] = reinterpret_cast<VkDisplayKHR &>(it->second);
750 }
751 }
752 }
753 return result;
754}
755
756VKAPI_ATTR VkResult VKAPI_CALL GetDisplayModePropertiesKHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
757 uint32_t *pPropertyCount, VkDisplayModePropertiesKHR *pProperties) {
Chris Forbes5279a8c2017-05-02 16:26:23 -0700758 instance_layer_data *my_map_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), instance_layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600759 {
760 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700761 display = Unwrap(display);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600762 }
763
Dave Houltona9df0ce2018-02-07 10:51:23 -0700764 VkResult result = my_map_data->dispatch_table.GetDisplayModePropertiesKHR(physicalDevice, display, pPropertyCount, pProperties);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600765 if (result == VK_SUCCESS && pProperties) {
Chris Forbesef35afd2017-05-02 17:45:45 -0700766 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600767 for (uint32_t idx0 = 0; idx0 < *pPropertyCount; ++idx0) {
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700768 pProperties[idx0].displayMode = WrapNew(pProperties[idx0].displayMode);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600769 }
770 }
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600771 return result;
772}
Norbert Nopper1dec9a52016-11-25 07:55:13 +0100773
774VKAPI_ATTR VkResult VKAPI_CALL GetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
775 uint32_t planeIndex, VkDisplayPlaneCapabilitiesKHR *pCapabilities) {
Chris Forbes5279a8c2017-05-02 16:26:23 -0700776 instance_layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), instance_layer_data_map);
Norbert Nopper1dec9a52016-11-25 07:55:13 +0100777 {
778 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700779 mode = Unwrap(mode);
Norbert Nopper1dec9a52016-11-25 07:55:13 +0100780 }
Dave Houltona9df0ce2018-02-07 10:51:23 -0700781 VkResult result = dev_data->dispatch_table.GetDisplayPlaneCapabilitiesKHR(physicalDevice, mode, planeIndex, pCapabilities);
Norbert Nopper1dec9a52016-11-25 07:55:13 +0100782 return result;
783}
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600784#endif
785
Mark Lobodzinskie4f2c5f2017-07-17 14:26:47 -0600786VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectTagEXT(VkDevice device, const VkDebugMarkerObjectTagInfoEXT *pTagInfo) {
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600787 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
788 auto local_tag_info = new safe_VkDebugMarkerObjectTagInfoEXT(pTagInfo);
789 {
790 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700791 auto it = unique_id_mapping.find(reinterpret_cast<uint64_t &>(local_tag_info->object));
792 if (it != unique_id_mapping.end()) {
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600793 local_tag_info->object = it->second;
794 }
795 }
Chris Forbes44c05302017-05-02 16:42:55 -0700796 VkResult result = device_data->dispatch_table.DebugMarkerSetObjectTagEXT(
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600797 device, reinterpret_cast<VkDebugMarkerObjectTagInfoEXT *>(local_tag_info));
798 return result;
799}
800
Mark Lobodzinskie4f2c5f2017-07-17 14:26:47 -0600801VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectNameEXT(VkDevice device, const VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600802 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
803 auto local_name_info = new safe_VkDebugMarkerObjectNameInfoEXT(pNameInfo);
804 {
805 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700806 auto it = unique_id_mapping.find(reinterpret_cast<uint64_t &>(local_name_info->object));
807 if (it != unique_id_mapping.end()) {
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600808 local_name_info->object = it->second;
809 }
810 }
Chris Forbes44c05302017-05-02 16:42:55 -0700811 VkResult result = device_data->dispatch_table.DebugMarkerSetObjectNameEXT(
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600812 device, reinterpret_cast<VkDebugMarkerObjectNameInfoEXT *>(local_name_info));
813 return result;
814}
815
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700816} // namespace unique_objects
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600817
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600818VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
819 VkExtensionProperties *pProperties) {
820 return unique_objects::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
821}
822
823VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
824 VkLayerProperties *pProperties) {
825 return unique_objects::EnumerateInstanceLayerProperties(pCount, pProperties);
826}
827
828VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
829 VkLayerProperties *pProperties) {
830 assert(physicalDevice == VK_NULL_HANDLE);
831 return unique_objects::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
832}
833
834VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
835 return unique_objects::GetDeviceProcAddr(dev, funcName);
836}
837
838VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
839 return unique_objects::GetInstanceProcAddr(instance, funcName);
840}
841
842VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
843 const char *pLayerName, uint32_t *pCount,
844 VkExtensionProperties *pProperties) {
845 assert(physicalDevice == VK_NULL_HANDLE);
846 return unique_objects::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
847}
Mark Young39389872017-01-19 21:10:49 -0700848
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700849VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
850 const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -0700851 return unique_objects::GetPhysicalDeviceProcAddr(instance, funcName);
852}
853
854VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
855 assert(pVersionStruct != NULL);
856 assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
857
858 // Fill in the function pointers if our version is at least capable of having the structure contain them.
859 if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
860 pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
861 pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
862 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
863 }
864
865 if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
866 unique_objects::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
867 } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
868 pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
869 }
870
871 return VK_SUCCESS;
872}