blob: d637ac64fdb4827acf01ee274508ea83a04bb206 [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"
48#include "vulkan/vk_layer.h"
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060049
Mike Stroyanb985fca2016-11-01 11:50:16 -060050// This intentionally includes a cpp file
51#include "vk_safe_struct.cpp"
52
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060053#include "unique_objects_wrappers.h"
54
55namespace unique_objects {
56
Mark Young39389872017-01-19 21:10:49 -070057static uint32_t loader_layer_if_version = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
58
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060059static void initUniqueObjects(layer_data *instance_data, const VkAllocationCallbacks *pAllocator) {
60 layer_debug_actions(instance_data->report_data, instance_data->logging_callback, pAllocator, "google_unique_objects");
61}
62
63// Handle CreateInstance Extensions
64static void checkInstanceRegisterExtensions(const VkInstanceCreateInfo *pCreateInfo, VkInstance instance) {
65 uint32_t i;
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070066 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -060067 VkLayerInstanceDispatchTable *disp_table = instance_data->instance_dispatch_table;
68 instance_ext_map[disp_table] = {};
69
70 for (i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
71 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SURFACE_EXTENSION_NAME) == 0) {
72 instance_ext_map[disp_table].wsi_enabled = true;
73 }
74 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_DISPLAY_EXTENSION_NAME) == 0) {
75 instance_ext_map[disp_table].display_enabled = true;
76 }
77#ifdef VK_USE_PLATFORM_XLIB_KHR
78 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XLIB_SURFACE_EXTENSION_NAME) == 0) {
79 instance_ext_map[disp_table].xlib_enabled = true;
80 }
81#endif
82#ifdef VK_USE_PLATFORM_XCB_KHR
83 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XCB_SURFACE_EXTENSION_NAME) == 0) {
84 instance_ext_map[disp_table].xcb_enabled = true;
85 }
86#endif
87#ifdef VK_USE_PLATFORM_WAYLAND_KHR
88 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME) == 0) {
89 instance_ext_map[disp_table].wayland_enabled = true;
90 }
91#endif
92#ifdef VK_USE_PLATFORM_MIR_KHR
93 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_MIR_SURFACE_EXTENSION_NAME) == 0) {
94 instance_ext_map[disp_table].mir_enabled = true;
95 }
96#endif
97#ifdef VK_USE_PLATFORM_ANDROID_KHR
98 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_ANDROID_SURFACE_EXTENSION_NAME) == 0) {
99 instance_ext_map[disp_table].android_enabled = true;
100 }
101#endif
102#ifdef VK_USE_PLATFORM_WIN32_KHR
103 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WIN32_SURFACE_EXTENSION_NAME) == 0) {
104 instance_ext_map[disp_table].win32_enabled = true;
105 }
106#endif
107
108 // Check for recognized instance extensions
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600109 if (!white_list(pCreateInfo->ppEnabledExtensionNames[i], kUniqueObjectsSupportedInstanceExtensions)) {
110 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 -0700111 VALIDATION_ERROR_UNDEFINED, "UniqueObjects",
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600112 "Instance Extension %s is not supported by this layer. Using this extension may adversely affect "
113 "validation results and/or produce undefined behavior.",
114 pCreateInfo->ppEnabledExtensionNames[i]);
115 }
116 }
117}
118
119// Handle CreateDevice Extensions
120static void createDeviceRegisterExtensions(const VkDeviceCreateInfo *pCreateInfo, VkDevice device) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700121 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600122 VkLayerDispatchTable *disp_table = device_data->device_dispatch_table;
123 PFN_vkGetDeviceProcAddr gpa = disp_table->GetDeviceProcAddr;
124
125 device_data->device_dispatch_table->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR)gpa(device, "vkCreateSwapchainKHR");
126 disp_table->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR)gpa(device, "vkDestroySwapchainKHR");
127 disp_table->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR)gpa(device, "vkGetSwapchainImagesKHR");
128 disp_table->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR)gpa(device, "vkAcquireNextImageKHR");
129 disp_table->QueuePresentKHR = (PFN_vkQueuePresentKHR)gpa(device, "vkQueuePresentKHR");
130 device_data->wsi_enabled = false;
131
132 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
133 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) {
134 device_data->wsi_enabled = true;
135 }
136 // Check for recognized device extensions
137 if (!white_list(pCreateInfo->ppEnabledExtensionNames[i], kUniqueObjectsSupportedDeviceExtensions)) {
138 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 -0700139 VALIDATION_ERROR_UNDEFINED, "UniqueObjects",
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600140 "Device Extension %s is not supported by this layer. Using this extension may adversely affect "
141 "validation results and/or produce undefined behavior.",
142 pCreateInfo->ppEnabledExtensionNames[i]);
143 }
144 }
145}
146
147VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
148 VkInstance *pInstance) {
149 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
150
151 assert(chain_info->u.pLayerInfo);
152 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
153 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
154 if (fpCreateInstance == NULL) {
155 return VK_ERROR_INITIALIZATION_FAILED;
156 }
157
158 // Advance the link info for the next element on the chain
159 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
160
161 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
162 if (result != VK_SUCCESS) {
163 return result;
164 }
165
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700166 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(*pInstance), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600167 instance_data->instance = *pInstance;
168 instance_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
169 layer_init_instance_dispatch_table(*pInstance, instance_data->instance_dispatch_table, fpGetInstanceProcAddr);
170
171 instance_data->instance = *pInstance;
172 instance_data->report_data =
173 debug_report_create_instance(instance_data->instance_dispatch_table, *pInstance, pCreateInfo->enabledExtensionCount,
174 pCreateInfo->ppEnabledExtensionNames);
175
176 // Set up temporary debug callbacks to output messages at CreateInstance-time
177 if (!layer_copy_tmp_callbacks(pCreateInfo->pNext, &instance_data->num_tmp_callbacks, &instance_data->tmp_dbg_create_infos,
178 &instance_data->tmp_callbacks)) {
179 if (instance_data->num_tmp_callbacks > 0) {
180 if (layer_enable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks,
181 instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks)) {
182 layer_free_tmp_callbacks(instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks);
183 instance_data->num_tmp_callbacks = 0;
184 }
185 }
186 }
187
188 initUniqueObjects(instance_data, pAllocator);
189 checkInstanceRegisterExtensions(pCreateInfo, *pInstance);
190
191 // Disable and free tmp callbacks, no longer necessary
192 if (instance_data->num_tmp_callbacks > 0) {
193 layer_disable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks, instance_data->tmp_callbacks);
194 layer_free_tmp_callbacks(instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks);
195 instance_data->num_tmp_callbacks = 0;
196 }
197
198 return result;
199}
200
201VKAPI_ATTR void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
202 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700203 layer_data *instance_data = GetLayerDataPtr(key, layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600204 VkLayerInstanceDispatchTable *disp_table = instance_data->instance_dispatch_table;
205 instance_ext_map.erase(disp_table);
206 disp_table->DestroyInstance(instance, pAllocator);
207
208 // Clean up logging callback, if any
209 while (instance_data->logging_callback.size() > 0) {
210 VkDebugReportCallbackEXT callback = instance_data->logging_callback.back();
211 layer_destroy_msg_callback(instance_data->report_data, callback, pAllocator);
212 instance_data->logging_callback.pop_back();
213 }
214
215 layer_debug_report_destroy_instance(instance_data->report_data);
216 layer_data_map.erase(key);
217}
218
219VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
220 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700221 layer_data *my_instance_data = GetLayerDataPtr(get_dispatch_key(gpu), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600222 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
223
224 assert(chain_info->u.pLayerInfo);
225 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
226 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
227 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(my_instance_data->instance, "vkCreateDevice");
228 if (fpCreateDevice == NULL) {
229 return VK_ERROR_INITIALIZATION_FAILED;
230 }
231
232 // Advance the link info for the next element on the chain
233 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
234
235 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
236 if (result != VK_SUCCESS) {
237 return result;
238 }
239
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700240 layer_data *my_device_data = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600241 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
242
243 // Setup layer's device dispatch table
244 my_device_data->device_dispatch_table = new VkLayerDispatchTable;
245 layer_init_device_dispatch_table(*pDevice, my_device_data->device_dispatch_table, fpGetDeviceProcAddr);
246
247 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
248 // Set gpu for this device in order to get at any objects mapped at instance level
249
250 my_device_data->gpu = gpu;
251
252 return result;
253}
254
255VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
256 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700257 layer_data *dev_data = GetLayerDataPtr(key, layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600258
259 layer_debug_report_destroy_device(device);
260 dev_data->device_dispatch_table->DestroyDevice(device, pAllocator);
261 layer_data_map.erase(key);
262}
263
264static const VkLayerProperties globalLayerProps = {"VK_LAYER_GOOGLE_unique_objects",
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700265 VK_LAYER_API_VERSION, // specVersion
266 1, // implementationVersion
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600267 "Google Validation Layer"};
268
Mark Young39389872017-01-19 21:10:49 -0700269/// Declare prototype for these functions
270VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName);
271
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600272static inline PFN_vkVoidFunction layer_intercept_proc(const char *name) {
Jamie Madill6069c822016-12-15 09:35:36 -0500273 for (unsigned int i = 0; i < sizeof(procmap) / sizeof(procmap[0]); i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700274 if (!strcmp(name, procmap[i].name)) return procmap[i].pFunc;
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600275 }
Mark Young39389872017-01-19 21:10:49 -0700276 if (0 == strcmp(name, "vk_layerGetPhysicalDeviceProcAddr")) {
277 return (PFN_vkVoidFunction)GetPhysicalDeviceProcAddr;
278 }
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600279 return NULL;
280}
281
282VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
283 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
284}
285
286VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
287 VkLayerProperties *pProperties) {
288 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
289}
290
291VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
292 VkExtensionProperties *pProperties) {
293 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
294 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
295
296 return VK_ERROR_LAYER_NOT_PRESENT;
297}
298
299VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
300 uint32_t *pCount, VkExtensionProperties *pProperties) {
301 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
302 return util_GetExtensionProperties(0, nullptr, pCount, pProperties);
303
304 assert(physicalDevice);
305
306 dispatch_key key = get_dispatch_key(physicalDevice);
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700307 layer_data *instance_data = GetLayerDataPtr(key, layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600308 return instance_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
309}
310
311VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
312 PFN_vkVoidFunction addr;
313 assert(device);
314 addr = layer_intercept_proc(funcName);
315 if (addr) {
316 return addr;
317 }
318
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700319 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600320 VkLayerDispatchTable *disp_table = dev_data->device_dispatch_table;
321 if (disp_table->GetDeviceProcAddr == NULL) {
322 return NULL;
323 }
324 return disp_table->GetDeviceProcAddr(device, funcName);
325}
326
327VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
328 PFN_vkVoidFunction addr;
329
330 addr = layer_intercept_proc(funcName);
331 if (addr) {
332 return addr;
333 }
334 assert(instance);
335
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700336 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600337 addr = debug_report_get_instance_proc_addr(instance_data->report_data, funcName);
338 if (addr) {
339 return addr;
340 }
341
342 VkLayerInstanceDispatchTable *disp_table = instance_data->instance_dispatch_table;
343 if (disp_table->GetInstanceProcAddr == NULL) {
344 return NULL;
345 }
346 return disp_table->GetInstanceProcAddr(instance, funcName);
347}
348
Mark Young39389872017-01-19 21:10:49 -0700349VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
350 assert(instance);
351
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700352 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Mark Young39389872017-01-19 21:10:49 -0700353 VkLayerInstanceDispatchTable *disp_table = instance_data->instance_dispatch_table;
354 if (disp_table->GetPhysicalDeviceProcAddr == NULL) {
355 return NULL;
356 }
357 return disp_table->GetPhysicalDeviceProcAddr(instance, funcName);
358}
359
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600360VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
361 const VkComputePipelineCreateInfo *pCreateInfos,
362 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700363 layer_data *my_device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600364 safe_VkComputePipelineCreateInfo *local_pCreateInfos = NULL;
365 if (pCreateInfos) {
366 std::lock_guard<std::mutex> lock(global_lock);
367 local_pCreateInfos = new safe_VkComputePipelineCreateInfo[createInfoCount];
368 for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) {
369 local_pCreateInfos[idx0].initialize(&pCreateInfos[idx0]);
370 if (pCreateInfos[idx0].basePipelineHandle) {
371 local_pCreateInfos[idx0].basePipelineHandle =
372 (VkPipeline)my_device_data
373 ->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[idx0].basePipelineHandle)];
374 }
375 if (pCreateInfos[idx0].layout) {
376 local_pCreateInfos[idx0].layout =
377 (VkPipelineLayout)
378 my_device_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[idx0].layout)];
379 }
380 if (pCreateInfos[idx0].stage.module) {
381 local_pCreateInfos[idx0].stage.module =
382 (VkShaderModule)
383 my_device_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[idx0].stage.module)];
384 }
385 }
386 }
387 if (pipelineCache) {
388 std::lock_guard<std::mutex> lock(global_lock);
389 pipelineCache = (VkPipelineCache)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t &>(pipelineCache)];
390 }
391
392 VkResult result = my_device_data->device_dispatch_table->CreateComputePipelines(
393 device, pipelineCache, createInfoCount, (const VkComputePipelineCreateInfo *)local_pCreateInfos, pAllocator, pPipelines);
394 delete[] local_pCreateInfos;
Maciej Jesionowski42200702016-11-23 10:44:34 +0100395 {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600396 uint64_t unique_id = 0;
397 std::lock_guard<std::mutex> lock(global_lock);
398 for (uint32_t i = 0; i < createInfoCount; ++i) {
Maciej Jesionowski42200702016-11-23 10:44:34 +0100399 if (pPipelines[i] != VK_NULL_HANDLE) {
400 unique_id = global_unique_id++;
401 my_device_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(pPipelines[i]);
402 pPipelines[i] = reinterpret_cast<VkPipeline &>(unique_id);
403 }
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600404 }
405 }
406 return result;
407}
408
409VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
410 const VkGraphicsPipelineCreateInfo *pCreateInfos,
411 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700412 layer_data *my_device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600413 safe_VkGraphicsPipelineCreateInfo *local_pCreateInfos = NULL;
414 if (pCreateInfos) {
415 local_pCreateInfos = new safe_VkGraphicsPipelineCreateInfo[createInfoCount];
416 std::lock_guard<std::mutex> lock(global_lock);
417 for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) {
418 local_pCreateInfos[idx0].initialize(&pCreateInfos[idx0]);
419 if (pCreateInfos[idx0].basePipelineHandle) {
420 local_pCreateInfos[idx0].basePipelineHandle =
421 (VkPipeline)my_device_data
422 ->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[idx0].basePipelineHandle)];
423 }
424 if (pCreateInfos[idx0].layout) {
425 local_pCreateInfos[idx0].layout =
426 (VkPipelineLayout)
427 my_device_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[idx0].layout)];
428 }
429 if (pCreateInfos[idx0].pStages) {
430 for (uint32_t idx1 = 0; idx1 < pCreateInfos[idx0].stageCount; ++idx1) {
431 if (pCreateInfos[idx0].pStages[idx1].module) {
432 local_pCreateInfos[idx0].pStages[idx1].module =
433 (VkShaderModule)my_device_data
434 ->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[idx0].pStages[idx1].module)];
435 }
436 }
437 }
438 if (pCreateInfos[idx0].renderPass) {
439 local_pCreateInfos[idx0].renderPass =
440 (VkRenderPass)
441 my_device_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[idx0].renderPass)];
442 }
443 }
444 }
445 if (pipelineCache) {
446 std::lock_guard<std::mutex> lock(global_lock);
447 pipelineCache = (VkPipelineCache)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t &>(pipelineCache)];
448 }
449
450 VkResult result = my_device_data->device_dispatch_table->CreateGraphicsPipelines(
451 device, pipelineCache, createInfoCount, (const VkGraphicsPipelineCreateInfo *)local_pCreateInfos, pAllocator, pPipelines);
452 delete[] local_pCreateInfos;
Maciej Jesionowski42200702016-11-23 10:44:34 +0100453 {
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600454 uint64_t unique_id = 0;
455 std::lock_guard<std::mutex> lock(global_lock);
456 for (uint32_t i = 0; i < createInfoCount; ++i) {
Maciej Jesionowski42200702016-11-23 10:44:34 +0100457 if (pPipelines[i] != VK_NULL_HANDLE) {
458 unique_id = global_unique_id++;
459 my_device_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(pPipelines[i]);
460 pPipelines[i] = reinterpret_cast<VkPipeline &>(unique_id);
461 }
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600462 }
463 }
464 return result;
465}
466
467VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(VkInstance instance,
468 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
469 const VkAllocationCallbacks *pAllocator,
470 VkDebugReportCallbackEXT *pMsgCallback) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700471 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600472 VkResult result =
473 instance_data->instance_dispatch_table->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
474
475 if (VK_SUCCESS == result) {
476 result = layer_create_msg_callback(instance_data->report_data, false, pCreateInfo, pAllocator, pMsgCallback);
477 }
478 return result;
479}
480
481VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback,
482 const VkAllocationCallbacks *pAllocator) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700483 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600484 instance_data->instance_dispatch_table->DestroyDebugReportCallbackEXT(instance, callback, pAllocator);
485 layer_destroy_msg_callback(instance_data->report_data, callback, pAllocator);
486}
487
488VKAPI_ATTR void VKAPI_CALL DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
489 VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
490 int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700491 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600492 instance_data->instance_dispatch_table->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix,
493 pMsg);
494}
495
496VKAPI_ATTR VkResult VKAPI_CALL CreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo,
497 const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700498 layer_data *my_map_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600499 safe_VkSwapchainCreateInfoKHR *local_pCreateInfo = NULL;
500 if (pCreateInfo) {
501 std::lock_guard<std::mutex> lock(global_lock);
502 local_pCreateInfo = new safe_VkSwapchainCreateInfoKHR(pCreateInfo);
503 local_pCreateInfo->oldSwapchain =
504 (VkSwapchainKHR)my_map_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfo->oldSwapchain)];
505 // Need to pull surface mapping from the instance-level map
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700506 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(my_map_data->gpu), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600507 local_pCreateInfo->surface =
508 (VkSurfaceKHR)instance_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfo->surface)];
509 }
510
511 VkResult result = my_map_data->device_dispatch_table->CreateSwapchainKHR(
512 device, (const VkSwapchainCreateInfoKHR *)local_pCreateInfo, pAllocator, pSwapchain);
513 if (local_pCreateInfo) {
514 delete local_pCreateInfo;
515 }
516 if (VK_SUCCESS == result) {
517 std::lock_guard<std::mutex> lock(global_lock);
518 uint64_t unique_id = global_unique_id++;
519 my_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(*pSwapchain);
520 *pSwapchain = reinterpret_cast<VkSwapchainKHR &>(unique_id);
521 }
522 return result;
523}
524
Dustin Graves9a6eb052017-03-28 14:18:54 -0600525VKAPI_ATTR VkResult VKAPI_CALL CreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
526 const VkSwapchainCreateInfoKHR *pCreateInfos,
527 const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchains) {
528 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
529 safe_VkSwapchainCreateInfoKHR *local_pCreateInfos = NULL;
530 {
531 std::lock_guard<std::mutex> lock(global_lock);
532 if (pCreateInfos) {
533 // Need to pull surface mapping from the instance-level map
534 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(dev_data->gpu), layer_data_map);
535 local_pCreateInfos = new safe_VkSwapchainCreateInfoKHR[swapchainCount];
536 for (uint32_t i = 0; i < swapchainCount; ++i) {
537 local_pCreateInfos[i].initialize(&pCreateInfos[i]);
538 if (pCreateInfos[i].surface) {
539 local_pCreateInfos[i].surface =
540 (VkSurfaceKHR)instance_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[i].surface)];
541 }
542 if (pCreateInfos[i].oldSwapchain) {
543 local_pCreateInfos[i].oldSwapchain =
544 (VkSwapchainKHR)
545 dev_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfos[i].oldSwapchain)];
546 }
547 }
548 }
549 }
550 VkResult result = dev_data->device_dispatch_table->CreateSharedSwapchainsKHR(
551 device, swapchainCount, (const VkSwapchainCreateInfoKHR *)local_pCreateInfos, pAllocator, pSwapchains);
552 if (local_pCreateInfos) delete[] local_pCreateInfos;
553 if (VK_SUCCESS == result) {
554 std::lock_guard<std::mutex> lock(global_lock);
555 for (uint32_t i = 0; i < swapchainCount; i++) {
556 uint64_t unique_id = global_unique_id++;
557 dev_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(pSwapchains[i]);
558 pSwapchains[i] = reinterpret_cast<VkSwapchainKHR &>(unique_id);
559 }
560 }
561 return result;
562}
563
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600564VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount,
565 VkImage *pSwapchainImages) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700566 layer_data *my_device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600567 if (VK_NULL_HANDLE != swapchain) {
568 std::lock_guard<std::mutex> lock(global_lock);
569 swapchain = (VkSwapchainKHR)my_device_data->unique_id_mapping[reinterpret_cast<uint64_t &>(swapchain)];
570 }
571 VkResult result =
572 my_device_data->device_dispatch_table->GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
573 // TODO : Need to add corresponding code to delete these images
574 if (VK_SUCCESS == result) {
575 if ((*pSwapchainImageCount > 0) && pSwapchainImages) {
576 uint64_t unique_id = 0;
577 std::lock_guard<std::mutex> lock(global_lock);
578 for (uint32_t i = 0; i < *pSwapchainImageCount; ++i) {
579 unique_id = global_unique_id++;
580 my_device_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(pSwapchainImages[i]);
581 pSwapchainImages[i] = reinterpret_cast<VkImage &>(unique_id);
582 }
583 }
584 }
585 return result;
586}
587
Chris Forbes0f507f22017-04-16 13:13:17 +1200588VKAPI_ATTR VkResult VKAPI_CALL QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) {
589 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(queue), layer_data_map);
590 safe_VkPresentInfoKHR *local_pPresentInfo = NULL;
591 {
592 std::lock_guard<std::mutex> lock(global_lock);
593 if (pPresentInfo) {
594 local_pPresentInfo = new safe_VkPresentInfoKHR(pPresentInfo);
595 if (local_pPresentInfo->pWaitSemaphores) {
596 for (uint32_t index1 = 0; index1 < local_pPresentInfo->waitSemaphoreCount; ++index1) {
597 local_pPresentInfo->pWaitSemaphores[index1] =
598 (VkSemaphore)dev_data
599 ->unique_id_mapping[reinterpret_cast<const uint64_t &>(local_pPresentInfo->pWaitSemaphores[index1])];
600 }
601 }
602 if (local_pPresentInfo->pSwapchains) {
603 for (uint32_t index1 = 0; index1 < local_pPresentInfo->swapchainCount; ++index1) {
604 local_pPresentInfo->pSwapchains[index1] =
605 (VkSwapchainKHR)dev_data
606 ->unique_id_mapping[reinterpret_cast<const uint64_t &>(local_pPresentInfo->pSwapchains[index1])];
607 }
608 }
609 }
610 }
611 VkResult result = dev_data->device_dispatch_table->QueuePresentKHR(queue, (const VkPresentInfoKHR *)local_pPresentInfo);
612
613 // pResults is an output array embedded in a structure. The code generator neglects to copy back from the safe_* version,
614 // so handle it as a special case here:
615 if (pPresentInfo && pPresentInfo->pResults) {
616 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
617 pPresentInfo->pResults[i] = local_pPresentInfo->pResults[i];
618 }
619 }
620
621 if (local_pPresentInfo) delete local_pPresentInfo;
622 return result;
623}
624
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700625VKAPI_ATTR VkResult VKAPI_CALL CreateDescriptorUpdateTemplateKHR(VkDevice device,
626 const VkDescriptorUpdateTemplateCreateInfoKHR *pCreateInfo,
627 const VkAllocationCallbacks *pAllocator,
628 VkDescriptorUpdateTemplateKHR *pDescriptorUpdateTemplate) {
629 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700630 safe_VkDescriptorUpdateTemplateCreateInfoKHR *local_create_info = NULL;
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700631 {
632 std::lock_guard<std::mutex> lock(global_lock);
633 if (pCreateInfo) {
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700634 local_create_info = new safe_VkDescriptorUpdateTemplateCreateInfoKHR(pCreateInfo);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700635 if (pCreateInfo->descriptorSetLayout) {
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700636 local_create_info->descriptorSetLayout =
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700637 (VkDescriptorSetLayout)
638 dev_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfo->descriptorSetLayout)];
639 }
640 if (pCreateInfo->pipelineLayout) {
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700641 local_create_info->pipelineLayout =
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700642 (VkPipelineLayout)dev_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pCreateInfo->pipelineLayout)];
643 }
644 }
645 }
646 VkResult result = dev_data->device_dispatch_table->CreateDescriptorUpdateTemplateKHR(
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700647 device, (const VkDescriptorUpdateTemplateCreateInfoKHR *)local_create_info, pAllocator, pDescriptorUpdateTemplate);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700648 if (VK_SUCCESS == result) {
649 std::lock_guard<std::mutex> lock(global_lock);
650 uint64_t unique_id = global_unique_id++;
651 dev_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(*pDescriptorUpdateTemplate);
652 *pDescriptorUpdateTemplate = reinterpret_cast<VkDescriptorUpdateTemplateKHR &>(unique_id);
Mark Lobodzinski4f3ce672017-03-03 10:28:21 -0700653
654 // Shadow template createInfo for later updates
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700655 std::unique_ptr<TEMPLATE_STATE> template_state(new TEMPLATE_STATE(*pDescriptorUpdateTemplate, local_create_info));
Mark Lobodzinski4f3ce672017-03-03 10:28:21 -0700656 dev_data->desc_template_map[unique_id] = std::move(template_state);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700657 }
658 return result;
659}
660
661VKAPI_ATTR void VKAPI_CALL DestroyDescriptorUpdateTemplateKHR(VkDevice device,
662 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
663 const VkAllocationCallbacks *pAllocator) {
664 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
665 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700666 uint64_t descriptor_update_template_id = reinterpret_cast<uint64_t &>(descriptorUpdateTemplate);
667 dev_data->desc_template_map.erase(descriptor_update_template_id);
668 descriptorUpdateTemplate = (VkDescriptorUpdateTemplateKHR)dev_data->unique_id_mapping[descriptor_update_template_id];
669 dev_data->unique_id_mapping.erase(descriptor_update_template_id);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700670 lock.unlock();
671 dev_data->device_dispatch_table->DestroyDescriptorUpdateTemplateKHR(device, descriptorUpdateTemplate, pAllocator);
672}
673
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700674void *BuildUnwrappedUpdateTemplateBuffer(layer_data *dev_data, uint64_t descriptorUpdateTemplate, const void *pData) {
675 auto const template_map_entry = dev_data->desc_template_map.find(descriptorUpdateTemplate);
676 if (template_map_entry == dev_data->desc_template_map.end()) {
677 assert(0);
678 }
679 auto const &create_info = template_map_entry->second->create_info;
680 size_t allocation_size = 0;
681 std::vector<std::tuple<size_t, VkDebugReportObjectTypeEXT, void *>> template_entries;
682
683 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
684 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
685 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
686 char *update_entry = (char *)(pData) + offset;
687
688 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
689 case VK_DESCRIPTOR_TYPE_SAMPLER:
690 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
691 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
692 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
693 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
694 auto image_entry = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
695 allocation_size = std::max(allocation_size, offset + sizeof(VkDescriptorImageInfo));
696
697 VkDescriptorImageInfo *wrapped_entry = new VkDescriptorImageInfo(*image_entry);
698 wrapped_entry->sampler = reinterpret_cast<VkSampler &>(
699 dev_data->unique_id_mapping[reinterpret_cast<uint64_t &>(image_entry->sampler)]);
700 wrapped_entry->imageView = reinterpret_cast<VkImageView &>(
701 dev_data->unique_id_mapping[reinterpret_cast<uint64_t &>(image_entry->imageView)]);
702 template_entries.emplace_back(offset, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
703 reinterpret_cast<void *>(wrapped_entry));
704 } break;
705
706 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
707 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
708 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
709 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
710 auto buffer_entry = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
711 allocation_size = std::max(allocation_size, offset + sizeof(VkDescriptorBufferInfo));
712
713 VkDescriptorBufferInfo *wrapped_entry = new VkDescriptorBufferInfo(*buffer_entry);
Mark Lobodzinskid5197d02017-03-15 13:13:49 -0600714 wrapped_entry->buffer = reinterpret_cast<VkBuffer &>(
715 dev_data->unique_id_mapping[reinterpret_cast<uint64_t &>(buffer_entry->buffer)]);
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700716 template_entries.emplace_back(offset, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
717 reinterpret_cast<void *>(wrapped_entry));
718 } break;
719
720 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
721 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
Mark Lobodzinskid5197d02017-03-15 13:13:49 -0600722 auto buffer_view_handle = reinterpret_cast<uint64_t *>(update_entry);
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700723 allocation_size = std::max(allocation_size, offset + sizeof(VkBufferView));
724
Mark Lobodzinskid5197d02017-03-15 13:13:49 -0600725 uint64_t wrapped_entry = dev_data->unique_id_mapping[reinterpret_cast<uint64_t &>(*buffer_view_handle)];
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700726 template_entries.emplace_back(offset, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT,
727 reinterpret_cast<void *>(wrapped_entry));
728 } break;
729 default:
730 assert(0);
731 break;
732 }
733 }
734 }
735 // Allocate required buffer size and populate with source/unwrapped data
736 void *unwrapped_data = malloc(allocation_size);
737 for (auto &this_entry : template_entries) {
738 VkDebugReportObjectTypeEXT type = std::get<1>(this_entry);
739 void *destination = (char *)unwrapped_data + std::get<0>(this_entry);
740 void *source = (char *)std::get<2>(this_entry);
741
742 switch (type) {
743 case VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT:
744 *(reinterpret_cast<VkDescriptorImageInfo *>(destination)) = *(reinterpret_cast<VkDescriptorImageInfo *>(source));
745 delete reinterpret_cast<VkDescriptorImageInfo *>(source);
746 break;
747 case VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT:
748 *(reinterpret_cast<VkDescriptorBufferInfo *>(destination)) = *(reinterpret_cast<VkDescriptorBufferInfo *>(source));
749 delete reinterpret_cast<VkDescriptorBufferInfo *>(source);
750 break;
751 case VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT:
Mark Lobodzinskid5197d02017-03-15 13:13:49 -0600752 *(reinterpret_cast<VkBufferView *>(destination)) = reinterpret_cast<VkBufferView>(source);
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700753 break;
754 default:
755 assert(0);
756 break;
757 }
758 }
759 return (void *)unwrapped_data;
760}
761
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700762VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSetWithTemplateKHR(VkDevice device, VkDescriptorSet descriptorSet,
763 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
764 const void *pData) {
765 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700766 uint64_t template_handle = reinterpret_cast<uint64_t &>(descriptorUpdateTemplate);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700767 {
768 std::lock_guard<std::mutex> lock(global_lock);
769 descriptorSet = (VkDescriptorSet)dev_data->unique_id_mapping[reinterpret_cast<uint64_t &>(descriptorSet)];
Mark Lobodzinski94d9e8c2017-03-06 16:18:19 -0700770 descriptorUpdateTemplate = (VkDescriptorUpdateTemplateKHR)dev_data->unique_id_mapping[template_handle];
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700771 }
Mark Lobodzinski5e7a02f2017-03-29 11:55:44 -0600772 void *unwrapped_buffer = BuildUnwrappedUpdateTemplateBuffer(dev_data, template_handle, pData);
Mark Lobodzinskib523f7c2017-03-06 09:00:21 -0700773 dev_data->device_dispatch_table->UpdateDescriptorSetWithTemplateKHR(device, descriptorSet, descriptorUpdateTemplate,
774 unwrapped_buffer);
775 free(unwrapped_buffer);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700776}
777
778VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSetWithTemplateKHR(VkCommandBuffer commandBuffer,
779 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
780 VkPipelineLayout layout, uint32_t set, const void *pData) {
781 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(commandBuffer), layer_data_map);
Mark Lobodzinski1c471262017-03-28 16:22:56 -0600782 uint64_t template_handle = reinterpret_cast<uint64_t &>(descriptorUpdateTemplate);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700783 {
784 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinski1c471262017-03-28 16:22:56 -0600785 descriptorUpdateTemplate = (VkDescriptorUpdateTemplateKHR)dev_data->unique_id_mapping[template_handle];
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700786 layout = (VkPipelineLayout)dev_data->unique_id_mapping[reinterpret_cast<uint64_t &>(layout)];
787 }
Mark Lobodzinski1c471262017-03-28 16:22:56 -0600788 void *unwrapped_buffer = BuildUnwrappedUpdateTemplateBuffer(dev_data, template_handle, pData);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700789 dev_data->device_dispatch_table->CmdPushDescriptorSetWithTemplateKHR(commandBuffer, descriptorUpdateTemplate, layout, set,
Mark Lobodzinski1c471262017-03-28 16:22:56 -0600790 unwrapped_buffer);
791 free(unwrapped_buffer);
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700792}
793
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600794#ifndef __ANDROID__
795VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceDisplayPropertiesKHR(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
796 VkDisplayPropertiesKHR *pProperties) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700797 layer_data *my_map_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600798 safe_VkDisplayPropertiesKHR *local_pProperties = NULL;
799 {
800 std::lock_guard<std::mutex> lock(global_lock);
801 if (pProperties) {
802 local_pProperties = new safe_VkDisplayPropertiesKHR[*pPropertyCount];
803 for (uint32_t idx0 = 0; idx0 < *pPropertyCount; ++idx0) {
804 local_pProperties[idx0].initialize(&pProperties[idx0]);
805 if (pProperties[idx0].display) {
806 local_pProperties[idx0].display =
807 (VkDisplayKHR)my_map_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(pProperties[idx0].display)];
808 }
809 }
810 }
811 }
812
813 VkResult result = my_map_data->instance_dispatch_table->GetPhysicalDeviceDisplayPropertiesKHR(
814 physicalDevice, pPropertyCount, (VkDisplayPropertiesKHR *)local_pProperties);
815 if (result == VK_SUCCESS && pProperties) {
816 for (uint32_t idx0 = 0; idx0 < *pPropertyCount; ++idx0) {
817 std::lock_guard<std::mutex> lock(global_lock);
818
819 uint64_t unique_id = global_unique_id++;
820 my_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(local_pProperties[idx0].display);
821 pProperties[idx0].display = reinterpret_cast<VkDisplayKHR &>(unique_id);
822 pProperties[idx0].displayName = local_pProperties[idx0].displayName;
823 pProperties[idx0].physicalDimensions = local_pProperties[idx0].physicalDimensions;
824 pProperties[idx0].physicalResolution = local_pProperties[idx0].physicalResolution;
825 pProperties[idx0].supportedTransforms = local_pProperties[idx0].supportedTransforms;
826 pProperties[idx0].planeReorderPossible = local_pProperties[idx0].planeReorderPossible;
827 pProperties[idx0].persistentContent = local_pProperties[idx0].persistentContent;
828 }
829 }
830 if (local_pProperties) {
831 delete[] local_pProperties;
832 }
833 return result;
834}
835
836VKAPI_ATTR VkResult VKAPI_CALL GetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
837 uint32_t *pDisplayCount, VkDisplayKHR *pDisplays) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700838 layer_data *my_map_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600839 VkResult result = my_map_data->instance_dispatch_table->GetDisplayPlaneSupportedDisplaysKHR(physicalDevice, planeIndex,
840 pDisplayCount, pDisplays);
841 if (VK_SUCCESS == result) {
842 if ((*pDisplayCount > 0) && pDisplays) {
843 std::lock_guard<std::mutex> lock(global_lock);
844 for (uint32_t i = 0; i < *pDisplayCount; i++) {
845 auto it = my_map_data->unique_id_mapping.find(reinterpret_cast<const uint64_t &>(pDisplays[i]));
846 assert(it != my_map_data->unique_id_mapping.end());
847 pDisplays[i] = reinterpret_cast<VkDisplayKHR &>(it->second);
848 }
849 }
850 }
851 return result;
852}
853
854VKAPI_ATTR VkResult VKAPI_CALL GetDisplayModePropertiesKHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
855 uint32_t *pPropertyCount, VkDisplayModePropertiesKHR *pProperties) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700856 layer_data *my_map_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Mark Lobodzinskif0650ff2017-01-03 08:52:14 -0700857 VkDisplayModePropertiesKHR *local_pProperties = NULL;
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600858 {
859 std::lock_guard<std::mutex> lock(global_lock);
860 display = (VkDisplayKHR)my_map_data->unique_id_mapping[reinterpret_cast<uint64_t &>(display)];
861 if (pProperties) {
Mark Lobodzinskif0650ff2017-01-03 08:52:14 -0700862 local_pProperties = new VkDisplayModePropertiesKHR[*pPropertyCount];
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600863 }
864 }
865
866 VkResult result = my_map_data->instance_dispatch_table->GetDisplayModePropertiesKHR(
867 physicalDevice, display, pPropertyCount, (VkDisplayModePropertiesKHR *)local_pProperties);
868 if (result == VK_SUCCESS && pProperties) {
869 for (uint32_t idx0 = 0; idx0 < *pPropertyCount; ++idx0) {
870 std::lock_guard<std::mutex> lock(global_lock);
871
872 uint64_t unique_id = global_unique_id++;
873 my_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(local_pProperties[idx0].displayMode);
874 pProperties[idx0].displayMode = reinterpret_cast<VkDisplayModeKHR &>(unique_id);
875 pProperties[idx0].parameters.visibleRegion.width = local_pProperties[idx0].parameters.visibleRegion.width;
876 pProperties[idx0].parameters.visibleRegion.height = local_pProperties[idx0].parameters.visibleRegion.height;
877 pProperties[idx0].parameters.refreshRate = local_pProperties[idx0].parameters.refreshRate;
878 }
879 }
880 if (local_pProperties) {
881 delete[] local_pProperties;
882 }
883 return result;
884}
Norbert Nopper1dec9a52016-11-25 07:55:13 +0100885
886VKAPI_ATTR VkResult VKAPI_CALL GetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
887 uint32_t planeIndex, VkDisplayPlaneCapabilitiesKHR *pCapabilities) {
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700888 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
Norbert Nopper1dec9a52016-11-25 07:55:13 +0100889 {
890 std::lock_guard<std::mutex> lock(global_lock);
891 auto it = dev_data->unique_id_mapping.find(reinterpret_cast<uint64_t &>(mode));
892 if (it == dev_data->unique_id_mapping.end()) {
893 uint64_t unique_id = global_unique_id++;
894 dev_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(mode);
895
896 mode = reinterpret_cast<VkDisplayModeKHR &>(unique_id);
897 } else {
898 mode = reinterpret_cast<VkDisplayModeKHR &>(it->second);
899 }
900 }
901 VkResult result =
902 dev_data->instance_dispatch_table->GetDisplayPlaneCapabilitiesKHR(physicalDevice, mode, planeIndex, pCapabilities);
903 return result;
904}
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600905#endif
906
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600907VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectTagEXT(VkDevice device, VkDebugMarkerObjectTagInfoEXT *pTagInfo) {
908 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
909 auto local_tag_info = new safe_VkDebugMarkerObjectTagInfoEXT(pTagInfo);
910 {
911 std::lock_guard<std::mutex> lock(global_lock);
912 auto it = device_data->unique_id_mapping.find(reinterpret_cast<uint64_t &>(local_tag_info->object));
913 if (it != device_data->unique_id_mapping.end()) {
914 local_tag_info->object = it->second;
915 }
916 }
917 VkResult result = device_data->device_dispatch_table->DebugMarkerSetObjectTagEXT(
918 device, reinterpret_cast<VkDebugMarkerObjectTagInfoEXT *>(local_tag_info));
919 return result;
920}
921
922VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectNameEXT(VkDevice device, VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
923 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
924 auto local_name_info = new safe_VkDebugMarkerObjectNameInfoEXT(pNameInfo);
925 {
926 std::lock_guard<std::mutex> lock(global_lock);
927 auto it = device_data->unique_id_mapping.find(reinterpret_cast<uint64_t &>(local_name_info->object));
928 if (it != device_data->unique_id_mapping.end()) {
929 local_name_info->object = it->second;
930 }
931 }
932 VkResult result = device_data->device_dispatch_table->DebugMarkerSetObjectNameEXT(
933 device, reinterpret_cast<VkDebugMarkerObjectNameInfoEXT *>(local_name_info));
934 return result;
935}
936
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700937} // namespace unique_objects
Mark Lobodzinskidc3bd852016-09-06 16:12:23 -0600938
939// vk_layer_logging.h expects these to be defined
940VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugReportCallbackEXT(VkInstance instance,
941 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
942 const VkAllocationCallbacks *pAllocator,
943 VkDebugReportCallbackEXT *pMsgCallback) {
944 return unique_objects::CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
945}
946
947VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback,
948 const VkAllocationCallbacks *pAllocator) {
949 unique_objects::DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
950}
951
952VKAPI_ATTR void VKAPI_CALL vkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
953 VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
954 int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
955 unique_objects::DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
956}
957
958VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
959 VkExtensionProperties *pProperties) {
960 return unique_objects::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
961}
962
963VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
964 VkLayerProperties *pProperties) {
965 return unique_objects::EnumerateInstanceLayerProperties(pCount, pProperties);
966}
967
968VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
969 VkLayerProperties *pProperties) {
970 assert(physicalDevice == VK_NULL_HANDLE);
971 return unique_objects::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
972}
973
974VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
975 return unique_objects::GetDeviceProcAddr(dev, funcName);
976}
977
978VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
979 return unique_objects::GetInstanceProcAddr(instance, funcName);
980}
981
982VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
983 const char *pLayerName, uint32_t *pCount,
984 VkExtensionProperties *pProperties) {
985 assert(physicalDevice == VK_NULL_HANDLE);
986 return unique_objects::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
987}
Mark Young39389872017-01-19 21:10:49 -0700988
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700989VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
990 const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -0700991 return unique_objects::GetPhysicalDeviceProcAddr(instance, funcName);
992}
993
994VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
995 assert(pVersionStruct != NULL);
996 assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
997
998 // Fill in the function pointers if our version is at least capable of having the structure contain them.
999 if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
1000 pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
1001 pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
1002 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
1003 }
1004
1005 if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1006 unique_objects::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
1007 } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1008 pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
1009 }
1010
1011 return VK_SUCCESS;
1012}