blob: c55603666ac5d605ecf7f08eed249ee2cd8fadc5 [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()) {
43 OT_QUEUE_INFO *p_queue_info = new OT_QUEUE_INFO;
44 if (p_queue_info != NULL) {
45 memset(p_queue_info, 0, sizeof(OT_QUEUE_INFO));
46 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()) {
88 OT_QUEUE_INFO *pQueueInfo = queue_item->second;
89 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
111 OBJTRACK_NODE *pNewObjNode = new OBJTRACK_NODE;
112 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()) {
131 OBJTRACK_NODE *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
132
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
158 OBJTRACK_NODE *pNewObjNode = new OBJTRACK_NODE;
159 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()) {
174 OBJTRACK_NODE *pNode = dsItem->second;
175
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
193void CreateQueue(VkDevice device, VkQueue vkObj) {
194 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
195
196 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
197 HandleToUint64(vkObj), __LINE__, OBJTRACK_NONE, LayerName, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
198 object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT", HandleToUint64(vkObj));
199
200 OBJTRACK_NODE *p_obj_node = NULL;
201 auto queue_item = device_data->object_map[kVulkanObjectTypeQueue].find(HandleToUint64(vkObj));
202 if (queue_item == device_data->object_map[kVulkanObjectTypeQueue].end()) {
203 p_obj_node = new OBJTRACK_NODE;
204 device_data->object_map[kVulkanObjectTypeQueue][HandleToUint64(vkObj)] = p_obj_node;
205 device_data->num_objects[kVulkanObjectTypeQueue]++;
206 device_data->num_total_objects++;
207 } else {
208 p_obj_node = queue_item->second;
209 }
210 p_obj_node->object_type = kVulkanObjectTypeQueue;
211 p_obj_node->status = OBJSTATUS_NONE;
212 p_obj_node->handle = HandleToUint64(vkObj);
213}
214
215void CreateSwapchainImageObject(VkDevice dispatchable_object, VkImage swapchain_image, VkSwapchainKHR swapchain) {
216 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(dispatchable_object), layer_data_map);
217 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
218 HandleToUint64(swapchain_image), __LINE__, OBJTRACK_NONE, LayerName,
219 "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64, object_track_index++, "SwapchainImage",
220 HandleToUint64(swapchain_image));
221
222 OBJTRACK_NODE *pNewObjNode = new OBJTRACK_NODE;
223 pNewObjNode->object_type = kVulkanObjectTypeImage;
224 pNewObjNode->status = OBJSTATUS_NONE;
225 pNewObjNode->handle = HandleToUint64(swapchain_image);
226 pNewObjNode->parent_object = HandleToUint64(swapchain);
227 device_data->swapchainImageMap[HandleToUint64(swapchain_image)] = pNewObjNode;
228}
229
230void DeviceReportUndestroyedObjects(VkDevice device, VulkanObjectType object_type, enum UNIQUE_VALIDATION_ERROR_CODE error_code) {
231 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
232 for (auto item = device_data->object_map[object_type].begin(); item != device_data->object_map[object_type].end();) {
233 OBJTRACK_NODE *object_info = item->second;
234 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, get_debug_report_enum[object_type], object_info->handle,
235 __LINE__, error_code, LayerName,
236 "OBJ ERROR : For device 0x%" PRIxLEAST64 ", %s object 0x%" PRIxLEAST64 " has not been destroyed. %s",
237 HandleToUint64(device), object_string[object_type], object_info->handle, validation_error_map[error_code]);
238 item = device_data->object_map[object_type].erase(item);
239 }
240}
241
242VKAPI_ATTR void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
243 std::unique_lock<std::mutex> lock(global_lock);
244
245 dispatch_key key = get_dispatch_key(instance);
246 layer_data *instance_data = GetLayerDataPtr(key, layer_data_map);
247
248 // Enable the temporary callback(s) here to catch cleanup issues:
249 bool callback_setup = false;
250 if (instance_data->num_tmp_callbacks > 0) {
251 if (!layer_enable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks,
252 instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks)) {
253 callback_setup = true;
254 }
255 }
256
257 // TODO: The instance handle can not be validated here. The loader will likely have to validate it.
258 ValidateObject(instance, instance, kVulkanObjectTypeInstance, true, VALIDATION_ERROR_2580bc01, VALIDATION_ERROR_UNDEFINED);
259
260 // Destroy physical devices
261 for (auto iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
262 iit != instance_data->object_map[kVulkanObjectTypePhysicalDevice].end();) {
263 OBJTRACK_NODE *pNode = iit->second;
264
265 VkPhysicalDevice physical_device = reinterpret_cast<VkPhysicalDevice>(pNode->handle);
266 DestroyObject(instance, physical_device, kVulkanObjectTypePhysicalDevice, nullptr, VALIDATION_ERROR_UNDEFINED,
267 VALIDATION_ERROR_UNDEFINED);
268 iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
269 }
270
271 DestroyObject(instance, instance, kVulkanObjectTypeInstance, pAllocator, VALIDATION_ERROR_258004ec, VALIDATION_ERROR_258004ee);
272 // Report any remaining objects in LL
273
274 for (auto iit = instance_data->object_map[kVulkanObjectTypeDevice].begin();
275 iit != instance_data->object_map[kVulkanObjectTypeDevice].end();) {
276 OBJTRACK_NODE *pNode = iit->second;
277
278 VkDevice device = reinterpret_cast<VkDevice>(pNode->handle);
279 VkDebugReportObjectTypeEXT debug_object_type = get_debug_report_enum[pNode->object_type];
280
281 log_msg(instance_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, debug_object_type, pNode->handle, __LINE__,
282 OBJTRACK_OBJECT_LEAK, LayerName, "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.",
283 string_VkDebugReportObjectTypeEXT(debug_object_type), pNode->handle);
284
285 object_tracker_report_undestroyed_objects(device, VALIDATION_ERROR_258004ea);
286 }
287 instance_data->object_map[kVulkanObjectTypeDevice].clear();
288
289 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
290 pInstanceTable->DestroyInstance(instance, pAllocator);
291
292 // Disable and cleanup the temporary callback(s):
293 if (callback_setup) {
294 layer_disable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks, instance_data->tmp_callbacks);
295 }
296 if (instance_data->num_tmp_callbacks > 0) {
297 layer_free_tmp_callbacks(instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks);
298 instance_data->num_tmp_callbacks = 0;
299 }
300
301 // Clean up logging callback, if any
302 while (instance_data->logging_callback.size() > 0) {
303 VkDebugReportCallbackEXT callback = instance_data->logging_callback.back();
304 layer_destroy_msg_callback(instance_data->report_data, callback, pAllocator);
305 instance_data->logging_callback.pop_back();
306 }
307
308 layer_debug_report_destroy_instance(instance_data->report_data);
309 FreeLayerDataPtr(key, layer_data_map);
310
311 lock.unlock();
312 ot_instance_table_map.erase(key);
313 delete pInstanceTable;
314}
315
316VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
317 std::unique_lock<std::mutex> lock(global_lock);
318 ValidateObject(device, device, kVulkanObjectTypeDevice, true, VALIDATION_ERROR_24a05601, VALIDATION_ERROR_UNDEFINED);
319 DestroyObject(device, device, kVulkanObjectTypeDevice, pAllocator, VALIDATION_ERROR_24a002f6, VALIDATION_ERROR_24a002f8);
320
321 // Report any remaining objects associated with this VkDevice object in LL
322
323 object_tracker_report_undestroyed_objects(device, VALIDATION_ERROR_24a002f4);
324
325 // Clean up Queue's MemRef Linked Lists
326 DestroyQueueDataStructures(device);
327
328 lock.unlock();
329
330 dispatch_key key = get_dispatch_key(device);
331 VkLayerDispatchTable *pDisp = get_dispatch_table(ot_device_table_map, device);
332 pDisp->DestroyDevice(device, pAllocator);
333 ot_device_table_map.erase(key);
334 delete pDisp;
335
336 FreeLayerDataPtr(key, layer_data_map);
337}
338
339VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
340 const VkWriteDescriptorSet *pDescriptorWrites, uint32_t descriptorCopyCount,
341 const VkCopyDescriptorSet *pDescriptorCopies) {
342 bool skip = false;
343 {
344 std::lock_guard<std::mutex> lock(global_lock);
345 skip |=
346 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_33c05601, VALIDATION_ERROR_UNDEFINED);
347 if (pDescriptorCopies) {
348 for (uint32_t idx0 = 0; idx0 < descriptorCopyCount; ++idx0) {
349 if (pDescriptorCopies[idx0].dstSet) {
350 skip |= ValidateObject(device, pDescriptorCopies[idx0].dstSet, kVulkanObjectTypeDescriptorSet, false,
351 VALIDATION_ERROR_03207601, VALIDATION_ERROR_03200009);
352 }
353 if (pDescriptorCopies[idx0].srcSet) {
354 skip |= ValidateObject(device, pDescriptorCopies[idx0].srcSet, kVulkanObjectTypeDescriptorSet, false,
355 VALIDATION_ERROR_0322d201, VALIDATION_ERROR_03200009);
356 }
357 }
358 }
359 if (pDescriptorWrites) {
360 for (uint32_t idx1 = 0; idx1 < descriptorWriteCount; ++idx1) {
361 if (pDescriptorWrites[idx1].dstSet) {
362 skip |= ValidateObject(device, pDescriptorWrites[idx1].dstSet, kVulkanObjectTypeDescriptorSet, false,
363 VALIDATION_ERROR_15c00280, VALIDATION_ERROR_15c00009);
364 }
365 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||
366 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) {
367 for (uint32_t idx2 = 0; idx2 < pDescriptorWrites[idx1].descriptorCount; ++idx2) {
368 skip |= ValidateObject(device, pDescriptorWrites[idx1].pTexelBufferView[idx2], kVulkanObjectTypeBufferView,
369 false, VALIDATION_ERROR_15c00286, VALIDATION_ERROR_15c00009);
370 }
371 }
372 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||
373 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) ||
374 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ||
375 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
376 for (uint32_t idx3 = 0; idx3 < pDescriptorWrites[idx1].descriptorCount; ++idx3) {
377 skip |=
378 ValidateObject(device, pDescriptorWrites[idx1].pImageInfo[idx3].imageView, kVulkanObjectTypeImageView,
379 false, VALIDATION_ERROR_15c0028c, VALIDATION_ERROR_04600009);
380 }
381 }
382 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
383 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||
384 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
385 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
386 for (uint32_t idx4 = 0; idx4 < pDescriptorWrites[idx1].descriptorCount; ++idx4) {
387 if (pDescriptorWrites[idx1].pBufferInfo[idx4].buffer) {
388 skip |=
389 ValidateObject(device, pDescriptorWrites[idx1].pBufferInfo[idx4].buffer, kVulkanObjectTypeBuffer,
390 false, VALIDATION_ERROR_04401a01, VALIDATION_ERROR_UNDEFINED);
391 }
392 }
393 }
394 }
395 }
396 }
397 if (skip) {
398 return;
399 }
400 get_dispatch_table(ot_device_table_map, device)
401 ->UpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);
402}
403
404VKAPI_ATTR VkResult VKAPI_CALL ResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
405 VkDescriptorPoolResetFlags flags) {
406 bool skip = false;
407 std::unique_lock<std::mutex> lock(global_lock);
408 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
409 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_32a05601, VALIDATION_ERROR_UNDEFINED);
410 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_32a04601,
411 VALIDATION_ERROR_32a04607);
412 if (skip) {
413 return VK_ERROR_VALIDATION_FAILED_EXT;
414 }
415 // A DescriptorPool's descriptor sets are implicitly deleted when the pool is reset.
416 // Remove this pool's descriptor sets from our descriptorSet map.
417 auto itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
418 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
419 OBJTRACK_NODE *pNode = (*itr).second;
420 auto del_itr = itr++;
421 if (pNode->parent_object == HandleToUint64(descriptorPool)) {
422 DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
423 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
424 }
425 }
426 lock.unlock();
427 VkResult result = get_dispatch_table(ot_device_table_map, device)->ResetDescriptorPool(device, descriptorPool, flags);
428 return result;
429}
430
431VKAPI_ATTR VkResult VKAPI_CALL BeginCommandBuffer(VkCommandBuffer command_buffer, const VkCommandBufferBeginInfo *begin_info) {
432 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(command_buffer), layer_data_map);
433 bool skip = false;
434 {
435 std::lock_guard<std::mutex> lock(global_lock);
436 skip |= ValidateObject(command_buffer, command_buffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_16e02401,
437 VALIDATION_ERROR_UNDEFINED);
438 if (begin_info) {
439 OBJTRACK_NODE *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
440 if ((begin_info->pInheritanceInfo) && (pNode->status & OBJSTATUS_COMMAND_BUFFER_SECONDARY) &&
441 (begin_info->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
442 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->framebuffer, kVulkanObjectTypeFramebuffer,
443 true, VALIDATION_ERROR_0280006e, VALIDATION_ERROR_02a00009);
444 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->renderPass, kVulkanObjectTypeRenderPass, false,
445 VALIDATION_ERROR_0280006a, VALIDATION_ERROR_02a00009);
446 }
447 }
448 }
449 if (skip) {
450 return VK_ERROR_VALIDATION_FAILED_EXT;
451 }
452 VkResult result = get_dispatch_table(ot_device_table_map, command_buffer)->BeginCommandBuffer(command_buffer, begin_info);
453 return result;
454}
455
456VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(VkInstance instance,
457 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
458 const VkAllocationCallbacks *pAllocator,
459 VkDebugReportCallbackEXT *pCallback) {
460 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
461 VkResult result = pInstanceTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);
462 if (VK_SUCCESS == result) {
463 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
464 result = layer_create_msg_callback(instance_data->report_data, false, pCreateInfo, pAllocator, pCallback);
465 CreateObject(instance, *pCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator);
466 }
467 return result;
468}
469
470VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback,
471 const VkAllocationCallbacks *pAllocator) {
472 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
473 pInstanceTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
474 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
475 layer_destroy_msg_callback(instance_data->report_data, msgCallback, pAllocator);
476 DestroyObject(instance, msgCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator, VALIDATION_ERROR_242009b4,
477 VALIDATION_ERROR_242009b6);
478}
479
480VKAPI_ATTR void VKAPI_CALL DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
481 VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
482 int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
483 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
484 pInstanceTable->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
485}
486
487static const VkExtensionProperties instance_extensions[] = {{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION}};
488
489static const VkLayerProperties globalLayerProps = {"VK_LAYER_LUNARG_object_tracker",
490 VK_LAYER_API_VERSION, // specVersion
491 1, // implementationVersion
492 "LunarG Validation Layer"};
493
494VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
495 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
496}
497
498VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
499 VkLayerProperties *pProperties) {
500 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
501}
502
503VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
504 VkExtensionProperties *pProperties) {
505 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
506 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
507
508 return VK_ERROR_LAYER_NOT_PRESENT;
509}
510
511VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
512 uint32_t *pCount, VkExtensionProperties *pProperties) {
513 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
514 return util_GetExtensionProperties(0, nullptr, pCount, pProperties);
515
516 assert(physicalDevice);
517 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(ot_instance_table_map, physicalDevice);
518 return pTable->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
519}
520
521VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
522 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
523 std::lock_guard<std::mutex> lock(global_lock);
524 bool skip = ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_1fc27a01,
525 VALIDATION_ERROR_UNDEFINED);
526 if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
527
528 layer_data *phy_dev_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
529 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
530
531 assert(chain_info->u.pLayerInfo);
532 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
533 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
534 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(phy_dev_data->instance, "vkCreateDevice");
535 if (fpCreateDevice == NULL) {
536 return VK_ERROR_INITIALIZATION_FAILED;
537 }
538
539 // Advance the link info for the next element on the chain
540 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
541
542 VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
543 if (result != VK_SUCCESS) {
544 return result;
545 }
546
547 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
548 device_data->report_data = layer_debug_report_create_device(phy_dev_data->report_data, *pDevice);
549 layer_init_device_dispatch_table(*pDevice, &device_data->dispatch_table, fpGetDeviceProcAddr);
550
551 // Add link back to physDev
552 device_data->physical_device = physicalDevice;
553
554 initDeviceTable(*pDevice, fpGetDeviceProcAddr, ot_device_table_map);
555
556 CreateObject(*pDevice, *pDevice, kVulkanObjectTypeDevice, pAllocator);
557
558 return result;
559}
560
561VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
562 uint32_t *pQueueFamilyPropertyCount,
563 VkQueueFamilyProperties *pQueueFamilyProperties) {
564 bool skip = false;
565 {
566 std::lock_guard<std::mutex> lock(global_lock);
567 skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_2da27a01,
568 VALIDATION_ERROR_UNDEFINED);
569 }
570 if (skip) {
571 return;
572 }
573 get_dispatch_table(ot_instance_table_map, physicalDevice)
574 ->GetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
575 std::lock_guard<std::mutex> lock(global_lock);
576 if (pQueueFamilyProperties != NULL) {
577 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
578 if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
579 instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
580 }
581 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
582 instance_data->queue_family_properties[i] = pQueueFamilyProperties[i];
583 }
584 }
585}
586
587VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
588 VkInstance *pInstance) {
589 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
590
591 assert(chain_info->u.pLayerInfo);
592 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
593 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
594 if (fpCreateInstance == NULL) {
595 return VK_ERROR_INITIALIZATION_FAILED;
596 }
597
598 // Advance the link info for the next element on the chain
599 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
600
601 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
602 if (result != VK_SUCCESS) {
603 return result;
604 }
605
606 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(*pInstance), layer_data_map);
607 instance_data->instance = *pInstance;
608 initInstanceTable(*pInstance, fpGetInstanceProcAddr, ot_instance_table_map);
609 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, *pInstance);
610
611 // Look for one or more debug report create info structures, and copy the
612 // callback(s) for each one found (for use by vkDestroyInstance)
613 layer_copy_tmp_callbacks(pCreateInfo->pNext, &instance_data->num_tmp_callbacks, &instance_data->tmp_dbg_create_infos,
614 &instance_data->tmp_callbacks);
615
616 instance_data->report_data = debug_report_create_instance(pInstanceTable, *pInstance, pCreateInfo->enabledExtensionCount,
617 pCreateInfo->ppEnabledExtensionNames);
618
619 InitObjectTracker(instance_data, pAllocator);
620
621 CreateObject(*pInstance, *pInstance, kVulkanObjectTypeInstance, pAllocator);
622
623 return result;
624}
625
626VKAPI_ATTR VkResult VKAPI_CALL EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
627 VkPhysicalDevice *pPhysicalDevices) {
628 bool skip = VK_FALSE;
629 std::unique_lock<std::mutex> lock(global_lock);
630 skip |=
631 ValidateObject(instance, instance, kVulkanObjectTypeInstance, false, VALIDATION_ERROR_2800bc01, VALIDATION_ERROR_UNDEFINED);
632 lock.unlock();
633 if (skip) {
634 return VK_ERROR_VALIDATION_FAILED_EXT;
635 }
636 VkResult result = get_dispatch_table(ot_instance_table_map, instance)
637 ->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
638 lock.lock();
639 if (result == VK_SUCCESS) {
640 if (pPhysicalDevices) {
641 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
642 CreateObject(instance, pPhysicalDevices[i], kVulkanObjectTypePhysicalDevice, nullptr);
643 }
644 }
645 }
646 lock.unlock();
647 return result;
648}
649
650VKAPI_ATTR VkResult VKAPI_CALL AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo,
651 VkCommandBuffer *pCommandBuffers) {
652 bool skip = VK_FALSE;
653 std::unique_lock<std::mutex> lock(global_lock);
654 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16805601, VALIDATION_ERROR_UNDEFINED);
655 skip |= ValidateObject(device, pAllocateInfo->commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_02602801,
656 VALIDATION_ERROR_UNDEFINED);
657 lock.unlock();
658
659 if (skip) {
660 return VK_ERROR_VALIDATION_FAILED_EXT;
661 }
662
663 VkResult result =
664 get_dispatch_table(ot_device_table_map, device)->AllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
665
666 lock.lock();
667 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) {
668 AllocateCommandBuffer(device, pAllocateInfo->commandPool, pCommandBuffers[i], pAllocateInfo->level);
669 }
670 lock.unlock();
671
672 return result;
673}
674
675VKAPI_ATTR VkResult VKAPI_CALL AllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo,
676 VkDescriptorSet *pDescriptorSets) {
677 bool skip = VK_FALSE;
678 std::unique_lock<std::mutex> lock(global_lock);
679 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16a05601, VALIDATION_ERROR_UNDEFINED);
680 skip |= ValidateObject(device, pAllocateInfo->descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_04c04601,
681 VALIDATION_ERROR_04c00009);
682 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
683 skip |= ValidateObject(device, pAllocateInfo->pSetLayouts[i], kVulkanObjectTypeDescriptorSetLayout, false,
684 VALIDATION_ERROR_04c22c01, VALIDATION_ERROR_04c00009);
685 }
686 lock.unlock();
687 if (skip) {
688 return VK_ERROR_VALIDATION_FAILED_EXT;
689 }
690
691 VkResult result =
692 get_dispatch_table(ot_device_table_map, device)->AllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets);
693
694 if (VK_SUCCESS == result) {
695 lock.lock();
696 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
697 AllocateDescriptorSet(device, pAllocateInfo->descriptorPool, pDescriptorSets[i]);
698 }
699 lock.unlock();
700 }
701
702 return result;
703}
704
705VKAPI_ATTR void VKAPI_CALL FreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
706 const VkCommandBuffer *pCommandBuffers) {
707 bool skip = false;
708 std::unique_lock<std::mutex> lock(global_lock);
709 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28405601, VALIDATION_ERROR_UNDEFINED);
710 ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_28402801, VALIDATION_ERROR_28402807);
711 for (uint32_t i = 0; i < commandBufferCount; i++) {
712 if (pCommandBuffers[i] != VK_NULL_HANDLE) {
713 skip |= ValidateCommandBuffer(device, commandPool, pCommandBuffers[i]);
714 }
715 }
716
717 for (uint32_t i = 0; i < commandBufferCount; i++) {
718 DestroyObject(device, pCommandBuffers[i], kVulkanObjectTypeCommandBuffer, nullptr, VALIDATION_ERROR_UNDEFINED,
719 VALIDATION_ERROR_UNDEFINED);
720 }
721
722 lock.unlock();
723 if (!skip) {
724 get_dispatch_table(ot_device_table_map, device)
725 ->FreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
726 }
727}
728
729VKAPI_ATTR void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
730 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
731 std::unique_lock<std::mutex> lock(global_lock);
732 // A swapchain's images are implicitly deleted when the swapchain is deleted.
733 // Remove this swapchain's images from our map of such images.
734 std::unordered_map<uint64_t, OBJTRACK_NODE *>::iterator itr = device_data->swapchainImageMap.begin();
735 while (itr != device_data->swapchainImageMap.end()) {
736 OBJTRACK_NODE *pNode = (*itr).second;
737 if (pNode->parent_object == HandleToUint64(swapchain)) {
738 delete pNode;
739 auto delete_item = itr++;
740 device_data->swapchainImageMap.erase(delete_item);
741 } else {
742 ++itr;
743 }
744 }
745 DestroyObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, pAllocator, VALIDATION_ERROR_26e00a06,
746 VALIDATION_ERROR_26e00a08);
747 lock.unlock();
748
749 get_dispatch_table(ot_device_table_map, device)->DestroySwapchainKHR(device, swapchain, pAllocator);
750}
751
752VKAPI_ATTR VkResult VKAPI_CALL FreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
753 const VkDescriptorSet *pDescriptorSets) {
754 bool skip = false;
755 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
756 std::unique_lock<std::mutex> lock(global_lock);
757 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28605601, VALIDATION_ERROR_UNDEFINED);
758 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_28604601,
759 VALIDATION_ERROR_28604607);
760 for (uint32_t i = 0; i < descriptorSetCount; i++) {
761 if (pDescriptorSets[i] != VK_NULL_HANDLE) {
762 skip |= ValidateDescriptorSet(device, descriptorPool, pDescriptorSets[i]);
763 }
764 }
765
766 for (uint32_t i = 0; i < descriptorSetCount; i++) {
767 DestroyObject(device, pDescriptorSets[i], kVulkanObjectTypeDescriptorSet, nullptr, VALIDATION_ERROR_UNDEFINED,
768 VALIDATION_ERROR_UNDEFINED);
769 }
770
771 lock.unlock();
772 if (!skip) {
773 result = get_dispatch_table(ot_device_table_map, device)
774 ->FreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets);
775 }
776 return result;
777}
778
779VKAPI_ATTR void VKAPI_CALL DestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
780 const VkAllocationCallbacks *pAllocator) {
781 bool skip = VK_FALSE;
782 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
783 std::unique_lock<std::mutex> lock(global_lock);
784 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24405601, VALIDATION_ERROR_UNDEFINED);
785 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, true, VALIDATION_ERROR_24404601,
786 VALIDATION_ERROR_24404607);
787 lock.unlock();
788 if (skip) {
789 return;
790 }
791 // A DescriptorPool's descriptor sets are implicitly deleted when the pool is deleted.
792 // Remove this pool's descriptor sets from our descriptorSet map.
793 lock.lock();
794 std::unordered_map<uint64_t, OBJTRACK_NODE *>::iterator itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
795 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
796 OBJTRACK_NODE *pNode = (*itr).second;
797 auto del_itr = itr++;
798 if (pNode->parent_object == HandleToUint64(descriptorPool)) {
799 DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
800 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
801 }
802 }
803 DestroyObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, pAllocator, VALIDATION_ERROR_24400260,
804 VALIDATION_ERROR_24400262);
805 lock.unlock();
806 get_dispatch_table(ot_device_table_map, device)->DestroyDescriptorPool(device, descriptorPool, pAllocator);
807}
808
809VKAPI_ATTR void VKAPI_CALL DestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) {
810 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
811 bool skip = false;
812 std::unique_lock<std::mutex> lock(global_lock);
813 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24005601, VALIDATION_ERROR_UNDEFINED);
814 skip |= ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, true, VALIDATION_ERROR_24002801,
815 VALIDATION_ERROR_24002807);
816 lock.unlock();
817 if (skip) {
818 return;
819 }
820 lock.lock();
821 // A CommandPool's command buffers are implicitly deleted when the pool is deleted.
822 // Remove this pool's cmdBuffers from our cmd buffer map.
823 auto itr = device_data->object_map[kVulkanObjectTypeCommandBuffer].begin();
824 auto del_itr = itr;
825 while (itr != device_data->object_map[kVulkanObjectTypeCommandBuffer].end()) {
826 OBJTRACK_NODE *pNode = (*itr).second;
827 del_itr = itr++;
828 if (pNode->parent_object == HandleToUint64(commandPool)) {
829 skip |= ValidateCommandBuffer(device, commandPool, reinterpret_cast<VkCommandBuffer>((*del_itr).first));
830 DestroyObject(device, reinterpret_cast<VkCommandBuffer>((*del_itr).first), kVulkanObjectTypeCommandBuffer, nullptr,
831 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
832 }
833 }
834 DestroyObject(device, commandPool, kVulkanObjectTypeCommandPool, pAllocator, VALIDATION_ERROR_24000054,
835 VALIDATION_ERROR_24000056);
836 lock.unlock();
837 get_dispatch_table(ot_device_table_map, device)->DestroyCommandPool(device, commandPool, pAllocator);
838}
839
840VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
841 uint32_t *pQueueFamilyPropertyCount,
842 VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
843 bool skip = false;
844 {
845 std::lock_guard<std::mutex> lock(global_lock);
846 skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_UNDEFINED,
847 VALIDATION_ERROR_UNDEFINED);
848 }
849 if (skip) {
850 return;
851 }
852 get_dispatch_table(ot_instance_table_map, physicalDevice)
853 ->GetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
854 std::lock_guard<std::mutex> lock(global_lock);
855 if (pQueueFamilyProperties != NULL) {
856 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
857 if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
858 instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
859 }
860 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
861 instance_data->queue_family_properties[i] = pQueueFamilyProperties[i].queueFamilyProperties;
862 }
863 }
864}
865
866VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectNameEXT(VkDevice device, VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
867 bool skip = VK_FALSE;
868 std::unique_lock<std::mutex> lock(global_lock);
869 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
870 if (pNameInfo->pObjectName) {
871 dev_data->report_data->debugObjectNameMap->insert(
872 std::make_pair<uint64_t, std::string>((uint64_t &&) pNameInfo->object, pNameInfo->pObjectName));
873 } else {
874 dev_data->report_data->debugObjectNameMap->erase(pNameInfo->object);
875 }
876 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_23605601, VALIDATION_ERROR_UNDEFINED);
877 lock.unlock();
878 if (skip) {
879 return VK_ERROR_VALIDATION_FAILED_EXT;
880 }
881 VkResult result = dev_data->dispatch_table.DebugMarkerSetObjectNameEXT(device, pNameInfo);
882 return result;
883}
884
885VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
886 assert(instance);
887
888 if (get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr == NULL) {
889 return NULL;
890 }
891 return get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr(instance, funcName);
892}
893
894VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
895 const auto item = name_to_funcptr_map.find(funcName);
896 if (item != name_to_funcptr_map.end()) {
897 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
898 }
899
900 auto table = get_dispatch_table(ot_device_table_map, device);
901 if (!table->GetDeviceProcAddr) return NULL;
902 return table->GetDeviceProcAddr(device, funcName);
903}
904
905VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
906 const auto item = name_to_funcptr_map.find(funcName);
907 if (item != name_to_funcptr_map.end()) {
908 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
909 }
910
911 auto table = get_dispatch_table(ot_instance_table_map, instance);
912 if (!table->GetInstanceProcAddr) return nullptr;
913 return table->GetInstanceProcAddr(instance, funcName);
914}
915
916} // namespace object_tracker
917
918VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
919 VkExtensionProperties *pProperties) {
920 return object_tracker::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
921}
922
923VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
924 VkLayerProperties *pProperties) {
925 return object_tracker::EnumerateInstanceLayerProperties(pCount, pProperties);
926}
927
928VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
929 VkLayerProperties *pProperties) {
930 // The layer command handles VK_NULL_HANDLE just fine internally
931 assert(physicalDevice == VK_NULL_HANDLE);
932 return object_tracker::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
933}
934
935VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
936 return object_tracker::GetDeviceProcAddr(dev, funcName);
937}
938
939VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
940 return object_tracker::GetInstanceProcAddr(instance, funcName);
941}
942
943VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
944 const char *pLayerName, uint32_t *pCount,
945 VkExtensionProperties *pProperties) {
946 // The layer command handles VK_NULL_HANDLE just fine internally
947 assert(physicalDevice == VK_NULL_HANDLE);
948 return object_tracker::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
949}
950
951VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
952 const char *funcName) {
953 return object_tracker::GetPhysicalDeviceProcAddr(instance, funcName);
954}
955
956VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
957 assert(pVersionStruct != NULL);
958 assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
959
960 // Fill in the function pointers if our version is at least capable of having the structure contain them.
961 if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
962 pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
963 pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
964 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
965 }
966
967 if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
968 object_tracker::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
969 } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
970 pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
971 }
972
973 return VK_SUCCESS;
974}