blob: 7cf113bf9917b38bcf6fbca9e5596c06b37d5a42 [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 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>
19 *
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060020 */
21
22#ifndef LAYER_LOGGING_H
23#define LAYER_LOGGING_H
24
Mark Lobodzinskib87f9022016-05-24 16:04:56 -060025#include "vk_loader_layer.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060026#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060027#include "vk_layer_data.h"
28#include "vk_layer_table.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060029#include "vk_loader_platform.h"
30#include "vulkan/vk_layer.h"
Karl Schultzd7f37542016-05-10 11:36:08 -060031#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060032#include <stdarg.h>
33#include <stdbool.h>
34#include <stdio.h>
35#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060036#include <vector>
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060037
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060038typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060039 VkLayerDbgFunctionNode *debug_callback_list;
40 VkLayerDbgFunctionNode *default_debug_callback_list;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -060041 VkFlags active_flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060042 bool g_DEBUG_REPORT;
Tony Barbour3431dac2017-06-19 16:50:37 -060043 std::unordered_map<uint64_t, std::string> *debugObjectNameMap;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060044} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060045
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070046template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
Jon Ashburn5484e0c2016-03-08 17:48:44 -070047 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060048
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060049// Forward Declarations
50static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
51 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
52 const char *pLayerPrefix, const char *pMsg);
53
54// Add a debug message callback node structure to the specified callback linked list
55static inline void AddDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
56 VkLayerDbgFunctionNode *new_node) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060057 new_node->pNext = *list_head;
58 *list_head = new_node;
59}
60
61// Remove specified debug message callback node structure from the specified callback linked list
62static inline void RemoveDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
63 VkDebugReportCallbackEXT callback) {
64 VkLayerDbgFunctionNode *cur_callback = *list_head;
65 VkLayerDbgFunctionNode *prev_callback = cur_callback;
66 bool matched = false;
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070067 VkFlags local_flags = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060068
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060069 while (cur_callback) {
70 if (cur_callback->msgCallback == callback) {
71 matched = true;
72 prev_callback->pNext = cur_callback->pNext;
73 if (*list_head == cur_callback) {
74 *list_head = cur_callback->pNext;
75 }
76 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -060077 reinterpret_cast<uint64_t &>(cur_callback->msgCallback), 0, 0, "DebugReport",
78 "Destroyed callback\n");
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060079 } else {
80 matched = false;
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070081 local_flags |= cur_callback->msgFlags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060082 }
83 prev_callback = cur_callback;
84 cur_callback = cur_callback->pNext;
85 if (matched) {
86 free(prev_callback);
87 }
88 }
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070089 debug_data->active_flags = local_flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060090}
91
92// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
93static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
94 VkLayerDbgFunctionNode *current_callback = *list_head;
95 VkLayerDbgFunctionNode *prev_callback = current_callback;
96
97 while (current_callback) {
98 prev_callback = current_callback->pNext;
99 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600100 (uint64_t)current_callback->msgCallback, 0, 0, "DebugReport",
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600101 "Debug Report callbacks not removed before DestroyInstance");
102 free(current_callback);
103 current_callback = prev_callback;
104 }
105 *list_head = NULL;
106}
107
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600108// Utility function to handle reporting
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600109static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
110 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
111 const char *pLayerPrefix, const char *pMsg) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600112 bool bail = false;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600113 VkLayerDbgFunctionNode *pTrav = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600114
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600115 if (debug_data->debug_callback_list != NULL) {
116 pTrav = debug_data->debug_callback_list;
117 } else {
118 pTrav = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600119 }
120
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600121 while (pTrav) {
122 if (pTrav->msgFlags & msgFlags) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600123 auto it = debug_data->debugObjectNameMap->find(srcObject);
124 if (it == debug_data->debugObjectNameMap->end()) {
125 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg,
126 pTrav->pUserData)) {
127 bail = true;
128 }
129 } else {
130 std::string newMsg = "SrcObject name = ";
131 newMsg.append(it->second.c_str());
132 newMsg.append(" ");
133 newMsg.append(pMsg);
134 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, newMsg.c_str(),
135 pTrav->pUserData)) {
136 bail = true;
137 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600138 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600139 }
140 pTrav = pTrav->pNext;
141 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600142
143 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600144}
145
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700146static inline debug_report_data *debug_report_create_instance(
147 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
148 const char *const *ppEnabledExtensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600149{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600150 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700151 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600152
153 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600154 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600155 // TODO: Check other property fields
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700156 if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600157 debug_data->g_DEBUG_REPORT = true;
158 }
159 }
Tony Barbour3431dac2017-06-19 16:50:37 -0600160 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600161 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600162}
163
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700164static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600165 if (debug_data) {
166 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
167 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
Tony Barbour3431dac2017-06-19 16:50:37 -0600168 delete debug_data->debugObjectNameMap;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600169 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600170 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600171}
172
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700173static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600174 // DEBUG_REPORT shares data between Instance and Device,
175 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600176 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600177}
178
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600179static inline void layer_debug_report_destroy_device(VkDevice device) {
180 // Nothing to do since we're using instance data record
181}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600182
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600183static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
184 const VkAllocationCallbacks *pAllocator) {
185 RemoveDebugMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
186 RemoveDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
187}
188
189static inline VkResult layer_create_msg_callback(debug_report_data *debug_data, bool default_callback,
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700190 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
191 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700192 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700193 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600194
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600195 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700196 if (!(*pCallback)) *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700197 pNewDbgFuncNode->msgCallback = *pCallback;
198 pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback;
199 pNewDbgFuncNode->msgFlags = pCreateInfo->flags;
200 pNewDbgFuncNode->pUserData = pCreateInfo->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600201
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600202 if (default_callback) {
203 AddDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski2134fc32016-11-10 09:10:08 -0700204 debug_data->active_flags |= pCreateInfo->flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600205 } else {
206 AddDebugMessageCallback(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski2134fc32016-11-10 09:10:08 -0700207 debug_data->active_flags = pCreateInfo->flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600208 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600209
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700210 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600211 (uint64_t)*pCallback, 0, 0, "DebugReport", "Added callback");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600212 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600213}
214
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700215static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) {
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600216 if (!debug_data || !debug_data->g_DEBUG_REPORT) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600217 return NULL;
218 }
219
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700220 if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700221 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600222 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700223 if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700224 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600225 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700226 if (!strcmp(funcName, "vkDebugReportMessageEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700227 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700228 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600229 return NULL;
230}
231
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600232// This utility (called at vkCreateInstance() time), looks at a pNext chain.
233// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
234// then allocates an array that can hold that many structs, as well as that
235// many VkDebugReportCallbackEXT handles. It then copies each
236// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
237static VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks, VkDebugReportCallbackCreateInfoEXT **infos,
238 VkDebugReportCallbackEXT **callbacks) {
239 uint32_t n = *num_callbacks = 0;
240
241 const void *pNext = pChain;
242 while (pNext) {
243 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
244 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
245 n++;
246 }
247 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
248 }
249 if (n == 0) {
250 return VK_SUCCESS;
251 }
252
253 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
254 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
255 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
256 if (!pInfos) {
257 return VK_ERROR_OUT_OF_HOST_MEMORY;
258 }
259 // 3rd, allocate memory for a unique handle for each callback:
260 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
261 if (!pCallbacks) {
262 free(pInfos);
263 return VK_ERROR_OUT_OF_HOST_MEMORY;
264 }
265 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
266 // vkDestroyInstance, and assign a unique handle to each callback (just
267 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
268 pNext = pChain;
269 while (pNext) {
270 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
271 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
272 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
273 }
274 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
275 }
276
277 *num_callbacks = n;
278 return VK_SUCCESS;
279}
280
281// This utility frees the arrays allocated by layer_copy_tmp_callbacks()
282static void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
283 free(infos);
284 free(callbacks);
285}
286
287// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
288// that were copied by layer_copy_tmp_callbacks()
289static VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
290 VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
291 VkResult rtn = VK_SUCCESS;
292 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600293 rtn = layer_create_msg_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600294 if (rtn != VK_SUCCESS) {
295 for (uint32_t j = 0; j < i; j++) {
296 layer_destroy_msg_callback(debug_data, callbacks[j], NULL);
297 }
298 return rtn;
299 }
300 }
301 return rtn;
302}
303
304// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
305// that were copied by layer_copy_tmp_callbacks()
306static void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
307 VkDebugReportCallbackEXT *callbacks) {
308 for (uint32_t i = 0; i < num_callbacks; i++) {
309 layer_destroy_msg_callback(debug_data, callbacks[i], NULL);
310 }
311}
312
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600313// Checks if the message will get logged.
314// Allows layer to defer collecting & formating data if the
315// message will be discarded.
Dustin Graves8f1eab92016-04-05 09:41:17 -0600316static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) {
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600317 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600318 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600319 return false;
320 }
321
322 return true;
323}
324
Chris Forbes8a25bce2016-03-24 12:06:35 +1300325#ifdef WIN32
326static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
327 *strp = nullptr;
328 int size = _vscprintf(fmt, ap);
329 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700330 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300331 if (!*strp) {
332 return -1;
333 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700334 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300335 }
336 return size;
337}
338#endif
339
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600340// Output log message via DEBUG_REPORT
341// Takes format and variable arg list so that output string
342// is only computed if a message needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800343#ifndef WIN32
Tobin Ehlis17978d82016-05-17 07:58:01 -0600344static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600345 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...)
346 __attribute__((format(printf, 8, 9)));
Michael Lentine010f4692015-11-03 16:19:46 -0800347#endif
Tobin Ehlis17978d82016-05-17 07:58:01 -0600348static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600349 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format,
350 ...) {
Courtney Goeltzenleuchter61cb70a2015-06-26 15:14:50 -0600351 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600352 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600353 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600354 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600355
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600356 va_list argptr;
357 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300358 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200359 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600360 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200361 str = nullptr;
362 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600363 va_end(argptr);
Chris Forbesed6a1b32016-04-28 14:27:19 +1200364 bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix,
365 str ? str : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300366 free(str);
367 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600368}
369
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700370static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
371 size_t location, int32_t msgCode, const char *pLayerPrefix,
372 const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600373 char msg_flags[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600374
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600375 print_msg_flags(msgFlags, msg_flags);
376
Mark Muelleraab36502016-05-03 13:17:29 -0600377 fprintf((FILE *)pUserData, "%s(%s): object: 0x%" PRIx64 " type: %d location: %lu msgCode: %d: %s\n", pLayerPrefix, msg_flags,
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700378 srcObject, objType, (unsigned long)location, msgCode, pMsg);
379 fflush((FILE *)pUserData);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600380
381 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600382}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600383
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700384static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
385 uint64_t srcObject, size_t location, int32_t msgCode,
386 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600387#ifdef WIN32
388 char msg_flags[30];
389 char buf[2048];
390
391 print_msg_flags(msgFlags, msg_flags);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700392 _snprintf(buf, sizeof(buf) - 1,
393 "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix,
394 msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg);
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600395
396 OutputDebugString(buf);
397#endif
398
399 return false;
400}
401
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600402// TODO: Could be autogenerated for the specific handles for extra type safety...
403template <typename HANDLE_T>
404static inline uint64_t HandleToUint64(HANDLE_T *h) {
405 return reinterpret_cast<uint64_t>(h);
Petr Krause9388f62017-05-13 20:53:12 +0200406}
407
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600408template <typename HANDLE_T>
409uint64_t HandleToUint64(HANDLE_T h);
410
411static inline uint64_t HandleToUint64(uint64_t h) { return h; }
412
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700413#endif // LAYER_LOGGING_H