blob: 81ddd5d4f0ac4c05cc2e9a5809eb11deb93807ff [file] [log] [blame]
Jon Ashburnbe582642014-12-22 12:04:40 -07001/**************************************************************************
2 *
3 * Copyright 2014 Lunarg, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25#include <fstream>
26#include <string>
27#include <map>
28#include <string.h>
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060029#include <vkLayer.h>
Ian Elliottf15eb8a2015-02-12 11:36:03 -070030#include "loader_platform.h"
Jon Ashburnbe582642014-12-22 12:04:40 -070031#include "layers_config.h"
Ian Elliott655cad72015-02-12 17:08:34 -070032// The following is #included again to catch certain OS-specific functions
33// being used:
34#include "loader_platform.h"
Jon Ashburnbe582642014-12-22 12:04:40 -070035
36#define MAX_CHARS_PER_LINE 4096
37
38class ConfigFile
39{
40public:
41 ConfigFile();
42 ~ConfigFile();
43
44 const char *getOption(const std::string &_option);
Jon Ashburndec60512015-01-13 17:24:01 -070045 void setOption(const std::string &_option, const std::string &_val);
46
Jon Ashburnbe582642014-12-22 12:04:40 -070047private:
48 bool m_fileIsParsed;
49 std::map<std::string, std::string> m_valueMap;
50
51 void parseFile(const char *filename);
52};
53
54static ConfigFile g_configFileObj;
Jon Ashburndec60512015-01-13 17:24:01 -070055
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060056static VkLayerDbgAction stringToDbgAction(const char *_enum)
Jon Ashburndec60512015-01-13 17:24:01 -070057{
58 // only handles single enum values
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060059 if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_IGNORE"))
60 return VK_DBG_LAYER_ACTION_IGNORE;
61 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_CALLBACK"))
62 return VK_DBG_LAYER_ACTION_CALLBACK;
63 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_LOG_MSG"))
64 return VK_DBG_LAYER_ACTION_LOG_MSG;
65 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_BREAK"))
66 return VK_DBG_LAYER_ACTION_BREAK;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060067 return (VkLayerDbgAction) 0;
Jon Ashburndec60512015-01-13 17:24:01 -070068}
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060069
70static VkFlags stringToDbgReportFlags(const char *_enum)
71{
72 // only handles single enum values
73 if (!strcmp(_enum, "VK_DBG_REPORT_INFO"))
74 return VK_DBG_REPORT_INFO_BIT;
75 else if (!strcmp(_enum, "VK_DBG_REPORT_WARN"))
76 return VK_DBG_REPORT_WARN_BIT;
77 else if (!strcmp(_enum, "VK_DBG_REPORT_PERF_WARN"))
78 return VK_DBG_REPORT_PERF_WARN_BIT;
79 else if (!strcmp(_enum, "VK_DBG_REPORT_ERROR"))
80 return VK_DBG_REPORT_ERROR_BIT;
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -060081 else if (!strcmp(_enum, "VK_DBG_REPORT_DEBUG"))
82 return VK_DBG_REPORT_DEBUG_BIT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060083 return (VkFlags) 0;
84}
85
86static unsigned int convertStringEnumVal(const char *_enum)
87{
88 unsigned int ret;
89
90 ret = stringToDbgAction(_enum);
91 if (ret)
92 return ret;
93
94 return stringToDbgReportFlags(_enum);
95}
96
Jon Ashburnbe582642014-12-22 12:04:40 -070097const char *getLayerOption(const char *_option)
98{
Jon Ashburnbe582642014-12-22 12:04:40 -070099 return g_configFileObj.getOption(_option);
100}
101
Courtney Goeltzenleuchterb874b8c2015-06-14 11:34:49 -0600102uint32_t getLayerOptionFlags(const char *_option, uint32_t optionDefault)
103{
104 uint32_t flags = optionDefault;
105 const char *option = (g_configFileObj.getOption(_option));
106
107 /* parse comma-separated options */
108 while (option) {
109 const char *p = strchr(option, ',');
110 size_t len;
111
112 if (p)
113 len = p - option;
114 else
115 len = strlen(option);
116
117 if (len > 0) {
118 if (strncmp(option, "warn", len) == 0) {
119 flags |= VK_DBG_REPORT_WARN_BIT;
120 } else if (strncmp(option, "info", len) == 0) {
121 flags |= VK_DBG_REPORT_INFO_BIT;
122 } else if (strncmp(option, "perf", len) == 0) {
123 flags |= VK_DBG_REPORT_PERF_WARN_BIT;
124 } else if (strncmp(option, "error", len) == 0) {
125 flags |= VK_DBG_REPORT_ERROR_BIT;
126 } else if (strncmp(option, "debug", len) == 0) {
127 flags |= VK_DBG_REPORT_DEBUG_BIT;
128 }
129 }
130
131 if (!p)
132 break;
133
134 option = p + 1;
135 }
136 return flags;
137}
138
Jon Ashburna8aa8372015-03-03 15:07:15 -0700139bool getLayerOptionEnum(const char *_option, uint32_t *optionDefault)
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -0600140{
Jon Ashburna8aa8372015-03-03 15:07:15 -0700141 bool res;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -0600142 const char *option = (g_configFileObj.getOption(_option));
143 if (option != NULL) {
Jon Ashburna8aa8372015-03-03 15:07:15 -0700144 *optionDefault = convertStringEnumVal(option);
145 res = false;
Mark Lobodzinski0d6e66d2015-02-25 15:14:06 -0600146 } else {
Jon Ashburna8aa8372015-03-03 15:07:15 -0700147 res = true;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -0600148 }
Jon Ashburna8aa8372015-03-03 15:07:15 -0700149 return res;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -0600150}
151
Jon Ashburndec60512015-01-13 17:24:01 -0700152void setLayerOptionEnum(const char *_option, const char *_valEnum)
153{
154 unsigned int val = convertStringEnumVal(_valEnum);
155 char strVal[24];
156 snprintf(strVal, 24, "%u", val);
157 g_configFileObj.setOption(_option, strVal);
158}
159
160void setLayerOption(const char *_option, const char *_val)
161{
162 g_configFileObj.setOption(_option, _val);
163}
164
Jon Ashburnbe582642014-12-22 12:04:40 -0700165ConfigFile::ConfigFile() : m_fileIsParsed(false)
166{
167}
168
169ConfigFile::~ConfigFile()
170{
171}
172
173const char *ConfigFile::getOption(const std::string &_option)
174{
175 std::map<std::string, std::string>::const_iterator it;
176 if (!m_fileIsParsed)
177 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600178 parseFile("vk_layer_settings.txt");
Jon Ashburnbe582642014-12-22 12:04:40 -0700179 }
180
181 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
182 return NULL;
183 else
184 return it->second.c_str();
185}
186
Jon Ashburndec60512015-01-13 17:24:01 -0700187void ConfigFile::setOption(const std::string &_option, const std::string &_val)
188{
189 if (!m_fileIsParsed)
190 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600191 parseFile("vk_layer_settings.txt");
Jon Ashburndec60512015-01-13 17:24:01 -0700192 }
193
194 m_valueMap[_option] = _val;
195}
196
Jon Ashburnbe582642014-12-22 12:04:40 -0700197void ConfigFile::parseFile(const char *filename)
198{
199 std::ifstream file;
200 char buf[MAX_CHARS_PER_LINE];
201
202 m_fileIsParsed = true;
203 m_valueMap.clear();
204
205 file.open(filename);
206 if (!file.good())
207 return;
208
209 // read tokens from the file and form option, value pairs
210 file.getline(buf, MAX_CHARS_PER_LINE);
211 while (!file.eof())
212 {
213 char option[512];
214 char value[512];
215
216 char *pComment;
217
218 //discard any comments delimited by '#' in the line
219 pComment = strchr(buf, '#');
220 if (pComment)
221 *pComment = '\0';
222
223 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
224 {
225 std::string optStr(option);
226 std::string valStr(value);
227 m_valueMap[optStr] = valStr;
228 }
229 file.getline(buf, MAX_CHARS_PER_LINE);
230 }
231}
232
Courtney Goeltzenleuchterf85e3612015-06-14 11:33:06 -0600233void print_msg_flags(VkFlags msgFlags, char *msg_flags)
234{
235 bool separator = false;
236
237 msg_flags[0] = 0;
238 if (msgFlags & VK_DBG_REPORT_DEBUG_BIT) {
239 strcat(msg_flags, "DEBUG");
240 separator = true;
241 }
242 if (msgFlags & VK_DBG_REPORT_INFO_BIT) {
243 if (separator) strcat(msg_flags, ",");
244 strcat(msg_flags, "INFO");
245 separator = true;
246 }
247 if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
248 if (separator) strcat(msg_flags, ",");
249 strcat(msg_flags, "WARN");
250 separator = true;
251 }
252 if (msgFlags & VK_DBG_REPORT_PERF_WARN_BIT) {
253 if (separator) strcat(msg_flags, ",");
254 strcat(msg_flags, "PERF");
255 separator = true;
256 }
257 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
258 if (separator) strcat(msg_flags, ",");
259 strcat(msg_flags, "ERROR");
260 }
261}
262