blob: 88b4d0f49d425af0874932bad330f3e423f9e784 [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
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
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600200 ObjTrackState *p_obj_node = NULL;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600201 auto queue_item = device_data->object_map[kVulkanObjectTypeQueue].find(HandleToUint64(vkObj));
202 if (queue_item == device_data->object_map[kVulkanObjectTypeQueue].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600203 p_obj_node = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600204 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
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600222 ObjTrackState *pNewObjNode = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600223 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();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600233 ObjTrackState *object_info = item->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600234 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();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600263 ObjTrackState *pNode = iit->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600264
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();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600276 ObjTrackState *pNode = iit->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600277
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
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600285 ReportUndestroyedObjects(device, VALIDATION_ERROR_258004ea);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600286 }
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
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600322 ReportUndestroyedObjects(device, VALIDATION_ERROR_24a002f4);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600323
324 // Clean up Queue's MemRef Linked Lists
325 DestroyQueueDataStructures(device);
326
327 lock.unlock();
328
329 dispatch_key key = get_dispatch_key(device);
330 VkLayerDispatchTable *pDisp = get_dispatch_table(ot_device_table_map, device);
331 pDisp->DestroyDevice(device, pAllocator);
332 ot_device_table_map.erase(key);
333 delete pDisp;
334
335 FreeLayerDataPtr(key, layer_data_map);
336}
337
Mark Lobodzinski439645a2017-07-19 15:18:15 -0600338VKAPI_ATTR void VKAPI_CALL GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue *pQueue) {
339 std::unique_lock<std::mutex> lock(global_lock);
340 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_29605601, VALIDATION_ERROR_UNDEFINED);
341 lock.unlock();
342
343 get_dispatch_table(ot_device_table_map, device)->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
344
345 lock.lock();
346 CreateQueue(device, *pQueue);
347 AddQueueInfo(device, queueFamilyIndex, *pQueue);
348}
349
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600350VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
351 const VkWriteDescriptorSet *pDescriptorWrites, uint32_t descriptorCopyCount,
352 const VkCopyDescriptorSet *pDescriptorCopies) {
353 bool skip = false;
354 {
355 std::lock_guard<std::mutex> lock(global_lock);
356 skip |=
357 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_33c05601, VALIDATION_ERROR_UNDEFINED);
358 if (pDescriptorCopies) {
359 for (uint32_t idx0 = 0; idx0 < descriptorCopyCount; ++idx0) {
360 if (pDescriptorCopies[idx0].dstSet) {
361 skip |= ValidateObject(device, pDescriptorCopies[idx0].dstSet, kVulkanObjectTypeDescriptorSet, false,
362 VALIDATION_ERROR_03207601, VALIDATION_ERROR_03200009);
363 }
364 if (pDescriptorCopies[idx0].srcSet) {
365 skip |= ValidateObject(device, pDescriptorCopies[idx0].srcSet, kVulkanObjectTypeDescriptorSet, false,
366 VALIDATION_ERROR_0322d201, VALIDATION_ERROR_03200009);
367 }
368 }
369 }
370 if (pDescriptorWrites) {
371 for (uint32_t idx1 = 0; idx1 < descriptorWriteCount; ++idx1) {
372 if (pDescriptorWrites[idx1].dstSet) {
373 skip |= ValidateObject(device, pDescriptorWrites[idx1].dstSet, kVulkanObjectTypeDescriptorSet, false,
374 VALIDATION_ERROR_15c00280, VALIDATION_ERROR_15c00009);
375 }
376 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||
377 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) {
378 for (uint32_t idx2 = 0; idx2 < pDescriptorWrites[idx1].descriptorCount; ++idx2) {
379 skip |= ValidateObject(device, pDescriptorWrites[idx1].pTexelBufferView[idx2], kVulkanObjectTypeBufferView,
380 false, VALIDATION_ERROR_15c00286, VALIDATION_ERROR_15c00009);
381 }
382 }
383 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||
384 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) ||
385 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ||
386 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
387 for (uint32_t idx3 = 0; idx3 < pDescriptorWrites[idx1].descriptorCount; ++idx3) {
388 skip |=
389 ValidateObject(device, pDescriptorWrites[idx1].pImageInfo[idx3].imageView, kVulkanObjectTypeImageView,
390 false, VALIDATION_ERROR_15c0028c, VALIDATION_ERROR_04600009);
391 }
392 }
393 if ((pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
394 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||
395 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
396 (pDescriptorWrites[idx1].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
397 for (uint32_t idx4 = 0; idx4 < pDescriptorWrites[idx1].descriptorCount; ++idx4) {
398 if (pDescriptorWrites[idx1].pBufferInfo[idx4].buffer) {
399 skip |=
400 ValidateObject(device, pDescriptorWrites[idx1].pBufferInfo[idx4].buffer, kVulkanObjectTypeBuffer,
401 false, VALIDATION_ERROR_04401a01, VALIDATION_ERROR_UNDEFINED);
402 }
403 }
404 }
405 }
406 }
407 }
408 if (skip) {
409 return;
410 }
411 get_dispatch_table(ot_device_table_map, device)
412 ->UpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);
413}
414
Mark Lobodzinski2d26c5f2017-07-19 12:37:04 -0600415VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
416 const VkComputePipelineCreateInfo *pCreateInfos,
417 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
418 bool skip = VK_FALSE;
419 std::unique_lock<std::mutex> lock(global_lock);
420 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_1f205601, VALIDATION_ERROR_UNDEFINED);
421 if (pCreateInfos) {
422 for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) {
423 if (pCreateInfos[idx0].basePipelineHandle) {
424 skip |= ValidateObject(device, pCreateInfos[idx0].basePipelineHandle, kVulkanObjectTypePipeline, true,
425 VALIDATION_ERROR_03000572, VALIDATION_ERROR_03000009);
426 }
427 if (pCreateInfos[idx0].layout) {
428 skip |= ValidateObject(device, pCreateInfos[idx0].layout, kVulkanObjectTypePipelineLayout, false,
429 VALIDATION_ERROR_0300be01, VALIDATION_ERROR_03000009);
430 }
431 if (pCreateInfos[idx0].stage.module) {
432 skip |= ValidateObject(device, pCreateInfos[idx0].stage.module, kVulkanObjectTypeShaderModule, false,
433 VALIDATION_ERROR_1060d201, VALIDATION_ERROR_UNDEFINED);
434 }
435 }
436 }
437 if (pipelineCache) {
438 skip |= ValidateObject(device, pipelineCache, kVulkanObjectTypePipelineCache, true, VALIDATION_ERROR_1f228001,
439 VALIDATION_ERROR_1f228007);
440 }
441 lock.unlock();
442 if (skip) {
443 for (uint32_t i = 0; i < createInfoCount; i++) {
444 pPipelines[i] = VK_NULL_HANDLE;
445 }
446 return VK_ERROR_VALIDATION_FAILED_EXT;
447 }
448 VkResult result = get_dispatch_table(ot_device_table_map, device)
449 ->CreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator, pPipelines);
450 lock.lock();
451 for (uint32_t idx1 = 0; idx1 < createInfoCount; ++idx1) {
452 if (pPipelines[idx1] != VK_NULL_HANDLE) {
453 CreateObject(device, pPipelines[idx1], kVulkanObjectTypePipeline, pAllocator);
454 }
455 }
456 lock.unlock();
457 return result;
458}
459
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600460VKAPI_ATTR VkResult VKAPI_CALL ResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
461 VkDescriptorPoolResetFlags flags) {
462 bool skip = false;
463 std::unique_lock<std::mutex> lock(global_lock);
464 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
465 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_32a05601, VALIDATION_ERROR_UNDEFINED);
466 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_32a04601,
467 VALIDATION_ERROR_32a04607);
468 if (skip) {
469 return VK_ERROR_VALIDATION_FAILED_EXT;
470 }
471 // A DescriptorPool's descriptor sets are implicitly deleted when the pool is reset.
472 // Remove this pool's descriptor sets from our descriptorSet map.
473 auto itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
474 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600475 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600476 auto del_itr = itr++;
477 if (pNode->parent_object == HandleToUint64(descriptorPool)) {
478 DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
479 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
480 }
481 }
482 lock.unlock();
483 VkResult result = get_dispatch_table(ot_device_table_map, device)->ResetDescriptorPool(device, descriptorPool, flags);
484 return result;
485}
486
487VKAPI_ATTR VkResult VKAPI_CALL BeginCommandBuffer(VkCommandBuffer command_buffer, const VkCommandBufferBeginInfo *begin_info) {
488 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(command_buffer), layer_data_map);
489 bool skip = false;
490 {
491 std::lock_guard<std::mutex> lock(global_lock);
492 skip |= ValidateObject(command_buffer, command_buffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_16e02401,
493 VALIDATION_ERROR_UNDEFINED);
494 if (begin_info) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600495 ObjTrackState *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600496 if ((begin_info->pInheritanceInfo) && (pNode->status & OBJSTATUS_COMMAND_BUFFER_SECONDARY) &&
497 (begin_info->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
498 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->framebuffer, kVulkanObjectTypeFramebuffer,
499 true, VALIDATION_ERROR_0280006e, VALIDATION_ERROR_02a00009);
500 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->renderPass, kVulkanObjectTypeRenderPass, false,
501 VALIDATION_ERROR_0280006a, VALIDATION_ERROR_02a00009);
502 }
503 }
504 }
505 if (skip) {
506 return VK_ERROR_VALIDATION_FAILED_EXT;
507 }
508 VkResult result = get_dispatch_table(ot_device_table_map, command_buffer)->BeginCommandBuffer(command_buffer, begin_info);
509 return result;
510}
511
512VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(VkInstance instance,
513 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
514 const VkAllocationCallbacks *pAllocator,
515 VkDebugReportCallbackEXT *pCallback) {
516 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
517 VkResult result = pInstanceTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);
518 if (VK_SUCCESS == result) {
519 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
520 result = layer_create_msg_callback(instance_data->report_data, false, pCreateInfo, pAllocator, pCallback);
521 CreateObject(instance, *pCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator);
522 }
523 return result;
524}
525
526VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback,
527 const VkAllocationCallbacks *pAllocator) {
528 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
529 pInstanceTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
530 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
531 layer_destroy_msg_callback(instance_data->report_data, msgCallback, pAllocator);
532 DestroyObject(instance, msgCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator, VALIDATION_ERROR_242009b4,
533 VALIDATION_ERROR_242009b6);
534}
535
536VKAPI_ATTR void VKAPI_CALL DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
537 VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
538 int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
539 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
540 pInstanceTable->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
541}
542
543static const VkExtensionProperties instance_extensions[] = {{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION}};
544
545static const VkLayerProperties globalLayerProps = {"VK_LAYER_LUNARG_object_tracker",
546 VK_LAYER_API_VERSION, // specVersion
547 1, // implementationVersion
548 "LunarG Validation Layer"};
549
550VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
551 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
552}
553
554VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
555 VkLayerProperties *pProperties) {
556 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
557}
558
559VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
560 VkExtensionProperties *pProperties) {
561 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
562 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
563
564 return VK_ERROR_LAYER_NOT_PRESENT;
565}
566
567VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
568 uint32_t *pCount, VkExtensionProperties *pProperties) {
569 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
570 return util_GetExtensionProperties(0, nullptr, pCount, pProperties);
571
572 assert(physicalDevice);
573 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(ot_instance_table_map, physicalDevice);
574 return pTable->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
575}
576
577VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
578 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
579 std::lock_guard<std::mutex> lock(global_lock);
580 bool skip = ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_1fc27a01,
581 VALIDATION_ERROR_UNDEFINED);
582 if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
583
584 layer_data *phy_dev_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
585 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
586
587 assert(chain_info->u.pLayerInfo);
588 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
589 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
590 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(phy_dev_data->instance, "vkCreateDevice");
591 if (fpCreateDevice == NULL) {
592 return VK_ERROR_INITIALIZATION_FAILED;
593 }
594
595 // Advance the link info for the next element on the chain
596 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
597
598 VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
599 if (result != VK_SUCCESS) {
600 return result;
601 }
602
603 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
604 device_data->report_data = layer_debug_report_create_device(phy_dev_data->report_data, *pDevice);
605 layer_init_device_dispatch_table(*pDevice, &device_data->dispatch_table, fpGetDeviceProcAddr);
606
607 // Add link back to physDev
608 device_data->physical_device = physicalDevice;
609
610 initDeviceTable(*pDevice, fpGetDeviceProcAddr, ot_device_table_map);
611
612 CreateObject(*pDevice, *pDevice, kVulkanObjectTypeDevice, pAllocator);
613
614 return result;
615}
616
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600617VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount,
618 VkImage *pSwapchainImages) {
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600619 bool skip = false;
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600620 std::unique_lock<std::mutex> lock(global_lock);
621 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_30805601, VALIDATION_ERROR_UNDEFINED);
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600622 skip |= ValidateObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, false, VALIDATION_ERROR_3082f001,
623 VALIDATION_ERROR_UNDEFINED);
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600624 lock.unlock();
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600625 if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
626
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600627 VkResult result = get_dispatch_table(ot_device_table_map, device)
628 ->GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
629 if (pSwapchainImages != NULL) {
630 lock.lock();
631 for (uint32_t i = 0; i < *pSwapchainImageCount; i++) {
632 CreateSwapchainImageObject(device, pSwapchainImages[i], swapchain);
633 }
634 lock.unlock();
635 }
636 return result;
637}
638
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600639VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
640 uint32_t *pQueueFamilyPropertyCount,
641 VkQueueFamilyProperties *pQueueFamilyProperties) {
642 bool skip = false;
643 {
644 std::lock_guard<std::mutex> lock(global_lock);
645 skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_2da27a01,
646 VALIDATION_ERROR_UNDEFINED);
647 }
648 if (skip) {
649 return;
650 }
651 get_dispatch_table(ot_instance_table_map, physicalDevice)
652 ->GetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
653 std::lock_guard<std::mutex> lock(global_lock);
654 if (pQueueFamilyProperties != NULL) {
655 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
656 if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
657 instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
658 }
659 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
660 instance_data->queue_family_properties[i] = pQueueFamilyProperties[i];
661 }
662 }
663}
664
665VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
666 VkInstance *pInstance) {
667 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
668
669 assert(chain_info->u.pLayerInfo);
670 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
671 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
672 if (fpCreateInstance == NULL) {
673 return VK_ERROR_INITIALIZATION_FAILED;
674 }
675
676 // Advance the link info for the next element on the chain
677 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
678
679 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
680 if (result != VK_SUCCESS) {
681 return result;
682 }
683
684 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(*pInstance), layer_data_map);
685 instance_data->instance = *pInstance;
686 initInstanceTable(*pInstance, fpGetInstanceProcAddr, ot_instance_table_map);
687 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, *pInstance);
688
689 // Look for one or more debug report create info structures, and copy the
690 // callback(s) for each one found (for use by vkDestroyInstance)
691 layer_copy_tmp_callbacks(pCreateInfo->pNext, &instance_data->num_tmp_callbacks, &instance_data->tmp_dbg_create_infos,
692 &instance_data->tmp_callbacks);
693
694 instance_data->report_data = debug_report_create_instance(pInstanceTable, *pInstance, pCreateInfo->enabledExtensionCount,
695 pCreateInfo->ppEnabledExtensionNames);
696
697 InitObjectTracker(instance_data, pAllocator);
698
699 CreateObject(*pInstance, *pInstance, kVulkanObjectTypeInstance, pAllocator);
700
701 return result;
702}
703
704VKAPI_ATTR VkResult VKAPI_CALL EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
705 VkPhysicalDevice *pPhysicalDevices) {
706 bool skip = VK_FALSE;
707 std::unique_lock<std::mutex> lock(global_lock);
708 skip |=
709 ValidateObject(instance, instance, kVulkanObjectTypeInstance, false, VALIDATION_ERROR_2800bc01, VALIDATION_ERROR_UNDEFINED);
710 lock.unlock();
711 if (skip) {
712 return VK_ERROR_VALIDATION_FAILED_EXT;
713 }
714 VkResult result = get_dispatch_table(ot_instance_table_map, instance)
715 ->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
716 lock.lock();
717 if (result == VK_SUCCESS) {
718 if (pPhysicalDevices) {
719 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
720 CreateObject(instance, pPhysicalDevices[i], kVulkanObjectTypePhysicalDevice, nullptr);
721 }
722 }
723 }
724 lock.unlock();
725 return result;
726}
727
728VKAPI_ATTR VkResult VKAPI_CALL AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo,
729 VkCommandBuffer *pCommandBuffers) {
730 bool skip = VK_FALSE;
731 std::unique_lock<std::mutex> lock(global_lock);
732 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16805601, VALIDATION_ERROR_UNDEFINED);
733 skip |= ValidateObject(device, pAllocateInfo->commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_02602801,
734 VALIDATION_ERROR_UNDEFINED);
735 lock.unlock();
736
737 if (skip) {
738 return VK_ERROR_VALIDATION_FAILED_EXT;
739 }
740
741 VkResult result =
742 get_dispatch_table(ot_device_table_map, device)->AllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
743
744 lock.lock();
745 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) {
746 AllocateCommandBuffer(device, pAllocateInfo->commandPool, pCommandBuffers[i], pAllocateInfo->level);
747 }
748 lock.unlock();
749
750 return result;
751}
752
753VKAPI_ATTR VkResult VKAPI_CALL AllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo,
754 VkDescriptorSet *pDescriptorSets) {
755 bool skip = VK_FALSE;
756 std::unique_lock<std::mutex> lock(global_lock);
757 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16a05601, VALIDATION_ERROR_UNDEFINED);
758 skip |= ValidateObject(device, pAllocateInfo->descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_04c04601,
759 VALIDATION_ERROR_04c00009);
760 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
761 skip |= ValidateObject(device, pAllocateInfo->pSetLayouts[i], kVulkanObjectTypeDescriptorSetLayout, false,
762 VALIDATION_ERROR_04c22c01, VALIDATION_ERROR_04c00009);
763 }
764 lock.unlock();
765 if (skip) {
766 return VK_ERROR_VALIDATION_FAILED_EXT;
767 }
768
769 VkResult result =
770 get_dispatch_table(ot_device_table_map, device)->AllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets);
771
772 if (VK_SUCCESS == result) {
773 lock.lock();
774 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
775 AllocateDescriptorSet(device, pAllocateInfo->descriptorPool, pDescriptorSets[i]);
776 }
777 lock.unlock();
778 }
779
780 return result;
781}
782
783VKAPI_ATTR void VKAPI_CALL FreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
784 const VkCommandBuffer *pCommandBuffers) {
785 bool skip = false;
786 std::unique_lock<std::mutex> lock(global_lock);
787 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28405601, VALIDATION_ERROR_UNDEFINED);
788 ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_28402801, VALIDATION_ERROR_28402807);
789 for (uint32_t i = 0; i < commandBufferCount; i++) {
790 if (pCommandBuffers[i] != VK_NULL_HANDLE) {
791 skip |= ValidateCommandBuffer(device, commandPool, pCommandBuffers[i]);
792 }
793 }
794
795 for (uint32_t i = 0; i < commandBufferCount; i++) {
796 DestroyObject(device, pCommandBuffers[i], kVulkanObjectTypeCommandBuffer, nullptr, VALIDATION_ERROR_UNDEFINED,
797 VALIDATION_ERROR_UNDEFINED);
798 }
799
800 lock.unlock();
801 if (!skip) {
802 get_dispatch_table(ot_device_table_map, device)
803 ->FreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
804 }
805}
806
807VKAPI_ATTR void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
808 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
809 std::unique_lock<std::mutex> lock(global_lock);
810 // A swapchain's images are implicitly deleted when the swapchain is deleted.
811 // Remove this swapchain's images from our map of such images.
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600812 std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->swapchainImageMap.begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600813 while (itr != device_data->swapchainImageMap.end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600814 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600815 if (pNode->parent_object == HandleToUint64(swapchain)) {
816 delete pNode;
817 auto delete_item = itr++;
818 device_data->swapchainImageMap.erase(delete_item);
819 } else {
820 ++itr;
821 }
822 }
823 DestroyObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, pAllocator, VALIDATION_ERROR_26e00a06,
824 VALIDATION_ERROR_26e00a08);
825 lock.unlock();
826
827 get_dispatch_table(ot_device_table_map, device)->DestroySwapchainKHR(device, swapchain, pAllocator);
828}
829
830VKAPI_ATTR VkResult VKAPI_CALL FreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
831 const VkDescriptorSet *pDescriptorSets) {
832 bool skip = false;
833 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
834 std::unique_lock<std::mutex> lock(global_lock);
835 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28605601, VALIDATION_ERROR_UNDEFINED);
836 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_28604601,
837 VALIDATION_ERROR_28604607);
838 for (uint32_t i = 0; i < descriptorSetCount; i++) {
839 if (pDescriptorSets[i] != VK_NULL_HANDLE) {
840 skip |= ValidateDescriptorSet(device, descriptorPool, pDescriptorSets[i]);
841 }
842 }
843
844 for (uint32_t i = 0; i < descriptorSetCount; i++) {
845 DestroyObject(device, pDescriptorSets[i], kVulkanObjectTypeDescriptorSet, nullptr, VALIDATION_ERROR_UNDEFINED,
846 VALIDATION_ERROR_UNDEFINED);
847 }
848
849 lock.unlock();
850 if (!skip) {
851 result = get_dispatch_table(ot_device_table_map, device)
852 ->FreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets);
853 }
854 return result;
855}
856
857VKAPI_ATTR void VKAPI_CALL DestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
858 const VkAllocationCallbacks *pAllocator) {
859 bool skip = VK_FALSE;
860 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
861 std::unique_lock<std::mutex> lock(global_lock);
862 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24405601, VALIDATION_ERROR_UNDEFINED);
863 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, true, VALIDATION_ERROR_24404601,
864 VALIDATION_ERROR_24404607);
865 lock.unlock();
866 if (skip) {
867 return;
868 }
869 // A DescriptorPool's descriptor sets are implicitly deleted when the pool is deleted.
870 // Remove this pool's descriptor sets from our descriptorSet map.
871 lock.lock();
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600872 std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600873 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600874 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600875 auto del_itr = itr++;
876 if (pNode->parent_object == HandleToUint64(descriptorPool)) {
877 DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
878 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
879 }
880 }
881 DestroyObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, pAllocator, VALIDATION_ERROR_24400260,
882 VALIDATION_ERROR_24400262);
883 lock.unlock();
884 get_dispatch_table(ot_device_table_map, device)->DestroyDescriptorPool(device, descriptorPool, pAllocator);
885}
886
887VKAPI_ATTR void VKAPI_CALL DestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) {
888 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
889 bool skip = false;
890 std::unique_lock<std::mutex> lock(global_lock);
891 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24005601, VALIDATION_ERROR_UNDEFINED);
892 skip |= ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, true, VALIDATION_ERROR_24002801,
893 VALIDATION_ERROR_24002807);
894 lock.unlock();
895 if (skip) {
896 return;
897 }
898 lock.lock();
899 // A CommandPool's command buffers are implicitly deleted when the pool is deleted.
900 // Remove this pool's cmdBuffers from our cmd buffer map.
901 auto itr = device_data->object_map[kVulkanObjectTypeCommandBuffer].begin();
902 auto del_itr = itr;
903 while (itr != device_data->object_map[kVulkanObjectTypeCommandBuffer].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600904 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600905 del_itr = itr++;
906 if (pNode->parent_object == HandleToUint64(commandPool)) {
907 skip |= ValidateCommandBuffer(device, commandPool, reinterpret_cast<VkCommandBuffer>((*del_itr).first));
908 DestroyObject(device, reinterpret_cast<VkCommandBuffer>((*del_itr).first), kVulkanObjectTypeCommandBuffer, nullptr,
909 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
910 }
911 }
912 DestroyObject(device, commandPool, kVulkanObjectTypeCommandPool, pAllocator, VALIDATION_ERROR_24000054,
913 VALIDATION_ERROR_24000056);
914 lock.unlock();
915 get_dispatch_table(ot_device_table_map, device)->DestroyCommandPool(device, commandPool, pAllocator);
916}
917
918VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
919 uint32_t *pQueueFamilyPropertyCount,
920 VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
921 bool skip = false;
922 {
923 std::lock_guard<std::mutex> lock(global_lock);
924 skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_UNDEFINED,
925 VALIDATION_ERROR_UNDEFINED);
926 }
927 if (skip) {
928 return;
929 }
930 get_dispatch_table(ot_instance_table_map, physicalDevice)
931 ->GetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
932 std::lock_guard<std::mutex> lock(global_lock);
933 if (pQueueFamilyProperties != NULL) {
934 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
935 if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
936 instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
937 }
938 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
939 instance_data->queue_family_properties[i] = pQueueFamilyProperties[i].queueFamilyProperties;
940 }
941 }
942}
943
Mark Lobodzinskidfe5e172017-07-19 13:03:22 -0600944VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectNameEXT(VkDevice device, const VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600945 bool skip = VK_FALSE;
946 std::unique_lock<std::mutex> lock(global_lock);
947 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
948 if (pNameInfo->pObjectName) {
949 dev_data->report_data->debugObjectNameMap->insert(
950 std::make_pair<uint64_t, std::string>((uint64_t &&) pNameInfo->object, pNameInfo->pObjectName));
951 } else {
952 dev_data->report_data->debugObjectNameMap->erase(pNameInfo->object);
953 }
954 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_23605601, VALIDATION_ERROR_UNDEFINED);
955 lock.unlock();
956 if (skip) {
957 return VK_ERROR_VALIDATION_FAILED_EXT;
958 }
959 VkResult result = dev_data->dispatch_table.DebugMarkerSetObjectNameEXT(device, pNameInfo);
960 return result;
961}
962
963VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
964 assert(instance);
965
966 if (get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr == NULL) {
967 return NULL;
968 }
969 return get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr(instance, funcName);
970}
971
972VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
973 const auto item = name_to_funcptr_map.find(funcName);
974 if (item != name_to_funcptr_map.end()) {
975 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
976 }
977
978 auto table = get_dispatch_table(ot_device_table_map, device);
979 if (!table->GetDeviceProcAddr) return NULL;
980 return table->GetDeviceProcAddr(device, funcName);
981}
982
983VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
984 const auto item = name_to_funcptr_map.find(funcName);
985 if (item != name_to_funcptr_map.end()) {
986 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
987 }
988
989 auto table = get_dispatch_table(ot_instance_table_map, instance);
990 if (!table->GetInstanceProcAddr) return nullptr;
991 return table->GetInstanceProcAddr(instance, funcName);
992}
993
994} // namespace object_tracker
995
996VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
997 VkExtensionProperties *pProperties) {
998 return object_tracker::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
999}
1000
1001VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
1002 VkLayerProperties *pProperties) {
1003 return object_tracker::EnumerateInstanceLayerProperties(pCount, pProperties);
1004}
1005
1006VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
1007 VkLayerProperties *pProperties) {
1008 // The layer command handles VK_NULL_HANDLE just fine internally
1009 assert(physicalDevice == VK_NULL_HANDLE);
1010 return object_tracker::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
1011}
1012
1013VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
1014 return object_tracker::GetDeviceProcAddr(dev, funcName);
1015}
1016
1017VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
1018 return object_tracker::GetInstanceProcAddr(instance, funcName);
1019}
1020
1021VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1022 const char *pLayerName, uint32_t *pCount,
1023 VkExtensionProperties *pProperties) {
1024 // The layer command handles VK_NULL_HANDLE just fine internally
1025 assert(physicalDevice == VK_NULL_HANDLE);
1026 return object_tracker::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
1027}
1028
1029VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1030 const char *funcName) {
1031 return object_tracker::GetPhysicalDeviceProcAddr(instance, funcName);
1032}
1033
1034VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
1035 assert(pVersionStruct != NULL);
1036 assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
1037
1038 // Fill in the function pointers if our version is at least capable of having the structure contain them.
1039 if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
1040 pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
1041 pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
1042 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
1043 }
1044
1045 if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1046 object_tracker::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
1047 } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1048 pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
1049 }
1050
1051 return VK_SUCCESS;
1052}