blob: 6cad036c5e1f8f896962825ba7ef5d465def4283 [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
Chris Forbes2c600e92017-10-20 11:13:20 -0700193template<typename DispObj>
194static bool ValidateDescriptorWrite(DispObj disp, VkWriteDescriptorSet const *desc, bool isPush) {
195 bool skip = false;
196
197 if (!isPush && desc->dstSet) {
198 skip |= ValidateObject(disp, desc->dstSet, kVulkanObjectTypeDescriptorSet, false,
199 VALIDATION_ERROR_15c00280, VALIDATION_ERROR_15c00009);
200 }
201
202 if ((desc->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||
203 (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) {
204 for (uint32_t idx2 = 0; idx2 < desc->descriptorCount; ++idx2) {
205 skip |= ValidateObject(disp, desc->pTexelBufferView[idx2], kVulkanObjectTypeBufferView,
206 false, VALIDATION_ERROR_15c00286, VALIDATION_ERROR_15c00009);
207 }
208 }
209
210 if ((desc->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||
211 (desc->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) ||
212 (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ||
213 (desc->descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
214 for (uint32_t idx3 = 0; idx3 < desc->descriptorCount; ++idx3) {
215 skip |=
216 ValidateObject(disp, desc->pImageInfo[idx3].imageView, kVulkanObjectTypeImageView,
217 false, VALIDATION_ERROR_15c0028c, VALIDATION_ERROR_04600009);
218 }
219 }
220
221 if ((desc->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
222 (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||
223 (desc->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
224 (desc->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
225 for (uint32_t idx4 = 0; idx4 < desc->descriptorCount; ++idx4) {
226 if (desc->pBufferInfo[idx4].buffer) {
227 skip |=
228 ValidateObject(disp, desc->pBufferInfo[idx4].buffer, kVulkanObjectTypeBuffer,
229 false, VALIDATION_ERROR_04401a01, VALIDATION_ERROR_UNDEFINED);
230 }
231 }
232 }
233
234 return skip;
235}
236
Tony Barbour2fd0c2c2017-08-08 12:51:33 -0600237VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSetKHR(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
238 VkPipelineLayout layout, uint32_t set, uint32_t descriptorWriteCount,
239 const VkWriteDescriptorSet *pDescriptorWrites) {
240 bool skip = false;
241 {
242 std::lock_guard<std::mutex> lock(global_lock);
243 skip |= ValidateObject(commandBuffer, commandBuffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_1be02401,
244 VALIDATION_ERROR_1be00009);
245 skip |= ValidateObject(commandBuffer, layout, kVulkanObjectTypePipelineLayout, false, VALIDATION_ERROR_1be0be01,
246 VALIDATION_ERROR_1be00009);
247 if (pDescriptorWrites) {
248 for (uint32_t index0 = 0; index0 < descriptorWriteCount; ++index0) {
249 if (pDescriptorWrites[index0].pImageInfo) {
Chris Forbes2c600e92017-10-20 11:13:20 -0700250
Tony Barbour2fd0c2c2017-08-08 12:51:33 -0600251 for (uint32_t index1 = 0; index1 < pDescriptorWrites[index0].descriptorCount; ++index1) {
252 skip |=
253 ValidateObject(commandBuffer, pDescriptorWrites[index0].pImageInfo[index1].sampler,
254 kVulkanObjectTypeSampler, false, VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_04600009);
255 skip |= ValidateObject(commandBuffer, pDescriptorWrites[index0].pImageInfo[index1].imageView,
256 kVulkanObjectTypeImageView, false, VALIDATION_ERROR_UNDEFINED,
257 VALIDATION_ERROR_04600009);
258 }
259 }
260 if (pDescriptorWrites[index0].pBufferInfo) {
261 for (uint32_t index1 = 0; index1 < pDescriptorWrites[index0].descriptorCount; ++index1) {
262 skip |=
263 ValidateObject(commandBuffer, pDescriptorWrites[index0].pBufferInfo[index1].buffer,
264 kVulkanObjectTypeBuffer, false, VALIDATION_ERROR_04401a01, VALIDATION_ERROR_UNDEFINED);
265 }
266 }
267 if (pDescriptorWrites[index0].pTexelBufferView) {
268 for (uint32_t index1 = 0; index1 < pDescriptorWrites[index0].descriptorCount; ++index1) {
269 skip |= ValidateObject(commandBuffer, pDescriptorWrites[index0].pTexelBufferView[index1],
270 kVulkanObjectTypeBufferView, false, VALIDATION_ERROR_UNDEFINED,
271 VALIDATION_ERROR_15c00009);
272 }
273 }
274 }
275 }
276 }
277 if (skip) return;
278 get_dispatch_table(ot_device_table_map, commandBuffer)
279 ->CmdPushDescriptorSetKHR(commandBuffer, pipelineBindPoint, layout, set, descriptorWriteCount, pDescriptorWrites);
280}
281
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600282void CreateQueue(VkDevice device, VkQueue vkObj) {
283 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
284
285 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
286 HandleToUint64(vkObj), __LINE__, OBJTRACK_NONE, LayerName, "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64,
287 object_track_index++, "VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT", HandleToUint64(vkObj));
288
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600289 ObjTrackState *p_obj_node = NULL;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600290 auto queue_item = device_data->object_map[kVulkanObjectTypeQueue].find(HandleToUint64(vkObj));
291 if (queue_item == device_data->object_map[kVulkanObjectTypeQueue].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600292 p_obj_node = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600293 device_data->object_map[kVulkanObjectTypeQueue][HandleToUint64(vkObj)] = p_obj_node;
294 device_data->num_objects[kVulkanObjectTypeQueue]++;
295 device_data->num_total_objects++;
296 } else {
297 p_obj_node = queue_item->second;
298 }
299 p_obj_node->object_type = kVulkanObjectTypeQueue;
300 p_obj_node->status = OBJSTATUS_NONE;
301 p_obj_node->handle = HandleToUint64(vkObj);
302}
303
304void CreateSwapchainImageObject(VkDevice dispatchable_object, VkImage swapchain_image, VkSwapchainKHR swapchain) {
305 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(dispatchable_object), layer_data_map);
306 log_msg(device_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
307 HandleToUint64(swapchain_image), __LINE__, OBJTRACK_NONE, LayerName,
308 "OBJ[0x%" PRIxLEAST64 "] : CREATE %s object 0x%" PRIxLEAST64, object_track_index++, "SwapchainImage",
309 HandleToUint64(swapchain_image));
310
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600311 ObjTrackState *pNewObjNode = new ObjTrackState;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600312 pNewObjNode->object_type = kVulkanObjectTypeImage;
313 pNewObjNode->status = OBJSTATUS_NONE;
314 pNewObjNode->handle = HandleToUint64(swapchain_image);
315 pNewObjNode->parent_object = HandleToUint64(swapchain);
316 device_data->swapchainImageMap[HandleToUint64(swapchain_image)] = pNewObjNode;
317}
318
319void DeviceReportUndestroyedObjects(VkDevice device, VulkanObjectType object_type, enum UNIQUE_VALIDATION_ERROR_CODE error_code) {
320 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
321 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 -0600322 ObjTrackState *object_info = item->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600323 log_msg(device_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, get_debug_report_enum[object_type], object_info->handle,
324 __LINE__, error_code, LayerName,
325 "OBJ ERROR : For device 0x%" PRIxLEAST64 ", %s object 0x%" PRIxLEAST64 " has not been destroyed. %s",
326 HandleToUint64(device), object_string[object_type], object_info->handle, validation_error_map[error_code]);
327 item = device_data->object_map[object_type].erase(item);
328 }
329}
330
331VKAPI_ATTR void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
332 std::unique_lock<std::mutex> lock(global_lock);
333
334 dispatch_key key = get_dispatch_key(instance);
335 layer_data *instance_data = GetLayerDataPtr(key, layer_data_map);
336
337 // Enable the temporary callback(s) here to catch cleanup issues:
338 bool callback_setup = false;
339 if (instance_data->num_tmp_callbacks > 0) {
340 if (!layer_enable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks,
341 instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks)) {
342 callback_setup = true;
343 }
344 }
345
346 // TODO: The instance handle can not be validated here. The loader will likely have to validate it.
347 ValidateObject(instance, instance, kVulkanObjectTypeInstance, true, VALIDATION_ERROR_2580bc01, VALIDATION_ERROR_UNDEFINED);
348
349 // Destroy physical devices
350 for (auto iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
351 iit != instance_data->object_map[kVulkanObjectTypePhysicalDevice].end();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600352 ObjTrackState *pNode = iit->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600353
354 VkPhysicalDevice physical_device = reinterpret_cast<VkPhysicalDevice>(pNode->handle);
355 DestroyObject(instance, physical_device, kVulkanObjectTypePhysicalDevice, nullptr, VALIDATION_ERROR_UNDEFINED,
356 VALIDATION_ERROR_UNDEFINED);
357 iit = instance_data->object_map[kVulkanObjectTypePhysicalDevice].begin();
358 }
359
360 DestroyObject(instance, instance, kVulkanObjectTypeInstance, pAllocator, VALIDATION_ERROR_258004ec, VALIDATION_ERROR_258004ee);
361 // Report any remaining objects in LL
362
363 for (auto iit = instance_data->object_map[kVulkanObjectTypeDevice].begin();
364 iit != instance_data->object_map[kVulkanObjectTypeDevice].end();) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600365 ObjTrackState *pNode = iit->second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600366
367 VkDevice device = reinterpret_cast<VkDevice>(pNode->handle);
368 VkDebugReportObjectTypeEXT debug_object_type = get_debug_report_enum[pNode->object_type];
369
370 log_msg(instance_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, debug_object_type, pNode->handle, __LINE__,
371 OBJTRACK_OBJECT_LEAK, LayerName, "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.",
372 string_VkDebugReportObjectTypeEXT(debug_object_type), pNode->handle);
373
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600374 ReportUndestroyedObjects(device, VALIDATION_ERROR_258004ea);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600375 }
376 instance_data->object_map[kVulkanObjectTypeDevice].clear();
377
378 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
379 pInstanceTable->DestroyInstance(instance, pAllocator);
380
381 // Disable and cleanup the temporary callback(s):
382 if (callback_setup) {
383 layer_disable_tmp_callbacks(instance_data->report_data, instance_data->num_tmp_callbacks, instance_data->tmp_callbacks);
384 }
385 if (instance_data->num_tmp_callbacks > 0) {
386 layer_free_tmp_callbacks(instance_data->tmp_dbg_create_infos, instance_data->tmp_callbacks);
387 instance_data->num_tmp_callbacks = 0;
388 }
389
390 // Clean up logging callback, if any
391 while (instance_data->logging_callback.size() > 0) {
392 VkDebugReportCallbackEXT callback = instance_data->logging_callback.back();
393 layer_destroy_msg_callback(instance_data->report_data, callback, pAllocator);
394 instance_data->logging_callback.pop_back();
395 }
396
397 layer_debug_report_destroy_instance(instance_data->report_data);
398 FreeLayerDataPtr(key, layer_data_map);
399
400 lock.unlock();
401 ot_instance_table_map.erase(key);
402 delete pInstanceTable;
403}
404
405VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
406 std::unique_lock<std::mutex> lock(global_lock);
407 ValidateObject(device, device, kVulkanObjectTypeDevice, true, VALIDATION_ERROR_24a05601, VALIDATION_ERROR_UNDEFINED);
408 DestroyObject(device, device, kVulkanObjectTypeDevice, pAllocator, VALIDATION_ERROR_24a002f6, VALIDATION_ERROR_24a002f8);
409
410 // Report any remaining objects associated with this VkDevice object in LL
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600411 ReportUndestroyedObjects(device, VALIDATION_ERROR_24a002f4);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600412
413 // Clean up Queue's MemRef Linked Lists
414 DestroyQueueDataStructures(device);
415
416 lock.unlock();
417
418 dispatch_key key = get_dispatch_key(device);
419 VkLayerDispatchTable *pDisp = get_dispatch_table(ot_device_table_map, device);
420 pDisp->DestroyDevice(device, pAllocator);
421 ot_device_table_map.erase(key);
422 delete pDisp;
423
424 FreeLayerDataPtr(key, layer_data_map);
425}
426
Mark Lobodzinski439645a2017-07-19 15:18:15 -0600427VKAPI_ATTR void VKAPI_CALL GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue *pQueue) {
428 std::unique_lock<std::mutex> lock(global_lock);
429 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_29605601, VALIDATION_ERROR_UNDEFINED);
430 lock.unlock();
431
432 get_dispatch_table(ot_device_table_map, device)->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
433
434 lock.lock();
435 CreateQueue(device, *pQueue);
436 AddQueueInfo(device, queueFamilyIndex, *pQueue);
437}
438
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600439VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
440 const VkWriteDescriptorSet *pDescriptorWrites, uint32_t descriptorCopyCount,
441 const VkCopyDescriptorSet *pDescriptorCopies) {
442 bool skip = false;
443 {
444 std::lock_guard<std::mutex> lock(global_lock);
445 skip |=
446 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_33c05601, VALIDATION_ERROR_UNDEFINED);
447 if (pDescriptorCopies) {
448 for (uint32_t idx0 = 0; idx0 < descriptorCopyCount; ++idx0) {
449 if (pDescriptorCopies[idx0].dstSet) {
450 skip |= ValidateObject(device, pDescriptorCopies[idx0].dstSet, kVulkanObjectTypeDescriptorSet, false,
451 VALIDATION_ERROR_03207601, VALIDATION_ERROR_03200009);
452 }
453 if (pDescriptorCopies[idx0].srcSet) {
454 skip |= ValidateObject(device, pDescriptorCopies[idx0].srcSet, kVulkanObjectTypeDescriptorSet, false,
455 VALIDATION_ERROR_0322d201, VALIDATION_ERROR_03200009);
456 }
457 }
458 }
459 if (pDescriptorWrites) {
460 for (uint32_t idx1 = 0; idx1 < descriptorWriteCount; ++idx1) {
Chris Forbes2c600e92017-10-20 11:13:20 -0700461 skip |= ValidateDescriptorWrite(device, &pDescriptorWrites[idx1], false);
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600462 }
463 }
464 }
465 if (skip) {
466 return;
467 }
468 get_dispatch_table(ot_device_table_map, device)
469 ->UpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);
470}
471
Mark Lobodzinski2d26c5f2017-07-19 12:37:04 -0600472VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
473 const VkComputePipelineCreateInfo *pCreateInfos,
474 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
475 bool skip = VK_FALSE;
476 std::unique_lock<std::mutex> lock(global_lock);
477 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_1f205601, VALIDATION_ERROR_UNDEFINED);
478 if (pCreateInfos) {
479 for (uint32_t idx0 = 0; idx0 < createInfoCount; ++idx0) {
480 if (pCreateInfos[idx0].basePipelineHandle) {
481 skip |= ValidateObject(device, pCreateInfos[idx0].basePipelineHandle, kVulkanObjectTypePipeline, true,
482 VALIDATION_ERROR_03000572, VALIDATION_ERROR_03000009);
483 }
484 if (pCreateInfos[idx0].layout) {
485 skip |= ValidateObject(device, pCreateInfos[idx0].layout, kVulkanObjectTypePipelineLayout, false,
486 VALIDATION_ERROR_0300be01, VALIDATION_ERROR_03000009);
487 }
488 if (pCreateInfos[idx0].stage.module) {
489 skip |= ValidateObject(device, pCreateInfos[idx0].stage.module, kVulkanObjectTypeShaderModule, false,
490 VALIDATION_ERROR_1060d201, VALIDATION_ERROR_UNDEFINED);
491 }
492 }
493 }
494 if (pipelineCache) {
495 skip |= ValidateObject(device, pipelineCache, kVulkanObjectTypePipelineCache, true, VALIDATION_ERROR_1f228001,
496 VALIDATION_ERROR_1f228007);
497 }
498 lock.unlock();
499 if (skip) {
500 for (uint32_t i = 0; i < createInfoCount; i++) {
501 pPipelines[i] = VK_NULL_HANDLE;
502 }
503 return VK_ERROR_VALIDATION_FAILED_EXT;
504 }
505 VkResult result = get_dispatch_table(ot_device_table_map, device)
506 ->CreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator, pPipelines);
507 lock.lock();
508 for (uint32_t idx1 = 0; idx1 < createInfoCount; ++idx1) {
509 if (pPipelines[idx1] != VK_NULL_HANDLE) {
510 CreateObject(device, pPipelines[idx1], kVulkanObjectTypePipeline, pAllocator);
511 }
512 }
513 lock.unlock();
514 return result;
515}
516
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600517VKAPI_ATTR VkResult VKAPI_CALL ResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
518 VkDescriptorPoolResetFlags flags) {
519 bool skip = false;
520 std::unique_lock<std::mutex> lock(global_lock);
521 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
522 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_32a05601, VALIDATION_ERROR_UNDEFINED);
523 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_32a04601,
524 VALIDATION_ERROR_32a04607);
525 if (skip) {
526 return VK_ERROR_VALIDATION_FAILED_EXT;
527 }
528 // A DescriptorPool's descriptor sets are implicitly deleted when the pool is reset.
529 // Remove this pool's descriptor sets from our descriptorSet map.
530 auto itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
531 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600532 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600533 auto del_itr = itr++;
534 if (pNode->parent_object == HandleToUint64(descriptorPool)) {
535 DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
536 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
537 }
538 }
539 lock.unlock();
540 VkResult result = get_dispatch_table(ot_device_table_map, device)->ResetDescriptorPool(device, descriptorPool, flags);
541 return result;
542}
543
544VKAPI_ATTR VkResult VKAPI_CALL BeginCommandBuffer(VkCommandBuffer command_buffer, const VkCommandBufferBeginInfo *begin_info) {
545 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(command_buffer), layer_data_map);
546 bool skip = false;
547 {
548 std::lock_guard<std::mutex> lock(global_lock);
549 skip |= ValidateObject(command_buffer, command_buffer, kVulkanObjectTypeCommandBuffer, false, VALIDATION_ERROR_16e02401,
550 VALIDATION_ERROR_UNDEFINED);
551 if (begin_info) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600552 ObjTrackState *pNode = device_data->object_map[kVulkanObjectTypeCommandBuffer][HandleToUint64(command_buffer)];
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600553 if ((begin_info->pInheritanceInfo) && (pNode->status & OBJSTATUS_COMMAND_BUFFER_SECONDARY) &&
554 (begin_info->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
555 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->framebuffer, kVulkanObjectTypeFramebuffer,
556 true, VALIDATION_ERROR_0280006e, VALIDATION_ERROR_02a00009);
557 skip |= ValidateObject(command_buffer, begin_info->pInheritanceInfo->renderPass, kVulkanObjectTypeRenderPass, false,
558 VALIDATION_ERROR_0280006a, VALIDATION_ERROR_02a00009);
559 }
560 }
561 }
562 if (skip) {
563 return VK_ERROR_VALIDATION_FAILED_EXT;
564 }
565 VkResult result = get_dispatch_table(ot_device_table_map, command_buffer)->BeginCommandBuffer(command_buffer, begin_info);
566 return result;
567}
568
569VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(VkInstance instance,
570 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
571 const VkAllocationCallbacks *pAllocator,
572 VkDebugReportCallbackEXT *pCallback) {
573 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
574 VkResult result = pInstanceTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);
575 if (VK_SUCCESS == result) {
576 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
577 result = layer_create_msg_callback(instance_data->report_data, false, pCreateInfo, pAllocator, pCallback);
578 CreateObject(instance, *pCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator);
579 }
580 return result;
581}
582
583VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback,
584 const VkAllocationCallbacks *pAllocator) {
585 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
586 pInstanceTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
587 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(instance), layer_data_map);
588 layer_destroy_msg_callback(instance_data->report_data, msgCallback, pAllocator);
589 DestroyObject(instance, msgCallback, kVulkanObjectTypeDebugReportCallbackEXT, pAllocator, VALIDATION_ERROR_242009b4,
590 VALIDATION_ERROR_242009b6);
591}
592
593VKAPI_ATTR void VKAPI_CALL DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
594 VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
595 int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
596 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, instance);
597 pInstanceTable->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
598}
599
600static const VkExtensionProperties instance_extensions[] = {{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION}};
601
602static const VkLayerProperties globalLayerProps = {"VK_LAYER_LUNARG_object_tracker",
603 VK_LAYER_API_VERSION, // specVersion
604 1, // implementationVersion
605 "LunarG Validation Layer"};
606
607VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
608 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
609}
610
611VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
612 VkLayerProperties *pProperties) {
613 return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);
614}
615
616VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
617 VkExtensionProperties *pProperties) {
618 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
619 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
620
621 return VK_ERROR_LAYER_NOT_PRESENT;
622}
623
624VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
625 uint32_t *pCount, VkExtensionProperties *pProperties) {
626 if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))
627 return util_GetExtensionProperties(0, nullptr, pCount, pProperties);
628
629 assert(physicalDevice);
630 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(ot_instance_table_map, physicalDevice);
631 return pTable->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
632}
633
634VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
635 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
636 std::lock_guard<std::mutex> lock(global_lock);
637 bool skip = ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_1fc27a01,
638 VALIDATION_ERROR_UNDEFINED);
639 if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
640
641 layer_data *phy_dev_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
642 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
643
644 assert(chain_info->u.pLayerInfo);
645 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
646 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
647 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(phy_dev_data->instance, "vkCreateDevice");
648 if (fpCreateDevice == NULL) {
649 return VK_ERROR_INITIALIZATION_FAILED;
650 }
651
652 // Advance the link info for the next element on the chain
653 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
654
655 VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
656 if (result != VK_SUCCESS) {
657 return result;
658 }
659
660 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
661 device_data->report_data = layer_debug_report_create_device(phy_dev_data->report_data, *pDevice);
662 layer_init_device_dispatch_table(*pDevice, &device_data->dispatch_table, fpGetDeviceProcAddr);
663
664 // Add link back to physDev
665 device_data->physical_device = physicalDevice;
666
667 initDeviceTable(*pDevice, fpGetDeviceProcAddr, ot_device_table_map);
668
669 CreateObject(*pDevice, *pDevice, kVulkanObjectTypeDevice, pAllocator);
670
671 return result;
672}
673
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600674VKAPI_ATTR VkResult VKAPI_CALL GetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount,
675 VkImage *pSwapchainImages) {
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600676 bool skip = false;
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600677 std::unique_lock<std::mutex> lock(global_lock);
678 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_30805601, VALIDATION_ERROR_UNDEFINED);
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600679 skip |= ValidateObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, false, VALIDATION_ERROR_3082f001,
680 VALIDATION_ERROR_UNDEFINED);
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600681 lock.unlock();
Mark Lobodzinski09fa2d42017-07-21 10:16:53 -0600682 if (skip) return VK_ERROR_VALIDATION_FAILED_EXT;
683
Mark Lobodzinski216843a2017-07-21 13:23:13 -0600684 VkResult result = get_dispatch_table(ot_device_table_map, device)
685 ->GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
686 if (pSwapchainImages != NULL) {
687 lock.lock();
688 for (uint32_t i = 0; i < *pSwapchainImageCount; i++) {
689 CreateSwapchainImageObject(device, pSwapchainImages[i], swapchain);
690 }
691 lock.unlock();
692 }
693 return result;
694}
695
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600696VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
697 uint32_t *pQueueFamilyPropertyCount,
698 VkQueueFamilyProperties *pQueueFamilyProperties) {
699 bool skip = false;
700 {
701 std::lock_guard<std::mutex> lock(global_lock);
702 skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_2da27a01,
703 VALIDATION_ERROR_UNDEFINED);
704 }
705 if (skip) {
706 return;
707 }
708 get_dispatch_table(ot_instance_table_map, physicalDevice)
709 ->GetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
710 std::lock_guard<std::mutex> lock(global_lock);
711 if (pQueueFamilyProperties != NULL) {
712 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
713 if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
714 instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
715 }
716 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
717 instance_data->queue_family_properties[i] = pQueueFamilyProperties[i];
718 }
719 }
720}
721
722VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
723 VkInstance *pInstance) {
724 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
725
726 assert(chain_info->u.pLayerInfo);
727 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
728 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
729 if (fpCreateInstance == NULL) {
730 return VK_ERROR_INITIALIZATION_FAILED;
731 }
732
733 // Advance the link info for the next element on the chain
734 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
735
736 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
737 if (result != VK_SUCCESS) {
738 return result;
739 }
740
741 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(*pInstance), layer_data_map);
742 instance_data->instance = *pInstance;
743 initInstanceTable(*pInstance, fpGetInstanceProcAddr, ot_instance_table_map);
744 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ot_instance_table_map, *pInstance);
745
746 // Look for one or more debug report create info structures, and copy the
747 // callback(s) for each one found (for use by vkDestroyInstance)
748 layer_copy_tmp_callbacks(pCreateInfo->pNext, &instance_data->num_tmp_callbacks, &instance_data->tmp_dbg_create_infos,
749 &instance_data->tmp_callbacks);
750
751 instance_data->report_data = debug_report_create_instance(pInstanceTable, *pInstance, pCreateInfo->enabledExtensionCount,
752 pCreateInfo->ppEnabledExtensionNames);
753
754 InitObjectTracker(instance_data, pAllocator);
755
756 CreateObject(*pInstance, *pInstance, kVulkanObjectTypeInstance, pAllocator);
757
758 return result;
759}
760
761VKAPI_ATTR VkResult VKAPI_CALL EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
762 VkPhysicalDevice *pPhysicalDevices) {
763 bool skip = VK_FALSE;
764 std::unique_lock<std::mutex> lock(global_lock);
765 skip |=
766 ValidateObject(instance, instance, kVulkanObjectTypeInstance, false, VALIDATION_ERROR_2800bc01, VALIDATION_ERROR_UNDEFINED);
767 lock.unlock();
768 if (skip) {
769 return VK_ERROR_VALIDATION_FAILED_EXT;
770 }
771 VkResult result = get_dispatch_table(ot_instance_table_map, instance)
772 ->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
773 lock.lock();
774 if (result == VK_SUCCESS) {
775 if (pPhysicalDevices) {
776 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
777 CreateObject(instance, pPhysicalDevices[i], kVulkanObjectTypePhysicalDevice, nullptr);
778 }
779 }
780 }
781 lock.unlock();
782 return result;
783}
784
785VKAPI_ATTR VkResult VKAPI_CALL AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo,
786 VkCommandBuffer *pCommandBuffers) {
787 bool skip = VK_FALSE;
788 std::unique_lock<std::mutex> lock(global_lock);
789 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16805601, VALIDATION_ERROR_UNDEFINED);
790 skip |= ValidateObject(device, pAllocateInfo->commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_02602801,
791 VALIDATION_ERROR_UNDEFINED);
792 lock.unlock();
793
794 if (skip) {
795 return VK_ERROR_VALIDATION_FAILED_EXT;
796 }
797
798 VkResult result =
799 get_dispatch_table(ot_device_table_map, device)->AllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
800
801 lock.lock();
802 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) {
803 AllocateCommandBuffer(device, pAllocateInfo->commandPool, pCommandBuffers[i], pAllocateInfo->level);
804 }
805 lock.unlock();
806
807 return result;
808}
809
810VKAPI_ATTR VkResult VKAPI_CALL AllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo,
811 VkDescriptorSet *pDescriptorSets) {
812 bool skip = VK_FALSE;
813 std::unique_lock<std::mutex> lock(global_lock);
814 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_16a05601, VALIDATION_ERROR_UNDEFINED);
815 skip |= ValidateObject(device, pAllocateInfo->descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_04c04601,
816 VALIDATION_ERROR_04c00009);
817 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
818 skip |= ValidateObject(device, pAllocateInfo->pSetLayouts[i], kVulkanObjectTypeDescriptorSetLayout, false,
819 VALIDATION_ERROR_04c22c01, VALIDATION_ERROR_04c00009);
820 }
821 lock.unlock();
822 if (skip) {
823 return VK_ERROR_VALIDATION_FAILED_EXT;
824 }
825
826 VkResult result =
827 get_dispatch_table(ot_device_table_map, device)->AllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets);
828
829 if (VK_SUCCESS == result) {
830 lock.lock();
831 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
832 AllocateDescriptorSet(device, pAllocateInfo->descriptorPool, pDescriptorSets[i]);
833 }
834 lock.unlock();
835 }
836
837 return result;
838}
839
840VKAPI_ATTR void VKAPI_CALL FreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
841 const VkCommandBuffer *pCommandBuffers) {
842 bool skip = false;
843 std::unique_lock<std::mutex> lock(global_lock);
844 ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28405601, VALIDATION_ERROR_UNDEFINED);
845 ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, false, VALIDATION_ERROR_28402801, VALIDATION_ERROR_28402807);
846 for (uint32_t i = 0; i < commandBufferCount; i++) {
847 if (pCommandBuffers[i] != VK_NULL_HANDLE) {
848 skip |= ValidateCommandBuffer(device, commandPool, pCommandBuffers[i]);
849 }
850 }
851
852 for (uint32_t i = 0; i < commandBufferCount; i++) {
853 DestroyObject(device, pCommandBuffers[i], kVulkanObjectTypeCommandBuffer, nullptr, VALIDATION_ERROR_UNDEFINED,
854 VALIDATION_ERROR_UNDEFINED);
855 }
856
857 lock.unlock();
858 if (!skip) {
859 get_dispatch_table(ot_device_table_map, device)
860 ->FreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
861 }
862}
863
864VKAPI_ATTR void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
865 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
866 std::unique_lock<std::mutex> lock(global_lock);
867 // A swapchain's images are implicitly deleted when the swapchain is deleted.
868 // Remove this swapchain's images from our map of such images.
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600869 std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->swapchainImageMap.begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600870 while (itr != device_data->swapchainImageMap.end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600871 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600872 if (pNode->parent_object == HandleToUint64(swapchain)) {
873 delete pNode;
874 auto delete_item = itr++;
875 device_data->swapchainImageMap.erase(delete_item);
876 } else {
877 ++itr;
878 }
879 }
880 DestroyObject(device, swapchain, kVulkanObjectTypeSwapchainKHR, pAllocator, VALIDATION_ERROR_26e00a06,
881 VALIDATION_ERROR_26e00a08);
882 lock.unlock();
883
884 get_dispatch_table(ot_device_table_map, device)->DestroySwapchainKHR(device, swapchain, pAllocator);
885}
886
887VKAPI_ATTR VkResult VKAPI_CALL FreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
888 const VkDescriptorSet *pDescriptorSets) {
889 bool skip = false;
890 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
891 std::unique_lock<std::mutex> lock(global_lock);
892 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_28605601, VALIDATION_ERROR_UNDEFINED);
893 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, false, VALIDATION_ERROR_28604601,
894 VALIDATION_ERROR_28604607);
895 for (uint32_t i = 0; i < descriptorSetCount; i++) {
896 if (pDescriptorSets[i] != VK_NULL_HANDLE) {
897 skip |= ValidateDescriptorSet(device, descriptorPool, pDescriptorSets[i]);
898 }
899 }
900
901 for (uint32_t i = 0; i < descriptorSetCount; i++) {
902 DestroyObject(device, pDescriptorSets[i], kVulkanObjectTypeDescriptorSet, nullptr, VALIDATION_ERROR_UNDEFINED,
903 VALIDATION_ERROR_UNDEFINED);
904 }
905
906 lock.unlock();
907 if (!skip) {
908 result = get_dispatch_table(ot_device_table_map, device)
909 ->FreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets);
910 }
911 return result;
912}
913
914VKAPI_ATTR void VKAPI_CALL DestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
915 const VkAllocationCallbacks *pAllocator) {
916 bool skip = VK_FALSE;
917 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
918 std::unique_lock<std::mutex> lock(global_lock);
919 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24405601, VALIDATION_ERROR_UNDEFINED);
920 skip |= ValidateObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, true, VALIDATION_ERROR_24404601,
921 VALIDATION_ERROR_24404607);
922 lock.unlock();
923 if (skip) {
924 return;
925 }
926 // A DescriptorPool's descriptor sets are implicitly deleted when the pool is deleted.
927 // Remove this pool's descriptor sets from our descriptorSet map.
928 lock.lock();
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600929 std::unordered_map<uint64_t, ObjTrackState *>::iterator itr = device_data->object_map[kVulkanObjectTypeDescriptorSet].begin();
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600930 while (itr != device_data->object_map[kVulkanObjectTypeDescriptorSet].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600931 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600932 auto del_itr = itr++;
933 if (pNode->parent_object == HandleToUint64(descriptorPool)) {
934 DestroyObject(device, (VkDescriptorSet)((*del_itr).first), kVulkanObjectTypeDescriptorSet, nullptr,
935 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
936 }
937 }
938 DestroyObject(device, descriptorPool, kVulkanObjectTypeDescriptorPool, pAllocator, VALIDATION_ERROR_24400260,
939 VALIDATION_ERROR_24400262);
940 lock.unlock();
941 get_dispatch_table(ot_device_table_map, device)->DestroyDescriptorPool(device, descriptorPool, pAllocator);
942}
943
944VKAPI_ATTR void VKAPI_CALL DestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) {
945 layer_data *device_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
946 bool skip = false;
947 std::unique_lock<std::mutex> lock(global_lock);
948 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_24005601, VALIDATION_ERROR_UNDEFINED);
949 skip |= ValidateObject(device, commandPool, kVulkanObjectTypeCommandPool, true, VALIDATION_ERROR_24002801,
950 VALIDATION_ERROR_24002807);
951 lock.unlock();
952 if (skip) {
953 return;
954 }
955 lock.lock();
956 // A CommandPool's command buffers are implicitly deleted when the pool is deleted.
957 // Remove this pool's cmdBuffers from our cmd buffer map.
958 auto itr = device_data->object_map[kVulkanObjectTypeCommandBuffer].begin();
959 auto del_itr = itr;
960 while (itr != device_data->object_map[kVulkanObjectTypeCommandBuffer].end()) {
Mark Lobodzinskiefc64392017-07-18 13:15:47 -0600961 ObjTrackState *pNode = (*itr).second;
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -0600962 del_itr = itr++;
963 if (pNode->parent_object == HandleToUint64(commandPool)) {
964 skip |= ValidateCommandBuffer(device, commandPool, reinterpret_cast<VkCommandBuffer>((*del_itr).first));
965 DestroyObject(device, reinterpret_cast<VkCommandBuffer>((*del_itr).first), kVulkanObjectTypeCommandBuffer, nullptr,
966 VALIDATION_ERROR_UNDEFINED, VALIDATION_ERROR_UNDEFINED);
967 }
968 }
969 DestroyObject(device, commandPool, kVulkanObjectTypeCommandPool, pAllocator, VALIDATION_ERROR_24000054,
970 VALIDATION_ERROR_24000056);
971 lock.unlock();
972 get_dispatch_table(ot_device_table_map, device)->DestroyCommandPool(device, commandPool, pAllocator);
973}
974
975VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
976 uint32_t *pQueueFamilyPropertyCount,
977 VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
978 bool skip = false;
979 {
980 std::lock_guard<std::mutex> lock(global_lock);
981 skip |= ValidateObject(physicalDevice, physicalDevice, kVulkanObjectTypePhysicalDevice, false, VALIDATION_ERROR_UNDEFINED,
982 VALIDATION_ERROR_UNDEFINED);
983 }
984 if (skip) {
985 return;
986 }
987 get_dispatch_table(ot_instance_table_map, physicalDevice)
988 ->GetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
989 std::lock_guard<std::mutex> lock(global_lock);
990 if (pQueueFamilyProperties != NULL) {
991 layer_data *instance_data = GetLayerDataPtr(get_dispatch_key(physicalDevice), layer_data_map);
992 if (instance_data->queue_family_properties.size() < *pQueueFamilyPropertyCount) {
993 instance_data->queue_family_properties.resize(*pQueueFamilyPropertyCount);
994 }
995 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; i++) {
996 instance_data->queue_family_properties[i] = pQueueFamilyProperties[i].queueFamilyProperties;
997 }
998 }
999}
1000
Mark Lobodzinskidfe5e172017-07-19 13:03:22 -06001001VKAPI_ATTR VkResult VKAPI_CALL DebugMarkerSetObjectNameEXT(VkDevice device, const VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
Mark Lobodzinskib2de97f2017-07-06 15:28:11 -06001002 bool skip = VK_FALSE;
1003 std::unique_lock<std::mutex> lock(global_lock);
1004 layer_data *dev_data = GetLayerDataPtr(get_dispatch_key(device), layer_data_map);
1005 if (pNameInfo->pObjectName) {
1006 dev_data->report_data->debugObjectNameMap->insert(
1007 std::make_pair<uint64_t, std::string>((uint64_t &&) pNameInfo->object, pNameInfo->pObjectName));
1008 } else {
1009 dev_data->report_data->debugObjectNameMap->erase(pNameInfo->object);
1010 }
1011 skip |= ValidateObject(device, device, kVulkanObjectTypeDevice, false, VALIDATION_ERROR_23605601, VALIDATION_ERROR_UNDEFINED);
1012 lock.unlock();
1013 if (skip) {
1014 return VK_ERROR_VALIDATION_FAILED_EXT;
1015 }
1016 VkResult result = dev_data->dispatch_table.DebugMarkerSetObjectNameEXT(device, pNameInfo);
1017 return result;
1018}
1019
1020VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetPhysicalDeviceProcAddr(VkInstance instance, const char *funcName) {
1021 assert(instance);
1022
1023 if (get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr == NULL) {
1024 return NULL;
1025 }
1026 return get_dispatch_table(ot_instance_table_map, instance)->GetPhysicalDeviceProcAddr(instance, funcName);
1027}
1028
1029VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *funcName) {
1030 const auto item = name_to_funcptr_map.find(funcName);
1031 if (item != name_to_funcptr_map.end()) {
1032 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
1033 }
1034
1035 auto table = get_dispatch_table(ot_device_table_map, device);
1036 if (!table->GetDeviceProcAddr) return NULL;
1037 return table->GetDeviceProcAddr(device, funcName);
1038}
1039
1040VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *funcName) {
1041 const auto item = name_to_funcptr_map.find(funcName);
1042 if (item != name_to_funcptr_map.end()) {
1043 return reinterpret_cast<PFN_vkVoidFunction>(item->second);
1044 }
1045
1046 auto table = get_dispatch_table(ot_instance_table_map, instance);
1047 if (!table->GetInstanceProcAddr) return nullptr;
1048 return table->GetInstanceProcAddr(instance, funcName);
1049}
1050
1051} // namespace object_tracker
1052
1053VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount,
1054 VkExtensionProperties *pProperties) {
1055 return object_tracker::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);
1056}
1057
1058VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount,
1059 VkLayerProperties *pProperties) {
1060 return object_tracker::EnumerateInstanceLayerProperties(pCount, pProperties);
1061}
1062
1063VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
1064 VkLayerProperties *pProperties) {
1065 // The layer command handles VK_NULL_HANDLE just fine internally
1066 assert(physicalDevice == VK_NULL_HANDLE);
1067 return object_tracker::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);
1068}
1069
1070VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
1071 return object_tracker::GetDeviceProcAddr(dev, funcName);
1072}
1073
1074VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
1075 return object_tracker::GetInstanceProcAddr(instance, funcName);
1076}
1077
1078VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1079 const char *pLayerName, uint32_t *pCount,
1080 VkExtensionProperties *pProperties) {
1081 // The layer command handles VK_NULL_HANDLE just fine internally
1082 assert(physicalDevice == VK_NULL_HANDLE);
1083 return object_tracker::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);
1084}
1085
1086VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1087 const char *funcName) {
1088 return object_tracker::GetPhysicalDeviceProcAddr(instance, funcName);
1089}
1090
1091VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) {
1092 assert(pVersionStruct != NULL);
1093 assert(pVersionStruct->sType == LAYER_NEGOTIATE_INTERFACE_STRUCT);
1094
1095 // Fill in the function pointers if our version is at least capable of having the structure contain them.
1096 if (pVersionStruct->loaderLayerInterfaceVersion >= 2) {
1097 pVersionStruct->pfnGetInstanceProcAddr = vkGetInstanceProcAddr;
1098 pVersionStruct->pfnGetDeviceProcAddr = vkGetDeviceProcAddr;
1099 pVersionStruct->pfnGetPhysicalDeviceProcAddr = vk_layerGetPhysicalDeviceProcAddr;
1100 }
1101
1102 if (pVersionStruct->loaderLayerInterfaceVersion < CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1103 object_tracker::loader_layer_if_version = pVersionStruct->loaderLayerInterfaceVersion;
1104 } else if (pVersionStruct->loaderLayerInterfaceVersion > CURRENT_LOADER_LAYER_INTERFACE_VERSION) {
1105 pVersionStruct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
1106 }
1107
1108 return VK_SUCCESS;
1109}