[Piglit] [PATCH 12/21] interpolation_qualifier...: combine loops

Dylan Baker baker.dylan.c at gmail.com
Mon Nov 24 13:17:31 PST 2014


This uses a single loop (generated by itertools.product), and individual
generators functions to produce tests. The obvious advantage is that we
loop over the same data a fewer number of times, the other advantage is
a clearer division of code by splitting a function with several hundred
lines of code into multiple functions.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 .../interpolation-qualifier-built-in-variable.py   | 207 +++++++++++----------
 1 file changed, 108 insertions(+), 99 deletions(-)

diff --git a/generated_tests/interpolation-qualifier-built-in-variable.py b/generated_tests/interpolation-qualifier-built-in-variable.py
index efb7171..feec755 100644
--- a/generated_tests/interpolation-qualifier-built-in-variable.py
+++ b/generated_tests/interpolation-qualifier-built-in-variable.py
@@ -23,6 +23,7 @@
 
 from __future__ import print_function
 import os
+import itertools
 
 from templates import template_dir
 
@@ -62,6 +63,107 @@ vertex_shader_to_fragment_shader_variable_map = {
 }
 
 
+def make_fs_vs_tests(fs_mode, vs_mode, dirname):
+    if vs_mode == fs_mode:
+        return
+
+    for var in vertex_shader_variables:
+        filename = os.path.join(
+            dirname,
+            '{0}-{1}-{2}-{3}.shader_test'.format(
+                vs_mode, var, fs_mode,
+                vertex_shader_to_fragment_shader_variable_map[var]))
+        print(filename)
+
+        with open(filename, 'w') as f:
+            f.write(TEMPLATES.get_template('vs-fs.shader_test.mako').render(
+                vs_mode=vs_mode,
+                vs_variable=var,
+                fs_mode=fs_mode,
+                fs_variable=vertex_shader_to_fragment_shader_variable_map[var]))
+
+
+def make_vs_unused_tests(vs_mode, dirname):
+    if vs_mode == 'default':
+        return
+
+    for var in vertex_shader_variables:
+        filename = os.path.join(
+            dirname,
+            '{0}-{1}-unused-{2}.shader_test'.format(vs_mode, var,
+                vertex_shader_to_fragment_shader_variable_map[var]))
+        print(filename)
+
+        with open(filename, 'w') as f:
+            f.write(
+                TEMPLATES.get_template('vs-unused.shader_test.mako').render(
+                    vs_mode=vs_mode,
+                    vs_variable=var))
+
+
+def make_fs_unused_tests(fs_mode, vs_mode, dirname):
+    if fs_mode == 'default':
+        return
+
+    for var in vertex_shader_variables_front_only:
+        filename = os.path.join(
+            dirname,
+            'unused-{0}-{1}-{2}.shader_test'.format(
+                var, fs_mode,
+                vertex_shader_to_fragment_shader_variable_map[var]))
+        print(filename)
+
+        with open(filename, 'w') as f:
+            f.write(TEMPLATES.get_template('fs-unused.shader_test.mako').render(
+                vs_mode=vs_mode,
+                vs_variable=var,
+                fs_mode=fs_mode,
+                fs_variable=vertex_shader_to_fragment_shader_variable_map[var]))
+
+
+def make_vs_fs_unused_tests(fs_mode, vs_mode, dirname):
+    if vs_mode == fs_mode:
+        return
+
+    for var in vertex_shader_variables:
+        filename = os.path.join(
+            dirname,
+            'unused-{0}-{1}-unused-{2}-{3}.shader_test'.format(
+                vs_mode, var, fs_mode,
+                vertex_shader_to_fragment_shader_variable_map[var]))
+        print(filename)
+
+        with open(filename, 'w') as f:
+            f.write(TEMPLATES.get_template(
+                'fs-vs-unused.shader_test.mako').render(
+                    vs_mode=vs_mode,
+                    vs_variable=var,
+                    fs_mode=fs_mode,
+                    fs_variable=vertex_shader_to_fragment_shader_variable_map[var]))
+
+
+def make_vs_fs_flip_tests(fs_mode, vs_mode, dirname):
+    if vs_mode == fs_mode:
+        return
+
+    for this_side in vertex_shader_variables:
+        other_side = other_side_map[this_side]
+        filename = os.path.join(
+            dirname,
+            '{0}-{1}-{2}-{3}.shader_test'.format(
+                vs_mode, this_side, fs_mode, other_side))
+        print(filename)
+
+        with open(filename, 'w') as f:
+            f.write(TEMPLATES.get_template(
+                'vs-fs-flip.shader_test.mako').render(
+                    vs_mode=vs_mode,
+                    this_side_variable=this_side,
+                    other_side_variable=other_side,
+                    fs_mode=fs_mode,
+                    fs_variable=vertex_shader_to_fragment_shader_variable_map[this_side]))
+
+
 def main():
     """main function."""
     dirname = os.path.join('spec', 'glsl-1.30', 'linker',
@@ -70,105 +172,12 @@ def main():
     if not os.path.exists(dirname):
         os.makedirs(dirname)
 
-    for fs_mode in interpolation_modes:
-        for vs_mode in interpolation_modes:
-            if vs_mode == fs_mode:
-                continue
-
-            for var in vertex_shader_variables:
-                filename = os.path.join(
-                    dirname,
-                    '{0}-{1}-{2}-{3}.shader_test'.format(
-                        vs_mode, var, fs_mode,
-                        vertex_shader_to_fragment_shader_variable_map[var]))
-                print(filename)
-
-                with open(filename, 'w') as f:
-                    f.write(TEMPLATES.get_template('vs-fs.shader_test.mako').render(
-                        vs_mode=vs_mode,
-                        vs_variable=var,
-                        fs_mode=fs_mode,
-                        fs_variable=vertex_shader_to_fragment_shader_variable_map[var]))
-
-
-    for vs_mode in interpolation_modes:
-        if vs_mode == 'default':
-            continue
-
-        for var in vertex_shader_variables:
-            filename = os.path.join(
-                dirname,
-                '{0}-{1}-unused-{2}.shader_test'.format(vs_mode, var,
-                    vertex_shader_to_fragment_shader_variable_map[var]))
-            print(filename)
-
-            with open(filename, 'w') as f:
-                f.write(
-                    TEMPLATES.get_template('vs-unused.shader_test.mako').render(
-                        vs_mode=vs_mode,
-                        vs_variable=var))
-
-    for fs_mode in interpolation_modes:
-        if fs_mode == 'default':
-            continue
-
-        for var in vertex_shader_variables_front_only:
-            filename = os.path.join(
-                dirname,
-                'unused-{0}-{1}-{2}.shader_test'.format(
-                    var, fs_mode,
-                    vertex_shader_to_fragment_shader_variable_map[var]))
-            print(filename)
-
-            with open(filename, 'w') as f:
-                f.write(TEMPLATES.get_template('fs-unused.shader_test.mako').render(
-                    vs_mode=vs_mode,
-                    vs_variable=var,
-                    fs_mode=fs_mode,
-                    fs_variable=vertex_shader_to_fragment_shader_variable_map[var]))
-
-    for fs_mode in interpolation_modes:
-        for vs_mode in interpolation_modes:
-            if vs_mode == fs_mode:
-                continue
-
-            for var in vertex_shader_variables:
-                filename = os.path.join(
-                    dirname,
-                    'unused-{0}-{1}-unused-{2}-{3}.shader_test'.format(
-                        vs_mode, var, fs_mode,
-                        vertex_shader_to_fragment_shader_variable_map[var]))
-                print(filename)
-
-                with open(filename, 'w') as f:
-                    f.write(TEMPLATES.get_template(
-                        'fs-vs-unused.shader_test.mako').render(
-                            vs_mode=vs_mode,
-                            vs_variable=var,
-                            fs_mode=fs_mode,
-                            fs_variable=vertex_shader_to_fragment_shader_variable_map[var]))
-
-    for fs_mode in interpolation_modes:
-        for vs_mode in interpolation_modes:
-            if vs_mode == fs_mode:
-                continue
-
-            for this_side in vertex_shader_variables:
-                other_side = other_side_map[this_side]
-                filename = os.path.join(
-                    dirname,
-                    '{0}-{1}-{2}-{3}.shader_test'.format(
-                        vs_mode, this_side, fs_mode, other_side))
-                print(filename)
-
-                with open(filename, 'w') as f:
-                    f.write(TEMPLATES.get_template(
-                        'vs-fs-flip.shader_test.mako').render(
-                            vs_mode=vs_mode,
-                            this_side_variable=this_side,
-                            other_side_variable=other_side,
-                            fs_mode=fs_mode,
-                            fs_variable=vertex_shader_to_fragment_shader_variable_map[this_side]))
+    for fs_mode, vs_mode in itertools.product(interpolation_modes, repeat=2):
+        make_fs_vs_tests(fs_mode, vs_mode, dirname)
+        make_vs_unused_tests(vs_mode, dirname)
+        make_fs_unused_tests(fs_mode, vs_mode, dirname)
+        make_vs_fs_unused_tests(fs_mode, vs_mode, dirname)
+        make_vs_fs_flip_tests(fs_mode, vs_mode, dirname)
 
 
 if __name__ == '__main__':
-- 
2.1.3



More information about the Piglit mailing list