blob: 9cb7a637d822989eb003521a37c1ae5fea20295d [file] [log] [blame]
Mark Young6ba8abe2017-11-09 10:37:04 -07001/* Copyright (c) 2015-2017 The Khronos Group Inc.
2 * Copyright (c) 2015-2017 Valve Corporation
3 * Copyright (c) 2015-2017 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>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060020 *
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060021 */
22
23#ifndef LAYER_LOGGING_H
24#define LAYER_LOGGING_H
25
Mark Lobodzinskib87f9022016-05-24 16:04:56 -060026#include "vk_loader_layer.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060027#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060028#include "vk_layer_data.h"
29#include "vk_layer_table.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"
Mark Lobodzinskic9d81652017-08-10 11:01:17 -060034#include <signal.h>
Karl Schultzd7f37542016-05-10 11:36:08 -060035#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060036#include <stdarg.h>
37#include <stdbool.h>
38#include <stdio.h>
39#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060040#include <vector>
Mark Young6ba8abe2017-11-09 10:37:04 -070041#include <sstream>
42#include <string>
43
Dave Houlton8e9f6542018-05-18 12:18:22 -060044static const char *kVUIDUndefined = "VUID_Undefined";
45
Mark Young6ba8abe2017-11-09 10:37:04 -070046// TODO: Could be autogenerated for the specific handles for extra type safety...
47template <typename HANDLE_T>
48static inline uint64_t HandleToUint64(HANDLE_T *h) {
49 return reinterpret_cast<uint64_t>(h);
50}
51
52static inline uint64_t HandleToUint64(uint64_t h) { return h; }
53
54// Data we store per label for logging
55typedef struct _LoggingLabelData {
56 std::string name;
57 float color[4];
58} LoggingLabelData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060059
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060060typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060061 VkLayerDbgFunctionNode *debug_callback_list;
62 VkLayerDbgFunctionNode *default_debug_callback_list;
Mark Young6ba8abe2017-11-09 10:37:04 -070063 VkDebugUtilsMessageSeverityFlagsEXT active_severities;
64 VkDebugUtilsMessageTypeFlagsEXT active_types;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060065 bool g_DEBUG_REPORT;
Mark Young6ba8abe2017-11-09 10:37:04 -070066 bool g_DEBUG_UTILS;
Tony Barbour3431dac2017-06-19 16:50:37 -060067 std::unordered_map<uint64_t, std::string> *debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -070068 std::unordered_map<uint64_t, std::string> *debugUtilsObjectNameMap;
69 std::unordered_map<VkQueue, std::vector<LoggingLabelData>> *debugUtilsQueueLabels;
70 bool queueLabelHasInsert;
71 std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>> *debugUtilsCmdBufLabels;
72 bool cmdBufLabelHasInsert;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060073} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060074
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070075template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
Jon Ashburn5484e0c2016-03-08 17:48:44 -070076 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060077
Mark Young6ba8abe2017-11-09 10:37:04 -070078static inline void DebugReportFlagsToAnnotFlags(VkDebugReportFlagsEXT dr_flags, bool default_flag_is_spec,
79 VkDebugUtilsMessageSeverityFlagsEXT *da_severity,
80 VkDebugUtilsMessageTypeFlagsEXT *da_type) {
81 // All layer warnings are spec warnings currently. At least as far as anything not specifically
82 // called out. In the future, we'll label things using the new split severity and type values.
83 *da_type = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
84 *da_severity = 0;
85 if ((dr_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) != 0) {
86 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
87 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
88 }
89 if ((dr_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) != 0) {
90 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
91 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
92 }
93 if ((dr_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) != 0) {
94 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
95 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
96 }
97 if ((dr_flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) {
98 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
99 }
100 if ((dr_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) {
101 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
102 }
103}
104
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600105// Forward Declarations
Mark Young6ba8abe2017-11-09 10:37:04 -0700106static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
107 uint64_t src_object, size_t location, int32_t msg_code, const char *layer_prefix,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700108 const char *message, const char *text_vuid = NULL);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600109
110// Add a debug message callback node structure to the specified callback linked list
Mark Young6ba8abe2017-11-09 10:37:04 -0700111static inline void AddDebugCallbackNode(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
112 VkLayerDbgFunctionNode *new_node) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600113 new_node->pNext = *list_head;
114 *list_head = new_node;
115}
116
Mark Young6ba8abe2017-11-09 10:37:04 -0700117// Remove specified debug messenger node structure from the specified linked list
118static inline void RemoveDebugUtilsMessenger(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
119 VkDebugUtilsMessengerEXT messenger) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600120 VkLayerDbgFunctionNode *cur_callback = *list_head;
121 VkLayerDbgFunctionNode *prev_callback = cur_callback;
122 bool matched = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700123 VkFlags local_severities = 0;
124 VkFlags local_types = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600125
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600126 while (cur_callback) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700127 if (cur_callback->is_messenger && cur_callback->messenger.messenger == messenger) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600128 matched = true;
129 prev_callback->pNext = cur_callback->pNext;
130 if (*list_head == cur_callback) {
131 *list_head = cur_callback->pNext;
132 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700133 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
134 reinterpret_cast<uint64_t &>(cur_callback->messenger.messenger), 0, 0, "DebugUtilsMessenger",
135 "Destroyed messenger\n");
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600136 } else {
137 matched = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700138 local_severities |= cur_callback->messenger.messageSeverity;
139 local_types |= cur_callback->messenger.messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600140 }
141 prev_callback = cur_callback;
142 cur_callback = cur_callback->pNext;
143 if (matched) {
144 free(prev_callback);
145 }
146 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700147 debug_data->active_severities = local_severities;
148 debug_data->active_types = local_types;
149}
150
151// Remove specified debug message callback node structure from the specified callback linked list
152static inline void RemoveDebugUtilsMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
153 VkDebugReportCallbackEXT callback) {
154 VkLayerDbgFunctionNode *cur_callback = *list_head;
155 VkLayerDbgFunctionNode *prev_callback = cur_callback;
156 bool matched = false;
157 VkFlags local_severities = 0;
158 VkFlags local_types = 0;
159
160 while (cur_callback) {
161 if (!cur_callback->is_messenger && cur_callback->report.msgCallback == callback) {
162 matched = true;
163 prev_callback->pNext = cur_callback->pNext;
164 if (*list_head == cur_callback) {
165 *list_head = cur_callback->pNext;
166 }
167 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
168 reinterpret_cast<uint64_t &>(cur_callback->report.msgCallback), 0, 0, "DebugReport",
169 "Destroyed callback\n");
170 } else {
171 matched = false;
172 VkFlags this_severities = 0;
173 VkFlags this_types = 0;
174 DebugReportFlagsToAnnotFlags(cur_callback->report.msgFlags, true, &this_severities, &this_types);
175 local_severities |= this_severities;
176 local_types |= this_types;
177 }
178 prev_callback = cur_callback;
179 cur_callback = cur_callback->pNext;
180 if (matched) {
181 free(prev_callback);
182 }
183 }
184 debug_data->active_severities = local_severities;
185 debug_data->active_types = local_types;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600186}
187
188// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
189static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
190 VkLayerDbgFunctionNode *current_callback = *list_head;
191 VkLayerDbgFunctionNode *prev_callback = current_callback;
192
193 while (current_callback) {
194 prev_callback = current_callback->pNext;
Mark Young6ba8abe2017-11-09 10:37:04 -0700195 if (!current_callback->is_messenger) {
196 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
197 (uint64_t)current_callback->report.msgCallback, 0, 0, "DebugReport",
198 "Debug Report callbacks not removed before DestroyInstance");
199 } else {
200 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
201 (uint64_t)current_callback->messenger.messenger, 0, 0, "Messenger",
202 "Debug messengers not removed before DestroyInstance");
203 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600204 free(current_callback);
205 current_callback = prev_callback;
206 }
207 *list_head = NULL;
208}
209
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700210// Note that text_vuid is a default parameter, and is optional. See the above forward declaration
Mark Young6ba8abe2017-11-09 10:37:04 -0700211static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
212 uint64_t src_object, size_t location, int32_t msg_code, const char *layer_prefix,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700213 const char *message, const char *text_vuid) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600214 bool bail = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700215 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600216
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600217 if (debug_data->debug_callback_list != NULL) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700218 layer_dbg_node = debug_data->debug_callback_list;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600219 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700220 layer_dbg_node = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600221 }
222
Mark Young6ba8abe2017-11-09 10:37:04 -0700223 VkDebugUtilsMessageSeverityFlagsEXT severity;
224 VkDebugUtilsMessageTypeFlagsEXT types;
225 VkDebugUtilsMessengerCallbackDataEXT callback_data;
226 VkDebugUtilsObjectNameInfoEXT object_name_info;
227
228 // Convert the info to the VK_EXT_debug_utils form in case we need it.
229 DebugReportFlagsToAnnotFlags(msg_flags, true, &severity, &types);
230 object_name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
231 object_name_info.pNext = NULL;
232 object_name_info.objectType = convertDebugReportObjectToCoreObject(object_type);
233 object_name_info.objectHandle = (uint64_t)(uintptr_t)src_object;
234 object_name_info.pObjectName = NULL;
235
236 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
237 callback_data.pNext = NULL;
238 callback_data.flags = 0;
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700239 callback_data.pMessageIdName = text_vuid;
Mark Young6ba8abe2017-11-09 10:37:04 -0700240 callback_data.messageIdNumber = msg_code;
241 callback_data.pMessage = message;
242 callback_data.queueLabelCount = 0;
243 callback_data.pQueueLabels = NULL;
244 callback_data.cmdBufLabelCount = 0;
245 callback_data.pCmdBufLabels = NULL;
246 callback_data.objectCount = 1;
247 callback_data.pObjects = &object_name_info;
248
249 VkDebugUtilsLabelEXT *queue_labels = nullptr;
250 VkDebugUtilsLabelEXT *cmd_buf_labels = nullptr;
251 std::string new_debug_report_message = "";
252 std::ostringstream oss;
Mark Young6ba8abe2017-11-09 10:37:04 -0700253
254 if (0 != src_object) {
Mark Young8504ba62018-03-21 13:35:34 -0600255 oss << "Object: 0x" << std::hex << src_object;
Mark Young6ba8abe2017-11-09 10:37:04 -0700256 // If this is a queue, add any queue labels to the callback data.
257 if (VK_OBJECT_TYPE_QUEUE == object_name_info.objectType) {
258 auto label_iter = debug_data->debugUtilsQueueLabels->find(reinterpret_cast<VkQueue>(src_object));
259 if (label_iter != debug_data->debugUtilsQueueLabels->end()) {
260 queue_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
261 if (nullptr != queue_labels) {
262 // Record the labels, but record them in reverse order since we want the
263 // most recent at the top.
264 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
265 uint32_t last_index = label_size - 1;
266 for (uint32_t label = 0; label < label_size; ++label) {
267 queue_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
268 queue_labels[last_index - label].pNext = nullptr;
269 queue_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
270 queue_labels[last_index - label].color[0] = label_iter->second[label].color[0];
271 queue_labels[last_index - label].color[1] = label_iter->second[label].color[1];
272 queue_labels[last_index - label].color[2] = label_iter->second[label].color[2];
273 queue_labels[last_index - label].color[3] = label_iter->second[label].color[3];
274 }
275 callback_data.queueLabelCount = label_size;
276 callback_data.pQueueLabels = queue_labels;
277 }
278 }
279 // If this is a command buffer, add any command buffer labels to the callback data.
280 } else if (VK_OBJECT_TYPE_COMMAND_BUFFER == object_name_info.objectType) {
281 auto label_iter = debug_data->debugUtilsCmdBufLabels->find(reinterpret_cast<VkCommandBuffer>(src_object));
282 if (label_iter != debug_data->debugUtilsCmdBufLabels->end()) {
283 cmd_buf_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
284 if (nullptr != cmd_buf_labels) {
285 // Record the labels, but record them in reverse order since we want the
286 // most recent at the top.
287 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
288 uint32_t last_index = label_size - 1;
289 for (uint32_t label = 0; label < label_size; ++label) {
290 cmd_buf_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
291 cmd_buf_labels[last_index - label].pNext = nullptr;
292 cmd_buf_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
293 cmd_buf_labels[last_index - label].color[0] = label_iter->second[label].color[0];
294 cmd_buf_labels[last_index - label].color[1] = label_iter->second[label].color[1];
295 cmd_buf_labels[last_index - label].color[2] = label_iter->second[label].color[2];
296 cmd_buf_labels[last_index - label].color[3] = label_iter->second[label].color[3];
297 }
298 callback_data.cmdBufLabelCount = label_size;
299 callback_data.pCmdBufLabels = cmd_buf_labels;
300 }
301 }
302 }
303 // Look for any debug utils or marker names to use for this object
304 callback_data.pObjects[0].pObjectName = NULL;
305 auto utils_name_iter = debug_data->debugUtilsObjectNameMap->find(src_object);
306 if (utils_name_iter != debug_data->debugUtilsObjectNameMap->end()) {
307 callback_data.pObjects[0].pObjectName = utils_name_iter->second.c_str();
308 } else {
309 auto marker_name_iter = debug_data->debugObjectNameMap->find(src_object);
310 if (marker_name_iter != debug_data->debugObjectNameMap->end()) {
311 callback_data.pObjects[0].pObjectName = marker_name_iter->second.c_str();
312 }
313 }
314 if (NULL != callback_data.pObjects[0].pObjectName) {
Mark Young8504ba62018-03-21 13:35:34 -0600315 oss << " (Name = " << callback_data.pObjects[0].pObjectName << " : Type = ";
316 } else {
317 oss << " (Type = ";
Mark Young6ba8abe2017-11-09 10:37:04 -0700318 }
Mark Young8504ba62018-03-21 13:35:34 -0600319 oss << std::to_string(object_type) << ")";
320 } else {
321 oss << "Object: VK_NULL_HANDLE (Type = " << std::to_string(object_type) << ")";
Mark Young6ba8abe2017-11-09 10:37:04 -0700322 }
323 new_debug_report_message += oss.str();
324 new_debug_report_message += " | ";
325 new_debug_report_message += message;
326
327 while (layer_dbg_node) {
Mark Young8504ba62018-03-21 13:35:34 -0600328 // If the app uses the VK_EXT_debug_report extension, call all of those registered callbacks.
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700329 if (!layer_dbg_node->is_messenger && (layer_dbg_node->report.msgFlags & msg_flags)) {
330 if (text_vuid != nullptr) {
331 // If a text vuid is supplied for the old debug report extension, prepend it to the message string
332 new_debug_report_message.insert(0, " ] ");
333 new_debug_report_message.insert(0, text_vuid);
334 new_debug_report_message.insert(0, " [ ");
335 }
336
337 if (layer_dbg_node->report.pfnMsgCallback(msg_flags, object_type, src_object, location, msg_code, layer_prefix,
338 new_debug_report_message.c_str(), layer_dbg_node->pUserData)) {
339 bail = true;
340 }
Mark Young8504ba62018-03-21 13:35:34 -0600341 // If the app uses the VK_EXT_debug_utils extension, call all of those registered callbacks.
Mark Young6ba8abe2017-11-09 10:37:04 -0700342 } else if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & severity) &&
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700343 (layer_dbg_node->messenger.messageType & types)) {
344 if (layer_dbg_node->messenger.pfnUserCallback(static_cast<VkDebugUtilsMessageSeverityFlagBitsEXT>(severity), types,
345 &callback_data, layer_dbg_node->pUserData)) {
346 bail = true;
347 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700348 }
349 layer_dbg_node = layer_dbg_node->pNext;
350 }
351
352 if (nullptr != queue_labels) {
353 delete[] queue_labels;
354 }
355 if (nullptr != cmd_buf_labels) {
356 delete[] cmd_buf_labels;
357 }
358
359 return bail;
360}
361
362static inline void DebugAnnotFlagsToReportFlags(VkDebugUtilsMessageSeverityFlagBitsEXT da_severity,
363 VkDebugUtilsMessageTypeFlagsEXT da_type, VkDebugReportFlagsEXT *dr_flags) {
364 *dr_flags = 0;
365
366 if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
367 *dr_flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
368 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) {
369 if ((da_type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) {
370 *dr_flags |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
371 } else {
372 *dr_flags |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
373 }
374 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) {
375 *dr_flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
376 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0) {
377 *dr_flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
378 }
379}
380
381static inline bool debug_messenger_log_msg(const debug_report_data *debug_data,
382 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
383 VkDebugUtilsMessageTypeFlagsEXT message_type,
384 VkDebugUtilsMessengerCallbackDataEXT *callback_data) {
385 bool bail = false;
386 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
387
388 if (debug_data->debug_callback_list != NULL) {
389 layer_dbg_node = debug_data->debug_callback_list;
390 } else {
391 layer_dbg_node = debug_data->default_debug_callback_list;
392 }
393
394 VkDebugReportFlagsEXT object_flags = 0;
395
396 DebugAnnotFlagsToReportFlags(message_severity, message_type, &object_flags);
397
398 while (layer_dbg_node) {
399 if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & message_severity) &&
400 (layer_dbg_node->messenger.messageType & message_type)) {
401 // Loop through each object and give it the proper name if it was set.
402 for (uint32_t obj = 0; obj < callback_data->objectCount; obj++) {
403 auto it = debug_data->debugUtilsObjectNameMap->find(callback_data->pObjects[obj].objectHandle);
404 if (it == debug_data->debugUtilsObjectNameMap->end()) {
405 continue;
406 }
407 callback_data->pObjects[obj].pObjectName = it->second.c_str();
408 }
409 if (layer_dbg_node->messenger.pfnUserCallback(message_severity, message_type, callback_data,
410 layer_dbg_node->pUserData)) {
411 bail = true;
412 }
413 } else if (!layer_dbg_node->is_messenger && layer_dbg_node->report.msgFlags & object_flags) {
414 auto it = debug_data->debugObjectNameMap->find(callback_data->pObjects[0].objectHandle);
John Zulauf536649b2018-05-01 13:28:27 -0600415 VkDebugReportObjectTypeEXT object_type = convertCoreObjectToDebugReportObject(callback_data->pObjects[0].objectType);
Tony Barbour3431dac2017-06-19 16:50:37 -0600416 if (it == debug_data->debugObjectNameMap->end()) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700417 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
418 callback_data->messageIdNumber, callback_data->pMessageIdName,
419 callback_data->pMessage, layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600420 bail = true;
421 }
422 } else {
423 std::string newMsg = "SrcObject name = ";
424 newMsg.append(it->second.c_str());
425 newMsg.append(" ");
Mark Young6ba8abe2017-11-09 10:37:04 -0700426 newMsg.append(callback_data->pMessage);
427 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
428 callback_data->messageIdNumber, callback_data->pMessageIdName,
429 newMsg.c_str(), layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600430 bail = true;
431 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600432 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600433 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700434 layer_dbg_node = layer_dbg_node->pNext;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600435 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600436
437 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600438}
439
Mark Young6ba8abe2017-11-09 10:37:04 -0700440static inline debug_report_data *debug_utils_create_instance(
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700441 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
Mark Young6ba8abe2017-11-09 10:37:04 -0700442 const char *const *enabled_extensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600443{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600444 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700445 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600446
447 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600448 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600449 // TODO: Check other property fields
Mark Young6ba8abe2017-11-09 10:37:04 -0700450 if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600451 debug_data->g_DEBUG_REPORT = true;
Mark Young6ba8abe2017-11-09 10:37:04 -0700452 } else if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) {
453 debug_data->g_DEBUG_UTILS = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600454 }
455 }
Tony Barbour3431dac2017-06-19 16:50:37 -0600456 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
Mark Young6ba8abe2017-11-09 10:37:04 -0700457 debug_data->debugUtilsObjectNameMap = new std::unordered_map<uint64_t, std::string>;
458 debug_data->debugUtilsQueueLabels = new std::unordered_map<VkQueue, std::vector<LoggingLabelData>>;
459 debug_data->debugUtilsCmdBufLabels = new std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>>;
460 debug_data->queueLabelHasInsert = false;
461 debug_data->cmdBufLabelHasInsert = false;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600462 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600463}
464
Mark Young6ba8abe2017-11-09 10:37:04 -0700465static inline void layer_debug_utils_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600466 if (debug_data) {
467 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
468 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
Tony Barbour3431dac2017-06-19 16:50:37 -0600469 delete debug_data->debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -0700470 delete debug_data->debugUtilsObjectNameMap;
471 delete debug_data->debugUtilsQueueLabels;
472 delete debug_data->debugUtilsCmdBufLabels;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600473 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600474 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600475}
476
Mark Young6ba8abe2017-11-09 10:37:04 -0700477static inline debug_report_data *layer_debug_utils_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600478 // DEBUG_REPORT shares data between Instance and Device,
479 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600480 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600481}
482
Mark Young6ba8abe2017-11-09 10:37:04 -0700483static inline void layer_debug_utils_destroy_device(VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600484 // Nothing to do since we're using instance data record
485}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600486
Mark Young6ba8abe2017-11-09 10:37:04 -0700487static inline void layer_destroy_messenger_callback(debug_report_data *debug_data, VkDebugUtilsMessengerEXT messenger,
488 const VkAllocationCallbacks *allocator) {
489 RemoveDebugUtilsMessenger(debug_data, &debug_data->debug_callback_list, messenger);
490 RemoveDebugUtilsMessenger(debug_data, &debug_data->default_debug_callback_list, messenger);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600491}
492
Mark Young6ba8abe2017-11-09 10:37:04 -0700493static inline VkResult layer_create_messenger_callback(debug_report_data *debug_data, bool default_callback,
494 const VkDebugUtilsMessengerCreateInfoEXT *create_info,
495 const VkAllocationCallbacks *allocator,
496 VkDebugUtilsMessengerEXT *messenger) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700497 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700498 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young6ba8abe2017-11-09 10:37:04 -0700499 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
500 pNewDbgFuncNode->is_messenger = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600501
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600502 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Young6ba8abe2017-11-09 10:37:04 -0700503 if (!(*messenger)) *messenger = (VkDebugUtilsMessengerEXT)pNewDbgFuncNode;
504 pNewDbgFuncNode->messenger.messenger = *messenger;
505 pNewDbgFuncNode->messenger.pfnUserCallback = create_info->pfnUserCallback;
506 pNewDbgFuncNode->messenger.messageSeverity = create_info->messageSeverity;
507 pNewDbgFuncNode->messenger.messageType = create_info->messageType;
508 pNewDbgFuncNode->pUserData = create_info->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600509
Mark Young6ba8abe2017-11-09 10:37:04 -0700510 debug_data->active_severities |= create_info->messageSeverity;
511 debug_data->active_types |= create_info->messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600512 if (default_callback) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700513 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600514 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700515 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600516 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600517
Mark Young6ba8abe2017-11-09 10:37:04 -0700518 VkDebugUtilsMessengerCallbackDataEXT callback_data = {};
519 VkDebugUtilsObjectNameInfoEXT blank_object = {};
520 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
521 callback_data.pNext = NULL;
522 callback_data.flags = 0;
523 callback_data.pMessageIdName = "Layer Internal Message";
524 callback_data.messageIdNumber = 0;
525 callback_data.pMessage = "Added messenger";
526 callback_data.queueLabelCount = 0;
527 callback_data.pQueueLabels = NULL;
528 callback_data.cmdBufLabelCount = 0;
529 callback_data.pCmdBufLabels = NULL;
530 callback_data.objectCount = 1;
531 callback_data.pObjects = &blank_object;
532 blank_object.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
533 blank_object.pNext = NULL;
534 blank_object.objectType = VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT;
535 blank_object.objectHandle = HandleToUint64(*messenger);
536 blank_object.pObjectName = NULL;
537 debug_messenger_log_msg(debug_data, VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,
538 VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT, &callback_data);
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600539 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600540}
541
Mark Young6ba8abe2017-11-09 10:37:04 -0700542static inline void layer_destroy_report_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
543 const VkAllocationCallbacks *allocator) {
544 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
545 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
546}
547
548static inline VkResult layer_create_report_callback(debug_report_data *debug_data, bool default_callback,
549 const VkDebugReportCallbackCreateInfoEXT *create_info,
550 const VkAllocationCallbacks *allocator, VkDebugReportCallbackEXT *callback) {
551 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
552 if (!pNewDbgFuncNode) {
553 return VK_ERROR_OUT_OF_HOST_MEMORY;
554 }
555 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
556 pNewDbgFuncNode->is_messenger = false;
557
558 // Handle of 0 is logging_callback so use allocated Node address as unique handle
559 if (!(*callback)) *callback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
560 pNewDbgFuncNode->report.msgCallback = *callback;
561 pNewDbgFuncNode->report.pfnMsgCallback = create_info->pfnCallback;
562 pNewDbgFuncNode->report.msgFlags = create_info->flags;
563 pNewDbgFuncNode->pUserData = create_info->pUserData;
564
565 VkFlags local_severity = 0;
566 VkFlags local_type = 0;
567 DebugReportFlagsToAnnotFlags(create_info->flags, true, &local_severity, &local_type);
568 debug_data->active_severities |= local_severity;
569 debug_data->active_types |= local_type;
570 if (default_callback) {
571 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
572 } else {
573 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600574 }
575
Mark Young6ba8abe2017-11-09 10:37:04 -0700576 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, (uint64_t)*callback, 0,
577 0, "DebugReport", "Added callback");
578 return VK_SUCCESS;
579}
580
581static inline PFN_vkVoidFunction debug_utils_get_instance_proc_addr(debug_report_data *debug_data, const char *func_name) {
582 if (!debug_data) {
583 return NULL;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600584 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700585 if (debug_data->g_DEBUG_REPORT) {
586 if (!strcmp(func_name, "vkCreateDebugReportCallbackEXT")) {
587 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
588 }
589 if (!strcmp(func_name, "vkDestroyDebugReportCallbackEXT")) {
590 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
591 }
592 if (!strcmp(func_name, "vkDebugReportMessageEXT")) {
593 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
594 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600595 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700596 if (debug_data->g_DEBUG_UTILS) {
597 if (!strcmp(func_name, "vkCreateDebugUtilsMessengerEXT")) {
598 return (PFN_vkVoidFunction)vkCreateDebugUtilsMessengerEXT;
599 }
600 if (!strcmp(func_name, "vkDestroyDebugUtilsMessengerEXT")) {
601 return (PFN_vkVoidFunction)vkDestroyDebugUtilsMessengerEXT;
602 }
603 if (!strcmp(func_name, "vkSubmitDebugUtilsMessageEXT")) {
604 return (PFN_vkVoidFunction)vkSubmitDebugUtilsMessageEXT;
605 }
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700606 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600607 return NULL;
608}
609
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600610// This utility (called at vkCreateInstance() time), looks at a pNext chain.
611// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
612// then allocates an array that can hold that many structs, as well as that
613// many VkDebugReportCallbackEXT handles. It then copies each
614// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
Mark Young6ba8abe2017-11-09 10:37:04 -0700615static inline VkResult layer_copy_tmp_report_callbacks(const void *pChain, uint32_t *num_callbacks,
616 VkDebugReportCallbackCreateInfoEXT **infos,
617 VkDebugReportCallbackEXT **callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600618 uint32_t n = *num_callbacks = 0;
619
620 const void *pNext = pChain;
621 while (pNext) {
622 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
623 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
624 n++;
625 }
626 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
627 }
628 if (n == 0) {
629 return VK_SUCCESS;
630 }
631
632 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
633 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
634 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
635 if (!pInfos) {
636 return VK_ERROR_OUT_OF_HOST_MEMORY;
637 }
638 // 3rd, allocate memory for a unique handle for each callback:
639 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
640 if (!pCallbacks) {
641 free(pInfos);
642 return VK_ERROR_OUT_OF_HOST_MEMORY;
643 }
644 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
645 // vkDestroyInstance, and assign a unique handle to each callback (just
646 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
647 pNext = pChain;
648 while (pNext) {
649 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
650 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
651 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
652 }
653 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
654 }
655
656 *num_callbacks = n;
657 return VK_SUCCESS;
658}
659
Mark Young6ba8abe2017-11-09 10:37:04 -0700660// This utility frees the arrays allocated by layer_copy_tmp_report_callbacks()
661static inline void layer_free_tmp_report_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600662 free(infos);
663 free(callbacks);
664}
665
666// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700667// that were copied by layer_copy_tmp_report_callbacks()
668static inline VkResult layer_enable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
669 VkDebugReportCallbackCreateInfoEXT *infos,
670 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600671 VkResult rtn = VK_SUCCESS;
672 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700673 rtn = layer_create_report_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600674 if (rtn != VK_SUCCESS) {
675 for (uint32_t j = 0; j < i; j++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700676 layer_destroy_report_callback(debug_data, callbacks[j], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600677 }
678 return rtn;
679 }
680 }
681 return rtn;
682}
683
684// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700685// that were copied by layer_copy_tmp_report_callbacks()
686static inline void layer_disable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
687 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600688 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700689 layer_destroy_report_callback(debug_data, callbacks[i], NULL);
690 }
691}
692
693// This utility (called at vkCreateInstance() time), looks at a pNext chain.
694// It counts any VkDebugUtilsMessengerCreateInfoEXT structs that it finds. It
695// then allocates an array that can hold that many structs, as well as that
696// many VkDebugUtilsMessengerEXT handles. It then copies each
697// VkDebugUtilsMessengerCreateInfoEXT, and initializes each handle.
Mark Young8504ba62018-03-21 13:35:34 -0600698static inline VkResult layer_copy_tmp_debug_messengers(const void *pChain, uint32_t *num_messengers,
699 VkDebugUtilsMessengerCreateInfoEXT **infos,
700 VkDebugUtilsMessengerEXT **messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700701 uint32_t n = *num_messengers = 0;
702
703 const void *pNext = pChain;
704 while (pNext) {
705 // 1st, count the number VkDebugUtilsMessengerCreateInfoEXT:
706 if (((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
707 n++;
708 }
709 pNext = (void *)((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->pNext;
710 }
711 if (n == 0) {
712 return VK_SUCCESS;
713 }
714
715 // 2nd, allocate memory for each VkDebugUtilsMessengerCreateInfoEXT:
716 VkDebugUtilsMessengerCreateInfoEXT *pInfos = *infos =
717 ((VkDebugUtilsMessengerCreateInfoEXT *)malloc(n * sizeof(VkDebugUtilsMessengerCreateInfoEXT)));
718 if (!pInfos) {
719 return VK_ERROR_OUT_OF_HOST_MEMORY;
720 }
721 // 3rd, allocate memory for a unique handle for each messenger:
722 VkDebugUtilsMessengerEXT *pMessengers = *messengers =
723 ((VkDebugUtilsMessengerEXT *)malloc(n * sizeof(VkDebugUtilsMessengerEXT)));
724 if (!pMessengers) {
725 free(pInfos);
726 return VK_ERROR_OUT_OF_HOST_MEMORY;
727 }
728 // 4th, copy each VkDebugUtilsMessengerCreateInfoEXT for use by
729 // vkDestroyInstance, and assign a unique handle to each callback (just
730 // use the address of the copied VkDebugUtilsMessengerCreateInfoEXT):
731 pNext = pChain;
732 while (pNext) {
733 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
734 memcpy(pInfos, pNext, sizeof(VkDebugUtilsMessengerCreateInfoEXT));
735 *pMessengers++ = (VkDebugUtilsMessengerEXT)pInfos++;
736 }
737 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
738 }
739
740 *num_messengers = n;
741 return VK_SUCCESS;
742}
743
744// This utility frees the arrays allocated by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600745static inline void layer_free_tmp_debug_messengers(VkDebugUtilsMessengerCreateInfoEXT *infos,
746 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700747 free(infos);
748 free(messengers);
749}
750
751// This utility enables all of the VkDebugUtilsMessengerCreateInfoEXT structs
752// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600753static inline VkResult layer_enable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
754 VkDebugUtilsMessengerCreateInfoEXT *infos,
755 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700756 VkResult rtn = VK_SUCCESS;
757 for (uint32_t i = 0; i < num_messengers; i++) {
758 rtn = layer_create_messenger_callback(debug_data, false, &infos[i], NULL, &messengers[i]);
759 if (rtn != VK_SUCCESS) {
760 for (uint32_t j = 0; j < i; j++) {
761 layer_destroy_messenger_callback(debug_data, messengers[j], NULL);
762 }
763 return rtn;
764 }
765 }
766 return rtn;
767}
768
769// This utility disables all of the VkDebugUtilsMessengerCreateInfoEXT structs
770// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600771static inline void layer_disable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
772 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700773 for (uint32_t i = 0; i < num_messengers; i++) {
774 layer_destroy_messenger_callback(debug_data, messengers[i], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600775 }
776}
777
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600778// Checks if the message will get logged.
779// Allows layer to defer collecting & formating data if the
780// message will be discarded.
Mark Young6ba8abe2017-11-09 10:37:04 -0700781static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msg_flags) {
782 VkFlags local_severity = 0;
783 VkFlags local_type = 0;
784 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
785 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600786 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600787 return false;
788 }
789
790 return true;
791}
John Zulauf6664e272018-01-17 11:00:22 -0700792#ifndef WIN32
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700793static inline int string_sprintf(std::string *output, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
John Zulauf6664e272018-01-17 11:00:22 -0700794#endif
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700795static inline int string_sprintf(std::string *output, const char *fmt, ...) {
John Zulauf6664e272018-01-17 11:00:22 -0700796 std::string &formatted = *output;
797 va_list argptr;
798 va_start(argptr, fmt);
799 int reserve = vsnprintf(nullptr, 0, fmt, argptr);
800 va_end(argptr);
801 formatted.reserve(reserve + 1);
802 va_start(argptr, fmt);
803 int result = vsnprintf((char *)formatted.data(), formatted.capacity(), fmt, argptr);
804 va_end(argptr);
805 assert(result == reserve);
806 return result;
807}
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600808
Chris Forbes8a25bce2016-03-24 12:06:35 +1300809#ifdef WIN32
810static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
811 *strp = nullptr;
812 int size = _vscprintf(fmt, ap);
813 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700814 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300815 if (!*strp) {
816 return -1;
817 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700818 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300819 }
820 return size;
821}
822#endif
823
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700824// Output log message via DEBUG_REPORT. Takes format and variable arg list so that output string is only computed if a message
825// needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800826#ifndef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700827static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600828 uint64_t src_object, int32_t msg_code, const char *format, ...) __attribute__((format(printf, 6, 7)));
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700829static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
830 uint64_t src_object, std::string vuid_text, const char *format, ...)
831 __attribute__((format(printf, 6, 7)));
Michael Lentine010f4692015-11-03 16:19:46 -0800832#endif
Mark Young6ba8abe2017-11-09 10:37:04 -0700833static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600834 uint64_t src_object, int32_t msg_code, const char *format, ...) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700835 VkFlags local_severity = 0;
836 VkFlags local_type = 0;
837 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
838 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600839 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600840 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600841 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600842
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600843 va_list argptr;
844 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300845 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200846 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600847 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200848 str = nullptr;
849 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600850 va_end(argptr);
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600851
852 std::string str_plus_spec_text(str);
853
854 // If the msg_code is in the error map, tack on spec text to error message.
855 if (validation_error_map.find(msg_code) != validation_error_map.end()) {
856 str_plus_spec_text += " ";
857 str_plus_spec_text += validation_error_map[msg_code];
858 }
859
Mark Lobodzinski88529492018-04-01 10:38:15 -0600860 bool result = debug_log_msg(debug_data, msg_flags, object_type, src_object, 0, msg_code, "Validation",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600861 str_plus_spec_text.c_str() ? str_plus_spec_text.c_str() : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300862 free(str);
863 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600864}
865
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700866// Overload of log_msg that takes a VUID string in place of a numerical VUID abstraction
867static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
868 uint64_t src_object, std::string vuid_text, const char *format, ...) {
869 VkFlags local_severity = 0;
870 VkFlags local_type = 0;
871 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
872 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
873 // Message is not wanted
874 return false;
875 }
876
877 va_list argptr;
878 va_start(argptr, format);
879 char *str;
880 if (-1 == vasprintf(&str, format, argptr)) {
881 // On failure, glibc vasprintf leaves str undefined
882 str = nullptr;
883 }
884 va_end(argptr);
885
886 std::string str_plus_spec_text(str);
887
Dave Houlton8e9f6542018-05-18 12:18:22 -0600888 // If the vuid string is in the error map: find the legacy enum, look up spec text, and tack it onto error message.
889 int32_t legacy_vuid_enum = VALIDATION_ERROR_UNDEFINED;
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700890 if (validation_error_text_map.find(vuid_text.c_str()) != validation_error_text_map.end()) {
Dave Houlton8e9f6542018-05-18 12:18:22 -0600891 legacy_vuid_enum = validation_error_text_map[vuid_text.c_str()];
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700892 str_plus_spec_text += " ";
Dave Houlton8e9f6542018-05-18 12:18:22 -0600893 str_plus_spec_text += validation_error_map[legacy_vuid_enum];
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700894 }
895
Dave Houlton8e9f6542018-05-18 12:18:22 -0600896 // Append layer prefix with VUID string, pass in recovered legacy numerical VUID
897 bool result = debug_log_msg(debug_data, msg_flags, object_type, src_object, 0, legacy_vuid_enum, "Validation",
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700898 str_plus_spec_text.c_str() ? str_plus_spec_text.c_str() : "Allocation failure", vuid_text.c_str());
899
900 free(str);
901 return result;
902}
903
Mark Young6ba8abe2017-11-09 10:37:04 -0700904static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_log_callback(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
905 uint64_t src_object, size_t location, int32_t msg_code,
906 const char *layer_prefix, const char *message, void *user_data) {
907 char msg_flag_string[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600908
Mark Young6ba8abe2017-11-09 10:37:04 -0700909 PrintMessageFlags(msg_flags, msg_flag_string);
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600910
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600911 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 -0700912 fflush((FILE *)user_data);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600913
914 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600915}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600916
Mark Young6ba8abe2017-11-09 10:37:04 -0700917static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_win32_debug_output_msg(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
918 uint64_t src_object, size_t location, int32_t msg_code,
919 const char *layer_prefix, const char *message,
920 void *user_data) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600921#ifdef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700922 char msg_flag_string[30];
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600923 char buf[2048];
924
Mark Young6ba8abe2017-11-09 10:37:04 -0700925 PrintMessageFlags(msg_flags, msg_flag_string);
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600926 _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 -0600927
928 OutputDebugString(buf);
929#endif
930
931 return false;
932}
933
Mark Young6ba8abe2017-11-09 10:37:04 -0700934static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT obj_type,
935 uint64_t src_object, size_t location, int32_t msg_code,
936 const char *layer_prefix, const char *message, void *user_data) {
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600937#ifdef WIN32
938 DebugBreak();
939#else
940 raise(SIGTRAP);
941#endif
942
943 return false;
944}
945
Mark Young6ba8abe2017-11-09 10:37:04 -0700946static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_log_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
947 VkDebugUtilsMessageTypeFlagsEXT message_type,
948 const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
949 void *user_data) {
950 char msg_severity[30];
951 char msg_type[30];
952
953 PrintMessageSeverity(message_severity, msg_severity);
954 PrintMessageType(message_type, msg_type);
955
Mark Young8504ba62018-03-21 13:35:34 -0600956 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 -0700957 callback_data->messageIdNumber, callback_data->pMessage);
Mark Young8504ba62018-03-21 13:35:34 -0600958 fprintf((FILE *)user_data, " Objects: %d\n", callback_data->objectCount);
959 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
960 fprintf((FILE *)user_data, " [%d] 0x%" PRIx64 ", type: %d, name: %s\n", obj,
961 callback_data->pObjects[obj].objectHandle, callback_data->pObjects[obj].objectType,
962 callback_data->pObjects[obj].pObjectName);
963 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700964 fflush((FILE *)user_data);
965
966 return false;
Petr Krause9388f62017-05-13 20:53:12 +0200967}
968
Mark Young6ba8abe2017-11-09 10:37:04 -0700969static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_win32_debug_output_msg(
970 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type,
971 const VkDebugUtilsMessengerCallbackDataEXT *callback_data, void *user_data) {
972#ifdef WIN32
973 char buf[2048];
974 char msg_severity[30];
975 char msg_type[30];
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600976
Mark Young6ba8abe2017-11-09 10:37:04 -0700977 PrintMessageSeverity(message_severity, msg_severity);
978 PrintMessageType(message_type, msg_type);
979
Mark Young8504ba62018-03-21 13:35:34 -0600980 size_t buffer_space = sizeof(buf) - 1;
981 size_t remaining_space = buffer_space;
982 _snprintf(buf, sizeof(buf) - 1, "%s(%s / %s): msgNum: %d - %s\n", callback_data->pMessageIdName, msg_severity, msg_type,
983 callback_data->messageIdNumber, callback_data->pMessage);
984 remaining_space = buffer_space - strlen(buf);
985 _snprintf(buf, remaining_space, " Objects: %d\n", callback_data->objectCount);
986 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
987 remaining_space = buffer_space - strlen(buf);
988 if (remaining_space > 0) {
989 _snprintf(buf, remaining_space, " [%d] 0x%" PRIx64 ", type: %d, name: %s\n", obj,
990 callback_data->pObjects[obj].objectHandle, callback_data->pObjects[obj].objectType,
991 callback_data->pObjects[obj].pObjectName);
992 }
993 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700994 OutputDebugString(buf);
995#endif
996
997 return false;
998}
999
1000// This utility converts from the VkDebugUtilsLabelEXT structure into the logging version of the structure.
1001// In the logging version, we only record what we absolutely need to convey back to the callbacks.
1002static inline void InsertLabelIntoLog(const VkDebugUtilsLabelEXT *utils_label, std::vector<LoggingLabelData> &log_vector) {
1003 LoggingLabelData log_label_data = {};
1004 log_label_data.name = utils_label->pLabelName;
1005 log_label_data.color[0] = utils_label->color[0];
1006 log_label_data.color[1] = utils_label->color[1];
1007 log_label_data.color[2] = utils_label->color[2];
1008 log_label_data.color[3] = utils_label->color[3];
1009 log_vector.push_back(log_label_data);
1010}
1011
Mark Young8504ba62018-03-21 13:35:34 -06001012static inline void BeginQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1013 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001014 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1015 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1016 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1017 std::vector<LoggingLabelData> new_queue_labels;
1018 InsertLabelIntoLog(label_info, new_queue_labels);
1019 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1020 } else {
1021 // If the last thing was a label insert, we need to pop it off of the label vector before any
1022 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1023 // temporary location that exists until the next operation occurs. In this case, a new
1024 // "vkQueueBeginDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1025 if (report_data->queueLabelHasInsert) {
1026 report_data->queueLabelHasInsert = false;
1027 label_iter->second.pop_back();
1028 }
1029 InsertLabelIntoLog(label_info, label_iter->second);
1030 }
1031 }
1032}
1033
Mark Young8504ba62018-03-21 13:35:34 -06001034static inline void EndQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001035 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1036 if (label_iter != report_data->debugUtilsQueueLabels->end()) {
1037 // If the last thing was a label insert, we need to pop it off of the label vector before any
1038 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1039 // temporary location that exists until the next operation occurs. In this case, a
1040 // "vkQueueEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1041 if (report_data->queueLabelHasInsert) {
1042 report_data->queueLabelHasInsert = false;
1043 label_iter->second.pop_back();
1044 }
1045 // Now pop the normal item
1046 label_iter->second.pop_back();
1047 }
1048}
1049
Mark Young8504ba62018-03-21 13:35:34 -06001050static inline void InsertQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1051 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001052 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1053 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1054 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1055 std::vector<LoggingLabelData> new_queue_labels;
1056 InsertLabelIntoLog(label_info, new_queue_labels);
1057 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1058 } else {
1059 // If the last thing was a label insert, we need to pop it off of the label vector before any
1060 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1061 // temporary location that exists until the next operation occurs. In this case, a new
1062 // "vkQueueInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1063 if (report_data->queueLabelHasInsert) {
1064 label_iter->second.pop_back();
1065 }
1066 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1067 // the next queue label operation.
1068 InsertLabelIntoLog(label_info, label_iter->second);
1069 report_data->queueLabelHasInsert = true;
1070 }
1071 }
1072}
1073
Mark Young8504ba62018-03-21 13:35:34 -06001074static inline void BeginCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1075 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001076 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1077 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1078 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1079 std::vector<LoggingLabelData> new_cmdbuf_labels;
1080 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1081 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1082 } else {
1083 // If the last thing was a label insert, we need to pop it off of the label vector before any
1084 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1085 // temporary location that exists until the next operation occurs. In this case, a
1086 // "vkCmdBeginDebugUtilsLabelEXT" has occurred erasing the inserted label.
1087 if (report_data->cmdBufLabelHasInsert) {
1088 report_data->cmdBufLabelHasInsert = false;
1089 label_iter->second.pop_back();
1090 }
1091 InsertLabelIntoLog(label_info, label_iter->second);
1092 }
1093 }
1094}
1095
Mark Young8504ba62018-03-21 13:35:34 -06001096static inline void EndCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001097 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1098 if (label_iter != report_data->debugUtilsCmdBufLabels->end()) {
1099 // If the last thing was a label insert, we need to pop it off of the label vector before any
1100 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1101 // temporary location that exists until the next operation occurs. In this case, a
1102 // "vkCmdEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1103 if (report_data->cmdBufLabelHasInsert) {
1104 report_data->cmdBufLabelHasInsert = false;
1105 label_iter->second.pop_back();
1106 }
1107 // Now pop the normal item
1108 label_iter->second.pop_back();
1109 }
1110}
1111
Mark Young8504ba62018-03-21 13:35:34 -06001112static inline void InsertCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1113 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001114 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1115 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1116 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1117 std::vector<LoggingLabelData> new_cmdbuf_labels;
1118 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1119 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1120 } else {
1121 // If the last thing was a label insert, we need to pop it off of the label vector before any
1122 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1123 // temporary location that exists until the next operation occurs. In this case, a new
1124 // "vkCmdInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1125 if (report_data->cmdBufLabelHasInsert) {
1126 label_iter->second.pop_back();
1127 }
1128 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1129 // the next command buffer label operation.
1130 InsertLabelIntoLog(label_info, label_iter->second);
1131 report_data->cmdBufLabelHasInsert = true;
1132 }
1133 }
1134}
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001135
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001136#endif // LAYER_LOGGING_H