layers: Track display and display mode objects
[platform/upstream/Vulkan-LoaderAndValidationLayers.git] / layers / object_tracker_utils.cpp
1 /* Copyright (c) 2015-2017 The Khronos Group Inc.
2  * Copyright (c) 2015-2017 Valve Corporation
3  * Copyright (c) 2015-2017 LunarG, Inc.
4  * Copyright (C) 2015-2017 Google Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Mark Lobodzinski <mark@lunarg.com>
19  * Author: Jon Ashburn <jon@lunarg.com>
20  * Author: Tobin Ehlis <tobin@lunarg.com>
21  */
22
23 #include "object_tracker.h"
24
25 namespace object_tracker {
26
27 std::unordered_map<void *, layer_data *> layer_data_map;
28 device_table_map ot_device_table_map;
29 instance_table_map ot_instance_table_map;
30 std::mutex global_lock;
31 uint64_t object_track_index = 0;
32 uint32_t loader_layer_if_version = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
33
34 void InitObjectTracker(layer_data *my_data, const VkAllocationCallbacks *pAllocator) {
35     layer_debug_report_actions(my_data->report_data, my_data->logging_callback, pAllocator, "lunarg_object_tracker");
36     layer_debug_messenger_actions(my_data->report_data, my_data->logging_messenger, pAllocator, "lunarg_object_tracker");
37 }
38
39 // Add new queue to head of global queue list
40 void AddQueueInfo(VkDevice device, uint32_t queue_node_index, VkQueue queue) {
41     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
42     auto queueItem = device_data->queue_info_map.find(queue);
43     if (queueItem == device_data->queue_info_map.end()) {
44         ObjTrackQueueInfo *p_queue_info = new ObjTrackQueueInfo;
45         if (p_queue_info != NULL) {
46             memset(p_queue_info, 0, sizeof(ObjTrackQueueInfo));
47             p_queue_info->queue = queue;
48             p_queue_info->queue_node_index = queue_node_index;
49             device_data->queue_info_map[queue] = p_queue_info;
50         } else {
51             log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
52                     HandleToUint64(queue), OBJTRACK_INTERNAL_ERROR,
53                     "ERROR:  VK_ERROR_OUT_OF_HOST_MEMORY -- could not allocate memory for Queue Information");
54         }
55     }
56 }
57
58 // Destroy memRef lists and free all memory
59 void DestroyQueueDataStructures(VkDevice device) {
60     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
61
62     for (auto queue_item : device_data->queue_info_map) {
63         delete queue_item.second;
64     }
65     device_data->queue_info_map.clear();
66
67     // Destroy the items in the queue map
68     auto queue = device_data->object_map[kVulkanObjectTypeQueue].begin();
69     while (queue != device_data->object_map[kVulkanObjectTypeQueue].end()) {
70         uint32_t obj_index = queue->second->object_type;
71         assert(device_data->num_total_objects > 0);
72         device_data->num_total_objects--;
73         assert(device_data->num_objects[obj_index] > 0);
74         device_data->num_objects[obj_index]--;
75         log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
76                 queue->second->handle, OBJTRACK_NONE,
77                 "OBJ_STAT Destroy Queue obj 0x%" PRIxLEAST64 " (%" PRIu64 " total objs remain & %" PRIu64 " Queue objs).",
78                 queue->second->handle, device_data->num_total_objects, device_data->num_objects[obj_index]);
79         delete queue->second;
80         queue = device_data->object_map[kVulkanObjectTypeQueue].erase(queue);
81     }
82 }
83
84 // Check Queue type flags for selected queue operations
85 void ValidateQueueFlags(VkQueue queue, const char *function) {
86     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(queue), layer_data_map);
87     auto queue_item = device_data->queue_info_map.find(queue);
88     if (queue_item != device_data->queue_info_map.end()) {
89         ObjTrackQueueInfo *pQueueInfo = queue_item->second;
90         if (pQueueInfo != NULL) {
91             layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(device_data->physical_device), layer_data_map);
92             if ((instance_data->queue_family_properties[pQueueInfo->queue_node_index].queueFlags & VK_QUEUE_SPARSE_BINDING_BIT) ==
93                 0) {
94                 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
95                         HandleToUint64(queue), VALIDATION_ERROR_31600011,
96                         "Attempting %s on a non-memory-management capable queue -- VK_QUEUE_SPARSE_BINDING_BIT not set.", function);
97             }
98         }
99     }
100 }
101
102 // Look for this device object in any of the instance child devices lists.
103 // NOTE: This is of dubious value. In most circumstances Vulkan will die a flaming death if a dispatchable object is invalid.
104 // However, if this layer is loaded first and GetProcAddress is used to make API calls, it will detect bad DOs.
105 bool ValidateDeviceObject(uint64_t device_handle, enum UNIQUE_VALIDATION_ERROR_CODE invalid_handle_code,
106                           enum UNIQUE_VALIDATION_ERROR_CODE wrong_device_code) {
107     VkInstance last_instance = nullptr;
108     for (auto layer_data : layer_data_map) {
109         for (auto object : layer_data.second->object_map[kVulkanObjectTypeDevice]) {
110             // Grab last instance to use for possible error message
111             last_instance = layer_data.second->instance;
112             if (object.second->handle == device_handle) return false;
113         }
114     }
115
116     layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(last_instance), layer_data_map);
117     return log_msg(instance_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, device_handle,
118                    invalid_handle_code, "Invalid Device Object 0x%" PRIxLEAST64 ".", device_handle);
119 }
120
121 void AllocateCommandBuffer(VkDevice device, const VkCommandPool command_pool, const VkCommandBuffer command_buffer,
122                            VkCommandBufferLevel level) {
123     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
124
125     log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
126             HandleToUint64(command_buffer), OBJTRACK_NONE, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
127             object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT", HandleToUint64(command_buffer));
128
129     ObjTrackState *pNewObjNode = new ObjTrackState;
130     pNewObjNode->object_type = kVulkanObjectTypeCommandBuffer;
131     pNewObjNode->handle = HandleToUint64(command_buffer);
132     pNewObjNode->parent_object = HandleToUint64(command_pool);
133     if (level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
134         pNewObjNode->status = OBJSTATUS_COMMAND_BUFFER_SECONDARY;
135     } else {
136         pNewObjNode->status = OBJSTATUS_NONE;
137     }
138     device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)] = pNewObjNode;
139     device_data->num_objects[kVulkanObjectTypeCommandBuffer]++;
140     device_data->num_total_objects++;
141 }
142
143 bool ValidateCommandBuffer(VkDevice device, VkCommandPool command_pool, VkCommandBuffer command_buffer) {
144     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
145     bool skip = false;
146     uint64_t object_handle = HandleToUint64(command_buffer);
147     if (device_data->object_map[kVulkanObjectTypeCommandBuffer].find(object_handle) !=
148         device_data->object_map[kVulkanObjectTypeCommandBuffer].end()) {
149         ObjTrackState *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
150
151         if (pNode->parent_object != HandleToUint64(command_pool)) {
152             skip |= log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
153                             object_handle, VALIDATION_ERROR_28411407,
154                             "FreeCommandBuffers is attempting to free Command Buffer 0x%" PRIxLEAST64
155                             " belonging to Command Pool 0x%" PRIxLEAST64 " from pool 0x%" PRIxLEAST64 ").",
156                             HandleToUint64(command_buffer), pNode->parent_object, HandleToUint64(command_pool));
157         }
158     } else {
159         skip |= log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
160                         object_handle, VALIDATION_ERROR_28400060, "Invalid %s Object 0x%" PRIxLEAST64 ".",
161                         object_string[kVulkanObjectTypeCommandBuffer], object_handle);
162     }
163     return skip;
164 }
165
166 void AllocateDescriptorSet(VkDevice device, VkDescriptorPool descriptor_pool, VkDescriptorSet descriptor_set) {
167     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
168
169     log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
170             HandleToUint64(descriptor_set), OBJTRACK_NONE, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
171             object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT", HandleToUint64(descriptor_set));
172
173     ObjTrackState *pNewObjNode = new ObjTrackState;
174     pNewObjNode->object_type = kVulkanObjectTypeDescriptorSet;
175     pNewObjNode->status = OBJSTATUS_NONE;
176     pNewObjNode->handle = HandleToUint64(descriptor_set);
177     pNewObjNode->parent_object = HandleToUint64(descriptor_pool);
178     device_data->object_map[kVulkanObjectTypeDescriptorSet][HandleToUint64(descriptor_set)] = pNewObjNode;
179     device_data->num_objects[kVulkanObjectTypeDescriptorSet]++;
180     device_data->num_total_objects++;
181 }
182
183 bool ValidateDescriptorSet(VkDevice device, VkDescriptorPool descriptor_pool, VkDescriptorSet descriptor_set) {
184     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
185     bool skip = false;
186     uint64_t object_handle = HandleToUint64(descriptor_set);
187     auto dsItem = device_data->object_map[kVulkanObjectTypeDescriptorSet].find(object_handle);
188     if (dsItem != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
189         ObjTrackState *pNode = dsItem->second;
190
191         if (pNode->parent_object != HandleToUint64(descriptor_pool)) {
192             skip |= log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
193                             object_handle, VALIDATION_ERROR_28613007,
194                             "FreeDescriptorSets is attempting to free descriptorSet 0x%" PRIxLEAST64
195                             " belonging to Descriptor Pool 0x%" PRIxLEAST64 " from pool 0x%" PRIxLEAST64 ").",
196                             HandleToUint64(descriptor_set), pNode->parent_object, HandleToUint64(descriptor_pool));
197         }
198     } else {
199         skip |= log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
200                         object_handle, VALIDATION_ERROR_2860026c, "Invalid %s Object 0x%" PRIxLEAST64 ".",
201                         object_string[kVulkanObjectTypeDescriptorSet], object_handle);
202     }
203     return skip;
204 }
205
206 template <typename DispObj>
207 static bool ValidateDescriptorWrite(DispObj disp, VkWriteDescriptorSet const *desc, bool isPush) {
208     bool skip = false;
209
210     if (!isPush && desc->dstSet) {
211         skip |= ValidateObject(disp, desc->dstSet, kVulkanObjectTypeDescriptorSet, false, VALIDATION_ERROR_15c00280,
212                                VALIDATION_ERROR_15c00009);
213     }
214
215     if ((desc->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||
216         (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) {
217         for (uint32_t idx2 = 0; idx2 < desc->descriptorCount; ++idx2) {
218             skip |= ValidateObject(disp, desc->pTexelBufferView[idx2], kVulkanObjectTypeBufferView, false,
219                                    VALIDATION_ERROR_15c00286, VALIDATION_ERROR_15c00009);
220         }
221     }
222
223     if ((desc->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||
224         (desc->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ||
225         (desc->descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
226         for (uint32_t idx3 = 0; idx3 < desc->descriptorCount; ++idx3) {
227             skip |= ValidateObject(disp, desc->pImageInfo[idx3].imageView, kVulkanObjectTypeImageView, false,
228                                    VALIDATION_ERROR_15c0028c, VALIDATION_ERROR_04600009);
229         }
230     }
231
232     if ((desc->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
233         (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||
234         (desc->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
235         (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
236         for (uint32_t idx4 = 0; idx4 < desc->descriptorCount; ++idx4) {
237             if (desc->pBufferInfo[idx4].buffer) {
238                 skip |= ValidateObject(disp, desc->pBufferInfo[idx4].buffer, kVulkanObjectTypeBuffer, false,
239                                        VALIDATION_ERROR_04401a01, VALIDATION_ERROR_UNDEFINED);
240             }
241         }
242     }
243
244     return skip;
245 }
246
247 VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSetKHR(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
248                                                    VkPipelineLayout layout, uint32_t set, uint32_t descriptorWriteCount,
249                                                    const VkWriteDescriptorSet *pDescriptorWrites) {
250     bool skip = false;
251     {
252         std::lock_guard<std::mutex> lock(global_lock);
253         skip |= ValidateObject(commandBuffer, commandBuffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_1be02401,
254                                VALIDATION_ERROR_1be00009);
255         skip |= ValidateObject(commandBuffer, layout, kVulkanObjectTypePipelineLayout, false, VALIDATION_ERROR_1be0be01,
256                                VALIDATION_ERROR_1be00009);
257         if (pDescriptorWrites) {
258             for (uint32_t index0 = 0; index0 < descriptorWriteCount; ++index0) {
259                 skip |= ValidateDescriptorWrite(commandBuffer, &pDescriptorWrites[index0], true);
260             }
261         }
262     }
263     if (skip) return;
264     get_dispatch_table(ot_device_table_map, commandBuffer)
265         ->CmdPushDescriptorSetKHR(commandBuffer, pipelineBindPoint, layout, set, descriptorWriteCount, pDescriptorWrites);
266 }
267
268 void CreateQueue(VkDevice device, VkQueue vkObj) {
269     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
270
271     log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
272             HandleToUint64(vkObj), OBJTRACK_NONE, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
273             object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT", HandleToUint64(vkObj));
274
275     ObjTrackState *p_obj_node = NULL;
276     auto queue_item = device_data->object_map[kVulkanObjectTypeQueue].find(HandleToUint64(vkObj));
277     if (queue_item == device_data->object_map[kVulkanObjectTypeQueue].end()) {
278         p_obj_node = new ObjTrackState;
279         device_data->object_map[kVulkanObjectTypeQueue][HandleToUint64(vkObj)] = p_obj_node;
280         device_data->num_objects[kVulkanObjectTypeQueue]++;
281         device_data->num_total_objects++;
282     } else {
283         p_obj_node = queue_item->second;
284     }
285     p_obj_node->object_type = kVulkanObjectTypeQueue;
286     p_obj_node->status = OBJSTATUS_NONE;
287     p_obj_node->handle = HandleToUint64(vkObj);
288 }
289
290 void CreateSwapchainImageObject(VkDevice dispatchable_object, VkImage swapchain_image, VkSwapchainKHR swapchain) {
291     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(dispatchable_object), layer_data_map);
292     log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
293             HandleToUint64(swapchain_image), OBJTRACK_NONE, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
294             object_track_index++, "SwapchainImage", HandleToUint64(swapchain_image));
295
296     ObjTrackState *pNewObjNode = new ObjTrackState;
297     pNewObjNode->object_type = kVulkanObjectTypeImage;
298     pNewObjNode->status = OBJSTATUS_NONE;
299     pNewObjNode->handle = HandleToUint64(swapchain_image);
300     pNewObjNode->parent_object = HandleToUint64(swapchain);
301     device_data->swapchainImageMap[HandleToUint64(swapchain_image)] = pNewObjNode;
302 }
303
304 void DeviceReportUndestroyedObjects(VkDevice device, VulkanObjectType object_type, enum UNIQUE_VALIDATION_ERROR_CODE error_code) {
305     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
306     for (const auto &item : device_data->object_map[object_type]) {
307         const ObjTrackState *object_info = item.second;
308         log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, get_debug_report_enum[object_type], object_info->handle,
309                 error_code, "OBJ ERROR : For device 0x%" PRIxLEAST64 ", %s object 0x%" PRIxLEAST64 " has not been destroyed.",
310                 HandleToUint64(device), object_string[object_type], object_info->handle);
311     }
312 }
313
314 void DeviceDestroyUndestroyedObjects(VkDevice device, VulkanObjectType object_type) {
315     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
316     while (!device_data->object_map[object_type].empty()) {
317         auto item = device_data->object_map[object_type].begin();
318
319         ObjTrackState *object_info = item->second;
320         DestroyObjectSilently(device, object_info->handle, object_type);
321     }
322 }
323
324 VKAPI_ATTR void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
325     std::unique_lock<std::mutex> lock(global_lock);
326
327     dispatch_key key = get_dispatch_key(instance);
328     layer_data *instance_data = GetLayerDataPtr(key, layer_data_map);
329
330     // Enable the temporary callback(s) here to catch cleanup issues:
331     if (instance_data->num_tmp_debug_messengers > 0) {
332         layer_enable_tmp_debug_messengers(instance_data->report_data, instance_data->num_tmp_debug_messengers,
333                                           instance_data->tmp_messenger_create_infos, instance_data->tmp_debug_messengers);
334     }
335     if (instance_data->num_tmp_report_callbacks > 0) {
336         layer_enable_tmp_report_callbacks(instance_data->report_data, instance_data->num_tmp_report_callbacks,
337                                           instance_data->tmp_report_create_infos, instance_data->tmp_report_callbacks);
338     }
339
340     // TODO: The instance handle can not be validated here. The loader will likely have to validate it.
341     ValidateObject(instance, instance, kVulkanObjectTypeInstance, true, VALIDATION_ERROR_2580bc01, VALIDATION_ERROR_UNDEFINED);
342
343     // Destroy physical devices
344     for (auto iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
345          iit != instance_data->object_map[kVulkanObjectTypePhysicalDevice].end();) {
346         ObjTrackState *pNode = iit->second;
347         VkPhysicalDevice physical_device = reinterpret_cast<VkPhysicalDevice>(pNode->handle);
348
349         DestroyObject(instance, physical_device, kVulkanObjectTypePhysicalDevice, nullptr, VALIDATION_ERROR_UNDEFINED,
350                       VALIDATION_ERROR_UNDEFINED);
351         iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
352     }
353
354     // Destroy child devices
355     for (auto iit = instance_data->object_map[kVulkanObjectTypeDevice].begin();
356          iit != instance_data->object_map[kVulkanObjectTypeDevice].end();) {
357         ObjTrackState *pNode = iit->second;
358
359         VkDevice device = reinterpret_cast<VkDevice>(pNode->handle);
360         VkDebugReportObjectTypeEXT debug_object_type = get_debug_report_enum[pNode->object_type];
361
362         log_msg(instance_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, debug_object_type, pNode->handle, OBJTRACK_OBJECT_LEAK,
363                 "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.",
364                 string_VkDebugReportObjectTypeEXT(debug_object_type), pNode->handle);
365
366         // Report any remaining objects in LL
367         ReportUndestroyedObjects(device, VALIDATION_ERROR_258004ea);
368         DestroyUndestroyedObjects(device);
369
370         DestroyObject(instance, device, kVulkanObjectTypeDevice, pAllocator, VALIDATION_ERROR_258004ec, VALIDATION_ERROR_258004ee);
371         iit = instance_data->object_map[kVulkanObjectTypeDevice].begin();
372     }
373
374     instance_data->object_map[kVulkanObjectTypeDevice].clear();
375
376     VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
377     pInstanceTable->DestroyInstance(instance, pAllocator);
378
379     // Disable and cleanup the temporary callback(s):
380     layer_disable_tmp_debug_messengers(instance_data->report_data, instance_data->num_tmp_debug_messengers,
381                                        instance_data->tmp_debug_messengers);
382     layer_disable_tmp_report_callbacks(instance_data->report_data, instance_data->num_tmp_report_callbacks,
383                                        instance_data->tmp_report_callbacks);
384     if (instance_data->num_tmp_debug_messengers > 0) {
385         layer_free_tmp_debug_messengers(instance_data->tmp_messenger_create_infos, instance_data->tmp_debug_messengers);
386         instance_data->num_tmp_debug_messengers = 0;
387     }
388     if (instance_data->num_tmp_report_callbacks > 0) {
389         layer_free_tmp_report_callbacks(instance_data->tmp_report_create_infos, instance_data->tmp_report_callbacks);
390         instance_data->num_tmp_report_callbacks = 0;
391     }
392
393     // Clean up logging callback, if any
394     while (instance_data->logging_messenger.size() > 0) {
395         VkDebugUtilsMessengerEXT messenger = instance_data->logging_messenger.back();
396         layer_destroy_messenger_callback(instance_data->report_data, messenger, pAllocator);
397         instance_data->logging_messenger.pop_back();
398     }
399     while (instance_data->logging_callback.size() > 0) {
400         VkDebugReportCallbackEXT callback = instance_data->logging_callback.back();
401         layer_destroy_report_callback(instance_data->report_data, callback, pAllocator);
402         instance_data->logging_callback.pop_back();
403     }
404
405     DestroyObject(instance, instance, kVulkanObjectTypeInstance, pAllocator, VALIDATION_ERROR_258004ec, VALIDATION_ERROR_258004ee);
406
407     layer_debug_utils_destroy_instance(instance_data->report_data);
408     FreeLayerDataPtr(key, layer_data_map);
409
410     lock.unlock();
411     ot_instance_table_map.erase(key);
412     delete pInstanceTable;
413 }
414
415 VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
416     std::unique_lock<std::mutex> lock(global_lock);
417     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
418     ValidateObject(device, device, kVulkanObjectTypeDevice, true, VALIDATION_ERROR_24a05601, VALIDATION_ERROR_UNDEFINED);
419     DestroyObject(device_data->instance, device, kVulkanObjectTypeDevice, pAllocator, VALIDATION_ERROR_24a002f6,
420                   VALIDATION_ERROR_24a002f8);
421
422     // Report any remaining objects associated with this VkDevice object in LL
423     ReportUndestroyedObjects(device, VALIDATION_ERROR_24a002f4);
424     DestroyUndestroyedObjects(device);
425
426     // Clean up Queue's MemRef Linked Lists
427     DestroyQueueDataStructures(device);
428
429     lock.unlock();
430
431     dispatch_key key = get_dispatch_key(device);
432     VkLayerDispatchTable *pDisp = get_dispatch_table(ot_device_table_map, device);
433     pDisp->DestroyDevice(device, pAllocator);
434     ot_device_table_map.erase(key);
435     delete pDisp;
436
437     FreeLayerDataPtr(key, layer_data_map);
438 }
439
440 VKAPI_ATTR void VKAPI_CALL GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue *pQueue) {
441     std::unique_lock<std::mutex> lock(global_lock);
442     ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_29605601, VALIDATION_ERROR_UNDEFINED);
443     lock.unlock();
444
445     get_dispatch_table(ot_device_table_map, device)->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
446
447     lock.lock();
448     CreateQueue(device, *pQueue);
449     AddQueueInfo(device, queueFamilyIndex, *pQueue);
450 }
451
452 VKAPI_ATTR void VKAPI_CALL GetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *pQueueInfo, VkQueue *pQueue) {
453     std::unique_lock<std::mutex> lock(global_lock);
454     ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_43405601, VALIDATION_ERROR_UNDEFINED);
455     lock.unlock();
456
457     get_dispatch_table(ot_device_table_map, device)->GetDeviceQueue2(device, pQueueInfo, pQueue);
458
459     lock.lock();
460     if (*pQueue != VK_NULL_HANDLE) {
461         CreateQueue(device, *pQueue);
462         AddQueueInfo(device, pQueueInfo->queueFamilyIndex, *pQueue);
463     }
464 }
465
466 VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
467                                                 const VkWriteDescriptorSet *pDescriptorWrites, uint32_t descriptorCopyCount,
468                                                 const VkCopyDescriptorSet *pDescriptorCopies) {
469     bool skip = false;
470     {
471         std::lock_guard<std::mutex> lock(global_lock);
472         skip |=
473             ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_33c05601, VALIDATION_ERROR_UNDEFINED);
474         if (pDescriptorCopies) {
475             for (uint32_t idx0 = 0; idx0 < descriptorCopyCount; ++idx0) {
476                 if (pDescriptorCopies[idx0].dstSet) {
477                     skip |= ValidateObject(device, pDescriptorCopies[idx0].dstSet, kVulkanObjectTypeDescriptorSet, false,
478                                            VALIDATION_ERROR_03207601, VALIDATION_ERROR_03200009);
479                 }
480                 if (pDescriptorCopies[idx0].srcSet) {
481                     skip |= ValidateObject(device, pDescriptorCopies[idx0].srcSet, kVulkanObjectTypeDescriptorSet, false,
482                                            VALIDATION_ERROR_0322d201, VALIDATION_ERROR_03200009);
483                 }
484             }
485         }
486         if (pDescriptorWrites) {
487             for (uint32_t idx1 = 0; idx1 < descriptorWriteCount; ++idx1) {
488                 skip |= ValidateDescriptorWrite(device, &pDescriptorWrites[idx1], false);
489             }
490         }
491     }
492     if (skip) {
493         return;
494     }
495     get_dispatch_table(ot_device_table_map, device)
496         ->UpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);
497 }
498
499 VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
500                                                       const VkComputePipelineCreateInfo *pCreateInfos,
501                                                       const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
502     bool skip = VK_FALSE;
503     std::unique_lock<std::mutex> lock(global_lock);
504     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_1f205601, VALIDATION_ERROR_UNDEFINED);
505     if (pCreateInfos) {
506         for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) {
507             if (pCreateInfos[idx0].basePipelineHandle) {
508                 skip |= ValidateObject(device, pCreateInfos[idx0].basePipelineHandle, kVulkanObjectTypePipeline, true,
509                                        VALIDATION_ERROR_03000572, VALIDATION_ERROR_03000009);
510             }
511             if (pCreateInfos[idx0].layout) {
512                 skip |= ValidateObject(device, pCreateInfos[idx0].layout, kVulkanObjectTypePipelineLayout, false,
513                                        VALIDATION_ERROR_0300be01, VALIDATION_ERROR_03000009);
514             }
515             if (pCreateInfos[idx0].stage.module) {
516                 skip |= ValidateObject(device, pCreateInfos[idx0].stage.module, kVulkanObjectTypeShaderModule, false,
517                                        VALIDATION_ERROR_1060d201, VALIDATION_ERROR_UNDEFINED);
518             }
519         }
520     }
521     if (pipelineCache) {
522         skip |= ValidateObject(device, pipelineCache, kVulkanObjectTypePipelineCache, true, VALIDATION_ERROR_1f228001,
523                                VALIDATION_ERROR_1f228007);
524     }
525     lock.unlock();
526     if (skip) {
527         for (uint32_t i = 0; i < createInfoCount; i++) {
528             pPipelines[i] = VK_NULL_HANDLE;
529         }
530         return VK_ERROR_VALIDATION_FAILED_EXT;
531     }
532     VkResult result = get_dispatch_table(ot_device_table_map, device)
533                           ->CreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator, pPipelines);
534     lock.lock();
535     for (uint32_t idx1 = 0; idx1 < createInfoCount; ++idx1) {
536         if (pPipelines[idx1] != VK_NULL_HANDLE) {
537             CreateObject(device, pPipelines[idx1], kVulkanObjectTypePipeline, pAllocator);
538         }
539     }
540     lock.unlock();
541     return result;
542 }
543
544 VKAPI_ATTR VkResult VKAPI_CALL ResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
545                                                    VkDescriptorPoolResetFlags flags) {
546     bool skip = false;
547     std::unique_lock<std::mutex> lock(global_lock);
548     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
549     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_32a05601, VALIDATION_ERROR_UNDEFINED);
550     skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_32a04601,
551                            VALIDATION_ERROR_32a04607);
552     if (skip) {
553         return VK_ERROR_VALIDATION_FAILED_EXT;
554     }
555     // A DescriptorPool's descriptor sets are implicitly deleted when the pool is reset.
556     // Remove this pool's descriptor sets from our descriptorSet map.
557     auto itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
558     while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
559         ObjTrackState *pNode = (*itr).second;
560         auto del_itr = itr++;
561         if (pNode->parent_object == HandleToUint64(descriptorPool)) {
562             DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
563                           VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
564         }
565     }
566     lock.unlock();
567     VkResult result = get_dispatch_table(ot_device_table_map, device)->ResetDescriptorPool(device, descriptorPool, flags);
568     return result;
569 }
570
571 VKAPI_ATTR VkResult VKAPI_CALL BeginCommandBuffer(VkCommandBuffer command_buffer, const VkCommandBufferBeginInfo *begin_info) {
572     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(command_buffer), layer_data_map);
573     bool skip = false;
574     {
575         std::lock_guard<std::mutex> lock(global_lock);
576         skip |= ValidateObject(command_buffer, command_buffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_16e02401,
577                                VALIDATION_ERROR_UNDEFINED);
578         if (begin_info) {
579             ObjTrackState *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
580             if ((begin_info->pInheritanceInfo) && (pNode->status & OBJSTATUS_COMMAND_BUFFER_SECONDARY) &&
581                 (begin_info->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
582                 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->framebuffer, kVulkanObjectTypeFramebuffer,
583                                        true, VALIDATION_ERROR_0280006e, VALIDATION_ERROR_02a00009);
584                 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->renderPass, kVulkanObjectTypeRenderPass, false,
585                                        VALIDATION_ERROR_0280006a, VALIDATION_ERROR_02a00009);
586             }
587         }
588     }
589     if (skip) {
590         return VK_ERROR_VALIDATION_FAILED_EXT;
591     }
592     VkResult result = get_dispatch_table(ot_device_table_map, command_buffer)->BeginCommandBuffer(command_buffer, begin_info);
593     return result;
594 }
595
596 VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(VkInstance instance,
597                                                             const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
598                                                             const VkAllocationCallbacks *pAllocator,
599                                                             VkDebugReportCallbackEXT *pCallback) {
600     VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
601     VkResult result = pInstanceTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);
602     if (VK_SUCCESS == result) {
603         layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
604         result = layer_create_report_callback(instance_data->report_data, false, pCreateInfo, pAllocator, pCallback);
605         CreateObject(instance, *pCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator);
606     }
607     return result;
608 }
609
610 VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback,
611                                                          const VkAllocationCallbacks *pAllocator) {
612     VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
613     pInstanceTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
614     layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
615     layer_destroy_report_callback(instance_data->report_data, msgCallback, pAllocator);
616     DestroyObject(instance, msgCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator, VALIDATION_ERROR_242009b4,
617                   VALIDATION_ERROR_242009b6);
618 }
619
620 VKAPI_ATTR void VKAPI_CALL DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
621                                                  VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
622                                                  int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
623     VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
624     pInstanceTable->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
625 }
626
627 // VK_EXT_debug_utils commands
628 VKAPI_ATTR VkResult VKAPI_CALL SetDebugUtilsObjectNameEXT(VkDevice device, const VkDebugUtilsObjectNameInfoEXT *pNameInfo) {
629     bool skip = VK_FALSE;
630     std::unique_lock<std::mutex> lock(global_lock);
631     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
632     lock.unlock();
633     if (skip) {
634         return VK_ERROR_VALIDATION_FAILED_EXT;
635     }
636     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
637     if (pNameInfo->pObjectName) {
638         dev_data->report_data->debugUtilsObjectNameMap->insert(
639             std::make_pair<uint64_t, std::string>((uint64_t &&) pNameInfo->objectHandle, pNameInfo->pObjectName));
640     } else {
641         dev_data->report_data->debugUtilsObjectNameMap->erase(pNameInfo->objectHandle);
642     }
643     VkResult result = dev_data->dispatch_table.SetDebugUtilsObjectNameEXT(device, pNameInfo);
644     return result;
645 }
646
647 VKAPI_ATTR VkResult VKAPI_CALL SetDebugUtilsObjectTagEXT(VkDevice device, const VkDebugUtilsObjectTagInfoEXT *pTagInfo) {
648     bool skip = VK_FALSE;
649     std::unique_lock<std::mutex> lock(global_lock);
650     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
651     lock.unlock();
652     if (skip) {
653         return VK_ERROR_VALIDATION_FAILED_EXT;
654     }
655     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
656     VkResult result = dev_data->dispatch_table.SetDebugUtilsObjectTagEXT(device, pTagInfo);
657     return result;
658 }
659
660 VKAPI_ATTR void VKAPI_CALL QueueBeginDebugUtilsLabelEXT(VkQueue queue, const VkDebugUtilsLabelEXT *pLabelInfo) {
661     bool skip = VK_FALSE;
662     std::unique_lock<std::mutex> lock(global_lock);
663     skip |= ValidateObject(queue, queue, kVulkanObjectTypeQueue, false, VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
664     lock.unlock();
665     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(queue), layer_data_map);
666     if (!skip) {
667         BeginQueueDebugUtilsLabel(dev_data->report_data, queue, pLabelInfo);
668         if (dev_data->dispatch_table.QueueBeginDebugUtilsLabelEXT) {
669             dev_data->dispatch_table.QueueBeginDebugUtilsLabelEXT(queue, pLabelInfo);
670         }
671     }
672 }
673
674 VKAPI_ATTR void VKAPI_CALL QueueEndDebugUtilsLabelEXT(VkQueue queue) {
675     bool skip = VK_FALSE;
676     std::unique_lock<std::mutex> lock(global_lock);
677     skip |= ValidateObject(queue, queue, kVulkanObjectTypeQueue, false, VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
678     lock.unlock();
679     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(queue), layer_data_map);
680     if (!skip) {
681         if (dev_data->dispatch_table.QueueEndDebugUtilsLabelEXT) {
682             dev_data->dispatch_table.QueueEndDebugUtilsLabelEXT(queue);
683         }
684         EndQueueDebugUtilsLabel(dev_data->report_data, queue);
685     }
686 }
687
688 VKAPI_ATTR void VKAPI_CALL QueueInsertDebugUtilsLabelEXT(VkQueue queue, const VkDebugUtilsLabelEXT *pLabelInfo) {
689     bool skip = VK_FALSE;
690     std::unique_lock<std::mutex> lock(global_lock);
691     skip |= ValidateObject(queue, queue, kVulkanObjectTypeQueue, false, VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
692     lock.unlock();
693     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(queue), layer_data_map);
694     if (!skip) {
695         InsertQueueDebugUtilsLabel(dev_data->report_data, queue, pLabelInfo);
696         if (dev_data->dispatch_table.QueueInsertDebugUtilsLabelEXT) {
697             dev_data->dispatch_table.QueueInsertDebugUtilsLabelEXT(queue, pLabelInfo);
698         }
699     }
700 }
701
702 VKAPI_ATTR void VKAPI_CALL CmdBeginDebugUtilsLabelEXT(VkCommandBuffer commandBuffer, const VkDebugUtilsLabelEXT *pLabelInfo) {
703     bool skip = VK_FALSE;
704     std::unique_lock<std::mutex> lock(global_lock);
705     skip |= ValidateObject(commandBuffer, commandBuffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_UNDEFINED,
706                            VALIDATION_ERROR_UNDEFINED);
707     lock.unlock();
708     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(commandBuffer), layer_data_map);
709     if (!skip) {
710         BeginCmdDebugUtilsLabel(dev_data->report_data, commandBuffer, pLabelInfo);
711         if (dev_data->dispatch_table.CmdBeginDebugUtilsLabelEXT) {
712             dev_data->dispatch_table.CmdBeginDebugUtilsLabelEXT(commandBuffer, pLabelInfo);
713         }
714     }
715 }
716
717 VKAPI_ATTR void VKAPI_CALL CmdEndDebugUtilsLabelEXT(VkCommandBuffer commandBuffer) {
718     bool skip = VK_FALSE;
719     std::unique_lock<std::mutex> lock(global_lock);
720     skip |= ValidateObject(commandBuffer, commandBuffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_UNDEFINED,
721                            VALIDATION_ERROR_UNDEFINED);
722     lock.unlock();
723     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(commandBuffer), layer_data_map);
724     if (!skip) {
725         if (dev_data->dispatch_table.CmdEndDebugUtilsLabelEXT) {
726             dev_data->dispatch_table.CmdEndDebugUtilsLabelEXT(commandBuffer);
727         }
728         EndCmdDebugUtilsLabel(dev_data->report_data, commandBuffer);
729     }
730 }
731
732 VKAPI_ATTR void VKAPI_CALL CmdInsertDebugUtilsLabelEXT(VkCommandBuffer commandBuffer, const VkDebugUtilsLabelEXT *pLabelInfo) {
733     bool skip = VK_FALSE;
734     std::unique_lock<std::mutex> lock(global_lock);
735     skip |= ValidateObject(commandBuffer, commandBuffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_UNDEFINED,
736                            VALIDATION_ERROR_UNDEFINED);
737     lock.unlock();
738     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(commandBuffer), layer_data_map);
739     if (!skip) {
740         InsertCmdDebugUtilsLabel(dev_data->report_data, commandBuffer, pLabelInfo);
741         if (dev_data->dispatch_table.CmdInsertDebugUtilsLabelEXT) {
742             dev_data->dispatch_table.CmdInsertDebugUtilsLabelEXT(commandBuffer, pLabelInfo);
743         }
744     }
745 }
746
747 VKAPI_ATTR VkResult VKAPI_CALL CreateDebugUtilsMessengerEXT(VkInstance instance,
748                                                             const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
749                                                             const VkAllocationCallbacks *pAllocator,
750                                                             VkDebugUtilsMessengerEXT *pMessenger) {
751     VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
752     VkResult result = pInstanceTable->CreateDebugUtilsMessengerEXT(instance, pCreateInfo, pAllocator, pMessenger);
753     if (VK_SUCCESS == result) {
754         layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
755         result = layer_create_messenger_callback(instance_data->report_data, false, pCreateInfo, pAllocator, pMessenger);
756         CreateObject(instance, *pMessenger, kVulkanObjectTypeDebugUtilsMessengerEXT, pAllocator);
757     }
758     return result;
759 }
760
761 VKAPI_ATTR void VKAPI_CALL DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger,
762                                                          const VkAllocationCallbacks *pAllocator) {
763     VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
764     pInstanceTable->DestroyDebugUtilsMessengerEXT(instance, messenger, pAllocator);
765     layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
766     layer_destroy_messenger_callback(instance_data->report_data, messenger, pAllocator);
767     DestroyObject(instance, messenger, kVulkanObjectTypeDebugUtilsMessengerEXT, pAllocator, VALIDATION_ERROR_UNDEFINED,
768                   VALIDATION_ERROR_UNDEFINED);
769 }
770
771 VKAPI_ATTR void VKAPI_CALL SubmitDebugUtilsMessageEXT(VkInstance instance, VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
772                                                       VkDebugUtilsMessageTypeFlagsEXT messageTypes,
773                                                       const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData) {
774     VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
775     pInstanceTable->SubmitDebugUtilsMessageEXT(instance, messageSeverity, messageTypes, pCallbackData);
776 }
777
778 static const VkExtensionProperties instance_extensions[] = {{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION},
779                                                             {VK_EXT_DEBUG_UTILS_EXTENSION_NAME, VK_EXT_DEBUG_UTILS_SPEC_VERSION}};
780
781 static const VkLayerProperties globalLayerProps = {"VK_LAYER_LUNARG_object_tracker",
782                                                    VK_LAYER_API_VERSION,  // specVersion
783                                                    1,                     // implementationVersion
784                                                    "LunarG Validation Layer"};
785
786 VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
787     return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
788 }
789
790 VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
791                                                               VkLayerProperties *pProperties) {
792     return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
793 }
794
795 VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
796                                                                     VkExtensionProperties *pProperties) {
797     if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
798         return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
799
800     return VK_ERROR_LAYER_NOT_PRESENT;
801 }
802
803 VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
804                                                                   uint32_t *pCount, VkExtensionProperties *pProperties) {
805     if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
806         return util_GetExtensionProperties(0, nullptr, pCount, pProperties);
807
808     assert(physicalDevice);
809     VkLayerInstanceDispatchTable *pTable = get_dispatch_table(ot_instance_table_map, physicalDevice);
810     return pTable->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
811 }
812
813 VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
814                                             const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
815     std::lock_guard<std::mutex> lock(global_lock);
816     bool skip = ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_1fc27a01,
817                                VALIDATION_ERROR_UNDEFINED);
818     if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
819
820     layer_data *phy_dev_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
821     VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
822
823     assert(chain_info->u.pLayerInfo);
824     PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
825     PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
826     PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(phy_dev_data->instance, "vkCreateDevice");
827     if (fpCreateDevice == NULL) {
828         return VK_ERROR_INITIALIZATION_FAILED;
829     }
830
831     // Advance the link info for the next element on the chain
832     chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
833
834     VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
835     if (result != VK_SUCCESS) {
836         return result;
837     }
838
839     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
840     device_data->report_data = layer_debug_utils_create_device(phy_dev_data->report_data, *pDevice);
841     layer_init_device_dispatch_table(*pDevice, &device_data->dispatch_table, fpGetDeviceProcAddr);
842
843     // Add link back to physDev
844     device_data->physical_device = physicalDevice;
845     device_data->instance = phy_dev_data->instance;
846
847     initDeviceTable(*pDevice, fpGetDeviceProcAddr, ot_device_table_map);
848
849     CreateObject(phy_dev_data->instance, *pDevice, kVulkanObjectTypeDevice, pAllocator);
850
851     return result;
852 }
853
854 VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount,
855                                                      VkImage *pSwapchainImages) {
856     bool skip = false;
857     std::unique_lock<std::mutex> lock(global_lock);
858     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_30805601, VALIDATION_ERROR_UNDEFINED);
859     skip |= ValidateObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, false, VALIDATION_ERROR_3082f001,
860                            VALIDATION_ERROR_UNDEFINED);
861     lock.unlock();
862     if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
863
864     VkResult result = get_dispatch_table(ot_device_table_map, device)
865                           ->GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
866     if (pSwapchainImages != NULL) {
867         lock.lock();
868         for (uint32_t i = 0; i < *pSwapchainImageCount; i++) {
869             CreateSwapchainImageObject(device, pSwapchainImages[i], swapchain);
870         }
871         lock.unlock();
872     }
873     return result;
874 }
875
876 VKAPI_ATTR VkResult VKAPI_CALL CreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
877                                                          const VkAllocationCallbacks *pAllocator,
878                                                          VkDescriptorSetLayout *pSetLayout) {
879     bool skip = false;
880     {
881         std::lock_guard<std::mutex> lock(global_lock);
882         skip |=
883             ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_1f805601, VALIDATION_ERROR_UNDEFINED);
884         if (pCreateInfo) {
885             if (pCreateInfo->pBindings) {
886                 for (uint32_t binding_index = 0; binding_index < pCreateInfo->bindingCount; ++binding_index) {
887                     const VkDescriptorSetLayoutBinding &binding = pCreateInfo->pBindings[binding_index];
888                     const bool is_sampler_type = binding.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
889                                                  binding.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
890                     if (binding.pImmutableSamplers && is_sampler_type) {
891                         for (uint32_t index2 = 0; index2 < binding.descriptorCount; ++index2) {
892                             const VkSampler sampler = binding.pImmutableSamplers[index2];
893                             skip |= ValidateObject(device, sampler, kVulkanObjectTypeSampler, false, VALIDATION_ERROR_04e00234,
894                                                    VALIDATION_ERROR_UNDEFINED);
895                         }
896                     }
897                 }
898             }
899         }
900     }
901     if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
902     VkResult result =
903         get_dispatch_table(ot_device_table_map, device)->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
904     if (VK_SUCCESS == result) {
905         std::lock_guard<std::mutex> lock(global_lock);
906         CreateObject(device, *pSetLayout, kVulkanObjectTypeDescriptorSetLayout, pAllocator);
907     }
908     return result;
909 }
910
911 VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
912                                                                   uint32_t *pQueueFamilyPropertyCount,
913                                                                   VkQueueFamilyProperties *pQueueFamilyProperties) {
914     bool skip = false;
915     {
916         std::lock_guard<std::mutex> lock(global_lock);
917         skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_2da27a01,
918                                VALIDATION_ERROR_UNDEFINED);
919     }
920     if (skip) {
921         return;
922     }
923     get_dispatch_table(ot_instance_table_map, physicalDevice)
924         ->GetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
925     std::lock_guard<std::mutex> lock(global_lock);
926     if (pQueueFamilyProperties != NULL) {
927         layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
928         if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
929             instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
930         }
931         for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
932             instance_data->queue_family_properties[i] = pQueueFamilyProperties[i];
933         }
934     }
935 }
936
937 VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
938                                               VkInstance *pInstance) {
939     VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
940
941     assert(chain_info->u.pLayerInfo);
942     PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
943     PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
944     if (fpCreateInstance == NULL) {
945         return VK_ERROR_INITIALIZATION_FAILED;
946     }
947
948     // Advance the link info for the next element on the chain
949     chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
950
951     VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
952     if (result != VK_SUCCESS) {
953         return result;
954     }
955
956     layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(*pInstance), layer_data_map);
957     instance_data->instance = *pInstance;
958     initInstanceTable(*pInstance, fpGetInstanceProcAddr, ot_instance_table_map);
959     VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, *pInstance);
960
961     // Look for one or more debug report create info structures, and copy the
962     // callback(s) for each one found (for use by vkDestroyInstance)
963     layer_copy_tmp_debug_messengers(pCreateInfo->pNext, &instance_data->num_tmp_debug_messengers,
964                                     &instance_data->tmp_messenger_create_infos, &instance_data->tmp_debug_messengers);
965     layer_copy_tmp_report_callbacks(pCreateInfo->pNext, &instance_data->num_tmp_report_callbacks,
966                                     &instance_data->tmp_report_create_infos, &instance_data->tmp_report_callbacks);
967
968     instance_data->report_data = debug_utils_create_instance(pInstanceTable, *pInstance, pCreateInfo->enabledExtensionCount,
969                                                              pCreateInfo->ppEnabledExtensionNames);
970
971     InitObjectTracker(instance_data, pAllocator);
972
973     CreateObject(*pInstance, *pInstance, kVulkanObjectTypeInstance, pAllocator);
974
975     return result;
976 }
977
978 VKAPI_ATTR VkResult VKAPI_CALL EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
979                                                         VkPhysicalDevice *pPhysicalDevices) {
980     bool skip = VK_FALSE;
981     std::unique_lock<std::mutex> lock(global_lock);
982     skip |=
983         ValidateObject(instance, instance, kVulkanObjectTypeInstance, false, VALIDATION_ERROR_2800bc01, VALIDATION_ERROR_UNDEFINED);
984     lock.unlock();
985     if (skip) {
986         return VK_ERROR_VALIDATION_FAILED_EXT;
987     }
988     VkResult result = get_dispatch_table(ot_instance_table_map, instance)
989                           ->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
990     lock.lock();
991     if (result == VK_SUCCESS) {
992         if (pPhysicalDevices) {
993             for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
994                 CreateObject(instance, pPhysicalDevices[i], kVulkanObjectTypePhysicalDevice, nullptr);
995             }
996         }
997     }
998     lock.unlock();
999     return result;
1000 }
1001
1002 VKAPI_ATTR VkResult VKAPI_CALL AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo,
1003                                                       VkCommandBuffer *pCommandBuffers) {
1004     bool skip = VK_FALSE;
1005     std::unique_lock<std::mutex> lock(global_lock);
1006     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16805601, VALIDATION_ERROR_UNDEFINED);
1007     skip |= ValidateObject(device, pAllocateInfo->commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_02602801,
1008                            VALIDATION_ERROR_UNDEFINED);
1009     lock.unlock();
1010
1011     if (skip) {
1012         return VK_ERROR_VALIDATION_FAILED_EXT;
1013     }
1014
1015     VkResult result =
1016         get_dispatch_table(ot_device_table_map, device)->AllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
1017
1018     lock.lock();
1019     for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) {
1020         AllocateCommandBuffer(device, pAllocateInfo->commandPool, pCommandBuffers[i], pAllocateInfo->level);
1021     }
1022     lock.unlock();
1023
1024     return result;
1025 }
1026
1027 VKAPI_ATTR VkResult VKAPI_CALL AllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo,
1028                                                       VkDescriptorSet *pDescriptorSets) {
1029     bool skip = VK_FALSE;
1030     std::unique_lock<std::mutex> lock(global_lock);
1031     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16a05601, VALIDATION_ERROR_UNDEFINED);
1032     skip |= ValidateObject(device, pAllocateInfo->descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_04c04601,
1033                            VALIDATION_ERROR_04c00009);
1034     for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
1035         skip |= ValidateObject(device, pAllocateInfo->pSetLayouts[i], kVulkanObjectTypeDescriptorSetLayout, false,
1036                                VALIDATION_ERROR_04c22c01, VALIDATION_ERROR_04c00009);
1037     }
1038     lock.unlock();
1039     if (skip) {
1040         return VK_ERROR_VALIDATION_FAILED_EXT;
1041     }
1042
1043     VkResult result =
1044         get_dispatch_table(ot_device_table_map, device)->AllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets);
1045
1046     if (VK_SUCCESS == result) {
1047         lock.lock();
1048         for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
1049             AllocateDescriptorSet(device, pAllocateInfo->descriptorPool, pDescriptorSets[i]);
1050         }
1051         lock.unlock();
1052     }
1053
1054     return result;
1055 }
1056
1057 VKAPI_ATTR void VKAPI_CALL FreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
1058                                               const VkCommandBuffer *pCommandBuffers) {
1059     bool skip = false;
1060     std::unique_lock<std::mutex> lock(global_lock);
1061     ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28405601, VALIDATION_ERROR_UNDEFINED);
1062     ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_28402801, VALIDATION_ERROR_28402807);
1063     for (uint32_t i = 0; i < commandBufferCount; i++) {
1064         if (pCommandBuffers[i] != VK_NULL_HANDLE) {
1065             skip |= ValidateCommandBuffer(device, commandPool, pCommandBuffers[i]);
1066         }
1067     }
1068
1069     for (uint32_t i = 0; i < commandBufferCount; i++) {
1070         DestroyObject(device, pCommandBuffers[i], kVulkanObjectTypeCommandBuffer, nullptr, VALIDATION_ERROR_UNDEFINED,
1071                       VALIDATION_ERROR_UNDEFINED);
1072     }
1073
1074     lock.unlock();
1075     if (!skip) {
1076         get_dispatch_table(ot_device_table_map, device)
1077             ->FreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
1078     }
1079 }
1080
1081 VKAPI_ATTR void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
1082     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
1083     std::unique_lock<std::mutex> lock(global_lock);
1084     // A swapchain's images are implicitly deleted when the swapchain is deleted.
1085     // Remove this swapchain's images from our map of such images.
1086     std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->swapchainImageMap.begin();
1087     while (itr != device_data->swapchainImageMap.end()) {
1088         ObjTrackState *pNode = (*itr).second;
1089         if (pNode->parent_object == HandleToUint64(swapchain)) {
1090             delete pNode;
1091             auto delete_item = itr++;
1092             device_data->swapchainImageMap.erase(delete_item);
1093         } else {
1094             ++itr;
1095         }
1096     }
1097     DestroyObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, pAllocator, VALIDATION_ERROR_26e00a06,
1098                   VALIDATION_ERROR_26e00a08);
1099     lock.unlock();
1100
1101     get_dispatch_table(ot_device_table_map, device)->DestroySwapchainKHR(device, swapchain, pAllocator);
1102 }
1103
1104 VKAPI_ATTR VkResult VKAPI_CALL FreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
1105                                                   const VkDescriptorSet *pDescriptorSets) {
1106     bool skip = false;
1107     VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
1108     std::unique_lock<std::mutex> lock(global_lock);
1109     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28605601, VALIDATION_ERROR_UNDEFINED);
1110     skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_28604601,
1111                            VALIDATION_ERROR_28604607);
1112     for (uint32_t i = 0; i < descriptorSetCount; i++) {
1113         if (pDescriptorSets[i] != VK_NULL_HANDLE) {
1114             skip |= ValidateDescriptorSet(device, descriptorPool, pDescriptorSets[i]);
1115         }
1116     }
1117
1118     for (uint32_t i = 0; i < descriptorSetCount; i++) {
1119         DestroyObject(device, pDescriptorSets[i], kVulkanObjectTypeDescriptorSet, nullptr, VALIDATION_ERROR_UNDEFINED,
1120                       VALIDATION_ERROR_UNDEFINED);
1121     }
1122
1123     lock.unlock();
1124     if (!skip) {
1125         result = get_dispatch_table(ot_device_table_map, device)
1126                      ->FreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets);
1127     }
1128     return result;
1129 }
1130
1131 VKAPI_ATTR void VKAPI_CALL DestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
1132                                                  const VkAllocationCallbacks *pAllocator) {
1133     bool skip = VK_FALSE;
1134     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
1135     std::unique_lock<std::mutex> lock(global_lock);
1136     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24405601, VALIDATION_ERROR_UNDEFINED);
1137     skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, true, VALIDATION_ERROR_24404601,
1138                            VALIDATION_ERROR_24404607);
1139     lock.unlock();
1140     if (skip) {
1141         return;
1142     }
1143     // A DescriptorPool's descriptor sets are implicitly deleted when the pool is deleted.
1144     // Remove this pool's descriptor sets from our descriptorSet map.
1145     lock.lock();
1146     std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
1147     while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
1148         ObjTrackState *pNode = (*itr).second;
1149         auto del_itr = itr++;
1150         if (pNode->parent_object == HandleToUint64(descriptorPool)) {
1151             DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
1152                           VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
1153         }
1154     }
1155     DestroyObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, pAllocator, VALIDATION_ERROR_24400260,
1156                   VALIDATION_ERROR_24400262);
1157     lock.unlock();
1158     get_dispatch_table(ot_device_table_map, device)->DestroyDescriptorPool(device, descriptorPool, pAllocator);
1159 }
1160
1161 VKAPI_ATTR void VKAPI_CALL DestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) {
1162     layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
1163     bool skip = false;
1164     std::unique_lock<std::mutex> lock(global_lock);
1165     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24005601, VALIDATION_ERROR_UNDEFINED);
1166     skip |= ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, true, VALIDATION_ERROR_24002801,
1167                            VALIDATION_ERROR_24002807);
1168     lock.unlock();
1169     if (skip) {
1170         return;
1171     }
1172     lock.lock();
1173     // A CommandPool's command buffers are implicitly deleted when the pool is deleted.
1174     // Remove this pool's cmdBuffers from our cmd buffer map.
1175     auto itr = device_data->object_map[kVulkanObjectTypeCommandBuffer].begin();
1176     auto del_itr = itr;
1177     while (itr != device_data->object_map[kVulkanObjectTypeCommandBuffer].end()) {
1178         ObjTrackState *pNode = (*itr).second;
1179         del_itr = itr++;
1180         if (pNode->parent_object == HandleToUint64(commandPool)) {
1181             skip |= ValidateCommandBuffer(device, commandPool, reinterpret_cast<VkCommandBuffer>((*del_itr).first));
1182             DestroyObject(device, reinterpret_cast<VkCommandBuffer>((*del_itr).first), kVulkanObjectTypeCommandBuffer, nullptr,
1183                           VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
1184         }
1185     }
1186     DestroyObject(device, commandPool, kVulkanObjectTypeCommandPool, pAllocator, VALIDATION_ERROR_24000054,
1187                   VALIDATION_ERROR_24000056);
1188     lock.unlock();
1189     get_dispatch_table(ot_device_table_map, device)->DestroyCommandPool(device, commandPool, pAllocator);
1190 }
1191
1192 // Note: This is the core version of this routine.  The extension version is below.
1193 VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
1194                                                                    uint32_t *pQueueFamilyPropertyCount,
1195                                                                    VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
1196     bool skip = false;
1197     {
1198         std::lock_guard<std::mutex> lock(global_lock);
1199         skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_UNDEFINED,
1200                                VALIDATION_ERROR_UNDEFINED);
1201     }
1202     if (skip) {
1203         return;
1204     }
1205     get_dispatch_table(ot_instance_table_map, physicalDevice)
1206         ->GetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1207     std::lock_guard<std::mutex> lock(global_lock);
1208     if (pQueueFamilyProperties != NULL) {
1209         layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
1210         if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
1211             instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
1212         }
1213         for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
1214             instance_data->queue_family_properties[i] = pQueueFamilyProperties[i].queueFamilyProperties;
1215         }
1216     }
1217 }
1218
1219 // Note: This is the extension version of this routine.  The core version is above.
1220 VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
1221                                                                       uint32_t *pQueueFamilyPropertyCount,
1222                                                                       VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
1223     bool skip = false;
1224     {
1225         std::lock_guard<std::mutex> lock(global_lock);
1226         skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_UNDEFINED,
1227                                VALIDATION_ERROR_UNDEFINED);
1228     }
1229     if (skip) {
1230         return;
1231     }
1232     get_dispatch_table(ot_instance_table_map, physicalDevice)
1233         ->GetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1234     std::lock_guard<std::mutex> lock(global_lock);
1235     if (pQueueFamilyProperties != NULL) {
1236         layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
1237         if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
1238             instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
1239         }
1240         for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
1241             instance_data->queue_family_properties[i] = pQueueFamilyProperties[i].queueFamilyProperties;
1242         }
1243     }
1244 }
1245
1246 VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceDisplayPropertiesKHR(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
1247                                                                      VkDisplayPropertiesKHR *pProperties) {
1248     bool skip = false;
1249     std::unique_lock<std::mutex> lock(global_lock);
1250     skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_2b827a01,
1251                            VALIDATION_ERROR_UNDEFINED);
1252     lock.unlock();
1253
1254     if (skip) {
1255         return VK_ERROR_VALIDATION_FAILED_EXT;
1256     }
1257     VkResult result = get_dispatch_table(ot_instance_table_map, physicalDevice)
1258                           ->GetPhysicalDeviceDisplayPropertiesKHR(physicalDevice, pPropertyCount, pProperties);
1259
1260     lock.lock();
1261     if (result == VK_SUCCESS) {
1262         if (pProperties) {
1263             for (uint32_t i = 0; i < *pPropertyCount; ++i) {
1264                 CreateObject(physicalDevice, pProperties[i].display, kVulkanObjectTypeDisplayKHR, nullptr);
1265             }
1266         }
1267     }
1268     lock.unlock();
1269
1270     return result;
1271 }
1272
1273 VKAPI_ATTR VkResult VKAPI_CALL GetDisplayModePropertiesKHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
1274                                                            uint32_t *pPropertyCount, VkDisplayModePropertiesKHR *pProperties) {
1275     bool skip = false;
1276     std::unique_lock<std::mutex> lock(global_lock);
1277     skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_29827a01,
1278                            VALIDATION_ERROR_UNDEFINED);
1279     skip |= ValidateObject(physicalDevice, display, kVulkanObjectTypeDisplayKHR, false, VALIDATION_ERROR_29806001,
1280                            VALIDATION_ERROR_UNDEFINED);
1281     lock.unlock();
1282
1283     if (skip) {
1284         return VK_ERROR_VALIDATION_FAILED_EXT;
1285     }
1286     VkResult result = get_dispatch_table(ot_instance_table_map, physicalDevice)
1287                           ->GetDisplayModePropertiesKHR(physicalDevice, display, pPropertyCount, pProperties);
1288
1289     lock.lock();
1290     if (result == VK_SUCCESS) {
1291         if (pProperties) {
1292             for (uint32_t i = 0; i < *pPropertyCount; ++i) {
1293                 CreateObject(physicalDevice, pProperties[i].displayMode, kVulkanObjectTypeDisplayModeKHR, nullptr);
1294             }
1295         }
1296     }
1297     lock.unlock();
1298
1299     return result;
1300 }
1301
1302 VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectNameEXT(VkDevice device, const VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
1303     bool skip = VK_FALSE;
1304     std::unique_lock<std::mutex> lock(global_lock);
1305     layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
1306     if (pNameInfo->pObjectName) {
1307         dev_data->report_data->debugObjectNameMap->insert(
1308             std::make_pair<uint64_t, std::string>((uint64_t &&) pNameInfo->object, pNameInfo->pObjectName));
1309     } else {
1310         dev_data->report_data->debugObjectNameMap->erase(pNameInfo->object);
1311     }
1312     skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_23605601, VALIDATION_ERROR_UNDEFINED);
1313     lock.unlock();
1314     if (skip) {
1315         return VK_ERROR_VALIDATION_FAILED_EXT;
1316     }
1317     VkResult result = dev_data->dispatch_table.DebugMarkerSetObjectNameEXT(device, pNameInfo);
1318     return result;
1319 }
1320
1321 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
1322     assert(instance);
1323
1324     if (get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr == NULL) {
1325         return NULL;
1326     }
1327     return get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr(instance, funcName);
1328 }
1329
1330 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
1331     const auto item = name_to_funcptr_map.find(funcName);
1332     if (item != name_to_funcptr_map.end()) {
1333         return reinterpret_cast<PFN_vkVoidFunction>(item->second);
1334     }
1335
1336     auto table = get_dispatch_table(ot_device_table_map, device);
1337     if (!table->GetDeviceProcAddr) return NULL;
1338     return table->GetDeviceProcAddr(device, funcName);
1339 }
1340
1341 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
1342     const auto item = name_to_funcptr_map.find(funcName);
1343     if (item != name_to_funcptr_map.end()) {
1344         return reinterpret_cast<PFN_vkVoidFunction>(item->second);
1345     }
1346
1347     auto table = get_dispatch_table(ot_instance_table_map, instance);
1348     if (!table->GetInstanceProcAddr) return nullptr;
1349     return table->GetInstanceProcAddr(instance, funcName);
1350 }
1351
1352 }  // namespace object_tracker
1353
1354 VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
1355                                                                                       VkExtensionProperties *pProperties) {
1356     return object_tracker::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
1357 }
1358
1359 VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
1360                                                                                   VkLayerProperties *pProperties) {
1361     return object_tracker::EnumerateInstanceLayerProperties(pCount, pProperties);
1362 }
1363
1364 VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
1365                                                                                 VkLayerProperties *pProperties) {
1366     // The layer command handles VK_NULL_HANDLE just fine internally
1367     assert(physicalDevice == VK_NULL_HANDLE);
1368     return object_tracker::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
1369 }
1370
1371 VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
1372     return object_tracker::GetDeviceProcAddr(dev, funcName);
1373 }
1374
1375 VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
1376     return object_tracker::GetInstanceProcAddr(instance, funcName);
1377 }
1378
1379 VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1380                                                                                     const char *pLayerName, uint32_t *pCount,
1381                                                                                     VkExtensionProperties *pProperties) {
1382     // The layer command handles VK_NULL_HANDLE just fine internally
1383     assert(physicalDevice == VK_NULL_HANDLE);
1384     return object_tracker::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
1385 }
1386
1387 VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1388                                                                                            const char *funcName) {
1389     return object_tracker::GetPhysicalDeviceProcAddr(instance, funcName);
1390 }
1391
1392 VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
1393     assert(pVersionStruct != NULL);
1394     assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
1395
1396     // Fill in the function pointers if our version is at least capable of having the structure contain them.
1397     if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
1398         pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
1399         pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
1400         pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
1401     }
1402
1403     if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1404         object_tracker::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
1405     } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1406         pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
1407     }
1408
1409     return VK_SUCCESS;
1410 }