blob: 88208f9b963493ff751a8b4e410ebf5f3bee967c [file] [log] [blame]
Jon Ashburn21001f62015-02-16 08:26:50 -07001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Jon Ashburn21001f62015-02-16 08:26:50 -07003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24#include <stdio.h>
Jon Ashburne4722392015-03-03 15:07:15 -070025#include <stdbool.h>
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060026#include "vkLayer.h"
Jon Ashburn21001f62015-02-16 08:26:50 -070027
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060028static VkLayerDbgFunctionNode *g_pDbgFunctionHead = NULL;
29static VkFlags g_reportFlags = (VkFlags) 0;
30static VkLayerDbgAction g_debugAction = VK_DBG_LAYER_ACTION_LOG_MSG;
Jon Ashburne4722392015-03-03 15:07:15 -070031static bool g_actionIsDefault = true;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060032static bool g_DEBUG_REPORT = false;
Jon Ashburn21001f62015-02-16 08:26:50 -070033static FILE *g_logFile = NULL;
34
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060035static void enable_debug_report(
36 uint32_t extension_count,
37 const VkExtensionProperties* pEnabledExtensions) // layer or extension name to be enabled
38{
39 for (uint32_t i = 0; i < extension_count; i++) {
40 /* TODO: Check other property fields */
41 if (strcmp(pEnabledExtensions[i].name, DEBUG_REPORT_EXTENSION_NAME) == 0) {
42 g_DEBUG_REPORT = true;
43 }
44 }
45}
46
47static VkResult layer_create_msg_callback(
48 VkInstance instance,
49 VkLayerInstanceDispatchTable* nextTable,
50 VkFlags msgFlags,
51 const PFN_vkDbgMsgCallback pfnMsgCallback,
52 void* pUserData,
53 VkDbgMsgCallback* pMsgCallback)
54{
55 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode*)malloc(sizeof(VkLayerDbgFunctionNode));
56 if (!pNewDbgFuncNode)
57 return VK_ERROR_OUT_OF_HOST_MEMORY;
58 VkResult result = nextTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
59 if (result == VK_SUCCESS) {
60 pNewDbgFuncNode->msgCallback = *pMsgCallback;
61 pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;
62 pNewDbgFuncNode->msgFlags = msgFlags;
63 pNewDbgFuncNode->pUserData = pUserData;
64 pNewDbgFuncNode->pNext = g_pDbgFunctionHead;
65 g_pDbgFunctionHead = pNewDbgFuncNode;
66 } else {
67 free(pNewDbgFuncNode);
68 }
69 return result;
70}
71
72static VkResult layer_destroy_msg_callback(
73 VkInstance instance,
74 VkLayerInstanceDispatchTable *nextTable,
75 VkDbgMsgCallback msg_callback)
76{
77 VkLayerDbgFunctionNode *pTrav = g_pDbgFunctionHead;
78 VkLayerDbgFunctionNode *pPrev = pTrav;
79
80 VkResult result = nextTable->DbgDestroyMsgCallback(instance, msg_callback);
81
82 while (pTrav) {
83 if (pTrav->msgCallback == msg_callback) {
84 pPrev->pNext = pTrav->pNext;
85 if (g_pDbgFunctionHead == pTrav)
86 g_pDbgFunctionHead = pTrav->pNext;
87 free(pTrav);
88 break;
89 }
90 pPrev = pTrav;
91 pTrav = pTrav->pNext;
92 }
93
94 return result;
95}
96
Jon Ashburn21001f62015-02-16 08:26:50 -070097// Utility function to handle reporting
98// If callbacks are enabled, use them, otherwise use printf
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060099static void layerCbMsg(
100 VkFlags msgFlags,
101 VkObjectType objectType,
102 VkObject srcObject,
103 size_t location,
104 int32_t msgCode,
105 const char* pLayerPrefix,
106 const char* pMsg)
Jon Ashburn21001f62015-02-16 08:26:50 -0700107{
Courtney Goeltzenleuchterd661a822015-02-22 16:15:32 -0700108 if (g_logFile == NULL) {
109 g_logFile = stdout;
110 }
111
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600112 VkLayerDbgFunctionNode *pTrav = g_pDbgFunctionHead;
113 while (pTrav) {
114 if (pTrav->msgFlags & msgFlags) {
115 pTrav->pfnMsgCallback(msgFlags,
116 objectType, srcObject,
117 location,
118 msgCode,
119 pLayerPrefix,
120 pMsg,
121 (void *) pTrav->pUserData);
122 }
123 pTrav = pTrav->pNext;
124 }
125#if 0
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600126 if (g_debugAction & (VK_DBG_LAYER_ACTION_LOG_MSG | VK_DBG_LAYER_ACTION_CALLBACK)) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600127 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
128 if (g_reportFlags <= VK_DBG_LAYER_LEVEL_ERROR) {
129 if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG) {
130 fprintf(g_logFile, "{%s}ERROR : %s\n", pLayerPrefix, pMsg);
131 fflush(g_logFile);
Jon Ashburn21001f62015-02-16 08:26:50 -0700132 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600133 if (g_debugAction & VK_DBG_LAYER_ACTION_CALLBACK) {
134 while (pTrav) {
135 pTrav->pfnMsgCallback(msgFlags,
136 objectType, srcObject,
137 location,
138 msgCode,
139 pLayerPrefix,
140 pMsg,
141 pTrav->pUserData);
142 pTrav = pTrav->pNext;
143 }
144 }
145 }
146 }
147 switch (msgType) {
148 case VK_DBG_REPORT_ERROR_BIT:
Jon Ashburn21001f62015-02-16 08:26:50 -0700149 break;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600150 case VK_DBG_MSG_WARNING:
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600151 if (g_reportFlags <= VK_DBG_LAYER_LEVEL_WARN) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600152 if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)
Jon Ashburn21001f62015-02-16 08:26:50 -0700153 fprintf(g_logFile, "{%s}WARN : %s\n", pLayerPrefix, pMsg);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600154 if (g_debugAction & VK_DBG_LAYER_ACTION_CALLBACK)
Jon Ashburn21001f62015-02-16 08:26:50 -0700155 while (pTrav) {
156 pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);
157 pTrav = pTrav->pNext;
158 }
159 }
160 break;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600161 case VK_DBG_MSG_PERF_WARNING:
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600162 if (g_reportFlags <= VK_DBG_LAYER_LEVEL_PERF_WARN) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600163 if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)
Jon Ashburn21001f62015-02-16 08:26:50 -0700164 fprintf(g_logFile, "{%s}PERF_WARN : %s\n", pLayerPrefix, pMsg);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600165 if (g_debugAction & VK_DBG_LAYER_ACTION_CALLBACK)
Jon Ashburn21001f62015-02-16 08:26:50 -0700166 while (pTrav) {
167 pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);
168 pTrav = pTrav->pNext;
169 }
170 }
171 break;
172 default:
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600173 if (g_reportFlags <= VK_DBG_LAYER_LEVEL_INFO) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600174 if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)
Jon Ashburn21001f62015-02-16 08:26:50 -0700175 fprintf(g_logFile, "{%s}INFO : %s\n", pLayerPrefix, pMsg);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600176 if (g_debugAction & VK_DBG_LAYER_ACTION_CALLBACK)
Jon Ashburn21001f62015-02-16 08:26:50 -0700177 while (pTrav) {
178 pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);
179 pTrav = pTrav->pNext;
180 }
181 }
182 break;
183 }
184 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600185#endif
Jon Ashburn21001f62015-02-16 08:26:50 -0700186}