[Piglit] [RFC 07/12] gen_constant_array generators.py: merge fp64 and non-fp64 generators

Dylan Baker baker.dylan.c at gmail.com
Mon Dec 8 17:11:28 PST 2014


This combines the fp64 and the non-fp64 variants of the
gen_constant_array_size_tests generators into a single generator, using
mako. This removes a lot of LOC, most of which is either a direct copy
of other code, or slightly modified copies of other code.

This results in some very slight changes from the original generator. It
adds extension requirements to the config block for non-fp64 tests
(this was fixed in the fp64 tests, but not in the original tests). This
is a minor change, but a good one. The second change is that the
non-fp64 used function() * function(), while the non-fp64 tests use
pow(function(), 2), after this commit pow is not longer used. This
decision was made because pow() is not supported for doubles per
ARB_gpu_shader_fp64 Section 8.2, which defines only sqrt and inverse sqrt
for doubles.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 generated_tests/CMakeLists.txt                     |   9 +-
 generated_tests/gen_constant_array_size_tests.py   |  40 ++-
 .../gen_constant_array_size_tests_fp64.py          | 274 ---------------------
 3 files changed, 28 insertions(+), 295 deletions(-)
 delete mode 100644 generated_tests/gen_constant_array_size_tests_fp64.py

diff --git a/generated_tests/CMakeLists.txt b/generated_tests/CMakeLists.txt
index 04913ae..f634330 100644
--- a/generated_tests/CMakeLists.txt
+++ b/generated_tests/CMakeLists.txt
@@ -41,6 +41,7 @@ piglit_make_generated_tests(
 	constant_array_size_tests.list
 	gen_constant_array_size_tests.py
 	builtin_function.py
+	builtin_function_fp64.py
 	builtins/glsl_types.py
 	builtins/generators.py
 	builtins/math.py
@@ -117,13 +118,6 @@ piglit_make_generated_tests(
 	builtins/generators.py
 	builtins/math.py)
 piglit_make_generated_tests(
-	constant_array_size_tests_fp64.list
-	gen_constant_array_size_tests_fp64.py
-	builtin_function_fp64.py
-	builtins/glsl_types.py
-	builtins/generators.py
-	builtins/math.py)
-piglit_make_generated_tests(
 	shader_image_load_store_tests.list
 	gen_shader_image_load_store_tests.py)
 
@@ -161,7 +155,6 @@ add_custom_target(gen-gl-tests
 			uniform-initializer_tests.list
 			interpolation-qualifier-built-in-variable.list
 			builtin_uniform_tests_fp64.list
-			constant_array_size_tests_fp64.list
 			shader_image_load_store_tests.list
 )
 
diff --git a/generated_tests/gen_constant_array_size_tests.py b/generated_tests/gen_constant_array_size_tests.py
index 0195ebf..40b19e3 100644
--- a/generated_tests/gen_constant_array_size_tests.py
+++ b/generated_tests/gen_constant_array_size_tests.py
@@ -44,6 +44,7 @@ import os
 from mako.template import Template
 
 import builtin_function
+import builtin_function_fp64
 from builtins import glsl_types, generators
 
 
@@ -51,6 +52,9 @@ TEMPLATE = Template("""\
 /* [config]
  * expect_result: pass
  * glsl_version: ${glsl_version}
+% if extension:
+ * require_extensions: GL_${extension}
+% endif
  * [end config]
  *
  * Check that the following test vectors are constantfolded correctly:
@@ -66,7 +70,7 @@ TEMPLATE = Template("""\
 void main()
 {
 % for i, vector in enumerate(vectors):
-  float[${vector} ? 1 : -1] array${i};
+  ${type_[0]}[${vector} ? 1 : -1] array${i};
 % else:
   ## This is a clever (but maybe not good) use of python's for/else syntax.
   ## vectors is a generator, which means that we cannot get a value again
@@ -75,7 +79,7 @@ void main()
   ## storing the value of i because the else is still part of the for loop,
   ## and all of the variables from the last iteration of the  for loop are
   ## still in scope.
-  ${output_var} = vec4(${' + '.join('array{}.length()'.format(x) for x in xrange(i + 1))});
+  ${output_var} = ${type_[1]}(${' + '.join('array{}.length()'.format(x) for x in xrange(i + 1))});
 % endfor
 }
 """)
@@ -97,7 +101,8 @@ def make_condition(signature, test_vector):
     """
     invocation = signature.template.format(
         *[generators.glsl_constant(x) for x in test_vector.arguments])
-    if signature.rettype.base_type == glsl_types.GLSL_FLOAT:
+    if signature.rettype.base_type in (glsl_types.GLSL_FLOAT,
+                                       glsl_types.GLSL_DOUBLE):
         # Test floating-point values within tolerance
         if signature.name == 'distance':
             # Don't use the distance() function to test itself.
@@ -106,15 +111,15 @@ def make_condition(signature, test_vector):
                 invocation,
                 test_vector.result + test_vector.tolerance)
         elif signature.rettype.is_matrix:
-            # We can't apply distance() to matrices.  So apply it
-            # to each column and root-sum-square the results.  It
-            # is safe to use pow() here because its behavior is
-            # verified in the pow() tests.
+            # pow is safe for floating point data, but not safe for doubles
+            # Accorsing to ARB_gpu_shader_fp64 only sqrt and inversesqrt are
+            # supported for doubles, so use * instead
             terms = []
             for col in xrange(signature.rettype.num_cols):
-                terms.append('pow(distance({0}[{1}], {2}), 2)'.format(
-                    invocation, col,
-                    generators.glsl_constant(test_vector.result[:, col])))
+                terms.append(
+                    '(distance({0}[{1}], {2}) * distance({0}[{1}], {2}))'.format(
+                        invocation, col,
+                        generators.glsl_constant(test_vector.result[:, col])))
             rss_distance = ' + '.join(terms)
             sq_tolerance = test_vector.tolerance * test_vector.tolerance
             return '{0} <= {1}'.format(
@@ -176,15 +181,23 @@ def comment_generator(signature, vectors):
 
 
 def all_tests():
-    for signature, vectors in sorted(builtin_function.test_suite.iteritems()):
+    """Generator that returns all tests."""
+    for signature, vectors in builtin_function.test_suite.iteritems():
         # Assignment operators other than = cannot be used in the constant
         # array size tests
         if not signature.name.startswith('op-assign'):
-            yield signature, vectors
+            yield signature, vectors, ('float', 'vec4')
+    for signature, vectors in builtin_function_fp64.test_suite.iteritems():
+        # Ideally we would loop over these in a chain (using itertools.chain),
+        # however, we need to be able to change some assignment types, and
+        # there seems to be no straightforward way to do that except at this
+        # point.
+        if not signature.name.startswith('op-assign'):
+            yield signature, vectors, ('double', 'dvec4')
 
 
 def main():
-    for signature, vectors in all_tests():
+    for signature, vectors, type_ in all_tests():
         for stage in ['frag', 'vert', 'geom']:
             filename = get_filename(signature, stage)
 
@@ -197,6 +210,7 @@ def main():
                     vectors=(make_condition(signature, v) for v in vectors),
                     comments=comment_generator(signature, vectors),
                     output_var=make_output_var(stage),
+                    type_=type_,
                     extension=signature.extension))
             print(filename)
 
