Jon Ashburn | 47e9289 | 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 | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 29 | #include <vkLayer.h> |
Ian Elliott | 5ada89b | 2015-02-12 11:36:03 -0700 | [diff] [blame] | 30 | #include "loader_platform.h" |
Jon Ashburn | 47e9289 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 31 | #include "layers_config.h" |
Ian Elliott | 20f0687 | 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 | 47e9289 | 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 | 0f1dbf1 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 45 | void setOption(const std::string &_option, const std::string &_val); |
| 46 | |
Jon Ashburn | 47e9289 | 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 | 0f1dbf1 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 55 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 56 | static VkLayerDbgAction stringToDbgAction(const char *_enum) |
Jon Ashburn | 0f1dbf1 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 57 | { |
| 58 | // only handles single enum values |
Courtney Goeltzenleuchter | 9cc421e | 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; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 61 | else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_LOG_MSG")) |
| 62 | return VK_DBG_LAYER_ACTION_LOG_MSG; |
| 63 | else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_BREAK")) |
| 64 | return VK_DBG_LAYER_ACTION_BREAK; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 65 | return (VkLayerDbgAction) 0; |
Jon Ashburn | 0f1dbf1 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 66 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 67 | |
| 68 | static VkFlags stringToDbgReportFlags(const char *_enum) |
| 69 | { |
| 70 | // only handles single enum values |
| 71 | if (!strcmp(_enum, "VK_DBG_REPORT_INFO")) |
| 72 | return VK_DBG_REPORT_INFO_BIT; |
| 73 | else if (!strcmp(_enum, "VK_DBG_REPORT_WARN")) |
| 74 | return VK_DBG_REPORT_WARN_BIT; |
| 75 | else if (!strcmp(_enum, "VK_DBG_REPORT_PERF_WARN")) |
| 76 | return VK_DBG_REPORT_PERF_WARN_BIT; |
| 77 | else if (!strcmp(_enum, "VK_DBG_REPORT_ERROR")) |
| 78 | return VK_DBG_REPORT_ERROR_BIT; |
Courtney Goeltzenleuchter | 35b4da9 | 2015-06-14 11:34:49 -0600 | [diff] [blame] | 79 | else if (!strcmp(_enum, "VK_DBG_REPORT_DEBUG")) |
| 80 | return VK_DBG_REPORT_DEBUG_BIT; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 81 | return (VkFlags) 0; |
| 82 | } |
| 83 | |
| 84 | static unsigned int convertStringEnumVal(const char *_enum) |
| 85 | { |
| 86 | unsigned int ret; |
| 87 | |
| 88 | ret = stringToDbgAction(_enum); |
| 89 | if (ret) |
| 90 | return ret; |
| 91 | |
| 92 | return stringToDbgReportFlags(_enum); |
| 93 | } |
| 94 | |
Jon Ashburn | 47e9289 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 95 | const char *getLayerOption(const char *_option) |
| 96 | { |
Jon Ashburn | 47e9289 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 97 | return g_configFileObj.getOption(_option); |
| 98 | } |
| 99 | |
Courtney Goeltzenleuchter | 35b4da9 | 2015-06-14 11:34:49 -0600 | [diff] [blame] | 100 | uint32_t getLayerOptionFlags(const char *_option, uint32_t optionDefault) |
| 101 | { |
| 102 | uint32_t flags = optionDefault; |
| 103 | const char *option = (g_configFileObj.getOption(_option)); |
| 104 | |
| 105 | /* parse comma-separated options */ |
| 106 | while (option) { |
| 107 | const char *p = strchr(option, ','); |
| 108 | size_t len; |
| 109 | |
| 110 | if (p) |
| 111 | len = p - option; |
| 112 | else |
| 113 | len = strlen(option); |
| 114 | |
| 115 | if (len > 0) { |
| 116 | if (strncmp(option, "warn", len) == 0) { |
| 117 | flags |= VK_DBG_REPORT_WARN_BIT; |
| 118 | } else if (strncmp(option, "info", len) == 0) { |
| 119 | flags |= VK_DBG_REPORT_INFO_BIT; |
| 120 | } else if (strncmp(option, "perf", len) == 0) { |
| 121 | flags |= VK_DBG_REPORT_PERF_WARN_BIT; |
| 122 | } else if (strncmp(option, "error", len) == 0) { |
| 123 | flags |= VK_DBG_REPORT_ERROR_BIT; |
| 124 | } else if (strncmp(option, "debug", len) == 0) { |
| 125 | flags |= VK_DBG_REPORT_DEBUG_BIT; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (!p) |
| 130 | break; |
| 131 | |
| 132 | option = p + 1; |
| 133 | } |
| 134 | return flags; |
| 135 | } |
| 136 | |
Jon Ashburn | e472239 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 137 | bool getLayerOptionEnum(const char *_option, uint32_t *optionDefault) |
Mark Lobodzinski | c5eaea6 | 2015-02-25 12:23:20 -0600 | [diff] [blame] | 138 | { |
Jon Ashburn | e472239 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 139 | bool res; |
Mark Lobodzinski | c5eaea6 | 2015-02-25 12:23:20 -0600 | [diff] [blame] | 140 | const char *option = (g_configFileObj.getOption(_option)); |
| 141 | if (option != NULL) { |
Jon Ashburn | e472239 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 142 | *optionDefault = convertStringEnumVal(option); |
| 143 | res = false; |
Mark Lobodzinski | 1ee9f3a | 2015-02-25 15:14:06 -0600 | [diff] [blame] | 144 | } else { |
Jon Ashburn | e472239 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 145 | res = true; |
Mark Lobodzinski | c5eaea6 | 2015-02-25 12:23:20 -0600 | [diff] [blame] | 146 | } |
Jon Ashburn | e472239 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 147 | return res; |
Mark Lobodzinski | c5eaea6 | 2015-02-25 12:23:20 -0600 | [diff] [blame] | 148 | } |
| 149 | |
Jon Ashburn | 0f1dbf1 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 150 | void setLayerOptionEnum(const char *_option, const char *_valEnum) |
| 151 | { |
| 152 | unsigned int val = convertStringEnumVal(_valEnum); |
| 153 | char strVal[24]; |
| 154 | snprintf(strVal, 24, "%u", val); |
| 155 | g_configFileObj.setOption(_option, strVal); |
| 156 | } |
| 157 | |
| 158 | void setLayerOption(const char *_option, const char *_val) |
| 159 | { |
| 160 | g_configFileObj.setOption(_option, _val); |
| 161 | } |
| 162 | |
Jon Ashburn | 47e9289 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 163 | ConfigFile::ConfigFile() : m_fileIsParsed(false) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | ConfigFile::~ConfigFile() |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | const char *ConfigFile::getOption(const std::string &_option) |
| 172 | { |
| 173 | std::map<std::string, std::string>::const_iterator it; |
| 174 | if (!m_fileIsParsed) |
| 175 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 176 | parseFile("vk_layer_settings.txt"); |
Jon Ashburn | 47e9289 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if ((it = m_valueMap.find(_option)) == m_valueMap.end()) |
| 180 | return NULL; |
| 181 | else |
| 182 | return it->second.c_str(); |
| 183 | } |
| 184 | |
Jon Ashburn | 0f1dbf1 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 185 | void ConfigFile::setOption(const std::string &_option, const std::string &_val) |
| 186 | { |
| 187 | if (!m_fileIsParsed) |
| 188 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 189 | parseFile("vk_layer_settings.txt"); |
Jon Ashburn | 0f1dbf1 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | m_valueMap[_option] = _val; |
| 193 | } |
| 194 | |
Jon Ashburn | 47e9289 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 195 | void ConfigFile::parseFile(const char *filename) |
| 196 | { |
| 197 | std::ifstream file; |
| 198 | char buf[MAX_CHARS_PER_LINE]; |
| 199 | |
| 200 | m_fileIsParsed = true; |
| 201 | m_valueMap.clear(); |
| 202 | |
| 203 | file.open(filename); |
| 204 | if (!file.good()) |
| 205 | return; |
| 206 | |
| 207 | // read tokens from the file and form option, value pairs |
| 208 | file.getline(buf, MAX_CHARS_PER_LINE); |
| 209 | while (!file.eof()) |
| 210 | { |
| 211 | char option[512]; |
| 212 | char value[512]; |
| 213 | |
| 214 | char *pComment; |
| 215 | |
| 216 | //discard any comments delimited by '#' in the line |
| 217 | pComment = strchr(buf, '#'); |
| 218 | if (pComment) |
| 219 | *pComment = '\0'; |
| 220 | |
| 221 | if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2) |
| 222 | { |
| 223 | std::string optStr(option); |
| 224 | std::string valStr(value); |
| 225 | m_valueMap[optStr] = valStr; |
| 226 | } |
| 227 | file.getline(buf, MAX_CHARS_PER_LINE); |
| 228 | } |
| 229 | } |
| 230 | |
Courtney Goeltzenleuchter | 065843d | 2015-06-14 11:33:06 -0600 | [diff] [blame] | 231 | void print_msg_flags(VkFlags msgFlags, char *msg_flags) |
| 232 | { |
| 233 | bool separator = false; |
| 234 | |
| 235 | msg_flags[0] = 0; |
| 236 | if (msgFlags & VK_DBG_REPORT_DEBUG_BIT) { |
| 237 | strcat(msg_flags, "DEBUG"); |
| 238 | separator = true; |
| 239 | } |
| 240 | if (msgFlags & VK_DBG_REPORT_INFO_BIT) { |
| 241 | if (separator) strcat(msg_flags, ","); |
| 242 | strcat(msg_flags, "INFO"); |
| 243 | separator = true; |
| 244 | } |
| 245 | if (msgFlags & VK_DBG_REPORT_WARN_BIT) { |
| 246 | if (separator) strcat(msg_flags, ","); |
| 247 | strcat(msg_flags, "WARN"); |
| 248 | separator = true; |
| 249 | } |
| 250 | if (msgFlags & VK_DBG_REPORT_PERF_WARN_BIT) { |
| 251 | if (separator) strcat(msg_flags, ","); |
| 252 | strcat(msg_flags, "PERF"); |
| 253 | separator = true; |
| 254 | } |
| 255 | if (msgFlags & VK_DBG_REPORT_ERROR_BIT) { |
| 256 | if (separator) strcat(msg_flags, ","); |
| 257 | strcat(msg_flags, "ERROR"); |
| 258 | } |
| 259 | } |
| 260 | |