1 /* Copyright (c) 2015-2016, 2019 The Khronos Group Inc.
2 * Copyright (c) 2015-2016, 2019 Valve Corporation
3 * Copyright (c) 2015-2016, 2019 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 <string.h>
23 #include <string>
24 #include <vector>
25 #include <map>
26
27 #include "vulkan/vulkan.h"
28 #include "vk_layer_config.h"
29 #include "vk_layer_utils.h"
30
31 static const uint8_t UTF8_ONE_BYTE_CODE = 0xC0;
32 static const uint8_t UTF8_ONE_BYTE_MASK = 0xE0;
33 static const uint8_t UTF8_TWO_BYTE_CODE = 0xE0;
34 static const uint8_t UTF8_TWO_BYTE_MASK = 0xF0;
35 static const uint8_t UTF8_THREE_BYTE_CODE = 0xF0;
36 static const uint8_t UTF8_THREE_BYTE_MASK = 0xF8;
37 static const uint8_t UTF8_DATA_BYTE_CODE = 0x80;
38 static const uint8_t UTF8_DATA_BYTE_MASK = 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] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
54 num_char_bytes = 1;
55 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
56 num_char_bytes = 2;
57 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
58 num_char_bytes = 3;
59 } else {
60 result = VK_STRING_ERROR_BAD_DATA;
61 }
62
63 // Validate the following num_char_bytes of data
64 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
65 if (++i == max_length) {
66 result |= VK_STRING_ERROR_LENGTH;
67 break;
68 }
69 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
70 result |= VK_STRING_ERROR_BAD_DATA;
71 }
72 }
73 }
74 return result;
75 }
76
77 // Utility function for determining if a string is in a set of strings
white_list(const char * item,const std::set<std::string> & list)78 VK_LAYER_EXPORT bool white_list(const char *item, const std::set<std::string> &list) { return (list.find(item) != list.end()); }
79
80 // Debug callbacks get created in three ways:
81 // o Application-defined debug callbacks
82 // o Through settings in a vk_layer_settings.txt file
83 // o By default, if neither an app-defined debug callback nor a vk_layer_settings.txt file is present
84 //
85 // At layer initialization time, default logging callbacks are created to output layer error messages.
86 // If a vk_layer_settings.txt file is present its settings will override any default settings.
87 //
88 // If a vk_layer_settings.txt file is present and an application defines a debug callback, both callbacks
89 // will be active. If no vk_layer_settings.txt file is present, creating an application-defined debug
90 // callback will cause the default callbacks to be unregisterd and removed.
layer_debug_messenger_actions(debug_report_data * report_data,std::vector<VkDebugUtilsMessengerEXT> & logging_messenger,const VkAllocationCallbacks * pAllocator,const char * layer_identifier)91 VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data,
92 std::vector<VkDebugUtilsMessengerEXT> &logging_messenger,
93 const VkAllocationCallbacks *pAllocator, const char *layer_identifier) {
94 VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE;
95
96 std::string report_flags_key = layer_identifier;
97 std::string debug_action_key = layer_identifier;
98 std::string log_filename_key = layer_identifier;
99 report_flags_key.append(".report_flags");
100 debug_action_key.append(".debug_action");
101 log_filename_key.append(".log_filename");
102
103 // Initialize layer options
104 VkDebugReportFlagsEXT report_flags = GetLayerOptionFlags(report_flags_key, report_flags_option_definitions, 0);
105 VkLayerDbgActionFlags debug_action = GetLayerOptionFlags(debug_action_key, debug_actions_option_definitions, 0);
106 // Flag as default if these settings are not from a vk_layer_settings.txt file
107 bool default_layer_callback = (debug_action & VK_DBG_LAYER_ACTION_DEFAULT) ? true : false;
108 VkDebugUtilsMessengerCreateInfoEXT dbgCreateInfo;
109 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
110 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
111 dbgCreateInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
112 if (report_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
113 dbgCreateInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
114 }
115 if (report_flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
116 dbgCreateInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
117 }
118 if (report_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
119 dbgCreateInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
120 dbgCreateInfo.messageType |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
121 }
122 if (report_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
123 dbgCreateInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
124 }
125 if (report_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
126 dbgCreateInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
127 }
128
129 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) {
130 const char *log_filename = getLayerOption(log_filename_key.c_str());
131 FILE *log_output = getLayerLogOutput(log_filename, layer_identifier);
132 dbgCreateInfo.pfnUserCallback = messenger_log_callback;
133 dbgCreateInfo.pUserData = (void *)log_output;
134 layer_create_messenger_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &messenger);
135 logging_messenger.push_back(messenger);
136 }
137
138 messenger = VK_NULL_HANDLE;
139
140 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
141 dbgCreateInfo.pfnUserCallback = messenger_win32_debug_output_msg;
142 dbgCreateInfo.pUserData = NULL;
143 layer_create_messenger_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &messenger);
144 logging_messenger.push_back(messenger);
145 }
146
147 messenger = VK_NULL_HANDLE;
148
149 if (debug_action & VK_DBG_LAYER_ACTION_BREAK) {
150 dbgCreateInfo.pfnUserCallback = MessengerBreakCallback;
151 dbgCreateInfo.pUserData = NULL;
152 layer_create_messenger_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &messenger);
153 logging_messenger.push_back(messenger);
154 }
155 }
156
157 // NOTE: This function has been deprecated, and the above function (layer_debug_messenger_actions) should be
158 // used in its place.
layer_debug_report_actions(debug_report_data * report_data,std::vector<VkDebugReportCallbackEXT> & logging_callback,const VkAllocationCallbacks * pAllocator,const char * layer_identifier)159 VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data,
160 std::vector<VkDebugReportCallbackEXT> &logging_callback,
161 const VkAllocationCallbacks *pAllocator, const char *layer_identifier) {
162 VkDebugReportCallbackEXT callback = VK_NULL_HANDLE;
163
164 std::string report_flags_key = layer_identifier;
165 std::string debug_action_key = layer_identifier;
166 std::string log_filename_key = layer_identifier;
167 report_flags_key.append(".report_flags");
168 debug_action_key.append(".debug_action");
169 log_filename_key.append(".log_filename");
170
171 // Initialize layer options
172 VkDebugReportFlagsEXT report_flags = GetLayerOptionFlags(report_flags_key, report_flags_option_definitions, 0);
173 VkLayerDbgActionFlags debug_action = GetLayerOptionFlags(debug_action_key, debug_actions_option_definitions, 0);
174 // Flag as default if these settings are not from a vk_layer_settings.txt file
175 bool default_layer_callback = (debug_action & VK_DBG_LAYER_ACTION_DEFAULT) ? true : false;
176
177 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) {
178 const char *log_filename = getLayerOption(log_filename_key.c_str());
179 FILE *log_output = getLayerLogOutput(log_filename, layer_identifier);
180 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
181 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
182 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
183 dbgCreateInfo.flags = report_flags;
184 dbgCreateInfo.pfnCallback = report_log_callback;
185 dbgCreateInfo.pUserData = (void *)log_output;
186 layer_create_report_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback);
187 logging_callback.push_back(callback);
188 }
189
190 callback = VK_NULL_HANDLE;
191
192 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
193 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
194 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
195 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
196 dbgCreateInfo.flags = report_flags;
197 dbgCreateInfo.pfnCallback = report_win32_debug_output_msg;
198 dbgCreateInfo.pUserData = NULL;
199 layer_create_report_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback);
200 logging_callback.push_back(callback);
201 }
202
203 callback = VK_NULL_HANDLE;
204
205 if (debug_action & VK_DBG_LAYER_ACTION_BREAK) {
206 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
207 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
208 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
209 dbgCreateInfo.flags = report_flags;
210 dbgCreateInfo.pfnCallback = DebugBreakCallback;
211 dbgCreateInfo.pUserData = NULL;
212 layer_create_report_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback);
213 logging_callback.push_back(callback);
214 }
215 }
216
get_chain_info(const VkInstanceCreateInfo * pCreateInfo,VkLayerFunction func)217 VK_LAYER_EXPORT VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func) {
218 VkLayerInstanceCreateInfo *chain_info = (VkLayerInstanceCreateInfo *)pCreateInfo->pNext;
219 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == func)) {
220 chain_info = (VkLayerInstanceCreateInfo *)chain_info->pNext;
221 }
222 assert(chain_info != NULL);
223 return chain_info;
224 }
225
get_chain_info(const VkDeviceCreateInfo * pCreateInfo,VkLayerFunction func)226 VK_LAYER_EXPORT VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func) {
227 VkLayerDeviceCreateInfo *chain_info = (VkLayerDeviceCreateInfo *)pCreateInfo->pNext;
228 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && chain_info->function == func)) {
229 chain_info = (VkLayerDeviceCreateInfo *)chain_info->pNext;
230 }
231 assert(chain_info != NULL);
232 return chain_info;
233 }
234