[Mesa-dev] [PATCH 12/13] swr/rast: split gen_knobs template into .cpp and .h files
Tim Rowley
timothy.o.rowley at intel.com
Mon Jul 31 19:40:10 UTC 2017
---
src/gallium/drivers/swr/Makefile.am | 3 +-
src/gallium/drivers/swr/SConscript | 4 +-
.../drivers/swr/rasterizer/codegen/gen_knobs.py | 14 +-
.../swr/rasterizer/codegen/templates/gen_knobs.cpp | 112 +---------------
.../swr/rasterizer/codegen/templates/gen_knobs.h | 147 +++++++++++++++++++++
.../drivers/swr/rasterizer/core/knobs_init.h | 12 +-
6 files changed, 166 insertions(+), 126 deletions(-)
create mode 100644 src/gallium/drivers/swr/rasterizer/codegen/templates/gen_knobs.h
diff --git a/src/gallium/drivers/swr/Makefile.am b/src/gallium/drivers/swr/Makefile.am
index 73fe904..b20f128 100644
--- a/src/gallium/drivers/swr/Makefile.am
+++ b/src/gallium/drivers/swr/Makefile.am
@@ -115,7 +115,7 @@ rasterizer/codegen/gen_knobs.cpp: rasterizer/codegen/gen_knobs.py rasterizer/cod
--output rasterizer/codegen/gen_knobs.cpp \
--gen_cpp
-rasterizer/codegen/gen_knobs.h: rasterizer/codegen/gen_knobs.py rasterizer/codegen/knob_defs.py rasterizer/codegen/templates/gen_knobs.cpp rasterizer/codegen/gen_common.py
+rasterizer/codegen/gen_knobs.h: rasterizer/codegen/gen_knobs.py rasterizer/codegen/knob_defs.py rasterizer/codegen/templates/gen_knobs.h rasterizer/codegen/gen_common.py
$(MKDIR_GEN)
$(PYTHON_GEN) \
$(srcdir)/rasterizer/codegen/gen_knobs.py \
@@ -347,5 +347,6 @@ EXTRA_DIST = \
rasterizer/codegen/templates/gen_builder.hpp \
rasterizer/codegen/templates/gen_header_init.hpp \
rasterizer/codegen/templates/gen_knobs.cpp \
+ rasterizer/codegen/templates/gen_knobs.h \
rasterizer/codegen/templates/gen_llvm.hpp \
rasterizer/codegen/templates/gen_rasterizer.cpp
diff --git a/src/gallium/drivers/swr/SConscript b/src/gallium/drivers/swr/SConscript
index a32807d..b394cbc 100644
--- a/src/gallium/drivers/swr/SConscript
+++ b/src/gallium/drivers/swr/SConscript
@@ -53,8 +53,8 @@ env.CodeGenerate(
source = '',
command = python_cmd + ' $SCRIPT --output $TARGET --gen_h'
)
-Depends('rasterizer/codegen/gen_knobs.cpp',
- swrroot + 'rasterizer/codegen/templates/gen_knobs.cpp')
+Depends('rasterizer/codegen/gen_knobs.h',
+ swrroot + 'rasterizer/codegen/templates/gen_knobs.h')
env.CodeGenerate(
target = 'rasterizer/jitter/gen_state_llvm.h',
diff --git a/src/gallium/drivers/swr/rasterizer/codegen/gen_knobs.py b/src/gallium/drivers/swr/rasterizer/codegen/gen_knobs.py
index 2c271c7..33f62a2 100644
--- a/src/gallium/drivers/swr/rasterizer/codegen/gen_knobs.py
+++ b/src/gallium/drivers/swr/rasterizer/codegen/gen_knobs.py
@@ -37,27 +37,25 @@ def main(args=sys.argv[1:]):
args = parser.parse_args()
cur_dir = os.path.dirname(os.path.abspath(__file__))
- template_file = os.path.join(cur_dir, 'templates', 'gen_knobs.cpp')
+ template_cpp = os.path.join(cur_dir, 'templates', 'gen_knobs.cpp')
+ template_h = os.path.join(cur_dir, 'templates', 'gen_knobs.h')
if args.gen_h:
MakoTemplateWriter.to_file(
- template_file,
+ template_h,
args.output,
cmdline=sys.argv,
filename='gen_knobs',
- knobs=knob_defs.KNOBS,
- includes=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'],
- gen_header=True)
+ knobs=knob_defs.KNOBS)
if args.gen_cpp:
MakoTemplateWriter.to_file(
- template_file,
+ template_cpp,
args.output,
cmdline=sys.argv,
filename='gen_knobs',
knobs=knob_defs.KNOBS,
- includes=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'],
- gen_header=False)
+ includes=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'])
return 0
diff --git a/src/gallium/drivers/swr/rasterizer/codegen/templates/gen_knobs.cpp b/src/gallium/drivers/swr/rasterizer/codegen/templates/gen_knobs.cpp
index 06b93bd..01cb801 100644
--- a/src/gallium/drivers/swr/rasterizer/codegen/templates/gen_knobs.cpp
+++ b/src/gallium/drivers/swr/rasterizer/codegen/templates/gen_knobs.cpp
@@ -15,11 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
-% if gen_header:
-* @file ${filename}.h
-% else:
* @file ${filename}.cpp
-% endif
*
* @brief Dynamic Knobs for Core.
*
@@ -30,105 +26,6 @@
*
******************************************************************************/
<% calc_max_knob_len(knobs) %>
-%if gen_header:
-#pragma once
-#include <string>
-
-struct KnobBase
-{
-private:
- // Update the input string.
- static void autoExpandEnvironmentVariables(std::string &text);
-
-protected:
- // Leave input alone and return new string.
- static std::string expandEnvironmentVariables(std::string const &input)
- {
- std::string text = input;
- autoExpandEnvironmentVariables(text);
- return text;
- }
-
- template <typename T>
- static T expandEnvironmentVariables(T const &input)
- {
- return input;
- }
-};
-
-template <typename T>
-struct Knob : KnobBase
-{
-public:
- const T& Value() const { return m_Value; }
- const T& Value(T const &newValue)
- {
- m_Value = expandEnvironmentVariables(newValue);
- return Value();
- }
-
-protected:
- Knob(T const &defaultValue) :
- m_Value(expandEnvironmentVariables(defaultValue))
- {
- }
-
-private:
- T m_Value;
-};
-
-#define DEFINE_KNOB(_name, _type, _default) \\
-
- struct Knob_##_name : Knob<_type> \\
-
- { \\
-
- Knob_##_name() : Knob<_type>(_default) { } \\
-
- static const char* Name() { return "KNOB_" #_name; } \\
-
- } _name;
-
-#define GET_KNOB(_name) g_GlobalKnobs._name.Value()
-#define SET_KNOB(_name, _newValue) g_GlobalKnobs._name.Value(_newValue)
-
-struct GlobalKnobs
-{
- % for knob in knobs:
- //-----------------------------------------------------------
- // KNOB_${knob[0]}
- //
- % for line in knob[1]['desc']:
- // ${line}
- % endfor
- % if knob[1].get('choices'):
- <%
- choices = knob[1].get('choices')
- _max_len = calc_max_name_len(choices) %>//
- % for i in range(len(choices)):
- // ${choices[i]['name']}${space_name(choices[i]['name'], _max_len)} = ${format(choices[i]['value'], '#010x')}
- % endfor
- % endif
- //
- % if knob[1]['type'] == 'std::string':
- DEFINE_KNOB(${knob[0]}, ${knob[1]['type']}, "${repr(knob[1]['default'])[1:-1]}");
- % else:
- DEFINE_KNOB(${knob[0]}, ${knob[1]['type']}, ${knob[1]['default']});
- % endif
-
- % endfor
- GlobalKnobs();
- std::string ToString(const char* optPerLinePrefix="");
-};
-extern GlobalKnobs g_GlobalKnobs;
-
-#undef DEFINE_KNOB
-
-% for knob in knobs:
-#define KNOB_${knob[0]}${space_knob(knob[0])} GET_KNOB(${knob[0]})
-% endfor
-
-% else:
% for inc in includes:
#include <${inc}>
% endfor
@@ -198,8 +95,8 @@ GlobalKnobs g_GlobalKnobs;
//========================================================
GlobalKnobs::GlobalKnobs()
{
- % for knob in knobs:
- InitKnob(${knob[0]});
+ % for knob in knobs :
+ InitKnob(${ knob[0] });
% endfor
}
@@ -228,9 +125,6 @@ std::string GlobalKnobs::ToString(const char* optPerLinePrefix)
return str.str();
}
-
-% endif
-
<%!
# Globally available python
max_len = 0
@@ -257,6 +151,4 @@ std::string GlobalKnobs::ToString(const char* optPerLinePrefix)
def space_name(name, max_len):
name_len = len(name)
return ' '*(max_len - name_len)
-
-
%>
diff --git a/src/gallium/drivers/swr/rasterizer/codegen/templates/gen_knobs.h b/src/gallium/drivers/swr/rasterizer/codegen/templates/gen_knobs.h
new file mode 100644
index 0000000..fbb5295
--- /dev/null
+++ b/src/gallium/drivers/swr/rasterizer/codegen/templates/gen_knobs.h
@@ -0,0 +1,147 @@
+/******************************************************************************
+*
+* Copyright 2015-2017
+* Intel Corporation
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http ://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+* @file ${filename}.h
+*
+* @brief Dynamic Knobs for Core.
+*
+* ======================= AUTO GENERATED: DO NOT EDIT !!! ====================
+*
+* Generation Command Line:
+* ${'\n* '.join(cmdline)}
+*
+******************************************************************************/
+<% calc_max_knob_len(knobs) %>
+#pragma once
+#include <string>
+
+struct KnobBase
+{
+private:
+ // Update the input string.
+ static void autoExpandEnvironmentVariables(std::string &text);
+
+protected:
+ // Leave input alone and return new string.
+ static std::string expandEnvironmentVariables(std::string const &input)
+ {
+ std::string text = input;
+ autoExpandEnvironmentVariables(text);
+ return text;
+ }
+
+ template <typename T>
+ static T expandEnvironmentVariables(T const &input)
+ {
+ return input;
+ }
+};
+
+template <typename T>
+struct Knob : KnobBase
+{
+public:
+ const T& Value() const { return m_Value; }
+ const T& Value(T const &newValue)
+ {
+ m_Value = expandEnvironmentVariables(newValue);
+ return Value();
+ }
+
+private:
+ T m_Value;
+};
+
+#define DEFINE_KNOB(_name, _type, _default) \\
+
+ struct Knob_##_name : Knob<_type> \\
+
+ { \\
+
+ static const char* Name() { return "KNOB_" #_name; } \\
+
+ static _type DefaultValue() { return (_default); } \\
+
+ } _name;
+
+#define GET_KNOB(_name) g_GlobalKnobs._name.Value()
+#define SET_KNOB(_name, _newValue) g_GlobalKnobs._name.Value(_newValue)
+
+struct GlobalKnobs
+{
+ % for knob in knobs:
+ //-----------------------------------------------------------
+ // KNOB_${knob[0]}
+ //
+ % for line in knob[1]['desc']:
+ // ${line}
+ % endfor
+ % if knob[1].get('choices'):
+ <%
+ choices = knob[1].get('choices')
+ _max_len = calc_max_name_len(choices) %>//
+ % for i in range(len(choices)):
+ // ${choices[i]['name']}${space_name(choices[i]['name'], _max_len)} = ${format(choices[i]['value'], '#010x')}
+ % endfor
+ % endif
+ //
+ % if knob[1]['type'] == 'std::string':
+ DEFINE_KNOB(${knob[0]}, ${knob[1]['type']}, "${repr(knob[1]['default'])[1:-1]}");
+ % else:
+ DEFINE_KNOB(${knob[0]}, ${knob[1]['type']}, ${knob[1]['default']});
+ % endif
+
+ % endfor
+
+ std::string ToString(const char* optPerLinePrefix="");
+ GlobalKnobs();
+};
+extern GlobalKnobs g_GlobalKnobs;
+
+#undef DEFINE_KNOB
+
+% for knob in knobs:
+#define KNOB_${knob[0]}${space_knob(knob[0])} GET_KNOB(${knob[0]})
+% endfor
+
+<%!
+ # Globally available python
+ max_len = 0
+ def calc_max_knob_len(knobs):
+ global max_len
+ max_len = 0
+ for knob in knobs:
+ if len(knob[0]) > max_len: max_len = len(knob[0])
+ max_len += len('KNOB_ ')
+ if max_len % 4: max_len += 4 - (max_len % 4)
+
+ def space_knob(knob):
+ knob_len = len('KNOB_' + knob)
+ return ' '*(max_len - knob_len)
+
+ def calc_max_name_len(choices_array):
+ _max_len = 0
+ for choice in choices_array:
+ if len(choice['name']) > _max_len: _max_len = len(choice['name'])
+
+ if _max_len % 4: _max_len += 4 - (_max_len % 4)
+ return _max_len
+
+ def space_name(name, max_len):
+ name_len = len(name)
+ return ' '*(max_len - name_len)
+%>
diff --git a/src/gallium/drivers/swr/rasterizer/core/knobs_init.h b/src/gallium/drivers/swr/rasterizer/core/knobs_init.h
index ba2df22..12c2a30 100644
--- a/src/gallium/drivers/swr/rasterizer/core/knobs_init.h
+++ b/src/gallium/drivers/swr/rasterizer/core/knobs_init.h
@@ -91,16 +91,18 @@ static inline void ConvertEnvToKnob(const char* pOverride, std::string& knobValu
template <typename T>
static inline void InitKnob(T& knob)
{
-
- // TODO, read registry first
-
- // Second, read environment variables
+ // Read environment variables
const char* pOverride = getenv(knob.Name());
if (pOverride)
{
- auto knobValue = knob.Value();
+ auto knobValue = knob.DefaultValue();
ConvertEnvToKnob(pOverride, knobValue);
knob.Value(knobValue);
}
+ else
+ {
+ // Set default value
+ knob.Value(knob.DefaultValue());
+ }
}
--
2.7.4
More information about the mesa-dev
mailing list