blob: 7521b8d72a098804e7b53f41ff79a69937aef78c [file] [log] [blame]
Dave Houlton4d9b2f82018-10-24 18:21:06 -06001/* Copyright (c) 2015-2018 The Khronos Group Inc.
2 * Copyright (c) 2015-2018 Valve Corporation
3 * Copyright (c) 2015-2018 LunarG, Inc.
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -06004 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060016 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060017 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
18 * Author: Tobin Ehlis <tobin@lunarg.com>
Mark Young6ba8abe2017-11-09 10:37:04 -070019 * Author: Mark Young <marky@lunarg.com>
Dave Houlton4d9b2f82018-10-24 18:21:06 -060020 * Author: Dave Houlton <daveh@lunarg.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060021 *
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060022 */
23
24#ifndef LAYER_LOGGING_H
25#define LAYER_LOGGING_H
26
Mark Lobodzinskib87f9022016-05-24 16:04:56 -060027#include "vk_loader_layer.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060028#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060029#include "vk_layer_data.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060030#include "vk_loader_platform.h"
31#include "vulkan/vk_layer.h"
Mark Young6ba8abe2017-11-09 10:37:04 -070032#include "vk_object_types.h"
Mark Lobodzinski487a0d12018-03-30 10:09:03 -060033#include "vk_validation_error_messages.h"
Lenny Komow4c0da772018-07-03 10:17:21 -060034#include "vk_layer_dispatch_table.h"
Mark Lobodzinskic9d81652017-08-10 11:01:17 -060035#include <signal.h>
Karl Schultzd7f37542016-05-10 11:36:08 -060036#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060037#include <stdarg.h>
38#include <stdbool.h>
39#include <stdio.h>
40#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060041#include <vector>
Mark Young6ba8abe2017-11-09 10:37:04 -070042#include <sstream>
43#include <string>
44
Dave Houlton57ae22f2018-05-18 16:20:52 -060045// Suppress unused warning on Linux
46#if defined(__GNUC__)
Dave Houltoncfcecbf2018-05-31 16:20:57 -060047#define DECORATE_UNUSED __attribute__((unused))
48#else
49#define DECORATE_UNUSED
Dave Houlton57ae22f2018-05-18 16:20:52 -060050#endif
51
Mark Lobodzinski706e15a2018-12-12 15:35:29 -070052#if defined __ANDROID__
53#include <android/log.h>
54#define LOGCONSOLE(...) ((void)__android_log_print(ANDROID_LOG_INFO, "VALIDATION", __VA_ARGS__))
55#else
56#define LOGCONSOLE(...) \
57 { \
58 printf(__VA_ARGS__); \
59 printf("\n"); \
60 }
61#endif
62
Dave Houltoncfcecbf2018-05-31 16:20:57 -060063static const char DECORATE_UNUSED *kVUIDUndefined = "VUID_Undefined";
Dave Houlton8e9f6542018-05-18 12:18:22 -060064
Dave Houltoncfcecbf2018-05-31 16:20:57 -060065#undef DECORATE_UNUSED
Dave Houlton57ae22f2018-05-18 16:20:52 -060066
Mark Young6ba8abe2017-11-09 10:37:04 -070067// TODO: Could be autogenerated for the specific handles for extra type safety...
68template <typename HANDLE_T>
69static inline uint64_t HandleToUint64(HANDLE_T *h) {
70 return reinterpret_cast<uint64_t>(h);
71}
72
73static inline uint64_t HandleToUint64(uint64_t h) { return h; }
74
75// Data we store per label for logging
76typedef struct _LoggingLabelData {
77 std::string name;
78 float color[4];
79} LoggingLabelData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060080
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060081typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060082 VkLayerDbgFunctionNode *debug_callback_list;
83 VkLayerDbgFunctionNode *default_debug_callback_list;
Mark Young6ba8abe2017-11-09 10:37:04 -070084 VkDebugUtilsMessageSeverityFlagsEXT active_severities;
85 VkDebugUtilsMessageTypeFlagsEXT active_types;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060086 bool g_DEBUG_REPORT;
Mark Young6ba8abe2017-11-09 10:37:04 -070087 bool g_DEBUG_UTILS;
Tony Barbour3431dac2017-06-19 16:50:37 -060088 std::unordered_map<uint64_t, std::string> *debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -070089 std::unordered_map<uint64_t, std::string> *debugUtilsObjectNameMap;
90 std::unordered_map<VkQueue, std::vector<LoggingLabelData>> *debugUtilsQueueLabels;
91 bool queueLabelHasInsert;
92 std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>> *debugUtilsCmdBufLabels;
93 bool cmdBufLabelHasInsert;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060094} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060095
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070096template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
Jon Ashburn5484e0c2016-03-08 17:48:44 -070097 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060098
Mark Young6ba8abe2017-11-09 10:37:04 -070099static inline void DebugReportFlagsToAnnotFlags(VkDebugReportFlagsEXT dr_flags, bool default_flag_is_spec,
100 VkDebugUtilsMessageSeverityFlagsEXT *da_severity,
101 VkDebugUtilsMessageTypeFlagsEXT *da_type) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700102 *da_severity = 0;
Mark Youngfbaae712018-10-04 14:33:50 -0600103 *da_type = 0;
Mark Youngc2347792018-05-30 08:41:25 -0600104 // If it's explicitly listed as a performance warning, treat it as a performance message.
105 // Otherwise, treat it as a validation issue.
106 if ((dr_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) != 0) {
Mark Youngfbaae712018-10-04 14:33:50 -0600107 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
108 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
Mark Youngc2347792018-05-30 08:41:25 -0600109 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700110 if ((dr_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) != 0) {
Mark Youngfbaae712018-10-04 14:33:50 -0600111 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700112 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700113 }
114 if ((dr_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) != 0) {
Mark Youngfbaae712018-10-04 14:33:50 -0600115 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700116 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700117 }
Mark Youngfbaae712018-10-04 14:33:50 -0600118 if ((dr_flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) {
119 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700120 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
121 }
122 if ((dr_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) {
Mark Youngfbaae712018-10-04 14:33:50 -0600123 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Young6ba8abe2017-11-09 10:37:04 -0700124 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
125 }
126}
127
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600128// Forward Declarations
Mark Young6ba8abe2017-11-09 10:37:04 -0700129static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Dave Houlton407df732018-08-06 17:58:24 -0600130 uint64_t src_object, size_t location, const char *layer_prefix, const char *message,
131 const char *text_vuid);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600132
133// Add a debug message callback node structure to the specified callback linked list
Mark Young6ba8abe2017-11-09 10:37:04 -0700134static inline void AddDebugCallbackNode(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
135 VkLayerDbgFunctionNode *new_node) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600136 new_node->pNext = *list_head;
137 *list_head = new_node;
138}
139
Mark Young6ba8abe2017-11-09 10:37:04 -0700140// Remove specified debug messenger node structure from the specified linked list
141static inline void RemoveDebugUtilsMessenger(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
142 VkDebugUtilsMessengerEXT messenger) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600143 VkLayerDbgFunctionNode *cur_callback = *list_head;
Mark Youngfbaae712018-10-04 14:33:50 -0600144 VkLayerDbgFunctionNode *prev_callback = nullptr;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600145 bool matched = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700146 VkFlags local_severities = 0;
147 VkFlags local_types = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600148
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600149 while (cur_callback) {
Mark Youngfbaae712018-10-04 14:33:50 -0600150 if (cur_callback->is_messenger) {
151 // If it's actually a messenger, then set it up for deletion.
152 if (cur_callback->messenger.messenger == messenger) {
153 matched = true;
154 if (*list_head == cur_callback) {
155 *list_head = cur_callback->pNext;
156 } else {
157 assert(nullptr != prev_callback);
158 prev_callback->pNext = cur_callback->pNext;
159 }
160 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
161 reinterpret_cast<uint64_t &>(cur_callback->messenger.messenger), 0, "DebugUtilsMessenger",
162 "Destroyed messenger\n", kVUIDUndefined);
163 } else {
164 // If it's not the one we're looking for, just keep the types/severities
165 local_severities |= cur_callback->messenger.messageSeverity;
166 local_types |= cur_callback->messenger.messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600167 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600168 } else {
Mark Youngfbaae712018-10-04 14:33:50 -0600169 // If it's not a messenger, just keep the types/severities
170 VkFlags this_severities = 0;
171 VkFlags this_types = 0;
172 DebugReportFlagsToAnnotFlags(cur_callback->report.msgFlags, true, &this_severities, &this_types);
173 local_severities |= this_severities;
174 local_types |= this_types;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600175 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600176 if (matched) {
Mark Youngfbaae712018-10-04 14:33:50 -0600177 free(cur_callback);
178 matched = false;
179 // Intentionally keep the last prev_callback, but select the proper cur_callback
180 if (nullptr != prev_callback) {
181 cur_callback = prev_callback->pNext;
182 } else {
183 cur_callback = *list_head;
184 }
185 } else {
186 prev_callback = cur_callback;
187 cur_callback = cur_callback->pNext;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600188 }
189 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700190 debug_data->active_severities = local_severities;
191 debug_data->active_types = local_types;
192}
193
194// Remove specified debug message callback node structure from the specified callback linked list
195static inline void RemoveDebugUtilsMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
196 VkDebugReportCallbackEXT callback) {
197 VkLayerDbgFunctionNode *cur_callback = *list_head;
Mark Youngfbaae712018-10-04 14:33:50 -0600198 VkLayerDbgFunctionNode *prev_callback = nullptr;
Mark Young6ba8abe2017-11-09 10:37:04 -0700199 bool matched = false;
200 VkFlags local_severities = 0;
201 VkFlags local_types = 0;
202
203 while (cur_callback) {
Mark Youngfbaae712018-10-04 14:33:50 -0600204 if (!cur_callback->is_messenger) {
205 // If it's actually a callback, then set it up for deletion.
206 if (cur_callback->report.msgCallback == callback) {
207 matched = true;
208 if (*list_head == cur_callback) {
209 *list_head = cur_callback->pNext;
210 } else {
211 assert(nullptr != prev_callback);
212 prev_callback->pNext = cur_callback->pNext;
213 }
214 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
215 reinterpret_cast<uint64_t &>(cur_callback->report.msgCallback), 0, "DebugReport",
216 "Destroyed callback\n", kVUIDUndefined);
217 } else {
218 // If it's not the one we're looking for, just keep the types/severities
219 VkFlags this_severities = 0;
220 VkFlags this_types = 0;
221 DebugReportFlagsToAnnotFlags(cur_callback->report.msgFlags, true, &this_severities, &this_types);
222 local_severities |= this_severities;
223 local_types |= this_types;
Mark Young6ba8abe2017-11-09 10:37:04 -0700224 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700225 } else {
Mark Youngfbaae712018-10-04 14:33:50 -0600226 // If it's not a callback, just keep the types/severities
227 local_severities |= cur_callback->messenger.messageSeverity;
228 local_types |= cur_callback->messenger.messageType;
Mark Young6ba8abe2017-11-09 10:37:04 -0700229 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700230 if (matched) {
Mark Youngfbaae712018-10-04 14:33:50 -0600231 free(cur_callback);
232 matched = false;
233 // Intentionally keep the last prev_callback, but select the proper cur_callback
234 if (nullptr != prev_callback) {
235 cur_callback = prev_callback->pNext;
236 } else {
237 cur_callback = *list_head;
238 }
239 } else {
240 prev_callback = cur_callback;
241 cur_callback = cur_callback->pNext;
Mark Young6ba8abe2017-11-09 10:37:04 -0700242 }
243 }
244 debug_data->active_severities = local_severities;
245 debug_data->active_types = local_types;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600246}
247
248// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
249static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
250 VkLayerDbgFunctionNode *current_callback = *list_head;
251 VkLayerDbgFunctionNode *prev_callback = current_callback;
252
253 while (current_callback) {
254 prev_callback = current_callback->pNext;
Mark Young6ba8abe2017-11-09 10:37:04 -0700255 if (!current_callback->is_messenger) {
256 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Dave Houlton407df732018-08-06 17:58:24 -0600257 (uint64_t)current_callback->report.msgCallback, 0, "DebugReport",
258 "Debug Report callbacks not removed before DestroyInstance", kVUIDUndefined);
Mark Young6ba8abe2017-11-09 10:37:04 -0700259 } else {
260 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Dave Houlton407df732018-08-06 17:58:24 -0600261 (uint64_t)current_callback->messenger.messenger, 0, "Messenger",
262 "Debug messengers not removed before DestroyInstance", kVUIDUndefined);
Mark Young6ba8abe2017-11-09 10:37:04 -0700263 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600264 free(current_callback);
265 current_callback = prev_callback;
266 }
267 *list_head = NULL;
268}
269
Mark Young6ba8abe2017-11-09 10:37:04 -0700270static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Dave Houlton407df732018-08-06 17:58:24 -0600271 uint64_t src_object, size_t location, const char *layer_prefix, const char *message,
272 const char *text_vuid) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600273 bool bail = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700274 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600275
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600276 if (debug_data->debug_callback_list != NULL) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700277 layer_dbg_node = debug_data->debug_callback_list;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600278 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700279 layer_dbg_node = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600280 }
281
Mark Young6ba8abe2017-11-09 10:37:04 -0700282 VkDebugUtilsMessageSeverityFlagsEXT severity;
283 VkDebugUtilsMessageTypeFlagsEXT types;
284 VkDebugUtilsMessengerCallbackDataEXT callback_data;
285 VkDebugUtilsObjectNameInfoEXT object_name_info;
286
287 // Convert the info to the VK_EXT_debug_utils form in case we need it.
288 DebugReportFlagsToAnnotFlags(msg_flags, true, &severity, &types);
289 object_name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
290 object_name_info.pNext = NULL;
291 object_name_info.objectType = convertDebugReportObjectToCoreObject(object_type);
292 object_name_info.objectHandle = (uint64_t)(uintptr_t)src_object;
293 object_name_info.pObjectName = NULL;
294
295 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
296 callback_data.pNext = NULL;
297 callback_data.flags = 0;
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700298 callback_data.pMessageIdName = text_vuid;
Dave Houlton407df732018-08-06 17:58:24 -0600299 callback_data.messageIdNumber = 0; // deprecated, validation layers use only the pMessageIdName
Mark Young6ba8abe2017-11-09 10:37:04 -0700300 callback_data.pMessage = message;
301 callback_data.queueLabelCount = 0;
302 callback_data.pQueueLabels = NULL;
303 callback_data.cmdBufLabelCount = 0;
304 callback_data.pCmdBufLabels = NULL;
305 callback_data.objectCount = 1;
306 callback_data.pObjects = &object_name_info;
307
308 VkDebugUtilsLabelEXT *queue_labels = nullptr;
309 VkDebugUtilsLabelEXT *cmd_buf_labels = nullptr;
310 std::string new_debug_report_message = "";
311 std::ostringstream oss;
Mark Young6ba8abe2017-11-09 10:37:04 -0700312
313 if (0 != src_object) {
Mark Young8504ba62018-03-21 13:35:34 -0600314 oss << "Object: 0x" << std::hex << src_object;
Mark Young6ba8abe2017-11-09 10:37:04 -0700315 // If this is a queue, add any queue labels to the callback data.
316 if (VK_OBJECT_TYPE_QUEUE == object_name_info.objectType) {
317 auto label_iter = debug_data->debugUtilsQueueLabels->find(reinterpret_cast<VkQueue>(src_object));
318 if (label_iter != debug_data->debugUtilsQueueLabels->end()) {
319 queue_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
320 if (nullptr != queue_labels) {
321 // Record the labels, but record them in reverse order since we want the
322 // most recent at the top.
323 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
324 uint32_t last_index = label_size - 1;
325 for (uint32_t label = 0; label < label_size; ++label) {
326 queue_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
327 queue_labels[last_index - label].pNext = nullptr;
328 queue_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
329 queue_labels[last_index - label].color[0] = label_iter->second[label].color[0];
330 queue_labels[last_index - label].color[1] = label_iter->second[label].color[1];
331 queue_labels[last_index - label].color[2] = label_iter->second[label].color[2];
332 queue_labels[last_index - label].color[3] = label_iter->second[label].color[3];
333 }
334 callback_data.queueLabelCount = label_size;
335 callback_data.pQueueLabels = queue_labels;
336 }
337 }
338 // If this is a command buffer, add any command buffer labels to the callback data.
339 } else if (VK_OBJECT_TYPE_COMMAND_BUFFER == object_name_info.objectType) {
340 auto label_iter = debug_data->debugUtilsCmdBufLabels->find(reinterpret_cast<VkCommandBuffer>(src_object));
341 if (label_iter != debug_data->debugUtilsCmdBufLabels->end()) {
342 cmd_buf_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
343 if (nullptr != cmd_buf_labels) {
344 // Record the labels, but record them in reverse order since we want the
345 // most recent at the top.
346 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
347 uint32_t last_index = label_size - 1;
348 for (uint32_t label = 0; label < label_size; ++label) {
349 cmd_buf_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
350 cmd_buf_labels[last_index - label].pNext = nullptr;
351 cmd_buf_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
352 cmd_buf_labels[last_index - label].color[0] = label_iter->second[label].color[0];
353 cmd_buf_labels[last_index - label].color[1] = label_iter->second[label].color[1];
354 cmd_buf_labels[last_index - label].color[2] = label_iter->second[label].color[2];
355 cmd_buf_labels[last_index - label].color[3] = label_iter->second[label].color[3];
356 }
357 callback_data.cmdBufLabelCount = label_size;
358 callback_data.pCmdBufLabels = cmd_buf_labels;
359 }
360 }
361 }
362 // Look for any debug utils or marker names to use for this object
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700363 object_name_info.pObjectName = NULL;
Mark Young6ba8abe2017-11-09 10:37:04 -0700364 auto utils_name_iter = debug_data->debugUtilsObjectNameMap->find(src_object);
365 if (utils_name_iter != debug_data->debugUtilsObjectNameMap->end()) {
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700366 object_name_info.pObjectName = utils_name_iter->second.c_str();
Mark Young6ba8abe2017-11-09 10:37:04 -0700367 } else {
368 auto marker_name_iter = debug_data->debugObjectNameMap->find(src_object);
369 if (marker_name_iter != debug_data->debugObjectNameMap->end()) {
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700370 object_name_info.pObjectName = marker_name_iter->second.c_str();
Mark Young6ba8abe2017-11-09 10:37:04 -0700371 }
372 }
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700373 if (NULL != object_name_info.pObjectName) {
374 oss << " (Name = " << object_name_info.pObjectName << " : Type = ";
Mark Young8504ba62018-03-21 13:35:34 -0600375 } else {
376 oss << " (Type = ";
Mark Young6ba8abe2017-11-09 10:37:04 -0700377 }
Mark Young8504ba62018-03-21 13:35:34 -0600378 oss << std::to_string(object_type) << ")";
379 } else {
380 oss << "Object: VK_NULL_HANDLE (Type = " << std::to_string(object_type) << ")";
Mark Young6ba8abe2017-11-09 10:37:04 -0700381 }
382 new_debug_report_message += oss.str();
383 new_debug_report_message += " | ";
384 new_debug_report_message += message;
385
386 while (layer_dbg_node) {
Mark Young8504ba62018-03-21 13:35:34 -0600387 // If the app uses the VK_EXT_debug_report extension, call all of those registered callbacks.
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700388 if (!layer_dbg_node->is_messenger && (layer_dbg_node->report.msgFlags & msg_flags)) {
389 if (text_vuid != nullptr) {
390 // If a text vuid is supplied for the old debug report extension, prepend it to the message string
391 new_debug_report_message.insert(0, " ] ");
392 new_debug_report_message.insert(0, text_vuid);
393 new_debug_report_message.insert(0, " [ ");
394 }
395
Dave Houlton407df732018-08-06 17:58:24 -0600396 if (layer_dbg_node->report.pfnMsgCallback(msg_flags, object_type, src_object, location, 0, layer_prefix,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700397 new_debug_report_message.c_str(), layer_dbg_node->pUserData)) {
398 bail = true;
399 }
Mark Young8504ba62018-03-21 13:35:34 -0600400 // If the app uses the VK_EXT_debug_utils extension, call all of those registered callbacks.
Mark Young6ba8abe2017-11-09 10:37:04 -0700401 } else if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & severity) &&
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700402 (layer_dbg_node->messenger.messageType & types)) {
403 if (layer_dbg_node->messenger.pfnUserCallback(static_cast<VkDebugUtilsMessageSeverityFlagBitsEXT>(severity), types,
404 &callback_data, layer_dbg_node->pUserData)) {
405 bail = true;
406 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700407 }
408 layer_dbg_node = layer_dbg_node->pNext;
409 }
410
411 if (nullptr != queue_labels) {
412 delete[] queue_labels;
413 }
414 if (nullptr != cmd_buf_labels) {
415 delete[] cmd_buf_labels;
416 }
417
418 return bail;
419}
420
421static inline void DebugAnnotFlagsToReportFlags(VkDebugUtilsMessageSeverityFlagBitsEXT da_severity,
422 VkDebugUtilsMessageTypeFlagsEXT da_type, VkDebugReportFlagsEXT *dr_flags) {
423 *dr_flags = 0;
424
425 if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
426 *dr_flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
427 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) {
428 if ((da_type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) {
429 *dr_flags |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
430 } else {
431 *dr_flags |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
432 }
433 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) {
434 *dr_flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
435 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0) {
436 *dr_flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
437 }
438}
439
440static inline bool debug_messenger_log_msg(const debug_report_data *debug_data,
441 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
442 VkDebugUtilsMessageTypeFlagsEXT message_type,
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700443 VkDebugUtilsMessengerCallbackDataEXT *callback_data,
444 const VkDebugUtilsMessengerEXT *messenger) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700445 bool bail = false;
446 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
447
448 if (debug_data->debug_callback_list != NULL) {
449 layer_dbg_node = debug_data->debug_callback_list;
450 } else {
451 layer_dbg_node = debug_data->default_debug_callback_list;
452 }
453
454 VkDebugReportFlagsEXT object_flags = 0;
455
456 DebugAnnotFlagsToReportFlags(message_severity, message_type, &object_flags);
457
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700458 VkDebugUtilsObjectNameInfoEXT object_name_info;
459 object_name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
460 object_name_info.pNext = NULL;
461 object_name_info.objectType = VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT;
462 object_name_info.objectHandle = HandleToUint64(*messenger);
463 object_name_info.pObjectName = NULL;
464 callback_data->pObjects = &object_name_info;
465 callback_data->objectCount = 1;
466
Mark Young6ba8abe2017-11-09 10:37:04 -0700467 while (layer_dbg_node) {
468 if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & message_severity) &&
469 (layer_dbg_node->messenger.messageType & message_type)) {
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700470 auto it = debug_data->debugUtilsObjectNameMap->find(object_name_info.objectHandle);
471 if (it != debug_data->debugUtilsObjectNameMap->end()) {
472 object_name_info.pObjectName = it->second.c_str();
Mark Young6ba8abe2017-11-09 10:37:04 -0700473 }
474 if (layer_dbg_node->messenger.pfnUserCallback(message_severity, message_type, callback_data,
475 layer_dbg_node->pUserData)) {
476 bail = true;
477 }
478 } else if (!layer_dbg_node->is_messenger && layer_dbg_node->report.msgFlags & object_flags) {
479 auto it = debug_data->debugObjectNameMap->find(callback_data->pObjects[0].objectHandle);
John Zulauf536649b2018-05-01 13:28:27 -0600480 VkDebugReportObjectTypeEXT object_type = convertCoreObjectToDebugReportObject(callback_data->pObjects[0].objectType);
Tony Barbour3431dac2017-06-19 16:50:37 -0600481 if (it == debug_data->debugObjectNameMap->end()) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700482 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
483 callback_data->messageIdNumber, callback_data->pMessageIdName,
484 callback_data->pMessage, layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600485 bail = true;
486 }
487 } else {
488 std::string newMsg = "SrcObject name = ";
489 newMsg.append(it->second.c_str());
490 newMsg.append(" ");
Mark Young6ba8abe2017-11-09 10:37:04 -0700491 newMsg.append(callback_data->pMessage);
492 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
493 callback_data->messageIdNumber, callback_data->pMessageIdName,
494 newMsg.c_str(), layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600495 bail = true;
496 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600497 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600498 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700499 layer_dbg_node = layer_dbg_node->pNext;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600500 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600501
502 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600503}
504
Mark Young6ba8abe2017-11-09 10:37:04 -0700505static inline debug_report_data *debug_utils_create_instance(
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700506 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
Mark Young6ba8abe2017-11-09 10:37:04 -0700507 const char *const *enabled_extensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600508{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600509 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700510 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600511
512 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600513 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600514 // TODO: Check other property fields
Mark Young6ba8abe2017-11-09 10:37:04 -0700515 if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600516 debug_data->g_DEBUG_REPORT = true;
Mark Young6ba8abe2017-11-09 10:37:04 -0700517 } else if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) {
518 debug_data->g_DEBUG_UTILS = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600519 }
520 }
Tony Barbour3431dac2017-06-19 16:50:37 -0600521 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
Mark Young6ba8abe2017-11-09 10:37:04 -0700522 debug_data->debugUtilsObjectNameMap = new std::unordered_map<uint64_t, std::string>;
523 debug_data->debugUtilsQueueLabels = new std::unordered_map<VkQueue, std::vector<LoggingLabelData>>;
524 debug_data->debugUtilsCmdBufLabels = new std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>>;
525 debug_data->queueLabelHasInsert = false;
526 debug_data->cmdBufLabelHasInsert = false;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600527 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600528}
529
Mark Young6ba8abe2017-11-09 10:37:04 -0700530static inline void layer_debug_utils_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600531 if (debug_data) {
532 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
533 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
Tony Barbour3431dac2017-06-19 16:50:37 -0600534 delete debug_data->debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -0700535 delete debug_data->debugUtilsObjectNameMap;
536 delete debug_data->debugUtilsQueueLabels;
537 delete debug_data->debugUtilsCmdBufLabels;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600538 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600539 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600540}
541
Mark Young6ba8abe2017-11-09 10:37:04 -0700542static inline debug_report_data *layer_debug_utils_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600543 // DEBUG_REPORT shares data between Instance and Device,
544 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600545 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600546}
547
Mark Young6ba8abe2017-11-09 10:37:04 -0700548static inline void layer_debug_utils_destroy_device(VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600549 // Nothing to do since we're using instance data record
550}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600551
Mark Young6ba8abe2017-11-09 10:37:04 -0700552static inline void layer_destroy_messenger_callback(debug_report_data *debug_data, VkDebugUtilsMessengerEXT messenger,
553 const VkAllocationCallbacks *allocator) {
554 RemoveDebugUtilsMessenger(debug_data, &debug_data->debug_callback_list, messenger);
555 RemoveDebugUtilsMessenger(debug_data, &debug_data->default_debug_callback_list, messenger);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600556}
557
Mark Young6ba8abe2017-11-09 10:37:04 -0700558static inline VkResult layer_create_messenger_callback(debug_report_data *debug_data, bool default_callback,
559 const VkDebugUtilsMessengerCreateInfoEXT *create_info,
560 const VkAllocationCallbacks *allocator,
561 VkDebugUtilsMessengerEXT *messenger) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700562 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700563 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young6ba8abe2017-11-09 10:37:04 -0700564 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
565 pNewDbgFuncNode->is_messenger = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600566
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600567 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Young6ba8abe2017-11-09 10:37:04 -0700568 if (!(*messenger)) *messenger = (VkDebugUtilsMessengerEXT)pNewDbgFuncNode;
569 pNewDbgFuncNode->messenger.messenger = *messenger;
570 pNewDbgFuncNode->messenger.pfnUserCallback = create_info->pfnUserCallback;
571 pNewDbgFuncNode->messenger.messageSeverity = create_info->messageSeverity;
572 pNewDbgFuncNode->messenger.messageType = create_info->messageType;
573 pNewDbgFuncNode->pUserData = create_info->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600574
Mark Young6ba8abe2017-11-09 10:37:04 -0700575 debug_data->active_severities |= create_info->messageSeverity;
576 debug_data->active_types |= create_info->messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600577 if (default_callback) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700578 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600579 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700580 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600581 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600582
Mark Young6ba8abe2017-11-09 10:37:04 -0700583 VkDebugUtilsMessengerCallbackDataEXT callback_data = {};
Mark Young6ba8abe2017-11-09 10:37:04 -0700584 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
585 callback_data.pNext = NULL;
586 callback_data.flags = 0;
587 callback_data.pMessageIdName = "Layer Internal Message";
588 callback_data.messageIdNumber = 0;
589 callback_data.pMessage = "Added messenger";
590 callback_data.queueLabelCount = 0;
591 callback_data.pQueueLabels = NULL;
592 callback_data.cmdBufLabelCount = 0;
593 callback_data.pCmdBufLabels = NULL;
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700594 callback_data.objectCount = 0;
595 callback_data.pObjects = NULL;
Mark Young6ba8abe2017-11-09 10:37:04 -0700596 debug_messenger_log_msg(debug_data, VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,
Shannon McPherson7fbbe362018-12-03 11:57:42 -0700597 VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT, &callback_data, messenger);
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600598 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600599}
600
Mark Young6ba8abe2017-11-09 10:37:04 -0700601static inline void layer_destroy_report_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
602 const VkAllocationCallbacks *allocator) {
603 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
604 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
605}
606
607static inline VkResult layer_create_report_callback(debug_report_data *debug_data, bool default_callback,
608 const VkDebugReportCallbackCreateInfoEXT *create_info,
609 const VkAllocationCallbacks *allocator, VkDebugReportCallbackEXT *callback) {
610 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
611 if (!pNewDbgFuncNode) {
612 return VK_ERROR_OUT_OF_HOST_MEMORY;
613 }
614 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
615 pNewDbgFuncNode->is_messenger = false;
616
617 // Handle of 0 is logging_callback so use allocated Node address as unique handle
618 if (!(*callback)) *callback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
619 pNewDbgFuncNode->report.msgCallback = *callback;
620 pNewDbgFuncNode->report.pfnMsgCallback = create_info->pfnCallback;
621 pNewDbgFuncNode->report.msgFlags = create_info->flags;
622 pNewDbgFuncNode->pUserData = create_info->pUserData;
623
624 VkFlags local_severity = 0;
625 VkFlags local_type = 0;
626 DebugReportFlagsToAnnotFlags(create_info->flags, true, &local_severity, &local_type);
627 debug_data->active_severities |= local_severity;
628 debug_data->active_types |= local_type;
629 if (default_callback) {
630 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
631 } else {
632 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600633 }
634
Mark Young6ba8abe2017-11-09 10:37:04 -0700635 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, (uint64_t)*callback, 0,
Dave Houlton407df732018-08-06 17:58:24 -0600636 "DebugReport", "Added callback", kVUIDUndefined);
Mark Young6ba8abe2017-11-09 10:37:04 -0700637 return VK_SUCCESS;
638}
639
640static inline PFN_vkVoidFunction debug_utils_get_instance_proc_addr(debug_report_data *debug_data, const char *func_name) {
641 if (!debug_data) {
642 return NULL;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600643 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700644 if (debug_data->g_DEBUG_REPORT) {
645 if (!strcmp(func_name, "vkCreateDebugReportCallbackEXT")) {
646 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
647 }
648 if (!strcmp(func_name, "vkDestroyDebugReportCallbackEXT")) {
649 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
650 }
651 if (!strcmp(func_name, "vkDebugReportMessageEXT")) {
652 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
653 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600654 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700655 if (debug_data->g_DEBUG_UTILS) {
656 if (!strcmp(func_name, "vkCreateDebugUtilsMessengerEXT")) {
657 return (PFN_vkVoidFunction)vkCreateDebugUtilsMessengerEXT;
658 }
659 if (!strcmp(func_name, "vkDestroyDebugUtilsMessengerEXT")) {
660 return (PFN_vkVoidFunction)vkDestroyDebugUtilsMessengerEXT;
661 }
662 if (!strcmp(func_name, "vkSubmitDebugUtilsMessageEXT")) {
663 return (PFN_vkVoidFunction)vkSubmitDebugUtilsMessageEXT;
664 }
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700665 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600666 return NULL;
667}
668
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600669// This utility (called at vkCreateInstance() time), looks at a pNext chain.
670// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
671// then allocates an array that can hold that many structs, as well as that
672// many VkDebugReportCallbackEXT handles. It then copies each
673// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
Mark Young6ba8abe2017-11-09 10:37:04 -0700674static inline VkResult layer_copy_tmp_report_callbacks(const void *pChain, uint32_t *num_callbacks,
675 VkDebugReportCallbackCreateInfoEXT **infos,
676 VkDebugReportCallbackEXT **callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600677 uint32_t n = *num_callbacks = 0;
678
679 const void *pNext = pChain;
680 while (pNext) {
681 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
682 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
683 n++;
684 }
685 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
686 }
687 if (n == 0) {
688 return VK_SUCCESS;
689 }
690
691 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
692 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
693 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
694 if (!pInfos) {
695 return VK_ERROR_OUT_OF_HOST_MEMORY;
696 }
697 // 3rd, allocate memory for a unique handle for each callback:
698 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
699 if (!pCallbacks) {
700 free(pInfos);
701 return VK_ERROR_OUT_OF_HOST_MEMORY;
702 }
703 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
704 // vkDestroyInstance, and assign a unique handle to each callback (just
705 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
706 pNext = pChain;
707 while (pNext) {
708 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
709 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
710 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
711 }
712 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
713 }
714
715 *num_callbacks = n;
716 return VK_SUCCESS;
717}
718
Mark Young6ba8abe2017-11-09 10:37:04 -0700719// This utility frees the arrays allocated by layer_copy_tmp_report_callbacks()
720static inline void layer_free_tmp_report_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600721 free(infos);
722 free(callbacks);
723}
724
725// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700726// that were copied by layer_copy_tmp_report_callbacks()
727static inline VkResult layer_enable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
728 VkDebugReportCallbackCreateInfoEXT *infos,
729 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600730 VkResult rtn = VK_SUCCESS;
731 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700732 rtn = layer_create_report_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600733 if (rtn != VK_SUCCESS) {
734 for (uint32_t j = 0; j < i; j++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700735 layer_destroy_report_callback(debug_data, callbacks[j], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600736 }
737 return rtn;
738 }
739 }
740 return rtn;
741}
742
743// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700744// that were copied by layer_copy_tmp_report_callbacks()
745static inline void layer_disable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
746 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600747 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700748 layer_destroy_report_callback(debug_data, callbacks[i], NULL);
749 }
750}
751
752// This utility (called at vkCreateInstance() time), looks at a pNext chain.
753// It counts any VkDebugUtilsMessengerCreateInfoEXT structs that it finds. It
754// then allocates an array that can hold that many structs, as well as that
755// many VkDebugUtilsMessengerEXT handles. It then copies each
756// VkDebugUtilsMessengerCreateInfoEXT, and initializes each handle.
Mark Young8504ba62018-03-21 13:35:34 -0600757static inline VkResult layer_copy_tmp_debug_messengers(const void *pChain, uint32_t *num_messengers,
758 VkDebugUtilsMessengerCreateInfoEXT **infos,
759 VkDebugUtilsMessengerEXT **messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700760 uint32_t n = *num_messengers = 0;
761
762 const void *pNext = pChain;
763 while (pNext) {
764 // 1st, count the number VkDebugUtilsMessengerCreateInfoEXT:
765 if (((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
766 n++;
767 }
768 pNext = (void *)((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->pNext;
769 }
770 if (n == 0) {
771 return VK_SUCCESS;
772 }
773
774 // 2nd, allocate memory for each VkDebugUtilsMessengerCreateInfoEXT:
775 VkDebugUtilsMessengerCreateInfoEXT *pInfos = *infos =
776 ((VkDebugUtilsMessengerCreateInfoEXT *)malloc(n * sizeof(VkDebugUtilsMessengerCreateInfoEXT)));
777 if (!pInfos) {
778 return VK_ERROR_OUT_OF_HOST_MEMORY;
779 }
780 // 3rd, allocate memory for a unique handle for each messenger:
781 VkDebugUtilsMessengerEXT *pMessengers = *messengers =
782 ((VkDebugUtilsMessengerEXT *)malloc(n * sizeof(VkDebugUtilsMessengerEXT)));
783 if (!pMessengers) {
784 free(pInfos);
785 return VK_ERROR_OUT_OF_HOST_MEMORY;
786 }
787 // 4th, copy each VkDebugUtilsMessengerCreateInfoEXT for use by
788 // vkDestroyInstance, and assign a unique handle to each callback (just
789 // use the address of the copied VkDebugUtilsMessengerCreateInfoEXT):
790 pNext = pChain;
791 while (pNext) {
792 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
793 memcpy(pInfos, pNext, sizeof(VkDebugUtilsMessengerCreateInfoEXT));
794 *pMessengers++ = (VkDebugUtilsMessengerEXT)pInfos++;
795 }
796 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
797 }
798
799 *num_messengers = n;
800 return VK_SUCCESS;
801}
802
803// This utility frees the arrays allocated by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600804static inline void layer_free_tmp_debug_messengers(VkDebugUtilsMessengerCreateInfoEXT *infos,
805 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700806 free(infos);
807 free(messengers);
808}
809
810// This utility enables all of the VkDebugUtilsMessengerCreateInfoEXT structs
811// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600812static inline VkResult layer_enable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
813 VkDebugUtilsMessengerCreateInfoEXT *infos,
814 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700815 VkResult rtn = VK_SUCCESS;
816 for (uint32_t i = 0; i < num_messengers; i++) {
817 rtn = layer_create_messenger_callback(debug_data, false, &infos[i], NULL, &messengers[i]);
818 if (rtn != VK_SUCCESS) {
819 for (uint32_t j = 0; j < i; j++) {
820 layer_destroy_messenger_callback(debug_data, messengers[j], NULL);
821 }
822 return rtn;
823 }
824 }
825 return rtn;
826}
827
828// This utility disables all of the VkDebugUtilsMessengerCreateInfoEXT structs
829// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600830static inline void layer_disable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
831 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700832 for (uint32_t i = 0; i < num_messengers; i++) {
833 layer_destroy_messenger_callback(debug_data, messengers[i], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600834 }
835}
836
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600837// Checks if the message will get logged.
838// Allows layer to defer collecting & formating data if the
839// message will be discarded.
Mark Young6ba8abe2017-11-09 10:37:04 -0700840static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msg_flags) {
841 VkFlags local_severity = 0;
842 VkFlags local_type = 0;
843 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
844 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600845 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600846 return false;
847 }
848
849 return true;
850}
John Zulauf6664e272018-01-17 11:00:22 -0700851#ifndef WIN32
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700852static inline int string_sprintf(std::string *output, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
John Zulauf6664e272018-01-17 11:00:22 -0700853#endif
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700854static inline int string_sprintf(std::string *output, const char *fmt, ...) {
John Zulauf6664e272018-01-17 11:00:22 -0700855 std::string &formatted = *output;
856 va_list argptr;
857 va_start(argptr, fmt);
858 int reserve = vsnprintf(nullptr, 0, fmt, argptr);
859 va_end(argptr);
860 formatted.reserve(reserve + 1);
861 va_start(argptr, fmt);
862 int result = vsnprintf((char *)formatted.data(), formatted.capacity(), fmt, argptr);
863 va_end(argptr);
864 assert(result == reserve);
865 return result;
866}
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600867
Chris Forbes8a25bce2016-03-24 12:06:35 +1300868#ifdef WIN32
869static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
870 *strp = nullptr;
871 int size = _vscprintf(fmt, ap);
872 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700873 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300874 if (!*strp) {
875 return -1;
876 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700877 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300878 }
879 return size;
880}
881#endif
882
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700883// Output log message via DEBUG_REPORT. Takes format and variable arg list so that output string is only computed if a message
884// needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800885#ifndef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700886static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700887 uint64_t src_object, std::string vuid_text, const char *format, ...)
888 __attribute__((format(printf, 6, 7)));
Michael Lentine010f4692015-11-03 16:19:46 -0800889#endif
Mark Young6ba8abe2017-11-09 10:37:04 -0700890static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700891 uint64_t src_object, std::string vuid_text, const char *format, ...) {
892 VkFlags local_severity = 0;
893 VkFlags local_type = 0;
894 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
895 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
896 // Message is not wanted
897 return false;
898 }
899
900 va_list argptr;
901 va_start(argptr, format);
902 char *str;
903 if (-1 == vasprintf(&str, format, argptr)) {
904 // On failure, glibc vasprintf leaves str undefined
905 str = nullptr;
906 }
907 va_end(argptr);
908
Dave Houlton407df732018-08-06 17:58:24 -0600909 std::string str_plus_spec_text(str ? str : "Allocation failure");
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700910
Dave Houlton407df732018-08-06 17:58:24 -0600911 // Append the spec error text to the error message, unless it's an UNASSIGNED or UNDEFINED vuid
912 if ((vuid_text.find("UNASSIGNED-") == std::string::npos) && (vuid_text.find(kVUIDUndefined) == std::string::npos)) {
Dave Houlton4d9b2f82018-10-24 18:21:06 -0600913 // Linear search makes no assumptions about the layout of the string table
914 // This is not fast, but it does not need to be at this point in the error reporting path
915 uint32_t num_vuids = sizeof(vuid_spec_text) / sizeof(vuid_spec_text_pair);
916 const char *spec_text = nullptr;
917 for (uint32_t i = 0; i < num_vuids; i++) {
918 if (0 == strcmp(vuid_text.c_str(), vuid_spec_text[i].vuid)) {
919 spec_text = vuid_spec_text[i].spec_text;
920 break;
921 }
922 }
923
924 if (nullptr == spec_text) {
Dave Houlton407df732018-08-06 17:58:24 -0600925 // If this happens, you've hit a VUID string that isn't defined in the spec's json file
926 // Try running 'vk_validation_stats -c' to look for invalid VUID strings in the repo code
927 assert(0);
928 } else {
929 str_plus_spec_text += " The Vulkan spec states: ";
Dave Houlton4d9b2f82018-10-24 18:21:06 -0600930 str_plus_spec_text += spec_text;
Dave Houlton407df732018-08-06 17:58:24 -0600931 }
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700932 }
933
Dave Houlton8e9f6542018-05-18 12:18:22 -0600934 // Append layer prefix with VUID string, pass in recovered legacy numerical VUID
Dave Houlton407df732018-08-06 17:58:24 -0600935 bool result = debug_log_msg(debug_data, msg_flags, object_type, src_object, 0, "Validation", str_plus_spec_text.c_str(),
936 vuid_text.c_str());
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700937
938 free(str);
939 return result;
940}
941
Mark Young6ba8abe2017-11-09 10:37:04 -0700942static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_log_callback(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
943 uint64_t src_object, size_t location, int32_t msg_code,
944 const char *layer_prefix, const char *message, void *user_data) {
Mark Lobodzinski706e15a2018-12-12 15:35:29 -0700945 std::ostringstream msg_buffer;
Mark Young6ba8abe2017-11-09 10:37:04 -0700946 char msg_flag_string[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600947
Mark Young6ba8abe2017-11-09 10:37:04 -0700948 PrintMessageFlags(msg_flags, msg_flag_string);
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600949
Mark Lobodzinski706e15a2018-12-12 15:35:29 -0700950 msg_buffer << layer_prefix << "(" << msg_flag_string << "): msg_code: " << msg_code << ": " << message << "\n";
951 const std::string tmp = msg_buffer.str();
952 const char *cstr = tmp.c_str();
953
954 fprintf((FILE *)user_data, "%s", cstr);
Mark Young6ba8abe2017-11-09 10:37:04 -0700955 fflush((FILE *)user_data);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600956
Mark Lobodzinski706e15a2018-12-12 15:35:29 -0700957#if defined __ANDROID__
958 LOGCONSOLE("%s", cstr);
959#endif
960
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600961 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600962}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600963
Mark Young6ba8abe2017-11-09 10:37:04 -0700964static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_win32_debug_output_msg(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
965 uint64_t src_object, size_t location, int32_t msg_code,
966 const char *layer_prefix, const char *message,
967 void *user_data) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600968#ifdef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700969 char msg_flag_string[30];
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600970 char buf[2048];
971
Mark Young6ba8abe2017-11-09 10:37:04 -0700972 PrintMessageFlags(msg_flags, msg_flag_string);
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600973 _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 -0600974
975 OutputDebugString(buf);
976#endif
977
978 return false;
979}
980
Mark Young6ba8abe2017-11-09 10:37:04 -0700981static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT obj_type,
982 uint64_t src_object, size_t location, int32_t msg_code,
983 const char *layer_prefix, const char *message, void *user_data) {
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600984#ifdef WIN32
985 DebugBreak();
986#else
987 raise(SIGTRAP);
988#endif
989
990 return false;
991}
992
Mark Young6ba8abe2017-11-09 10:37:04 -0700993static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_log_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
994 VkDebugUtilsMessageTypeFlagsEXT message_type,
995 const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
996 void *user_data) {
Mark Lobodzinski706e15a2018-12-12 15:35:29 -0700997 std::ostringstream msg_buffer;
Mark Young6ba8abe2017-11-09 10:37:04 -0700998 char msg_severity[30];
999 char msg_type[30];
1000
1001 PrintMessageSeverity(message_severity, msg_severity);
1002 PrintMessageType(message_type, msg_type);
1003
Mark Lobodzinski706e15a2018-12-12 15:35:29 -07001004 msg_buffer << callback_data->pMessageIdName << "(" << msg_severity << " / " << msg_type
1005 << "): msgNum: " << callback_data->messageIdNumber << " - " << callback_data->pMessage << "\n";
1006 msg_buffer << " Objects: " << callback_data->objectCount << "\n";
Mark Young8504ba62018-03-21 13:35:34 -06001007 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
Mark Lobodzinski706e15a2018-12-12 15:35:29 -07001008 msg_buffer << " [" << obj << "] " << std::hex << std::showbase
1009 << HandleToUint64(callback_data->pObjects[obj].objectHandle) << ", type: " << std::dec << std::noshowbase
1010 << callback_data->pObjects[obj].objectType
1011 << ", name: " << (callback_data->pObjects[obj].pObjectName ? callback_data->pObjects[obj].pObjectName : "NULL")
1012 << "\n";
Mark Young8504ba62018-03-21 13:35:34 -06001013 }
Mark Lobodzinski706e15a2018-12-12 15:35:29 -07001014 const std::string tmp = msg_buffer.str();
1015 const char *cstr = tmp.c_str();
1016 fprintf((FILE *)user_data, "%s", cstr);
Mark Young6ba8abe2017-11-09 10:37:04 -07001017 fflush((FILE *)user_data);
1018
Mark Lobodzinski706e15a2018-12-12 15:35:29 -07001019#if defined __ANDROID__
1020 LOGCONSOLE("%s", cstr);
1021#endif
1022
Mark Young6ba8abe2017-11-09 10:37:04 -07001023 return false;
Petr Krause9388f62017-05-13 20:53:12 +02001024}
1025
Mark Young6ba8abe2017-11-09 10:37:04 -07001026static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_win32_debug_output_msg(
1027 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type,
1028 const VkDebugUtilsMessengerCallbackDataEXT *callback_data, void *user_data) {
1029#ifdef WIN32
Mark Lobodzinski3be125e2018-11-19 09:58:10 -07001030 std::ostringstream msg_buffer;
Mark Young6ba8abe2017-11-09 10:37:04 -07001031 char msg_severity[30];
1032 char msg_type[30];
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001033
Mark Young6ba8abe2017-11-09 10:37:04 -07001034 PrintMessageSeverity(message_severity, msg_severity);
1035 PrintMessageType(message_type, msg_type);
1036
Mark Lobodzinski3be125e2018-11-19 09:58:10 -07001037 msg_buffer << callback_data->pMessageIdName << "(" << msg_severity << " / " << msg_type
1038 << "): msgNum: " << callback_data->messageIdNumber << " - " << callback_data->pMessage << "\n";
1039 msg_buffer << " Objects: " << callback_data->objectCount << "\n";
1040
Mark Young8504ba62018-03-21 13:35:34 -06001041 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
Mark Lobodzinski3be125e2018-11-19 09:58:10 -07001042 msg_buffer << " [" << obj << "] " << std::hex << std::showbase
1043 << HandleToUint64(callback_data->pObjects[obj].objectHandle) << ", type: " << std::dec << std::noshowbase
1044 << callback_data->pObjects[obj].objectType
1045 << ", name: " << (callback_data->pObjects[obj].pObjectName ? callback_data->pObjects[obj].pObjectName : "NULL")
1046 << "\n";
Mark Young8504ba62018-03-21 13:35:34 -06001047 }
Mark Lobodzinski3be125e2018-11-19 09:58:10 -07001048 const std::string tmp = msg_buffer.str();
1049 const char *cstr = tmp.c_str();
1050 OutputDebugString(cstr);
Mark Young6ba8abe2017-11-09 10:37:04 -07001051#endif
1052
1053 return false;
1054}
1055
1056// This utility converts from the VkDebugUtilsLabelEXT structure into the logging version of the structure.
1057// In the logging version, we only record what we absolutely need to convey back to the callbacks.
1058static inline void InsertLabelIntoLog(const VkDebugUtilsLabelEXT *utils_label, std::vector<LoggingLabelData> &log_vector) {
1059 LoggingLabelData log_label_data = {};
1060 log_label_data.name = utils_label->pLabelName;
1061 log_label_data.color[0] = utils_label->color[0];
1062 log_label_data.color[1] = utils_label->color[1];
1063 log_label_data.color[2] = utils_label->color[2];
1064 log_label_data.color[3] = utils_label->color[3];
1065 log_vector.push_back(log_label_data);
1066}
1067
Mark Young8504ba62018-03-21 13:35:34 -06001068static inline void BeginQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1069 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001070 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1071 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1072 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1073 std::vector<LoggingLabelData> new_queue_labels;
1074 InsertLabelIntoLog(label_info, new_queue_labels);
1075 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1076 } else {
1077 // If the last thing was a label insert, we need to pop it off of the label vector before any
1078 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1079 // temporary location that exists until the next operation occurs. In this case, a new
1080 // "vkQueueBeginDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1081 if (report_data->queueLabelHasInsert) {
1082 report_data->queueLabelHasInsert = false;
1083 label_iter->second.pop_back();
1084 }
1085 InsertLabelIntoLog(label_info, label_iter->second);
1086 }
1087 }
1088}
1089
Mark Young8504ba62018-03-21 13:35:34 -06001090static inline void EndQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001091 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1092 if (label_iter != report_data->debugUtilsQueueLabels->end()) {
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 "vkQueueInsertDebugUtilsLabelEXT" is only a
1095 // temporary location that exists until the next operation occurs. In this case, a
1096 // "vkQueueEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1097 if (report_data->queueLabelHasInsert) {
1098 report_data->queueLabelHasInsert = false;
1099 label_iter->second.pop_back();
1100 }
1101 // Now pop the normal item
1102 label_iter->second.pop_back();
1103 }
1104}
1105
Mark Young8504ba62018-03-21 13:35:34 -06001106static inline void InsertQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1107 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001108 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1109 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1110 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1111 std::vector<LoggingLabelData> new_queue_labels;
1112 InsertLabelIntoLog(label_info, new_queue_labels);
1113 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1114 } else {
1115 // If the last thing was a label insert, we need to pop it off of the label vector before any
1116 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1117 // temporary location that exists until the next operation occurs. In this case, a new
1118 // "vkQueueInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1119 if (report_data->queueLabelHasInsert) {
1120 label_iter->second.pop_back();
1121 }
1122 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1123 // the next queue label operation.
1124 InsertLabelIntoLog(label_info, label_iter->second);
1125 report_data->queueLabelHasInsert = true;
1126 }
1127 }
1128}
1129
Mark Young8504ba62018-03-21 13:35:34 -06001130static inline void BeginCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1131 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001132 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1133 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1134 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1135 std::vector<LoggingLabelData> new_cmdbuf_labels;
1136 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1137 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1138 } else {
1139 // If the last thing was a label insert, we need to pop it off of the label vector before any
1140 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1141 // temporary location that exists until the next operation occurs. In this case, a
1142 // "vkCmdBeginDebugUtilsLabelEXT" has occurred erasing the inserted label.
1143 if (report_data->cmdBufLabelHasInsert) {
1144 report_data->cmdBufLabelHasInsert = false;
1145 label_iter->second.pop_back();
1146 }
1147 InsertLabelIntoLog(label_info, label_iter->second);
1148 }
1149 }
1150}
1151
Mark Young8504ba62018-03-21 13:35:34 -06001152static inline void EndCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001153 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1154 if (label_iter != report_data->debugUtilsCmdBufLabels->end()) {
1155 // If the last thing was a label insert, we need to pop it off of the label vector before any
1156 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1157 // temporary location that exists until the next operation occurs. In this case, a
1158 // "vkCmdEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1159 if (report_data->cmdBufLabelHasInsert) {
1160 report_data->cmdBufLabelHasInsert = false;
1161 label_iter->second.pop_back();
1162 }
1163 // Now pop the normal item
1164 label_iter->second.pop_back();
1165 }
1166}
1167
Mark Young8504ba62018-03-21 13:35:34 -06001168static inline void InsertCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1169 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001170 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1171 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1172 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1173 std::vector<LoggingLabelData> new_cmdbuf_labels;
1174 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1175 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1176 } else {
1177 // If the last thing was a label insert, we need to pop it off of the label vector before any
1178 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1179 // temporary location that exists until the next operation occurs. In this case, a new
1180 // "vkCmdInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1181 if (report_data->cmdBufLabelHasInsert) {
1182 label_iter->second.pop_back();
1183 }
1184 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1185 // the next command buffer label operation.
1186 InsertLabelIntoLog(label_info, label_iter->second);
1187 report_data->cmdBufLabelHasInsert = true;
1188 }
1189 }
1190}
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001191
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001192#endif // LAYER_LOGGING_H