blob: 66254c7416652e479e40d1010b5577b0dce33347 [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;
62 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_BREAK"))
63 return VK_DBG_LAYER_ACTION_BREAK;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060064 return (VkLayerDbgAction) 0;
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070065}
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060066
67static VkFlags stringToDbgReportFlags(const char *_enum)
68{
69 // only handles single enum values
70 if (!strcmp(_enum, "VK_DBG_REPORT_INFO"))
71 return VK_DBG_REPORT_INFO_BIT;
72 else if (!strcmp(_enum, "VK_DBG_REPORT_WARN"))
73 return VK_DBG_REPORT_WARN_BIT;
74 else if (!strcmp(_enum, "VK_DBG_REPORT_PERF_WARN"))
75 return VK_DBG_REPORT_PERF_WARN_BIT;
76 else if (!strcmp(_enum, "VK_DBG_REPORT_ERROR"))
77 return VK_DBG_REPORT_ERROR_BIT;
Courtney Goeltzenleuchter35b4da92015-06-14 11:34:49 -060078 else if (!strcmp(_enum, "VK_DBG_REPORT_DEBUG"))
79 return VK_DBG_REPORT_DEBUG_BIT;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060080 return (VkFlags) 0;
81}
82
83static unsigned int convertStringEnumVal(const char *_enum)
84{
85 unsigned int ret;
86
87 ret = stringToDbgAction(_enum);
88 if (ret)
89 return ret;
90
91 return stringToDbgReportFlags(_enum);
92}
93
Jon Ashburn47e92892014-12-22 12:04:40 -070094const char *getLayerOption(const char *_option)
95{
Jon Ashburn47e92892014-12-22 12:04:40 -070096 return g_configFileObj.getOption(_option);
97}
98
Tobin Ehlisb4b6e7c2015-09-15 09:55:54 -060099// If option is NULL or stdout, return stdout, otherwise try to open option
100// as a filename. If successful, return file handle, otherwise stdout
101FILE* getLayerLogOutput(const char *_option, const char *layerName)
102{
103 FILE* log_output = NULL;
104 if (!_option || !strcmp("stdout", _option))
105 log_output = stdout;
106 else {
107 log_output = fopen(_option, "w");
108 if (log_output == NULL) {
109 if (_option)
110 std::cout << std::endl << layerName << " ERROR: Bad output filename specified: " << _option << ". Writing to STDOUT instead" << std::endl << std::endl;
111 log_output = stdout;
112 }
113 }
Cody Northrop866bb9c2015-09-16 08:35:29 -0600114 return log_output;
Tobin Ehlisb4b6e7c2015-09-15 09:55:54 -0600115}
116
Courtney Goeltzenleuchter35b4da92015-06-14 11:34:49 -0600117uint32_t getLayerOptionFlags(const char *_option, uint32_t optionDefault)
118{
119 uint32_t flags = optionDefault;
120 const char *option = (g_configFileObj.getOption(_option));
121
122 /* parse comma-separated options */
123 while (option) {
124 const char *p = strchr(option, ',');
125 size_t len;
126
127 if (p)
128 len = p - option;
129 else
130 len = strlen(option);
131
132 if (len > 0) {
133 if (strncmp(option, "warn", len) == 0) {
134 flags |= VK_DBG_REPORT_WARN_BIT;
135 } else if (strncmp(option, "info", len) == 0) {
136 flags |= VK_DBG_REPORT_INFO_BIT;
137 } else if (strncmp(option, "perf", len) == 0) {
138 flags |= VK_DBG_REPORT_PERF_WARN_BIT;
139 } else if (strncmp(option, "error", len) == 0) {
140 flags |= VK_DBG_REPORT_ERROR_BIT;
141 } else if (strncmp(option, "debug", len) == 0) {
142 flags |= VK_DBG_REPORT_DEBUG_BIT;
143 }
144 }
145
146 if (!p)
147 break;
148
149 option = p + 1;
150 }
151 return flags;
152}
153
Jon Ashburne4722392015-03-03 15:07:15 -0700154bool getLayerOptionEnum(const char *_option, uint32_t *optionDefault)
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600155{
Jon Ashburne4722392015-03-03 15:07:15 -0700156 bool res;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600157 const char *option = (g_configFileObj.getOption(_option));
158 if (option != NULL) {
Jon Ashburne4722392015-03-03 15:07:15 -0700159 *optionDefault = convertStringEnumVal(option);
160 res = false;
Mark Lobodzinski1ee9f3a2015-02-25 15:14:06 -0600161 } else {
Jon Ashburne4722392015-03-03 15:07:15 -0700162 res = true;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600163 }
Jon Ashburne4722392015-03-03 15:07:15 -0700164 return res;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600165}
166
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700167void setLayerOptionEnum(const char *_option, const char *_valEnum)
168{
169 unsigned int val = convertStringEnumVal(_valEnum);
170 char strVal[24];
171 snprintf(strVal, 24, "%u", val);
172 g_configFileObj.setOption(_option, strVal);
173}
174
175void setLayerOption(const char *_option, const char *_val)
176{
177 g_configFileObj.setOption(_option, _val);
178}
179
Jon Ashburn47e92892014-12-22 12:04:40 -0700180ConfigFile::ConfigFile() : m_fileIsParsed(false)
181{
182}
183
184ConfigFile::~ConfigFile()
185{
186}
187
188const char *ConfigFile::getOption(const std::string &_option)
189{
190 std::map<std::string, std::string>::const_iterator it;
191 if (!m_fileIsParsed)
192 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600193 parseFile("vk_layer_settings.txt");
Jon Ashburn47e92892014-12-22 12:04:40 -0700194 }
195
196 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
197 return NULL;
198 else
199 return it->second.c_str();
200}
201
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700202void ConfigFile::setOption(const std::string &_option, const std::string &_val)
203{
204 if (!m_fileIsParsed)
205 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600206 parseFile("vk_layer_settings.txt");
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700207 }
208
209 m_valueMap[_option] = _val;
210}
211
Jon Ashburn47e92892014-12-22 12:04:40 -0700212void ConfigFile::parseFile(const char *filename)
213{
214 std::ifstream file;
215 char buf[MAX_CHARS_PER_LINE];
216
217 m_fileIsParsed = true;
218 m_valueMap.clear();
219
220 file.open(filename);
221 if (!file.good())
222 return;
223
224 // read tokens from the file and form option, value pairs
225 file.getline(buf, MAX_CHARS_PER_LINE);
226 while (!file.eof())
227 {
228 char option[512];
229 char value[512];
230
231 char *pComment;
232
233 //discard any comments delimited by '#' in the line
234 pComment = strchr(buf, '#');
235 if (pComment)
236 *pComment = '\0';
237
238 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
239 {
240 std::string optStr(option);
241 std::string valStr(value);
242 m_valueMap[optStr] = valStr;
243 }
244 file.getline(buf, MAX_CHARS_PER_LINE);
245 }
246}
247
Courtney Goeltzenleuchter065843d2015-06-14 11:33:06 -0600248void print_msg_flags(VkFlags msgFlags, char *msg_flags)
249{
250 bool separator = false;
251
252 msg_flags[0] = 0;
253 if (msgFlags & VK_DBG_REPORT_DEBUG_BIT) {
254 strcat(msg_flags, "DEBUG");
255 separator = true;
256 }
257 if (msgFlags & VK_DBG_REPORT_INFO_BIT) {
258 if (separator) strcat(msg_flags, ",");
259 strcat(msg_flags, "INFO");
260 separator = true;
261 }
262 if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
263 if (separator) strcat(msg_flags, ",");
264 strcat(msg_flags, "WARN");
265 separator = true;
266 }
267 if (msgFlags & VK_DBG_REPORT_PERF_WARN_BIT) {
268 if (separator) strcat(msg_flags, ",");
269 strcat(msg_flags, "PERF");
270 separator = true;
271 }
272 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
273 if (separator) strcat(msg_flags, ",");
274 strcat(msg_flags, "ERROR");
275 }
276}
277