blob: 3a58045d0b89c8004a475c0161360697da672b3e [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 Houlton57ae22f2018-05-18 16:20:52 -060044// Suppress unused warning on Linux
45#if defined(__GNUC__)
46#pragma GCC diagnostic push
47#pragma GCC diagnostic ignored "-Wunused-variable"
48#endif
49
Dave Houlton8e9f6542018-05-18 12:18:22 -060050static const char *kVUIDUndefined = "VUID_Undefined";
51
Dave Houlton57ae22f2018-05-18 16:20:52 -060052#if defined(__GNUC__)
53#pragma GCC diagnostic pop
54#endif
55
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) {
91 // All layer warnings are spec warnings currently. At least as far as anything not specifically
92 // called out. In the future, we'll label things using the new split severity and type values.
93 *da_type = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
94 *da_severity = 0;
95 if ((dr_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) != 0) {
96 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
97 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
98 }
99 if ((dr_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) != 0) {
100 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
101 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
102 }
103 if ((dr_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) != 0) {
104 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
105 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
106 }
107 if ((dr_flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) {
108 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
109 }
110 if ((dr_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) {
111 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
112 }
113}
114
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600115// Forward Declarations
Mark Young6ba8abe2017-11-09 10:37:04 -0700116static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
117 uint64_t src_object, size_t location, int32_t msg_code, const char *layer_prefix,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700118 const char *message, const char *text_vuid = NULL);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600119
120// Add a debug message callback node structure to the specified callback linked list
Mark Young6ba8abe2017-11-09 10:37:04 -0700121static inline void AddDebugCallbackNode(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
122 VkLayerDbgFunctionNode *new_node) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600123 new_node->pNext = *list_head;
124 *list_head = new_node;
125}
126
Mark Young6ba8abe2017-11-09 10:37:04 -0700127// Remove specified debug messenger node structure from the specified linked list
128static inline void RemoveDebugUtilsMessenger(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
129 VkDebugUtilsMessengerEXT messenger) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600130 VkLayerDbgFunctionNode *cur_callback = *list_head;
131 VkLayerDbgFunctionNode *prev_callback = cur_callback;
132 bool matched = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700133 VkFlags local_severities = 0;
134 VkFlags local_types = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600135
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600136 while (cur_callback) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700137 if (cur_callback->is_messenger && cur_callback->messenger.messenger == messenger) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600138 matched = true;
139 prev_callback->pNext = cur_callback->pNext;
140 if (*list_head == cur_callback) {
141 *list_head = cur_callback->pNext;
142 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700143 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
144 reinterpret_cast<uint64_t &>(cur_callback->messenger.messenger), 0, 0, "DebugUtilsMessenger",
145 "Destroyed messenger\n");
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600146 } else {
147 matched = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700148 local_severities |= cur_callback->messenger.messageSeverity;
149 local_types |= cur_callback->messenger.messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600150 }
151 prev_callback = cur_callback;
152 cur_callback = cur_callback->pNext;
153 if (matched) {
154 free(prev_callback);
155 }
156 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700157 debug_data->active_severities = local_severities;
158 debug_data->active_types = local_types;
159}
160
161// Remove specified debug message callback node structure from the specified callback linked list
162static inline void RemoveDebugUtilsMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
163 VkDebugReportCallbackEXT callback) {
164 VkLayerDbgFunctionNode *cur_callback = *list_head;
165 VkLayerDbgFunctionNode *prev_callback = cur_callback;
166 bool matched = false;
167 VkFlags local_severities = 0;
168 VkFlags local_types = 0;
169
170 while (cur_callback) {
171 if (!cur_callback->is_messenger && cur_callback->report.msgCallback == callback) {
172 matched = true;
173 prev_callback->pNext = cur_callback->pNext;
174 if (*list_head == cur_callback) {
175 *list_head = cur_callback->pNext;
176 }
177 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
178 reinterpret_cast<uint64_t &>(cur_callback->report.msgCallback), 0, 0, "DebugReport",
179 "Destroyed callback\n");
180 } else {
181 matched = false;
182 VkFlags this_severities = 0;
183 VkFlags this_types = 0;
184 DebugReportFlagsToAnnotFlags(cur_callback->report.msgFlags, true, &this_severities, &this_types);
185 local_severities |= this_severities;
186 local_types |= this_types;
187 }
188 prev_callback = cur_callback;
189 cur_callback = cur_callback->pNext;
190 if (matched) {
191 free(prev_callback);
192 }
193 }
194 debug_data->active_severities = local_severities;
195 debug_data->active_types = local_types;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600196}
197
198// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
199static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
200 VkLayerDbgFunctionNode *current_callback = *list_head;
201 VkLayerDbgFunctionNode *prev_callback = current_callback;
202
203 while (current_callback) {
204 prev_callback = current_callback->pNext;
Mark Young6ba8abe2017-11-09 10:37:04 -0700205 if (!current_callback->is_messenger) {
206 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
207 (uint64_t)current_callback->report.msgCallback, 0, 0, "DebugReport",
208 "Debug Report callbacks not removed before DestroyInstance");
209 } else {
210 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
211 (uint64_t)current_callback->messenger.messenger, 0, 0, "Messenger",
212 "Debug messengers not removed before DestroyInstance");
213 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600214 free(current_callback);
215 current_callback = prev_callback;
216 }
217 *list_head = NULL;
218}
219
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700220// Note that text_vuid is a default parameter, and is optional. See the above forward declaration
Mark Young6ba8abe2017-11-09 10:37:04 -0700221static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
222 uint64_t src_object, size_t location, int32_t msg_code, const char *layer_prefix,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700223 const char *message, const char *text_vuid) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600224 bool bail = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700225 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600226
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600227 if (debug_data->debug_callback_list != NULL) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700228 layer_dbg_node = debug_data->debug_callback_list;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600229 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700230 layer_dbg_node = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600231 }
232
Mark Young6ba8abe2017-11-09 10:37:04 -0700233 VkDebugUtilsMessageSeverityFlagsEXT severity;
234 VkDebugUtilsMessageTypeFlagsEXT types;
235 VkDebugUtilsMessengerCallbackDataEXT callback_data;
236 VkDebugUtilsObjectNameInfoEXT object_name_info;
237
238 // Convert the info to the VK_EXT_debug_utils form in case we need it.
239 DebugReportFlagsToAnnotFlags(msg_flags, true, &severity, &types);
240 object_name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
241 object_name_info.pNext = NULL;
242 object_name_info.objectType = convertDebugReportObjectToCoreObject(object_type);
243 object_name_info.objectHandle = (uint64_t)(uintptr_t)src_object;
244 object_name_info.pObjectName = NULL;
245
246 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
247 callback_data.pNext = NULL;
248 callback_data.flags = 0;
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700249 callback_data.pMessageIdName = text_vuid;
Mark Young6ba8abe2017-11-09 10:37:04 -0700250 callback_data.messageIdNumber = msg_code;
251 callback_data.pMessage = message;
252 callback_data.queueLabelCount = 0;
253 callback_data.pQueueLabels = NULL;
254 callback_data.cmdBufLabelCount = 0;
255 callback_data.pCmdBufLabels = NULL;
256 callback_data.objectCount = 1;
257 callback_data.pObjects = &object_name_info;
258
259 VkDebugUtilsLabelEXT *queue_labels = nullptr;
260 VkDebugUtilsLabelEXT *cmd_buf_labels = nullptr;
261 std::string new_debug_report_message = "";
262 std::ostringstream oss;
Mark Young6ba8abe2017-11-09 10:37:04 -0700263
264 if (0 != src_object) {
Mark Young8504ba62018-03-21 13:35:34 -0600265 oss << "Object: 0x" << std::hex << src_object;
Mark Young6ba8abe2017-11-09 10:37:04 -0700266 // If this is a queue, add any queue labels to the callback data.
267 if (VK_OBJECT_TYPE_QUEUE == object_name_info.objectType) {
268 auto label_iter = debug_data->debugUtilsQueueLabels->find(reinterpret_cast<VkQueue>(src_object));
269 if (label_iter != debug_data->debugUtilsQueueLabels->end()) {
270 queue_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
271 if (nullptr != queue_labels) {
272 // Record the labels, but record them in reverse order since we want the
273 // most recent at the top.
274 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
275 uint32_t last_index = label_size - 1;
276 for (uint32_t label = 0; label < label_size; ++label) {
277 queue_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
278 queue_labels[last_index - label].pNext = nullptr;
279 queue_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
280 queue_labels[last_index - label].color[0] = label_iter->second[label].color[0];
281 queue_labels[last_index - label].color[1] = label_iter->second[label].color[1];
282 queue_labels[last_index - label].color[2] = label_iter->second[label].color[2];
283 queue_labels[last_index - label].color[3] = label_iter->second[label].color[3];
284 }
285 callback_data.queueLabelCount = label_size;
286 callback_data.pQueueLabels = queue_labels;
287 }
288 }
289 // If this is a command buffer, add any command buffer labels to the callback data.
290 } else if (VK_OBJECT_TYPE_COMMAND_BUFFER == object_name_info.objectType) {
291 auto label_iter = debug_data->debugUtilsCmdBufLabels->find(reinterpret_cast<VkCommandBuffer>(src_object));
292 if (label_iter != debug_data->debugUtilsCmdBufLabels->end()) {
293 cmd_buf_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
294 if (nullptr != cmd_buf_labels) {
295 // Record the labels, but record them in reverse order since we want the
296 // most recent at the top.
297 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
298 uint32_t last_index = label_size - 1;
299 for (uint32_t label = 0; label < label_size; ++label) {
300 cmd_buf_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
301 cmd_buf_labels[last_index - label].pNext = nullptr;
302 cmd_buf_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
303 cmd_buf_labels[last_index - label].color[0] = label_iter->second[label].color[0];
304 cmd_buf_labels[last_index - label].color[1] = label_iter->second[label].color[1];
305 cmd_buf_labels[last_index - label].color[2] = label_iter->second[label].color[2];
306 cmd_buf_labels[last_index - label].color[3] = label_iter->second[label].color[3];
307 }
308 callback_data.cmdBufLabelCount = label_size;
309 callback_data.pCmdBufLabels = cmd_buf_labels;
310 }
311 }
312 }
313 // Look for any debug utils or marker names to use for this object
314 callback_data.pObjects[0].pObjectName = NULL;
315 auto utils_name_iter = debug_data->debugUtilsObjectNameMap->find(src_object);
316 if (utils_name_iter != debug_data->debugUtilsObjectNameMap->end()) {
317 callback_data.pObjects[0].pObjectName = utils_name_iter->second.c_str();
318 } else {
319 auto marker_name_iter = debug_data->debugObjectNameMap->find(src_object);
320 if (marker_name_iter != debug_data->debugObjectNameMap->end()) {
321 callback_data.pObjects[0].pObjectName = marker_name_iter->second.c_str();
322 }
323 }
324 if (NULL != callback_data.pObjects[0].pObjectName) {
Mark Young8504ba62018-03-21 13:35:34 -0600325 oss << " (Name = " << callback_data.pObjects[0].pObjectName << " : Type = ";
326 } else {
327 oss << " (Type = ";
Mark Young6ba8abe2017-11-09 10:37:04 -0700328 }
Mark Young8504ba62018-03-21 13:35:34 -0600329 oss << std::to_string(object_type) << ")";
330 } else {
331 oss << "Object: VK_NULL_HANDLE (Type = " << std::to_string(object_type) << ")";
Mark Young6ba8abe2017-11-09 10:37:04 -0700332 }
333 new_debug_report_message += oss.str();
334 new_debug_report_message += " | ";
335 new_debug_report_message += message;
336
337 while (layer_dbg_node) {
Mark Young8504ba62018-03-21 13:35:34 -0600338 // If the app uses the VK_EXT_debug_report extension, call all of those registered callbacks.
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700339 if (!layer_dbg_node->is_messenger && (layer_dbg_node->report.msgFlags & msg_flags)) {
340 if (text_vuid != nullptr) {
341 // If a text vuid is supplied for the old debug report extension, prepend it to the message string
342 new_debug_report_message.insert(0, " ] ");
343 new_debug_report_message.insert(0, text_vuid);
344 new_debug_report_message.insert(0, " [ ");
345 }
346
347 if (layer_dbg_node->report.pfnMsgCallback(msg_flags, object_type, src_object, location, msg_code, layer_prefix,
348 new_debug_report_message.c_str(), layer_dbg_node->pUserData)) {
349 bail = true;
350 }
Mark Young8504ba62018-03-21 13:35:34 -0600351 // If the app uses the VK_EXT_debug_utils extension, call all of those registered callbacks.
Mark Young6ba8abe2017-11-09 10:37:04 -0700352 } else if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & severity) &&
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700353 (layer_dbg_node->messenger.messageType & types)) {
354 if (layer_dbg_node->messenger.pfnUserCallback(static_cast<VkDebugUtilsMessageSeverityFlagBitsEXT>(severity), types,
355 &callback_data, layer_dbg_node->pUserData)) {
356 bail = true;
357 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700358 }
359 layer_dbg_node = layer_dbg_node->pNext;
360 }
361
362 if (nullptr != queue_labels) {
363 delete[] queue_labels;
364 }
365 if (nullptr != cmd_buf_labels) {
366 delete[] cmd_buf_labels;
367 }
368
369 return bail;
370}
371
372static inline void DebugAnnotFlagsToReportFlags(VkDebugUtilsMessageSeverityFlagBitsEXT da_severity,
373 VkDebugUtilsMessageTypeFlagsEXT da_type, VkDebugReportFlagsEXT *dr_flags) {
374 *dr_flags = 0;
375
376 if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
377 *dr_flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
378 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) {
379 if ((da_type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) {
380 *dr_flags |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
381 } else {
382 *dr_flags |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
383 }
384 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) {
385 *dr_flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
386 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0) {
387 *dr_flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
388 }
389}
390
391static inline bool debug_messenger_log_msg(const debug_report_data *debug_data,
392 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
393 VkDebugUtilsMessageTypeFlagsEXT message_type,
394 VkDebugUtilsMessengerCallbackDataEXT *callback_data) {
395 bool bail = false;
396 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
397
398 if (debug_data->debug_callback_list != NULL) {
399 layer_dbg_node = debug_data->debug_callback_list;
400 } else {
401 layer_dbg_node = debug_data->default_debug_callback_list;
402 }
403
404 VkDebugReportFlagsEXT object_flags = 0;
405
406 DebugAnnotFlagsToReportFlags(message_severity, message_type, &object_flags);
407
408 while (layer_dbg_node) {
409 if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & message_severity) &&
410 (layer_dbg_node->messenger.messageType & message_type)) {
411 // Loop through each object and give it the proper name if it was set.
412 for (uint32_t obj = 0; obj < callback_data->objectCount; obj++) {
413 auto it = debug_data->debugUtilsObjectNameMap->find(callback_data->pObjects[obj].objectHandle);
414 if (it == debug_data->debugUtilsObjectNameMap->end()) {
415 continue;
416 }
417 callback_data->pObjects[obj].pObjectName = it->second.c_str();
418 }
419 if (layer_dbg_node->messenger.pfnUserCallback(message_severity, message_type, callback_data,
420 layer_dbg_node->pUserData)) {
421 bail = true;
422 }
423 } else if (!layer_dbg_node->is_messenger && layer_dbg_node->report.msgFlags & object_flags) {
424 auto it = debug_data->debugObjectNameMap->find(callback_data->pObjects[0].objectHandle);
John Zulauf536649b2018-05-01 13:28:27 -0600425 VkDebugReportObjectTypeEXT object_type = convertCoreObjectToDebugReportObject(callback_data->pObjects[0].objectType);
Tony Barbour3431dac2017-06-19 16:50:37 -0600426 if (it == debug_data->debugObjectNameMap->end()) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700427 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
428 callback_data->messageIdNumber, callback_data->pMessageIdName,
429 callback_data->pMessage, layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600430 bail = true;
431 }
432 } else {
433 std::string newMsg = "SrcObject name = ";
434 newMsg.append(it->second.c_str());
435 newMsg.append(" ");
Mark Young6ba8abe2017-11-09 10:37:04 -0700436 newMsg.append(callback_data->pMessage);
437 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
438 callback_data->messageIdNumber, callback_data->pMessageIdName,
439 newMsg.c_str(), layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600440 bail = true;
441 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600442 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600443 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700444 layer_dbg_node = layer_dbg_node->pNext;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600445 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600446
447 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600448}
449
Mark Young6ba8abe2017-11-09 10:37:04 -0700450static inline debug_report_data *debug_utils_create_instance(
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700451 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
Mark Young6ba8abe2017-11-09 10:37:04 -0700452 const char *const *enabled_extensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600453{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600454 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700455 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600456
457 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600458 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600459 // TODO: Check other property fields
Mark Young6ba8abe2017-11-09 10:37:04 -0700460 if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600461 debug_data->g_DEBUG_REPORT = true;
Mark Young6ba8abe2017-11-09 10:37:04 -0700462 } else if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) {
463 debug_data->g_DEBUG_UTILS = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600464 }
465 }
Tony Barbour3431dac2017-06-19 16:50:37 -0600466 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
Mark Young6ba8abe2017-11-09 10:37:04 -0700467 debug_data->debugUtilsObjectNameMap = new std::unordered_map<uint64_t, std::string>;
468 debug_data->debugUtilsQueueLabels = new std::unordered_map<VkQueue, std::vector<LoggingLabelData>>;
469 debug_data->debugUtilsCmdBufLabels = new std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>>;
470 debug_data->queueLabelHasInsert = false;
471 debug_data->cmdBufLabelHasInsert = false;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600472 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600473}
474
Mark Young6ba8abe2017-11-09 10:37:04 -0700475static inline void layer_debug_utils_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600476 if (debug_data) {
477 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
478 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
Tony Barbour3431dac2017-06-19 16:50:37 -0600479 delete debug_data->debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -0700480 delete debug_data->debugUtilsObjectNameMap;
481 delete debug_data->debugUtilsQueueLabels;
482 delete debug_data->debugUtilsCmdBufLabels;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600483 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600484 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600485}
486
Mark Young6ba8abe2017-11-09 10:37:04 -0700487static inline debug_report_data *layer_debug_utils_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600488 // DEBUG_REPORT shares data between Instance and Device,
489 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600490 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600491}
492
Mark Young6ba8abe2017-11-09 10:37:04 -0700493static inline void layer_debug_utils_destroy_device(VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600494 // Nothing to do since we're using instance data record
495}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600496
Mark Young6ba8abe2017-11-09 10:37:04 -0700497static inline void layer_destroy_messenger_callback(debug_report_data *debug_data, VkDebugUtilsMessengerEXT messenger,
498 const VkAllocationCallbacks *allocator) {
499 RemoveDebugUtilsMessenger(debug_data, &debug_data->debug_callback_list, messenger);
500 RemoveDebugUtilsMessenger(debug_data, &debug_data->default_debug_callback_list, messenger);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600501}
502
Mark Young6ba8abe2017-11-09 10:37:04 -0700503static inline VkResult layer_create_messenger_callback(debug_report_data *debug_data, bool default_callback,
504 const VkDebugUtilsMessengerCreateInfoEXT *create_info,
505 const VkAllocationCallbacks *allocator,
506 VkDebugUtilsMessengerEXT *messenger) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700507 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700508 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young6ba8abe2017-11-09 10:37:04 -0700509 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
510 pNewDbgFuncNode->is_messenger = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600511
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600512 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Young6ba8abe2017-11-09 10:37:04 -0700513 if (!(*messenger)) *messenger = (VkDebugUtilsMessengerEXT)pNewDbgFuncNode;
514 pNewDbgFuncNode->messenger.messenger = *messenger;
515 pNewDbgFuncNode->messenger.pfnUserCallback = create_info->pfnUserCallback;
516 pNewDbgFuncNode->messenger.messageSeverity = create_info->messageSeverity;
517 pNewDbgFuncNode->messenger.messageType = create_info->messageType;
518 pNewDbgFuncNode->pUserData = create_info->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600519
Mark Young6ba8abe2017-11-09 10:37:04 -0700520 debug_data->active_severities |= create_info->messageSeverity;
521 debug_data->active_types |= create_info->messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600522 if (default_callback) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700523 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600524 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700525 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600526 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600527
Mark Young6ba8abe2017-11-09 10:37:04 -0700528 VkDebugUtilsMessengerCallbackDataEXT callback_data = {};
529 VkDebugUtilsObjectNameInfoEXT blank_object = {};
530 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
531 callback_data.pNext = NULL;
532 callback_data.flags = 0;
533 callback_data.pMessageIdName = "Layer Internal Message";
534 callback_data.messageIdNumber = 0;
535 callback_data.pMessage = "Added messenger";
536 callback_data.queueLabelCount = 0;
537 callback_data.pQueueLabels = NULL;
538 callback_data.cmdBufLabelCount = 0;
539 callback_data.pCmdBufLabels = NULL;
540 callback_data.objectCount = 1;
541 callback_data.pObjects = &blank_object;
542 blank_object.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
543 blank_object.pNext = NULL;
544 blank_object.objectType = VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT;
545 blank_object.objectHandle = HandleToUint64(*messenger);
546 blank_object.pObjectName = NULL;
547 debug_messenger_log_msg(debug_data, VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,
548 VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT, &callback_data);
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600549 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600550}
551
Mark Young6ba8abe2017-11-09 10:37:04 -0700552static inline void layer_destroy_report_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
553 const VkAllocationCallbacks *allocator) {
554 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
555 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
556}
557
558static inline VkResult layer_create_report_callback(debug_report_data *debug_data, bool default_callback,
559 const VkDebugReportCallbackCreateInfoEXT *create_info,
560 const VkAllocationCallbacks *allocator, VkDebugReportCallbackEXT *callback) {
561 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
562 if (!pNewDbgFuncNode) {
563 return VK_ERROR_OUT_OF_HOST_MEMORY;
564 }
565 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
566 pNewDbgFuncNode->is_messenger = false;
567
568 // Handle of 0 is logging_callback so use allocated Node address as unique handle
569 if (!(*callback)) *callback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
570 pNewDbgFuncNode->report.msgCallback = *callback;
571 pNewDbgFuncNode->report.pfnMsgCallback = create_info->pfnCallback;
572 pNewDbgFuncNode->report.msgFlags = create_info->flags;
573 pNewDbgFuncNode->pUserData = create_info->pUserData;
574
575 VkFlags local_severity = 0;
576 VkFlags local_type = 0;
577 DebugReportFlagsToAnnotFlags(create_info->flags, true, &local_severity, &local_type);
578 debug_data->active_severities |= local_severity;
579 debug_data->active_types |= local_type;
580 if (default_callback) {
581 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
582 } else {
583 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600584 }
585
Mark Young6ba8abe2017-11-09 10:37:04 -0700586 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, (uint64_t)*callback, 0,
587 0, "DebugReport", "Added callback");
588 return VK_SUCCESS;
589}
590
591static inline PFN_vkVoidFunction debug_utils_get_instance_proc_addr(debug_report_data *debug_data, const char *func_name) {
592 if (!debug_data) {
593 return NULL;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600594 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700595 if (debug_data->g_DEBUG_REPORT) {
596 if (!strcmp(func_name, "vkCreateDebugReportCallbackEXT")) {
597 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
598 }
599 if (!strcmp(func_name, "vkDestroyDebugReportCallbackEXT")) {
600 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
601 }
602 if (!strcmp(func_name, "vkDebugReportMessageEXT")) {
603 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
604 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600605 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700606 if (debug_data->g_DEBUG_UTILS) {
607 if (!strcmp(func_name, "vkCreateDebugUtilsMessengerEXT")) {
608 return (PFN_vkVoidFunction)vkCreateDebugUtilsMessengerEXT;
609 }
610 if (!strcmp(func_name, "vkDestroyDebugUtilsMessengerEXT")) {
611 return (PFN_vkVoidFunction)vkDestroyDebugUtilsMessengerEXT;
612 }
613 if (!strcmp(func_name, "vkSubmitDebugUtilsMessageEXT")) {
614 return (PFN_vkVoidFunction)vkSubmitDebugUtilsMessageEXT;
615 }
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700616 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600617 return NULL;
618}
619
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600620// This utility (called at vkCreateInstance() time), looks at a pNext chain.
621// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
622// then allocates an array that can hold that many structs, as well as that
623// many VkDebugReportCallbackEXT handles. It then copies each
624// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
Mark Young6ba8abe2017-11-09 10:37:04 -0700625static inline VkResult layer_copy_tmp_report_callbacks(const void *pChain, uint32_t *num_callbacks,
626 VkDebugReportCallbackCreateInfoEXT **infos,
627 VkDebugReportCallbackEXT **callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600628 uint32_t n = *num_callbacks = 0;
629
630 const void *pNext = pChain;
631 while (pNext) {
632 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
633 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
634 n++;
635 }
636 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
637 }
638 if (n == 0) {
639 return VK_SUCCESS;
640 }
641
642 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
643 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
644 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
645 if (!pInfos) {
646 return VK_ERROR_OUT_OF_HOST_MEMORY;
647 }
648 // 3rd, allocate memory for a unique handle for each callback:
649 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
650 if (!pCallbacks) {
651 free(pInfos);
652 return VK_ERROR_OUT_OF_HOST_MEMORY;
653 }
654 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
655 // vkDestroyInstance, and assign a unique handle to each callback (just
656 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
657 pNext = pChain;
658 while (pNext) {
659 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
660 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
661 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
662 }
663 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
664 }
665
666 *num_callbacks = n;
667 return VK_SUCCESS;
668}
669
Mark Young6ba8abe2017-11-09 10:37:04 -0700670// This utility frees the arrays allocated by layer_copy_tmp_report_callbacks()
671static inline void layer_free_tmp_report_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600672 free(infos);
673 free(callbacks);
674}
675
676// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700677// that were copied by layer_copy_tmp_report_callbacks()
678static inline VkResult layer_enable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
679 VkDebugReportCallbackCreateInfoEXT *infos,
680 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600681 VkResult rtn = VK_SUCCESS;
682 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700683 rtn = layer_create_report_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600684 if (rtn != VK_SUCCESS) {
685 for (uint32_t j = 0; j < i; j++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700686 layer_destroy_report_callback(debug_data, callbacks[j], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600687 }
688 return rtn;
689 }
690 }
691 return rtn;
692}
693
694// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700695// that were copied by layer_copy_tmp_report_callbacks()
696static inline void layer_disable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
697 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600698 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700699 layer_destroy_report_callback(debug_data, callbacks[i], NULL);
700 }
701}
702
703// This utility (called at vkCreateInstance() time), looks at a pNext chain.
704// It counts any VkDebugUtilsMessengerCreateInfoEXT structs that it finds. It
705// then allocates an array that can hold that many structs, as well as that
706// many VkDebugUtilsMessengerEXT handles. It then copies each
707// VkDebugUtilsMessengerCreateInfoEXT, and initializes each handle.
Mark Young8504ba62018-03-21 13:35:34 -0600708static inline VkResult layer_copy_tmp_debug_messengers(const void *pChain, uint32_t *num_messengers,
709 VkDebugUtilsMessengerCreateInfoEXT **infos,
710 VkDebugUtilsMessengerEXT **messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700711 uint32_t n = *num_messengers = 0;
712
713 const void *pNext = pChain;
714 while (pNext) {
715 // 1st, count the number VkDebugUtilsMessengerCreateInfoEXT:
716 if (((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
717 n++;
718 }
719 pNext = (void *)((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->pNext;
720 }
721 if (n == 0) {
722 return VK_SUCCESS;
723 }
724
725 // 2nd, allocate memory for each VkDebugUtilsMessengerCreateInfoEXT:
726 VkDebugUtilsMessengerCreateInfoEXT *pInfos = *infos =
727 ((VkDebugUtilsMessengerCreateInfoEXT *)malloc(n * sizeof(VkDebugUtilsMessengerCreateInfoEXT)));
728 if (!pInfos) {
729 return VK_ERROR_OUT_OF_HOST_MEMORY;
730 }
731 // 3rd, allocate memory for a unique handle for each messenger:
732 VkDebugUtilsMessengerEXT *pMessengers = *messengers =
733 ((VkDebugUtilsMessengerEXT *)malloc(n * sizeof(VkDebugUtilsMessengerEXT)));
734 if (!pMessengers) {
735 free(pInfos);
736 return VK_ERROR_OUT_OF_HOST_MEMORY;
737 }
738 // 4th, copy each VkDebugUtilsMessengerCreateInfoEXT for use by
739 // vkDestroyInstance, and assign a unique handle to each callback (just
740 // use the address of the copied VkDebugUtilsMessengerCreateInfoEXT):
741 pNext = pChain;
742 while (pNext) {
743 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
744 memcpy(pInfos, pNext, sizeof(VkDebugUtilsMessengerCreateInfoEXT));
745 *pMessengers++ = (VkDebugUtilsMessengerEXT)pInfos++;
746 }
747 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
748 }
749
750 *num_messengers = n;
751 return VK_SUCCESS;
752}
753
754// This utility frees the arrays allocated by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600755static inline void layer_free_tmp_debug_messengers(VkDebugUtilsMessengerCreateInfoEXT *infos,
756 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700757 free(infos);
758 free(messengers);
759}
760
761// This utility enables all of the VkDebugUtilsMessengerCreateInfoEXT structs
762// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600763static inline VkResult layer_enable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
764 VkDebugUtilsMessengerCreateInfoEXT *infos,
765 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700766 VkResult rtn = VK_SUCCESS;
767 for (uint32_t i = 0; i < num_messengers; i++) {
768 rtn = layer_create_messenger_callback(debug_data, false, &infos[i], NULL, &messengers[i]);
769 if (rtn != VK_SUCCESS) {
770 for (uint32_t j = 0; j < i; j++) {
771 layer_destroy_messenger_callback(debug_data, messengers[j], NULL);
772 }
773 return rtn;
774 }
775 }
776 return rtn;
777}
778
779// This utility disables all of the VkDebugUtilsMessengerCreateInfoEXT structs
780// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600781static inline void layer_disable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
782 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700783 for (uint32_t i = 0; i < num_messengers; i++) {
784 layer_destroy_messenger_callback(debug_data, messengers[i], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600785 }
786}
787
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600788// Checks if the message will get logged.
789// Allows layer to defer collecting & formating data if the
790// message will be discarded.
Mark Young6ba8abe2017-11-09 10:37:04 -0700791static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msg_flags) {
792 VkFlags local_severity = 0;
793 VkFlags local_type = 0;
794 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
795 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600796 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600797 return false;
798 }
799
800 return true;
801}
John Zulauf6664e272018-01-17 11:00:22 -0700802#ifndef WIN32
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700803static inline int string_sprintf(std::string *output, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
John Zulauf6664e272018-01-17 11:00:22 -0700804#endif
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700805static inline int string_sprintf(std::string *output, const char *fmt, ...) {
John Zulauf6664e272018-01-17 11:00:22 -0700806 std::string &formatted = *output;
807 va_list argptr;
808 va_start(argptr, fmt);
809 int reserve = vsnprintf(nullptr, 0, fmt, argptr);
810 va_end(argptr);
811 formatted.reserve(reserve + 1);
812 va_start(argptr, fmt);
813 int result = vsnprintf((char *)formatted.data(), formatted.capacity(), fmt, argptr);
814 va_end(argptr);
815 assert(result == reserve);
816 return result;
817}
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600818
Chris Forbes8a25bce2016-03-24 12:06:35 +1300819#ifdef WIN32
820static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
821 *strp = nullptr;
822 int size = _vscprintf(fmt, ap);
823 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700824 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300825 if (!*strp) {
826 return -1;
827 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700828 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300829 }
830 return size;
831}
832#endif
833
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700834// Output log message via DEBUG_REPORT. Takes format and variable arg list so that output string is only computed if a message
835// needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800836#ifndef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700837static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600838 uint64_t src_object, int32_t msg_code, const char *format, ...) __attribute__((format(printf, 6, 7)));
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700839static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
840 uint64_t src_object, std::string vuid_text, const char *format, ...)
841 __attribute__((format(printf, 6, 7)));
Michael Lentine010f4692015-11-03 16:19:46 -0800842#endif
Mark Young6ba8abe2017-11-09 10:37:04 -0700843static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600844 uint64_t src_object, int32_t msg_code, const char *format, ...) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700845 VkFlags local_severity = 0;
846 VkFlags local_type = 0;
847 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
848 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600849 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600850 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600851 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600852
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600853 va_list argptr;
854 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300855 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200856 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600857 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200858 str = nullptr;
859 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600860 va_end(argptr);
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600861
862 std::string str_plus_spec_text(str);
863
864 // If the msg_code is in the error map, tack on spec text to error message.
865 if (validation_error_map.find(msg_code) != validation_error_map.end()) {
866 str_plus_spec_text += " ";
867 str_plus_spec_text += validation_error_map[msg_code];
868 }
869
Mark Lobodzinski88529492018-04-01 10:38:15 -0600870 bool result = debug_log_msg(debug_data, msg_flags, object_type, src_object, 0, msg_code, "Validation",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600871 str_plus_spec_text.c_str() ? str_plus_spec_text.c_str() : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300872 free(str);
873 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600874}
875
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700876// Overload of log_msg that takes a VUID string in place of a numerical VUID abstraction
877static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
878 uint64_t src_object, std::string vuid_text, const char *format, ...) {
879 VkFlags local_severity = 0;
880 VkFlags local_type = 0;
881 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
882 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
883 // Message is not wanted
884 return false;
885 }
886
887 va_list argptr;
888 va_start(argptr, format);
889 char *str;
890 if (-1 == vasprintf(&str, format, argptr)) {
891 // On failure, glibc vasprintf leaves str undefined
892 str = nullptr;
893 }
894 va_end(argptr);
895
896 std::string str_plus_spec_text(str);
897
Dave Houlton8e9f6542018-05-18 12:18:22 -0600898 // If the vuid string is in the error map: find the legacy enum, look up spec text, and tack it onto error message.
899 int32_t legacy_vuid_enum = VALIDATION_ERROR_UNDEFINED;
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700900 if (validation_error_text_map.find(vuid_text.c_str()) != validation_error_text_map.end()) {
Dave Houlton8e9f6542018-05-18 12:18:22 -0600901 legacy_vuid_enum = validation_error_text_map[vuid_text.c_str()];
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700902 str_plus_spec_text += " ";
Dave Houlton8e9f6542018-05-18 12:18:22 -0600903 str_plus_spec_text += validation_error_map[legacy_vuid_enum];
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700904 }
905
Dave Houlton8e9f6542018-05-18 12:18:22 -0600906 // Append layer prefix with VUID string, pass in recovered legacy numerical VUID
907 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 -0700908 str_plus_spec_text.c_str() ? str_plus_spec_text.c_str() : "Allocation failure", vuid_text.c_str());
909
910 free(str);
911 return result;
912}
913
Mark Young6ba8abe2017-11-09 10:37:04 -0700914static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_log_callback(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
915 uint64_t src_object, size_t location, int32_t msg_code,
916 const char *layer_prefix, const char *message, void *user_data) {
917 char msg_flag_string[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600918
Mark Young6ba8abe2017-11-09 10:37:04 -0700919 PrintMessageFlags(msg_flags, msg_flag_string);
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600920
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600921 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 -0700922 fflush((FILE *)user_data);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600923
924 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600925}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600926
Mark Young6ba8abe2017-11-09 10:37:04 -0700927static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_win32_debug_output_msg(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
928 uint64_t src_object, size_t location, int32_t msg_code,
929 const char *layer_prefix, const char *message,
930 void *user_data) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600931#ifdef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700932 char msg_flag_string[30];
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600933 char buf[2048];
934
Mark Young6ba8abe2017-11-09 10:37:04 -0700935 PrintMessageFlags(msg_flags, msg_flag_string);
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600936 _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 -0600937
938 OutputDebugString(buf);
939#endif
940
941 return false;
942}
943
Mark Young6ba8abe2017-11-09 10:37:04 -0700944static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT obj_type,
945 uint64_t src_object, size_t location, int32_t msg_code,
946 const char *layer_prefix, const char *message, void *user_data) {
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600947#ifdef WIN32
948 DebugBreak();
949#else
950 raise(SIGTRAP);
951#endif
952
953 return false;
954}
955
Mark Young6ba8abe2017-11-09 10:37:04 -0700956static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_log_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
957 VkDebugUtilsMessageTypeFlagsEXT message_type,
958 const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
959 void *user_data) {
960 char msg_severity[30];
961 char msg_type[30];
962
963 PrintMessageSeverity(message_severity, msg_severity);
964 PrintMessageType(message_type, msg_type);
965
Mark Young8504ba62018-03-21 13:35:34 -0600966 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 -0700967 callback_data->messageIdNumber, callback_data->pMessage);
Mark Young8504ba62018-03-21 13:35:34 -0600968 fprintf((FILE *)user_data, " Objects: %d\n", callback_data->objectCount);
969 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
970 fprintf((FILE *)user_data, " [%d] 0x%" PRIx64 ", type: %d, name: %s\n", obj,
971 callback_data->pObjects[obj].objectHandle, callback_data->pObjects[obj].objectType,
972 callback_data->pObjects[obj].pObjectName);
973 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700974 fflush((FILE *)user_data);
975
976 return false;
Petr Krause9388f62017-05-13 20:53:12 +0200977}
978
Mark Young6ba8abe2017-11-09 10:37:04 -0700979static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_win32_debug_output_msg(
980 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type,
981 const VkDebugUtilsMessengerCallbackDataEXT *callback_data, void *user_data) {
982#ifdef WIN32
983 char buf[2048];
984 char msg_severity[30];
985 char msg_type[30];
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600986
Mark Young6ba8abe2017-11-09 10:37:04 -0700987 PrintMessageSeverity(message_severity, msg_severity);
988 PrintMessageType(message_type, msg_type);
989
Mark Young8504ba62018-03-21 13:35:34 -0600990 size_t buffer_space = sizeof(buf) - 1;
991 size_t remaining_space = buffer_space;
992 _snprintf(buf, sizeof(buf) - 1, "%s(%s / %s): msgNum: %d - %s\n", callback_data->pMessageIdName, msg_severity, msg_type,
993 callback_data->messageIdNumber, callback_data->pMessage);
994 remaining_space = buffer_space - strlen(buf);
995 _snprintf(buf, remaining_space, " Objects: %d\n", callback_data->objectCount);
996 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
997 remaining_space = buffer_space - strlen(buf);
998 if (remaining_space > 0) {
999 _snprintf(buf, remaining_space, " [%d] 0x%" PRIx64 ", type: %d, name: %s\n", obj,
1000 callback_data->pObjects[obj].objectHandle, callback_data->pObjects[obj].objectType,
1001 callback_data->pObjects[obj].pObjectName);
1002 }
1003 }
Mark Young6ba8abe2017-11-09 10:37:04 -07001004 OutputDebugString(buf);
1005#endif
1006
1007 return false;
1008}
1009
1010// This utility converts from the VkDebugUtilsLabelEXT structure into the logging version of the structure.
1011// In the logging version, we only record what we absolutely need to convey back to the callbacks.
1012static inline void InsertLabelIntoLog(const VkDebugUtilsLabelEXT *utils_label, std::vector<LoggingLabelData> &log_vector) {
1013 LoggingLabelData log_label_data = {};
1014 log_label_data.name = utils_label->pLabelName;
1015 log_label_data.color[0] = utils_label->color[0];
1016 log_label_data.color[1] = utils_label->color[1];
1017 log_label_data.color[2] = utils_label->color[2];
1018 log_label_data.color[3] = utils_label->color[3];
1019 log_vector.push_back(log_label_data);
1020}
1021
Mark Young8504ba62018-03-21 13:35:34 -06001022static inline void BeginQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1023 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001024 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1025 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1026 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1027 std::vector<LoggingLabelData> new_queue_labels;
1028 InsertLabelIntoLog(label_info, new_queue_labels);
1029 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1030 } else {
1031 // If the last thing was a label insert, we need to pop it off of the label vector before any
1032 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1033 // temporary location that exists until the next operation occurs. In this case, a new
1034 // "vkQueueBeginDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1035 if (report_data->queueLabelHasInsert) {
1036 report_data->queueLabelHasInsert = false;
1037 label_iter->second.pop_back();
1038 }
1039 InsertLabelIntoLog(label_info, label_iter->second);
1040 }
1041 }
1042}
1043
Mark Young8504ba62018-03-21 13:35:34 -06001044static inline void EndQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001045 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1046 if (label_iter != report_data->debugUtilsQueueLabels->end()) {
1047 // If the last thing was a label insert, we need to pop it off of the label vector before any
1048 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1049 // temporary location that exists until the next operation occurs. In this case, a
1050 // "vkQueueEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1051 if (report_data->queueLabelHasInsert) {
1052 report_data->queueLabelHasInsert = false;
1053 label_iter->second.pop_back();
1054 }
1055 // Now pop the normal item
1056 label_iter->second.pop_back();
1057 }
1058}
1059
Mark Young8504ba62018-03-21 13:35:34 -06001060static inline void InsertQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1061 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001062 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1063 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1064 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1065 std::vector<LoggingLabelData> new_queue_labels;
1066 InsertLabelIntoLog(label_info, new_queue_labels);
1067 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1068 } else {
1069 // If the last thing was a label insert, we need to pop it off of the label vector before any
1070 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1071 // temporary location that exists until the next operation occurs. In this case, a new
1072 // "vkQueueInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1073 if (report_data->queueLabelHasInsert) {
1074 label_iter->second.pop_back();
1075 }
1076 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1077 // the next queue label operation.
1078 InsertLabelIntoLog(label_info, label_iter->second);
1079 report_data->queueLabelHasInsert = true;
1080 }
1081 }
1082}
1083
Mark Young8504ba62018-03-21 13:35:34 -06001084static inline void BeginCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1085 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001086 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1087 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1088 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1089 std::vector<LoggingLabelData> new_cmdbuf_labels;
1090 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1091 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1092 } else {
1093 // If the last thing was a label insert, we need to pop it off of the label vector before any
1094 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1095 // temporary location that exists until the next operation occurs. In this case, a
1096 // "vkCmdBeginDebugUtilsLabelEXT" has occurred erasing the inserted label.
1097 if (report_data->cmdBufLabelHasInsert) {
1098 report_data->cmdBufLabelHasInsert = false;
1099 label_iter->second.pop_back();
1100 }
1101 InsertLabelIntoLog(label_info, label_iter->second);
1102 }
1103 }
1104}
1105
Mark Young8504ba62018-03-21 13:35:34 -06001106static inline void EndCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001107 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1108 if (label_iter != report_data->debugUtilsCmdBufLabels->end()) {
1109 // If the last thing was a label insert, we need to pop it off of the label vector before any
1110 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1111 // temporary location that exists until the next operation occurs. In this case, a
1112 // "vkCmdEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1113 if (report_data->cmdBufLabelHasInsert) {
1114 report_data->cmdBufLabelHasInsert = false;
1115 label_iter->second.pop_back();
1116 }
1117 // Now pop the normal item
1118 label_iter->second.pop_back();
1119 }
1120}
1121
Mark Young8504ba62018-03-21 13:35:34 -06001122static inline void InsertCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1123 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001124 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1125 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1126 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1127 std::vector<LoggingLabelData> new_cmdbuf_labels;
1128 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1129 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1130 } else {
1131 // If the last thing was a label insert, we need to pop it off of the label vector before any
1132 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1133 // temporary location that exists until the next operation occurs. In this case, a new
1134 // "vkCmdInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1135 if (report_data->cmdBufLabelHasInsert) {
1136 label_iter->second.pop_back();
1137 }
1138 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1139 // the next command buffer label operation.
1140 InsertLabelIntoLog(label_info, label_iter->second);
1141 report_data->cmdBufLabelHasInsert = true;
1142 }
1143 }
1144}
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001145
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001146#endif // LAYER_LOGGING_H