diff --git a/generated_tests/gen_constant_array_size_tests_fp64.py b/generated_tests/gen_constant_array_size_tests_fp64.py
deleted file mode 100644
index 10e93d5..0000000
--- a/generated_tests/gen_constant_array_size_tests_fp64.py
+++ /dev/null
@@ -1,274 +0,0 @@
-# coding=utf-8
-#
-# Copyright © 2011 Intel Corporation
-#
-# Permission is hereby granted, free of charge, to any person obtaining a
-# copy of this software and associated documentation files (the "Software"),
-# to deal in the Software without restriction, including without limitation
-# the rights to use, copy, modify, merge, publish, distribute, sublicense,
-# and/or sell copies of the Software, and to permit persons to whom the
-# Software is furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice (including the next
-# paragraph) shall be included in all copies or substantial portions of the
-# Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
-# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-# DEALINGS IN THE SOFTWARE.
-
-# Generate a pair of glsl parser tests for every overloaded version of
-# every built-in function, which test that the built-in functions are
-# handled properly when applied to constant arguments inside an array
-# size declaration.
-#
-# In each pair of generated tests, one test exercises the built-in
-# function in vertex shaders, and the other exercises it in fragment
-# shaders.
-#
-# This program outputs, to stdout, the name of each file it generates.
-# With the optional argument --names-only, it only outputs the names
-# of the files; it doesn't generate them.
-
-from __future__ import absolute_import
-import abc
-import optparse
-import os
-
-from builtins import glsl_types, generators
-
-import builtin_function_fp64
-
-
-class ParserTest(object):
-    """Class used to build a test of a single built-in.  This is an
-    abstract base class--derived types should override test_suffix(),
-    output_var(), and other functions if necessary.
-    """
-
-    def __init__(self, signature, test_vectors):
-        """Prepare to build a test for a single built-in.  signature
-        is the signature of the built-in (a key from the
-        builtin_function.test_suite dict), and test_vectors is the
-        list of test vectors for testing the given builtin (the
-        corresponding value from the builtin_function.test_suite
-        dict).
-        """
-        self.__add_exten = ""
-        self.__signature = signature
-        self.__test_vectors = test_vectors
-        if self.__signature.extension:
-            self.__add_exten += 'GL_{0}'.format(self.__signature.extension)
-
-    def glsl_version(self):
-        if self.__signature.version_introduced < 120:
-            # Before version 1.20, built-in function invocations
-            # weren't allowed in constant expressions.  So even if
-            # this built-in was introduced prior to 1.20, test it in
-            # version 1.20.
-            return 120
-        else:
-            return self.__signature.version_introduced
-
-    def version_directive(self):
-        return '#version {0}\n'.format(self.glsl_version())
-
-    def additional_declarations(self):
-        """Return a string containing any additional declarations that
-        should be placed after the version directive.  Returns the
-        empty string by default.
-        """
-        return ''
-
-    def additional_extensions(self):
-        """Return a list (or other iterable) containing any additional
-        extension requirements that the test has.  Returns the empty
-        list by default.
-        """
-        return self.__add_exten
-
-    @abc.abstractmethod
-    def test_suffix(self):
-        """Return the suffix that should be used in the test file name
-        to identify the type of shader, e.g. "vert" for a vertex
-        shader test.
-        """
-
-    def make_condition(self, test_vector):
-        """Generate a GLSL constant expression that should evaluate to
-        true if the GLSL compiler's constant evaluation produces the
-        correct result for the given test vector, and false if not.
-        """
-        invocation = self.__signature.template.format(
-            *[generators.glsl_constant(x) for x in test_vector.arguments])
-        if self.__signature.rettype.base_type == glsl_types.GLSL_DOUBLE:
-            # Test floating-point values within tolerance
-            if self.__signature.name == 'distance':
-                # Don't use the distance() function to test itself.
-                return '{0} <= {1} && {1} <= {2}'.format(
-                    test_vector.result - test_vector.tolerance,
-                    invocation,
-                    test_vector.result + test_vector.tolerance)
-            elif self.__signature.rettype.is_matrix:
-                # We can't apply distance() to matrices.  So apply it
-                # to each column and root-sum-square the results.  It
-                # is safe to use pow() here because its behavior is
-                # verified in the pow() tests.
-                terms = []
-                for col in xrange(self.__signature.rettype.num_cols):
-                    terms.append('(distance({0}[{1}], {2}) * distance({0}[{1}], {2}))'.format(
-                        invocation, col,
-                        generators.glsl_constant(test_vector.result[:, col])))
-                rss_distance = ' + '.join(terms)
-                sq_tolerance = test_vector.tolerance * test_vector.tolerance
-                return '{0} <= {1}'.format(
-                    rss_distance, generators.glsl_constant(sq_tolerance))
-            else:
-                return 'distance({0}, {1}) <= {2}'.format(
-                    invocation, generators.glsl_constant(test_vector.result),
-                    generators.glsl_constant(test_vector.tolerance))
-        else:
-            # Test non-floating point values exactly
-            assert not self.__signature.rettype.is_matrix
-            if self.__signature.name == 'equal':
-                # Don't use the equal() function to test itself.
-                assert self.__signature.rettype.is_vector
-                terms = []
-                for row in xrange(self.__signature.rettype.num_rows):
-                    terms.append('{0}[{1}] == {2}'.format(
-                        invocation, row,
-                        generators.glsl_constant(test_vector.result[row])))
-                return ' && '.join(terms)
-            elif self.__signature.rettype.is_vector:
-                return 'all(equal({0}, {1}))'.format(
-                    invocation, generators.glsl_constant(test_vector.result))
-            else:
-                return '{0} == {1}'.format(
-                    invocation, generators.glsl_constant(test_vector.result))
-
-    def make_shader(self):
-        """Generate the shader code necessary to test the built-in."""
-        shader = self.version_directive()
-        if self.__signature.extension:
-            shader += '#extension GL_{0} : require\n'.format(self.__signature.extension)
-        shader += self.additional_declarations()
-        shader += '\n'
-        shader += 'void main()\n'
-        shader += '{\n'
-        lengths = []
-        for i, test_vector in enumerate(self.__test_vectors):
-            shader += '  double[{0} ? 1 : -1] array{1};\n'.format(
-                self.make_condition(test_vector), i)
-            lengths.append('array{0}.length()'.format(i))
-        shader += '  {0} = dvec4({1});\n'.format(
-            self.output_var(), ' + '.join(lengths))
-        shader += '}\n'
-        return shader
-
-    def filename(self):
-        argtype_names = '-'.join(
-            str(argtype) for argtype in self.__signature.argtypes)
-        if self.__signature.extension:
-            subdir = self.__signature.extension.lower()
-        else:
-            subdir = 'glsl-{0:1.2f}'.format(float(self.glsl_version()) / 100)
-        return os.path.join(
-            'spec', subdir, 'compiler', 'built-in-functions',
-            '{0}-{1}.{2}'.format(
-                self.__signature.name, argtype_names, self.test_suffix()))
-
-    def generate_parser_test(self):
-        """Generate the test and write it to the output file."""
-        parser_test = '/* [config]\n'
-        parser_test += ' * expect_result: pass\n'
-        parser_test += ' * glsl_version: {0:1.2f}\n'.format(
-            float(self.glsl_version()) / 100)
-        if self.additional_extensions():
-            parser_test += ' * require_extensions: {0}\n'.format(self.additional_extensions())
-        parser_test += ' * [end config]\n'
-        parser_test += ' *\n'
-        parser_test += ' * Check that the following test vectors are constant'\
-                       'folded correctly:\n'
-        for test_vector in self.__test_vectors:
-            parser_test += ' * {0} => {1}\n'.format(
-                self.__signature.template.format(
-                    *[generators.glsl_constant(arg)
-                      for arg in test_vector.arguments]),
-                generators.glsl_constant(test_vector.result))
-        parser_test += ' */\n'
-        parser_test += self.make_shader()
-        filename = self.filename()
-        dirname = os.path.dirname(filename)
-        if not os.path.exists(dirname):
-            os.makedirs(dirname)
-        with open(filename, 'w') as f:
-            f.write(parser_test)
-
-
-class VertexParserTest(ParserTest):
-    """Derived class for tests that exercise the built-in in a vertex
-    shader.
-    """
-    def test_suffix(self):
-        return 'vert'
-
-    def output_var(self):
-        return 'gl_Position'
-
-
-class GeometryParserTest(ParserTest):
-    """Derived class for tests that exercise the built-in in a geometry
-    shader.
-    """
-    def glsl_version(self):
-	return max(150, ParserTest.glsl_version(self))
-
-    def test_suffix(self):
-        return 'geom'
-
-    def output_var(self):
-        return 'gl_Position'
-
-
-class FragmentParserTest(ParserTest):
-    """Derived class for tests that exercise the built-in in a fagment
-    shader.
-    """
-    def test_suffix(self):
-        return 'frag'
-
-    def output_var(self):
-        return 'gl_FragColor'
-
-
-def all_tests():
-    for signature, vectors in builtin_function_fp64.test_suite.iteritems():
-        yield VertexParserTest(signature, vectors)
-        yield GeometryParserTest(signature, vectors)
-        yield FragmentParserTest(signature, vectors)
-
-
-def main():
-    desc = 'Generate shader tests that test built-in functions using constant'\
-           'array sizes'
-    usage = 'usage: %prog [-h] [--names-only]'
-    parser = optparse.OptionParser(description=desc, usage=usage)
-    parser.add_option('--names-only',
-                      dest='names_only',
-                      action='store_true',
-                      help="Don't output files, just generate a list of"
-                           "filenames to stdout")
-    options, args = parser.parse_args()
-
-    for test in all_tests():
-        if not options.names_only:
-            test.generate_parser_test()
-        print test.filename()
-
-
-if __name__ == '__main__':
-    main()
-- 
2.2.0



More information about the Piglit mailing list