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