blob: c9b2700bb7a5af17dea8e421407ed3e818a04dd9 [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) {
35 layer_debug_actions(my_data->report_data, my_data->logging_callback, pAllocator, "lunarg_object_tracker");
36}
37
38// Add new queue to head of global queue list
39void AddQueueInfo(VkDevice device, uint32_t queue_node_index, VkQueue queue) {
40 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
41 auto queueItem = device_data->queue_info_map.find(queue);
42 if (queueItem == device_data->queue_info_map.end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -060043 ObjTrackQueueInfo *p_queue_info = new ObjTrackQueueInfo;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060044 if (p_queue_info != NULL) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -060045 memset(p_queue_info, 0, sizeof(ObjTrackQueueInfo));
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060046 p_queue_info->queue = queue;
47 p_queue_info->queue_node_index = queue_node_index;
48 device_data->queue_info_map[queue] = p_queue_info;
49 } else {
50 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
51 HandleToUint64(queue), __LINE__, OBJTRACK_INTERNAL_ERROR, LayerName,
52 "ERROR: VK_ERROR_OUT_OF_HOST_MEMORY -- could not allocate memory for Queue Information");
53 }
54 }
55}
56
57// Destroy memRef lists and free all memory
58void DestroyQueueDataStructures(VkDevice device) {
59 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
60
61 for (auto queue_item : device_data->queue_info_map) {
62 delete queue_item.second;
63 }
64 device_data->queue_info_map.clear();
65
66 // Destroy the items in the queue map
67 auto queue = device_data->object_map[kVulkanObjectTypeQueue].begin();
68 while (queue != device_data->object_map[kVulkanObjectTypeQueue].end()) {
69 uint32_t obj_index = queue->second->object_type;
70 assert(device_data->num_total_objects > 0);
71 device_data->num_total_objects--;
72 assert(device_data->num_objects[obj_index] > 0);
73 device_data->num_objects[obj_index]--;
74 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
75 queue->second->handle, __LINE__, OBJTRACK_NONE, LayerName,
76 "OBJ_STAT Destroy Queue obj 0x%" PRIxLEAST64 " (%" PRIu64 " total objs remain & %" PRIu64 " Queue objs).",
77 queue->second->handle, device_data->num_total_objects, device_data->num_objects[obj_index]);
78 delete queue->second;
79 queue = device_data->object_map[kVulkanObjectTypeQueue].erase(queue);
80 }
81}
82
83// Check Queue type flags for selected queue operations
84void ValidateQueueFlags(VkQueue queue, const char *function) {
85 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(queue), layer_data_map);
86 auto queue_item = device_data->queue_info_map.find(queue);
87 if (queue_item != device_data->queue_info_map.end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -060088 ObjTrackQueueInfo *pQueueInfo = queue_item->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -060089 if (pQueueInfo != NULL) {
90 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(device_data->physical_device), layer_data_map);
91 if ((instance_data->queue_family_properties[pQueueInfo->queue_node_index].queueFlags & VK_QUEUE_SPARSE_BINDING_BIT) ==
92 0) {
93 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
94 HandleToUint64(queue), __LINE__, VALIDATION_ERROR_31600011, LayerName,
95 "Attempting %s on a non-memory-management capable queue -- VK_QUEUE_SPARSE_BINDING_BIT not set. %s",
96 function, validation_error_map[VALIDATION_ERROR_31600011]);
97 }
98 }
99 }
100}
101
102void AllocateCommandBuffer(VkDevice device, const VkCommandPool command_pool, const VkCommandBuffer command_buffer,
103 VkCommandBufferLevel level) {
104 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
105
106 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
107 HandleToUint64(command_buffer), __LINE__, OBJTRACK_NONE, LayerName,
108 "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64, object_track_index++,
109 "VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT", HandleToUint64(command_buffer));
110
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600111 ObjTrackState *pNewObjNode = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600112 pNewObjNode->object_type = kVulkanObjectTypeCommandBuffer;
113 pNewObjNode->handle = HandleToUint64(command_buffer);
114 pNewObjNode->parent_object = HandleToUint64(command_pool);
115 if (level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
116 pNewObjNode->status = OBJSTATUS_COMMAND_BUFFER_SECONDARY;
117 } else {
118 pNewObjNode->status = OBJSTATUS_NONE;
119 }
120 device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)] = pNewObjNode;
121 device_data->num_objects[kVulkanObjectTypeCommandBuffer]++;
122 device_data->num_total_objects++;
123}
124
125bool ValidateCommandBuffer(VkDevice device, VkCommandPool command_pool, VkCommandBuffer command_buffer) {
126 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
127 bool skip = false;
128 uint64_t object_handle = HandleToUint64(command_buffer);
129 if (device_data->object_map[kVulkanObjectTypeCommandBuffer].find(object_handle) !=
130 device_data->object_map[kVulkanObjectTypeCommandBuffer].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600131 ObjTrackState *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600132
133 if (pNode->parent_object != HandleToUint64(command_pool)) {
134 skip |= log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
135 object_handle, __LINE__, VALIDATION_ERROR_28411407, LayerName,
136 "FreeCommandBuffers is attempting to free Command Buffer 0x%" PRIxLEAST64
137 " belonging to Command Pool 0x%" PRIxLEAST64 " from pool 0x%" PRIxLEAST64 "). %s",
138 HandleToUint64(command_buffer), pNode->parent_object, HandleToUint64(command_pool),
139 validation_error_map[VALIDATION_ERROR_28411407]);
140 }
141 } else {
142 skip |=
143 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
144 object_handle, __LINE__, VALIDATION_ERROR_28400060, LayerName, "Invalid %s Object 0x%" PRIxLEAST64 ". %s",
145 object_string[kVulkanObjectTypeCommandBuffer], object_handle, validation_error_map[VALIDATION_ERROR_28400060]);
146 }
147 return skip;
148}
149
150void AllocateDescriptorSet(VkDevice device, VkDescriptorPool descriptor_pool, VkDescriptorSet descriptor_set) {
151 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
152
153 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
154 HandleToUint64(descriptor_set), __LINE__, OBJTRACK_NONE, LayerName,
155 "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64, object_track_index++,
156 "VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT", HandleToUint64(descriptor_set));
157
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600158 ObjTrackState *pNewObjNode = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600159 pNewObjNode->object_type = kVulkanObjectTypeDescriptorSet;
160 pNewObjNode->status = OBJSTATUS_NONE;
161 pNewObjNode->handle = HandleToUint64(descriptor_set);
162 pNewObjNode->parent_object = HandleToUint64(descriptor_pool);
163 device_data->object_map[kVulkanObjectTypeDescriptorSet][HandleToUint64(descriptor_set)] = pNewObjNode;
164 device_data->num_objects[kVulkanObjectTypeDescriptorSet]++;
165 device_data->num_total_objects++;
166}
167
168bool ValidateDescriptorSet(VkDevice device, VkDescriptorPool descriptor_pool, VkDescriptorSet descriptor_set) {
169 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
170 bool skip = false;
171 uint64_t object_handle = HandleToUint64(descriptor_set);
172 auto dsItem = device_data->object_map[kVulkanObjectTypeDescriptorSet].find(object_handle);
173 if (dsItem != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600174 ObjTrackState *pNode = dsItem->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600175
176 if (pNode->parent_object != HandleToUint64(descriptor_pool)) {
177 skip |= log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
178 object_handle, __LINE__, VALIDATION_ERROR_28613007, LayerName,
179 "FreeDescriptorSets is attempting to free descriptorSet 0x%" PRIxLEAST64
180 " belonging to Descriptor Pool 0x%" PRIxLEAST64 " from pool 0x%" PRIxLEAST64 "). %s",
181 HandleToUint64(descriptor_set), pNode->parent_object, HandleToUint64(descriptor_pool),
182 validation_error_map[VALIDATION_ERROR_28613007]);
183 }
184 } else {
185 skip |=
186 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
187 object_handle, __LINE__, VALIDATION_ERROR_2860026c, LayerName, "Invalid %s Object 0x%" PRIxLEAST64 ". %s",
188 object_string[kVulkanObjectTypeDescriptorSet], object_handle, validation_error_map[VALIDATION_ERROR_2860026c]);
189 }
190 return skip;
191}
192
Tony Barbour2fd0c2c2017-08-08 12:51:33 -0600193VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSetKHR(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
194 VkPipelineLayout layout, uint32_t set, uint32_t descriptorWriteCount,
195 const VkWriteDescriptorSet *pDescriptorWrites) {
196 bool skip = false;
197 {
198 std::lock_guard<std::mutex> lock(global_lock);
199 skip |= ValidateObject(commandBuffer, commandBuffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_1be02401,
200 VALIDATION_ERROR_1be00009);
201 skip |= ValidateObject(commandBuffer, layout, kVulkanObjectTypePipelineLayout, false, VALIDATION_ERROR_1be0be01,
202 VALIDATION_ERROR_1be00009);
203 if (pDescriptorWrites) {
204 for (uint32_t index0 = 0; index0 < descriptorWriteCount; ++index0) {
205 if (pDescriptorWrites[index0].pImageInfo) {
206 for (uint32_t index1 = 0; index1 < pDescriptorWrites[index0].descriptorCount; ++index1) {
207 skip |=
208 ValidateObject(commandBuffer, pDescriptorWrites[index0].pImageInfo[index1].sampler,
209 kVulkanObjectTypeSampler, false, VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_04600009);
210 skip |= ValidateObject(commandBuffer, pDescriptorWrites[index0].pImageInfo[index1].imageView,
211 kVulkanObjectTypeImageView, false, VALIDATION_ERROR_UNDEFINED,
212 VALIDATION_ERROR_04600009);
213 }
214 }
215 if (pDescriptorWrites[index0].pBufferInfo) {
216 for (uint32_t index1 = 0; index1 < pDescriptorWrites[index0].descriptorCount; ++index1) {
217 skip |=
218 ValidateObject(commandBuffer, pDescriptorWrites[index0].pBufferInfo[index1].buffer,
219 kVulkanObjectTypeBuffer, false, VALIDATION_ERROR_04401a01, VALIDATION_ERROR_UNDEFINED);
220 }
221 }
222 if (pDescriptorWrites[index0].pTexelBufferView) {
223 for (uint32_t index1 = 0; index1 < pDescriptorWrites[index0].descriptorCount; ++index1) {
224 skip |= ValidateObject(commandBuffer, pDescriptorWrites[index0].pTexelBufferView[index1],
225 kVulkanObjectTypeBufferView, false, VALIDATION_ERROR_UNDEFINED,
226 VALIDATION_ERROR_15c00009);
227 }
228 }
229 }
230 }
231 }
232 if (skip) return;
233 get_dispatch_table(ot_device_table_map, commandBuffer)
234 ->CmdPushDescriptorSetKHR(commandBuffer, pipelineBindPoint, layout, set, descriptorWriteCount, pDescriptorWrites);
235}
236
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600237void CreateQueue(VkDevice device, VkQueue vkObj) {
238 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
239
240 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
241 HandleToUint64(vkObj), __LINE__, OBJTRACK_NONE, LayerName, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
242 object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT", HandleToUint64(vkObj));
243
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600244 ObjTrackState *p_obj_node = NULL;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600245 auto queue_item = device_data->object_map[kVulkanObjectTypeQueue].find(HandleToUint64(vkObj));
246 if (queue_item == device_data->object_map[kVulkanObjectTypeQueue].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600247 p_obj_node = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600248 device_data->object_map[kVulkanObjectTypeQueue][HandleToUint64(vkObj)] = p_obj_node;
249 device_data->num_objects[kVulkanObjectTypeQueue]++;
250 device_data->num_total_objects++;
251 } else {
252 p_obj_node = queue_item->second;
253 }
254 p_obj_node->object_type = kVulkanObjectTypeQueue;
255 p_obj_node->status = OBJSTATUS_NONE;
256 p_obj_node->handle = HandleToUint64(vkObj);
257}
258
259void CreateSwapchainImageObject(VkDevice dispatchable_object, VkImage swapchain_image, VkSwapchainKHR swapchain) {
260 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(dispatchable_object), layer_data_map);
261 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
262 HandleToUint64(swapchain_image), __LINE__, OBJTRACK_NONE, LayerName,
263 "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64, object_track_index++, "SwapchainImage",
264 HandleToUint64(swapchain_image));
265
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600266 ObjTrackState *pNewObjNode = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600267 pNewObjNode->object_type = kVulkanObjectTypeImage;
268 pNewObjNode->status = OBJSTATUS_NONE;
269 pNewObjNode->handle = HandleToUint64(swapchain_image);
270 pNewObjNode->parent_object = HandleToUint64(swapchain);
271 device_data->swapchainImageMap[HandleToUint64(swapchain_image)] = pNewObjNode;
272}
273
274void DeviceReportUndestroyedObjects(VkDevice device, VulkanObjectType object_type, enum UNIQUE_VALIDATION_ERROR_CODE error_code) {
275 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
276 for (auto item = device_data->object_map[object_type].begin(); item != device_data->object_map[object_type].end();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600277 ObjTrackState *object_info = item->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600278 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, get_debug_report_enum[object_type], object_info->handle,
279 __LINE__, error_code, LayerName,
280 "OBJ ERROR : For device 0x%" PRIxLEAST64 ", %s object 0x%" PRIxLEAST64 " has not been destroyed. %s",
281 HandleToUint64(device), object_string[object_type], object_info->handle, validation_error_map[error_code]);
282 item = device_data->object_map[object_type].erase(item);
283 }
284}
285
286VKAPI_ATTR void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
287 std::unique_lock<std::mutex> lock(global_lock);
288
289 dispatch_key key = get_dispatch_key(instance);
290 layer_data *instance_data = GetLayerDataPtr(key, layer_data_map);
291
292 // Enable the temporary callback(s) here to catch cleanup issues:
293 bool callback_setup = false;
294 if (instance_data->num_tmp_callbacks > 0) {
295 if (!layer_enable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks,
296 instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks)) {
297 callback_setup = true;
298 }
299 }
300
301 // TODO: The instance handle can not be validated here. The loader will likely have to validate it.
302 ValidateObject(instance, instance, kVulkanObjectTypeInstance, true, VALIDATION_ERROR_2580bc01, VALIDATION_ERROR_UNDEFINED);
303
304 // Destroy physical devices
305 for (auto iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
306 iit != instance_data->object_map[kVulkanObjectTypePhysicalDevice].end();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600307 ObjTrackState *pNode = iit->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600308
309 VkPhysicalDevice physical_device = reinterpret_cast<VkPhysicalDevice>(pNode->handle);
310 DestroyObject(instance, physical_device, kVulkanObjectTypePhysicalDevice, nullptr, VALIDATION_ERROR_UNDEFINED,
311 VALIDATION_ERROR_UNDEFINED);
312 iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
313 }
314
315 DestroyObject(instance, instance, kVulkanObjectTypeInstance, pAllocator, VALIDATION_ERROR_258004ec, VALIDATION_ERROR_258004ee);
316 // Report any remaining objects in LL
317
318 for (auto iit = instance_data->object_map[kVulkanObjectTypeDevice].begin();
319 iit != instance_data->object_map[kVulkanObjectTypeDevice].end();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600320 ObjTrackState *pNode = iit->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600321
322 VkDevice device = reinterpret_cast<VkDevice>(pNode->handle);
323 VkDebugReportObjectTypeEXT debug_object_type = get_debug_report_enum[pNode->object_type];
324
325 log_msg(instance_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, debug_object_type, pNode->handle, __LINE__,
326 OBJTRACK_OBJECT_LEAK, LayerName, "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.",
327 string_VkDebugReportObjectTypeEXT(debug_object_type), pNode->handle);
328
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600329 ReportUndestroyedObjects(device, VALIDATION_ERROR_258004ea);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600330 }
331 instance_data->object_map[kVulkanObjectTypeDevice].clear();
332
333 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
334 pInstanceTable->DestroyInstance(instance, pAllocator);
335
336 // Disable and cleanup the temporary callback(s):
337 if (callback_setup) {
338 layer_disable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks, instance_data->tmp_callbacks);
339 }
340 if (instance_data->num_tmp_callbacks > 0) {
341 layer_free_tmp_callbacks(instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks);
342 instance_data->num_tmp_callbacks = 0;
343 }
344
345 // Clean up logging callback, if any
346 while (instance_data->logging_callback.size() > 0) {
347 VkDebugReportCallbackEXT callback = instance_data->logging_callback.back();
348 layer_destroy_msg_callback(instance_data->report_data, callback, pAllocator);
349 instance_data->logging_callback.pop_back();
350 }
351
352 layer_debug_report_destroy_instance(instance_data->report_data);
353 FreeLayerDataPtr(key, layer_data_map);
354
355 lock.unlock();
356 ot_instance_table_map.erase(key);
357 delete pInstanceTable;
358}
359
360VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
361 std::unique_lock<std::mutex> lock(global_lock);
362 ValidateObject(device, device, kVulkanObjectTypeDevice, true, VALIDATION_ERROR_24a05601, VALIDATION_ERROR_UNDEFINED);
363 DestroyObject(device, device, kVulkanObjectTypeDevice, pAllocator, VALIDATION_ERROR_24a002f6, VALIDATION_ERROR_24a002f8);
364
365 // Report any remaining objects associated with this VkDevice object in LL
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600366 ReportUndestroyedObjects(device, VALIDATION_ERROR_24a002f4);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600367
368 // Clean up Queue's MemRef Linked Lists
369 DestroyQueueDataStructures(device);
370
371 lock.unlock();
372
373 dispatch_key key = get_dispatch_key(device);
374 VkLayerDispatchTable *pDisp = get_dispatch_table(ot_device_table_map, device);
375 pDisp->DestroyDevice(device, pAllocator);
376 ot_device_table_map.erase(key);
377 delete pDisp;
378
379 FreeLayerDataPtr(key, layer_data_map);
380}
381
Mark Lobodzinski439645a2017-07-19 15:18:15 -0600382VKAPI_ATTR void VKAPI_CALL GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue *pQueue) {
383 std::unique_lock<std::mutex> lock(global_lock);
384 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_29605601, VALIDATION_ERROR_UNDEFINED);
385 lock.unlock();
386
387 get_dispatch_table(ot_device_table_map, device)->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
388
389 lock.lock();
390 CreateQueue(device, *pQueue);
391 AddQueueInfo(device, queueFamilyIndex, *pQueue);
392}
393
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600394VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
395 const VkWriteDescriptorSet *pDescriptorWrites, uint32_t descriptorCopyCount,
396 const VkCopyDescriptorSet *pDescriptorCopies) {
397 bool skip = false;
398 {
399 std::lock_guard<std::mutex> lock(global_lock);
400 skip |=
401 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_33c05601, VALIDATION_ERROR_UNDEFINED);
402 if (pDescriptorCopies) {
403 for (uint32_t idx0 = 0; idx0 < descriptorCopyCount; ++idx0) {
404 if (pDescriptorCopies[idx0].dstSet) {
405 skip |= ValidateObject(device, pDescriptorCopies[idx0].dstSet, kVulkanObjectTypeDescriptorSet, false,
406 VALIDATION_ERROR_03207601, VALIDATION_ERROR_03200009);
407 }
408 if (pDescriptorCopies[idx0].srcSet) {
409 skip |= ValidateObject(device, pDescriptorCopies[idx0].srcSet, kVulkanObjectTypeDescriptorSet, false,
410 VALIDATION_ERROR_0322d201, VALIDATION_ERROR_03200009);
411 }
412 }
413 }
414 if (pDescriptorWrites) {
415 for (uint32_t idx1 = 0; idx1 < descriptorWriteCount; ++idx1) {
416 if (pDescriptorWrites[idx1].dstSet) {
417 skip |= ValidateObject(device, pDescriptorWrites[idx1].dstSet, kVulkanObjectTypeDescriptorSet, false,
418 VALIDATION_ERROR_15c00280, VALIDATION_ERROR_15c00009);
419 }
420 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||
421 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) {
422 for (uint32_t idx2 = 0; idx2 < pDescriptorWrites[idx1].descriptorCount; ++idx2) {
423 skip |= ValidateObject(device, pDescriptorWrites[idx1].pTexelBufferView[idx2], kVulkanObjectTypeBufferView,
424 false, VALIDATION_ERROR_15c00286, VALIDATION_ERROR_15c00009);
425 }
426 }
427 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||
428 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) ||
429 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ||
430 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
431 for (uint32_t idx3 = 0; idx3 < pDescriptorWrites[idx1].descriptorCount; ++idx3) {
432 skip |=
433 ValidateObject(device, pDescriptorWrites[idx1].pImageInfo[idx3].imageView, kVulkanObjectTypeImageView,
434 false, VALIDATION_ERROR_15c0028c, VALIDATION_ERROR_04600009);
435 }
436 }
437 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
438 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||
439 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
440 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
441 for (uint32_t idx4 = 0; idx4 < pDescriptorWrites[idx1].descriptorCount; ++idx4) {
442 if (pDescriptorWrites[idx1].pBufferInfo[idx4].buffer) {
443 skip |=
444 ValidateObject(device, pDescriptorWrites[idx1].pBufferInfo[idx4].buffer, kVulkanObjectTypeBuffer,
445 false, VALIDATION_ERROR_04401a01, VALIDATION_ERROR_UNDEFINED);
446 }
447 }
448 }
449 }
450 }
451 }
452 if (skip) {
453 return;
454 }
455 get_dispatch_table(ot_device_table_map, device)
456 ->UpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);
457}
458
Mark Lobodzinski2d26c5f2017-07-19 12:37:04 -0600459VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
460 const VkComputePipelineCreateInfo *pCreateInfos,
461 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
462 bool skip = VK_FALSE;
463 std::unique_lock<std::mutex> lock(global_lock);
464 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_1f205601, VALIDATION_ERROR_UNDEFINED);
465 if (pCreateInfos) {
466 for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) {
467 if (pCreateInfos[idx0].basePipelineHandle) {
468 skip |= ValidateObject(device, pCreateInfos[idx0].basePipelineHandle, kVulkanObjectTypePipeline, true,
469 VALIDATION_ERROR_03000572, VALIDATION_ERROR_03000009);
470 }
471 if (pCreateInfos[idx0].layout) {
472 skip |= ValidateObject(device, pCreateInfos[idx0].layout, kVulkanObjectTypePipelineLayout, false,
473 VALIDATION_ERROR_0300be01, VALIDATION_ERROR_03000009);
474 }
475 if (pCreateInfos[idx0].stage.module) {
476 skip |= ValidateObject(device, pCreateInfos[idx0].stage.module, kVulkanObjectTypeShaderModule, false,
477 VALIDATION_ERROR_1060d201, VALIDATION_ERROR_UNDEFINED);
478 }
479 }
480 }
481 if (pipelineCache) {
482 skip |= ValidateObject(device, pipelineCache, kVulkanObjectTypePipelineCache, true, VALIDATION_ERROR_1f228001,
483 VALIDATION_ERROR_1f228007);
484 }
485 lock.unlock();
486 if (skip) {
487 for (uint32_t i = 0; i < createInfoCount; i++) {
488 pPipelines[i] = VK_NULL_HANDLE;
489 }
490 return VK_ERROR_VALIDATION_FAILED_EXT;
491 }
492 VkResult result = get_dispatch_table(ot_device_table_map, device)
493 ->CreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator, pPipelines);
494 lock.lock();
495 for (uint32_t idx1 = 0; idx1 < createInfoCount; ++idx1) {
496 if (pPipelines[idx1] != VK_NULL_HANDLE) {
497 CreateObject(device, pPipelines[idx1], kVulkanObjectTypePipeline, pAllocator);
498 }
499 }
500 lock.unlock();
501 return result;
502}
503
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600504VKAPI_ATTR VkResult VKAPI_CALL ResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
505 VkDescriptorPoolResetFlags flags) {
506 bool skip = false;
507 std::unique_lock<std::mutex> lock(global_lock);
508 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
509 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_32a05601, VALIDATION_ERROR_UNDEFINED);
510 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_32a04601,
511 VALIDATION_ERROR_32a04607);
512 if (skip) {
513 return VK_ERROR_VALIDATION_FAILED_EXT;
514 }
515 // A DescriptorPool's descriptor sets are implicitly deleted when the pool is reset.
516 // Remove this pool's descriptor sets from our descriptorSet map.
517 auto itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
518 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600519 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600520 auto del_itr = itr++;
521 if (pNode->parent_object == HandleToUint64(descriptorPool)) {
522 DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
523 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
524 }
525 }
526 lock.unlock();
527 VkResult result = get_dispatch_table(ot_device_table_map, device)->ResetDescriptorPool(device, descriptorPool, flags);
528 return result;
529}
530
531VKAPI_ATTR VkResult VKAPI_CALL BeginCommandBuffer(VkCommandBuffer command_buffer, const VkCommandBufferBeginInfo *begin_info) {
532 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(command_buffer), layer_data_map);
533 bool skip = false;
534 {
535 std::lock_guard<std::mutex> lock(global_lock);
536 skip |= ValidateObject(command_buffer, command_buffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_16e02401,
537 VALIDATION_ERROR_UNDEFINED);
538 if (begin_info) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600539 ObjTrackState *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600540 if ((begin_info->pInheritanceInfo) && (pNode->status & OBJSTATUS_COMMAND_BUFFER_SECONDARY) &&
541 (begin_info->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
542 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->framebuffer, kVulkanObjectTypeFramebuffer,
543 true, VALIDATION_ERROR_0280006e, VALIDATION_ERROR_02a00009);
544 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->renderPass, kVulkanObjectTypeRenderPass, false,
545 VALIDATION_ERROR_0280006a, VALIDATION_ERROR_02a00009);
546 }
547 }
548 }
549 if (skip) {
550 return VK_ERROR_VALIDATION_FAILED_EXT;
551 }
552 VkResult result = get_dispatch_table(ot_device_table_map, command_buffer)->BeginCommandBuffer(command_buffer, begin_info);
553 return result;
554}
555
556VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(VkInstance instance,
557 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
558 const VkAllocationCallbacks *pAllocator,
559 VkDebugReportCallbackEXT *pCallback) {
560 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
561 VkResult result = pInstanceTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);
562 if (VK_SUCCESS == result) {
563 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
564 result = layer_create_msg_callback(instance_data->report_data, false, pCreateInfo, pAllocator, pCallback);
565 CreateObject(instance, *pCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator);
566 }
567 return result;
568}
569
570VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback,
571 const VkAllocationCallbacks *pAllocator) {
572 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
573 pInstanceTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
574 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
575 layer_destroy_msg_callback(instance_data->report_data, msgCallback, pAllocator);
576 DestroyObject(instance, msgCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator, VALIDATION_ERROR_242009b4,
577 VALIDATION_ERROR_242009b6);
578}
579
580VKAPI_ATTR void VKAPI_CALL DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
581 VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
582 int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
583 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
584 pInstanceTable->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
585}
586
587static const VkExtensionProperties instance_extensions[] = {{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION}};
588
589static const VkLayerProperties globalLayerProps = {"VK_LAYER_LUNARG_object_tracker",
590 VK_LAYER_API_VERSION, // specVersion
591 1, // implementationVersion
592 "LunarG Validation Layer"};
593
594VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
595 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
596}
597
598VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
599 VkLayerProperties *pProperties) {
600 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
601}
602
603VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
604 VkExtensionProperties *pProperties) {
605 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
606 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
607
608 return VK_ERROR_LAYER_NOT_PRESENT;
609}
610
611VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
612 uint32_t *pCount, VkExtensionProperties *pProperties) {
613 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
614 return util_GetExtensionProperties(0, nullptr, pCount, pProperties);
615
616 assert(physicalDevice);
617 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(ot_instance_table_map, physicalDevice);
618 return pTable->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
619}
620
621VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
622 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
623 std::lock_guard<std::mutex> lock(global_lock);
624 bool skip = ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_1fc27a01,
625 VALIDATION_ERROR_UNDEFINED);
626 if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
627
628 layer_data *phy_dev_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
629 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
630
631 assert(chain_info->u.pLayerInfo);
632 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
633 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
634 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(phy_dev_data->instance, "vkCreateDevice");
635 if (fpCreateDevice == NULL) {
636 return VK_ERROR_INITIALIZATION_FAILED;
637 }
638
639 // Advance the link info for the next element on the chain
640 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
641
642 VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
643 if (result != VK_SUCCESS) {
644 return result;
645 }
646
647 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
648 device_data->report_data = layer_debug_report_create_device(phy_dev_data->report_data, *pDevice);
649 layer_init_device_dispatch_table(*pDevice, &device_data->dispatch_table, fpGetDeviceProcAddr);
650
651 // Add link back to physDev
652 device_data->physical_device = physicalDevice;
653
654 initDeviceTable(*pDevice, fpGetDeviceProcAddr, ot_device_table_map);
655
656 CreateObject(*pDevice, *pDevice, kVulkanObjectTypeDevice, pAllocator);
657
658 return result;
659}
660
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600661VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount,
662 VkImage *pSwapchainImages) {
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600663 bool skip = false;
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600664 std::unique_lock<std::mutex> lock(global_lock);
665 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_30805601, VALIDATION_ERROR_UNDEFINED);
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600666 skip |= ValidateObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, false, VALIDATION_ERROR_3082f001,
667 VALIDATION_ERROR_UNDEFINED);
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600668 lock.unlock();
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600669 if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
670
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600671 VkResult result = get_dispatch_table(ot_device_table_map, device)
672 ->GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
673 if (pSwapchainImages != NULL) {
674 lock.lock();
675 for (uint32_t i = 0; i < *pSwapchainImageCount; i++) {
676 CreateSwapchainImageObject(device, pSwapchainImages[i], swapchain);
677 }
678 lock.unlock();
679 }
680 return result;
681}
682
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600683VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
684 uint32_t *pQueueFamilyPropertyCount,
685 VkQueueFamilyProperties *pQueueFamilyProperties) {
686 bool skip = false;
687 {
688 std::lock_guard<std::mutex> lock(global_lock);
689 skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_2da27a01,
690 VALIDATION_ERROR_UNDEFINED);
691 }
692 if (skip) {
693 return;
694 }
695 get_dispatch_table(ot_instance_table_map, physicalDevice)
696 ->GetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
697 std::lock_guard<std::mutex> lock(global_lock);
698 if (pQueueFamilyProperties != NULL) {
699 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
700 if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
701 instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
702 }
703 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
704 instance_data->queue_family_properties[i] = pQueueFamilyProperties[i];
705 }
706 }
707}
708
709VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
710 VkInstance *pInstance) {
711 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
712
713 assert(chain_info->u.pLayerInfo);
714 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
715 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
716 if (fpCreateInstance == NULL) {
717 return VK_ERROR_INITIALIZATION_FAILED;
718 }
719
720 // Advance the link info for the next element on the chain
721 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
722
723 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
724 if (result != VK_SUCCESS) {
725 return result;
726 }
727
728 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(*pInstance), layer_data_map);
729 instance_data->instance = *pInstance;
730 initInstanceTable(*pInstance, fpGetInstanceProcAddr, ot_instance_table_map);
731 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, *pInstance);
732
733 // Look for one or more debug report create info structures, and copy the
734 // callback(s) for each one found (for use by vkDestroyInstance)
735 layer_copy_tmp_callbacks(pCreateInfo->pNext, &instance_data->num_tmp_callbacks, &instance_data->tmp_dbg_create_infos,
736 &instance_data->tmp_callbacks);
737
738 instance_data->report_data = debug_report_create_instance(pInstanceTable, *pInstance, pCreateInfo->enabledExtensionCount,
739 pCreateInfo->ppEnabledExtensionNames);
740
741 InitObjectTracker(instance_data, pAllocator);
742
743 CreateObject(*pInstance, *pInstance, kVulkanObjectTypeInstance, pAllocator);
744
745 return result;
746}
747
748VKAPI_ATTR VkResult VKAPI_CALL EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
749 VkPhysicalDevice *pPhysicalDevices) {
750 bool skip = VK_FALSE;
751 std::unique_lock<std::mutex> lock(global_lock);
752 skip |=
753 ValidateObject(instance, instance, kVulkanObjectTypeInstance, false, VALIDATION_ERROR_2800bc01, VALIDATION_ERROR_UNDEFINED);
754 lock.unlock();
755 if (skip) {
756 return VK_ERROR_VALIDATION_FAILED_EXT;
757 }
758 VkResult result = get_dispatch_table(ot_instance_table_map, instance)
759 ->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
760 lock.lock();
761 if (result == VK_SUCCESS) {
762 if (pPhysicalDevices) {
763 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
764 CreateObject(instance, pPhysicalDevices[i], kVulkanObjectTypePhysicalDevice, nullptr);
765 }
766 }
767 }
768 lock.unlock();
769 return result;
770}
771
772VKAPI_ATTR VkResult VKAPI_CALL AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo,
773 VkCommandBuffer *pCommandBuffers) {
774 bool skip = VK_FALSE;
775 std::unique_lock<std::mutex> lock(global_lock);
776 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16805601, VALIDATION_ERROR_UNDEFINED);
777 skip |= ValidateObject(device, pAllocateInfo->commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_02602801,
778 VALIDATION_ERROR_UNDEFINED);
779 lock.unlock();
780
781 if (skip) {
782 return VK_ERROR_VALIDATION_FAILED_EXT;
783 }
784
785 VkResult result =
786 get_dispatch_table(ot_device_table_map, device)->AllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
787
788 lock.lock();
789 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) {
790 AllocateCommandBuffer(device, pAllocateInfo->commandPool, pCommandBuffers[i], pAllocateInfo->level);
791 }
792 lock.unlock();
793
794 return result;
795}
796
797VKAPI_ATTR VkResult VKAPI_CALL AllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo,
798 VkDescriptorSet *pDescriptorSets) {
799 bool skip = VK_FALSE;
800 std::unique_lock<std::mutex> lock(global_lock);
801 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16a05601, VALIDATION_ERROR_UNDEFINED);
802 skip |= ValidateObject(device, pAllocateInfo->descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_04c04601,
803 VALIDATION_ERROR_04c00009);
804 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
805 skip |= ValidateObject(device, pAllocateInfo->pSetLayouts[i], kVulkanObjectTypeDescriptorSetLayout, false,
806 VALIDATION_ERROR_04c22c01, VALIDATION_ERROR_04c00009);
807 }
808 lock.unlock();
809 if (skip) {
810 return VK_ERROR_VALIDATION_FAILED_EXT;
811 }
812
813 VkResult result =
814 get_dispatch_table(ot_device_table_map, device)->AllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets);
815
816 if (VK_SUCCESS == result) {
817 lock.lock();
818 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
819 AllocateDescriptorSet(device, pAllocateInfo->descriptorPool, pDescriptorSets[i]);
820 }
821 lock.unlock();
822 }
823
824 return result;
825}
826
827VKAPI_ATTR void VKAPI_CALL FreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
828 const VkCommandBuffer *pCommandBuffers) {
829 bool skip = false;
830 std::unique_lock<std::mutex> lock(global_lock);
831 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28405601, VALIDATION_ERROR_UNDEFINED);
832 ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_28402801, VALIDATION_ERROR_28402807);
833 for (uint32_t i = 0; i < commandBufferCount; i++) {
834 if (pCommandBuffers[i] != VK_NULL_HANDLE) {
835 skip |= ValidateCommandBuffer(device, commandPool, pCommandBuffers[i]);
836 }
837 }
838
839 for (uint32_t i = 0; i < commandBufferCount; i++) {
840 DestroyObject(device, pCommandBuffers[i], kVulkanObjectTypeCommandBuffer, nullptr, VALIDATION_ERROR_UNDEFINED,
841 VALIDATION_ERROR_UNDEFINED);
842 }
843
844 lock.unlock();
845 if (!skip) {
846 get_dispatch_table(ot_device_table_map, device)
847 ->FreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
848 }
849}
850
851VKAPI_ATTR void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
852 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
853 std::unique_lock<std::mutex> lock(global_lock);
854 // A swapchain's images are implicitly deleted when the swapchain is deleted.
855 // Remove this swapchain's images from our map of such images.
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600856 std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->swapchainImageMap.begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600857 while (itr != device_data->swapchainImageMap.end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600858 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600859 if (pNode->parent_object == HandleToUint64(swapchain)) {
860 delete pNode;
861 auto delete_item = itr++;
862 device_data->swapchainImageMap.erase(delete_item);
863 } else {
864 ++itr;
865 }
866 }
867 DestroyObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, pAllocator, VALIDATION_ERROR_26e00a06,
868 VALIDATION_ERROR_26e00a08);
869 lock.unlock();
870
871 get_dispatch_table(ot_device_table_map, device)->DestroySwapchainKHR(device, swapchain, pAllocator);
872}
873
874VKAPI_ATTR VkResult VKAPI_CALL FreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
875 const VkDescriptorSet *pDescriptorSets) {
876 bool skip = false;
877 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
878 std::unique_lock<std::mutex> lock(global_lock);
879 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28605601, VALIDATION_ERROR_UNDEFINED);
880 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_28604601,
881 VALIDATION_ERROR_28604607);
882 for (uint32_t i = 0; i < descriptorSetCount; i++) {
883 if (pDescriptorSets[i] != VK_NULL_HANDLE) {
884 skip |= ValidateDescriptorSet(device, descriptorPool, pDescriptorSets[i]);
885 }
886 }
887
888 for (uint32_t i = 0; i < descriptorSetCount; i++) {
889 DestroyObject(device, pDescriptorSets[i], kVulkanObjectTypeDescriptorSet, nullptr, VALIDATION_ERROR_UNDEFINED,
890 VALIDATION_ERROR_UNDEFINED);
891 }
892
893 lock.unlock();
894 if (!skip) {
895 result = get_dispatch_table(ot_device_table_map, device)
896 ->FreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets);
897 }
898 return result;
899}
900
901VKAPI_ATTR void VKAPI_CALL DestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
902 const VkAllocationCallbacks *pAllocator) {
903 bool skip = VK_FALSE;
904 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
905 std::unique_lock<std::mutex> lock(global_lock);
906 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24405601, VALIDATION_ERROR_UNDEFINED);
907 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, true, VALIDATION_ERROR_24404601,
908 VALIDATION_ERROR_24404607);
909 lock.unlock();
910 if (skip) {
911 return;
912 }
913 // A DescriptorPool's descriptor sets are implicitly deleted when the pool is deleted.
914 // Remove this pool's descriptor sets from our descriptorSet map.
915 lock.lock();
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600916 std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600917 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600918 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600919 auto del_itr = itr++;
920 if (pNode->parent_object == HandleToUint64(descriptorPool)) {
921 DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
922 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
923 }
924 }
925 DestroyObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, pAllocator, VALIDATION_ERROR_24400260,
926 VALIDATION_ERROR_24400262);
927 lock.unlock();
928 get_dispatch_table(ot_device_table_map, device)->DestroyDescriptorPool(device, descriptorPool, pAllocator);
929}
930
931VKAPI_ATTR void VKAPI_CALL DestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) {
932 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
933 bool skip = false;
934 std::unique_lock<std::mutex> lock(global_lock);
935 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24005601, VALIDATION_ERROR_UNDEFINED);
936 skip |= ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, true, VALIDATION_ERROR_24002801,
937 VALIDATION_ERROR_24002807);
938 lock.unlock();
939 if (skip) {
940 return;
941 }
942 lock.lock();
943 // A CommandPool's command buffers are implicitly deleted when the pool is deleted.
944 // Remove this pool's cmdBuffers from our cmd buffer map.
945 auto itr = device_data->object_map[kVulkanObjectTypeCommandBuffer].begin();
946 auto del_itr = itr;
947 while (itr != device_data->object_map[kVulkanObjectTypeCommandBuffer].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600948 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600949 del_itr = itr++;
950 if (pNode->parent_object == HandleToUint64(commandPool)) {
951 skip |= ValidateCommandBuffer(device, commandPool, reinterpret_cast<VkCommandBuffer>((*del_itr).first));
952 DestroyObject(device, reinterpret_cast<VkCommandBuffer>((*del_itr).first), kVulkanObjectTypeCommandBuffer, nullptr,
953 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
954 }
955 }
956 DestroyObject(device, commandPool, kVulkanObjectTypeCommandPool, pAllocator, VALIDATION_ERROR_24000054,
957 VALIDATION_ERROR_24000056);
958 lock.unlock();
959 get_dispatch_table(ot_device_table_map, device)->DestroyCommandPool(device, commandPool, pAllocator);
960}
961
962VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
963 uint32_t *pQueueFamilyPropertyCount,
964 VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
965 bool skip = false;
966 {
967 std::lock_guard<std::mutex> lock(global_lock);
968 skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_UNDEFINED,
969 VALIDATION_ERROR_UNDEFINED);
970 }
971 if (skip) {
972 return;
973 }
974 get_dispatch_table(ot_instance_table_map, physicalDevice)
975 ->GetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
976 std::lock_guard<std::mutex> lock(global_lock);
977 if (pQueueFamilyProperties != NULL) {
978 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
979 if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
980 instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
981 }
982 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
983 instance_data->queue_family_properties[i] = pQueueFamilyProperties[i].queueFamilyProperties;
984 }
985 }
986}
987
Mark Lobodzinskidfe5e172017-07-19 13:03:22 -0600988VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectNameEXT(VkDevice device, const VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600989 bool skip = VK_FALSE;
990 std::unique_lock<std::mutex> lock(global_lock);
991 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
992 if (pNameInfo->pObjectName) {
993 dev_data->report_data->debugObjectNameMap->insert(
994 std::make_pair<uint64_t, std::string>((uint64_t &&) pNameInfo->object, pNameInfo->pObjectName));
995 } else {
996 dev_data->report_data->debugObjectNameMap->erase(pNameInfo->object);
997 }
998 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_23605601, VALIDATION_ERROR_UNDEFINED);
999 lock.unlock();
1000 if (skip) {
1001 return VK_ERROR_VALIDATION_FAILED_EXT;
1002 }
1003 VkResult result = dev_data->dispatch_table.DebugMarkerSetObjectNameEXT(device, pNameInfo);
1004 return result;
1005}
1006
1007VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
1008 assert(instance);
1009
1010 if (get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr == NULL) {
1011 return NULL;
1012 }
1013 return get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr(instance, funcName);
1014}
1015
1016VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
1017 const auto item = name_to_funcptr_map.find(funcName);
1018 if (item != name_to_funcptr_map.end()) {
1019 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
1020 }
1021
1022 auto table = get_dispatch_table(ot_device_table_map, device);
1023 if (!table->GetDeviceProcAddr) return NULL;
1024 return table->GetDeviceProcAddr(device, funcName);
1025}
1026
1027VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
1028 const auto item = name_to_funcptr_map.find(funcName);
1029 if (item != name_to_funcptr_map.end()) {
1030 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
1031 }
1032
1033 auto table = get_dispatch_table(ot_instance_table_map, instance);
1034 if (!table->GetInstanceProcAddr) return nullptr;
1035 return table->GetInstanceProcAddr(instance, funcName);
1036}
1037
1038} // namespace object_tracker
1039
1040VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
1041 VkExtensionProperties *pProperties) {
1042 return object_tracker::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
1043}
1044
1045VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
1046 VkLayerProperties *pProperties) {
1047 return object_tracker::EnumerateInstanceLayerProperties(pCount, pProperties);
1048}
1049
1050VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
1051 VkLayerProperties *pProperties) {
1052 // The layer command handles VK_NULL_HANDLE just fine internally
1053 assert(physicalDevice == VK_NULL_HANDLE);
1054 return object_tracker::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
1055}
1056
1057VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
1058 return object_tracker::GetDeviceProcAddr(dev, funcName);
1059}
1060
1061VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
1062 return object_tracker::GetInstanceProcAddr(instance, funcName);
1063}
1064
1065VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1066 const char *pLayerName, uint32_t *pCount,
1067 VkExtensionProperties *pProperties) {
1068 // The layer command handles VK_NULL_HANDLE just fine internally
1069 assert(physicalDevice == VK_NULL_HANDLE);
1070 return object_tracker::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
1071}
1072
1073VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1074 const char *funcName) {
1075 return object_tracker::GetPhysicalDeviceProcAddr(instance, funcName);
1076}
1077
1078VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
1079 assert(pVersionStruct != NULL);
1080 assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
1081
1082 // Fill in the function pointers if our version is at least capable of having the structure contain them.
1083 if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
1084 pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
1085 pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
1086 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
1087 }
1088
1089 if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1090 object_tracker::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
1091 } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1092 pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
1093 }
1094
1095 return VK_SUCCESS;
1096}