blob: 49cbb9db08e6c406b9916814368d2d4cd72b590b [file] [log] [blame]
Jon Ashburnbe582642014-12-22 12:04:40 -07001/**************************************************************************
2 *
Jon Ashburne922f712015-11-03 13:41:23 -07003 * Copyright 2014 Valve Software
Michael Lentine695f2c22015-09-09 12:39:13 -07004 * Copyright 2015 Google Inc.
Jon Ashburnbe582642014-12-22 12:04:40 -07005 * All Rights Reserved.
6 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06007 * 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
Jon Ashburnbe582642014-12-22 12:04:40 -070010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * http://www.apache.org/licenses/LICENSE-2.0
Jon Ashburnbe582642014-12-22 12:04:40 -070012 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060013 * 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.
Jon Ashburnbe582642014-12-22 12:04:40 -070018 *
Jon Ashburne922f712015-11-03 13:41:23 -070019 * Author: Jon Ashburn <jon@lunarg.com>
20 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
21 * Author: Tobin Ehlis <tobin@lunarg.com>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060022 * Author: Mark Lobodzinski <mark@lunarg.com>
Jon Ashburnbe582642014-12-22 12:04:40 -070023 **************************************************************************/
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060024#include "vk_layer_config.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070025#include "vulkan/vk_sdk_platform.h"
Lenny Komow807ebf02016-09-30 14:15:25 -060026#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>
Jon Ashburnbe582642014-12-22 12:04:40 -070033
34#define MAX_CHARS_PER_LINE 4096
35
Jon Ashburn5484e0c2016-03-08 17:48:44 -070036class ConfigFile {
37 public:
Jon Ashburnbe582642014-12-22 12:04:40 -070038 ConfigFile();
39 ~ConfigFile();
40
41 const char *getOption(const std::string &_option);
Jon Ashburndec60512015-01-13 17:24:01 -070042 void setOption(const std::string &_option, const std::string &_val);
43
Jon Ashburn5484e0c2016-03-08 17:48:44 -070044 private:
Jon Ashburnbe582642014-12-22 12:04:40 -070045 bool m_fileIsParsed;
46 std::map<std::string, std::string> m_valueMap;
47
48 void parseFile(const char *filename);
49};
50
51static ConfigFile g_configFileObj;
Jon Ashburndec60512015-01-13 17:24:01 -070052
Lenny Komow807ebf02016-09-30 14:15:25 -060053std::string getEnvironment(const char *variable) {
54#if !defined(__ANDROID__) && !defined(_WIN32)
55 const char *output = getenv(variable);
56 return output == NULL ? "" : output;
57#elif defined(_WIN32)
58 int size = GetEnvironmentVariable(variable, NULL, 0);
59 if (size == 0) {
60 return "";
61 }
62 char *buffer = new char[size];
63 GetEnvironmentVariable(variable, buffer, size);
64 std::string output = buffer;
65 delete[] buffer;
66 return output;
67#else
68 return "";
69#endif
70}
71
Jon Ashburn5484e0c2016-03-08 17:48:44 -070072const char *getLayerOption(const char *_option) { return g_configFileObj.getOption(_option); }
Jon Ashburnbe582642014-12-22 12:04:40 -070073
Tobin Ehlisb1df55e2015-09-15 09:55:54 -060074// If option is NULL or stdout, return stdout, otherwise try to open option
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060075// as a filename. If successful, return file handle, otherwise stdout
Jon Ashburn5484e0c2016-03-08 17:48:44 -070076FILE *getLayerLogOutput(const char *_option, const char *layerName) {
77 FILE *log_output = NULL;
Tobin Ehlisb1df55e2015-09-15 09:55:54 -060078 if (!_option || !strcmp("stdout", _option))
79 log_output = stdout;
80 else {
81 log_output = fopen(_option, "w");
82 if (log_output == NULL) {
83 if (_option)
Jon Ashburn5484e0c2016-03-08 17:48:44 -070084 std::cout << std::endl
85 << layerName << " ERROR: Bad output filename specified: " << _option << ". Writing to STDOUT instead"
86 << std::endl
87 << std::endl;
Tobin Ehlisb1df55e2015-09-15 09:55:54 -060088 log_output = stdout;
89 }
90 }
Cody Northrope3e39ef2015-09-16 08:35:29 -060091 return log_output;
Tobin Ehlisb1df55e2015-09-15 09:55:54 -060092}
93
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060094// Map option strings to flag enum values
95VkFlags GetLayerOptionFlags(std::string _option, std::unordered_map<std::string, VkFlags> const &enum_data,
96 uint32_t option_default) {
97 VkDebugReportFlagsEXT flags = option_default;
98 std::string option_list = g_configFileObj.getOption(_option.c_str());
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -060099
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600100 while (option_list.length() != 0) {
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600101
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600102 // Find length of option string
103 std::size_t option_length = option_list.find(",");
104 if (option_length == option_list.npos) {
105 option_length = option_list.size();
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600106 }
107
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600108 // Get first option in list
109 const std::string option = option_list.substr(0, option_length);
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600110
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600111 auto enum_value = enum_data.find(option);
112 if (enum_value != enum_data.end()) {
113 flags |= enum_value->second;
114 }
115
116 // Remove first option from option_list
117 option_list.erase(0, option_length);
118 // Remove possible comma separator
119 std::size_t char_position = option_list.find(",");
120 if (char_position == 0) {
121 option_list.erase(char_position, 1);
122 }
123 // Remove possible space
124 char_position = option_list.find(" ");
125 if (char_position == 0) {
126 option_list.erase(char_position, 1);
127 }
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600128 }
129 return flags;
130}
131
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700132void setLayerOption(const char *_option, const char *_val) { g_configFileObj.setOption(_option, _val); }
Jon Ashburndec60512015-01-13 17:24:01 -0700133
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600134// Constructor for ConfigFile. Initialize layers to log error messages to stdout by default. If a vk_layer_settings file is present,
135// its settings will override the defaults.
136ConfigFile::ConfigFile() : m_fileIsParsed(false) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600137 m_valueMap["lunarg_core_validation.report_flags"] = "error";
138 m_valueMap["lunarg_image.report_flags"] = "error";
139 m_valueMap["lunarg_object_tracker.report_flags"] = "error";
140 m_valueMap["lunarg_parameter_validation.report_flags"] = "error";
141 m_valueMap["lunarg_swapchain.report_flags"] = "error";
142 m_valueMap["google_threading.report_flags"] = "error";
Mark Lobodzinskiad8448e2016-08-26 08:35:19 -0600143 m_valueMap["google_unique_objects.report_flags"] = "error";
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600144
145#ifdef WIN32
146 // For Windows, enable message logging AND OutputDebugString
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700147 m_valueMap["lunarg_core_validation.debug_action"] =
148 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT";
149 m_valueMap["lunarg_image.debug_action"] =
150 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT";
151 m_valueMap["lunarg_object_tracker.debug_action"] =
152 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT";
153 m_valueMap["lunarg_parameter_validation.debug_action"] =
154 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT";
155 m_valueMap["lunarg_swapchain.debug_action"] =
156 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT";
157 m_valueMap["google_threading.debug_action"] =
158 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT";
159 m_valueMap["google_unique_objects.debug_action"] =
160 "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT";
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600161#else // WIN32
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600162 m_valueMap["lunarg_core_validation.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG";
163 m_valueMap["lunarg_image.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG";
164 m_valueMap["lunarg_object_tracker.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG";
165 m_valueMap["lunarg_parameter_validation.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG";
166 m_valueMap["lunarg_swapchain.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG";
167 m_valueMap["google_threading.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG";
Mark Lobodzinskiad8448e2016-08-26 08:35:19 -0600168 m_valueMap["google_unique_objects.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG";
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600169#endif // WIN32
170
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600171 m_valueMap["lunarg_core_validation.log_filename"] = "stdout";
172 m_valueMap["lunarg_image.log_filename"] = "stdout";
173 m_valueMap["lunarg_object_tracker.log_filename"] = "stdout";
174 m_valueMap["lunarg_parameter_validation.log_filename"] = "stdout";
175 m_valueMap["lunarg_swapchain.log_filename"] = "stdout";
176 m_valueMap["google_threading.log_filename"] = "stdout";
Mark Lobodzinskiad8448e2016-08-26 08:35:19 -0600177 m_valueMap["google_unique_objects.log_filename"] = "stdout";
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600178}
Jon Ashburnbe582642014-12-22 12:04:40 -0700179
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700180ConfigFile::~ConfigFile() {}
Jon Ashburnbe582642014-12-22 12:04:40 -0700181
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700182const char *ConfigFile::getOption(const std::string &_option) {
Jon Ashburnbe582642014-12-22 12:04:40 -0700183 std::map<std::string, std::string>::const_iterator it;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700184 if (!m_fileIsParsed) {
Lenny Komow807ebf02016-09-30 14:15:25 -0600185 std::string envPath = getEnvironment("VK_LAYER_SETTINGS_PATH");
186
187 // If the path exists use it, else use vk_layer_settings
188 struct stat info;
189 if (stat(envPath.c_str(), &info) == 0) {
190 // If this is a directory, look for vk_layer_settings within the directory
191 if (info.st_mode & S_IFDIR) {
192 envPath += "/vk_layer_settings.txt";
193 }
194 parseFile(envPath.c_str());
195 } else {
196 parseFile("vk_layer_settings.txt");
197 }
Jon Ashburnbe582642014-12-22 12:04:40 -0700198 }
199
200 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600201 return "";
Jon Ashburnbe582642014-12-22 12:04:40 -0700202 else
203 return it->second.c_str();
204}
205
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700206void ConfigFile::setOption(const std::string &_option, const std::string &_val) {
207 if (!m_fileIsParsed) {
Lenny Komow807ebf02016-09-30 14:15:25 -0600208 std::string envPath = getEnvironment("VK_LAYER_SETTINGS_PATH");
209
210 // If the path exists use it, else use vk_layer_settings
211 struct stat info;
212 if (stat(envPath.c_str(), &info) == 0) {
213 // If this is a directory, look for vk_layer_settings within the directory
214 if (info.st_mode & S_IFDIR) {
215 envPath += "/vk_layer_settings.txt";
216 }
217 parseFile(envPath.c_str());
218 } else {
219 parseFile("vk_layer_settings.txt");
220 }
Jon Ashburndec60512015-01-13 17:24:01 -0700221 }
222
223 m_valueMap[_option] = _val;
224}
225
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700226void ConfigFile::parseFile(const char *filename) {
Jon Ashburnbe582642014-12-22 12:04:40 -0700227 std::ifstream file;
228 char buf[MAX_CHARS_PER_LINE];
229
230 m_fileIsParsed = true;
Jon Ashburnbe582642014-12-22 12:04:40 -0700231
232 file.open(filename);
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600233 if (!file.good()) {
Jon Ashburnbe582642014-12-22 12:04:40 -0700234 return;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600235 }
236
Jon Ashburnbe582642014-12-22 12:04:40 -0700237 // read tokens from the file and form option, value pairs
238 file.getline(buf, MAX_CHARS_PER_LINE);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700239 while (!file.eof()) {
Jon Ashburnbe582642014-12-22 12:04:40 -0700240 char option[512];
241 char value[512];
242
243 char *pComment;
244
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700245 // discard any comments delimited by '#' in the line
Jon Ashburnbe582642014-12-22 12:04:40 -0700246 pComment = strchr(buf, '#');
247 if (pComment)
248 *pComment = '\0';
249
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700250 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2) {
Jon Ashburnbe582642014-12-22 12:04:40 -0700251 std::string optStr(option);
252 std::string valStr(value);
253 m_valueMap[optStr] = valStr;
254 }
255 file.getline(buf, MAX_CHARS_PER_LINE);
256 }
257}
258
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700259void print_msg_flags(VkFlags msgFlags, char *msg_flags) {
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600260 bool separator = false;
261
262 msg_flags[0] = 0;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700263 if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600264 strcat(msg_flags, "DEBUG");
265 separator = true;
266 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700267 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700268 if (separator)
269 strcat(msg_flags, ",");
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600270 strcat(msg_flags, "INFO");
271 separator = true;
272 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700273 if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700274 if (separator)
275 strcat(msg_flags, ",");
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600276 strcat(msg_flags, "WARN");
277 separator = true;
278 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700279 if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700280 if (separator)
281 strcat(msg_flags, ",");
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600282 strcat(msg_flags, "PERF");
283 separator = true;
284 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700285 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700286 if (separator)
287 strcat(msg_flags, ",");
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600288 strcat(msg_flags, "ERROR");
289 }
290}