1/************************************************************************** 2 * 3 * Copyright 2014 Valve Software 4 * Copyright 2015 Google Inc. 5 * All Rights Reserved. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 * Author: Jon Ashburn <jon@lunarg.com> 20 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> 21 * Author: Tobin Ehlis <tobin@lunarg.com> 22 * Author: Mark Lobodzinski <mark@lunarg.com> 23 **************************************************************************/ 24#include "vk_layer_config.h" 25#include "vulkan/vk_sdk_platform.h" 26#include <fstream> 27#include <iostream> 28#include <map> 29#include <string.h> 30#include <string> 31#include <sys/stat.h> 32#include <vulkan/vk_layer.h> 33 34#if defined(_WIN32) 35#include <Windows.h> 36#endif 37 38#define MAX_CHARS_PER_LINE 4096 39 40class ConfigFile { 41 public: 42 ConfigFile(); 43 ~ConfigFile(); 44 45 const char *getOption(const std::string &_option); 46 void setOption(const std::string &_option, const std::string &_val); 47 48 private: 49 bool m_fileIsParsed; 50 std::map<std::string, std::string> m_valueMap; 51 52 void parseFile(const char *filename); 53}; 54 55static ConfigFile g_configFileObj; 56 57std::string getEnvironment(const char *variable) { 58#if !defined(__ANDROID__) && !defined(_WIN32) 59 const char *output = getenv(variable); 60 return output == NULL ? "" : output; 61#elif defined(_WIN32) 62 int size = GetEnvironmentVariable(variable, NULL, 0); 63 if (size == 0) { 64 return ""; 65 } 66 char *buffer = new char[size]; 67 GetEnvironmentVariable(variable, buffer, size); 68 std::string output = buffer; 69 delete[] buffer; 70 return output; 71#else 72 return ""; 73#endif 74} 75 76VK_LAYER_EXPORT const char *getLayerOption(const char *_option) { return g_configFileObj.getOption(_option); } 77 78// If option is NULL or stdout, return stdout, otherwise try to open option 79// as a filename. If successful, return file handle, otherwise stdout 80VK_LAYER_EXPORT FILE *getLayerLogOutput(const char *_option, const char *layerName) { 81 FILE *log_output = NULL; 82 if (!_option || !strcmp("stdout", _option)) 83 log_output = stdout; 84 else { 85 log_output = fopen(_option, "w"); 86 if (log_output == NULL) { 87 if (_option) 88 std::cout << std::endl 89 << layerName << " ERROR: Bad output filename specified: " << _option << ". Writing to STDOUT instead" 90 << std::endl 91 << std::endl; 92 log_output = stdout; 93 } 94 } 95 return log_output; 96} 97 98// Map option strings to flag enum values 99VK_LAYER_EXPORT VkFlags GetLayerOptionFlags(std::string _option, std::unordered_map<std::string, VkFlags> const &enum_data, 100 uint32_t option_default) { 101 VkDebugReportFlagsEXT flags = option_default; 102 std::string option_list = g_configFileObj.getOption(_option.c_str()); 103 104 while (option_list.length() != 0) { 105 // Find length of option string 106 std::size_t option_length = option_list.find(","); 107 if (option_length == option_list.npos) { 108 option_length = option_list.size(); 109 } 110 111 // Get first option in list 112 const std::string option = option_list.substr(0, option_length); 113 114 auto enum_value = enum_data.find(option); 115 if (enum_value != enum_data.end()) { 116 flags |= enum_value->second; 117 } 118 119 // Remove first option from option_list 120 option_list.erase(0, option_length); 121 // Remove possible comma separator 122 std::size_t char_position = option_list.find(","); 123 if (char_position == 0) { 124 option_list.erase(char_position, 1); 125 } 126 // Remove possible space 127 char_position = option_list.find(" "); 128 if (char_position == 0) { 129 option_list.erase(char_position, 1); 130 } 131 } 132 return flags; 133} 134 135VK_LAYER_EXPORT void setLayerOption(const char *_option, const char *_val) { g_configFileObj.setOption(_option, _val); } 136 137// Constructor for ConfigFile. Initialize layers to log error messages to stdout by default. If a vk_layer_settings file is present, 138// its settings will override the defaults. 139ConfigFile::ConfigFile() : m_fileIsParsed(false) { 140 m_valueMap["lunarg_core_validation.report_flags"] = "error"; 141 m_valueMap["lunarg_object_tracker.report_flags"] = "error"; 142 m_valueMap["lunarg_parameter_validation.report_flags"] = "error"; 143 m_valueMap["google_threading.report_flags"] = "error"; 144 m_valueMap["google_unique_objects.report_flags"] = "error"; 145 146#ifdef WIN32 147 // For Windows, enable message logging AND OutputDebugString 148 m_valueMap["lunarg_core_validation.debug_action"] = 149 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT"; 150 m_valueMap["lunarg_object_tracker.debug_action"] = 151 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT"; 152 m_valueMap["lunarg_parameter_validation.debug_action"] = 153 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT"; 154 m_valueMap["google_threading.debug_action"] = 155 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT"; 156 m_valueMap["google_unique_objects.debug_action"] = 157 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT"; 158#else // WIN32 159 m_valueMap["lunarg_core_validation.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG"; 160 m_valueMap["lunarg_object_tracker.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG"; 161 m_valueMap["lunarg_parameter_validation.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG"; 162 m_valueMap["google_threading.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG"; 163 m_valueMap["google_unique_objects.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG"; 164#endif // WIN32 165 166 m_valueMap["lunarg_core_validation.log_filename"] = "stdout"; 167 m_valueMap["lunarg_object_tracker.log_filename"] = "stdout"; 168 m_valueMap["lunarg_parameter_validation.log_filename"] = "stdout"; 169 m_valueMap["google_threading.log_filename"] = "stdout"; 170 m_valueMap["google_unique_objects.log_filename"] = "stdout"; 171} 172 173ConfigFile::~ConfigFile() {} 174 175const char *ConfigFile::getOption(const std::string &_option) { 176 std::map<std::string, std::string>::const_iterator it; 177 if (!m_fileIsParsed) { 178 std::string envPath = getEnvironment("VK_LAYER_SETTINGS_PATH"); 179 180 // If the path exists use it, else use vk_layer_settings 181 struct stat info; 182 if (stat(envPath.c_str(), &info) == 0) { 183 // If this is a directory, look for vk_layer_settings within the directory 184 if (info.st_mode & S_IFDIR) { 185 envPath += "/vk_layer_settings.txt"; 186 } 187 parseFile(envPath.c_str()); 188 } else { 189 parseFile("vk_layer_settings.txt"); 190 } 191 } 192 193 if ((it = m_valueMap.find(_option)) == m_valueMap.end()) 194 return ""; 195 else 196 return it->second.c_str(); 197} 198 199void ConfigFile::setOption(const std::string &_option, const std::string &_val) { 200 if (!m_fileIsParsed) { 201 std::string envPath = getEnvironment("VK_LAYER_SETTINGS_PATH"); 202 203 // If the path exists use it, else use vk_layer_settings 204 struct stat info; 205 if (stat(envPath.c_str(), &info) == 0) { 206 // If this is a directory, look for vk_layer_settings within the directory 207 if (info.st_mode & S_IFDIR) { 208 envPath += "/vk_layer_settings.txt"; 209 } 210 parseFile(envPath.c_str()); 211 } else { 212 parseFile("vk_layer_settings.txt"); 213 } 214 } 215 216 m_valueMap[_option] = _val; 217} 218 219void ConfigFile::parseFile(const char *filename) { 220 std::ifstream file; 221 char buf[MAX_CHARS_PER_LINE]; 222 223 m_fileIsParsed = true; 224 225 file.open(filename); 226 if (!file.good()) { 227 return; 228 } 229 230 // read tokens from the file and form option, value pairs 231 file.getline(buf, MAX_CHARS_PER_LINE); 232 while (!file.eof()) { 233 char option[512]; 234 char value[512]; 235 236 char *pComment; 237 238 // discard any comments delimited by '#' in the line 239 pComment = strchr(buf, '#'); 240 if (pComment) *pComment = '\0'; 241 242 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2) { 243 std::string optStr(option); 244 std::string valStr(value); 245 m_valueMap[optStr] = valStr; 246 } 247 file.getline(buf, MAX_CHARS_PER_LINE); 248 } 249} 250 251VK_LAYER_EXPORT void print_msg_flags(VkFlags msgFlags, char *msg_flags) { 252 bool separator = false; 253 254 msg_flags[0] = 0; 255 if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) { 256 strcat(msg_flags, "DEBUG"); 257 separator = true; 258 } 259 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) { 260 if (separator) strcat(msg_flags, ","); 261 strcat(msg_flags, "INFO"); 262 separator = true; 263 } 264 if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { 265 if (separator) strcat(msg_flags, ","); 266 strcat(msg_flags, "WARN"); 267 separator = true; 268 } 269 if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) { 270 if (separator) strcat(msg_flags, ","); 271 strcat(msg_flags, "PERF"); 272 separator = true; 273 } 274 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { 275 if (separator) strcat(msg_flags, ","); 276 strcat(msg_flags, "ERROR"); 277 } 278} 279