1 /* Copyright (c) 2015-2016, 2020-2021 The Khronos Group Inc.
2 * Copyright (c) 2015-2016, 2020-2021 Valve Corporation
3 * Copyright (c) 2015-2016, 2020-2021 LunarG, Inc.
4 *
5 * 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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * 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.
16 *
17 * Author: Mark Lobodzinski <mark@lunarg.com>
18 * Author: Dave Houlton <daveh@lunarg.com>
19 *
20 */
21
22 #include "vk_layer_utils.h"
23
24 #include <string.h>
25 #include <string>
26 #include <vector>
27
28 #include "vulkan/vulkan.h"
29 #include "vk_layer_config.h"
30
31 static const uint8_t kUtF8OneByteCode = 0xC0;
32 static const uint8_t kUtF8OneByteMask = 0xE0;
33 static const uint8_t kUtF8TwoByteCode = 0xE0;
34 static const uint8_t kUtF8TwoByteMask = 0xF0;
35 static const uint8_t kUtF8ThreeByteCode = 0xF0;
36 static const uint8_t kUtF8ThreeByteMask = 0xF8;
37 static const uint8_t kUtF8DataByteCode = 0x80;
38 static const uint8_t kUtF8DataByteMask = 0xC0;
39
vk_string_validate(const int max_length,const char * utf8)40 VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
41 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
42 int num_char_bytes = 0;
43 int i, j;
44
45 for (i = 0; i <= max_length; i++) {
46 if (utf8[i] == 0) {
47 break;
48 } else if (i == max_length) {
49 result |= VK_STRING_ERROR_LENGTH;
50 break;
51 } else if ((utf8[i] >= 0xa) && (utf8[i] < 0x7f)) {
52 num_char_bytes = 0;
53 } else if ((utf8[i] & kUtF8OneByteMask) == kUtF8OneByteCode) {
54 num_char_bytes = 1;
55 } else if ((utf8[i] & kUtF8TwoByteMask) == kUtF8TwoByteCode) {
56 num_char_bytes = 2;
57 } else if ((utf8[i] & kUtF8ThreeByteMask) == kUtF8ThreeByteCode) {
58 num_char_bytes = 3;
59 } else {
60 result |= VK_STRING_ERROR_BAD_DATA;
61 break;
62 }
63
64 // Validate the following num_char_bytes of data
65 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
66 if (++i == max_length) {
67 result |= VK_STRING_ERROR_LENGTH;
68 break;
69 }
70 if ((utf8[i] & kUtF8DataByteMask) != kUtF8DataByteCode) {
71 result |= VK_STRING_ERROR_BAD_DATA;
72 break;
73 }
74 }
75 if (result != VK_STRING_ERROR_NONE) break;
76 }
77 return result;
78 }
79
80 // Utility function for determining if a string is in a set of strings
white_list(const char * item,const std::set<std::string> & list)81 VK_LAYER_EXPORT bool white_list(const char *item, const std::set<std::string> &list) { return (list.find(item) != list.end()); }
82
83 // Debug callbacks get created in three ways:
84 // o Application-defined debug callbacks
85 // o Through settings in a vk_layer_settings.txt file
86 // o By default, if neither an app-defined debug callback nor a vk_layer_settings.txt file is present
87 //
88 // At layer initialization time, default logging callbacks are created to output layer error messages.
89 // If a vk_layer_settings.txt file is present its settings will override any default settings.
90 //
91 // If a vk_layer_settings.txt file is present and an application defines a debug callback, both callbacks
92 // will be active. If no vk_layer_settings.txt file is present, creating an application-defined debug
93 // callback will cause the default callbacks to be unregisterd and removed.
layer_debug_messenger_actions(debug_report_data * report_data,const VkAllocationCallbacks * pAllocator,const char * layer_identifier)94 VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
95 const char *layer_identifier) {
96 VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE;
97
98 std::string report_flags_key = layer_identifier;
99 std::string debug_action_key = layer_identifier;
100 std::string log_filename_key = layer_identifier;
101 report_flags_key.append(".report_flags");
102 debug_action_key.append(".debug_action");
103 log_filename_key.append(".log_filename");
104
105 // Initialize layer options
106 LogMessageTypeFlags report_flags = GetLayerOptionFlags(report_flags_key, log_msg_type_option_definitions, 0);
107 VkLayerDbgActionFlags debug_action = GetLayerOptionFlags(debug_action_key, debug_actions_option_definitions, 0);
108 // Flag as default if these settings are not from a vk_layer_settings.txt file
109 bool default_layer_callback = (debug_action & VK_DBG_LAYER_ACTION_DEFAULT) ? true : false;
110
111 auto dbg_create_info = LvlInitStruct<VkDebugUtilsMessengerCreateInfoEXT>();
112 dbg_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
113 if (report_flags & kErrorBit) {
114 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
115 }
116 if (report_flags & kWarningBit) {
117 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
118 }
119 if (report_flags & kPerformanceWarningBit) {
120 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
121 dbg_create_info.messageType |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
122 }
123 if (report_flags & kInformationBit) {
124 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
125 }
126 if (report_flags & kDebugBit) {
127 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
128 }
129
130 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) {
131 const char *log_filename = getLayerOption(log_filename_key.c_str());
132 FILE *log_output = getLayerLogOutput(log_filename, layer_identifier);
133 dbg_create_info.pfnUserCallback = messenger_log_callback;
134 dbg_create_info.pUserData = (void *)log_output;
135 layer_create_messenger_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &messenger);
136 }
137
138 messenger = VK_NULL_HANDLE;
139
140 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
141 dbg_create_info.pfnUserCallback = messenger_win32_debug_output_msg;
142 dbg_create_info.pUserData = NULL;
143 layer_create_messenger_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &messenger);
144 }
145
146 messenger = VK_NULL_HANDLE;
147
148 if (debug_action & VK_DBG_LAYER_ACTION_BREAK) {
149 dbg_create_info.pfnUserCallback = MessengerBreakCallback;
150 dbg_create_info.pUserData = NULL;
151 layer_create_messenger_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &messenger);
152 }
153 }
154
155 // NOTE: This function has been deprecated, and the above function (layer_debug_messenger_actions) should be
156 // used in its place.
layer_debug_report_actions(debug_report_data * report_data,const VkAllocationCallbacks * pAllocator,const char * layer_identifier)157 VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
158 const char *layer_identifier) {
159 VkDebugReportCallbackEXT callback = VK_NULL_HANDLE;
160
161 std::string report_flags_key = layer_identifier;
162 std::string debug_action_key = layer_identifier;
163 std::string log_filename_key = layer_identifier;
164 report_flags_key.append(".report_flags");
165 debug_action_key.append(".debug_action");
166 log_filename_key.append(".log_filename");
167
168 // Initialize layer options
169 VkDebugReportFlagsEXT report_flags = GetLayerOptionFlags(report_flags_key, report_flags_option_definitions, 0);
170 VkLayerDbgActionFlags debug_action = GetLayerOptionFlags(debug_action_key, debug_actions_option_definitions, 0);
171 // Flag as default if these settings are not from a vk_layer_settings.txt file
172 bool default_layer_callback = (debug_action & VK_DBG_LAYER_ACTION_DEFAULT) ? true : false;
173
174 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) {
175 const char *log_filename = getLayerOption(log_filename_key.c_str());
176 FILE *log_output = getLayerLogOutput(log_filename, layer_identifier);
177 auto dbg_create_info = LvlInitStruct<VkDebugReportCallbackCreateInfoEXT>();
178 dbg_create_info.flags = report_flags;
179 dbg_create_info.pfnCallback = report_log_callback;
180 dbg_create_info.pUserData = (void *)log_output;
181 layer_create_report_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &callback);
182 }
183
184 callback = VK_NULL_HANDLE;
185
186 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
187 auto dbg_create_info = LvlInitStruct<VkDebugReportCallbackCreateInfoEXT>();
188 dbg_create_info.flags = report_flags;
189 dbg_create_info.pfnCallback = report_win32_debug_output_msg;
190 dbg_create_info.pUserData = NULL;
191 layer_create_report_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &callback);
192 }
193
194 callback = VK_NULL_HANDLE;
195
196 if (debug_action & VK_DBG_LAYER_ACTION_BREAK) {
197 auto dbg_create_info = LvlInitStruct<VkDebugReportCallbackCreateInfoEXT>();
198 dbg_create_info.flags = report_flags;
199 dbg_create_info.pfnCallback = DebugBreakCallback;
200 dbg_create_info.pUserData = NULL;
201 layer_create_report_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &callback);
202 }
203 }
204
get_chain_info(const VkInstanceCreateInfo * pCreateInfo,VkLayerFunction func)205 VK_LAYER_EXPORT VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func) {
206 VkLayerInstanceCreateInfo *chain_info = (VkLayerInstanceCreateInfo *)pCreateInfo->pNext;
207 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == func)) {
208 chain_info = (VkLayerInstanceCreateInfo *)chain_info->pNext;
209 }
210 assert(chain_info != NULL);
211 return chain_info;
212 }
213
get_chain_info(const VkDeviceCreateInfo * pCreateInfo,VkLayerFunction func)214 VK_LAYER_EXPORT VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func) {
215 VkLayerDeviceCreateInfo *chain_info = (VkLayerDeviceCreateInfo *)pCreateInfo->pNext;
216 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && chain_info->function == func)) {
217 chain_info = (VkLayerDeviceCreateInfo *)chain_info->pNext;
218 }
219 assert(chain_info != NULL);
220 return chain_info;
221 }
222