blob: 642e0b49b63bdde490d5e291a0e91112666f7231 [file] [log] [blame]
Jon Ashburn47e92892014-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060029#include <vkLayer.h>
Ian Elliott5ada89b2015-02-12 11:36:03 -070030#include "loader_platform.h"
Jon Ashburn47e92892014-12-22 12:04:40 -070031#include "layers_config.h"
Ian Elliott20f06872015-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 Ashburn47e92892014-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 Ashburn0f1dbf12015-01-13 17:24:01 -070045 void setOption(const std::string &_option, const std::string &_val);
46
Jon Ashburn47e92892014-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 Ashburn0f1dbf12015-01-13 17:24:01 -070055
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060056static VkLayerDbgAction stringToDbgAction(const char *_enum)
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070057{
58 // only handles single enum values
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060059 if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_IGNORE"))
60 return VK_DBG_LAYER_ACTION_IGNORE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060061 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_LOG_MSG"))
62 return VK_DBG_LAYER_ACTION_LOG_MSG;
63 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_BREAK"))
64 return VK_DBG_LAYER_ACTION_BREAK;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060065 return (VkLayerDbgAction) 0;
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070066}
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060067
68static VkFlags stringToDbgReportFlags(const char *_enum)
69{
70 // only handles single enum values
71 if (!strcmp(_enum, "VK_DBG_REPORT_INFO"))
72 return VK_DBG_REPORT_INFO_BIT;
73 else if (!strcmp(_enum, "VK_DBG_REPORT_WARN"))
74 return VK_DBG_REPORT_WARN_BIT;
75 else if (!strcmp(_enum, "VK_DBG_REPORT_PERF_WARN"))
76 return VK_DBG_REPORT_PERF_WARN_BIT;
77 else if (!strcmp(_enum, "VK_DBG_REPORT_ERROR"))
78 return VK_DBG_REPORT_ERROR_BIT;
Courtney Goeltzenleuchter35b4da92015-06-14 11:34:49 -060079 else if (!strcmp(_enum, "VK_DBG_REPORT_DEBUG"))
80 return VK_DBG_REPORT_DEBUG_BIT;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060081 return (VkFlags) 0;
82}
83
84static unsigned int convertStringEnumVal(const char *_enum)
85{
86 unsigned int ret;
87
88 ret = stringToDbgAction(_enum);
89 if (ret)
90 return ret;
91
92 return stringToDbgReportFlags(_enum);
93}
94
Jon Ashburn47e92892014-12-22 12:04:40 -070095const char *getLayerOption(const char *_option)
96{
Jon Ashburn47e92892014-12-22 12:04:40 -070097 return g_configFileObj.getOption(_option);
98}
99
Courtney Goeltzenleuchter35b4da92015-06-14 11:34:49 -0600100uint32_t getLayerOptionFlags(const char *_option, uint32_t optionDefault)
101{
102 uint32_t flags = optionDefault;
103 const char *option = (g_configFileObj.getOption(_option));
104
105 /* parse comma-separated options */
106 while (option) {
107 const char *p = strchr(option, ',');
108 size_t len;
109
110 if (p)
111 len = p - option;
112 else
113 len = strlen(option);
114
115 if (len > 0) {
116 if (strncmp(option, "warn", len) == 0) {
117 flags |= VK_DBG_REPORT_WARN_BIT;
118 } else if (strncmp(option, "info", len) == 0) {
119 flags |= VK_DBG_REPORT_INFO_BIT;
120 } else if (strncmp(option, "perf", len) == 0) {
121 flags |= VK_DBG_REPORT_PERF_WARN_BIT;
122 } else if (strncmp(option, "error", len) == 0) {
123 flags |= VK_DBG_REPORT_ERROR_BIT;
124 } else if (strncmp(option, "debug", len) == 0) {
125 flags |= VK_DBG_REPORT_DEBUG_BIT;
126 }
127 }
128
129 if (!p)
130 break;
131
132 option = p + 1;
133 }
134 return flags;
135}
136
Jon Ashburne4722392015-03-03 15:07:15 -0700137bool getLayerOptionEnum(const char *_option, uint32_t *optionDefault)
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600138{
Jon Ashburne4722392015-03-03 15:07:15 -0700139 bool res;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600140 const char *option = (g_configFileObj.getOption(_option));
141 if (option != NULL) {
Jon Ashburne4722392015-03-03 15:07:15 -0700142 *optionDefault = convertStringEnumVal(option);
143 res = false;
Mark Lobodzinski1ee9f3a2015-02-25 15:14:06 -0600144 } else {
Jon Ashburne4722392015-03-03 15:07:15 -0700145 res = true;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600146 }
Jon Ashburne4722392015-03-03 15:07:15 -0700147 return res;
Mark Lobodzinskic5eaea62015-02-25 12:23:20 -0600148}
149
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700150void setLayerOptionEnum(const char *_option, const char *_valEnum)
151{
152 unsigned int val = convertStringEnumVal(_valEnum);
153 char strVal[24];
154 snprintf(strVal, 24, "%u", val);
155 g_configFileObj.setOption(_option, strVal);
156}
157
158void setLayerOption(const char *_option, const char *_val)
159{
160 g_configFileObj.setOption(_option, _val);
161}
162
Jon Ashburn47e92892014-12-22 12:04:40 -0700163ConfigFile::ConfigFile() : m_fileIsParsed(false)
164{
165}
166
167ConfigFile::~ConfigFile()
168{
169}
170
171const char *ConfigFile::getOption(const std::string &_option)
172{
173 std::map<std::string, std::string>::const_iterator it;
174 if (!m_fileIsParsed)
175 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600176 parseFile("vk_layer_settings.txt");
Jon Ashburn47e92892014-12-22 12:04:40 -0700177 }
178
179 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
180 return NULL;
181 else
182 return it->second.c_str();
183}
184
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700185void ConfigFile::setOption(const std::string &_option, const std::string &_val)
186{
187 if (!m_fileIsParsed)
188 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600189 parseFile("vk_layer_settings.txt");
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700190 }
191
192 m_valueMap[_option] = _val;
193}
194
Jon Ashburn47e92892014-12-22 12:04:40 -0700195void ConfigFile::parseFile(const char *filename)
196{
197 std::ifstream file;
198 char buf[MAX_CHARS_PER_LINE];
199
200 m_fileIsParsed = true;
201 m_valueMap.clear();
202
203 file.open(filename);
204 if (!file.good())
205 return;
206
207 // read tokens from the file and form option, value pairs
208 file.getline(buf, MAX_CHARS_PER_LINE);
209 while (!file.eof())
210 {
211 char option[512];
212 char value[512];
213
214 char *pComment;
215
216 //discard any comments delimited by '#' in the line
217 pComment = strchr(buf, '#');
218 if (pComment)
219 *pComment = '\0';
220
221 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
222 {
223 std::string optStr(option);
224 std::string valStr(value);
225 m_valueMap[optStr] = valStr;
226 }
227 file.getline(buf, MAX_CHARS_PER_LINE);
228 }
229}
230
Courtney Goeltzenleuchter065843d2015-06-14 11:33:06 -0600231void print_msg_flags(VkFlags msgFlags, char *msg_flags)
232{
233 bool separator = false;
234
235 msg_flags[0] = 0;
236 if (msgFlags & VK_DBG_REPORT_DEBUG_BIT) {
237 strcat(msg_flags, "DEBUG");
238 separator = true;
239 }
240 if (msgFlags & VK_DBG_REPORT_INFO_BIT) {
241 if (separator) strcat(msg_flags, ",");
242 strcat(msg_flags, "INFO");
243 separator = true;
244 }
245 if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
246 if (separator) strcat(msg_flags, ",");
247 strcat(msg_flags, "WARN");
248 separator = true;
249 }
250 if (msgFlags & VK_DBG_REPORT_PERF_WARN_BIT) {
251 if (separator) strcat(msg_flags, ",");
252 strcat(msg_flags, "PERF");
253 separator = true;
254 }
255 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
256 if (separator) strcat(msg_flags, ",");
257 strcat(msg_flags, "ERROR");
258 }
259}
260