blob: 1d8bde27f15ebacaaf5395060aed98fc6a4693e5 [file] [log] [blame]
Jon Ashburn47e92892014-12-22 12:04:40 -07001/**************************************************************************
2 *
3 * Copyright 2014 Lunarg, Inc.
Michael Lentine92689442015-09-09 12:39:13 -07004 * Copyright 2015 Google Inc.
Jon Ashburn47e92892014-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 *
25 **************************************************************************/
26#include <fstream>
27#include <string>
28#include <map>
29#include <string.h>
Tobin Ehlis2d1d9702015-07-03 09:42:57 -060030#include <vk_layer.h>
Tobin Ehlisb4b6e7c2015-09-15 09:55:54 -060031#include <iostream>
Tobin Ehlis56d204a2015-07-03 10:15:26 -060032#include "vk_layer_config.h"
Courtney Goeltzenleuchterc3f37422015-09-24 11:51:05 -060033#include "vk_sdk_platform.h"
Jon Ashburn47e92892014-12-22 12:04:40 -070034
35#define MAX_CHARS_PER_LINE 4096
36
37class ConfigFile
38{
39public:
40 ConfigFile();
41 ~ConfigFile();
42
43 const char *getOption(const std::string &_option);
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070044 void setOption(const std::string &_option, const std::string &_val);
45
Jon Ashburn47e92892014-12-22 12:04:40 -070046private:
47 bool m_fileIsParsed;
48 std::map<std::string, std::string> m_valueMap;
49
50 void parseFile(const char *filename);
51};
52
53static ConfigFile g_configFileObj;
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070054
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060055static VkLayerDbgAction stringToDbgAction(const char *_enum)
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070056{
57 // only handles single enum values
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060058 if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_IGNORE"))
59 return VK_DBG_LAYER_ACTION_IGNORE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060060 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_LOG_MSG"))
61 return VK_DBG_LAYER_ACTION_LOG_MSG;
Courtney Goeltzenleuchterb94f0512015-10-05 14:41:34 -060062#ifdef WIN32
63 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_DEBUG_OUTPUT"))
64 return VK_DBG_LAYER_ACTION_DEBUG_OUTPUT;
65#endif
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060066 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_BREAK"))
67 return VK_DBG_LAYER_ACTION_BREAK;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060068 return (VkLayerDbgAction) 0;
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070069}
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060070
71static 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 Goeltzenleuchter35b4da92015-06-14 11:34:49 -060082 else if (!strcmp(_enum, "VK_DBG_REPORT_DEBUG"))
83 return VK_DBG_REPORT_DEBUG_BIT;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060084 return (VkFlags) 0;
85}
86
87static 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 Ashburn47e92892014-12-22 12:04:40 -070098const char *getLayerOption(const char *_option)
99{
Jon Ashburn47e92892014-12-22 12:04:40 -0700100 return g_configFileObj.getOption(_option);
101}
102
Tobin Ehlisb4b6e7c2015-09-15 09:55:54 -0600103// If option is NULL or stdout, return stdout, otherwise try to open option
104// as a filename. If successful, return file handle, otherwise stdout
105FILE* 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 Northrop866bb9c2015-09-16 08:35:29 -0600118 return log_output;
Tobin Ehlisb4b6e7c2015-09-15 09:55:54 -0600119}
120
Courtney Goeltzenleuchter35b4da92015-06-14 11:34:49 -0600121uint32_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 Ashburne4722392015-03-03 15:07:15 -0700158bool getLayerOptionEnum(const char *_option, uint32_t *optionDefault)
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600159{
Jon Ashburne4722392015-03-03 15:07:15 -0700160 bool res;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600161 const char *option = (g_configFileObj.getOption(_option));
162 if (option != NULL) {
Jon Ashburne4722392015-03-03 15:07:15 -0700163 *optionDefault = convertStringEnumVal(option);
164 res = false;
Mark Lobodzinski1ee9f3a2015-02-25 15:14:06 -0600165 } else {
Jon Ashburne4722392015-03-03 15:07:15 -0700166 res = true;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600167 }
Jon Ashburne4722392015-03-03 15:07:15 -0700168 return res;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600169}
170
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700171void 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
179void setLayerOption(const char *_option, const char *_val)
180{
181 g_configFileObj.setOption(_option, _val);
182}
183
Jon Ashburn47e92892014-12-22 12:04:40 -0700184ConfigFile::ConfigFile() : m_fileIsParsed(false)
185{
186}
187
188ConfigFile::~ConfigFile()
189{
190}
191
192const char *ConfigFile::getOption(const std::string &_option)
193{
194 std::map<std::string, std::string>::const_iterator it;
195 if (!m_fileIsParsed)
196 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600197 parseFile("vk_layer_settings.txt");
Jon Ashburn47e92892014-12-22 12:04:40 -0700198 }
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 Ashburn0f1dbf12015-01-13 17:24:01 -0700206void ConfigFile::setOption(const std::string &_option, const std::string &_val)
207{
208 if (!m_fileIsParsed)
209 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600210 parseFile("vk_layer_settings.txt");
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700211 }
212
213 m_valueMap[_option] = _val;
214}
215
Jon Ashburn47e92892014-12-22 12:04:40 -0700216void 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 Goeltzenleuchter065843d2015-06-14 11:33:06 -0600252void 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