Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2014 Lunarg, Inc. |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | **************************************************************************/ |
| 25 | #include <fstream> |
| 26 | #include <string> |
| 27 | #include <map> |
| 28 | #include <string.h> |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 29 | #include <vkLayer.h> |
Ian Elliott | f15eb8a | 2015-02-12 11:36:03 -0700 | [diff] [blame] | 30 | #include "loader_platform.h" |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 31 | #include "layers_config.h" |
Ian Elliott | 655cad7 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 32 | // The following is #included again to catch certain OS-specific functions |
| 33 | // being used: |
| 34 | #include "loader_platform.h" |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 35 | |
| 36 | #define MAX_CHARS_PER_LINE 4096 |
| 37 | |
| 38 | class ConfigFile |
| 39 | { |
| 40 | public: |
| 41 | ConfigFile(); |
| 42 | ~ConfigFile(); |
| 43 | |
| 44 | const char *getOption(const std::string &_option); |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 45 | void setOption(const std::string &_option, const std::string &_val); |
| 46 | |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 47 | private: |
| 48 | bool m_fileIsParsed; |
| 49 | std::map<std::string, std::string> m_valueMap; |
| 50 | |
| 51 | void parseFile(const char *filename); |
| 52 | }; |
| 53 | |
| 54 | static ConfigFile g_configFileObj; |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 55 | |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 56 | static VkLayerDbgAction stringToDbgAction(const char *_enum) |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 57 | { |
| 58 | // only handles single enum values |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 59 | if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_IGNORE")) |
| 60 | return VK_DBG_LAYER_ACTION_IGNORE; |
| 61 | else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_CALLBACK")) |
| 62 | return VK_DBG_LAYER_ACTION_CALLBACK; |
| 63 | else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_LOG_MSG")) |
| 64 | return VK_DBG_LAYER_ACTION_LOG_MSG; |
| 65 | else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_BREAK")) |
| 66 | return VK_DBG_LAYER_ACTION_BREAK; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 67 | return (VkLayerDbgAction) 0; |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 68 | } |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 69 | |
| 70 | static VkFlags stringToDbgReportFlags(const char *_enum) |
| 71 | { |
| 72 | // only handles single enum values |
| 73 | if (!strcmp(_enum, "VK_DBG_REPORT_INFO")) |
| 74 | return VK_DBG_REPORT_INFO_BIT; |
| 75 | else if (!strcmp(_enum, "VK_DBG_REPORT_WARN")) |
| 76 | return VK_DBG_REPORT_WARN_BIT; |
| 77 | else if (!strcmp(_enum, "VK_DBG_REPORT_PERF_WARN")) |
| 78 | return VK_DBG_REPORT_PERF_WARN_BIT; |
| 79 | else if (!strcmp(_enum, "VK_DBG_REPORT_ERROR")) |
| 80 | return VK_DBG_REPORT_ERROR_BIT; |
Courtney Goeltzenleuchter | b874b8c | 2015-06-14 11:34:49 -0600 | [diff] [blame^] | 81 | else if (!strcmp(_enum, "VK_DBG_REPORT_DEBUG")) |
| 82 | return VK_DBG_REPORT_DEBUG_BIT; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 83 | return (VkFlags) 0; |
| 84 | } |
| 85 | |
| 86 | static unsigned int convertStringEnumVal(const char *_enum) |
| 87 | { |
| 88 | unsigned int ret; |
| 89 | |
| 90 | ret = stringToDbgAction(_enum); |
| 91 | if (ret) |
| 92 | return ret; |
| 93 | |
| 94 | return stringToDbgReportFlags(_enum); |
| 95 | } |
| 96 | |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 97 | const char *getLayerOption(const char *_option) |
| 98 | { |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 99 | return g_configFileObj.getOption(_option); |
| 100 | } |
| 101 | |
Courtney Goeltzenleuchter | b874b8c | 2015-06-14 11:34:49 -0600 | [diff] [blame^] | 102 | uint32_t getLayerOptionFlags(const char *_option, uint32_t optionDefault) |
| 103 | { |
| 104 | uint32_t flags = optionDefault; |
| 105 | const char *option = (g_configFileObj.getOption(_option)); |
| 106 | |
| 107 | /* parse comma-separated options */ |
| 108 | while (option) { |
| 109 | const char *p = strchr(option, ','); |
| 110 | size_t len; |
| 111 | |
| 112 | if (p) |
| 113 | len = p - option; |
| 114 | else |
| 115 | len = strlen(option); |
| 116 | |
| 117 | if (len > 0) { |
| 118 | if (strncmp(option, "warn", len) == 0) { |
| 119 | flags |= VK_DBG_REPORT_WARN_BIT; |
| 120 | } else if (strncmp(option, "info", len) == 0) { |
| 121 | flags |= VK_DBG_REPORT_INFO_BIT; |
| 122 | } else if (strncmp(option, "perf", len) == 0) { |
| 123 | flags |= VK_DBG_REPORT_PERF_WARN_BIT; |
| 124 | } else if (strncmp(option, "error", len) == 0) { |
| 125 | flags |= VK_DBG_REPORT_ERROR_BIT; |
| 126 | } else if (strncmp(option, "debug", len) == 0) { |
| 127 | flags |= VK_DBG_REPORT_DEBUG_BIT; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (!p) |
| 132 | break; |
| 133 | |
| 134 | option = p + 1; |
| 135 | } |
| 136 | return flags; |
| 137 | } |
| 138 | |
Jon Ashburn | a8aa837 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 139 | bool getLayerOptionEnum(const char *_option, uint32_t *optionDefault) |
Mark Lobodzinski | 8a2b75b | 2015-02-25 12:23:20 -0600 | [diff] [blame] | 140 | { |
Jon Ashburn | a8aa837 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 141 | bool res; |
Mark Lobodzinski | 8a2b75b | 2015-02-25 12:23:20 -0600 | [diff] [blame] | 142 | const char *option = (g_configFileObj.getOption(_option)); |
| 143 | if (option != NULL) { |
Jon Ashburn | a8aa837 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 144 | *optionDefault = convertStringEnumVal(option); |
| 145 | res = false; |
Mark Lobodzinski | 0d6e66d | 2015-02-25 15:14:06 -0600 | [diff] [blame] | 146 | } else { |
Jon Ashburn | a8aa837 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 147 | res = true; |
Mark Lobodzinski | 8a2b75b | 2015-02-25 12:23:20 -0600 | [diff] [blame] | 148 | } |
Jon Ashburn | a8aa837 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 149 | return res; |
Mark Lobodzinski | 8a2b75b | 2015-02-25 12:23:20 -0600 | [diff] [blame] | 150 | } |
| 151 | |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 152 | void setLayerOptionEnum(const char *_option, const char *_valEnum) |
| 153 | { |
| 154 | unsigned int val = convertStringEnumVal(_valEnum); |
| 155 | char strVal[24]; |
| 156 | snprintf(strVal, 24, "%u", val); |
| 157 | g_configFileObj.setOption(_option, strVal); |
| 158 | } |
| 159 | |
| 160 | void setLayerOption(const char *_option, const char *_val) |
| 161 | { |
| 162 | g_configFileObj.setOption(_option, _val); |
| 163 | } |
| 164 | |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 165 | ConfigFile::ConfigFile() : m_fileIsParsed(false) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | ConfigFile::~ConfigFile() |
| 170 | { |
| 171 | } |
| 172 | |
| 173 | const char *ConfigFile::getOption(const std::string &_option) |
| 174 | { |
| 175 | std::map<std::string, std::string>::const_iterator it; |
| 176 | if (!m_fileIsParsed) |
| 177 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 178 | parseFile("vk_layer_settings.txt"); |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if ((it = m_valueMap.find(_option)) == m_valueMap.end()) |
| 182 | return NULL; |
| 183 | else |
| 184 | return it->second.c_str(); |
| 185 | } |
| 186 | |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 187 | void ConfigFile::setOption(const std::string &_option, const std::string &_val) |
| 188 | { |
| 189 | if (!m_fileIsParsed) |
| 190 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 191 | parseFile("vk_layer_settings.txt"); |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | m_valueMap[_option] = _val; |
| 195 | } |
| 196 | |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 197 | void ConfigFile::parseFile(const char *filename) |
| 198 | { |
| 199 | std::ifstream file; |
| 200 | char buf[MAX_CHARS_PER_LINE]; |
| 201 | |
| 202 | m_fileIsParsed = true; |
| 203 | m_valueMap.clear(); |
| 204 | |
| 205 | file.open(filename); |
| 206 | if (!file.good()) |
| 207 | return; |
| 208 | |
| 209 | // read tokens from the file and form option, value pairs |
| 210 | file.getline(buf, MAX_CHARS_PER_LINE); |
| 211 | while (!file.eof()) |
| 212 | { |
| 213 | char option[512]; |
| 214 | char value[512]; |
| 215 | |
| 216 | char *pComment; |
| 217 | |
| 218 | //discard any comments delimited by '#' in the line |
| 219 | pComment = strchr(buf, '#'); |
| 220 | if (pComment) |
| 221 | *pComment = '\0'; |
| 222 | |
| 223 | if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2) |
| 224 | { |
| 225 | std::string optStr(option); |
| 226 | std::string valStr(value); |
| 227 | m_valueMap[optStr] = valStr; |
| 228 | } |
| 229 | file.getline(buf, MAX_CHARS_PER_LINE); |
| 230 | } |
| 231 | } |
| 232 | |
Courtney Goeltzenleuchter | f85e361 | 2015-06-14 11:33:06 -0600 | [diff] [blame] | 233 | void print_msg_flags(VkFlags msgFlags, char *msg_flags) |
| 234 | { |
| 235 | bool separator = false; |
| 236 | |
| 237 | msg_flags[0] = 0; |
| 238 | if (msgFlags & VK_DBG_REPORT_DEBUG_BIT) { |
| 239 | strcat(msg_flags, "DEBUG"); |
| 240 | separator = true; |
| 241 | } |
| 242 | if (msgFlags & VK_DBG_REPORT_INFO_BIT) { |
| 243 | if (separator) strcat(msg_flags, ","); |
| 244 | strcat(msg_flags, "INFO"); |
| 245 | separator = true; |
| 246 | } |
| 247 | if (msgFlags & VK_DBG_REPORT_WARN_BIT) { |
| 248 | if (separator) strcat(msg_flags, ","); |
| 249 | strcat(msg_flags, "WARN"); |
| 250 | separator = true; |
| 251 | } |
| 252 | if (msgFlags & VK_DBG_REPORT_PERF_WARN_BIT) { |
| 253 | if (separator) strcat(msg_flags, ","); |
| 254 | strcat(msg_flags, "PERF"); |
| 255 | separator = true; |
| 256 | } |
| 257 | if (msgFlags & VK_DBG_REPORT_ERROR_BIT) { |
| 258 | if (separator) strcat(msg_flags, ","); |
| 259 | strcat(msg_flags, "ERROR"); |
| 260 | } |
| 261 | } |
| 262 | |