blob: 31bedd8d222cb57a6d3181809639708f8c080a80 [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 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
22#include <string.h>
Mark Lobodzinski882655d2016-01-05 11:32:53 -070023#include <string>
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060024#include <vector>
Mark Lobodzinski9bfeb042016-08-26 11:31:25 -060025#include <map>
David Pinedo9316d3b2015-11-06 12:54:48 -070026#include "vulkan/vulkan.h"
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060027#include "vk_layer_config.h"
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060028#include "vk_layer_utils.h"
29
Dustin Graves33b7cbe2016-04-06 11:06:19 -060030static const uint8_t UTF8_ONE_BYTE_CODE = 0xC0;
31static const uint8_t UTF8_ONE_BYTE_MASK = 0xE0;
32static const uint8_t UTF8_TWO_BYTE_CODE = 0xE0;
33static const uint8_t UTF8_TWO_BYTE_MASK = 0xF0;
34static const uint8_t UTF8_THREE_BYTE_CODE = 0xF0;
35static const uint8_t UTF8_THREE_BYTE_MASK = 0xF8;
36static const uint8_t UTF8_DATA_BYTE_CODE = 0x80;
37static const uint8_t UTF8_DATA_BYTE_MASK = 0xC0;
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070038
Mike Stroyana551bc02016-09-28 09:42:28 -060039VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinski9a1c7b72016-01-12 11:52:05 -070040 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Courtney Goeltzenleuchter8ed83c82016-03-22 16:31:34 -060041 int num_char_bytes = 0;
Jon Ashburn5484e0c2016-03-08 17:48:44 -070042 int i, j;
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070043
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -070044 for (i = 0; i <= max_length; i++) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070045 if (utf8[i] == 0) {
46 break;
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -070047 } else if (i == max_length) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070048 result = VK_STRING_ERROR_LENGTH;
49 break;
Tony Barbour4575af42016-03-31 16:54:49 -060050 } else if ((utf8[i] >= 0xa) && (utf8[i] < 0x7f)) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070051 num_char_bytes = 0;
Jon Ashburn5484e0c2016-03-08 17:48:44 -070052 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070053 num_char_bytes = 1;
Jon Ashburn5484e0c2016-03-08 17:48:44 -070054 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskia9f33492016-01-11 14:17:05 -070055 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}
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060075
Mark Lobodzinskiaa96d322016-08-26 08:47:37 -060076// Utility function for finding a text string in another string
Mike Stroyana551bc02016-09-28 09:42:28 -060077VK_LAYER_EXPORT bool white_list(const char *item, const char *list) {
Mark Lobodzinskiaa96d322016-08-26 08:47:37 -060078 std::string candidate(item);
79 std::string white_list(list);
80 return (white_list.find(candidate) != std::string::npos);
81}
82
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.
Mike Stroyana551bc02016-09-28 09:42:28 -060094VK_LAYER_EXPORT void layer_debug_actions(debug_report_data *report_data, std::vector<VkDebugReportCallbackEXT> &logging_callback,
95 const VkAllocationCallbacks *pAllocator, const char *layer_identifier) {
Mark Lobodzinskif8568472016-03-17 13:59:51 -060096 VkDebugReportCallbackEXT callback = VK_NULL_HANDLE;
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060097
Dustin Gravesbce0a372016-03-16 18:43:34 -060098 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");
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600104
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600105 // Initialize layer options
106 VkDebugReportFlagsEXT report_flags = GetLayerOptionFlags(report_flags_key, report_flags_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;
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600110
111 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) {
Dustin Gravesbce0a372016-03-16 18:43:34 -0600112 const char *log_filename = getLayerOption(log_filename_key.c_str());
113 FILE *log_output = getLayerLogOutput(log_filename, layer_identifier);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600114 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
115 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
116 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
117 dbgCreateInfo.flags = report_flags;
118 dbgCreateInfo.pfnCallback = log_callback;
119 dbgCreateInfo.pUserData = (void *)log_output;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600120 layer_create_msg_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600121 logging_callback.push_back(callback);
122 }
123
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600124 callback = VK_NULL_HANDLE;
125
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600126 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
127 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
128 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
129 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
130 dbgCreateInfo.flags = report_flags;
131 dbgCreateInfo.pfnCallback = win32_debug_output_msg;
132 dbgCreateInfo.pUserData = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600133 layer_create_msg_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600134 logging_callback.push_back(callback);
135 }
136}