blob: bca1c5287f9754afe5e2c557573d46fe845f1197 [file] [log] [blame]
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001/* 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.
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -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
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -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.
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060016 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060017 * Author: Mark Lobodzinski <mark@lunarg.com>
Dave Houlton3c9fca72017-03-27 17:25:54 -060018 * Author: Dave Houlton <daveh@lunarg.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060019 *
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060020 */
21
Petr Krausc53604b2019-09-06 00:31:48 +020022#include "vk_layer_utils.h"
23
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060024#include <string.h>
Mark Lobodzinski882655d2016-01-05 11:32:53 -070025#include <string>
Petr Krausc53604b2019-09-06 00:31:48 +020026#include <vector>
John Zulauf2c2ccd42019-04-05 13:13:13 -060027
David Pinedo9316d3b2015-11-06 12:54:48 -070028#include "vulkan/vulkan.h"
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060029#include "vk_layer_config.h"
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060030
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070031static const uint8_t kUtF8OneByteCode = 0xC0;
32static const uint8_t kUtF8OneByteMask = 0xE0;
33static const uint8_t kUtF8TwoByteCode = 0xE0;
34static const uint8_t kUtF8TwoByteMask = 0xF0;
35static const uint8_t kUtF8ThreeByteCode = 0xF0;
36static const uint8_t kUtF8ThreeByteMask = 0xF8;
37static const uint8_t kUtF8DataByteCode = 0x80;
38static const uint8_t kUtF8DataByteMask = 0xC0;
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070039
Mike Stroyana551bc02016-09-28 09:42:28 -060040VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinski9a1c7b72016-01-12 11:52:05 -070041 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Courtney Goeltzenleuchter8ed83c82016-03-22 16:31:34 -060042 int num_char_bytes = 0;
Jon Ashburn5484e0c2016-03-08 17:48:44 -070043 int i, j;
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070044
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -070045 for (i = 0; i <= max_length; i++) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070046 if (utf8[i] == 0) {
47 break;
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -070048 } else if (i == max_length) {
Mark Lobodzinski07a973f2020-02-12 15:36:48 -070049 result |= VK_STRING_ERROR_LENGTH;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070050 break;
Tony Barbour4575af42016-03-31 16:54:49 -060051 } else if ((utf8[i] >= 0xa) && (utf8[i] < 0x7f)) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070052 num_char_bytes = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070053 } else if ((utf8[i] & kUtF8OneByteMask) == kUtF8OneByteCode) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070054 num_char_bytes = 1;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070055 } else if ((utf8[i] & kUtF8TwoByteMask) == kUtF8TwoByteCode) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070056 num_char_bytes = 2;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070057 } else if ((utf8[i] & kUtF8ThreeByteMask) == kUtF8ThreeByteCode) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070058 num_char_bytes = 3;
59 } else {
Mark Lobodzinski07a973f2020-02-12 15:36:48 -070060 result |= VK_STRING_ERROR_BAD_DATA;
61 break;
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070062 }
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 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070070 if ((utf8[i] & kUtF8DataByteMask) != kUtF8DataByteCode) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070071 result |= VK_STRING_ERROR_BAD_DATA;
Mark Lobodzinski07a973f2020-02-12 15:36:48 -070072 break;
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070073 }
74 }
Mark Lobodzinski07a973f2020-02-12 15:36:48 -070075 if (result != VK_STRING_ERROR_NONE) break;
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070076 }
77 return result;
78}
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060079
Mark Lobodzinskia0555012018-08-15 16:43:49 -060080// Utility function for determining if a string is in a set of strings
81VK_LAYER_EXPORT bool white_list(const char *item, const std::set<std::string> &list) { return (list.find(item) != list.end()); }
Mark Lobodzinskiaa96d322016-08-26 08:47:37 -060082
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060083// 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.
Petr Kraus4ed81e32019-09-02 23:41:19 +020094VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
95 const char *layer_identifier) {
Mark Lobodzinski22d32452018-11-19 10:21:59 -070096 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
Mark Lobodzinskie12c8a82020-02-28 10:11:15 -0700106 LogMessageTypeFlags report_flags = GetLayerOptionFlags(report_flags_key, log_msg_type_option_definitions, 0);
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700107 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
Nathaniel Cesariofc6291e2021-04-06 00:22:15 -0600109 auto dbg_create_info = LvlInitStruct<VkDebugUtilsMessengerCreateInfoEXT>();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700110 dbg_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
Mark Lobodzinskie12c8a82020-02-28 10:11:15 -0700111 if (report_flags & kErrorBit) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700112 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700113 }
Mark Lobodzinskie12c8a82020-02-28 10:11:15 -0700114 if (report_flags & kWarningBit) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700115 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700116 }
Mark Lobodzinskie12c8a82020-02-28 10:11:15 -0700117 if (report_flags & kPerformanceWarningBit) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700118 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
119 dbg_create_info.messageType |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700120 }
Mark Lobodzinskie12c8a82020-02-28 10:11:15 -0700121 if (report_flags & kInformationBit) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700122 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700123 }
Mark Lobodzinskie12c8a82020-02-28 10:11:15 -0700124 if (report_flags & kDebugBit) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700125 dbg_create_info.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700126 }
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);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700131 dbg_create_info.pfnUserCallback = messenger_log_callback;
132 dbg_create_info.pUserData = (void *)log_output;
133 layer_create_messenger_callback(report_data, true, &dbg_create_info, pAllocator, &messenger);
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700134 }
135
136 messenger = VK_NULL_HANDLE;
137
138 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700139 dbg_create_info.pfnUserCallback = messenger_win32_debug_output_msg;
140 dbg_create_info.pUserData = NULL;
141 layer_create_messenger_callback(report_data, true, &dbg_create_info, pAllocator, &messenger);
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700142 }
Mark Lobodzinskif5d5e652019-07-03 14:44:51 -0600143
144 messenger = VK_NULL_HANDLE;
145
146 if (debug_action & VK_DBG_LAYER_ACTION_BREAK) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700147 dbg_create_info.pfnUserCallback = MessengerBreakCallback;
148 dbg_create_info.pUserData = NULL;
149 layer_create_messenger_callback(report_data, true, &dbg_create_info, pAllocator, &messenger);
Mark Lobodzinskif5d5e652019-07-03 14:44:51 -0600150 }
Mark Lobodzinski22d32452018-11-19 10:21:59 -0700151}
152
153// NOTE: This function has been deprecated, and the above function (layer_debug_messenger_actions) should be
154// used in its place.
Petr Kraus4ed81e32019-09-02 23:41:19 +0200155VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
156 const char *layer_identifier) {
Mark Lobodzinskif8568472016-03-17 13:59:51 -0600157 VkDebugReportCallbackEXT callback = VK_NULL_HANDLE;
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600158
Dustin Gravesbce0a372016-03-16 18:43:34 -0600159 std::string report_flags_key = layer_identifier;
160 std::string debug_action_key = layer_identifier;
161 std::string log_filename_key = layer_identifier;
162 report_flags_key.append(".report_flags");
163 debug_action_key.append(".debug_action");
164 log_filename_key.append(".log_filename");
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600165
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600166 // Initialize layer options
167 VkDebugReportFlagsEXT report_flags = GetLayerOptionFlags(report_flags_key, report_flags_option_definitions, 0);
168 VkLayerDbgActionFlags debug_action = GetLayerOptionFlags(debug_action_key, debug_actions_option_definitions, 0);
169 // Flag as default if these settings are not from a vk_layer_settings.txt file
170 bool default_layer_callback = (debug_action & VK_DBG_LAYER_ACTION_DEFAULT) ? true : false;
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600171
172 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) {
Dustin Gravesbce0a372016-03-16 18:43:34 -0600173 const char *log_filename = getLayerOption(log_filename_key.c_str());
174 FILE *log_output = getLayerLogOutput(log_filename, layer_identifier);
Nathaniel Cesariofc6291e2021-04-06 00:22:15 -0600175 auto dbg_create_info = LvlInitStruct<VkDebugReportCallbackCreateInfoEXT>();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700176 dbg_create_info.flags = report_flags;
177 dbg_create_info.pfnCallback = report_log_callback;
178 dbg_create_info.pUserData = (void *)log_output;
179 layer_create_report_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &callback);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600180 }
181
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600182 callback = VK_NULL_HANDLE;
183
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600184 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
Nathaniel Cesariofc6291e2021-04-06 00:22:15 -0600185 auto dbg_create_info = LvlInitStruct<VkDebugReportCallbackCreateInfoEXT>();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700186 dbg_create_info.flags = report_flags;
187 dbg_create_info.pfnCallback = report_win32_debug_output_msg;
188 dbg_create_info.pUserData = NULL;
189 layer_create_report_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &callback);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600190 }
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600191
192 callback = VK_NULL_HANDLE;
193
194 if (debug_action & VK_DBG_LAYER_ACTION_BREAK) {
Nathaniel Cesariofc6291e2021-04-06 00:22:15 -0600195 auto dbg_create_info = LvlInitStruct<VkDebugReportCallbackCreateInfoEXT>();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700196 dbg_create_info.flags = report_flags;
197 dbg_create_info.pfnCallback = DebugBreakCallback;
198 dbg_create_info.pUserData = NULL;
199 layer_create_report_callback(report_data, default_layer_callback, &dbg_create_info, pAllocator, &callback);
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600200 }
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600201}
Mark Young6ba8abe2017-11-09 10:37:04 -0700202
Mark Lobodzinskic1b5b882018-06-25 14:54:04 -0600203VK_LAYER_EXPORT VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func) {
204 VkLayerInstanceCreateInfo *chain_info = (VkLayerInstanceCreateInfo *)pCreateInfo->pNext;
205 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == func)) {
206 chain_info = (VkLayerInstanceCreateInfo *)chain_info->pNext;
207 }
208 assert(chain_info != NULL);
209 return chain_info;
210}
211
212VK_LAYER_EXPORT VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func) {
213 VkLayerDeviceCreateInfo *chain_info = (VkLayerDeviceCreateInfo *)pCreateInfo->pNext;
214 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && chain_info->function == func)) {
215 chain_info = (VkLayerDeviceCreateInfo *)chain_info->pNext;
216 }
217 assert(chain_info != NULL);
218 return chain_info;
219}