blob: ad02bb6f84b5adb979e1d4c64a790c136c135308 [file] [log] [blame]
Dave Houlton4d9b2f82018-10-24 18:21:06 -06001/* Copyright (c) 2015-2018 The Khronos Group Inc.
2 * Copyright (c) 2015-2018 Valve Corporation
3 * Copyright (c) 2015-2018 LunarG, Inc.
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -06004 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060016 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060017 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
18 * Author: Tobin Ehlis <tobin@lunarg.com>
Mark Young6ba8abe2017-11-09 10:37:04 -070019 * Author: Mark Young <marky@lunarg.com>
Dave Houlton4d9b2f82018-10-24 18:21:06 -060020 * Author: Dave Houlton <daveh@lunarg.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060021 *
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060022 */
23
24#ifndef LAYER_LOGGING_H
25#define LAYER_LOGGING_H
26
Mark Lobodzinskib87f9022016-05-24 16:04:56 -060027#include "vk_loader_layer.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060028#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060029#include "vk_layer_data.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060030#include "vk_loader_platform.h"
31#include "vulkan/vk_layer.h"
Mark Young6ba8abe2017-11-09 10:37:04 -070032#include "vk_object_types.h"
Mark Lobodzinski487a0d12018-03-30 10:09:03 -060033#include "vk_validation_error_messages.h"
Lenny Komow4c0da772018-07-03 10:17:21 -060034#include "vk_layer_dispatch_table.h"
Mark Lobodzinskic9d81652017-08-10 11:01:17 -060035#include <signal.h>
Karl Schultzd7f37542016-05-10 11:36:08 -060036#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060037#include <stdarg.h>
38#include <stdbool.h>
39#include <stdio.h>
40#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060041#include <vector>
Mark Young6ba8abe2017-11-09 10:37:04 -070042#include <sstream>
43#include <string>
44
Dave Houlton57ae22f2018-05-18 16:20:52 -060045// Suppress unused warning on Linux
46#if defined(__GNUC__)
Dave Houltoncfcecbf2018-05-31 16:20:57 -060047#define DECORATE_UNUSED __attribute__((unused))
48#else
49#define DECORATE_UNUSED
Dave Houlton57ae22f2018-05-18 16:20:52 -060050#endif
51
Dave Houltoncfcecbf2018-05-31 16:20:57 -060052static const char DECORATE_UNUSED *kVUIDUndefined = "VUID_Undefined";
Dave Houlton8e9f6542018-05-18 12:18:22 -060053
Dave Houltoncfcecbf2018-05-31 16:20:57 -060054#undef DECORATE_UNUSED
Dave Houlton57ae22f2018-05-18 16:20:52 -060055
Mark Young6ba8abe2017-11-09 10:37:04 -070056// TODO: Could be autogenerated for the specific handles for extra type safety...
57template <typename HANDLE_T>
58static inline uint64_t HandleToUint64(HANDLE_T *h) {
59 return reinterpret_cast<uint64_t>(h);
60}
61
62static inline uint64_t HandleToUint64(uint64_t h) { return h; }
63
64// Data we store per label for logging
65typedef struct _LoggingLabelData {
66 std::string name;
67 float color[4];
68} LoggingLabelData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060069
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060070typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060071 VkLayerDbgFunctionNode *debug_callback_list;
72 VkLayerDbgFunctionNode *default_debug_callback_list;
Mark Young6ba8abe2017-11-09 10:37:04 -070073 VkDebugUtilsMessageSeverityFlagsEXT active_severities;
74 VkDebugUtilsMessageTypeFlagsEXT active_types;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060075 bool g_DEBUG_REPORT;
Mark Young6ba8abe2017-11-09 10:37:04 -070076 bool g_DEBUG_UTILS;
Tony Barbour3431dac2017-06-19 16:50:37 -060077 std::unordered_map<uint64_t, std::string> *debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -070078 std::unordered_map<uint64_t, std::string> *debugUtilsObjectNameMap;
79 std::unordered_map<VkQueue, std::vector<LoggingLabelData>> *debugUtilsQueueLabels;
80 bool queueLabelHasInsert;
81 std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>> *debugUtilsCmdBufLabels;
82 bool cmdBufLabelHasInsert;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060083} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060084
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070085template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
Jon Ashburn5484e0c2016-03-08 17:48:44 -070086 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060087
Mark Young6ba8abe2017-11-09 10:37:04 -070088static inline void DebugReportFlagsToAnnotFlags(VkDebugReportFlagsEXT dr_flags, bool default_flag_is_spec,
89 VkDebugUtilsMessageSeverityFlagsEXT *da_severity,
90 VkDebugUtilsMessageTypeFlagsEXT *da_type) {
Mark Young6ba8abe2017-11-09 10:37:04 -070091 *da_severity = 0;
Mark Youngfbaae712018-10-04 14:33:50 -060092 *da_type = 0;
Mark Youngc2347792018-05-30 08:41:25 -060093 // If it's explicitly listed as a performance warning, treat it as a performance message.
94 // Otherwise, treat it as a validation issue.
95 if ((dr_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) != 0) {
Mark Youngfbaae712018-10-04 14:33:50 -060096 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
97 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
Mark Youngc2347792018-05-30 08:41:25 -060098 }
Mark Young6ba8abe2017-11-09 10:37:04 -070099 if ((dr_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) != 0) {
Mark Youngfbaae712018-10-04 14:33:50 -0600100 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700101 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700102 }
103 if ((dr_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) != 0) {
Mark Youngfbaae712018-10-04 14:33:50 -0600104 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700105 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700106 }
Mark Youngfbaae712018-10-04 14:33:50 -0600107 if ((dr_flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) {
108 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700109 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
110 }
111 if ((dr_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) {
Mark Youngfbaae712018-10-04 14:33:50 -0600112 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700113 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
114 }
115}
116
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600117// Forward Declarations
Mark Young6ba8abe2017-11-09 10:37:04 -0700118static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Dave Houlton407df732018-08-06 17:58:24 -0600119 uint64_t src_object, size_t location, const char *layer_prefix, const char *message,
120 const char *text_vuid);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600121
122// Add a debug message callback node structure to the specified callback linked list
Mark Young6ba8abe2017-11-09 10:37:04 -0700123static inline void AddDebugCallbackNode(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
124 VkLayerDbgFunctionNode *new_node) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600125 new_node->pNext = *list_head;
126 *list_head = new_node;
127}
128
Mark Young6ba8abe2017-11-09 10:37:04 -0700129// Remove specified debug messenger node structure from the specified linked list
130static inline void RemoveDebugUtilsMessenger(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
131 VkDebugUtilsMessengerEXT messenger) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600132 VkLayerDbgFunctionNode *cur_callback = *list_head;
Mark Youngfbaae712018-10-04 14:33:50 -0600133 VkLayerDbgFunctionNode *prev_callback = nullptr;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600134 bool matched = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700135 VkFlags local_severities = 0;
136 VkFlags local_types = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600137
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600138 while (cur_callback) {
Mark Youngfbaae712018-10-04 14:33:50 -0600139 if (cur_callback->is_messenger) {
140 // If it's actually a messenger, then set it up for deletion.
141 if (cur_callback->messenger.messenger == messenger) {
142 matched = true;
143 if (*list_head == cur_callback) {
144 *list_head = cur_callback->pNext;
145 } else {
146 assert(nullptr != prev_callback);
147 prev_callback->pNext = cur_callback->pNext;
148 }
149 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
150 reinterpret_cast<uint64_t &>(cur_callback->messenger.messenger), 0, "DebugUtilsMessenger",
151 "Destroyed messenger\n", kVUIDUndefined);
152 } else {
153 // If it's not the one we're looking for, just keep the types/severities
154 local_severities |= cur_callback->messenger.messageSeverity;
155 local_types |= cur_callback->messenger.messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600156 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600157 } else {
Mark Youngfbaae712018-10-04 14:33:50 -0600158 // If it's not a messenger, just keep the types/severities
159 VkFlags this_severities = 0;
160 VkFlags this_types = 0;
161 DebugReportFlagsToAnnotFlags(cur_callback->report.msgFlags, true, &this_severities, &this_types);
162 local_severities |= this_severities;
163 local_types |= this_types;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600164 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600165 if (matched) {
Mark Youngfbaae712018-10-04 14:33:50 -0600166 free(cur_callback);
167 matched = false;
168 // Intentionally keep the last prev_callback, but select the proper cur_callback
169 if (nullptr != prev_callback) {
170 cur_callback = prev_callback->pNext;
171 } else {
172 cur_callback = *list_head;
173 }
174 } else {
175 prev_callback = cur_callback;
176 cur_callback = cur_callback->pNext;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600177 }
178 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700179 debug_data->active_severities = local_severities;
180 debug_data->active_types = local_types;
181}
182
183// Remove specified debug message callback node structure from the specified callback linked list
184static inline void RemoveDebugUtilsMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
185 VkDebugReportCallbackEXT callback) {
186 VkLayerDbgFunctionNode *cur_callback = *list_head;
Mark Youngfbaae712018-10-04 14:33:50 -0600187 VkLayerDbgFunctionNode *prev_callback = nullptr;
Mark Young6ba8abe2017-11-09 10:37:04 -0700188 bool matched = false;
189 VkFlags local_severities = 0;
190 VkFlags local_types = 0;
191
192 while (cur_callback) {
Mark Youngfbaae712018-10-04 14:33:50 -0600193 if (!cur_callback->is_messenger) {
194 // If it's actually a callback, then set it up for deletion.
195 if (cur_callback->report.msgCallback == callback) {
196 matched = true;
197 if (*list_head == cur_callback) {
198 *list_head = cur_callback->pNext;
199 } else {
200 assert(nullptr != prev_callback);
201 prev_callback->pNext = cur_callback->pNext;
202 }
203 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
204 reinterpret_cast<uint64_t &>(cur_callback->report.msgCallback), 0, "DebugReport",
205 "Destroyed callback\n", kVUIDUndefined);
206 } else {
207 // If it's not the one we're looking for, just keep the types/severities
208 VkFlags this_severities = 0;
209 VkFlags this_types = 0;
210 DebugReportFlagsToAnnotFlags(cur_callback->report.msgFlags, true, &this_severities, &this_types);
211 local_severities |= this_severities;
212 local_types |= this_types;
Mark Young6ba8abe2017-11-09 10:37:04 -0700213 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700214 } else {
Mark Youngfbaae712018-10-04 14:33:50 -0600215 // If it's not a callback, just keep the types/severities
216 local_severities |= cur_callback->messenger.messageSeverity;
217 local_types |= cur_callback->messenger.messageType;
Mark Young6ba8abe2017-11-09 10:37:04 -0700218 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700219 if (matched) {
Mark Youngfbaae712018-10-04 14:33:50 -0600220 free(cur_callback);
221 matched = false;
222 // Intentionally keep the last prev_callback, but select the proper cur_callback
223 if (nullptr != prev_callback) {
224 cur_callback = prev_callback->pNext;
225 } else {
226 cur_callback = *list_head;
227 }
228 } else {
229 prev_callback = cur_callback;
230 cur_callback = cur_callback->pNext;
Mark Young6ba8abe2017-11-09 10:37:04 -0700231 }
232 }
233 debug_data->active_severities = local_severities;
234 debug_data->active_types = local_types;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600235}
236
237// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
238static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
239 VkLayerDbgFunctionNode *current_callback = *list_head;
240 VkLayerDbgFunctionNode *prev_callback = current_callback;
241
242 while (current_callback) {
243 prev_callback = current_callback->pNext;
Mark Young6ba8abe2017-11-09 10:37:04 -0700244 if (!current_callback->is_messenger) {
245 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Dave Houlton407df732018-08-06 17:58:24 -0600246 (uint64_t)current_callback->report.msgCallback, 0, "DebugReport",
247 "Debug Report callbacks not removed before DestroyInstance", kVUIDUndefined);
Mark Young6ba8abe2017-11-09 10:37:04 -0700248 } else {
249 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Dave Houlton407df732018-08-06 17:58:24 -0600250 (uint64_t)current_callback->messenger.messenger, 0, "Messenger",
251 "Debug messengers not removed before DestroyInstance", kVUIDUndefined);
Mark Young6ba8abe2017-11-09 10:37:04 -0700252 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600253 free(current_callback);
254 current_callback = prev_callback;
255 }
256 *list_head = NULL;
257}
258
Mark Young6ba8abe2017-11-09 10:37:04 -0700259static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Dave Houlton407df732018-08-06 17:58:24 -0600260 uint64_t src_object, size_t location, const char *layer_prefix, const char *message,
261 const char *text_vuid) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600262 bool bail = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700263 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600264
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600265 if (debug_data->debug_callback_list != NULL) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700266 layer_dbg_node = debug_data->debug_callback_list;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600267 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700268 layer_dbg_node = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600269 }
270
Mark Young6ba8abe2017-11-09 10:37:04 -0700271 VkDebugUtilsMessageSeverityFlagsEXT severity;
272 VkDebugUtilsMessageTypeFlagsEXT types;
273 VkDebugUtilsMessengerCallbackDataEXT callback_data;
274 VkDebugUtilsObjectNameInfoEXT object_name_info;
275
276 // Convert the info to the VK_EXT_debug_utils form in case we need it.
277 DebugReportFlagsToAnnotFlags(msg_flags, true, &severity, &types);
278 object_name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
279 object_name_info.pNext = NULL;
280 object_name_info.objectType = convertDebugReportObjectToCoreObject(object_type);
281 object_name_info.objectHandle = (uint64_t)(uintptr_t)src_object;
282 object_name_info.pObjectName = NULL;
283
284 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
285 callback_data.pNext = NULL;
286 callback_data.flags = 0;
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700287 callback_data.pMessageIdName = text_vuid;
Dave Houlton407df732018-08-06 17:58:24 -0600288 callback_data.messageIdNumber = 0; // deprecated, validation layers use only the pMessageIdName
Mark Young6ba8abe2017-11-09 10:37:04 -0700289 callback_data.pMessage = message;
290 callback_data.queueLabelCount = 0;
291 callback_data.pQueueLabels = NULL;
292 callback_data.cmdBufLabelCount = 0;
293 callback_data.pCmdBufLabels = NULL;
294 callback_data.objectCount = 1;
295 callback_data.pObjects = &object_name_info;
296
297 VkDebugUtilsLabelEXT *queue_labels = nullptr;
298 VkDebugUtilsLabelEXT *cmd_buf_labels = nullptr;
299 std::string new_debug_report_message = "";
300 std::ostringstream oss;
Mark Young6ba8abe2017-11-09 10:37:04 -0700301
302 if (0 != src_object) {
Mark Young8504ba62018-03-21 13:35:34 -0600303 oss << "Object: 0x" << std::hex << src_object;
Mark Young6ba8abe2017-11-09 10:37:04 -0700304 // If this is a queue, add any queue labels to the callback data.
305 if (VK_OBJECT_TYPE_QUEUE == object_name_info.objectType) {
306 auto label_iter = debug_data->debugUtilsQueueLabels->find(reinterpret_cast<VkQueue>(src_object));
307 if (label_iter != debug_data->debugUtilsQueueLabels->end()) {
308 queue_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
309 if (nullptr != queue_labels) {
310 // Record the labels, but record them in reverse order since we want the
311 // most recent at the top.
312 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
313 uint32_t last_index = label_size - 1;
314 for (uint32_t label = 0; label < label_size; ++label) {
315 queue_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
316 queue_labels[last_index - label].pNext = nullptr;
317 queue_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
318 queue_labels[last_index - label].color[0] = label_iter->second[label].color[0];
319 queue_labels[last_index - label].color[1] = label_iter->second[label].color[1];
320 queue_labels[last_index - label].color[2] = label_iter->second[label].color[2];
321 queue_labels[last_index - label].color[3] = label_iter->second[label].color[3];
322 }
323 callback_data.queueLabelCount = label_size;
324 callback_data.pQueueLabels = queue_labels;
325 }
326 }
327 // If this is a command buffer, add any command buffer labels to the callback data.
328 } else if (VK_OBJECT_TYPE_COMMAND_BUFFER == object_name_info.objectType) {
329 auto label_iter = debug_data->debugUtilsCmdBufLabels->find(reinterpret_cast<VkCommandBuffer>(src_object));
330 if (label_iter != debug_data->debugUtilsCmdBufLabels->end()) {
331 cmd_buf_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
332 if (nullptr != cmd_buf_labels) {
333 // Record the labels, but record them in reverse order since we want the
334 // most recent at the top.
335 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
336 uint32_t last_index = label_size - 1;
337 for (uint32_t label = 0; label < label_size; ++label) {
338 cmd_buf_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
339 cmd_buf_labels[last_index - label].pNext = nullptr;
340 cmd_buf_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
341 cmd_buf_labels[last_index - label].color[0] = label_iter->second[label].color[0];
342 cmd_buf_labels[last_index - label].color[1] = label_iter->second[label].color[1];
343 cmd_buf_labels[last_index - label].color[2] = label_iter->second[label].color[2];
344 cmd_buf_labels[last_index - label].color[3] = label_iter->second[label].color[3];
345 }
346 callback_data.cmdBufLabelCount = label_size;
347 callback_data.pCmdBufLabels = cmd_buf_labels;
348 }
349 }
350 }
351 // Look for any debug utils or marker names to use for this object
352 callback_data.pObjects[0].pObjectName = NULL;
353 auto utils_name_iter = debug_data->debugUtilsObjectNameMap->find(src_object);
354 if (utils_name_iter != debug_data->debugUtilsObjectNameMap->end()) {
355 callback_data.pObjects[0].pObjectName = utils_name_iter->second.c_str();
356 } else {
357 auto marker_name_iter = debug_data->debugObjectNameMap->find(src_object);
358 if (marker_name_iter != debug_data->debugObjectNameMap->end()) {
359 callback_data.pObjects[0].pObjectName = marker_name_iter->second.c_str();
360 }
361 }
362 if (NULL != callback_data.pObjects[0].pObjectName) {
Mark Young8504ba62018-03-21 13:35:34 -0600363 oss << " (Name = " << callback_data.pObjects[0].pObjectName << " : Type = ";
364 } else {
365 oss << " (Type = ";
Mark Young6ba8abe2017-11-09 10:37:04 -0700366 }
Mark Young8504ba62018-03-21 13:35:34 -0600367 oss << std::to_string(object_type) << ")";
368 } else {
369 oss << "Object: VK_NULL_HANDLE (Type = " << std::to_string(object_type) << ")";
Mark Young6ba8abe2017-11-09 10:37:04 -0700370 }
371 new_debug_report_message += oss.str();
372 new_debug_report_message += " | ";
373 new_debug_report_message += message;
374
375 while (layer_dbg_node) {
Mark Young8504ba62018-03-21 13:35:34 -0600376 // If the app uses the VK_EXT_debug_report extension, call all of those registered callbacks.
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700377 if (!layer_dbg_node->is_messenger && (layer_dbg_node->report.msgFlags & msg_flags)) {
378 if (text_vuid != nullptr) {
379 // If a text vuid is supplied for the old debug report extension, prepend it to the message string
380 new_debug_report_message.insert(0, " ] ");
381 new_debug_report_message.insert(0, text_vuid);
382 new_debug_report_message.insert(0, " [ ");
383 }
384
Dave Houlton407df732018-08-06 17:58:24 -0600385 if (layer_dbg_node->report.pfnMsgCallback(msg_flags, object_type, src_object, location, 0, layer_prefix,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700386 new_debug_report_message.c_str(), layer_dbg_node->pUserData)) {
387 bail = true;
388 }
Mark Young8504ba62018-03-21 13:35:34 -0600389 // If the app uses the VK_EXT_debug_utils extension, call all of those registered callbacks.
Mark Young6ba8abe2017-11-09 10:37:04 -0700390 } else if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & severity) &&
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700391 (layer_dbg_node->messenger.messageType & types)) {
392 if (layer_dbg_node->messenger.pfnUserCallback(static_cast<VkDebugUtilsMessageSeverityFlagBitsEXT>(severity), types,
393 &callback_data, layer_dbg_node->pUserData)) {
394 bail = true;
395 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700396 }
397 layer_dbg_node = layer_dbg_node->pNext;
398 }
399
400 if (nullptr != queue_labels) {
401 delete[] queue_labels;
402 }
403 if (nullptr != cmd_buf_labels) {
404 delete[] cmd_buf_labels;
405 }
406
407 return bail;
408}
409
410static inline void DebugAnnotFlagsToReportFlags(VkDebugUtilsMessageSeverityFlagBitsEXT da_severity,
411 VkDebugUtilsMessageTypeFlagsEXT da_type, VkDebugReportFlagsEXT *dr_flags) {
412 *dr_flags = 0;
413
414 if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
415 *dr_flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
416 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) {
417 if ((da_type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) {
418 *dr_flags |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
419 } else {
420 *dr_flags |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
421 }
422 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) {
423 *dr_flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
424 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0) {
425 *dr_flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
426 }
427}
428
429static inline bool debug_messenger_log_msg(const debug_report_data *debug_data,
430 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
431 VkDebugUtilsMessageTypeFlagsEXT message_type,
432 VkDebugUtilsMessengerCallbackDataEXT *callback_data) {
433 bool bail = false;
434 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
435
436 if (debug_data->debug_callback_list != NULL) {
437 layer_dbg_node = debug_data->debug_callback_list;
438 } else {
439 layer_dbg_node = debug_data->default_debug_callback_list;
440 }
441
442 VkDebugReportFlagsEXT object_flags = 0;
443
444 DebugAnnotFlagsToReportFlags(message_severity, message_type, &object_flags);
445
446 while (layer_dbg_node) {
447 if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & message_severity) &&
448 (layer_dbg_node->messenger.messageType & message_type)) {
449 // Loop through each object and give it the proper name if it was set.
450 for (uint32_t obj = 0; obj < callback_data->objectCount; obj++) {
451 auto it = debug_data->debugUtilsObjectNameMap->find(callback_data->pObjects[obj].objectHandle);
452 if (it == debug_data->debugUtilsObjectNameMap->end()) {
453 continue;
454 }
455 callback_data->pObjects[obj].pObjectName = it->second.c_str();
456 }
457 if (layer_dbg_node->messenger.pfnUserCallback(message_severity, message_type, callback_data,
458 layer_dbg_node->pUserData)) {
459 bail = true;
460 }
461 } else if (!layer_dbg_node->is_messenger && layer_dbg_node->report.msgFlags & object_flags) {
462 auto it = debug_data->debugObjectNameMap->find(callback_data->pObjects[0].objectHandle);
John Zulauf536649b2018-05-01 13:28:27 -0600463 VkDebugReportObjectTypeEXT object_type = convertCoreObjectToDebugReportObject(callback_data->pObjects[0].objectType);
Tony Barbour3431dac2017-06-19 16:50:37 -0600464 if (it == debug_data->debugObjectNameMap->end()) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700465 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
466 callback_data->messageIdNumber, callback_data->pMessageIdName,
467 callback_data->pMessage, layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600468 bail = true;
469 }
470 } else {
471 std::string newMsg = "SrcObject name = ";
472 newMsg.append(it->second.c_str());
473 newMsg.append(" ");
Mark Young6ba8abe2017-11-09 10:37:04 -0700474 newMsg.append(callback_data->pMessage);
475 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
476 callback_data->messageIdNumber, callback_data->pMessageIdName,
477 newMsg.c_str(), layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600478 bail = true;
479 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600480 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600481 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700482 layer_dbg_node = layer_dbg_node->pNext;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600483 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600484
485 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600486}
487
Mark Young6ba8abe2017-11-09 10:37:04 -0700488static inline debug_report_data *debug_utils_create_instance(
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700489 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
Mark Young6ba8abe2017-11-09 10:37:04 -0700490 const char *const *enabled_extensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600491{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600492 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700493 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600494
495 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600496 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600497 // TODO: Check other property fields
Mark Young6ba8abe2017-11-09 10:37:04 -0700498 if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600499 debug_data->g_DEBUG_REPORT = true;
Mark Young6ba8abe2017-11-09 10:37:04 -0700500 } else if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) {
501 debug_data->g_DEBUG_UTILS = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600502 }
503 }
Tony Barbour3431dac2017-06-19 16:50:37 -0600504 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
Mark Young6ba8abe2017-11-09 10:37:04 -0700505 debug_data->debugUtilsObjectNameMap = new std::unordered_map<uint64_t, std::string>;
506 debug_data->debugUtilsQueueLabels = new std::unordered_map<VkQueue, std::vector<LoggingLabelData>>;
507 debug_data->debugUtilsCmdBufLabels = new std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>>;
508 debug_data->queueLabelHasInsert = false;
509 debug_data->cmdBufLabelHasInsert = false;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600510 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600511}
512
Mark Young6ba8abe2017-11-09 10:37:04 -0700513static inline void layer_debug_utils_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600514 if (debug_data) {
515 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
516 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
Tony Barbour3431dac2017-06-19 16:50:37 -0600517 delete debug_data->debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -0700518 delete debug_data->debugUtilsObjectNameMap;
519 delete debug_data->debugUtilsQueueLabels;
520 delete debug_data->debugUtilsCmdBufLabels;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600521 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600522 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600523}
524
Mark Young6ba8abe2017-11-09 10:37:04 -0700525static inline debug_report_data *layer_debug_utils_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600526 // DEBUG_REPORT shares data between Instance and Device,
527 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600528 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600529}
530
Mark Young6ba8abe2017-11-09 10:37:04 -0700531static inline void layer_debug_utils_destroy_device(VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600532 // Nothing to do since we're using instance data record
533}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600534
Mark Young6ba8abe2017-11-09 10:37:04 -0700535static inline void layer_destroy_messenger_callback(debug_report_data *debug_data, VkDebugUtilsMessengerEXT messenger,
536 const VkAllocationCallbacks *allocator) {
537 RemoveDebugUtilsMessenger(debug_data, &debug_data->debug_callback_list, messenger);
538 RemoveDebugUtilsMessenger(debug_data, &debug_data->default_debug_callback_list, messenger);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600539}
540
Mark Young6ba8abe2017-11-09 10:37:04 -0700541static inline VkResult layer_create_messenger_callback(debug_report_data *debug_data, bool default_callback,
542 const VkDebugUtilsMessengerCreateInfoEXT *create_info,
543 const VkAllocationCallbacks *allocator,
544 VkDebugUtilsMessengerEXT *messenger) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700545 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700546 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young6ba8abe2017-11-09 10:37:04 -0700547 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
548 pNewDbgFuncNode->is_messenger = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600549
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600550 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Young6ba8abe2017-11-09 10:37:04 -0700551 if (!(*messenger)) *messenger = (VkDebugUtilsMessengerEXT)pNewDbgFuncNode;
552 pNewDbgFuncNode->messenger.messenger = *messenger;
553 pNewDbgFuncNode->messenger.pfnUserCallback = create_info->pfnUserCallback;
554 pNewDbgFuncNode->messenger.messageSeverity = create_info->messageSeverity;
555 pNewDbgFuncNode->messenger.messageType = create_info->messageType;
556 pNewDbgFuncNode->pUserData = create_info->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600557
Mark Young6ba8abe2017-11-09 10:37:04 -0700558 debug_data->active_severities |= create_info->messageSeverity;
559 debug_data->active_types |= create_info->messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600560 if (default_callback) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700561 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600562 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700563 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600564 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600565
Mark Young6ba8abe2017-11-09 10:37:04 -0700566 VkDebugUtilsMessengerCallbackDataEXT callback_data = {};
567 VkDebugUtilsObjectNameInfoEXT blank_object = {};
568 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
569 callback_data.pNext = NULL;
570 callback_data.flags = 0;
571 callback_data.pMessageIdName = "Layer Internal Message";
572 callback_data.messageIdNumber = 0;
573 callback_data.pMessage = "Added messenger";
574 callback_data.queueLabelCount = 0;
575 callback_data.pQueueLabels = NULL;
576 callback_data.cmdBufLabelCount = 0;
577 callback_data.pCmdBufLabels = NULL;
578 callback_data.objectCount = 1;
579 callback_data.pObjects = &blank_object;
580 blank_object.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
581 blank_object.pNext = NULL;
582 blank_object.objectType = VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT;
583 blank_object.objectHandle = HandleToUint64(*messenger);
584 blank_object.pObjectName = NULL;
585 debug_messenger_log_msg(debug_data, VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,
586 VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT, &callback_data);
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600587 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600588}
589
Mark Young6ba8abe2017-11-09 10:37:04 -0700590static inline void layer_destroy_report_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
591 const VkAllocationCallbacks *allocator) {
592 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
593 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
594}
595
596static inline VkResult layer_create_report_callback(debug_report_data *debug_data, bool default_callback,
597 const VkDebugReportCallbackCreateInfoEXT *create_info,
598 const VkAllocationCallbacks *allocator, VkDebugReportCallbackEXT *callback) {
599 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
600 if (!pNewDbgFuncNode) {
601 return VK_ERROR_OUT_OF_HOST_MEMORY;
602 }
603 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
604 pNewDbgFuncNode->is_messenger = false;
605
606 // Handle of 0 is logging_callback so use allocated Node address as unique handle
607 if (!(*callback)) *callback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
608 pNewDbgFuncNode->report.msgCallback = *callback;
609 pNewDbgFuncNode->report.pfnMsgCallback = create_info->pfnCallback;
610 pNewDbgFuncNode->report.msgFlags = create_info->flags;
611 pNewDbgFuncNode->pUserData = create_info->pUserData;
612
613 VkFlags local_severity = 0;
614 VkFlags local_type = 0;
615 DebugReportFlagsToAnnotFlags(create_info->flags, true, &local_severity, &local_type);
616 debug_data->active_severities |= local_severity;
617 debug_data->active_types |= local_type;
618 if (default_callback) {
619 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
620 } else {
621 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600622 }
623
Mark Young6ba8abe2017-11-09 10:37:04 -0700624 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, (uint64_t)*callback, 0,
Dave Houlton407df732018-08-06 17:58:24 -0600625 "DebugReport", "Added callback", kVUIDUndefined);
Mark Young6ba8abe2017-11-09 10:37:04 -0700626 return VK_SUCCESS;
627}
628
629static inline PFN_vkVoidFunction debug_utils_get_instance_proc_addr(debug_report_data *debug_data, const char *func_name) {
630 if (!debug_data) {
631 return NULL;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600632 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700633 if (debug_data->g_DEBUG_REPORT) {
634 if (!strcmp(func_name, "vkCreateDebugReportCallbackEXT")) {
635 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
636 }
637 if (!strcmp(func_name, "vkDestroyDebugReportCallbackEXT")) {
638 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
639 }
640 if (!strcmp(func_name, "vkDebugReportMessageEXT")) {
641 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
642 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600643 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700644 if (debug_data->g_DEBUG_UTILS) {
645 if (!strcmp(func_name, "vkCreateDebugUtilsMessengerEXT")) {
646 return (PFN_vkVoidFunction)vkCreateDebugUtilsMessengerEXT;
647 }
648 if (!strcmp(func_name, "vkDestroyDebugUtilsMessengerEXT")) {
649 return (PFN_vkVoidFunction)vkDestroyDebugUtilsMessengerEXT;
650 }
651 if (!strcmp(func_name, "vkSubmitDebugUtilsMessageEXT")) {
652 return (PFN_vkVoidFunction)vkSubmitDebugUtilsMessageEXT;
653 }
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700654 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600655 return NULL;
656}
657
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600658// This utility (called at vkCreateInstance() time), looks at a pNext chain.
659// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
660// then allocates an array that can hold that many structs, as well as that
661// many VkDebugReportCallbackEXT handles. It then copies each
662// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
Mark Young6ba8abe2017-11-09 10:37:04 -0700663static inline VkResult layer_copy_tmp_report_callbacks(const void *pChain, uint32_t *num_callbacks,
664 VkDebugReportCallbackCreateInfoEXT **infos,
665 VkDebugReportCallbackEXT **callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600666 uint32_t n = *num_callbacks = 0;
667
668 const void *pNext = pChain;
669 while (pNext) {
670 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
671 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
672 n++;
673 }
674 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
675 }
676 if (n == 0) {
677 return VK_SUCCESS;
678 }
679
680 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
681 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
682 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
683 if (!pInfos) {
684 return VK_ERROR_OUT_OF_HOST_MEMORY;
685 }
686 // 3rd, allocate memory for a unique handle for each callback:
687 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
688 if (!pCallbacks) {
689 free(pInfos);
690 return VK_ERROR_OUT_OF_HOST_MEMORY;
691 }
692 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
693 // vkDestroyInstance, and assign a unique handle to each callback (just
694 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
695 pNext = pChain;
696 while (pNext) {
697 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
698 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
699 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
700 }
701 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
702 }
703
704 *num_callbacks = n;
705 return VK_SUCCESS;
706}
707
Mark Young6ba8abe2017-11-09 10:37:04 -0700708// This utility frees the arrays allocated by layer_copy_tmp_report_callbacks()
709static inline void layer_free_tmp_report_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600710 free(infos);
711 free(callbacks);
712}
713
714// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700715// that were copied by layer_copy_tmp_report_callbacks()
716static inline VkResult layer_enable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
717 VkDebugReportCallbackCreateInfoEXT *infos,
718 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600719 VkResult rtn = VK_SUCCESS;
720 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700721 rtn = layer_create_report_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600722 if (rtn != VK_SUCCESS) {
723 for (uint32_t j = 0; j < i; j++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700724 layer_destroy_report_callback(debug_data, callbacks[j], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600725 }
726 return rtn;
727 }
728 }
729 return rtn;
730}
731
732// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700733// that were copied by layer_copy_tmp_report_callbacks()
734static inline void layer_disable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
735 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600736 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700737 layer_destroy_report_callback(debug_data, callbacks[i], NULL);
738 }
739}
740
741// This utility (called at vkCreateInstance() time), looks at a pNext chain.
742// It counts any VkDebugUtilsMessengerCreateInfoEXT structs that it finds. It
743// then allocates an array that can hold that many structs, as well as that
744// many VkDebugUtilsMessengerEXT handles. It then copies each
745// VkDebugUtilsMessengerCreateInfoEXT, and initializes each handle.
Mark Young8504ba62018-03-21 13:35:34 -0600746static inline VkResult layer_copy_tmp_debug_messengers(const void *pChain, uint32_t *num_messengers,
747 VkDebugUtilsMessengerCreateInfoEXT **infos,
748 VkDebugUtilsMessengerEXT **messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700749 uint32_t n = *num_messengers = 0;
750
751 const void *pNext = pChain;
752 while (pNext) {
753 // 1st, count the number VkDebugUtilsMessengerCreateInfoEXT:
754 if (((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
755 n++;
756 }
757 pNext = (void *)((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->pNext;
758 }
759 if (n == 0) {
760 return VK_SUCCESS;
761 }
762
763 // 2nd, allocate memory for each VkDebugUtilsMessengerCreateInfoEXT:
764 VkDebugUtilsMessengerCreateInfoEXT *pInfos = *infos =
765 ((VkDebugUtilsMessengerCreateInfoEXT *)malloc(n * sizeof(VkDebugUtilsMessengerCreateInfoEXT)));
766 if (!pInfos) {
767 return VK_ERROR_OUT_OF_HOST_MEMORY;
768 }
769 // 3rd, allocate memory for a unique handle for each messenger:
770 VkDebugUtilsMessengerEXT *pMessengers = *messengers =
771 ((VkDebugUtilsMessengerEXT *)malloc(n * sizeof(VkDebugUtilsMessengerEXT)));
772 if (!pMessengers) {
773 free(pInfos);
774 return VK_ERROR_OUT_OF_HOST_MEMORY;
775 }
776 // 4th, copy each VkDebugUtilsMessengerCreateInfoEXT for use by
777 // vkDestroyInstance, and assign a unique handle to each callback (just
778 // use the address of the copied VkDebugUtilsMessengerCreateInfoEXT):
779 pNext = pChain;
780 while (pNext) {
781 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
782 memcpy(pInfos, pNext, sizeof(VkDebugUtilsMessengerCreateInfoEXT));
783 *pMessengers++ = (VkDebugUtilsMessengerEXT)pInfos++;
784 }
785 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
786 }
787
788 *num_messengers = n;
789 return VK_SUCCESS;
790}
791
792// This utility frees the arrays allocated by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600793static inline void layer_free_tmp_debug_messengers(VkDebugUtilsMessengerCreateInfoEXT *infos,
794 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700795 free(infos);
796 free(messengers);
797}
798
799// This utility enables all of the VkDebugUtilsMessengerCreateInfoEXT structs
800// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600801static inline VkResult layer_enable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
802 VkDebugUtilsMessengerCreateInfoEXT *infos,
803 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700804 VkResult rtn = VK_SUCCESS;
805 for (uint32_t i = 0; i < num_messengers; i++) {
806 rtn = layer_create_messenger_callback(debug_data, false, &infos[i], NULL, &messengers[i]);
807 if (rtn != VK_SUCCESS) {
808 for (uint32_t j = 0; j < i; j++) {
809 layer_destroy_messenger_callback(debug_data, messengers[j], NULL);
810 }
811 return rtn;
812 }
813 }
814 return rtn;
815}
816
817// This utility disables all of the VkDebugUtilsMessengerCreateInfoEXT structs
818// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600819static inline void layer_disable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
820 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700821 for (uint32_t i = 0; i < num_messengers; i++) {
822 layer_destroy_messenger_callback(debug_data, messengers[i], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600823 }
824}
825
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600826// Checks if the message will get logged.
827// Allows layer to defer collecting & formating data if the
828// message will be discarded.
Mark Young6ba8abe2017-11-09 10:37:04 -0700829static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msg_flags) {
830 VkFlags local_severity = 0;
831 VkFlags local_type = 0;
832 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
833 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600834 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600835 return false;
836 }
837
838 return true;
839}
John Zulauf6664e272018-01-17 11:00:22 -0700840#ifndef WIN32
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700841static inline int string_sprintf(std::string *output, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
John Zulauf6664e272018-01-17 11:00:22 -0700842#endif
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700843static inline int string_sprintf(std::string *output, const char *fmt, ...) {
John Zulauf6664e272018-01-17 11:00:22 -0700844 std::string &formatted = *output;
845 va_list argptr;
846 va_start(argptr, fmt);
847 int reserve = vsnprintf(nullptr, 0, fmt, argptr);
848 va_end(argptr);
849 formatted.reserve(reserve + 1);
850 va_start(argptr, fmt);
851 int result = vsnprintf((char *)formatted.data(), formatted.capacity(), fmt, argptr);
852 va_end(argptr);
853 assert(result == reserve);
854 return result;
855}
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600856
Chris Forbes8a25bce2016-03-24 12:06:35 +1300857#ifdef WIN32
858static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
859 *strp = nullptr;
860 int size = _vscprintf(fmt, ap);
861 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700862 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300863 if (!*strp) {
864 return -1;
865 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700866 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300867 }
868 return size;
869}
870#endif
871
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700872// Output log message via DEBUG_REPORT. Takes format and variable arg list so that output string is only computed if a message
873// needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800874#ifndef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700875static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700876 uint64_t src_object, std::string vuid_text, const char *format, ...)
877 __attribute__((format(printf, 6, 7)));
Michael Lentine010f4692015-11-03 16:19:46 -0800878#endif
Mark Young6ba8abe2017-11-09 10:37:04 -0700879static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700880 uint64_t src_object, std::string vuid_text, const char *format, ...) {
881 VkFlags local_severity = 0;
882 VkFlags local_type = 0;
883 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
884 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
885 // Message is not wanted
886 return false;
887 }
888
889 va_list argptr;
890 va_start(argptr, format);
891 char *str;
892 if (-1 == vasprintf(&str, format, argptr)) {
893 // On failure, glibc vasprintf leaves str undefined
894 str = nullptr;
895 }
896 va_end(argptr);
897
Dave Houlton407df732018-08-06 17:58:24 -0600898 std::string str_plus_spec_text(str ? str : "Allocation failure");
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700899
Dave Houlton407df732018-08-06 17:58:24 -0600900 // Append the spec error text to the error message, unless it's an UNASSIGNED or UNDEFINED vuid
901 if ((vuid_text.find("UNASSIGNED-") == std::string::npos) && (vuid_text.find(kVUIDUndefined) == std::string::npos)) {
Dave Houlton4d9b2f82018-10-24 18:21:06 -0600902 // Linear search makes no assumptions about the layout of the string table
903 // This is not fast, but it does not need to be at this point in the error reporting path
904 uint32_t num_vuids = sizeof(vuid_spec_text) / sizeof(vuid_spec_text_pair);
905 const char *spec_text = nullptr;
906 for (uint32_t i = 0; i < num_vuids; i++) {
907 if (0 == strcmp(vuid_text.c_str(), vuid_spec_text[i].vuid)) {
908 spec_text = vuid_spec_text[i].spec_text;
909 break;
910 }
911 }
912
913 if (nullptr == spec_text) {
Dave Houlton407df732018-08-06 17:58:24 -0600914 // If this happens, you've hit a VUID string that isn't defined in the spec's json file
915 // Try running 'vk_validation_stats -c' to look for invalid VUID strings in the repo code
916 assert(0);
917 } else {
918 str_plus_spec_text += " The Vulkan spec states: ";
Dave Houlton4d9b2f82018-10-24 18:21:06 -0600919 str_plus_spec_text += spec_text;
Dave Houlton407df732018-08-06 17:58:24 -0600920 }
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700921 }
922
Dave Houlton8e9f6542018-05-18 12:18:22 -0600923 // Append layer prefix with VUID string, pass in recovered legacy numerical VUID
Dave Houlton407df732018-08-06 17:58:24 -0600924 bool result = debug_log_msg(debug_data, msg_flags, object_type, src_object, 0, "Validation", str_plus_spec_text.c_str(),
925 vuid_text.c_str());
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700926
927 free(str);
928 return result;
929}
930
Mark Young6ba8abe2017-11-09 10:37:04 -0700931static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_log_callback(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
932 uint64_t src_object, size_t location, int32_t msg_code,
933 const char *layer_prefix, const char *message, void *user_data) {
934 char msg_flag_string[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600935
Mark Young6ba8abe2017-11-09 10:37:04 -0700936 PrintMessageFlags(msg_flags, msg_flag_string);
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600937
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600938 fprintf((FILE *)user_data, "%s(%s): msg_code: %d: %s\n", layer_prefix, msg_flag_string, msg_code, message);
Mark Young6ba8abe2017-11-09 10:37:04 -0700939 fflush((FILE *)user_data);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600940
941 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600942}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600943
Mark Young6ba8abe2017-11-09 10:37:04 -0700944static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_win32_debug_output_msg(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
945 uint64_t src_object, size_t location, int32_t msg_code,
946 const char *layer_prefix, const char *message,
947 void *user_data) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600948#ifdef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700949 char msg_flag_string[30];
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600950 char buf[2048];
951
Mark Young6ba8abe2017-11-09 10:37:04 -0700952 PrintMessageFlags(msg_flags, msg_flag_string);
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600953 _snprintf(buf, sizeof(buf) - 1, "%s (%s): msg_code: %d: %s\n", layer_prefix, msg_flag_string, msg_code, message);
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600954
955 OutputDebugString(buf);
956#endif
957
958 return false;
959}
960
Mark Young6ba8abe2017-11-09 10:37:04 -0700961static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT obj_type,
962 uint64_t src_object, size_t location, int32_t msg_code,
963 const char *layer_prefix, const char *message, void *user_data) {
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600964#ifdef WIN32
965 DebugBreak();
966#else
967 raise(SIGTRAP);
968#endif
969
970 return false;
971}
972
Mark Young6ba8abe2017-11-09 10:37:04 -0700973static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_log_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
974 VkDebugUtilsMessageTypeFlagsEXT message_type,
975 const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
976 void *user_data) {
977 char msg_severity[30];
978 char msg_type[30];
979
980 PrintMessageSeverity(message_severity, msg_severity);
981 PrintMessageType(message_type, msg_type);
982
Mark Young8504ba62018-03-21 13:35:34 -0600983 fprintf((FILE *)user_data, "%s(%s / %s): msgNum: %d - %s\n", callback_data->pMessageIdName, msg_severity, msg_type,
Mark Young6ba8abe2017-11-09 10:37:04 -0700984 callback_data->messageIdNumber, callback_data->pMessage);
Mark Young8504ba62018-03-21 13:35:34 -0600985 fprintf((FILE *)user_data, " Objects: %d\n", callback_data->objectCount);
986 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
987 fprintf((FILE *)user_data, " [%d] 0x%" PRIx64 ", type: %d, name: %s\n", obj,
988 callback_data->pObjects[obj].objectHandle, callback_data->pObjects[obj].objectType,
989 callback_data->pObjects[obj].pObjectName);
990 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700991 fflush((FILE *)user_data);
992
993 return false;
Petr Krause9388f62017-05-13 20:53:12 +0200994}
995
Mark Young6ba8abe2017-11-09 10:37:04 -0700996static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_win32_debug_output_msg(
997 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type,
998 const VkDebugUtilsMessengerCallbackDataEXT *callback_data, void *user_data) {
999#ifdef WIN32
1000 char buf[2048];
1001 char msg_severity[30];
1002 char msg_type[30];
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001003
Mark Young6ba8abe2017-11-09 10:37:04 -07001004 PrintMessageSeverity(message_severity, msg_severity);
1005 PrintMessageType(message_type, msg_type);
1006
Mark Young8504ba62018-03-21 13:35:34 -06001007 size_t buffer_space = sizeof(buf) - 1;
1008 size_t remaining_space = buffer_space;
1009 _snprintf(buf, sizeof(buf) - 1, "%s(%s / %s): msgNum: %d - %s\n", callback_data->pMessageIdName, msg_severity, msg_type,
1010 callback_data->messageIdNumber, callback_data->pMessage);
1011 remaining_space = buffer_space - strlen(buf);
1012 _snprintf(buf, remaining_space, " Objects: %d\n", callback_data->objectCount);
1013 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
1014 remaining_space = buffer_space - strlen(buf);
1015 if (remaining_space > 0) {
1016 _snprintf(buf, remaining_space, " [%d] 0x%" PRIx64 ", type: %d, name: %s\n", obj,
1017 callback_data->pObjects[obj].objectHandle, callback_data->pObjects[obj].objectType,
1018 callback_data->pObjects[obj].pObjectName);
1019 }
1020 }
Mark Young6ba8abe2017-11-09 10:37:04 -07001021 OutputDebugString(buf);
1022#endif
1023
1024 return false;
1025}
1026
1027// This utility converts from the VkDebugUtilsLabelEXT structure into the logging version of the structure.
1028// In the logging version, we only record what we absolutely need to convey back to the callbacks.
1029static inline void InsertLabelIntoLog(const VkDebugUtilsLabelEXT *utils_label, std::vector<LoggingLabelData> &log_vector) {
1030 LoggingLabelData log_label_data = {};
1031 log_label_data.name = utils_label->pLabelName;
1032 log_label_data.color[0] = utils_label->color[0];
1033 log_label_data.color[1] = utils_label->color[1];
1034 log_label_data.color[2] = utils_label->color[2];
1035 log_label_data.color[3] = utils_label->color[3];
1036 log_vector.push_back(log_label_data);
1037}
1038
Mark Young8504ba62018-03-21 13:35:34 -06001039static inline void BeginQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1040 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001041 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1042 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1043 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1044 std::vector<LoggingLabelData> new_queue_labels;
1045 InsertLabelIntoLog(label_info, new_queue_labels);
1046 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1047 } else {
1048 // If the last thing was a label insert, we need to pop it off of the label vector before any
1049 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1050 // temporary location that exists until the next operation occurs. In this case, a new
1051 // "vkQueueBeginDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1052 if (report_data->queueLabelHasInsert) {
1053 report_data->queueLabelHasInsert = false;
1054 label_iter->second.pop_back();
1055 }
1056 InsertLabelIntoLog(label_info, label_iter->second);
1057 }
1058 }
1059}
1060
Mark Young8504ba62018-03-21 13:35:34 -06001061static inline void EndQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001062 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1063 if (label_iter != report_data->debugUtilsQueueLabels->end()) {
1064 // If the last thing was a label insert, we need to pop it off of the label vector before any
1065 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1066 // temporary location that exists until the next operation occurs. In this case, a
1067 // "vkQueueEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1068 if (report_data->queueLabelHasInsert) {
1069 report_data->queueLabelHasInsert = false;
1070 label_iter->second.pop_back();
1071 }
1072 // Now pop the normal item
1073 label_iter->second.pop_back();
1074 }
1075}
1076
Mark Young8504ba62018-03-21 13:35:34 -06001077static inline void InsertQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1078 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001079 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1080 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1081 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1082 std::vector<LoggingLabelData> new_queue_labels;
1083 InsertLabelIntoLog(label_info, new_queue_labels);
1084 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1085 } else {
1086 // If the last thing was a label insert, we need to pop it off of the label vector before any
1087 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1088 // temporary location that exists until the next operation occurs. In this case, a new
1089 // "vkQueueInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1090 if (report_data->queueLabelHasInsert) {
1091 label_iter->second.pop_back();
1092 }
1093 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1094 // the next queue label operation.
1095 InsertLabelIntoLog(label_info, label_iter->second);
1096 report_data->queueLabelHasInsert = true;
1097 }
1098 }
1099}
1100
Mark Young8504ba62018-03-21 13:35:34 -06001101static inline void BeginCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1102 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001103 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1104 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1105 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1106 std::vector<LoggingLabelData> new_cmdbuf_labels;
1107 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1108 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1109 } else {
1110 // If the last thing was a label insert, we need to pop it off of the label vector before any
1111 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1112 // temporary location that exists until the next operation occurs. In this case, a
1113 // "vkCmdBeginDebugUtilsLabelEXT" has occurred erasing the inserted label.
1114 if (report_data->cmdBufLabelHasInsert) {
1115 report_data->cmdBufLabelHasInsert = false;
1116 label_iter->second.pop_back();
1117 }
1118 InsertLabelIntoLog(label_info, label_iter->second);
1119 }
1120 }
1121}
1122
Mark Young8504ba62018-03-21 13:35:34 -06001123static inline void EndCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001124 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1125 if (label_iter != report_data->debugUtilsCmdBufLabels->end()) {
1126 // If the last thing was a label insert, we need to pop it off of the label vector before any
1127 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1128 // temporary location that exists until the next operation occurs. In this case, a
1129 // "vkCmdEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1130 if (report_data->cmdBufLabelHasInsert) {
1131 report_data->cmdBufLabelHasInsert = false;
1132 label_iter->second.pop_back();
1133 }
1134 // Now pop the normal item
1135 label_iter->second.pop_back();
1136 }
1137}
1138
Mark Young8504ba62018-03-21 13:35:34 -06001139static inline void InsertCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1140 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001141 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1142 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1143 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1144 std::vector<LoggingLabelData> new_cmdbuf_labels;
1145 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1146 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1147 } else {
1148 // If the last thing was a label insert, we need to pop it off of the label vector before any
1149 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1150 // temporary location that exists until the next operation occurs. In this case, a new
1151 // "vkCmdInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1152 if (report_data->cmdBufLabelHasInsert) {
1153 label_iter->second.pop_back();
1154 }
1155 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1156 // the next command buffer label operation.
1157 InsertLabelIntoLog(label_info, label_iter->second);
1158 report_data->cmdBufLabelHasInsert = true;
1159 }
1160 }
1161}
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001162
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001163#endif // LAYER_LOGGING_H