blob: dcafd9539ffc76547d1e6788a75ad1bfa895155e [file] [log] [blame]
Mark Lobodzinskifeb61fe2019-01-10 08:56:57 -07001/* Copyright (c) 2015-2019 The Khronos Group Inc.
2 * Copyright (c) 2015-2019 Valve Corporation
3 * Copyright (c) 2015-2019 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);
John Zulaufdeaa0132018-12-12 16:22:30 -0700860 formatted.reserve(reserve + 1); // Set the storage length long enough to hold the output + null
861 formatted.resize(reserve); // Set the *logical* length to be what vsprintf will write
John Zulauf6664e272018-01-17 11:00:22 -0700862 va_start(argptr, fmt);
863 int result = vsnprintf((char *)formatted.data(), formatted.capacity(), fmt, argptr);
864 va_end(argptr);
865 assert(result == reserve);
John Zulaufdeaa0132018-12-12 16:22:30 -0700866 assert((formatted.size() == strlen(formatted.c_str())));
John Zulauf6664e272018-01-17 11:00:22 -0700867 return result;
868}
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600869
Chris Forbes8a25bce2016-03-24 12:06:35 +1300870#ifdef WIN32
871static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
872 *strp = nullptr;
873 int size = _vscprintf(fmt, ap);
874 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700875 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300876 if (!*strp) {
877 return -1;
878 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700879 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300880 }
881 return size;
882}
883#endif
884
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700885// Output log message via DEBUG_REPORT. Takes format and variable arg list so that output string is only computed if a message
886// needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800887#ifndef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700888static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700889 uint64_t src_object, std::string vuid_text, const char *format, ...)
890 __attribute__((format(printf, 6, 7)));
Michael Lentine010f4692015-11-03 16:19:46 -0800891#endif
Mark Young6ba8abe2017-11-09 10:37:04 -0700892static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700893 uint64_t src_object, std::string vuid_text, const char *format, ...) {
894 VkFlags local_severity = 0;
895 VkFlags local_type = 0;
896 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
897 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
898 // Message is not wanted
899 return false;
900 }
901
902 va_list argptr;
903 va_start(argptr, format);
904 char *str;
905 if (-1 == vasprintf(&str, format, argptr)) {
906 // On failure, glibc vasprintf leaves str undefined
907 str = nullptr;
908 }
909 va_end(argptr);
910
Dave Houlton407df732018-08-06 17:58:24 -0600911 std::string str_plus_spec_text(str ? str : "Allocation failure");
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700912
Dave Houlton407df732018-08-06 17:58:24 -0600913 // Append the spec error text to the error message, unless it's an UNASSIGNED or UNDEFINED vuid
914 if ((vuid_text.find("UNASSIGNED-") == std::string::npos) && (vuid_text.find(kVUIDUndefined) == std::string::npos)) {
Dave Houlton4d9b2f82018-10-24 18:21:06 -0600915 // Linear search makes no assumptions about the layout of the string table
916 // This is not fast, but it does not need to be at this point in the error reporting path
917 uint32_t num_vuids = sizeof(vuid_spec_text) / sizeof(vuid_spec_text_pair);
918 const char *spec_text = nullptr;
919 for (uint32_t i = 0; i < num_vuids; i++) {
920 if (0 == strcmp(vuid_text.c_str(), vuid_spec_text[i].vuid)) {
921 spec_text = vuid_spec_text[i].spec_text;
922 break;
923 }
924 }
925
926 if (nullptr == spec_text) {
Dave Houlton407df732018-08-06 17:58:24 -0600927 // If this happens, you've hit a VUID string that isn't defined in the spec's json file
928 // Try running 'vk_validation_stats -c' to look for invalid VUID strings in the repo code
929 assert(0);
930 } else {
931 str_plus_spec_text += " The Vulkan spec states: ";
Dave Houlton4d9b2f82018-10-24 18:21:06 -0600932 str_plus_spec_text += spec_text;
Dave Houlton407df732018-08-06 17:58:24 -0600933 }
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700934 }
935
Dave Houlton8e9f6542018-05-18 12:18:22 -0600936 // Append layer prefix with VUID string, pass in recovered legacy numerical VUID
Dave Houlton407df732018-08-06 17:58:24 -0600937 bool result = debug_log_msg(debug_data, msg_flags, object_type, src_object, 0, "Validation", str_plus_spec_text.c_str(),
938 vuid_text.c_str());
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700939
940 free(str);
941 return result;
942}
943
Mark Young6ba8abe2017-11-09 10:37:04 -0700944static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_log_callback(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
945 uint64_t src_object, size_t location, int32_t msg_code,
946 const char *layer_prefix, const char *message, void *user_data) {
Mark Lobodzinski706e15a2018-12-12 15:35:29 -0700947 std::ostringstream msg_buffer;
Mark Young6ba8abe2017-11-09 10:37:04 -0700948 char msg_flag_string[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600949
Mark Young6ba8abe2017-11-09 10:37:04 -0700950 PrintMessageFlags(msg_flags, msg_flag_string);
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600951
Mark Lobodzinski706e15a2018-12-12 15:35:29 -0700952 msg_buffer << layer_prefix << "(" << msg_flag_string << "): msg_code: " << msg_code << ": " << message << "\n";
953 const std::string tmp = msg_buffer.str();
954 const char *cstr = tmp.c_str();
955
956 fprintf((FILE *)user_data, "%s", cstr);
Mark Young6ba8abe2017-11-09 10:37:04 -0700957 fflush((FILE *)user_data);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600958
Mark Lobodzinski706e15a2018-12-12 15:35:29 -0700959#if defined __ANDROID__
960 LOGCONSOLE("%s", cstr);
961#endif
962
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600963 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600964}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600965
Mark Young6ba8abe2017-11-09 10:37:04 -0700966static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_win32_debug_output_msg(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
967 uint64_t src_object, size_t location, int32_t msg_code,
968 const char *layer_prefix, const char *message,
969 void *user_data) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600970#ifdef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700971 char msg_flag_string[30];
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600972 char buf[2048];
973
Mark Young6ba8abe2017-11-09 10:37:04 -0700974 PrintMessageFlags(msg_flags, msg_flag_string);
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600975 _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 -0600976
977 OutputDebugString(buf);
978#endif
979
980 return false;
981}
982
Mark Young6ba8abe2017-11-09 10:37:04 -0700983static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT obj_type,
984 uint64_t src_object, size_t location, int32_t msg_code,
985 const char *layer_prefix, const char *message, void *user_data) {
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600986#ifdef WIN32
987 DebugBreak();
988#else
989 raise(SIGTRAP);
990#endif
991
992 return false;
993}
994
Mark Young6ba8abe2017-11-09 10:37:04 -0700995static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_log_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
996 VkDebugUtilsMessageTypeFlagsEXT message_type,
997 const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
998 void *user_data) {
Mark Lobodzinski706e15a2018-12-12 15:35:29 -0700999 std::ostringstream msg_buffer;
Mark Young6ba8abe2017-11-09 10:37:04 -07001000 char msg_severity[30];
1001 char msg_type[30];
1002
1003 PrintMessageSeverity(message_severity, msg_severity);
1004 PrintMessageType(message_type, msg_type);
1005
Mark Lobodzinski706e15a2018-12-12 15:35:29 -07001006 msg_buffer << callback_data->pMessageIdName << "(" << msg_severity << " / " << msg_type
1007 << "): msgNum: " << callback_data->messageIdNumber << " - " << callback_data->pMessage << "\n";
1008 msg_buffer << " Objects: " << callback_data->objectCount << "\n";
Mark Young8504ba62018-03-21 13:35:34 -06001009 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
Mark Lobodzinski706e15a2018-12-12 15:35:29 -07001010 msg_buffer << " [" << obj << "] " << std::hex << std::showbase
1011 << HandleToUint64(callback_data->pObjects[obj].objectHandle) << ", type: " << std::dec << std::noshowbase
1012 << callback_data->pObjects[obj].objectType
1013 << ", name: " << (callback_data->pObjects[obj].pObjectName ? callback_data->pObjects[obj].pObjectName : "NULL")
1014 << "\n";
Mark Young8504ba62018-03-21 13:35:34 -06001015 }
Mark Lobodzinski706e15a2018-12-12 15:35:29 -07001016 const std::string tmp = msg_buffer.str();
1017 const char *cstr = tmp.c_str();
1018 fprintf((FILE *)user_data, "%s", cstr);
Mark Young6ba8abe2017-11-09 10:37:04 -07001019 fflush((FILE *)user_data);
1020
Mark Lobodzinski706e15a2018-12-12 15:35:29 -07001021#if defined __ANDROID__
1022 LOGCONSOLE("%s", cstr);
1023#endif
1024
Mark Young6ba8abe2017-11-09 10:37:04 -07001025 return false;
Petr Krause9388f62017-05-13 20:53:12 +02001026}
1027
Mark Young6ba8abe2017-11-09 10:37:04 -07001028static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_win32_debug_output_msg(
1029 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type,
1030 const VkDebugUtilsMessengerCallbackDataEXT *callback_data, void *user_data) {
1031#ifdef WIN32
Mark Lobodzinski3be125e2018-11-19 09:58:10 -07001032 std::ostringstream msg_buffer;
Mark Young6ba8abe2017-11-09 10:37:04 -07001033 char msg_severity[30];
1034 char msg_type[30];
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001035
Mark Young6ba8abe2017-11-09 10:37:04 -07001036 PrintMessageSeverity(message_severity, msg_severity);
1037 PrintMessageType(message_type, msg_type);
1038
Mark Lobodzinski3be125e2018-11-19 09:58:10 -07001039 msg_buffer << callback_data->pMessageIdName << "(" << msg_severity << " / " << msg_type
1040 << "): msgNum: " << callback_data->messageIdNumber << " - " << callback_data->pMessage << "\n";
1041 msg_buffer << " Objects: " << callback_data->objectCount << "\n";
1042
Mark Young8504ba62018-03-21 13:35:34 -06001043 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
Mark Lobodzinski3be125e2018-11-19 09:58:10 -07001044 msg_buffer << " [" << obj << "] " << std::hex << std::showbase
1045 << HandleToUint64(callback_data->pObjects[obj].objectHandle) << ", type: " << std::dec << std::noshowbase
1046 << callback_data->pObjects[obj].objectType
1047 << ", name: " << (callback_data->pObjects[obj].pObjectName ? callback_data->pObjects[obj].pObjectName : "NULL")
1048 << "\n";
Mark Young8504ba62018-03-21 13:35:34 -06001049 }
Mark Lobodzinski3be125e2018-11-19 09:58:10 -07001050 const std::string tmp = msg_buffer.str();
1051 const char *cstr = tmp.c_str();
1052 OutputDebugString(cstr);
Mark Young6ba8abe2017-11-09 10:37:04 -07001053#endif
1054
1055 return false;
1056}
1057
1058// This utility converts from the VkDebugUtilsLabelEXT structure into the logging version of the structure.
1059// In the logging version, we only record what we absolutely need to convey back to the callbacks.
1060static inline void InsertLabelIntoLog(const VkDebugUtilsLabelEXT *utils_label, std::vector<LoggingLabelData> &log_vector) {
1061 LoggingLabelData log_label_data = {};
1062 log_label_data.name = utils_label->pLabelName;
1063 log_label_data.color[0] = utils_label->color[0];
1064 log_label_data.color[1] = utils_label->color[1];
1065 log_label_data.color[2] = utils_label->color[2];
1066 log_label_data.color[3] = utils_label->color[3];
1067 log_vector.push_back(log_label_data);
1068}
1069
Mark Young8504ba62018-03-21 13:35:34 -06001070static inline void BeginQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1071 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001072 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1073 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1074 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1075 std::vector<LoggingLabelData> new_queue_labels;
1076 InsertLabelIntoLog(label_info, new_queue_labels);
1077 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1078 } else {
1079 // If the last thing was a label insert, we need to pop it off of the label vector before any
1080 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1081 // temporary location that exists until the next operation occurs. In this case, a new
1082 // "vkQueueBeginDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1083 if (report_data->queueLabelHasInsert) {
1084 report_data->queueLabelHasInsert = false;
1085 label_iter->second.pop_back();
1086 }
1087 InsertLabelIntoLog(label_info, label_iter->second);
1088 }
1089 }
1090}
1091
Mark Young8504ba62018-03-21 13:35:34 -06001092static inline void EndQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001093 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1094 if (label_iter != report_data->debugUtilsQueueLabels->end()) {
1095 // If the last thing was a label insert, we need to pop it off of the label vector before any
1096 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1097 // temporary location that exists until the next operation occurs. In this case, a
1098 // "vkQueueEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1099 if (report_data->queueLabelHasInsert) {
1100 report_data->queueLabelHasInsert = false;
1101 label_iter->second.pop_back();
1102 }
1103 // Now pop the normal item
1104 label_iter->second.pop_back();
1105 }
1106}
1107
Mark Young8504ba62018-03-21 13:35:34 -06001108static inline void InsertQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1109 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001110 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1111 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1112 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1113 std::vector<LoggingLabelData> new_queue_labels;
1114 InsertLabelIntoLog(label_info, new_queue_labels);
1115 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1116 } else {
1117 // If the last thing was a label insert, we need to pop it off of the label vector before any
1118 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1119 // temporary location that exists until the next operation occurs. In this case, a new
1120 // "vkQueueInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1121 if (report_data->queueLabelHasInsert) {
1122 label_iter->second.pop_back();
1123 }
1124 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1125 // the next queue label operation.
1126 InsertLabelIntoLog(label_info, label_iter->second);
1127 report_data->queueLabelHasInsert = true;
1128 }
1129 }
1130}
1131
Mark Young8504ba62018-03-21 13:35:34 -06001132static inline void BeginCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1133 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001134 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1135 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1136 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1137 std::vector<LoggingLabelData> new_cmdbuf_labels;
1138 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1139 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1140 } else {
1141 // If the last thing was a label insert, we need to pop it off of the label vector before any
1142 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1143 // temporary location that exists until the next operation occurs. In this case, a
1144 // "vkCmdBeginDebugUtilsLabelEXT" has occurred erasing the inserted label.
1145 if (report_data->cmdBufLabelHasInsert) {
1146 report_data->cmdBufLabelHasInsert = false;
1147 label_iter->second.pop_back();
1148 }
1149 InsertLabelIntoLog(label_info, label_iter->second);
1150 }
1151 }
1152}
1153
Mark Young8504ba62018-03-21 13:35:34 -06001154static inline void EndCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001155 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1156 if (label_iter != report_data->debugUtilsCmdBufLabels->end()) {
1157 // If the last thing was a label insert, we need to pop it off of the label vector before any
1158 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1159 // temporary location that exists until the next operation occurs. In this case, a
1160 // "vkCmdEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1161 if (report_data->cmdBufLabelHasInsert) {
1162 report_data->cmdBufLabelHasInsert = false;
1163 label_iter->second.pop_back();
1164 }
Shahbaz Youssefi10b94a62019-01-09 23:00:15 -05001165 // Guard against unbalanced markers.
1166 if (label_iter->second.size() > 0) {
1167 // Now pop the normal item
1168 label_iter->second.pop_back();
1169 }
Mark Young6ba8abe2017-11-09 10:37:04 -07001170 }
1171}
1172
Mark Young8504ba62018-03-21 13:35:34 -06001173static inline void InsertCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1174 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001175 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1176 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1177 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1178 std::vector<LoggingLabelData> new_cmdbuf_labels;
1179 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1180 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1181 } else {
1182 // If the last thing was a label insert, we need to pop it off of the label vector before any
1183 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1184 // temporary location that exists until the next operation occurs. In this case, a new
1185 // "vkCmdInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1186 if (report_data->cmdBufLabelHasInsert) {
1187 label_iter->second.pop_back();
1188 }
1189 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1190 // the next command buffer label operation.
1191 InsertLabelIntoLog(label_info, label_iter->second);
1192 report_data->cmdBufLabelHasInsert = true;
1193 }
1194 }
1195}
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001196
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001197#endif // LAYER_LOGGING_H