[Piglit] [PATCH 1/2] generators/modules: Add GLSL types representations

Dylan Baker dylan at pnwbakers.com
Fri Jun 10 23:42:26 UTC 2016


This adds a new module with glsl type representations. The immediate
plan it to use these in the gen_vs_in_fp64 generator to reduce runtime
by removing the need for the cols and rows functions, which are
expensive.

These may be applicable to other generators, but those will have to
wait.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---

 generated_tests/modules/types.py   | 199 +++++++++++++++++++++++++++++++++++++
 unittests/generators/test_types.py |  79 +++++++++++++++
 2 files changed, 278 insertions(+)
 create mode 100644 generated_tests/modules/types.py
 create mode 100644 unittests/generators/test_types.py

diff --git a/generated_tests/modules/types.py b/generated_tests/modules/types.py
new file mode 100644
index 0000000..96f50d5
--- /dev/null
+++ b/generated_tests/modules/types.py
@@ -0,0 +1,199 @@
+# encoding=utf-8
+# Copyright © 2016 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 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.
+
+"""Classes describing various GLSL types.
+
+These classes do not contain values themselves, rather they describe the GLSL
+types.
+
+Included are representations of Matrices, Vectors, and Scalars as containers,
+and Ints, Uints, Floats, and Doubles as types. This can easily be expanded to
+include more types, and the corresponding containers.
+
+Although the classes that implement these representations are not marked as
+private, most consumers will be happier using the provided constants than using
+the classes themselves. Probably the only real use for the types is to do type
+checking.
+
+"""
+
+from __future__ import (
+    absolute_import, division, print_function, unicode_literals
+)
+
+import six
+
+from .utils import lazy_property
+
+
+class Container(object):
+    """A Base class for GLSL container.
+
+    This class defines most of the attributes needed by scalars, matrixes and
+    vectors, but returns None for them. This allows all three classes to be
+    used interchangably since they have the same API.
+
+    It provides the following attributes:
+    name    -- The string formatted name of the container
+    scalar  -- True if the type is scalar
+    vector  -- True if the type is vector
+    matrix  -- True if the type is matrix
+    type    -- A Type object that is the type contained by the container
+    rows    -- The number of rows the container has. Scalars return None
+    columns -- The number of rows container has. Scalars and Vectors return None
+
+    """
+    def __init__(self, name, is_scalar=False, is_vector=False, is_matrix=False,
+                 rows=None, columns=None, contains=None):
+        assert all(isinstance(x, bool)
+                   for x in [is_scalar, is_vector, is_matrix]), \
+            'is_scalar, is_vector, and is_matrix must be bools'
+        assert [is_scalar, is_vector, is_matrix].count(True) == 1, \
+            'A type must be exactly one of: scalar, vector, matrix'
+        assert isinstance(contains, Type), 'contains must be a type instance'
+        assert isinstance(name, six.text_type), 'name must be string'
+        assert columns is None or isinstance(columns, int), \
+            'columns must be an int if provided'
+        assert rows is None or isinstance(rows, int), \
+            'row must be an int if provided'
+
+        self.name = name
+        self.scalar = is_scalar
+        self.vector = is_vector
+        self.matrix = is_matrix
+        self.type = contains
+        self.rows = rows
+        self.columns = columns
+
+
+class Scalar(Container):
+    """A base class for Scalar types."""
+    def __init__(self, name, contains):
+        super(Scalar, self).__init__(name, is_scalar=True, contains=contains)
+
+
+class Vector(Container):
+    """A base class for Vector Containers."""
+    def __init__(self, name, rows, contains):
+        assert isinstance(rows, int), 'rows must be an integer'
+        assert rows in [2, 3, 4], 'vecs can only be 2-4 in GLSL'
+
+        super(Vector, self).__init__(name, is_vector=True, rows=rows,
+                                     contains=contains)
+
+
+class Matrix(Container):
+    """A base class for vectory Containers."""
+    def __init__(self, name, columns, rows, contains):
+        assert isinstance(rows, int), 'rows must be an integer'
+        assert isinstance(columns, int), 'columns must be an integer'
+        assert rows in [2, 3, 4], 'Matrix rows can only be 2-4 in GLSL'
+        assert columns in [2, 3, 4], 'Matrix columns can only be 2-4 in GLSL'
+
+        super(Matrix, self).__init__(name, is_matrix=True, rows=rows,
+                                     columns=columns, contains=contains)
+
+    @lazy_property
+    def square(self):
+        return self.columns == self.rows
+
+
+class Type(object):
+    """Class representing a GLSL type.
+
+    Provides the following interfaces currently:
+    integer -- True if the type is an integer type
+    signed  -- True if the type is a signed type
+    float   -- True if the type is a floating point type
+    size    -- The integer size of the type (8, 16, 32, 64, 128, etc)
+    name    -- A formated string name of the type. (float, double, int64, ...)
+
+    """
+    def __init__(self, name, integer=False, signed=False, floating=False,
+                 size=None):
+        assert [integer, floating].count(True) == 1, \
+            'Type can onnly be float or int'
+        assert integer or (float and signed), 'Floats cannot be unsigned'
+        assert isinstance(size, int), 'size must be an int'
+
+        self.name = name
+        self.integer = integer
+        self.signed = signed
+        self.float = floating
+        self.size = size
+
+
+# pylint: disable=bad-whitespace
+# Type definitions, these are used internally by the Scalar/Vector/Matrix
+# constructs
+INT_TYPE    = Type('int',    integer=True,  signed=True,  size=32)
+UINT_TYPE   = Type('uint',   integer=True,  signed=False, size=32)
+FLOAT_TYPE  = Type('float',  floating=True, signed=True,  size=32)
+DOUBLE_TYPE = Type('double', floating=True, signed=True,  size=64)
+
+# Container definitions
+INT     = Scalar('int',    INT_TYPE)
+UINT    = Scalar('uint',   UINT_TYPE)
+FLOAT   = Scalar('float',  FLOAT_TYPE)
+DOUBLE  = Scalar('double', DOUBLE_TYPE)
+
+VEC2    = Vector('vec2',  2, FLOAT_TYPE)
+VEC3    = Vector('vec3',  3, FLOAT_TYPE)
+VEC4    = Vector('vec4',  4, FLOAT_TYPE)
+
+IVEC2   = Vector('ivec2', 2, INT_TYPE)
+IVEC3   = Vector('ivec3', 3, INT_TYPE)
+IVEC4   = Vector('ivec4', 4, INT_TYPE)
+
+UVEC2   = Vector('uvec2', 2, UINT_TYPE)
+UVEC3   = Vector('uvec3', 3, UINT_TYPE)
+UVEC4   = Vector('uvec4', 4, UINT_TYPE)
+
+DVEC2   = Vector('dvec2', 2, DOUBLE_TYPE)
+DVEC3   = Vector('dvec3', 3, DOUBLE_TYPE)
+DVEC4   = Vector('dvec4', 4, DOUBLE_TYPE)
+
+MAT2    = Matrix('mat2',    2, 2, FLOAT_TYPE)
+MAT3    = Matrix('mat3',    3, 3, FLOAT_TYPE)
+MAT4    = Matrix('mat4',    4, 4, FLOAT_TYPE)
+MAT2X2  = Matrix('mat2x2',  2, 2, FLOAT_TYPE)
+MAT2X3  = Matrix('mat2x3',  2, 3, FLOAT_TYPE)
+MAT2X4  = Matrix('mat2x4',  2, 4, FLOAT_TYPE)
+MAT3X2  = Matrix('mat3x2',  3, 2, FLOAT_TYPE)
+MAT3X3  = Matrix('mat3x3',  3, 3, FLOAT_TYPE)
+MAT3X4  = Matrix('mat3x4',  3, 4, FLOAT_TYPE)
+MAT4X2  = Matrix('mat4x2',  4, 2, FLOAT_TYPE)
+MAT4X3  = Matrix('mat4x3',  4, 3, FLOAT_TYPE)
+MAT4X4  = Matrix('mat4x4',  4, 4, FLOAT_TYPE)
+
+DMAT2   = Matrix('dmat2',   2, 2, DOUBLE_TYPE)
+DMAT3   = Matrix('dmat3',   3, 3, DOUBLE_TYPE)
+DMAT4   = Matrix('dmat4',   4, 4, DOUBLE_TYPE)
+DMAT2X2 = Matrix('dmat2x2', 2, 2, DOUBLE_TYPE)
+DMAT2X3 = Matrix('dmat2x3', 2, 3, DOUBLE_TYPE)
+DMAT2X4 = Matrix('dmat2x4', 2, 4, DOUBLE_TYPE)
+DMAT3X2 = Matrix('dmat3x2', 3, 2, DOUBLE_TYPE)
+DMAT3X3 = Matrix('dmat3x3', 3, 3, DOUBLE_TYPE)
+DMAT3X4 = Matrix('dmat3x4', 3, 4, DOUBLE_TYPE)
+DMAT4X2 = Matrix('dmat4x2', 4, 2, DOUBLE_TYPE)
+DMAT4X3 = Matrix('dmat4x3', 4, 3, DOUBLE_TYPE)
+DMAT4X4 = Matrix('dmat4x4', 4, 4, DOUBLE_TYPE)
+# pylint: enable=bad-whitespace
diff --git a/unittests/generators/test_types.py b/unittests/generators/test_types.py
new file mode 100644
index 0000000..f60282c
--- /dev/null
+++ b/unittests/generators/test_types.py
@@ -0,0 +1,79 @@
+# encoding=utf-8
+# Copyright © 2016 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 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.
+
+"""Tests from generated_tests/modules/types.py"""
+
+from __future__ import (
+    absolute_import, division, print_function, unicode_literals
+)
+import itertools
+import os
+import sys
+
+import nose.tools as nt
+
+from .. import utils
+
+# Add <piglit root>/generated_tests to the module path, this allows it to be
+# imported for testing.
+sys.path.insert(0, os.path.abspath(
+    os.path.join(os.path.dirname(__file__), '..', '..', 'generated_tests')))
+
+# pylint can't figure out the sys.path manipulation.
+from modules import types  # pylint: disable=import-error
+
+
+ at utils.nose.Skip(not __debug__, 'Test requires debug asserts')
+def test_container_is_type_assert():
+    """modules.types.Container: Only accept one of is_scalar or is_vector or is_matrix"""
+    for s, v, m in itertools.product([True, False], repeat=3):
+        # Don't test the valid case
+        if [s, v, m].count(True) == 0:
+            continue
+
+        with nt.assert_raises(AssertionError):
+            types.Container('foo', is_scalar=s, is_vector=v, is_matrix=m,
+                            contains=types.FLOAT)
+
+
+def test_matrix_is_square():
+    """modules.types.Matrix.square: works for square matricies"""
+    for mat in [types.MAT2, types.DMAT3X3]:
+        nt.eq_(mat.square, True)
+
+
+def test_matrix_is_not_square():
+    """modules.types.Matrix.square: works for non-square matricies"""
+    nt.eq_(types.MAT2X4.square, False)
+
+
+ at utils.nose.Skip(not __debug__, 'Test requires debug asserts')
+ at nt.raises(AssertionError)
+def test_type_int_float_assert():
+    """modules.types.Type: only integer or floating can be passed."""
+    types.Type('foo', integer=True, floating=True, size=32)
+
+
+ at utils.nose.Skip(not __debug__, 'Test requires debug asserts')
+ at nt.raises(AssertionError)
+def test_type_float_signed_assert():
+    """modules.types.Type: floating types must be signed."""
+    types.Type('foo', floating=True, signed=False, size=32)
-- 
2.8.3



More information about the Piglit mailing list