e109fd26597511d4a86cc39f626340c32d315031
[mesa.git] / src / gallium / drivers / swr / rasterizer / codegen / templates / gen_knobs.cpp
1 /******************************************************************************
2 *
3 * Copyright 2015-2017
4 * Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http ://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 % if gen_header:
19 * @file ${filename}.h
20 % else:
21 * @file ${filename}.cpp
22 % endif
23 *
24 * @brief Dynamic Knobs for Core.
25 *
26 * ======================= AUTO GENERATED: DO NOT EDIT !!! ====================
27 *
28 * Generation Command Line:
29 * ${'\n* '.join(cmdline)}
30 *
31 ******************************************************************************/
32 <% calc_max_knob_len(knobs) %>
33 %if gen_header:
34 #pragma once
35 #include <string>
36
37 struct KnobBase
38 {
39 private:
40 // Update the input string.
41 static void autoExpandEnvironmentVariables(std::string &text);
42
43 protected:
44 // Leave input alone and return new string.
45 static std::string expandEnvironmentVariables(std::string const &input)
46 {
47 std::string text = input;
48 autoExpandEnvironmentVariables(text);
49 return text;
50 }
51
52 template <typename T>
53 static T expandEnvironmentVariables(T const &input)
54 {
55 return input;
56 }
57 };
58
59 template <typename T>
60 struct Knob : KnobBase
61 {
62 public:
63 const T& Value() const { return m_Value; }
64 const T& Value(T const &newValue)
65 {
66 m_Value = expandEnvironmentVariables(newValue);
67 return Value();
68 }
69
70 protected:
71 Knob(T const &defaultValue) :
72 m_Value(expandEnvironmentVariables(defaultValue))
73 {
74 }
75
76 private:
77 T m_Value;
78 };
79
80 #define DEFINE_KNOB(_name, _type, _default) \\
81
82 struct Knob_##_name : Knob<_type> \\
83
84 { \\
85
86 Knob_##_name() : Knob<_type>(_default) { } \\
87
88 static const char* Name() { return "KNOB_" #_name; } \\
89
90 } _name;
91
92 #define GET_KNOB(_name) g_GlobalKnobs._name.Value()
93 #define SET_KNOB(_name, _newValue) g_GlobalKnobs._name.Value(_newValue)
94
95 struct GlobalKnobs
96 {
97 % for knob in knobs:
98 //-----------------------------------------------------------
99 // KNOB_${knob[0]}
100 //
101 % for line in knob[1]['desc']:
102 // ${line}
103 % endfor
104 % if knob[1].get('choices'):
105 <%
106 choices = knob[1].get('choices')
107 _max_len = calc_max_name_len(choices) %>//
108 % for i in range(len(choices)):
109 // ${choices[i]['name']}${space_name(choices[i]['name'], _max_len)} = ${format(choices[i]['value'], '#010x')}
110 % endfor
111 % endif
112 //
113 % if knob[1]['type'] == 'std::string':
114 DEFINE_KNOB(${knob[0]}, ${knob[1]['type']}, "${repr(knob[1]['default'])[1:-1]}");
115 % else:
116 DEFINE_KNOB(${knob[0]}, ${knob[1]['type']}, ${knob[1]['default']});
117 % endif
118
119 % endfor
120 GlobalKnobs();
121 std::string ToString(const char* optPerLinePrefix="");
122 };
123 extern GlobalKnobs g_GlobalKnobs;
124
125 #undef DEFINE_KNOB
126
127 % for knob in knobs:
128 #define KNOB_${knob[0]}${space_knob(knob[0])} GET_KNOB(${knob[0]})
129 % endfor
130
131 % else:
132 % for inc in includes:
133 #include <${inc}>
134 % endfor
135 #include <regex>
136 #include <core/utils.h>
137
138 //========================================================
139 // Implementation
140 //========================================================
141 void KnobBase::autoExpandEnvironmentVariables(std::string &text)
142 {
143 {
144 // unix style variable replacement
145 static std::regex env("\\$\\{([^}]+)\\}");
146 std::smatch match;
147 while (std::regex_search(text, match, env))
148 {
149 const std::string var = GetEnv(match[1].str());
150 // certain combinations of gcc/libstd++ have problems with this
151 // text.replace(match[0].first, match[0].second, var);
152 text.replace(match.prefix().length(), match[0].length(), var);
153 }
154 }
155 {
156 // win32 style variable replacement
157 static std::regex env("\\%([^}]+)\\%");
158 std::smatch match;
159 while (std::regex_search(text, match, env))
160 {
161 const std::string var = GetEnv(match[1].str());
162 // certain combinations of gcc/libstd++ have problems with this
163 // text.replace(match[0].first, match[0].second, var);
164 text.replace(match.prefix().length(), match[0].length(), var);
165 }
166 }
167 }
168
169
170 //========================================================
171 // Static Data Members
172 //========================================================
173 GlobalKnobs g_GlobalKnobs;
174
175 //========================================================
176 // Knob Initialization
177 //========================================================
178 GlobalKnobs::GlobalKnobs()
179 {
180 % for knob in knobs:
181 InitKnob(${knob[0]});
182 % endfor
183 }
184
185 //========================================================
186 // Knob Display (Convert to String)
187 //========================================================
188 std::string GlobalKnobs::ToString(const char* optPerLinePrefix)
189 {
190 std::basic_stringstream<char> str;
191 str << std::showbase << std::setprecision(1) << std::fixed;
192
193 if (optPerLinePrefix == nullptr) { optPerLinePrefix = ""; }
194
195 % for knob in knobs:
196 str << optPerLinePrefix << "KNOB_${knob[0]}:${space_knob(knob[0])}";
197 % if knob[1]['type'] == 'bool':
198 str << (KNOB_${knob[0]} ? "+\n" : "-\n");
199 % elif knob[1]['type'] != 'float' and knob[1]['type'] != 'std::string':
200 str << std::hex << std::setw(11) << std::left << KNOB_${knob[0]};
201 str << std::dec << KNOB_${knob[0]} << "\n";
202 % else:
203 str << KNOB_${knob[0]} << "\n";
204 % endif
205 % endfor
206 str << std::ends;
207
208 return str.str();
209 }
210
211 % endif
212
213 <%!
214 # Globally available python
215 max_len = 0
216 def calc_max_knob_len(knobs):
217 global max_len
218 max_len = 0
219 for knob in knobs:
220 if len(knob[0]) > max_len: max_len = len(knob[0])
221 max_len += len('KNOB_ ')
222 if max_len % 4: max_len += 4 - (max_len % 4)
223
224 def space_knob(knob):
225 knob_len = len('KNOB_' + knob)
226 return ' '*(max_len - knob_len)
227
228 def calc_max_name_len(choices_array):
229 _max_len = 0
230 for choice in choices_array:
231 if len(choice['name']) > _max_len: _max_len = len(choice['name'])
232
233 if _max_len % 4: _max_len += 4 - (_max_len % 4)
234 return _max_len
235
236 def space_name(name, max_len):
237 name_len = len(name)
238 return ' '*(max_len - name_len)
239
240
241 %>