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