blob: 6d5c55bf9693872848e577986472f4132eee6a68 [file] [log] [blame]
Mark Young6ba8abe2017-11-09 10:37:04 -07001/* Copyright (c) 2015-2017 The Khronos Group Inc.
2 * Copyright (c) 2015-2017 Valve Corporation
3 * Copyright (c) 2015-2017 LunarG, Inc.
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -06004 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060016 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060017 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
18 * Author: Tobin Ehlis <tobin@lunarg.com>
Mark Young6ba8abe2017-11-09 10:37:04 -070019 * Author: Mark Young <marky@lunarg.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060020 *
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060021 */
22
23#ifndef LAYER_LOGGING_H
24#define LAYER_LOGGING_H
25
Mark Lobodzinskib87f9022016-05-24 16:04:56 -060026#include "vk_loader_layer.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060027#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060028#include "vk_layer_data.h"
29#include "vk_layer_table.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060030#include "vk_loader_platform.h"
31#include "vulkan/vk_layer.h"
Mark Young6ba8abe2017-11-09 10:37:04 -070032#include "vk_object_types.h"
Mark Lobodzinski487a0d12018-03-30 10:09:03 -060033#include "vk_validation_error_messages.h"
Mark Lobodzinskic9d81652017-08-10 11:01:17 -060034#include <signal.h>
Karl Schultzd7f37542016-05-10 11:36:08 -060035#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060036#include <stdarg.h>
37#include <stdbool.h>
38#include <stdio.h>
39#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060040#include <vector>
Mark Young6ba8abe2017-11-09 10:37:04 -070041#include <sstream>
42#include <string>
43
44// TODO: Could be autogenerated for the specific handles for extra type safety...
45template <typename HANDLE_T>
46static inline uint64_t HandleToUint64(HANDLE_T *h) {
47 return reinterpret_cast<uint64_t>(h);
48}
49
50static inline uint64_t HandleToUint64(uint64_t h) { return h; }
51
52// Data we store per label for logging
53typedef struct _LoggingLabelData {
54 std::string name;
55 float color[4];
56} LoggingLabelData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060057
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060058typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060059 VkLayerDbgFunctionNode *debug_callback_list;
60 VkLayerDbgFunctionNode *default_debug_callback_list;
Mark Young6ba8abe2017-11-09 10:37:04 -070061 VkDebugUtilsMessageSeverityFlagsEXT active_severities;
62 VkDebugUtilsMessageTypeFlagsEXT active_types;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060063 bool g_DEBUG_REPORT;
Mark Young6ba8abe2017-11-09 10:37:04 -070064 bool g_DEBUG_UTILS;
Tony Barbour3431dac2017-06-19 16:50:37 -060065 std::unordered_map<uint64_t, std::string> *debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -070066 std::unordered_map<uint64_t, std::string> *debugUtilsObjectNameMap;
67 std::unordered_map<VkQueue, std::vector<LoggingLabelData>> *debugUtilsQueueLabels;
68 bool queueLabelHasInsert;
69 std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>> *debugUtilsCmdBufLabels;
70 bool cmdBufLabelHasInsert;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060071} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060072
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070073template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
Jon Ashburn5484e0c2016-03-08 17:48:44 -070074 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060075
Mark Young6ba8abe2017-11-09 10:37:04 -070076static inline void DebugReportFlagsToAnnotFlags(VkDebugReportFlagsEXT dr_flags, bool default_flag_is_spec,
77 VkDebugUtilsMessageSeverityFlagsEXT *da_severity,
78 VkDebugUtilsMessageTypeFlagsEXT *da_type) {
79 // All layer warnings are spec warnings currently. At least as far as anything not specifically
80 // called out. In the future, we'll label things using the new split severity and type values.
81 *da_type = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
82 *da_severity = 0;
83 if ((dr_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) != 0) {
84 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
85 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
86 }
87 if ((dr_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) != 0) {
88 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
89 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
90 }
91 if ((dr_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) != 0) {
92 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
93 *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
94 }
95 if ((dr_flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) {
96 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
97 }
98 if ((dr_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) {
99 *da_severity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
100 }
101}
102
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600103// Forward Declarations
Mark Young6ba8abe2017-11-09 10:37:04 -0700104static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
105 uint64_t src_object, size_t location, int32_t msg_code, const char *layer_prefix,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700106 const char *message, const char *text_vuid = NULL);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600107
108// Add a debug message callback node structure to the specified callback linked list
Mark Young6ba8abe2017-11-09 10:37:04 -0700109static inline void AddDebugCallbackNode(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
110 VkLayerDbgFunctionNode *new_node) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600111 new_node->pNext = *list_head;
112 *list_head = new_node;
113}
114
Mark Young6ba8abe2017-11-09 10:37:04 -0700115// Remove specified debug messenger node structure from the specified linked list
116static inline void RemoveDebugUtilsMessenger(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
117 VkDebugUtilsMessengerEXT messenger) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600118 VkLayerDbgFunctionNode *cur_callback = *list_head;
119 VkLayerDbgFunctionNode *prev_callback = cur_callback;
120 bool matched = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700121 VkFlags local_severities = 0;
122 VkFlags local_types = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600123
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600124 while (cur_callback) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700125 if (cur_callback->is_messenger && cur_callback->messenger.messenger == messenger) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600126 matched = true;
127 prev_callback->pNext = cur_callback->pNext;
128 if (*list_head == cur_callback) {
129 *list_head = cur_callback->pNext;
130 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700131 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
132 reinterpret_cast<uint64_t &>(cur_callback->messenger.messenger), 0, 0, "DebugUtilsMessenger",
133 "Destroyed messenger\n");
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600134 } else {
135 matched = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700136 local_severities |= cur_callback->messenger.messageSeverity;
137 local_types |= cur_callback->messenger.messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600138 }
139 prev_callback = cur_callback;
140 cur_callback = cur_callback->pNext;
141 if (matched) {
142 free(prev_callback);
143 }
144 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700145 debug_data->active_severities = local_severities;
146 debug_data->active_types = local_types;
147}
148
149// Remove specified debug message callback node structure from the specified callback linked list
150static inline void RemoveDebugUtilsMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
151 VkDebugReportCallbackEXT callback) {
152 VkLayerDbgFunctionNode *cur_callback = *list_head;
153 VkLayerDbgFunctionNode *prev_callback = cur_callback;
154 bool matched = false;
155 VkFlags local_severities = 0;
156 VkFlags local_types = 0;
157
158 while (cur_callback) {
159 if (!cur_callback->is_messenger && cur_callback->report.msgCallback == callback) {
160 matched = true;
161 prev_callback->pNext = cur_callback->pNext;
162 if (*list_head == cur_callback) {
163 *list_head = cur_callback->pNext;
164 }
165 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
166 reinterpret_cast<uint64_t &>(cur_callback->report.msgCallback), 0, 0, "DebugReport",
167 "Destroyed callback\n");
168 } else {
169 matched = false;
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;
175 }
176 prev_callback = cur_callback;
177 cur_callback = cur_callback->pNext;
178 if (matched) {
179 free(prev_callback);
180 }
181 }
182 debug_data->active_severities = local_severities;
183 debug_data->active_types = local_types;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600184}
185
186// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
187static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
188 VkLayerDbgFunctionNode *current_callback = *list_head;
189 VkLayerDbgFunctionNode *prev_callback = current_callback;
190
191 while (current_callback) {
192 prev_callback = current_callback->pNext;
Mark Young6ba8abe2017-11-09 10:37:04 -0700193 if (!current_callback->is_messenger) {
194 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
195 (uint64_t)current_callback->report.msgCallback, 0, 0, "DebugReport",
196 "Debug Report callbacks not removed before DestroyInstance");
197 } else {
198 debug_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
199 (uint64_t)current_callback->messenger.messenger, 0, 0, "Messenger",
200 "Debug messengers not removed before DestroyInstance");
201 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600202 free(current_callback);
203 current_callback = prev_callback;
204 }
205 *list_head = NULL;
206}
207
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700208// Note that text_vuid is a default parameter, and is optional. See the above forward declaration
Mark Young6ba8abe2017-11-09 10:37:04 -0700209static inline bool debug_log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
210 uint64_t src_object, size_t location, int32_t msg_code, const char *layer_prefix,
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700211 const char *message, const char *text_vuid) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600212 bool bail = false;
Mark Young6ba8abe2017-11-09 10:37:04 -0700213 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600214
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600215 if (debug_data->debug_callback_list != NULL) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700216 layer_dbg_node = debug_data->debug_callback_list;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600217 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700218 layer_dbg_node = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600219 }
220
Mark Young6ba8abe2017-11-09 10:37:04 -0700221 VkDebugUtilsMessageSeverityFlagsEXT severity;
222 VkDebugUtilsMessageTypeFlagsEXT types;
223 VkDebugUtilsMessengerCallbackDataEXT callback_data;
224 VkDebugUtilsObjectNameInfoEXT object_name_info;
225
226 // Convert the info to the VK_EXT_debug_utils form in case we need it.
227 DebugReportFlagsToAnnotFlags(msg_flags, true, &severity, &types);
228 object_name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
229 object_name_info.pNext = NULL;
230 object_name_info.objectType = convertDebugReportObjectToCoreObject(object_type);
231 object_name_info.objectHandle = (uint64_t)(uintptr_t)src_object;
232 object_name_info.pObjectName = NULL;
233
234 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
235 callback_data.pNext = NULL;
236 callback_data.flags = 0;
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700237 callback_data.pMessageIdName = text_vuid;
Mark Young6ba8abe2017-11-09 10:37:04 -0700238 callback_data.messageIdNumber = msg_code;
239 callback_data.pMessage = message;
240 callback_data.queueLabelCount = 0;
241 callback_data.pQueueLabels = NULL;
242 callback_data.cmdBufLabelCount = 0;
243 callback_data.pCmdBufLabels = NULL;
244 callback_data.objectCount = 1;
245 callback_data.pObjects = &object_name_info;
246
247 VkDebugUtilsLabelEXT *queue_labels = nullptr;
248 VkDebugUtilsLabelEXT *cmd_buf_labels = nullptr;
249 std::string new_debug_report_message = "";
250 std::ostringstream oss;
Mark Young6ba8abe2017-11-09 10:37:04 -0700251
252 if (0 != src_object) {
Mark Young8504ba62018-03-21 13:35:34 -0600253 oss << "Object: 0x" << std::hex << src_object;
Mark Young6ba8abe2017-11-09 10:37:04 -0700254 // If this is a queue, add any queue labels to the callback data.
255 if (VK_OBJECT_TYPE_QUEUE == object_name_info.objectType) {
256 auto label_iter = debug_data->debugUtilsQueueLabels->find(reinterpret_cast<VkQueue>(src_object));
257 if (label_iter != debug_data->debugUtilsQueueLabels->end()) {
258 queue_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
259 if (nullptr != queue_labels) {
260 // Record the labels, but record them in reverse order since we want the
261 // most recent at the top.
262 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
263 uint32_t last_index = label_size - 1;
264 for (uint32_t label = 0; label < label_size; ++label) {
265 queue_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
266 queue_labels[last_index - label].pNext = nullptr;
267 queue_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
268 queue_labels[last_index - label].color[0] = label_iter->second[label].color[0];
269 queue_labels[last_index - label].color[1] = label_iter->second[label].color[1];
270 queue_labels[last_index - label].color[2] = label_iter->second[label].color[2];
271 queue_labels[last_index - label].color[3] = label_iter->second[label].color[3];
272 }
273 callback_data.queueLabelCount = label_size;
274 callback_data.pQueueLabels = queue_labels;
275 }
276 }
277 // If this is a command buffer, add any command buffer labels to the callback data.
278 } else if (VK_OBJECT_TYPE_COMMAND_BUFFER == object_name_info.objectType) {
279 auto label_iter = debug_data->debugUtilsCmdBufLabels->find(reinterpret_cast<VkCommandBuffer>(src_object));
280 if (label_iter != debug_data->debugUtilsCmdBufLabels->end()) {
281 cmd_buf_labels = new VkDebugUtilsLabelEXT[label_iter->second.size()];
282 if (nullptr != cmd_buf_labels) {
283 // Record the labels, but record them in reverse order since we want the
284 // most recent at the top.
285 uint32_t label_size = static_cast<uint32_t>(label_iter->second.size());
286 uint32_t last_index = label_size - 1;
287 for (uint32_t label = 0; label < label_size; ++label) {
288 cmd_buf_labels[last_index - label].sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
289 cmd_buf_labels[last_index - label].pNext = nullptr;
290 cmd_buf_labels[last_index - label].pLabelName = label_iter->second[label].name.c_str();
291 cmd_buf_labels[last_index - label].color[0] = label_iter->second[label].color[0];
292 cmd_buf_labels[last_index - label].color[1] = label_iter->second[label].color[1];
293 cmd_buf_labels[last_index - label].color[2] = label_iter->second[label].color[2];
294 cmd_buf_labels[last_index - label].color[3] = label_iter->second[label].color[3];
295 }
296 callback_data.cmdBufLabelCount = label_size;
297 callback_data.pCmdBufLabels = cmd_buf_labels;
298 }
299 }
300 }
301 // Look for any debug utils or marker names to use for this object
302 callback_data.pObjects[0].pObjectName = NULL;
303 auto utils_name_iter = debug_data->debugUtilsObjectNameMap->find(src_object);
304 if (utils_name_iter != debug_data->debugUtilsObjectNameMap->end()) {
305 callback_data.pObjects[0].pObjectName = utils_name_iter->second.c_str();
306 } else {
307 auto marker_name_iter = debug_data->debugObjectNameMap->find(src_object);
308 if (marker_name_iter != debug_data->debugObjectNameMap->end()) {
309 callback_data.pObjects[0].pObjectName = marker_name_iter->second.c_str();
310 }
311 }
312 if (NULL != callback_data.pObjects[0].pObjectName) {
Mark Young8504ba62018-03-21 13:35:34 -0600313 oss << " (Name = " << callback_data.pObjects[0].pObjectName << " : Type = ";
314 } else {
315 oss << " (Type = ";
Mark Young6ba8abe2017-11-09 10:37:04 -0700316 }
Mark Young8504ba62018-03-21 13:35:34 -0600317 oss << std::to_string(object_type) << ")";
318 } else {
319 oss << "Object: VK_NULL_HANDLE (Type = " << std::to_string(object_type) << ")";
Mark Young6ba8abe2017-11-09 10:37:04 -0700320 }
321 new_debug_report_message += oss.str();
322 new_debug_report_message += " | ";
323 new_debug_report_message += message;
324
325 while (layer_dbg_node) {
Mark Young8504ba62018-03-21 13:35:34 -0600326 // If the app uses the VK_EXT_debug_report extension, call all of those registered callbacks.
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700327 if (!layer_dbg_node->is_messenger && (layer_dbg_node->report.msgFlags & msg_flags)) {
328 if (text_vuid != nullptr) {
329 // If a text vuid is supplied for the old debug report extension, prepend it to the message string
330 new_debug_report_message.insert(0, " ] ");
331 new_debug_report_message.insert(0, text_vuid);
332 new_debug_report_message.insert(0, " [ ");
333 }
334
335 if (layer_dbg_node->report.pfnMsgCallback(msg_flags, object_type, src_object, location, msg_code, layer_prefix,
336 new_debug_report_message.c_str(), layer_dbg_node->pUserData)) {
337 bail = true;
338 }
Mark Young8504ba62018-03-21 13:35:34 -0600339 // If the app uses the VK_EXT_debug_utils extension, call all of those registered callbacks.
Mark Young6ba8abe2017-11-09 10:37:04 -0700340 } else if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & severity) &&
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700341 (layer_dbg_node->messenger.messageType & types)) {
342 if (layer_dbg_node->messenger.pfnUserCallback(static_cast<VkDebugUtilsMessageSeverityFlagBitsEXT>(severity), types,
343 &callback_data, layer_dbg_node->pUserData)) {
344 bail = true;
345 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700346 }
347 layer_dbg_node = layer_dbg_node->pNext;
348 }
349
350 if (nullptr != queue_labels) {
351 delete[] queue_labels;
352 }
353 if (nullptr != cmd_buf_labels) {
354 delete[] cmd_buf_labels;
355 }
356
357 return bail;
358}
359
360static inline void DebugAnnotFlagsToReportFlags(VkDebugUtilsMessageSeverityFlagBitsEXT da_severity,
361 VkDebugUtilsMessageTypeFlagsEXT da_type, VkDebugReportFlagsEXT *dr_flags) {
362 *dr_flags = 0;
363
364 if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
365 *dr_flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
366 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) {
367 if ((da_type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) {
368 *dr_flags |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
369 } else {
370 *dr_flags |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
371 }
372 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) {
373 *dr_flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
374 } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0) {
375 *dr_flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
376 }
377}
378
379static inline bool debug_messenger_log_msg(const debug_report_data *debug_data,
380 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
381 VkDebugUtilsMessageTypeFlagsEXT message_type,
382 VkDebugUtilsMessengerCallbackDataEXT *callback_data) {
383 bool bail = false;
384 VkLayerDbgFunctionNode *layer_dbg_node = NULL;
385
386 if (debug_data->debug_callback_list != NULL) {
387 layer_dbg_node = debug_data->debug_callback_list;
388 } else {
389 layer_dbg_node = debug_data->default_debug_callback_list;
390 }
391
392 VkDebugReportFlagsEXT object_flags = 0;
393
394 DebugAnnotFlagsToReportFlags(message_severity, message_type, &object_flags);
395
396 while (layer_dbg_node) {
397 if (layer_dbg_node->is_messenger && (layer_dbg_node->messenger.messageSeverity & message_severity) &&
398 (layer_dbg_node->messenger.messageType & message_type)) {
399 // Loop through each object and give it the proper name if it was set.
400 for (uint32_t obj = 0; obj < callback_data->objectCount; obj++) {
401 auto it = debug_data->debugUtilsObjectNameMap->find(callback_data->pObjects[obj].objectHandle);
402 if (it == debug_data->debugUtilsObjectNameMap->end()) {
403 continue;
404 }
405 callback_data->pObjects[obj].pObjectName = it->second.c_str();
406 }
407 if (layer_dbg_node->messenger.pfnUserCallback(message_severity, message_type, callback_data,
408 layer_dbg_node->pUserData)) {
409 bail = true;
410 }
411 } else if (!layer_dbg_node->is_messenger && layer_dbg_node->report.msgFlags & object_flags) {
412 auto it = debug_data->debugObjectNameMap->find(callback_data->pObjects[0].objectHandle);
413 VkDebugReportObjectTypeEXT object_type = get_debug_report_enum[callback_data->pObjects[0].objectType];
Tony Barbour3431dac2017-06-19 16:50:37 -0600414 if (it == debug_data->debugObjectNameMap->end()) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700415 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
416 callback_data->messageIdNumber, callback_data->pMessageIdName,
417 callback_data->pMessage, layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600418 bail = true;
419 }
420 } else {
421 std::string newMsg = "SrcObject name = ";
422 newMsg.append(it->second.c_str());
423 newMsg.append(" ");
Mark Young6ba8abe2017-11-09 10:37:04 -0700424 newMsg.append(callback_data->pMessage);
425 if (layer_dbg_node->report.pfnMsgCallback(object_flags, object_type, callback_data->pObjects[0].objectHandle, 0,
426 callback_data->messageIdNumber, callback_data->pMessageIdName,
427 newMsg.c_str(), layer_dbg_node->pUserData)) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600428 bail = true;
429 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600430 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600431 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700432 layer_dbg_node = layer_dbg_node->pNext;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600433 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600434
435 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600436}
437
Mark Young6ba8abe2017-11-09 10:37:04 -0700438static inline debug_report_data *debug_utils_create_instance(
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700439 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
Mark Young6ba8abe2017-11-09 10:37:04 -0700440 const char *const *enabled_extensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600441{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600442 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700443 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600444
445 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600446 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600447 // TODO: Check other property fields
Mark Young6ba8abe2017-11-09 10:37:04 -0700448 if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600449 debug_data->g_DEBUG_REPORT = true;
Mark Young6ba8abe2017-11-09 10:37:04 -0700450 } else if (strcmp(enabled_extensions[i], VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) {
451 debug_data->g_DEBUG_UTILS = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600452 }
453 }
Tony Barbour3431dac2017-06-19 16:50:37 -0600454 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
Mark Young6ba8abe2017-11-09 10:37:04 -0700455 debug_data->debugUtilsObjectNameMap = new std::unordered_map<uint64_t, std::string>;
456 debug_data->debugUtilsQueueLabels = new std::unordered_map<VkQueue, std::vector<LoggingLabelData>>;
457 debug_data->debugUtilsCmdBufLabels = new std::unordered_map<VkCommandBuffer, std::vector<LoggingLabelData>>;
458 debug_data->queueLabelHasInsert = false;
459 debug_data->cmdBufLabelHasInsert = false;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600460 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600461}
462
Mark Young6ba8abe2017-11-09 10:37:04 -0700463static inline void layer_debug_utils_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600464 if (debug_data) {
465 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
466 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
Tony Barbour3431dac2017-06-19 16:50:37 -0600467 delete debug_data->debugObjectNameMap;
Mark Young6ba8abe2017-11-09 10:37:04 -0700468 delete debug_data->debugUtilsObjectNameMap;
469 delete debug_data->debugUtilsQueueLabels;
470 delete debug_data->debugUtilsCmdBufLabels;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600471 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600472 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600473}
474
Mark Young6ba8abe2017-11-09 10:37:04 -0700475static inline debug_report_data *layer_debug_utils_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600476 // DEBUG_REPORT shares data between Instance and Device,
477 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600478 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600479}
480
Mark Young6ba8abe2017-11-09 10:37:04 -0700481static inline void layer_debug_utils_destroy_device(VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600482 // Nothing to do since we're using instance data record
483}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600484
Mark Young6ba8abe2017-11-09 10:37:04 -0700485static inline void layer_destroy_messenger_callback(debug_report_data *debug_data, VkDebugUtilsMessengerEXT messenger,
486 const VkAllocationCallbacks *allocator) {
487 RemoveDebugUtilsMessenger(debug_data, &debug_data->debug_callback_list, messenger);
488 RemoveDebugUtilsMessenger(debug_data, &debug_data->default_debug_callback_list, messenger);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600489}
490
Mark Young6ba8abe2017-11-09 10:37:04 -0700491static inline VkResult layer_create_messenger_callback(debug_report_data *debug_data, bool default_callback,
492 const VkDebugUtilsMessengerCreateInfoEXT *create_info,
493 const VkAllocationCallbacks *allocator,
494 VkDebugUtilsMessengerEXT *messenger) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700495 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700496 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young6ba8abe2017-11-09 10:37:04 -0700497 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
498 pNewDbgFuncNode->is_messenger = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600499
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600500 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Young6ba8abe2017-11-09 10:37:04 -0700501 if (!(*messenger)) *messenger = (VkDebugUtilsMessengerEXT)pNewDbgFuncNode;
502 pNewDbgFuncNode->messenger.messenger = *messenger;
503 pNewDbgFuncNode->messenger.pfnUserCallback = create_info->pfnUserCallback;
504 pNewDbgFuncNode->messenger.messageSeverity = create_info->messageSeverity;
505 pNewDbgFuncNode->messenger.messageType = create_info->messageType;
506 pNewDbgFuncNode->pUserData = create_info->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600507
Mark Young6ba8abe2017-11-09 10:37:04 -0700508 debug_data->active_severities |= create_info->messageSeverity;
509 debug_data->active_types |= create_info->messageType;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600510 if (default_callback) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700511 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600512 } else {
Mark Young6ba8abe2017-11-09 10:37:04 -0700513 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600514 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600515
Mark Young6ba8abe2017-11-09 10:37:04 -0700516 VkDebugUtilsMessengerCallbackDataEXT callback_data = {};
517 VkDebugUtilsObjectNameInfoEXT blank_object = {};
518 callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
519 callback_data.pNext = NULL;
520 callback_data.flags = 0;
521 callback_data.pMessageIdName = "Layer Internal Message";
522 callback_data.messageIdNumber = 0;
523 callback_data.pMessage = "Added messenger";
524 callback_data.queueLabelCount = 0;
525 callback_data.pQueueLabels = NULL;
526 callback_data.cmdBufLabelCount = 0;
527 callback_data.pCmdBufLabels = NULL;
528 callback_data.objectCount = 1;
529 callback_data.pObjects = &blank_object;
530 blank_object.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
531 blank_object.pNext = NULL;
532 blank_object.objectType = VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT;
533 blank_object.objectHandle = HandleToUint64(*messenger);
534 blank_object.pObjectName = NULL;
535 debug_messenger_log_msg(debug_data, VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,
536 VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT, &callback_data);
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600537 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600538}
539
Mark Young6ba8abe2017-11-09 10:37:04 -0700540static inline void layer_destroy_report_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
541 const VkAllocationCallbacks *allocator) {
542 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
543 RemoveDebugUtilsMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
544}
545
546static inline VkResult layer_create_report_callback(debug_report_data *debug_data, bool default_callback,
547 const VkDebugReportCallbackCreateInfoEXT *create_info,
548 const VkAllocationCallbacks *allocator, VkDebugReportCallbackEXT *callback) {
549 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
550 if (!pNewDbgFuncNode) {
551 return VK_ERROR_OUT_OF_HOST_MEMORY;
552 }
553 memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode));
554 pNewDbgFuncNode->is_messenger = false;
555
556 // Handle of 0 is logging_callback so use allocated Node address as unique handle
557 if (!(*callback)) *callback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
558 pNewDbgFuncNode->report.msgCallback = *callback;
559 pNewDbgFuncNode->report.pfnMsgCallback = create_info->pfnCallback;
560 pNewDbgFuncNode->report.msgFlags = create_info->flags;
561 pNewDbgFuncNode->pUserData = create_info->pUserData;
562
563 VkFlags local_severity = 0;
564 VkFlags local_type = 0;
565 DebugReportFlagsToAnnotFlags(create_info->flags, true, &local_severity, &local_type);
566 debug_data->active_severities |= local_severity;
567 debug_data->active_types |= local_type;
568 if (default_callback) {
569 AddDebugCallbackNode(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
570 } else {
571 AddDebugCallbackNode(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600572 }
573
Mark Young6ba8abe2017-11-09 10:37:04 -0700574 debug_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, (uint64_t)*callback, 0,
575 0, "DebugReport", "Added callback");
576 return VK_SUCCESS;
577}
578
579static inline PFN_vkVoidFunction debug_utils_get_instance_proc_addr(debug_report_data *debug_data, const char *func_name) {
580 if (!debug_data) {
581 return NULL;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600582 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700583 if (debug_data->g_DEBUG_REPORT) {
584 if (!strcmp(func_name, "vkCreateDebugReportCallbackEXT")) {
585 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
586 }
587 if (!strcmp(func_name, "vkDestroyDebugReportCallbackEXT")) {
588 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
589 }
590 if (!strcmp(func_name, "vkDebugReportMessageEXT")) {
591 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
592 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600593 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700594 if (debug_data->g_DEBUG_UTILS) {
595 if (!strcmp(func_name, "vkCreateDebugUtilsMessengerEXT")) {
596 return (PFN_vkVoidFunction)vkCreateDebugUtilsMessengerEXT;
597 }
598 if (!strcmp(func_name, "vkDestroyDebugUtilsMessengerEXT")) {
599 return (PFN_vkVoidFunction)vkDestroyDebugUtilsMessengerEXT;
600 }
601 if (!strcmp(func_name, "vkSubmitDebugUtilsMessageEXT")) {
602 return (PFN_vkVoidFunction)vkSubmitDebugUtilsMessageEXT;
603 }
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700604 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600605 return NULL;
606}
607
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600608// This utility (called at vkCreateInstance() time), looks at a pNext chain.
609// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
610// then allocates an array that can hold that many structs, as well as that
611// many VkDebugReportCallbackEXT handles. It then copies each
612// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
Mark Young6ba8abe2017-11-09 10:37:04 -0700613static inline VkResult layer_copy_tmp_report_callbacks(const void *pChain, uint32_t *num_callbacks,
614 VkDebugReportCallbackCreateInfoEXT **infos,
615 VkDebugReportCallbackEXT **callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600616 uint32_t n = *num_callbacks = 0;
617
618 const void *pNext = pChain;
619 while (pNext) {
620 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
621 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
622 n++;
623 }
624 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
625 }
626 if (n == 0) {
627 return VK_SUCCESS;
628 }
629
630 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
631 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
632 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
633 if (!pInfos) {
634 return VK_ERROR_OUT_OF_HOST_MEMORY;
635 }
636 // 3rd, allocate memory for a unique handle for each callback:
637 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
638 if (!pCallbacks) {
639 free(pInfos);
640 return VK_ERROR_OUT_OF_HOST_MEMORY;
641 }
642 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
643 // vkDestroyInstance, and assign a unique handle to each callback (just
644 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
645 pNext = pChain;
646 while (pNext) {
647 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
648 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
649 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
650 }
651 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
652 }
653
654 *num_callbacks = n;
655 return VK_SUCCESS;
656}
657
Mark Young6ba8abe2017-11-09 10:37:04 -0700658// This utility frees the arrays allocated by layer_copy_tmp_report_callbacks()
659static inline void layer_free_tmp_report_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600660 free(infos);
661 free(callbacks);
662}
663
664// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700665// that were copied by layer_copy_tmp_report_callbacks()
666static inline VkResult layer_enable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
667 VkDebugReportCallbackCreateInfoEXT *infos,
668 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600669 VkResult rtn = VK_SUCCESS;
670 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700671 rtn = layer_create_report_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600672 if (rtn != VK_SUCCESS) {
673 for (uint32_t j = 0; j < i; j++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700674 layer_destroy_report_callback(debug_data, callbacks[j], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600675 }
676 return rtn;
677 }
678 }
679 return rtn;
680}
681
682// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
Mark Young6ba8abe2017-11-09 10:37:04 -0700683// that were copied by layer_copy_tmp_report_callbacks()
684static inline void layer_disable_tmp_report_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
685 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600686 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700687 layer_destroy_report_callback(debug_data, callbacks[i], NULL);
688 }
689}
690
691// This utility (called at vkCreateInstance() time), looks at a pNext chain.
692// It counts any VkDebugUtilsMessengerCreateInfoEXT structs that it finds. It
693// then allocates an array that can hold that many structs, as well as that
694// many VkDebugUtilsMessengerEXT handles. It then copies each
695// VkDebugUtilsMessengerCreateInfoEXT, and initializes each handle.
Mark Young8504ba62018-03-21 13:35:34 -0600696static inline VkResult layer_copy_tmp_debug_messengers(const void *pChain, uint32_t *num_messengers,
697 VkDebugUtilsMessengerCreateInfoEXT **infos,
698 VkDebugUtilsMessengerEXT **messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700699 uint32_t n = *num_messengers = 0;
700
701 const void *pNext = pChain;
702 while (pNext) {
703 // 1st, count the number VkDebugUtilsMessengerCreateInfoEXT:
704 if (((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
705 n++;
706 }
707 pNext = (void *)((VkDebugUtilsMessengerCreateInfoEXT *)pNext)->pNext;
708 }
709 if (n == 0) {
710 return VK_SUCCESS;
711 }
712
713 // 2nd, allocate memory for each VkDebugUtilsMessengerCreateInfoEXT:
714 VkDebugUtilsMessengerCreateInfoEXT *pInfos = *infos =
715 ((VkDebugUtilsMessengerCreateInfoEXT *)malloc(n * sizeof(VkDebugUtilsMessengerCreateInfoEXT)));
716 if (!pInfos) {
717 return VK_ERROR_OUT_OF_HOST_MEMORY;
718 }
719 // 3rd, allocate memory for a unique handle for each messenger:
720 VkDebugUtilsMessengerEXT *pMessengers = *messengers =
721 ((VkDebugUtilsMessengerEXT *)malloc(n * sizeof(VkDebugUtilsMessengerEXT)));
722 if (!pMessengers) {
723 free(pInfos);
724 return VK_ERROR_OUT_OF_HOST_MEMORY;
725 }
726 // 4th, copy each VkDebugUtilsMessengerCreateInfoEXT for use by
727 // vkDestroyInstance, and assign a unique handle to each callback (just
728 // use the address of the copied VkDebugUtilsMessengerCreateInfoEXT):
729 pNext = pChain;
730 while (pNext) {
731 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
732 memcpy(pInfos, pNext, sizeof(VkDebugUtilsMessengerCreateInfoEXT));
733 *pMessengers++ = (VkDebugUtilsMessengerEXT)pInfos++;
734 }
735 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
736 }
737
738 *num_messengers = n;
739 return VK_SUCCESS;
740}
741
742// This utility frees the arrays allocated by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600743static inline void layer_free_tmp_debug_messengers(VkDebugUtilsMessengerCreateInfoEXT *infos,
744 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700745 free(infos);
746 free(messengers);
747}
748
749// This utility enables all of the VkDebugUtilsMessengerCreateInfoEXT structs
750// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600751static inline VkResult layer_enable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
752 VkDebugUtilsMessengerCreateInfoEXT *infos,
753 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700754 VkResult rtn = VK_SUCCESS;
755 for (uint32_t i = 0; i < num_messengers; i++) {
756 rtn = layer_create_messenger_callback(debug_data, false, &infos[i], NULL, &messengers[i]);
757 if (rtn != VK_SUCCESS) {
758 for (uint32_t j = 0; j < i; j++) {
759 layer_destroy_messenger_callback(debug_data, messengers[j], NULL);
760 }
761 return rtn;
762 }
763 }
764 return rtn;
765}
766
767// This utility disables all of the VkDebugUtilsMessengerCreateInfoEXT structs
768// that were copied by layer_copy_tmp_debug_messengers()
Mark Young8504ba62018-03-21 13:35:34 -0600769static inline void layer_disable_tmp_debug_messengers(debug_report_data *debug_data, uint32_t num_messengers,
770 VkDebugUtilsMessengerEXT *messengers) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700771 for (uint32_t i = 0; i < num_messengers; i++) {
772 layer_destroy_messenger_callback(debug_data, messengers[i], NULL);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600773 }
774}
775
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600776// Checks if the message will get logged.
777// Allows layer to defer collecting & formating data if the
778// message will be discarded.
Mark Young6ba8abe2017-11-09 10:37:04 -0700779static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msg_flags) {
780 VkFlags local_severity = 0;
781 VkFlags local_type = 0;
782 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
783 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600784 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600785 return false;
786 }
787
788 return true;
789}
John Zulauf6664e272018-01-17 11:00:22 -0700790#ifndef WIN32
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700791static inline int string_sprintf(std::string *output, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
John Zulauf6664e272018-01-17 11:00:22 -0700792#endif
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700793static inline int string_sprintf(std::string *output, const char *fmt, ...) {
John Zulauf6664e272018-01-17 11:00:22 -0700794 std::string &formatted = *output;
795 va_list argptr;
796 va_start(argptr, fmt);
797 int reserve = vsnprintf(nullptr, 0, fmt, argptr);
798 va_end(argptr);
799 formatted.reserve(reserve + 1);
800 va_start(argptr, fmt);
801 int result = vsnprintf((char *)formatted.data(), formatted.capacity(), fmt, argptr);
802 va_end(argptr);
803 assert(result == reserve);
804 return result;
805}
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600806
Chris Forbes8a25bce2016-03-24 12:06:35 +1300807#ifdef WIN32
808static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
809 *strp = nullptr;
810 int size = _vscprintf(fmt, ap);
811 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700812 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300813 if (!*strp) {
814 return -1;
815 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700816 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300817 }
818 return size;
819}
820#endif
821
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700822// Output log message via DEBUG_REPORT. Takes format and variable arg list so that output string is only computed if a message
823// needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800824#ifndef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700825static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600826 uint64_t src_object, int32_t msg_code, const char *format, ...) __attribute__((format(printf, 6, 7)));
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700827static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
828 uint64_t src_object, std::string vuid_text, const char *format, ...)
829 __attribute__((format(printf, 6, 7)));
Michael Lentine010f4692015-11-03 16:19:46 -0800830#endif
Mark Young6ba8abe2017-11-09 10:37:04 -0700831static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600832 uint64_t src_object, int32_t msg_code, const char *format, ...) {
Mark Young6ba8abe2017-11-09 10:37:04 -0700833 VkFlags local_severity = 0;
834 VkFlags local_type = 0;
835 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
836 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600837 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600838 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600839 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600840
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600841 va_list argptr;
842 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300843 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200844 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600845 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200846 str = nullptr;
847 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600848 va_end(argptr);
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600849
850 std::string str_plus_spec_text(str);
851
852 // If the msg_code is in the error map, tack on spec text to error message.
853 if (validation_error_map.find(msg_code) != validation_error_map.end()) {
854 str_plus_spec_text += " ";
855 str_plus_spec_text += validation_error_map[msg_code];
856 }
857
Mark Lobodzinski88529492018-04-01 10:38:15 -0600858 bool result = debug_log_msg(debug_data, msg_flags, object_type, src_object, 0, msg_code, "Validation",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600859 str_plus_spec_text.c_str() ? str_plus_spec_text.c_str() : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300860 free(str);
861 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600862}
863
Mark Lobodzinski316c18c2018-02-21 09:48:42 -0700864// Overload of log_msg that takes a VUID string in place of a numerical VUID abstraction
865static inline bool log_msg(const debug_report_data *debug_data, VkFlags msg_flags, VkDebugReportObjectTypeEXT object_type,
866 uint64_t src_object, std::string vuid_text, const char *format, ...) {
867 VkFlags local_severity = 0;
868 VkFlags local_type = 0;
869 DebugReportFlagsToAnnotFlags(msg_flags, true, &local_severity, &local_type);
870 if (!debug_data || !(debug_data->active_severities & local_severity) || !(debug_data->active_types & local_type)) {
871 // Message is not wanted
872 return false;
873 }
874
875 va_list argptr;
876 va_start(argptr, format);
877 char *str;
878 if (-1 == vasprintf(&str, format, argptr)) {
879 // On failure, glibc vasprintf leaves str undefined
880 str = nullptr;
881 }
882 va_end(argptr);
883
884 std::string str_plus_spec_text(str);
885
886 // If the msg_code is in the error map, get ID, look up spec text, and tack it onto error message.
887 if (validation_error_text_map.find(vuid_text.c_str()) != validation_error_text_map.end()) {
888 str_plus_spec_text += " ";
889 str_plus_spec_text += validation_error_map[validation_error_text_map[vuid_text.c_str()]];
890 }
891
892 // Append layer prefix with VUID string, pass in UNDEFINED for numerical VUID
893 static const int UNDEFINED_VUID = -1;
894 bool result = debug_log_msg(debug_data, msg_flags, object_type, src_object, 0, UNDEFINED_VUID, "Validation",
895 str_plus_spec_text.c_str() ? str_plus_spec_text.c_str() : "Allocation failure", vuid_text.c_str());
896
897 free(str);
898 return result;
899}
900
Mark Young6ba8abe2017-11-09 10:37:04 -0700901static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_log_callback(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
902 uint64_t src_object, size_t location, int32_t msg_code,
903 const char *layer_prefix, const char *message, void *user_data) {
904 char msg_flag_string[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600905
Mark Young6ba8abe2017-11-09 10:37:04 -0700906 PrintMessageFlags(msg_flags, msg_flag_string);
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600907
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600908 fprintf((FILE *)user_data, "%s(%s): msg_code: %d: %s\n", layer_prefix, msg_flag_string, msg_code, message);
Mark Young6ba8abe2017-11-09 10:37:04 -0700909 fflush((FILE *)user_data);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600910
911 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600912}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600913
Mark Young6ba8abe2017-11-09 10:37:04 -0700914static inline VKAPI_ATTR VkBool32 VKAPI_CALL report_win32_debug_output_msg(VkFlags msg_flags, VkDebugReportObjectTypeEXT obj_type,
915 uint64_t src_object, size_t location, int32_t msg_code,
916 const char *layer_prefix, const char *message,
917 void *user_data) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600918#ifdef WIN32
Mark Young6ba8abe2017-11-09 10:37:04 -0700919 char msg_flag_string[30];
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600920 char buf[2048];
921
Mark Young6ba8abe2017-11-09 10:37:04 -0700922 PrintMessageFlags(msg_flags, msg_flag_string);
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600923 _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 -0600924
925 OutputDebugString(buf);
926#endif
927
928 return false;
929}
930
Mark Young6ba8abe2017-11-09 10:37:04 -0700931static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT obj_type,
932 uint64_t src_object, size_t location, int32_t msg_code,
933 const char *layer_prefix, const char *message, void *user_data) {
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600934#ifdef WIN32
935 DebugBreak();
936#else
937 raise(SIGTRAP);
938#endif
939
940 return false;
941}
942
Mark Young6ba8abe2017-11-09 10:37:04 -0700943static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_log_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
944 VkDebugUtilsMessageTypeFlagsEXT message_type,
945 const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
946 void *user_data) {
947 char msg_severity[30];
948 char msg_type[30];
949
950 PrintMessageSeverity(message_severity, msg_severity);
951 PrintMessageType(message_type, msg_type);
952
Mark Young8504ba62018-03-21 13:35:34 -0600953 fprintf((FILE *)user_data, "%s(%s / %s): msgNum: %d - %s\n", callback_data->pMessageIdName, msg_severity, msg_type,
Mark Young6ba8abe2017-11-09 10:37:04 -0700954 callback_data->messageIdNumber, callback_data->pMessage);
Mark Young8504ba62018-03-21 13:35:34 -0600955 fprintf((FILE *)user_data, " Objects: %d\n", callback_data->objectCount);
956 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
957 fprintf((FILE *)user_data, " [%d] 0x%" PRIx64 ", type: %d, name: %s\n", obj,
958 callback_data->pObjects[obj].objectHandle, callback_data->pObjects[obj].objectType,
959 callback_data->pObjects[obj].pObjectName);
960 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700961 fflush((FILE *)user_data);
962
963 return false;
Petr Krause9388f62017-05-13 20:53:12 +0200964}
965
Mark Young6ba8abe2017-11-09 10:37:04 -0700966static inline VKAPI_ATTR VkBool32 VKAPI_CALL messenger_win32_debug_output_msg(
967 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type,
968 const VkDebugUtilsMessengerCallbackDataEXT *callback_data, void *user_data) {
969#ifdef WIN32
970 char buf[2048];
971 char msg_severity[30];
972 char msg_type[30];
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600973
Mark Young6ba8abe2017-11-09 10:37:04 -0700974 PrintMessageSeverity(message_severity, msg_severity);
975 PrintMessageType(message_type, msg_type);
976
Mark Young8504ba62018-03-21 13:35:34 -0600977 size_t buffer_space = sizeof(buf) - 1;
978 size_t remaining_space = buffer_space;
979 _snprintf(buf, sizeof(buf) - 1, "%s(%s / %s): msgNum: %d - %s\n", callback_data->pMessageIdName, msg_severity, msg_type,
980 callback_data->messageIdNumber, callback_data->pMessage);
981 remaining_space = buffer_space - strlen(buf);
982 _snprintf(buf, remaining_space, " Objects: %d\n", callback_data->objectCount);
983 for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
984 remaining_space = buffer_space - strlen(buf);
985 if (remaining_space > 0) {
986 _snprintf(buf, remaining_space, " [%d] 0x%" PRIx64 ", type: %d, name: %s\n", obj,
987 callback_data->pObjects[obj].objectHandle, callback_data->pObjects[obj].objectType,
988 callback_data->pObjects[obj].pObjectName);
989 }
990 }
Mark Young6ba8abe2017-11-09 10:37:04 -0700991 OutputDebugString(buf);
992#endif
993
994 return false;
995}
996
997// This utility converts from the VkDebugUtilsLabelEXT structure into the logging version of the structure.
998// In the logging version, we only record what we absolutely need to convey back to the callbacks.
999static inline void InsertLabelIntoLog(const VkDebugUtilsLabelEXT *utils_label, std::vector<LoggingLabelData> &log_vector) {
1000 LoggingLabelData log_label_data = {};
1001 log_label_data.name = utils_label->pLabelName;
1002 log_label_data.color[0] = utils_label->color[0];
1003 log_label_data.color[1] = utils_label->color[1];
1004 log_label_data.color[2] = utils_label->color[2];
1005 log_label_data.color[3] = utils_label->color[3];
1006 log_vector.push_back(log_label_data);
1007}
1008
Mark Young8504ba62018-03-21 13:35:34 -06001009static inline void BeginQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1010 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001011 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1012 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1013 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1014 std::vector<LoggingLabelData> new_queue_labels;
1015 InsertLabelIntoLog(label_info, new_queue_labels);
1016 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1017 } else {
1018 // If the last thing was a label insert, we need to pop it off of the label vector before any
1019 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1020 // temporary location that exists until the next operation occurs. In this case, a new
1021 // "vkQueueBeginDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1022 if (report_data->queueLabelHasInsert) {
1023 report_data->queueLabelHasInsert = false;
1024 label_iter->second.pop_back();
1025 }
1026 InsertLabelIntoLog(label_info, label_iter->second);
1027 }
1028 }
1029}
1030
Mark Young8504ba62018-03-21 13:35:34 -06001031static inline void EndQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001032 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1033 if (label_iter != report_data->debugUtilsQueueLabels->end()) {
1034 // If the last thing was a label insert, we need to pop it off of the label vector before any
1035 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1036 // temporary location that exists until the next operation occurs. In this case, a
1037 // "vkQueueEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1038 if (report_data->queueLabelHasInsert) {
1039 report_data->queueLabelHasInsert = false;
1040 label_iter->second.pop_back();
1041 }
1042 // Now pop the normal item
1043 label_iter->second.pop_back();
1044 }
1045}
1046
Mark Young8504ba62018-03-21 13:35:34 -06001047static inline void InsertQueueDebugUtilsLabel(debug_report_data *report_data, VkQueue queue,
1048 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001049 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1050 auto label_iter = report_data->debugUtilsQueueLabels->find(queue);
1051 if (label_iter == report_data->debugUtilsQueueLabels->end()) {
1052 std::vector<LoggingLabelData> new_queue_labels;
1053 InsertLabelIntoLog(label_info, new_queue_labels);
1054 report_data->debugUtilsQueueLabels->insert({queue, new_queue_labels});
1055 } else {
1056 // If the last thing was a label insert, we need to pop it off of the label vector before any
1057 // changes. This is because a label added with "vkQueueInsertDebugUtilsLabelEXT" is only a
1058 // temporary location that exists until the next operation occurs. In this case, a new
1059 // "vkQueueInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1060 if (report_data->queueLabelHasInsert) {
1061 label_iter->second.pop_back();
1062 }
1063 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1064 // the next queue label operation.
1065 InsertLabelIntoLog(label_info, label_iter->second);
1066 report_data->queueLabelHasInsert = true;
1067 }
1068 }
1069}
1070
Mark Young8504ba62018-03-21 13:35:34 -06001071static inline void BeginCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1072 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001073 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1074 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1075 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1076 std::vector<LoggingLabelData> new_cmdbuf_labels;
1077 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1078 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1079 } else {
1080 // If the last thing was a label insert, we need to pop it off of the label vector before any
1081 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1082 // temporary location that exists until the next operation occurs. In this case, a
1083 // "vkCmdBeginDebugUtilsLabelEXT" has occurred erasing the inserted label.
1084 if (report_data->cmdBufLabelHasInsert) {
1085 report_data->cmdBufLabelHasInsert = false;
1086 label_iter->second.pop_back();
1087 }
1088 InsertLabelIntoLog(label_info, label_iter->second);
1089 }
1090 }
1091}
1092
Mark Young8504ba62018-03-21 13:35:34 -06001093static inline void EndCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001094 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1095 if (label_iter != report_data->debugUtilsCmdBufLabels->end()) {
1096 // If the last thing was a label insert, we need to pop it off of the label vector before any
1097 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1098 // temporary location that exists until the next operation occurs. In this case, a
1099 // "vkCmdEndDebugUtilsLabelEXT" has occurred erasing the inserted label.
1100 if (report_data->cmdBufLabelHasInsert) {
1101 report_data->cmdBufLabelHasInsert = false;
1102 label_iter->second.pop_back();
1103 }
1104 // Now pop the normal item
1105 label_iter->second.pop_back();
1106 }
1107}
1108
Mark Young8504ba62018-03-21 13:35:34 -06001109static inline void InsertCmdDebugUtilsLabel(debug_report_data *report_data, VkCommandBuffer command_buffer,
1110 const VkDebugUtilsLabelEXT *label_info) {
Mark Young6ba8abe2017-11-09 10:37:04 -07001111 if (nullptr != label_info && nullptr != label_info->pLabelName) {
1112 auto label_iter = report_data->debugUtilsCmdBufLabels->find(command_buffer);
1113 if (label_iter == report_data->debugUtilsCmdBufLabels->end()) {
1114 std::vector<LoggingLabelData> new_cmdbuf_labels;
1115 InsertLabelIntoLog(label_info, new_cmdbuf_labels);
1116 report_data->debugUtilsCmdBufLabels->insert({command_buffer, new_cmdbuf_labels});
1117 } else {
1118 // If the last thing was a label insert, we need to pop it off of the label vector before any
1119 // changes. This is because a label added with "vkCmdInsertDebugUtilsLabelEXT" is only a
1120 // temporary location that exists until the next operation occurs. In this case, a new
1121 // "vkCmdInsertDebugUtilsLabelEXT" has occurred erasing the previous inserted label.
1122 if (report_data->cmdBufLabelHasInsert) {
1123 label_iter->second.pop_back();
1124 }
1125 // Insert this new label and mark it as one that has been "inserted" so we can remove it on
1126 // the next command buffer label operation.
1127 InsertLabelIntoLog(label_info, label_iter->second);
1128 report_data->cmdBufLabelHasInsert = true;
1129 }
1130 }
1131}
Mark Lobodzinski863d5de2017-05-22 10:10:07 -06001132
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001133#endif // LAYER_LOGGING_H