blob: 1d2cfc7e98a1a2c1d74e42371c541ff83aae1ef1 [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 *
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 *
Jon Ashburne922f712015-11-03 13:41:23 -070025 * Author: Jon Ashburn <jon@lunarg.com>
26 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
27 * Author: Tobin Ehlis <tobin@lunarg.com>
Jon Ashburnbe582642014-12-22 12:04:40 -070028 **************************************************************************/
29#include <fstream>
30#include <string>
31#include <map>
32#include <string.h>
David Pinedo9316d3b2015-11-06 12:54:48 -070033#include <vulkan/vk_layer.h>
Tobin Ehlisb1df55e2015-09-15 09:55:54 -060034#include <iostream>
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060035#include "vk_layer_config.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070036#include "vulkan/vk_sdk_platform.h"
Jon Ashburnbe582642014-12-22 12:04:40 -070037
38#define MAX_CHARS_PER_LINE 4096
39
40class ConfigFile
41{
42public:
43 ConfigFile();
44 ~ConfigFile();
45
46 const char *getOption(const std::string &_option);
Jon Ashburndec60512015-01-13 17:24:01 -070047 void setOption(const std::string &_option, const std::string &_val);
48
Jon Ashburnbe582642014-12-22 12:04:40 -070049private:
50 bool m_fileIsParsed;
51 std::map<std::string, std::string> m_valueMap;
52
53 void parseFile(const char *filename);
54};
55
56static ConfigFile g_configFileObj;
Jon Ashburndec60512015-01-13 17:24:01 -070057
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060058static VkLayerDbgAction stringToDbgAction(const char *_enum)
Jon Ashburndec60512015-01-13 17:24:01 -070059{
60 // only handles single enum values
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060061 if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_IGNORE"))
62 return VK_DBG_LAYER_ACTION_IGNORE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060063 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_LOG_MSG"))
64 return VK_DBG_LAYER_ACTION_LOG_MSG;
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -060065#ifdef WIN32
66 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_DEBUG_OUTPUT"))
67 return VK_DBG_LAYER_ACTION_DEBUG_OUTPUT;
68#endif
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060069 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_BREAK"))
70 return VK_DBG_LAYER_ACTION_BREAK;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060071 return (VkLayerDbgAction) 0;
Jon Ashburndec60512015-01-13 17:24:01 -070072}
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060073
74static VkFlags stringToDbgReportFlags(const char *_enum)
75{
76 // only handles single enum values
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -070077 if (!strcmp(_enum, "VK_DEBUG_REPORT_INFO"))
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070078 return VK_DEBUG_REPORT_INFO_BIT_EXT;
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -070079 else if (!strcmp(_enum, "VK_DEBUG_REPORT_WARN"))
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070080 return VK_DEBUG_REPORT_WARN_BIT_EXT;
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -070081 else if (!strcmp(_enum, "VK_DEBUG_REPORT_PERF_WARN"))
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070082 return VK_DEBUG_REPORT_PERF_WARN_BIT_EXT;
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -070083 else if (!strcmp(_enum, "VK_DEBUG_REPORT_ERROR"))
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070084 return VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -070085 else if (!strcmp(_enum, "VK_DEBUG_REPORT_DEBUG"))
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070086 return VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060087 return (VkFlags) 0;
88}
89
90static unsigned int convertStringEnumVal(const char *_enum)
91{
92 unsigned int ret;
93
94 ret = stringToDbgAction(_enum);
95 if (ret)
96 return ret;
97
98 return stringToDbgReportFlags(_enum);
99}
100
Jon Ashburnbe582642014-12-22 12:04:40 -0700101const char *getLayerOption(const char *_option)
102{
Jon Ashburnbe582642014-12-22 12:04:40 -0700103 return g_configFileObj.getOption(_option);
104}
105
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600106// If option is NULL or stdout, return stdout, otherwise try to open option
107// as a filename. If successful, return file handle, otherwise stdout
108FILE* getLayerLogOutput(const char *_option, const char *layerName)
109{
110 FILE* log_output = NULL;
111 if (!_option || !strcmp("stdout", _option))
112 log_output = stdout;
113 else {
114 log_output = fopen(_option, "w");
115 if (log_output == NULL) {
116 if (_option)
117 std::cout << std::endl << layerName << " ERROR: Bad output filename specified: " << _option << ". Writing to STDOUT instead" << std::endl << std::endl;
118 log_output = stdout;
119 }
120 }
Cody Northrope3e39ef2015-09-16 08:35:29 -0600121 return log_output;
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600122}
123
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700124VkDebugReportFlagsEXT getLayerOptionFlags(const char *_option, uint32_t optionDefault)
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600125{
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700126 VkDebugReportFlagsEXT flags = optionDefault;
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600127 const char *option = (g_configFileObj.getOption(_option));
128
129 /* parse comma-separated options */
130 while (option) {
131 const char *p = strchr(option, ',');
132 size_t len;
133
134 if (p)
135 len = p - option;
136 else
137 len = strlen(option);
138
139 if (len > 0) {
140 if (strncmp(option, "warn", len) == 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700141 flags |= VK_DEBUG_REPORT_WARN_BIT_EXT;
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600142 } else if (strncmp(option, "info", len) == 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700143 flags |= VK_DEBUG_REPORT_INFO_BIT_EXT;
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600144 } else if (strncmp(option, "perf", len) == 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700145 flags |= VK_DEBUG_REPORT_PERF_WARN_BIT_EXT;
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600146 } else if (strncmp(option, "error", len) == 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700147 flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600148 } else if (strncmp(option, "debug", len) == 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700149 flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600150 }
151 }
152
153 if (!p)
154 break;
155
156 option = p + 1;
157 }
158 return flags;
159}
160
Jon Ashburna8aa8372015-03-03 15:07:15 -0700161bool getLayerOptionEnum(const char *_option, uint32_t *optionDefault)
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -0600162{
Jon Ashburna8aa8372015-03-03 15:07:15 -0700163 bool res;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -0600164 const char *option = (g_configFileObj.getOption(_option));
165 if (option != NULL) {
Jon Ashburna8aa8372015-03-03 15:07:15 -0700166 *optionDefault = convertStringEnumVal(option);
167 res = false;
Mark Lobodzinski0d6e66d2015-02-25 15:14:06 -0600168 } else {
Jon Ashburna8aa8372015-03-03 15:07:15 -0700169 res = true;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -0600170 }
Jon Ashburna8aa8372015-03-03 15:07:15 -0700171 return res;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -0600172}
173
Jon Ashburndec60512015-01-13 17:24:01 -0700174void setLayerOptionEnum(const char *_option, const char *_valEnum)
175{
176 unsigned int val = convertStringEnumVal(_valEnum);
177 char strVal[24];
178 snprintf(strVal, 24, "%u", val);
179 g_configFileObj.setOption(_option, strVal);
180}
181
182void setLayerOption(const char *_option, const char *_val)
183{
184 g_configFileObj.setOption(_option, _val);
185}
186
Jon Ashburnbe582642014-12-22 12:04:40 -0700187ConfigFile::ConfigFile() : m_fileIsParsed(false)
188{
189}
190
191ConfigFile::~ConfigFile()
192{
193}
194
195const char *ConfigFile::getOption(const std::string &_option)
196{
197 std::map<std::string, std::string>::const_iterator it;
198 if (!m_fileIsParsed)
199 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600200 parseFile("vk_layer_settings.txt");
Jon Ashburnbe582642014-12-22 12:04:40 -0700201 }
202
203 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
204 return NULL;
205 else
206 return it->second.c_str();
207}
208
Jon Ashburndec60512015-01-13 17:24:01 -0700209void ConfigFile::setOption(const std::string &_option, const std::string &_val)
210{
211 if (!m_fileIsParsed)
212 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600213 parseFile("vk_layer_settings.txt");
Jon Ashburndec60512015-01-13 17:24:01 -0700214 }
215
216 m_valueMap[_option] = _val;
217}
218
Jon Ashburnbe582642014-12-22 12:04:40 -0700219void ConfigFile::parseFile(const char *filename)
220{
221 std::ifstream file;
222 char buf[MAX_CHARS_PER_LINE];
223
224 m_fileIsParsed = true;
225 m_valueMap.clear();
226
227 file.open(filename);
228 if (!file.good())
229 return;
230
231 // read tokens from the file and form option, value pairs
232 file.getline(buf, MAX_CHARS_PER_LINE);
233 while (!file.eof())
234 {
235 char option[512];
236 char value[512];
237
238 char *pComment;
239
240 //discard any comments delimited by '#' in the line
241 pComment = strchr(buf, '#');
242 if (pComment)
243 *pComment = '\0';
244
245 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
246 {
247 std::string optStr(option);
248 std::string valStr(value);
249 m_valueMap[optStr] = valStr;
250 }
251 file.getline(buf, MAX_CHARS_PER_LINE);
252 }
253}
254
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600255void print_msg_flags(VkFlags msgFlags, char *msg_flags)
256{
257 bool separator = false;
258
259 msg_flags[0] = 0;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700260 if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600261 strcat(msg_flags, "DEBUG");
262 separator = true;
263 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700264 if (msgFlags & VK_DEBUG_REPORT_INFO_BIT_EXT) {
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600265 if (separator) strcat(msg_flags, ",");
266 strcat(msg_flags, "INFO");
267 separator = true;
268 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700269 if (msgFlags & VK_DEBUG_REPORT_WARN_BIT_EXT) {
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600270 if (separator) strcat(msg_flags, ",");
271 strcat(msg_flags, "WARN");
272 separator = true;
273 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700274 if (msgFlags & VK_DEBUG_REPORT_PERF_WARN_BIT_EXT) {
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600275 if (separator) strcat(msg_flags, ",");
276 strcat(msg_flags, "PERF");
277 separator = true;
278 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700279 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600280 if (separator) strcat(msg_flags, ",");
281 strcat(msg_flags, "ERROR");
282 }
283}
284