blob: cb0f4c58806d0d162740f69f88f9f17d9bc5cc0f [file] [log] [blame]
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001/* 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
25namespace object_tracker {
26
27std::unordered_map<void *, layer_data *> layer_data_map;
28device_table_map ot_device_table_map;
29instance_table_map ot_instance_table_map;
30std::mutex global_lock;
31uint64_t object_track_index = 0;
32uint32_t loader_layer_if_version = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
33
34void InitObjectTracker(layer_data *my_data, const VkAllocationCallbacks *pAllocator) {
Mark Young6ba8abe2017-11-09 10:37:04 -070035 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");
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060037}
38
39// Add new queue to head of global queue list
40void 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()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -060044 ObjTrackQueueInfo *p_queue_info = new ObjTrackQueueInfo;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060045 if (p_queue_info != NULL) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -060046 memset(p_queue_info, 0, sizeof(ObjTrackQueueInfo));
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060047 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,
Mark Lobodzinski88529492018-04-01 10:38:15 -060052 HandleToUint64(queue), OBJTRACK_INTERNAL_ERROR,
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060053 "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
59void 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,
Mark Lobodzinski88529492018-04-01 10:38:15 -060076 queue->second->handle, OBJTRACK_NONE,
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060077 "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
85void 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()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -060089 ObjTrackQueueInfo *pQueueInfo = queue_item->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060090 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,
Mark Lobodzinski88529492018-04-01 10:38:15 -060095 HandleToUint64(queue), VALIDATION_ERROR_31600011,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -060096 "Attempting %s on a non-memory-management capable queue -- VK_QUEUE_SPARSE_BINDING_BIT not set.", function);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060097 }
98 }
99 }
100}
101
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700102// 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.
105bool 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,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600118 invalid_handle_code, "Invalid Device Object 0x%" PRIxLEAST64 ".", device_handle);
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700119}
120
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600121void 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,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600126 HandleToUint64(command_buffer), OBJTRACK_NONE, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600127 object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT", HandleToUint64(command_buffer));
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600128
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600129 ObjTrackState *pNewObjNode = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600130 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
143bool 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()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600149 ObjTrackState *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600150
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,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600153 object_handle, VALIDATION_ERROR_28411407,
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600154 "FreeCommandBuffers is attempting to free Command Buffer 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600155 " belonging to Command Pool 0x%" PRIxLEAST64 " from pool 0x%" PRIxLEAST64 ").",
156 HandleToUint64(command_buffer), pNode->parent_object, HandleToUint64(command_pool));
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600157 }
158 } else {
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600159 skip |= log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600160 object_handle, VALIDATION_ERROR_28400060, "Invalid %s Object 0x%" PRIxLEAST64 ".",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600161 object_string[kVulkanObjectTypeCommandBuffer], object_handle);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600162 }
163 return skip;
164}
165
166void 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,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600170 HandleToUint64(descriptor_set), OBJTRACK_NONE, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600171 object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT", HandleToUint64(descriptor_set));
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600172
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600173 ObjTrackState *pNewObjNode = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600174 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
183bool 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()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600189 ObjTrackState *pNode = dsItem->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600190
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,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600193 object_handle, VALIDATION_ERROR_28613007,
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600194 "FreeDescriptorSets is attempting to free descriptorSet 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600195 " belonging to Descriptor Pool 0x%" PRIxLEAST64 " from pool 0x%" PRIxLEAST64 ").",
196 HandleToUint64(descriptor_set), pNode->parent_object, HandleToUint64(descriptor_pool));
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600197 }
198 } else {
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600199 skip |= log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600200 object_handle, VALIDATION_ERROR_2860026c, "Invalid %s Object 0x%" PRIxLEAST64 ".",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600201 object_string[kVulkanObjectTypeDescriptorSet], object_handle);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600202 }
203 return skip;
204}
205
Dave Houltona9df0ce2018-02-07 10:51:23 -0700206template <typename DispObj>
Chris Forbes2c600e92017-10-20 11:13:20 -0700207static bool ValidateDescriptorWrite(DispObj disp, VkWriteDescriptorSet const *desc, bool isPush) {
208 bool skip = false;
209
210 if (!isPush && desc->dstSet) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700211 skip |= ValidateObject(disp, desc->dstSet, kVulkanObjectTypeDescriptorSet, false, VALIDATION_ERROR_15c00280,
212 VALIDATION_ERROR_15c00009);
Chris Forbes2c600e92017-10-20 11:13:20 -0700213 }
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) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700218 skip |= ValidateObject(disp, desc->pTexelBufferView[idx2], kVulkanObjectTypeBufferView, false,
219 VALIDATION_ERROR_15c00286, VALIDATION_ERROR_15c00009);
Chris Forbes2c600e92017-10-20 11:13:20 -0700220 }
221 }
222
223 if ((desc->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||
Dave Houltona9df0ce2018-02-07 10:51:23 -0700224 (desc->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ||
Chris Forbes2c600e92017-10-20 11:13:20 -0700225 (desc->descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
226 for (uint32_t idx3 = 0; idx3 < desc->descriptorCount; ++idx3) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700227 skip |= ValidateObject(disp, desc->pImageInfo[idx3].imageView, kVulkanObjectTypeImageView, false,
228 VALIDATION_ERROR_15c0028c, VALIDATION_ERROR_04600009);
Chris Forbes2c600e92017-10-20 11:13:20 -0700229 }
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) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700238 skip |= ValidateObject(disp, desc->pBufferInfo[idx4].buffer, kVulkanObjectTypeBuffer, false,
239 VALIDATION_ERROR_04401a01, VALIDATION_ERROR_UNDEFINED);
Chris Forbes2c600e92017-10-20 11:13:20 -0700240 }
241 }
242 }
243
244 return skip;
245}
246
Tony Barbour2fd0c2c2017-08-08 12:51:33 -0600247VKAPI_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) {
Chris Forbesa94b60b2017-10-20 11:28:02 -0700259 skip |= ValidateDescriptorWrite(commandBuffer, &pDescriptorWrites[index0], true);
Tony Barbour2fd0c2c2017-08-08 12:51:33 -0600260 }
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
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600268void 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,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600272 HandleToUint64(vkObj), OBJTRACK_NONE, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600273 object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT", HandleToUint64(vkObj));
274
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600275 ObjTrackState *p_obj_node = NULL;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600276 auto queue_item = device_data->object_map[kVulkanObjectTypeQueue].find(HandleToUint64(vkObj));
277 if (queue_item == device_data->object_map[kVulkanObjectTypeQueue].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600278 p_obj_node = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600279 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
290void 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,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600293 HandleToUint64(swapchain_image), OBJTRACK_NONE, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600294 object_track_index++, "SwapchainImage", HandleToUint64(swapchain_image));
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600295
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600296 ObjTrackState *pNewObjNode = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600297 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
304void 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);
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000306 for (const auto &item : device_data->object_map[object_type]) {
307 const ObjTrackState *object_info = item.second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600308 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, get_debug_report_enum[object_type], object_info->handle,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600309 error_code, "OBJ ERROR : For device 0x%" PRIxLEAST64 ", %s object 0x%" PRIxLEAST64 " has not been destroyed.",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600310 HandleToUint64(device), object_string[object_type], object_info->handle);
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000311 }
312}
313
314void 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);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600321 }
322}
323
324VKAPI_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:
Mark Young6ba8abe2017-11-09 10:37:04 -0700331 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);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600338 }
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();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600346 ObjTrackState *pNode = iit->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600347 VkPhysicalDevice physical_device = reinterpret_cast<VkPhysicalDevice>(pNode->handle);
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700348
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600349 DestroyObject(instance, physical_device, kVulkanObjectTypePhysicalDevice, nullptr, VALIDATION_ERROR_UNDEFINED,
350 VALIDATION_ERROR_UNDEFINED);
351 iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
352 }
353
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700354 // Destroy child devices
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600355 for (auto iit = instance_data->object_map[kVulkanObjectTypeDevice].begin();
356 iit != instance_data->object_map[kVulkanObjectTypeDevice].end();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600357 ObjTrackState *pNode = iit->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600358
359 VkDevice device = reinterpret_cast<VkDevice>(pNode->handle);
360 VkDebugReportObjectTypeEXT debug_object_type = get_debug_report_enum[pNode->object_type];
361
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600362 log_msg(instance_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, debug_object_type, pNode->handle, OBJTRACK_OBJECT_LEAK,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600363 "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.",
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600364 string_VkDebugReportObjectTypeEXT(debug_object_type), pNode->handle);
365
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700366 // Report any remaining objects in LL
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600367 ReportUndestroyedObjects(device, VALIDATION_ERROR_258004ea);
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000368 DestroyUndestroyedObjects(device);
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700369
370 DestroyObject(instance, device, kVulkanObjectTypeDevice, pAllocator, VALIDATION_ERROR_258004ec, VALIDATION_ERROR_258004ee);
371 iit = instance_data->object_map[kVulkanObjectTypeDevice].begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600372 }
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700373
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600374 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):
Mark Young6ba8abe2017-11-09 10:37:04 -0700380 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;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600387 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700388 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;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600391 }
392
393 // Clean up logging callback, if any
Mark Young6ba8abe2017-11-09 10:37:04 -0700394 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 }
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600399 while (instance_data->logging_callback.size() > 0) {
400 VkDebugReportCallbackEXT callback = instance_data->logging_callback.back();
Mark Young6ba8abe2017-11-09 10:37:04 -0700401 layer_destroy_report_callback(instance_data->report_data, callback, pAllocator);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600402 instance_data->logging_callback.pop_back();
403 }
404
Gabríel Arthúr Pétursson3de74ca2018-03-18 01:50:54 +0000405 DestroyObject(instance, instance, kVulkanObjectTypeInstance, pAllocator, VALIDATION_ERROR_258004ec, VALIDATION_ERROR_258004ee);
406
Mark Young6ba8abe2017-11-09 10:37:04 -0700407 layer_debug_utils_destroy_instance(instance_data->report_data);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600408 FreeLayerDataPtr(key, layer_data_map);
409
410 lock.unlock();
411 ot_instance_table_map.erase(key);
412 delete pInstanceTable;
413}
414
415VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
416 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700417 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600418 ValidateObject(device, device, kVulkanObjectTypeDevice, true, VALIDATION_ERROR_24a05601, VALIDATION_ERROR_UNDEFINED);
Dave Houltona9df0ce2018-02-07 10:51:23 -0700419 DestroyObject(device_data->instance, device, kVulkanObjectTypeDevice, pAllocator, VALIDATION_ERROR_24a002f6,
420 VALIDATION_ERROR_24a002f8);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600421
422 // Report any remaining objects associated with this VkDevice object in LL
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600423 ReportUndestroyedObjects(device, VALIDATION_ERROR_24a002f4);
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000424 DestroyUndestroyedObjects(device);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600425
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
Mark Lobodzinski439645a2017-07-19 15:18:15 -0600440VKAPI_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
Yiwei Zhang991d88d2018-02-14 14:39:46 -0800452VKAPI_ATTR void VKAPI_CALL GetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *pQueueInfo, VkQueue *pQueue) {
453 std::unique_lock<std::mutex> lock(global_lock);
Mike Schuchardt8da1bb52018-02-22 10:46:31 -0700454 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_43405601, VALIDATION_ERROR_UNDEFINED);
Yiwei Zhang991d88d2018-02-14 14:39:46 -0800455 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
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600466VKAPI_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) {
Chris Forbes2c600e92017-10-20 11:13:20 -0700488 skip |= ValidateDescriptorWrite(device, &pDescriptorWrites[idx1], false);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600489 }
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
Mark Lobodzinski2d26c5f2017-07-19 12:37:04 -0600499VKAPI_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
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600544VKAPI_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()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600559 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600560 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
571VKAPI_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) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600579 ObjTrackState *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600580 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
596VKAPI_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);
Mark Young6ba8abe2017-11-09 10:37:04 -0700604 result = layer_create_report_callback(instance_data->report_data, false, pCreateInfo, pAllocator, pCallback);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600605 CreateObject(instance, *pCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator);
606 }
607 return result;
608}
609
610VKAPI_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);
Mark Young6ba8abe2017-11-09 10:37:04 -0700615 layer_destroy_report_callback(instance_data->report_data, msgCallback, pAllocator);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600616 DestroyObject(instance, msgCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator, VALIDATION_ERROR_242009b4,
617 VALIDATION_ERROR_242009b6);
618}
619
620VKAPI_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
Mark Young6ba8abe2017-11-09 10:37:04 -0700627// VK_EXT_debug_utils commands
628VKAPI_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
647VKAPI_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
660VKAPI_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
674VKAPI_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
688VKAPI_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
702VKAPI_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
717VKAPI_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
732VKAPI_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
747VKAPI_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
761VKAPI_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
771VKAPI_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
778static 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}};
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600780
781static const VkLayerProperties globalLayerProps = {"VK_LAYER_LUNARG_object_tracker",
782 VK_LAYER_API_VERSION, // specVersion
783 1, // implementationVersion
784 "LunarG Validation Layer"};
785
786VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
787 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
788}
789
790VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
791 VkLayerProperties *pProperties) {
792 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
793}
794
795VKAPI_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
803VKAPI_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
813VKAPI_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);
Mark Young6ba8abe2017-11-09 10:37:04 -0700840 device_data->report_data = layer_debug_utils_create_device(phy_dev_data->report_data, *pDevice);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600841 layer_init_device_dispatch_table(*pDevice, &device_data->dispatch_table, fpGetDeviceProcAddr);
842
843 // Add link back to physDev
844 device_data->physical_device = physicalDevice;
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700845 device_data->instance = phy_dev_data->instance;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600846
847 initDeviceTable(*pDevice, fpGetDeviceProcAddr, ot_device_table_map);
848
Mark Lobodzinski9bd81192017-11-13 09:38:23 -0700849 CreateObject(phy_dev_data->instance, *pDevice, kVulkanObjectTypeDevice, pAllocator);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600850
851 return result;
852}
853
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600854VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount,
855 VkImage *pSwapchainImages) {
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600856 bool skip = false;
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600857 std::unique_lock<std::mutex> lock(global_lock);
858 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_30805601, VALIDATION_ERROR_UNDEFINED);
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600859 skip |= ValidateObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, false, VALIDATION_ERROR_3082f001,
860 VALIDATION_ERROR_UNDEFINED);
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600861 lock.unlock();
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600862 if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
863
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600864 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
Petr Kraus42f6f8d2017-12-17 17:37:33 +0100876VKAPI_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
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600911VKAPI_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
937VKAPI_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)
Mark Young6ba8abe2017-11-09 10:37:04 -0700963 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);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600967
Mark Young6ba8abe2017-11-09 10:37:04 -0700968 instance_data->report_data = debug_utils_create_instance(pInstanceTable, *pInstance, pCreateInfo->enabledExtensionCount,
969 pCreateInfo->ppEnabledExtensionNames);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600970
971 InitObjectTracker(instance_data, pAllocator);
972
973 CreateObject(*pInstance, *pInstance, kVulkanObjectTypeInstance, pAllocator);
974
975 return result;
976}
977
978VKAPI_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
1002VKAPI_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
1027VKAPI_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
1057VKAPI_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
1081VKAPI_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.
Mark Lobodzinskiefc64392017-07-18 13:15:47 -06001086 std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->swapchainImageMap.begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001087 while (itr != device_data->swapchainImageMap.end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -06001088 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001089 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
1104VKAPI_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
1131VKAPI_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();
Mark Lobodzinskiefc64392017-07-18 13:15:47 -06001146 std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001147 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -06001148 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001149 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
1161VKAPI_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()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -06001178 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001179 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
Mark Lobodzinski14ddc192017-10-25 16:57:04 -06001192// Note: This is the core version of this routine. The extension version is below.
1193VKAPI_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.
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001220VKAPI_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
Mark Lobodzinskidfe5e172017-07-19 13:03:22 -06001246VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectNameEXT(VkDevice device, const VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001247 bool skip = VK_FALSE;
1248 std::unique_lock<std::mutex> lock(global_lock);
1249 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
1250 if (pNameInfo->pObjectName) {
1251 dev_data->report_data->debugObjectNameMap->insert(
1252 std::make_pair<uint64_t, std::string>((uint64_t &&) pNameInfo->object, pNameInfo->pObjectName));
1253 } else {
1254 dev_data->report_data->debugObjectNameMap->erase(pNameInfo->object);
1255 }
1256 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_23605601, VALIDATION_ERROR_UNDEFINED);
1257 lock.unlock();
1258 if (skip) {
1259 return VK_ERROR_VALIDATION_FAILED_EXT;
1260 }
1261 VkResult result = dev_data->dispatch_table.DebugMarkerSetObjectNameEXT(device, pNameInfo);
1262 return result;
1263}
1264
1265VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
1266 assert(instance);
1267
1268 if (get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr == NULL) {
1269 return NULL;
1270 }
1271 return get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr(instance, funcName);
1272}
1273
1274VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
1275 const auto item = name_to_funcptr_map.find(funcName);
1276 if (item != name_to_funcptr_map.end()) {
1277 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
1278 }
1279
1280 auto table = get_dispatch_table(ot_device_table_map, device);
1281 if (!table->GetDeviceProcAddr) return NULL;
1282 return table->GetDeviceProcAddr(device, funcName);
1283}
1284
1285VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
1286 const auto item = name_to_funcptr_map.find(funcName);
1287 if (item != name_to_funcptr_map.end()) {
1288 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
1289 }
1290
1291 auto table = get_dispatch_table(ot_instance_table_map, instance);
1292 if (!table->GetInstanceProcAddr) return nullptr;
1293 return table->GetInstanceProcAddr(instance, funcName);
1294}
1295
1296} // namespace object_tracker
1297
1298VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
1299 VkExtensionProperties *pProperties) {
1300 return object_tracker::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
1301}
1302
1303VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
1304 VkLayerProperties *pProperties) {
1305 return object_tracker::EnumerateInstanceLayerProperties(pCount, pProperties);
1306}
1307
1308VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
1309 VkLayerProperties *pProperties) {
1310 // The layer command handles VK_NULL_HANDLE just fine internally
1311 assert(physicalDevice == VK_NULL_HANDLE);
1312 return object_tracker::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
1313}
1314
1315VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
1316 return object_tracker::GetDeviceProcAddr(dev, funcName);
1317}
1318
1319VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
1320 return object_tracker::GetInstanceProcAddr(instance, funcName);
1321}
1322
1323VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1324 const char *pLayerName, uint32_t *pCount,
1325 VkExtensionProperties *pProperties) {
1326 // The layer command handles VK_NULL_HANDLE just fine internally
1327 assert(physicalDevice == VK_NULL_HANDLE);
1328 return object_tracker::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
1329}
1330
1331VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1332 const char *funcName) {
1333 return object_tracker::GetPhysicalDeviceProcAddr(instance, funcName);
1334}
1335
1336VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
1337 assert(pVersionStruct != NULL);
1338 assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
1339
1340 // Fill in the function pointers if our version is at least capable of having the structure contain them.
1341 if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
1342 pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
1343 pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
1344 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
1345 }
1346
1347 if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1348 object_tracker::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
1349 } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1350 pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
1351 }
1352
1353 return VK_SUCCESS;
1354}